blob: 365d2ff5e37ea85de5a6d847634540e66c816b76 [file] [log] [blame]
Michael J. Spencer2670c252011-01-20 06:39:06 +00001//===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program is a utility that works like binutils "objdump", that is, it
11// dumps out a plethora of information about an object file depending on the
12// flags.
13//
Michael J. Spencerd7e70032013-02-05 20:27:22 +000014// The flags and output of this program should be near identical to those of
15// binutils objdump.
16//
Michael J. Spencer2670c252011-01-20 06:39:06 +000017//===----------------------------------------------------------------------===//
18
Benjamin Kramer43a772e2011-09-19 17:56:04 +000019#include "llvm-objdump.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000020#include "llvm/ADT/STLExtras.h"
Michael J. Spencer4e25c022011-10-17 17:13:22 +000021#include "llvm/ADT/StringExtras.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000022#include "llvm/ADT/Triple.h"
23#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000024#include "llvm/MC/MCContext.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000025#include "llvm/MC/MCDisassembler.h"
26#include "llvm/MC/MCInst.h"
27#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000028#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper54bfde72012-04-02 06:09:36 +000029#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000030#include "llvm/MC/MCObjectFileInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000031#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000032#include "llvm/MC/MCRelocationInfo.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000033#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Object/Archive.h"
35#include "llvm/Object/COFF.h"
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000036#include "llvm/Object/MachO.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Object/ObjectFile.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000038#include "llvm/Support/Casting.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000039#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/Debug.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000041#include "llvm/Support/FileSystem.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000042#include "llvm/Support/Format.h"
Benjamin Kramerbf115312011-07-25 23:04:36 +000043#include "llvm/Support/GraphWriter.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000044#include "llvm/Support/Host.h"
45#include "llvm/Support/ManagedStatic.h"
46#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000047#include "llvm/Support/PrettyStackTrace.h"
48#include "llvm/Support/Signals.h"
49#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000050#include "llvm/Support/TargetRegistry.h"
51#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000052#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000053#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000054#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000055#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000056#include <system_error>
Ahmed Bougacha17926472013-08-21 07:29:02 +000057
Michael J. Spencer2670c252011-01-20 06:39:06 +000058using namespace llvm;
59using namespace object;
60
Benjamin Kramer43a772e2011-09-19 17:56:04 +000061static cl::list<std::string>
62InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000063
Kevin Enderbye2297dd2015-01-07 21:02:18 +000064cl::opt<bool>
65llvm::Disassemble("disassemble",
Benjamin Kramer43a772e2011-09-19 17:56:04 +000066 cl::desc("Display assembler mnemonics for the machine instructions"));
67static cl::alias
68Disassembled("d", cl::desc("Alias for --disassemble"),
69 cl::aliasopt(Disassemble));
Michael J. Spencer2670c252011-01-20 06:39:06 +000070
Kevin Enderby98da6132015-01-20 21:47:46 +000071cl::opt<bool>
72llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
Michael J. Spencerba4a3622011-10-08 00:18:30 +000073
Kevin Enderby98da6132015-01-20 21:47:46 +000074cl::opt<bool>
75llvm::SectionContents("s", cl::desc("Display the content of each section"));
Michael J. Spencer4e25c022011-10-17 17:13:22 +000076
Kevin Enderby98da6132015-01-20 21:47:46 +000077cl::opt<bool>
78llvm::SymbolTable("t", cl::desc("Display the symbol table"));
Michael J. Spencerbfa06782011-10-18 19:32:17 +000079
Kevin Enderbye2297dd2015-01-07 21:02:18 +000080cl::opt<bool>
81llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
Nick Kledzikd04bc352014-08-30 00:20:14 +000082
Kevin Enderbye2297dd2015-01-07 21:02:18 +000083cl::opt<bool>
84llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
Nick Kledzikac431442014-09-12 21:34:15 +000085
Kevin Enderbye2297dd2015-01-07 21:02:18 +000086cl::opt<bool>
87llvm::Bind("bind", cl::desc("Display mach-o binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000088
Kevin Enderbye2297dd2015-01-07 21:02:18 +000089cl::opt<bool>
90llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000091
Kevin Enderbye2297dd2015-01-07 21:02:18 +000092cl::opt<bool>
93llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +000094
95static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000096MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +000097static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000098MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +000099
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000100cl::opt<std::string>
101llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
102 "see -version for available targets"));
103
104cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000105llvm::MCPU("mcpu",
106 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
107 cl::value_desc("cpu-name"),
108 cl::init(""));
109
110cl::opt<std::string>
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000111llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000112 "see -version for available targets"));
113
Kevin Enderby98da6132015-01-20 21:47:46 +0000114cl::opt<bool>
115llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
116 "headers for each section."));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000117static cl::alias
118SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
119 cl::aliasopt(SectionHeaders));
120static cl::alias
121SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
122 cl::aliasopt(SectionHeaders));
123
Kevin Enderbyc9595622014-08-06 23:24:41 +0000124cl::list<std::string>
125llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000126 cl::CommaSeparated,
127 cl::desc("Target specific attributes"),
128 cl::value_desc("a1,+a2,-a3,..."));
129
Kevin Enderbybf246f52014-09-24 23:08:22 +0000130cl::opt<bool>
131llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
132 "instructions, do not print "
133 "the instruction bytes."));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000134
Kevin Enderby98da6132015-01-20 21:47:46 +0000135cl::opt<bool>
136llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000137
138static cl::alias
139UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
140 cl::aliasopt(UnwindInfo));
141
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000142cl::opt<bool>
143llvm::PrivateHeaders("private-headers",
144 cl::desc("Display format specific file headers"));
Michael J. Spencer209565db2013-01-06 03:56:49 +0000145
146static cl::alias
147PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
148 cl::aliasopt(PrivateHeaders));
149
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000150static StringRef ToolName;
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000151static int ReturnValue = EXIT_SUCCESS;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000152
Rafael Espindola4453e42942014-06-13 03:07:50 +0000153bool llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000154 if (!EC)
155 return false;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000156
Mark Seaborneb03ac52014-01-25 00:32:01 +0000157 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000158 outs().flush();
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000159 ReturnValue = EXIT_FAILURE;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000160 return true;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000161}
162
Craig Toppere6cb63e2014-04-25 04:24:47 +0000163static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000164 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000165 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000166 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000167 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000168 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000169 // TheTriple defaults to ELF, and COFF doesn't have an environment:
170 // the best we can do here is indicate that it is mach-o.
171 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000172 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000173
174 if (Obj->isCOFF()) {
175 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
176 if (COFFObj->getArch() == Triple::thumb)
177 TheTriple.setTriple("thumbv7-windows");
178 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000179 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000180 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000181 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000182
183 // Get the target specific parser.
184 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000185 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
186 Error);
187 if (!TheTarget) {
188 errs() << ToolName << ": " << Error;
Craig Toppere6cb63e2014-04-25 04:24:47 +0000189 return nullptr;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000190 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000191
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000192 // Update the triple name and return the found target.
193 TripleName = TheTriple.getTriple();
194 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000195}
196
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000197void llvm::DumpBytes(StringRef bytes) {
198 static const char hex_rep[] = "0123456789abcdef";
Colin LeMahieu916c3b42015-03-18 18:41:23 +0000199 SmallString<64> output;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000200
Colin LeMahieu916c3b42015-03-18 18:41:23 +0000201 for (char i: bytes) {
202 output.push_back(hex_rep[(i & 0xF0) >> 4]);
203 output.push_back(hex_rep[i & 0xF]);
204 output.push_back(' ');
Michael J. Spencer2670c252011-01-20 06:39:06 +0000205 }
206
Colin LeMahieu916c3b42015-03-18 18:41:23 +0000207 outs() << output.c_str();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000208}
209
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000210bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Michael J. Spencer51862b32011-10-13 22:17:18 +0000211 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000212 if (error(a.getOffset(a_addr))) return false;
213 if (error(b.getOffset(b_addr))) return false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000214 return a_addr < b_addr;
215}
216
217static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000218 const Target *TheTarget = getTarget(Obj);
219 // getTarget() will have already issued a diagnostic if necessary, so
220 // just bail here if it failed.
221 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000222 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000223
Jack Carter551efd72012-08-28 19:24:49 +0000224 // Package up features to be passed to target/subtarget
225 std::string FeaturesStr;
226 if (MAttrs.size()) {
227 SubtargetFeatures Features;
228 for (unsigned i = 0; i != MAttrs.size(); ++i)
229 Features.AddFeature(MAttrs[i]);
230 FeaturesStr = Features.getString();
231 }
232
Ahmed Charles56440fd2014-03-06 05:51:42 +0000233 std::unique_ptr<const MCRegisterInfo> MRI(
234 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000235 if (!MRI) {
236 errs() << "error: no register info for target " << TripleName << "\n";
237 return;
238 }
239
240 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000241 std::unique_ptr<const MCAsmInfo> AsmInfo(
242 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000243 if (!AsmInfo) {
244 errs() << "error: no assembly info for target " << TripleName << "\n";
245 return;
246 }
247
Ahmed Charles56440fd2014-03-06 05:51:42 +0000248 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000249 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000250 if (!STI) {
251 errs() << "error: no subtarget info for target " << TripleName << "\n";
252 return;
253 }
254
Ahmed Charles56440fd2014-03-06 05:51:42 +0000255 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000256 if (!MII) {
257 errs() << "error: no instruction info for target " << TripleName << "\n";
258 return;
259 }
260
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000261 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
262 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
263
264 std::unique_ptr<MCDisassembler> DisAsm(
265 TheTarget->createMCDisassembler(*STI, Ctx));
266
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000267 if (!DisAsm) {
268 errs() << "error: no disassembler for target " << TripleName << "\n";
269 return;
270 }
271
Ahmed Charles56440fd2014-03-06 05:51:42 +0000272 std::unique_ptr<const MCInstrAnalysis> MIA(
273 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000274
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000275 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000276 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000277 AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
278 if (!IP) {
279 errs() << "error: no instruction printer for target " << TripleName
280 << '\n';
281 return;
282 }
283
Greg Fitzgerald18432272014-03-20 22:55:15 +0000284 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
285 "\t\t\t%08" PRIx64 ": ";
286
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000287 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
288 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000289 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000290 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
291 for (const SectionRef &Section : Obj->sections()) {
292 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000293 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000294 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000295 }
296
Alexey Samsonov48803e52014-03-13 14:37:36 +0000297 for (const SectionRef &Section : Obj->sections()) {
David Majnemer236b0ca2014-11-17 11:17:17 +0000298 if (!Section.isText() || Section.isVirtual())
Mark Seaborneb03ac52014-01-25 00:32:01 +0000299 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000300
Rafael Espindola80291272014-10-08 15:28:58 +0000301 uint64_t SectionAddr = Section.getAddress();
302 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000303 if (!SectSize)
304 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000305
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000306 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000307 std::vector<std::pair<uint64_t, StringRef>> Symbols;
308 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000309 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000310 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000311 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000312 break;
313 if (Address == UnknownAddressOrSize)
314 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000315 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000316 if (Address >= SectSize)
317 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000318
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000319 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000320 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000321 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000322 Symbols.push_back(std::make_pair(Address, Name));
323 }
324 }
325
326 // Sort the symbols by address, just in case they didn't come in that way.
327 array_pod_sort(Symbols.begin(), Symbols.end());
328
Michael J. Spencer51862b32011-10-13 22:17:18 +0000329 // Make a list of all the relocations for this section.
330 std::vector<RelocationRef> Rels;
331 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000332 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
333 for (const RelocationRef &Reloc : RelocSec.relocations()) {
334 Rels.push_back(Reloc);
335 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000336 }
337 }
338
339 // Sort relocations by address.
340 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
341
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000342 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000343 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000344 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000345 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000346 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000347 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000348 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000349 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000350 outs() << "Disassembly of section ";
351 if (!SegmentName.empty())
352 outs() << SegmentName << ",";
353 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000354
355 // If the section has no symbols just insert a dummy one and disassemble
356 // the whole section.
357 if (Symbols.empty())
358 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000359
Alp Tokere69170a2014-06-26 22:52:05 +0000360
361 SmallString<40> Comments;
362 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000363
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000364 StringRef BytesStr;
365 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000366 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000367 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
368 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000369
Michael J. Spencer2670c252011-01-20 06:39:06 +0000370 uint64_t Size;
371 uint64_t Index;
372
Michael J. Spencer51862b32011-10-13 22:17:18 +0000373 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
374 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000375 // Disassemble symbol by symbol.
376 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000377
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000378 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000379 // The end is either the section end or the beginning of the next symbol.
380 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
381 // If this symbol has the same address as the next symbol, then skip it.
382 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000383 continue;
384
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000385 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000386
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000387#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000388 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000389#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000390 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000391#endif
392
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000393 for (Index = Start; Index < End; Index += Size) {
394 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000395
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000396 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
397 SectionAddr + Index, DebugOut,
398 CommentStream)) {
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000399 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
400 if (!NoShowRawInsn) {
401 outs() << "\t";
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000402 DumpBytes(StringRef(
403 reinterpret_cast<const char *>(Bytes.data()) + Index, Size));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000404 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000405 IP->printInst(&Inst, outs(), "");
Alp Tokere69170a2014-06-26 22:52:05 +0000406 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000407 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000408 outs() << "\n";
409 } else {
410 errs() << ToolName << ": warning: invalid instruction encoding\n";
411 if (Size == 0)
412 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000413 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000414
415 // Print relocation for instruction.
416 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000417 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000418 uint64_t addr;
419 SmallString<16> name;
420 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000421
422 // If this relocation is hidden, skip it.
423 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
424 if (hidden) goto skip_print_rel;
425
Rafael Espindola1e483872013-04-25 12:28:45 +0000426 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000427 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000428 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000429 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
430 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
431
Greg Fitzgerald18432272014-03-20 22:55:15 +0000432 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000433 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000434
435 skip_print_rel:
436 ++rel_cur;
437 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000438 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000439 }
440 }
441}
442
Kevin Enderby98da6132015-01-20 21:47:46 +0000443void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000444 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
445 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000446 // Regular objdump doesn't print relocations in non-relocatable object
447 // files.
448 if (!Obj->isRelocatableObject())
449 return;
450
Alexey Samsonov48803e52014-03-13 14:37:36 +0000451 for (const SectionRef &Section : Obj->sections()) {
452 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000453 continue;
454 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000455 if (error(Section.getName(secname)))
456 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000457 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000458 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000459 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000460 uint64_t address;
461 SmallString<32> relocname;
462 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000463 if (error(Reloc.getHidden(hidden)))
464 continue;
465 if (hidden)
466 continue;
467 if (error(Reloc.getTypeName(relocname)))
468 continue;
469 if (error(Reloc.getOffset(address)))
470 continue;
471 if (error(Reloc.getValueString(valuestr)))
472 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000473 outs() << format(Fmt.data(), address) << " " << relocname << " "
474 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000475 }
476 outs() << "\n";
477 }
478}
479
Kevin Enderby98da6132015-01-20 21:47:46 +0000480void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000481 outs() << "Sections:\n"
482 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000483 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000484 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000485 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000486 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000487 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000488 uint64_t Address = Section.getAddress();
489 uint64_t Size = Section.getSize();
490 bool Text = Section.isText();
491 bool Data = Section.isData();
492 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000493 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000494 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000495 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
496 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000497 ++i;
498 }
499}
500
Kevin Enderby98da6132015-01-20 21:47:46 +0000501void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000502 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000503 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000504 StringRef Name;
505 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000506 if (error(Section.getName(Name)))
507 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000508 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000509 uint64_t Size = Section.getSize();
510 if (!Size)
511 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000512
513 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000514 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000515 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000516 ", %04" PRIx64 ")>\n",
517 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000518 continue;
519 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000520
David Majnemer8f6b04c2014-07-14 16:20:14 +0000521 if (error(Section.getContents(Contents)))
522 continue;
523
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000524 // Dump out the content as hex and printable ascii characters.
525 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000526 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000527 // Dump line of hex.
528 for (std::size_t i = 0; i < 16; ++i) {
529 if (i != 0 && i % 4 == 0)
530 outs() << ' ';
531 if (addr + i < end)
532 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
533 << hexdigit(Contents[addr + i] & 0xF, true);
534 else
535 outs() << " ";
536 }
537 // Print ascii.
538 outs() << " ";
539 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000540 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000541 outs() << Contents[addr + i];
542 else
543 outs() << ".";
544 }
545 outs() << "\n";
546 }
547 }
548}
549
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000550static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000551 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
552 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000553 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000554 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000555 return;
556
David Majnemer44f51e52014-09-10 12:51:52 +0000557 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000558 return;
559
560 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000561 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000562 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000563 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
564 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
565 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
566 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000567 << Name << "\n";
568
David Majnemer44f51e52014-09-10 12:51:52 +0000569 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000570 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000571 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000572 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000573 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000574
David Majnemer4d571592014-09-15 19:42:42 +0000575 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
576
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000577 outs() << "AUX "
578 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
579 , unsigned(asd->Length)
580 , unsigned(asd->NumberOfRelocations)
581 , unsigned(asd->NumberOfLinenumbers)
582 , unsigned(asd->CheckSum))
583 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000584 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000585 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000586 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000587 const char *FileName;
588 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000589 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000590
David Majnemer44f51e52014-09-10 12:51:52 +0000591 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
592 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000593 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000594
David Majnemer44f51e52014-09-10 12:51:52 +0000595 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000596 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000597 } else {
598 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000599 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000600 }
601 }
602}
603
Kevin Enderby98da6132015-01-20 21:47:46 +0000604void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000605 outs() << "SYMBOL TABLE:\n";
606
Rui Ueyama4e39f712014-03-18 18:58:51 +0000607 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000608 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000609 return;
610 }
611 for (const SymbolRef &Symbol : o->symbols()) {
612 StringRef Name;
613 uint64_t Address;
614 SymbolRef::Type Type;
615 uint64_t Size;
616 uint32_t Flags = Symbol.getFlags();
617 section_iterator Section = o->section_end();
618 if (error(Symbol.getName(Name)))
619 continue;
620 if (error(Symbol.getAddress(Address)))
621 continue;
622 if (error(Symbol.getType(Type)))
623 continue;
624 if (error(Symbol.getSize(Size)))
625 continue;
626 if (error(Symbol.getSection(Section)))
627 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000628
Rui Ueyama4e39f712014-03-18 18:58:51 +0000629 bool Global = Flags & SymbolRef::SF_Global;
630 bool Weak = Flags & SymbolRef::SF_Weak;
631 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000632 bool Common = Flags & SymbolRef::SF_Common;
David Meyer1df4b842012-02-28 23:47:53 +0000633
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000634 if (Common) {
635 uint32_t Alignment;
636 if (error(Symbol.getAlignment(Alignment)))
637 Alignment = 0;
638 Address = Size;
639 Size = Alignment;
640 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000641 if (Address == UnknownAddressOrSize)
642 Address = 0;
643 if (Size == UnknownAddressOrSize)
644 Size = 0;
645 char GlobLoc = ' ';
646 if (Type != SymbolRef::ST_Unknown)
647 GlobLoc = Global ? 'g' : 'l';
648 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
649 ? 'd' : ' ';
650 char FileFunc = ' ';
651 if (Type == SymbolRef::ST_File)
652 FileFunc = 'f';
653 else if (Type == SymbolRef::ST_Function)
654 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000655
Rui Ueyama4e39f712014-03-18 18:58:51 +0000656 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
657 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000658
Rui Ueyama4e39f712014-03-18 18:58:51 +0000659 outs() << format(Fmt, Address) << " "
660 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
661 << (Weak ? 'w' : ' ') // Weak?
662 << ' ' // Constructor. Not supported yet.
663 << ' ' // Warning. Not supported yet.
664 << ' ' // Indirect reference to another symbol.
665 << Debug // Debugging (d) or dynamic (D) symbol.
666 << FileFunc // Name of function (F), file (f) or object (O).
667 << ' ';
668 if (Absolute) {
669 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000670 } else if (Common) {
671 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +0000672 } else if (Section == o->section_end()) {
673 outs() << "*UND*";
674 } else {
675 if (const MachOObjectFile *MachO =
676 dyn_cast<const MachOObjectFile>(o)) {
677 DataRefImpl DR = Section->getRawDataRefImpl();
678 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
679 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000680 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000681 StringRef SectionName;
682 if (error(Section->getName(SectionName)))
683 SectionName = "";
684 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000685 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000686 outs() << '\t'
687 << format("%08" PRIx64 " ", Size)
688 << Name
689 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000690 }
691}
692
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000693static void PrintUnwindInfo(const ObjectFile *o) {
694 outs() << "Unwind info:\n\n";
695
696 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
697 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000698 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
699 printMachOUnwindInfo(MachO);
700 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000701 // TODO: Extract DWARF dump tool to objdump.
702 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000703 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000704 return;
705 }
706}
707
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000708void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +0000709 outs() << "Exports trie:\n";
710 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
711 printMachOExportsTrie(MachO);
712 else {
713 errs() << "This operation is only currently supported "
714 "for Mach-O executable files.\n";
715 return;
716 }
717}
718
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000719void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +0000720 outs() << "Rebase table:\n";
721 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
722 printMachORebaseTable(MachO);
723 else {
724 errs() << "This operation is only currently supported "
725 "for Mach-O executable files.\n";
726 return;
727 }
728}
729
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000730void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000731 outs() << "Bind table:\n";
732 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
733 printMachOBindTable(MachO);
734 else {
735 errs() << "This operation is only currently supported "
736 "for Mach-O executable files.\n";
737 return;
738 }
739}
740
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000741void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000742 outs() << "Lazy bind table:\n";
743 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
744 printMachOLazyBindTable(MachO);
745 else {
746 errs() << "This operation is only currently supported "
747 "for Mach-O executable files.\n";
748 return;
749 }
750}
751
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000752void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000753 outs() << "Weak bind table:\n";
754 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
755 printMachOWeakBindTable(MachO);
756 else {
757 errs() << "This operation is only currently supported "
758 "for Mach-O executable files.\n";
759 return;
760 }
761}
Nick Kledzikac431442014-09-12 21:34:15 +0000762
Rui Ueyamac2bed422013-09-27 21:04:00 +0000763static void printPrivateFileHeader(const ObjectFile *o) {
764 if (o->isELF()) {
765 printELFFileHeader(o);
766 } else if (o->isCOFF()) {
767 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000768 } else if (o->isMachO()) {
769 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000770 }
771}
772
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000773static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000774 outs() << '\n';
775 outs() << o->getFileName()
776 << ":\tfile format " << o->getFileFormatName() << "\n\n";
777
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000778 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000779 DisassembleObject(o, Relocations);
780 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000781 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000782 if (SectionHeaders)
783 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000784 if (SectionContents)
785 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000786 if (SymbolTable)
787 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000788 if (UnwindInfo)
789 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000790 if (PrivateHeaders)
791 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000792 if (ExportsTrie)
793 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000794 if (Rebase)
795 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000796 if (Bind)
797 printBindTable(o);
798 if (LazyBind)
799 printLazyBindTable(o);
800 if (WeakBind)
801 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000802}
803
804/// @brief Dump each object file in \a a;
805static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000806 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
807 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000808 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
809 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000810 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000811 if (EC != object_error::invalid_file_type)
812 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000813 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000814 continue;
815 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000816 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000817 DumpObject(o);
818 else
819 errs() << ToolName << ": '" << a->getFileName() << "': "
820 << "Unrecognized file type.\n";
821 }
822}
823
824/// @brief Open file and figure out how to dump it.
825static void DumpInput(StringRef file) {
826 // If file isn't stdin, check that it exists.
827 if (file != "-" && !sys::fs::exists(file)) {
828 errs() << ToolName << ": '" << file << "': " << "No such file\n";
829 return;
830 }
831
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000832 // If we are using the Mach-O specific object file parser, then let it parse
833 // the file and process the command line options. So the -arch flags can
834 // be used to select specific slices, etc.
835 if (MachOOpt) {
836 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000837 return;
838 }
839
840 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000841 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000842 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000843 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000844 return;
845 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000846 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000847
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000848 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000849 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000850 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000851 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000852 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000853 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000854}
855
Michael J. Spencer2670c252011-01-20 06:39:06 +0000856int main(int argc, char **argv) {
857 // Print a stack trace if we signal out.
858 sys::PrintStackTraceOnErrorSignal();
859 PrettyStackTraceProgram X(argc, argv);
860 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
861
862 // Initialize targets and assembly printers/parsers.
863 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000864 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000865 llvm::InitializeAllAsmParsers();
866 llvm::InitializeAllDisassemblers();
867
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000868 // Register the target printer for --version.
869 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
870
Michael J. Spencer2670c252011-01-20 06:39:06 +0000871 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
872 TripleName = Triple::normalize(TripleName);
873
874 ToolName = argv[0];
875
876 // Defaults to a.out if no filenames specified.
877 if (InputFilenames.size() == 0)
878 InputFilenames.push_back("a.out");
879
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000880 if (!Disassemble
881 && !Relocations
882 && !SectionHeaders
883 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000884 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000885 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000886 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000887 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000888 && !Rebase
889 && !Bind
890 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +0000891 && !WeakBind
Kevin Enderby13023a12015-01-15 23:19:11 +0000892 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000893 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000894 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +0000895 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000896 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +0000897 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000898 && !(DylibsUsed && MachOOpt)
899 && !(DylibId && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000900 && !(DumpSections.size() != 0 && MachOOpt)) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000901 cl::PrintHelpMessage();
902 return 2;
903 }
904
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000905 std::for_each(InputFilenames.begin(), InputFilenames.end(),
906 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000907
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000908 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000909}