blob: 1204a973d45849ac61bff3ebc4c944859b8afdee [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/PrettyStackTrace.h"
48#include "llvm/Support/Signals.h"
49#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000050#include "llvm/Support/TargetRegistry.h"
51#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000052#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000053#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000054#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000055#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000056#include <system_error>
Ahmed Bougacha17926472013-08-21 07:29:02 +000057
Michael J. Spencer2670c252011-01-20 06:39:06 +000058using namespace llvm;
59using namespace object;
60
Benjamin Kramer43a772e2011-09-19 17:56:04 +000061static cl::list<std::string>
62InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000063
Kevin Enderbye2297dd2015-01-07 21:02:18 +000064cl::opt<bool>
65llvm::Disassemble("disassemble",
Benjamin Kramer43a772e2011-09-19 17:56:04 +000066 cl::desc("Display assembler mnemonics for the machine instructions"));
67static cl::alias
68Disassembled("d", cl::desc("Alias for --disassemble"),
69 cl::aliasopt(Disassemble));
Michael J. Spencer2670c252011-01-20 06:39:06 +000070
Kevin Enderby98da6132015-01-20 21:47:46 +000071cl::opt<bool>
72llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
Michael J. Spencerba4a3622011-10-08 00:18:30 +000073
Kevin Enderby98da6132015-01-20 21:47:46 +000074cl::opt<bool>
75llvm::SectionContents("s", cl::desc("Display the content of each section"));
Michael J. Spencer4e25c022011-10-17 17:13:22 +000076
Kevin Enderby98da6132015-01-20 21:47:46 +000077cl::opt<bool>
78llvm::SymbolTable("t", cl::desc("Display the symbol table"));
Michael J. Spencerbfa06782011-10-18 19:32:17 +000079
Kevin Enderbye2297dd2015-01-07 21:02:18 +000080cl::opt<bool>
81llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
Nick Kledzikd04bc352014-08-30 00:20:14 +000082
Kevin Enderbye2297dd2015-01-07 21:02:18 +000083cl::opt<bool>
84llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
Nick Kledzikac431442014-09-12 21:34:15 +000085
Kevin Enderbye2297dd2015-01-07 21:02:18 +000086cl::opt<bool>
87llvm::Bind("bind", cl::desc("Display mach-o binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000088
Kevin Enderbye2297dd2015-01-07 21:02:18 +000089cl::opt<bool>
90llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000091
Kevin Enderbye2297dd2015-01-07 21:02:18 +000092cl::opt<bool>
93llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000094
95static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000096MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +000097static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000098MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +000099
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000100cl::opt<std::string>
101llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
102 "see -version for available targets"));
103
104cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000105llvm::MCPU("mcpu",
106 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
107 cl::value_desc("cpu-name"),
108 cl::init(""));
109
110cl::opt<std::string>
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000111llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000112 "see -version for available targets"));
113
Kevin Enderby98da6132015-01-20 21:47:46 +0000114cl::opt<bool>
115llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
116 "headers for each section."));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000117static cl::alias
118SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
119 cl::aliasopt(SectionHeaders));
120static cl::alias
121SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
122 cl::aliasopt(SectionHeaders));
123
Kevin Enderbyc9595622014-08-06 23:24:41 +0000124cl::list<std::string>
125llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000126 cl::CommaSeparated,
127 cl::desc("Target specific attributes"),
128 cl::value_desc("a1,+a2,-a3,..."));
129
Kevin Enderbybf246f52014-09-24 23:08:22 +0000130cl::opt<bool>
131llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
132 "instructions, do not print "
133 "the instruction bytes."));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000134
Kevin Enderby98da6132015-01-20 21:47:46 +0000135cl::opt<bool>
136llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000137
138static cl::alias
139UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
140 cl::aliasopt(UnwindInfo));
141
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000142cl::opt<bool>
143llvm::PrivateHeaders("private-headers",
144 cl::desc("Display format specific file headers"));
Michael J. Spencer209565db2013-01-06 03:56:49 +0000145
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;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000151static int ReturnValue = EXIT_SUCCESS;
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();
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000159 ReturnValue = EXIT_FAILURE;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000160 return true;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000161}
162
Craig Toppere6cb63e2014-04-25 04:24:47 +0000163static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000164 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000165 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000166 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000167 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000168 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000169 // TheTriple defaults to ELF, and COFF doesn't have an environment:
170 // the best we can do here is indicate that it is mach-o.
171 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000172 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000173
174 if (Obj->isCOFF()) {
175 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
176 if (COFFObj->getArch() == Triple::thumb)
177 TheTriple.setTriple("thumbv7-windows");
178 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000179 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000180 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000181 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000182
183 // Get the target specific parser.
184 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000185 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
186 Error);
187 if (!TheTarget) {
188 errs() << ToolName << ": " << Error;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000189 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000190 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000191
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000192 // Update the triple name and return the found target.
193 TripleName = TheTriple.getTriple();
194 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000195}
196
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000197void llvm::DumpBytes(StringRef bytes) {
198 static const char hex_rep[] = "0123456789abcdef";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000199 // FIXME: The real way to do this is to figure out the longest instruction
200 // and align to that size before printing. I'll fix this when I get
201 // around to outputting relocations.
202 // 15 is the longest x86 instruction
203 // 3 is for the hex rep of a byte + a space.
204 // 1 is for the null terminator.
205 enum { OutputSize = (15 * 3) + 1 };
206 char output[OutputSize];
207
208 assert(bytes.size() <= 15
209 && "DumpBytes only supports instructions of up to 15 bytes");
210 memset(output, ' ', sizeof(output));
211 unsigned index = 0;
212 for (StringRef::iterator i = bytes.begin(),
213 e = bytes.end(); i != e; ++i) {
214 output[index] = hex_rep[(*i & 0xF0) >> 4];
215 output[index + 1] = hex_rep[*i & 0xF];
216 index += 3;
217 }
218
219 output[sizeof(output) - 1] = 0;
220 outs() << output;
221}
222
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000223bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer51862b32011-10-13 22:17:18 +0000224 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000225 if (error(a.getOffset(a_addr))) return false;
226 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000227 return a_addr < b_addr;
228}
229
230static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000231 const Target *TheTarget = getTarget(Obj);
232 // getTarget() will have already issued a diagnostic if necessary, so
233 // just bail here if it failed.
234 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000235 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000236
Jack Carter551efd72012-08-28 19:24:49 +0000237 // Package up features to be passed to target/subtarget
238 std::string FeaturesStr;
239 if (MAttrs.size()) {
240 SubtargetFeatures Features;
241 for (unsigned i = 0; i != MAttrs.size(); ++i)
242 Features.AddFeature(MAttrs[i]);
243 FeaturesStr = Features.getString();
244 }
245
Ahmed Charles56440fd2014-03-06 05:51:42 +0000246 std::unique_ptr<const MCRegisterInfo> MRI(
247 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000248 if (!MRI) {
249 errs() << "error: no register info for target " << TripleName << "\n";
250 return;
251 }
252
253 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000254 std::unique_ptr<const MCAsmInfo> AsmInfo(
255 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000256 if (!AsmInfo) {
257 errs() << "error: no assembly info for target " << TripleName << "\n";
258 return;
259 }
260
Ahmed Charles56440fd2014-03-06 05:51:42 +0000261 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000262 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000263 if (!STI) {
264 errs() << "error: no subtarget info for target " << TripleName << "\n";
265 return;
266 }
267
Ahmed Charles56440fd2014-03-06 05:51:42 +0000268 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000269 if (!MII) {
270 errs() << "error: no instruction info for target " << TripleName << "\n";
271 return;
272 }
273
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000274 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
275 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
276
277 std::unique_ptr<MCDisassembler> DisAsm(
278 TheTarget->createMCDisassembler(*STI, Ctx));
279
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000280 if (!DisAsm) {
281 errs() << "error: no disassembler for target " << TripleName << "\n";
282 return;
283 }
284
Ahmed Charles56440fd2014-03-06 05:51:42 +0000285 std::unique_ptr<const MCInstrAnalysis> MIA(
286 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000287
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000288 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000289 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000290 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
291 if (!IP) {
292 errs() << "error: no instruction printer for target " << TripleName
293 << '\n';
294 return;
295 }
296
Greg Fitzgerald18432272014-03-20 22:55:15 +0000297 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
298 "\t\t\t%08" PRIx64 ": ";
299
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000300 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
301 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000302 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000303 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
304 for (const SectionRef &Section : Obj->sections()) {
305 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000306 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000307 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000308 }
309
Alexey Samsonov48803e52014-03-13 14:37:36 +0000310 for (const SectionRef &Section : Obj->sections()) {
David Majnemer236b0ca2014-11-17 11:17:17 +0000311 if (!Section.isText() || Section.isVirtual())
Mark Seaborneb03ac52014-01-25 00:32:01 +0000312 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000313
Rafael Espindola80291272014-10-08 15:28:58 +0000314 uint64_t SectionAddr = Section.getAddress();
315 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000316 if (!SectSize)
317 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000318
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000319 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000320 std::vector<std::pair<uint64_t, StringRef>> Symbols;
321 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000322 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000323 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000324 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000325 break;
326 if (Address == UnknownAddressOrSize)
327 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000328 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000329 if (Address >= SectSize)
330 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000331
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000332 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000333 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000334 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000335 Symbols.push_back(std::make_pair(Address, Name));
336 }
337 }
338
339 // Sort the symbols by address, just in case they didn't come in that way.
340 array_pod_sort(Symbols.begin(), Symbols.end());
341
Michael J. Spencer51862b32011-10-13 22:17:18 +0000342 // Make a list of all the relocations for this section.
343 std::vector<RelocationRef> Rels;
344 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000345 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
346 for (const RelocationRef &Reloc : RelocSec.relocations()) {
347 Rels.push_back(Reloc);
348 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000349 }
350 }
351
352 // Sort relocations by address.
353 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
354
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000355 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000356 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000357 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000358 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000359 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000360 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000361 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000362 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000363 outs() << "Disassembly of section ";
364 if (!SegmentName.empty())
365 outs() << SegmentName << ",";
366 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000367
368 // If the section has no symbols just insert a dummy one and disassemble
369 // the whole section.
370 if (Symbols.empty())
371 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000372
Alp Tokere69170a2014-06-26 22:52:05 +0000373
374 SmallString<40> Comments;
375 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000376
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000377 StringRef BytesStr;
378 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000379 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000380 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
381 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000382
Michael J. Spencer2670c252011-01-20 06:39:06 +0000383 uint64_t Size;
384 uint64_t Index;
385
Michael J. Spencer51862b32011-10-13 22:17:18 +0000386 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
387 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000388 // Disassemble symbol by symbol.
389 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000390
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000391 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000392 // The end is either the section end or the beginning of the next symbol.
393 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
394 // If this symbol has the same address as the next symbol, then skip it.
395 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000396 continue;
397
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000398 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000399
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000400#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000401 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000402#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000403 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000404#endif
405
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000406 for (Index = Start; Index < End; Index += Size) {
407 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000408
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000409 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
410 SectionAddr + Index, DebugOut,
411 CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000412 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
413 if (!NoShowRawInsn) {
414 outs() << "\t";
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000415 DumpBytes(StringRef(
416 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000417 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000418 IP->printInst(&Inst, outs(), "");
Alp Tokere69170a2014-06-26 22:52:05 +0000419 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000420 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000421 outs() << "\n";
422 } else {
423 errs() << ToolName << ": warning: invalid instruction encoding\n";
424 if (Size == 0)
425 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000426 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000427
428 // Print relocation for instruction.
429 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000430 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000431 uint64_t addr;
432 SmallString<16> name;
433 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000434
435 // If this relocation is hidden, skip it.
436 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
437 if (hidden) goto skip_print_rel;
438
Rafael Espindola1e483872013-04-25 12:28:45 +0000439 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000440 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000441 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000442 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
443 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
444
Greg Fitzgerald18432272014-03-20 22:55:15 +0000445 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000446 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000447
448 skip_print_rel:
449 ++rel_cur;
450 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000451 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000452 }
453 }
454}
455
Kevin Enderby98da6132015-01-20 21:47:46 +0000456void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000457 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
458 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000459 // Regular objdump doesn't print relocations in non-relocatable object
460 // files.
461 if (!Obj->isRelocatableObject())
462 return;
463
Alexey Samsonov48803e52014-03-13 14:37:36 +0000464 for (const SectionRef &Section : Obj->sections()) {
465 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000466 continue;
467 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000468 if (error(Section.getName(secname)))
469 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000470 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000471 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000472 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000473 uint64_t address;
474 SmallString<32> relocname;
475 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000476 if (error(Reloc.getHidden(hidden)))
477 continue;
478 if (hidden)
479 continue;
480 if (error(Reloc.getTypeName(relocname)))
481 continue;
482 if (error(Reloc.getOffset(address)))
483 continue;
484 if (error(Reloc.getValueString(valuestr)))
485 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000486 outs() << format(Fmt.data(), address) << " " << relocname << " "
487 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000488 }
489 outs() << "\n";
490 }
491}
492
Kevin Enderby98da6132015-01-20 21:47:46 +0000493void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000494 outs() << "Sections:\n"
495 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000496 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000497 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000498 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000499 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000500 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000501 uint64_t Address = Section.getAddress();
502 uint64_t Size = Section.getSize();
503 bool Text = Section.isText();
504 bool Data = Section.isData();
505 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000506 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000507 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000508 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
509 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000510 ++i;
511 }
512}
513
Kevin Enderby98da6132015-01-20 21:47:46 +0000514void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000515 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000516 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000517 StringRef Name;
518 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000519 if (error(Section.getName(Name)))
520 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000521 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000522 uint64_t Size = Section.getSize();
523 if (!Size)
524 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000525
526 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000527 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000528 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000529 ", %04" PRIx64 ")>\n",
530 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000531 continue;
532 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000533
David Majnemer8f6b04c2014-07-14 16:20:14 +0000534 if (error(Section.getContents(Contents)))
535 continue;
536
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000537 // Dump out the content as hex and printable ascii characters.
538 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000539 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000540 // Dump line of hex.
541 for (std::size_t i = 0; i < 16; ++i) {
542 if (i != 0 && i % 4 == 0)
543 outs() << ' ';
544 if (addr + i < end)
545 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
546 << hexdigit(Contents[addr + i] & 0xF, true);
547 else
548 outs() << " ";
549 }
550 // Print ascii.
551 outs() << " ";
552 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000553 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000554 outs() << Contents[addr + i];
555 else
556 outs() << ".";
557 }
558 outs() << "\n";
559 }
560 }
561}
562
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000563static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000564 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
565 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000566 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000567 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000568 return;
569
David Majnemer44f51e52014-09-10 12:51:52 +0000570 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000571 return;
572
573 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000574 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000575 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000576 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
577 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
578 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
579 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000580 << Name << "\n";
581
David Majnemer44f51e52014-09-10 12:51:52 +0000582 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000583 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000584 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000585 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000586 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000587
David Majnemer4d571592014-09-15 19:42:42 +0000588 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
589
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000590 outs() << "AUX "
591 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
592 , unsigned(asd->Length)
593 , unsigned(asd->NumberOfRelocations)
594 , unsigned(asd->NumberOfLinenumbers)
595 , unsigned(asd->CheckSum))
596 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000597 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000598 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000599 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000600 const char *FileName;
601 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000602 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000603
David Majnemer44f51e52014-09-10 12:51:52 +0000604 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
605 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000606 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000607
David Majnemer44f51e52014-09-10 12:51:52 +0000608 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000609 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000610 } else {
611 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000612 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000613 }
614 }
615}
616
Kevin Enderby98da6132015-01-20 21:47:46 +0000617void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000618 outs() << "SYMBOL TABLE:\n";
619
Rui Ueyama4e39f712014-03-18 18:58:51 +0000620 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000621 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000622 return;
623 }
624 for (const SymbolRef &Symbol : o->symbols()) {
625 StringRef Name;
626 uint64_t Address;
627 SymbolRef::Type Type;
628 uint64_t Size;
629 uint32_t Flags = Symbol.getFlags();
630 section_iterator Section = o->section_end();
631 if (error(Symbol.getName(Name)))
632 continue;
633 if (error(Symbol.getAddress(Address)))
634 continue;
635 if (error(Symbol.getType(Type)))
636 continue;
637 if (error(Symbol.getSize(Size)))
638 continue;
639 if (error(Symbol.getSection(Section)))
640 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000641
Rui Ueyama4e39f712014-03-18 18:58:51 +0000642 bool Global = Flags & SymbolRef::SF_Global;
643 bool Weak = Flags & SymbolRef::SF_Weak;
644 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000645 bool Common = Flags & SymbolRef::SF_Common;
David Meyer1df4b842012-02-28 23:47:53 +0000646
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000647 if (Common) {
648 uint32_t Alignment;
649 if (error(Symbol.getAlignment(Alignment)))
650 Alignment = 0;
651 Address = Size;
652 Size = Alignment;
653 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000654 if (Address == UnknownAddressOrSize)
655 Address = 0;
656 if (Size == UnknownAddressOrSize)
657 Size = 0;
658 char GlobLoc = ' ';
659 if (Type != SymbolRef::ST_Unknown)
660 GlobLoc = Global ? 'g' : 'l';
661 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
662 ? 'd' : ' ';
663 char FileFunc = ' ';
664 if (Type == SymbolRef::ST_File)
665 FileFunc = 'f';
666 else if (Type == SymbolRef::ST_Function)
667 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000668
Rui Ueyama4e39f712014-03-18 18:58:51 +0000669 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
670 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000671
Rui Ueyama4e39f712014-03-18 18:58:51 +0000672 outs() << format(Fmt, Address) << " "
673 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
674 << (Weak ? 'w' : ' ') // Weak?
675 << ' ' // Constructor. Not supported yet.
676 << ' ' // Warning. Not supported yet.
677 << ' ' // Indirect reference to another symbol.
678 << Debug // Debugging (d) or dynamic (D) symbol.
679 << FileFunc // Name of function (F), file (f) or object (O).
680 << ' ';
681 if (Absolute) {
682 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000683 } else if (Common) {
684 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +0000685 } else if (Section == o->section_end()) {
686 outs() << "*UND*";
687 } else {
688 if (const MachOObjectFile *MachO =
689 dyn_cast<const MachOObjectFile>(o)) {
690 DataRefImpl DR = Section->getRawDataRefImpl();
691 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
692 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000693 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000694 StringRef SectionName;
695 if (error(Section->getName(SectionName)))
696 SectionName = "";
697 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000698 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000699 outs() << '\t'
700 << format("%08" PRIx64 " ", Size)
701 << Name
702 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000703 }
704}
705
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000706static void PrintUnwindInfo(const ObjectFile *o) {
707 outs() << "Unwind info:\n\n";
708
709 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
710 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000711 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
712 printMachOUnwindInfo(MachO);
713 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000714 // TODO: Extract DWARF dump tool to objdump.
715 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000716 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000717 return;
718 }
719}
720
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000721void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +0000722 outs() << "Exports trie:\n";
723 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
724 printMachOExportsTrie(MachO);
725 else {
726 errs() << "This operation is only currently supported "
727 "for Mach-O executable files.\n";
728 return;
729 }
730}
731
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000732void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +0000733 outs() << "Rebase table:\n";
734 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
735 printMachORebaseTable(MachO);
736 else {
737 errs() << "This operation is only currently supported "
738 "for Mach-O executable files.\n";
739 return;
740 }
741}
742
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000743void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000744 outs() << "Bind table:\n";
745 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
746 printMachOBindTable(MachO);
747 else {
748 errs() << "This operation is only currently supported "
749 "for Mach-O executable files.\n";
750 return;
751 }
752}
753
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000754void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000755 outs() << "Lazy bind table:\n";
756 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
757 printMachOLazyBindTable(MachO);
758 else {
759 errs() << "This operation is only currently supported "
760 "for Mach-O executable files.\n";
761 return;
762 }
763}
764
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000765void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000766 outs() << "Weak bind table:\n";
767 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
768 printMachOWeakBindTable(MachO);
769 else {
770 errs() << "This operation is only currently supported "
771 "for Mach-O executable files.\n";
772 return;
773 }
774}
Nick Kledzikac431442014-09-12 21:34:15 +0000775
Rui Ueyamac2bed422013-09-27 21:04:00 +0000776static void printPrivateFileHeader(const ObjectFile *o) {
777 if (o->isELF()) {
778 printELFFileHeader(o);
779 } else if (o->isCOFF()) {
780 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000781 } else if (o->isMachO()) {
782 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000783 }
784}
785
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000786static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000787 outs() << '\n';
788 outs() << o->getFileName()
789 << ":\tfile format " << o->getFileFormatName() << "\n\n";
790
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000791 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000792 DisassembleObject(o, Relocations);
793 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000794 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000795 if (SectionHeaders)
796 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000797 if (SectionContents)
798 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000799 if (SymbolTable)
800 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000801 if (UnwindInfo)
802 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000803 if (PrivateHeaders)
804 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000805 if (ExportsTrie)
806 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000807 if (Rebase)
808 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000809 if (Bind)
810 printBindTable(o);
811 if (LazyBind)
812 printLazyBindTable(o);
813 if (WeakBind)
814 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000815}
816
817/// @brief Dump each object file in \a a;
818static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000819 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
820 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000821 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
822 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000823 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000824 if (EC != object_error::invalid_file_type)
825 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000826 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000827 continue;
828 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000829 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000830 DumpObject(o);
831 else
832 errs() << ToolName << ": '" << a->getFileName() << "': "
833 << "Unrecognized file type.\n";
834 }
835}
836
837/// @brief Open file and figure out how to dump it.
838static void DumpInput(StringRef file) {
839 // If file isn't stdin, check that it exists.
840 if (file != "-" && !sys::fs::exists(file)) {
841 errs() << ToolName << ": '" << file << "': " << "No such file\n";
842 return;
843 }
844
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000845 // If we are using the Mach-O specific object file parser, then let it parse
846 // the file and process the command line options. So the -arch flags can
847 // be used to select specific slices, etc.
848 if (MachOOpt) {
849 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000850 return;
851 }
852
853 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000854 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000855 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000856 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000857 return;
858 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000859 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000860
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000861 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000862 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000863 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000864 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000865 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000866 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000867}
868
Michael J. Spencer2670c252011-01-20 06:39:06 +0000869int main(int argc, char **argv) {
870 // Print a stack trace if we signal out.
871 sys::PrintStackTraceOnErrorSignal();
872 PrettyStackTraceProgram X(argc, argv);
873 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
874
875 // Initialize targets and assembly printers/parsers.
876 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000877 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000878 llvm::InitializeAllAsmParsers();
879 llvm::InitializeAllDisassemblers();
880
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000881 // Register the target printer for --version.
882 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
883
Michael J. Spencer2670c252011-01-20 06:39:06 +0000884 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
885 TripleName = Triple::normalize(TripleName);
886
887 ToolName = argv[0];
888
889 // Defaults to a.out if no filenames specified.
890 if (InputFilenames.size() == 0)
891 InputFilenames.push_back("a.out");
892
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000893 if (!Disassemble
894 && !Relocations
895 && !SectionHeaders
896 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000897 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000898 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000899 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000900 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000901 && !Rebase
902 && !Bind
903 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +0000904 && !WeakBind
Kevin Enderby13023a12015-01-15 23:19:11 +0000905 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000906 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000907 && !(IndirectSymbols && MachOOpt)
908 && !(DataInCode && MachOOpt)) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000909 cl::PrintHelpMessage();
910 return 2;
911 }
912
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000913 std::for_each(InputFilenames.begin(), InputFilenames.end(),
914 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000915
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000916 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000917}