blob: 0cf42e3fec7bf29c0673d23c5c96ec3e9c6ee29c [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"
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +000026#include "llvm/DebugInfo/Symbolize/Symbolize.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000027#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000028#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000029#include "llvm/MC/MCDisassembler/MCDisassembler.h"
30#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000031#include "llvm/MC/MCInst.h"
32#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000033#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper54bfde72012-04-02 06:09:36 +000034#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000035#include "llvm/MC/MCObjectFileInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000036#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000037#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Object/Archive.h"
39#include "llvm/Object/COFF.h"
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +000040#include "llvm/Object/COFFImportFile.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000041#include "llvm/Object/ELFObjectFile.h"
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000042#include "llvm/Object/MachO.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000043#include "llvm/Object/ObjectFile.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000044#include "llvm/Support/Casting.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000045#include "llvm/Support/CommandLine.h"
46#include "llvm/Support/Debug.h"
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +000047#include "llvm/Support/Errc.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000048#include "llvm/Support/FileSystem.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000049#include "llvm/Support/Format.h"
Benjamin Kramerbf115312011-07-25 23:04:36 +000050#include "llvm/Support/GraphWriter.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000051#include "llvm/Support/Host.h"
52#include "llvm/Support/ManagedStatic.h"
53#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000054#include "llvm/Support/PrettyStackTrace.h"
55#include "llvm/Support/Signals.h"
56#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000057#include "llvm/Support/TargetRegistry.h"
58#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000059#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000060#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000061#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000062#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000063#include <system_error>
Benjamin Kramer82de7d32016-05-27 14:27:24 +000064#include <utility>
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +000065#include <unordered_map>
Ahmed Bougacha17926472013-08-21 07:29:02 +000066
Michael J. Spencer2670c252011-01-20 06:39:06 +000067using namespace llvm;
68using namespace object;
69
Benjamin Kramer43a772e2011-09-19 17:56:04 +000070static cl::list<std::string>
71InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000072
Kevin Enderbye2297dd2015-01-07 21:02:18 +000073cl::opt<bool>
74llvm::Disassemble("disassemble",
Benjamin Kramer43a772e2011-09-19 17:56:04 +000075 cl::desc("Display assembler mnemonics for the machine instructions"));
76static cl::alias
77Disassembled("d", cl::desc("Alias for --disassemble"),
Colin LeMahieu77804be2015-07-29 15:45:39 +000078 cl::aliasopt(Disassemble));
79
80cl::opt<bool>
81llvm::DisassembleAll("disassemble-all",
82 cl::desc("Display assembler mnemonics for the machine instructions"));
83static cl::alias
84DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
Colin LeMahieuf34933e2015-07-23 20:58:49 +000085 cl::aliasopt(DisassembleAll));
Michael J. Spencer2670c252011-01-20 06:39:06 +000086
Kevin Enderby98da6132015-01-20 21:47:46 +000087cl::opt<bool>
88llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
Michael J. Spencerba4a3622011-10-08 00:18:30 +000089
Kevin Enderby98da6132015-01-20 21:47:46 +000090cl::opt<bool>
91llvm::SectionContents("s", cl::desc("Display the content of each section"));
Michael J. Spencer4e25c022011-10-17 17:13:22 +000092
Kevin Enderby98da6132015-01-20 21:47:46 +000093cl::opt<bool>
94llvm::SymbolTable("t", cl::desc("Display the symbol table"));
Michael J. Spencerbfa06782011-10-18 19:32:17 +000095
Kevin Enderbye2297dd2015-01-07 21:02:18 +000096cl::opt<bool>
97llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
Nick Kledzikd04bc352014-08-30 00:20:14 +000098
Kevin Enderbye2297dd2015-01-07 21:02:18 +000099cl::opt<bool>
100llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
Nick Kledzikac431442014-09-12 21:34:15 +0000101
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000102cl::opt<bool>
103llvm::Bind("bind", cl::desc("Display mach-o binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000104
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000105cl::opt<bool>
106llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000107
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000108cl::opt<bool>
109llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000110
Adrian Prantl437105a2015-07-08 02:04:15 +0000111cl::opt<bool>
112llvm::RawClangAST("raw-clang-ast",
113 cl::desc("Dump the raw binary contents of the clang AST section"));
114
Nick Kledzik56ebef42014-09-16 01:41:51 +0000115static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000116MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000117static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000118MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000119
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000120cl::opt<std::string>
121llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
122 "see -version for available targets"));
123
124cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000125llvm::MCPU("mcpu",
126 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
127 cl::value_desc("cpu-name"),
128 cl::init(""));
129
130cl::opt<std::string>
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000131llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000132 "see -version for available targets"));
133
Kevin Enderby98da6132015-01-20 21:47:46 +0000134cl::opt<bool>
135llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
136 "headers for each section."));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000137static cl::alias
138SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
139 cl::aliasopt(SectionHeaders));
140static cl::alias
141SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
142 cl::aliasopt(SectionHeaders));
Colin LeMahieufcc32762015-07-29 19:08:10 +0000143
Colin LeMahieu77804be2015-07-29 15:45:39 +0000144cl::list<std::string>
Colin LeMahieufcc32762015-07-29 19:08:10 +0000145llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
146 "With -macho dump segment,section"));
147cl::alias
148static FilterSectionsj("j", cl::desc("Alias for --section"),
149 cl::aliasopt(llvm::FilterSections));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000150
Kevin Enderbyc9595622014-08-06 23:24:41 +0000151cl::list<std::string>
152llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000153 cl::CommaSeparated,
154 cl::desc("Target specific attributes"),
155 cl::value_desc("a1,+a2,-a3,..."));
156
Kevin Enderbybf246f52014-09-24 23:08:22 +0000157cl::opt<bool>
158llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
159 "instructions, do not print "
160 "the instruction bytes."));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000161
Kevin Enderby98da6132015-01-20 21:47:46 +0000162cl::opt<bool>
163llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000164
165static cl::alias
166UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
167 cl::aliasopt(UnwindInfo));
168
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000169cl::opt<bool>
170llvm::PrivateHeaders("private-headers",
171 cl::desc("Display format specific file headers"));
Michael J. Spencer209565db2013-01-06 03:56:49 +0000172
Kevin Enderby0ae163f2016-01-13 00:25:36 +0000173cl::opt<bool>
174llvm::FirstPrivateHeader("private-header",
175 cl::desc("Display only the first format specific file "
176 "header"));
177
Michael J. Spencer209565db2013-01-06 03:56:49 +0000178static cl::alias
179PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
180 cl::aliasopt(PrivateHeaders));
181
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000182cl::opt<bool>
183 llvm::PrintImmHex("print-imm-hex",
Colin LeMahieuefe37322016-04-08 18:15:37 +0000184 cl::desc("Use hex format for immediate values"));
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000185
Sanjoy Das6f567a42015-06-22 18:03:02 +0000186cl::opt<bool> PrintFaultMaps("fault-map-section",
187 cl::desc("Display contents of faultmap section"));
188
Igor Laevsky03a670c2016-01-26 15:09:42 +0000189cl::opt<DIDumpType> llvm::DwarfDumpType(
190 "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
191 cl::values(clEnumValN(DIDT_Frames, "frames", ".debug_frame"),
192 clEnumValEnd));
193
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000194cl::opt<bool> PrintSource(
195 "source",
196 cl::desc(
197 "Display source inlined with disassembly. Implies disassmble object"));
198
199cl::alias PrintSourceShort("S", cl::desc("Alias for -source"),
200 cl::aliasopt(PrintSource));
201
202cl::opt<bool> PrintLines("line-numbers",
203 cl::desc("Display source line numbers with "
204 "disassembly. Implies disassemble object"));
205
206cl::alias PrintLinesShort("l", cl::desc("Alias for -line-numbers"),
207 cl::aliasopt(PrintLines));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000208static StringRef ToolName;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000209
Colin LeMahieu77804be2015-07-29 15:45:39 +0000210namespace {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000211typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000212
213class SectionFilterIterator {
214public:
215 SectionFilterIterator(FilterPredicate P,
216 llvm::object::section_iterator const &I,
217 llvm::object::section_iterator const &E)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000218 : Predicate(std::move(P)), Iterator(I), End(E) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000219 ScanPredicate();
220 }
Benjamin Kramerac9257b2015-09-24 14:52:52 +0000221 const llvm::object::SectionRef &operator*() const { return *Iterator; }
Colin LeMahieu77804be2015-07-29 15:45:39 +0000222 SectionFilterIterator &operator++() {
223 ++Iterator;
224 ScanPredicate();
225 return *this;
226 }
227 bool operator!=(SectionFilterIterator const &Other) const {
228 return Iterator != Other.Iterator;
229 }
230
231private:
232 void ScanPredicate() {
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000233 while (Iterator != End && !Predicate(*Iterator)) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000234 ++Iterator;
235 }
236 }
237 FilterPredicate Predicate;
238 llvm::object::section_iterator Iterator;
239 llvm::object::section_iterator End;
240};
241
242class SectionFilter {
243public:
244 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000245 : Predicate(std::move(P)), Object(O) {}
Colin LeMahieu77804be2015-07-29 15:45:39 +0000246 SectionFilterIterator begin() {
247 return SectionFilterIterator(Predicate, Object.section_begin(),
248 Object.section_end());
249 }
250 SectionFilterIterator end() {
251 return SectionFilterIterator(Predicate, Object.section_end(),
252 Object.section_end());
253 }
254
255private:
256 FilterPredicate Predicate;
257 llvm::object::ObjectFile const &Object;
258};
259SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
David Majnemer42531262016-08-12 03:55:06 +0000260 return SectionFilter(
261 [](llvm::object::SectionRef const &S) {
262 if (FilterSections.empty())
263 return true;
264 llvm::StringRef String;
265 std::error_code error = S.getName(String);
266 if (error)
267 return false;
268 return is_contained(FilterSections, String);
269 },
270 O);
Colin LeMahieu77804be2015-07-29 15:45:39 +0000271}
272}
273
Davide Italianoccd53fe2015-08-05 07:18:31 +0000274void llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000275 if (!EC)
Davide Italianoccd53fe2015-08-05 07:18:31 +0000276 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000277
Davide Italiano140af642015-12-25 18:16:45 +0000278 errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
279 errs().flush();
Davide Italiano7f6c3012015-08-06 00:18:52 +0000280 exit(1);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000281}
282
Kevin Enderby42398052016-06-28 23:16:13 +0000283LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
284 errs() << ToolName << ": " << Message << ".\n";
285 errs().flush();
286 exit(1);
287}
288
Davide Italianoed9d95b2015-12-29 13:41:02 +0000289LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
290 std::error_code EC) {
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000291 assert(EC);
292 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
Davide Italianoccd53fe2015-08-05 07:18:31 +0000293 exit(1);
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000294}
295
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000296LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
297 llvm::Error E) {
298 assert(E);
299 std::string Buf;
300 raw_string_ostream OS(Buf);
301 logAllUnhandledErrors(std::move(E), OS, "");
302 OS.flush();
Kevin Enderbyb34e3a12016-05-05 17:43:35 +0000303 errs() << ToolName << ": '" << File << "': " << Buf;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000304 exit(1);
305}
306
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000307LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
308 StringRef FileName,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000309 llvm::Error E,
310 StringRef ArchitectureName) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000311 assert(E);
312 errs() << ToolName << ": ";
313 if (ArchiveName != "")
314 errs() << ArchiveName << "(" << FileName << ")";
315 else
316 errs() << FileName;
Kevin Enderby9acb1092016-05-31 20:35:34 +0000317 if (!ArchitectureName.empty())
318 errs() << " (for architecture " << ArchitectureName << ")";
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000319 std::string Buf;
320 raw_string_ostream OS(Buf);
321 logAllUnhandledErrors(std::move(E), OS, "");
322 OS.flush();
323 errs() << " " << Buf;
324 exit(1);
325}
326
327LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
328 const object::Archive::Child &C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000329 llvm::Error E,
330 StringRef ArchitectureName) {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000331 Expected<StringRef> NameOrErr = C.getName();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000332 // TODO: if we have a error getting the name then it would be nice to print
333 // the index of which archive member this is and or its offset in the
334 // archive instead of "???" as the name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000335 if (!NameOrErr) {
336 consumeError(NameOrErr.takeError());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000337 llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000338 } else
Kevin Enderby9acb1092016-05-31 20:35:34 +0000339 llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E),
340 ArchitectureName);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000341}
342
Craig Toppere6cb63e2014-04-25 04:24:47 +0000343static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000344 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000345 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000346 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000347 if (Obj) {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000348 TheTriple.setArch(Triple::ArchType(Obj->getArch()));
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000349 // TheTriple defaults to ELF, and COFF doesn't have an environment:
350 // the best we can do here is indicate that it is mach-o.
351 if (Obj->isMachO())
Saleem Abdulrasool35476332014-03-06 20:47:11 +0000352 TheTriple.setObjectFormat(Triple::MachO);
Saleem Abdulrasool98938f12014-04-17 06:17:23 +0000353
354 if (Obj->isCOFF()) {
355 const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
356 if (COFFObj->getArch() == Triple::thumb)
357 TheTriple.setTriple("thumbv7-windows");
358 }
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000359 }
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000360 } else
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000361 TheTriple.setTriple(Triple::normalize(TripleName));
Michael J. Spencer2670c252011-01-20 06:39:06 +0000362
363 // Get the target specific parser.
364 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000365 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
366 Error);
Davide Italianobb599e32015-12-03 22:13:40 +0000367 if (!TheTarget)
368 report_fatal_error("can't find target: " + Error);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000369
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000370 // Update the triple name and return the found target.
371 TripleName = TheTriple.getTriple();
372 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000373}
374
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000375bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindola704cd842015-07-06 15:47:43 +0000376 return a.getOffset() < b.getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +0000377}
378
Colin LeMahieufb76b002015-05-28 19:07:14 +0000379namespace {
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000380class SourcePrinter {
381protected:
382 DILineInfo OldLineInfo;
383 const ObjectFile *Obj;
384 std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
385 // File name to file contents of source
386 std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
387 // Mark the line endings of the cached source
388 std::unordered_map<std::string, std::vector<StringRef>> LineCache;
389
390private:
391 bool cacheSource(std::string File);
392
393public:
394 virtual ~SourcePrinter() {}
395 SourcePrinter() : Obj(nullptr), Symbolizer(nullptr) {}
396 SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) {
397 symbolize::LLVMSymbolizer::Options SymbolizerOpts(
398 DILineInfoSpecifier::FunctionNameKind::None, true, false, false,
399 DefaultArch);
400 Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
401 }
402 virtual void printSourceLine(raw_ostream &OS, uint64_t Address,
403 StringRef Delimiter = "; ");
404};
405
406bool SourcePrinter::cacheSource(std::string File) {
407 auto BufferOrError = MemoryBuffer::getFile(File);
408 if (!BufferOrError)
409 return false;
410 // Chomp the file to get lines
411 size_t BufferSize = (*BufferOrError)->getBufferSize();
412 const char *BufferStart = (*BufferOrError)->getBufferStart();
413 for (const char *Start = BufferStart, *End = BufferStart;
414 End < BufferStart + BufferSize; End++)
415 if (*End == '\n' || End == BufferStart + BufferSize - 1 ||
416 (*End == '\r' && *(End + 1) == '\n')) {
417 LineCache[File].push_back(StringRef(Start, End - Start));
418 if (*End == '\r')
419 End++;
420 Start = End + 1;
421 }
422 SourceCache[File] = std::move(*BufferOrError);
423 return true;
424}
425
426void SourcePrinter::printSourceLine(raw_ostream &OS, uint64_t Address,
427 StringRef Delimiter) {
428 if (!Symbolizer)
429 return;
430 DILineInfo LineInfo = DILineInfo();
431 auto ExpectecLineInfo =
432 Symbolizer->symbolizeCode(Obj->getFileName(), Address);
433 if (!ExpectecLineInfo)
434 consumeError(ExpectecLineInfo.takeError());
435 else
436 LineInfo = *ExpectecLineInfo;
437
438 if ((LineInfo.FileName == "<invalid>") || OldLineInfo.Line == LineInfo.Line ||
439 LineInfo.Line == 0)
440 return;
441
442 if (PrintLines)
443 OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line << "\n";
444 if (PrintSource) {
445 if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
446 if (!cacheSource(LineInfo.FileName))
447 return;
448 auto FileBuffer = SourceCache.find(LineInfo.FileName);
449 if (FileBuffer != SourceCache.end()) {
450 auto LineBuffer = LineCache.find(LineInfo.FileName);
451 if (LineBuffer != LineCache.end())
452 // Vector begins at 0, line numbers are non-zero
453 OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim()
454 << "\n";
455 }
456 }
457 OldLineInfo = LineInfo;
458}
459
Hemant Kulkarni5b60f632016-08-25 19:41:08 +0000460static bool isArmElf(const ObjectFile *Obj) {
461 return (Obj->isELF() &&
462 (Obj->getArch() == Triple::aarch64 ||
463 Obj->getArch() == Triple::aarch64_be ||
464 Obj->getArch() == Triple::arm || Obj->getArch() == Triple::armeb ||
465 Obj->getArch() == Triple::thumb ||
466 Obj->getArch() == Triple::thumbeb));
467}
468
Colin LeMahieufb76b002015-05-28 19:07:14 +0000469class PrettyPrinter {
470public:
Colin LeMahieu0b5890d2015-05-28 20:59:08 +0000471 virtual ~PrettyPrinter(){}
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000472 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
Colin LeMahieufb76b002015-05-28 19:07:14 +0000473 ArrayRef<uint8_t> Bytes, uint64_t Address,
474 raw_ostream &OS, StringRef Annot,
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000475 MCSubtargetInfo const &STI, SourcePrinter *SP) {
476 if (SP && (PrintSource || PrintLines))
477 SP->printSourceLine(OS, Address);
Colin LeMahieu307a83d2016-03-18 16:26:48 +0000478 OS << format("%8" PRIx64 ":", Address);
Colin LeMahieufb76b002015-05-28 19:07:14 +0000479 if (!NoShowRawInsn) {
Colin LeMahieu307a83d2016-03-18 16:26:48 +0000480 OS << "\t";
481 dumpBytes(Bytes, OS);
Colin LeMahieufb76b002015-05-28 19:07:14 +0000482 }
Colin LeMahieu307a83d2016-03-18 16:26:48 +0000483 if (MI)
484 IP.printInst(MI, OS, "", STI);
485 else
486 OS << " <unknown>";
Colin LeMahieufb76b002015-05-28 19:07:14 +0000487 }
488};
489PrettyPrinter PrettyPrinterInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000490class HexagonPrettyPrinter : public PrettyPrinter {
491public:
492 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
493 raw_ostream &OS) {
494 uint32_t opcode =
495 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
496 OS << format("%8" PRIx64 ":", Address);
497 if (!NoShowRawInsn) {
498 OS << "\t";
499 dumpBytes(Bytes.slice(0, 4), OS);
500 OS << format("%08" PRIx32, opcode);
501 }
502 }
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000503 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
504 uint64_t Address, raw_ostream &OS, StringRef Annot,
505 MCSubtargetInfo const &STI, SourcePrinter *SP) override {
Hemant Kulkarnie77a0a92016-08-18 21:50:13 +0000506 if (SP && (PrintSource || PrintLines))
507 SP->printSourceLine(OS, Address, "");
Colin LeMahieu307a83d2016-03-18 16:26:48 +0000508 if (!MI) {
509 printLead(Bytes, Address, OS);
510 OS << " <unknown>";
511 return;
512 }
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000513 std::string Buffer;
514 {
515 raw_string_ostream TempStream(Buffer);
516 IP.printInst(MI, TempStream, "", STI);
517 }
518 StringRef Contents(Buffer);
519 // Split off bundle attributes
520 auto PacketBundle = Contents.rsplit('\n');
521 // Split off first instruction from the rest
522 auto HeadTail = PacketBundle.first.split('\n');
523 auto Preamble = " { ";
524 auto Separator = "";
525 while(!HeadTail.first.empty()) {
526 OS << Separator;
527 Separator = "\n";
Hemant Kulkarnie77a0a92016-08-18 21:50:13 +0000528 if (SP && (PrintSource || PrintLines))
529 SP->printSourceLine(OS, Address, "");
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000530 printLead(Bytes, Address, OS);
531 OS << Preamble;
532 Preamble = " ";
533 StringRef Inst;
534 auto Duplex = HeadTail.first.split('\v');
535 if(!Duplex.second.empty()){
536 OS << Duplex.first;
537 OS << "; ";
538 Inst = Duplex.second;
539 }
540 else
541 Inst = HeadTail.first;
542 OS << Inst;
543 Bytes = Bytes.slice(4);
544 Address += 4;
545 HeadTail = HeadTail.second.split('\n');
546 }
547 OS << " } " << PacketBundle.second;
548 }
549};
550HexagonPrettyPrinter HexagonPrettyPrinterInst;
Valery Pykhtinde048052016-04-07 07:24:01 +0000551
552class AMDGCNPrettyPrinter : public PrettyPrinter {
553public:
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000554 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
555 uint64_t Address, raw_ostream &OS, StringRef Annot,
556 MCSubtargetInfo const &STI, SourcePrinter *SP) override {
Matt Arsenault87d80db2016-04-22 21:23:41 +0000557 if (!MI) {
558 OS << " <unknown>";
559 return;
560 }
561
Valery Pykhtinde048052016-04-07 07:24:01 +0000562 SmallString<40> InstStr;
563 raw_svector_ostream IS(InstStr);
564
565 IP.printInst(MI, IS, "", STI);
566
Valery Pykhtin8e79f5b2016-04-07 08:38:20 +0000567 OS << left_justify(IS.str(), 60) << format("// %012" PRIX64 ": ", Address);
Valery Pykhtinde048052016-04-07 07:24:01 +0000568 typedef support::ulittle32_t U32;
569 for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()),
570 Bytes.size() / sizeof(U32)))
571 // D should be explicitly casted to uint32_t here as it is passed
572 // by format to snprintf as vararg.
Valery Pykhtin8e79f5b2016-04-07 08:38:20 +0000573 OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D));
Valery Pykhtinde048052016-04-07 07:24:01 +0000574
575 if (!Annot.empty())
576 OS << "// " << Annot;
577 }
578};
579AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
580
Colin LeMahieu35436a22015-05-29 14:48:25 +0000581PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000582 switch(Triple.getArch()) {
583 default:
584 return PrettyPrinterInst;
585 case Triple::hexagon:
586 return HexagonPrettyPrinterInst;
Valery Pykhtinde048052016-04-07 07:24:01 +0000587 case Triple::amdgcn:
588 return AMDGCNPrettyPrinterInst;
Colin LeMahieu68d967d2015-05-29 14:44:13 +0000589 }
Colin LeMahieufb76b002015-05-28 19:07:14 +0000590}
591}
592
Rafael Espindola37070a52015-06-03 04:48:06 +0000593template <class ELFT>
Rafael Espindola37070a52015-06-03 04:48:06 +0000594static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000595 const RelocationRef &RelRef,
Rafael Espindola37070a52015-06-03 04:48:06 +0000596 SmallVectorImpl<char> &Result) {
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000597 DataRefImpl Rel = RelRef.getRawDataRefImpl();
598
Rafael Espindola37070a52015-06-03 04:48:06 +0000599 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
600 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000601 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
602
Rafael Espindola37070a52015-06-03 04:48:06 +0000603 const ELFFile<ELFT> &EF = *Obj->getELFFile();
604
Rafael Espindola6def3042015-07-01 12:56:27 +0000605 ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a);
606 if (std::error_code EC = SecOrErr.getError())
607 return EC;
608 const Elf_Shdr *Sec = *SecOrErr;
609 ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link);
610 if (std::error_code EC = SymTabOrErr.getError())
611 return EC;
612 const Elf_Shdr *SymTab = *SymTabOrErr;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000613 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
614 SymTab->sh_type == ELF::SHT_DYNSYM);
Rafael Espindola6def3042015-07-01 12:56:27 +0000615 ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link);
616 if (std::error_code EC = StrTabSec.getError())
617 return EC;
618 ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec);
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000619 if (std::error_code EC = StrTabOrErr.getError())
620 return EC;
621 StringRef StrTab = *StrTabOrErr;
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000622 uint8_t type = RelRef.getType();
Rafael Espindola37070a52015-06-03 04:48:06 +0000623 StringRef res;
624 int64_t addend = 0;
Rafael Espindola6def3042015-07-01 12:56:27 +0000625 switch (Sec->sh_type) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000626 default:
627 return object_error::parse_failed;
628 case ELF::SHT_REL: {
Rafael Espindola37070a52015-06-03 04:48:06 +0000629 // TODO: Read implicit addend from section data.
630 break;
631 }
632 case ELF::SHT_RELA: {
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000633 const Elf_Rela *ERela = Obj->getRela(Rel);
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000634 addend = ERela->r_addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000635 break;
636 }
637 }
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000638 symbol_iterator SI = RelRef.getSymbol();
639 const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
Rafael Espindola75d5b542015-06-03 05:14:22 +0000640 StringRef Target;
Rafael Espindola75d5b542015-06-03 05:14:22 +0000641 if (symb->getType() == ELF::STT_SECTION) {
Kevin Enderby7bd8d992016-05-02 20:28:12 +0000642 Expected<section_iterator> SymSI = SI->getSection();
643 if (!SymSI)
644 return errorToErrorCode(SymSI.takeError());
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000645 const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
646 ErrorOr<StringRef> SecName = EF.getSectionName(SymSec);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000647 if (std::error_code EC = SecName.getError())
648 return EC;
649 Target = *SecName;
650 } else {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000651 Expected<StringRef> SymName = symb->getName(StrTab);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000652 if (!SymName)
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000653 return errorToErrorCode(SymName.takeError());
Rafael Espindola75d5b542015-06-03 05:14:22 +0000654 Target = *SymName;
655 }
Rafael Espindola37070a52015-06-03 04:48:06 +0000656 switch (EF.getHeader()->e_machine) {
657 case ELF::EM_X86_64:
658 switch (type) {
659 case ELF::R_X86_64_PC8:
660 case ELF::R_X86_64_PC16:
661 case ELF::R_X86_64_PC32: {
662 std::string fmtbuf;
663 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000664 fmt << Target << (addend < 0 ? "" : "+") << addend << "-P";
Rafael Espindola37070a52015-06-03 04:48:06 +0000665 fmt.flush();
666 Result.append(fmtbuf.begin(), fmtbuf.end());
667 } break;
668 case ELF::R_X86_64_8:
669 case ELF::R_X86_64_16:
670 case ELF::R_X86_64_32:
671 case ELF::R_X86_64_32S:
672 case ELF::R_X86_64_64: {
673 std::string fmtbuf;
674 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000675 fmt << Target << (addend < 0 ? "" : "+") << addend;
Rafael Espindola37070a52015-06-03 04:48:06 +0000676 fmt.flush();
677 Result.append(fmtbuf.begin(), fmtbuf.end());
678 } break;
679 default:
680 res = "Unknown";
681 }
682 break;
Jacques Pienaarea9f25a2016-03-01 21:21:42 +0000683 case ELF::EM_LANAI:
Rafael Espindola37070a52015-06-03 04:48:06 +0000684 case ELF::EM_AARCH64: {
685 std::string fmtbuf;
686 raw_string_ostream fmt(fmtbuf);
Rafael Espindola75d5b542015-06-03 05:14:22 +0000687 fmt << Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000688 if (addend != 0)
689 fmt << (addend < 0 ? "" : "+") << addend;
690 fmt.flush();
691 Result.append(fmtbuf.begin(), fmtbuf.end());
692 break;
693 }
694 case ELF::EM_386:
Michael Kupersteina3b79dd2015-11-04 11:21:50 +0000695 case ELF::EM_IAMCU:
Rafael Espindola37070a52015-06-03 04:48:06 +0000696 case ELF::EM_ARM:
697 case ELF::EM_HEXAGON:
698 case ELF::EM_MIPS:
Alexei Starovoitovcfb51f52016-07-15 22:27:55 +0000699 case ELF::EM_BPF:
Rafael Espindola75d5b542015-06-03 05:14:22 +0000700 res = Target;
Rafael Espindola37070a52015-06-03 04:48:06 +0000701 break;
Dan Gohman46350172016-01-12 20:56:01 +0000702 case ELF::EM_WEBASSEMBLY:
703 switch (type) {
704 case ELF::R_WEBASSEMBLY_DATA: {
705 std::string fmtbuf;
706 raw_string_ostream fmt(fmtbuf);
707 fmt << Target << (addend < 0 ? "" : "+") << addend;
708 fmt.flush();
709 Result.append(fmtbuf.begin(), fmtbuf.end());
710 break;
711 }
712 case ELF::R_WEBASSEMBLY_FUNCTION:
713 res = Target;
714 break;
715 default:
716 res = "Unknown";
717 }
718 break;
Rafael Espindola37070a52015-06-03 04:48:06 +0000719 default:
720 res = "Unknown";
721 }
722 if (Result.empty())
723 Result.append(res.begin(), res.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000724 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000725}
726
727static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000728 const RelocationRef &Rel,
Rafael Espindola37070a52015-06-03 04:48:06 +0000729 SmallVectorImpl<char> &Result) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000730 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
731 return getRelocationValueString(ELF32LE, Rel, Result);
732 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
733 return getRelocationValueString(ELF64LE, Rel, Result);
734 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
735 return getRelocationValueString(ELF32BE, Rel, Result);
736 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
737 return getRelocationValueString(ELF64BE, Rel, Result);
738}
739
740static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
741 const RelocationRef &Rel,
742 SmallVectorImpl<char> &Result) {
743 symbol_iterator SymI = Rel.getSymbol();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000744 Expected<StringRef> SymNameOrErr = SymI->getName();
745 if (!SymNameOrErr)
746 return errorToErrorCode(SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000747 StringRef SymName = *SymNameOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000748 Result.append(SymName.begin(), SymName.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000749 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000750}
751
752static void printRelocationTargetName(const MachOObjectFile *O,
753 const MachO::any_relocation_info &RE,
754 raw_string_ostream &fmt) {
755 bool IsScattered = O->isRelocationScattered(RE);
756
757 // Target of a scattered relocation is an address. In the interest of
758 // generating pretty output, scan through the symbol table looking for a
759 // symbol that aligns with that address. If we find one, print it.
760 // Otherwise, we just print the hex address of the target.
761 if (IsScattered) {
762 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
763
764 for (const SymbolRef &Symbol : O->symbols()) {
765 std::error_code ec;
Kevin Enderby931cb652016-06-24 18:24:42 +0000766 Expected<uint64_t> Addr = Symbol.getAddress();
767 if (!Addr) {
768 std::string Buf;
769 raw_string_ostream OS(Buf);
770 logAllUnhandledErrors(Addr.takeError(), OS, "");
771 OS.flush();
772 report_fatal_error(Buf);
773 }
Rafael Espindolaed067c42015-07-03 18:19:00 +0000774 if (*Addr != Val)
Rafael Espindola37070a52015-06-03 04:48:06 +0000775 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000776 Expected<StringRef> Name = Symbol.getName();
777 if (!Name) {
778 std::string Buf;
779 raw_string_ostream OS(Buf);
780 logAllUnhandledErrors(Name.takeError(), OS, "");
781 OS.flush();
782 report_fatal_error(Buf);
783 }
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000784 fmt << *Name;
Rafael Espindola37070a52015-06-03 04:48:06 +0000785 return;
786 }
787
788 // If we couldn't find a symbol that this relocation refers to, try
789 // to find a section beginning instead.
Colin LeMahieu77804be2015-07-29 15:45:39 +0000790 for (const SectionRef &Section : ToolSectionFilter(*O)) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000791 std::error_code ec;
792
793 StringRef Name;
794 uint64_t Addr = Section.getAddress();
795 if (Addr != Val)
796 continue;
797 if ((ec = Section.getName(Name)))
798 report_fatal_error(ec.message());
799 fmt << Name;
800 return;
801 }
802
803 fmt << format("0x%x", Val);
804 return;
805 }
806
807 StringRef S;
808 bool isExtern = O->getPlainRelocationExternal(RE);
809 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
810
811 if (isExtern) {
812 symbol_iterator SI = O->symbol_begin();
813 advance(SI, Val);
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000814 Expected<StringRef> SOrErr = SI->getName();
815 error(errorToErrorCode(SOrErr.takeError()));
Davide Italianoccd53fe2015-08-05 07:18:31 +0000816 S = *SOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000817 } else {
818 section_iterator SI = O->section_begin();
819 // Adjust for the fact that sections are 1-indexed.
820 advance(SI, Val - 1);
821 SI->getName(S);
822 }
823
824 fmt << S;
825}
826
827static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
828 const RelocationRef &RelRef,
829 SmallVectorImpl<char> &Result) {
830 DataRefImpl Rel = RelRef.getRawDataRefImpl();
831 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
832
833 unsigned Arch = Obj->getArch();
834
835 std::string fmtbuf;
836 raw_string_ostream fmt(fmtbuf);
837 unsigned Type = Obj->getAnyRelocationType(RE);
838 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
839
840 // Determine any addends that should be displayed with the relocation.
841 // These require decoding the relocation type, which is triple-specific.
842
843 // X86_64 has entirely custom relocation types.
844 if (Arch == Triple::x86_64) {
845 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
846
847 switch (Type) {
848 case MachO::X86_64_RELOC_GOT_LOAD:
849 case MachO::X86_64_RELOC_GOT: {
850 printRelocationTargetName(Obj, RE, fmt);
851 fmt << "@GOT";
852 if (isPCRel)
853 fmt << "PCREL";
854 break;
855 }
856 case MachO::X86_64_RELOC_SUBTRACTOR: {
857 DataRefImpl RelNext = Rel;
858 Obj->moveRelocationNext(RelNext);
859 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
860
861 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
862 // X86_64_RELOC_UNSIGNED.
863 // NOTE: Scattered relocations don't exist on x86_64.
864 unsigned RType = Obj->getAnyRelocationType(RENext);
865 if (RType != MachO::X86_64_RELOC_UNSIGNED)
866 report_fatal_error("Expected X86_64_RELOC_UNSIGNED after "
867 "X86_64_RELOC_SUBTRACTOR.");
868
869 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
870 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
871 printRelocationTargetName(Obj, RENext, fmt);
872 fmt << "-";
873 printRelocationTargetName(Obj, RE, fmt);
874 break;
875 }
876 case MachO::X86_64_RELOC_TLV:
877 printRelocationTargetName(Obj, RE, fmt);
878 fmt << "@TLV";
879 if (isPCRel)
880 fmt << "P";
881 break;
882 case MachO::X86_64_RELOC_SIGNED_1:
883 printRelocationTargetName(Obj, RE, fmt);
884 fmt << "-1";
885 break;
886 case MachO::X86_64_RELOC_SIGNED_2:
887 printRelocationTargetName(Obj, RE, fmt);
888 fmt << "-2";
889 break;
890 case MachO::X86_64_RELOC_SIGNED_4:
891 printRelocationTargetName(Obj, RE, fmt);
892 fmt << "-4";
893 break;
894 default:
895 printRelocationTargetName(Obj, RE, fmt);
896 break;
897 }
898 // X86 and ARM share some relocation types in common.
899 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
900 Arch == Triple::ppc) {
901 // Generic relocation types...
902 switch (Type) {
903 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rui Ueyama7d099192015-06-09 15:20:42 +0000904 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000905 case MachO::GENERIC_RELOC_SECTDIFF: {
906 DataRefImpl RelNext = Rel;
907 Obj->moveRelocationNext(RelNext);
908 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
909
910 // X86 sect diff's must be followed by a relocation of type
911 // GENERIC_RELOC_PAIR.
912 unsigned RType = Obj->getAnyRelocationType(RENext);
913
914 if (RType != MachO::GENERIC_RELOC_PAIR)
915 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
916 "GENERIC_RELOC_SECTDIFF.");
917
918 printRelocationTargetName(Obj, RE, fmt);
919 fmt << "-";
920 printRelocationTargetName(Obj, RENext, fmt);
921 break;
922 }
923 }
924
925 if (Arch == Triple::x86 || Arch == Triple::ppc) {
926 switch (Type) {
927 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
928 DataRefImpl RelNext = Rel;
929 Obj->moveRelocationNext(RelNext);
930 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
931
932 // X86 sect diff's must be followed by a relocation of type
933 // GENERIC_RELOC_PAIR.
934 unsigned RType = Obj->getAnyRelocationType(RENext);
935 if (RType != MachO::GENERIC_RELOC_PAIR)
936 report_fatal_error("Expected GENERIC_RELOC_PAIR after "
937 "GENERIC_RELOC_LOCAL_SECTDIFF.");
938
939 printRelocationTargetName(Obj, RE, fmt);
940 fmt << "-";
941 printRelocationTargetName(Obj, RENext, fmt);
942 break;
943 }
944 case MachO::GENERIC_RELOC_TLV: {
945 printRelocationTargetName(Obj, RE, fmt);
946 fmt << "@TLV";
947 if (IsPCRel)
948 fmt << "P";
949 break;
950 }
951 default:
952 printRelocationTargetName(Obj, RE, fmt);
953 }
954 } else { // ARM-specific relocations
955 switch (Type) {
956 case MachO::ARM_RELOC_HALF:
957 case MachO::ARM_RELOC_HALF_SECTDIFF: {
958 // Half relocations steal a bit from the length field to encode
959 // whether this is an upper16 or a lower16 relocation.
960 bool isUpper = Obj->getAnyRelocationLength(RE) >> 1;
961
962 if (isUpper)
963 fmt << ":upper16:(";
964 else
965 fmt << ":lower16:(";
966 printRelocationTargetName(Obj, RE, fmt);
967
968 DataRefImpl RelNext = Rel;
969 Obj->moveRelocationNext(RelNext);
970 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
971
972 // ARM half relocs must be followed by a relocation of type
973 // ARM_RELOC_PAIR.
974 unsigned RType = Obj->getAnyRelocationType(RENext);
975 if (RType != MachO::ARM_RELOC_PAIR)
976 report_fatal_error("Expected ARM_RELOC_PAIR after "
977 "ARM_RELOC_HALF");
978
979 // NOTE: The half of the target virtual address is stashed in the
980 // address field of the secondary relocation, but we can't reverse
981 // engineer the constant offset from it without decoding the movw/movt
982 // instruction to find the other half in its immediate field.
983
984 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
985 // symbol/section pointer of the follow-on relocation.
986 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
987 fmt << "-";
988 printRelocationTargetName(Obj, RENext, fmt);
989 }
990
991 fmt << ")";
992 break;
993 }
994 default: { printRelocationTargetName(Obj, RE, fmt); }
995 }
996 }
997 } else
998 printRelocationTargetName(Obj, RE, fmt);
999
1000 fmt.flush();
1001 Result.append(fmtbuf.begin(), fmtbuf.end());
Rui Ueyama7d099192015-06-09 15:20:42 +00001002 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +00001003}
1004
1005static std::error_code getRelocationValueString(const RelocationRef &Rel,
1006 SmallVectorImpl<char> &Result) {
Rafael Espindola854038e2015-06-26 14:51:16 +00001007 const ObjectFile *Obj = Rel.getObject();
Rafael Espindola37070a52015-06-03 04:48:06 +00001008 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
1009 return getRelocationValueString(ELF, Rel, Result);
1010 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
1011 return getRelocationValueString(COFF, Rel, Result);
1012 auto *MachO = cast<MachOObjectFile>(Obj);
1013 return getRelocationValueString(MachO, Rel, Result);
1014}
1015
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001016/// @brief Indicates whether this relocation should hidden when listing
1017/// relocations, usually because it is the trailing part of a multipart
1018/// relocation that will be printed as part of the leading relocation.
1019static bool getHidden(RelocationRef RelRef) {
1020 const ObjectFile *Obj = RelRef.getObject();
1021 auto *MachO = dyn_cast<MachOObjectFile>(Obj);
1022 if (!MachO)
1023 return false;
1024
1025 unsigned Arch = MachO->getArch();
1026 DataRefImpl Rel = RelRef.getRawDataRefImpl();
1027 uint64_t Type = MachO->getRelocationType(Rel);
1028
1029 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
1030 // is always hidden.
1031 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
1032 if (Type == MachO::GENERIC_RELOC_PAIR)
1033 return true;
1034 } else if (Arch == Triple::x86_64) {
1035 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
1036 // an X86_64_RELOC_SUBTRACTOR.
1037 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
1038 DataRefImpl RelPrev = Rel;
1039 RelPrev.d.a--;
1040 uint64_t PrevType = MachO->getRelocationType(RelPrev);
1041 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
1042 return true;
1043 }
1044 }
1045
1046 return false;
1047}
1048
Sam Koltonc05d7782016-08-17 10:17:57 +00001049static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) {
1050 assert(Obj->isELF());
1051 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1052 return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1053 if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1054 return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1055 if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1056 return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1057 if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1058 return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1059 llvm_unreachable("Unsupported binary format");
1060}
1061
Michael J. Spencer51862b32011-10-13 22:17:18 +00001062static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Jim Grosbachaf9aec02012-08-07 17:53:14 +00001063 const Target *TheTarget = getTarget(Obj);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001064
Jack Carter551efd72012-08-28 19:24:49 +00001065 // Package up features to be passed to target/subtarget
Daniel Sanders1d148642016-06-16 09:17:03 +00001066 SubtargetFeatures Features = Obj->getFeatures();
Jack Carter551efd72012-08-28 19:24:49 +00001067 if (MAttrs.size()) {
Jack Carter551efd72012-08-28 19:24:49 +00001068 for (unsigned i = 0; i != MAttrs.size(); ++i)
1069 Features.AddFeature(MAttrs[i]);
Jack Carter551efd72012-08-28 19:24:49 +00001070 }
1071
Ahmed Charles56440fd2014-03-06 05:51:42 +00001072 std::unique_ptr<const MCRegisterInfo> MRI(
1073 TheTarget->createMCRegInfo(TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +00001074 if (!MRI)
1075 report_fatal_error("error: no register info for target " + TripleName);
Ahmed Bougacha0835ca12013-05-16 21:28:23 +00001076
1077 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00001078 std::unique_ptr<const MCAsmInfo> AsmInfo(
1079 TheTarget->createMCAsmInfo(*MRI, TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +00001080 if (!AsmInfo)
1081 report_fatal_error("error: no assembly info for target " + TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +00001082 std::unique_ptr<const MCSubtargetInfo> STI(
Daniel Sanders1d148642016-06-16 09:17:03 +00001083 TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
Davide Italiano711e4952015-12-17 01:59:50 +00001084 if (!STI)
1085 report_fatal_error("error: no subtarget info for target " + TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +00001086 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Davide Italiano711e4952015-12-17 01:59:50 +00001087 if (!MII)
1088 report_fatal_error("error: no instruction info for target " + TripleName);
Lang Hamesa1bc0f52014-04-15 04:40:56 +00001089 std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
1090 MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
1091
1092 std::unique_ptr<MCDisassembler> DisAsm(
1093 TheTarget->createMCDisassembler(*STI, Ctx));
Davide Italiano711e4952015-12-17 01:59:50 +00001094 if (!DisAsm)
1095 report_fatal_error("error: no disassembler for target " + TripleName);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001096
Ahmed Charles56440fd2014-03-06 05:51:42 +00001097 std::unique_ptr<const MCInstrAnalysis> MIA(
1098 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +00001099
Ahmed Bougacha0835ca12013-05-16 21:28:23 +00001100 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Daniel Sanders50f17232015-09-15 16:17:27 +00001101 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
1102 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Davide Italiano711e4952015-12-17 01:59:50 +00001103 if (!IP)
1104 report_fatal_error("error: no instruction printer for target " +
1105 TripleName);
Colin LeMahieu14ec76e2015-06-07 21:07:17 +00001106 IP->setPrintImmHex(PrintImmHex);
Colin LeMahieu35436a22015-05-29 14:48:25 +00001107 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +00001108
Greg Fitzgerald18432272014-03-20 22:55:15 +00001109 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
1110 "\t\t\t%08" PRIx64 ": ";
1111
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001112 SourcePrinter SP(Obj, TheTarget->getName());
1113
Mark Seaborn0929d3d2014-01-25 17:38:19 +00001114 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
1115 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +00001116 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +00001117 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001118 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001119 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +00001120 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +00001121 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +00001122 }
1123
David Majnemer81afca62015-07-07 22:06:59 +00001124 // Create a mapping from virtual address to symbol name. This is used to
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001125 // pretty print the symbols while disassembling.
Sam Koltonc05d7782016-08-17 10:17:57 +00001126 typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001127 std::map<SectionRef, SectionSymbolsTy> AllSymbols;
1128 for (const SymbolRef &Symbol : Obj->symbols()) {
Kevin Enderby931cb652016-06-24 18:24:42 +00001129 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
1130 error(errorToErrorCode(AddressOrErr.takeError()));
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001131 uint64_t Address = *AddressOrErr;
David Majnemer2603a8fa2015-07-09 18:11:40 +00001132
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001133 Expected<StringRef> Name = Symbol.getName();
1134 error(errorToErrorCode(Name.takeError()));
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001135 if (Name->empty())
1136 continue;
David Majnemer81afca62015-07-07 22:06:59 +00001137
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001138 Expected<section_iterator> SectionOrErr = Symbol.getSection();
1139 error(errorToErrorCode(SectionOrErr.takeError()));
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001140 section_iterator SecI = *SectionOrErr;
1141 if (SecI == Obj->section_end())
1142 continue;
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001143
Sam Koltonc05d7782016-08-17 10:17:57 +00001144 uint8_t SymbolType = ELF::STT_NOTYPE;
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001145 if (Obj->isELF())
Sam Koltonc05d7782016-08-17 10:17:57 +00001146 SymbolType = getElfSymbolType(Obj, Symbol);
David Majnemer81afca62015-07-07 22:06:59 +00001147
Sam Koltonc05d7782016-08-17 10:17:57 +00001148 AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
1149
David Majnemer81afca62015-07-07 22:06:59 +00001150 }
1151
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001152 // Create a mapping from virtual address to section.
1153 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1154 for (SectionRef Sec : Obj->sections())
1155 SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1156 array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
1157
1158 // Linked executables (.exe and .dll files) typically don't include a real
1159 // symbol table but they might contain an export table.
1160 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
1161 for (const auto &ExportEntry : COFFObj->export_directories()) {
1162 StringRef Name;
1163 error(ExportEntry.getSymbolName(Name));
1164 if (Name.empty())
1165 continue;
1166 uint32_t RVA;
1167 error(ExportEntry.getExportRVA(RVA));
1168
1169 uint64_t VA = COFFObj->getImageBase() + RVA;
1170 auto Sec = std::upper_bound(
1171 SectionAddresses.begin(), SectionAddresses.end(), VA,
1172 [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
1173 return LHS < RHS.first;
1174 });
1175 if (Sec != SectionAddresses.begin())
1176 --Sec;
1177 else
1178 Sec = SectionAddresses.end();
1179
1180 if (Sec != SectionAddresses.end())
Sam Koltonc05d7782016-08-17 10:17:57 +00001181 AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001182 }
1183 }
1184
1185 // Sort all the symbols, this allows us to use a simple binary search to find
1186 // a symbol near an address.
1187 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1188 array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
1189
Colin LeMahieu77804be2015-07-29 15:45:39 +00001190 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Colin LeMahieuf34933e2015-07-23 20:58:49 +00001191 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
Mark Seaborneb03ac52014-01-25 00:32:01 +00001192 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +00001193
Rafael Espindola80291272014-10-08 15:28:58 +00001194 uint64_t SectionAddr = Section.getAddress();
1195 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +00001196 if (!SectSize)
1197 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +00001198
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001199 // Get the list of all the symbols in this section.
1200 SectionSymbolsTy &Symbols = AllSymbols[Section];
Davide Italianof0706882015-10-01 21:57:09 +00001201 std::vector<uint64_t> DataMappingSymsAddr;
1202 std::vector<uint64_t> TextMappingSymsAddr;
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001203 if (isArmElf(Obj)) {
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001204 for (const auto &Symb : Symbols) {
Sam Koltonc05d7782016-08-17 10:17:57 +00001205 uint64_t Address = std::get<0>(Symb);
1206 StringRef Name = std::get<1>(Symb);
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001207 if (Name.startswith("$d"))
David Majnemer153722d2015-11-18 04:35:32 +00001208 DataMappingSymsAddr.push_back(Address - SectionAddr);
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001209 if (Name.startswith("$x"))
David Majnemer153722d2015-11-18 04:35:32 +00001210 TextMappingSymsAddr.push_back(Address - SectionAddr);
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001211 if (Name.startswith("$a"))
1212 TextMappingSymsAddr.push_back(Address - SectionAddr);
1213 if (Name.startswith("$t"))
1214 TextMappingSymsAddr.push_back(Address - SectionAddr);
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001215 }
1216 }
1217
Davide Italianof0706882015-10-01 21:57:09 +00001218 std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
1219 std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001220
Michael J. Spencer51862b32011-10-13 22:17:18 +00001221 // Make a list of all the relocations for this section.
1222 std::vector<RelocationRef> Rels;
1223 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001224 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1225 for (const RelocationRef &Reloc : RelocSec.relocations()) {
1226 Rels.push_back(Reloc);
1227 }
Michael J. Spencer51862b32011-10-13 22:17:18 +00001228 }
1229 }
1230
1231 // Sort relocations by address.
1232 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
1233
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001234 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +00001235 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001236 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001237 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001238 }
Michael J. Spencer1d6167f2011-06-25 17:55:23 +00001239 StringRef name;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001240 error(Section.getName(name));
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001241 outs() << "Disassembly of section ";
1242 if (!SegmentName.empty())
1243 outs() << SegmentName << ",";
1244 outs() << name << ':';
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001245
Rafael Espindola7884c952015-06-04 15:01:05 +00001246 // If the section has no symbol at the start, just insert a dummy one.
Sam Koltonc05d7782016-08-17 10:17:57 +00001247 if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) {
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001248 Symbols.insert(Symbols.begin(),
1249 std::make_tuple(SectionAddr, name, Section.isText()
1250 ? ELF::STT_FUNC
1251 : ELF::STT_OBJECT));
Sam Koltonc05d7782016-08-17 10:17:57 +00001252 }
Alp Tokere69170a2014-06-26 22:52:05 +00001253
1254 SmallString<40> Comments;
1255 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001256
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001257 StringRef BytesStr;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001258 error(Section.getContents(BytesStr));
Aaron Ballman106fd7b2014-11-12 14:01:17 +00001259 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1260 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001261
Michael J. Spencer2670c252011-01-20 06:39:06 +00001262 uint64_t Size;
1263 uint64_t Index;
1264
Michael J. Spencer51862b32011-10-13 22:17:18 +00001265 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1266 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001267 // Disassemble symbol by symbol.
1268 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Sam Koltonc05d7782016-08-17 10:17:57 +00001269 uint64_t Start = std::get<0>(Symbols[si]) - SectionAddr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001270 // The end is either the section end or the beginning of the next
1271 // symbol.
1272 uint64_t End =
Sam Koltonc05d7782016-08-17 10:17:57 +00001273 (si == se - 1) ? SectSize : std::get<0>(Symbols[si + 1]) - SectionAddr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001274 // Don't try to disassemble beyond the end of section contents.
1275 if (End > SectSize)
1276 End = SectSize;
Rafael Espindolae45c7402014-08-17 16:31:39 +00001277 // If this symbol has the same address as the next symbol, then skip it.
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001278 if (Start >= End)
Michael J. Spenceree84f642011-10-13 20:37:08 +00001279 continue;
1280
Valery Pykhtinde048052016-04-07 07:24:01 +00001281 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1282 // make size 4 bytes folded
1283 End = Start + ((End - Start) & ~0x3ull);
Sam Koltonc05d7782016-08-17 10:17:57 +00001284 if (std::get<2>(Symbols[si]) == ELF::STT_AMDGPU_HSA_KERNEL) {
1285 // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes)
1286 Start += 256;
1287 }
1288 if (si == se - 1 ||
1289 std::get<2>(Symbols[si + 1]) == ELF::STT_AMDGPU_HSA_KERNEL) {
1290 // cut trailing zeroes at the end of kernel
1291 // cut up to 256 bytes
1292 const uint64_t EndAlign = 256;
1293 const auto Limit = End - (std::min)(EndAlign, End - Start);
1294 while (End > Limit &&
1295 *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
1296 End -= 4;
1297 }
Valery Pykhtinde048052016-04-07 07:24:01 +00001298 }
1299
Sam Koltonc05d7782016-08-17 10:17:57 +00001300 outs() << '\n' << std::get<1>(Symbols[si]) << ":\n";
Michael J. Spencer2670c252011-01-20 06:39:06 +00001301
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001302#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +00001303 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001304#else
Mark Seaborneb03ac52014-01-25 00:32:01 +00001305 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001306#endif
1307
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001308 for (Index = Start; Index < End; Index += Size) {
1309 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +00001310
Davide Italianof0706882015-10-01 21:57:09 +00001311 // AArch64 ELF binaries can interleave data and text in the
1312 // same section. We rely on the markers introduced to
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001313 // understand what we need to dump. If the data marker is within a
1314 // function, it is denoted as a word/short etc
1315 if (isArmElf(Obj) && std::get<2>(Symbols[si]) != ELF::STT_OBJECT &&
1316 !DisassembleAll) {
Davide Italianof0706882015-10-01 21:57:09 +00001317 uint64_t Stride = 0;
1318
1319 auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1320 DataMappingSymsAddr.end(), Index);
1321 if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1322 // Switch to data.
1323 while (Index < End) {
1324 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1325 outs() << "\t";
1326 if (Index + 4 <= End) {
1327 Stride = 4;
1328 dumpBytes(Bytes.slice(Index, 4), outs());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001329 outs() << "\t.word\t";
1330 uint32_t Data = 0;
1331 if (Obj->isLittleEndian()) {
1332 const auto Word =
1333 reinterpret_cast<const support::ulittle32_t *>(
1334 Bytes.data() + Index);
1335 Data = *Word;
1336 } else {
1337 const auto Word = reinterpret_cast<const support::ubig32_t *>(
1338 Bytes.data() + Index);
1339 Data = *Word;
1340 }
1341 outs() << "0x" << format("%08" PRIx32, Data);
Davide Italianof0706882015-10-01 21:57:09 +00001342 } else if (Index + 2 <= End) {
1343 Stride = 2;
1344 dumpBytes(Bytes.slice(Index, 2), outs());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001345 outs() << "\t\t.short\t";
1346 uint16_t Data = 0;
1347 if (Obj->isLittleEndian()) {
1348 const auto Short =
1349 reinterpret_cast<const support::ulittle16_t *>(
1350 Bytes.data() + Index);
1351 Data = *Short;
1352 } else {
1353 const auto Short =
1354 reinterpret_cast<const support::ubig16_t *>(Bytes.data() +
1355 Index);
1356 Data = *Short;
1357 }
1358 outs() << "0x" << format("%04" PRIx16, Data);
Davide Italianof0706882015-10-01 21:57:09 +00001359 } else {
1360 Stride = 1;
1361 dumpBytes(Bytes.slice(Index, 1), outs());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001362 outs() << "\t\t.byte\t";
1363 outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]);
Davide Italianof0706882015-10-01 21:57:09 +00001364 }
1365 Index += Stride;
1366 outs() << "\n";
1367 auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1368 TextMappingSymsAddr.end(), Index);
1369 if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1370 break;
1371 }
1372 }
1373 }
1374
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001375 // If there is a data symbol inside an ELF text section and we are only
1376 // disassembling text (applicable all architectures),
1377 // we are in a situation where we must print the data and not
1378 // disassemble it.
1379 if (Obj->isELF() && std::get<2>(Symbols[si]) == ELF::STT_OBJECT &&
1380 !DisassembleAll && Section.isText()) {
1381 // print out data up to 8 bytes at a time in hex and ascii
1382 uint8_t AsciiData[9] = {'\0'};
1383 uint8_t Byte;
1384 int NumBytes = 0;
1385
1386 for (Index = Start; Index < End; Index += 1) {
1387 if (NumBytes == 0) {
1388 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1389 outs() << "\t";
1390 }
1391 Byte = Bytes.slice(Index)[0];
1392 outs() << format(" %02x", Byte);
1393 AsciiData[NumBytes] = isprint(Byte) ? Byte : '.';
1394
1395 uint8_t IndentOffset = 0;
1396 NumBytes++;
1397 if (Index == End - 1 || NumBytes > 8) {
1398 // Indent the space for less than 8 bytes data.
1399 // 2 spaces for byte and one for space between bytes
1400 IndentOffset = 3 * (8 - NumBytes);
1401 for (int Excess = 8 - NumBytes; Excess < 8; Excess++)
1402 AsciiData[Excess] = '\0';
1403 NumBytes = 8;
1404 }
1405 if (NumBytes == 8) {
1406 AsciiData[8] = '\0';
1407 outs() << std::string(IndentOffset, ' ') << " ";
1408 outs() << reinterpret_cast<char *>(AsciiData);
1409 outs() << '\n';
1410 NumBytes = 0;
1411 }
1412 }
1413 }
Davide Italianof0706882015-10-01 21:57:09 +00001414 if (Index >= End)
1415 break;
1416
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001417 // Disassemble a real instruction or a data when disassemble all is
1418 // provided
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001419 bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1420 SectionAddr + Index, DebugOut,
1421 CommentStream);
1422 if (Size == 0)
1423 Size = 1;
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001424
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001425 PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001426 Bytes.slice(Index, Size), SectionAddr + Index, outs(), "",
1427 *STI, &SP);
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001428 outs() << CommentStream.str();
1429 Comments.clear();
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001430
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001431 // Try to resolve the target of a call, tail call, etc. to a specific
1432 // symbol.
1433 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1434 MIA->isConditionalBranch(Inst))) {
1435 uint64_t Target;
1436 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1437 // In a relocatable object, the target's section must reside in
1438 // the same section as the call instruction or it is accessed
1439 // through a relocation.
1440 //
1441 // In a non-relocatable object, the target may be in any section.
1442 //
1443 // N.B. We don't walk the relocations in the relocatable case yet.
1444 auto *TargetSectionSymbols = &Symbols;
1445 if (!Obj->isRelocatableObject()) {
1446 auto SectionAddress = std::upper_bound(
1447 SectionAddresses.begin(), SectionAddresses.end(), Target,
1448 [](uint64_t LHS,
1449 const std::pair<uint64_t, SectionRef> &RHS) {
1450 return LHS < RHS.first;
1451 });
1452 if (SectionAddress != SectionAddresses.begin()) {
1453 --SectionAddress;
1454 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1455 } else {
1456 TargetSectionSymbols = nullptr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001457 }
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001458 }
David Majnemer2603a8fa2015-07-09 18:11:40 +00001459
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001460 // Find the first symbol in the section whose offset is less than
1461 // or equal to the target.
1462 if (TargetSectionSymbols) {
1463 auto TargetSym = std::upper_bound(
1464 TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1465 Target, [](uint64_t LHS,
Sam Koltonc05d7782016-08-17 10:17:57 +00001466 const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
1467 return LHS < std::get<0>(RHS);
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001468 });
1469 if (TargetSym != TargetSectionSymbols->begin()) {
1470 --TargetSym;
1471 uint64_t TargetAddress = std::get<0>(*TargetSym);
1472 StringRef TargetName = std::get<1>(*TargetSym);
1473 outs() << " <" << TargetName;
1474 uint64_t Disp = Target - TargetAddress;
1475 if (Disp)
1476 outs() << "+0x" << utohexstr(Disp);
1477 outs() << '>';
David Majnemer81afca62015-07-07 22:06:59 +00001478 }
1479 }
1480 }
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001481 }
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001482 outs() << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +00001483
1484 // Print relocation for instruction.
1485 while (rel_cur != rel_end) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001486 bool hidden = getHidden(*rel_cur);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001487 uint64_t addr = rel_cur->getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +00001488 SmallString<16> name;
1489 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +00001490
1491 // If this relocation is hidden, skip it.
Owen Andersonfa3e5202011-10-25 20:35:53 +00001492 if (hidden) goto skip_print_rel;
1493
Michael J. Spencer51862b32011-10-13 22:17:18 +00001494 // Stop when rel_cur's address is past the current instruction.
Owen Andersonf20e3e52011-10-25 20:15:39 +00001495 if (addr >= Index + Size) break;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001496 rel_cur->getTypeName(name);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001497 error(getRelocationValueString(*rel_cur, val));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001498 outs() << format(Fmt.data(), SectionAddr + addr) << name
Benjamin Kramer82803112012-03-10 02:04:38 +00001499 << "\t" << val << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +00001500
1501 skip_print_rel:
1502 ++rel_cur;
1503 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +00001504 }
Michael J. Spencer2670c252011-01-20 06:39:06 +00001505 }
1506 }
1507}
1508
Kevin Enderby98da6132015-01-20 21:47:46 +00001509void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +00001510 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1511 "%08" PRIx64;
Rafael Espindola9219fe72016-03-21 20:59:15 +00001512 // Regular objdump doesn't print relocations in non-relocatable object
1513 // files.
1514 if (!Obj->isRelocatableObject())
1515 return;
Rafael Espindolac66d7612014-08-17 19:09:37 +00001516
Colin LeMahieu77804be2015-07-29 15:45:39 +00001517 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001518 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001519 continue;
1520 StringRef secname;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001521 error(Section.getName(secname));
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001522 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001523 for (const RelocationRef &Reloc : Section.relocations()) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001524 bool hidden = getHidden(Reloc);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001525 uint64_t address = Reloc.getOffset();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001526 SmallString<32> relocname;
1527 SmallString<32> valuestr;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001528 if (hidden)
1529 continue;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001530 Reloc.getTypeName(relocname);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001531 error(getRelocationValueString(Reloc, valuestr));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001532 outs() << format(Fmt.data(), address) << " " << relocname << " "
1533 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001534 }
1535 outs() << "\n";
1536 }
1537}
1538
Kevin Enderby98da6132015-01-20 21:47:46 +00001539void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001540 outs() << "Sections:\n"
1541 "Idx Name Size Address Type\n";
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001542 unsigned i = 0;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001543 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001544 StringRef Name;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001545 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001546 uint64_t Address = Section.getAddress();
1547 uint64_t Size = Section.getSize();
1548 bool Text = Section.isText();
1549 bool Data = Section.isData();
1550 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001551 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +00001552 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
Alexey Samsonov48803e52014-03-13 14:37:36 +00001553 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i,
1554 Name.str().c_str(), Size, Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001555 ++i;
1556 }
1557}
1558
Kevin Enderby98da6132015-01-20 21:47:46 +00001559void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +00001560 std::error_code EC;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001561 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001562 StringRef Name;
1563 StringRef Contents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001564 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001565 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +00001566 uint64_t Size = Section.getSize();
1567 if (!Size)
1568 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001569
1570 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +00001571 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +00001572 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +00001573 ", %04" PRIx64 ")>\n",
1574 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +00001575 continue;
1576 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001577
Davide Italianoccd53fe2015-08-05 07:18:31 +00001578 error(Section.getContents(Contents));
David Majnemer8f6b04c2014-07-14 16:20:14 +00001579
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001580 // Dump out the content as hex and printable ascii characters.
1581 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +00001582 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001583 // Dump line of hex.
1584 for (std::size_t i = 0; i < 16; ++i) {
1585 if (i != 0 && i % 4 == 0)
1586 outs() << ' ';
1587 if (addr + i < end)
1588 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1589 << hexdigit(Contents[addr + i] & 0xF, true);
1590 else
1591 outs() << " ";
1592 }
1593 // Print ascii.
1594 outs() << " ";
1595 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001596 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001597 outs() << Contents[addr + i];
1598 else
1599 outs() << ".";
1600 }
1601 outs() << "\n";
1602 }
1603 }
1604}
1605
Kevin Enderby9acb1092016-05-31 20:35:34 +00001606void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
1607 StringRef ArchitectureName) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001608 outs() << "SYMBOL TABLE:\n";
1609
Rui Ueyama4e39f712014-03-18 18:58:51 +00001610 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Davide Italianoe85abf72015-12-20 09:54:34 +00001611 printCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +00001612 return;
1613 }
1614 for (const SymbolRef &Symbol : o->symbols()) {
Kevin Enderby931cb652016-06-24 18:24:42 +00001615 Expected<uint64_t> AddressOrError = Symbol.getAddress();
1616 if (!AddressOrError)
1617 report_error(ArchiveName, o->getFileName(), AddressOrError.takeError());
Rafael Espindolaed067c42015-07-03 18:19:00 +00001618 uint64_t Address = *AddressOrError;
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001619 Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
1620 if (!TypeOrError)
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001621 report_error(ArchiveName, o->getFileName(), TypeOrError.takeError());
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001622 SymbolRef::Type Type = *TypeOrError;
Rui Ueyama4e39f712014-03-18 18:58:51 +00001623 uint32_t Flags = Symbol.getFlags();
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001624 Expected<section_iterator> SectionOrErr = Symbol.getSection();
1625 error(errorToErrorCode(SectionOrErr.takeError()));
Rafael Espindola8bab8892015-08-07 23:27:14 +00001626 section_iterator Section = *SectionOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001627 StringRef Name;
1628 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1629 Section->getName(Name);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001630 } else {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001631 Expected<StringRef> NameOrErr = Symbol.getName();
1632 if (!NameOrErr)
Kevin Enderby9acb1092016-05-31 20:35:34 +00001633 report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(),
1634 ArchitectureName);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001635 Name = *NameOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001636 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001637
Rui Ueyama4e39f712014-03-18 18:58:51 +00001638 bool Global = Flags & SymbolRef::SF_Global;
1639 bool Weak = Flags & SymbolRef::SF_Weak;
1640 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001641 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +00001642 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +00001643
Rui Ueyama4e39f712014-03-18 18:58:51 +00001644 char GlobLoc = ' ';
1645 if (Type != SymbolRef::ST_Unknown)
1646 GlobLoc = Global ? 'g' : 'l';
1647 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1648 ? 'd' : ' ';
1649 char FileFunc = ' ';
1650 if (Type == SymbolRef::ST_File)
1651 FileFunc = 'f';
1652 else if (Type == SymbolRef::ST_Function)
1653 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001654
Rui Ueyama4e39f712014-03-18 18:58:51 +00001655 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1656 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +00001657
Rui Ueyama4e39f712014-03-18 18:58:51 +00001658 outs() << format(Fmt, Address) << " "
1659 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1660 << (Weak ? 'w' : ' ') // Weak?
1661 << ' ' // Constructor. Not supported yet.
1662 << ' ' // Warning. Not supported yet.
1663 << ' ' // Indirect reference to another symbol.
1664 << Debug // Debugging (d) or dynamic (D) symbol.
1665 << FileFunc // Name of function (F), file (f) or object (O).
1666 << ' ';
1667 if (Absolute) {
1668 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001669 } else if (Common) {
1670 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +00001671 } else if (Section == o->section_end()) {
1672 outs() << "*UND*";
1673 } else {
1674 if (const MachOObjectFile *MachO =
1675 dyn_cast<const MachOObjectFile>(o)) {
1676 DataRefImpl DR = Section->getRawDataRefImpl();
1677 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1678 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001679 }
Rui Ueyama4e39f712014-03-18 18:58:51 +00001680 StringRef SectionName;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001681 error(Section->getName(SectionName));
Rui Ueyama4e39f712014-03-18 18:58:51 +00001682 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001683 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001684
1685 outs() << '\t';
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001686 if (Common || isa<ELFObjectFileBase>(o)) {
Rafael Espindoladbb6bd32015-06-25 22:10:04 +00001687 uint64_t Val =
1688 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001689 outs() << format("\t %08" PRIx64 " ", Val);
1690 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001691
Davide Italianocd2514d2015-04-30 23:08:53 +00001692 if (Hidden) {
1693 outs() << ".hidden ";
1694 }
1695 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +00001696 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001697 }
1698}
1699
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001700static void PrintUnwindInfo(const ObjectFile *o) {
1701 outs() << "Unwind info:\n\n";
1702
1703 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
1704 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +00001705 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1706 printMachOUnwindInfo(MachO);
1707 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001708 // TODO: Extract DWARF dump tool to objdump.
1709 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +00001710 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001711 return;
1712 }
1713}
1714
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001715void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +00001716 outs() << "Exports trie:\n";
1717 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1718 printMachOExportsTrie(MachO);
1719 else {
1720 errs() << "This operation is only currently supported "
1721 "for Mach-O executable files.\n";
1722 return;
1723 }
1724}
1725
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001726void llvm::printRebaseTable(const ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +00001727 outs() << "Rebase table:\n";
1728 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1729 printMachORebaseTable(MachO);
1730 else {
1731 errs() << "This operation is only currently supported "
1732 "for Mach-O executable files.\n";
1733 return;
1734 }
1735}
1736
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001737void llvm::printBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001738 outs() << "Bind table:\n";
1739 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1740 printMachOBindTable(MachO);
1741 else {
1742 errs() << "This operation is only currently supported "
1743 "for Mach-O executable files.\n";
1744 return;
1745 }
1746}
1747
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001748void llvm::printLazyBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001749 outs() << "Lazy bind table:\n";
1750 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1751 printMachOLazyBindTable(MachO);
1752 else {
1753 errs() << "This operation is only currently supported "
1754 "for Mach-O executable files.\n";
1755 return;
1756 }
1757}
1758
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001759void llvm::printWeakBindTable(const ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00001760 outs() << "Weak bind table:\n";
1761 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
1762 printMachOWeakBindTable(MachO);
1763 else {
1764 errs() << "This operation is only currently supported "
1765 "for Mach-O executable files.\n";
1766 return;
1767 }
1768}
Nick Kledzikac431442014-09-12 21:34:15 +00001769
Adrian Prantl437105a2015-07-08 02:04:15 +00001770/// Dump the raw contents of the __clangast section so the output can be piped
1771/// into llvm-bcanalyzer.
1772void llvm::printRawClangAST(const ObjectFile *Obj) {
1773 if (outs().is_displayed()) {
1774 errs() << "The -raw-clang-ast option will dump the raw binary contents of "
1775 "the clang ast section.\n"
1776 "Please redirect the output to a file or another program such as "
1777 "llvm-bcanalyzer.\n";
1778 return;
1779 }
1780
1781 StringRef ClangASTSectionName("__clangast");
1782 if (isa<COFFObjectFile>(Obj)) {
1783 ClangASTSectionName = "clangast";
1784 }
1785
1786 Optional<object::SectionRef> ClangASTSection;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001787 for (auto Sec : ToolSectionFilter(*Obj)) {
Adrian Prantl437105a2015-07-08 02:04:15 +00001788 StringRef Name;
1789 Sec.getName(Name);
1790 if (Name == ClangASTSectionName) {
1791 ClangASTSection = Sec;
1792 break;
1793 }
1794 }
1795 if (!ClangASTSection)
1796 return;
1797
1798 StringRef ClangASTContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001799 error(ClangASTSection.getValue().getContents(ClangASTContents));
Adrian Prantl437105a2015-07-08 02:04:15 +00001800 outs().write(ClangASTContents.data(), ClangASTContents.size());
1801}
1802
Sanjoy Das6f567a42015-06-22 18:03:02 +00001803static void printFaultMaps(const ObjectFile *Obj) {
1804 const char *FaultMapSectionName = nullptr;
1805
1806 if (isa<ELFObjectFileBase>(Obj)) {
1807 FaultMapSectionName = ".llvm_faultmaps";
1808 } else if (isa<MachOObjectFile>(Obj)) {
1809 FaultMapSectionName = "__llvm_faultmaps";
1810 } else {
1811 errs() << "This operation is only currently supported "
1812 "for ELF and Mach-O executable files.\n";
1813 return;
1814 }
1815
1816 Optional<object::SectionRef> FaultMapSection;
1817
Colin LeMahieu77804be2015-07-29 15:45:39 +00001818 for (auto Sec : ToolSectionFilter(*Obj)) {
Sanjoy Das6f567a42015-06-22 18:03:02 +00001819 StringRef Name;
1820 Sec.getName(Name);
1821 if (Name == FaultMapSectionName) {
1822 FaultMapSection = Sec;
1823 break;
1824 }
1825 }
1826
1827 outs() << "FaultMap table:\n";
1828
1829 if (!FaultMapSection.hasValue()) {
1830 outs() << "<not found>\n";
1831 return;
1832 }
1833
1834 StringRef FaultMapContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001835 error(FaultMapSection.getValue().getContents(FaultMapContents));
Sanjoy Das6f567a42015-06-22 18:03:02 +00001836
1837 FaultMapParser FMP(FaultMapContents.bytes_begin(),
1838 FaultMapContents.bytes_end());
1839
1840 outs() << FMP;
1841}
1842
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001843static void printPrivateFileHeaders(const ObjectFile *o) {
1844 if (o->isELF())
1845 printELFFileHeader(o);
1846 else if (o->isCOFF())
1847 printCOFFFileHeader(o);
1848 else if (o->isMachO()) {
1849 printMachOFileHeader(o);
1850 printMachOLoadCommands(o);
1851 } else
1852 report_fatal_error("Invalid/Unsupported object file format");
1853}
1854
1855static void printFirstPrivateFileHeader(const ObjectFile *o) {
Davide Italiano540e9212015-12-19 22:09:40 +00001856 if (o->isELF())
Rui Ueyamac2bed422013-09-27 21:04:00 +00001857 printELFFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001858 else if (o->isCOFF())
Rui Ueyamac2bed422013-09-27 21:04:00 +00001859 printCOFFFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001860 else if (o->isMachO())
Kevin Enderbyb76d3862014-08-22 20:35:18 +00001861 printMachOFileHeader(o);
Davide Italiano540e9212015-12-19 22:09:40 +00001862 else
1863 report_fatal_error("Invalid/Unsupported object file format");
Rui Ueyamac2bed422013-09-27 21:04:00 +00001864}
1865
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001866static void DumpObject(const ObjectFile *o, const Archive *a = nullptr) {
1867 StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
Adrian Prantl437105a2015-07-08 02:04:15 +00001868 // Avoid other output when using a raw option.
1869 if (!RawClangAST) {
1870 outs() << '\n';
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001871 if (a)
1872 outs() << a->getFileName() << "(" << o->getFileName() << ")";
1873 else
1874 outs() << o->getFileName();
1875 outs() << ":\tfile format " << o->getFileFormatName() << "\n\n";
Adrian Prantl437105a2015-07-08 02:04:15 +00001876 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001877
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001878 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +00001879 DisassembleObject(o, Relocations);
1880 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001881 PrintRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001882 if (SectionHeaders)
1883 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001884 if (SectionContents)
1885 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001886 if (SymbolTable)
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001887 PrintSymbolTable(o, ArchiveName);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00001888 if (UnwindInfo)
1889 PrintUnwindInfo(o);
Rui Ueyamac2bed422013-09-27 21:04:00 +00001890 if (PrivateHeaders)
Kevin Enderby0ae163f2016-01-13 00:25:36 +00001891 printPrivateFileHeaders(o);
1892 if (FirstPrivateHeader)
1893 printFirstPrivateFileHeader(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +00001894 if (ExportsTrie)
1895 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +00001896 if (Rebase)
1897 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +00001898 if (Bind)
1899 printBindTable(o);
1900 if (LazyBind)
1901 printLazyBindTable(o);
1902 if (WeakBind)
1903 printWeakBindTable(o);
Adrian Prantl437105a2015-07-08 02:04:15 +00001904 if (RawClangAST)
1905 printRawClangAST(o);
Sanjoy Das6f567a42015-06-22 18:03:02 +00001906 if (PrintFaultMaps)
1907 printFaultMaps(o);
Igor Laevsky03a670c2016-01-26 15:09:42 +00001908 if (DwarfDumpType != DIDT_Null) {
1909 std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*o));
1910 // Dump the complete DWARF structure.
1911 DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */);
1912 }
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001913}
1914
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +00001915static void DumpObject(const COFFImportFile *I, const Archive *A) {
1916 StringRef ArchiveName = A ? A->getFileName() : "";
1917
1918 // Avoid other output when using a raw option.
1919 if (!RawClangAST)
1920 outs() << '\n'
1921 << ArchiveName << "(" << I->getFileName() << ")"
1922 << ":\tfile format COFF-import-file"
1923 << "\n\n";
1924
1925 if (SymbolTable)
1926 printCOFFSymbolTable(I);
1927}
1928
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001929/// @brief Dump each object file in \a a;
1930static void DumpArchive(const Archive *a) {
Lang Hamesfc209622016-07-14 02:24:01 +00001931 Error Err;
1932 for (auto &C : a->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001933 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
1934 if (!ChildOrErr) {
1935 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
1936 report_error(a->getFileName(), C, std::move(E));
1937 continue;
1938 }
Rafael Espindolaae460022014-06-16 16:08:36 +00001939 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Kevin Enderbyac9e1552016-05-17 17:10:12 +00001940 DumpObject(o, a);
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +00001941 else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
1942 DumpObject(I, a);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001943 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001944 report_error(a->getFileName(), object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001945 }
Lang Hamesfc209622016-07-14 02:24:01 +00001946 if (Err)
1947 report_error(a->getFileName(), std::move(Err));
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001948}
1949
1950/// @brief Open file and figure out how to dump it.
1951static void DumpInput(StringRef file) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001952
Kevin Enderbye2297dd2015-01-07 21:02:18 +00001953 // If we are using the Mach-O specific object file parser, then let it parse
1954 // the file and process the command line options. So the -arch flags can
1955 // be used to select specific slices, etc.
1956 if (MachOOpt) {
1957 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001958 return;
1959 }
1960
1961 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00001962 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
1963 if (!BinaryOrErr)
Kevin Enderbyb34e3a12016-05-05 17:43:35 +00001964 report_error(file, BinaryOrErr.takeError());
Rafael Espindola48af1c22014-08-19 18:44:46 +00001965 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001966
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001967 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001968 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +00001969 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001970 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +00001971 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00001972 report_error(file, object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001973}
1974
Michael J. Spencer2670c252011-01-20 06:39:06 +00001975int main(int argc, char **argv) {
1976 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +00001977 sys::PrintStackTraceOnErrorSignal(argv[0]);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001978 PrettyStackTraceProgram X(argc, argv);
1979 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
1980
1981 // Initialize targets and assembly printers/parsers.
1982 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +00001983 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +00001984 llvm::InitializeAllDisassemblers();
1985
Pete Cooper28fb4fc2012-05-03 23:20:10 +00001986 // Register the target printer for --version.
1987 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
1988
Michael J. Spencer2670c252011-01-20 06:39:06 +00001989 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
1990 TripleName = Triple::normalize(TripleName);
1991
1992 ToolName = argv[0];
1993
1994 // Defaults to a.out if no filenames specified.
1995 if (InputFilenames.size() == 0)
1996 InputFilenames.push_back("a.out");
1997
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001998 if (DisassembleAll || PrintSource || PrintLines)
Colin LeMahieuf34933e2015-07-23 20:58:49 +00001999 Disassemble = true;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00002000 if (!Disassemble
2001 && !Relocations
2002 && !SectionHeaders
2003 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00002004 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +00002005 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +00002006 && !PrivateHeaders
Kevin Enderby0ae163f2016-01-13 00:25:36 +00002007 && !FirstPrivateHeader
Nick Kledzikac431442014-09-12 21:34:15 +00002008 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +00002009 && !Rebase
2010 && !Bind
2011 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +00002012 && !WeakBind
Adrian Prantl437105a2015-07-08 02:04:15 +00002013 && !RawClangAST
Kevin Enderby13023a12015-01-15 23:19:11 +00002014 && !(UniversalHeaders && MachOOpt)
Kevin Enderbya7bdc7e2015-01-22 18:55:27 +00002015 && !(ArchiveHeaders && MachOOpt)
Kevin Enderby69fe98d2015-01-23 18:52:17 +00002016 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +00002017 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002018 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +00002019 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +00002020 && !(DylibsUsed && MachOOpt)
2021 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002022 && !(ObjcMetaData && MachOOpt)
Colin LeMahieufcc32762015-07-29 19:08:10 +00002023 && !(FilterSections.size() != 0 && MachOOpt)
Igor Laevsky03a670c2016-01-26 15:09:42 +00002024 && !PrintFaultMaps
2025 && DwarfDumpType == DIDT_Null) {
Michael J. Spencer2670c252011-01-20 06:39:06 +00002026 cl::PrintHelpMessage();
2027 return 2;
2028 }
2029
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002030 std::for_each(InputFilenames.begin(), InputFilenames.end(),
2031 DumpInput);
Michael J. Spencer2670c252011-01-20 06:39:06 +00002032
Davide Italianoccd53fe2015-08-05 07:18:31 +00002033 return EXIT_SUCCESS;
Michael J. Spencer2670c252011-01-20 06:39:06 +00002034}