blob: da5c275be8dc3906796384e0b56c98194d098b95 [file] [log] [blame]
Michael J. Spencer2670c252011-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. Spencerd7e70032013-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. Spencer2670c252011-01-20 06:39:06 +000017//===----------------------------------------------------------------------===//
18
Benjamin Kramer43a772e2011-09-19 17:56:04 +000019#include "llvm-objdump.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/ADT/STLExtras.h"
Michael J. Spencer4e25c022011-10-17 17:13:22 +000021#include "llvm/ADT/StringExtras.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000022#include "llvm/ADT/Triple.h"
23#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000024#include "llvm/MC/MCContext.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000025#include "llvm/MC/MCDisassembler.h"
26#include "llvm/MC/MCInst.h"
27#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000028#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper54bfde72012-04-02 06:09:36 +000029#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000030#include "llvm/MC/MCObjectFileInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000031#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000032#include "llvm/MC/MCRelocationInfo.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000033#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Object/Archive.h"
35#include "llvm/Object/COFF.h"
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000036#include "llvm/Object/MachO.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Object/ObjectFile.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000038#include "llvm/Support/Casting.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000039#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/Debug.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000041#include "llvm/Support/FileSystem.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000042#include "llvm/Support/Format.h"
Benjamin Kramerbf115312011-07-25 23:04:36 +000043#include "llvm/Support/GraphWriter.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000044#include "llvm/Support/Host.h"
45#include "llvm/Support/ManagedStatic.h"
46#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000047#include "llvm/Support/MemoryObject.h"
48#include "llvm/Support/PrettyStackTrace.h"
49#include "llvm/Support/Signals.h"
50#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000051#include "llvm/Support/TargetRegistry.h"
52#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000053#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000054#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000055#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000056#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000057#include <system_error>
Ahmed Bougacha17926472013-08-21 07:29:02 +000058
Michael J. Spencer2670c252011-01-20 06:39:06 +000059using namespace llvm;
60using namespace object;
61
Benjamin Kramer43a772e2011-09-19 17:56:04 +000062static cl::list<std::string>
63InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000064
Benjamin Kramer43a772e2011-09-19 17:56:04 +000065static cl::opt<bool>
66Disassemble("disassemble",
67 cl::desc("Display assembler mnemonics for the machine instructions"));
68static cl::alias
69Disassembled("d", cl::desc("Alias for --disassemble"),
70 cl::aliasopt(Disassemble));
Michael J. Spencer2670c252011-01-20 06:39:06 +000071
Benjamin Kramer43a772e2011-09-19 17:56:04 +000072static cl::opt<bool>
Michael J. Spencerba4a3622011-10-08 00:18:30 +000073Relocations("r", cl::desc("Display the relocation entries in the file"));
74
75static cl::opt<bool>
Michael J. Spencer4e25c022011-10-17 17:13:22 +000076SectionContents("s", cl::desc("Display the content of each section"));
77
78static cl::opt<bool>
Michael J. Spencerbfa06782011-10-18 19:32:17 +000079SymbolTable("t", cl::desc("Display the symbol table"));
80
81static cl::opt<bool>
Nick Kledzikd04bc352014-08-30 00:20:14 +000082ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
83
84static cl::opt<bool>
Nick Kledzikac431442014-09-12 21:34:15 +000085Rebase("rebase", cl::desc("Display mach-o rebasing info"));
86
87static cl::opt<bool>
Nick Kledzik56ebef42014-09-16 01:41:51 +000088Bind("bind", cl::desc("Display mach-o binding info"));
89
90static cl::opt<bool>
91LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
92
93static cl::opt<bool>
94WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
95
96static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000097MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +000098static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000099MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000100
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000101cl::opt<std::string>
102llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
103 "see -version for available targets"));
104
105cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000106llvm::MCPU("mcpu",
107 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
108 cl::value_desc("cpu-name"),
109 cl::init(""));
110
111cl::opt<std::string>
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000112llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000113 "see -version for available targets"));
114
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000115static cl::opt<bool>
116SectionHeaders("section-headers", cl::desc("Display summaries of the headers "
117 "for each section."));
118static cl::alias
119SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
120 cl::aliasopt(SectionHeaders));
121static cl::alias
122SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
123 cl::aliasopt(SectionHeaders));
124
Kevin Enderbyc9595622014-08-06 23:24:41 +0000125cl::list<std::string>
126llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000127 cl::CommaSeparated,
128 cl::desc("Target specific attributes"),
129 cl::value_desc("a1,+a2,-a3,..."));
130
Kevin Enderbybf246f52014-09-24 23:08:22 +0000131cl::opt<bool>
132llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
133 "instructions, do not print "
134 "the instruction bytes."));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000135
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000136static cl::opt<bool>
137UnwindInfo("unwind-info", cl::desc("Display unwind information"));
138
139static cl::alias
140UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
141 cl::aliasopt(UnwindInfo));
142
Michael J. Spencer209565db2013-01-06 03:56:49 +0000143static cl::opt<bool>
144PrivateHeaders("private-headers",
145 cl::desc("Display format specific file headers"));
146
147static cl::alias
148PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
149 cl::aliasopt(PrivateHeaders));
150
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000151static StringRef ToolName;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000152
Rafael Espindola4453e42942014-06-13 03:07:50 +0000153bool llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000154 if (!EC)
155 return false;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000156
Mark Seaborneb03ac52014-01-25 00:32:01 +0000157 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000158 outs().flush();
159 return true;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000160}
161
Craig Toppere6cb63e2014-04-25 04:24:47 +0000162static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000163 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000164 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000165 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000166 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000167 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000168 // TheTriple defaults to ELF, and COFF doesn't have an environment:
169 // the best we can do here is indicate that it is mach-o.
170 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000171 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000172
173 if (Obj->isCOFF()) {
174 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
175 if (COFFObj->getArch() == Triple::thumb)
176 TheTriple.setTriple("thumbv7-windows");
177 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000178 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000179 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000180 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000181
182 // Get the target specific parser.
183 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000184 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
185 Error);
186 if (!TheTarget) {
187 errs() << ToolName << ": " << Error;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000188 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000189 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000190
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000191 // Update the triple name and return the found target.
192 TripleName = TheTriple.getTriple();
193 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000194}
195
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000196void llvm::DumpBytes(StringRef bytes) {
197 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000198 // FIXME: The real way to do this is to figure out the longest instruction
199 // and align to that size before printing. I'll fix this when I get
200 // around to outputting relocations.
201 // 15 is the longest x86 instruction
202 // 3 is for the hex rep of a byte + a space.
203 // 1 is for the null terminator.
204 enum { OutputSize = (15 * 3) + 1 };
205 char output[OutputSize];
206
207 assert(bytes.size() <= 15
208 && "DumpBytes only supports instructions of up to 15 bytes");
209 memset(output, ' ', sizeof(output));
210 unsigned index = 0;
211 for (StringRef::iterator i = bytes.begin(),
212 e = bytes.end(); i != e; ++i) {
213 output[index] = hex_rep[(*i & 0xF0) >> 4];
214 output[index + 1] = hex_rep[*i & 0xF];
215 index += 3;
216 }
217
218 output[sizeof(output) - 1] = 0;
219 outs() << output;
220}
221
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000222bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer51862b32011-10-13 22:17:18 +0000223 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000224 if (error(a.getOffset(a_addr))) return false;
225 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000226 return a_addr < b_addr;
227}
228
229static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000230 const Target *TheTarget = getTarget(Obj);
231 // getTarget() will have already issued a diagnostic if necessary, so
232 // just bail here if it failed.
233 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000234 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000235
Jack Carter551efd72012-08-28 19:24:49 +0000236 // Package up features to be passed to target/subtarget
237 std::string FeaturesStr;
238 if (MAttrs.size()) {
239 SubtargetFeatures Features;
240 for (unsigned i = 0; i != MAttrs.size(); ++i)
241 Features.AddFeature(MAttrs[i]);
242 FeaturesStr = Features.getString();
243 }
244
Ahmed Charles56440fd2014-03-06 05:51:42 +0000245 std::unique_ptr<const MCRegisterInfo> MRI(
246 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000247 if (!MRI) {
248 errs() << "error: no register info for target " << TripleName << "\n";
249 return;
250 }
251
252 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000253 std::unique_ptr<const MCAsmInfo> AsmInfo(
254 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000255 if (!AsmInfo) {
256 errs() << "error: no assembly info for target " << TripleName << "\n";
257 return;
258 }
259
Ahmed Charles56440fd2014-03-06 05:51:42 +0000260 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000261 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000262 if (!STI) {
263 errs() << "error: no subtarget info for target " << TripleName << "\n";
264 return;
265 }
266
Ahmed Charles56440fd2014-03-06 05:51:42 +0000267 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000268 if (!MII) {
269 errs() << "error: no instruction info for target " << TripleName << "\n";
270 return;
271 }
272
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000273 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
274 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
275
276 std::unique_ptr<MCDisassembler> DisAsm(
277 TheTarget->createMCDisassembler(*STI, Ctx));
278
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000279 if (!DisAsm) {
280 errs() << "error: no disassembler for target " << TripleName << "\n";
281 return;
282 }
283
Ahmed Charles56440fd2014-03-06 05:51:42 +0000284 std::unique_ptr<const MCInstrAnalysis> MIA(
285 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000286
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000287 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000288 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000289 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
290 if (!IP) {
291 errs() << "error: no instruction printer for target " << TripleName
292 << '\n';
293 return;
294 }
295
Greg Fitzgerald18432272014-03-20 22:55:15 +0000296 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
297 "\t\t\t%08" PRIx64 ": ";
298
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000299 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
300 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000301 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000302 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
303 for (const SectionRef &Section : Obj->sections()) {
304 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000305 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000306 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000307 }
308
Alexey Samsonov48803e52014-03-13 14:37:36 +0000309 for (const SectionRef &Section : Obj->sections()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000310 bool Text = Section.isText();
Mark Seaborneb03ac52014-01-25 00:32:01 +0000311 if (!Text)
312 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000313
Rafael Espindola80291272014-10-08 15:28:58 +0000314 uint64_t SectionAddr = Section.getAddress();
315 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000316 if (!SectSize)
317 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000318
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000319 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000320 std::vector<std::pair<uint64_t, StringRef>> Symbols;
321 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000322 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000323 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000324 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000325 break;
326 if (Address == UnknownAddressOrSize)
327 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000328 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000329 if (Address >= SectSize)
330 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000331
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000332 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000333 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000334 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000335 Symbols.push_back(std::make_pair(Address, Name));
336 }
337 }
338
339 // Sort the symbols by address, just in case they didn't come in that way.
340 array_pod_sort(Symbols.begin(), Symbols.end());
341
Michael J. Spencer51862b32011-10-13 22:17:18 +0000342 // Make a list of all the relocations for this section.
343 std::vector<RelocationRef> Rels;
344 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000345 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
346 for (const RelocationRef &Reloc : RelocSec.relocations()) {
347 Rels.push_back(Reloc);
348 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000349 }
350 }
351
352 // Sort relocations by address.
353 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
354
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000355 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000356 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000357 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000358 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000359 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000360 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000361 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000362 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000363 outs() << "Disassembly of section ";
364 if (!SegmentName.empty())
365 outs() << SegmentName << ",";
366 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000367
368 // If the section has no symbols just insert a dummy one and disassemble
369 // the whole section.
370 if (Symbols.empty())
371 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000372
Alp Tokere69170a2014-06-26 22:52:05 +0000373
374 SmallString<40> Comments;
375 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000376
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000377 StringRef BytesStr;
378 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000379 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000380 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
381 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000382
Michael J. Spencer2670c252011-01-20 06:39:06 +0000383 uint64_t Size;
384 uint64_t Index;
385
Michael J. Spencer51862b32011-10-13 22:17:18 +0000386 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
387 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000388 // Disassemble symbol by symbol.
389 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000390
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000391 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000392 // The end is either the section end or the beginning of the next symbol.
393 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
394 // If this symbol has the same address as the next symbol, then skip it.
395 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000396 continue;
397
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000398 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000399
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000400#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000401 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000402#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000403 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000404#endif
405
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000406 for (Index = Start; Index < End; Index += Size) {
407 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000408
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000409 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
410 SectionAddr + Index, DebugOut,
411 CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000412 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
413 if (!NoShowRawInsn) {
414 outs() << "\t";
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000415 DumpBytes(StringRef(
416 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000417 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000418 IP->printInst(&Inst, outs(), "");
Alp Tokere69170a2014-06-26 22:52:05 +0000419 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000420 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000421 outs() << "\n";
422 } else {
423 errs() << ToolName << ": warning: invalid instruction encoding\n";
424 if (Size == 0)
425 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000426 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000427
428 // Print relocation for instruction.
429 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000430 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000431 uint64_t addr;
432 SmallString<16> name;
433 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000434
435 // If this relocation is hidden, skip it.
436 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
437 if (hidden) goto skip_print_rel;
438
Rafael Espindola1e483872013-04-25 12:28:45 +0000439 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000440 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000441 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000442 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
443 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
444
Greg Fitzgerald18432272014-03-20 22:55:15 +0000445 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000446 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000447
448 skip_print_rel:
449 ++rel_cur;
450 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000451 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000452 }
453 }
454}
455
Alexey Samsonov48803e52014-03-13 14:37:36 +0000456static void PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000457 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
458 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000459 // Regular objdump doesn't print relocations in non-relocatable object
460 // files.
461 if (!Obj->isRelocatableObject())
462 return;
463
Alexey Samsonov48803e52014-03-13 14:37:36 +0000464 for (const SectionRef &Section : Obj->sections()) {
465 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000466 continue;
467 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000468 if (error(Section.getName(secname)))
469 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000470 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000471 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000472 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000473 uint64_t address;
474 SmallString<32> relocname;
475 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000476 if (error(Reloc.getHidden(hidden)))
477 continue;
478 if (hidden)
479 continue;
480 if (error(Reloc.getTypeName(relocname)))
481 continue;
482 if (error(Reloc.getOffset(address)))
483 continue;
484 if (error(Reloc.getValueString(valuestr)))
485 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000486 outs() << format(Fmt.data(), address) << " " << relocname << " "
487 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000488 }
489 outs() << "\n";
490 }
491}
492
Alexey Samsonov48803e52014-03-13 14:37:36 +0000493static void PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000494 outs() << "Sections:\n"
495 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000496 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000497 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000498 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000499 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000500 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000501 uint64_t Address = Section.getAddress();
502 uint64_t Size = Section.getSize();
503 bool Text = Section.isText();
504 bool Data = Section.isData();
505 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000506 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000507 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000508 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
509 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000510 ++i;
511 }
512}
513
Alexey Samsonov48803e52014-03-13 14:37:36 +0000514static void PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000515 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000516 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000517 StringRef Name;
518 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000519 if (error(Section.getName(Name)))
520 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000521 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000522 uint64_t Size = Section.getSize();
523 if (!Size)
524 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000525
526 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000527 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000528 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000529 ", %04" PRIx64 ")>\n",
530 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000531 continue;
532 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000533
David Majnemer8f6b04c2014-07-14 16:20:14 +0000534 if (error(Section.getContents(Contents)))
535 continue;
536
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000537 // Dump out the content as hex and printable ascii characters.
538 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000539 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000540 // Dump line of hex.
541 for (std::size_t i = 0; i < 16; ++i) {
542 if (i != 0 && i % 4 == 0)
543 outs() << ' ';
544 if (addr + i < end)
545 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
546 << hexdigit(Contents[addr + i] & 0xF, true);
547 else
548 outs() << " ";
549 }
550 // Print ascii.
551 outs() << " ";
552 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000553 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000554 outs() << Contents[addr + i];
555 else
556 outs() << ".";
557 }
558 outs() << "\n";
559 }
560 }
561}
562
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000563static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000564 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
565 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000566 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000567 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000568 return;
569
David Majnemer44f51e52014-09-10 12:51:52 +0000570 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000571 return;
572
573 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000574 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000575 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000576 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
577 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
578 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
579 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000580 << Name << "\n";
581
David Majnemer44f51e52014-09-10 12:51:52 +0000582 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000583 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000584 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000585 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000586 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000587
David Majnemer4d571592014-09-15 19:42:42 +0000588 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
589
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000590 outs() << "AUX "
591 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
592 , unsigned(asd->Length)
593 , unsigned(asd->NumberOfRelocations)
594 , unsigned(asd->NumberOfLinenumbers)
595 , unsigned(asd->CheckSum))
596 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000597 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000598 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000599 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000600 const char *FileName;
601 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000602 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000603
David Majnemer44f51e52014-09-10 12:51:52 +0000604 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
605 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000606 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000607
David Majnemer44f51e52014-09-10 12:51:52 +0000608 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000609 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000610 } else {
611 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000612 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000613 }
614 }
615}
616
617static void PrintSymbolTable(const ObjectFile *o) {
618 outs() << "SYMBOL TABLE:\n";
619
Rui Ueyama4e39f712014-03-18 18:58:51 +0000620 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000621 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000622 return;
623 }
624 for (const SymbolRef &Symbol : o->symbols()) {
625 StringRef Name;
626 uint64_t Address;
627 SymbolRef::Type Type;
628 uint64_t Size;
629 uint32_t Flags = Symbol.getFlags();
630 section_iterator Section = o->section_end();
631 if (error(Symbol.getName(Name)))
632 continue;
633 if (error(Symbol.getAddress(Address)))
634 continue;
635 if (error(Symbol.getType(Type)))
636 continue;
637 if (error(Symbol.getSize(Size)))
638 continue;
639 if (error(Symbol.getSection(Section)))
640 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000641
Rui Ueyama4e39f712014-03-18 18:58:51 +0000642 bool Global = Flags & SymbolRef::SF_Global;
643 bool Weak = Flags & SymbolRef::SF_Weak;
644 bool Absolute = Flags & SymbolRef::SF_Absolute;
David Meyer1df4b842012-02-28 23:47:53 +0000645
Rui Ueyama4e39f712014-03-18 18:58:51 +0000646 if (Address == UnknownAddressOrSize)
647 Address = 0;
648 if (Size == UnknownAddressOrSize)
649 Size = 0;
650 char GlobLoc = ' ';
651 if (Type != SymbolRef::ST_Unknown)
652 GlobLoc = Global ? 'g' : 'l';
653 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
654 ? 'd' : ' ';
655 char FileFunc = ' ';
656 if (Type == SymbolRef::ST_File)
657 FileFunc = 'f';
658 else if (Type == SymbolRef::ST_Function)
659 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000660
Rui Ueyama4e39f712014-03-18 18:58:51 +0000661 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
662 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000663
Rui Ueyama4e39f712014-03-18 18:58:51 +0000664 outs() << format(Fmt, Address) << " "
665 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
666 << (Weak ? 'w' : ' ') // Weak?
667 << ' ' // Constructor. Not supported yet.
668 << ' ' // Warning. Not supported yet.
669 << ' ' // Indirect reference to another symbol.
670 << Debug // Debugging (d) or dynamic (D) symbol.
671 << FileFunc // Name of function (F), file (f) or object (O).
672 << ' ';
673 if (Absolute) {
674 outs() << "*ABS*";
675 } else if (Section == o->section_end()) {
676 outs() << "*UND*";
677 } else {
678 if (const MachOObjectFile *MachO =
679 dyn_cast<const MachOObjectFile>(o)) {
680 DataRefImpl DR = Section->getRawDataRefImpl();
681 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
682 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000683 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000684 StringRef SectionName;
685 if (error(Section->getName(SectionName)))
686 SectionName = "";
687 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000688 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000689 outs() << '\t'
690 << format("%08" PRIx64 " ", Size)
691 << Name
692 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000693 }
694}
695
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000696static void PrintUnwindInfo(const ObjectFile *o) {
697 outs() << "Unwind info:\n\n";
698
699 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
700 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000701 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
702 printMachOUnwindInfo(MachO);
703 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000704 // TODO: Extract DWARF dump tool to objdump.
705 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000706 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000707 return;
708 }
709}
710
Nick Kledzikd04bc352014-08-30 00:20:14 +0000711static void printExportsTrie(const ObjectFile *o) {
712 outs() << "Exports trie:\n";
713 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
714 printMachOExportsTrie(MachO);
715 else {
716 errs() << "This operation is only currently supported "
717 "for Mach-O executable files.\n";
718 return;
719 }
720}
721
Nick Kledzikac431442014-09-12 21:34:15 +0000722static void printRebaseTable(const ObjectFile *o) {
723 outs() << "Rebase table:\n";
724 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
725 printMachORebaseTable(MachO);
726 else {
727 errs() << "This operation is only currently supported "
728 "for Mach-O executable files.\n";
729 return;
730 }
731}
732
Nick Kledzik56ebef42014-09-16 01:41:51 +0000733static void printBindTable(const ObjectFile *o) {
734 outs() << "Bind table:\n";
735 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
736 printMachOBindTable(MachO);
737 else {
738 errs() << "This operation is only currently supported "
739 "for Mach-O executable files.\n";
740 return;
741 }
742}
743
744static void printLazyBindTable(const ObjectFile *o) {
745 outs() << "Lazy bind table:\n";
746 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
747 printMachOLazyBindTable(MachO);
748 else {
749 errs() << "This operation is only currently supported "
750 "for Mach-O executable files.\n";
751 return;
752 }
753}
754
755static void printWeakBindTable(const ObjectFile *o) {
756 outs() << "Weak bind table:\n";
757 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
758 printMachOWeakBindTable(MachO);
759 else {
760 errs() << "This operation is only currently supported "
761 "for Mach-O executable files.\n";
762 return;
763 }
764}
Nick Kledzikac431442014-09-12 21:34:15 +0000765
Rui Ueyamac2bed422013-09-27 21:04:00 +0000766static void printPrivateFileHeader(const ObjectFile *o) {
767 if (o->isELF()) {
768 printELFFileHeader(o);
769 } else if (o->isCOFF()) {
770 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000771 } else if (o->isMachO()) {
772 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000773 }
774}
775
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000776static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000777 outs() << '\n';
778 outs() << o->getFileName()
779 << ":\tfile format " << o->getFileFormatName() << "\n\n";
780
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000781 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000782 DisassembleObject(o, Relocations);
783 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000784 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000785 if (SectionHeaders)
786 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000787 if (SectionContents)
788 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000789 if (SymbolTable)
790 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000791 if (UnwindInfo)
792 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000793 if (PrivateHeaders)
794 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000795 if (ExportsTrie)
796 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000797 if (Rebase)
798 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000799 if (Bind)
800 printBindTable(o);
801 if (LazyBind)
802 printLazyBindTable(o);
803 if (WeakBind)
804 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000805}
806
807/// @brief Dump each object file in \a a;
808static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000809 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
810 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000811 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
812 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000813 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000814 if (EC != object_error::invalid_file_type)
815 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000816 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000817 continue;
818 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000819 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000820 DumpObject(o);
821 else
822 errs() << ToolName << ": '" << a->getFileName() << "': "
823 << "Unrecognized file type.\n";
824 }
825}
826
827/// @brief Open file and figure out how to dump it.
828static void DumpInput(StringRef file) {
829 // If file isn't stdin, check that it exists.
830 if (file != "-" && !sys::fs::exists(file)) {
831 errs() << ToolName << ": '" << file << "': " << "No such file\n";
832 return;
833 }
834
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000835 if (MachOOpt && Disassemble) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000836 DisassembleInputMachO(file);
837 return;
838 }
839
840 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000841 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000842 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000843 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000844 return;
845 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000846 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000847
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000848 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000849 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000850 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000851 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000852 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000853 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000854}
855
Michael J. Spencer2670c252011-01-20 06:39:06 +0000856int main(int argc, char **argv) {
857 // Print a stack trace if we signal out.
858 sys::PrintStackTraceOnErrorSignal();
859 PrettyStackTraceProgram X(argc, argv);
860 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
861
862 // Initialize targets and assembly printers/parsers.
863 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000864 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000865 llvm::InitializeAllAsmParsers();
866 llvm::InitializeAllDisassemblers();
867
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000868 // Register the target printer for --version.
869 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
870
Michael J. Spencer2670c252011-01-20 06:39:06 +0000871 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
872 TripleName = Triple::normalize(TripleName);
873
874 ToolName = argv[0];
875
876 // Defaults to a.out if no filenames specified.
877 if (InputFilenames.size() == 0)
878 InputFilenames.push_back("a.out");
879
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000880 if (!Disassemble
881 && !Relocations
882 && !SectionHeaders
883 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000884 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000885 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000886 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000887 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000888 && !Rebase
889 && !Bind
890 && !LazyBind
891 && !WeakBind) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000892 cl::PrintHelpMessage();
893 return 2;
894 }
895
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000896 std::for_each(InputFilenames.begin(), InputFilenames.end(),
897 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000898
899 return 0;
900}