blob: 7aaebb28a4632788c9fb6f000a4de0ff68a47b2a [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"
Craig Topper17463b32012-04-02 06:09:36 +000029#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachc6449b62012-03-05 19:33:20 +000030#include "llvm/MC/MCRegisterInfo.h"
James Molloyb9505852011-09-07 17:24:38 +000031#include "llvm/MC/MCSubtargetInfo.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>
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000075MachO("macho", cl::desc("Use MachO specific object file parser"));
76static cl::alias
77MachOm("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
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000097static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000098
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000099static bool error(error_code ec) {
100 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000101
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000102 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
103 outs().flush();
104 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000105}
106
107static const Target *GetTarget(const ObjectFile *Obj = NULL) {
108 // Figure out the target triple.
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000109 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000110 if (TripleName.empty()) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000111 if (Obj)
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000112 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000113 } else
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000114 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000115
116 // Get the target specific parser.
117 std::string Error;
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000118 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
119 Error);
120 if (!TheTarget) {
121 errs() << ToolName << ": " << Error;
122 return 0;
123 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000124
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000125 // Update the triple name and return the found target.
126 TripleName = TheTriple.getTriple();
127 return TheTarget;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000128}
129
David Blaikie2d24e2a2011-12-20 02:50:00 +0000130void llvm::StringRefMemoryObject::anchor() { }
131
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000132void llvm::DumpBytes(StringRef bytes) {
133 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000134 // FIXME: The real way to do this is to figure out the longest instruction
135 // and align to that size before printing. I'll fix this when I get
136 // around to outputting relocations.
137 // 15 is the longest x86 instruction
138 // 3 is for the hex rep of a byte + a space.
139 // 1 is for the null terminator.
140 enum { OutputSize = (15 * 3) + 1 };
141 char output[OutputSize];
142
143 assert(bytes.size() <= 15
144 && "DumpBytes only supports instructions of up to 15 bytes");
145 memset(output, ' ', sizeof(output));
146 unsigned index = 0;
147 for (StringRef::iterator i = bytes.begin(),
148 e = bytes.end(); i != e; ++i) {
149 output[index] = hex_rep[(*i & 0xF0) >> 4];
150 output[index + 1] = hex_rep[*i & 0xF];
151 index += 3;
152 }
153
154 output[sizeof(output) - 1] = 0;
155 outs() << output;
156}
157
Michael J. Spencer942eb002011-10-13 22:17:18 +0000158static bool RelocAddressLess(RelocationRef a, RelocationRef b) {
159 uint64_t a_addr, b_addr;
160 if (error(a.getAddress(a_addr))) return false;
161 if (error(b.getAddress(b_addr))) return false;
162 return a_addr < b_addr;
163}
164
165static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000166 const Target *TheTarget = GetTarget(Obj);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000167 if (!TheTarget) {
168 // GetTarget prints out stuff.
169 return;
170 }
171
Michael J. Spencer25b15772011-06-25 17:55:23 +0000172 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000173 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000174 e = Obj->end_sections();
175 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000176 if (error(ec)) break;
177 bool text;
178 if (error(i->isText(text))) break;
179 if (!text) continue;
180
Michael J. Spencer942eb002011-10-13 22:17:18 +0000181 uint64_t SectionAddr;
182 if (error(i->getAddress(SectionAddr))) break;
183
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000184 // Make a list of all the symbols in this section.
185 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000186 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000187 se = Obj->end_symbols();
188 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000189 bool contains;
190 if (!error(i->containsSymbol(*si, contains)) && contains) {
191 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000192 if (error(si->getAddress(Address))) break;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000193 Address -= SectionAddr;
194
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000195 StringRef Name;
196 if (error(si->getName(Name))) break;
197 Symbols.push_back(std::make_pair(Address, Name));
198 }
199 }
200
201 // Sort the symbols by address, just in case they didn't come in that way.
202 array_pod_sort(Symbols.begin(), Symbols.end());
203
Michael J. Spencer942eb002011-10-13 22:17:18 +0000204 // Make a list of all the relocations for this section.
205 std::vector<RelocationRef> Rels;
206 if (InlineRelocs) {
207 for (relocation_iterator ri = i->begin_relocations(),
208 re = i->end_relocations();
209 ri != re; ri.increment(ec)) {
210 if (error(ec)) break;
211 Rels.push_back(*ri);
212 }
213 }
214
215 // Sort relocations by address.
216 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
217
Michael J. Spencer25b15772011-06-25 17:55:23 +0000218 StringRef name;
219 if (error(i->getName(name))) break;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000220 outs() << "Disassembly of section " << name << ':';
221
222 // If the section has no symbols just insert a dummy one and disassemble
223 // the whole section.
224 if (Symbols.empty())
225 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000226
227 // Set up disassembler.
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000228 OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000229
230 if (!AsmInfo) {
231 errs() << "error: no assembly info for target " << TripleName << "\n";
232 return;
233 }
234
Michael J. Spencer27781b72011-10-08 00:18:30 +0000235 OwningPtr<const MCSubtargetInfo> STI(
236 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
James Molloyb9505852011-09-07 17:24:38 +0000237
238 if (!STI) {
239 errs() << "error: no subtarget info for target " << TripleName << "\n";
240 return;
241 }
242
Owen Anderson10c044e2011-10-27 21:55:13 +0000243 OwningPtr<const MCDisassembler> DisAsm(
Michael J. Spencer27781b72011-10-08 00:18:30 +0000244 TheTarget->createMCDisassembler(*STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000245 if (!DisAsm) {
246 errs() << "error: no disassembler for target " << TripleName << "\n";
247 return;
248 }
249
Jim Grosbachc6449b62012-03-05 19:33:20 +0000250 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
251 if (!MRI) {
252 errs() << "error: no register info for target " << TripleName << "\n";
253 return;
254 }
255
Craig Topper17463b32012-04-02 06:09:36 +0000256 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
257 if (!MII) {
258 errs() << "error: no instruction info for target " << TripleName << "\n";
259 return;
260 }
261
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000262 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
263 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Craig Topper17463b32012-04-02 06:09:36 +0000264 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000265 if (!IP) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000266 errs() << "error: no instruction printer for target " << TripleName
267 << '\n';
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000268 return;
269 }
270
Michael J. Spencer25b15772011-06-25 17:55:23 +0000271 StringRef Bytes;
272 if (error(i->getContents(Bytes))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000273 StringRefMemoryObject memoryObject(Bytes);
274 uint64_t Size;
275 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000276 uint64_t SectSize;
277 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000278
Michael J. Spencer942eb002011-10-13 22:17:18 +0000279 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
280 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000281 // Disassemble symbol by symbol.
282 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
283 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000284 uint64_t End;
285 // The end is either the size of the section or the beginning of the next
286 // symbol.
287 if (si == se - 1)
288 End = SectSize;
289 // Make sure this symbol takes up space.
290 else if (Symbols[si + 1].first != Start)
291 End = Symbols[si + 1].first - 1;
292 else
293 // This symbol has the same address as the next symbol. Skip it.
294 continue;
295
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000296 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000297
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000298#ifndef NDEBUG
299 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
300#else
301 raw_ostream &DebugOut = nulls();
302#endif
303
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000304 for (Index = Start; Index < End; Index += Size) {
305 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000306
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000307 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
308 DebugOut, nulls())) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000309 outs() << format("%8" PRIx64 ":\t", SectionAddr + Index);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000310 DumpBytes(StringRef(Bytes.data() + Index, Size));
311 IP->printInst(&Inst, outs(), "");
312 outs() << "\n";
313 } else {
314 errs() << ToolName << ": warning: invalid instruction encoding\n";
315 if (Size == 0)
316 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000317 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000318
319 // Print relocation for instruction.
320 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000321 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000322 uint64_t addr;
323 SmallString<16> name;
324 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000325
326 // If this relocation is hidden, skip it.
327 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
328 if (hidden) goto skip_print_rel;
329
Michael J. Spencer942eb002011-10-13 22:17:18 +0000330 if (error(rel_cur->getAddress(addr))) goto skip_print_rel;
331 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000332 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000333 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
334 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
335
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000336 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
337 << "\t" << val << "\n";
Michael J. Spencer942eb002011-10-13 22:17:18 +0000338
339 skip_print_rel:
340 ++rel_cur;
341 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000342 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000343 }
344 }
345}
346
Michael J. Spencer27781b72011-10-08 00:18:30 +0000347static void PrintRelocations(const ObjectFile *o) {
348 error_code ec;
349 for (section_iterator si = o->begin_sections(), se = o->end_sections();
350 si != se; si.increment(ec)){
351 if (error(ec)) return;
352 if (si->begin_relocations() == si->end_relocations())
353 continue;
354 StringRef secname;
355 if (error(si->getName(secname))) continue;
356 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
357 for (relocation_iterator ri = si->begin_relocations(),
358 re = si->end_relocations();
359 ri != re; ri.increment(ec)) {
360 if (error(ec)) return;
361
Owen Anderson0685e942011-10-25 20:35:53 +0000362 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000363 uint64_t address;
364 SmallString<32> relocname;
365 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000366 if (error(ri->getHidden(hidden))) continue;
367 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000368 if (error(ri->getTypeName(relocname))) continue;
369 if (error(ri->getAddress(address))) continue;
370 if (error(ri->getValueString(valuestr))) continue;
371 outs() << address << " " << relocname << " " << valuestr << "\n";
372 }
373 outs() << "\n";
374 }
375}
376
Nick Lewycky023bb152011-10-10 21:21:34 +0000377static void PrintSectionHeaders(const ObjectFile *o) {
378 outs() << "Sections:\n"
379 "Idx Name Size Address Type\n";
380 error_code ec;
381 unsigned i = 0;
382 for (section_iterator si = o->begin_sections(), se = o->end_sections();
383 si != se; si.increment(ec)) {
384 if (error(ec)) return;
385 StringRef Name;
386 if (error(si->getName(Name))) return;
387 uint64_t Address;
388 if (error(si->getAddress(Address))) return;
389 uint64_t Size;
390 if (error(si->getSize(Size))) return;
391 bool Text, Data, BSS;
392 if (error(si->isText(Text))) return;
393 if (error(si->isData(Data))) return;
394 if (error(si->isBSS(BSS))) return;
395 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000396 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000397 outs() << format("%3d %-13s %09" PRIx64 " %017" PRIx64 " %s\n",
398 i, Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewycky023bb152011-10-10 21:21:34 +0000399 ++i;
400 }
401}
402
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000403static void PrintSectionContents(const ObjectFile *o) {
404 error_code ec;
405 for (section_iterator si = o->begin_sections(),
406 se = o->end_sections();
407 si != se; si.increment(ec)) {
408 if (error(ec)) return;
409 StringRef Name;
410 StringRef Contents;
411 uint64_t BaseAddr;
412 if (error(si->getName(Name))) continue;
413 if (error(si->getContents(Contents))) continue;
414 if (error(si->getAddress(BaseAddr))) continue;
415
416 outs() << "Contents of section " << Name << ":\n";
417
418 // Dump out the content as hex and printable ascii characters.
419 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000420 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000421 // Dump line of hex.
422 for (std::size_t i = 0; i < 16; ++i) {
423 if (i != 0 && i % 4 == 0)
424 outs() << ' ';
425 if (addr + i < end)
426 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
427 << hexdigit(Contents[addr + i] & 0xF, true);
428 else
429 outs() << " ";
430 }
431 // Print ascii.
432 outs() << " ";
433 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
434 if (std::isprint(Contents[addr + i] & 0xFF))
435 outs() << Contents[addr + i];
436 else
437 outs() << ".";
438 }
439 outs() << "\n";
440 }
441 }
442}
443
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000444static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
445 const coff_file_header *header;
446 if (error(coff->getHeader(header))) return;
447 int aux_count = 0;
448 const coff_symbol *symbol = 0;
449 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
450 if (aux_count--) {
451 // Figure out which type of aux this is.
452 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
453 && symbol->Value == 0) { // Section definition.
454 const coff_aux_section_definition *asd;
455 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
456 return;
457 outs() << "AUX "
458 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
459 , unsigned(asd->Length)
460 , unsigned(asd->NumberOfRelocations)
461 , unsigned(asd->NumberOfLinenumbers)
462 , unsigned(asd->CheckSum))
463 << format("assoc %d comdat %d\n"
464 , unsigned(asd->Number)
465 , unsigned(asd->Selection));
466 } else {
467 outs() << "AUX Unknown\n";
468 }
469 } else {
470 StringRef name;
471 if (error(coff->getSymbol(i, symbol))) return;
472 if (error(coff->getSymbolName(symbol, name))) return;
473 outs() << "[" << format("%2d", i) << "]"
474 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
475 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
476 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
477 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
478 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
479 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
480 << name << "\n";
481 aux_count = symbol->NumberOfAuxSymbols;
482 }
483 }
484}
485
486static void PrintSymbolTable(const ObjectFile *o) {
487 outs() << "SYMBOL TABLE:\n";
488
489 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
490 PrintCOFFSymbolTable(coff);
491 else {
492 error_code ec;
493 for (symbol_iterator si = o->begin_symbols(),
494 se = o->end_symbols(); si != se; si.increment(ec)) {
495 if (error(ec)) return;
496 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000497 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000498 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000499 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000500 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000501 section_iterator Section = o->end_sections();
502 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000503 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000504 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000505 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000506 if (error(si->getSize(Size))) continue;
507 if (error(si->getSection(Section))) continue;
508
David Meyerc46255a2012-02-28 23:47:53 +0000509 bool Global = Flags & SymbolRef::SF_Global;
510 bool Weak = Flags & SymbolRef::SF_Weak;
511 bool Absolute = Flags & SymbolRef::SF_Absolute;
512
Danil Malyshevb0436a72011-11-29 17:40:10 +0000513 if (Address == UnknownAddressOrSize)
514 Address = 0;
515 if (Size == UnknownAddressOrSize)
516 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000517 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000518 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000519 GlobLoc = Global ? 'g' : 'l';
520 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
521 ? 'd' : ' ';
522 char FileFunc = ' ';
523 if (Type == SymbolRef::ST_File)
524 FileFunc = 'f';
525 else if (Type == SymbolRef::ST_Function)
526 FileFunc = 'F';
527
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000528 outs() << format("%08" PRIx64, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000529 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
530 << (Weak ? 'w' : ' ') // Weak?
531 << ' ' // Constructor. Not supported yet.
532 << ' ' // Warning. Not supported yet.
533 << ' ' // Indirect reference to another symbol.
534 << Debug // Debugging (d) or dynamic (D) symbol.
535 << FileFunc // Name of function (F), file (f) or object (O).
536 << ' ';
537 if (Absolute)
538 outs() << "*ABS*";
539 else if (Section == o->end_sections())
540 outs() << "*UND*";
541 else {
542 StringRef SectionName;
543 if (error(Section->getName(SectionName)))
544 SectionName = "";
545 outs() << SectionName;
546 }
547 outs() << '\t'
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000548 << format("%08" PRIx64 " ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000549 << Name
550 << '\n';
551 }
552 }
553}
554
Michael J. Spencer27781b72011-10-08 00:18:30 +0000555static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000556 outs() << '\n';
557 outs() << o->getFileName()
558 << ":\tfile format " << o->getFileFormatName() << "\n\n";
559
Michael J. Spencer27781b72011-10-08 00:18:30 +0000560 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000561 DisassembleObject(o, Relocations);
562 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000563 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000564 if (SectionHeaders)
565 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000566 if (SectionContents)
567 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000568 if (SymbolTable)
569 PrintSymbolTable(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000570}
571
572/// @brief Dump each object file in \a a;
573static void DumpArchive(const Archive *a) {
574 for (Archive::child_iterator i = a->begin_children(),
575 e = a->end_children(); i != e; ++i) {
576 OwningPtr<Binary> child;
577 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000578 // Ignore non-object files.
579 if (ec != object_error::invalid_file_type)
580 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
581 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000582 continue;
583 }
584 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
585 DumpObject(o);
586 else
587 errs() << ToolName << ": '" << a->getFileName() << "': "
588 << "Unrecognized file type.\n";
589 }
590}
591
592/// @brief Open file and figure out how to dump it.
593static void DumpInput(StringRef file) {
594 // If file isn't stdin, check that it exists.
595 if (file != "-" && !sys::fs::exists(file)) {
596 errs() << ToolName << ": '" << file << "': " << "No such file\n";
597 return;
598 }
599
600 if (MachO && Disassemble) {
601 DisassembleInputMachO(file);
602 return;
603 }
604
605 // Attempt to open the binary.
606 OwningPtr<Binary> binary;
607 if (error_code ec = createBinary(file, binary)) {
608 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
609 return;
610 }
611
612 if (Archive *a = dyn_cast<Archive>(binary.get())) {
613 DumpArchive(a);
614 } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
615 DumpObject(o);
616 } else {
617 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
618 }
619}
620
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000621int main(int argc, char **argv) {
622 // Print a stack trace if we signal out.
623 sys::PrintStackTraceOnErrorSignal();
624 PrettyStackTraceProgram X(argc, argv);
625 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
626
627 // Initialize targets and assembly printers/parsers.
628 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000629 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000630 llvm::InitializeAllAsmParsers();
631 llvm::InitializeAllDisassemblers();
632
Pete Cooperff204962012-05-03 23:20:10 +0000633 // Register the target printer for --version.
634 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
635
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000636 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
637 TripleName = Triple::normalize(TripleName);
638
639 ToolName = argv[0];
640
641 // Defaults to a.out if no filenames specified.
642 if (InputFilenames.size() == 0)
643 InputFilenames.push_back("a.out");
644
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000645 if (!Disassemble
646 && !Relocations
647 && !SectionHeaders
648 && !SectionContents
649 && !SymbolTable) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000650 cl::PrintHelpMessage();
651 return 2;
652 }
653
Michael J. Spencer27781b72011-10-08 00:18:30 +0000654 std::for_each(InputFilenames.begin(), InputFilenames.end(),
655 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000656
657 return 0;
658}