blob: 71de8adca70b8b3958bda3c8372b0b12bfc7d8ab [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
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000197void llvm::DumpBytes(ArrayRef<uint8_t> bytes) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000198 static const char hex_rep[] = "0123456789abcdef";
Colin LeMahieu916c3b42015-03-18 18:41:23 +0000199 SmallString<64> output;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000200
Colin LeMahieu916c3b42015-03-18 18:41:23 +0000201 for (char i: bytes) {
202 output.push_back(hex_rep[(i & 0xF0) >> 4]);
203 output.push_back(hex_rep[i & 0xF]);
204 output.push_back(' ');
Michael J. Spencer2670c252011-01-20 06:39:06 +0000205 }
206
Colin LeMahieu916c3b42015-03-18 18:41:23 +0000207 outs() << output.c_str();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000208}
209
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000210bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer51862b32011-10-13 22:17:18 +0000211 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000212 if (error(a.getOffset(a_addr))) return false;
213 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000214 return a_addr < b_addr;
215}
216
217static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000218 const Target *TheTarget = getTarget(Obj);
219 // getTarget() will have already issued a diagnostic if necessary, so
220 // just bail here if it failed.
221 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000222 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000223
Jack Carter551efd72012-08-28 19:24:49 +0000224 // Package up features to be passed to target/subtarget
225 std::string FeaturesStr;
226 if (MAttrs.size()) {
227 SubtargetFeatures Features;
228 for (unsigned i = 0; i != MAttrs.size(); ++i)
229 Features.AddFeature(MAttrs[i]);
230 FeaturesStr = Features.getString();
231 }
232
Ahmed Charles56440fd2014-03-06 05:51:42 +0000233 std::unique_ptr<const MCRegisterInfo> MRI(
234 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000235 if (!MRI) {
236 errs() << "error: no register info for target " << TripleName << "\n";
237 return;
238 }
239
240 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000241 std::unique_ptr<const MCAsmInfo> AsmInfo(
242 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000243 if (!AsmInfo) {
244 errs() << "error: no assembly info for target " << TripleName << "\n";
245 return;
246 }
247
Ahmed Charles56440fd2014-03-06 05:51:42 +0000248 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000249 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000250 if (!STI) {
251 errs() << "error: no subtarget info for target " << TripleName << "\n";
252 return;
253 }
254
Ahmed Charles56440fd2014-03-06 05:51:42 +0000255 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000256 if (!MII) {
257 errs() << "error: no instruction info for target " << TripleName << "\n";
258 return;
259 }
260
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000261 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
262 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
263
264 std::unique_ptr<MCDisassembler> DisAsm(
265 TheTarget->createMCDisassembler(*STI, Ctx));
266
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000267 if (!DisAsm) {
268 errs() << "error: no disassembler for target " << TripleName << "\n";
269 return;
270 }
271
Ahmed Charles56440fd2014-03-06 05:51:42 +0000272 std::unique_ptr<const MCInstrAnalysis> MIA(
273 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000274
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000275 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000276 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +0000277 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000278 if (!IP) {
279 errs() << "error: no instruction printer for target " << TripleName
280 << '\n';
281 return;
282 }
283
Greg Fitzgerald18432272014-03-20 22:55:15 +0000284 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
285 "\t\t\t%08" PRIx64 ": ";
286
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000287 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
288 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000289 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000290 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
291 for (const SectionRef &Section : Obj->sections()) {
292 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000293 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000294 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000295 }
296
Alexey Samsonov48803e52014-03-13 14:37:36 +0000297 for (const SectionRef &Section : Obj->sections()) {
David Majnemer236b0ca2014-11-17 11:17:17 +0000298 if (!Section.isText() || Section.isVirtual())
Mark Seaborneb03ac52014-01-25 00:32:01 +0000299 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000300
Rafael Espindola80291272014-10-08 15:28:58 +0000301 uint64_t SectionAddr = Section.getAddress();
302 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000303 if (!SectSize)
304 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000305
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000306 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000307 std::vector<std::pair<uint64_t, StringRef>> Symbols;
308 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000309 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000310 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000311 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000312 break;
313 if (Address == UnknownAddressOrSize)
314 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000315 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000316 if (Address >= SectSize)
317 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000318
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000319 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000320 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000321 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000322 Symbols.push_back(std::make_pair(Address, Name));
323 }
324 }
325
326 // Sort the symbols by address, just in case they didn't come in that way.
327 array_pod_sort(Symbols.begin(), Symbols.end());
328
Michael J. Spencer51862b32011-10-13 22:17:18 +0000329 // Make a list of all the relocations for this section.
330 std::vector<RelocationRef> Rels;
331 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000332 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
333 for (const RelocationRef &Reloc : RelocSec.relocations()) {
334 Rels.push_back(Reloc);
335 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000336 }
337 }
338
339 // Sort relocations by address.
340 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
341
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000342 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000343 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000344 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000345 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000346 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000347 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000348 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000349 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000350 outs() << "Disassembly of section ";
351 if (!SegmentName.empty())
352 outs() << SegmentName << ",";
353 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000354
355 // If the section has no symbols just insert a dummy one and disassemble
356 // the whole section.
357 if (Symbols.empty())
358 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000359
Alp Tokere69170a2014-06-26 22:52:05 +0000360
361 SmallString<40> Comments;
362 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000363
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000364 StringRef BytesStr;
365 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000366 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000367 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
368 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000369
Michael J. Spencer2670c252011-01-20 06:39:06 +0000370 uint64_t Size;
371 uint64_t Index;
372
Michael J. Spencer51862b32011-10-13 22:17:18 +0000373 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
374 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000375 // Disassemble symbol by symbol.
376 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000377
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000378 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000379 // The end is either the section end or the beginning of the next symbol.
380 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
381 // If this symbol has the same address as the next symbol, then skip it.
382 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000383 continue;
384
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000385 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000386
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000387#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000388 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000389#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000390 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000391#endif
392
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000393 for (Index = Start; Index < End; Index += Size) {
394 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000395
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000396 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
397 SectionAddr + Index, DebugOut,
398 CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000399 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
400 if (!NoShowRawInsn) {
401 outs() << "\t";
Colin LeMahieufc32b1b2015-03-18 19:27:31 +0000402 DumpBytes(ArrayRef<uint8_t>(Bytes.data() + Index, Size));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000403 }
Akira Hatanakab46d0232015-03-27 20:36:02 +0000404 IP->printInst(&Inst, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +0000405 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000406 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000407 outs() << "\n";
408 } else {
409 errs() << ToolName << ": warning: invalid instruction encoding\n";
410 if (Size == 0)
411 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000412 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000413
414 // Print relocation for instruction.
415 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000416 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000417 uint64_t addr;
418 SmallString<16> name;
419 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000420
421 // If this relocation is hidden, skip it.
422 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
423 if (hidden) goto skip_print_rel;
424
Rafael Espindola1e483872013-04-25 12:28:45 +0000425 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000426 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000427 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000428 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
429 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
430
Greg Fitzgerald18432272014-03-20 22:55:15 +0000431 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000432 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000433
434 skip_print_rel:
435 ++rel_cur;
436 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000437 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000438 }
439 }
440}
441
Kevin Enderby98da6132015-01-20 21:47:46 +0000442void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000443 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
444 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000445 // Regular objdump doesn't print relocations in non-relocatable object
446 // files.
447 if (!Obj->isRelocatableObject())
448 return;
449
Alexey Samsonov48803e52014-03-13 14:37:36 +0000450 for (const SectionRef &Section : Obj->sections()) {
451 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000452 continue;
453 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000454 if (error(Section.getName(secname)))
455 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000456 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000457 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000458 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000459 uint64_t address;
460 SmallString<32> relocname;
461 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000462 if (error(Reloc.getHidden(hidden)))
463 continue;
464 if (hidden)
465 continue;
466 if (error(Reloc.getTypeName(relocname)))
467 continue;
468 if (error(Reloc.getOffset(address)))
469 continue;
470 if (error(Reloc.getValueString(valuestr)))
471 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000472 outs() << format(Fmt.data(), address) << " " << relocname << " "
473 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000474 }
475 outs() << "\n";
476 }
477}
478
Kevin Enderby98da6132015-01-20 21:47:46 +0000479void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000480 outs() << "Sections:\n"
481 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000482 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000483 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000484 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000485 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000486 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000487 uint64_t Address = Section.getAddress();
488 uint64_t Size = Section.getSize();
489 bool Text = Section.isText();
490 bool Data = Section.isData();
491 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000492 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000493 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000494 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
495 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000496 ++i;
497 }
498}
499
Kevin Enderby98da6132015-01-20 21:47:46 +0000500void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000501 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000502 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000503 StringRef Name;
504 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000505 if (error(Section.getName(Name)))
506 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000507 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000508 uint64_t Size = Section.getSize();
509 if (!Size)
510 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000511
512 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000513 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000514 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000515 ", %04" PRIx64 ")>\n",
516 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000517 continue;
518 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000519
David Majnemer8f6b04c2014-07-14 16:20:14 +0000520 if (error(Section.getContents(Contents)))
521 continue;
522
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000523 // Dump out the content as hex and printable ascii characters.
524 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000525 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000526 // Dump line of hex.
527 for (std::size_t i = 0; i < 16; ++i) {
528 if (i != 0 && i % 4 == 0)
529 outs() << ' ';
530 if (addr + i < end)
531 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
532 << hexdigit(Contents[addr + i] & 0xF, true);
533 else
534 outs() << " ";
535 }
536 // Print ascii.
537 outs() << " ";
538 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000539 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000540 outs() << Contents[addr + i];
541 else
542 outs() << ".";
543 }
544 outs() << "\n";
545 }
546 }
547}
548
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000549static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000550 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
551 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000552 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000553 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000554 return;
555
David Majnemer44f51e52014-09-10 12:51:52 +0000556 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000557 return;
558
559 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000560 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000561 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000562 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
563 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
564 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
565 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000566 << Name << "\n";
567
David Majnemer44f51e52014-09-10 12:51:52 +0000568 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000569 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000570 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000571 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000572 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000573
David Majnemer4d571592014-09-15 19:42:42 +0000574 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
575
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000576 outs() << "AUX "
577 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
578 , unsigned(asd->Length)
579 , unsigned(asd->NumberOfRelocations)
580 , unsigned(asd->NumberOfLinenumbers)
581 , unsigned(asd->CheckSum))
582 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000583 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000584 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000585 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000586 const char *FileName;
587 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000588 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000589
David Majnemer44f51e52014-09-10 12:51:52 +0000590 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
591 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000592 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000593
David Majnemer44f51e52014-09-10 12:51:52 +0000594 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000595 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000596 } else {
597 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000598 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000599 }
600 }
601}
602
Kevin Enderby98da6132015-01-20 21:47:46 +0000603void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000604 outs() << "SYMBOL TABLE:\n";
605
Rui Ueyama4e39f712014-03-18 18:58:51 +0000606 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000607 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000608 return;
609 }
610 for (const SymbolRef &Symbol : o->symbols()) {
611 StringRef Name;
612 uint64_t Address;
613 SymbolRef::Type Type;
614 uint64_t Size;
615 uint32_t Flags = Symbol.getFlags();
616 section_iterator Section = o->section_end();
617 if (error(Symbol.getName(Name)))
618 continue;
619 if (error(Symbol.getAddress(Address)))
620 continue;
621 if (error(Symbol.getType(Type)))
622 continue;
623 if (error(Symbol.getSize(Size)))
624 continue;
625 if (error(Symbol.getSection(Section)))
626 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000627
Rui Ueyama4e39f712014-03-18 18:58:51 +0000628 bool Global = Flags & SymbolRef::SF_Global;
629 bool Weak = Flags & SymbolRef::SF_Weak;
630 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000631 bool Common = Flags & SymbolRef::SF_Common;
David Meyer1df4b842012-02-28 23:47:53 +0000632
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000633 if (Common) {
634 uint32_t Alignment;
635 if (error(Symbol.getAlignment(Alignment)))
636 Alignment = 0;
637 Address = Size;
638 Size = Alignment;
639 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000640 if (Address == UnknownAddressOrSize)
641 Address = 0;
642 if (Size == UnknownAddressOrSize)
643 Size = 0;
644 char GlobLoc = ' ';
645 if (Type != SymbolRef::ST_Unknown)
646 GlobLoc = Global ? 'g' : 'l';
647 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
648 ? 'd' : ' ';
649 char FileFunc = ' ';
650 if (Type == SymbolRef::ST_File)
651 FileFunc = 'f';
652 else if (Type == SymbolRef::ST_Function)
653 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000654
Rui Ueyama4e39f712014-03-18 18:58:51 +0000655 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
656 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000657
Rui Ueyama4e39f712014-03-18 18:58:51 +0000658 outs() << format(Fmt, Address) << " "
659 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
660 << (Weak ? 'w' : ' ') // Weak?
661 << ' ' // Constructor. Not supported yet.
662 << ' ' // Warning. Not supported yet.
663 << ' ' // Indirect reference to another symbol.
664 << Debug // Debugging (d) or dynamic (D) symbol.
665 << FileFunc // Name of function (F), file (f) or object (O).
666 << ' ';
667 if (Absolute) {
668 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000669 } else if (Common) {
670 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +0000671 } else if (Section == o->section_end()) {
672 outs() << "*UND*";
673 } else {
674 if (const MachOObjectFile *MachO =
675 dyn_cast<const MachOObjectFile>(o)) {
676 DataRefImpl DR = Section->getRawDataRefImpl();
677 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
678 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000679 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000680 StringRef SectionName;
681 if (error(Section->getName(SectionName)))
682 SectionName = "";
683 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000684 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000685 outs() << '\t'
686 << format("%08" PRIx64 " ", Size)
687 << Name
688 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000689 }
690}
691
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000692static void PrintUnwindInfo(const ObjectFile *o) {
693 outs() << "Unwind info:\n\n";
694
695 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
696 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000697 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
698 printMachOUnwindInfo(MachO);
699 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000700 // TODO: Extract DWARF dump tool to objdump.
701 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000702 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000703 return;
704 }
705}
706
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000707void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +0000708 outs() << "Exports trie:\n";
709 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
710 printMachOExportsTrie(MachO);
711 else {
712 errs() << "This operation is only currently supported "
713 "for Mach-O executable files.\n";
714 return;
715 }
716}
717
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000718void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +0000719 outs() << "Rebase table:\n";
720 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
721 printMachORebaseTable(MachO);
722 else {
723 errs() << "This operation is only currently supported "
724 "for Mach-O executable files.\n";
725 return;
726 }
727}
728
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000729void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000730 outs() << "Bind table:\n";
731 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
732 printMachOBindTable(MachO);
733 else {
734 errs() << "This operation is only currently supported "
735 "for Mach-O executable files.\n";
736 return;
737 }
738}
739
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000740void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000741 outs() << "Lazy bind table:\n";
742 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
743 printMachOLazyBindTable(MachO);
744 else {
745 errs() << "This operation is only currently supported "
746 "for Mach-O executable files.\n";
747 return;
748 }
749}
750
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000751void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000752 outs() << "Weak bind table:\n";
753 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
754 printMachOWeakBindTable(MachO);
755 else {
756 errs() << "This operation is only currently supported "
757 "for Mach-O executable files.\n";
758 return;
759 }
760}
Nick Kledzikac431442014-09-12 21:34:15 +0000761
Rui Ueyamac2bed422013-09-27 21:04:00 +0000762static void printPrivateFileHeader(const ObjectFile *o) {
763 if (o->isELF()) {
764 printELFFileHeader(o);
765 } else if (o->isCOFF()) {
766 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000767 } else if (o->isMachO()) {
768 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000769 }
770}
771
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000772static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000773 outs() << '\n';
774 outs() << o->getFileName()
775 << ":\tfile format " << o->getFileFormatName() << "\n\n";
776
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000777 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000778 DisassembleObject(o, Relocations);
779 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000780 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000781 if (SectionHeaders)
782 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000783 if (SectionContents)
784 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000785 if (SymbolTable)
786 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000787 if (UnwindInfo)
788 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000789 if (PrivateHeaders)
790 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000791 if (ExportsTrie)
792 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000793 if (Rebase)
794 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000795 if (Bind)
796 printBindTable(o);
797 if (LazyBind)
798 printLazyBindTable(o);
799 if (WeakBind)
800 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000801}
802
803/// @brief Dump each object file in \a a;
804static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000805 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
806 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000807 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
808 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000809 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000810 if (EC != object_error::invalid_file_type)
811 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000812 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000813 continue;
814 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000815 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000816 DumpObject(o);
817 else
818 errs() << ToolName << ": '" << a->getFileName() << "': "
819 << "Unrecognized file type.\n";
820 }
821}
822
823/// @brief Open file and figure out how to dump it.
824static void DumpInput(StringRef file) {
825 // If file isn't stdin, check that it exists.
826 if (file != "-" && !sys::fs::exists(file)) {
827 errs() << ToolName << ": '" << file << "': " << "No such file\n";
828 return;
829 }
830
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000831 // If we are using the Mach-O specific object file parser, then let it parse
832 // the file and process the command line options. So the -arch flags can
833 // be used to select specific slices, etc.
834 if (MachOOpt) {
835 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000836 return;
837 }
838
839 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000840 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000841 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000842 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000843 return;
844 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000845 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000846
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000847 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000848 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000849 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000850 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000851 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000852 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000853}
854
Michael J. Spencer2670c252011-01-20 06:39:06 +0000855int main(int argc, char **argv) {
856 // Print a stack trace if we signal out.
857 sys::PrintStackTraceOnErrorSignal();
858 PrettyStackTraceProgram X(argc, argv);
859 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
860
861 // Initialize targets and assembly printers/parsers.
862 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000863 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000864 llvm::InitializeAllAsmParsers();
865 llvm::InitializeAllDisassemblers();
866
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000867 // Register the target printer for --version.
868 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
869
Michael J. Spencer2670c252011-01-20 06:39:06 +0000870 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
871 TripleName = Triple::normalize(TripleName);
872
873 ToolName = argv[0];
874
875 // Defaults to a.out if no filenames specified.
876 if (InputFilenames.size() == 0)
877 InputFilenames.push_back("a.out");
878
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000879 if (!Disassemble
880 && !Relocations
881 && !SectionHeaders
882 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000883 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000884 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000885 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000886 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000887 && !Rebase
888 && !Bind
889 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +0000890 && !WeakBind
Kevin Enderby13023a12015-01-15 23:19:11 +0000891 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000892 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000893 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +0000894 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000895 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +0000896 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000897 && !(DylibsUsed && MachOOpt)
898 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +0000899 && !(ObjcMetaData && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000900 && !(DumpSections.size() != 0 && MachOOpt)) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000901 cl::PrintHelpMessage();
902 return 2;
903 }
904
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000905 std::for_each(InputFilenames.begin(), InputFilenames.end(),
906 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000907
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000908 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000909}