blob: 12cf1f72b099dd811e3d28c825a827497e5d611a [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
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000131static cl::opt<bool>
132NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling instructions, "
133 "do not print the instruction bytes."));
134
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000135static cl::opt<bool>
136UnwindInfo("unwind-info", cl::desc("Display unwind information"));
137
138static cl::alias
139UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
140 cl::aliasopt(UnwindInfo));
141
Michael J. Spencer209565db2013-01-06 03:56:49 +0000142static cl::opt<bool>
143PrivateHeaders("private-headers",
144 cl::desc("Display format specific file headers"));
145
146static cl::alias
147PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
148 cl::aliasopt(PrivateHeaders));
149
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000150static StringRef ToolName;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000151
Rafael Espindola4453e42942014-06-13 03:07:50 +0000152bool llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000153 if (!EC)
154 return false;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000155
Mark Seaborneb03ac52014-01-25 00:32:01 +0000156 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000157 outs().flush();
158 return true;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000159}
160
Craig Toppere6cb63e2014-04-25 04:24:47 +0000161static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000162 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000163 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000164 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000165 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000166 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000167 // TheTriple defaults to ELF, and COFF doesn't have an environment:
168 // the best we can do here is indicate that it is mach-o.
169 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000170 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000171
172 if (Obj->isCOFF()) {
173 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
174 if (COFFObj->getArch() == Triple::thumb)
175 TheTriple.setTriple("thumbv7-windows");
176 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000177 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000178 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000179 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000180
181 // Get the target specific parser.
182 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000183 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
184 Error);
185 if (!TheTarget) {
186 errs() << ToolName << ": " << Error;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000187 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000188 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000189
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000190 // Update the triple name and return the found target.
191 TripleName = TheTriple.getTriple();
192 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000193}
194
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000195void llvm::DumpBytes(StringRef bytes) {
196 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000197 // FIXME: The real way to do this is to figure out the longest instruction
198 // and align to that size before printing. I'll fix this when I get
199 // around to outputting relocations.
200 // 15 is the longest x86 instruction
201 // 3 is for the hex rep of a byte + a space.
202 // 1 is for the null terminator.
203 enum { OutputSize = (15 * 3) + 1 };
204 char output[OutputSize];
205
206 assert(bytes.size() <= 15
207 && "DumpBytes only supports instructions of up to 15 bytes");
208 memset(output, ' ', sizeof(output));
209 unsigned index = 0;
210 for (StringRef::iterator i = bytes.begin(),
211 e = bytes.end(); i != e; ++i) {
212 output[index] = hex_rep[(*i & 0xF0) >> 4];
213 output[index + 1] = hex_rep[*i & 0xF];
214 index += 3;
215 }
216
217 output[sizeof(output) - 1] = 0;
218 outs() << output;
219}
220
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000221bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer51862b32011-10-13 22:17:18 +0000222 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000223 if (error(a.getOffset(a_addr))) return false;
224 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000225 return a_addr < b_addr;
226}
227
228static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000229 const Target *TheTarget = getTarget(Obj);
230 // getTarget() will have already issued a diagnostic if necessary, so
231 // just bail here if it failed.
232 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000233 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000234
Jack Carter551efd72012-08-28 19:24:49 +0000235 // Package up features to be passed to target/subtarget
236 std::string FeaturesStr;
237 if (MAttrs.size()) {
238 SubtargetFeatures Features;
239 for (unsigned i = 0; i != MAttrs.size(); ++i)
240 Features.AddFeature(MAttrs[i]);
241 FeaturesStr = Features.getString();
242 }
243
Ahmed Charles56440fd2014-03-06 05:51:42 +0000244 std::unique_ptr<const MCRegisterInfo> MRI(
245 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000246 if (!MRI) {
247 errs() << "error: no register info for target " << TripleName << "\n";
248 return;
249 }
250
251 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000252 std::unique_ptr<const MCAsmInfo> AsmInfo(
253 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000254 if (!AsmInfo) {
255 errs() << "error: no assembly info for target " << TripleName << "\n";
256 return;
257 }
258
Ahmed Charles56440fd2014-03-06 05:51:42 +0000259 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000260 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000261 if (!STI) {
262 errs() << "error: no subtarget info for target " << TripleName << "\n";
263 return;
264 }
265
Ahmed Charles56440fd2014-03-06 05:51:42 +0000266 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000267 if (!MII) {
268 errs() << "error: no instruction info for target " << TripleName << "\n";
269 return;
270 }
271
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000272 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
273 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
274
275 std::unique_ptr<MCDisassembler> DisAsm(
276 TheTarget->createMCDisassembler(*STI, Ctx));
277
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000278 if (!DisAsm) {
279 errs() << "error: no disassembler for target " << TripleName << "\n";
280 return;
281 }
282
Ahmed Charles56440fd2014-03-06 05:51:42 +0000283 std::unique_ptr<const MCInstrAnalysis> MIA(
284 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000285
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000286 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000287 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000288 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
289 if (!IP) {
290 errs() << "error: no instruction printer for target " << TripleName
291 << '\n';
292 return;
293 }
294
Greg Fitzgerald18432272014-03-20 22:55:15 +0000295 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
296 "\t\t\t%08" PRIx64 ": ";
297
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000298 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
299 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000300 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000301 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
302 for (const SectionRef &Section : Obj->sections()) {
303 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000304 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000305 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000306 }
307
Alexey Samsonov48803e52014-03-13 14:37:36 +0000308 for (const SectionRef &Section : Obj->sections()) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000309 bool Text;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000310 if (error(Section.isText(Text)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000311 break;
312 if (!Text)
313 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000314
Michael J. Spencer51862b32011-10-13 22:17:18 +0000315 uint64_t SectionAddr;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000316 if (error(Section.getAddress(SectionAddr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000317 break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000318
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000319 uint64_t SectSize;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000320 if (error(Section.getSize(SectSize)))
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000321 break;
322
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000323 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000324 std::vector<std::pair<uint64_t, StringRef>> Symbols;
325 for (const SymbolRef &Symbol : Obj->symbols()) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000326 bool contains;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000327 if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000328 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000329 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000330 break;
331 if (Address == UnknownAddressOrSize)
332 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000333 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000334 if (Address >= SectSize)
335 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000336
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000337 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000338 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000339 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000340 Symbols.push_back(std::make_pair(Address, Name));
341 }
342 }
343
344 // Sort the symbols by address, just in case they didn't come in that way.
345 array_pod_sort(Symbols.begin(), Symbols.end());
346
Michael J. Spencer51862b32011-10-13 22:17:18 +0000347 // Make a list of all the relocations for this section.
348 std::vector<RelocationRef> Rels;
349 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000350 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
351 for (const RelocationRef &Reloc : RelocSec.relocations()) {
352 Rels.push_back(Reloc);
353 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000354 }
355 }
356
357 // Sort relocations by address.
358 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
359
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000360 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000361 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000362 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000363 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000364 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000365 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000366 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000367 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000368 outs() << "Disassembly of section ";
369 if (!SegmentName.empty())
370 outs() << SegmentName << ",";
371 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000372
373 // If the section has no symbols just insert a dummy one and disassemble
374 // the whole section.
375 if (Symbols.empty())
376 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000377
Alp Tokere69170a2014-06-26 22:52:05 +0000378
379 SmallString<40> Comments;
380 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000381
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000382 StringRef Bytes;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000383 if (error(Section.getContents(Bytes)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000384 break;
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000385 StringRefMemoryObject memoryObject(Bytes, SectionAddr);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000386 uint64_t Size;
387 uint64_t Index;
388
Michael J. Spencer51862b32011-10-13 22:17:18 +0000389 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
390 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000391 // Disassemble symbol by symbol.
392 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000393
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000394 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000395 // The end is either the section end or the beginning of the next symbol.
396 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
397 // If this symbol has the same address as the next symbol, then skip it.
398 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000399 continue;
400
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000401 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000402
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000403#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000404 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000405#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000406 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000407#endif
408
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000409 for (Index = Start; Index < End; Index += Size) {
410 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000411
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000412 if (DisAsm->getInstruction(Inst, Size, memoryObject,
Alp Tokere69170a2014-06-26 22:52:05 +0000413 SectionAddr + Index,
414 DebugOut, CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000415 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
416 if (!NoShowRawInsn) {
417 outs() << "\t";
418 DumpBytes(StringRef(Bytes.data() + Index, Size));
419 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000420 IP->printInst(&Inst, outs(), "");
Alp Tokere69170a2014-06-26 22:52:05 +0000421 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000422 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000423 outs() << "\n";
424 } else {
425 errs() << ToolName << ": warning: invalid instruction encoding\n";
426 if (Size == 0)
427 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000428 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000429
430 // Print relocation for instruction.
431 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000432 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000433 uint64_t addr;
434 SmallString<16> name;
435 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000436
437 // If this relocation is hidden, skip it.
438 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
439 if (hidden) goto skip_print_rel;
440
Rafael Espindola1e483872013-04-25 12:28:45 +0000441 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000442 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000443 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000444 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
445 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
446
Greg Fitzgerald18432272014-03-20 22:55:15 +0000447 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000448 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000449
450 skip_print_rel:
451 ++rel_cur;
452 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000453 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000454 }
455 }
456}
457
Alexey Samsonov48803e52014-03-13 14:37:36 +0000458static void PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000459 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
460 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000461 // Regular objdump doesn't print relocations in non-relocatable object
462 // files.
463 if (!Obj->isRelocatableObject())
464 return;
465
Alexey Samsonov48803e52014-03-13 14:37:36 +0000466 for (const SectionRef &Section : Obj->sections()) {
467 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000468 continue;
469 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000470 if (error(Section.getName(secname)))
471 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000472 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000473 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000474 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000475 uint64_t address;
476 SmallString<32> relocname;
477 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000478 if (error(Reloc.getHidden(hidden)))
479 continue;
480 if (hidden)
481 continue;
482 if (error(Reloc.getTypeName(relocname)))
483 continue;
484 if (error(Reloc.getOffset(address)))
485 continue;
486 if (error(Reloc.getValueString(valuestr)))
487 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000488 outs() << format(Fmt.data(), address) << " " << relocname << " "
489 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000490 }
491 outs() << "\n";
492 }
493}
494
Alexey Samsonov48803e52014-03-13 14:37:36 +0000495static void PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000496 outs() << "Sections:\n"
497 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000498 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000499 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000500 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000501 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000502 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000503 uint64_t Address;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000504 if (error(Section.getAddress(Address)))
505 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000506 uint64_t Size;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000507 if (error(Section.getSize(Size)))
508 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000509 bool Text, Data, BSS;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000510 if (error(Section.isText(Text)))
511 return;
512 if (error(Section.isData(Data)))
513 return;
514 if (error(Section.isBSS(BSS)))
515 return;
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000516 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000517 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000518 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
519 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000520 ++i;
521 }
522}
523
Alexey Samsonov48803e52014-03-13 14:37:36 +0000524static void PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000525 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000526 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000527 StringRef Name;
528 StringRef Contents;
529 uint64_t BaseAddr;
Alexey Samsonov209095c2013-04-16 10:53:11 +0000530 bool BSS;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000531 if (error(Section.getName(Name)))
532 continue;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000533 if (error(Section.getAddress(BaseAddr)))
534 continue;
535 if (error(Section.isBSS(BSS)))
536 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000537
538 outs() << "Contents of section " << Name << ":\n";
Alexey Samsonov209095c2013-04-16 10:53:11 +0000539 if (BSS) {
David Majnemer8f6b04c2014-07-14 16:20:14 +0000540 uint64_t Size;
541 if (error(Section.getSize(Size)))
542 continue;
Alexey Samsonov209095c2013-04-16 10:53:11 +0000543 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000544 ", %04" PRIx64 ")>\n",
545 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000546 continue;
547 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000548
David Majnemer8f6b04c2014-07-14 16:20:14 +0000549 if (error(Section.getContents(Contents)))
550 continue;
551
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000552 // Dump out the content as hex and printable ascii characters.
553 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000554 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000555 // Dump line of hex.
556 for (std::size_t i = 0; i < 16; ++i) {
557 if (i != 0 && i % 4 == 0)
558 outs() << ' ';
559 if (addr + i < end)
560 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
561 << hexdigit(Contents[addr + i] & 0xF, true);
562 else
563 outs() << " ";
564 }
565 // Print ascii.
566 outs() << " ";
567 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000568 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000569 outs() << Contents[addr + i];
570 else
571 outs() << ".";
572 }
573 outs() << "\n";
574 }
575 }
576}
577
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000578static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000579 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
580 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000581 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000582 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000583 return;
584
David Majnemer44f51e52014-09-10 12:51:52 +0000585 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000586 return;
587
588 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000589 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000590 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000591 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
592 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
593 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
594 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000595 << Name << "\n";
596
David Majnemer44f51e52014-09-10 12:51:52 +0000597 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000598 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000599 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000600 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000601 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000602
David Majnemer4d571592014-09-15 19:42:42 +0000603 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
604
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000605 outs() << "AUX "
606 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
607 , unsigned(asd->Length)
608 , unsigned(asd->NumberOfRelocations)
609 , unsigned(asd->NumberOfLinenumbers)
610 , unsigned(asd->CheckSum))
611 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000612 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000613 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000614 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000615 const char *FileName;
616 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000617 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000618
David Majnemer44f51e52014-09-10 12:51:52 +0000619 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
620 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000621 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000622
David Majnemer44f51e52014-09-10 12:51:52 +0000623 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000624 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000625 } else {
626 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000627 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000628 }
629 }
630}
631
632static void PrintSymbolTable(const ObjectFile *o) {
633 outs() << "SYMBOL TABLE:\n";
634
Rui Ueyama4e39f712014-03-18 18:58:51 +0000635 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000636 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000637 return;
638 }
639 for (const SymbolRef &Symbol : o->symbols()) {
640 StringRef Name;
641 uint64_t Address;
642 SymbolRef::Type Type;
643 uint64_t Size;
644 uint32_t Flags = Symbol.getFlags();
645 section_iterator Section = o->section_end();
646 if (error(Symbol.getName(Name)))
647 continue;
648 if (error(Symbol.getAddress(Address)))
649 continue;
650 if (error(Symbol.getType(Type)))
651 continue;
652 if (error(Symbol.getSize(Size)))
653 continue;
654 if (error(Symbol.getSection(Section)))
655 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000656
Rui Ueyama4e39f712014-03-18 18:58:51 +0000657 bool Global = Flags & SymbolRef::SF_Global;
658 bool Weak = Flags & SymbolRef::SF_Weak;
659 bool Absolute = Flags & SymbolRef::SF_Absolute;
David Meyer1df4b842012-02-28 23:47:53 +0000660
Rui Ueyama4e39f712014-03-18 18:58:51 +0000661 if (Address == UnknownAddressOrSize)
662 Address = 0;
663 if (Size == UnknownAddressOrSize)
664 Size = 0;
665 char GlobLoc = ' ';
666 if (Type != SymbolRef::ST_Unknown)
667 GlobLoc = Global ? 'g' : 'l';
668 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
669 ? 'd' : ' ';
670 char FileFunc = ' ';
671 if (Type == SymbolRef::ST_File)
672 FileFunc = 'f';
673 else if (Type == SymbolRef::ST_Function)
674 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000675
Rui Ueyama4e39f712014-03-18 18:58:51 +0000676 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
677 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000678
Rui Ueyama4e39f712014-03-18 18:58:51 +0000679 outs() << format(Fmt, Address) << " "
680 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
681 << (Weak ? 'w' : ' ') // Weak?
682 << ' ' // Constructor. Not supported yet.
683 << ' ' // Warning. Not supported yet.
684 << ' ' // Indirect reference to another symbol.
685 << Debug // Debugging (d) or dynamic (D) symbol.
686 << FileFunc // Name of function (F), file (f) or object (O).
687 << ' ';
688 if (Absolute) {
689 outs() << "*ABS*";
690 } else if (Section == o->section_end()) {
691 outs() << "*UND*";
692 } else {
693 if (const MachOObjectFile *MachO =
694 dyn_cast<const MachOObjectFile>(o)) {
695 DataRefImpl DR = Section->getRawDataRefImpl();
696 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
697 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000698 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000699 StringRef SectionName;
700 if (error(Section->getName(SectionName)))
701 SectionName = "";
702 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000703 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000704 outs() << '\t'
705 << format("%08" PRIx64 " ", Size)
706 << Name
707 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000708 }
709}
710
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000711static void PrintUnwindInfo(const ObjectFile *o) {
712 outs() << "Unwind info:\n\n";
713
714 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
715 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000716 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
717 printMachOUnwindInfo(MachO);
718 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000719 // TODO: Extract DWARF dump tool to objdump.
720 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000721 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000722 return;
723 }
724}
725
Nick Kledzikd04bc352014-08-30 00:20:14 +0000726static void printExportsTrie(const ObjectFile *o) {
727 outs() << "Exports trie:\n";
728 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
729 printMachOExportsTrie(MachO);
730 else {
731 errs() << "This operation is only currently supported "
732 "for Mach-O executable files.\n";
733 return;
734 }
735}
736
Nick Kledzikac431442014-09-12 21:34:15 +0000737static void printRebaseTable(const ObjectFile *o) {
738 outs() << "Rebase table:\n";
739 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
740 printMachORebaseTable(MachO);
741 else {
742 errs() << "This operation is only currently supported "
743 "for Mach-O executable files.\n";
744 return;
745 }
746}
747
Nick Kledzik56ebef42014-09-16 01:41:51 +0000748static void printBindTable(const ObjectFile *o) {
749 outs() << "Bind table:\n";
750 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
751 printMachOBindTable(MachO);
752 else {
753 errs() << "This operation is only currently supported "
754 "for Mach-O executable files.\n";
755 return;
756 }
757}
758
759static void printLazyBindTable(const ObjectFile *o) {
760 outs() << "Lazy bind table:\n";
761 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
762 printMachOLazyBindTable(MachO);
763 else {
764 errs() << "This operation is only currently supported "
765 "for Mach-O executable files.\n";
766 return;
767 }
768}
769
770static void printWeakBindTable(const ObjectFile *o) {
771 outs() << "Weak bind table:\n";
772 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
773 printMachOWeakBindTable(MachO);
774 else {
775 errs() << "This operation is only currently supported "
776 "for Mach-O executable files.\n";
777 return;
778 }
779}
Nick Kledzikac431442014-09-12 21:34:15 +0000780
Rui Ueyamac2bed422013-09-27 21:04:00 +0000781static void printPrivateFileHeader(const ObjectFile *o) {
782 if (o->isELF()) {
783 printELFFileHeader(o);
784 } else if (o->isCOFF()) {
785 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000786 } else if (o->isMachO()) {
787 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000788 }
789}
790
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000791static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000792 outs() << '\n';
793 outs() << o->getFileName()
794 << ":\tfile format " << o->getFileFormatName() << "\n\n";
795
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000796 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000797 DisassembleObject(o, Relocations);
798 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000799 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000800 if (SectionHeaders)
801 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000802 if (SectionContents)
803 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000804 if (SymbolTable)
805 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000806 if (UnwindInfo)
807 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000808 if (PrivateHeaders)
809 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000810 if (ExportsTrie)
811 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000812 if (Rebase)
813 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000814 if (Bind)
815 printBindTable(o);
816 if (LazyBind)
817 printLazyBindTable(o);
818 if (WeakBind)
819 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000820}
821
822/// @brief Dump each object file in \a a;
823static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000824 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
825 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000826 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
827 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000828 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000829 if (EC != object_error::invalid_file_type)
830 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000831 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000832 continue;
833 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000834 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000835 DumpObject(o);
836 else
837 errs() << ToolName << ": '" << a->getFileName() << "': "
838 << "Unrecognized file type.\n";
839 }
840}
841
842/// @brief Open file and figure out how to dump it.
843static void DumpInput(StringRef file) {
844 // If file isn't stdin, check that it exists.
845 if (file != "-" && !sys::fs::exists(file)) {
846 errs() << ToolName << ": '" << file << "': " << "No such file\n";
847 return;
848 }
849
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000850 if (MachOOpt && Disassemble) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000851 DisassembleInputMachO(file);
852 return;
853 }
854
855 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000856 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000857 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000858 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000859 return;
860 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000861 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000862
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000863 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000864 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000865 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000866 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000867 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000868 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000869}
870
Michael J. Spencer2670c252011-01-20 06:39:06 +0000871int main(int argc, char **argv) {
872 // Print a stack trace if we signal out.
873 sys::PrintStackTraceOnErrorSignal();
874 PrettyStackTraceProgram X(argc, argv);
875 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
876
877 // Initialize targets and assembly printers/parsers.
878 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000879 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000880 llvm::InitializeAllAsmParsers();
881 llvm::InitializeAllDisassemblers();
882
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000883 // Register the target printer for --version.
884 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
885
Michael J. Spencer2670c252011-01-20 06:39:06 +0000886 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
887 TripleName = Triple::normalize(TripleName);
888
889 ToolName = argv[0];
890
891 // Defaults to a.out if no filenames specified.
892 if (InputFilenames.size() == 0)
893 InputFilenames.push_back("a.out");
894
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000895 if (!Disassemble
896 && !Relocations
897 && !SectionHeaders
898 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000899 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000900 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000901 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000902 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000903 && !Rebase
904 && !Bind
905 && !LazyBind
906 && !WeakBind) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000907 cl::PrintHelpMessage();
908 return 2;
909 }
910
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000911 std::for_each(InputFilenames.begin(), InputFilenames.end(),
912 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000913
914 return 0;
915}