blob: a78d1d267dd8c1a2614ee912e7c71ab77cd6a806 [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
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000197bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer51862b32011-10-13 22:17:18 +0000198 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000199 if (error(a.getOffset(a_addr))) return false;
200 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000201 return a_addr < b_addr;
202}
203
Colin LeMahieufb76b002015-05-28 19:07:14 +0000204namespace {
205class PrettyPrinter {
206public:
207 virtual void printInst(MCInstPrinter &IP, const MCInst *MI, bool ShowRawInsn,
208 ArrayRef<uint8_t> Bytes, uint64_t Address,
209 raw_ostream &OS, StringRef Annot,
210 MCSubtargetInfo const &STI) {
211 outs() << format("%8" PRIx64 ":", Address);
212 if (!NoShowRawInsn) {
213 outs() << "\t";
214 dumpBytes(Bytes, outs());
215 }
216 IP.printInst(MI, outs(), "", STI);
217 }
218};
219PrettyPrinter PrettyPrinterInst;
220PrettyPrinter &selectPrettyPrinter(Triple const &Triple, MCInstPrinter &IP) {
221 switch(Triple.getArch()) {
222 default:
223 return PrettyPrinterInst;
224 }
225}
226}
227
Michael J. Spencer51862b32011-10-13 22:17:18 +0000228static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000229 const Target *TheTarget = getTarget(Obj);
230 // getTarget() will have already issued a diagnostic if necessary, so
231 // just bail here if it failed.
232 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000233 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000234
Jack Carter551efd72012-08-28 19:24:49 +0000235 // Package up features to be passed to target/subtarget
236 std::string FeaturesStr;
237 if (MAttrs.size()) {
238 SubtargetFeatures Features;
239 for (unsigned i = 0; i != MAttrs.size(); ++i)
240 Features.AddFeature(MAttrs[i]);
241 FeaturesStr = Features.getString();
242 }
243
Ahmed Charles56440fd2014-03-06 05:51:42 +0000244 std::unique_ptr<const MCRegisterInfo> MRI(
245 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000246 if (!MRI) {
247 errs() << "error: no register info for target " << TripleName << "\n";
248 return;
249 }
250
251 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000252 std::unique_ptr<const MCAsmInfo> AsmInfo(
253 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000254 if (!AsmInfo) {
255 errs() << "error: no assembly info for target " << TripleName << "\n";
256 return;
257 }
258
Ahmed Charles56440fd2014-03-06 05:51:42 +0000259 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000260 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000261 if (!STI) {
262 errs() << "error: no subtarget info for target " << TripleName << "\n";
263 return;
264 }
265
Ahmed Charles56440fd2014-03-06 05:51:42 +0000266 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000267 if (!MII) {
268 errs() << "error: no instruction info for target " << TripleName << "\n";
269 return;
270 }
271
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000272 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
273 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
274
275 std::unique_ptr<MCDisassembler> DisAsm(
276 TheTarget->createMCDisassembler(*STI, Ctx));
277
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000278 if (!DisAsm) {
279 errs() << "error: no disassembler for target " << TripleName << "\n";
280 return;
281 }
282
Ahmed Charles56440fd2014-03-06 05:51:42 +0000283 std::unique_ptr<const MCInstrAnalysis> MIA(
284 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000285
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000286 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000287 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +0000288 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000289 if (!IP) {
290 errs() << "error: no instruction printer for target " << TripleName
291 << '\n';
292 return;
293 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000294 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName), *IP);
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000295
Greg Fitzgerald18432272014-03-20 22:55:15 +0000296 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
297 "\t\t\t%08" PRIx64 ": ";
298
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000299 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
300 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000301 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000302 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
303 for (const SectionRef &Section : Obj->sections()) {
304 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000305 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000306 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000307 }
308
Alexey Samsonov48803e52014-03-13 14:37:36 +0000309 for (const SectionRef &Section : Obj->sections()) {
David Majnemer236b0ca2014-11-17 11:17:17 +0000310 if (!Section.isText() || Section.isVirtual())
Mark Seaborneb03ac52014-01-25 00:32:01 +0000311 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000312
Rafael Espindola80291272014-10-08 15:28:58 +0000313 uint64_t SectionAddr = Section.getAddress();
314 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000315 if (!SectSize)
316 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000317
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000318 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000319 std::vector<std::pair<uint64_t, StringRef>> Symbols;
320 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000321 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000322 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000323 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000324 break;
325 if (Address == UnknownAddressOrSize)
326 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000327 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000328 if (Address >= SectSize)
329 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000330
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000331 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000332 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000333 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000334 Symbols.push_back(std::make_pair(Address, Name));
335 }
336 }
337
338 // Sort the symbols by address, just in case they didn't come in that way.
339 array_pod_sort(Symbols.begin(), Symbols.end());
340
Michael J. Spencer51862b32011-10-13 22:17:18 +0000341 // Make a list of all the relocations for this section.
342 std::vector<RelocationRef> Rels;
343 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000344 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
345 for (const RelocationRef &Reloc : RelocSec.relocations()) {
346 Rels.push_back(Reloc);
347 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000348 }
349 }
350
351 // Sort relocations by address.
352 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
353
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000354 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000355 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000356 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000357 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000358 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000359 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000360 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000361 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000362 outs() << "Disassembly of section ";
363 if (!SegmentName.empty())
364 outs() << SegmentName << ",";
365 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000366
367 // If the section has no symbols just insert a dummy one and disassemble
368 // the whole section.
369 if (Symbols.empty())
370 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000371
Alp Tokere69170a2014-06-26 22:52:05 +0000372
373 SmallString<40> Comments;
374 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000375
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000376 StringRef BytesStr;
377 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000378 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000379 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
380 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000381
Michael J. Spencer2670c252011-01-20 06:39:06 +0000382 uint64_t Size;
383 uint64_t Index;
384
Michael J. Spencer51862b32011-10-13 22:17:18 +0000385 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
386 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000387 // Disassemble symbol by symbol.
388 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000389
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000390 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000391 // The end is either the section end or the beginning of the next symbol.
392 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
393 // If this symbol has the same address as the next symbol, then skip it.
394 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000395 continue;
396
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000397 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000398
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000399#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000400 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000401#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000402 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000403#endif
404
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000405 for (Index = Start; Index < End; Index += Size) {
406 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000407
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000408 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
409 SectionAddr + Index, DebugOut,
410 CommentStream)) {
Colin LeMahieufb76b002015-05-28 19:07:14 +0000411 PIP.printInst(*IP, &Inst, !NoShowRawInsn,
412 Bytes.slice(Index, Size),
413 SectionAddr + Index, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +0000414 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000415 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000416 outs() << "\n";
417 } else {
418 errs() << ToolName << ": warning: invalid instruction encoding\n";
419 if (Size == 0)
420 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000421 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000422
423 // Print relocation for instruction.
424 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000425 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000426 uint64_t addr;
427 SmallString<16> name;
428 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000429
430 // If this relocation is hidden, skip it.
431 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
432 if (hidden) goto skip_print_rel;
433
Rafael Espindola1e483872013-04-25 12:28:45 +0000434 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000435 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000436 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000437 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
438 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
439
Greg Fitzgerald18432272014-03-20 22:55:15 +0000440 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000441 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000442
443 skip_print_rel:
444 ++rel_cur;
445 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000446 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000447 }
448 }
449}
450
Kevin Enderby98da6132015-01-20 21:47:46 +0000451void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000452 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
453 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000454 // Regular objdump doesn't print relocations in non-relocatable object
455 // files.
456 if (!Obj->isRelocatableObject())
457 return;
458
Alexey Samsonov48803e52014-03-13 14:37:36 +0000459 for (const SectionRef &Section : Obj->sections()) {
460 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000461 continue;
462 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000463 if (error(Section.getName(secname)))
464 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000465 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000466 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000467 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000468 uint64_t address;
469 SmallString<32> relocname;
470 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000471 if (error(Reloc.getHidden(hidden)))
472 continue;
473 if (hidden)
474 continue;
475 if (error(Reloc.getTypeName(relocname)))
476 continue;
477 if (error(Reloc.getOffset(address)))
478 continue;
479 if (error(Reloc.getValueString(valuestr)))
480 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000481 outs() << format(Fmt.data(), address) << " " << relocname << " "
482 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000483 }
484 outs() << "\n";
485 }
486}
487
Kevin Enderby98da6132015-01-20 21:47:46 +0000488void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000489 outs() << "Sections:\n"
490 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000491 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000492 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000493 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000494 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000495 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000496 uint64_t Address = Section.getAddress();
497 uint64_t Size = Section.getSize();
498 bool Text = Section.isText();
499 bool Data = Section.isData();
500 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000501 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000502 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000503 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
504 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000505 ++i;
506 }
507}
508
Kevin Enderby98da6132015-01-20 21:47:46 +0000509void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000510 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000511 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000512 StringRef Name;
513 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000514 if (error(Section.getName(Name)))
515 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000516 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000517 uint64_t Size = Section.getSize();
518 if (!Size)
519 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000520
521 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000522 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000523 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000524 ", %04" PRIx64 ")>\n",
525 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000526 continue;
527 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000528
David Majnemer8f6b04c2014-07-14 16:20:14 +0000529 if (error(Section.getContents(Contents)))
530 continue;
531
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000532 // Dump out the content as hex and printable ascii characters.
533 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000534 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000535 // Dump line of hex.
536 for (std::size_t i = 0; i < 16; ++i) {
537 if (i != 0 && i % 4 == 0)
538 outs() << ' ';
539 if (addr + i < end)
540 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
541 << hexdigit(Contents[addr + i] & 0xF, true);
542 else
543 outs() << " ";
544 }
545 // Print ascii.
546 outs() << " ";
547 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000548 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000549 outs() << Contents[addr + i];
550 else
551 outs() << ".";
552 }
553 outs() << "\n";
554 }
555 }
556}
557
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000558static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000559 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
560 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000561 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000562 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000563 return;
564
David Majnemer44f51e52014-09-10 12:51:52 +0000565 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000566 return;
567
568 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000569 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000570 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000571 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
572 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
573 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
574 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000575 << Name << "\n";
576
David Majnemer44f51e52014-09-10 12:51:52 +0000577 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000578 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000579 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000580 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000581 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000582
David Majnemer4d571592014-09-15 19:42:42 +0000583 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
584
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000585 outs() << "AUX "
586 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
587 , unsigned(asd->Length)
588 , unsigned(asd->NumberOfRelocations)
589 , unsigned(asd->NumberOfLinenumbers)
590 , unsigned(asd->CheckSum))
591 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000592 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000593 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000594 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000595 const char *FileName;
596 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000597 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000598
David Majnemer44f51e52014-09-10 12:51:52 +0000599 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
600 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000601 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000602
David Majnemer44f51e52014-09-10 12:51:52 +0000603 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000604 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000605 } else {
606 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000607 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000608 }
609 }
610}
611
Kevin Enderby98da6132015-01-20 21:47:46 +0000612void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000613 outs() << "SYMBOL TABLE:\n";
614
Rui Ueyama4e39f712014-03-18 18:58:51 +0000615 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000616 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000617 return;
618 }
619 for (const SymbolRef &Symbol : o->symbols()) {
620 StringRef Name;
621 uint64_t Address;
622 SymbolRef::Type Type;
623 uint64_t Size;
624 uint32_t Flags = Symbol.getFlags();
625 section_iterator Section = o->section_end();
626 if (error(Symbol.getName(Name)))
627 continue;
628 if (error(Symbol.getAddress(Address)))
629 continue;
630 if (error(Symbol.getType(Type)))
631 continue;
632 if (error(Symbol.getSize(Size)))
633 continue;
634 if (error(Symbol.getSection(Section)))
635 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000636
Rui Ueyama4e39f712014-03-18 18:58:51 +0000637 bool Global = Flags & SymbolRef::SF_Global;
638 bool Weak = Flags & SymbolRef::SF_Weak;
639 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000640 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +0000641 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +0000642
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000643 if (Common) {
644 uint32_t Alignment;
645 if (error(Symbol.getAlignment(Alignment)))
646 Alignment = 0;
647 Address = Size;
648 Size = Alignment;
649 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000650 if (Address == UnknownAddressOrSize)
651 Address = 0;
652 if (Size == UnknownAddressOrSize)
653 Size = 0;
654 char GlobLoc = ' ';
655 if (Type != SymbolRef::ST_Unknown)
656 GlobLoc = Global ? 'g' : 'l';
657 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
658 ? 'd' : ' ';
659 char FileFunc = ' ';
660 if (Type == SymbolRef::ST_File)
661 FileFunc = 'f';
662 else if (Type == SymbolRef::ST_Function)
663 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000664
Rui Ueyama4e39f712014-03-18 18:58:51 +0000665 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
666 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000667
Rui Ueyama4e39f712014-03-18 18:58:51 +0000668 outs() << format(Fmt, Address) << " "
669 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
670 << (Weak ? 'w' : ' ') // Weak?
671 << ' ' // Constructor. Not supported yet.
672 << ' ' // Warning. Not supported yet.
673 << ' ' // Indirect reference to another symbol.
674 << Debug // Debugging (d) or dynamic (D) symbol.
675 << FileFunc // Name of function (F), file (f) or object (O).
676 << ' ';
677 if (Absolute) {
678 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000679 } else if (Common) {
680 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +0000681 } else if (Section == o->section_end()) {
682 outs() << "*UND*";
683 } else {
684 if (const MachOObjectFile *MachO =
685 dyn_cast<const MachOObjectFile>(o)) {
686 DataRefImpl DR = Section->getRawDataRefImpl();
687 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
688 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000689 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000690 StringRef SectionName;
691 if (error(Section->getName(SectionName)))
692 SectionName = "";
693 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000694 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000695 outs() << '\t'
Davide Italianocd2514d2015-04-30 23:08:53 +0000696 << format("%08" PRIx64 " ", Size);
697 if (Hidden) {
698 outs() << ".hidden ";
699 }
700 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +0000701 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000702 }
703}
704
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000705static void PrintUnwindInfo(const ObjectFile *o) {
706 outs() << "Unwind info:\n\n";
707
708 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
709 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000710 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
711 printMachOUnwindInfo(MachO);
712 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000713 // TODO: Extract DWARF dump tool to objdump.
714 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000715 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000716 return;
717 }
718}
719
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000720void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +0000721 outs() << "Exports trie:\n";
722 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
723 printMachOExportsTrie(MachO);
724 else {
725 errs() << "This operation is only currently supported "
726 "for Mach-O executable files.\n";
727 return;
728 }
729}
730
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000731void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +0000732 outs() << "Rebase table:\n";
733 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
734 printMachORebaseTable(MachO);
735 else {
736 errs() << "This operation is only currently supported "
737 "for Mach-O executable files.\n";
738 return;
739 }
740}
741
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000742void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000743 outs() << "Bind table:\n";
744 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
745 printMachOBindTable(MachO);
746 else {
747 errs() << "This operation is only currently supported "
748 "for Mach-O executable files.\n";
749 return;
750 }
751}
752
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000753void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000754 outs() << "Lazy bind table:\n";
755 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
756 printMachOLazyBindTable(MachO);
757 else {
758 errs() << "This operation is only currently supported "
759 "for Mach-O executable files.\n";
760 return;
761 }
762}
763
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000764void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000765 outs() << "Weak bind table:\n";
766 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
767 printMachOWeakBindTable(MachO);
768 else {
769 errs() << "This operation is only currently supported "
770 "for Mach-O executable files.\n";
771 return;
772 }
773}
Nick Kledzikac431442014-09-12 21:34:15 +0000774
Rui Ueyamac2bed422013-09-27 21:04:00 +0000775static void printPrivateFileHeader(const ObjectFile *o) {
776 if (o->isELF()) {
777 printELFFileHeader(o);
778 } else if (o->isCOFF()) {
779 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000780 } else if (o->isMachO()) {
781 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000782 }
783}
784
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000785static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000786 outs() << '\n';
787 outs() << o->getFileName()
788 << ":\tfile format " << o->getFileFormatName() << "\n\n";
789
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000790 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000791 DisassembleObject(o, Relocations);
792 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000793 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000794 if (SectionHeaders)
795 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000796 if (SectionContents)
797 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000798 if (SymbolTable)
799 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000800 if (UnwindInfo)
801 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000802 if (PrivateHeaders)
803 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000804 if (ExportsTrie)
805 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000806 if (Rebase)
807 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000808 if (Bind)
809 printBindTable(o);
810 if (LazyBind)
811 printLazyBindTable(o);
812 if (WeakBind)
813 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000814}
815
816/// @brief Dump each object file in \a a;
817static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000818 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
819 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000820 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
821 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000822 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000823 if (EC != object_error::invalid_file_type)
824 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000825 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000826 continue;
827 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000828 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000829 DumpObject(o);
830 else
831 errs() << ToolName << ": '" << a->getFileName() << "': "
832 << "Unrecognized file type.\n";
833 }
834}
835
836/// @brief Open file and figure out how to dump it.
837static void DumpInput(StringRef file) {
838 // If file isn't stdin, check that it exists.
839 if (file != "-" && !sys::fs::exists(file)) {
840 errs() << ToolName << ": '" << file << "': " << "No such file\n";
841 return;
842 }
843
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000844 // If we are using the Mach-O specific object file parser, then let it parse
845 // the file and process the command line options. So the -arch flags can
846 // be used to select specific slices, etc.
847 if (MachOOpt) {
848 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000849 return;
850 }
851
852 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000853 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000854 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000855 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000856 return;
857 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000858 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000859
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000860 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000861 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000862 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000863 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000864 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000865 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000866}
867
Michael J. Spencer2670c252011-01-20 06:39:06 +0000868int main(int argc, char **argv) {
869 // Print a stack trace if we signal out.
870 sys::PrintStackTraceOnErrorSignal();
871 PrettyStackTraceProgram X(argc, argv);
872 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
873
874 // Initialize targets and assembly printers/parsers.
875 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000876 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000877 llvm::InitializeAllAsmParsers();
878 llvm::InitializeAllDisassemblers();
879
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000880 // Register the target printer for --version.
881 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
882
Michael J. Spencer2670c252011-01-20 06:39:06 +0000883 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
884 TripleName = Triple::normalize(TripleName);
885
886 ToolName = argv[0];
887
888 // Defaults to a.out if no filenames specified.
889 if (InputFilenames.size() == 0)
890 InputFilenames.push_back("a.out");
891
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000892 if (!Disassemble
893 && !Relocations
894 && !SectionHeaders
895 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000896 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000897 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000898 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000899 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000900 && !Rebase
901 && !Bind
902 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +0000903 && !WeakBind
Kevin Enderby13023a12015-01-15 23:19:11 +0000904 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000905 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000906 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +0000907 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000908 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +0000909 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000910 && !(DylibsUsed && MachOOpt)
911 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +0000912 && !(ObjcMetaData && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000913 && !(DumpSections.size() != 0 && MachOOpt)) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000914 cl::PrintHelpMessage();
915 return 2;
916 }
917
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000918 std::for_each(InputFilenames.begin(), InputFilenames.end(),
919 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000920
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000921 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000922}