blob: 570ec7ed6f89b3473c46347ce7083f09f64365d1 [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//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000014// The flags and output of this program should be near identical to those of
15// binutils objdump.
16//
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000017//===----------------------------------------------------------------------===//
18
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000019#include "llvm-objdump.h"
Benjamin Kramer685a2502011-07-20 19:37:35 +000020#include "MCFunction.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000021#include "llvm/ADT/OwningPtr.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000022#include "llvm/ADT/STLExtras.h"
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000023#include "llvm/ADT/StringExtras.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000024#include "llvm/ADT/Triple.h"
25#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +000026#include "llvm/MC/MCContext.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000027#include "llvm/MC/MCDisassembler.h"
28#include "llvm/MC/MCInst.h"
29#include "llvm/MC/MCInstPrinter.h"
Craig Topper17463b32012-04-02 06:09:36 +000030#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +000031#include "llvm/MC/MCObjectFileInfo.h"
32#include "llvm/MC/MCObjectSymbolizer.h"
Jim Grosbachc6449b62012-03-05 19:33:20 +000033#include "llvm/MC/MCRegisterInfo.h"
James Molloyb9505852011-09-07 17:24:38 +000034#include "llvm/MC/MCSubtargetInfo.h"
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +000035#include "llvm/MC/MCRelocationInfo.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000036#include "llvm/Object/Archive.h"
37#include "llvm/Object/COFF.h"
Rafael Espindolacef81b32012-12-21 03:47:03 +000038#include "llvm/Object/MachO.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000039#include "llvm/Object/ObjectFile.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000040#include "llvm/Support/Casting.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000041#include "llvm/Support/CommandLine.h"
42#include "llvm/Support/Debug.h"
Michael J. Spencer27781b72011-10-08 00:18:30 +000043#include "llvm/Support/FileSystem.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000044#include "llvm/Support/Format.h"
Benjamin Kramer853b0fd2011-07-25 23:04:36 +000045#include "llvm/Support/GraphWriter.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000046#include "llvm/Support/Host.h"
47#include "llvm/Support/ManagedStatic.h"
48#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000049#include "llvm/Support/MemoryObject.h"
50#include "llvm/Support/PrettyStackTrace.h"
51#include "llvm/Support/Signals.h"
52#include "llvm/Support/SourceMgr.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000053#include "llvm/Support/TargetRegistry.h"
54#include "llvm/Support/TargetSelect.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000055#include "llvm/Support/raw_ostream.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000056#include "llvm/Support/system_error.h"
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000057#include <algorithm>
Benjamin Kramer81bbdfd2012-03-23 11:49:32 +000058#include <cctype>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000059#include <cstring>
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000060using namespace llvm;
61using namespace object;
62
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000063static cl::list<std::string>
64InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000065
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000066static cl::opt<bool>
67Disassemble("disassemble",
68 cl::desc("Display assembler mnemonics for the machine instructions"));
69static cl::alias
70Disassembled("d", cl::desc("Alias for --disassemble"),
71 cl::aliasopt(Disassemble));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000072
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000073static cl::opt<bool>
Michael J. Spencer27781b72011-10-08 00:18:30 +000074Relocations("r", cl::desc("Display the relocation entries in the file"));
75
76static cl::opt<bool>
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +000077SectionContents("s", cl::desc("Display the content of each section"));
78
79static cl::opt<bool>
Michael J. Spencer22ff0f32011-10-18 19:32:17 +000080SymbolTable("t", cl::desc("Display the symbol table"));
81
82static cl::opt<bool>
Rafael Espindolacef81b32012-12-21 03:47:03 +000083MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000084static cl::alias
Rafael Espindolacef81b32012-12-21 03:47:03 +000085MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer685a2502011-07-20 19:37:35 +000086
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000087cl::opt<std::string>
88llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
89 "see -version for available targets"));
90
91cl::opt<std::string>
92llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer92e1deb2011-01-20 06:39:06 +000093 "see -version for available targets"));
94
Nick Lewycky023bb152011-10-10 21:21:34 +000095static cl::opt<bool>
96SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
97 "for each section."));
98static cl::alias
99SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
100 cl::aliasopt(SectionHeaders));
101static cl::alias
102SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
103 cl::aliasopt(SectionHeaders));
104
Jack Carterfd6d1652012-08-28 19:24:49 +0000105static cl::list<std::string>
106MAttrs("mattr",
107 cl::CommaSeparated,
108 cl::desc("Target specific attributes"),
109 cl::value_desc("a1,+a2,-a3,..."));
110
Eli Bendersky8b9da532012-11-20 22:57:02 +0000111static cl::opt<bool>
112NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, "
113 "do not print the instruction bytes."));
114
Michael J. Spencereef7b622012-12-05 20:12:35 +0000115static cl::opt<bool>
116UnwindInfo("unwind-info", cl::desc("Display unwind information"));
117
118static cl::alias
119UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
120 cl::aliasopt(UnwindInfo));
121
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000122static cl::opt<bool>
123PrivateHeaders("private-headers",
124 cl::desc("Display format specific file headers"));
125
126static cl::alias
127PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
128 cl::aliasopt(PrivateHeaders));
129
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000130static cl::opt<bool>
131Symbolize("symbolize", cl::desc("When disassembling instructions, "
132 "try to symbolize operands."));
133
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000134static StringRef ToolName;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000135
Michael J. Spencereef7b622012-12-05 20:12:35 +0000136bool llvm::error(error_code ec) {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000137 if (!ec) return false;
Michael J. Spencer25b15772011-06-25 17:55:23 +0000138
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000139 outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
140 outs().flush();
141 return true;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000142}
143
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000144static const Target *getTarget(const ObjectFile *Obj = NULL) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000145 // Figure out the target triple.
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000146 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000147 if (TripleName.empty()) {
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000148 if (Obj) {
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000149 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000150 // TheTriple defaults to ELF, and COFF doesn't have an environment:
151 // the best we can do here is indicate that it is mach-o.
152 if (Obj->isMachO())
153 TheTriple.setEnvironment(Triple::MachO);
154 }
Michael J. Spencerd11699d2011-01-20 07:22:04 +0000155 } else
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000156 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000157
158 // Get the target specific parser.
159 std::string Error;
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000160 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
161 Error);
162 if (!TheTarget) {
163 errs() << ToolName << ": " << Error;
164 return 0;
165 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000166
Kevin Enderby9ed9e5d2012-05-08 23:38:45 +0000167 // Update the triple name and return the found target.
168 TripleName = TheTriple.getTriple();
169 return TheTarget;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000170}
171
David Blaikie2d24e2a2011-12-20 02:50:00 +0000172void llvm::StringRefMemoryObject::anchor() { }
173
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000174void llvm::DumpBytes(StringRef bytes) {
175 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000176 // FIXME: The real way to do this is to figure out the longest instruction
177 // and align to that size before printing. I'll fix this when I get
178 // around to outputting relocations.
179 // 15 is the longest x86 instruction
180 // 3 is for the hex rep of a byte + a space.
181 // 1 is for the null terminator.
182 enum { OutputSize = (15 * 3) + 1 };
183 char output[OutputSize];
184
185 assert(bytes.size() <= 15
186 && "DumpBytes only supports instructions of up to 15 bytes");
187 memset(output, ' ', sizeof(output));
188 unsigned index = 0;
189 for (StringRef::iterator i = bytes.begin(),
190 e = bytes.end(); i != e; ++i) {
191 output[index] = hex_rep[(*i & 0xF0) >> 4];
192 output[index + 1] = hex_rep[*i & 0xF];
193 index += 3;
194 }
195
196 output[sizeof(output) - 1] = 0;
197 outs() << output;
198}
199
Michael J. Spencereef7b622012-12-05 20:12:35 +0000200bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000201 uint64_t a_addr, b_addr;
Rafael Espindola956ca722013-04-25 12:28:45 +0000202 if (error(a.getOffset(a_addr))) return false;
203 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000204 return a_addr < b_addr;
205}
206
207static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000208 const Target *TheTarget = getTarget(Obj);
209 // getTarget() will have already issued a diagnostic if necessary, so
210 // just bail here if it failed.
211 if (!TheTarget)
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000212 return;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000213
Jack Carterfd6d1652012-08-28 19:24:49 +0000214 // Package up features to be passed to target/subtarget
215 std::string FeaturesStr;
216 if (MAttrs.size()) {
217 SubtargetFeatures Features;
218 for (unsigned i = 0; i != MAttrs.size(); ++i)
219 Features.AddFeature(MAttrs[i]);
220 FeaturesStr = Features.getString();
221 }
222
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000223 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
224 if (!MRI) {
225 errs() << "error: no register info for target " << TripleName << "\n";
226 return;
227 }
228
229 // Set up disassembler.
230 OwningPtr<const MCAsmInfo> AsmInfo(
231 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000232 if (!AsmInfo) {
233 errs() << "error: no assembly info for target " << TripleName << "\n";
234 return;
235 }
236
237 OwningPtr<const MCSubtargetInfo> STI(
238 TheTarget->createMCSubtargetInfo(TripleName, "", FeaturesStr));
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000239 if (!STI) {
240 errs() << "error: no subtarget info for target " << TripleName << "\n";
241 return;
242 }
243
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000244 OwningPtr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
245 if (!MII) {
246 errs() << "error: no instruction info for target " << TripleName << "\n";
247 return;
248 }
249
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000250 OwningPtr<MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
251 if (!DisAsm) {
252 errs() << "error: no disassembler for target " << TripleName << "\n";
253 return;
254 }
255
256 OwningPtr<const MCObjectFileInfo> MOFI;
257 OwningPtr<MCContext> Ctx;
258
259 if (Symbolize) {
260 MOFI.reset(new MCObjectFileInfo);
261 Ctx.reset(new MCContext(*AsmInfo.get(), *MRI.get(), MOFI.get()));
262 OwningPtr<MCRelocationInfo> RelInfo(
263 TheTarget->createMCRelocationInfo(TripleName, *Ctx.get()));
264 if (RelInfo) {
265 OwningPtr<MCSymbolizer> Symzer(
266 MCObjectSymbolizer::createObjectSymbolizer(*Ctx.get(), RelInfo, Obj));
267 if (Symzer)
268 DisAsm->setSymbolizer(Symzer);
269 }
270 }
271
Ahmed Bougacha27a33ad2013-05-16 21:28:23 +0000272 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
273 OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
274 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
275 if (!IP) {
276 errs() << "error: no instruction printer for target " << TripleName
277 << '\n';
278 return;
279 }
280
Michael J. Spencer25b15772011-06-25 17:55:23 +0000281 error_code ec;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000282 for (section_iterator i = Obj->begin_sections(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000283 e = Obj->end_sections();
284 i != e; i.increment(ec)) {
Michael J. Spencer25b15772011-06-25 17:55:23 +0000285 if (error(ec)) break;
286 bool text;
287 if (error(i->isText(text))) break;
288 if (!text) continue;
289
Michael J. Spencer942eb002011-10-13 22:17:18 +0000290 uint64_t SectionAddr;
291 if (error(i->getAddress(SectionAddr))) break;
292
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000293 // Make a list of all the symbols in this section.
294 std::vector<std::pair<uint64_t, StringRef> > Symbols;
Michael J. Spencer4344b1e2011-10-07 19:25:32 +0000295 for (symbol_iterator si = Obj->begin_symbols(),
Michael J. Spencer27781b72011-10-08 00:18:30 +0000296 se = Obj->end_symbols();
297 si != se; si.increment(ec)) {
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000298 bool contains;
299 if (!error(i->containsSymbol(*si, contains)) && contains) {
300 uint64_t Address;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000301 if (error(si->getAddress(Address))) break;
Eric Christopher99ff2ba2013-04-03 18:31:23 +0000302 if (Address == UnknownAddressOrSize) continue;
Cameron Zwarichaab21912012-02-03 04:13:37 +0000303 Address -= SectionAddr;
304
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000305 StringRef Name;
306 if (error(si->getName(Name))) break;
307 Symbols.push_back(std::make_pair(Address, Name));
308 }
309 }
310
311 // Sort the symbols by address, just in case they didn't come in that way.
312 array_pod_sort(Symbols.begin(), Symbols.end());
313
Michael J. Spencer942eb002011-10-13 22:17:18 +0000314 // Make a list of all the relocations for this section.
315 std::vector<RelocationRef> Rels;
316 if (InlineRelocs) {
317 for (relocation_iterator ri = i->begin_relocations(),
318 re = i->end_relocations();
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000319 ri != re; ri.increment(ec)) {
Michael J. Spencer942eb002011-10-13 22:17:18 +0000320 if (error(ec)) break;
321 Rels.push_back(*ri);
322 }
323 }
324
325 // Sort relocations by address.
326 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
327
Rafael Espindolacef81b32012-12-21 03:47:03 +0000328 StringRef SegmentName = "";
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000329 if (const MachOObjectFile *MachO =
330 dyn_cast<const MachOObjectFile>(Obj)) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000331 DataRefImpl DR = i->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000332 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000333 }
Michael J. Spencer25b15772011-06-25 17:55:23 +0000334 StringRef name;
335 if (error(i->getName(name))) break;
Rafael Espindolacef81b32012-12-21 03:47:03 +0000336 outs() << "Disassembly of section ";
337 if (!SegmentName.empty())
338 outs() << SegmentName << ",";
339 outs() << name << ':';
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000340
341 // If the section has no symbols just insert a dummy one and disassemble
342 // the whole section.
343 if (Symbols.empty())
344 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000345
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000346
347 SmallString<40> Comments;
348 raw_svector_ostream CommentStream(Comments);
349
Michael J. Spencer25b15772011-06-25 17:55:23 +0000350 StringRef Bytes;
351 if (error(i->getContents(Bytes))) break;
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000352 StringRefMemoryObject memoryObject(Bytes, SectionAddr);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000353 uint64_t Size;
354 uint64_t Index;
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000355 uint64_t SectSize;
356 if (error(i->getSize(SectSize))) break;
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000357
Michael J. Spencer942eb002011-10-13 22:17:18 +0000358 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
359 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000360 // Disassemble symbol by symbol.
361 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
362 uint64_t Start = Symbols[si].first;
Michael J. Spencer178dbd42011-10-13 20:37:08 +0000363 uint64_t End;
364 // The end is either the size of the section or the beginning of the next
365 // symbol.
366 if (si == se - 1)
367 End = SectSize;
368 // Make sure this symbol takes up space.
369 else if (Symbols[si + 1].first != Start)
370 End = Symbols[si + 1].first - 1;
371 else
372 // This symbol has the same address as the next symbol. Skip it.
373 continue;
374
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000375 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000376
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000377#ifndef NDEBUG
378 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
379#else
380 raw_ostream &DebugOut = nulls();
381#endif
382
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000383 for (Index = Start; Index < End; Index += Size) {
384 MCInst Inst;
Owen Anderson98c5dda2011-09-15 23:38:46 +0000385
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000386 if (DisAsm->getInstruction(Inst, Size, memoryObject,
387 SectionAddr + Index,
388 DebugOut, CommentStream)) {
Eli Bendersky8b9da532012-11-20 22:57:02 +0000389 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
390 if (!NoShowRawInsn) {
391 outs() << "\t";
392 DumpBytes(StringRef(Bytes.data() + Index, Size));
393 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000394 IP->printInst(&Inst, outs(), "");
Ahmed Bougacha2c94d0f2013-05-24 00:39:57 +0000395 outs() << CommentStream.str();
396 Comments.clear();
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000397 outs() << "\n";
398 } else {
399 errs() << ToolName << ": warning: invalid instruction encoding\n";
400 if (Size == 0)
401 Size = 1; // skip illegible bytes
Benjamin Kramer739b65b2011-07-15 18:39:24 +0000402 }
Michael J. Spencer942eb002011-10-13 22:17:18 +0000403
404 // Print relocation for instruction.
405 while (rel_cur != rel_end) {
Owen Anderson0685e942011-10-25 20:35:53 +0000406 bool hidden = false;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000407 uint64_t addr;
408 SmallString<16> name;
409 SmallString<32> val;
Owen Anderson0685e942011-10-25 20:35:53 +0000410
411 // If this relocation is hidden, skip it.
412 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
413 if (hidden) goto skip_print_rel;
414
Rafael Espindola956ca722013-04-25 12:28:45 +0000415 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000416 // Stop when rel_cur's address is past the current instruction.
Owen Anderson34749ce2011-10-25 20:15:39 +0000417 if (addr >= Index + Size) break;
Michael J. Spencer942eb002011-10-13 22:17:18 +0000418 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
419 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
420
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000421 outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
422 << "\t" << val << "\n";
Michael J. Spencer942eb002011-10-13 22:17:18 +0000423
424 skip_print_rel:
425 ++rel_cur;
426 }
Benjamin Kramer685a2502011-07-20 19:37:35 +0000427 }
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000428 }
429 }
430}
431
Michael J. Spencer27781b72011-10-08 00:18:30 +0000432static void PrintRelocations(const ObjectFile *o) {
433 error_code ec;
434 for (section_iterator si = o->begin_sections(), se = o->end_sections();
435 si != se; si.increment(ec)){
436 if (error(ec)) return;
437 if (si->begin_relocations() == si->end_relocations())
438 continue;
439 StringRef secname;
440 if (error(si->getName(secname))) continue;
441 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
442 for (relocation_iterator ri = si->begin_relocations(),
443 re = si->end_relocations();
444 ri != re; ri.increment(ec)) {
445 if (error(ec)) return;
446
Owen Anderson0685e942011-10-25 20:35:53 +0000447 bool hidden;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000448 uint64_t address;
449 SmallString<32> relocname;
450 SmallString<32> valuestr;
Owen Anderson0685e942011-10-25 20:35:53 +0000451 if (error(ri->getHidden(hidden))) continue;
452 if (hidden) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000453 if (error(ri->getTypeName(relocname))) continue;
Rafael Espindola956ca722013-04-25 12:28:45 +0000454 if (error(ri->getOffset(address))) continue;
Michael J. Spencer27781b72011-10-08 00:18:30 +0000455 if (error(ri->getValueString(valuestr))) continue;
456 outs() << address << " " << relocname << " " << valuestr << "\n";
457 }
458 outs() << "\n";
459 }
460}
461
Nick Lewycky023bb152011-10-10 21:21:34 +0000462static void PrintSectionHeaders(const ObjectFile *o) {
463 outs() << "Sections:\n"
464 "Idx Name Size Address Type\n";
465 error_code ec;
466 unsigned i = 0;
467 for (section_iterator si = o->begin_sections(), se = o->end_sections();
468 si != se; si.increment(ec)) {
469 if (error(ec)) return;
470 StringRef Name;
471 if (error(si->getName(Name))) return;
472 uint64_t Address;
473 if (error(si->getAddress(Address))) return;
474 uint64_t Size;
475 if (error(si->getSize(Size))) return;
476 bool Text, Data, BSS;
477 if (error(si->isText(Text))) return;
478 if (error(si->isData(Data))) return;
479 if (error(si->isBSS(BSS))) return;
480 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer14a5f462011-10-13 20:37:20 +0000481 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Michael J. Spencer27b2b1b2013-01-10 22:40:50 +0000482 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000483 i, Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewycky023bb152011-10-10 21:21:34 +0000484 ++i;
485 }
486}
487
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000488static void PrintSectionContents(const ObjectFile *o) {
489 error_code ec;
490 for (section_iterator si = o->begin_sections(),
491 se = o->end_sections();
492 si != se; si.increment(ec)) {
493 if (error(ec)) return;
494 StringRef Name;
495 StringRef Contents;
496 uint64_t BaseAddr;
Alexey Samsonov0eaa6f62013-04-16 10:53:11 +0000497 bool BSS;
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000498 if (error(si->getName(Name))) continue;
499 if (error(si->getContents(Contents))) continue;
500 if (error(si->getAddress(BaseAddr))) continue;
Alexey Samsonov0eaa6f62013-04-16 10:53:11 +0000501 if (error(si->isBSS(BSS))) continue;
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000502
503 outs() << "Contents of section " << Name << ":\n";
Alexey Samsonov0eaa6f62013-04-16 10:53:11 +0000504 if (BSS) {
505 outs() << format("<skipping contents of bss section at [%04" PRIx64
506 ", %04" PRIx64 ")>\n", BaseAddr,
507 BaseAddr + Contents.size());
508 continue;
509 }
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000510
511 // Dump out the content as hex and printable ascii characters.
512 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000513 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000514 // Dump line of hex.
515 for (std::size_t i = 0; i < 16; ++i) {
516 if (i != 0 && i % 4 == 0)
517 outs() << ' ';
518 if (addr + i < end)
519 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
520 << hexdigit(Contents[addr + i] & 0xF, true);
521 else
522 outs() << " ";
523 }
524 // Print ascii.
525 outs() << " ";
526 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei87d0b9e2013-02-12 21:21:59 +0000527 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000528 outs() << Contents[addr + i];
529 else
530 outs() << ".";
531 }
532 outs() << "\n";
533 }
534 }
535}
536
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000537static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
538 const coff_file_header *header;
539 if (error(coff->getHeader(header))) return;
540 int aux_count = 0;
541 const coff_symbol *symbol = 0;
542 for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
543 if (aux_count--) {
544 // Figure out which type of aux this is.
545 if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
546 && symbol->Value == 0) { // Section definition.
547 const coff_aux_section_definition *asd;
548 if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
549 return;
550 outs() << "AUX "
551 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
552 , unsigned(asd->Length)
553 , unsigned(asd->NumberOfRelocations)
554 , unsigned(asd->NumberOfLinenumbers)
555 , unsigned(asd->CheckSum))
556 << format("assoc %d comdat %d\n"
557 , unsigned(asd->Number)
558 , unsigned(asd->Selection));
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000559 } else
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000560 outs() << "AUX Unknown\n";
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000561 } else {
562 StringRef name;
563 if (error(coff->getSymbol(i, symbol))) return;
564 if (error(coff->getSymbolName(symbol, name))) return;
565 outs() << "[" << format("%2d", i) << "]"
566 << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
567 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
568 << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
569 << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
570 << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
571 << "0x" << format("%08x", unsigned(symbol->Value)) << " "
572 << name << "\n";
573 aux_count = symbol->NumberOfAuxSymbols;
574 }
575 }
576}
577
578static void PrintSymbolTable(const ObjectFile *o) {
579 outs() << "SYMBOL TABLE:\n";
580
581 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
582 PrintCOFFSymbolTable(coff);
583 else {
584 error_code ec;
585 for (symbol_iterator si = o->begin_symbols(),
586 se = o->end_symbols(); si != se; si.increment(ec)) {
587 if (error(ec)) return;
588 StringRef Name;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000589 uint64_t Address;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000590 SymbolRef::Type Type;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000591 uint64_t Size;
David Meyerc46255a2012-02-28 23:47:53 +0000592 uint32_t Flags;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000593 section_iterator Section = o->end_sections();
594 if (error(si->getName(Name))) continue;
Danil Malyshevb0436a72011-11-29 17:40:10 +0000595 if (error(si->getAddress(Address))) continue;
David Meyerc46255a2012-02-28 23:47:53 +0000596 if (error(si->getFlags(Flags))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000597 if (error(si->getType(Type))) continue;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000598 if (error(si->getSize(Size))) continue;
599 if (error(si->getSection(Section))) continue;
600
David Meyerc46255a2012-02-28 23:47:53 +0000601 bool Global = Flags & SymbolRef::SF_Global;
602 bool Weak = Flags & SymbolRef::SF_Weak;
603 bool Absolute = Flags & SymbolRef::SF_Absolute;
604
Danil Malyshevb0436a72011-11-29 17:40:10 +0000605 if (Address == UnknownAddressOrSize)
606 Address = 0;
607 if (Size == UnknownAddressOrSize)
608 Size = 0;
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000609 char GlobLoc = ' ';
David Meyer2c677272012-02-29 02:11:55 +0000610 if (Type != SymbolRef::ST_Unknown)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000611 GlobLoc = Global ? 'g' : 'l';
612 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
613 ? 'd' : ' ';
614 char FileFunc = ' ';
615 if (Type == SymbolRef::ST_File)
616 FileFunc = 'f';
617 else if (Type == SymbolRef::ST_Function)
618 FileFunc = 'F';
619
Michael J. Spencer27b2b1b2013-01-10 22:40:50 +0000620 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
621 "%08" PRIx64;
622
623 outs() << format(Fmt, Address) << " "
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000624 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
625 << (Weak ? 'w' : ' ') // Weak?
626 << ' ' // Constructor. Not supported yet.
627 << ' ' // Warning. Not supported yet.
628 << ' ' // Indirect reference to another symbol.
629 << Debug // Debugging (d) or dynamic (D) symbol.
630 << FileFunc // Name of function (F), file (f) or object (O).
631 << ' ';
632 if (Absolute)
633 outs() << "*ABS*";
634 else if (Section == o->end_sections())
635 outs() << "*UND*";
636 else {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000637 if (const MachOObjectFile *MachO =
638 dyn_cast<const MachOObjectFile>(o)) {
Rafael Espindolacef81b32012-12-21 03:47:03 +0000639 DataRefImpl DR = Section->getRawDataRefImpl();
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000640 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolacef81b32012-12-21 03:47:03 +0000641 outs() << SegmentName << ",";
642 }
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000643 StringRef SectionName;
644 if (error(Section->getName(SectionName)))
645 SectionName = "";
646 outs() << SectionName;
647 }
648 outs() << '\t'
Benjamin Kramer51cf8662012-03-10 02:04:38 +0000649 << format("%08" PRIx64 " ", Size)
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000650 << Name
651 << '\n';
652 }
653 }
654}
655
Michael J. Spencereef7b622012-12-05 20:12:35 +0000656static void PrintUnwindInfo(const ObjectFile *o) {
657 outs() << "Unwind info:\n\n";
658
659 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
660 printCOFFUnwindInfo(coff);
661 } else {
662 // TODO: Extract DWARF dump tool to objdump.
663 errs() << "This operation is only currently supported "
664 "for COFF object files.\n";
665 return;
666 }
667}
668
Michael J. Spencer27781b72011-10-08 00:18:30 +0000669static void DumpObject(const ObjectFile *o) {
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000670 outs() << '\n';
671 outs() << o->getFileName()
672 << ":\tfile format " << o->getFileFormatName() << "\n\n";
673
Michael J. Spencer27781b72011-10-08 00:18:30 +0000674 if (Disassemble)
Michael J. Spencer942eb002011-10-13 22:17:18 +0000675 DisassembleObject(o, Relocations);
676 if (Relocations && !Disassemble)
Michael J. Spencer27781b72011-10-08 00:18:30 +0000677 PrintRelocations(o);
Nick Lewycky023bb152011-10-10 21:21:34 +0000678 if (SectionHeaders)
679 PrintSectionHeaders(o);
Michael J. Spencer1e8ba3f2011-10-17 17:13:22 +0000680 if (SectionContents)
681 PrintSectionContents(o);
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000682 if (SymbolTable)
683 PrintSymbolTable(o);
Michael J. Spencereef7b622012-12-05 20:12:35 +0000684 if (UnwindInfo)
685 PrintUnwindInfo(o);
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000686 if (PrivateHeaders && o->isELF())
687 printELFFileHeader(o);
Michael J. Spencer27781b72011-10-08 00:18:30 +0000688}
689
690/// @brief Dump each object file in \a a;
691static void DumpArchive(const Archive *a) {
692 for (Archive::child_iterator i = a->begin_children(),
693 e = a->end_children(); i != e; ++i) {
694 OwningPtr<Binary> child;
695 if (error_code ec = i->getAsBinary(child)) {
Michael J. Spencerf81285c2011-11-16 01:24:41 +0000696 // Ignore non-object files.
697 if (ec != object_error::invalid_file_type)
698 errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
699 << ".\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000700 continue;
701 }
702 if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
703 DumpObject(o);
704 else
705 errs() << ToolName << ": '" << a->getFileName() << "': "
706 << "Unrecognized file type.\n";
707 }
708}
709
710/// @brief Open file and figure out how to dump it.
711static void DumpInput(StringRef file) {
712 // If file isn't stdin, check that it exists.
713 if (file != "-" && !sys::fs::exists(file)) {
714 errs() << ToolName << ": '" << file << "': " << "No such file\n";
715 return;
716 }
717
Rafael Espindolacef81b32012-12-21 03:47:03 +0000718 if (MachOOpt && Disassemble) {
Michael J. Spencer27781b72011-10-08 00:18:30 +0000719 DisassembleInputMachO(file);
720 return;
721 }
722
723 // Attempt to open the binary.
724 OwningPtr<Binary> binary;
725 if (error_code ec = createBinary(file, binary)) {
726 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
727 return;
728 }
729
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000730 if (Archive *a = dyn_cast<Archive>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000731 DumpArchive(a);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000732 else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get()))
Michael J. Spencer27781b72011-10-08 00:18:30 +0000733 DumpObject(o);
Jim Grosbach3f5d1a22012-08-07 17:53:14 +0000734 else
Michael J. Spencer27781b72011-10-08 00:18:30 +0000735 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencer27781b72011-10-08 00:18:30 +0000736}
737
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000738int main(int argc, char **argv) {
739 // Print a stack trace if we signal out.
740 sys::PrintStackTraceOnErrorSignal();
741 PrettyStackTraceProgram X(argc, argv);
742 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
743
744 // Initialize targets and assembly printers/parsers.
745 llvm::InitializeAllTargetInfos();
Evan Chenge78085a2011-07-22 21:58:54 +0000746 llvm::InitializeAllTargetMCs();
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000747 llvm::InitializeAllAsmParsers();
748 llvm::InitializeAllDisassemblers();
749
Pete Cooperff204962012-05-03 23:20:10 +0000750 // Register the target printer for --version.
751 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
752
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000753 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
754 TripleName = Triple::normalize(TripleName);
755
756 ToolName = argv[0];
757
758 // Defaults to a.out if no filenames specified.
759 if (InputFilenames.size() == 0)
760 InputFilenames.push_back("a.out");
761
Michael J. Spencer22ff0f32011-10-18 19:32:17 +0000762 if (!Disassemble
763 && !Relocations
764 && !SectionHeaders
765 && !SectionContents
Michael J. Spencereef7b622012-12-05 20:12:35 +0000766 && !SymbolTable
Michael J. Spencerb2c064c2013-01-06 03:56:49 +0000767 && !UnwindInfo
768 && !PrivateHeaders) {
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000769 cl::PrintHelpMessage();
770 return 2;
771 }
772
Michael J. Spencer27781b72011-10-08 00:18:30 +0000773 std::for_each(InputFilenames.begin(), InputFilenames.end(),
774 DumpInput);
Michael J. Spencer92e1deb2011-01-20 06:39:06 +0000775
776 return 0;
777}