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/MCSubtargetInfo.h"
30 #include "llvm/Support/Casting.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Format.h"
35 #include "llvm/Support/GraphWriter.h"
36 #include "llvm/Support/Host.h"
37 #include "llvm/Support/ManagedStatic.h"
38 #include "llvm/Support/MemoryBuffer.h"
39 #include "llvm/Support/MemoryObject.h"
40 #include "llvm/Support/PrettyStackTrace.h"
41 #include "llvm/Support/Signals.h"
42 #include "llvm/Support/SourceMgr.h"
43 #include "llvm/Support/TargetRegistry.h"
44 #include "llvm/Support/TargetSelect.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include "llvm/Support/system_error.h"
50 using namespace object;
52 static cl::list<std::string>
53 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
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 Relocations("r", cl::desc("Display the relocation entries in the file"));
66 SectionContents("s", cl::desc("Display the content of each section"));
69 SymbolTable("t", cl::desc("Display the symbol table"));
72 MachO("macho", cl::desc("Use MachO specific object file parser"));
74 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
77 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
78 "see -version for available targets"));
81 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
82 "see -version for available targets"));
85 SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
86 "for each section."));
88 SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
89 cl::aliasopt(SectionHeaders));
91 SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
92 cl::aliasopt(SectionHeaders));
94 static StringRef ToolName;
96 static bool error(error_code ec) {
97 if (!ec) return false;
99 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
104 static const Target *GetTarget(const ObjectFile *Obj = NULL) {
105 // Figure out the target triple.
106 llvm::Triple TT("unknown-unknown-unknown");
107 if (TripleName.empty()) {
109 TT.setArch(Triple::ArchType(Obj->getArch()));
111 TT.setTriple(Triple::normalize(TripleName));
113 if (!ArchName.empty())
114 TT.setArchName(ArchName);
116 TripleName = TT.str();
118 // Get the target specific parser.
120 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
124 errs() << ToolName << ": error: unable to get target for '" << TripleName
125 << "', see --version and --triple.\n";
129 void llvm::DumpBytes(StringRef bytes) {
130 static const char hex_rep[] = "0123456789abcdef";
131 // FIXME: The real way to do this is to figure out the longest instruction
132 // and align to that size before printing. I'll fix this when I get
133 // around to outputting relocations.
134 // 15 is the longest x86 instruction
135 // 3 is for the hex rep of a byte + a space.
136 // 1 is for the null terminator.
137 enum { OutputSize = (15 * 3) + 1 };
138 char output[OutputSize];
140 assert(bytes.size() <= 15
141 && "DumpBytes only supports instructions of up to 15 bytes");
142 memset(output, ' ', sizeof(output));
144 for (StringRef::iterator i = bytes.begin(),
145 e = bytes.end(); i != e; ++i) {
146 output[index] = hex_rep[(*i & 0xF0) >> 4];
147 output[index + 1] = hex_rep[*i & 0xF];
151 output[sizeof(output) - 1] = 0;
155 static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
156 uint64_t a_addr, b_addr;
157 if (error(a.getAddress(a_addr))) return false;
158 if (error(b.getAddress(b_addr))) return false;
159 return a_addr < b_addr;
162 static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
163 const Target *TheTarget = GetTarget(Obj);
165 // GetTarget prints out stuff.
170 for (section_iterator i = Obj->begin_sections(),
171 e = Obj->end_sections();
172 i != e; i.increment(ec)) {
173 if (error(ec)) break;
175 if (error(i->isText(text))) break;
178 uint64_t SectionAddr;
179 if (error(i->getAddress(SectionAddr))) break;
181 // Make a list of all the symbols in this section.
182 std::vector<std::pair<uint64_t, StringRef> > Symbols;
183 for (symbol_iterator si = Obj->begin_symbols(),
184 se = Obj->end_symbols();
185 si != se; si.increment(ec)) {
187 if (!error(i->containsSymbol(*si, contains)) && contains) {
189 if (error(si->getOffset(Address))) break;
191 if (error(si->getName(Name))) break;
192 Symbols.push_back(std::make_pair(Address, Name));
196 // Sort the symbols by address, just in case they didn't come in that way.
197 array_pod_sort(Symbols.begin(), Symbols.end());
199 // Make a list of all the relocations for this section.
200 std::vector<RelocationRef> Rels;
202 for (relocation_iterator ri = i->begin_relocations(),
203 re = i->end_relocations();
204 ri != re; ri.increment(ec)) {
205 if (error(ec)) break;
210 // Sort relocations by address.
211 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
214 if (error(i->getName(name))) break;
215 outs() << "Disassembly of section " << name << ':';
217 // If the section has no symbols just insert a dummy one and disassemble
218 // the whole section.
220 Symbols.push_back(std::make_pair(0, name));
222 // Set up disassembler.
223 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
226 errs() << "error: no assembly info for target " << TripleName << "\n";
230 OwningPtr<const MCSubtargetInfo> STI(
231 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
234 errs() << "error: no subtarget info for target " << TripleName << "\n";
238 OwningPtr<const MCDisassembler> DisAsm(
239 TheTarget->createMCDisassembler(*STI));
241 errs() << "error: no disassembler for target " << TripleName << "\n";
245 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
246 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
247 AsmPrinterVariant, *AsmInfo, *STI));
249 errs() << "error: no instruction printer for target " << TripleName
255 if (error(i->getContents(Bytes))) break;
256 StringRefMemoryObject memoryObject(Bytes);
260 if (error(i->getSize(SectSize))) break;
262 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
263 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
264 // Disassemble symbol by symbol.
265 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
266 uint64_t Start = Symbols[si].first;
268 // The end is either the size of the section or the beginning of the next
272 // Make sure this symbol takes up space.
273 else if (Symbols[si + 1].first != Start)
274 End = Symbols[si + 1].first - 1;
276 // This symbol has the same address as the next symbol. Skip it.
279 outs() << '\n' << Symbols[si].second << ":\n";
282 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
284 raw_ostream &DebugOut = nulls();
287 for (Index = Start; Index < End; Index += Size) {
290 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
291 DebugOut, nulls())) {
292 outs() << format("%8llx:\t", SectionAddr + Index);
293 DumpBytes(StringRef(Bytes.data() + Index, Size));
294 IP->printInst(&Inst, outs(), "");
297 errs() << ToolName << ": warning: invalid instruction encoding\n";
299 Size = 1; // skip illegible bytes
302 // Print relocation for instruction.
303 while (rel_cur != rel_end) {
306 SmallString<16> name;
309 // If this relocation is hidden, skip it.
310 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
311 if (hidden) goto skip_print_rel;
313 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
314 // Stop when rel_cur's address is past the current instruction.
315 if (addr >= Index + Size) break;
316 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
317 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
319 outs() << format("\t\t\t%8llx: ", SectionAddr + addr) << name << "\t"
330 static void PrintRelocations(const ObjectFile *o) {
332 for (section_iterator si = o->begin_sections(), se = o->end_sections();
333 si != se; si.increment(ec)){
334 if (error(ec)) return;
335 if (si->begin_relocations() == si->end_relocations())
338 if (error(si->getName(secname))) continue;
339 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
340 for (relocation_iterator ri = si->begin_relocations(),
341 re = si->end_relocations();
342 ri != re; ri.increment(ec)) {
343 if (error(ec)) return;
347 SmallString<32> relocname;
348 SmallString<32> valuestr;
349 if (error(ri->getHidden(hidden))) continue;
350 if (hidden) continue;
351 if (error(ri->getTypeName(relocname))) continue;
352 if (error(ri->getAddress(address))) continue;
353 if (error(ri->getValueString(valuestr))) continue;
354 outs() << address << " " << relocname << " " << valuestr << "\n";
360 static void PrintSectionHeaders(const ObjectFile *o) {
361 outs() << "Sections:\n"
362 "Idx Name Size Address Type\n";
365 for (section_iterator si = o->begin_sections(), se = o->end_sections();
366 si != se; si.increment(ec)) {
367 if (error(ec)) return;
369 if (error(si->getName(Name))) return;
371 if (error(si->getAddress(Address))) return;
373 if (error(si->getSize(Size))) return;
374 bool Text, Data, BSS;
375 if (error(si->isText(Text))) return;
376 if (error(si->isData(Data))) return;
377 if (error(si->isBSS(BSS))) return;
378 std::string Type = (std::string(Text ? "TEXT " : "") +
379 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
380 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
381 Address, Type.c_str());
386 static void PrintSectionContents(const ObjectFile *o) {
388 for (section_iterator si = o->begin_sections(),
389 se = o->end_sections();
390 si != se; si.increment(ec)) {
391 if (error(ec)) return;
395 if (error(si->getName(Name))) continue;
396 if (error(si->getContents(Contents))) continue;
397 if (error(si->getAddress(BaseAddr))) continue;
399 outs() << "Contents of section " << Name << ":\n";
401 // Dump out the content as hex and printable ascii characters.
402 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
403 outs() << format(" %04llx ", BaseAddr + addr);
405 for (std::size_t i = 0; i < 16; ++i) {
406 if (i != 0 && i % 4 == 0)
409 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
410 << hexdigit(Contents[addr + i] & 0xF, true);
416 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
417 if (std::isprint(Contents[addr + i] & 0xFF))
418 outs() << Contents[addr + i];
427 static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
428 const coff_file_header *header;
429 if (error(coff->getHeader(header))) return;
431 const coff_symbol *symbol = 0;
432 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
434 // Figure out which type of aux this is.
435 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
436 && symbol->Value == 0) { // Section definition.
437 const coff_aux_section_definition *asd;
438 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
441 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
442 , unsigned(asd->Length)
443 , unsigned(asd->NumberOfRelocations)
444 , unsigned(asd->NumberOfLinenumbers)
445 , unsigned(asd->CheckSum))
446 << format("assoc %d comdat %d\n"
447 , unsigned(asd->Number)
448 , unsigned(asd->Selection));
450 outs() << "AUX Unknown\n";
454 if (error(coff->getSymbol(i, symbol))) return;
455 if (error(coff->getSymbolName(symbol, name))) return;
456 outs() << "[" << format("%2d", i) << "]"
457 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
458 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
459 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
460 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
461 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
462 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
464 aux_count = symbol->NumberOfAuxSymbols;
469 static void PrintSymbolTable(const ObjectFile *o) {
470 outs() << "SYMBOL TABLE:\n";
472 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
473 PrintCOFFSymbolTable(coff);
476 for (symbol_iterator si = o->begin_symbols(),
477 se = o->end_symbols(); si != se; si.increment(ec)) {
478 if (error(ec)) return;
482 SymbolRef::Type Type;
486 section_iterator Section = o->end_sections();
487 if (error(si->getName(Name))) continue;
488 if (error(si->getOffset(Offset))) continue;
489 if (error(si->isGlobal(Global))) continue;
490 if (error(si->getType(Type))) continue;
491 if (error(si->isWeak(Weak))) continue;
492 if (error(si->isAbsolute(Absolute))) continue;
493 if (error(si->getSize(Size))) continue;
494 if (error(si->getSection(Section))) continue;
496 if (Offset == UnknownAddressOrSize)
499 if (Type != SymbolRef::ST_External)
500 GlobLoc = Global ? 'g' : 'l';
501 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
504 if (Type == SymbolRef::ST_File)
506 else if (Type == SymbolRef::ST_Function)
509 outs() << format("%08llx", Offset) << " "
510 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
511 << (Weak ? 'w' : ' ') // Weak?
512 << ' ' // Constructor. Not supported yet.
513 << ' ' // Warning. Not supported yet.
514 << ' ' // Indirect reference to another symbol.
515 << Debug // Debugging (d) or dynamic (D) symbol.
516 << FileFunc // Name of function (F), file (f) or object (O).
520 else if (Section == o->end_sections())
523 StringRef SectionName;
524 if (error(Section->getName(SectionName)))
526 outs() << SectionName;
529 << format("%08llx ", Size)
536 static void DumpObject(const ObjectFile *o) {
538 outs() << o->getFileName()
539 << ":\tfile format " << o->getFileFormatName() << "\n\n";
542 DisassembleObject(o, Relocations);
543 if (Relocations && !Disassemble)
546 PrintSectionHeaders(o);
548 PrintSectionContents(o);
553 /// @brief Dump each object file in \a a;
554 static void DumpArchive(const Archive *a) {
555 for (Archive::child_iterator i = a->begin_children(),
556 e = a->end_children(); i != e; ++i) {
557 OwningPtr<Binary> child;
558 if (error_code ec = i->getAsBinary(child)) {
559 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
563 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
566 errs() << ToolName << ": '" << a->getFileName() << "': "
567 << "Unrecognized file type.\n";
571 /// @brief Open file and figure out how to dump it.
572 static void DumpInput(StringRef file) {
573 // If file isn't stdin, check that it exists.
574 if (file != "-" && !sys::fs::exists(file)) {
575 errs() << ToolName << ": '" << file << "': " << "No such file\n";
579 if (MachO && Disassemble) {
580 DisassembleInputMachO(file);
584 // Attempt to open the binary.
585 OwningPtr<Binary> binary;
586 if (error_code ec = createBinary(file, binary)) {
587 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
591 if (Archive *a = dyn_cast<Archive>(binary.get())) {
593 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
596 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
600 int main(int argc, char **argv) {
601 // Print a stack trace if we signal out.
602 sys::PrintStackTraceOnErrorSignal();
603 PrettyStackTraceProgram X(argc, argv);
604 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
606 // Initialize targets and assembly printers/parsers.
607 llvm::InitializeAllTargetInfos();
608 llvm::InitializeAllTargetMCs();
609 llvm::InitializeAllAsmParsers();
610 llvm::InitializeAllDisassemblers();
612 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
613 TripleName = Triple::normalize(TripleName);
617 // Defaults to a.out if no filenames specified.
618 if (InputFilenames.size() == 0)
619 InputFilenames.push_back("a.out");
626 cl::PrintHelpMessage();
630 std::for_each(InputFilenames.begin(), InputFilenames.end(),