blob: 2838a2a2b300c674fd18577af4345eab26f71889 [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"
31#include "llvm/Object/ObjectFile.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000032#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000033#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000035#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000036#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000037#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000038#include "llvm/Support/Host.h"
39#include "llvm/Support/ManagedStatic.h"
40#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000041#include "llvm/Support/MemoryObject.h"
42#include "llvm/Support/PrettyStackTrace.h"
43#include "llvm/Support/Signals.h"
44#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000045#include "llvm/Support/TargetRegistry.h"
46#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000047#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000048#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049#include <algorithm>
Benjamin Kramer81bbdfd2012-03-23 11:49:32 +000050#include <cctype>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000051#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000052using namespace llvm;
53using namespace object;
54
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000055static cl::list<std::string>
56InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000057
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000058static cl::opt<bool>
59Disassemble("disassemble",
60 cl::desc("Display assembler mnemonics for the machine instructions"));
61static cl::alias
62Disassembled("d", cl::desc("Alias for --disassemble"),
63 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000064
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000065static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000066Relocations("r", cl::desc("Display the relocation entries in the file"));
67
68static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000069SectionContents("s", cl::desc("Display the content of each section"));
70
71static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000072SymbolTable("t", cl::desc("Display the symbol table"));
73
74static cl::opt<bool>
Rafael Espindolacd7ee1c2012-12-19 14:48:05 +000075MachO("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000076static cl::alias
Rafael Espindolacd7ee1c2012-12-19 14:48:05 +000077MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
Benjamin Kramer685a2502011-07-20 19:37:35 +000078
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000079cl::opt<std::string>
80llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
81 "see -version for available targets"));
82
83cl::opt<std::string>
84llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000085 "see -version for available targets"));
86
Nick Lewycky023bb152011-10-10 21:21:34 +000087static cl::opt<bool>
88SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
89 "for each section."));
90static cl::alias
91SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
92 cl::aliasopt(SectionHeaders));
93static cl::alias
94SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
95 cl::aliasopt(SectionHeaders));
96
Jack Carterfd6d1652012-08-28 19:24:49 +000097static cl::list<std::string>
98MAttrs("mattr",
99 cl::CommaSeparated,
100 cl::desc("Target specific attributes"),
101 cl::value_desc("a1,+a2,-a3,..."));
102
Eli Bendersky8b9da532012-11-20 22:57:02 +0000103static cl::opt<bool>
104NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, "
105 "do not print the instruction bytes."));
106
Michael J. Spencereef7b622012-12-05 20:12:35 +0000107static cl::opt<bool>
108UnwindInfo("unwind-info", cl::desc("Display unwind information"));
109
110static cl::alias
111UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
112 cl::aliasopt(UnwindInfo));
113
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000114static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000115
Michael J. Spencereef7b622012-12-05 20:12:35 +0000116bool llvm::error(error_code ec) {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000117 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000118
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000119 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
120 outs().flush();
121 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000122}
123
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000124static const Target *getTarget(const ObjectFile *Obj = NULL) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000125 // Figure out the target triple.
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000126 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000127 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000128 if (Obj)
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000129 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000130 } else
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000131 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000132
133 // Get the target specific parser.
134 std::string Error;
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000135 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
136 Error);
137 if (!TheTarget) {
138 errs() << ToolName << ": " << Error;
139 return 0;
140 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000141
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000142 // Update the triple name and return the found target.
143 TripleName = TheTriple.getTriple();
144 return TheTarget;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000145}
146
David Blaikie2d24e2a2011-12-20 02:50:00 +0000147void llvm::StringRefMemoryObject::anchor() { }
148
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000149void llvm::DumpBytes(StringRef bytes) {
150 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000151 // FIXME: The real way to do this is to figure out the longest instruction
152 // and align to that size before printing. I'll fix this when I get
153 // around to outputting relocations.
154 // 15 is the longest x86 instruction
155 // 3 is for the hex rep of a byte + a space.
156 // 1 is for the null terminator.
157 enum { OutputSize = (15 * 3) + 1 };
158 char output[OutputSize];
159
160 assert(bytes.size() <= 15
161 && "DumpBytes only supports instructions of up to 15 bytes");
162 memset(output, ' ', sizeof(output));
163 unsigned index = 0;
164 for (StringRef::iterator i = bytes.begin(),
165 e = bytes.end(); i != e; ++i) {
166 output[index] = hex_rep[(*i & 0xF0) >> 4];
167 output[index + 1] = hex_rep[*i & 0xF];
168 index += 3;
169 }
170
171 output[sizeof(output) - 1] = 0;
172 outs() << output;
173}
174
Michael J. Spencereef7b622012-12-05 20:12:35 +0000175bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000176 uint64_t a_addr, b_addr;
177 if (error(a.getAddress(a_addr))) return false;
178 if (error(b.getAddress(b_addr))) return false;
179 return a_addr < b_addr;
180}
181
182static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000183 const Target *TheTarget = getTarget(Obj);
184 // getTarget() will have already issued a diagnostic if necessary, so
185 // just bail here if it failed.
186 if (!TheTarget)
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000187 return;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000188
Jack Carterfd6d1652012-08-28 19:24:49 +0000189 // Package up features to be passed to target/subtarget
190 std::string FeaturesStr;
191 if (MAttrs.size()) {
192 SubtargetFeatures Features;
193 for (unsigned i = 0; i != MAttrs.size(); ++i)
194 Features.AddFeature(MAttrs[i]);
195 FeaturesStr = Features.getString();
196 }
197
Michael J. Spencer25b15772011-06-25 17:55:23 +0000198 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000199 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000200 e = Obj->end_sections();
201 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000202 if (error(ec)) break;
203 bool text;
204 if (error(i->isText(text))) break;
205 if (!text) continue;
206
Michael J. Spencer942eb002011-10-13 22:17:18 +0000207 uint64_t SectionAddr;
208 if (error(i->getAddress(SectionAddr))) break;
209
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000210 // Make a list of all the symbols in this section.
211 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000212 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000213 se = Obj->end_symbols();
214 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000215 bool contains;
216 if (!error(i->containsSymbol(*si, contains)) && contains) {
217 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000218 if (error(si->getAddress(Address))) break;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000219 Address -= SectionAddr;
220
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000221 StringRef Name;
222 if (error(si->getName(Name))) break;
223 Symbols.push_back(std::make_pair(Address, Name));
224 }
225 }
226
227 // Sort the symbols by address, just in case they didn't come in that way.
228 array_pod_sort(Symbols.begin(), Symbols.end());
229
Michael J. Spencer942eb002011-10-13 22:17:18 +0000230 // Make a list of all the relocations for this section.
231 std::vector<RelocationRef> Rels;
232 if (InlineRelocs) {
233 for (relocation_iterator ri = i->begin_relocations(),
234 re = i->end_relocations();
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000235 ri != re; ri.increment(ec)) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000236 if (error(ec)) break;
237 Rels.push_back(*ri);
238 }
239 }
240
241 // Sort relocations by address.
242 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
243
Michael J. Spencer25b15772011-06-25 17:55:23 +0000244 StringRef name;
245 if (error(i->getName(name))) break;
Rafael Espindolacd7ee1c2012-12-19 14:48:05 +0000246 outs() << "Disassembly of section " << name << ':';
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000247
248 // If the section has no symbols just insert a dummy one and disassemble
249 // the whole section.
250 if (Symbols.empty())
251 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000252
253 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000254 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000255
256 if (!AsmInfo) {
257 errs() << "error: no assembly info for target " << TripleName << "\n";
258 return;
259 }
260
Michael J. Spencer27781b72011-10-08 00:18:30 +0000261 OwningPtr<const MCSubtargetInfo> STI(
Jack Carterfd6d1652012-08-28 19:24:49 +0000262 TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr));
James Molloyb9505852011-09-07 17:24:38 +0000263
264 if (!STI) {
265 errs() << "error: no subtarget info for target " << TripleName << "\n";
266 return;
267 }
268
Owen Anderson10c044e2011-10-27 21:55:13 +0000269 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000270 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000271 if (!DisAsm) {
272 errs() << "error: no disassembler for target " << TripleName << "\n";
273 return;
274 }
275
Jim Grosbachc6449b62012-03-05 19:33:20 +0000276 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
277 if (!MRI) {
278 errs() << "error: no register info for target " << TripleName << "\n";
279 return;
280 }
281
Craig Topper17463b32012-04-02 06:09:36 +0000282 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
283 if (!MII) {
284 errs() << "error: no instruction info for target " << TripleName << "\n";
285 return;
286 }
287
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000288 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
289 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Craig Topper17463b32012-04-02 06:09:36 +0000290 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000291 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000292 errs() << "error: no instruction printer for target " << TripleName
293 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000294 return;
295 }
296
Michael J. Spencer25b15772011-06-25 17:55:23 +0000297 StringRef Bytes;
298 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000299 StringRefMemoryObject memoryObject(Bytes);
300 uint64_t Size;
301 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000302 uint64_t SectSize;
303 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000304
Michael J. Spencer942eb002011-10-13 22:17:18 +0000305 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
306 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000307 // Disassemble symbol by symbol.
308 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
309 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000310 uint64_t End;
311 // The end is either the size of the section or the beginning of the next
312 // symbol.
313 if (si == se - 1)
314 End = SectSize;
315 // Make sure this symbol takes up space.
316 else if (Symbols[si + 1].first != Start)
317 End = Symbols[si + 1].first - 1;
318 else
319 // This symbol has the same address as the next symbol. Skip it.
320 continue;
321
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000322 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000323
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000324#ifndef NDEBUG
325 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
326#else
327 raw_ostream &DebugOut = nulls();
328#endif
329
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000330 for (Index = Start; Index < End; Index += Size) {
331 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000332
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000333 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
334 DebugOut, nulls())) {
Eli Bendersky8b9da532012-11-20 22:57:02 +0000335 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
336 if (!NoShowRawInsn) {
337 outs() << "\t";
338 DumpBytes(StringRef(Bytes.data() + Index, Size));
339 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000340 IP->printInst(&Inst, outs(), "");
341 outs() << "\n";
342 } else {
343 errs() << ToolName << ": warning: invalid instruction encoding\n";
344 if (Size == 0)
345 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000346 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000347
348 // Print relocation for instruction.
349 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000350 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000351 uint64_t addr;
352 SmallString<16> name;
353 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000354
355 // If this relocation is hidden, skip it.
356 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
357 if (hidden) goto skip_print_rel;
358
Michael J. Spencer942eb002011-10-13 22:17:18 +0000359 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
360 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000361 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000362 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
363 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
364
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000365 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
366 << "\t" << val << "\n";
Michael J. Spencer942eb002011-10-13 22:17:18 +0000367
368 skip_print_rel:
369 ++rel_cur;
370 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000371 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000372 }
373 }
374}
375
Michael J. Spencer27781b72011-10-08 00:18:30 +0000376static void PrintRelocations(const ObjectFile *o) {
377 error_code ec;
378 for (section_iterator si = o->begin_sections(), se = o->end_sections();
379 si != se; si.increment(ec)){
380 if (error(ec)) return;
381 if (si->begin_relocations() == si->end_relocations())
382 continue;
383 StringRef secname;
384 if (error(si->getName(secname))) continue;
385 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
386 for (relocation_iterator ri = si->begin_relocations(),
387 re = si->end_relocations();
388 ri != re; ri.increment(ec)) {
389 if (error(ec)) return;
390
Owen Anderson0685e942011-10-25 20:35:53 +0000391 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000392 uint64_t address;
393 SmallString<32> relocname;
394 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000395 if (error(ri->getHidden(hidden))) continue;
396 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000397 if (error(ri->getTypeName(relocname))) continue;
398 if (error(ri->getAddress(address))) continue;
399 if (error(ri->getValueString(valuestr))) continue;
400 outs() << address << " " << relocname << " " << valuestr << "\n";
401 }
402 outs() << "\n";
403 }
404}
405
Nick Lewycky023bb152011-10-10 21:21:34 +0000406static void PrintSectionHeaders(const ObjectFile *o) {
407 outs() << "Sections:\n"
408 "Idx Name Size Address Type\n";
409 error_code ec;
410 unsigned i = 0;
411 for (section_iterator si = o->begin_sections(), se = o->end_sections();
412 si != se; si.increment(ec)) {
413 if (error(ec)) return;
414 StringRef Name;
415 if (error(si->getName(Name))) return;
416 uint64_t Address;
417 if (error(si->getAddress(Address))) return;
418 uint64_t Size;
419 if (error(si->getSize(Size))) return;
420 bool Text, Data, BSS;
421 if (error(si->isText(Text))) return;
422 if (error(si->isData(Data))) return;
423 if (error(si->isBSS(BSS))) return;
424 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000425 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000426 outs() << format("%3d %-13s %09" PRIx64 " %017" PRIx64 " %s\n",
427 i, Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewycky023bb152011-10-10 21:21:34 +0000428 ++i;
429 }
430}
431
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000432static void PrintSectionContents(const ObjectFile *o) {
433 error_code ec;
434 for (section_iterator si = o->begin_sections(),
435 se = o->end_sections();
436 si != se; si.increment(ec)) {
437 if (error(ec)) return;
438 StringRef Name;
439 StringRef Contents;
440 uint64_t BaseAddr;
441 if (error(si->getName(Name))) continue;
442 if (error(si->getContents(Contents))) continue;
443 if (error(si->getAddress(BaseAddr))) continue;
444
445 outs() << "Contents of section " << Name << ":\n";
446
447 // Dump out the content as hex and printable ascii characters.
448 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000449 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000450 // Dump line of hex.
451 for (std::size_t i = 0; i < 16; ++i) {
452 if (i != 0 && i % 4 == 0)
453 outs() << ' ';
454 if (addr + i < end)
455 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
456 << hexdigit(Contents[addr + i] & 0xF, true);
457 else
458 outs() << " ";
459 }
460 // Print ascii.
461 outs() << " ";
462 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
463 if (std::isprint(Contents[addr + i] & 0xFF))
464 outs() << Contents[addr + i];
465 else
466 outs() << ".";
467 }
468 outs() << "\n";
469 }
470 }
471}
472
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000473static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
474 const coff_file_header *header;
475 if (error(coff->getHeader(header))) return;
476 int aux_count = 0;
477 const coff_symbol *symbol = 0;
478 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
479 if (aux_count--) {
480 // Figure out which type of aux this is.
481 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
482 && symbol->Value == 0) { // Section definition.
483 const coff_aux_section_definition *asd;
484 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
485 return;
486 outs() << "AUX "
487 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
488 , unsigned(asd->Length)
489 , unsigned(asd->NumberOfRelocations)
490 , unsigned(asd->NumberOfLinenumbers)
491 , unsigned(asd->CheckSum))
492 << format("assoc %d comdat %d\n"
493 , unsigned(asd->Number)
494 , unsigned(asd->Selection));
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000495 } else
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000496 outs() << "AUX Unknown\n";
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000497 } else {
498 StringRef name;
499 if (error(coff->getSymbol(i, symbol))) return;
500 if (error(coff->getSymbolName(symbol, name))) return;
501 outs() << "[" << format("%2d", i) << "]"
502 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
503 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
504 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
505 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
506 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
507 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
508 << name << "\n";
509 aux_count = symbol->NumberOfAuxSymbols;
510 }
511 }
512}
513
514static void PrintSymbolTable(const ObjectFile *o) {
515 outs() << "SYMBOL TABLE:\n";
516
517 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
518 PrintCOFFSymbolTable(coff);
519 else {
520 error_code ec;
521 for (symbol_iterator si = o->begin_symbols(),
522 se = o->end_symbols(); si != se; si.increment(ec)) {
523 if (error(ec)) return;
524 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000525 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000526 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000527 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000528 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000529 section_iterator Section = o->end_sections();
530 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000531 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000532 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000533 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000534 if (error(si->getSize(Size))) continue;
535 if (error(si->getSection(Section))) continue;
536
David Meyerc46255a2012-02-28 23:47:53 +0000537 bool Global = Flags & SymbolRef::SF_Global;
538 bool Weak = Flags & SymbolRef::SF_Weak;
539 bool Absolute = Flags & SymbolRef::SF_Absolute;
540
Danil Malyshevb0436a72011-11-29 17:40:10 +0000541 if (Address == UnknownAddressOrSize)
542 Address = 0;
543 if (Size == UnknownAddressOrSize)
544 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000545 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000546 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000547 GlobLoc = Global ? 'g' : 'l';
548 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
549 ? 'd' : ' ';
550 char FileFunc = ' ';
551 if (Type == SymbolRef::ST_File)
552 FileFunc = 'f';
553 else if (Type == SymbolRef::ST_Function)
554 FileFunc = 'F';
555
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000556 outs() << format("%08" PRIx64, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000557 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
558 << (Weak ? 'w' : ' ') // Weak?
559 << ' ' // Constructor. Not supported yet.
560 << ' ' // Warning. Not supported yet.
561 << ' ' // Indirect reference to another symbol.
562 << Debug // Debugging (d) or dynamic (D) symbol.
563 << FileFunc // Name of function (F), file (f) or object (O).
564 << ' ';
565 if (Absolute)
566 outs() << "*ABS*";
567 else if (Section == o->end_sections())
568 outs() << "*UND*";
569 else {
570 StringRef SectionName;
571 if (error(Section->getName(SectionName)))
572 SectionName = "";
573 outs() << SectionName;
574 }
575 outs() << '\t'
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000576 << format("%08" PRIx64 " ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000577 << Name
578 << '\n';
579 }
580 }
581}
582
Michael J. Spencereef7b622012-12-05 20:12:35 +0000583static void PrintUnwindInfo(const ObjectFile *o) {
584 outs() << "Unwind info:\n\n";
585
586 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
587 printCOFFUnwindInfo(coff);
588 } else {
589 // TODO: Extract DWARF dump tool to objdump.
590 errs() << "This operation is only currently supported "
591 "for COFF object files.\n";
592 return;
593 }
594}
595
Michael J. Spencer27781b72011-10-08 00:18:30 +0000596static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000597 outs() << '\n';
598 outs() << o->getFileName()
599 << ":\tfile format " << o->getFileFormatName() << "\n\n";
600
Michael J. Spencer27781b72011-10-08 00:18:30 +0000601 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000602 DisassembleObject(o, Relocations);
603 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000604 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000605 if (SectionHeaders)
606 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000607 if (SectionContents)
608 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000609 if (SymbolTable)
610 PrintSymbolTable(o);
Michael J. Spencereef7b622012-12-05 20:12:35 +0000611 if (UnwindInfo)
612 PrintUnwindInfo(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000613}
614
615/// @brief Dump each object file in \a a;
616static void DumpArchive(const Archive *a) {
617 for (Archive::child_iterator i = a->begin_children(),
618 e = a->end_children(); i != e; ++i) {
619 OwningPtr<Binary> child;
620 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000621 // Ignore non-object files.
622 if (ec != object_error::invalid_file_type)
623 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
624 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000625 continue;
626 }
627 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
628 DumpObject(o);
629 else
630 errs() << ToolName << ": '" << a->getFileName() << "': "
631 << "Unrecognized file type.\n";
632 }
633}
634
635/// @brief Open file and figure out how to dump it.
636static void DumpInput(StringRef file) {
637 // If file isn't stdin, check that it exists.
638 if (file != "-" && !sys::fs::exists(file)) {
639 errs() << ToolName << ": '" << file << "': " << "No such file\n";
640 return;
641 }
642
Rafael Espindolacd7ee1c2012-12-19 14:48:05 +0000643 if (MachO && Disassemble) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000644 DisassembleInputMachO(file);
645 return;
646 }
647
648 // Attempt to open the binary.
649 OwningPtr<Binary> binary;
650 if (error_code ec = createBinary(file, binary)) {
651 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
652 return;
653 }
654
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000655 if (Archive *a = dyn_cast<Archive>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000656 DumpArchive(a);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000657 else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000658 DumpObject(o);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000659 else
Michael J. Spencer27781b72011-10-08 00:18:30 +0000660 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000661}
662
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000663int main(int argc, char **argv) {
664 // Print a stack trace if we signal out.
665 sys::PrintStackTraceOnErrorSignal();
666 PrettyStackTraceProgram X(argc, argv);
667 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
668
669 // Initialize targets and assembly printers/parsers.
670 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000671 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000672 llvm::InitializeAllAsmParsers();
673 llvm::InitializeAllDisassemblers();
674
Pete Cooperff204962012-05-03 23:20:10 +0000675 // Register the target printer for --version.
676 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
677
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000678 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
679 TripleName = Triple::normalize(TripleName);
680
681 ToolName = argv[0];
682
683 // Defaults to a.out if no filenames specified.
684 if (InputFilenames.size() == 0)
685 InputFilenames.push_back("a.out");
686
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000687 if (!Disassemble
688 && !Relocations
689 && !SectionHeaders
690 && !SectionContents
Michael J. Spencereef7b622012-12-05 20:12:35 +0000691 && !SymbolTable
692 && !UnwindInfo) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000693 cl::PrintHelpMessage();
694 return 2;
695 }
696
Michael J. Spencer27781b72011-10-08 00:18:30 +0000697 std::for_each(InputFilenames.begin(), InputFilenames.end(),
698 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000699
700 return 0;
701}