blob: 8a61716e346ce399feb7d093570b9f113f914891 [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"
Sanjoy Das6f567a42015-06-22 18:03:02 +000020#include "llvm/ADT/Optional.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000021#include "llvm/ADT/STLExtras.h"
Michael J. Spencer4e25c022011-10-17 17:13:22 +000022#include "llvm/ADT/StringExtras.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000023#include "llvm/ADT/Triple.h"
Sanjoy Das3f1bc3b2015-06-23 20:09:03 +000024#include "llvm/CodeGen/FaultMaps.h"
Igor Laevsky03a670c2016-01-26 15:09:42 +000025#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000026#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000027#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000028#include "llvm/MC/MCDisassembler/MCDisassembler.h"
29#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000030#include "llvm/MC/MCInst.h"
31#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000032#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper54bfde72012-04-02 06:09:36 +000033#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000034#include "llvm/MC/MCObjectFileInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000035#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000036#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Object/Archive.h"
38#include "llvm/Object/COFF.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000039#include "llvm/Object/ELFObjectFile.h"
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000040#include "llvm/Object/MachO.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Object/ObjectFile.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000042#include "llvm/Support/Casting.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000043#include "llvm/Support/CommandLine.h"
44#include "llvm/Support/Debug.h"
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +000045#include "llvm/Support/Errc.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000046#include "llvm/Support/FileSystem.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000047#include "llvm/Support/Format.h"
Benjamin Kramerbf115312011-07-25 23:04:36 +000048#include "llvm/Support/GraphWriter.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000049#include "llvm/Support/Host.h"
50#include "llvm/Support/ManagedStatic.h"
51#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000052#include "llvm/Support/PrettyStackTrace.h"
53#include "llvm/Support/Signals.h"
54#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000055#include "llvm/Support/TargetRegistry.h"
56#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000057#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000058#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000059#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000060#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000061#include <system_error>
Ahmed Bougacha17926472013-08-21 07:29:02 +000062
Michael J. Spencer2670c252011-01-20 06:39:06 +000063using namespace llvm;
64using namespace object;
65
Benjamin Kramer43a772e2011-09-19 17:56:04 +000066static cl::list<std::string>
67InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000068
Kevin Enderbye2297dd2015-01-07 21:02:18 +000069cl::opt<bool>
70llvm::Disassemble("disassemble",
Benjamin Kramer43a772e2011-09-19 17:56:04 +000071 cl::desc("Display assembler mnemonics for the machine instructions"));
72static cl::alias
73Disassembled("d", cl::desc("Alias for --disassemble"),
Colin LeMahieu77804be2015-07-29 15:45:39 +000074 cl::aliasopt(Disassemble));
75
76cl::opt<bool>
77llvm::DisassembleAll("disassemble-all",
78 cl::desc("Display assembler mnemonics for the machine instructions"));
79static cl::alias
80DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
Colin LeMahieuf34933e2015-07-23 20:58:49 +000081 cl::aliasopt(DisassembleAll));
Michael J. Spencer2670c252011-01-20 06:39:06 +000082
Kevin Enderby98da6132015-01-20 21:47:46 +000083cl::opt<bool>
84llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
Michael J. Spencerba4a3622011-10-08 00:18:30 +000085
Kevin Enderby98da6132015-01-20 21:47:46 +000086cl::opt<bool>
87llvm::SectionContents("s", cl::desc("Display the content of each section"));
Michael J. Spencer4e25c022011-10-17 17:13:22 +000088
Kevin Enderby98da6132015-01-20 21:47:46 +000089cl::opt<bool>
90llvm::SymbolTable("t", cl::desc("Display the symbol table"));
Michael J. Spencerbfa06782011-10-18 19:32:17 +000091
Kevin Enderbye2297dd2015-01-07 21:02:18 +000092cl::opt<bool>
93llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
Nick Kledzikd04bc352014-08-30 00:20:14 +000094
Kevin Enderbye2297dd2015-01-07 21:02:18 +000095cl::opt<bool>
96llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
Nick Kledzikac431442014-09-12 21:34:15 +000097
Kevin Enderbye2297dd2015-01-07 21:02:18 +000098cl::opt<bool>
99llvm::Bind("bind", cl::desc("Display mach-o binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000100
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000101cl::opt<bool>
102llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000103
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000104cl::opt<bool>
105llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000106
Adrian Prantl437105a2015-07-08 02:04:15 +0000107cl::opt<bool>
108llvm::RawClangAST("raw-clang-ast",
109 cl::desc("Dump the raw binary contents of the clang AST section"));
110
Nick Kledzik56ebef42014-09-16 01:41:51 +0000111static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000112MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000113static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000114MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000115
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000116cl::opt<std::string>
117llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
118 "see -version for available targets"));
119
120cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000121llvm::MCPU("mcpu",
122 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
123 cl::value_desc("cpu-name"),
124 cl::init(""));
125
126cl::opt<std::string>
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000127llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000128 "see -version for available targets"));
129
Kevin Enderby98da6132015-01-20 21:47:46 +0000130cl::opt<bool>
131llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
132 "headers for each section."));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000133static cl::alias
134SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
135 cl::aliasopt(SectionHeaders));
136static cl::alias
137SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
138 cl::aliasopt(SectionHeaders));
Colin LeMahieufcc32762015-07-29 19:08:10 +0000139
Colin LeMahieu77804be2015-07-29 15:45:39 +0000140cl::list<std::string>
Colin LeMahieufcc32762015-07-29 19:08:10 +0000141llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
142 "With -macho dump segment,section"));
143cl::alias
144static FilterSectionsj("j", cl::desc("Alias for --section"),
145 cl::aliasopt(llvm::FilterSections));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000146
Kevin Enderbyc9595622014-08-06 23:24:41 +0000147cl::list<std::string>
148llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000149 cl::CommaSeparated,
150 cl::desc("Target specific attributes"),
151 cl::value_desc("a1,+a2,-a3,..."));
152
Kevin Enderbybf246f52014-09-24 23:08:22 +0000153cl::opt<bool>
154llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
155 "instructions, do not print "
156 "the instruction bytes."));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000157
Kevin Enderby98da6132015-01-20 21:47:46 +0000158cl::opt<bool>
159llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000160
161static cl::alias
162UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
163 cl::aliasopt(UnwindInfo));
164
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000165cl::opt<bool>
166llvm::PrivateHeaders("private-headers",
167 cl::desc("Display format specific file headers"));
Michael J. Spencer209565db2013-01-06 03:56:49 +0000168
Kevin Enderby0ae163f2016-01-13 00:25:36 +0000169cl::opt<bool>
170llvm::FirstPrivateHeader("private-header",
171 cl::desc("Display only the first format specific file "
172 "header"));
173
Michael J. Spencer209565db2013-01-06 03:56:49 +0000174static cl::alias
175PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
176 cl::aliasopt(PrivateHeaders));
177
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000178cl::opt<bool>
179 llvm::PrintImmHex("print-imm-hex",
180 cl::desc("Use hex format for immediate values"));
181
Sanjoy Das6f567a42015-06-22 18:03:02 +0000182cl::opt<bool> PrintFaultMaps("fault-map-section",
183 cl::desc("Display contents of faultmap section"));
184
Igor Laevsky03a670c2016-01-26 15:09:42 +0000185cl::opt<DIDumpType> llvm::DwarfDumpType(
186 "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
187 cl::values(clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
188 clEnumValEnd));
189
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000190static StringRef ToolName;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000191
Colin LeMahieu77804be2015-07-29 15:45:39 +0000192namespace {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000193typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000194
195class SectionFilterIterator {
196public:
197 SectionFilterIterator(FilterPredicate P,
198 llvm::object::section_iterator const &I,
199 llvm::object::section_iterator const &E)
200 : Predicate(P), Iterator(I), End(E) {
201 ScanPredicate();
202 }
Benjamin Kramerac9257b2015-09-24 14:52:52 +0000203 const llvm::object::SectionRef &operator*() const { return *Iterator; }
Colin LeMahieu77804be2015-07-29 15:45:39 +0000204 SectionFilterIterator &operator++() {
205 ++Iterator;
206 ScanPredicate();
207 return *this;
208 }
209 bool operator!=(SectionFilterIterator const &Other) const {
210 return Iterator != Other.Iterator;
211 }
212
213private:
214 void ScanPredicate() {
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000215 while (Iterator != End && !Predicate(*Iterator)) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000216 ++Iterator;
217 }
218 }
219 FilterPredicate Predicate;
220 llvm::object::section_iterator Iterator;
221 llvm::object::section_iterator End;
222};
223
224class SectionFilter {
225public:
226 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
227 : Predicate(P), Object(O) {}
228 SectionFilterIterator begin() {
229 return SectionFilterIterator(Predicate, Object.section_begin(),
230 Object.section_end());
231 }
232 SectionFilterIterator end() {
233 return SectionFilterIterator(Predicate, Object.section_end(),
234 Object.section_end());
235 }
236
237private:
238 FilterPredicate Predicate;
239 llvm::object::ObjectFile const &Object;
240};
241SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000242 return SectionFilter([](llvm::object::SectionRef const &S) {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000243 if(FilterSections.empty())
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000244 return true;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000245 llvm::StringRef String;
246 std::error_code error = S.getName(String);
Colin LeMahieufcc32762015-07-29 19:08:10 +0000247 if (error)
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000248 return false;
Colin LeMahieufcc32762015-07-29 19:08:10 +0000249 return std::find(FilterSections.begin(),
250 FilterSections.end(),
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000251 String) != FilterSections.end();
Colin LeMahieu77804be2015-07-29 15:45:39 +0000252 },
253 O);
254}
255}
256
Davide Italianoccd53fe2015-08-05 07:18:31 +0000257void llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000258 if (!EC)
Davide Italianoccd53fe2015-08-05 07:18:31 +0000259 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000260
Davide Italiano140af642015-12-25 18:16:45 +0000261 errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
262 errs().flush();
Davide Italiano7f6c3012015-08-06 00:18:52 +0000263 exit(1);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000264}
265
Davide Italianoed9d95b2015-12-29 13:41:02 +0000266LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
267 std::error_code EC) {
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000268 assert(EC);
269 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
Davide Italianoccd53fe2015-08-05 07:18:31 +0000270 exit(1);
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000271}
272
Craig Toppere6cb63e2014-04-25 04:24:47 +0000273static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000274 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000275 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000276 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000277 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000278 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000279 // TheTriple defaults to ELF, and COFF doesn't have an environment:
280 // the best we can do here is indicate that it is mach-o.
281 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000282 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000283
284 if (Obj->isCOFF()) {
285 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
286 if (COFFObj->getArch() == Triple::thumb)
287 TheTriple.setTriple("thumbv7-windows");
288 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000289 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000290 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000291 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000292
293 // Get the target specific parser.
294 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000295 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
296 Error);
Davide Italianobb599e32015-12-03 22:13:40 +0000297 if (!TheTarget)
298 report_fatal_error("can't find target: " + Error);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000299
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000300 // Update the triple name and return the found target.
301 TripleName = TheTriple.getTriple();
302 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000303}
304
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000305bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindola704cd842015-07-06 15:47:43 +0000306 return a.getOffset() < b.getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +0000307}
308
Colin LeMahieufb76b002015-05-28 19:07:14 +0000309namespace {
310class PrettyPrinter {
311public:
Colin LeMahieu0b5890d2015-05-28 20:59:08 +0000312 virtual ~PrettyPrinter(){}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000313 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
Colin LeMahieufb76b002015-05-28 19:07:14 +0000314 ArrayRef<uint8_t> Bytes, uint64_t Address,
315 raw_ostream &OS, StringRef Annot,
316 MCSubtargetInfo const &STI) {
317 outs() << format("%8" PRIx64 ":", Address);
318 if (!NoShowRawInsn) {
319 outs() << "\t";
320 dumpBytes(Bytes, outs());
321 }
322 IP.printInst(MI, outs(), "", STI);
323 }
324};
325PrettyPrinter PrettyPrinterInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000326class HexagonPrettyPrinter : public PrettyPrinter {
327public:
328 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
329 raw_ostream &OS) {
330 uint32_t opcode =
331 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
332 OS << format("%8" PRIx64 ":", Address);
333 if (!NoShowRawInsn) {
334 OS << "\t";
335 dumpBytes(Bytes.slice(0, 4), OS);
336 OS << format("%08" PRIx32, opcode);
337 }
338 }
339 void printInst(MCInstPrinter &IP, const MCInst *MI,
340 ArrayRef<uint8_t> Bytes, uint64_t Address,
341 raw_ostream &OS, StringRef Annot,
342 MCSubtargetInfo const &STI) override {
343 std::string Buffer;
344 {
345 raw_string_ostream TempStream(Buffer);
346 IP.printInst(MI, TempStream, "", STI);
347 }
348 StringRef Contents(Buffer);
349 // Split off bundle attributes
350 auto PacketBundle = Contents.rsplit('\n');
351 // Split off first instruction from the rest
352 auto HeadTail = PacketBundle.first.split('\n');
353 auto Preamble = " { ";
354 auto Separator = "";
355 while(!HeadTail.first.empty()) {
356 OS << Separator;
357 Separator = "\n";
358 printLead(Bytes, Address, OS);
359 OS << Preamble;
360 Preamble = " ";
361 StringRef Inst;
362 auto Duplex = HeadTail.first.split('\v');
363 if(!Duplex.second.empty()){
364 OS << Duplex.first;
365 OS << "; ";
366 Inst = Duplex.second;
367 }
368 else
369 Inst = HeadTail.first;
370 OS << Inst;
371 Bytes = Bytes.slice(4);
372 Address += 4;
373 HeadTail = HeadTail.second.split('\n');
374 }
375 OS << " } " << PacketBundle.second;
376 }
377};
378HexagonPrettyPrinter HexagonPrettyPrinterInst;
Colin LeMahieu35436a22015-05-29 14:48:25 +0000379PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000380 switch(Triple.getArch()) {
381 default:
382 return PrettyPrinterInst;
383 case Triple::hexagon:
384 return HexagonPrettyPrinterInst;
385 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000386}
387}
388
Rafael Espindola37070a52015-06-03 04:48:06 +0000389template <class ELFT>
Rafael Espindola37070a52015-06-03 04:48:06 +0000390static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000391 const RelocationRef &RelRef,
Rafael Espindola37070a52015-06-03 04:48:06 +0000392 SmallVectorImpl<char> &Result) {
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000393 DataRefImpl Rel = RelRef.getRawDataRefImpl();
394
Rafael Espindola37070a52015-06-03 04:48:06 +0000395 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
396 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000397 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
398
Rafael Espindola37070a52015-06-03 04:48:06 +0000399 const ELFFile<ELFT> &EF = *Obj->getELFFile();
400
Rafael Espindola6def3042015-07-01 12:56:27 +0000401 ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
402 if (std::error_code EC = SecOrErr.getError())
403 return EC;
404 const Elf_Shdr *Sec = *SecOrErr;
405 ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
406 if (std::error_code EC = SymTabOrErr.getError())
407 return EC;
408 const Elf_Shdr *SymTab = *SymTabOrErr;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000409 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
410 SymTab->sh_type == ELF::SHT_DYNSYM);
Rafael Espindola6def3042015-07-01 12:56:27 +0000411 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
412 if (std::error_code EC = StrTabSec.getError())
413 return EC;
414 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000415 if (std::error_code EC = StrTabOrErr.getError())
416 return EC;
417 StringRef StrTab = *StrTabOrErr;
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000418 uint8_t type = RelRef.getType();
Rafael Espindola37070a52015-06-03 04:48:06 +0000419 StringRef res;
420 int64_t addend = 0;
Rafael Espindola6def3042015-07-01 12:56:27 +0000421 switch (Sec->sh_type) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000422 default:
423 return object_error::parse_failed;
424 case ELF::SHT_REL: {
Rafael Espindola37070a52015-06-03 04:48:06 +0000425 // TODO: Read implicit addend from section data.
426 break;
427 }
428 case ELF::SHT_RELA: {
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000429 const Elf_Rela *ERela = Obj->getRela(Rel);
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000430 addend = ERela->r_addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000431 break;
432 }
433 }
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000434 symbol_iterator SI = RelRef.getSymbol();
435 const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
Rafael Espindola75d5b542015-06-03 05:14:22 +0000436 StringRef Target;
Rafael Espindola75d5b542015-06-03 05:14:22 +0000437 if (symb->getType() == ELF::STT_SECTION) {
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000438 ErrorOr<section_iterator> SymSI = SI->getSection();
439 if (std::error_code EC = SymSI.getError())
440 return EC;
441 const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
442 ErrorOr<StringRef> SecName = EF.getSectionName(SymSec);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000443 if (std::error_code EC = SecName.getError())
444 return EC;
445 Target = *SecName;
446 } else {
Rafael Espindola44c28712015-06-29 21:24:55 +0000447 ErrorOr<StringRef> SymName = symb->getName(StrTab);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000448 if (!SymName)
449 return SymName.getError();
450 Target = *SymName;
451 }
Rafael Espindola37070a52015-06-03 04:48:06 +0000452 switch (EF.getHeader()->e_machine) {
453 case ELF::EM_X86_64:
454 switch (type) {
455 case ELF::R_X86_64_PC8:
456 case ELF::R_X86_64_PC16:
457 case ELF::R_X86_64_PC32: {
458 std::string fmtbuf;
459 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000460 fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
Rafael Espindola37070a52015-06-03 04:48:06 +0000461 fmt.flush();
462 Result.append(fmtbuf.begin(), fmtbuf.end());
463 } break;
464 case ELF::R_X86_64_8:
465 case ELF::R_X86_64_16:
466 case ELF::R_X86_64_32:
467 case ELF::R_X86_64_32S:
468 case ELF::R_X86_64_64: {
469 std::string fmtbuf;
470 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000471 fmt << Target << (addend < 0 ? "" : "+") << addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000472 fmt.flush();
473 Result.append(fmtbuf.begin(), fmtbuf.end());
474 } break;
475 default:
476 res = "Unknown";
477 }
478 break;
Jacques Pienaarea9f25a2016-03-01 21:21:42 +0000479 case ELF::EM_LANAI:
Rafael Espindola37070a52015-06-03 04:48:06 +0000480 case ELF::EM_AARCH64: {
481 std::string fmtbuf;
482 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000483 fmt << Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000484 if (addend != 0)
485 fmt << (addend < 0 ? "" : "+") << addend;
486 fmt.flush();
487 Result.append(fmtbuf.begin(), fmtbuf.end());
488 break;
489 }
490 case ELF::EM_386:
Michael Kupersteina3b79dd2015-11-04 11:21:50 +0000491 case ELF::EM_IAMCU:
Rafael Espindola37070a52015-06-03 04:48:06 +0000492 case ELF::EM_ARM:
493 case ELF::EM_HEXAGON:
494 case ELF::EM_MIPS:
Rafael Espindola75d5b542015-06-03 05:14:22 +0000495 res = Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000496 break;
Dan Gohman46350172016-01-12 20:56:01 +0000497 case ELF::EM_WEBASSEMBLY:
498 switch (type) {
499 case ELF::R_WEBASSEMBLY_DATA: {
500 std::string fmtbuf;
501 raw_string_ostream fmt(fmtbuf);
502 fmt << Target << (addend < 0 ? "" : "+") << addend;
503 fmt.flush();
504 Result.append(fmtbuf.begin(), fmtbuf.end());
505 break;
506 }
507 case ELF::R_WEBASSEMBLY_FUNCTION:
508 res = Target;
509 break;
510 default:
511 res = "Unknown";
512 }
513 break;
Rafael Espindola37070a52015-06-03 04:48:06 +0000514 default:
515 res = "Unknown";
516 }
517 if (Result.empty())
518 Result.append(res.begin(), res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000519 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000520}
521
522static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000523 const RelocationRef &Rel,
Rafael Espindola37070a52015-06-03 04:48:06 +0000524 SmallVectorImpl<char> &Result) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000525 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
526 return getRelocationValueString(ELF32LE, Rel, Result);
527 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
528 return getRelocationValueString(ELF64LE, Rel, Result);
529 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
530 return getRelocationValueString(ELF32BE, Rel, Result);
531 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
532 return getRelocationValueString(ELF64BE, Rel, Result);
533}
534
535static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
536 const RelocationRef &Rel,
537 SmallVectorImpl<char> &Result) {
538 symbol_iterator SymI = Rel.getSymbol();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000539 ErrorOr<StringRef> SymNameOrErr = SymI->getName();
540 if (std::error_code EC = SymNameOrErr.getError())
Rafael Espindola37070a52015-06-03 04:48:06 +0000541 return EC;
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000542 StringRef SymName = *SymNameOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000543 Result.append(SymName.begin(), SymName.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000544 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000545}
546
547static void printRelocationTargetName(const MachOObjectFile *O,
548 const MachO::any_relocation_info &RE,
549 raw_string_ostream &fmt) {
550 bool IsScattered = O->isRelocationScattered(RE);
551
552 // Target of a scattered relocation is an address. In the interest of
553 // generating pretty output, scan through the symbol table looking for a
554 // symbol that aligns with that address. If we find one, print it.
555 // Otherwise, we just print the hex address of the target.
556 if (IsScattered) {
557 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
558
559 for (const SymbolRef &Symbol : O->symbols()) {
560 std::error_code ec;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000561 ErrorOr<uint64_t> Addr = Symbol.getAddress();
562 if ((ec = Addr.getError()))
Rafael Espindola37070a52015-06-03 04:48:06 +0000563 report_fatal_error(ec.message());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000564 if (*Addr != Val)
Rafael Espindola37070a52015-06-03 04:48:06 +0000565 continue;
Rafael Espindolaed067c42015-07-03 18:19:00 +0000566 ErrorOr<StringRef> Name = Symbol.getName();
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000567 if (std::error_code EC = Name.getError())
568 report_fatal_error(EC.message());
569 fmt << *Name;
Rafael Espindola37070a52015-06-03 04:48:06 +0000570 return;
571 }
572
573 // If we couldn't find a symbol that this relocation refers to, try
574 // to find a section beginning instead.
Colin LeMahieu77804be2015-07-29 15:45:39 +0000575 for (const SectionRef &Section : ToolSectionFilter(*O)) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000576 std::error_code ec;
577
578 StringRef Name;
579 uint64_t Addr = Section.getAddress();
580 if (Addr != Val)
581 continue;
582 if ((ec = Section.getName(Name)))
583 report_fatal_error(ec.message());
584 fmt << Name;
585 return;
586 }
587
588 fmt << format("0x%x", Val);
589 return;
590 }
591
592 StringRef S;
593 bool isExtern = O->getPlainRelocationExternal(RE);
594 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
595
596 if (isExtern) {
597 symbol_iterator SI = O->symbol_begin();
598 advance(SI, Val);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000599 ErrorOr<StringRef> SOrErr = SI->getName();
Davide Italianoccd53fe2015-08-05 07:18:31 +0000600 error(SOrErr.getError());
601 S = *SOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000602 } else {
603 section_iterator SI = O->section_begin();
604 // Adjust for the fact that sections are 1-indexed.
605 advance(SI, Val - 1);
606 SI->getName(S);
607 }
608
609 fmt << S;
610}
611
612static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
613 const RelocationRef &RelRef,
614 SmallVectorImpl<char> &Result) {
615 DataRefImpl Rel = RelRef.getRawDataRefImpl();
616 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
617
618 unsigned Arch = Obj->getArch();
619
620 std::string fmtbuf;
621 raw_string_ostream fmt(fmtbuf);
622 unsigned Type = Obj->getAnyRelocationType(RE);
623 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
624
625 // Determine any addends that should be displayed with the relocation.
626 // These require decoding the relocation type, which is triple-specific.
627
628 // X86_64 has entirely custom relocation types.
629 if (Arch == Triple::x86_64) {
630 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
631
632 switch (Type) {
633 case MachO::X86_64_RELOC_GOT_LOAD:
634 case MachO::X86_64_RELOC_GOT: {
635 printRelocationTargetName(Obj, RE, fmt);
636 fmt << "@GOT";
637 if (isPCRel)
638 fmt << "PCREL";
639 break;
640 }
641 case MachO::X86_64_RELOC_SUBTRACTOR: {
642 DataRefImpl RelNext = Rel;
643 Obj->moveRelocationNext(RelNext);
644 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
645
646 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
647 // X86_64_RELOC_UNSIGNED.
648 // NOTE: Scattered relocations don't exist on x86_64.
649 unsigned RType = Obj->getAnyRelocationType(RENext);
650 if (RType != MachO::X86_64_RELOC_UNSIGNED)
651 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
652 "X86_64_RELOC_SUBTRACTOR.");
653
654 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
655 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
656 printRelocationTargetName(Obj, RENext, fmt);
657 fmt << "-";
658 printRelocationTargetName(Obj, RE, fmt);
659 break;
660 }
661 case MachO::X86_64_RELOC_TLV:
662 printRelocationTargetName(Obj, RE, fmt);
663 fmt << "@TLV";
664 if (isPCRel)
665 fmt << "P";
666 break;
667 case MachO::X86_64_RELOC_SIGNED_1:
668 printRelocationTargetName(Obj, RE, fmt);
669 fmt << "-1";
670 break;
671 case MachO::X86_64_RELOC_SIGNED_2:
672 printRelocationTargetName(Obj, RE, fmt);
673 fmt << "-2";
674 break;
675 case MachO::X86_64_RELOC_SIGNED_4:
676 printRelocationTargetName(Obj, RE, fmt);
677 fmt << "-4";
678 break;
679 default:
680 printRelocationTargetName(Obj, RE, fmt);
681 break;
682 }
683 // X86 and ARM share some relocation types in common.
684 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
685 Arch == Triple::ppc) {
686 // Generic relocation types...
687 switch (Type) {
688 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rui Ueyama7d099192015-06-09 15:20:42 +0000689 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000690 case MachO::GENERIC_RELOC_SECTDIFF: {
691 DataRefImpl RelNext = Rel;
692 Obj->moveRelocationNext(RelNext);
693 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
694
695 // X86 sect diff's must be followed by a relocation of type
696 // GENERIC_RELOC_PAIR.
697 unsigned RType = Obj->getAnyRelocationType(RENext);
698
699 if (RType != MachO::GENERIC_RELOC_PAIR)
700 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
701 "GENERIC_RELOC_SECTDIFF.");
702
703 printRelocationTargetName(Obj, RE, fmt);
704 fmt << "-";
705 printRelocationTargetName(Obj, RENext, fmt);
706 break;
707 }
708 }
709
710 if (Arch == Triple::x86 || Arch == Triple::ppc) {
711 switch (Type) {
712 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
713 DataRefImpl RelNext = Rel;
714 Obj->moveRelocationNext(RelNext);
715 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
716
717 // X86 sect diff's must be followed by a relocation of type
718 // GENERIC_RELOC_PAIR.
719 unsigned RType = Obj->getAnyRelocationType(RENext);
720 if (RType != MachO::GENERIC_RELOC_PAIR)
721 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
722 "GENERIC_RELOC_LOCAL_SECTDIFF.");
723
724 printRelocationTargetName(Obj, RE, fmt);
725 fmt << "-";
726 printRelocationTargetName(Obj, RENext, fmt);
727 break;
728 }
729 case MachO::GENERIC_RELOC_TLV: {
730 printRelocationTargetName(Obj, RE, fmt);
731 fmt << "@TLV";
732 if (IsPCRel)
733 fmt << "P";
734 break;
735 }
736 default:
737 printRelocationTargetName(Obj, RE, fmt);
738 }
739 } else { // ARM-specific relocations
740 switch (Type) {
741 case MachO::ARM_RELOC_HALF:
742 case MachO::ARM_RELOC_HALF_SECTDIFF: {
743 // Half relocations steal a bit from the length field to encode
744 // whether this is an upper16 or a lower16 relocation.
745 bool isUpper = Obj->getAnyRelocationLength(RE) >> 1;
746
747 if (isUpper)
748 fmt << ":upper16:(";
749 else
750 fmt << ":lower16:(";
751 printRelocationTargetName(Obj, RE, fmt);
752
753 DataRefImpl RelNext = Rel;
754 Obj->moveRelocationNext(RelNext);
755 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
756
757 // ARM half relocs must be followed by a relocation of type
758 // ARM_RELOC_PAIR.
759 unsigned RType = Obj->getAnyRelocationType(RENext);
760 if (RType != MachO::ARM_RELOC_PAIR)
761 report_fatal_error("Expected ARM_RELOC_PAIR after "
762 "ARM_RELOC_HALF");
763
764 // NOTE: The half of the target virtual address is stashed in the
765 // address field of the secondary relocation, but we can't reverse
766 // engineer the constant offset from it without decoding the movw/movt
767 // instruction to find the other half in its immediate field.
768
769 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
770 // symbol/section pointer of the follow-on relocation.
771 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
772 fmt << "-";
773 printRelocationTargetName(Obj, RENext, fmt);
774 }
775
776 fmt << ")";
777 break;
778 }
779 default: { printRelocationTargetName(Obj, RE, fmt); }
780 }
781 }
782 } else
783 printRelocationTargetName(Obj, RE, fmt);
784
785 fmt.flush();
786 Result.append(fmtbuf.begin(), fmtbuf.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000787 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000788}
789
790static std::error_code getRelocationValueString(const RelocationRef &Rel,
791 SmallVectorImpl<char> &Result) {
Rafael Espindola854038e2015-06-26 14:51:16 +0000792 const ObjectFile *Obj = Rel.getObject();
Rafael Espindola37070a52015-06-03 04:48:06 +0000793 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
794 return getRelocationValueString(ELF, Rel, Result);
795 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
796 return getRelocationValueString(COFF, Rel, Result);
797 auto *MachO = cast<MachOObjectFile>(Obj);
798 return getRelocationValueString(MachO, Rel, Result);
799}
800
Rafael Espindola0ad71d92015-06-30 03:41:26 +0000801/// @brief Indicates whether this relocation should hidden when listing
802/// relocations, usually because it is the trailing part of a multipart
803/// relocation that will be printed as part of the leading relocation.
804static bool getHidden(RelocationRef RelRef) {
805 const ObjectFile *Obj = RelRef.getObject();
806 auto *MachO = dyn_cast<MachOObjectFile>(Obj);
807 if (!MachO)
808 return false;
809
810 unsigned Arch = MachO->getArch();
811 DataRefImpl Rel = RelRef.getRawDataRefImpl();
812 uint64_t Type = MachO->getRelocationType(Rel);
813
814 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
815 // is always hidden.
816 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
817 if (Type == MachO::GENERIC_RELOC_PAIR)
818 return true;
819 } else if (Arch == Triple::x86_64) {
820 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
821 // an X86_64_RELOC_SUBTRACTOR.
822 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
823 DataRefImpl RelPrev = Rel;
824 RelPrev.d.a--;
825 uint64_t PrevType = MachO->getRelocationType(RelPrev);
826 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
827 return true;
828 }
829 }
830
831 return false;
832}
833
Michael J. Spencer51862b32011-10-13 22:17:18 +0000834static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +0000835 const Target *TheTarget = getTarget(Obj);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000836
Jack Carter551efd72012-08-28 19:24:49 +0000837 // Package up features to be passed to target/subtarget
838 std::string FeaturesStr;
839 if (MAttrs.size()) {
840 SubtargetFeatures Features;
841 for (unsigned i = 0; i != MAttrs.size(); ++i)
842 Features.AddFeature(MAttrs[i]);
843 FeaturesStr = Features.getString();
844 }
845
Ahmed Charles56440fd2014-03-06 05:51:42 +0000846 std::unique_ptr<const MCRegisterInfo> MRI(
847 TheTarget->createMCRegInfo(TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +0000848 if (!MRI)
849 report_fatal_error("error: no register info for target " + TripleName);
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000850
851 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000852 std::unique_ptr<const MCAsmInfo> AsmInfo(
853 TheTarget->createMCAsmInfo(*MRI, TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +0000854 if (!AsmInfo)
855 report_fatal_error("error: no assembly info for target " + TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +0000856 std::unique_ptr<const MCSubtargetInfo> STI(
Kevin Enderbyc9595622014-08-06 23:24:41 +0000857 TheTarget->createMCSubtargetInfo(TripleName, MCPU, FeaturesStr));
Davide Italiano711e4952015-12-17 01:59:50 +0000858 if (!STI)
859 report_fatal_error("error: no subtarget info for target " + TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +0000860 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Davide Italiano711e4952015-12-17 01:59:50 +0000861 if (!MII)
862 report_fatal_error("error: no instruction info for target " + TripleName);
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000863 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
864 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
865
866 std::unique_ptr<MCDisassembler> DisAsm(
867 TheTarget->createMCDisassembler(*STI, Ctx));
Davide Italiano711e4952015-12-17 01:59:50 +0000868 if (!DisAsm)
869 report_fatal_error("error: no disassembler for target " + TripleName);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000870
Ahmed Charles56440fd2014-03-06 05:51:42 +0000871 std::unique_ptr<const MCInstrAnalysis> MIA(
872 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000873
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000874 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Daniel Sanders50f17232015-09-15 16:17:27 +0000875 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
876 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Davide Italiano711e4952015-12-17 01:59:50 +0000877 if (!IP)
878 report_fatal_error("error: no instruction printer for target " +
879 TripleName);
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000880 IP->setPrintImmHex(PrintImmHex);
Colin LeMahieu35436a22015-05-29 14:48:25 +0000881 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +0000882
Greg Fitzgerald18432272014-03-20 22:55:15 +0000883 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
884 "\t\t\t%08" PRIx64 ": ";
885
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000886 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
887 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000888 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000889 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000890 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000891 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +0000892 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +0000893 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +0000894 }
895
David Majnemer81afca62015-07-07 22:06:59 +0000896 // Create a mapping from virtual address to symbol name. This is used to
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000897 // pretty print the symbols while disassembling.
898 typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy;
899 std::map<SectionRef, SectionSymbolsTy> AllSymbols;
900 for (const SymbolRef &Symbol : Obj->symbols()) {
901 ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress();
902 error(AddressOrErr.getError());
903 uint64_t Address = *AddressOrErr;
David Majnemer2603a8fa2015-07-09 18:11:40 +0000904
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000905 ErrorOr<StringRef> Name = Symbol.getName();
906 error(Name.getError());
907 if (Name->empty())
908 continue;
David Majnemer81afca62015-07-07 22:06:59 +0000909
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000910 ErrorOr<section_iterator> SectionOrErr = Symbol.getSection();
911 error(SectionOrErr.getError());
912 section_iterator SecI = *SectionOrErr;
913 if (SecI == Obj->section_end())
914 continue;
David Majnemer81afca62015-07-07 22:06:59 +0000915
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000916 AllSymbols[*SecI].emplace_back(Address, *Name);
David Majnemer81afca62015-07-07 22:06:59 +0000917 }
918
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000919 // Create a mapping from virtual address to section.
920 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
921 for (SectionRef Sec : Obj->sections())
922 SectionAddresses.emplace_back(Sec.getAddress(), Sec);
923 array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
924
925 // Linked executables (.exe and .dll files) typically don't include a real
926 // symbol table but they might contain an export table.
927 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
928 for (const auto &ExportEntry : COFFObj->export_directories()) {
929 StringRef Name;
930 error(ExportEntry.getSymbolName(Name));
931 if (Name.empty())
932 continue;
933 uint32_t RVA;
934 error(ExportEntry.getExportRVA(RVA));
935
936 uint64_t VA = COFFObj->getImageBase() + RVA;
937 auto Sec = std::upper_bound(
938 SectionAddresses.begin(), SectionAddresses.end(), VA,
939 [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
940 return LHS < RHS.first;
941 });
942 if (Sec != SectionAddresses.begin())
943 --Sec;
944 else
945 Sec = SectionAddresses.end();
946
947 if (Sec != SectionAddresses.end())
948 AllSymbols[Sec->second].emplace_back(VA, Name);
949 }
950 }
951
952 // Sort all the symbols, this allows us to use a simple binary search to find
953 // a symbol near an address.
954 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
955 array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
956
Colin LeMahieu77804be2015-07-29 15:45:39 +0000957 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Colin LeMahieuf34933e2015-07-23 20:58:49 +0000958 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
Mark Seaborneb03ac52014-01-25 00:32:01 +0000959 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000960
Rafael Espindola80291272014-10-08 15:28:58 +0000961 uint64_t SectionAddr = Section.getAddress();
962 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +0000963 if (!SectSize)
964 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +0000965
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000966 // Get the list of all the symbols in this section.
967 SectionSymbolsTy &Symbols = AllSymbols[Section];
Davide Italianof0706882015-10-01 21:57:09 +0000968 std::vector<uint64_t> DataMappingSymsAddr;
969 std::vector<uint64_t> TextMappingSymsAddr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000970 if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
971 for (const auto &Symb : Symbols) {
972 uint64_t Address = Symb.first;
973 StringRef Name = Symb.second;
974 if (Name.startswith("$d"))
David Majnemer153722d2015-11-18 04:35:32 +0000975 DataMappingSymsAddr.push_back(Address - SectionAddr);
David Majnemerfbb1c3a2015-11-18 02:49:19 +0000976 if (Name.startswith("$x"))
David Majnemer153722d2015-11-18 04:35:32 +0000977 TextMappingSymsAddr.push_back(Address - SectionAddr);
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000978 }
979 }
980
Davide Italianof0706882015-10-01 21:57:09 +0000981 std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
982 std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
Benjamin Kramere0dda9c2011-07-15 18:39:24 +0000983
Michael J. Spencer51862b32011-10-13 22:17:18 +0000984 // Make a list of all the relocations for this section.
985 std::vector<RelocationRef> Rels;
986 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000987 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
988 for (const RelocationRef &Reloc : RelocSec.relocations()) {
989 Rels.push_back(Reloc);
990 }
Michael J. Spencer51862b32011-10-13 22:17:18 +0000991 }
992 }
993
994 // Sort relocations by address.
995 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
996
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000997 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +0000998 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +0000999 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001000 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001001 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +00001002 StringRef name;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001003 error(Section.getName(name));
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001004 outs() << "Disassembly of section ";
1005 if (!SegmentName.empty())
1006 outs() << SegmentName << ",";
1007 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001008
Rafael Espindola7884c952015-06-04 15:01:05 +00001009 // If the section has no symbol at the start, just insert a dummy one.
1010 if (Symbols.empty() || Symbols[0].first != 0)
David Majnemer153722d2015-11-18 04:35:32 +00001011 Symbols.insert(Symbols.begin(), std::make_pair(SectionAddr, name));
Alp Tokere69170a2014-06-26 22:52:05 +00001012
1013 SmallString<40> Comments;
1014 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001015
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001016 StringRef BytesStr;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001017 error(Section.getContents(BytesStr));
Aaron Ballman106fd7b2014-11-12 14:01:17 +00001018 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1019 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001020
Michael J. Spencer2670c252011-01-20 06:39:06 +00001021 uint64_t Size;
1022 uint64_t Index;
1023
Michael J. Spencer51862b32011-10-13 22:17:18 +00001024 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1025 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001026 // Disassemble symbol by symbol.
1027 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Rafael Espindolae45c7402014-08-17 16:31:39 +00001028
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001029 uint64_t Start = Symbols[si].first - SectionAddr;
1030 // The end is either the section end or the beginning of the next
1031 // symbol.
1032 uint64_t End =
1033 (si == se - 1) ? SectSize : Symbols[si + 1].first - SectionAddr;
1034 // Don't try to disassemble beyond the end of section contents.
1035 if (End > SectSize)
1036 End = SectSize;
Rafael Espindolae45c7402014-08-17 16:31:39 +00001037 // If this symbol has the same address as the next symbol, then skip it.
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001038 if (Start >= End)
Michael J. Spenceree84f642011-10-13 20:37:08 +00001039 continue;
1040
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001041 outs() << '\n' << Symbols[si].second << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +00001042
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001043#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +00001044 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001045#else
Mark Seaborneb03ac52014-01-25 00:32:01 +00001046 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001047#endif
1048
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001049 for (Index = Start; Index < End; Index += Size) {
1050 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +00001051
Davide Italianof0706882015-10-01 21:57:09 +00001052 // AArch64 ELF binaries can interleave data and text in the
1053 // same section. We rely on the markers introduced to
1054 // understand what we need to dump.
1055 if (Obj->isELF() && Obj->getArch() == Triple::aarch64) {
1056 uint64_t Stride = 0;
1057
1058 auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1059 DataMappingSymsAddr.end(), Index);
1060 if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1061 // Switch to data.
1062 while (Index < End) {
1063 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1064 outs() << "\t";
1065 if (Index + 4 <= End) {
1066 Stride = 4;
1067 dumpBytes(Bytes.slice(Index, 4), outs());
1068 outs() << "\t.word";
1069 } else if (Index + 2 <= End) {
1070 Stride = 2;
1071 dumpBytes(Bytes.slice(Index, 2), outs());
1072 outs() << "\t.short";
1073 } else {
1074 Stride = 1;
1075 dumpBytes(Bytes.slice(Index, 1), outs());
1076 outs() << "\t.byte";
1077 }
1078 Index += Stride;
1079 outs() << "\n";
1080 auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1081 TextMappingSymsAddr.end(), Index);
1082 if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1083 break;
1084 }
1085 }
1086 }
1087
1088 if (Index >= End)
1089 break;
1090
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001091 if (DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1092 SectionAddr + Index, DebugOut,
1093 CommentStream)) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +00001094 PIP.printInst(*IP, &Inst,
Colin LeMahieufb76b002015-05-28 19:07:14 +00001095 Bytes.slice(Index, Size),
1096 SectionAddr + Index, outs(), "", *STI);
Alp Tokere69170a2014-06-26 22:52:05 +00001097 outs() << CommentStream.str();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001098 Comments.clear();
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001099
1100 // Try to resolve the target of a call, tail call, etc. to a specific
1101 // symbol.
David Majnemer2603a8fa2015-07-09 18:11:40 +00001102 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1103 MIA->isConditionalBranch(Inst))) {
David Majnemer81afca62015-07-07 22:06:59 +00001104 uint64_t Target;
1105 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001106 // In a relocatable object, the target's section must reside in
1107 // the same section as the call instruction or it is accessed
1108 // through a relocation.
1109 //
1110 // In a non-relocatable object, the target may be in any section.
1111 //
1112 // N.B. We don't walk the relocations in the relocatable case yet.
1113 auto *TargetSectionSymbols = &Symbols;
1114 if (!Obj->isRelocatableObject()) {
1115 auto SectionAddress = std::upper_bound(
1116 SectionAddresses.begin(), SectionAddresses.end(), Target,
1117 [](uint64_t LHS,
1118 const std::pair<uint64_t, SectionRef> &RHS) {
1119 return LHS < RHS.first;
1120 });
1121 if (SectionAddress != SectionAddresses.begin()) {
1122 --SectionAddress;
1123 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1124 } else {
1125 TargetSectionSymbols = nullptr;
1126 }
1127 }
David Majnemer2603a8fa2015-07-09 18:11:40 +00001128
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001129 // Find the first symbol in the section whose offset is less than
1130 // or equal to the target.
1131 if (TargetSectionSymbols) {
1132 auto TargetSym = std::upper_bound(
1133 TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1134 Target, [](uint64_t LHS,
1135 const std::pair<uint64_t, StringRef> &RHS) {
1136 return LHS < RHS.first;
1137 });
David Majnemer153722d2015-11-18 04:35:32 +00001138 if (TargetSym != TargetSectionSymbols->begin()) {
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001139 --TargetSym;
1140 uint64_t TargetAddress = std::get<0>(*TargetSym);
1141 StringRef TargetName = std::get<1>(*TargetSym);
1142 outs() << " <" << TargetName;
1143 uint64_t Disp = Target - TargetAddress;
1144 if (Disp)
1145 outs() << '+' << utohexstr(Disp);
1146 outs() << '>';
1147 }
David Majnemer81afca62015-07-07 22:06:59 +00001148 }
1149 }
1150 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001151 outs() << "\n";
1152 } else {
1153 errs() << ToolName << ": warning: invalid instruction encoding\n";
1154 if (Size == 0)
1155 Size = 1; // skip illegible bytes
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001156 }
Michael J. Spencer51862b32011-10-13 22:17:18 +00001157
1158 // Print relocation for instruction.
1159 while (rel_cur != rel_end) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001160 bool hidden = getHidden(*rel_cur);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001161 uint64_t addr = rel_cur->getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +00001162 SmallString<16> name;
1163 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +00001164
1165 // If this relocation is hidden, skip it.
Owen Andersonfa3e5202011-10-25 20:35:53 +00001166 if (hidden) goto skip_print_rel;
1167
Michael J. Spencer51862b32011-10-13 22:17:18 +00001168 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +00001169 if (addr >= Index + Size) break;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001170 rel_cur->getTypeName(name);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001171 error(getRelocationValueString(*rel_cur, val));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001172 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +00001173 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +00001174
1175 skip_print_rel:
1176 ++rel_cur;
1177 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +00001178 }
Michael J. Spencer2670c252011-01-20 06:39:06 +00001179 }
1180 }
1181}
1182
Kevin Enderby98da6132015-01-20 21:47:46 +00001183void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +00001184 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1185 "%08" PRIx64;
Rafael Espindolac66d7612014-08-17 19:09:37 +00001186 // Regular objdump doesn't print relocations in non-relocatable object
1187 // files.
1188 if (!Obj->isRelocatableObject())
1189 return;
1190
Colin LeMahieu77804be2015-07-29 15:45:39 +00001191 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001192 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001193 continue;
1194 StringRef secname;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001195 error(Section.getName(secname));
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001196 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001197 for (const RelocationRef &Reloc : Section.relocations()) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001198 bool hidden = getHidden(Reloc);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001199 uint64_t address = Reloc.getOffset();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001200 SmallString<32> relocname;
1201 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001202 if (hidden)
1203 continue;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001204 Reloc.getTypeName(relocname);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001205 error(getRelocationValueString(Reloc, valuestr));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001206 outs() << format(Fmt.data(), address) << " " << relocname << " "
1207 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001208 }
1209 outs() << "\n";
1210 }
1211}
1212
Kevin Enderby98da6132015-01-20 21:47:46 +00001213void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001214 outs() << "Sections:\n"
1215 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001216 unsigned i = 0;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001217 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001218 StringRef Name;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001219 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001220 uint64_t Address = Section.getAddress();
1221 uint64_t Size = Section.getSize();
1222 bool Text = Section.isText();
1223 bool Data = Section.isData();
1224 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001225 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +00001226 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +00001227 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1228 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001229 ++i;
1230 }
1231}
1232
Kevin Enderby98da6132015-01-20 21:47:46 +00001233void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +00001234 std::error_code EC;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001235 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001236 StringRef Name;
1237 StringRef Contents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001238 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001239 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +00001240 uint64_t Size = Section.getSize();
1241 if (!Size)
1242 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001243
1244 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +00001245 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +00001246 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +00001247 ", %04" PRIx64 ")>\n",
1248 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +00001249 continue;
1250 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001251
Davide Italianoccd53fe2015-08-05 07:18:31 +00001252 error(Section.getContents(Contents));
David Majnemer8f6b04c2014-07-14 16:20:14 +00001253
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001254 // Dump out the content as hex and printable ascii characters.
1255 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +00001256 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001257 // Dump line of hex.
1258 for (std::size_t i = 0; i < 16; ++i) {
1259 if (i != 0 && i % 4 == 0)
1260 outs() << ' ';
1261 if (addr + i < end)
1262 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1263 << hexdigit(Contents[addr + i] & 0xF, true);
1264 else
1265 outs() << " ";
1266 }
1267 // Print ascii.
1268 outs() << " ";
1269 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001270 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001271 outs() << Contents[addr + i];
1272 else
1273 outs() << ".";
1274 }
1275 outs() << "\n";
1276 }
1277 }
1278}
1279
Kevin Enderby98da6132015-01-20 21:47:46 +00001280void llvm::PrintSymbolTable(const ObjectFile *o) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001281 outs() << "SYMBOL TABLE:\n";
1282
Rui Ueyama4e39f712014-03-18 18:58:51 +00001283 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Davide Italianoe85abf72015-12-20 09:54:34 +00001284 printCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +00001285 return;
1286 }
1287 for (const SymbolRef &Symbol : o->symbols()) {
Rafael Espindolaed067c42015-07-03 18:19:00 +00001288 ErrorOr<uint64_t> AddressOrError = Symbol.getAddress();
Davide Italianoccd53fe2015-08-05 07:18:31 +00001289 error(AddressOrError.getError());
Rafael Espindolaed067c42015-07-03 18:19:00 +00001290 uint64_t Address = *AddressOrError;
Rafael Espindola2fa80cc2015-06-26 12:18:49 +00001291 SymbolRef::Type Type = Symbol.getType();
Rui Ueyama4e39f712014-03-18 18:58:51 +00001292 uint32_t Flags = Symbol.getFlags();
Rafael Espindola8bab8892015-08-07 23:27:14 +00001293 ErrorOr<section_iterator> SectionOrErr = Symbol.getSection();
1294 error(SectionOrErr.getError());
1295 section_iterator Section = *SectionOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001296 StringRef Name;
1297 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1298 Section->getName(Name);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001299 } else {
1300 ErrorOr<StringRef> NameOrErr = Symbol.getName();
Davide Italianoccd53fe2015-08-05 07:18:31 +00001301 error(NameOrErr.getError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001302 Name = *NameOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001303 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001304
Rui Ueyama4e39f712014-03-18 18:58:51 +00001305 bool Global = Flags & SymbolRef::SF_Global;
1306 bool Weak = Flags & SymbolRef::SF_Weak;
1307 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001308 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +00001309 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +00001310
Rui Ueyama4e39f712014-03-18 18:58:51 +00001311 char GlobLoc = ' ';
1312 if (Type != SymbolRef::ST_Unknown)
1313 GlobLoc = Global ? 'g' : 'l';
1314 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1315 ? 'd' : ' ';
1316 char FileFunc = ' ';
1317 if (Type == SymbolRef::ST_File)
1318 FileFunc = 'f';
1319 else if (Type == SymbolRef::ST_Function)
1320 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001321
Rui Ueyama4e39f712014-03-18 18:58:51 +00001322 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1323 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +00001324
Rui Ueyama4e39f712014-03-18 18:58:51 +00001325 outs() << format(Fmt, Address) << " "
1326 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1327 << (Weak ? 'w' : ' ') // Weak?
1328 << ' ' // Constructor. Not supported yet.
1329 << ' ' // Warning. Not supported yet.
1330 << ' ' // Indirect reference to another symbol.
1331 << Debug // Debugging (d) or dynamic (D) symbol.
1332 << FileFunc // Name of function (F), file (f) or object (O).
1333 << ' ';
1334 if (Absolute) {
1335 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001336 } else if (Common) {
1337 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +00001338 } else if (Section == o->section_end()) {
1339 outs() << "*UND*";
1340 } else {
1341 if (const MachOObjectFile *MachO =
1342 dyn_cast<const MachOObjectFile>(o)) {
1343 DataRefImpl DR = Section->getRawDataRefImpl();
1344 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1345 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001346 }
Rui Ueyama4e39f712014-03-18 18:58:51 +00001347 StringRef SectionName;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001348 error(Section->getName(SectionName));
Rui Ueyama4e39f712014-03-18 18:58:51 +00001349 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001350 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001351
1352 outs() << '\t';
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001353 if (Common || isa<ELFObjectFileBase>(o)) {
Rafael Espindoladbb6bd32015-06-25 22:10:04 +00001354 uint64_t Val =
1355 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001356 outs() << format("\t %08" PRIx64 " ", Val);
1357 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001358
Davide Italianocd2514d2015-04-30 23:08:53 +00001359 if (Hidden) {
1360 outs() << ".hidden ";
1361 }
1362 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +00001363 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001364 }
1365}
1366
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001367static void PrintUnwindInfo(const ObjectFile *o) {
1368 outs() << "Unwind info:\n\n";
1369
1370 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1371 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +00001372 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1373 printMachOUnwindInfo(MachO);
1374 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001375 // TODO: Extract DWARF dump tool to objdump.
1376 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +00001377 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001378 return;
1379 }
1380}
1381
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001382void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +00001383 outs() << "Exports trie:\n";
1384 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1385 printMachOExportsTrie(MachO);
1386 else {
1387 errs() << "This operation is only currently supported "
1388 "for Mach-O executable files.\n";
1389 return;
1390 }
1391}
1392
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001393void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +00001394 outs() << "Rebase table:\n";
1395 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1396 printMachORebaseTable(MachO);
1397 else {
1398 errs() << "This operation is only currently supported "
1399 "for Mach-O executable files.\n";
1400 return;
1401 }
1402}
1403
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001404void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001405 outs() << "Bind table:\n";
1406 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1407 printMachOBindTable(MachO);
1408 else {
1409 errs() << "This operation is only currently supported "
1410 "for Mach-O executable files.\n";
1411 return;
1412 }
1413}
1414
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001415void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001416 outs() << "Lazy bind table:\n";
1417 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1418 printMachOLazyBindTable(MachO);
1419 else {
1420 errs() << "This operation is only currently supported "
1421 "for Mach-O executable files.\n";
1422 return;
1423 }
1424}
1425
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001426void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001427 outs() << "Weak bind table:\n";
1428 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1429 printMachOWeakBindTable(MachO);
1430 else {
1431 errs() << "This operation is only currently supported "
1432 "for Mach-O executable files.\n";
1433 return;
1434 }
1435}
Nick Kledzikac431442014-09-12 21:34:15 +00001436
Adrian Prantl437105a2015-07-08 02:04:15 +00001437/// Dump the raw contents of the __clangast section so the output can be piped
1438/// into llvm-bcanalyzer.
1439void llvm::printRawClangAST(const ObjectFile *Obj) {
1440 if (outs().is_displayed()) {
1441 errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1442 "the clang ast section.\n"
1443 "Please redirect the output to a file or another program such as "
1444 "llvm-bcanalyzer.\n";
1445 return;
1446 }
1447
1448 StringRef ClangASTSectionName("__clangast");
1449 if (isa<COFFObjectFile>(Obj)) {
1450 ClangASTSectionName = "clangast";
1451 }
1452
1453 Optional<object::SectionRef> ClangASTSection;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001454 for (auto Sec : ToolSectionFilter(*Obj)) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001455 StringRef Name;
1456 Sec.getName(Name);
1457 if (Name == ClangASTSectionName) {
1458 ClangASTSection = Sec;
1459 break;
1460 }
1461 }
1462 if (!ClangASTSection)
1463 return;
1464
1465 StringRef ClangASTContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001466 error(ClangASTSection.getValue().getContents(ClangASTContents));
Adrian Prantl437105a2015-07-08 02:04:15 +00001467 outs().write(ClangASTContents.data(), ClangASTContents.size());
1468}
1469
Sanjoy Das6f567a42015-06-22 18:03:02 +00001470static void printFaultMaps(const ObjectFile *Obj) {
1471 const char *FaultMapSectionName = nullptr;
1472
1473 if (isa<ELFObjectFileBase>(Obj)) {
1474 FaultMapSectionName = ".llvm_faultmaps";
1475 } else if (isa<MachOObjectFile>(Obj)) {
1476 FaultMapSectionName = "__llvm_faultmaps";
1477 } else {
1478 errs() << "This operation is only currently supported "
1479 "for ELF and Mach-O executable files.\n";
1480 return;
1481 }
1482
1483 Optional<object::SectionRef> FaultMapSection;
1484
Colin LeMahieu77804be2015-07-29 15:45:39 +00001485 for (auto Sec : ToolSectionFilter(*Obj)) {
Sanjoy Das6f567a42015-06-22 18:03:02 +00001486 StringRef Name;
1487 Sec.getName(Name);
1488 if (Name == FaultMapSectionName) {
1489 FaultMapSection = Sec;
1490 break;
1491 }
1492 }
1493
1494 outs() << "FaultMap table:\n";
1495
1496 if (!FaultMapSection.hasValue()) {
1497 outs() << "<not found>\n";
1498 return;
1499 }
1500
1501 StringRef FaultMapContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001502 error(FaultMapSection.getValue().getContents(FaultMapContents));
Sanjoy Das6f567a42015-06-22 18:03:02 +00001503
1504 FaultMapParser FMP(FaultMapContents.bytes_begin(),
1505 FaultMapContents.bytes_end());
1506
1507 outs() << FMP;
1508}
1509
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001510static void printPrivateFileHeaders(const ObjectFile *o) {
1511 if (o->isELF())
1512 printELFFileHeader(o);
1513 else if (o->isCOFF())
1514 printCOFFFileHeader(o);
1515 else if (o->isMachO()) {
1516 printMachOFileHeader(o);
1517 printMachOLoadCommands(o);
1518 } else
1519 report_fatal_error("Invalid/Unsupported object file format");
1520}
1521
1522static void printFirstPrivateFileHeader(const ObjectFile *o) {
Davide Italiano540e9212015-12-19 22:09:40 +00001523 if (o->isELF())
Rui Ueyamac2bed422013-09-27 21:04:00 +00001524 printELFFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001525 else if (o->isCOFF())
Rui Ueyamac2bed422013-09-27 21:04:00 +00001526 printCOFFFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001527 else if (o->isMachO())
Kevin Enderbyb76d3862014-08-22 20:35:18 +00001528 printMachOFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001529 else
1530 report_fatal_error("Invalid/Unsupported object file format");
Rui Ueyamac2bed422013-09-27 21:04:00 +00001531}
1532
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001533static void DumpObject(const ObjectFile *o) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001534 // Avoid other output when using a raw option.
1535 if (!RawClangAST) {
1536 outs() << '\n';
1537 outs() << o->getFileName()
1538 << ":\tfile format " << o->getFileFormatName() << "\n\n";
1539 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001540
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001541 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +00001542 DisassembleObject(o, Relocations);
1543 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001544 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001545 if (SectionHeaders)
1546 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001547 if (SectionContents)
1548 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001549 if (SymbolTable)
1550 PrintSymbolTable(o);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001551 if (UnwindInfo)
1552 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001553 if (PrivateHeaders)
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001554 printPrivateFileHeaders(o);
1555 if (FirstPrivateHeader)
1556 printFirstPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +00001557 if (ExportsTrie)
1558 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +00001559 if (Rebase)
1560 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +00001561 if (Bind)
1562 printBindTable(o);
1563 if (LazyBind)
1564 printLazyBindTable(o);
1565 if (WeakBind)
1566 printWeakBindTable(o);
Adrian Prantl437105a2015-07-08 02:04:15 +00001567 if (RawClangAST)
1568 printRawClangAST(o);
Sanjoy Das6f567a42015-06-22 18:03:02 +00001569 if (PrintFaultMaps)
1570 printFaultMaps(o);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001571 if (DwarfDumpType != DIDT_Null) {
1572 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*o));
1573 // Dump the complete DWARF structure.
1574 DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1575 }
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001576}
1577
1578/// @brief Dump each object file in \a a;
1579static void DumpArchive(const Archive *a) {
Kevin Enderby7a969422015-11-05 19:24:56 +00001580 for (auto &ErrorOrChild : a->children()) {
1581 if (std::error_code EC = ErrorOrChild.getError())
1582 report_error(a->getFileName(), EC);
1583 const Archive::Child &C = *ErrorOrChild;
Davide Italiano95dd3cb2015-08-03 21:46:32 +00001584 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
Davide Italianoccd53fe2015-08-05 07:18:31 +00001585 if (std::error_code EC = ChildOrErr.getError())
Mark Seaborneb03ac52014-01-25 00:32:01 +00001586 if (EC != object_error::invalid_file_type)
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001587 report_error(a->getFileName(), EC);
Rafael Espindolaae460022014-06-16 16:08:36 +00001588 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001589 DumpObject(o);
1590 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001591 report_error(a->getFileName(), object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001592 }
1593}
1594
1595/// @brief Open file and figure out how to dump it.
1596static void DumpInput(StringRef file) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001597
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001598 // If we are using the Mach-O specific object file parser, then let it parse
1599 // the file and process the command line options. So the -arch flags can
1600 // be used to select specific slices, etc.
1601 if (MachOOpt) {
1602 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001603 return;
1604 }
1605
1606 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +00001607 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001608 if (std::error_code EC = BinaryOrErr.getError())
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001609 report_error(file, EC);
Rafael Espindola48af1c22014-08-19 18:44:46 +00001610 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001611
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001612 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001613 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001614 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001615 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +00001616 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001617 report_error(file, object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001618}
1619
Michael J. Spencer2670c252011-01-20 06:39:06 +00001620int main(int argc, char **argv) {
1621 // Print a stack trace if we signal out.
1622 sys::PrintStackTraceOnErrorSignal();
1623 PrettyStackTraceProgram X(argc, argv);
1624 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1625
1626 // Initialize targets and assembly printers/parsers.
1627 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +00001628 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +00001629 llvm::InitializeAllDisassemblers();
1630
Pete Cooper28fb4fc2012-05-03 23:20:10 +00001631 // Register the target printer for --version.
1632 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1633
Michael J. Spencer2670c252011-01-20 06:39:06 +00001634 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1635 TripleName = Triple::normalize(TripleName);
1636
1637 ToolName = argv[0];
1638
1639 // Defaults to a.out if no filenames specified.
1640 if (InputFilenames.size() == 0)
1641 InputFilenames.push_back("a.out");
1642
Colin LeMahieuf34933e2015-07-23 20:58:49 +00001643 if (DisassembleAll)
1644 Disassemble = true;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001645 if (!Disassemble
1646 && !Relocations
1647 && !SectionHeaders
1648 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001649 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +00001650 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +00001651 && !PrivateHeaders
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001652 && !FirstPrivateHeader
Nick Kledzikac431442014-09-12 21:34:15 +00001653 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +00001654 && !Rebase
1655 && !Bind
1656 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +00001657 && !WeakBind
Adrian Prantl437105a2015-07-08 02:04:15 +00001658 && !RawClangAST
Kevin Enderby13023a12015-01-15 23:19:11 +00001659 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00001660 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +00001661 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +00001662 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00001663 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +00001664 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +00001665 && !(DylibsUsed && MachOOpt)
1666 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +00001667 && !(ObjcMetaData && MachOOpt)
Colin LeMahieufcc32762015-07-29 19:08:10 +00001668 && !(FilterSections.size() != 0 && MachOOpt)
Igor Laevsky03a670c2016-01-26 15:09:42 +00001669 && !PrintFaultMaps
1670 && DwarfDumpType == DIDT_Null) {
Michael J. Spencer2670c252011-01-20 06:39:06 +00001671 cl::PrintHelpMessage();
1672 return 2;
1673 }
1674
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001675 std::for_each(InputFilenames.begin(), InputFilenames.end(),
1676 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001677
Davide Italianoccd53fe2015-08-05 07:18:31 +00001678 return EXIT_SUCCESS;
Michael J. Spencer2670c252011-01-20 06:39:06 +00001679}