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/TargetRegistry.h"
42 #include "llvm/Target/TargetSelect.h"
48 using namespace object;
52 InputFilenames(cl::Positional, cl::desc("<input object files>"),
56 Disassemble("disassemble",
57 cl::desc("Display assembler mnemonics for the machine instructions"));
59 Disassembled("d", cl::desc("Alias for --disassemble"),
60 cl::aliasopt(Disassemble));
63 TripleName("triple", cl::desc("Target triple to disassemble for, "
64 "see -version for available targets"));
67 ArchName("arch", cl::desc("Target arch to disassemble for, "
68 "see -version for available targets"));
72 bool error(error_code ec) {
73 if (!ec) return false;
75 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
81 static const Target *GetTarget(const ObjectFile *Obj = NULL) {
82 // Figure out the target triple.
83 llvm::Triple TT("unknown-unknown-unknown");
84 if (TripleName.empty()) {
86 TT.setArch(Triple::ArchType(Obj->getArch()));
88 TT.setTriple(Triple::normalize(TripleName));
90 if (!ArchName.empty())
91 TT.setArchName(ArchName);
93 TripleName = TT.str();
95 // Get the target specific parser.
97 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
101 errs() << ToolName << ": error: unable to get target for '" << TripleName
102 << "', see --version and --triple.\n";
107 class StringRefMemoryObject : public MemoryObject {
111 StringRefMemoryObject(StringRef bytes) : Bytes(bytes) {}
113 uint64_t getBase() const { return 0; }
114 uint64_t getExtent() const { return Bytes.size(); }
116 int readByte(uint64_t Addr, uint8_t *Byte) const {
117 if (Addr > getExtent())
125 static void DumpBytes(StringRef bytes) {
126 static char hex_rep[] = "0123456789abcdef";
127 // FIXME: The real way to do this is to figure out the longest instruction
128 // and align to that size before printing. I'll fix this when I get
129 // around to outputting relocations.
130 // 15 is the longest x86 instruction
131 // 3 is for the hex rep of a byte + a space.
132 // 1 is for the null terminator.
133 enum { OutputSize = (15 * 3) + 1 };
134 char output[OutputSize];
136 assert(bytes.size() <= 15
137 && "DumpBytes only supports instructions of up to 15 bytes");
138 memset(output, ' ', sizeof(output));
140 for (StringRef::iterator i = bytes.begin(),
141 e = bytes.end(); i != e; ++i) {
142 output[index] = hex_rep[(*i & 0xF0) >> 4];
143 output[index + 1] = hex_rep[*i & 0xF];
147 output[sizeof(output) - 1] = 0;
151 static void DisassembleInput(const StringRef &Filename) {
152 OwningPtr<MemoryBuffer> Buff;
154 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
155 errs() << ToolName << ": " << Filename << ": " << ec.message() << "\n";
159 OwningPtr<ObjectFile> Obj(ObjectFile::createObjectFile(Buff.take()));
161 const Target *TheTarget = GetTarget(Obj.get());
163 // GetTarget prints out stuff.
169 << ":\tfile format " << Obj->getFileFormatName() << "\n\n\n";
172 for (ObjectFile::section_iterator i = Obj->begin_sections(),
173 e = Obj->end_sections();
174 i != e; i.increment(ec)) {
175 if (error(ec)) break;
177 if (error(i->isText(text))) break;
181 if (error(i->getName(name))) break;
182 outs() << "Disassembly of section " << name << ":\n\n";
184 // Set up disassembler.
185 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createAsmInfo(TripleName));
188 errs() << "error: no assembly info for target " << TripleName << "\n";
192 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler());
194 errs() << "error: no disassembler for target " << TripleName << "\n";
198 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
199 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
200 AsmPrinterVariant, *AsmInfo));
202 errs() << "error: no instruction printer for target " << TripleName << '\n';
207 if (error(i->getContents(Bytes))) break;
208 StringRefMemoryObject memoryObject(Bytes);
212 for (Index = 0; Index < Bytes.size(); Index += Size) {
216 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
218 raw_ostream &DebugOut = nulls();
221 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index, DebugOut)) {
223 if (error(i->getAddress(addr))) break;
224 outs() << format("%8x:\t", addr + Index);
225 DumpBytes(StringRef(Bytes.data() + Index, Size));
226 IP->printInst(&Inst, outs());
229 errs() << ToolName << ": warning: invalid instruction encoding\n";
231 Size = 1; // skip illegible bytes
237 int main(int argc, char **argv) {
238 // Print a stack trace if we signal out.
239 sys::PrintStackTraceOnErrorSignal();
240 PrettyStackTraceProgram X(argc, argv);
241 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
243 // Initialize targets and assembly printers/parsers.
244 llvm::InitializeAllTargetInfos();
245 // FIXME: We shouldn't need to initialize the Target(Machine)s.
246 llvm::InitializeAllTargets();
247 llvm::InitializeAllAsmPrinters();
248 llvm::InitializeAllAsmParsers();
249 llvm::InitializeAllDisassemblers();
251 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
252 TripleName = Triple::normalize(TripleName);
256 // Defaults to a.out if no filenames specified.
257 if (InputFilenames.size() == 0)
258 InputFilenames.push_back("a.out");
260 // -d is the only flag that is currently implemented, so just print help if
263 cl::PrintHelpMessage();
267 std::for_each(InputFilenames.begin(), InputFilenames.end(),