1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
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 program is a utility that works like binutils "objdump", that is, it
11 // dumps out a plethora of information about an object file depending on the
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Object/ObjectFile.h"
17 // This config must be included before llvm-config.h.
18 #include "llvm/Config/config.h"
19 #include "../../lib/MC/MCDisassembler/EDDisassembler.h"
20 #include "../../lib/MC/MCDisassembler/EDInst.h"
21 #include "../../lib/MC/MCDisassembler/EDOperand.h"
22 #include "../../lib/MC/MCDisassembler/EDToken.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/ADT/Triple.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCDisassembler.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCInstPrinter.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Format.h"
32 #include "llvm/Support/Host.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/MemoryObject.h"
36 #include "llvm/Support/PrettyStackTrace.h"
37 #include "llvm/Support/Signals.h"
38 #include "llvm/Support/SourceMgr.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Support/system_error.h"
41 #include "llvm/Target/TargetMachine.h"
42 #include "llvm/Target/TargetRegistry.h"
43 #include "llvm/Target/TargetSelect.h"
49 using namespace object;
53 InputFilenames(cl::Positional, cl::desc("<input object files>"),
57 Disassemble("disassemble",
58 cl::desc("Display assembler mnemonics for the machine instructions"));
60 Disassembled("d", cl::desc("Alias for --disassemble"),
61 cl::aliasopt(Disassemble));
64 TripleName("triple", cl::desc("Target triple to disassemble for, "
65 "see -version for available targets"));
68 ArchName("arch", cl::desc("Target arch to disassemble for, "
69 "see -version for available targets"));
74 static const Target *GetTarget(const ObjectFile *Obj = NULL) {
75 // Figure out the target triple.
76 llvm::Triple TT("unknown-unknown-unknown");
77 if (TripleName.empty()) {
79 TT.setArch(Triple::ArchType(Obj->getArch()));
81 TT.setTriple(Triple::normalize(TripleName));
83 if (!ArchName.empty())
84 TT.setArchName(ArchName);
86 TripleName = TT.str();
88 // Get the target specific parser.
90 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
94 errs() << ToolName << ": error: unable to get target for '" << TripleName
95 << "', see --version and --triple.\n";
100 class StringRefMemoryObject : public MemoryObject {
104 StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {}
106 uint64_t getBase() const { return 0; }
107 uint64_t getExtent() const { return Bytes.size(); }
109 int readByte(uint64_t Addr, uint8_t *Byte) const {
110 if (Addr > getExtent())
118 static void DumpBytes(StringRef bytes) {
119 static char hex_rep[] = "0123456789abcdef";
120 // FIXME: The real way to do this is to figure out the longest instruction
121 // and align to that size before printing. I'll fix this when I get
122 // around to outputting relocations.
123 // 15 is the longest x86 instruction
124 // 3 is for the hex rep of a byte + a space.
125 // 1 is for the null terminator.
126 enum { OutputSize = (15 * 3) + 1 };
127 char output[OutputSize];
129 assert(bytes.size() <= 15
130 && "DumpBytes only supports instructions of up to 15 bytes");
131 memset(output, ' ', sizeof(output));
133 for (StringRef::iterator i = bytes.begin(),
134 e = bytes.end(); i != e; ++i) {
135 output[index] = hex_rep[(*i & 0xF0) >> 4];
136 output[index + 1] = hex_rep[*i & 0xF];
140 output[sizeof(output) - 1] = 0;
144 static void DisassembleInput(const StringRef &Filename) {
145 OwningPtr<MemoryBuffer> Buff;
147 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
148 errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n";
152 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
154 const Target *TheTarget = GetTarget(Obj.get());
156 // GetTarget prints out stuff.
162 << ":\tfile format " << Obj->getFileFormatName() << "\n\n\n";
164 for (ObjectFile::section_iterator i = Obj->begin_sections(),
165 e = Obj->end_sections();
169 outs() << "Disassembly of section " << i->getName() << ":\n\n";
171 // Set up disassembler.
172 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName));
175 errs() << "error: no assembly info for target " << TripleName << "\n";
179 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler());
181 errs() << "error: no disassembler for target " << TripleName << "\n";
185 // FIXME: We shouldn't need to do this (and link in codegen).
186 // When we split this out, we should do it in a way that makes
187 // it straightforward to switch subtargets on the fly (.e.g,
188 // the .cpu and .code16 directives).
189 std::string FeaturesStr;
190 OwningPtr<TargetMachine> TM(TheTarget->createTargetMachine(TripleName,
193 errs() << "error: could not create target for triple " << TripleName << "\n";
197 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
198 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
199 *TM, AsmPrinterVariant, *AsmInfo));
201 errs() << "error: no instruction printer for target " << TripleName << '\n';
205 StringRef Bytes = i->getContents();
206 StringRefMemoryObject memoryObject(Bytes);
210 for (Index = 0; Index < Bytes.size(); Index += Size) {
214 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
216 raw_ostream &DebugOut = nulls();
219 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) {
220 outs() << format("%8x:\t", i->getAddress() + Index);
221 DumpBytes(StringRef(Bytes.data() + Index, Size));
222 IP->printInst(&Inst, outs());
225 errs() << ToolName << ": warning: invalid instruction encoding\n";
227 Size = 1; // skip illegible bytes
233 int main(int argc, char **argv) {
234 // Print a stack trace if we signal out.
235 sys::PrintStackTraceOnErrorSignal();
236 PrettyStackTraceProgram X(argc, argv);
237 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
239 // Initialize targets and assembly printers/parsers.
240 llvm::InitializeAllTargetInfos();
241 // FIXME: We shouldn't need to initialize the Target(Machine)s.
242 llvm::InitializeAllTargets();
243 llvm::InitializeAllAsmPrinters();
244 llvm::InitializeAllAsmParsers();
245 llvm::InitializeAllDisassemblers();
247 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
248 TripleName = Triple::normalize(TripleName);
252 // Defaults to a.out if no filenames specified.
253 if (InputFilenames.size() == 0)
254 InputFilenames.push_back("a.out");
256 // -d is the only flag that is currently implemented, so just print help if
259 cl::PrintHelpMessage();
263 std::for_each(InputFilenames.begin(), InputFilenames.end(),