blob: deb4dd1fcbcf37e71af35cbd7dd438f80734e178 [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"
Jim Grosbachc6449b62012-03-05 19:33:20 +000029#include "llvm/MC/MCRegisterInfo.h"
James Molloyb9505852011-09-07 17:24:38 +000030#include "llvm/MC/MCSubtargetInfo.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000031#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000032#include "llvm/Support/CommandLine.h"
33#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000034#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000035#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000036#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000037#include "llvm/Support/Host.h"
38#include "llvm/Support/ManagedStatic.h"
39#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000040#include "llvm/Support/MemoryObject.h"
41#include "llvm/Support/PrettyStackTrace.h"
42#include "llvm/Support/Signals.h"
43#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000044#include "llvm/Support/TargetRegistry.h"
45#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048#include <algorithm>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000050using namespace llvm;
51using namespace object;
52
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000053static cl::list<std::string>
54InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000055
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000056static cl::opt<bool>
57Disassemble("disassemble",
58 cl::desc("Display assembler mnemonics for the machine instructions"));
59static cl::alias
60Disassembled("d", cl::desc("Alias for --disassemble"),
61 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000062
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000063static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000064Relocations("r", cl::desc("Display the relocation entries in the file"));
65
66static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000067SectionContents("s", cl::desc("Display the content of each section"));
68
69static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000070SymbolTable("t", cl::desc("Display the symbol table"));
71
72static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000073MachO("macho", cl::desc("Use MachO specific object file parser"));
74static cl::alias
75MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000076
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000077cl::opt<std::string>
78llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
79 "see -version for available targets"));
80
81cl::opt<std::string>
82llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000083 "see -version for available targets"));
84
Nick Lewycky023bb152011-10-10 21:21:34 +000085static cl::opt<bool>
86SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
87 "for each section."));
88static cl::alias
89SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
90 cl::aliasopt(SectionHeaders));
91static cl::alias
92SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
93 cl::aliasopt(SectionHeaders));
94
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000095static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000096
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000097static bool error(error_code ec) {
98 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +000099
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000100 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
101 outs().flush();
102 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000103}
104
105static const Target *GetTarget(const ObjectFile *Obj = NULL) {
106 // Figure out the target triple.
107 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000108 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000109 if (Obj)
110 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000111 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000112 TT.setTriple(Triple::normalize(TripleName));
113
114 if (!ArchName.empty())
115 TT.setArchName(ArchName);
116
117 TripleName = TT.str();
118
119 // Get the target specific parser.
120 std::string Error;
121 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
122 if (TheTarget)
123 return TheTarget;
124
125 errs() << ToolName << ": error: unable to get target for '" << TripleName
126 << "', see --version and --triple.\n";
127 return 0;
128}
129
David Blaikie2d24e2a2011-12-20 02:50:00 +0000130void llvm::StringRefMemoryObject::anchor() { }
131
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000132void llvm::DumpBytes(StringRef bytes) {
133 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000134 // FIXME: The real way to do this is to figure out the longest instruction
135 // and align to that size before printing. I'll fix this when I get
136 // around to outputting relocations.
137 // 15 is the longest x86 instruction
138 // 3 is for the hex rep of a byte + a space.
139 // 1 is for the null terminator.
140 enum { OutputSize = (15 * 3) + 1 };
141 char output[OutputSize];
142
143 assert(bytes.size() <= 15
144 && "DumpBytes only supports instructions of up to 15 bytes");
145 memset(output, ' ', sizeof(output));
146 unsigned index = 0;
147 for (StringRef::iterator i = bytes.begin(),
148 e = bytes.end(); i != e; ++i) {
149 output[index] = hex_rep[(*i & 0xF0) >> 4];
150 output[index + 1] = hex_rep[*i & 0xF];
151 index += 3;
152 }
153
154 output[sizeof(output) - 1] = 0;
155 outs() << output;
156}
157
Michael J. Spencer942eb002011-10-13 22:17:18 +0000158static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
159 uint64_t a_addr, b_addr;
160 if (error(a.getAddress(a_addr))) return false;
161 if (error(b.getAddress(b_addr))) return false;
162 return a_addr < b_addr;
163}
164
165static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000166 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000167 if (!TheTarget) {
168 // GetTarget prints out stuff.
169 return;
170 }
171
Michael J. Spencer25b15772011-06-25 17:55:23 +0000172 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000173 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000174 e = Obj->end_sections();
175 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000176 if (error(ec)) break;
177 bool text;
178 if (error(i->isText(text))) break;
179 if (!text) continue;
180
Michael J. Spencer942eb002011-10-13 22:17:18 +0000181 uint64_t SectionAddr;
182 if (error(i->getAddress(SectionAddr))) break;
183
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000184 // Make a list of all the symbols in this section.
185 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000186 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000187 se = Obj->end_symbols();
188 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000189 bool contains;
190 if (!error(i->containsSymbol(*si, contains)) && contains) {
191 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000192 if (error(si->getAddress(Address))) break;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000193 Address -= SectionAddr;
194
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000195 StringRef Name;
196 if (error(si->getName(Name))) break;
197 Symbols.push_back(std::make_pair(Address, Name));
198 }
199 }
200
201 // Sort the symbols by address, just in case they didn't come in that way.
202 array_pod_sort(Symbols.begin(), Symbols.end());
203
Michael J. Spencer942eb002011-10-13 22:17:18 +0000204 // Make a list of all the relocations for this section.
205 std::vector<RelocationRef> Rels;
206 if (InlineRelocs) {
207 for (relocation_iterator ri = i->begin_relocations(),
208 re = i->end_relocations();
209 ri != re; ri.increment(ec)) {
210 if (error(ec)) break;
211 Rels.push_back(*ri);
212 }
213 }
214
215 // Sort relocations by address.
216 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
217
Michael J. Spencer25b15772011-06-25 17:55:23 +0000218 StringRef name;
219 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000220 outs() << "Disassembly of section " << name << ':';
221
222 // If the section has no symbols just insert a dummy one and disassemble
223 // the whole section.
224 if (Symbols.empty())
225 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000226
227 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000228 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000229
230 if (!AsmInfo) {
231 errs() << "error: no assembly info for target " << TripleName << "\n";
232 return;
233 }
234
Michael J. Spencer27781b72011-10-08 00:18:30 +0000235 OwningPtr<const MCSubtargetInfo> STI(
236 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000237
238 if (!STI) {
239 errs() << "error: no subtarget info for target " << TripleName << "\n";
240 return;
241 }
242
Owen Anderson10c044e2011-10-27 21:55:13 +0000243 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000244 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000245 if (!DisAsm) {
246 errs() << "error: no disassembler for target " << TripleName << "\n";
247 return;
248 }
249
Jim Grosbachc6449b62012-03-05 19:33:20 +0000250 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
251 if (!MRI) {
252 errs() << "error: no register info for target " << TripleName << "\n";
253 return;
254 }
255
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000256 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
257 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Jim Grosbachc6449b62012-03-05 19:33:20 +0000258 AsmPrinterVariant, *AsmInfo, *MRI, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000259 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000260 errs() << "error: no instruction printer for target " << TripleName
261 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000262 return;
263 }
264
Michael J. Spencer25b15772011-06-25 17:55:23 +0000265 StringRef Bytes;
266 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000267 StringRefMemoryObject memoryObject(Bytes);
268 uint64_t Size;
269 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000270 uint64_t SectSize;
271 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000272
Michael J. Spencer942eb002011-10-13 22:17:18 +0000273 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
274 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000275 // Disassemble symbol by symbol.
276 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
277 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000278 uint64_t End;
279 // The end is either the size of the section or the beginning of the next
280 // symbol.
281 if (si == se - 1)
282 End = SectSize;
283 // Make sure this symbol takes up space.
284 else if (Symbols[si + 1].first != Start)
285 End = Symbols[si + 1].first - 1;
286 else
287 // This symbol has the same address as the next symbol. Skip it.
288 continue;
289
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000290 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000291
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000292#ifndef NDEBUG
293 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
294#else
295 raw_ostream &DebugOut = nulls();
296#endif
297
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000298 for (Index = Start; Index < End; Index += Size) {
299 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000300
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000301 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
302 DebugOut, nulls())) {
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000303 outs() << format("%8"PRIx64":\t", SectionAddr + Index);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000304 DumpBytes(StringRef(Bytes.data() + Index, Size));
305 IP->printInst(&Inst, outs(), "");
306 outs() << "\n";
307 } else {
308 errs() << ToolName << ": warning: invalid instruction encoding\n";
309 if (Size == 0)
310 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000311 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000312
313 // Print relocation for instruction.
314 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000315 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000316 uint64_t addr;
317 SmallString<16> name;
318 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000319
320 // If this relocation is hidden, skip it.
321 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
322 if (hidden) goto skip_print_rel;
323
Michael J. Spencer942eb002011-10-13 22:17:18 +0000324 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
325 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000326 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000327 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
328 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
329
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000330 outs() << format("\t\t\t%8"PRIx64": ", SectionAddr + addr) << name << "\t"
Michael J. Spencer942eb002011-10-13 22:17:18 +0000331 << val << "\n";
332
333 skip_print_rel:
334 ++rel_cur;
335 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000336 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000337 }
338 }
339}
340
Michael J. Spencer27781b72011-10-08 00:18:30 +0000341static void PrintRelocations(const ObjectFile *o) {
342 error_code ec;
343 for (section_iterator si = o->begin_sections(), se = o->end_sections();
344 si != se; si.increment(ec)){
345 if (error(ec)) return;
346 if (si->begin_relocations() == si->end_relocations())
347 continue;
348 StringRef secname;
349 if (error(si->getName(secname))) continue;
350 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
351 for (relocation_iterator ri = si->begin_relocations(),
352 re = si->end_relocations();
353 ri != re; ri.increment(ec)) {
354 if (error(ec)) return;
355
Owen Anderson0685e942011-10-25 20:35:53 +0000356 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000357 uint64_t address;
358 SmallString<32> relocname;
359 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000360 if (error(ri->getHidden(hidden))) continue;
361 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000362 if (error(ri->getTypeName(relocname))) continue;
363 if (error(ri->getAddress(address))) continue;
364 if (error(ri->getValueString(valuestr))) continue;
365 outs() << address << " " << relocname << " " << valuestr << "\n";
366 }
367 outs() << "\n";
368 }
369}
370
Nick Lewycky023bb152011-10-10 21:21:34 +0000371static void PrintSectionHeaders(const ObjectFile *o) {
372 outs() << "Sections:\n"
373 "Idx Name Size Address Type\n";
374 error_code ec;
375 unsigned i = 0;
376 for (section_iterator si = o->begin_sections(), se = o->end_sections();
377 si != se; si.increment(ec)) {
378 if (error(ec)) return;
379 StringRef Name;
380 if (error(si->getName(Name))) return;
381 uint64_t Address;
382 if (error(si->getAddress(Address))) return;
383 uint64_t Size;
384 if (error(si->getSize(Size))) return;
385 bool Text, Data, BSS;
386 if (error(si->isText(Text))) return;
387 if (error(si->isData(Data))) return;
388 if (error(si->isBSS(BSS))) return;
389 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000390 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
NAKAMURA Takumif048c3f2011-10-11 12:51:50 +0000391 outs() << format("%3d %-13s %09"PRIx64" %017"PRIx64" %s\n", i, Name.str().c_str(), Size,
Nick Lewycky023bb152011-10-10 21:21:34 +0000392 Address, Type.c_str());
393 ++i;
394 }
395}
396
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000397static void PrintSectionContents(const ObjectFile *o) {
398 error_code ec;
399 for (section_iterator si = o->begin_sections(),
400 se = o->end_sections();
401 si != se; si.increment(ec)) {
402 if (error(ec)) return;
403 StringRef Name;
404 StringRef Contents;
405 uint64_t BaseAddr;
406 if (error(si->getName(Name))) continue;
407 if (error(si->getContents(Contents))) continue;
408 if (error(si->getAddress(BaseAddr))) continue;
409
410 outs() << "Contents of section " << Name << ":\n";
411
412 // Dump out the content as hex and printable ascii characters.
413 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000414 outs() << format(" %04"PRIx64" ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000415 // Dump line of hex.
416 for (std::size_t i = 0; i < 16; ++i) {
417 if (i != 0 && i % 4 == 0)
418 outs() << ' ';
419 if (addr + i < end)
420 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
421 << hexdigit(Contents[addr + i] & 0xF, true);
422 else
423 outs() << " ";
424 }
425 // Print ascii.
426 outs() << " ";
427 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
428 if (std::isprint(Contents[addr + i] & 0xFF))
429 outs() << Contents[addr + i];
430 else
431 outs() << ".";
432 }
433 outs() << "\n";
434 }
435 }
436}
437
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000438static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
439 const coff_file_header *header;
440 if (error(coff->getHeader(header))) return;
441 int aux_count = 0;
442 const coff_symbol *symbol = 0;
443 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
444 if (aux_count--) {
445 // Figure out which type of aux this is.
446 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
447 && symbol->Value == 0) { // Section definition.
448 const coff_aux_section_definition *asd;
449 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
450 return;
451 outs() << "AUX "
452 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
453 , unsigned(asd->Length)
454 , unsigned(asd->NumberOfRelocations)
455 , unsigned(asd->NumberOfLinenumbers)
456 , unsigned(asd->CheckSum))
457 << format("assoc %d comdat %d\n"
458 , unsigned(asd->Number)
459 , unsigned(asd->Selection));
460 } else {
461 outs() << "AUX Unknown\n";
462 }
463 } else {
464 StringRef name;
465 if (error(coff->getSymbol(i, symbol))) return;
466 if (error(coff->getSymbolName(symbol, name))) return;
467 outs() << "[" << format("%2d", i) << "]"
468 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
469 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
470 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
471 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
472 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
473 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
474 << name << "\n";
475 aux_count = symbol->NumberOfAuxSymbols;
476 }
477 }
478}
479
480static void PrintSymbolTable(const ObjectFile *o) {
481 outs() << "SYMBOL TABLE:\n";
482
483 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
484 PrintCOFFSymbolTable(coff);
485 else {
486 error_code ec;
487 for (symbol_iterator si = o->begin_symbols(),
488 se = o->end_symbols(); si != se; si.increment(ec)) {
489 if (error(ec)) return;
490 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000491 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000492 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000493 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000494 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000495 section_iterator Section = o->end_sections();
496 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000497 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000498 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000499 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000500 if (error(si->getSize(Size))) continue;
501 if (error(si->getSection(Section))) continue;
502
David Meyerc46255a2012-02-28 23:47:53 +0000503 bool Global = Flags & SymbolRef::SF_Global;
504 bool Weak = Flags & SymbolRef::SF_Weak;
505 bool Absolute = Flags & SymbolRef::SF_Absolute;
506
Danil Malyshevb0436a72011-11-29 17:40:10 +0000507 if (Address == UnknownAddressOrSize)
508 Address = 0;
509 if (Size == UnknownAddressOrSize)
510 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000511 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000512 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000513 GlobLoc = Global ? 'g' : 'l';
514 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
515 ? 'd' : ' ';
516 char FileFunc = ' ';
517 if (Type == SymbolRef::ST_File)
518 FileFunc = 'f';
519 else if (Type == SymbolRef::ST_Function)
520 FileFunc = 'F';
521
Danil Malyshevb0436a72011-11-29 17:40:10 +0000522 outs() << format("%08"PRIx64, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000523 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
524 << (Weak ? 'w' : ' ') // Weak?
525 << ' ' // Constructor. Not supported yet.
526 << ' ' // Warning. Not supported yet.
527 << ' ' // Indirect reference to another symbol.
528 << Debug // Debugging (d) or dynamic (D) symbol.
529 << FileFunc // Name of function (F), file (f) or object (O).
530 << ' ';
531 if (Absolute)
532 outs() << "*ABS*";
533 else if (Section == o->end_sections())
534 outs() << "*UND*";
535 else {
536 StringRef SectionName;
537 if (error(Section->getName(SectionName)))
538 SectionName = "";
539 outs() << SectionName;
540 }
541 outs() << '\t'
Stepan Dyatkovskiy9df3b912011-10-28 13:07:32 +0000542 << format("%08"PRIx64" ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000543 << Name
544 << '\n';
545 }
546 }
547}
548
Michael J. Spencer27781b72011-10-08 00:18:30 +0000549static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000550 outs() << '\n';
551 outs() << o->getFileName()
552 << ":\tfile format " << o->getFileFormatName() << "\n\n";
553
Michael J. Spencer27781b72011-10-08 00:18:30 +0000554 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000555 DisassembleObject(o, Relocations);
556 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000557 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000558 if (SectionHeaders)
559 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000560 if (SectionContents)
561 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000562 if (SymbolTable)
563 PrintSymbolTable(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000564}
565
566/// @brief Dump each object file in \a a;
567static void DumpArchive(const Archive *a) {
568 for (Archive::child_iterator i = a->begin_children(),
569 e = a->end_children(); i != e; ++i) {
570 OwningPtr<Binary> child;
571 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000572 // Ignore non-object files.
573 if (ec != object_error::invalid_file_type)
574 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
575 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000576 continue;
577 }
578 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
579 DumpObject(o);
580 else
581 errs() << ToolName << ": '" << a->getFileName() << "': "
582 << "Unrecognized file type.\n";
583 }
584}
585
586/// @brief Open file and figure out how to dump it.
587static void DumpInput(StringRef file) {
588 // If file isn't stdin, check that it exists.
589 if (file != "-" && !sys::fs::exists(file)) {
590 errs() << ToolName << ": '" << file << "': " << "No such file\n";
591 return;
592 }
593
594 if (MachO && Disassemble) {
595 DisassembleInputMachO(file);
596 return;
597 }
598
599 // Attempt to open the binary.
600 OwningPtr<Binary> binary;
601 if (error_code ec = createBinary(file, binary)) {
602 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
603 return;
604 }
605
606 if (Archive *a = dyn_cast<Archive>(binary.get())) {
607 DumpArchive(a);
608 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
609 DumpObject(o);
610 } else {
611 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
612 }
613}
614
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000615int main(int argc, char **argv) {
616 // Print a stack trace if we signal out.
617 sys::PrintStackTraceOnErrorSignal();
618 PrettyStackTraceProgram X(argc, argv);
619 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
620
621 // Initialize targets and assembly printers/parsers.
622 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000623 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000624 llvm::InitializeAllAsmParsers();
625 llvm::InitializeAllDisassemblers();
626
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000627 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
628 TripleName = Triple::normalize(TripleName);
629
630 ToolName = argv[0];
631
632 // Defaults to a.out if no filenames specified.
633 if (InputFilenames.size() == 0)
634 InputFilenames.push_back("a.out");
635
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000636 if (!Disassemble
637 && !Relocations
638 && !SectionHeaders
639 && !SectionContents
640 && !SymbolTable) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000641 cl::PrintHelpMessage();
642 return 2;
643 }
644
Michael J. Spencer27781b72011-10-08 00:18:30 +0000645 std::for_each(InputFilenames.begin(), InputFilenames.end(),
646 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000647
648 return 0;
649}