blob: 9071d58b0e7e27a68883ae8249bdc924a3399a16 [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
Owen Anderson10c044e2011-10-27 21:55:13 +0000238 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000239 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())) {
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000292 outs() << format("%8"PRIx64":\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) {
Owen Anderson0685e942011-10-25 20:35:53 +0000304 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000305 uint64_t addr;
306 SmallString<16> name;
307 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000308
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;
312
Michael J. Spencer942eb002011-10-13 22:17:18 +0000313 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
314 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000315 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000316 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
317 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
318
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000319 outs() << format("\t\t\t%8"PRIx64": ", SectionAddr + addr) << name << "\t"
Michael J. Spencer942eb002011-10-13 22:17:18 +0000320 << val << "\n";
321
322 skip_print_rel:
323 ++rel_cur;
324 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000325 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000326 }
327 }
328}
329
Michael J. Spencer27781b72011-10-08 00:18:30 +0000330static void PrintRelocations(const ObjectFile *o) {
331 error_code ec;
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())
336 continue;
337 StringRef secname;
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;
344
Owen Anderson0685e942011-10-25 20:35:53 +0000345 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000346 uint64_t address;
347 SmallString<32> relocname;
348 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000349 if (error(ri->getHidden(hidden))) continue;
350 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000351 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";
355 }
356 outs() << "\n";
357 }
358}
359
Nick Lewycky023bb152011-10-10 21:21:34 +0000360static void PrintSectionHeaders(const ObjectFile *o) {
361 outs() << "Sections:\n"
362 "Idx Name Size Address Type\n";
363 error_code ec;
364 unsigned i = 0;
365 for (section_iterator si = o->begin_sections(), se = o->end_sections();
366 si != se; si.increment(ec)) {
367 if (error(ec)) return;
368 StringRef Name;
369 if (error(si->getName(Name))) return;
370 uint64_t Address;
371 if (error(si->getAddress(Address))) return;
372 uint64_t Size;
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 " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000379 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
NAKAMURA Takumif048c3f2011-10-11 12:51:50 +0000380 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
Nick Lewycky023bb152011-10-10 21:21:34 +0000381 Address, Type.c_str());
382 ++i;
383 }
384}
385
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000386static void PrintSectionContents(const ObjectFile *o) {
387 error_code ec;
388 for (section_iterator si = o->begin_sections(),
389 se = o->end_sections();
390 si != se; si.increment(ec)) {
391 if (error(ec)) return;
392 StringRef Name;
393 StringRef Contents;
394 uint64_t BaseAddr;
395 if (error(si->getName(Name))) continue;
396 if (error(si->getContents(Contents))) continue;
397 if (error(si->getAddress(BaseAddr))) continue;
398
399 outs() << "Contents of section " << Name << ":\n";
400
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) {
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000403 outs() << format(" %04"PRIx64" ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000404 // Dump line of hex.
405 for (std::size_t i = 0; i < 16; ++i) {
406 if (i != 0 && i % 4 == 0)
407 outs() << ' ';
408 if (addr + i < end)
409 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
410 << hexdigit(Contents[addr + i] & 0xF, true);
411 else
412 outs() << " ";
413 }
414 // Print ascii.
415 outs() << " ";
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];
419 else
420 outs() << ".";
421 }
422 outs() << "\n";
423 }
424 }
425}
426
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000427static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
428 const coff_file_header *header;
429 if (error(coff->getHeader(header))) return;
430 int aux_count = 0;
431 const coff_symbol *symbol = 0;
432 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
433 if (aux_count--) {
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)))
439 return;
440 outs() << "AUX "
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));
449 } else {
450 outs() << "AUX Unknown\n";
451 }
452 } else {
453 StringRef name;
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)) << " "
463 << name << "\n";
464 aux_count = symbol->NumberOfAuxSymbols;
465 }
466 }
467}
468
469static void PrintSymbolTable(const ObjectFile *o) {
470 outs() << "SYMBOL TABLE:\n";
471
472 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
473 PrintCOFFSymbolTable(coff);
474 else {
475 error_code ec;
476 for (symbol_iterator si = o->begin_symbols(),
477 se = o->end_symbols(); si != se; si.increment(ec)) {
478 if (error(ec)) return;
479 StringRef Name;
480 uint64_t Offset;
481 bool Global;
482 SymbolRef::Type Type;
483 bool Weak;
484 bool Absolute;
485 uint64_t Size;
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;
495
496 if (Offset == UnknownAddressOrSize)
497 Offset = 0;
498 char GlobLoc = ' ';
499 if (Type != SymbolRef::ST_External)
500 GlobLoc = Global ? 'g' : 'l';
501 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
502 ? 'd' : ' ';
503 char FileFunc = ' ';
504 if (Type == SymbolRef::ST_File)
505 FileFunc = 'f';
506 else if (Type == SymbolRef::ST_Function)
507 FileFunc = 'F';
508
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000509 outs() << format("%08"PRIx64, Offset) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000510 << 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).
517 << ' ';
518 if (Absolute)
519 outs() << "*ABS*";
520 else if (Section == o->end_sections())
521 outs() << "*UND*";
522 else {
523 StringRef SectionName;
524 if (error(Section->getName(SectionName)))
525 SectionName = "";
526 outs() << SectionName;
527 }
528 outs() << '\t'
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000529 << format("%08"PRIx64" ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000530 << Name
531 << '\n';
532 }
533 }
534}
535
Michael J. Spencer27781b72011-10-08 00:18:30 +0000536static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000537 outs() << '\n';
538 outs() << o->getFileName()
539 << ":\tfile format " << o->getFileFormatName() << "\n\n";
540
Michael J. Spencer27781b72011-10-08 00:18:30 +0000541 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000542 DisassembleObject(o, Relocations);
543 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000544 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000545 if (SectionHeaders)
546 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000547 if (SectionContents)
548 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000549 if (SymbolTable)
550 PrintSymbolTable(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000551}
552
553/// @brief Dump each object file in \a a;
554static 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)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000559 // Ignore non-object files.
560 if (ec != object_error::invalid_file_type)
561 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
562 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000563 continue;
564 }
565 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
566 DumpObject(o);
567 else
568 errs() << ToolName << ": '" << a->getFileName() << "': "
569 << "Unrecognized file type.\n";
570 }
571}
572
573/// @brief Open file and figure out how to dump it.
574static void DumpInput(StringRef file) {
575 // If file isn't stdin, check that it exists.
576 if (file != "-" && !sys::fs::exists(file)) {
577 errs() << ToolName << ": '" << file << "': " << "No such file\n";
578 return;
579 }
580
581 if (MachO && Disassemble) {
582 DisassembleInputMachO(file);
583 return;
584 }
585
586 // Attempt to open the binary.
587 OwningPtr<Binary> binary;
588 if (error_code ec = createBinary(file, binary)) {
589 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
590 return;
591 }
592
593 if (Archive *a = dyn_cast<Archive>(binary.get())) {
594 DumpArchive(a);
595 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
596 DumpObject(o);
597 } else {
598 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
599 }
600}
601
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000602int main(int argc, char **argv) {
603 // Print a stack trace if we signal out.
604 sys::PrintStackTraceOnErrorSignal();
605 PrettyStackTraceProgram X(argc, argv);
606 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
607
608 // Initialize targets and assembly printers/parsers.
609 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000610 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000611 llvm::InitializeAllAsmParsers();
612 llvm::InitializeAllDisassemblers();
613
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000614 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
615 TripleName = Triple::normalize(TripleName);
616
617 ToolName = argv[0];
618
619 // Defaults to a.out if no filenames specified.
620 if (InputFilenames.size() == 0)
621 InputFilenames.push_back("a.out");
622
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000623 if (!Disassemble
624 && !Relocations
625 && !SectionHeaders
626 && !SectionContents
627 && !SymbolTable) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000628 cl::PrintHelpMessage();
629 return 2;
630 }
631
Michael J. Spencer27781b72011-10-08 00:18:30 +0000632 std::for_each(InputFilenames.begin(), InputFilenames.end(),
633 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000634
635 return 0;
636}