blob: 81ce7a5903640c683bfd6dd784eee92df7f80740 [file] [log] [blame]
Benjamin Kramera9232f72013-08-09 10:31:14 +00001//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
David Meyer5c2b4ea2012-03-01 01:36:50 +00002//
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//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000010// This is a tool similar to readelf, except it works on multiple object file
11// formats. The main purpose of this tool is to provide detailed output suitable
12// for FileCheck.
David Meyeree37b6e2012-03-02 23:43:51 +000013//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000014// Flags should be similar to readelf where supported, but the output format
15// does not need to be identical. The point is to not make users learn yet
16// another set of flags.
David Meyeree37b6e2012-03-02 23:43:51 +000017//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000018// Output should be specialized for each format where appropriate.
David Meyeree37b6e2012-03-02 23:43:51 +000019//
20//===----------------------------------------------------------------------===//
David Meyer5c2b4ea2012-03-01 01:36:50 +000021
Michael J. Spencerd326d052013-02-20 02:37:12 +000022#include "llvm-readobj.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000023#include "Error.h"
24#include "ObjDumper.h"
Marek Sokolowski96bd9232017-09-20 18:33:35 +000025#include "WindowsResourceDumper.h"
Zachary Turner47856b22017-11-30 18:39:50 +000026#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000027#include "llvm/Object/Archive.h"
Rui Ueyama40e22512015-08-28 07:40:30 +000028#include "llvm/Object/COFFImportFile.h"
Rafael Espindola71be19d2015-03-24 20:26:55 +000029#include "llvm/Object/MachOUniversal.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000030#include "llvm/Object/ObjectFile.h"
Marek Sokolowski96bd9232017-09-20 18:33:35 +000031#include "llvm/Object/WindowsResource.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000032#include "llvm/Support/Casting.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000033#include "llvm/Support/CommandLine.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000034#include "llvm/Support/DataTypes.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000035#include "llvm/Support/Debug.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000036#include "llvm/Support/FileSystem.h"
Sam Clegg52483262018-01-10 00:14:19 +000037#include "llvm/Support/FormatVariadic.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000038#include "llvm/Support/InitLLVM.h"
Petr Hosekf144b002017-07-19 02:09:37 +000039#include "llvm/Support/Path.h"
Zachary Turner2448e9f2016-05-03 00:28:04 +000040#include "llvm/Support/ScopedPrinter.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000041#include "llvm/Support/TargetRegistry.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000042
David Meyer5c2b4ea2012-03-01 01:36:50 +000043using namespace llvm;
44using namespace llvm::object;
45
Eric Christopher76e70f32013-04-03 18:31:38 +000046namespace opts {
47 cl::list<std::string> InputFilenames(cl::Positional,
48 cl::desc("<input object files>"),
49 cl::ZeroOrMore);
David Meyer5c2b4ea2012-03-01 01:36:50 +000050
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +000051 // -all, -a
52 cl::opt<bool>
53 All("all",
54 cl::desc("Equivalent to setting: --file-headers, --program-headers, "
55 "--section-headers, --symbols, --relocations, "
56 "--dynamic-table, --notes, --version-info, --unwind, "
57 "--section-groups and --elf-hash-histogram."));
58 cl::alias AllShort("a", cl::desc("Alias for --all"), cl::aliasopt(All));
59
Sid Manningcd44df22018-12-11 16:15:03 +000060 // --headers -e
61 cl::opt<bool>
62 Headers("headers",
63 cl::desc("Equivalent to setting: --file-headers, --program-headers, "
64 "--section-headers"));
65 cl::alias HeadersShort("e", cl::desc("Alias for --headers"),
66 cl::aliasopt(Headers));
67
Petr Hosek35375a32017-07-18 23:58:15 +000068 // -wide, -W
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +000069 cl::opt<bool>
70 WideOutput("wide", cl::desc("Ignored for compatibility with GNU readelf"),
71 cl::Hidden);
Petr Hosek35375a32017-07-18 23:58:15 +000072 cl::alias WideOutputShort("W",
73 cl::desc("Alias for --wide"),
74 cl::aliasopt(WideOutput));
75
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +000076 // -file-headers, -file-header, -h
Eric Christopher76e70f32013-04-03 18:31:38 +000077 cl::opt<bool> FileHeaders("file-headers",
78 cl::desc("Display file headers "));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +000079 cl::alias FileHeadersShort("h", cl::desc("Alias for --file-headers"),
80 cl::aliasopt(FileHeaders), cl::NotHidden);
81 cl::alias FileHeadersSingular("file-header",
82 cl::desc("Alias for --file-headers"),
83 cl::aliasopt(FileHeaders));
Eric Christopher76e70f32013-04-03 18:31:38 +000084
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +000085 // -section-headers, -sections, -S
86 // Also -s in llvm-readobj mode.
87 cl::opt<bool> SectionHeaders("section-headers",
88 cl::desc("Display all section headers."));
89 cl::alias SectionsShortUpper("S", cl::desc("Alias for --section-headers"),
90 cl::aliasopt(SectionHeaders), cl::NotHidden);
91 cl::alias SectionHeadersAlias("sections",
92 cl::desc("Alias for --section-headers"),
93 cl::aliasopt(SectionHeaders), cl::NotHidden);
Eric Christopher76e70f32013-04-03 18:31:38 +000094
Jordan Rupprechte55d66c2019-01-15 17:04:40 +000095 // -section-relocations
96 // Also -sr in llvm-readobj mode.
Eric Christopher76e70f32013-04-03 18:31:38 +000097 cl::opt<bool> SectionRelocations("section-relocations",
98 cl::desc("Display relocations for each section shown."));
Eric Christopher76e70f32013-04-03 18:31:38 +000099
Jordan Rupprechte55d66c2019-01-15 17:04:40 +0000100 // -section-symbols
101 // Also -st in llvm-readobj mode.
Eric Christopher76e70f32013-04-03 18:31:38 +0000102 cl::opt<bool> SectionSymbols("section-symbols",
103 cl::desc("Display symbols for each section shown."));
Eric Christopher76e70f32013-04-03 18:31:38 +0000104
Jordan Rupprechte55d66c2019-01-15 17:04:40 +0000105 // -section-data
106 // Also -sd in llvm-readobj mode.
Eric Christopher76e70f32013-04-03 18:31:38 +0000107 cl::opt<bool> SectionData("section-data",
108 cl::desc("Display section data for each section shown."));
Eric Christopher76e70f32013-04-03 18:31:38 +0000109
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000110 // -relocations, -relocs, -r
Eric Christopher76e70f32013-04-03 18:31:38 +0000111 cl::opt<bool> Relocations("relocations",
112 cl::desc("Display the relocation entries in the file"));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000113 cl::alias RelocationsShort("r", cl::desc("Alias for --relocations"),
114 cl::aliasopt(Relocations), cl::NotHidden);
115 cl::alias RelocationsGNU("relocs", cl::desc("Alias for --relocations"),
116 cl::aliasopt(Relocations));
Eric Christopher76e70f32013-04-03 18:31:38 +0000117
Saleem Abdulrasoolb899d192016-08-30 18:52:02 +0000118 // -notes, -n
119 cl::opt<bool> Notes("notes", cl::desc("Display the ELF notes in the file"));
120 cl::alias NotesShort("n", cl::desc("Alias for --notes"), cl::aliasopt(Notes));
121
Michael J. Spencer4cbb2db2015-06-25 21:47:32 +0000122 // -dyn-relocations
123 cl::opt<bool> DynRelocs("dyn-relocations",
124 cl::desc("Display the dynamic relocation entries in the file"));
125
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000126 // -symbols
127 // Also -s in llvm-readelf mode, or -t in llvm-readobj mode.
Eric Christopher76e70f32013-04-03 18:31:38 +0000128 cl::opt<bool> Symbols("symbols",
129 cl::desc("Display the symbol table"));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000130 cl::alias SymbolsGNU("syms", cl::desc("Alias for --symbols"),
131 cl::aliasopt(Symbols));
Eric Christopher76e70f32013-04-03 18:31:38 +0000132
Jordan Rupprechte55d66c2019-01-15 17:04:40 +0000133 // -dyn-symbols, -dyn-syms
134 // Also -dt in llvm-readobj mode.
Eric Christopher76e70f32013-04-03 18:31:38 +0000135 cl::opt<bool> DynamicSymbols("dyn-symbols",
136 cl::desc("Display the dynamic symbol table"));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000137 cl::alias DynSymsGNU("dyn-syms", cl::desc("Alias for --dyn-symbols"),
138 cl::aliasopt(DynamicSymbols));
Eric Christopher76e70f32013-04-03 18:31:38 +0000139
140 // -unwind, -u
141 cl::opt<bool> UnwindInfo("unwind",
142 cl::desc("Display unwind information"));
143 cl::alias UnwindInfoShort("u",
144 cl::desc("Alias for --unwind"),
145 cl::aliasopt(UnwindInfo));
146
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000147 // -dynamic-table, -dynamic, -d
Eric Christopher76e70f32013-04-03 18:31:38 +0000148 cl::opt<bool> DynamicTable("dynamic-table",
149 cl::desc("Display the ELF .dynamic section table"));
Saleem Abdulrasoolddff6282016-07-20 01:16:28 +0000150 cl::alias DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000151 cl::aliasopt(DynamicTable), cl::NotHidden);
152 cl::alias DynamicTableAlias("dynamic", cl::desc("Alias for --dynamic-table"),
Saleem Abdulrasoolddff6282016-07-20 01:16:28 +0000153 cl::aliasopt(DynamicTable));
Eric Christopher76e70f32013-04-03 18:31:38 +0000154
155 // -needed-libs
156 cl::opt<bool> NeededLibraries("needed-libs",
157 cl::desc("Display the needed libraries"));
Nico Rieck1c8dfa52013-04-12 04:01:52 +0000158
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000159 // -program-headers, -segments, -l
Nico Rieckcf3b55a2013-04-12 04:07:39 +0000160 cl::opt<bool> ProgramHeaders("program-headers",
161 cl::desc("Display ELF program headers"));
Saleem Abdulrasoolddff6282016-07-20 01:16:28 +0000162 cl::alias ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000163 cl::aliasopt(ProgramHeaders), cl::NotHidden);
164 cl::alias SegmentsAlias("segments", cl::desc("Alias for --program-headers"),
165 cl::aliasopt(ProgramHeaders));
Nico Rieckcf3b55a2013-04-12 04:07:39 +0000166
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000167 // -string-dump, -p
Paul Semel064db432018-06-15 14:15:02 +0000168 cl::list<std::string> StringDump("string-dump", cl::desc("<number|name>"),
169 cl::ZeroOrMore);
170 cl::alias StringDumpShort("p", cl::desc("Alias for --string-dump"),
171 cl::aliasopt(StringDump));
172
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000173 // -hex-dump, -x
Paul Semel94b09402018-07-11 10:00:29 +0000174 cl::list<std::string> HexDump("hex-dump", cl::desc("<number|name>"),
175 cl::ZeroOrMore);
176 cl::alias HexDumpShort("x", cl::desc("Alias for --hex-dump"),
177 cl::aliasopt(HexDump));
178
Michael J. Spencer81097632015-07-09 22:32:24 +0000179 // -hash-table
180 cl::opt<bool> HashTable("hash-table",
181 cl::desc("Display ELF hash table"));
182
Igor Kudrin45981542015-10-14 12:11:50 +0000183 // -gnu-hash-table
184 cl::opt<bool> GnuHashTable("gnu-hash-table",
185 cl::desc("Display ELF .gnu.hash section"));
186
Nico Rieck1c8dfa52013-04-12 04:01:52 +0000187 // -expand-relocs
188 cl::opt<bool> ExpandRelocs("expand-relocs",
189 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanove493a992013-12-19 11:37:14 +0000190
Jake Ehrlicheefdbb42018-06-28 21:07:34 +0000191 // -raw-relr
192 cl::opt<bool> RawRelr("raw-relr",
193 cl::desc("Do not decode relocations in SHT_RELR section, display raw contents"));
194
Zachary Turner23719012015-02-18 19:32:05 +0000195 // -codeview
196 cl::opt<bool> CodeView("codeview",
197 cl::desc("Display CodeView debug information"));
198
Reid Klecknerfed38e22016-05-14 00:02:53 +0000199 // -codeview-merged-types
200 cl::opt<bool>
201 CodeViewMergedTypes("codeview-merged-types",
202 cl::desc("Display the merged CodeView type stream"));
203
Zachary Turner23719012015-02-18 19:32:05 +0000204 // -codeview-subsection-bytes
205 cl::opt<bool> CodeViewSubsectionBytes(
206 "codeview-subsection-bytes",
207 cl::desc("Dump raw contents of codeview debug sections and records"));
Saleem Abdulrasool459c9492014-01-30 04:46:33 +0000208
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000209 // -arm-attributes
Saleem Abdulrasool459c9492014-01-30 04:46:33 +0000210 cl::opt<bool> ARMAttributes("arm-attributes",
211 cl::desc("Display the ARM attributes section"));
Simon Atanasyan42469f62014-06-18 08:47:09 +0000212
213 // -mips-plt-got
214 cl::opt<bool>
215 MipsPLTGOT("mips-plt-got",
216 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Rui Ueyamaf3cd10b2014-10-02 17:02:18 +0000217
Simon Atanasyanef7c2562015-05-07 15:40:35 +0000218 // -mips-abi-flags
219 cl::opt<bool> MipsABIFlags("mips-abi-flags",
220 cl::desc("Display the MIPS.abiflags section"));
221
Simon Atanasyan3bcfd462015-06-16 21:47:43 +0000222 // -mips-reginfo
223 cl::opt<bool> MipsReginfo("mips-reginfo",
224 cl::desc("Display the MIPS .reginfo section"));
225
Simon Atanasyan125d6462016-05-04 05:58:57 +0000226 // -mips-options
227 cl::opt<bool> MipsOptions("mips-options",
228 cl::desc("Display the MIPS .MIPS.options section"));
229
Rui Ueyamaf3cd10b2014-10-02 17:02:18 +0000230 // -coff-imports
231 cl::opt<bool>
232 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
Saleem Abdulrasooldb02fa52014-10-07 19:37:52 +0000233
Saleem Abdulrasoolb19a4852015-01-03 21:35:09 +0000234 // -coff-exports
235 cl::opt<bool>
236 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
237
Saleem Abdulrasooldb02fa52014-10-07 19:37:52 +0000238 // -coff-directives
239 cl::opt<bool>
240 COFFDirectives("coff-directives",
Rui Ueyama6272b8c2014-11-19 00:18:07 +0000241 cl::desc("Display the PE/COFF .drectve section"));
242
243 // -coff-basereloc
244 cl::opt<bool>
245 COFFBaseRelocs("coff-basereloc",
246 cl::desc("Display the PE/COFF .reloc section"));
Lang Hames63f40542015-06-26 23:56:53 +0000247
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000248 // -coff-debug-directory
249 cl::opt<bool>
250 COFFDebugDirectory("coff-debug-directory",
251 cl::desc("Display the PE/COFF debug directory"));
252
Zachary Turner224acd22017-04-27 19:38:38 +0000253 // -coff-resources
254 cl::opt<bool> COFFResources("coff-resources",
255 cl::desc("Display the PE/COFF .rsrc section"));
256
Reid Kleckner367f21d2017-06-22 01:10:29 +0000257 // -coff-load-config
258 cl::opt<bool>
259 COFFLoadConfig("coff-load-config",
260 cl::desc("Display the PE/COFF load config"));
261
Saleem Abdulrasoole7676fe2018-01-30 16:29:29 +0000262 // -elf-linker-options
263 cl::opt<bool>
264 ELFLinkerOptions("elf-linker-options",
265 cl::desc("Display the ELF .linker-options section"));
266
Davide Italiano241f7162015-08-21 20:28:30 +0000267 // -macho-data-in-code
268 cl::opt<bool>
269 MachODataInCode("macho-data-in-code",
270 cl::desc("Display MachO Data in Code command"));
271
Davide Italiano7ab20092015-09-03 18:10:28 +0000272 // -macho-indirect-symbols
273 cl::opt<bool>
274 MachOIndirectSymbols("macho-indirect-symbols",
275 cl::desc("Display MachO indirect symbols"));
276
Davide Italiano793616c2015-09-09 00:21:18 +0000277 // -macho-linker-options
278 cl::opt<bool>
279 MachOLinkerOptions("macho-linker-options",
280 cl::desc("Display MachO linker options"));
281
Davide Italiano885eb982015-09-02 16:24:24 +0000282 // -macho-segment
283 cl::opt<bool>
284 MachOSegment("macho-segment",
285 cl::desc("Display MachO Segment command"));
286
Davide Italiano7dbe74f2015-08-27 15:11:32 +0000287 // -macho-version-min
288 cl::opt<bool>
289 MachOVersionMin("macho-version-min",
290 cl::desc("Display MachO version min command"));
Davide Italiano19c43a62015-08-31 19:32:31 +0000291
292 // -macho-dysymtab
293 cl::opt<bool>
294 MachODysymtab("macho-dysymtab",
295 cl::desc("Display MachO Dysymtab command"));
296
Lang Hames63f40542015-06-26 23:56:53 +0000297 // -stackmap
298 cl::opt<bool>
299 PrintStackMap("stackmap",
300 cl::desc("Display contents of stackmap section"));
301
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000302 // -version-info, -V
Davide Italianoc813f4c2015-10-16 23:19:01 +0000303 cl::opt<bool>
304 VersionInfo("version-info",
305 cl::desc("Display ELF version sections (if present)"));
306 cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
307 cl::aliasopt(VersionInfo));
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000308
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000309 // -elf-section-groups, -section-groups, -g
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000310 cl::opt<bool> SectionGroups("elf-section-groups",
311 cl::desc("Display ELF section group contents"));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000312 cl::alias SectionGroupsAlias("section-groups",
313 cl::desc("Alias for -elf-sections-groups"),
314 cl::aliasopt(SectionGroups));
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000315 cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
316 cl::aliasopt(SectionGroups));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000317
318 // -elf-hash-histogram, -histogram, -I
Hemant Kulkarni14dddbf2016-04-11 17:15:30 +0000319 cl::opt<bool> HashHistogram(
320 "elf-hash-histogram",
321 cl::desc("Display bucket list histogram for hash sections"));
322 cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
323 cl::aliasopt(HashHistogram));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000324 cl::alias HistogramAlias("histogram",
325 cl::desc("Alias for --elf-hash-histogram"),
326 cl::aliasopt(HashHistogram));
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000327
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000328 // -elf-cg-profile
Michael J. Spencer56500c72018-06-02 16:33:01 +0000329 cl::opt<bool> CGProfile("elf-cg-profile", cl::desc("Display callgraph profile section"));
330
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000331 // -addrsig
Peter Collingbourne38b60af2018-08-22 23:58:16 +0000332 cl::opt<bool> Addrsig("addrsig",
Peter Collingbournea29ff2e2018-07-17 22:17:18 +0000333 cl::desc("Display address-significance table"));
334
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000335 // -elf-output-style
Tim Northover9645a0d2016-03-01 21:45:22 +0000336 cl::opt<OutputStyleTy>
Hemant Kulkarni54f4d3a2016-02-10 20:40:55 +0000337 Output("elf-output-style", cl::desc("Specify ELF dump style"),
338 cl::values(clEnumVal(LLVM, "LLVM default style"),
Mehdi Amini3ffe1132016-10-08 19:41:06 +0000339 clEnumVal(GNU, "GNU readelf style")),
Hemant Kulkarni54f4d3a2016-02-10 20:40:55 +0000340 cl::init(LLVM));
Eric Christopher76e70f32013-04-03 18:31:38 +0000341} // namespace opts
342
Rafael Espindolafa1a4b22015-07-20 03:38:17 +0000343namespace llvm {
344
Davide Italianof6712102015-12-04 19:29:49 +0000345LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
Davide Italianob98f7ad2015-12-23 19:29:34 +0000346 errs() << "\nError reading file: " << Msg << ".\n";
347 errs().flush();
Rafael Espindolad60ced82015-07-20 03:23:55 +0000348 exit(1);
Rafael Espindolab9adbd92015-07-20 03:01:49 +0000349}
350
Zachary Turner3483f212016-08-05 21:45:34 +0000351void error(Error EC) {
Zachary Turner92cd0ec2016-08-04 19:39:55 +0000352 if (!EC)
353 return;
Zachary Turner92cd0ec2016-08-04 19:39:55 +0000354 handleAllErrors(std::move(EC),
Zachary Turner3483f212016-08-05 21:45:34 +0000355 [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
Zachary Turner92cd0ec2016-08-04 19:39:55 +0000356}
357
Rafael Espindolad60ced82015-07-20 03:23:55 +0000358void error(std::error_code EC) {
Eric Christopher76e70f32013-04-03 18:31:38 +0000359 if (!EC)
Rafael Espindolad60ced82015-07-20 03:23:55 +0000360 return;
Rafael Espindola8a0ff182015-08-06 21:54:37 +0000361 reportError(EC.message());
Rafael Espindola148ee4f2012-12-31 16:05:21 +0000362}
363
Eric Christopher76e70f32013-04-03 18:31:38 +0000364bool relocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindola297f5052015-07-06 15:53:43 +0000365 return a.getOffset() < b.getOffset();
Eric Christopher76e70f32013-04-03 18:31:38 +0000366}
367
368} // namespace llvm
369
Rafael Espindola1ad45022014-06-13 03:07:50 +0000370static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher76e70f32013-04-03 18:31:38 +0000371 if (Input == "-")
372 Input = "<stdin>";
373
Rafael Espindolab9adbd92015-07-20 03:01:49 +0000374 reportError(Twine(Input) + ": " + EC.message());
Eric Christopher76e70f32013-04-03 18:31:38 +0000375}
376
Lang Hamesaacf2fb2016-07-14 02:24:01 +0000377static void reportError(StringRef Input, Error Err) {
378 if (Input == "-")
379 Input = "<stdin>";
380 std::string ErrMsg;
381 {
382 raw_string_ostream ErrStream(ErrMsg);
383 logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
384 }
385 reportError(ErrMsg);
386}
387
Simon Atanasyan42469f62014-06-18 08:47:09 +0000388static bool isMipsArch(unsigned Arch) {
389 switch (Arch) {
390 case llvm::Triple::mips:
391 case llvm::Triple::mipsel:
392 case llvm::Triple::mips64:
393 case llvm::Triple::mips64el:
394 return true;
395 default:
396 return false;
397 }
398}
Zachary Turnerbe73fbc2016-09-09 17:46:17 +0000399namespace {
Zachary Turner73069562016-11-08 22:24:53 +0000400struct ReadObjTypeTableBuilder {
Reid Klecknerd6031922017-03-24 17:26:38 +0000401 ReadObjTypeTableBuilder()
402 : Allocator(), IDTable(Allocator), TypeTable(Allocator) {}
Simon Atanasyan42469f62014-06-18 08:47:09 +0000403
Zachary Turnerbe73fbc2016-09-09 17:46:17 +0000404 llvm::BumpPtrAllocator Allocator;
Zachary Turner47856b22017-11-30 18:39:50 +0000405 llvm::codeview::MergingTypeTableBuilder IDTable;
406 llvm::codeview::MergingTypeTableBuilder TypeTable;
Zachary Turnerbe73fbc2016-09-09 17:46:17 +0000407};
408}
Zachary Turner73069562016-11-08 22:24:53 +0000409static ReadObjTypeTableBuilder CVTypes;
Reid Klecknerfed38e22016-05-14 00:02:53 +0000410
Adrian Prantl0b24b742018-05-01 16:10:38 +0000411/// Creates an format-specific object file dumper.
Zachary Turner2448e9f2016-05-03 00:28:04 +0000412static std::error_code createDumper(const ObjectFile *Obj,
413 ScopedPrinter &Writer,
Rafael Espindola1ad45022014-06-13 03:07:50 +0000414 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher76e70f32013-04-03 18:31:38 +0000415 if (!Obj)
416 return readobj_error::unsupported_file_format;
417
418 if (Obj->isCOFF())
419 return createCOFFDumper(Obj, Writer, Result);
420 if (Obj->isELF())
421 return createELFDumper(Obj, Writer, Result);
422 if (Obj->isMachO())
423 return createMachODumper(Obj, Writer, Result);
Derek Schuffc20099f2017-01-30 23:30:52 +0000424 if (Obj->isWasm())
425 return createWasmDumper(Obj, Writer, Result);
Eric Christopher76e70f32013-04-03 18:31:38 +0000426
427 return readobj_error::unsupported_obj_file_format;
428}
429
Adrian Prantl0b24b742018-05-01 16:10:38 +0000430/// Dumps the specified object file.
Sam Clegg52483262018-01-10 00:14:19 +0000431static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer) {
Ahmed Charlesf4ccd112014-03-06 05:51:42 +0000432 std::unique_ptr<ObjDumper> Dumper;
Davide Italiano3aa10342015-12-05 23:36:52 +0000433 if (std::error_code EC = createDumper(Obj, Writer, Dumper))
Eric Christopher76e70f32013-04-03 18:31:38 +0000434 reportError(Obj->getFileName(), EC);
Eric Christopher76e70f32013-04-03 18:31:38 +0000435
Hemant Kulkarni54f4d3a2016-02-10 20:40:55 +0000436 if (opts::Output == opts::LLVM) {
Sam Clegg52483262018-01-10 00:14:19 +0000437 Writer.startLine() << "\n";
438 Writer.printString("File", Obj->getFileName());
439 Writer.printString("Format", Obj->getFileFormatName());
440 Writer.printString("Arch", Triple::getArchTypeName(
441 (llvm::Triple::ArchType)Obj->getArch()));
442 Writer.printString("AddressSize",
443 formatv("{0}bit", 8 * Obj->getBytesInAddress()));
Hemant Kulkarni54f4d3a2016-02-10 20:40:55 +0000444 Dumper->printLoadName();
445 }
Eric Christopher76e70f32013-04-03 18:31:38 +0000446
447 if (opts::FileHeaders)
448 Dumper->printFileHeaders();
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000449 if (opts::SectionHeaders)
450 Dumper->printSectionHeaders();
Eric Christopher76e70f32013-04-03 18:31:38 +0000451 if (opts::Relocations)
452 Dumper->printRelocations();
Michael J. Spencer4cbb2db2015-06-25 21:47:32 +0000453 if (opts::DynRelocs)
454 Dumper->printDynamicRelocations();
Eric Christopher76e70f32013-04-03 18:31:38 +0000455 if (opts::Symbols)
456 Dumper->printSymbols();
457 if (opts::DynamicSymbols)
458 Dumper->printDynamicSymbols();
459 if (opts::UnwindInfo)
460 Dumper->printUnwindInfo();
461 if (opts::DynamicTable)
462 Dumper->printDynamicTable();
463 if (opts::NeededLibraries)
464 Dumper->printNeededLibraries();
Nico Rieckcf3b55a2013-04-12 04:07:39 +0000465 if (opts::ProgramHeaders)
466 Dumper->printProgramHeaders();
Paul Semel064db432018-06-15 14:15:02 +0000467 if (!opts::StringDump.empty())
Paul Semel9f006d32018-07-18 18:00:41 +0000468 llvm::for_each(opts::StringDump, [&Dumper, Obj](StringRef SectionName) {
469 Dumper->printSectionAsString(Obj, SectionName);
Paul Semel064db432018-06-15 14:15:02 +0000470 });
Paul Semel94b09402018-07-11 10:00:29 +0000471 if (!opts::HexDump.empty())
Paul Semelcac968e2018-07-25 10:04:37 +0000472 llvm::for_each(opts::HexDump, [&Dumper, Obj](StringRef SectionName) {
473 Dumper->printSectionAsHex(Obj, SectionName);
Paul Semel94b09402018-07-11 10:00:29 +0000474 });
Michael J. Spencer81097632015-07-09 22:32:24 +0000475 if (opts::HashTable)
476 Dumper->printHashTable();
Igor Kudrin45981542015-10-14 12:11:50 +0000477 if (opts::GnuHashTable)
478 Dumper->printGnuHashTable();
Davide Italianoc813f4c2015-10-16 23:19:01 +0000479 if (opts::VersionInfo)
480 Dumper->printVersionInfo();
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000481 if (Obj->isELF()) {
Saleem Abdulrasoole7676fe2018-01-30 16:29:29 +0000482 if (opts::ELFLinkerOptions)
483 Dumper->printELFLinkerOptions();
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000484 if (Obj->getArch() == llvm::Triple::arm)
485 if (opts::ARMAttributes)
486 Dumper->printAttributes();
487 if (isMipsArch(Obj->getArch())) {
488 if (opts::MipsPLTGOT)
489 Dumper->printMipsPLTGOT();
490 if (opts::MipsABIFlags)
491 Dumper->printMipsABIFlags();
492 if (opts::MipsReginfo)
493 Dumper->printMipsReginfo();
Simon Atanasyan125d6462016-05-04 05:58:57 +0000494 if (opts::MipsOptions)
495 Dumper->printMipsOptions();
Hemant Kulkarni8822b472016-01-26 19:46:39 +0000496 }
497 if (opts::SectionGroups)
498 Dumper->printGroupSections();
Hemant Kulkarni14dddbf2016-04-11 17:15:30 +0000499 if (opts::HashHistogram)
500 Dumper->printHashHistogram();
Michael J. Spencer56500c72018-06-02 16:33:01 +0000501 if (opts::CGProfile)
502 Dumper->printCGProfile();
Peter Collingbournea29ff2e2018-07-17 22:17:18 +0000503 if (opts::Addrsig)
504 Dumper->printAddrsig();
Saleem Abdulrasoolb899d192016-08-30 18:52:02 +0000505 if (opts::Notes)
506 Dumper->printNotes();
Simon Atanasyanef7c2562015-05-07 15:40:35 +0000507 }
Davide Italianoc46ea192015-07-24 02:14:20 +0000508 if (Obj->isCOFF()) {
509 if (opts::COFFImports)
510 Dumper->printCOFFImports();
511 if (opts::COFFExports)
512 Dumper->printCOFFExports();
513 if (opts::COFFDirectives)
514 Dumper->printCOFFDirectives();
515 if (opts::COFFBaseRelocs)
516 Dumper->printCOFFBaseReloc();
Reid Klecknerfb1a9112016-06-02 17:10:43 +0000517 if (opts::COFFDebugDirectory)
518 Dumper->printCOFFDebugDirectory();
Zachary Turner224acd22017-04-27 19:38:38 +0000519 if (opts::COFFResources)
520 Dumper->printCOFFResources();
Reid Kleckner367f21d2017-06-22 01:10:29 +0000521 if (opts::COFFLoadConfig)
522 Dumper->printCOFFLoadConfig();
Peter Collingbourne38b60af2018-08-22 23:58:16 +0000523 if (opts::Addrsig)
524 Dumper->printAddrsig();
Reid Klecknerc81f0002015-12-16 18:28:12 +0000525 if (opts::CodeView)
526 Dumper->printCodeViewDebugInfo();
Reid Klecknerfed38e22016-05-14 00:02:53 +0000527 if (opts::CodeViewMergedTypes)
Reid Klecknerd6031922017-03-24 17:26:38 +0000528 Dumper->mergeCodeViewTypes(CVTypes.IDTable, CVTypes.TypeTable);
Davide Italianoc46ea192015-07-24 02:14:20 +0000529 }
Davide Italiano6997c3a2015-08-31 17:12:23 +0000530 if (Obj->isMachO()) {
Davide Italiano241f7162015-08-21 20:28:30 +0000531 if (opts::MachODataInCode)
532 Dumper->printMachODataInCode();
Davide Italiano7ab20092015-09-03 18:10:28 +0000533 if (opts::MachOIndirectSymbols)
534 Dumper->printMachOIndirectSymbols();
Davide Italiano793616c2015-09-09 00:21:18 +0000535 if (opts::MachOLinkerOptions)
536 Dumper->printMachOLinkerOptions();
Davide Italiano885eb982015-09-02 16:24:24 +0000537 if (opts::MachOSegment)
538 Dumper->printMachOSegment();
Davide Italiano7dbe74f2015-08-27 15:11:32 +0000539 if (opts::MachOVersionMin)
540 Dumper->printMachOVersionMin();
Davide Italiano19c43a62015-08-31 19:32:31 +0000541 if (opts::MachODysymtab)
542 Dumper->printMachODysymtab();
Davide Italiano6997c3a2015-08-31 17:12:23 +0000543 }
Lang Hames63f40542015-06-26 23:56:53 +0000544 if (opts::PrintStackMap)
545 Dumper->printStackMap();
Rafael Espindolafc738472012-12-31 16:29:44 +0000546}
547
Adrian Prantl0b24b742018-05-01 16:10:38 +0000548/// Dumps each object file in \a Arc;
Sam Clegg52483262018-01-10 00:14:19 +0000549static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
Mehdi Aminidf0b8bc2016-11-11 04:28:40 +0000550 Error Err = Error::success();
Lang Hamesaacf2fb2016-07-14 02:24:01 +0000551 for (auto &Child : Arc->children(Err)) {
Kevin Enderby77be0942016-05-17 17:10:12 +0000552 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
553 if (!ChildOrErr) {
554 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
Sam Clegg2436e452017-05-10 14:18:11 +0000555 reportError(Arc->getFileName(), ChildOrErr.takeError());
Kevin Enderby77be0942016-05-17 17:10:12 +0000556 }
Eric Christopher76e70f32013-04-03 18:31:38 +0000557 continue;
David Meyer2d70e262012-03-09 20:59:52 +0000558 }
Rafael Espindola06599282014-06-16 16:08:36 +0000559 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Sam Clegg52483262018-01-10 00:14:19 +0000560 dumpObject(Obj, Writer);
Saleem Abdulrasoolcec50452016-08-18 14:32:11 +0000561 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
Sam Clegg52483262018-01-10 00:14:19 +0000562 dumpCOFFImportFile(Imp, Writer);
Eric Christopher76e70f32013-04-03 18:31:38 +0000563 else
564 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
565 }
Lang Hamesaacf2fb2016-07-14 02:24:01 +0000566 if (Err)
567 reportError(Arc->getFileName(), std::move(Err));
Eric Christopher76e70f32013-04-03 18:31:38 +0000568}
569
Adrian Prantl0b24b742018-05-01 16:10:38 +0000570/// Dumps each object file in \a MachO Universal Binary;
Sam Clegg52483262018-01-10 00:14:19 +0000571static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary,
572 ScopedPrinter &Writer) {
Rafael Espindola71be19d2015-03-24 20:26:55 +0000573 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
Kevin Enderbye9ddf3a2016-05-31 20:35:34 +0000574 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
David Blaikie2d35a462015-04-13 16:05:49 +0000575 if (ObjOrErr)
Sam Clegg52483262018-01-10 00:14:19 +0000576 dumpObject(&*ObjOrErr.get(), Writer);
Kevin Enderbye9ddf3a2016-05-31 20:35:34 +0000577 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
Sam Clegg2436e452017-05-10 14:18:11 +0000578 reportError(UBinary->getFileName(), ObjOrErr.takeError());
Kevin Enderbye9ddf3a2016-05-31 20:35:34 +0000579 }
Kevin Enderbyc827f2c2016-06-28 23:16:13 +0000580 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
Sam Clegg52483262018-01-10 00:14:19 +0000581 dumpArchive(&*AOrErr.get(), Writer);
Rafael Espindola71be19d2015-03-24 20:26:55 +0000582 }
583}
Eric Christopher76e70f32013-04-03 18:31:38 +0000584
Adrian Prantl0b24b742018-05-01 16:10:38 +0000585/// Dumps \a WinRes, Windows Resource (.res) file;
Marek Sokolowski96bd9232017-09-20 18:33:35 +0000586static void dumpWindowsResourceFile(WindowsResource *WinRes) {
587 ScopedPrinter Printer{outs()};
588 WindowsRes::Dumper Dumper(WinRes, Printer);
589 if (auto Err = Dumper.printData())
590 reportError(WinRes->getFileName(), std::move(Err));
591}
592
593
Adrian Prantl0b24b742018-05-01 16:10:38 +0000594/// Opens \a File and dumps it.
Eric Christopher76e70f32013-04-03 18:31:38 +0000595static void dumpInput(StringRef File) {
Sam Clegg52483262018-01-10 00:14:19 +0000596 ScopedPrinter Writer(outs());
David Meyer5c2b4ea2012-03-01 01:36:50 +0000597
Eric Christopher76e70f32013-04-03 18:31:38 +0000598 // Attempt to open the binary.
Kevin Enderbyc6bf9be2016-04-06 22:14:09 +0000599 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
600 if (!BinaryOrErr)
Sam Clegg2436e452017-05-10 14:18:11 +0000601 reportError(File, BinaryOrErr.takeError());
Rafael Espindola548f2b62014-08-19 18:44:46 +0000602 Binary &Binary = *BinaryOrErr.get().getBinary();
Eric Christopher76e70f32013-04-03 18:31:38 +0000603
Rafael Espindola9aa0b5e2014-08-01 14:31:55 +0000604 if (Archive *Arc = dyn_cast<Archive>(&Binary))
Sam Clegg52483262018-01-10 00:14:19 +0000605 dumpArchive(Arc, Writer);
Rafael Espindola71be19d2015-03-24 20:26:55 +0000606 else if (MachOUniversalBinary *UBinary =
607 dyn_cast<MachOUniversalBinary>(&Binary))
Sam Clegg52483262018-01-10 00:14:19 +0000608 dumpMachOUniversalBinary(UBinary, Writer);
Rafael Espindola9aa0b5e2014-08-01 14:31:55 +0000609 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Sam Clegg52483262018-01-10 00:14:19 +0000610 dumpObject(Obj, Writer);
Rui Ueyama40e22512015-08-28 07:40:30 +0000611 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
Sam Clegg52483262018-01-10 00:14:19 +0000612 dumpCOFFImportFile(Import, Writer);
Marek Sokolowski96bd9232017-09-20 18:33:35 +0000613 else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(&Binary))
614 dumpWindowsResourceFile(WinRes);
Eric Christopher76e70f32013-04-03 18:31:38 +0000615 else
616 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindolafc738472012-12-31 16:29:44 +0000617}
618
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000619/// Registers aliases that should only be allowed by readobj.
620static void registerReadobjAliases() {
621 // -s has meant --sections for a very long time in llvm-readobj despite
622 // meaning --symbols in readelf.
623 static cl::alias SectionsShort("s", cl::desc("Alias for --section-headers"),
624 cl::aliasopt(opts::SectionHeaders),
625 cl::NotHidden);
626
627 // Only register -t in llvm-readobj, as readelf reserves it for
628 // --section-details (not implemented yet).
629 static cl::alias SymbolsShort("t", cl::desc("Alias for --symbols"),
630 cl::aliasopt(opts::Symbols), cl::NotHidden);
Jordan Rupprechte55d66c2019-01-15 17:04:40 +0000631
632 // The following two-letter aliases are only provided for readobj, as readelf
633 // allows single-letter args to be grouped together.
634 static cl::alias SectionRelocationsShort(
635 "sr", cl::desc("Alias for --section-relocations"),
636 cl::aliasopt(opts::SectionRelocations));
637 static cl::alias SectionDataShort("sd", cl::desc("Alias for --section-data"),
638 cl::aliasopt(opts::SectionData));
639 static cl::alias SectionSymbolsShort("st",
640 cl::desc("Alias for --section-symbols"),
641 cl::aliasopt(opts::SectionSymbols));
642 static cl::alias DynamicSymbolsShort("dt",
643 cl::desc("Alias for --dyn-symbols"),
644 cl::aliasopt(opts::DynamicSymbols));
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000645}
646
647/// Registers aliases that should only be allowed by readelf.
648static void registerReadelfAliases() {
649 // -s is here because for readobj it means --sections.
650 static cl::alias SymbolsShort("s", cl::desc("Alias for --symbols"),
Jordan Rupprechte55d66c2019-01-15 17:04:40 +0000651 cl::aliasopt(opts::Symbols), cl::NotHidden,
652 cl::Grouping);
653
654 // Allow all single letter flags to be grouped together.
655 for (auto &OptEntry : cl::getRegisteredOptions()) {
656 StringRef ArgName = OptEntry.getKey();
657 cl::Option *Option = OptEntry.getValue();
658 if (ArgName.size() == 1)
659 Option->setFormattingFlag(cl::Grouping);
660 }
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000661}
662
Eric Christopher76e70f32013-04-03 18:31:38 +0000663int main(int argc, const char *argv[]) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +0000664 InitLLVM X(argc, argv);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000665
Eric Christopher76e70f32013-04-03 18:31:38 +0000666 // Register the target printer for --version.
667 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000668
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000669 if (sys::path::stem(argv[0]).contains("readelf")) {
Petr Hosekf144b002017-07-19 02:09:37 +0000670 opts::Output = opts::GNU;
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000671 registerReadelfAliases();
672 } else {
673 registerReadobjAliases();
674 }
Petr Hosekf144b002017-07-19 02:09:37 +0000675
Eric Christopher76e70f32013-04-03 18:31:38 +0000676 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer5c2b4ea2012-03-01 01:36:50 +0000677
Jordan Rupprecht6856bcc2018-11-12 18:02:38 +0000678 if (opts::All) {
679 opts::FileHeaders = true;
680 opts::ProgramHeaders = true;
681 opts::SectionHeaders = true;
682 opts::Symbols = true;
683 opts::Relocations = true;
684 opts::DynamicTable = true;
685 opts::Notes = true;
686 opts::VersionInfo = true;
687 opts::UnwindInfo = true;
688 opts::SectionGroups = true;
689 opts::HashHistogram = true;
690 }
691
Sid Manningcd44df22018-12-11 16:15:03 +0000692 if (opts::Headers) {
693 opts::FileHeaders = true;
694 opts::ProgramHeaders = true;
695 opts::SectionHeaders = true;
696 }
697
Eric Christopher76e70f32013-04-03 18:31:38 +0000698 // Default to stdin if no filename is specified.
Jordan Rupprechtf6ea1292018-12-20 00:57:06 +0000699 if (opts::InputFilenames.empty())
Zachary Turner69d32742017-11-30 18:33:34 +0000700 opts::InputFilenames.push_back("-");
701
702 llvm::for_each(opts::InputFilenames, dumpInput);
703
704 if (opts::CodeViewMergedTypes) {
705 ScopedPrinter W(outs());
Reid Klecknerd6031922017-03-24 17:26:38 +0000706 dumpCodeViewMergedTypes(W, CVTypes.IDTable, CVTypes.TypeTable);
Reid Klecknerfed38e22016-05-14 00:02:53 +0000707 }
708
Rafael Espindolad60ced82015-07-20 03:23:55 +0000709 return 0;
David Meyer5c2b4ea2012-03-01 01:36:50 +0000710}