blob: 4c753da7bf4815902c64823a848ad31072abb402 [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()) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000310 bool Text;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000311 if (error(Section.isText(Text)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000312 break;
313 if (!Text)
314 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000315
Michael J. Spencer51862b32011-10-13 22:17:18 +0000316 uint64_t SectionAddr;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000317 if (error(Section.getAddress(SectionAddr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000318 break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000319
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000320 uint64_t SectSize;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000321 if (error(Section.getSize(SectSize)))
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000322 break;
323
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000324 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000325 std::vector<std::pair<uint64_t, StringRef>> Symbols;
326 for (const SymbolRef &Symbol : Obj->symbols()) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000327 bool contains;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000328 if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000329 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000330 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000331 break;
332 if (Address == UnknownAddressOrSize)
333 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000334 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000335 if (Address >= SectSize)
336 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000337
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000338 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000339 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000340 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000341 Symbols.push_back(std::make_pair(Address, Name));
342 }
343 }
344
345 // Sort the symbols by address, just in case they didn't come in that way.
346 array_pod_sort(Symbols.begin(), Symbols.end());
347
Michael J. Spencer51862b32011-10-13 22:17:18 +0000348 // Make a list of all the relocations for this section.
349 std::vector<RelocationRef> Rels;
350 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000351 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
352 for (const RelocationRef &Reloc : RelocSec.relocations()) {
353 Rels.push_back(Reloc);
354 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000355 }
356 }
357
358 // Sort relocations by address.
359 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
360
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000361 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000362 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000363 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000364 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000365 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000366 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000367 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000368 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000369 outs() << "Disassembly of section ";
370 if (!SegmentName.empty())
371 outs() << SegmentName << ",";
372 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000373
374 // If the section has no symbols just insert a dummy one and disassemble
375 // the whole section.
376 if (Symbols.empty())
377 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000378
Alp Tokere69170a2014-06-26 22:52:05 +0000379
380 SmallString<40> Comments;
381 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000382
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000383 StringRef Bytes;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000384 if (error(Section.getContents(Bytes)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000385 break;
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000386 StringRefMemoryObject memoryObject(Bytes, SectionAddr);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000387 uint64_t Size;
388 uint64_t Index;
389
Michael J. Spencer51862b32011-10-13 22:17:18 +0000390 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
391 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000392 // Disassemble symbol by symbol.
393 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000394
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000395 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000396 // The end is either the section end or the beginning of the next symbol.
397 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
398 // If this symbol has the same address as the next symbol, then skip it.
399 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000400 continue;
401
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000402 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000403
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000404#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000405 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000406#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000407 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000408#endif
409
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000410 for (Index = Start; Index < End; Index += Size) {
411 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000412
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000413 if (DisAsm->getInstruction(Inst, Size, memoryObject,
Alp Tokere69170a2014-06-26 22:52:05 +0000414 SectionAddr + Index,
415 DebugOut, CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000416 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
417 if (!NoShowRawInsn) {
418 outs() << "\t";
419 DumpBytes(StringRef(Bytes.data() + Index, Size));
420 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000421 IP->printInst(&Inst, outs(), "");
Alp Tokere69170a2014-06-26 22:52:05 +0000422 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000423 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000424 outs() << "\n";
425 } else {
426 errs() << ToolName << ": warning: invalid instruction encoding\n";
427 if (Size == 0)
428 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000429 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000430
431 // Print relocation for instruction.
432 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000433 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000434 uint64_t addr;
435 SmallString<16> name;
436 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000437
438 // If this relocation is hidden, skip it.
439 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
440 if (hidden) goto skip_print_rel;
441
Rafael Espindola1e483872013-04-25 12:28:45 +0000442 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000443 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000444 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000445 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
446 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
447
Greg Fitzgerald18432272014-03-20 22:55:15 +0000448 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000449 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000450
451 skip_print_rel:
452 ++rel_cur;
453 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000454 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000455 }
456 }
457}
458
Alexey Samsonov48803e52014-03-13 14:37:36 +0000459static void PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000460 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
461 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000462 // Regular objdump doesn't print relocations in non-relocatable object
463 // files.
464 if (!Obj->isRelocatableObject())
465 return;
466
Alexey Samsonov48803e52014-03-13 14:37:36 +0000467 for (const SectionRef &Section : Obj->sections()) {
468 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000469 continue;
470 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000471 if (error(Section.getName(secname)))
472 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000473 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000474 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000475 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000476 uint64_t address;
477 SmallString<32> relocname;
478 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000479 if (error(Reloc.getHidden(hidden)))
480 continue;
481 if (hidden)
482 continue;
483 if (error(Reloc.getTypeName(relocname)))
484 continue;
485 if (error(Reloc.getOffset(address)))
486 continue;
487 if (error(Reloc.getValueString(valuestr)))
488 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000489 outs() << format(Fmt.data(), address) << " " << relocname << " "
490 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000491 }
492 outs() << "\n";
493 }
494}
495
Alexey Samsonov48803e52014-03-13 14:37:36 +0000496static void PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000497 outs() << "Sections:\n"
498 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000499 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000500 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000501 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000502 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000503 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000504 uint64_t Address;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000505 if (error(Section.getAddress(Address)))
506 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000507 uint64_t Size;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000508 if (error(Section.getSize(Size)))
509 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000510 bool Text, Data, BSS;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000511 if (error(Section.isText(Text)))
512 return;
513 if (error(Section.isData(Data)))
514 return;
515 if (error(Section.isBSS(BSS)))
516 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000517 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000518 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000519 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
520 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000521 ++i;
522 }
523}
524
Alexey Samsonov48803e52014-03-13 14:37:36 +0000525static void PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000526 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000527 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000528 StringRef Name;
529 StringRef Contents;
530 uint64_t BaseAddr;
Alexey Samsonov209095c2013-04-16 10:53:11 +0000531 bool BSS;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000532 if (error(Section.getName(Name)))
533 continue;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000534 if (error(Section.getAddress(BaseAddr)))
535 continue;
536 if (error(Section.isBSS(BSS)))
537 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000538
539 outs() << "Contents of section " << Name << ":\n";
Alexey Samsonov209095c2013-04-16 10:53:11 +0000540 if (BSS) {
David Majnemer8f6b04c2014-07-14 16:20:14 +0000541 uint64_t Size;
542 if (error(Section.getSize(Size)))
543 continue;
Alexey Samsonov209095c2013-04-16 10:53:11 +0000544 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000545 ", %04" PRIx64 ")>\n",
546 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000547 continue;
548 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000549
David Majnemer8f6b04c2014-07-14 16:20:14 +0000550 if (error(Section.getContents(Contents)))
551 continue;
552
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000553 // Dump out the content as hex and printable ascii characters.
554 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000555 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000556 // Dump line of hex.
557 for (std::size_t i = 0; i < 16; ++i) {
558 if (i != 0 && i % 4 == 0)
559 outs() << ' ';
560 if (addr + i < end)
561 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
562 << hexdigit(Contents[addr + i] & 0xF, true);
563 else
564 outs() << " ";
565 }
566 // Print ascii.
567 outs() << " ";
568 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000569 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000570 outs() << Contents[addr + i];
571 else
572 outs() << ".";
573 }
574 outs() << "\n";
575 }
576 }
577}
578
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000579static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000580 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
581 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000582 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000583 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000584 return;
585
David Majnemer44f51e52014-09-10 12:51:52 +0000586 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000587 return;
588
589 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000590 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000591 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000592 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
593 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
594 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
595 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000596 << Name << "\n";
597
David Majnemer44f51e52014-09-10 12:51:52 +0000598 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000599 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000600 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000601 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000602 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000603
David Majnemer4d571592014-09-15 19:42:42 +0000604 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
605
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000606 outs() << "AUX "
607 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
608 , unsigned(asd->Length)
609 , unsigned(asd->NumberOfRelocations)
610 , unsigned(asd->NumberOfLinenumbers)
611 , unsigned(asd->CheckSum))
612 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000613 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000614 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000615 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000616 const char *FileName;
617 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000618 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000619
David Majnemer44f51e52014-09-10 12:51:52 +0000620 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
621 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000622 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000623
David Majnemer44f51e52014-09-10 12:51:52 +0000624 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000625 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000626 } else {
627 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000628 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000629 }
630 }
631}
632
633static void PrintSymbolTable(const ObjectFile *o) {
634 outs() << "SYMBOL TABLE:\n";
635
Rui Ueyama4e39f712014-03-18 18:58:51 +0000636 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000637 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000638 return;
639 }
640 for (const SymbolRef &Symbol : o->symbols()) {
641 StringRef Name;
642 uint64_t Address;
643 SymbolRef::Type Type;
644 uint64_t Size;
645 uint32_t Flags = Symbol.getFlags();
646 section_iterator Section = o->section_end();
647 if (error(Symbol.getName(Name)))
648 continue;
649 if (error(Symbol.getAddress(Address)))
650 continue;
651 if (error(Symbol.getType(Type)))
652 continue;
653 if (error(Symbol.getSize(Size)))
654 continue;
655 if (error(Symbol.getSection(Section)))
656 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000657
Rui Ueyama4e39f712014-03-18 18:58:51 +0000658 bool Global = Flags & SymbolRef::SF_Global;
659 bool Weak = Flags & SymbolRef::SF_Weak;
660 bool Absolute = Flags & SymbolRef::SF_Absolute;
David Meyer1df4b842012-02-28 23:47:53 +0000661
Rui Ueyama4e39f712014-03-18 18:58:51 +0000662 if (Address == UnknownAddressOrSize)
663 Address = 0;
664 if (Size == UnknownAddressOrSize)
665 Size = 0;
666 char GlobLoc = ' ';
667 if (Type != SymbolRef::ST_Unknown)
668 GlobLoc = Global ? 'g' : 'l';
669 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
670 ? 'd' : ' ';
671 char FileFunc = ' ';
672 if (Type == SymbolRef::ST_File)
673 FileFunc = 'f';
674 else if (Type == SymbolRef::ST_Function)
675 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000676
Rui Ueyama4e39f712014-03-18 18:58:51 +0000677 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
678 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000679
Rui Ueyama4e39f712014-03-18 18:58:51 +0000680 outs() << format(Fmt, Address) << " "
681 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
682 << (Weak ? 'w' : ' ') // Weak?
683 << ' ' // Constructor. Not supported yet.
684 << ' ' // Warning. Not supported yet.
685 << ' ' // Indirect reference to another symbol.
686 << Debug // Debugging (d) or dynamic (D) symbol.
687 << FileFunc // Name of function (F), file (f) or object (O).
688 << ' ';
689 if (Absolute) {
690 outs() << "*ABS*";
691 } else if (Section == o->section_end()) {
692 outs() << "*UND*";
693 } else {
694 if (const MachOObjectFile *MachO =
695 dyn_cast<const MachOObjectFile>(o)) {
696 DataRefImpl DR = Section->getRawDataRefImpl();
697 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
698 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000699 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000700 StringRef SectionName;
701 if (error(Section->getName(SectionName)))
702 SectionName = "";
703 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000704 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000705 outs() << '\t'
706 << format("%08" PRIx64 " ", Size)
707 << Name
708 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000709 }
710}
711
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000712static void PrintUnwindInfo(const ObjectFile *o) {
713 outs() << "Unwind info:\n\n";
714
715 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
716 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000717 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
718 printMachOUnwindInfo(MachO);
719 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000720 // TODO: Extract DWARF dump tool to objdump.
721 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000722 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000723 return;
724 }
725}
726
Nick Kledzikd04bc352014-08-30 00:20:14 +0000727static void printExportsTrie(const ObjectFile *o) {
728 outs() << "Exports trie:\n";
729 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
730 printMachOExportsTrie(MachO);
731 else {
732 errs() << "This operation is only currently supported "
733 "for Mach-O executable files.\n";
734 return;
735 }
736}
737
Nick Kledzikac431442014-09-12 21:34:15 +0000738static void printRebaseTable(const ObjectFile *o) {
739 outs() << "Rebase table:\n";
740 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
741 printMachORebaseTable(MachO);
742 else {
743 errs() << "This operation is only currently supported "
744 "for Mach-O executable files.\n";
745 return;
746 }
747}
748
Nick Kledzik56ebef42014-09-16 01:41:51 +0000749static void printBindTable(const ObjectFile *o) {
750 outs() << "Bind table:\n";
751 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
752 printMachOBindTable(MachO);
753 else {
754 errs() << "This operation is only currently supported "
755 "for Mach-O executable files.\n";
756 return;
757 }
758}
759
760static void printLazyBindTable(const ObjectFile *o) {
761 outs() << "Lazy bind table:\n";
762 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
763 printMachOLazyBindTable(MachO);
764 else {
765 errs() << "This operation is only currently supported "
766 "for Mach-O executable files.\n";
767 return;
768 }
769}
770
771static void printWeakBindTable(const ObjectFile *o) {
772 outs() << "Weak bind table:\n";
773 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
774 printMachOWeakBindTable(MachO);
775 else {
776 errs() << "This operation is only currently supported "
777 "for Mach-O executable files.\n";
778 return;
779 }
780}
Nick Kledzikac431442014-09-12 21:34:15 +0000781
Rui Ueyamac2bed422013-09-27 21:04:00 +0000782static void printPrivateFileHeader(const ObjectFile *o) {
783 if (o->isELF()) {
784 printELFFileHeader(o);
785 } else if (o->isCOFF()) {
786 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000787 } else if (o->isMachO()) {
788 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000789 }
790}
791
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000792static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000793 outs() << '\n';
794 outs() << o->getFileName()
795 << ":\tfile format " << o->getFileFormatName() << "\n\n";
796
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000797 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000798 DisassembleObject(o, Relocations);
799 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000800 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000801 if (SectionHeaders)
802 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000803 if (SectionContents)
804 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000805 if (SymbolTable)
806 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000807 if (UnwindInfo)
808 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000809 if (PrivateHeaders)
810 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000811 if (ExportsTrie)
812 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000813 if (Rebase)
814 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000815 if (Bind)
816 printBindTable(o);
817 if (LazyBind)
818 printLazyBindTable(o);
819 if (WeakBind)
820 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000821}
822
823/// @brief Dump each object file in \a a;
824static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000825 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
826 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000827 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
828 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000829 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000830 if (EC != object_error::invalid_file_type)
831 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000832 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000833 continue;
834 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000835 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000836 DumpObject(o);
837 else
838 errs() << ToolName << ": '" << a->getFileName() << "': "
839 << "Unrecognized file type.\n";
840 }
841}
842
843/// @brief Open file and figure out how to dump it.
844static void DumpInput(StringRef file) {
845 // If file isn't stdin, check that it exists.
846 if (file != "-" && !sys::fs::exists(file)) {
847 errs() << ToolName << ": '" << file << "': " << "No such file\n";
848 return;
849 }
850
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000851 if (MachOOpt && Disassemble) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000852 DisassembleInputMachO(file);
853 return;
854 }
855
856 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000857 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000858 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000859 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000860 return;
861 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000862 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000863
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000864 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000865 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000866 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000867 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000868 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000869 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000870}
871
Michael J. Spencer2670c252011-01-20 06:39:06 +0000872int main(int argc, char **argv) {
873 // Print a stack trace if we signal out.
874 sys::PrintStackTraceOnErrorSignal();
875 PrettyStackTraceProgram X(argc, argv);
876 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
877
878 // Initialize targets and assembly printers/parsers.
879 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000880 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000881 llvm::InitializeAllAsmParsers();
882 llvm::InitializeAllDisassemblers();
883
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000884 // Register the target printer for --version.
885 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
886
Michael J. Spencer2670c252011-01-20 06:39:06 +0000887 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
888 TripleName = Triple::normalize(TripleName);
889
890 ToolName = argv[0];
891
892 // Defaults to a.out if no filenames specified.
893 if (InputFilenames.size() == 0)
894 InputFilenames.push_back("a.out");
895
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000896 if (!Disassemble
897 && !Relocations
898 && !SectionHeaders
899 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000900 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000901 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000902 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000903 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000904 && !Rebase
905 && !Bind
906 && !LazyBind
907 && !WeakBind) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000908 cl::PrintHelpMessage();
909 return 2;
910 }
911
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000912 std::for_each(InputFilenames.begin(), InputFilenames.end(),
913 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000914
915 return 0;
916}