1 //===- Disassembler.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 "Disassembler.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"
29 typedef std::vector<std::pair<unsigned char, const char*> > ByteArrayTy;
32 class VectorMemoryObject : public MemoryObject {
34 const ByteArrayTy &Bytes;
36 VectorMemoryObject(const ByteArrayTy &bytes) : Bytes(bytes) {}
38 uint64_t getBase() const { return 0; }
39 uint64_t getExtent() const { return Bytes.size(); }
41 int readByte(uint64_t Addr, uint8_t *Byte) const {
42 if (Addr > getExtent())
44 *Byte = Bytes[Addr].first;
50 static bool PrintInsts(const llvm::MCDisassembler &DisAsm,
51 llvm::MCInstPrinter &Printer, const ByteArrayTy &Bytes,
53 // Wrap the vector in a MemoryObject.
54 VectorMemoryObject memoryObject(Bytes);
56 // Disassemble it to strings.
60 for (Index = 0; Index < Bytes.size(); Index += Size) {
63 if (DisAsm.getInstruction(Inst, Size, memoryObject, Index,
64 /*REMOVE*/ nulls())) {
65 Printer.printInst(&Inst);
69 SM.PrintMessage(SMLoc::getFromPointer(Bytes[Index].second),
70 "invalid instruction encoding", "warning");
72 Size = 1; // skip illegible bytes
79 int Disassembler::disassemble(const Target &T, const std::string &Triple,
80 MemoryBuffer &Buffer) {
81 // Set up disassembler.
82 llvm::OwningPtr<const llvm::MCAsmInfo> AsmInfo(T.createAsmInfo(Triple));
85 errs() << "error: no assembly info for target " << Triple << "\n";
89 llvm::OwningPtr<const llvm::MCDisassembler> DisAsm(T.createMCDisassembler());
91 errs() << "error: no disassembler for target " << Triple << "\n";
95 llvm::MCInstPrinter *InstPrinter = T.createMCInstPrinter(0, *AsmInfo, outs());
98 errs() << "error: no instruction printer for target " << Triple << '\n';
102 bool ErrorOccurred = false;
105 SM.AddNewSourceBuffer(&Buffer, SMLoc());
107 // Convert the input to a vector for disassembly.
108 ByteArrayTy ByteArray;
110 StringRef Str = Buffer.getBuffer();
111 while (!Str.empty()) {
112 // Strip horizontal whitespace.
113 if (size_t Pos = Str.find_first_not_of(" \t\r")) {
114 Str = Str.substr(Pos);
118 // If this is the end of a line or start of a comment, remove the rest of
120 if (Str[0] == '\n' || Str[0] == '#') {
121 // Strip to the end of line if we already processed any bytes on this
122 // line. This strips the comment and/or the \n.
126 Str = Str.substr(Str.find_first_of('\n'));
133 // Get the current token.
134 size_t Next = Str.find_first_of(" \t\n\r#");
135 StringRef Value = Str.substr(0, Next);
137 // Convert to a byte and add to the byte vector.
139 if (Value.getAsInteger(0, ByteVal) || ByteVal > 255) {
140 // If we have an error, print it and skip to the end of line.
141 SM.PrintMessage(SMLoc::getFromPointer(Value.data()),
142 "invalid input token", "error");
143 ErrorOccurred = true;
144 Str = Str.substr(Str.find('\n'));
149 ByteArray.push_back(std::make_pair((unsigned char)ByteVal, Value.data()));
150 Str = Str.substr(Next);
153 if (!ByteArray.empty())
154 ErrorOccurred |= PrintInsts(*DisAsm, *InstPrinter, ByteArray, SM);
156 return ErrorOccurred;