blob: 610cd11365ca200615bff5944d4ac186c8edd258 [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"
Rafael Aulerb0e4b912018-03-09 19:13:44 +000023#include "llvm/ADT/StringSet.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000024#include "llvm/ADT/Triple.h"
Sanjoy Das3f1bc3b2015-06-23 20:09:03 +000025#include "llvm/CodeGen/FaultMaps.h"
Igor Laevsky03a670c2016-01-26 15:09:42 +000026#include "llvm/DebugInfo/DWARF/DWARFContext.h"
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +000027#include "llvm/DebugInfo/Symbolize/Symbolize.h"
Paul Semel007dedb2018-07-18 16:39:21 +000028#include "llvm/Demangle/Demangle.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000029#include "llvm/MC/MCAsmInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000030#include "llvm/MC/MCContext.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000031#include "llvm/MC/MCDisassembler/MCDisassembler.h"
32#include "llvm/MC/MCDisassembler/MCRelocationInfo.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000033#include "llvm/MC/MCInst.h"
34#include "llvm/MC/MCInstPrinter.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000035#include "llvm/MC/MCInstrAnalysis.h"
Craig Topper54bfde72012-04-02 06:09:36 +000036#include "llvm/MC/MCInstrInfo.h"
Ahmed Bougachaad1084d2013-05-24 00:39:57 +000037#include "llvm/MC/MCObjectFileInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000038#include "llvm/MC/MCRegisterInfo.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000039#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Object/Archive.h"
41#include "llvm/Object/COFF.h"
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +000042#include "llvm/Object/COFFImportFile.h"
Benjamin Kramerf57c1972016-01-26 16:44:37 +000043#include "llvm/Object/ELFObjectFile.h"
Rafael Espindolaa9f810b2012-12-21 03:47:03 +000044#include "llvm/Object/MachO.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000045#include "llvm/Object/ObjectFile.h"
Sam Clegg4df5d762017-06-27 20:40:53 +000046#include "llvm/Object/Wasm.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000047#include "llvm/Support/Casting.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000048#include "llvm/Support/CommandLine.h"
49#include "llvm/Support/Debug.h"
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +000050#include "llvm/Support/Errc.h"
Michael J. Spencerba4a3622011-10-08 00:18:30 +000051#include "llvm/Support/FileSystem.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000052#include "llvm/Support/Format.h"
Benjamin Kramerbf115312011-07-25 23:04:36 +000053#include "llvm/Support/GraphWriter.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000054#include "llvm/Support/Host.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000055#include "llvm/Support/InitLLVM.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000056#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000057#include "llvm/Support/SourceMgr.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000058#include "llvm/Support/TargetRegistry.h"
59#include "llvm/Support/TargetSelect.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000060#include "llvm/Support/raw_ostream.h"
Michael J. Spencer2670c252011-01-20 06:39:06 +000061#include <algorithm>
Benjamin Kramera5177e62012-03-23 11:49:32 +000062#include <cctype>
Michael J. Spencer2670c252011-01-20 06:39:06 +000063#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000064#include <system_error>
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +000065#include <unordered_map>
Rafael Espindolac398e672017-07-19 22:27:28 +000066#include <utility>
Ahmed Bougacha17926472013-08-21 07:29:02 +000067
Michael J. Spencer2670c252011-01-20 06:39:06 +000068using namespace llvm;
69using namespace object;
70
Fangrui Song8513cd42018-06-27 20:45:11 +000071cl::opt<bool>
72 llvm::AllHeaders("all-headers",
73 cl::desc("Display all available header information"));
74static cl::alias AllHeadersShort("x", cl::desc("Alias for --all-headers"),
75 cl::aliasopt(AllHeaders));
76
Benjamin Kramer43a772e2011-09-19 17:56:04 +000077static cl::list<std::string>
78InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
Michael J. Spencer2670c252011-01-20 06:39:06 +000079
Kevin Enderbye2297dd2015-01-07 21:02:18 +000080cl::opt<bool>
81llvm::Disassemble("disassemble",
Benjamin Kramer43a772e2011-09-19 17:56:04 +000082 cl::desc("Display assembler mnemonics for the machine instructions"));
83static cl::alias
84Disassembled("d", cl::desc("Alias for --disassemble"),
Colin LeMahieu77804be2015-07-29 15:45:39 +000085 cl::aliasopt(Disassemble));
86
87cl::opt<bool>
88llvm::DisassembleAll("disassemble-all",
89 cl::desc("Display assembler mnemonics for the machine instructions"));
90static cl::alias
91DisassembleAlld("D", cl::desc("Alias for --disassemble-all"),
Colin LeMahieuf34933e2015-07-23 20:58:49 +000092 cl::aliasopt(DisassembleAll));
Michael J. Spencer2670c252011-01-20 06:39:06 +000093
Paul Semel007dedb2018-07-18 16:39:21 +000094cl::opt<std::string> llvm::Demangle("demangle",
95 cl::desc("Demangle symbols names"),
96 cl::ValueOptional, cl::init("none"));
97
98static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"),
99 cl::aliasopt(Demangle));
100
Rafael Aulerb0e4b912018-03-09 19:13:44 +0000101static cl::list<std::string>
102DisassembleFunctions("df",
103 cl::CommaSeparated,
104 cl::desc("List of functions to disassemble"));
105static StringSet<> DisasmFuncsSet;
106
Kevin Enderby98da6132015-01-20 21:47:46 +0000107cl::opt<bool>
108llvm::Relocations("r", cl::desc("Display the relocation entries in the file"));
Michael J. Spencerba4a3622011-10-08 00:18:30 +0000109
Kevin Enderby98da6132015-01-20 21:47:46 +0000110cl::opt<bool>
Paul Semelcb0f0432018-06-07 13:30:55 +0000111llvm::DynamicRelocations("dynamic-reloc",
112 cl::desc("Display the dynamic relocation entries in the file"));
113static cl::alias
114DynamicRelocationsd("R", cl::desc("Alias for --dynamic-reloc"),
115 cl::aliasopt(DynamicRelocations));
116
117cl::opt<bool>
Kevin Enderby98da6132015-01-20 21:47:46 +0000118llvm::SectionContents("s", cl::desc("Display the content of each section"));
Michael J. Spencer4e25c022011-10-17 17:13:22 +0000119
Kevin Enderby98da6132015-01-20 21:47:46 +0000120cl::opt<bool>
121llvm::SymbolTable("t", cl::desc("Display the symbol table"));
Michael J. Spencerbfa06782011-10-18 19:32:17 +0000122
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000123cl::opt<bool>
124llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
Nick Kledzikd04bc352014-08-30 00:20:14 +0000125
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000126cl::opt<bool>
127llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
Nick Kledzikac431442014-09-12 21:34:15 +0000128
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000129cl::opt<bool>
130llvm::Bind("bind", cl::desc("Display mach-o binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000131
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000132cl::opt<bool>
133llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000134
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000135cl::opt<bool>
136llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
Nick Kledzik56ebef42014-09-16 01:41:51 +0000137
Adrian Prantl437105a2015-07-08 02:04:15 +0000138cl::opt<bool>
139llvm::RawClangAST("raw-clang-ast",
140 cl::desc("Dump the raw binary contents of the clang AST section"));
141
Nick Kledzik56ebef42014-09-16 01:41:51 +0000142static cl::opt<bool>
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000143MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000144static cl::alias
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000145MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt));
Benjamin Kramer87ee76c2011-07-20 19:37:35 +0000146
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000147cl::opt<std::string>
148llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
149 "see -version for available targets"));
150
151cl::opt<std::string>
Kevin Enderbyc9595622014-08-06 23:24:41 +0000152llvm::MCPU("mcpu",
153 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
154 cl::value_desc("cpu-name"),
155 cl::init(""));
156
157cl::opt<std::string>
Kevin Enderbyef3ad2f2014-12-04 23:56:27 +0000158llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, "
Michael J. Spencer2670c252011-01-20 06:39:06 +0000159 "see -version for available targets"));
160
Kevin Enderby98da6132015-01-20 21:47:46 +0000161cl::opt<bool>
162llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the "
163 "headers for each section."));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000164static cl::alias
165SectionHeadersShort("headers", cl::desc("Alias for --section-headers"),
166 cl::aliasopt(SectionHeaders));
167static cl::alias
168SectionHeadersShorter("h", cl::desc("Alias for --section-headers"),
169 cl::aliasopt(SectionHeaders));
Colin LeMahieufcc32762015-07-29 19:08:10 +0000170
Colin LeMahieu77804be2015-07-29 15:45:39 +0000171cl::list<std::string>
Colin LeMahieufcc32762015-07-29 19:08:10 +0000172llvm::FilterSections("section", cl::desc("Operate on the specified sections only. "
173 "With -macho dump segment,section"));
174cl::alias
175static FilterSectionsj("j", cl::desc("Alias for --section"),
176 cl::aliasopt(llvm::FilterSections));
Nick Lewyckyfcf84622011-10-10 21:21:34 +0000177
Kevin Enderbyc9595622014-08-06 23:24:41 +0000178cl::list<std::string>
179llvm::MAttrs("mattr",
Jack Carter551efd72012-08-28 19:24:49 +0000180 cl::CommaSeparated,
181 cl::desc("Target specific attributes"),
182 cl::value_desc("a1,+a2,-a3,..."));
183
Kevin Enderbybf246f52014-09-24 23:08:22 +0000184cl::opt<bool>
185llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling "
186 "instructions, do not print "
187 "the instruction bytes."));
Saleem Abdulrasooldea14b22017-02-08 18:11:31 +0000188cl::opt<bool>
189llvm::NoLeadingAddr("no-leading-addr", cl::desc("Print no leading address"));
Eli Bendersky3a6808c2012-11-20 22:57:02 +0000190
Kevin Enderby98da6132015-01-20 21:47:46 +0000191cl::opt<bool>
192llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information"));
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000193
194static cl::alias
195UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
196 cl::aliasopt(UnwindInfo));
197
Kevin Enderbye2297dd2015-01-07 21:02:18 +0000198cl::opt<bool>
199llvm::PrivateHeaders("private-headers",
200 cl::desc("Display format specific file headers"));
Michael J. Spencer209565db2013-01-06 03:56:49 +0000201
Kevin Enderby0ae163f2016-01-13 00:25:36 +0000202cl::opt<bool>
203llvm::FirstPrivateHeader("private-header",
204 cl::desc("Display only the first format specific file "
205 "header"));
206
Michael J. Spencer209565db2013-01-06 03:56:49 +0000207static cl::alias
208PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
209 cl::aliasopt(PrivateHeaders));
210
Paul Semeld2af4d62018-07-04 15:25:03 +0000211cl::opt<bool> llvm::FileHeaders(
212 "file-headers",
213 cl::desc("Display the contents of the overall file header"));
214
215static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"),
216 cl::aliasopt(FileHeaders));
217
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000218cl::opt<bool>
Paul Semel0dc92f62018-07-05 14:43:29 +0000219 llvm::ArchiveHeaders("archive-headers",
220 cl::desc("Display archive header information"));
221
222cl::alias
223ArchiveHeadersShort("a", cl::desc("Alias for --archive-headers"),
224 cl::aliasopt(ArchiveHeaders));
225
226cl::opt<bool>
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000227 llvm::PrintImmHex("print-imm-hex",
Colin LeMahieuefe37322016-04-08 18:15:37 +0000228 cl::desc("Use hex format for immediate values"));
Colin LeMahieu14ec76e2015-06-07 21:07:17 +0000229
Sanjoy Das6f567a42015-06-22 18:03:02 +0000230cl::opt<bool> PrintFaultMaps("fault-map-section",
231 cl::desc("Display contents of faultmap section"));
232
Igor Laevsky03a670c2016-01-26 15:09:42 +0000233cl::opt<DIDumpType> llvm::DwarfDumpType(
234 "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"),
Jonas Devliegherec0a758d2017-09-18 14:15:57 +0000235 cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame")));
Igor Laevsky03a670c2016-01-26 15:09:42 +0000236
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000237cl::opt<bool> PrintSource(
238 "source",
239 cl::desc(
Alex Denisova07169e2018-02-02 19:20:37 +0000240 "Display source inlined with disassembly. Implies disassemble object"));
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +0000241
242cl::alias PrintSourceShort("S", cl::desc("Alias for -source"),
243 cl::aliasopt(PrintSource));
244
245cl::opt<bool> PrintLines("line-numbers",
246 cl::desc("Display source line numbers with "
247 "disassembly. Implies disassemble object"));
248
249cl::alias PrintLinesShort("l", cl::desc("Alias for -line-numbers"),
250 cl::aliasopt(PrintLines));
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +0000251
252cl::opt<unsigned long long>
253 StartAddress("start-address", cl::desc("Disassemble beginning at address"),
254 cl::value_desc("address"), cl::init(0));
255cl::opt<unsigned long long>
256 StopAddress("stop-address", cl::desc("Stop disassembly at address"),
257 cl::value_desc("address"), cl::init(UINT64_MAX));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000258static StringRef ToolName;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000259
Sam Parker5fba45a2017-02-08 09:44:18 +0000260typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy;
261
Colin LeMahieu77804be2015-07-29 15:45:39 +0000262namespace {
Colin LeMahieufcc32762015-07-29 19:08:10 +0000263typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate;
Colin LeMahieu77804be2015-07-29 15:45:39 +0000264
265class SectionFilterIterator {
266public:
267 SectionFilterIterator(FilterPredicate P,
268 llvm::object::section_iterator const &I,
269 llvm::object::section_iterator const &E)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000270 : Predicate(std::move(P)), Iterator(I), End(E) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000271 ScanPredicate();
272 }
Benjamin Kramerac9257b2015-09-24 14:52:52 +0000273 const llvm::object::SectionRef &operator*() const { return *Iterator; }
Colin LeMahieu77804be2015-07-29 15:45:39 +0000274 SectionFilterIterator &operator++() {
275 ++Iterator;
276 ScanPredicate();
277 return *this;
278 }
279 bool operator!=(SectionFilterIterator const &Other) const {
280 return Iterator != Other.Iterator;
281 }
282
283private:
284 void ScanPredicate() {
Colin LeMahieuda1723f2015-07-29 19:21:13 +0000285 while (Iterator != End && !Predicate(*Iterator)) {
Colin LeMahieu77804be2015-07-29 15:45:39 +0000286 ++Iterator;
287 }
288 }
289 FilterPredicate Predicate;
290 llvm::object::section_iterator Iterator;
291 llvm::object::section_iterator End;
292};
293
294class SectionFilter {
295public:
296 SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O)
Benjamin Kramer82de7d32016-05-27 14:27:24 +0000297 : Predicate(std::move(P)), Object(O) {}
Colin LeMahieu77804be2015-07-29 15:45:39 +0000298 SectionFilterIterator begin() {
299 return SectionFilterIterator(Predicate, Object.section_begin(),
300 Object.section_end());
301 }
302 SectionFilterIterator end() {
303 return SectionFilterIterator(Predicate, Object.section_end(),
304 Object.section_end());
305 }
306
307private:
308 FilterPredicate Predicate;
309 llvm::object::ObjectFile const &Object;
310};
311SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) {
David Majnemer42531262016-08-12 03:55:06 +0000312 return SectionFilter(
313 [](llvm::object::SectionRef const &S) {
314 if (FilterSections.empty())
315 return true;
316 llvm::StringRef String;
317 std::error_code error = S.getName(String);
318 if (error)
319 return false;
320 return is_contained(FilterSections, String);
321 },
322 O);
Colin LeMahieu77804be2015-07-29 15:45:39 +0000323}
324}
325
Davide Italianoccd53fe2015-08-05 07:18:31 +0000326void llvm::error(std::error_code EC) {
Mark Seaborneb03ac52014-01-25 00:32:01 +0000327 if (!EC)
Davide Italianoccd53fe2015-08-05 07:18:31 +0000328 return;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +0000329
Davide Italiano140af642015-12-25 18:16:45 +0000330 errs() << ToolName << ": error reading file: " << EC.message() << ".\n";
331 errs().flush();
Davide Italiano7f6c3012015-08-06 00:18:52 +0000332 exit(1);
Michael J. Spencer2670c252011-01-20 06:39:06 +0000333}
334
Kevin Enderby42398052016-06-28 23:16:13 +0000335LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) {
336 errs() << ToolName << ": " << Message << ".\n";
337 errs().flush();
338 exit(1);
339}
340
Paul Semel007dedb2018-07-18 16:39:21 +0000341void llvm::warn(StringRef Message) {
342 errs() << ToolName << ": warning: " << Message << ".\n";
343 errs().flush();
344}
345
Davide Italianoed9d95b2015-12-29 13:41:02 +0000346LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000347 Twine Message) {
348 errs() << ToolName << ": '" << File << "': " << Message << ".\n";
349 exit(1);
350}
351
352LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
Davide Italianoed9d95b2015-12-29 13:41:02 +0000353 std::error_code EC) {
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000354 assert(EC);
355 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
Davide Italianoccd53fe2015-08-05 07:18:31 +0000356 exit(1);
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +0000357}
358
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000359LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
360 llvm::Error E) {
361 assert(E);
362 std::string Buf;
363 raw_string_ostream OS(Buf);
364 logAllUnhandledErrors(std::move(E), OS, "");
365 OS.flush();
Kevin Enderbyb34e3a12016-05-05 17:43:35 +0000366 errs() << ToolName << ": '" << File << "': " << Buf;
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000367 exit(1);
368}
369
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000370LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
371 StringRef FileName,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000372 llvm::Error E,
373 StringRef ArchitectureName) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000374 assert(E);
375 errs() << ToolName << ": ";
376 if (ArchiveName != "")
377 errs() << ArchiveName << "(" << FileName << ")";
378 else
Justin Bogner31d8b7d2016-10-26 22:37:52 +0000379 errs() << "'" << FileName << "'";
Kevin Enderby9acb1092016-05-31 20:35:34 +0000380 if (!ArchitectureName.empty())
381 errs() << " (for architecture " << ArchitectureName << ")";
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000382 std::string Buf;
383 raw_string_ostream OS(Buf);
384 logAllUnhandledErrors(std::move(E), OS, "");
385 OS.flush();
Justin Bogner31d8b7d2016-10-26 22:37:52 +0000386 errs() << ": " << Buf;
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000387 exit(1);
388}
389
390LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName,
391 const object::Archive::Child &C,
Kevin Enderby9acb1092016-05-31 20:35:34 +0000392 llvm::Error E,
393 StringRef ArchitectureName) {
Kevin Enderbyf4586032016-07-29 17:44:13 +0000394 Expected<StringRef> NameOrErr = C.getName();
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000395 // TODO: if we have a error getting the name then it would be nice to print
396 // the index of which archive member this is and or its offset in the
397 // archive instead of "???" as the name.
Kevin Enderbyf4586032016-07-29 17:44:13 +0000398 if (!NameOrErr) {
399 consumeError(NameOrErr.takeError());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000400 llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName);
Kevin Enderbyf4586032016-07-29 17:44:13 +0000401 } else
Kevin Enderby9acb1092016-05-31 20:35:34 +0000402 llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E),
403 ArchitectureName);
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000404}
405
Craig Toppere6cb63e2014-04-25 04:24:47 +0000406static const Target *getTarget(const ObjectFile *Obj = nullptr) {
Michael J. Spencer2670c252011-01-20 06:39:06 +0000407 // Figure out the target triple.
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000408 llvm::Triple TheTriple("unknown-unknown-unknown");
Michael J. Spencer05350e6d2011-01-20 07:22:04 +0000409 if (TripleName.empty()) {
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000410 if (Obj) {
Vlad Tsyrklevichde620462017-09-19 02:22:48 +0000411 TheTriple = Obj->makeTriple();
Ahmed Bougachaad1084d2013-05-24 00:39:57 +0000412 }
Sam Parkerdf7c6ef2017-01-18 13:52:12 +0000413 } else {
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000414 TheTriple.setTriple(Triple::normalize(TripleName));
Vlad Tsyrklevichde620462017-09-19 02:22:48 +0000415
Sam Parkerdf7c6ef2017-01-18 13:52:12 +0000416 // Use the triple, but also try to combine with ARM build attributes.
417 if (Obj) {
418 auto Arch = Obj->getArch();
419 if (Arch == Triple::arm || Arch == Triple::armeb) {
420 Obj->setARMSubArch(TheTriple);
421 }
422 }
423 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000424
425 // Get the target specific parser.
426 std::string Error;
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000427 const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple,
428 Error);
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000429 if (!TheTarget) {
430 if (Obj)
431 report_error(Obj->getFileName(), "can't find target: " + Error);
432 else
433 error("can't find target: " + Error);
434 }
Michael J. Spencer2670c252011-01-20 06:39:06 +0000435
Kevin Enderbyfe3d0052012-05-08 23:38:45 +0000436 // Update the triple name and return the found target.
437 TripleName = TheTriple.getTriple();
438 return TheTarget;
Michael J. Spencer2670c252011-01-20 06:39:06 +0000439}
440
Michael J. Spencer0c6ec482012-12-05 20:12:35 +0000441bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindola704cd842015-07-06 15:47:43 +0000442 return a.getOffset() < b.getOffset();
Michael J. Spencer51862b32011-10-13 22:17:18 +0000443}
444
Rafael Espindola37070a52015-06-03 04:48:06 +0000445template <class ELFT>
Rafael Espindola37070a52015-06-03 04:48:06 +0000446static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000447 const RelocationRef &RelRef,
Rafael Espindola37070a52015-06-03 04:48:06 +0000448 SmallVectorImpl<char> &Result) {
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000449 DataRefImpl Rel = RelRef.getRawDataRefImpl();
450
Rafael Espindola37070a52015-06-03 04:48:06 +0000451 typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym;
452 typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000453 typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela;
454
Rafael Espindola37070a52015-06-03 04:48:06 +0000455 const ELFFile<ELFT> &EF = *Obj->getELFFile();
456
Davide Italiano6cf09262016-11-16 05:10:28 +0000457 auto SecOrErr = EF.getSection(Rel.d.a);
458 if (!SecOrErr)
459 return errorToErrorCode(SecOrErr.takeError());
Rafael Espindola6def3042015-07-01 12:56:27 +0000460 const Elf_Shdr *Sec = *SecOrErr;
Davide Italiano6cf09262016-11-16 05:10:28 +0000461 auto SymTabOrErr = EF.getSection(Sec->sh_link);
462 if (!SymTabOrErr)
463 return errorToErrorCode(SymTabOrErr.takeError());
Rafael Espindola6def3042015-07-01 12:56:27 +0000464 const Elf_Shdr *SymTab = *SymTabOrErr;
Rafael Espindola719dc7c2015-06-29 12:38:31 +0000465 assert(SymTab->sh_type == ELF::SHT_SYMTAB ||
466 SymTab->sh_type == ELF::SHT_DYNSYM);
Davide Italiano6cf09262016-11-16 05:10:28 +0000467 auto StrTabSec = EF.getSection(SymTab->sh_link);
468 if (!StrTabSec)
469 return errorToErrorCode(StrTabSec.takeError());
470 auto StrTabOrErr = EF.getStringTable(*StrTabSec);
471 if (!StrTabOrErr)
472 return errorToErrorCode(StrTabOrErr.takeError());
Rafael Espindola6a1bfb22015-06-29 14:39:25 +0000473 StringRef StrTab = *StrTabOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000474 int64_t addend = 0;
Paul Semelcb0f0432018-06-07 13:30:55 +0000475 // If there is no Symbol associated with the relocation, we set the undef
476 // boolean value to 'true'. This will prevent us from calling functions that
477 // requires the relocation to be associated with a symbol.
478 bool undef = false;
Rafael Espindola6def3042015-07-01 12:56:27 +0000479 switch (Sec->sh_type) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000480 default:
481 return object_error::parse_failed;
482 case ELF::SHT_REL: {
Rafael Espindola37070a52015-06-03 04:48:06 +0000483 // TODO: Read implicit addend from section data.
484 break;
485 }
486 case ELF::SHT_RELA: {
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000487 const Elf_Rela *ERela = Obj->getRela(Rel);
Rafael Espindola7f162ec2015-07-02 14:21:38 +0000488 addend = ERela->r_addend;
Paul Semelcb0f0432018-06-07 13:30:55 +0000489 undef = ERela->getSymbol(false) == 0;
Rafael Espindola37070a52015-06-03 04:48:06 +0000490 break;
491 }
492 }
Rafael Espindola75d5b542015-06-03 05:14:22 +0000493 StringRef Target;
Paul Semelcb0f0432018-06-07 13:30:55 +0000494 if (!undef) {
495 symbol_iterator SI = RelRef.getSymbol();
496 const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl());
497 if (symb->getType() == ELF::STT_SECTION) {
498 Expected<section_iterator> SymSI = SI->getSection();
499 if (!SymSI)
500 return errorToErrorCode(SymSI.takeError());
501 const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl());
502 auto SecName = EF.getSectionName(SymSec);
503 if (!SecName)
504 return errorToErrorCode(SecName.takeError());
505 Target = *SecName;
506 } else {
507 Expected<StringRef> SymName = symb->getName(StrTab);
508 if (!SymName)
509 return errorToErrorCode(SymName.takeError());
510 Target = *SymName;
511 }
512 } else
513 Target = "*ABS*";
Daniel Cedermand72b9fd2018-06-01 05:31:58 +0000514
515 // Default scheme is to print Target, as well as "+ <addend>" for nonzero
516 // addend. Should be acceptable for all normal purposes.
517 std::string fmtbuf;
518 raw_string_ostream fmt(fmtbuf);
519 fmt << Target;
520 if (addend != 0)
521 fmt << (addend < 0 ? "" : "+") << addend;
522 fmt.flush();
523 Result.append(fmtbuf.begin(), fmtbuf.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000524 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000525}
526
527static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj,
Rafael Espindolaa01ff222015-08-10 20:50:40 +0000528 const RelocationRef &Rel,
Rafael Espindola37070a52015-06-03 04:48:06 +0000529 SmallVectorImpl<char> &Result) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000530 if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj))
531 return getRelocationValueString(ELF32LE, Rel, Result);
532 if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj))
533 return getRelocationValueString(ELF64LE, Rel, Result);
534 if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj))
535 return getRelocationValueString(ELF32BE, Rel, Result);
536 auto *ELF64BE = cast<ELF64BEObjectFile>(Obj);
537 return getRelocationValueString(ELF64BE, Rel, Result);
538}
539
540static std::error_code getRelocationValueString(const COFFObjectFile *Obj,
541 const RelocationRef &Rel,
542 SmallVectorImpl<char> &Result) {
543 symbol_iterator SymI = Rel.getSymbol();
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000544 Expected<StringRef> SymNameOrErr = SymI->getName();
545 if (!SymNameOrErr)
546 return errorToErrorCode(SymNameOrErr.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000547 StringRef SymName = *SymNameOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000548 Result.append(SymName.begin(), SymName.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000549 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000550}
551
552static void printRelocationTargetName(const MachOObjectFile *O,
553 const MachO::any_relocation_info &RE,
554 raw_string_ostream &fmt) {
555 bool IsScattered = O->isRelocationScattered(RE);
556
557 // Target of a scattered relocation is an address. In the interest of
558 // generating pretty output, scan through the symbol table looking for a
559 // symbol that aligns with that address. If we find one, print it.
560 // Otherwise, we just print the hex address of the target.
561 if (IsScattered) {
562 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
563
564 for (const SymbolRef &Symbol : O->symbols()) {
565 std::error_code ec;
Kevin Enderby931cb652016-06-24 18:24:42 +0000566 Expected<uint64_t> Addr = Symbol.getAddress();
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000567 if (!Addr)
568 report_error(O->getFileName(), Addr.takeError());
Rafael Espindolaed067c42015-07-03 18:19:00 +0000569 if (*Addr != Val)
Rafael Espindola37070a52015-06-03 04:48:06 +0000570 continue;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000571 Expected<StringRef> Name = Symbol.getName();
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000572 if (!Name)
573 report_error(O->getFileName(), Name.takeError());
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +0000574 fmt << *Name;
Rafael Espindola37070a52015-06-03 04:48:06 +0000575 return;
576 }
577
578 // If we couldn't find a symbol that this relocation refers to, try
579 // to find a section beginning instead.
Colin LeMahieu77804be2015-07-29 15:45:39 +0000580 for (const SectionRef &Section : ToolSectionFilter(*O)) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000581 std::error_code ec;
582
583 StringRef Name;
584 uint64_t Addr = Section.getAddress();
585 if (Addr != Val)
586 continue;
587 if ((ec = Section.getName(Name)))
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000588 report_error(O->getFileName(), ec);
Rafael Espindola37070a52015-06-03 04:48:06 +0000589 fmt << Name;
590 return;
591 }
592
593 fmt << format("0x%x", Val);
594 return;
595 }
596
597 StringRef S;
598 bool isExtern = O->getPlainRelocationExternal(RE);
599 uint64_t Val = O->getPlainRelocationSymbolNum(RE);
600
Martin Storsjo8c0317d2017-07-13 17:03:02 +0000601 if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) {
Simon Dardis38867052017-08-07 12:29:38 +0000602 fmt << format("0x%0" PRIx64, Val);
Martin Storsjo8c0317d2017-07-13 17:03:02 +0000603 return;
604 } else if (isExtern) {
Rafael Espindola37070a52015-06-03 04:48:06 +0000605 symbol_iterator SI = O->symbol_begin();
606 advance(SI, Val);
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000607 Expected<StringRef> SOrErr = SI->getName();
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000608 if (!SOrErr)
609 report_error(O->getFileName(), SOrErr.takeError());
Davide Italianoccd53fe2015-08-05 07:18:31 +0000610 S = *SOrErr;
Rafael Espindola37070a52015-06-03 04:48:06 +0000611 } else {
612 section_iterator SI = O->section_begin();
613 // Adjust for the fact that sections are 1-indexed.
Kevin Enderby3fc91882017-11-03 21:32:44 +0000614 if (Val == 0) {
615 fmt << "0 (?,?)";
616 return;
617 }
618 uint32_t i = Val - 1;
619 while (i != 0 && SI != O->section_end()) {
620 i--;
621 advance(SI, 1);
622 }
623 if (SI == O->section_end())
624 fmt << Val << " (?,?)";
625 else
626 SI->getName(S);
Rafael Espindola37070a52015-06-03 04:48:06 +0000627 }
628
629 fmt << S;
630}
631
Sam Clegg4df5d762017-06-27 20:40:53 +0000632static std::error_code getRelocationValueString(const WasmObjectFile *Obj,
633 const RelocationRef &RelRef,
634 SmallVectorImpl<char> &Result) {
635 const wasm::WasmRelocation& Rel = Obj->getWasmRelocation(RelRef);
Sam Cleggf676cdd2018-04-26 16:41:51 +0000636 symbol_iterator SI = RelRef.getSymbol();
Sam Clegg4df5d762017-06-27 20:40:53 +0000637 std::string fmtbuf;
638 raw_string_ostream fmt(fmtbuf);
Sam Clegg8c4b0ce2018-04-26 17:05:04 +0000639 if (SI == Obj->symbol_end()) {
640 // Not all wasm relocations have symbols associated with them.
641 // In particular R_WEBASSEMBLY_TYPE_INDEX_LEB.
Sam Cleggf676cdd2018-04-26 16:41:51 +0000642 fmt << Rel.Index;
643 } else {
Sam Clegg8c4b0ce2018-04-26 17:05:04 +0000644 Expected<StringRef> SymNameOrErr = SI->getName();
645 if (!SymNameOrErr)
646 return errorToErrorCode(SymNameOrErr.takeError());
Sam Cleggf676cdd2018-04-26 16:41:51 +0000647 StringRef SymName = *SymNameOrErr;
648 Result.append(SymName.begin(), SymName.end());
649 }
650 fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend;
Sam Clegg4df5d762017-06-27 20:40:53 +0000651 fmt.flush();
652 Result.append(fmtbuf.begin(), fmtbuf.end());
653 return std::error_code();
654}
655
Rafael Espindola37070a52015-06-03 04:48:06 +0000656static std::error_code getRelocationValueString(const MachOObjectFile *Obj,
657 const RelocationRef &RelRef,
658 SmallVectorImpl<char> &Result) {
659 DataRefImpl Rel = RelRef.getRawDataRefImpl();
660 MachO::any_relocation_info RE = Obj->getRelocation(Rel);
661
662 unsigned Arch = Obj->getArch();
663
664 std::string fmtbuf;
665 raw_string_ostream fmt(fmtbuf);
666 unsigned Type = Obj->getAnyRelocationType(RE);
667 bool IsPCRel = Obj->getAnyRelocationPCRel(RE);
668
669 // Determine any addends that should be displayed with the relocation.
670 // These require decoding the relocation type, which is triple-specific.
671
672 // X86_64 has entirely custom relocation types.
673 if (Arch == Triple::x86_64) {
674 bool isPCRel = Obj->getAnyRelocationPCRel(RE);
675
676 switch (Type) {
677 case MachO::X86_64_RELOC_GOT_LOAD:
678 case MachO::X86_64_RELOC_GOT: {
679 printRelocationTargetName(Obj, RE, fmt);
680 fmt << "@GOT";
681 if (isPCRel)
682 fmt << "PCREL";
683 break;
684 }
685 case MachO::X86_64_RELOC_SUBTRACTOR: {
686 DataRefImpl RelNext = Rel;
687 Obj->moveRelocationNext(RelNext);
688 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
689
690 // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type
691 // X86_64_RELOC_UNSIGNED.
692 // NOTE: Scattered relocations don't exist on x86_64.
693 unsigned RType = Obj->getAnyRelocationType(RENext);
694 if (RType != MachO::X86_64_RELOC_UNSIGNED)
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000695 report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after "
696 "X86_64_RELOC_SUBTRACTOR.");
Rafael Espindola37070a52015-06-03 04:48:06 +0000697
698 // The X86_64_RELOC_UNSIGNED contains the minuend symbol;
699 // X86_64_RELOC_SUBTRACTOR contains the subtrahend.
700 printRelocationTargetName(Obj, RENext, fmt);
701 fmt << "-";
702 printRelocationTargetName(Obj, RE, fmt);
703 break;
704 }
705 case MachO::X86_64_RELOC_TLV:
706 printRelocationTargetName(Obj, RE, fmt);
707 fmt << "@TLV";
708 if (isPCRel)
709 fmt << "P";
710 break;
711 case MachO::X86_64_RELOC_SIGNED_1:
712 printRelocationTargetName(Obj, RE, fmt);
713 fmt << "-1";
714 break;
715 case MachO::X86_64_RELOC_SIGNED_2:
716 printRelocationTargetName(Obj, RE, fmt);
717 fmt << "-2";
718 break;
719 case MachO::X86_64_RELOC_SIGNED_4:
720 printRelocationTargetName(Obj, RE, fmt);
721 fmt << "-4";
722 break;
723 default:
724 printRelocationTargetName(Obj, RE, fmt);
725 break;
726 }
727 // X86 and ARM share some relocation types in common.
728 } else if (Arch == Triple::x86 || Arch == Triple::arm ||
729 Arch == Triple::ppc) {
730 // Generic relocation types...
731 switch (Type) {
732 case MachO::GENERIC_RELOC_PAIR: // prints no info
Rui Ueyama7d099192015-06-09 15:20:42 +0000733 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000734 case MachO::GENERIC_RELOC_SECTDIFF: {
735 DataRefImpl RelNext = Rel;
736 Obj->moveRelocationNext(RelNext);
737 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
738
739 // X86 sect diff's must be followed by a relocation of type
740 // GENERIC_RELOC_PAIR.
741 unsigned RType = Obj->getAnyRelocationType(RENext);
742
743 if (RType != MachO::GENERIC_RELOC_PAIR)
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000744 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
745 "GENERIC_RELOC_SECTDIFF.");
Rafael Espindola37070a52015-06-03 04:48:06 +0000746
747 printRelocationTargetName(Obj, RE, fmt);
748 fmt << "-";
749 printRelocationTargetName(Obj, RENext, fmt);
750 break;
751 }
752 }
753
754 if (Arch == Triple::x86 || Arch == Triple::ppc) {
755 switch (Type) {
756 case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: {
757 DataRefImpl RelNext = Rel;
758 Obj->moveRelocationNext(RelNext);
759 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
760
761 // X86 sect diff's must be followed by a relocation of type
762 // GENERIC_RELOC_PAIR.
763 unsigned RType = Obj->getAnyRelocationType(RENext);
764 if (RType != MachO::GENERIC_RELOC_PAIR)
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000765 report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after "
766 "GENERIC_RELOC_LOCAL_SECTDIFF.");
Rafael Espindola37070a52015-06-03 04:48:06 +0000767
768 printRelocationTargetName(Obj, RE, fmt);
769 fmt << "-";
770 printRelocationTargetName(Obj, RENext, fmt);
771 break;
772 }
773 case MachO::GENERIC_RELOC_TLV: {
774 printRelocationTargetName(Obj, RE, fmt);
775 fmt << "@TLV";
776 if (IsPCRel)
777 fmt << "P";
778 break;
779 }
780 default:
781 printRelocationTargetName(Obj, RE, fmt);
782 }
783 } else { // ARM-specific relocations
784 switch (Type) {
785 case MachO::ARM_RELOC_HALF:
786 case MachO::ARM_RELOC_HALF_SECTDIFF: {
787 // Half relocations steal a bit from the length field to encode
788 // whether this is an upper16 or a lower16 relocation.
Martin Storsjofa5183b2017-07-13 05:54:08 +0000789 bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1;
Rafael Espindola37070a52015-06-03 04:48:06 +0000790
791 if (isUpper)
792 fmt << ":upper16:(";
793 else
794 fmt << ":lower16:(";
795 printRelocationTargetName(Obj, RE, fmt);
796
797 DataRefImpl RelNext = Rel;
798 Obj->moveRelocationNext(RelNext);
799 MachO::any_relocation_info RENext = Obj->getRelocation(RelNext);
800
801 // ARM half relocs must be followed by a relocation of type
802 // ARM_RELOC_PAIR.
803 unsigned RType = Obj->getAnyRelocationType(RENext);
804 if (RType != MachO::ARM_RELOC_PAIR)
Kevin Enderby7fa40c92016-11-16 22:17:38 +0000805 report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after "
806 "ARM_RELOC_HALF");
Rafael Espindola37070a52015-06-03 04:48:06 +0000807
808 // NOTE: The half of the target virtual address is stashed in the
809 // address field of the secondary relocation, but we can't reverse
810 // engineer the constant offset from it without decoding the movw/movt
811 // instruction to find the other half in its immediate field.
812
813 // ARM_RELOC_HALF_SECTDIFF encodes the second section in the
814 // symbol/section pointer of the follow-on relocation.
815 if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) {
816 fmt << "-";
817 printRelocationTargetName(Obj, RENext, fmt);
818 }
819
820 fmt << ")";
821 break;
822 }
823 default: { printRelocationTargetName(Obj, RE, fmt); }
824 }
825 }
826 } else
827 printRelocationTargetName(Obj, RE, fmt);
828
829 fmt.flush();
830 Result.append(fmtbuf.begin(), fmtbuf.end());
Rui Ueyama7d099192015-06-09 15:20:42 +0000831 return std::error_code();
Rafael Espindola37070a52015-06-03 04:48:06 +0000832}
833
834static std::error_code getRelocationValueString(const RelocationRef &Rel,
835 SmallVectorImpl<char> &Result) {
Rafael Espindola854038e2015-06-26 14:51:16 +0000836 const ObjectFile *Obj = Rel.getObject();
Rafael Espindola37070a52015-06-03 04:48:06 +0000837 if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj))
838 return getRelocationValueString(ELF, Rel, Result);
839 if (auto *COFF = dyn_cast<COFFObjectFile>(Obj))
840 return getRelocationValueString(COFF, Rel, Result);
Sam Clegg4df5d762017-06-27 20:40:53 +0000841 if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj))
842 return getRelocationValueString(Wasm, Rel, Result);
843 if (auto *MachO = dyn_cast<MachOObjectFile>(Obj))
844 return getRelocationValueString(MachO, Rel, Result);
845 llvm_unreachable("unknown object file format");
Rafael Espindola37070a52015-06-03 04:48:06 +0000846}
847
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +0000848/// Indicates whether this relocation should hidden when listing
Rafael Espindola0ad71d92015-06-30 03:41:26 +0000849/// relocations, usually because it is the trailing part of a multipart
850/// relocation that will be printed as part of the leading relocation.
851static bool getHidden(RelocationRef RelRef) {
852 const ObjectFile *Obj = RelRef.getObject();
853 auto *MachO = dyn_cast<MachOObjectFile>(Obj);
854 if (!MachO)
855 return false;
856
857 unsigned Arch = MachO->getArch();
858 DataRefImpl Rel = RelRef.getRawDataRefImpl();
859 uint64_t Type = MachO->getRelocationType(Rel);
860
861 // On arches that use the generic relocations, GENERIC_RELOC_PAIR
862 // is always hidden.
863 if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) {
864 if (Type == MachO::GENERIC_RELOC_PAIR)
865 return true;
866 } else if (Arch == Triple::x86_64) {
867 // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows
868 // an X86_64_RELOC_SUBTRACTOR.
869 if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) {
870 DataRefImpl RelPrev = Rel;
871 RelPrev.d.a--;
872 uint64_t PrevType = MachO->getRelocationType(RelPrev);
873 if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR)
874 return true;
875 }
876 }
877
878 return false;
879}
880
Sid Manningd9f28732018-05-14 19:46:08 +0000881namespace {
882class SourcePrinter {
883protected:
884 DILineInfo OldLineInfo;
885 const ObjectFile *Obj = nullptr;
886 std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer;
887 // File name to file contents of source
888 std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache;
889 // Mark the line endings of the cached source
890 std::unordered_map<std::string, std::vector<StringRef>> LineCache;
891
892private:
893 bool cacheSource(const DILineInfo& LineInfoFile);
894
895public:
896 SourcePrinter() = default;
897 SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) {
898 symbolize::LLVMSymbolizer::Options SymbolizerOpts(
899 DILineInfoSpecifier::FunctionNameKind::None, true, false, false,
900 DefaultArch);
901 Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts));
902 }
903 virtual ~SourcePrinter() = default;
904 virtual void printSourceLine(raw_ostream &OS, uint64_t Address,
905 StringRef Delimiter = "; ");
906};
907
908bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) {
909 std::unique_ptr<MemoryBuffer> Buffer;
910 if (LineInfo.Source) {
911 Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source);
912 } else {
913 auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName);
914 if (!BufferOrError)
915 return false;
916 Buffer = std::move(*BufferOrError);
917 }
918 // Chomp the file to get lines
919 size_t BufferSize = Buffer->getBufferSize();
920 const char *BufferStart = Buffer->getBufferStart();
921 for (const char *Start = BufferStart, *End = BufferStart;
922 End < BufferStart + BufferSize; End++)
923 if (*End == '\n' || End == BufferStart + BufferSize - 1 ||
924 (*End == '\r' && *(End + 1) == '\n')) {
925 LineCache[LineInfo.FileName].push_back(StringRef(Start, End - Start));
926 if (*End == '\r')
927 End++;
928 Start = End + 1;
929 }
930 SourceCache[LineInfo.FileName] = std::move(Buffer);
931 return true;
932}
933
934void SourcePrinter::printSourceLine(raw_ostream &OS, uint64_t Address,
935 StringRef Delimiter) {
936 if (!Symbolizer)
937 return;
938 DILineInfo LineInfo = DILineInfo();
939 auto ExpectecLineInfo =
940 Symbolizer->symbolizeCode(Obj->getFileName(), Address);
941 if (!ExpectecLineInfo)
942 consumeError(ExpectecLineInfo.takeError());
943 else
944 LineInfo = *ExpectecLineInfo;
945
946 if ((LineInfo.FileName == "<invalid>") || OldLineInfo.Line == LineInfo.Line ||
947 LineInfo.Line == 0)
948 return;
949
950 if (PrintLines)
951 OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line << "\n";
952 if (PrintSource) {
953 if (SourceCache.find(LineInfo.FileName) == SourceCache.end())
954 if (!cacheSource(LineInfo))
955 return;
956 auto FileBuffer = SourceCache.find(LineInfo.FileName);
957 if (FileBuffer != SourceCache.end()) {
958 auto LineBuffer = LineCache.find(LineInfo.FileName);
959 if (LineBuffer != LineCache.end()) {
960 if (LineInfo.Line > LineBuffer->second.size())
961 return;
962 // Vector begins at 0, line numbers are non-zero
963 OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim()
964 << "\n";
965 }
966 }
967 }
968 OldLineInfo = LineInfo;
969}
970
971static bool isArmElf(const ObjectFile *Obj) {
972 return (Obj->isELF() &&
973 (Obj->getArch() == Triple::aarch64 ||
974 Obj->getArch() == Triple::aarch64_be ||
975 Obj->getArch() == Triple::arm || Obj->getArch() == Triple::armeb ||
976 Obj->getArch() == Triple::thumb ||
977 Obj->getArch() == Triple::thumbeb));
978}
979
980class PrettyPrinter {
981public:
982 virtual ~PrettyPrinter() = default;
983 virtual void printInst(MCInstPrinter &IP, const MCInst *MI,
984 ArrayRef<uint8_t> Bytes, uint64_t Address,
985 raw_ostream &OS, StringRef Annot,
986 MCSubtargetInfo const &STI, SourcePrinter *SP,
987 std::vector<RelocationRef> *Rels = nullptr) {
988 if (SP && (PrintSource || PrintLines))
989 SP->printSourceLine(OS, Address);
990 if (!NoLeadingAddr)
991 OS << format("%8" PRIx64 ":", Address);
992 if (!NoShowRawInsn) {
993 OS << "\t";
994 dumpBytes(Bytes, OS);
995 }
996 if (MI)
997 IP.printInst(MI, OS, "", STI);
998 else
999 OS << " <unknown>";
1000 }
1001};
1002PrettyPrinter PrettyPrinterInst;
1003class HexagonPrettyPrinter : public PrettyPrinter {
1004public:
1005 void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address,
1006 raw_ostream &OS) {
1007 uint32_t opcode =
1008 (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0];
1009 if (!NoLeadingAddr)
1010 OS << format("%8" PRIx64 ":", Address);
1011 if (!NoShowRawInsn) {
1012 OS << "\t";
1013 dumpBytes(Bytes.slice(0, 4), OS);
1014 OS << format("%08" PRIx32, opcode);
1015 }
1016 }
1017 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1018 uint64_t Address, raw_ostream &OS, StringRef Annot,
1019 MCSubtargetInfo const &STI, SourcePrinter *SP,
1020 std::vector<RelocationRef> *Rels) override {
1021 if (SP && (PrintSource || PrintLines))
1022 SP->printSourceLine(OS, Address, "");
1023 if (!MI) {
1024 printLead(Bytes, Address, OS);
1025 OS << " <unknown>";
1026 return;
1027 }
1028 std::string Buffer;
1029 {
1030 raw_string_ostream TempStream(Buffer);
1031 IP.printInst(MI, TempStream, "", STI);
1032 }
1033 StringRef Contents(Buffer);
1034 // Split off bundle attributes
1035 auto PacketBundle = Contents.rsplit('\n');
1036 // Split off first instruction from the rest
1037 auto HeadTail = PacketBundle.first.split('\n');
1038 auto Preamble = " { ";
1039 auto Separator = "";
1040 StringRef Fmt = "\t\t\t%08" PRIx64 ": ";
1041 std::vector<RelocationRef>::const_iterator rel_cur = Rels->begin();
1042 std::vector<RelocationRef>::const_iterator rel_end = Rels->end();
1043
1044 // Hexagon's packets require relocations to be inline rather than
1045 // clustered at the end of the packet.
1046 auto PrintReloc = [&]() -> void {
1047 while ((rel_cur != rel_end) && (rel_cur->getOffset() <= Address)) {
1048 if (rel_cur->getOffset() == Address) {
1049 SmallString<16> name;
1050 SmallString<32> val;
1051 rel_cur->getTypeName(name);
1052 error(getRelocationValueString(*rel_cur, val));
1053 OS << Separator << format(Fmt.data(), Address) << name << "\t" << val
1054 << "\n";
1055 return;
1056 }
1057 rel_cur++;
1058 }
1059 };
1060
1061 while(!HeadTail.first.empty()) {
1062 OS << Separator;
1063 Separator = "\n";
1064 if (SP && (PrintSource || PrintLines))
1065 SP->printSourceLine(OS, Address, "");
1066 printLead(Bytes, Address, OS);
1067 OS << Preamble;
1068 Preamble = " ";
1069 StringRef Inst;
1070 auto Duplex = HeadTail.first.split('\v');
1071 if(!Duplex.second.empty()){
1072 OS << Duplex.first;
1073 OS << "; ";
1074 Inst = Duplex.second;
1075 }
1076 else
1077 Inst = HeadTail.first;
1078 OS << Inst;
1079 HeadTail = HeadTail.second.split('\n');
1080 if (HeadTail.first.empty())
1081 OS << " } " << PacketBundle.second;
1082 PrintReloc();
1083 Bytes = Bytes.slice(4);
1084 Address += 4;
1085 }
1086 }
1087};
1088HexagonPrettyPrinter HexagonPrettyPrinterInst;
1089
1090class AMDGCNPrettyPrinter : public PrettyPrinter {
1091public:
1092 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1093 uint64_t Address, raw_ostream &OS, StringRef Annot,
1094 MCSubtargetInfo const &STI, SourcePrinter *SP,
1095 std::vector<RelocationRef> *Rels) override {
1096 if (SP && (PrintSource || PrintLines))
1097 SP->printSourceLine(OS, Address);
1098
1099 typedef support::ulittle32_t U32;
1100
1101 if (MI) {
1102 SmallString<40> InstStr;
1103 raw_svector_ostream IS(InstStr);
1104
1105 IP.printInst(MI, IS, "", STI);
1106
1107 OS << left_justify(IS.str(), 60);
1108 } else {
1109 // an unrecognized encoding - this is probably data so represent it
1110 // using the .long directive, or .byte directive if fewer than 4 bytes
1111 // remaining
1112 if (Bytes.size() >= 4) {
1113 OS << format("\t.long 0x%08" PRIx32 " ",
1114 static_cast<uint32_t>(*reinterpret_cast<const U32*>(Bytes.data())));
1115 OS.indent(42);
1116 } else {
1117 OS << format("\t.byte 0x%02" PRIx8, Bytes[0]);
1118 for (unsigned int i = 1; i < Bytes.size(); i++)
1119 OS << format(", 0x%02" PRIx8, Bytes[i]);
1120 OS.indent(55 - (6 * Bytes.size()));
1121 }
1122 }
1123
1124 OS << format("// %012" PRIX64 ": ", Address);
1125 if (Bytes.size() >=4) {
1126 for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()),
1127 Bytes.size() / sizeof(U32)))
1128 // D should be explicitly casted to uint32_t here as it is passed
1129 // by format to snprintf as vararg.
1130 OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D));
1131 } else {
1132 for (unsigned int i = 0; i < Bytes.size(); i++)
1133 OS << format("%02" PRIX8 " ", Bytes[i]);
1134 }
1135
1136 if (!Annot.empty())
1137 OS << "// " << Annot;
1138 }
1139};
1140AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst;
1141
1142class BPFPrettyPrinter : public PrettyPrinter {
1143public:
1144 void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes,
1145 uint64_t Address, raw_ostream &OS, StringRef Annot,
1146 MCSubtargetInfo const &STI, SourcePrinter *SP,
1147 std::vector<RelocationRef> *Rels) override {
1148 if (SP && (PrintSource || PrintLines))
1149 SP->printSourceLine(OS, Address);
1150 if (!NoLeadingAddr)
1151 OS << format("%8" PRId64 ":", Address / 8);
1152 if (!NoShowRawInsn) {
1153 OS << "\t";
1154 dumpBytes(Bytes, OS);
1155 }
1156 if (MI)
1157 IP.printInst(MI, OS, "", STI);
1158 else
1159 OS << " <unknown>";
1160 }
1161};
1162BPFPrettyPrinter BPFPrettyPrinterInst;
1163
1164PrettyPrinter &selectPrettyPrinter(Triple const &Triple) {
1165 switch(Triple.getArch()) {
1166 default:
1167 return PrettyPrinterInst;
1168 case Triple::hexagon:
1169 return HexagonPrettyPrinterInst;
1170 case Triple::amdgcn:
1171 return AMDGCNPrettyPrinterInst;
1172 case Triple::bpfel:
1173 case Triple::bpfeb:
1174 return BPFPrettyPrinterInst;
1175 }
1176}
1177}
1178
Sam Koltonc05d7782016-08-17 10:17:57 +00001179static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) {
1180 assert(Obj->isELF());
1181 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1182 return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1183 if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1184 return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1185 if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1186 return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1187 if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1188 return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType();
1189 llvm_unreachable("Unsupported binary format");
1190}
1191
Sam Parker5fba45a2017-02-08 09:44:18 +00001192template <class ELFT> static void
1193addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj,
1194 std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
1195 for (auto Symbol : Obj->getDynamicSymbolIterators()) {
1196 uint8_t SymbolType = Symbol.getELFType();
1197 if (SymbolType != ELF::STT_FUNC || Symbol.getSize() == 0)
1198 continue;
1199
1200 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
1201 if (!AddressOrErr)
1202 report_error(Obj->getFileName(), AddressOrErr.takeError());
1203 uint64_t Address = *AddressOrErr;
1204
1205 Expected<StringRef> Name = Symbol.getName();
1206 if (!Name)
1207 report_error(Obj->getFileName(), Name.takeError());
1208 if (Name->empty())
1209 continue;
1210
1211 Expected<section_iterator> SectionOrErr = Symbol.getSection();
1212 if (!SectionOrErr)
1213 report_error(Obj->getFileName(), SectionOrErr.takeError());
1214 section_iterator SecI = *SectionOrErr;
1215 if (SecI == Obj->section_end())
1216 continue;
1217
1218 AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
1219 }
1220}
1221
1222static void
1223addDynamicElfSymbols(const ObjectFile *Obj,
1224 std::map<SectionRef, SectionSymbolsTy> &AllSymbols) {
1225 assert(Obj->isELF());
1226 if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj))
1227 addDynamicElfSymbols(Elf32LEObj, AllSymbols);
1228 else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj))
1229 addDynamicElfSymbols(Elf64LEObj, AllSymbols);
1230 else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj))
1231 addDynamicElfSymbols(Elf32BEObj, AllSymbols);
1232 else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj))
1233 addDynamicElfSymbols(Elf64BEObj, AllSymbols);
1234 else
1235 llvm_unreachable("Unsupported binary format");
1236}
1237
Michael J. Spencer51862b32011-10-13 22:17:18 +00001238static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001239 if (StartAddress > StopAddress)
1240 error("Start address should be less than stop address");
1241
Jim Grosbachaf9aec02012-08-07 17:53:14 +00001242 const Target *TheTarget = getTarget(Obj);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001243
Jack Carter551efd72012-08-28 19:24:49 +00001244 // Package up features to be passed to target/subtarget
Daniel Sanders1d148642016-06-16 09:17:03 +00001245 SubtargetFeatures Features = Obj->getFeatures();
Jack Carter551efd72012-08-28 19:24:49 +00001246 if (MAttrs.size()) {
Jack Carter551efd72012-08-28 19:24:49 +00001247 for (unsigned i = 0; i != MAttrs.size(); ++i)
1248 Features.AddFeature(MAttrs[i]);
Jack Carter551efd72012-08-28 19:24:49 +00001249 }
1250
Ahmed Charles56440fd2014-03-06 05:51:42 +00001251 std::unique_ptr<const MCRegisterInfo> MRI(
1252 TheTarget->createMCRegInfo(TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +00001253 if (!MRI)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001254 report_error(Obj->getFileName(), "no register info for target " +
1255 TripleName);
Ahmed Bougacha0835ca12013-05-16 21:28:23 +00001256
1257 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +00001258 std::unique_ptr<const MCAsmInfo> AsmInfo(
1259 TheTarget->createMCAsmInfo(*MRI, TripleName));
Davide Italiano711e4952015-12-17 01:59:50 +00001260 if (!AsmInfo)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001261 report_error(Obj->getFileName(), "no assembly info for target " +
1262 TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +00001263 std::unique_ptr<const MCSubtargetInfo> STI(
Daniel Sanders1d148642016-06-16 09:17:03 +00001264 TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString()));
Davide Italiano711e4952015-12-17 01:59:50 +00001265 if (!STI)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001266 report_error(Obj->getFileName(), "no subtarget info for target " +
1267 TripleName);
Ahmed Charles56440fd2014-03-06 05:51:42 +00001268 std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo());
Davide Italiano711e4952015-12-17 01:59:50 +00001269 if (!MII)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001270 report_error(Obj->getFileName(), "no instruction info for target " +
1271 TripleName);
Sam Kolton3381d7a2016-10-06 13:46:08 +00001272 MCObjectFileInfo MOFI;
1273 MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI);
1274 // FIXME: for now initialize MCObjectFileInfo with default values
Rafael Espindola9f929952017-08-02 20:32:26 +00001275 MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx);
Lang Hamesa1bc0f52014-04-15 04:40:56 +00001276
1277 std::unique_ptr<MCDisassembler> DisAsm(
1278 TheTarget->createMCDisassembler(*STI, Ctx));
Davide Italiano711e4952015-12-17 01:59:50 +00001279 if (!DisAsm)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001280 report_error(Obj->getFileName(), "no disassembler for target " +
1281 TripleName);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001282
Ahmed Charles56440fd2014-03-06 05:51:42 +00001283 std::unique_ptr<const MCInstrAnalysis> MIA(
1284 TheTarget->createMCInstrAnalysis(MII.get()));
Ahmed Bougachaaa790682013-05-24 01:07:04 +00001285
Ahmed Bougacha0835ca12013-05-16 21:28:23 +00001286 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Daniel Sanders50f17232015-09-15 16:17:27 +00001287 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
1288 Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
Davide Italiano711e4952015-12-17 01:59:50 +00001289 if (!IP)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001290 report_error(Obj->getFileName(), "no instruction printer for target " +
1291 TripleName);
Colin LeMahieu14ec76e2015-06-07 21:07:17 +00001292 IP->setPrintImmHex(PrintImmHex);
Colin LeMahieu35436a22015-05-29 14:48:25 +00001293 PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName));
Ahmed Bougacha0835ca12013-05-16 21:28:23 +00001294
Greg Fitzgerald18432272014-03-20 22:55:15 +00001295 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
1296 "\t\t\t%08" PRIx64 ": ";
1297
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001298 SourcePrinter SP(Obj, TheTarget->getName());
1299
Mark Seaborn0929d3d2014-01-25 17:38:19 +00001300 // Create a mapping, RelocSecs = SectionRelocMap[S], where sections
1301 // in RelocSecs contain the relocations for section S.
Rafael Espindola4453e42942014-06-13 03:07:50 +00001302 std::error_code EC;
Alexey Samsonov48803e52014-03-13 14:37:36 +00001303 std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001304 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001305 section_iterator Sec2 = Section.getRelocatedSection();
Rafael Espindolab5155a52014-02-10 20:24:04 +00001306 if (Sec2 != Obj->section_end())
Alexey Samsonov48803e52014-03-13 14:37:36 +00001307 SectionRelocMap[*Sec2].push_back(Section);
Mark Seaborn0929d3d2014-01-25 17:38:19 +00001308 }
1309
David Majnemer81afca62015-07-07 22:06:59 +00001310 // Create a mapping from virtual address to symbol name. This is used to
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001311 // pretty print the symbols while disassembling.
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001312 std::map<SectionRef, SectionSymbolsTy> AllSymbols;
Sterling Augustinebc78b622018-06-28 18:57:13 +00001313 SectionSymbolsTy AbsoluteSymbols;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001314 for (const SymbolRef &Symbol : Obj->symbols()) {
Kevin Enderby931cb652016-06-24 18:24:42 +00001315 Expected<uint64_t> AddressOrErr = Symbol.getAddress();
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001316 if (!AddressOrErr)
1317 report_error(Obj->getFileName(), AddressOrErr.takeError());
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001318 uint64_t Address = *AddressOrErr;
David Majnemer2603a8fa2015-07-09 18:11:40 +00001319
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001320 Expected<StringRef> Name = Symbol.getName();
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001321 if (!Name)
1322 report_error(Obj->getFileName(), Name.takeError());
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001323 if (Name->empty())
1324 continue;
David Majnemer81afca62015-07-07 22:06:59 +00001325
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001326 Expected<section_iterator> SectionOrErr = Symbol.getSection();
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001327 if (!SectionOrErr)
1328 report_error(Obj->getFileName(), SectionOrErr.takeError());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001329
Sam Koltonc05d7782016-08-17 10:17:57 +00001330 uint8_t SymbolType = ELF::STT_NOTYPE;
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001331 if (Obj->isELF())
Sam Koltonc05d7782016-08-17 10:17:57 +00001332 SymbolType = getElfSymbolType(Obj, Symbol);
David Majnemer81afca62015-07-07 22:06:59 +00001333
Sterling Augustinebc78b622018-06-28 18:57:13 +00001334 section_iterator SecI = *SectionOrErr;
1335 if (SecI != Obj->section_end())
1336 AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
1337 else
1338 AbsoluteSymbols.emplace_back(Address, *Name, SymbolType);
1339
Sam Koltonc05d7782016-08-17 10:17:57 +00001340
David Majnemer81afca62015-07-07 22:06:59 +00001341 }
Sam Parker5fba45a2017-02-08 09:44:18 +00001342 if (AllSymbols.empty() && Obj->isELF())
1343 addDynamicElfSymbols(Obj, AllSymbols);
David Majnemer81afca62015-07-07 22:06:59 +00001344
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001345 // Create a mapping from virtual address to section.
1346 std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses;
1347 for (SectionRef Sec : Obj->sections())
1348 SectionAddresses.emplace_back(Sec.getAddress(), Sec);
1349 array_pod_sort(SectionAddresses.begin(), SectionAddresses.end());
1350
1351 // Linked executables (.exe and .dll files) typically don't include a real
1352 // symbol table but they might contain an export table.
1353 if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) {
1354 for (const auto &ExportEntry : COFFObj->export_directories()) {
1355 StringRef Name;
1356 error(ExportEntry.getSymbolName(Name));
1357 if (Name.empty())
1358 continue;
1359 uint32_t RVA;
1360 error(ExportEntry.getExportRVA(RVA));
1361
1362 uint64_t VA = COFFObj->getImageBase() + RVA;
1363 auto Sec = std::upper_bound(
1364 SectionAddresses.begin(), SectionAddresses.end(), VA,
1365 [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
1366 return LHS < RHS.first;
1367 });
1368 if (Sec != SectionAddresses.begin())
1369 --Sec;
1370 else
1371 Sec = SectionAddresses.end();
1372
1373 if (Sec != SectionAddresses.end())
Sam Koltonc05d7782016-08-17 10:17:57 +00001374 AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
Sterling Augustinebc78b622018-06-28 18:57:13 +00001375 else
1376 AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001377 }
1378 }
1379
1380 // Sort all the symbols, this allows us to use a simple binary search to find
1381 // a symbol near an address.
1382 for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols)
1383 array_pod_sort(SecSyms.second.begin(), SecSyms.second.end());
Sterling Augustinebc78b622018-06-28 18:57:13 +00001384 array_pod_sort(AbsoluteSymbols.begin(), AbsoluteSymbols.end());
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001385
Colin LeMahieu77804be2015-07-29 15:45:39 +00001386 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Colin LeMahieuf34933e2015-07-23 20:58:49 +00001387 if (!DisassembleAll && (!Section.isText() || Section.isVirtual()))
Mark Seaborneb03ac52014-01-25 00:32:01 +00001388 continue;
Michael J. Spencer1d6167f2011-06-25 17:55:23 +00001389
Rafael Espindola80291272014-10-08 15:28:58 +00001390 uint64_t SectionAddr = Section.getAddress();
1391 uint64_t SectSize = Section.getSize();
David Majnemer185b5b12014-11-11 09:58:25 +00001392 if (!SectSize)
1393 continue;
Simon Atanasyan2b614e12014-02-24 22:12:11 +00001394
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001395 // Get the list of all the symbols in this section.
1396 SectionSymbolsTy &Symbols = AllSymbols[Section];
Davide Italianof0706882015-10-01 21:57:09 +00001397 std::vector<uint64_t> DataMappingSymsAddr;
1398 std::vector<uint64_t> TextMappingSymsAddr;
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001399 if (isArmElf(Obj)) {
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001400 for (const auto &Symb : Symbols) {
Sam Koltonc05d7782016-08-17 10:17:57 +00001401 uint64_t Address = std::get<0>(Symb);
1402 StringRef Name = std::get<1>(Symb);
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001403 if (Name.startswith("$d"))
David Majnemer153722d2015-11-18 04:35:32 +00001404 DataMappingSymsAddr.push_back(Address - SectionAddr);
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001405 if (Name.startswith("$x"))
David Majnemer153722d2015-11-18 04:35:32 +00001406 TextMappingSymsAddr.push_back(Address - SectionAddr);
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001407 if (Name.startswith("$a"))
1408 TextMappingSymsAddr.push_back(Address - SectionAddr);
1409 if (Name.startswith("$t"))
1410 TextMappingSymsAddr.push_back(Address - SectionAddr);
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001411 }
1412 }
1413
Mandeep Singh Grang8db564e2018-04-01 21:24:53 +00001414 llvm::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end());
1415 llvm::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end());
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001416
Sam Kolton3381d7a2016-10-06 13:46:08 +00001417 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
1418 // AMDGPU disassembler uses symbolizer for printing labels
1419 std::unique_ptr<MCRelocationInfo> RelInfo(
1420 TheTarget->createMCRelocationInfo(TripleName, Ctx));
1421 if (RelInfo) {
1422 std::unique_ptr<MCSymbolizer> Symbolizer(
1423 TheTarget->createMCSymbolizer(
1424 TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo)));
1425 DisAsm->setSymbolizer(std::move(Symbolizer));
1426 }
1427 }
1428
Michael J. Spencer51862b32011-10-13 22:17:18 +00001429 // Make a list of all the relocations for this section.
1430 std::vector<RelocationRef> Rels;
1431 if (InlineRelocs) {
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001432 for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
1433 for (const RelocationRef &Reloc : RelocSec.relocations()) {
1434 Rels.push_back(Reloc);
1435 }
Michael J. Spencer51862b32011-10-13 22:17:18 +00001436 }
1437 }
1438
1439 // Sort relocations by address.
Mandeep Singh Grang8db564e2018-04-01 21:24:53 +00001440 llvm::sort(Rels.begin(), Rels.end(), RelocAddressLess);
Michael J. Spencer51862b32011-10-13 22:17:18 +00001441
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001442 StringRef SegmentName = "";
Mark Seaborneb03ac52014-01-25 00:32:01 +00001443 if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001444 DataRefImpl DR = Section.getRawDataRefImpl();
Rafael Espindola56f976f2013-04-18 18:08:55 +00001445 SegmentName = MachO->getSectionFinalSegmentName(DR);
Rafael Espindolaa9f810b2012-12-21 03:47:03 +00001446 }
Rafael Aulerb0e4b912018-03-09 19:13:44 +00001447 StringRef SectionName;
1448 error(Section.getName(SectionName));
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001449
Rafael Espindola7884c952015-06-04 15:01:05 +00001450 // If the section has no symbol at the start, just insert a dummy one.
Sam Koltonc05d7782016-08-17 10:17:57 +00001451 if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) {
Rafael Aulerb0e4b912018-03-09 19:13:44 +00001452 Symbols.insert(
1453 Symbols.begin(),
1454 std::make_tuple(SectionAddr, SectionName,
1455 Section.isText() ? ELF::STT_FUNC : ELF::STT_OBJECT));
Sam Koltonc05d7782016-08-17 10:17:57 +00001456 }
Alp Tokere69170a2014-06-26 22:52:05 +00001457
1458 SmallString<40> Comments;
1459 raw_svector_ostream CommentStream(Comments);
Ahmed Bougachaad1084d2013-05-24 00:39:57 +00001460
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001461 StringRef BytesStr;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001462 error(Section.getContents(BytesStr));
Aaron Ballman106fd7b2014-11-12 14:01:17 +00001463 ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()),
1464 BytesStr.size());
Rafael Espindola7fc5b872014-11-12 02:04:27 +00001465
Michael J. Spencer2670c252011-01-20 06:39:06 +00001466 uint64_t Size;
1467 uint64_t Index;
Rafael Aulerb0e4b912018-03-09 19:13:44 +00001468 bool PrintedSection = false;
Michael J. Spencer2670c252011-01-20 06:39:06 +00001469
Michael J. Spencer51862b32011-10-13 22:17:18 +00001470 std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin();
1471 std::vector<RelocationRef>::const_iterator rel_end = Rels.end();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001472 // Disassemble symbol by symbol.
1473 for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
Sam Koltonc05d7782016-08-17 10:17:57 +00001474 uint64_t Start = std::get<0>(Symbols[si]) - SectionAddr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001475 // The end is either the section end or the beginning of the next
1476 // symbol.
1477 uint64_t End =
Sam Koltonc05d7782016-08-17 10:17:57 +00001478 (si == se - 1) ? SectSize : std::get<0>(Symbols[si + 1]) - SectionAddr;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001479 // Don't try to disassemble beyond the end of section contents.
1480 if (End > SectSize)
1481 End = SectSize;
Rafael Espindolae45c7402014-08-17 16:31:39 +00001482 // If this symbol has the same address as the next symbol, then skip it.
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001483 if (Start >= End)
Michael J. Spenceree84f642011-10-13 20:37:08 +00001484 continue;
1485
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001486 // Check if we need to skip symbol
1487 // Skip if the symbol's data is not between StartAddress and StopAddress
1488 if (End + SectionAddr < StartAddress ||
1489 Start + SectionAddr > StopAddress) {
1490 continue;
1491 }
1492
Rafael Aulerb0e4b912018-03-09 19:13:44 +00001493 /// Skip if user requested specific symbols and this is not in the list
1494 if (!DisasmFuncsSet.empty() &&
1495 !DisasmFuncsSet.count(std::get<1>(Symbols[si])))
1496 continue;
1497
1498 if (!PrintedSection) {
1499 PrintedSection = true;
1500 outs() << "Disassembly of section ";
1501 if (!SegmentName.empty())
1502 outs() << SegmentName << ",";
1503 outs() << SectionName << ':';
1504 }
1505
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001506 // Stop disassembly at the stop address specified
1507 if (End + SectionAddr > StopAddress)
1508 End = StopAddress - SectionAddr;
1509
Valery Pykhtinde048052016-04-07 07:24:01 +00001510 if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) {
Sam Koltonc05d7782016-08-17 10:17:57 +00001511 if (std::get<2>(Symbols[si]) == ELF::STT_AMDGPU_HSA_KERNEL) {
1512 // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes)
1513 Start += 256;
1514 }
1515 if (si == se - 1 ||
1516 std::get<2>(Symbols[si + 1]) == ELF::STT_AMDGPU_HSA_KERNEL) {
1517 // cut trailing zeroes at the end of kernel
1518 // cut up to 256 bytes
1519 const uint64_t EndAlign = 256;
1520 const auto Limit = End - (std::min)(EndAlign, End - Start);
1521 while (End > Limit &&
1522 *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0)
1523 End -= 4;
1524 }
Valery Pykhtinde048052016-04-07 07:24:01 +00001525 }
1526
Paul Semel007dedb2018-07-18 16:39:21 +00001527 auto PrintSymbol = [](StringRef Name) {
1528 outs() << '\n' << Name << ":\n";
1529 };
1530 StringRef SymbolName = std::get<1>(Symbols[si]);
1531 if (Demangle.getValue() == "" || Demangle.getValue() == "itanium") {
1532 char *DemangledSymbol = nullptr;
1533 size_t Size = 0;
1534 int Status;
1535 DemangledSymbol =
1536 itaniumDemangle(SymbolName.data(), DemangledSymbol, &Size, &Status);
1537 if (Status == 0)
1538 PrintSymbol(StringRef(DemangledSymbol));
1539 else
1540 PrintSymbol(SymbolName);
1541
1542 if (Size != 0)
1543 free(DemangledSymbol);
1544 } else
1545 PrintSymbol(SymbolName);
Michael J. Spencer2670c252011-01-20 06:39:06 +00001546
Francis Visoiu Mistrih18346822018-04-19 17:02:57 +00001547 // Don't print raw contents of a virtual section. A virtual section
1548 // doesn't have any contents in the file.
1549 if (Section.isVirtual()) {
1550 outs() << "...\n";
1551 continue;
1552 }
1553
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001554#ifndef NDEBUG
Mark Seaborneb03ac52014-01-25 00:32:01 +00001555 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001556#else
Mark Seaborneb03ac52014-01-25 00:32:01 +00001557 raw_ostream &DebugOut = nulls();
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001558#endif
1559
Benjamin Kramer43a772e2011-09-19 17:56:04 +00001560 for (Index = Start; Index < End; Index += Size) {
1561 MCInst Inst;
Owen Andersona0c3b972011-09-15 23:38:46 +00001562
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001563 if (Index + SectionAddr < StartAddress ||
1564 Index + SectionAddr > StopAddress) {
1565 // skip byte by byte till StartAddress is reached
1566 Size = 1;
1567 continue;
1568 }
Davide Italianof0706882015-10-01 21:57:09 +00001569 // AArch64 ELF binaries can interleave data and text in the
1570 // same section. We rely on the markers introduced to
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001571 // understand what we need to dump. If the data marker is within a
1572 // function, it is denoted as a word/short etc
1573 if (isArmElf(Obj) && std::get<2>(Symbols[si]) != ELF::STT_OBJECT &&
1574 !DisassembleAll) {
Davide Italianof0706882015-10-01 21:57:09 +00001575 uint64_t Stride = 0;
1576
1577 auto DAI = std::lower_bound(DataMappingSymsAddr.begin(),
1578 DataMappingSymsAddr.end(), Index);
1579 if (DAI != DataMappingSymsAddr.end() && *DAI == Index) {
1580 // Switch to data.
1581 while (Index < End) {
1582 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1583 outs() << "\t";
1584 if (Index + 4 <= End) {
1585 Stride = 4;
1586 dumpBytes(Bytes.slice(Index, 4), outs());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001587 outs() << "\t.word\t";
1588 uint32_t Data = 0;
1589 if (Obj->isLittleEndian()) {
1590 const auto Word =
1591 reinterpret_cast<const support::ulittle32_t *>(
1592 Bytes.data() + Index);
1593 Data = *Word;
1594 } else {
1595 const auto Word = reinterpret_cast<const support::ubig32_t *>(
1596 Bytes.data() + Index);
1597 Data = *Word;
1598 }
1599 outs() << "0x" << format("%08" PRIx32, Data);
Davide Italianof0706882015-10-01 21:57:09 +00001600 } else if (Index + 2 <= End) {
1601 Stride = 2;
1602 dumpBytes(Bytes.slice(Index, 2), outs());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001603 outs() << "\t\t.short\t";
1604 uint16_t Data = 0;
1605 if (Obj->isLittleEndian()) {
1606 const auto Short =
1607 reinterpret_cast<const support::ulittle16_t *>(
1608 Bytes.data() + Index);
1609 Data = *Short;
1610 } else {
1611 const auto Short =
1612 reinterpret_cast<const support::ubig16_t *>(Bytes.data() +
1613 Index);
1614 Data = *Short;
1615 }
1616 outs() << "0x" << format("%04" PRIx16, Data);
Davide Italianof0706882015-10-01 21:57:09 +00001617 } else {
1618 Stride = 1;
1619 dumpBytes(Bytes.slice(Index, 1), outs());
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001620 outs() << "\t\t.byte\t";
1621 outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]);
Davide Italianof0706882015-10-01 21:57:09 +00001622 }
1623 Index += Stride;
1624 outs() << "\n";
1625 auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
1626 TextMappingSymsAddr.end(), Index);
1627 if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
1628 break;
1629 }
1630 }
1631 }
1632
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001633 // If there is a data symbol inside an ELF text section and we are only
1634 // disassembling text (applicable all architectures),
1635 // we are in a situation where we must print the data and not
1636 // disassemble it.
1637 if (Obj->isELF() && std::get<2>(Symbols[si]) == ELF::STT_OBJECT &&
1638 !DisassembleAll && Section.isText()) {
1639 // print out data up to 8 bytes at a time in hex and ascii
1640 uint8_t AsciiData[9] = {'\0'};
1641 uint8_t Byte;
1642 int NumBytes = 0;
1643
1644 for (Index = Start; Index < End; Index += 1) {
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001645 if (((SectionAddr + Index) < StartAddress) ||
1646 ((SectionAddr + Index) > StopAddress))
1647 continue;
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001648 if (NumBytes == 0) {
1649 outs() << format("%8" PRIx64 ":", SectionAddr + Index);
1650 outs() << "\t";
1651 }
1652 Byte = Bytes.slice(Index)[0];
1653 outs() << format(" %02x", Byte);
1654 AsciiData[NumBytes] = isprint(Byte) ? Byte : '.';
1655
1656 uint8_t IndentOffset = 0;
1657 NumBytes++;
1658 if (Index == End - 1 || NumBytes > 8) {
1659 // Indent the space for less than 8 bytes data.
1660 // 2 spaces for byte and one for space between bytes
1661 IndentOffset = 3 * (8 - NumBytes);
1662 for (int Excess = 8 - NumBytes; Excess < 8; Excess++)
1663 AsciiData[Excess] = '\0';
1664 NumBytes = 8;
1665 }
1666 if (NumBytes == 8) {
1667 AsciiData[8] = '\0';
1668 outs() << std::string(IndentOffset, ' ') << " ";
1669 outs() << reinterpret_cast<char *>(AsciiData);
1670 outs() << '\n';
1671 NumBytes = 0;
1672 }
1673 }
1674 }
Davide Italianof0706882015-10-01 21:57:09 +00001675 if (Index >= End)
1676 break;
1677
Hemant Kulkarni5b60f632016-08-25 19:41:08 +00001678 // Disassemble a real instruction or a data when disassemble all is
1679 // provided
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001680 bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index),
1681 SectionAddr + Index, DebugOut,
1682 CommentStream);
1683 if (Size == 0)
1684 Size = 1;
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001685
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001686 PIP.printInst(*IP, Disassembled ? &Inst : nullptr,
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00001687 Bytes.slice(Index, Size), SectionAddr + Index, outs(), "",
Sid Manningd9f28732018-05-14 19:46:08 +00001688 *STI, &SP, &Rels);
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001689 outs() << CommentStream.str();
1690 Comments.clear();
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001691
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001692 // Try to resolve the target of a call, tail call, etc. to a specific
1693 // symbol.
1694 if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) ||
1695 MIA->isConditionalBranch(Inst))) {
1696 uint64_t Target;
1697 if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) {
1698 // In a relocatable object, the target's section must reside in
1699 // the same section as the call instruction or it is accessed
1700 // through a relocation.
1701 //
1702 // In a non-relocatable object, the target may be in any section.
1703 //
1704 // N.B. We don't walk the relocations in the relocatable case yet.
1705 auto *TargetSectionSymbols = &Symbols;
1706 if (!Obj->isRelocatableObject()) {
1707 auto SectionAddress = std::upper_bound(
1708 SectionAddresses.begin(), SectionAddresses.end(), Target,
1709 [](uint64_t LHS,
1710 const std::pair<uint64_t, SectionRef> &RHS) {
1711 return LHS < RHS.first;
1712 });
1713 if (SectionAddress != SectionAddresses.begin()) {
1714 --SectionAddress;
1715 TargetSectionSymbols = &AllSymbols[SectionAddress->second];
1716 } else {
Sterling Augustinebc78b622018-06-28 18:57:13 +00001717 TargetSectionSymbols = &AbsoluteSymbols;
David Majnemerfbb1c3a2015-11-18 02:49:19 +00001718 }
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001719 }
David Majnemer2603a8fa2015-07-09 18:11:40 +00001720
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001721 // Find the first symbol in the section whose offset is less than
Sterling Augustinebc78b622018-06-28 18:57:13 +00001722 // or equal to the target. If there isn't a section that contains
1723 // the target, find the nearest preceding absolute symbol.
1724 auto TargetSym = std::upper_bound(
1725 TargetSectionSymbols->begin(), TargetSectionSymbols->end(),
1726 Target, [](uint64_t LHS,
1727 const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
1728 return LHS < std::get<0>(RHS);
1729 });
1730 if (TargetSym == TargetSectionSymbols->begin()) {
1731 TargetSectionSymbols = &AbsoluteSymbols;
1732 TargetSym = std::upper_bound(
1733 AbsoluteSymbols.begin(), AbsoluteSymbols.end(),
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001734 Target, [](uint64_t LHS,
Sam Koltonc05d7782016-08-17 10:17:57 +00001735 const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
Sterling Augustinebc78b622018-06-28 18:57:13 +00001736 return LHS < std::get<0>(RHS);
1737 });
1738 }
1739 if (TargetSym != TargetSectionSymbols->begin()) {
1740 --TargetSym;
1741 uint64_t TargetAddress = std::get<0>(*TargetSym);
1742 StringRef TargetName = std::get<1>(*TargetSym);
1743 outs() << " <" << TargetName;
1744 uint64_t Disp = Target - TargetAddress;
1745 if (Disp)
1746 outs() << "+0x" << Twine::utohexstr(Disp);
1747 outs() << '>';
David Majnemer81afca62015-07-07 22:06:59 +00001748 }
1749 }
Benjamin Kramere0dda9c2011-07-15 18:39:24 +00001750 }
Colin LeMahieu307a83d2016-03-18 16:26:48 +00001751 outs() << "\n";
Michael J. Spencer51862b32011-10-13 22:17:18 +00001752
Sid Manningd9f28732018-05-14 19:46:08 +00001753 // Hexagon does this in pretty printer
1754 if (Obj->getArch() != Triple::hexagon)
1755 // Print relocation for instruction.
1756 while (rel_cur != rel_end) {
1757 bool hidden = getHidden(*rel_cur);
1758 uint64_t addr = rel_cur->getOffset();
1759 SmallString<16> name;
1760 SmallString<32> val;
Owen Andersonfa3e5202011-10-25 20:35:53 +00001761
Sid Manningd9f28732018-05-14 19:46:08 +00001762 // If this relocation is hidden, skip it.
1763 if (hidden || ((SectionAddr + addr) < StartAddress)) {
1764 ++rel_cur;
1765 continue;
1766 }
1767
1768 // Stop when rel_cur's address is past the current instruction.
1769 if (addr >= Index + Size) break;
1770 rel_cur->getTypeName(name);
1771 error(getRelocationValueString(*rel_cur, val));
1772 outs() << format(Fmt.data(), SectionAddr + addr) << name
1773 << "\t" << val << "\n";
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001774 ++rel_cur;
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001775 }
Benjamin Kramer87ee76c2011-07-20 19:37:35 +00001776 }
Michael J. Spencer2670c252011-01-20 06:39:06 +00001777 }
1778 }
1779}
1780
Kevin Enderby98da6132015-01-20 21:47:46 +00001781void llvm::PrintRelocations(const ObjectFile *Obj) {
Greg Fitzgerald18432272014-03-20 22:55:15 +00001782 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
1783 "%08" PRIx64;
Rafael Espindola9219fe72016-03-21 20:59:15 +00001784 // Regular objdump doesn't print relocations in non-relocatable object
1785 // files.
1786 if (!Obj->isRelocatableObject())
1787 return;
Rafael Espindolac66d7612014-08-17 19:09:37 +00001788
Colin LeMahieu77804be2015-07-29 15:45:39 +00001789 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Alexey Samsonov48803e52014-03-13 14:37:36 +00001790 if (Section.relocation_begin() == Section.relocation_end())
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001791 continue;
1792 StringRef secname;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001793 error(Section.getName(secname));
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001794 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001795 for (const RelocationRef &Reloc : Section.relocations()) {
Rafael Espindola0ad71d92015-06-30 03:41:26 +00001796 bool hidden = getHidden(Reloc);
Rafael Espindola96d071c2015-06-29 23:29:12 +00001797 uint64_t address = Reloc.getOffset();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001798 SmallString<32> relocname;
1799 SmallString<32> valuestr;
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001800 if (address < StartAddress || address > StopAddress || hidden)
Alexey Samsonovaa4d2952014-03-14 14:22:49 +00001801 continue;
Rafael Espindola41bb4322015-06-30 04:08:37 +00001802 Reloc.getTypeName(relocname);
Davide Italianoccd53fe2015-08-05 07:18:31 +00001803 error(getRelocationValueString(Reloc, valuestr));
Greg Fitzgerald18432272014-03-20 22:55:15 +00001804 outs() << format(Fmt.data(), address) << " " << relocname << " "
1805 << valuestr << "\n";
Michael J. Spencerba4a3622011-10-08 00:18:30 +00001806 }
1807 outs() << "\n";
1808 }
1809}
1810
Paul Semelcb0f0432018-06-07 13:30:55 +00001811void llvm::PrintDynamicRelocations(const ObjectFile *Obj) {
1812
1813 // For the moment, this option is for ELF only
1814 if (!Obj->isELF())
1815 return;
1816
1817 const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj);
1818
1819 if (!Elf || Elf->getEType() != ELF::ET_DYN) {
1820 error("not a dynamic object");
1821 return;
1822 }
1823
1824 StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64;
1825
1826 std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections();
1827 if (DynRelSec.empty())
1828 return;
1829
1830 outs() << "DYNAMIC RELOCATION RECORDS\n";
1831 for (const SectionRef &Section : DynRelSec) {
1832 if (Section.relocation_begin() == Section.relocation_end())
1833 continue;
1834 for (const RelocationRef &Reloc : Section.relocations()) {
1835 uint64_t address = Reloc.getOffset();
1836 SmallString<32> relocname;
1837 SmallString<32> valuestr;
1838 Reloc.getTypeName(relocname);
1839 error(getRelocationValueString(Reloc, valuestr));
1840 outs() << format(Fmt.data(), address) << " " << relocname << " "
1841 << valuestr << "\n";
1842 }
1843 }
1844}
1845
Kevin Enderby98da6132015-01-20 21:47:46 +00001846void llvm::PrintSectionHeaders(const ObjectFile *Obj) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001847 outs() << "Sections:\n"
1848 "Idx Name Size Address Type\n";
Colin LeMahieu77804be2015-07-29 15:45:39 +00001849 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001850 StringRef Name;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001851 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001852 uint64_t Address = Section.getAddress();
1853 uint64_t Size = Section.getSize();
1854 bool Text = Section.isText();
1855 bool Data = Section.isData();
1856 bool BSS = Section.isBSS();
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001857 std::string Type = (std::string(Text ? "TEXT " : "") +
Michael J. Spencer8f67d472011-10-13 20:37:20 +00001858 (Data ? "DATA " : "") + (BSS ? "BSS" : ""));
George Rimare35e6442018-07-18 08:34:35 +00001859 outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n",
George Rimarc1090da2018-07-18 09:25:36 +00001860 (unsigned)Section.getIndex(), Name.str().c_str(), Size,
1861 Address, Type.c_str());
Nick Lewyckyfcf84622011-10-10 21:21:34 +00001862 }
1863}
1864
Kevin Enderby98da6132015-01-20 21:47:46 +00001865void llvm::PrintSectionContents(const ObjectFile *Obj) {
Rafael Espindola4453e42942014-06-13 03:07:50 +00001866 std::error_code EC;
Colin LeMahieu77804be2015-07-29 15:45:39 +00001867 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001868 StringRef Name;
1869 StringRef Contents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001870 error(Section.getName(Name));
Rafael Espindola80291272014-10-08 15:28:58 +00001871 uint64_t BaseAddr = Section.getAddress();
David Majnemer185b5b12014-11-11 09:58:25 +00001872 uint64_t Size = Section.getSize();
1873 if (!Size)
1874 continue;
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001875
1876 outs() << "Contents of section " << Name << ":\n";
David Majnemer185b5b12014-11-11 09:58:25 +00001877 if (Section.isBSS()) {
Alexey Samsonov209095c2013-04-16 10:53:11 +00001878 outs() << format("<skipping contents of bss section at [%04" PRIx64
David Majnemer8f6b04c2014-07-14 16:20:14 +00001879 ", %04" PRIx64 ")>\n",
1880 BaseAddr, BaseAddr + Size);
Alexey Samsonov209095c2013-04-16 10:53:11 +00001881 continue;
1882 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001883
Davide Italianoccd53fe2015-08-05 07:18:31 +00001884 error(Section.getContents(Contents));
David Majnemer8f6b04c2014-07-14 16:20:14 +00001885
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001886 // Dump out the content as hex and printable ascii characters.
1887 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
Benjamin Kramer82803112012-03-10 02:04:38 +00001888 outs() << format(" %04" PRIx64 " ", BaseAddr + addr);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001889 // Dump line of hex.
1890 for (std::size_t i = 0; i < 16; ++i) {
1891 if (i != 0 && i % 4 == 0)
1892 outs() << ' ';
1893 if (addr + i < end)
1894 outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true)
1895 << hexdigit(Contents[addr + i] & 0xF, true);
1896 else
1897 outs() << " ";
1898 }
1899 // Print ascii.
1900 outs() << " ";
1901 for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
Guy Benyei83c74e92013-02-12 21:21:59 +00001902 if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
Michael J. Spencer4e25c022011-10-17 17:13:22 +00001903 outs() << Contents[addr + i];
1904 else
1905 outs() << ".";
1906 }
1907 outs() << "\n";
1908 }
1909 }
1910}
1911
Kevin Enderby9acb1092016-05-31 20:35:34 +00001912void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName,
1913 StringRef ArchitectureName) {
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001914 outs() << "SYMBOL TABLE:\n";
1915
Rui Ueyama4e39f712014-03-18 18:58:51 +00001916 if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
Davide Italianoe85abf72015-12-20 09:54:34 +00001917 printCOFFSymbolTable(coff);
Rui Ueyama4e39f712014-03-18 18:58:51 +00001918 return;
1919 }
1920 for (const SymbolRef &Symbol : o->symbols()) {
Kevin Enderby931cb652016-06-24 18:24:42 +00001921 Expected<uint64_t> AddressOrError = Symbol.getAddress();
1922 if (!AddressOrError)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001923 report_error(ArchiveName, o->getFileName(), AddressOrError.takeError(),
1924 ArchitectureName);
Rafael Espindolaed067c42015-07-03 18:19:00 +00001925 uint64_t Address = *AddressOrError;
Hemant Kulkarniaecf9d02016-09-12 17:08:22 +00001926 if ((Address < StartAddress) || (Address > StopAddress))
1927 continue;
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001928 Expected<SymbolRef::Type> TypeOrError = Symbol.getType();
1929 if (!TypeOrError)
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001930 report_error(ArchiveName, o->getFileName(), TypeOrError.takeError(),
1931 ArchitectureName);
Kevin Enderby5afbc1c2016-03-23 20:27:00 +00001932 SymbolRef::Type Type = *TypeOrError;
Rui Ueyama4e39f712014-03-18 18:58:51 +00001933 uint32_t Flags = Symbol.getFlags();
Kevin Enderby7bd8d992016-05-02 20:28:12 +00001934 Expected<section_iterator> SectionOrErr = Symbol.getSection();
Kevin Enderby7fa40c92016-11-16 22:17:38 +00001935 if (!SectionOrErr)
1936 report_error(ArchiveName, o->getFileName(), SectionOrErr.takeError(),
1937 ArchitectureName);
Rafael Espindola8bab8892015-08-07 23:27:14 +00001938 section_iterator Section = *SectionOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001939 StringRef Name;
1940 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
1941 Section->getName(Name);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001942 } else {
Kevin Enderby81e8b7d2016-04-20 21:24:34 +00001943 Expected<StringRef> NameOrErr = Symbol.getName();
1944 if (!NameOrErr)
Kevin Enderby9acb1092016-05-31 20:35:34 +00001945 report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(),
1946 ArchitectureName);
Rafael Espindola5d0c2ff2015-07-02 20:55:21 +00001947 Name = *NameOrErr;
Rafael Espindola75d5b542015-06-03 05:14:22 +00001948 }
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001949
Rui Ueyama4e39f712014-03-18 18:58:51 +00001950 bool Global = Flags & SymbolRef::SF_Global;
1951 bool Weak = Flags & SymbolRef::SF_Weak;
1952 bool Absolute = Flags & SymbolRef::SF_Absolute;
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001953 bool Common = Flags & SymbolRef::SF_Common;
Davide Italianocd2514d2015-04-30 23:08:53 +00001954 bool Hidden = Flags & SymbolRef::SF_Hidden;
David Meyer1df4b842012-02-28 23:47:53 +00001955
Rui Ueyama4e39f712014-03-18 18:58:51 +00001956 char GlobLoc = ' ';
1957 if (Type != SymbolRef::ST_Unknown)
1958 GlobLoc = Global ? 'g' : 'l';
1959 char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
1960 ? 'd' : ' ';
1961 char FileFunc = ' ';
1962 if (Type == SymbolRef::ST_File)
1963 FileFunc = 'f';
1964 else if (Type == SymbolRef::ST_Function)
1965 FileFunc = 'F';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001966
Rui Ueyama4e39f712014-03-18 18:58:51 +00001967 const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
1968 "%08" PRIx64;
Michael J. Spencerd857c1c2013-01-10 22:40:50 +00001969
Rui Ueyama4e39f712014-03-18 18:58:51 +00001970 outs() << format(Fmt, Address) << " "
1971 << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
1972 << (Weak ? 'w' : ' ') // Weak?
1973 << ' ' // Constructor. Not supported yet.
1974 << ' ' // Warning. Not supported yet.
1975 << ' ' // Indirect reference to another symbol.
1976 << Debug // Debugging (d) or dynamic (D) symbol.
1977 << FileFunc // Name of function (F), file (f) or object (O).
1978 << ' ';
1979 if (Absolute) {
1980 outs() << "*ABS*";
Colin LeMahieubc2f47a2015-01-23 20:06:24 +00001981 } else if (Common) {
1982 outs() << "*COM*";
Rui Ueyama4e39f712014-03-18 18:58:51 +00001983 } else if (Section == o->section_end()) {
1984 outs() << "*UND*";
1985 } else {
1986 if (const MachOObjectFile *MachO =
1987 dyn_cast<const MachOObjectFile>(o)) {
1988 DataRefImpl DR = Section->getRawDataRefImpl();
1989 StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
1990 outs() << SegmentName << ",";
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001991 }
Rui Ueyama4e39f712014-03-18 18:58:51 +00001992 StringRef SectionName;
Davide Italianoccd53fe2015-08-05 07:18:31 +00001993 error(Section->getName(SectionName));
Rui Ueyama4e39f712014-03-18 18:58:51 +00001994 outs() << SectionName;
Michael J. Spencerbfa06782011-10-18 19:32:17 +00001995 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00001996
1997 outs() << '\t';
Rafael Espindolaae3ac082015-06-23 18:34:25 +00001998 if (Common || isa<ELFObjectFileBase>(o)) {
Rafael Espindoladbb6bd32015-06-25 22:10:04 +00001999 uint64_t Val =
2000 Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize();
Rafael Espindolaae3ac082015-06-23 18:34:25 +00002001 outs() << format("\t %08" PRIx64 " ", Val);
2002 }
Rafael Espindola5f7ade22015-06-23 15:45:38 +00002003
Davide Italianocd2514d2015-04-30 23:08:53 +00002004 if (Hidden) {
2005 outs() << ".hidden ";
2006 }
2007 outs() << Name
Rui Ueyama4e39f712014-03-18 18:58:51 +00002008 << '\n';
Michael J. Spencerbfa06782011-10-18 19:32:17 +00002009 }
2010}
2011
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00002012static void PrintUnwindInfo(const ObjectFile *o) {
2013 outs() << "Unwind info:\n\n";
2014
2015 if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) {
2016 printCOFFUnwindInfo(coff);
Tim Northover4bd286a2014-08-01 13:07:19 +00002017 } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2018 printMachOUnwindInfo(MachO);
2019 else {
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00002020 // TODO: Extract DWARF dump tool to objdump.
2021 errs() << "This operation is only currently supported "
Tim Northover4bd286a2014-08-01 13:07:19 +00002022 "for COFF and MachO object files.\n";
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00002023 return;
2024 }
2025}
2026
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002027void llvm::printExportsTrie(const ObjectFile *o) {
Nick Kledzikd04bc352014-08-30 00:20:14 +00002028 outs() << "Exports trie:\n";
2029 if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
2030 printMachOExportsTrie(MachO);
2031 else {
2032 errs() << "This operation is only currently supported "
2033 "for Mach-O executable files.\n";
2034 return;
2035 }
2036}
2037
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002038void llvm::printRebaseTable(ObjectFile *o) {
Nick Kledzikac431442014-09-12 21:34:15 +00002039 outs() << "Rebase table:\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002040 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
Nick Kledzikac431442014-09-12 21:34:15 +00002041 printMachORebaseTable(MachO);
2042 else {
2043 errs() << "This operation is only currently supported "
2044 "for Mach-O executable files.\n";
2045 return;
2046 }
2047}
2048
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002049void llvm::printBindTable(ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00002050 outs() << "Bind table:\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002051 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
Nick Kledzik56ebef42014-09-16 01:41:51 +00002052 printMachOBindTable(MachO);
2053 else {
2054 errs() << "This operation is only currently supported "
2055 "for Mach-O executable files.\n";
2056 return;
2057 }
2058}
2059
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002060void llvm::printLazyBindTable(ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00002061 outs() << "Lazy bind table:\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002062 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
Nick Kledzik56ebef42014-09-16 01:41:51 +00002063 printMachOLazyBindTable(MachO);
2064 else {
2065 errs() << "This operation is only currently supported "
2066 "for Mach-O executable files.\n";
2067 return;
2068 }
2069}
2070
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002071void llvm::printWeakBindTable(ObjectFile *o) {
Nick Kledzik56ebef42014-09-16 01:41:51 +00002072 outs() << "Weak bind table:\n";
Kevin Enderbya8d256c2017-03-20 19:46:55 +00002073 if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
Nick Kledzik56ebef42014-09-16 01:41:51 +00002074 printMachOWeakBindTable(MachO);
2075 else {
2076 errs() << "This operation is only currently supported "
2077 "for Mach-O executable files.\n";
2078 return;
2079 }
2080}
Nick Kledzikac431442014-09-12 21:34:15 +00002081
Adrian Prantl437105a2015-07-08 02:04:15 +00002082/// Dump the raw contents of the __clangast section so the output can be piped
2083/// into llvm-bcanalyzer.
2084void llvm::printRawClangAST(const ObjectFile *Obj) {
2085 if (outs().is_displayed()) {
2086 errs() << "The -raw-clang-ast option will dump the raw binary contents of "
2087 "the clang ast section.\n"
2088 "Please redirect the output to a file or another program such as "
2089 "llvm-bcanalyzer.\n";
2090 return;
2091 }
2092
2093 StringRef ClangASTSectionName("__clangast");
2094 if (isa<COFFObjectFile>(Obj)) {
2095 ClangASTSectionName = "clangast";
2096 }
2097
2098 Optional<object::SectionRef> ClangASTSection;
Colin LeMahieu77804be2015-07-29 15:45:39 +00002099 for (auto Sec : ToolSectionFilter(*Obj)) {
Adrian Prantl437105a2015-07-08 02:04:15 +00002100 StringRef Name;
2101 Sec.getName(Name);
2102 if (Name == ClangASTSectionName) {
2103 ClangASTSection = Sec;
2104 break;
2105 }
2106 }
2107 if (!ClangASTSection)
2108 return;
2109
2110 StringRef ClangASTContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00002111 error(ClangASTSection.getValue().getContents(ClangASTContents));
Adrian Prantl437105a2015-07-08 02:04:15 +00002112 outs().write(ClangASTContents.data(), ClangASTContents.size());
2113}
2114
Sanjoy Das6f567a42015-06-22 18:03:02 +00002115static void printFaultMaps(const ObjectFile *Obj) {
2116 const char *FaultMapSectionName = nullptr;
2117
2118 if (isa<ELFObjectFileBase>(Obj)) {
2119 FaultMapSectionName = ".llvm_faultmaps";
2120 } else if (isa<MachOObjectFile>(Obj)) {
2121 FaultMapSectionName = "__llvm_faultmaps";
2122 } else {
2123 errs() << "This operation is only currently supported "
2124 "for ELF and Mach-O executable files.\n";
2125 return;
2126 }
2127
2128 Optional<object::SectionRef> FaultMapSection;
2129
Colin LeMahieu77804be2015-07-29 15:45:39 +00002130 for (auto Sec : ToolSectionFilter(*Obj)) {
Sanjoy Das6f567a42015-06-22 18:03:02 +00002131 StringRef Name;
2132 Sec.getName(Name);
2133 if (Name == FaultMapSectionName) {
2134 FaultMapSection = Sec;
2135 break;
2136 }
2137 }
2138
2139 outs() << "FaultMap table:\n";
2140
2141 if (!FaultMapSection.hasValue()) {
2142 outs() << "<not found>\n";
2143 return;
2144 }
2145
2146 StringRef FaultMapContents;
Davide Italianoccd53fe2015-08-05 07:18:31 +00002147 error(FaultMapSection.getValue().getContents(FaultMapContents));
Sanjoy Das6f567a42015-06-22 18:03:02 +00002148
2149 FaultMapParser FMP(FaultMapContents.bytes_begin(),
2150 FaultMapContents.bytes_end());
2151
2152 outs() << FMP;
2153}
2154
Davide Italiano1bdaa202016-09-18 04:39:15 +00002155static void printPrivateFileHeaders(const ObjectFile *o, bool onlyFirst) {
Paul Semel0913dcd2018-07-25 11:09:20 +00002156 if (o->isELF()) {
2157 printELFFileHeader(o);
2158 return printELFDynamicSection(o);
2159 }
Davide Italiano1bdaa202016-09-18 04:39:15 +00002160 if (o->isCOFF())
2161 return printCOFFFileHeader(o);
Derek Schuff2c6f75d2016-11-30 16:49:11 +00002162 if (o->isWasm())
2163 return printWasmFileHeader(o);
Davide Italiano1bdaa202016-09-18 04:39:15 +00002164 if (o->isMachO()) {
Kevin Enderby0ae163f2016-01-13 00:25:36 +00002165 printMachOFileHeader(o);
Davide Italiano1bdaa202016-09-18 04:39:15 +00002166 if (!onlyFirst)
2167 printMachOLoadCommands(o);
2168 return;
2169 }
Kevin Enderby7fa40c92016-11-16 22:17:38 +00002170 report_error(o->getFileName(), "Invalid/Unsupported object file format");
Rui Ueyamac2bed422013-09-27 21:04:00 +00002171}
2172
Paul Semeld2af4d62018-07-04 15:25:03 +00002173static void printFileHeaders(const ObjectFile *o) {
2174 if (!o->isELF() && !o->isCOFF())
2175 report_error(o->getFileName(), "Invalid/Unsupported object file format");
2176
2177 Triple::ArchType AT = o->getArch();
2178 outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n";
2179 Expected<uint64_t> StartAddrOrErr = o->getStartAddress();
2180 if (!StartAddrOrErr)
2181 report_error(o->getFileName(), StartAddrOrErr.takeError());
2182 outs() << "start address: "
2183 << format("0x%0*x", o->getBytesInAddress(), StartAddrOrErr.get())
2184 << "\n";
2185}
2186
Paul Semel0dc92f62018-07-05 14:43:29 +00002187static void printArchiveChild(StringRef Filename, const Archive::Child &C) {
2188 Expected<sys::fs::perms> ModeOrErr = C.getAccessMode();
2189 if (!ModeOrErr) {
2190 errs() << "ill-formed archive entry.\n";
2191 consumeError(ModeOrErr.takeError());
2192 return;
2193 }
2194 sys::fs::perms Mode = ModeOrErr.get();
2195 outs() << ((Mode & sys::fs::owner_read) ? "r" : "-");
2196 outs() << ((Mode & sys::fs::owner_write) ? "w" : "-");
2197 outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-");
2198 outs() << ((Mode & sys::fs::group_read) ? "r" : "-");
2199 outs() << ((Mode & sys::fs::group_write) ? "w" : "-");
2200 outs() << ((Mode & sys::fs::group_exe) ? "x" : "-");
2201 outs() << ((Mode & sys::fs::others_read) ? "r" : "-");
2202 outs() << ((Mode & sys::fs::others_write) ? "w" : "-");
2203 outs() << ((Mode & sys::fs::others_exe) ? "x" : "-");
2204
2205 outs() << " ";
2206
2207 Expected<unsigned> UIDOrErr = C.getUID();
2208 if (!UIDOrErr)
2209 report_error(Filename, UIDOrErr.takeError());
2210 unsigned UID = UIDOrErr.get();
2211 outs() << format("%d/", UID);
2212
2213 Expected<unsigned> GIDOrErr = C.getGID();
2214 if (!GIDOrErr)
2215 report_error(Filename, GIDOrErr.takeError());
2216 unsigned GID = GIDOrErr.get();
2217 outs() << format("%-d ", GID);
2218
2219 Expected<uint64_t> Size = C.getRawSize();
2220 if (!Size)
2221 report_error(Filename, Size.takeError());
2222 outs() << format("%6" PRId64, Size.get()) << " ";
2223
2224 StringRef RawLastModified = C.getRawLastModified();
2225 unsigned Seconds;
2226 if (RawLastModified.getAsInteger(10, Seconds))
2227 outs() << "(date: \"" << RawLastModified
2228 << "\" contains non-decimal chars) ";
2229 else {
2230 // Since ctime(3) returns a 26 character string of the form:
2231 // "Sun Sep 16 01:03:52 1973\n\0"
2232 // just print 24 characters.
2233 time_t t = Seconds;
2234 outs() << format("%.24s ", ctime(&t));
2235 }
2236
2237 StringRef Name = "";
2238 Expected<StringRef> NameOrErr = C.getName();
2239 if (!NameOrErr) {
2240 consumeError(NameOrErr.takeError());
2241 Expected<StringRef> RawNameOrErr = C.getRawName();
2242 if (!RawNameOrErr)
2243 report_error(Filename, NameOrErr.takeError());
2244 Name = RawNameOrErr.get();
2245 } else {
2246 Name = NameOrErr.get();
2247 }
2248 outs() << Name << "\n";
2249}
2250
2251static void DumpObject(ObjectFile *o, const Archive *a = nullptr,
2252 const Archive::Child *c = nullptr) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002253 StringRef ArchiveName = a != nullptr ? a->getFileName() : "";
Adrian Prantl437105a2015-07-08 02:04:15 +00002254 // Avoid other output when using a raw option.
2255 if (!RawClangAST) {
2256 outs() << '\n';
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002257 if (a)
2258 outs() << a->getFileName() << "(" << o->getFileName() << ")";
2259 else
2260 outs() << o->getFileName();
2261 outs() << ":\tfile format " << o->getFileFormatName() << "\n\n";
Adrian Prantl437105a2015-07-08 02:04:15 +00002262 }
Michael J. Spencer4e25c022011-10-17 17:13:22 +00002263
Paul Semel0dc92f62018-07-05 14:43:29 +00002264 if (ArchiveHeaders && !MachOOpt)
2265 printArchiveChild(a->getFileName(), *c);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002266 if (Disassemble)
Michael J. Spencer51862b32011-10-13 22:17:18 +00002267 DisassembleObject(o, Relocations);
2268 if (Relocations && !Disassemble)
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002269 PrintRelocations(o);
Paul Semelcb0f0432018-06-07 13:30:55 +00002270 if (DynamicRelocations)
2271 PrintDynamicRelocations(o);
Nick Lewyckyfcf84622011-10-10 21:21:34 +00002272 if (SectionHeaders)
2273 PrintSectionHeaders(o);
Michael J. Spencer4e25c022011-10-17 17:13:22 +00002274 if (SectionContents)
2275 PrintSectionContents(o);
Michael J. Spencerbfa06782011-10-18 19:32:17 +00002276 if (SymbolTable)
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002277 PrintSymbolTable(o, ArchiveName);
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00002278 if (UnwindInfo)
2279 PrintUnwindInfo(o);
Davide Italiano1bdaa202016-09-18 04:39:15 +00002280 if (PrivateHeaders || FirstPrivateHeader)
2281 printPrivateFileHeaders(o, FirstPrivateHeader);
Paul Semeld2af4d62018-07-04 15:25:03 +00002282 if (FileHeaders)
2283 printFileHeaders(o);
Nick Kledzikd04bc352014-08-30 00:20:14 +00002284 if (ExportsTrie)
2285 printExportsTrie(o);
Nick Kledzikac431442014-09-12 21:34:15 +00002286 if (Rebase)
2287 printRebaseTable(o);
Nick Kledzik56ebef42014-09-16 01:41:51 +00002288 if (Bind)
2289 printBindTable(o);
2290 if (LazyBind)
2291 printLazyBindTable(o);
2292 if (WeakBind)
2293 printWeakBindTable(o);
Adrian Prantl437105a2015-07-08 02:04:15 +00002294 if (RawClangAST)
2295 printRawClangAST(o);
Sanjoy Das6f567a42015-06-22 18:03:02 +00002296 if (PrintFaultMaps)
2297 printFaultMaps(o);
Igor Laevsky03a670c2016-01-26 15:09:42 +00002298 if (DwarfDumpType != DIDT_Null) {
Rafael Espindolac398e672017-07-19 22:27:28 +00002299 std::unique_ptr<DIContext> DICtx = DWARFContext::create(*o);
Igor Laevsky03a670c2016-01-26 15:09:42 +00002300 // Dump the complete DWARF structure.
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00002301 DIDumpOptions DumpOpts;
2302 DumpOpts.DumpType = DwarfDumpType;
Adrian Prantlf4bc1f72017-06-01 18:18:23 +00002303 DICtx->dump(outs(), DumpOpts);
Igor Laevsky03a670c2016-01-26 15:09:42 +00002304 }
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002305}
2306
Paul Semel0dc92f62018-07-05 14:43:29 +00002307static void DumpObject(const COFFImportFile *I, const Archive *A,
2308 const Archive::Child *C = nullptr) {
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +00002309 StringRef ArchiveName = A ? A->getFileName() : "";
2310
2311 // Avoid other output when using a raw option.
2312 if (!RawClangAST)
2313 outs() << '\n'
2314 << ArchiveName << "(" << I->getFileName() << ")"
2315 << ":\tfile format COFF-import-file"
2316 << "\n\n";
2317
Paul Semel0dc92f62018-07-05 14:43:29 +00002318 if (ArchiveHeaders && !MachOOpt)
2319 printArchiveChild(A->getFileName(), *C);
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +00002320 if (SymbolTable)
2321 printCOFFSymbolTable(I);
2322}
2323
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002324/// Dump each object file in \a a;
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002325static void DumpArchive(const Archive *a) {
Mehdi Amini41af4302016-11-11 04:28:40 +00002326 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +00002327 for (auto &C : a->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +00002328 Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary();
2329 if (!ChildOrErr) {
2330 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError()))
2331 report_error(a->getFileName(), C, std::move(E));
2332 continue;
2333 }
Rafael Espindolaae460022014-06-16 16:08:36 +00002334 if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Paul Semel0dc92f62018-07-05 14:43:29 +00002335 DumpObject(o, a, &C);
Saleem Abdulrasoolc6bf5472016-08-18 16:39:19 +00002336 else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
Paul Semel0dc92f62018-07-05 14:43:29 +00002337 DumpObject(I, a, &C);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002338 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00002339 report_error(a->getFileName(), object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002340 }
Lang Hamesfc209622016-07-14 02:24:01 +00002341 if (Err)
2342 report_error(a->getFileName(), std::move(Err));
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002343}
2344
Adrian Prantl4dfcc4a2018-05-01 16:10:38 +00002345/// Open file and figure out how to dump it.
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002346static void DumpInput(StringRef file) {
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002347
Kevin Enderbye2297dd2015-01-07 21:02:18 +00002348 // If we are using the Mach-O specific object file parser, then let it parse
2349 // the file and process the command line options. So the -arch flags can
2350 // be used to select specific slices, etc.
2351 if (MachOOpt) {
2352 ParseInputMachO(file);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002353 return;
2354 }
2355
2356 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +00002357 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file);
2358 if (!BinaryOrErr)
Kevin Enderbyb34e3a12016-05-05 17:43:35 +00002359 report_error(file, BinaryOrErr.takeError());
Rafael Espindola48af1c22014-08-19 18:44:46 +00002360 Binary &Binary = *BinaryOrErr.get().getBinary();
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002361
Rafael Espindola3f6481d2014-08-01 14:31:55 +00002362 if (Archive *a = dyn_cast<Archive>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002363 DumpArchive(a);
Rafael Espindola3f6481d2014-08-01 14:31:55 +00002364 else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary))
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002365 DumpObject(o);
Jim Grosbachaf9aec02012-08-07 17:53:14 +00002366 else
Alexey Samsonov50d0fbd2015-06-04 18:34:11 +00002367 report_error(file, object_error::invalid_file_type);
Michael J. Spencerba4a3622011-10-08 00:18:30 +00002368}
2369
Michael J. Spencer2670c252011-01-20 06:39:06 +00002370int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +00002371 InitLLVM X(argc, argv);
Michael J. Spencer2670c252011-01-20 06:39:06 +00002372
2373 // Initialize targets and assembly printers/parsers.
2374 llvm::InitializeAllTargetInfos();
Evan Cheng8c886a42011-07-22 21:58:54 +00002375 llvm::InitializeAllTargetMCs();
Michael J. Spencer2670c252011-01-20 06:39:06 +00002376 llvm::InitializeAllDisassemblers();
2377
Pete Cooper28fb4fc2012-05-03 23:20:10 +00002378 // Register the target printer for --version.
2379 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
2380
Michael J. Spencer2670c252011-01-20 06:39:06 +00002381 cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
2382 TripleName = Triple::normalize(TripleName);
2383
2384 ToolName = argv[0];
2385
2386 // Defaults to a.out if no filenames specified.
2387 if (InputFilenames.size() == 0)
2388 InputFilenames.push_back("a.out");
2389
Fangrui Song8513cd42018-06-27 20:45:11 +00002390 if (AllHeaders)
2391 PrivateHeaders = Relocations = SectionHeaders = SymbolTable = true;
2392
Hemant Kulkarni8dfc0b52016-08-15 19:49:24 +00002393 if (DisassembleAll || PrintSource || PrintLines)
Colin LeMahieuf34933e2015-07-23 20:58:49 +00002394 Disassemble = true;
Paul Semel007dedb2018-07-18 16:39:21 +00002395
2396 if (Demangle.getValue() != "none" && Demangle.getValue() != "" &&
2397 Demangle.getValue() != "itanium")
2398 warn("Unsupported demangling style");
2399
Michael J. Spencerbfa06782011-10-18 19:32:17 +00002400 if (!Disassemble
2401 && !Relocations
Paul Semelcb0f0432018-06-07 13:30:55 +00002402 && !DynamicRelocations
Michael J. Spencerbfa06782011-10-18 19:32:17 +00002403 && !SectionHeaders
2404 && !SectionContents
Michael J. Spencer0c6ec482012-12-05 20:12:35 +00002405 && !SymbolTable
Michael J. Spencer209565db2013-01-06 03:56:49 +00002406 && !UnwindInfo
Nick Kledzikd04bc352014-08-30 00:20:14 +00002407 && !PrivateHeaders
Paul Semeld2af4d62018-07-04 15:25:03 +00002408 && !FileHeaders
Kevin Enderby0ae163f2016-01-13 00:25:36 +00002409 && !FirstPrivateHeader
Nick Kledzikac431442014-09-12 21:34:15 +00002410 && !ExportsTrie
Nick Kledzik56ebef42014-09-16 01:41:51 +00002411 && !Rebase
2412 && !Bind
2413 && !LazyBind
Kevin Enderby131d1772015-01-09 19:22:37 +00002414 && !WeakBind
Adrian Prantl437105a2015-07-08 02:04:15 +00002415 && !RawClangAST
Kevin Enderby13023a12015-01-15 23:19:11 +00002416 && !(UniversalHeaders && MachOOpt)
Paul Semel0dc92f62018-07-05 14:43:29 +00002417 && !ArchiveHeaders
Kevin Enderby69fe98d2015-01-23 18:52:17 +00002418 && !(IndirectSymbols && MachOOpt)
Kevin Enderby9a509442015-01-27 21:28:24 +00002419 && !(DataInCode && MachOOpt)
Kevin Enderbyf6d25852015-01-31 00:37:11 +00002420 && !(LinkOptHints && MachOOpt)
Kevin Enderbycd66be52015-03-11 22:06:32 +00002421 && !(InfoPlist && MachOOpt)
Kevin Enderbybc847fa2015-03-16 20:08:09 +00002422 && !(DylibsUsed && MachOOpt)
2423 && !(DylibId && MachOOpt)
Kevin Enderby0fc11822015-04-01 20:57:01 +00002424 && !(ObjcMetaData && MachOOpt)
Colin LeMahieufcc32762015-07-29 19:08:10 +00002425 && !(FilterSections.size() != 0 && MachOOpt)
Igor Laevsky03a670c2016-01-26 15:09:42 +00002426 && !PrintFaultMaps
2427 && DwarfDumpType == DIDT_Null) {
Michael J. Spencer2670c252011-01-20 06:39:06 +00002428 cl::PrintHelpMessage();
Dimitry Andrice4f5d012017-12-18 19:46:56 +00002429 return 2;
2430 }
2431
Rafael Aulerb0e4b912018-03-09 19:13:44 +00002432 DisasmFuncsSet.insert(DisassembleFunctions.begin(),
2433 DisassembleFunctions.end());
2434
Dimitry Andrice4f5d012017-12-18 19:46:56 +00002435 llvm::for_each(InputFilenames, DumpInput);
2436
2437 return EXIT_SUCCESS;
2438}