blob: 080a059d576b8db24f93e6491ef5def854b9b8d8 [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:
Colin LeMahieu0b5890d2015-05-28 20:59:08 +0000207 virtual ~PrettyPrinter(){}
Colin LeMahieufb76b002015-05-28 19:07:14 +0000208 virtual void printInst(MCInstPrinter &IP, const MCInst *MI, bool ShowRawInsn,
209 ArrayRef<uint8_t> Bytes, uint64_t Address,
210 raw_ostream &OS, StringRef Annot,
211 MCSubtargetInfo const &STI) {
212 outs() << format("%8" PRIx64 ":", Address);
213 if (!NoShowRawInsn) {
214 outs() << "\t";
215 dumpBytes(Bytes, outs());
216 }
217 IP.printInst(MI, outs(), "", STI);
218 }
219};
220PrettyPrinter PrettyPrinterInst;
221PrettyPrinter &selectPrettyPrinter(Triple const &Triple, MCInstPrinter &IP) {
Aaron Ballman1196ca22015-05-29 13:00:07 +0000222 return PrettyPrinterInst;
Colin LeMahieufb76b002015-05-28 19:07:14 +0000223}
224}
225
Michael J. Spencer51862b32011-10-13 22:17:18 +0000226static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000227 const Target *TheTarget = getTarget(Obj);
228 // getTarget() will have already issued a diagnostic if necessary, so
229 // just bail here if it failed.
230 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000231 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000232
Jack Carter551efd72012-08-28 19:24:49 +0000233 // Package up features to be passed to target/subtarget
234 std::string FeaturesStr;
235 if (MAttrs.size()) {
236 SubtargetFeatures Features;
237 for (unsigned i = 0; i != MAttrs.size(); ++i)
238 Features.AddFeature(MAttrs[i]);
239 FeaturesStr = Features.getString();
240 }
241
Ahmed Charles56440fd2014-03-06 05:51:42 +0000242 std::unique_ptr<const MCRegisterInfo> MRI(
243 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000244 if (!MRI) {
245 errs() << "error: no register info for target " << TripleName << "\n";
246 return;
247 }
248
249 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000250 std::unique_ptr<const MCAsmInfo> AsmInfo(
251 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000252 if (!AsmInfo) {
253 errs() << "error: no assembly info for target " << TripleName << "\n";
254 return;
255 }
256
Ahmed Charles56440fd2014-03-06 05:51:42 +0000257 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000258 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000259 if (!STI) {
260 errs() << "error: no subtarget info for target " << TripleName << "\n";
261 return;
262 }
263
Ahmed Charles56440fd2014-03-06 05:51:42 +0000264 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000265 if (!MII) {
266 errs() << "error: no instruction info for target " << TripleName << "\n";
267 return;
268 }
269
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000270 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
271 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
272
273 std::unique_ptr<MCDisassembler> DisAsm(
274 TheTarget->createMCDisassembler(*STI, Ctx));
275
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000276 if (!DisAsm) {
277 errs() << "error: no disassembler for target " << TripleName << "\n";
278 return;
279 }
280
Ahmed Charles56440fd2014-03-06 05:51:42 +0000281 std::unique_ptr<const MCInstrAnalysis> MIA(
282 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000283
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000284 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000285 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +0000286 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000287 if (!IP) {
288 errs() << "error: no instruction printer for target " << TripleName
289 << '\n';
290 return;
291 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000292 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName), *IP);
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000293
Greg Fitzgerald18432272014-03-20 22:55:15 +0000294 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
295 "\t\t\t%08" PRIx64 ": ";
296
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000297 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
298 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000299 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000300 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
301 for (const SectionRef &Section : Obj->sections()) {
302 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000303 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000304 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000305 }
306
Alexey Samsonov48803e52014-03-13 14:37:36 +0000307 for (const SectionRef &Section : Obj->sections()) {
David Majnemer236b0ca2014-11-17 11:17:17 +0000308 if (!Section.isText() || Section.isVirtual())
Mark Seaborneb03ac52014-01-25 00:32:01 +0000309 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000310
Rafael Espindola80291272014-10-08 15:28:58 +0000311 uint64_t SectionAddr = Section.getAddress();
312 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000313 if (!SectSize)
314 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000315
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000316 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000317 std::vector<std::pair<uint64_t, StringRef>> Symbols;
318 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000319 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000320 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000321 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000322 break;
323 if (Address == UnknownAddressOrSize)
324 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000325 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000326 if (Address >= SectSize)
327 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000328
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000329 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000330 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000331 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000332 Symbols.push_back(std::make_pair(Address, Name));
333 }
334 }
335
336 // Sort the symbols by address, just in case they didn't come in that way.
337 array_pod_sort(Symbols.begin(), Symbols.end());
338
Michael J. Spencer51862b32011-10-13 22:17:18 +0000339 // Make a list of all the relocations for this section.
340 std::vector<RelocationRef> Rels;
341 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000342 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
343 for (const RelocationRef &Reloc : RelocSec.relocations()) {
344 Rels.push_back(Reloc);
345 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000346 }
347 }
348
349 // Sort relocations by address.
350 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
351
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000352 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000353 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000354 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000355 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000356 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000357 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000358 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000359 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000360 outs() << "Disassembly of section ";
361 if (!SegmentName.empty())
362 outs() << SegmentName << ",";
363 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000364
365 // If the section has no symbols just insert a dummy one and disassemble
366 // the whole section.
367 if (Symbols.empty())
368 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000369
Alp Tokere69170a2014-06-26 22:52:05 +0000370
371 SmallString<40> Comments;
372 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000373
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000374 StringRef BytesStr;
375 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000376 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000377 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
378 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000379
Michael J. Spencer2670c252011-01-20 06:39:06 +0000380 uint64_t Size;
381 uint64_t Index;
382
Michael J. Spencer51862b32011-10-13 22:17:18 +0000383 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
384 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000385 // Disassemble symbol by symbol.
386 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000387
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000388 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000389 // The end is either the section end or the beginning of the next symbol.
390 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
391 // If this symbol has the same address as the next symbol, then skip it.
392 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000393 continue;
394
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000395 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000396
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000397#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000398 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000399#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000400 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000401#endif
402
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000403 for (Index = Start; Index < End; Index += Size) {
404 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000405
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000406 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
407 SectionAddr + Index, DebugOut,
408 CommentStream)) {
Colin LeMahieufb76b002015-05-28 19:07:14 +0000409 PIP.printInst(*IP, &Inst, !NoShowRawInsn,
410 Bytes.slice(Index, Size),
411 SectionAddr + Index, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +0000412 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000413 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000414 outs() << "\n";
415 } else {
416 errs() << ToolName << ": warning: invalid instruction encoding\n";
417 if (Size == 0)
418 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000419 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000420
421 // Print relocation for instruction.
422 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000423 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000424 uint64_t addr;
425 SmallString<16> name;
426 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000427
428 // If this relocation is hidden, skip it.
429 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
430 if (hidden) goto skip_print_rel;
431
Rafael Espindola1e483872013-04-25 12:28:45 +0000432 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000433 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000434 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000435 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
436 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
437
Greg Fitzgerald18432272014-03-20 22:55:15 +0000438 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000439 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000440
441 skip_print_rel:
442 ++rel_cur;
443 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000444 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000445 }
446 }
447}
448
Kevin Enderby98da6132015-01-20 21:47:46 +0000449void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000450 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
451 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000452 // Regular objdump doesn't print relocations in non-relocatable object
453 // files.
454 if (!Obj->isRelocatableObject())
455 return;
456
Alexey Samsonov48803e52014-03-13 14:37:36 +0000457 for (const SectionRef &Section : Obj->sections()) {
458 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000459 continue;
460 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000461 if (error(Section.getName(secname)))
462 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000463 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000464 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000465 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000466 uint64_t address;
467 SmallString<32> relocname;
468 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000469 if (error(Reloc.getHidden(hidden)))
470 continue;
471 if (hidden)
472 continue;
473 if (error(Reloc.getTypeName(relocname)))
474 continue;
475 if (error(Reloc.getOffset(address)))
476 continue;
477 if (error(Reloc.getValueString(valuestr)))
478 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000479 outs() << format(Fmt.data(), address) << " " << relocname << " "
480 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000481 }
482 outs() << "\n";
483 }
484}
485
Kevin Enderby98da6132015-01-20 21:47:46 +0000486void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000487 outs() << "Sections:\n"
488 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000489 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000490 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000491 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000492 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000493 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000494 uint64_t Address = Section.getAddress();
495 uint64_t Size = Section.getSize();
496 bool Text = Section.isText();
497 bool Data = Section.isData();
498 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000499 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000500 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000501 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
502 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000503 ++i;
504 }
505}
506
Kevin Enderby98da6132015-01-20 21:47:46 +0000507void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000508 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000509 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000510 StringRef Name;
511 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000512 if (error(Section.getName(Name)))
513 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000514 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000515 uint64_t Size = Section.getSize();
516 if (!Size)
517 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000518
519 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000520 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000521 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000522 ", %04" PRIx64 ")>\n",
523 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000524 continue;
525 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000526
David Majnemer8f6b04c2014-07-14 16:20:14 +0000527 if (error(Section.getContents(Contents)))
528 continue;
529
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000530 // Dump out the content as hex and printable ascii characters.
531 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000532 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000533 // Dump line of hex.
534 for (std::size_t i = 0; i < 16; ++i) {
535 if (i != 0 && i % 4 == 0)
536 outs() << ' ';
537 if (addr + i < end)
538 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
539 << hexdigit(Contents[addr + i] & 0xF, true);
540 else
541 outs() << " ";
542 }
543 // Print ascii.
544 outs() << " ";
545 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000546 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000547 outs() << Contents[addr + i];
548 else
549 outs() << ".";
550 }
551 outs() << "\n";
552 }
553 }
554}
555
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000556static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000557 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
558 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000559 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000560 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000561 return;
562
David Majnemer44f51e52014-09-10 12:51:52 +0000563 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000564 return;
565
566 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000567 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000568 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000569 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
570 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
571 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
572 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000573 << Name << "\n";
574
David Majnemer44f51e52014-09-10 12:51:52 +0000575 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000576 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000577 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000578 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000579 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000580
David Majnemer4d571592014-09-15 19:42:42 +0000581 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
582
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000583 outs() << "AUX "
584 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
585 , unsigned(asd->Length)
586 , unsigned(asd->NumberOfRelocations)
587 , unsigned(asd->NumberOfLinenumbers)
588 , unsigned(asd->CheckSum))
589 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000590 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000591 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000592 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000593 const char *FileName;
594 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000595 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000596
David Majnemer44f51e52014-09-10 12:51:52 +0000597 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
598 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000599 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000600
David Majnemer44f51e52014-09-10 12:51:52 +0000601 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000602 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000603 } else {
604 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000605 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000606 }
607 }
608}
609
Kevin Enderby98da6132015-01-20 21:47:46 +0000610void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000611 outs() << "SYMBOL TABLE:\n";
612
Rui Ueyama4e39f712014-03-18 18:58:51 +0000613 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000614 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000615 return;
616 }
617 for (const SymbolRef &Symbol : o->symbols()) {
618 StringRef Name;
619 uint64_t Address;
620 SymbolRef::Type Type;
621 uint64_t Size;
622 uint32_t Flags = Symbol.getFlags();
623 section_iterator Section = o->section_end();
624 if (error(Symbol.getName(Name)))
625 continue;
626 if (error(Symbol.getAddress(Address)))
627 continue;
628 if (error(Symbol.getType(Type)))
629 continue;
630 if (error(Symbol.getSize(Size)))
631 continue;
632 if (error(Symbol.getSection(Section)))
633 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000634
Rui Ueyama4e39f712014-03-18 18:58:51 +0000635 bool Global = Flags & SymbolRef::SF_Global;
636 bool Weak = Flags & SymbolRef::SF_Weak;
637 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000638 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +0000639 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +0000640
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000641 if (Common) {
642 uint32_t Alignment;
643 if (error(Symbol.getAlignment(Alignment)))
644 Alignment = 0;
645 Address = Size;
646 Size = Alignment;
647 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000648 if (Address == UnknownAddressOrSize)
649 Address = 0;
650 if (Size == UnknownAddressOrSize)
651 Size = 0;
652 char GlobLoc = ' ';
653 if (Type != SymbolRef::ST_Unknown)
654 GlobLoc = Global ? 'g' : 'l';
655 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
656 ? 'd' : ' ';
657 char FileFunc = ' ';
658 if (Type == SymbolRef::ST_File)
659 FileFunc = 'f';
660 else if (Type == SymbolRef::ST_Function)
661 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000662
Rui Ueyama4e39f712014-03-18 18:58:51 +0000663 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
664 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000665
Rui Ueyama4e39f712014-03-18 18:58:51 +0000666 outs() << format(Fmt, Address) << " "
667 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
668 << (Weak ? 'w' : ' ') // Weak?
669 << ' ' // Constructor. Not supported yet.
670 << ' ' // Warning. Not supported yet.
671 << ' ' // Indirect reference to another symbol.
672 << Debug // Debugging (d) or dynamic (D) symbol.
673 << FileFunc // Name of function (F), file (f) or object (O).
674 << ' ';
675 if (Absolute) {
676 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000677 } else if (Common) {
678 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +0000679 } else if (Section == o->section_end()) {
680 outs() << "*UND*";
681 } else {
682 if (const MachOObjectFile *MachO =
683 dyn_cast<const MachOObjectFile>(o)) {
684 DataRefImpl DR = Section->getRawDataRefImpl();
685 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
686 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000687 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000688 StringRef SectionName;
689 if (error(Section->getName(SectionName)))
690 SectionName = "";
691 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000692 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000693 outs() << '\t'
Davide Italianocd2514d2015-04-30 23:08:53 +0000694 << format("%08" PRIx64 " ", Size);
695 if (Hidden) {
696 outs() << ".hidden ";
697 }
698 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +0000699 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000700 }
701}
702
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000703static void PrintUnwindInfo(const ObjectFile *o) {
704 outs() << "Unwind info:\n\n";
705
706 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
707 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000708 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
709 printMachOUnwindInfo(MachO);
710 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000711 // TODO: Extract DWARF dump tool to objdump.
712 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000713 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000714 return;
715 }
716}
717
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000718void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +0000719 outs() << "Exports trie:\n";
720 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
721 printMachOExportsTrie(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::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +0000730 outs() << "Rebase table:\n";
731 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
732 printMachORebaseTable(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::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000741 outs() << "Bind table:\n";
742 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
743 printMachOBindTable(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::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000752 outs() << "Lazy bind table:\n";
753 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
754 printMachOLazyBindTable(MachO);
755 else {
756 errs() << "This operation is only currently supported "
757 "for Mach-O executable files.\n";
758 return;
759 }
760}
761
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000762void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000763 outs() << "Weak bind table:\n";
764 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
765 printMachOWeakBindTable(MachO);
766 else {
767 errs() << "This operation is only currently supported "
768 "for Mach-O executable files.\n";
769 return;
770 }
771}
Nick Kledzikac431442014-09-12 21:34:15 +0000772
Rui Ueyamac2bed422013-09-27 21:04:00 +0000773static void printPrivateFileHeader(const ObjectFile *o) {
774 if (o->isELF()) {
775 printELFFileHeader(o);
776 } else if (o->isCOFF()) {
777 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000778 } else if (o->isMachO()) {
779 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000780 }
781}
782
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000783static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000784 outs() << '\n';
785 outs() << o->getFileName()
786 << ":\tfile format " << o->getFileFormatName() << "\n\n";
787
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000788 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000789 DisassembleObject(o, Relocations);
790 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000791 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000792 if (SectionHeaders)
793 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000794 if (SectionContents)
795 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000796 if (SymbolTable)
797 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000798 if (UnwindInfo)
799 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000800 if (PrivateHeaders)
801 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000802 if (ExportsTrie)
803 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000804 if (Rebase)
805 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000806 if (Bind)
807 printBindTable(o);
808 if (LazyBind)
809 printLazyBindTable(o);
810 if (WeakBind)
811 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000812}
813
814/// @brief Dump each object file in \a a;
815static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000816 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
817 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000818 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
819 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000820 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000821 if (EC != object_error::invalid_file_type)
822 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000823 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000824 continue;
825 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000826 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000827 DumpObject(o);
828 else
829 errs() << ToolName << ": '" << a->getFileName() << "': "
830 << "Unrecognized file type.\n";
831 }
832}
833
834/// @brief Open file and figure out how to dump it.
835static void DumpInput(StringRef file) {
836 // If file isn't stdin, check that it exists.
837 if (file != "-" && !sys::fs::exists(file)) {
838 errs() << ToolName << ": '" << file << "': " << "No such file\n";
839 return;
840 }
841
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000842 // If we are using the Mach-O specific object file parser, then let it parse
843 // the file and process the command line options. So the -arch flags can
844 // be used to select specific slices, etc.
845 if (MachOOpt) {
846 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000847 return;
848 }
849
850 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000851 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000852 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000853 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000854 return;
855 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000856 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000857
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000858 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000859 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000860 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000861 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000862 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000863 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000864}
865
Michael J. Spencer2670c252011-01-20 06:39:06 +0000866int main(int argc, char **argv) {
867 // Print a stack trace if we signal out.
868 sys::PrintStackTraceOnErrorSignal();
869 PrettyStackTraceProgram X(argc, argv);
870 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
871
872 // Initialize targets and assembly printers/parsers.
873 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000874 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000875 llvm::InitializeAllAsmParsers();
876 llvm::InitializeAllDisassemblers();
877
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000878 // Register the target printer for --version.
879 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
880
Michael J. Spencer2670c252011-01-20 06:39:06 +0000881 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
882 TripleName = Triple::normalize(TripleName);
883
884 ToolName = argv[0];
885
886 // Defaults to a.out if no filenames specified.
887 if (InputFilenames.size() == 0)
888 InputFilenames.push_back("a.out");
889
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000890 if (!Disassemble
891 && !Relocations
892 && !SectionHeaders
893 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000894 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000895 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000896 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000897 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000898 && !Rebase
899 && !Bind
900 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +0000901 && !WeakBind
Kevin Enderby13023a12015-01-15 23:19:11 +0000902 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000903 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000904 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +0000905 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000906 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +0000907 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000908 && !(DylibsUsed && MachOOpt)
909 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +0000910 && !(ObjcMetaData && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000911 && !(DumpSections.size() != 0 && MachOOpt)) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000912 cl::PrintHelpMessage();
913 return 2;
914 }
915
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000916 std::for_each(InputFilenames.begin(), InputFilenames.end(),
917 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000918
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000919 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000920}