blob: 387f0567791cab17ab7df5c7b5389e888a586013 [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>
Benjamin Kramer81bbdfd2012-03-23 11:49:32 +000049#include <cctype>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000050#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000051using namespace llvm;
52using namespace object;
53
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000054static cl::list<std::string>
55InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000056
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000057static cl::opt<bool>
58Disassemble("disassemble",
59 cl::desc("Display assembler mnemonics for the machine instructions"));
60static cl::alias
61Disassembled("d", cl::desc("Alias for --disassemble"),
62 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000063
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000064static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000065Relocations("r", cl::desc("Display the relocation entries in the file"));
66
67static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000068SectionContents("s", cl::desc("Display the content of each section"));
69
70static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000071SymbolTable("t", cl::desc("Display the symbol table"));
72
73static cl::opt<bool>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000074MachO("macho", cl::desc("Use MachO specific object file parser"));
75static cl::alias
76MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000077
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000078cl::opt<std::string>
79llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
80 "see -version for available targets"));
81
82cl::opt<std::string>
83llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000084 "see -version for available targets"));
85
Nick Lewycky023bb152011-10-10 21:21:34 +000086static cl::opt<bool>
87SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
88 "for each section."));
89static cl::alias
90SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
91 cl::aliasopt(SectionHeaders));
92static cl::alias
93SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
94 cl::aliasopt(SectionHeaders));
95
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000096static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000097
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000098static bool error(error_code ec) {
99 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000100
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000101 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
102 outs().flush();
103 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000104}
105
106static const Target *GetTarget(const ObjectFile *Obj = NULL) {
107 // Figure out the target triple.
108 llvm::Triple TT("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000109 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000110 if (Obj)
111 TT.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000112 } else
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000113 TT.setTriple(Triple::normalize(TripleName));
114
115 if (!ArchName.empty())
116 TT.setArchName(ArchName);
117
118 TripleName = TT.str();
119
120 // Get the target specific parser.
121 std::string Error;
122 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
123 if (TheTarget)
124 return TheTarget;
125
126 errs() << ToolName << ": error: unable to get target for '" << TripleName
127 << "', see --version and --triple.\n";
128 return 0;
129}
130
David Blaikie2d24e2a2011-12-20 02:50:00 +0000131void llvm::StringRefMemoryObject::anchor() { }
132
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000133void llvm::DumpBytes(StringRef bytes) {
134 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000135 // FIXME: The real way to do this is to figure out the longest instruction
136 // and align to that size before printing. I'll fix this when I get
137 // around to outputting relocations.
138 // 15 is the longest x86 instruction
139 // 3 is for the hex rep of a byte + a space.
140 // 1 is for the null terminator.
141 enum { OutputSize = (15 * 3) + 1 };
142 char output[OutputSize];
143
144 assert(bytes.size() <= 15
145 && "DumpBytes only supports instructions of up to 15 bytes");
146 memset(output, ' ', sizeof(output));
147 unsigned index = 0;
148 for (StringRef::iterator i = bytes.begin(),
149 e = bytes.end(); i != e; ++i) {
150 output[index] = hex_rep[(*i & 0xF0) >> 4];
151 output[index + 1] = hex_rep[*i & 0xF];
152 index += 3;
153 }
154
155 output[sizeof(output) - 1] = 0;
156 outs() << output;
157}
158
Michael J. Spencer942eb002011-10-13 22:17:18 +0000159static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
160 uint64_t a_addr, b_addr;
161 if (error(a.getAddress(a_addr))) return false;
162 if (error(b.getAddress(b_addr))) return false;
163 return a_addr < b_addr;
164}
165
166static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000167 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000168 if (!TheTarget) {
169 // GetTarget prints out stuff.
170 return;
171 }
172
Michael J. Spencer25b15772011-06-25 17:55:23 +0000173 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000174 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000175 e = Obj->end_sections();
176 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000177 if (error(ec)) break;
178 bool text;
179 if (error(i->isText(text))) break;
180 if (!text) continue;
181
Michael J. Spencer942eb002011-10-13 22:17:18 +0000182 uint64_t SectionAddr;
183 if (error(i->getAddress(SectionAddr))) break;
184
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000185 // Make a list of all the symbols in this section.
186 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000187 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000188 se = Obj->end_symbols();
189 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000190 bool contains;
191 if (!error(i->containsSymbol(*si, contains)) && contains) {
192 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000193 if (error(si->getAddress(Address))) break;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000194 Address -= SectionAddr;
195
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000196 StringRef Name;
197 if (error(si->getName(Name))) break;
198 Symbols.push_back(std::make_pair(Address, Name));
199 }
200 }
201
202 // Sort the symbols by address, just in case they didn't come in that way.
203 array_pod_sort(Symbols.begin(), Symbols.end());
204
Michael J. Spencer942eb002011-10-13 22:17:18 +0000205 // Make a list of all the relocations for this section.
206 std::vector<RelocationRef> Rels;
207 if (InlineRelocs) {
208 for (relocation_iterator ri = i->begin_relocations(),
209 re = i->end_relocations();
210 ri != re; ri.increment(ec)) {
211 if (error(ec)) break;
212 Rels.push_back(*ri);
213 }
214 }
215
216 // Sort relocations by address.
217 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
218
Michael J. Spencer25b15772011-06-25 17:55:23 +0000219 StringRef name;
220 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000221 outs() << "Disassembly of section " << name << ':';
222
223 // If the section has no symbols just insert a dummy one and disassemble
224 // the whole section.
225 if (Symbols.empty())
226 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000227
228 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000229 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000230
231 if (!AsmInfo) {
232 errs() << "error: no assembly info for target " << TripleName << "\n";
233 return;
234 }
235
Michael J. Spencer27781b72011-10-08 00:18:30 +0000236 OwningPtr<const MCSubtargetInfo> STI(
237 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000238
239 if (!STI) {
240 errs() << "error: no subtarget info for target " << TripleName << "\n";
241 return;
242 }
243
Owen Anderson10c044e2011-10-27 21:55:13 +0000244 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000245 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000246 if (!DisAsm) {
247 errs() << "error: no disassembler for target " << TripleName << "\n";
248 return;
249 }
250
Jim Grosbachc6449b62012-03-05 19:33:20 +0000251 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
252 if (!MRI) {
253 errs() << "error: no register info for target " << TripleName << "\n";
254 return;
255 }
256
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000257 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
258 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Jim Grosbachc6449b62012-03-05 19:33:20 +0000259 AsmPrinterVariant, *AsmInfo, *MRI, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000260 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000261 errs() << "error: no instruction printer for target " << TripleName
262 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000263 return;
264 }
265
Michael J. Spencer25b15772011-06-25 17:55:23 +0000266 StringRef Bytes;
267 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000268 StringRefMemoryObject memoryObject(Bytes);
269 uint64_t Size;
270 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000271 uint64_t SectSize;
272 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000273
Michael J. Spencer942eb002011-10-13 22:17:18 +0000274 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
275 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000276 // Disassemble symbol by symbol.
277 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
278 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000279 uint64_t End;
280 // The end is either the size of the section or the beginning of the next
281 // symbol.
282 if (si == se - 1)
283 End = SectSize;
284 // Make sure this symbol takes up space.
285 else if (Symbols[si + 1].first != Start)
286 End = Symbols[si + 1].first - 1;
287 else
288 // This symbol has the same address as the next symbol. Skip it.
289 continue;
290
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000291 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000292
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000293#ifndef NDEBUG
294 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
295#else
296 raw_ostream &DebugOut = nulls();
297#endif
298
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000299 for (Index = Start; Index < End; Index += Size) {
300 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000301
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000302 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
303 DebugOut, nulls())) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000304 outs() << format("%8" PRIx64 ":\t", SectionAddr + Index);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000305 DumpBytes(StringRef(Bytes.data() + Index, Size));
306 IP->printInst(&Inst, outs(), "");
307 outs() << "\n";
308 } else {
309 errs() << ToolName << ": warning: invalid instruction encoding\n";
310 if (Size == 0)
311 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000312 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000313
314 // Print relocation for instruction.
315 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000316 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000317 uint64_t addr;
318 SmallString<16> name;
319 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000320
321 // If this relocation is hidden, skip it.
322 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
323 if (hidden) goto skip_print_rel;
324
Michael J. Spencer942eb002011-10-13 22:17:18 +0000325 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
326 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000327 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000328 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
329 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
330
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000331 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
332 << "\t" << val << "\n";
Michael J. Spencer942eb002011-10-13 22:17:18 +0000333
334 skip_print_rel:
335 ++rel_cur;
336 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000337 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000338 }
339 }
340}
341
Michael J. Spencer27781b72011-10-08 00:18:30 +0000342static void PrintRelocations(const ObjectFile *o) {
343 error_code ec;
344 for (section_iterator si = o->begin_sections(), se = o->end_sections();
345 si != se; si.increment(ec)){
346 if (error(ec)) return;
347 if (si->begin_relocations() == si->end_relocations())
348 continue;
349 StringRef secname;
350 if (error(si->getName(secname))) continue;
351 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
352 for (relocation_iterator ri = si->begin_relocations(),
353 re = si->end_relocations();
354 ri != re; ri.increment(ec)) {
355 if (error(ec)) return;
356
Owen Anderson0685e942011-10-25 20:35:53 +0000357 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000358 uint64_t address;
359 SmallString<32> relocname;
360 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000361 if (error(ri->getHidden(hidden))) continue;
362 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000363 if (error(ri->getTypeName(relocname))) continue;
364 if (error(ri->getAddress(address))) continue;
365 if (error(ri->getValueString(valuestr))) continue;
366 outs() << address << " " << relocname << " " << valuestr << "\n";
367 }
368 outs() << "\n";
369 }
370}
371
Nick Lewycky023bb152011-10-10 21:21:34 +0000372static void PrintSectionHeaders(const ObjectFile *o) {
373 outs() << "Sections:\n"
374 "Idx Name Size Address Type\n";
375 error_code ec;
376 unsigned i = 0;
377 for (section_iterator si = o->begin_sections(), se = o->end_sections();
378 si != se; si.increment(ec)) {
379 if (error(ec)) return;
380 StringRef Name;
381 if (error(si->getName(Name))) return;
382 uint64_t Address;
383 if (error(si->getAddress(Address))) return;
384 uint64_t Size;
385 if (error(si->getSize(Size))) return;
386 bool Text, Data, BSS;
387 if (error(si->isText(Text))) return;
388 if (error(si->isData(Data))) return;
389 if (error(si->isBSS(BSS))) return;
390 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000391 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000392 outs() << format("%3d %-13s %09" PRIx64 " %017" PRIx64 " %s\n",
393 i, Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewycky023bb152011-10-10 21:21:34 +0000394 ++i;
395 }
396}
397
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000398static void PrintSectionContents(const ObjectFile *o) {
399 error_code ec;
400 for (section_iterator si = o->begin_sections(),
401 se = o->end_sections();
402 si != se; si.increment(ec)) {
403 if (error(ec)) return;
404 StringRef Name;
405 StringRef Contents;
406 uint64_t BaseAddr;
407 if (error(si->getName(Name))) continue;
408 if (error(si->getContents(Contents))) continue;
409 if (error(si->getAddress(BaseAddr))) continue;
410
411 outs() << "Contents of section " << Name << ":\n";
412
413 // Dump out the content as hex and printable ascii characters.
414 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000415 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000416 // Dump line of hex.
417 for (std::size_t i = 0; i < 16; ++i) {
418 if (i != 0 && i % 4 == 0)
419 outs() << ' ';
420 if (addr + i < end)
421 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
422 << hexdigit(Contents[addr + i] & 0xF, true);
423 else
424 outs() << " ";
425 }
426 // Print ascii.
427 outs() << " ";
428 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
429 if (std::isprint(Contents[addr + i] & 0xFF))
430 outs() << Contents[addr + i];
431 else
432 outs() << ".";
433 }
434 outs() << "\n";
435 }
436 }
437}
438
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000439static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
440 const coff_file_header *header;
441 if (error(coff->getHeader(header))) return;
442 int aux_count = 0;
443 const coff_symbol *symbol = 0;
444 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
445 if (aux_count--) {
446 // Figure out which type of aux this is.
447 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
448 && symbol->Value == 0) { // Section definition.
449 const coff_aux_section_definition *asd;
450 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
451 return;
452 outs() << "AUX "
453 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
454 , unsigned(asd->Length)
455 , unsigned(asd->NumberOfRelocations)
456 , unsigned(asd->NumberOfLinenumbers)
457 , unsigned(asd->CheckSum))
458 << format("assoc %d comdat %d\n"
459 , unsigned(asd->Number)
460 , unsigned(asd->Selection));
461 } else {
462 outs() << "AUX Unknown\n";
463 }
464 } else {
465 StringRef name;
466 if (error(coff->getSymbol(i, symbol))) return;
467 if (error(coff->getSymbolName(symbol, name))) return;
468 outs() << "[" << format("%2d", i) << "]"
469 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
470 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
471 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
472 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
473 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
474 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
475 << name << "\n";
476 aux_count = symbol->NumberOfAuxSymbols;
477 }
478 }
479}
480
481static void PrintSymbolTable(const ObjectFile *o) {
482 outs() << "SYMBOL TABLE:\n";
483
484 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
485 PrintCOFFSymbolTable(coff);
486 else {
487 error_code ec;
488 for (symbol_iterator si = o->begin_symbols(),
489 se = o->end_symbols(); si != se; si.increment(ec)) {
490 if (error(ec)) return;
491 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000492 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000493 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000494 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000495 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000496 section_iterator Section = o->end_sections();
497 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000498 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000499 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000500 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000501 if (error(si->getSize(Size))) continue;
502 if (error(si->getSection(Section))) continue;
503
David Meyerc46255a2012-02-28 23:47:53 +0000504 bool Global = Flags & SymbolRef::SF_Global;
505 bool Weak = Flags & SymbolRef::SF_Weak;
506 bool Absolute = Flags & SymbolRef::SF_Absolute;
507
Danil Malyshevb0436a72011-11-29 17:40:10 +0000508 if (Address == UnknownAddressOrSize)
509 Address = 0;
510 if (Size == UnknownAddressOrSize)
511 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000512 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000513 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000514 GlobLoc = Global ? 'g' : 'l';
515 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
516 ? 'd' : ' ';
517 char FileFunc = ' ';
518 if (Type == SymbolRef::ST_File)
519 FileFunc = 'f';
520 else if (Type == SymbolRef::ST_Function)
521 FileFunc = 'F';
522
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000523 outs() << format("%08" PRIx64, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000524 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
525 << (Weak ? 'w' : ' ') // Weak?
526 << ' ' // Constructor. Not supported yet.
527 << ' ' // Warning. Not supported yet.
528 << ' ' // Indirect reference to another symbol.
529 << Debug // Debugging (d) or dynamic (D) symbol.
530 << FileFunc // Name of function (F), file (f) or object (O).
531 << ' ';
532 if (Absolute)
533 outs() << "*ABS*";
534 else if (Section == o->end_sections())
535 outs() << "*UND*";
536 else {
537 StringRef SectionName;
538 if (error(Section->getName(SectionName)))
539 SectionName = "";
540 outs() << SectionName;
541 }
542 outs() << '\t'
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000543 << format("%08" PRIx64 " ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000544 << Name
545 << '\n';
546 }
547 }
548}
549
Michael J. Spencer27781b72011-10-08 00:18:30 +0000550static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000551 outs() << '\n';
552 outs() << o->getFileName()
553 << ":\tfile format " << o->getFileFormatName() << "\n\n";
554
Michael J. Spencer27781b72011-10-08 00:18:30 +0000555 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000556 DisassembleObject(o, Relocations);
557 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000558 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000559 if (SectionHeaders)
560 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000561 if (SectionContents)
562 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000563 if (SymbolTable)
564 PrintSymbolTable(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000565}
566
567/// @brief Dump each object file in \a a;
568static void DumpArchive(const Archive *a) {
569 for (Archive::child_iterator i = a->begin_children(),
570 e = a->end_children(); i != e; ++i) {
571 OwningPtr<Binary> child;
572 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000573 // Ignore non-object files.
574 if (ec != object_error::invalid_file_type)
575 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
576 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000577 continue;
578 }
579 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
580 DumpObject(o);
581 else
582 errs() << ToolName << ": '" << a->getFileName() << "': "
583 << "Unrecognized file type.\n";
584 }
585}
586
587/// @brief Open file and figure out how to dump it.
588static void DumpInput(StringRef file) {
589 // If file isn't stdin, check that it exists.
590 if (file != "-" && !sys::fs::exists(file)) {
591 errs() << ToolName << ": '" << file << "': " << "No such file\n";
592 return;
593 }
594
595 if (MachO && Disassemble) {
596 DisassembleInputMachO(file);
597 return;
598 }
599
600 // Attempt to open the binary.
601 OwningPtr<Binary> binary;
602 if (error_code ec = createBinary(file, binary)) {
603 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
604 return;
605 }
606
607 if (Archive *a = dyn_cast<Archive>(binary.get())) {
608 DumpArchive(a);
609 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
610 DumpObject(o);
611 } else {
612 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
613 }
614}
615
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000616int main(int argc, char **argv) {
617 // Print a stack trace if we signal out.
618 sys::PrintStackTraceOnErrorSignal();
619 PrettyStackTraceProgram X(argc, argv);
620 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
621
622 // Initialize targets and assembly printers/parsers.
623 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000624 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000625 llvm::InitializeAllAsmParsers();
626 llvm::InitializeAllDisassemblers();
627
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000628 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
629 TripleName = Triple::normalize(TripleName);
630
631 ToolName = argv[0];
632
633 // Defaults to a.out if no filenames specified.
634 if (InputFilenames.size() == 0)
635 InputFilenames.push_back("a.out");
636
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000637 if (!Disassemble
638 && !Relocations
639 && !SectionHeaders
640 && !SectionContents
641 && !SymbolTable) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000642 cl::PrintHelpMessage();
643 return 2;
644 }
645
Michael J. Spencer27781b72011-10-08 00:18:30 +0000646 std::for_each(InputFilenames.begin(), InputFilenames.end(),
647 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000648
649 return 0;
650}