blob: 24b586cdf133e220c2cb4062b22afa960c707c65 [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. Spencer92e1deb2011-01-20 06:39:06 +000018#include "llvm/ADT/OwningPtr.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000019#include "llvm/ADT/STLExtras.h"
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000020#include "llvm/ADT/StringExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000021#include "llvm/ADT/Triple.h"
22#include "llvm/MC/MCAsmInfo.h"
23#include "llvm/MC/MCDisassembler.h"
24#include "llvm/MC/MCInst.h"
25#include "llvm/MC/MCInstPrinter.h"
Craig Topper17463b32012-04-02 06:09:36 +000026#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachc6449b62012-03-05 19:33:20 +000027#include "llvm/MC/MCRegisterInfo.h"
James Molloyb9505852011-09-07 17:24:38 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000029#include "llvm/Object/Archive.h"
30#include "llvm/Object/COFF.h"
Rafael Espindolacef81b32012-12-21 03:47:03 +000031#include "llvm/Object/MachO.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000032#include "llvm/Object/ObjectFile.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000033#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000036#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000037#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000038#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000039#include "llvm/Support/Host.h"
40#include "llvm/Support/ManagedStatic.h"
41#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000042#include "llvm/Support/MemoryObject.h"
43#include "llvm/Support/PrettyStackTrace.h"
44#include "llvm/Support/Signals.h"
45#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000046#include "llvm/Support/TargetRegistry.h"
47#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000050#include <algorithm>
Benjamin Kramer81bbdfd2012-03-23 11:49:32 +000051#include <cctype>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000052#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000053using namespace llvm;
54using namespace object;
55
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000056static cl::list<std::string>
57InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000058
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000059static cl::opt<bool>
60Disassemble("disassemble",
61 cl::desc("Display assembler mnemonics for the machine instructions"));
62static cl::alias
63Disassembled("d", cl::desc("Alias for --disassemble"),
64 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000065
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000066static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000067Relocations("r", cl::desc("Display the relocation entries in the file"));
68
69static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000070SectionContents("s", cl::desc("Display the content of each section"));
71
72static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000073SymbolTable("t", cl::desc("Display the symbol table"));
74
75static cl::opt<bool>
Rafael Espindolacef81b32012-12-21 03:47:03 +000076MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000077static cl::alias
Rafael Espindolacef81b32012-12-21 03:47:03 +000078MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer685a2502011-07-20 19:37:35 +000079
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000080cl::opt<std::string>
81llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
82 "see -version for available targets"));
83
84cl::opt<std::string>
85llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000086 "see -version for available targets"));
87
Nick Lewycky023bb152011-10-10 21:21:34 +000088static cl::opt<bool>
89SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
90 "for each section."));
91static cl::alias
92SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
93 cl::aliasopt(SectionHeaders));
94static cl::alias
95SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
96 cl::aliasopt(SectionHeaders));
97
Jack Carterfd6d1652012-08-28 19:24:49 +000098static cl::list<std::string>
99MAttrs("mattr",
100 cl::CommaSeparated,
101 cl::desc("Target specific attributes"),
102 cl::value_desc("a1,+a2,-a3,..."));
103
Eli Bendersky8b9da532012-11-20 22:57:02 +0000104static cl::opt<bool>
105NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, "
106 "do not print the instruction bytes."));
107
Michael J. Spencereef7b622012-12-05 20:12:35 +0000108static cl::opt<bool>
109UnwindInfo("unwind-info", cl::desc("Display unwind information"));
110
111static cl::alias
112UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
113 cl::aliasopt(UnwindInfo));
114
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000115static cl::opt<bool>
116PrivateHeaders("private-headers",
117 cl::desc("Display format specific file headers"));
118
119static cl::alias
120PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
121 cl::aliasopt(PrivateHeaders));
122
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000123static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000124
Michael J. Spencereef7b622012-12-05 20:12:35 +0000125bool llvm::error(error_code ec) {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000126 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000127
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000128 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
129 outs().flush();
130 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000131}
132
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000133static const Target *getTarget(const ObjectFile *Obj = NULL) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000134 // Figure out the target triple.
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000135 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000136 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000137 if (Obj)
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000138 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000139 } else
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000140 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000141
142 // Get the target specific parser.
143 std::string Error;
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000144 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
145 Error);
146 if (!TheTarget) {
147 errs() << ToolName << ": " << Error;
148 return 0;
149 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000150
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000151 // Update the triple name and return the found target.
152 TripleName = TheTriple.getTriple();
153 return TheTarget;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000154}
155
David Blaikie2d24e2a2011-12-20 02:50:00 +0000156void llvm::StringRefMemoryObject::anchor() { }
157
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000158void llvm::DumpBytes(StringRef bytes) {
159 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000160 // FIXME: The real way to do this is to figure out the longest instruction
161 // and align to that size before printing. I'll fix this when I get
162 // around to outputting relocations.
163 // 15 is the longest x86 instruction
164 // 3 is for the hex rep of a byte + a space.
165 // 1 is for the null terminator.
166 enum { OutputSize = (15 * 3) + 1 };
167 char output[OutputSize];
168
169 assert(bytes.size() <= 15
170 && "DumpBytes only supports instructions of up to 15 bytes");
171 memset(output, ' ', sizeof(output));
172 unsigned index = 0;
173 for (StringRef::iterator i = bytes.begin(),
174 e = bytes.end(); i != e; ++i) {
175 output[index] = hex_rep[(*i & 0xF0) >> 4];
176 output[index + 1] = hex_rep[*i & 0xF];
177 index += 3;
178 }
179
180 output[sizeof(output) - 1] = 0;
181 outs() << output;
182}
183
Michael J. Spencereef7b622012-12-05 20:12:35 +0000184bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000185 uint64_t a_addr, b_addr;
186 if (error(a.getAddress(a_addr))) return false;
187 if (error(b.getAddress(b_addr))) return false;
188 return a_addr < b_addr;
189}
190
191static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000192 const Target *TheTarget = getTarget(Obj);
193 // getTarget() will have already issued a diagnostic if necessary, so
194 // just bail here if it failed.
195 if (!TheTarget)
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000196 return;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000197
Jack Carterfd6d1652012-08-28 19:24:49 +0000198 // Package up features to be passed to target/subtarget
199 std::string FeaturesStr;
200 if (MAttrs.size()) {
201 SubtargetFeatures Features;
202 for (unsigned i = 0; i != MAttrs.size(); ++i)
203 Features.AddFeature(MAttrs[i]);
204 FeaturesStr = Features.getString();
205 }
206
Michael J. Spencer25b15772011-06-25 17:55:23 +0000207 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000208 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000209 e = Obj->end_sections();
210 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000211 if (error(ec)) break;
212 bool text;
213 if (error(i->isText(text))) break;
214 if (!text) continue;
215
Michael J. Spencer942eb002011-10-13 22:17:18 +0000216 uint64_t SectionAddr;
217 if (error(i->getAddress(SectionAddr))) break;
218
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000219 // Make a list of all the symbols in this section.
220 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000221 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000222 se = Obj->end_symbols();
223 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000224 bool contains;
225 if (!error(i->containsSymbol(*si, contains)) && contains) {
226 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000227 if (error(si->getAddress(Address))) break;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000228 Address -= SectionAddr;
229
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000230 StringRef Name;
231 if (error(si->getName(Name))) break;
232 Symbols.push_back(std::make_pair(Address, Name));
233 }
234 }
235
236 // Sort the symbols by address, just in case they didn't come in that way.
237 array_pod_sort(Symbols.begin(), Symbols.end());
238
Michael J. Spencer942eb002011-10-13 22:17:18 +0000239 // Make a list of all the relocations for this section.
240 std::vector<RelocationRef> Rels;
241 if (InlineRelocs) {
242 for (relocation_iterator ri = i->begin_relocations(),
243 re = i->end_relocations();
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000244 ri != re; ri.increment(ec)) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000245 if (error(ec)) break;
246 Rels.push_back(*ri);
247 }
248 }
249
250 // Sort relocations by address.
251 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
252
Rafael Espindolacef81b32012-12-21 03:47:03 +0000253 StringRef SegmentName = "";
254 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
255 DataRefImpl DR = i->getRawDataRefImpl();
256 if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
257 break;
258 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000259 StringRef name;
260 if (error(i->getName(name))) break;
Rafael Espindolacef81b32012-12-21 03:47:03 +0000261 outs() << "Disassembly of section ";
262 if (!SegmentName.empty())
263 outs() << SegmentName << ",";
264 outs() << name << ':';
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000265
266 // If the section has no symbols just insert a dummy one and disassemble
267 // the whole section.
268 if (Symbols.empty())
269 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000270
271 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000272 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000273
274 if (!AsmInfo) {
275 errs() << "error: no assembly info for target " << TripleName << "\n";
276 return;
277 }
278
Michael J. Spencer27781b72011-10-08 00:18:30 +0000279 OwningPtr<const MCSubtargetInfo> STI(
Jack Carterfd6d1652012-08-28 19:24:49 +0000280 TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr));
James Molloyb9505852011-09-07 17:24:38 +0000281
282 if (!STI) {
283 errs() << "error: no subtarget info for target " << TripleName << "\n";
284 return;
285 }
286
Owen Anderson10c044e2011-10-27 21:55:13 +0000287 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000288 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000289 if (!DisAsm) {
290 errs() << "error: no disassembler for target " << TripleName << "\n";
291 return;
292 }
293
Jim Grosbachc6449b62012-03-05 19:33:20 +0000294 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
295 if (!MRI) {
296 errs() << "error: no register info for target " << TripleName << "\n";
297 return;
298 }
299
Craig Topper17463b32012-04-02 06:09:36 +0000300 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
301 if (!MII) {
302 errs() << "error: no instruction info for target " << TripleName << "\n";
303 return;
304 }
305
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000306 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
307 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Craig Topper17463b32012-04-02 06:09:36 +0000308 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000309 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000310 errs() << "error: no instruction printer for target " << TripleName
311 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000312 return;
313 }
314
Michael J. Spencer25b15772011-06-25 17:55:23 +0000315 StringRef Bytes;
316 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000317 StringRefMemoryObject memoryObject(Bytes);
318 uint64_t Size;
319 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000320 uint64_t SectSize;
321 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000322
Michael J. Spencer942eb002011-10-13 22:17:18 +0000323 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
324 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000325 // Disassemble symbol by symbol.
326 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
327 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000328 uint64_t End;
329 // The end is either the size of the section or the beginning of the next
330 // symbol.
331 if (si == se - 1)
332 End = SectSize;
333 // Make sure this symbol takes up space.
334 else if (Symbols[si + 1].first != Start)
335 End = Symbols[si + 1].first - 1;
336 else
337 // This symbol has the same address as the next symbol. Skip it.
338 continue;
339
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000340 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000341
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000342#ifndef NDEBUG
343 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
344#else
345 raw_ostream &DebugOut = nulls();
346#endif
347
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000348 for (Index = Start; Index < End; Index += Size) {
349 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000350
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000351 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
352 DebugOut, nulls())) {
Eli Bendersky8b9da532012-11-20 22:57:02 +0000353 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
354 if (!NoShowRawInsn) {
355 outs() << "\t";
356 DumpBytes(StringRef(Bytes.data() + Index, Size));
357 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000358 IP->printInst(&Inst, outs(), "");
359 outs() << "\n";
360 } else {
361 errs() << ToolName << ": warning: invalid instruction encoding\n";
362 if (Size == 0)
363 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000364 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000365
366 // Print relocation for instruction.
367 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000368 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000369 uint64_t addr;
370 SmallString<16> name;
371 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000372
373 // If this relocation is hidden, skip it.
374 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
375 if (hidden) goto skip_print_rel;
376
Michael J. Spencer942eb002011-10-13 22:17:18 +0000377 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
378 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000379 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000380 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
381 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
382
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000383 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
384 << "\t" << val << "\n";
Michael J. Spencer942eb002011-10-13 22:17:18 +0000385
386 skip_print_rel:
387 ++rel_cur;
388 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000389 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000390 }
391 }
392}
393
Michael J. Spencer27781b72011-10-08 00:18:30 +0000394static void PrintRelocations(const ObjectFile *o) {
395 error_code ec;
396 for (section_iterator si = o->begin_sections(), se = o->end_sections();
397 si != se; si.increment(ec)){
398 if (error(ec)) return;
399 if (si->begin_relocations() == si->end_relocations())
400 continue;
401 StringRef secname;
402 if (error(si->getName(secname))) continue;
403 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
404 for (relocation_iterator ri = si->begin_relocations(),
405 re = si->end_relocations();
406 ri != re; ri.increment(ec)) {
407 if (error(ec)) return;
408
Owen Anderson0685e942011-10-25 20:35:53 +0000409 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000410 uint64_t address;
411 SmallString<32> relocname;
412 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000413 if (error(ri->getHidden(hidden))) continue;
414 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000415 if (error(ri->getTypeName(relocname))) continue;
416 if (error(ri->getAddress(address))) continue;
417 if (error(ri->getValueString(valuestr))) continue;
418 outs() << address << " " << relocname << " " << valuestr << "\n";
419 }
420 outs() << "\n";
421 }
422}
423
Nick Lewycky023bb152011-10-10 21:21:34 +0000424static void PrintSectionHeaders(const ObjectFile *o) {
425 outs() << "Sections:\n"
426 "Idx Name Size Address Type\n";
427 error_code ec;
428 unsigned i = 0;
429 for (section_iterator si = o->begin_sections(), se = o->end_sections();
430 si != se; si.increment(ec)) {
431 if (error(ec)) return;
432 StringRef Name;
433 if (error(si->getName(Name))) return;
434 uint64_t Address;
435 if (error(si->getAddress(Address))) return;
436 uint64_t Size;
437 if (error(si->getSize(Size))) return;
438 bool Text, Data, BSS;
439 if (error(si->isText(Text))) return;
440 if (error(si->isData(Data))) return;
441 if (error(si->isBSS(BSS))) return;
442 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000443 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Michael J. Spencer27b2b1b2013-01-10 22:40:50 +0000444 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000445 i, Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewycky023bb152011-10-10 21:21:34 +0000446 ++i;
447 }
448}
449
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000450static void PrintSectionContents(const ObjectFile *o) {
451 error_code ec;
452 for (section_iterator si = o->begin_sections(),
453 se = o->end_sections();
454 si != se; si.increment(ec)) {
455 if (error(ec)) return;
456 StringRef Name;
457 StringRef Contents;
458 uint64_t BaseAddr;
459 if (error(si->getName(Name))) continue;
460 if (error(si->getContents(Contents))) continue;
461 if (error(si->getAddress(BaseAddr))) continue;
462
463 outs() << "Contents of section " << Name << ":\n";
464
465 // Dump out the content as hex and printable ascii characters.
466 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000467 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000468 // Dump line of hex.
469 for (std::size_t i = 0; i < 16; ++i) {
470 if (i != 0 && i % 4 == 0)
471 outs() << ' ';
472 if (addr + i < end)
473 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
474 << hexdigit(Contents[addr + i] & 0xF, true);
475 else
476 outs() << " ";
477 }
478 // Print ascii.
479 outs() << " ";
480 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
481 if (std::isprint(Contents[addr + i] & 0xFF))
482 outs() << Contents[addr + i];
483 else
484 outs() << ".";
485 }
486 outs() << "\n";
487 }
488 }
489}
490
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000491static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
492 const coff_file_header *header;
493 if (error(coff->getHeader(header))) return;
494 int aux_count = 0;
495 const coff_symbol *symbol = 0;
496 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
497 if (aux_count--) {
498 // Figure out which type of aux this is.
499 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
500 && symbol->Value == 0) { // Section definition.
501 const coff_aux_section_definition *asd;
502 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
503 return;
504 outs() << "AUX "
505 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
506 , unsigned(asd->Length)
507 , unsigned(asd->NumberOfRelocations)
508 , unsigned(asd->NumberOfLinenumbers)
509 , unsigned(asd->CheckSum))
510 << format("assoc %d comdat %d\n"
511 , unsigned(asd->Number)
512 , unsigned(asd->Selection));
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000513 } else
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000514 outs() << "AUX Unknown\n";
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000515 } else {
516 StringRef name;
517 if (error(coff->getSymbol(i, symbol))) return;
518 if (error(coff->getSymbolName(symbol, name))) return;
519 outs() << "[" << format("%2d", i) << "]"
520 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
521 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
522 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
523 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
524 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
525 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
526 << name << "\n";
527 aux_count = symbol->NumberOfAuxSymbols;
528 }
529 }
530}
531
532static void PrintSymbolTable(const ObjectFile *o) {
533 outs() << "SYMBOL TABLE:\n";
534
535 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
536 PrintCOFFSymbolTable(coff);
537 else {
538 error_code ec;
539 for (symbol_iterator si = o->begin_symbols(),
540 se = o->end_symbols(); si != se; si.increment(ec)) {
541 if (error(ec)) return;
542 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000543 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000544 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000545 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000546 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000547 section_iterator Section = o->end_sections();
548 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000549 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000550 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000551 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000552 if (error(si->getSize(Size))) continue;
553 if (error(si->getSection(Section))) continue;
554
David Meyerc46255a2012-02-28 23:47:53 +0000555 bool Global = Flags & SymbolRef::SF_Global;
556 bool Weak = Flags & SymbolRef::SF_Weak;
557 bool Absolute = Flags & SymbolRef::SF_Absolute;
558
Danil Malyshevb0436a72011-11-29 17:40:10 +0000559 if (Address == UnknownAddressOrSize)
560 Address = 0;
561 if (Size == UnknownAddressOrSize)
562 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000563 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000564 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000565 GlobLoc = Global ? 'g' : 'l';
566 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
567 ? 'd' : ' ';
568 char FileFunc = ' ';
569 if (Type == SymbolRef::ST_File)
570 FileFunc = 'f';
571 else if (Type == SymbolRef::ST_Function)
572 FileFunc = 'F';
573
Michael J. Spencer27b2b1b2013-01-10 22:40:50 +0000574 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
575 "%08" PRIx64;
576
577 outs() << format(Fmt, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000578 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
579 << (Weak ? 'w' : ' ') // Weak?
580 << ' ' // Constructor. Not supported yet.
581 << ' ' // Warning. Not supported yet.
582 << ' ' // Indirect reference to another symbol.
583 << Debug // Debugging (d) or dynamic (D) symbol.
584 << FileFunc // Name of function (F), file (f) or object (O).
585 << ' ';
586 if (Absolute)
587 outs() << "*ABS*";
588 else if (Section == o->end_sections())
589 outs() << "*UND*";
590 else {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000591 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(o)) {
592 StringRef SegmentName;
593 DataRefImpl DR = Section->getRawDataRefImpl();
594 if (error(MachO->getSectionFinalSegmentName(DR, SegmentName)))
595 SegmentName = "";
596 outs() << SegmentName << ",";
597 }
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000598 StringRef SectionName;
599 if (error(Section->getName(SectionName)))
600 SectionName = "";
601 outs() << SectionName;
602 }
603 outs() << '\t'
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000604 << format("%08" PRIx64 " ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000605 << Name
606 << '\n';
607 }
608 }
609}
610
Michael J. Spencereef7b622012-12-05 20:12:35 +0000611static void PrintUnwindInfo(const ObjectFile *o) {
612 outs() << "Unwind info:\n\n";
613
614 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
615 printCOFFUnwindInfo(coff);
616 } else {
617 // TODO: Extract DWARF dump tool to objdump.
618 errs() << "This operation is only currently supported "
619 "for COFF object files.\n";
620 return;
621 }
622}
623
Michael J. Spencer27781b72011-10-08 00:18:30 +0000624static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000625 outs() << '\n';
626 outs() << o->getFileName()
627 << ":\tfile format " << o->getFileFormatName() << "\n\n";
628
Michael J. Spencer27781b72011-10-08 00:18:30 +0000629 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000630 DisassembleObject(o, Relocations);
631 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000632 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000633 if (SectionHeaders)
634 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000635 if (SectionContents)
636 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000637 if (SymbolTable)
638 PrintSymbolTable(o);
Michael J. Spencereef7b622012-12-05 20:12:35 +0000639 if (UnwindInfo)
640 PrintUnwindInfo(o);
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000641 if (PrivateHeaders && o->isELF())
642 printELFFileHeader(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000643}
644
645/// @brief Dump each object file in \a a;
646static void DumpArchive(const Archive *a) {
647 for (Archive::child_iterator i = a->begin_children(),
648 e = a->end_children(); i != e; ++i) {
649 OwningPtr<Binary> child;
650 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000651 // Ignore non-object files.
652 if (ec != object_error::invalid_file_type)
653 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
654 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000655 continue;
656 }
657 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
658 DumpObject(o);
659 else
660 errs() << ToolName << ": '" << a->getFileName() << "': "
661 << "Unrecognized file type.\n";
662 }
663}
664
665/// @brief Open file and figure out how to dump it.
666static void DumpInput(StringRef file) {
667 // If file isn't stdin, check that it exists.
668 if (file != "-" && !sys::fs::exists(file)) {
669 errs() << ToolName << ": '" << file << "': " << "No such file\n";
670 return;
671 }
672
Rafael Espindolacef81b32012-12-21 03:47:03 +0000673 if (MachOOpt && Disassemble) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000674 DisassembleInputMachO(file);
675 return;
676 }
677
678 // Attempt to open the binary.
679 OwningPtr<Binary> binary;
680 if (error_code ec = createBinary(file, binary)) {
681 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
682 return;
683 }
684
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000685 if (Archive *a = dyn_cast<Archive>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000686 DumpArchive(a);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000687 else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000688 DumpObject(o);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000689 else
Michael J. Spencer27781b72011-10-08 00:18:30 +0000690 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000691}
692
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000693int main(int argc, char **argv) {
694 // Print a stack trace if we signal out.
695 sys::PrintStackTraceOnErrorSignal();
696 PrettyStackTraceProgram X(argc, argv);
697 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
698
699 // Initialize targets and assembly printers/parsers.
700 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000701 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000702 llvm::InitializeAllAsmParsers();
703 llvm::InitializeAllDisassemblers();
704
Pete Cooperff204962012-05-03 23:20:10 +0000705 // Register the target printer for --version.
706 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
707
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000708 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
709 TripleName = Triple::normalize(TripleName);
710
711 ToolName = argv[0];
712
713 // Defaults to a.out if no filenames specified.
714 if (InputFilenames.size() == 0)
715 InputFilenames.push_back("a.out");
716
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000717 if (!Disassemble
718 && !Relocations
719 && !SectionHeaders
720 && !SectionContents
Michael J. Spencereef7b622012-12-05 20:12:35 +0000721 && !SymbolTable
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000722 && !UnwindInfo
723 && !PrivateHeaders) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000724 cl::PrintHelpMessage();
725 return 2;
726 }
727
Michael J. Spencer27781b72011-10-08 00:18:30 +0000728 std::for_each(InputFilenames.begin(), InputFilenames.end(),
729 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000730
731 return 0;
732}