blob: c2480df14157354ae32ffc01f853aab9fa225849 [file] [log] [blame]
Michael J. Spencer92e1deb2011-01-20 06:39:06 +00001//===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
12// flags.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000016#include "llvm-objdump.h"
Benjamin Kramer685a2502011-07-20 19:37:35 +000017#include "MCFunction.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000018#include "llvm/Object/Archive.h"
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000019#include "llvm/Object/COFF.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000020#include "llvm/Object/ObjectFile.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000021#include "llvm/ADT/OwningPtr.h"
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000022#include "llvm/ADT/StringExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000023#include "llvm/ADT/Triple.h"
Benjamin Kramer739b65b2011-07-15 18:39:24 +000024#include "llvm/ADT/STLExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000025#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCDisassembler.h"
27#include "llvm/MC/MCInst.h"
28#include "llvm/MC/MCInstPrinter.h"
James Molloyb9505852011-09-07 17:24:38 +000029#include "llvm/MC/MCSubtargetInfo.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000030#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000033#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000034#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000035#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000036#include "llvm/Support/Host.h"
37#include "llvm/Support/ManagedStatic.h"
38#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000039#include "llvm/Support/MemoryObject.h"
40#include "llvm/Support/PrettyStackTrace.h"
41#include "llvm/Support/Signals.h"
42#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000043#include "llvm/Support/TargetRegistry.h"
44#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000045#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047#include <algorithm>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049using namespace llvm;
50using namespace object;
51
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000052static cl::list<std::string>
53InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000054
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000055static cl::opt<bool>
56Disassemble("disassemble",
57 cl::desc("Display assembler mnemonics for the machine instructions"));
58static cl::alias
59Disassembled("d", cl::desc("Alias for --disassemble"),
60 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000061
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000062static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000063Relocations("r", cl::desc("Display the relocation entries in the file"));
64
65static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000066SectionContents("s", cl::desc("Display the content of each section"));
67
68static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000069SymbolTable("t", cl::desc("Display the symbol table"));
70
71static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000072MachO("macho", cl::desc("Use MachO specific object file parser"));
73static cl::alias
74MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000075
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000076cl::opt<std::string>
77llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
78 "see -version for available targets"));
79
80cl::opt<std::string>
81llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000082 "see -version for available targets"));
83
Nick Lewycky023bb152011-10-10 21:21:34 +000084static cl::opt<bool>
85SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
86 "for each section."));
87static cl::alias
88SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
89 cl::aliasopt(SectionHeaders));
90static cl::alias
91SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
92 cl::aliasopt(SectionHeaders));
93
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000094static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000095
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000096static bool error(error_code ec) {
97 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000098
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000099 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
100 outs().flush();
101 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000102}
103
104static const Target *GetTarget(const ObjectFile *Obj = NULL) {
105 // Figure out the target triple.
106 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000107 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000108 if (Obj)
109 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000110 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000111 TT.setTriple(Triple::normalize(TripleName));
112
113 if (!ArchName.empty())
114 TT.setArchName(ArchName);
115
116 TripleName = TT.str();
117
118 // Get the target specific parser.
119 std::string Error;
120 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
121 if (TheTarget)
122 return TheTarget;
123
124 errs() << ToolName << ": error: unable to get target for '" << TripleName
125 << "', see --version and --triple.\n";
126 return 0;
127}
128
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000129void llvm::DumpBytes(StringRef bytes) {
130 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000131 // 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];
139
140 assert(bytes.size() <= 15
141 && "DumpBytes only supports instructions of up to 15 bytes");
142 memset(output, ' ', sizeof(output));
143 unsigned index = 0;
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];
148 index += 3;
149 }
150
151 output[sizeof(output) - 1] = 0;
152 outs() << output;
153}
154
Michael J. Spencer942eb002011-10-13 22:17:18 +0000155static 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;
160}
161
162static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000163 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000164 if (!TheTarget) {
165 // GetTarget prints out stuff.
166 return;
167 }
168
Michael J. Spencer25b15772011-06-25 17:55:23 +0000169 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000170 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000171 e = Obj->end_sections();
172 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173 if (error(ec)) break;
174 bool text;
175 if (error(i->isText(text))) break;
176 if (!text) continue;
177
Michael J. Spencer942eb002011-10-13 22:17:18 +0000178 uint64_t SectionAddr;
179 if (error(i->getAddress(SectionAddr))) break;
180
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000181 // Make a list of all the symbols in this section.
182 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000183 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000184 se = Obj->end_symbols();
185 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000186 bool contains;
187 if (!error(i->containsSymbol(*si, contains)) && contains) {
188 uint64_t Address;
Benjamin Kramerac241fe2011-09-14 01:22:52 +0000189 if (error(si->getOffset(Address))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000190 StringRef Name;
191 if (error(si->getName(Name))) break;
192 Symbols.push_back(std::make_pair(Address, Name));
193 }
194 }
195
196 // Sort the symbols by address, just in case they didn't come in that way.
197 array_pod_sort(Symbols.begin(), Symbols.end());
198
Michael J. Spencer942eb002011-10-13 22:17:18 +0000199 // Make a list of all the relocations for this section.
200 std::vector<RelocationRef> Rels;
201 if (InlineRelocs) {
202 for (relocation_iterator ri = i->begin_relocations(),
203 re = i->end_relocations();
204 ri != re; ri.increment(ec)) {
205 if (error(ec)) break;
206 Rels.push_back(*ri);
207 }
208 }
209
210 // Sort relocations by address.
211 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
212
Michael J. Spencer25b15772011-06-25 17:55:23 +0000213 StringRef name;
214 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000215 outs() << "Disassembly of section " << name << ':';
216
217 // If the section has no symbols just insert a dummy one and disassemble
218 // the whole section.
219 if (Symbols.empty())
220 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000221
222 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000223 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000224
225 if (!AsmInfo) {
226 errs() << "error: no assembly info for target " << TripleName << "\n";
227 return;
228 }
229
Michael J. Spencer27781b72011-10-08 00:18:30 +0000230 OwningPtr<const MCSubtargetInfo> STI(
231 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000232
233 if (!STI) {
234 errs() << "error: no subtarget info for target " << TripleName << "\n";
235 return;
236 }
237
Michael J. Spencer27781b72011-10-08 00:18:30 +0000238 OwningPtr<const MCDisassembler> DisAsm(
239 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000240 if (!DisAsm) {
241 errs() << "error: no disassembler for target " << TripleName << "\n";
242 return;
243 }
244
245 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
246 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
James Molloyb9505852011-09-07 17:24:38 +0000247 AsmPrinterVariant, *AsmInfo, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000248 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000249 errs() << "error: no instruction printer for target " << TripleName
250 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000251 return;
252 }
253
Michael J. Spencer25b15772011-06-25 17:55:23 +0000254 StringRef Bytes;
255 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000256 StringRefMemoryObject memoryObject(Bytes);
257 uint64_t Size;
258 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000259 uint64_t SectSize;
260 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000261
Michael J. Spencer942eb002011-10-13 22:17:18 +0000262 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
263 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000264 // Disassemble symbol by symbol.
265 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
266 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000267 uint64_t End;
268 // The end is either the size of the section or the beginning of the next
269 // symbol.
270 if (si == se - 1)
271 End = SectSize;
272 // Make sure this symbol takes up space.
273 else if (Symbols[si + 1].first != Start)
274 End = Symbols[si + 1].first - 1;
275 else
276 // This symbol has the same address as the next symbol. Skip it.
277 continue;
278
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000279 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000280
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000281#ifndef NDEBUG
282 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
283#else
284 raw_ostream &DebugOut = nulls();
285#endif
286
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000287 for (Index = Start; Index < End; Index += Size) {
288 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000289
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000290 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
291 DebugOut, nulls())) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000292 outs() << format("%8x:\t", SectionAddr + Index);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000293 DumpBytes(StringRef(Bytes.data() + Index, Size));
294 IP->printInst(&Inst, outs(), "");
295 outs() << "\n";
296 } else {
297 errs() << ToolName << ": warning: invalid instruction encoding\n";
298 if (Size == 0)
299 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000300 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000301
302 // Print relocation for instruction.
303 while (rel_cur != rel_end) {
304 uint64_t addr;
305 SmallString<16> name;
306 SmallString<32> val;
307 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
308 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000309 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000310 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
311 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
312
313 outs() << format("\t\t\t%8x: ", SectionAddr + addr) << name << "\t"
314 << val << "\n";
315
316 skip_print_rel:
317 ++rel_cur;
318 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000319 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000320 }
321 }
322}
323
Michael J. Spencer27781b72011-10-08 00:18:30 +0000324static void PrintRelocations(const ObjectFile *o) {
325 error_code ec;
326 for (section_iterator si = o->begin_sections(), se = o->end_sections();
327 si != se; si.increment(ec)){
328 if (error(ec)) return;
329 if (si->begin_relocations() == si->end_relocations())
330 continue;
331 StringRef secname;
332 if (error(si->getName(secname))) continue;
333 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
334 for (relocation_iterator ri = si->begin_relocations(),
335 re = si->end_relocations();
336 ri != re; ri.increment(ec)) {
337 if (error(ec)) return;
338
339 uint64_t address;
340 SmallString<32> relocname;
341 SmallString<32> valuestr;
342 if (error(ri->getTypeName(relocname))) continue;
343 if (error(ri->getAddress(address))) continue;
344 if (error(ri->getValueString(valuestr))) continue;
345 outs() << address << " " << relocname << " " << valuestr << "\n";
346 }
347 outs() << "\n";
348 }
349}
350
Nick Lewycky023bb152011-10-10 21:21:34 +0000351static void PrintSectionHeaders(const ObjectFile *o) {
352 outs() << "Sections:\n"
353 "Idx Name Size Address Type\n";
354 error_code ec;
355 unsigned i = 0;
356 for (section_iterator si = o->begin_sections(), se = o->end_sections();
357 si != se; si.increment(ec)) {
358 if (error(ec)) return;
359 StringRef Name;
360 if (error(si->getName(Name))) return;
361 uint64_t Address;
362 if (error(si->getAddress(Address))) return;
363 uint64_t Size;
364 if (error(si->getSize(Size))) return;
365 bool Text, Data, BSS;
366 if (error(si->isText(Text))) return;
367 if (error(si->isData(Data))) return;
368 if (error(si->isBSS(BSS))) return;
369 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000370 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
NAKAMURA Takumif048c3f2011-10-11 12:51:50 +0000371 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
Nick Lewycky023bb152011-10-10 21:21:34 +0000372 Address, Type.c_str());
373 ++i;
374 }
375}
376
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000377static void PrintSectionContents(const ObjectFile *o) {
378 error_code ec;
379 for (section_iterator si = o->begin_sections(),
380 se = o->end_sections();
381 si != se; si.increment(ec)) {
382 if (error(ec)) return;
383 StringRef Name;
384 StringRef Contents;
385 uint64_t BaseAddr;
386 if (error(si->getName(Name))) continue;
387 if (error(si->getContents(Contents))) continue;
388 if (error(si->getAddress(BaseAddr))) continue;
389
390 outs() << "Contents of section " << Name << ":\n";
391
392 // Dump out the content as hex and printable ascii characters.
393 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
394 outs() << format(" %04x ", BaseAddr + addr);
395 // Dump line of hex.
396 for (std::size_t i = 0; i < 16; ++i) {
397 if (i != 0 && i % 4 == 0)
398 outs() << ' ';
399 if (addr + i < end)
400 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
401 << hexdigit(Contents[addr + i] & 0xF, true);
402 else
403 outs() << " ";
404 }
405 // Print ascii.
406 outs() << " ";
407 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
408 if (std::isprint(Contents[addr + i] & 0xFF))
409 outs() << Contents[addr + i];
410 else
411 outs() << ".";
412 }
413 outs() << "\n";
414 }
415 }
416}
417
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000418static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
419 const coff_file_header *header;
420 if (error(coff->getHeader(header))) return;
421 int aux_count = 0;
422 const coff_symbol *symbol = 0;
423 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
424 if (aux_count--) {
425 // Figure out which type of aux this is.
426 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
427 && symbol->Value == 0) { // Section definition.
428 const coff_aux_section_definition *asd;
429 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
430 return;
431 outs() << "AUX "
432 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
433 , unsigned(asd->Length)
434 , unsigned(asd->NumberOfRelocations)
435 , unsigned(asd->NumberOfLinenumbers)
436 , unsigned(asd->CheckSum))
437 << format("assoc %d comdat %d\n"
438 , unsigned(asd->Number)
439 , unsigned(asd->Selection));
440 } else {
441 outs() << "AUX Unknown\n";
442 }
443 } else {
444 StringRef name;
445 if (error(coff->getSymbol(i, symbol))) return;
446 if (error(coff->getSymbolName(symbol, name))) return;
447 outs() << "[" << format("%2d", i) << "]"
448 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
449 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
450 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
451 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
452 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
453 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
454 << name << "\n";
455 aux_count = symbol->NumberOfAuxSymbols;
456 }
457 }
458}
459
460static void PrintSymbolTable(const ObjectFile *o) {
461 outs() << "SYMBOL TABLE:\n";
462
463 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
464 PrintCOFFSymbolTable(coff);
465 else {
466 error_code ec;
467 for (symbol_iterator si = o->begin_symbols(),
468 se = o->end_symbols(); si != se; si.increment(ec)) {
469 if (error(ec)) return;
470 StringRef Name;
471 uint64_t Offset;
472 bool Global;
473 SymbolRef::Type Type;
474 bool Weak;
475 bool Absolute;
476 uint64_t Size;
477 section_iterator Section = o->end_sections();
478 if (error(si->getName(Name))) continue;
479 if (error(si->getOffset(Offset))) continue;
480 if (error(si->isGlobal(Global))) continue;
481 if (error(si->getType(Type))) continue;
482 if (error(si->isWeak(Weak))) continue;
483 if (error(si->isAbsolute(Absolute))) continue;
484 if (error(si->getSize(Size))) continue;
485 if (error(si->getSection(Section))) continue;
486
487 if (Offset == UnknownAddressOrSize)
488 Offset = 0;
489 char GlobLoc = ' ';
490 if (Type != SymbolRef::ST_External)
491 GlobLoc = Global ? 'g' : 'l';
492 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
493 ? 'd' : ' ';
494 char FileFunc = ' ';
495 if (Type == SymbolRef::ST_File)
496 FileFunc = 'f';
497 else if (Type == SymbolRef::ST_Function)
498 FileFunc = 'F';
499
500 outs() << format("%08x", Offset) << " "
501 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
502 << (Weak ? 'w' : ' ') // Weak?
503 << ' ' // Constructor. Not supported yet.
504 << ' ' // Warning. Not supported yet.
505 << ' ' // Indirect reference to another symbol.
506 << Debug // Debugging (d) or dynamic (D) symbol.
507 << FileFunc // Name of function (F), file (f) or object (O).
508 << ' ';
509 if (Absolute)
510 outs() << "*ABS*";
511 else if (Section == o->end_sections())
512 outs() << "*UND*";
513 else {
514 StringRef SectionName;
515 if (error(Section->getName(SectionName)))
516 SectionName = "";
517 outs() << SectionName;
518 }
519 outs() << '\t'
520 << format("%08x ", Size)
521 << Name
522 << '\n';
523 }
524 }
525}
526
Michael J. Spencer27781b72011-10-08 00:18:30 +0000527static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000528 outs() << '\n';
529 outs() << o->getFileName()
530 << ":\tfile format " << o->getFileFormatName() << "\n\n";
531
Michael J. Spencer27781b72011-10-08 00:18:30 +0000532 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000533 DisassembleObject(o, Relocations);
534 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000535 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000536 if (SectionHeaders)
537 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000538 if (SectionContents)
539 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000540 if (SymbolTable)
541 PrintSymbolTable(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000542}
543
544/// @brief Dump each object file in \a a;
545static void DumpArchive(const Archive *a) {
546 for (Archive::child_iterator i = a->begin_children(),
547 e = a->end_children(); i != e; ++i) {
548 OwningPtr<Binary> child;
549 if (error_code ec = i->getAsBinary(child)) {
550 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
551 << ".\n";
552 continue;
553 }
554 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
555 DumpObject(o);
556 else
557 errs() << ToolName << ": '" << a->getFileName() << "': "
558 << "Unrecognized file type.\n";
559 }
560}
561
562/// @brief Open file and figure out how to dump it.
563static void DumpInput(StringRef file) {
564 // If file isn't stdin, check that it exists.
565 if (file != "-" && !sys::fs::exists(file)) {
566 errs() << ToolName << ": '" << file << "': " << "No such file\n";
567 return;
568 }
569
570 if (MachO && Disassemble) {
571 DisassembleInputMachO(file);
572 return;
573 }
574
575 // Attempt to open the binary.
576 OwningPtr<Binary> binary;
577 if (error_code ec = createBinary(file, binary)) {
578 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
579 return;
580 }
581
582 if (Archive *a = dyn_cast<Archive>(binary.get())) {
583 DumpArchive(a);
584 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
585 DumpObject(o);
586 } else {
587 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
588 }
589}
590
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000591int main(int argc, char **argv) {
592 // Print a stack trace if we signal out.
593 sys::PrintStackTraceOnErrorSignal();
594 PrettyStackTraceProgram X(argc, argv);
595 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
596
597 // Initialize targets and assembly printers/parsers.
598 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000599 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000600 llvm::InitializeAllAsmParsers();
601 llvm::InitializeAllDisassemblers();
602
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000603 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
604 TripleName = Triple::normalize(TripleName);
605
606 ToolName = argv[0];
607
608 // Defaults to a.out if no filenames specified.
609 if (InputFilenames.size() == 0)
610 InputFilenames.push_back("a.out");
611
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000612 if (!Disassemble
613 && !Relocations
614 && !SectionHeaders
615 && !SectionContents
616 && !SymbolTable) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000617 cl::PrintHelpMessage();
618 return 2;
619 }
620
Michael J. Spencer27781b72011-10-08 00:18:30 +0000621 std::for_each(InputFilenames.begin(), InputFilenames.end(),
622 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000623
624 return 0;
625}