blob: c61a5b2a67b1939a61b1101ac935639c498391ca [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
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000377 StringRef Bytes;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000378 if (error(Section.getContents(Bytes)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000379 break;
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000380 StringRefMemoryObject memoryObject(Bytes, SectionAddr);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000381 uint64_t Size;
382 uint64_t Index;
383
Michael J. Spencer51862b32011-10-13 22:17:18 +0000384 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
385 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000386 // Disassemble symbol by symbol.
387 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000388
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000389 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000390 // The end is either the section end or the beginning of the next symbol.
391 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
392 // If this symbol has the same address as the next symbol, then skip it.
393 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000394 continue;
395
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000396 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000397
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000398#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000399 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000400#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000401 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000402#endif
403
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000404 for (Index = Start; Index < End; Index += Size) {
405 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000406
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000407 if (DisAsm->getInstruction(Inst, Size, memoryObject,
Alp Tokere69170a2014-06-26 22:52:05 +0000408 SectionAddr + Index,
409 DebugOut, CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000410 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
411 if (!NoShowRawInsn) {
412 outs() << "\t";
413 DumpBytes(StringRef(Bytes.data() + Index, Size));
414 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000415 IP->printInst(&Inst, outs(), "");
Alp Tokere69170a2014-06-26 22:52:05 +0000416 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000417 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000418 outs() << "\n";
419 } else {
420 errs() << ToolName << ": warning: invalid instruction encoding\n";
421 if (Size == 0)
422 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000423 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000424
425 // Print relocation for instruction.
426 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000427 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000428 uint64_t addr;
429 SmallString<16> name;
430 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000431
432 // If this relocation is hidden, skip it.
433 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
434 if (hidden) goto skip_print_rel;
435
Rafael Espindola1e483872013-04-25 12:28:45 +0000436 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000437 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000438 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000439 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
440 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
441
Greg Fitzgerald18432272014-03-20 22:55:15 +0000442 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000443 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000444
445 skip_print_rel:
446 ++rel_cur;
447 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000448 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000449 }
450 }
451}
452
Alexey Samsonov48803e52014-03-13 14:37:36 +0000453static void PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000454 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
455 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000456 // Regular objdump doesn't print relocations in non-relocatable object
457 // files.
458 if (!Obj->isRelocatableObject())
459 return;
460
Alexey Samsonov48803e52014-03-13 14:37:36 +0000461 for (const SectionRef &Section : Obj->sections()) {
462 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000463 continue;
464 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000465 if (error(Section.getName(secname)))
466 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000467 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000468 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000469 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000470 uint64_t address;
471 SmallString<32> relocname;
472 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000473 if (error(Reloc.getHidden(hidden)))
474 continue;
475 if (hidden)
476 continue;
477 if (error(Reloc.getTypeName(relocname)))
478 continue;
479 if (error(Reloc.getOffset(address)))
480 continue;
481 if (error(Reloc.getValueString(valuestr)))
482 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000483 outs() << format(Fmt.data(), address) << " " << relocname << " "
484 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000485 }
486 outs() << "\n";
487 }
488}
489
Alexey Samsonov48803e52014-03-13 14:37:36 +0000490static void PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000491 outs() << "Sections:\n"
492 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000493 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000494 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000495 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000496 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000497 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000498 uint64_t Address = Section.getAddress();
499 uint64_t Size = Section.getSize();
500 bool Text = Section.isText();
501 bool Data = Section.isData();
502 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000503 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000504 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000505 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
506 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000507 ++i;
508 }
509}
510
Alexey Samsonov48803e52014-03-13 14:37:36 +0000511static void PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000512 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000513 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000514 StringRef Name;
515 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000516 if (error(Section.getName(Name)))
517 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000518 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000519 uint64_t Size = Section.getSize();
520 if (!Size)
521 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000522
523 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000524 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000525 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000526 ", %04" PRIx64 ")>\n",
527 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000528 continue;
529 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000530
David Majnemer8f6b04c2014-07-14 16:20:14 +0000531 if (error(Section.getContents(Contents)))
532 continue;
533
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000534 // Dump out the content as hex and printable ascii characters.
535 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000536 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000537 // Dump line of hex.
538 for (std::size_t i = 0; i < 16; ++i) {
539 if (i != 0 && i % 4 == 0)
540 outs() << ' ';
541 if (addr + i < end)
542 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
543 << hexdigit(Contents[addr + i] & 0xF, true);
544 else
545 outs() << " ";
546 }
547 // Print ascii.
548 outs() << " ";
549 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000550 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000551 outs() << Contents[addr + i];
552 else
553 outs() << ".";
554 }
555 outs() << "\n";
556 }
557 }
558}
559
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000560static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000561 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
562 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000563 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000564 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000565 return;
566
David Majnemer44f51e52014-09-10 12:51:52 +0000567 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000568 return;
569
570 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000571 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000572 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000573 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
574 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
575 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
576 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000577 << Name << "\n";
578
David Majnemer44f51e52014-09-10 12:51:52 +0000579 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000580 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000581 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000582 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000583 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000584
David Majnemer4d571592014-09-15 19:42:42 +0000585 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
586
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000587 outs() << "AUX "
588 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
589 , unsigned(asd->Length)
590 , unsigned(asd->NumberOfRelocations)
591 , unsigned(asd->NumberOfLinenumbers)
592 , unsigned(asd->CheckSum))
593 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000594 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000595 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000596 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000597 const char *FileName;
598 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000599 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000600
David Majnemer44f51e52014-09-10 12:51:52 +0000601 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
602 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000603 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000604
David Majnemer44f51e52014-09-10 12:51:52 +0000605 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000606 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000607 } else {
608 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000609 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000610 }
611 }
612}
613
614static void PrintSymbolTable(const ObjectFile *o) {
615 outs() << "SYMBOL TABLE:\n";
616
Rui Ueyama4e39f712014-03-18 18:58:51 +0000617 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000618 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000619 return;
620 }
621 for (const SymbolRef &Symbol : o->symbols()) {
622 StringRef Name;
623 uint64_t Address;
624 SymbolRef::Type Type;
625 uint64_t Size;
626 uint32_t Flags = Symbol.getFlags();
627 section_iterator Section = o->section_end();
628 if (error(Symbol.getName(Name)))
629 continue;
630 if (error(Symbol.getAddress(Address)))
631 continue;
632 if (error(Symbol.getType(Type)))
633 continue;
634 if (error(Symbol.getSize(Size)))
635 continue;
636 if (error(Symbol.getSection(Section)))
637 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000638
Rui Ueyama4e39f712014-03-18 18:58:51 +0000639 bool Global = Flags & SymbolRef::SF_Global;
640 bool Weak = Flags & SymbolRef::SF_Weak;
641 bool Absolute = Flags & SymbolRef::SF_Absolute;
David Meyer1df4b842012-02-28 23:47:53 +0000642
Rui Ueyama4e39f712014-03-18 18:58:51 +0000643 if (Address == UnknownAddressOrSize)
644 Address = 0;
645 if (Size == UnknownAddressOrSize)
646 Size = 0;
647 char GlobLoc = ' ';
648 if (Type != SymbolRef::ST_Unknown)
649 GlobLoc = Global ? 'g' : 'l';
650 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
651 ? 'd' : ' ';
652 char FileFunc = ' ';
653 if (Type == SymbolRef::ST_File)
654 FileFunc = 'f';
655 else if (Type == SymbolRef::ST_Function)
656 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000657
Rui Ueyama4e39f712014-03-18 18:58:51 +0000658 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
659 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000660
Rui Ueyama4e39f712014-03-18 18:58:51 +0000661 outs() << format(Fmt, Address) << " "
662 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
663 << (Weak ? 'w' : ' ') // Weak?
664 << ' ' // Constructor. Not supported yet.
665 << ' ' // Warning. Not supported yet.
666 << ' ' // Indirect reference to another symbol.
667 << Debug // Debugging (d) or dynamic (D) symbol.
668 << FileFunc // Name of function (F), file (f) or object (O).
669 << ' ';
670 if (Absolute) {
671 outs() << "*ABS*";
672 } else if (Section == o->section_end()) {
673 outs() << "*UND*";
674 } else {
675 if (const MachOObjectFile *MachO =
676 dyn_cast<const MachOObjectFile>(o)) {
677 DataRefImpl DR = Section->getRawDataRefImpl();
678 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
679 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000680 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000681 StringRef SectionName;
682 if (error(Section->getName(SectionName)))
683 SectionName = "";
684 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000685 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000686 outs() << '\t'
687 << format("%08" PRIx64 " ", Size)
688 << Name
689 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000690 }
691}
692
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000693static void PrintUnwindInfo(const ObjectFile *o) {
694 outs() << "Unwind info:\n\n";
695
696 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
697 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000698 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
699 printMachOUnwindInfo(MachO);
700 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000701 // TODO: Extract DWARF dump tool to objdump.
702 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000703 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000704 return;
705 }
706}
707
Nick Kledzikd04bc352014-08-30 00:20:14 +0000708static void printExportsTrie(const ObjectFile *o) {
709 outs() << "Exports trie:\n";
710 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
711 printMachOExportsTrie(MachO);
712 else {
713 errs() << "This operation is only currently supported "
714 "for Mach-O executable files.\n";
715 return;
716 }
717}
718
Nick Kledzikac431442014-09-12 21:34:15 +0000719static void printRebaseTable(const ObjectFile *o) {
720 outs() << "Rebase table:\n";
721 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
722 printMachORebaseTable(MachO);
723 else {
724 errs() << "This operation is only currently supported "
725 "for Mach-O executable files.\n";
726 return;
727 }
728}
729
Nick Kledzik56ebef42014-09-16 01:41:51 +0000730static void printBindTable(const ObjectFile *o) {
731 outs() << "Bind table:\n";
732 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
733 printMachOBindTable(MachO);
734 else {
735 errs() << "This operation is only currently supported "
736 "for Mach-O executable files.\n";
737 return;
738 }
739}
740
741static void printLazyBindTable(const ObjectFile *o) {
742 outs() << "Lazy bind table:\n";
743 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
744 printMachOLazyBindTable(MachO);
745 else {
746 errs() << "This operation is only currently supported "
747 "for Mach-O executable files.\n";
748 return;
749 }
750}
751
752static void printWeakBindTable(const ObjectFile *o) {
753 outs() << "Weak bind table:\n";
754 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
755 printMachOWeakBindTable(MachO);
756 else {
757 errs() << "This operation is only currently supported "
758 "for Mach-O executable files.\n";
759 return;
760 }
761}
Nick Kledzikac431442014-09-12 21:34:15 +0000762
Rui Ueyamac2bed422013-09-27 21:04:00 +0000763static void printPrivateFileHeader(const ObjectFile *o) {
764 if (o->isELF()) {
765 printELFFileHeader(o);
766 } else if (o->isCOFF()) {
767 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000768 } else if (o->isMachO()) {
769 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000770 }
771}
772
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000773static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000774 outs() << '\n';
775 outs() << o->getFileName()
776 << ":\tfile format " << o->getFileFormatName() << "\n\n";
777
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000778 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000779 DisassembleObject(o, Relocations);
780 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000781 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000782 if (SectionHeaders)
783 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000784 if (SectionContents)
785 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000786 if (SymbolTable)
787 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000788 if (UnwindInfo)
789 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000790 if (PrivateHeaders)
791 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000792 if (ExportsTrie)
793 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000794 if (Rebase)
795 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000796 if (Bind)
797 printBindTable(o);
798 if (LazyBind)
799 printLazyBindTable(o);
800 if (WeakBind)
801 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000802}
803
804/// @brief Dump each object file in \a a;
805static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000806 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
807 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000808 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
809 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000810 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000811 if (EC != object_error::invalid_file_type)
812 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000813 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000814 continue;
815 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000816 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000817 DumpObject(o);
818 else
819 errs() << ToolName << ": '" << a->getFileName() << "': "
820 << "Unrecognized file type.\n";
821 }
822}
823
824/// @brief Open file and figure out how to dump it.
825static void DumpInput(StringRef file) {
826 // If file isn't stdin, check that it exists.
827 if (file != "-" && !sys::fs::exists(file)) {
828 errs() << ToolName << ": '" << file << "': " << "No such file\n";
829 return;
830 }
831
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000832 if (MachOOpt && Disassemble) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000833 DisassembleInputMachO(file);
834 return;
835 }
836
837 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000838 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000839 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000840 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000841 return;
842 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000843 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000844
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000845 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000846 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000847 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000848 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000849 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000850 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000851}
852
Michael J. Spencer2670c252011-01-20 06:39:06 +0000853int main(int argc, char **argv) {
854 // Print a stack trace if we signal out.
855 sys::PrintStackTraceOnErrorSignal();
856 PrettyStackTraceProgram X(argc, argv);
857 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
858
859 // Initialize targets and assembly printers/parsers.
860 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000861 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000862 llvm::InitializeAllAsmParsers();
863 llvm::InitializeAllDisassemblers();
864
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000865 // Register the target printer for --version.
866 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
867
Michael J. Spencer2670c252011-01-20 06:39:06 +0000868 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
869 TripleName = Triple::normalize(TripleName);
870
871 ToolName = argv[0];
872
873 // Defaults to a.out if no filenames specified.
874 if (InputFilenames.size() == 0)
875 InputFilenames.push_back("a.out");
876
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000877 if (!Disassemble
878 && !Relocations
879 && !SectionHeaders
880 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000881 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000882 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000883 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000884 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000885 && !Rebase
886 && !Bind
887 && !LazyBind
888 && !WeakBind) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000889 cl::PrintHelpMessage();
890 return 2;
891 }
892
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000893 std::for_each(InputFilenames.begin(), InputFilenames.end(),
894 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000895
896 return 0;
897}