blob: 5a8b56e5aa27871d2506bce904b458ef741373dc [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 LeMahieu68d967d2015-05-29 14:44:13 +0000208 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
Colin LeMahieufb76b002015-05-28 19:07:14 +0000209 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;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000221class HexagonPrettyPrinter : public PrettyPrinter {
222public:
223 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
224 raw_ostream &OS) {
225 uint32_t opcode =
226 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
227 OS << format("%8" PRIx64 ":", Address);
228 if (!NoShowRawInsn) {
229 OS << "\t";
230 dumpBytes(Bytes.slice(0, 4), OS);
231 OS << format("%08" PRIx32, opcode);
232 }
233 }
234 void printInst(MCInstPrinter &IP, const MCInst *MI,
235 ArrayRef<uint8_t> Bytes, uint64_t Address,
236 raw_ostream &OS, StringRef Annot,
237 MCSubtargetInfo const &STI) override {
238 std::string Buffer;
239 {
240 raw_string_ostream TempStream(Buffer);
241 IP.printInst(MI, TempStream, "", STI);
242 }
243 StringRef Contents(Buffer);
244 // Split off bundle attributes
245 auto PacketBundle = Contents.rsplit('\n');
246 // Split off first instruction from the rest
247 auto HeadTail = PacketBundle.first.split('\n');
248 auto Preamble = " { ";
249 auto Separator = "";
250 while(!HeadTail.first.empty()) {
251 OS << Separator;
252 Separator = "\n";
253 printLead(Bytes, Address, OS);
254 OS << Preamble;
255 Preamble = " ";
256 StringRef Inst;
257 auto Duplex = HeadTail.first.split('\v');
258 if(!Duplex.second.empty()){
259 OS << Duplex.first;
260 OS << "; ";
261 Inst = Duplex.second;
262 }
263 else
264 Inst = HeadTail.first;
265 OS << Inst;
266 Bytes = Bytes.slice(4);
267 Address += 4;
268 HeadTail = HeadTail.second.split('\n');
269 }
270 OS << " } " << PacketBundle.second;
271 }
272};
273HexagonPrettyPrinter HexagonPrettyPrinterInst;
Colin LeMahieu35436a22015-05-29 14:48:25 +0000274PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000275 switch(Triple.getArch()) {
276 default:
277 return PrettyPrinterInst;
278 case Triple::hexagon:
279 return HexagonPrettyPrinterInst;
280 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000281}
282}
283
Michael J. Spencer51862b32011-10-13 22:17:18 +0000284static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000285 const Target *TheTarget = getTarget(Obj);
286 // getTarget() will have already issued a diagnostic if necessary, so
287 // just bail here if it failed.
288 if (!TheTarget)
Michael J. Spencer2670c252011-01-20 06:39:06 +0000289 return;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000290
Jack Carter551efd72012-08-28 19:24:49 +0000291 // Package up features to be passed to target/subtarget
292 std::string FeaturesStr;
293 if (MAttrs.size()) {
294 SubtargetFeatures Features;
295 for (unsigned i = 0; i != MAttrs.size(); ++i)
296 Features.AddFeature(MAttrs[i]);
297 FeaturesStr = Features.getString();
298 }
299
Ahmed Charles56440fd2014-03-06 05:51:42 +0000300 std::unique_ptr<const MCRegisterInfo> MRI(
301 TheTarget->createMCRegInfo(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000302 if (!MRI) {
303 errs() << "error: no register info for target " << TripleName << "\n";
304 return;
305 }
306
307 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000308 std::unique_ptr<const MCAsmInfo> AsmInfo(
309 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000310 if (!AsmInfo) {
311 errs() << "error: no assembly info for target " << TripleName << "\n";
312 return;
313 }
314
Ahmed Charles56440fd2014-03-06 05:51:42 +0000315 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000316 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000317 if (!STI) {
318 errs() << "error: no subtarget info for target " << TripleName << "\n";
319 return;
320 }
321
Ahmed Charles56440fd2014-03-06 05:51:42 +0000322 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000323 if (!MII) {
324 errs() << "error: no instruction info for target " << TripleName << "\n";
325 return;
326 }
327
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000328 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
329 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
330
331 std::unique_ptr<MCDisassembler> DisAsm(
332 TheTarget->createMCDisassembler(*STI, Ctx));
333
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000334 if (!DisAsm) {
335 errs() << "error: no disassembler for target " << TripleName << "\n";
336 return;
337 }
338
Ahmed Charles56440fd2014-03-06 05:51:42 +0000339 std::unique_ptr<const MCInstrAnalysis> MIA(
340 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000341
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000342 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000343 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +0000344 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000345 if (!IP) {
346 errs() << "error: no instruction printer for target " << TripleName
347 << '\n';
348 return;
349 }
Colin LeMahieu35436a22015-05-29 14:48:25 +0000350 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000351
Greg Fitzgerald18432272014-03-20 22:55:15 +0000352 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
353 "\t\t\t%08" PRIx64 ": ";
354
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000355 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
356 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000357 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000358 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
359 for (const SectionRef &Section : Obj->sections()) {
360 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000361 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000362 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000363 }
364
Alexey Samsonov48803e52014-03-13 14:37:36 +0000365 for (const SectionRef &Section : Obj->sections()) {
David Majnemer236b0ca2014-11-17 11:17:17 +0000366 if (!Section.isText() || Section.isVirtual())
Mark Seaborneb03ac52014-01-25 00:32:01 +0000367 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000368
Rafael Espindola80291272014-10-08 15:28:58 +0000369 uint64_t SectionAddr = Section.getAddress();
370 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000371 if (!SectSize)
372 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000373
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000374 // Make a list of all the symbols in this section.
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000375 std::vector<std::pair<uint64_t, StringRef>> Symbols;
376 for (const SymbolRef &Symbol : Obj->symbols()) {
Rafael Espindola80291272014-10-08 15:28:58 +0000377 if (Section.containsSymbol(Symbol)) {
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000378 uint64_t Address;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000379 if (error(Symbol.getAddress(Address)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000380 break;
381 if (Address == UnknownAddressOrSize)
382 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000383 Address -= SectionAddr;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000384 if (Address >= SectSize)
385 continue;
Cameron Zwarich07f0f772012-02-03 04:13:37 +0000386
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000387 StringRef Name;
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000388 if (error(Symbol.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000389 break;
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000390 Symbols.push_back(std::make_pair(Address, Name));
391 }
392 }
393
394 // Sort the symbols by address, just in case they didn't come in that way.
395 array_pod_sort(Symbols.begin(), Symbols.end());
396
Michael J. Spencer51862b32011-10-13 22:17:18 +0000397 // Make a list of all the relocations for this section.
398 std::vector<RelocationRef> Rels;
399 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000400 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
401 for (const RelocationRef &Reloc : RelocSec.relocations()) {
402 Rels.push_back(Reloc);
403 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000404 }
405 }
406
407 // Sort relocations by address.
408 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
409
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000410 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000411 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000412 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +0000413 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000414 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000415 StringRef name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000416 if (error(Section.getName(name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000417 break;
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000418 outs() << "Disassembly of section ";
419 if (!SegmentName.empty())
420 outs() << SegmentName << ",";
421 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000422
423 // If the section has no symbols just insert a dummy one and disassemble
424 // the whole section.
425 if (Symbols.empty())
426 Symbols.push_back(std::make_pair(0, name));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000427
Alp Tokere69170a2014-06-26 22:52:05 +0000428
429 SmallString<40> Comments;
430 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000431
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000432 StringRef BytesStr;
433 if (error(Section.getContents(BytesStr)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000434 break;
Aaron Ballman106fd7b2014-11-12 14:01:17 +0000435 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
436 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000437
Michael J. Spencer2670c252011-01-20 06:39:06 +0000438 uint64_t Size;
439 uint64_t Index;
440
Michael J. Spencer51862b32011-10-13 22:17:18 +0000441 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
442 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000443 // Disassemble symbol by symbol.
444 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +0000445
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000446 uint64_t Start = Symbols[si].first;
Rafael Espindolae45c7402014-08-17 16:31:39 +0000447 // The end is either the section end or the beginning of the next symbol.
448 uint64_t End = (si == se - 1) ? SectSize : Symbols[si + 1].first;
449 // If this symbol has the same address as the next symbol, then skip it.
450 if (Start == End)
Michael J. Spenceree84f642011-10-13 20:37:08 +0000451 continue;
452
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000453 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +0000454
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000455#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +0000456 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000457#else
Mark Seaborneb03ac52014-01-25 00:32:01 +0000458 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000459#endif
460
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000461 for (Index = Start; Index < End; Index += Size) {
462 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +0000463
Rafael Espindola7fc5b872014-11-12 02:04:27 +0000464 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
465 SectionAddr + Index, DebugOut,
466 CommentStream)) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000467 PIP.printInst(*IP, &Inst,
Colin LeMahieufb76b002015-05-28 19:07:14 +0000468 Bytes.slice(Index, Size),
469 SectionAddr + Index, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +0000470 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000471 Comments.clear();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000472 outs() << "\n";
473 } else {
474 errs() << ToolName << ": warning: invalid instruction encoding\n";
475 if (Size == 0)
476 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000477 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000478
479 // Print relocation for instruction.
480 while (rel_cur != rel_end) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000481 bool hidden = false;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000482 uint64_t addr;
483 SmallString<16> name;
484 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +0000485
486 // If this relocation is hidden, skip it.
487 if (error(rel_cur->getHidden(hidden))) goto skip_print_rel;
488 if (hidden) goto skip_print_rel;
489
Rafael Espindola1e483872013-04-25 12:28:45 +0000490 if (error(rel_cur->getOffset(addr))) goto skip_print_rel;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000491 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +0000492 if (addr >= Index + Size) break;
Michael J. Spencer51862b32011-10-13 22:17:18 +0000493 if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
494 if (error(rel_cur->getValueString(val))) goto skip_print_rel;
495
Greg Fitzgerald18432272014-03-20 22:55:15 +0000496 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +0000497 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +0000498
499 skip_print_rel:
500 ++rel_cur;
501 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000502 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000503 }
504 }
505}
506
Kevin Enderby98da6132015-01-20 21:47:46 +0000507void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +0000508 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
509 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +0000510 // Regular objdump doesn't print relocations in non-relocatable object
511 // files.
512 if (!Obj->isRelocatableObject())
513 return;
514
Alexey Samsonov48803e52014-03-13 14:37:36 +0000515 for (const SectionRef &Section : Obj->sections()) {
516 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000517 continue;
518 StringRef secname;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000519 if (error(Section.getName(secname)))
520 continue;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000521 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000522 for (const RelocationRef &Reloc : Section.relocations()) {
Owen Andersonfa3e5202011-10-25 20:35:53 +0000523 bool hidden;
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000524 uint64_t address;
525 SmallString<32> relocname;
526 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000527 if (error(Reloc.getHidden(hidden)))
528 continue;
529 if (hidden)
530 continue;
531 if (error(Reloc.getTypeName(relocname)))
532 continue;
533 if (error(Reloc.getOffset(address)))
534 continue;
535 if (error(Reloc.getValueString(valuestr)))
536 continue;
Greg Fitzgerald18432272014-03-20 22:55:15 +0000537 outs() << format(Fmt.data(), address) << " " << relocname << " "
538 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000539 }
540 outs() << "\n";
541 }
542}
543
Kevin Enderby98da6132015-01-20 21:47:46 +0000544void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000545 outs() << "Sections:\n"
546 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000547 unsigned i = 0;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000548 for (const SectionRef &Section : Obj->sections()) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000549 StringRef Name;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000550 if (error(Section.getName(Name)))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000551 return;
Rafael Espindola80291272014-10-08 15:28:58 +0000552 uint64_t Address = Section.getAddress();
553 uint64_t Size = Section.getSize();
554 bool Text = Section.isText();
555 bool Data = Section.isData();
556 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000557 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +0000558 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +0000559 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
560 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000561 ++i;
562 }
563}
564
Kevin Enderby98da6132015-01-20 21:47:46 +0000565void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +0000566 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000567 for (const SectionRef &Section : Obj->sections()) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000568 StringRef Name;
569 StringRef Contents;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000570 if (error(Section.getName(Name)))
571 continue;
Rafael Espindola80291272014-10-08 15:28:58 +0000572 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +0000573 uint64_t Size = Section.getSize();
574 if (!Size)
575 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000576
577 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +0000578 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +0000579 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +0000580 ", %04" PRIx64 ")>\n",
581 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +0000582 continue;
583 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000584
David Majnemer8f6b04c2014-07-14 16:20:14 +0000585 if (error(Section.getContents(Contents)))
586 continue;
587
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000588 // Dump out the content as hex and printable ascii characters.
589 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +0000590 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000591 // Dump line of hex.
592 for (std::size_t i = 0; i < 16; ++i) {
593 if (i != 0 && i % 4 == 0)
594 outs() << ' ';
595 if (addr + i < end)
596 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
597 << hexdigit(Contents[addr + i] & 0xF, true);
598 else
599 outs() << " ";
600 }
601 // Print ascii.
602 outs() << " ";
603 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +0000604 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000605 outs() << Contents[addr + i];
606 else
607 outs() << ".";
608 }
609 outs() << "\n";
610 }
611 }
612}
613
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000614static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
David Majnemer44f51e52014-09-10 12:51:52 +0000615 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
616 ErrorOr<COFFSymbolRef> Symbol = coff->getSymbol(SI);
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000617 StringRef Name;
David Majnemer44f51e52014-09-10 12:51:52 +0000618 if (error(Symbol.getError()))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000619 return;
620
David Majnemer44f51e52014-09-10 12:51:52 +0000621 if (error(coff->getSymbolName(*Symbol, Name)))
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000622 return;
623
624 outs() << "[" << format("%2d", SI) << "]"
David Majnemer44f51e52014-09-10 12:51:52 +0000625 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000626 << "(fl 0x00)" // Flag bits, which COFF doesn't have.
David Majnemer44f51e52014-09-10 12:51:52 +0000627 << "(ty " << format("%3x", unsigned(Symbol->getType())) << ")"
628 << "(scl " << format("%3x", unsigned(Symbol->getStorageClass())) << ") "
629 << "(nx " << unsigned(Symbol->getNumberOfAuxSymbols()) << ") "
630 << "0x" << format("%08x", unsigned(Symbol->getValue())) << " "
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000631 << Name << "\n";
632
David Majnemer44f51e52014-09-10 12:51:52 +0000633 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000634 if (Symbol->isSectionDefinition()) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000635 const coff_aux_section_definition *asd;
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000636 if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000637 return;
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000638
David Majnemer4d571592014-09-15 19:42:42 +0000639 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
640
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000641 outs() << "AUX "
642 << format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
643 , unsigned(asd->Length)
644 , unsigned(asd->NumberOfRelocations)
645 , unsigned(asd->NumberOfLinenumbers)
646 , unsigned(asd->CheckSum))
647 << format("assoc %d comdat %d\n"
David Majnemer4d571592014-09-15 19:42:42 +0000648 , unsigned(AuxNumber)
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000649 , unsigned(asd->Selection));
Saleem Abdulrasool7050eed2014-04-14 02:37:28 +0000650 } else if (Symbol->isFileRecord()) {
David Majnemer44f51e52014-09-10 12:51:52 +0000651 const char *FileName;
652 if (error(coff->getAuxSymbol<char>(SI + 1, FileName)))
Saleem Abdulrasool63a0dd62014-04-13 22:54:11 +0000653 return;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000654
David Majnemer44f51e52014-09-10 12:51:52 +0000655 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
656 coff->getSymbolTableEntrySize());
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000657 outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000658
David Majnemer44f51e52014-09-10 12:51:52 +0000659 SI = SI + Symbol->getNumberOfAuxSymbols();
Saleem Abdulrasool13a3f692014-04-14 16:38:25 +0000660 break;
Saleem Abdulrasoold38c6b12014-04-14 02:37:23 +0000661 } else {
662 outs() << "AUX Unknown\n";
Saleem Abdulrasool9ede5c72014-04-13 03:11:08 +0000663 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000664 }
665 }
666}
667
Kevin Enderby98da6132015-01-20 21:47:46 +0000668void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000669 outs() << "SYMBOL TABLE:\n";
670
Rui Ueyama4e39f712014-03-18 18:58:51 +0000671 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000672 PrintCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +0000673 return;
674 }
675 for (const SymbolRef &Symbol : o->symbols()) {
676 StringRef Name;
677 uint64_t Address;
678 SymbolRef::Type Type;
679 uint64_t Size;
680 uint32_t Flags = Symbol.getFlags();
681 section_iterator Section = o->section_end();
682 if (error(Symbol.getName(Name)))
683 continue;
684 if (error(Symbol.getAddress(Address)))
685 continue;
686 if (error(Symbol.getType(Type)))
687 continue;
688 if (error(Symbol.getSize(Size)))
689 continue;
690 if (error(Symbol.getSection(Section)))
691 continue;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000692
Rui Ueyama4e39f712014-03-18 18:58:51 +0000693 bool Global = Flags & SymbolRef::SF_Global;
694 bool Weak = Flags & SymbolRef::SF_Weak;
695 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000696 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +0000697 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +0000698
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000699 if (Common) {
700 uint32_t Alignment;
701 if (error(Symbol.getAlignment(Alignment)))
702 Alignment = 0;
703 Address = Size;
704 Size = Alignment;
705 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000706 if (Address == UnknownAddressOrSize)
707 Address = 0;
708 if (Size == UnknownAddressOrSize)
709 Size = 0;
710 char GlobLoc = ' ';
711 if (Type != SymbolRef::ST_Unknown)
712 GlobLoc = Global ? 'g' : 'l';
713 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
714 ? 'd' : ' ';
715 char FileFunc = ' ';
716 if (Type == SymbolRef::ST_File)
717 FileFunc = 'f';
718 else if (Type == SymbolRef::ST_Function)
719 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000720
Rui Ueyama4e39f712014-03-18 18:58:51 +0000721 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
722 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +0000723
Rui Ueyama4e39f712014-03-18 18:58:51 +0000724 outs() << format(Fmt, Address) << " "
725 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
726 << (Weak ? 'w' : ' ') // Weak?
727 << ' ' // Constructor. Not supported yet.
728 << ' ' // Warning. Not supported yet.
729 << ' ' // Indirect reference to another symbol.
730 << Debug // Debugging (d) or dynamic (D) symbol.
731 << FileFunc // Name of function (F), file (f) or object (O).
732 << ' ';
733 if (Absolute) {
734 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +0000735 } else if (Common) {
736 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +0000737 } else if (Section == o->section_end()) {
738 outs() << "*UND*";
739 } else {
740 if (const MachOObjectFile *MachO =
741 dyn_cast<const MachOObjectFile>(o)) {
742 DataRefImpl DR = Section->getRawDataRefImpl();
743 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
744 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000745 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000746 StringRef SectionName;
747 if (error(Section->getName(SectionName)))
748 SectionName = "";
749 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000750 }
Rui Ueyama4e39f712014-03-18 18:58:51 +0000751 outs() << '\t'
Davide Italianocd2514d2015-04-30 23:08:53 +0000752 << format("%08" PRIx64 " ", Size);
753 if (Hidden) {
754 outs() << ".hidden ";
755 }
756 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +0000757 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000758 }
759}
760
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000761static void PrintUnwindInfo(const ObjectFile *o) {
762 outs() << "Unwind info:\n\n";
763
764 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
765 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +0000766 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
767 printMachOUnwindInfo(MachO);
768 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000769 // TODO: Extract DWARF dump tool to objdump.
770 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +0000771 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000772 return;
773 }
774}
775
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000776void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +0000777 outs() << "Exports trie:\n";
778 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
779 printMachOExportsTrie(MachO);
780 else {
781 errs() << "This operation is only currently supported "
782 "for Mach-O executable files.\n";
783 return;
784 }
785}
786
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000787void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +0000788 outs() << "Rebase table:\n";
789 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
790 printMachORebaseTable(MachO);
791 else {
792 errs() << "This operation is only currently supported "
793 "for Mach-O executable files.\n";
794 return;
795 }
796}
797
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000798void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000799 outs() << "Bind table:\n";
800 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
801 printMachOBindTable(MachO);
802 else {
803 errs() << "This operation is only currently supported "
804 "for Mach-O executable files.\n";
805 return;
806 }
807}
808
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000809void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000810 outs() << "Lazy bind table:\n";
811 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
812 printMachOLazyBindTable(MachO);
813 else {
814 errs() << "This operation is only currently supported "
815 "for Mach-O executable files.\n";
816 return;
817 }
818}
819
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000820void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +0000821 outs() << "Weak bind table:\n";
822 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
823 printMachOWeakBindTable(MachO);
824 else {
825 errs() << "This operation is only currently supported "
826 "for Mach-O executable files.\n";
827 return;
828 }
829}
Nick Kledzikac431442014-09-12 21:34:15 +0000830
Rui Ueyamac2bed422013-09-27 21:04:00 +0000831static void printPrivateFileHeader(const ObjectFile *o) {
832 if (o->isELF()) {
833 printELFFileHeader(o);
834 } else if (o->isCOFF()) {
835 printCOFFFileHeader(o);
Kevin Enderbyb76d3862014-08-22 20:35:18 +0000836 } else if (o->isMachO()) {
837 printMachOFileHeader(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000838 }
839}
840
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000841static void DumpObject(const ObjectFile *o) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000842 outs() << '\n';
843 outs() << o->getFileName()
844 << ":\tfile format " << o->getFileFormatName() << "\n\n";
845
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000846 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +0000847 DisassembleObject(o, Relocations);
848 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000849 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000850 if (SectionHeaders)
851 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000852 if (SectionContents)
853 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000854 if (SymbolTable)
855 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000856 if (UnwindInfo)
857 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +0000858 if (PrivateHeaders)
859 printPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +0000860 if (ExportsTrie)
861 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +0000862 if (Rebase)
863 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +0000864 if (Bind)
865 printBindTable(o);
866 if (LazyBind)
867 printLazyBindTable(o);
868 if (WeakBind)
869 printWeakBindTable(o);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000870}
871
872/// @brief Dump each object file in \a a;
873static void DumpArchive(const Archive *a) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000874 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
875 ++i) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000876 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = i->getAsBinary();
877 if (std::error_code EC = ChildOrErr.getError()) {
Michael J. Spencer53723de2011-11-16 01:24:41 +0000878 // Ignore non-object files.
Mark Seaborneb03ac52014-01-25 00:32:01 +0000879 if (EC != object_error::invalid_file_type)
880 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
Michael J. Spencer53723de2011-11-16 01:24:41 +0000881 << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000882 continue;
883 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000884 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000885 DumpObject(o);
886 else
887 errs() << ToolName << ": '" << a->getFileName() << "': "
888 << "Unrecognized file type.\n";
889 }
890}
891
892/// @brief Open file and figure out how to dump it.
893static void DumpInput(StringRef file) {
894 // If file isn't stdin, check that it exists.
895 if (file != "-" && !sys::fs::exists(file)) {
896 errs() << ToolName << ": '" << file << "': " << "No such file\n";
897 return;
898 }
899
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000900 // If we are using the Mach-O specific object file parser, then let it parse
901 // the file and process the command line options. So the -arch flags can
902 // be used to select specific slices, etc.
903 if (MachOOpt) {
904 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000905 return;
906 }
907
908 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000909 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000910 if (std::error_code EC = BinaryOrErr.getError()) {
Rafael Espindola63da2952014-01-15 19:37:43 +0000911 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000912 return;
913 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000914 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000915
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000916 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000917 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000918 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000919 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000920 else
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000921 errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000922}
923
Michael J. Spencer2670c252011-01-20 06:39:06 +0000924int main(int argc, char **argv) {
925 // Print a stack trace if we signal out.
926 sys::PrintStackTraceOnErrorSignal();
927 PrettyStackTraceProgram X(argc, argv);
928 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
929
930 // Initialize targets and assembly printers/parsers.
931 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +0000932 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +0000933 llvm::InitializeAllAsmParsers();
934 llvm::InitializeAllDisassemblers();
935
Pete Cooper28fb4fc2012-05-03 23:20:10 +0000936 // Register the target printer for --version.
937 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
938
Michael J. Spencer2670c252011-01-20 06:39:06 +0000939 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
940 TripleName = Triple::normalize(TripleName);
941
942 ToolName = argv[0];
943
944 // Defaults to a.out if no filenames specified.
945 if (InputFilenames.size() == 0)
946 InputFilenames.push_back("a.out");
947
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000948 if (!Disassemble
949 && !Relocations
950 && !SectionHeaders
951 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000952 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +0000953 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +0000954 && !PrivateHeaders
Nick Kledzikac431442014-09-12 21:34:15 +0000955 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +0000956 && !Rebase
957 && !Bind
958 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +0000959 && !WeakBind
Kevin Enderby13023a12015-01-15 23:19:11 +0000960 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +0000961 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +0000962 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +0000963 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000964 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +0000965 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +0000966 && !(DylibsUsed && MachOOpt)
967 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +0000968 && !(ObjcMetaData && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +0000969 && !(DumpSections.size() != 0 && MachOOpt)) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000970 cl::PrintHelpMessage();
971 return 2;
972 }
973
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000974 std::for_each(InputFilenames.begin(), InputFilenames.end(),
975 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000976
Rui Ueyama98fe58a2014-11-26 22:17:25 +0000977 return ReturnValue;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000978}