1 //===- HexDisassembler.cpp - Disassembler for hex strings -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the disassembler of strings of bytes written in
11 // hexadecimal, from standard input or from a file.
13 //===----------------------------------------------------------------------===//
15 #include "HexDisassembler.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCDisassembler.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCInstPrinter.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/MemoryObject.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Support/SourceMgr.h"
30 class VectorMemoryObject : public MemoryObject {
32 const std::vector<unsigned char> &Bytes;
34 VectorMemoryObject(const std::vector<unsigned char> &bytes) :
38 uint64_t getBase() const {
42 uint64_t getExtent() const {
46 int readByte(uint64_t addr, uint8_t *byte) const {
47 if (addr > getExtent())
54 void printInst(const llvm::MCDisassembler &disassembler,
55 llvm::MCInstPrinter &instPrinter,
56 const std::vector<unsigned char> &bytes) {
57 // Wrap the vector in a MemoryObject.
58 VectorMemoryObject memoryObject(bytes);
64 std::string verboseOStr;
65 llvm::raw_string_ostream verboseOS(verboseOStr);
67 if (disassembler.getInstruction(inst, size, memoryObject, 0, verboseOS)) {
68 instPrinter.printInst(&inst);
71 errs() << "error: invalid instruction" << "\n";
72 errs() << "Diagnostic log:" << "\n";
73 errs() << verboseOStr.c_str() << "\n";
77 int HexDisassembler::disassemble(const Target &T, const std::string &Triple,
78 MemoryBuffer &Buffer) {
79 // Set up disassembler.
80 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
83 errs() << "error: no assembly info for target " << Triple << "\n";
87 llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
89 errs() << "error: no disassembler for target " << Triple << "\n";
93 llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
96 errs() << "error: no instruction printer for target " << Triple
101 SourceMgr SourceManager;
102 SourceManager.AddNewSourceBuffer(&Buffer, SMLoc());
104 // Convert the input to a vector for disassembly.
105 std::vector<unsigned char> ByteArray;
107 StringRef Str = Buffer.getBuffer();
108 while (!Str.empty()) {
109 // Strip horizontal whitespace.
110 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
111 Str = Str.substr(Pos);
115 // If this is the end of a line or start of a comment, process the
116 // instruction we have so far.
117 if (Str[0] == '\n' || Str[0] == '#') {
118 // If we have bytes to process, do so.
119 if (!ByteArray.empty()) {
120 printInst(*DisAsm, *InstPrinter, ByteArray);
124 // Strip to the end of line if we already processed any bytes on this
125 // line. This strips the comment and/or the \n.
129 Str = Str.substr(Str.find_first_of('\n'));
136 // Get the current token.
137 size_t Next = Str.find_first_of(" \t\n\r#");
138 StringRef Value = Str.substr(0, Next);
140 // Convert to a byte and add to the byte vector.
142 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
143 // If we have an error, print it and skip to the end of line.
144 SourceManager.PrintMessage(SMLoc::getFromPointer(Value.data()),
145 "invalid input token", "error");
146 Str = Str.substr(Str.find('\n'));
151 ByteArray.push_back((unsigned char)ByteVal);
152 Str = Str.substr(Next);
155 if (!ByteArray.empty())
156 printInst(*DisAsm, *InstPrinter, ByteArray);