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-objdump.h"
17 #include "MCFunction.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/COFF.h"
20 #include "llvm/Object/ObjectFile.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Triple.h"
24 #include "llvm/ADT/STLExtras.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/MC/MCRegisterInfo.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Format.h"
36 #include "llvm/Support/GraphWriter.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/MemoryObject.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/Signals.h"
43 #include "llvm/Support/SourceMgr.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/TargetSelect.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/system_error.h"
52 using namespace object;
54 static cl::list<std::string>
55 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
58 Disassemble("disassemble",
59 cl::desc("Display assembler mnemonics for the machine instructions"));
61 Disassembled("d", cl::desc("Alias for --disassemble"),
62 cl::aliasopt(Disassemble));
65 Relocations("r", cl::desc("Display the relocation entries in the file"));
68 SectionContents("s", cl::desc("Display the content of each section"));
71 SymbolTable("t", cl::desc("Display the symbol table"));
74 MachO("macho", cl::desc("Use MachO specific object file parser"));
76 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
79 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
80 "see -version for available targets"));
83 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
84 "see -version for available targets"));
87 SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
88 "for each section."));
90 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
91 cl::aliasopt(SectionHeaders));
93 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
94 cl::aliasopt(SectionHeaders));
96 static StringRef ToolName;
98 static bool error(error_code ec) {
99 if (!ec) return false;
101 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
106 static const Target *GetTarget(const ObjectFile *Obj = NULL) {
107 // Figure out the target triple.
108 llvm::Triple TT("unknown-unknown-unknown");
109 if (TripleName.empty()) {
111 TT.setArch(Triple::ArchType(Obj->getArch()));
113 TT.setTriple(Triple::normalize(TripleName));
115 if (!ArchName.empty())
116 TT.setArchName(ArchName);
118 TripleName = TT.str();
120 // Get the target specific parser.
122 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
126 errs() << ToolName << ": error: unable to get target for '" << TripleName
127 << "', see --version and --triple.\n";
131 void llvm::StringRefMemoryObject::anchor() { }
133 void llvm::DumpBytes(StringRef bytes) {
134 static const char hex_rep[] = "0123456789abcdef";
135 // FIXME: The real way to do this is to figure out the longest instruction
136 // and align to that size before printing. I'll fix this when I get
137 // around to outputting relocations.
138 // 15 is the longest x86 instruction
139 // 3 is for the hex rep of a byte + a space.
140 // 1 is for the null terminator.
141 enum { OutputSize = (15 * 3) + 1 };
142 char output[OutputSize];
144 assert(bytes.size() <= 15
145 && "DumpBytes only supports instructions of up to 15 bytes");
146 memset(output, ' ', sizeof(output));
148 for (StringRef::iterator i = bytes.begin(),
149 e = bytes.end(); i != e; ++i) {
150 output[index] = hex_rep[(*i & 0xF0) >> 4];
151 output[index + 1] = hex_rep[*i & 0xF];
155 output[sizeof(output) - 1] = 0;
159 static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
160 uint64_t a_addr, b_addr;
161 if (error(a.getAddress(a_addr))) return false;
162 if (error(b.getAddress(b_addr))) return false;
163 return a_addr < b_addr;
166 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
167 const Target *TheTarget = GetTarget(Obj);
169 // GetTarget prints out stuff.
174 for (section_iterator i = Obj->begin_sections(),
175 e = Obj->end_sections();
176 i != e; i.increment(ec)) {
177 if (error(ec)) break;
179 if (error(i->isText(text))) break;
182 uint64_t SectionAddr;
183 if (error(i->getAddress(SectionAddr))) break;
185 // Make a list of all the symbols in this section.
186 std::vector<std::pair<uint64_t, StringRef> > Symbols;
187 for (symbol_iterator si = Obj->begin_symbols(),
188 se = Obj->end_symbols();
189 si != se; si.increment(ec)) {
191 if (!error(i->containsSymbol(*si, contains)) && contains) {
193 if (error(si->getAddress(Address))) break;
194 Address -= SectionAddr;
197 if (error(si->getName(Name))) break;
198 Symbols.push_back(std::make_pair(Address, Name));
202 // Sort the symbols by address, just in case they didn't come in that way.
203 array_pod_sort(Symbols.begin(), Symbols.end());
205 // Make a list of all the relocations for this section.
206 std::vector<RelocationRef> Rels;
208 for (relocation_iterator ri = i->begin_relocations(),
209 re = i->end_relocations();
210 ri != re; ri.increment(ec)) {
211 if (error(ec)) break;
216 // Sort relocations by address.
217 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
220 if (error(i->getName(name))) break;
221 outs() << "Disassembly of section " << name << ':';
223 // If the section has no symbols just insert a dummy one and disassemble
224 // the whole section.
226 Symbols.push_back(std::make_pair(0, name));
228 // Set up disassembler.
229 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
232 errs() << "error: no assembly info for target " << TripleName << "\n";
236 OwningPtr<const MCSubtargetInfo> STI(
237 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
240 errs() << "error: no subtarget info for target " << TripleName << "\n";
244 OwningPtr<const MCDisassembler> DisAsm(
245 TheTarget->createMCDisassembler(*STI));
247 errs() << "error: no disassembler for target " << TripleName << "\n";
251 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
253 errs() << "error: no register info for target " << TripleName << "\n";
257 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
258 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
259 AsmPrinterVariant, *AsmInfo, *MRI, *STI));
261 errs() << "error: no instruction printer for target " << TripleName
267 if (error(i->getContents(Bytes))) break;
268 StringRefMemoryObject memoryObject(Bytes);
272 if (error(i->getSize(SectSize))) break;
274 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
275 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
276 // Disassemble symbol by symbol.
277 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
278 uint64_t Start = Symbols[si].first;
280 // The end is either the size of the section or the beginning of the next
284 // Make sure this symbol takes up space.
285 else if (Symbols[si + 1].first != Start)
286 End = Symbols[si + 1].first - 1;
288 // This symbol has the same address as the next symbol. Skip it.
291 outs() << '\n' << Symbols[si].second << ":\n";
294 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
296 raw_ostream &DebugOut = nulls();
299 for (Index = Start; Index < End; Index += Size) {
302 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
303 DebugOut, nulls())) {
304 outs() << format("%8" PRIx64 ":\t", SectionAddr + Index);
305 DumpBytes(StringRef(Bytes.data() + Index, Size));
306 IP->printInst(&Inst, outs(), "");
309 errs() << ToolName << ": warning: invalid instruction encoding\n";
311 Size = 1; // skip illegible bytes
314 // Print relocation for instruction.
315 while (rel_cur != rel_end) {
318 SmallString<16> name;
321 // If this relocation is hidden, skip it.
322 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
323 if (hidden) goto skip_print_rel;
325 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
326 // Stop when rel_cur's address is past the current instruction.
327 if (addr >= Index + Size) break;
328 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
329 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
331 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
332 << "\t" << val << "\n";
342 static void PrintRelocations(const ObjectFile *o) {
344 for (section_iterator si = o->begin_sections(), se = o->end_sections();
345 si != se; si.increment(ec)){
346 if (error(ec)) return;
347 if (si->begin_relocations() == si->end_relocations())
350 if (error(si->getName(secname))) continue;
351 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
352 for (relocation_iterator ri = si->begin_relocations(),
353 re = si->end_relocations();
354 ri != re; ri.increment(ec)) {
355 if (error(ec)) return;
359 SmallString<32> relocname;
360 SmallString<32> valuestr;
361 if (error(ri->getHidden(hidden))) continue;
362 if (hidden) continue;
363 if (error(ri->getTypeName(relocname))) continue;
364 if (error(ri->getAddress(address))) continue;
365 if (error(ri->getValueString(valuestr))) continue;
366 outs() << address << " " << relocname << " " << valuestr << "\n";
372 static void PrintSectionHeaders(const ObjectFile *o) {
373 outs() << "Sections:\n"
374 "Idx Name Size Address Type\n";
377 for (section_iterator si = o->begin_sections(), se = o->end_sections();
378 si != se; si.increment(ec)) {
379 if (error(ec)) return;
381 if (error(si->getName(Name))) return;
383 if (error(si->getAddress(Address))) return;
385 if (error(si->getSize(Size))) return;
386 bool Text, Data, BSS;
387 if (error(si->isText(Text))) return;
388 if (error(si->isData(Data))) return;
389 if (error(si->isBSS(BSS))) return;
390 std::string Type = (std::string(Text ? "TEXT " : "") +
391 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
392 outs() << format("%3d %-13s %09" PRIx64 " %017" PRIx64 " %s\n",
393 i, Name.str().c_str(), Size, Address, Type.c_str());
398 static void PrintSectionContents(const ObjectFile *o) {
400 for (section_iterator si = o->begin_sections(),
401 se = o->end_sections();
402 si != se; si.increment(ec)) {
403 if (error(ec)) return;
407 if (error(si->getName(Name))) continue;
408 if (error(si->getContents(Contents))) continue;
409 if (error(si->getAddress(BaseAddr))) continue;
411 outs() << "Contents of section " << Name << ":\n";
413 // Dump out the content as hex and printable ascii characters.
414 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
415 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
417 for (std::size_t i = 0; i < 16; ++i) {
418 if (i != 0 && i % 4 == 0)
421 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
422 << hexdigit(Contents[addr + i] & 0xF, true);
428 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
429 if (std::isprint(Contents[addr + i] & 0xFF))
430 outs() << Contents[addr + i];
439 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
440 const coff_file_header *header;
441 if (error(coff->getHeader(header))) return;
443 const coff_symbol *symbol = 0;
444 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
446 // Figure out which type of aux this is.
447 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
448 && symbol->Value == 0) { // Section definition.
449 const coff_aux_section_definition *asd;
450 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
453 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
454 , unsigned(asd->Length)
455 , unsigned(asd->NumberOfRelocations)
456 , unsigned(asd->NumberOfLinenumbers)
457 , unsigned(asd->CheckSum))
458 << format("assoc %d comdat %d\n"
459 , unsigned(asd->Number)
460 , unsigned(asd->Selection));
462 outs() << "AUX Unknown\n";
466 if (error(coff->getSymbol(i, symbol))) return;
467 if (error(coff->getSymbolName(symbol, name))) return;
468 outs() << "[" << format("%2d", i) << "]"
469 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
470 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
471 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
472 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
473 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
474 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
476 aux_count = symbol->NumberOfAuxSymbols;
481 static void PrintSymbolTable(const ObjectFile *o) {
482 outs() << "SYMBOL TABLE:\n";
484 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
485 PrintCOFFSymbolTable(coff);
488 for (symbol_iterator si = o->begin_symbols(),
489 se = o->end_symbols(); si != se; si.increment(ec)) {
490 if (error(ec)) return;
493 SymbolRef::Type Type;
496 section_iterator Section = o->end_sections();
497 if (error(si->getName(Name))) continue;
498 if (error(si->getAddress(Address))) continue;
499 if (error(si->getFlags(Flags))) continue;
500 if (error(si->getType(Type))) continue;
501 if (error(si->getSize(Size))) continue;
502 if (error(si->getSection(Section))) continue;
504 bool Global = Flags & SymbolRef::SF_Global;
505 bool Weak = Flags & SymbolRef::SF_Weak;
506 bool Absolute = Flags & SymbolRef::SF_Absolute;
508 if (Address == UnknownAddressOrSize)
510 if (Size == UnknownAddressOrSize)
513 if (Type != SymbolRef::ST_Unknown)
514 GlobLoc = Global ? 'g' : 'l';
515 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
518 if (Type == SymbolRef::ST_File)
520 else if (Type == SymbolRef::ST_Function)
523 outs() << format("%08" PRIx64, Address) << " "
524 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
525 << (Weak ? 'w' : ' ') // Weak?
526 << ' ' // Constructor. Not supported yet.
527 << ' ' // Warning. Not supported yet.
528 << ' ' // Indirect reference to another symbol.
529 << Debug // Debugging (d) or dynamic (D) symbol.
530 << FileFunc // Name of function (F), file (f) or object (O).
534 else if (Section == o->end_sections())
537 StringRef SectionName;
538 if (error(Section->getName(SectionName)))
540 outs() << SectionName;
543 << format("%08" PRIx64 " ", Size)
550 static void DumpObject(const ObjectFile *o) {
552 outs() << o->getFileName()
553 << ":\tfile format " << o->getFileFormatName() << "\n\n";
556 DisassembleObject(o, Relocations);
557 if (Relocations && !Disassemble)
560 PrintSectionHeaders(o);
562 PrintSectionContents(o);
567 /// @brief Dump each object file in \a a;
568 static void DumpArchive(const Archive *a) {
569 for (Archive::child_iterator i = a->begin_children(),
570 e = a->end_children(); i != e; ++i) {
571 OwningPtr<Binary> child;
572 if (error_code ec = i->getAsBinary(child)) {
573 // Ignore non-object files.
574 if (ec != object_error::invalid_file_type)
575 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
579 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
582 errs() << ToolName << ": '" << a->getFileName() << "': "
583 << "Unrecognized file type.\n";
587 /// @brief Open file and figure out how to dump it.
588 static void DumpInput(StringRef file) {
589 // If file isn't stdin, check that it exists.
590 if (file != "-" && !sys::fs::exists(file)) {
591 errs() << ToolName << ": '" << file << "': " << "No such file\n";
595 if (MachO && Disassemble) {
596 DisassembleInputMachO(file);
600 // Attempt to open the binary.
601 OwningPtr<Binary> binary;
602 if (error_code ec = createBinary(file, binary)) {
603 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
607 if (Archive *a = dyn_cast<Archive>(binary.get())) {
609 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
612 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
616 int main(int argc, char **argv) {
617 // Print a stack trace if we signal out.
618 sys::PrintStackTraceOnErrorSignal();
619 PrettyStackTraceProgram X(argc, argv);
620 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
622 // Initialize targets and assembly printers/parsers.
623 llvm::InitializeAllTargetInfos();
624 llvm::InitializeAllTargetMCs();
625 llvm::InitializeAllAsmParsers();
626 llvm::InitializeAllDisassemblers();
628 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
629 TripleName = Triple::normalize(TripleName);
633 // Defaults to a.out if no filenames specified.
634 if (InputFilenames.size() == 0)
635 InputFilenames.push_back("a.out");
642 cl::PrintHelpMessage();
646 std::for_each(InputFilenames.begin(), InputFilenames.end(),