blob: 51991a3f067ba7de763493858e1a48a2f65d160e [file] [log] [blame]
Benjamin Kramer054f4222013-08-09 10:31:14 +00001//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
David Meyer2fc34c52012-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. Spencerd7e70032013-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 Meyerae11a782012-03-02 23:43:51 +000013//
Michael J. Spencerd7e70032013-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 Meyerae11a782012-03-02 23:43:51 +000017//
Michael J. Spencerd7e70032013-02-05 20:27:22 +000018// Output should be specialized for each format where appropriate.
David Meyerae11a782012-03-02 23:43:51 +000019//
20//===----------------------------------------------------------------------===//
David Meyer2fc34c52012-03-01 01:36:50 +000021
Michael J. Spencer6a8746b2013-02-20 02:37:12 +000022#include "llvm-readobj.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000023#include "Error.h"
24#include "ObjDumper.h"
Zachary Turner4efa0a42016-11-08 22:24:53 +000025#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000026#include "llvm/Object/Archive.h"
Rui Ueyama71ba9bd2015-08-28 07:40:30 +000027#include "llvm/Object/COFFImportFile.h"
Rafael Espindola67622312014-08-08 16:39:22 +000028#include "llvm/Object/ELFObjectFile.h"
Rafael Espindola8448a242015-03-24 20:26:55 +000029#include "llvm/Object/MachOUniversal.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Object/ObjectFile.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000031#include "llvm/Support/Casting.h"
David Meyer2fc34c52012-03-01 01:36:50 +000032#include "llvm/Support/CommandLine.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000033#include "llvm/Support/DataTypes.h"
David Meyer2fc34c52012-03-01 01:36:50 +000034#include "llvm/Support/Debug.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000035#include "llvm/Support/FileSystem.h"
36#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Support/PrettyStackTrace.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000038#include "llvm/Support/ScopedPrinter.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Support/Signals.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000040#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Support/TargetSelect.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000042#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000043#include <system_error>
Eric Christopher9cad53c2013-04-03 18:31:38 +000044
David Meyer2fc34c52012-03-01 01:36:50 +000045using namespace llvm;
46using namespace llvm::object;
47
Eric Christopher9cad53c2013-04-03 18:31:38 +000048namespace opts {
49 cl::list<std::string> InputFilenames(cl::Positional,
50 cl::desc("<input object files>"),
51 cl::ZeroOrMore);
David Meyer2fc34c52012-03-01 01:36:50 +000052
Eric Christopher9cad53c2013-04-03 18:31:38 +000053 // -file-headers, -h
54 cl::opt<bool> FileHeaders("file-headers",
55 cl::desc("Display file headers "));
56 cl::alias FileHeadersShort("h",
57 cl::desc("Alias for --file-headers"),
58 cl::aliasopt(FileHeaders));
59
60 // -sections, -s
61 cl::opt<bool> Sections("sections",
62 cl::desc("Display all sections."));
63 cl::alias SectionsShort("s",
64 cl::desc("Alias for --sections"),
65 cl::aliasopt(Sections));
66
67 // -section-relocations, -sr
68 cl::opt<bool> SectionRelocations("section-relocations",
69 cl::desc("Display relocations for each section shown."));
70 cl::alias SectionRelocationsShort("sr",
71 cl::desc("Alias for --section-relocations"),
72 cl::aliasopt(SectionRelocations));
73
74 // -section-symbols, -st
75 cl::opt<bool> SectionSymbols("section-symbols",
76 cl::desc("Display symbols for each section shown."));
77 cl::alias SectionSymbolsShort("st",
78 cl::desc("Alias for --section-symbols"),
79 cl::aliasopt(SectionSymbols));
80
81 // -section-data, -sd
82 cl::opt<bool> SectionData("section-data",
83 cl::desc("Display section data for each section shown."));
84 cl::alias SectionDataShort("sd",
85 cl::desc("Alias for --section-data"),
86 cl::aliasopt(SectionData));
87
88 // -relocations, -r
89 cl::opt<bool> Relocations("relocations",
90 cl::desc("Display the relocation entries in the file"));
91 cl::alias RelocationsShort("r",
92 cl::desc("Alias for --relocations"),
93 cl::aliasopt(Relocations));
94
Saleem Abdulrasool6a405442016-08-30 18:52:02 +000095 // -notes, -n
96 cl::opt<bool> Notes("notes", cl::desc("Display the ELF notes in the file"));
97 cl::alias NotesShort("n", cl::desc("Alias for --notes"), cl::aliasopt(Notes));
98
Michael J. Spencer594c0282015-06-25 21:47:32 +000099 // -dyn-relocations
100 cl::opt<bool> DynRelocs("dyn-relocations",
101 cl::desc("Display the dynamic relocation entries in the file"));
102
Eric Christopher9cad53c2013-04-03 18:31:38 +0000103 // -symbols, -t
104 cl::opt<bool> Symbols("symbols",
105 cl::desc("Display the symbol table"));
106 cl::alias SymbolsShort("t",
107 cl::desc("Alias for --symbols"),
108 cl::aliasopt(Symbols));
109
110 // -dyn-symbols, -dt
111 cl::opt<bool> DynamicSymbols("dyn-symbols",
112 cl::desc("Display the dynamic symbol table"));
113 cl::alias DynamicSymbolsShort("dt",
114 cl::desc("Alias for --dyn-symbols"),
115 cl::aliasopt(DynamicSymbols));
116
117 // -unwind, -u
118 cl::opt<bool> UnwindInfo("unwind",
119 cl::desc("Display unwind information"));
120 cl::alias UnwindInfoShort("u",
121 cl::desc("Alias for --unwind"),
122 cl::aliasopt(UnwindInfo));
123
124 // -dynamic-table
125 cl::opt<bool> DynamicTable("dynamic-table",
126 cl::desc("Display the ELF .dynamic section table"));
Saleem Abdulrasool6d9ca182016-07-20 01:16:28 +0000127 cl::alias DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
128 cl::aliasopt(DynamicTable));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000129
130 // -needed-libs
131 cl::opt<bool> NeededLibraries("needed-libs",
132 cl::desc("Display the needed libraries"));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000133
Nico Rieckd6df0542013-04-12 04:07:39 +0000134 // -program-headers
135 cl::opt<bool> ProgramHeaders("program-headers",
136 cl::desc("Display ELF program headers"));
Saleem Abdulrasool6d9ca182016-07-20 01:16:28 +0000137 cl::alias ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
138 cl::aliasopt(ProgramHeaders));
Nico Rieckd6df0542013-04-12 04:07:39 +0000139
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000140 // -hash-table
141 cl::opt<bool> HashTable("hash-table",
142 cl::desc("Display ELF hash table"));
143
Igor Kudrin496fb2f2015-10-14 12:11:50 +0000144 // -gnu-hash-table
145 cl::opt<bool> GnuHashTable("gnu-hash-table",
146 cl::desc("Display ELF .gnu.hash section"));
147
Nico Rieckf3f0b792013-04-12 04:01:52 +0000148 // -expand-relocs
149 cl::opt<bool> ExpandRelocs("expand-relocs",
150 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000151
Zachary Turner99f02152015-02-18 19:32:05 +0000152 // -codeview
153 cl::opt<bool> CodeView("codeview",
154 cl::desc("Display CodeView debug information"));
155
Reid Kleckner0b269742016-05-14 00:02:53 +0000156 // -codeview-merged-types
157 cl::opt<bool>
158 CodeViewMergedTypes("codeview-merged-types",
159 cl::desc("Display the merged CodeView type stream"));
160
Zachary Turner99f02152015-02-18 19:32:05 +0000161 // -codeview-subsection-bytes
162 cl::opt<bool> CodeViewSubsectionBytes(
163 "codeview-subsection-bytes",
164 cl::desc("Dump raw contents of codeview debug sections and records"));
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000165
166 // -arm-attributes, -a
167 cl::opt<bool> ARMAttributes("arm-attributes",
168 cl::desc("Display the ARM attributes section"));
Mehdi Aminia9d7aac2016-12-23 23:54:52 +0000169 cl::alias ARMAttributesShort("a", cl::desc("Alias for --arm-attributes"),
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000170 cl::aliasopt(ARMAttributes));
Simon Atanasyan80433902014-06-18 08:47:09 +0000171
172 // -mips-plt-got
173 cl::opt<bool>
174 MipsPLTGOT("mips-plt-got",
175 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Rui Ueyama1e152d52014-10-02 17:02:18 +0000176
Simon Atanasyanc914de22015-05-07 15:40:35 +0000177 // -mips-abi-flags
178 cl::opt<bool> MipsABIFlags("mips-abi-flags",
179 cl::desc("Display the MIPS.abiflags section"));
180
Simon Atanasyan6e07e932015-06-16 21:47:43 +0000181 // -mips-reginfo
182 cl::opt<bool> MipsReginfo("mips-reginfo",
183 cl::desc("Display the MIPS .reginfo section"));
184
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000185 // -mips-options
186 cl::opt<bool> MipsOptions("mips-options",
187 cl::desc("Display the MIPS .MIPS.options section"));
188
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000189 // -amdgpu-code-object-metadata
190 cl::opt<bool> AMDGPUCodeObjectMetadata(
191 "amdgpu-code-object-metadata",
192 cl::desc("Display AMDGPU code object metadata"));
Yaxun Liu07d659b2016-12-14 17:16:52 +0000193
Rui Ueyama1e152d52014-10-02 17:02:18 +0000194 // -coff-imports
195 cl::opt<bool>
196 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000197
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +0000198 // -coff-exports
199 cl::opt<bool>
200 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
201
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000202 // -coff-directives
203 cl::opt<bool>
204 COFFDirectives("coff-directives",
Rui Ueyama74e85132014-11-19 00:18:07 +0000205 cl::desc("Display the PE/COFF .drectve section"));
206
207 // -coff-basereloc
208 cl::opt<bool>
209 COFFBaseRelocs("coff-basereloc",
210 cl::desc("Display the PE/COFF .reloc section"));
Lang Hames0000afd2015-06-26 23:56:53 +0000211
Reid Kleckner2da433e2016-06-02 17:10:43 +0000212 // -coff-debug-directory
213 cl::opt<bool>
214 COFFDebugDirectory("coff-debug-directory",
215 cl::desc("Display the PE/COFF debug directory"));
216
Zachary Turner8d6396d2017-04-27 19:38:38 +0000217 // -coff-resources
218 cl::opt<bool> COFFResources("coff-resources",
219 cl::desc("Display the PE/COFF .rsrc section"));
220
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000221 // -coff-load-config
222 cl::opt<bool>
223 COFFLoadConfig("coff-load-config",
224 cl::desc("Display the PE/COFF load config"));
225
Davide Italiano07e7acb2015-08-21 20:28:30 +0000226 // -macho-data-in-code
227 cl::opt<bool>
228 MachODataInCode("macho-data-in-code",
229 cl::desc("Display MachO Data in Code command"));
230
Davide Italiano4410b222015-09-03 18:10:28 +0000231 // -macho-indirect-symbols
232 cl::opt<bool>
233 MachOIndirectSymbols("macho-indirect-symbols",
234 cl::desc("Display MachO indirect symbols"));
235
Davide Italiano9a429b72015-09-09 00:21:18 +0000236 // -macho-linker-options
237 cl::opt<bool>
238 MachOLinkerOptions("macho-linker-options",
239 cl::desc("Display MachO linker options"));
240
Davide Italianod1f09962015-09-02 16:24:24 +0000241 // -macho-segment
242 cl::opt<bool>
243 MachOSegment("macho-segment",
244 cl::desc("Display MachO Segment command"));
245
Davide Italiano976f4da2015-08-27 15:11:32 +0000246 // -macho-version-min
247 cl::opt<bool>
248 MachOVersionMin("macho-version-min",
249 cl::desc("Display MachO version min command"));
Davide Italiano35eebe12015-08-31 19:32:31 +0000250
251 // -macho-dysymtab
252 cl::opt<bool>
253 MachODysymtab("macho-dysymtab",
254 cl::desc("Display MachO Dysymtab command"));
255
Lang Hames0000afd2015-06-26 23:56:53 +0000256 // -stackmap
257 cl::opt<bool>
258 PrintStackMap("stackmap",
259 cl::desc("Display contents of stackmap section"));
260
Davide Italiano4f05f322015-10-16 23:19:01 +0000261 // -version-info
262 cl::opt<bool>
263 VersionInfo("version-info",
264 cl::desc("Display ELF version sections (if present)"));
265 cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
266 cl::aliasopt(VersionInfo));
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000267
268 cl::opt<bool> SectionGroups("elf-section-groups",
269 cl::desc("Display ELF section group contents"));
270 cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
271 cl::aliasopt(SectionGroups));
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000272 cl::opt<bool> HashHistogram(
273 "elf-hash-histogram",
274 cl::desc("Display bucket list histogram for hash sections"));
275 cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
276 cl::aliasopt(HashHistogram));
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000277
Tim Northoverd59b23a2016-03-01 21:45:22 +0000278 cl::opt<OutputStyleTy>
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000279 Output("elf-output-style", cl::desc("Specify ELF dump style"),
280 cl::values(clEnumVal(LLVM, "LLVM default style"),
Mehdi Amini732afdd2016-10-08 19:41:06 +0000281 clEnumVal(GNU, "GNU readelf style")),
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000282 cl::init(LLVM));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000283} // namespace opts
284
Rafael Espindola00ddb142015-07-20 03:38:17 +0000285namespace llvm {
286
Davide Italiano1eb92342015-12-04 19:29:49 +0000287LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
Davide Italiano0a07a822015-12-23 19:29:34 +0000288 errs() << "\nError reading file: " << Msg << ".\n";
289 errs().flush();
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000290 exit(1);
Rafael Espindola724d4b42015-07-20 03:01:49 +0000291}
292
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000293void error(Error EC) {
Zachary Turner660230e2016-08-04 19:39:55 +0000294 if (!EC)
295 return;
Zachary Turner660230e2016-08-04 19:39:55 +0000296 handleAllErrors(std::move(EC),
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000297 [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
Zachary Turner660230e2016-08-04 19:39:55 +0000298}
299
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000300void error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000301 if (!EC)
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000302 return;
Rafael Espindola8b3b09f2015-08-06 21:54:37 +0000303 reportError(EC.message());
Rafael Espindola144af2c2012-12-31 16:05:21 +0000304}
305
Eric Christopher9cad53c2013-04-03 18:31:38 +0000306bool relocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindolac7689302015-07-06 15:53:43 +0000307 return a.getOffset() < b.getOffset();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000308}
309
310} // namespace llvm
311
Rafael Espindola4453e42942014-06-13 03:07:50 +0000312static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000313 if (Input == "-")
314 Input = "<stdin>";
315
Rafael Espindola724d4b42015-07-20 03:01:49 +0000316 reportError(Twine(Input) + ": " + EC.message());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000317}
318
Lang Hamesfc209622016-07-14 02:24:01 +0000319static void reportError(StringRef Input, Error Err) {
320 if (Input == "-")
321 Input = "<stdin>";
322 std::string ErrMsg;
323 {
324 raw_string_ostream ErrStream(ErrMsg);
325 logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
326 }
327 reportError(ErrMsg);
328}
329
Simon Atanasyan80433902014-06-18 08:47:09 +0000330static bool isMipsArch(unsigned Arch) {
331 switch (Arch) {
332 case llvm::Triple::mips:
333 case llvm::Triple::mipsel:
334 case llvm::Triple::mips64:
335 case llvm::Triple::mips64el:
336 return true;
337 default:
338 return false;
339 }
340}
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000341namespace {
Zachary Turner4efa0a42016-11-08 22:24:53 +0000342struct ReadObjTypeTableBuilder {
Reid Kleckner5d577522017-03-24 17:26:38 +0000343 ReadObjTypeTableBuilder()
344 : Allocator(), IDTable(Allocator), TypeTable(Allocator) {}
Simon Atanasyan80433902014-06-18 08:47:09 +0000345
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000346 llvm::BumpPtrAllocator Allocator;
Reid Kleckner5d577522017-03-24 17:26:38 +0000347 llvm::codeview::TypeTableBuilder IDTable;
348 llvm::codeview::TypeTableBuilder TypeTable;
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000349};
350}
Zachary Turner4efa0a42016-11-08 22:24:53 +0000351static ReadObjTypeTableBuilder CVTypes;
Reid Kleckner0b269742016-05-14 00:02:53 +0000352
Eric Christopher9cad53c2013-04-03 18:31:38 +0000353/// @brief Creates an format-specific object file dumper.
Zachary Turner88bb1632016-05-03 00:28:04 +0000354static std::error_code createDumper(const ObjectFile *Obj,
355 ScopedPrinter &Writer,
Rafael Espindola4453e42942014-06-13 03:07:50 +0000356 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000357 if (!Obj)
358 return readobj_error::unsupported_file_format;
359
360 if (Obj->isCOFF())
361 return createCOFFDumper(Obj, Writer, Result);
362 if (Obj->isELF())
363 return createELFDumper(Obj, Writer, Result);
364 if (Obj->isMachO())
365 return createMachODumper(Obj, Writer, Result);
Derek Schuff6d76b7b2017-01-30 23:30:52 +0000366 if (Obj->isWasm())
367 return createWasmDumper(Obj, Writer, Result);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000368
369 return readobj_error::unsupported_obj_file_format;
370}
371
Eric Christopher9cad53c2013-04-03 18:31:38 +0000372/// @brief Dumps the specified object file.
373static void dumpObject(const ObjectFile *Obj) {
Zachary Turner88bb1632016-05-03 00:28:04 +0000374 ScopedPrinter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000375 std::unique_ptr<ObjDumper> Dumper;
Davide Italianoe66b73f2015-12-05 23:36:52 +0000376 if (std::error_code EC = createDumper(Obj, Writer, Dumper))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000377 reportError(Obj->getFileName(), EC);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000378
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000379 if (opts::Output == opts::LLVM) {
380 outs() << '\n';
381 outs() << "File: " << Obj->getFileName() << "\n";
382 outs() << "Format: " << Obj->getFileFormatName() << "\n";
383 outs() << "Arch: " << Triple::getArchTypeName(
384 (llvm::Triple::ArchType)Obj->getArch()) << "\n";
385 outs() << "AddressSize: " << (8 * Obj->getBytesInAddress()) << "bit\n";
386 Dumper->printLoadName();
387 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000388
389 if (opts::FileHeaders)
390 Dumper->printFileHeaders();
391 if (opts::Sections)
392 Dumper->printSections();
393 if (opts::Relocations)
394 Dumper->printRelocations();
Michael J. Spencer594c0282015-06-25 21:47:32 +0000395 if (opts::DynRelocs)
396 Dumper->printDynamicRelocations();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000397 if (opts::Symbols)
398 Dumper->printSymbols();
399 if (opts::DynamicSymbols)
400 Dumper->printDynamicSymbols();
401 if (opts::UnwindInfo)
402 Dumper->printUnwindInfo();
403 if (opts::DynamicTable)
404 Dumper->printDynamicTable();
405 if (opts::NeededLibraries)
406 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000407 if (opts::ProgramHeaders)
408 Dumper->printProgramHeaders();
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000409 if (opts::HashTable)
410 Dumper->printHashTable();
Igor Kudrin496fb2f2015-10-14 12:11:50 +0000411 if (opts::GnuHashTable)
412 Dumper->printGnuHashTable();
Davide Italiano4f05f322015-10-16 23:19:01 +0000413 if (opts::VersionInfo)
414 Dumper->printVersionInfo();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000415 if (Obj->isELF()) {
416 if (Obj->getArch() == llvm::Triple::arm)
417 if (opts::ARMAttributes)
418 Dumper->printAttributes();
419 if (isMipsArch(Obj->getArch())) {
420 if (opts::MipsPLTGOT)
421 Dumper->printMipsPLTGOT();
422 if (opts::MipsABIFlags)
423 Dumper->printMipsABIFlags();
424 if (opts::MipsReginfo)
425 Dumper->printMipsReginfo();
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000426 if (opts::MipsOptions)
427 Dumper->printMipsOptions();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000428 }
Yaxun Liu07d659b2016-12-14 17:16:52 +0000429 if (Obj->getArch() == llvm::Triple::amdgcn)
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000430 if (opts::AMDGPUCodeObjectMetadata)
431 Dumper->printAMDGPUCodeObjectMetadata();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000432 if (opts::SectionGroups)
433 Dumper->printGroupSections();
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000434 if (opts::HashHistogram)
435 Dumper->printHashHistogram();
Saleem Abdulrasool6a405442016-08-30 18:52:02 +0000436 if (opts::Notes)
437 Dumper->printNotes();
Simon Atanasyanc914de22015-05-07 15:40:35 +0000438 }
Davide Italianocd1b6db2015-07-24 02:14:20 +0000439 if (Obj->isCOFF()) {
440 if (opts::COFFImports)
441 Dumper->printCOFFImports();
442 if (opts::COFFExports)
443 Dumper->printCOFFExports();
444 if (opts::COFFDirectives)
445 Dumper->printCOFFDirectives();
446 if (opts::COFFBaseRelocs)
447 Dumper->printCOFFBaseReloc();
Reid Kleckner2da433e2016-06-02 17:10:43 +0000448 if (opts::COFFDebugDirectory)
449 Dumper->printCOFFDebugDirectory();
Zachary Turner8d6396d2017-04-27 19:38:38 +0000450 if (opts::COFFResources)
451 Dumper->printCOFFResources();
Reid Klecknerb7d716c2017-06-22 01:10:29 +0000452 if (opts::COFFLoadConfig)
453 Dumper->printCOFFLoadConfig();
Reid Kleckner83ebad32015-12-16 18:28:12 +0000454 if (opts::CodeView)
455 Dumper->printCodeViewDebugInfo();
Reid Kleckner0b269742016-05-14 00:02:53 +0000456 if (opts::CodeViewMergedTypes)
Reid Kleckner5d577522017-03-24 17:26:38 +0000457 Dumper->mergeCodeViewTypes(CVTypes.IDTable, CVTypes.TypeTable);
Davide Italianocd1b6db2015-07-24 02:14:20 +0000458 }
Davide Italiano1e12fc42015-08-31 17:12:23 +0000459 if (Obj->isMachO()) {
Davide Italiano07e7acb2015-08-21 20:28:30 +0000460 if (opts::MachODataInCode)
461 Dumper->printMachODataInCode();
Davide Italiano4410b222015-09-03 18:10:28 +0000462 if (opts::MachOIndirectSymbols)
463 Dumper->printMachOIndirectSymbols();
Davide Italiano9a429b72015-09-09 00:21:18 +0000464 if (opts::MachOLinkerOptions)
465 Dumper->printMachOLinkerOptions();
Davide Italianod1f09962015-09-02 16:24:24 +0000466 if (opts::MachOSegment)
467 Dumper->printMachOSegment();
Davide Italiano976f4da2015-08-27 15:11:32 +0000468 if (opts::MachOVersionMin)
469 Dumper->printMachOVersionMin();
Davide Italiano35eebe12015-08-31 19:32:31 +0000470 if (opts::MachODysymtab)
471 Dumper->printMachODysymtab();
Davide Italiano1e12fc42015-08-31 17:12:23 +0000472 }
Lang Hames0000afd2015-06-26 23:56:53 +0000473 if (opts::PrintStackMap)
474 Dumper->printStackMap();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000475}
476
Eric Christopher9cad53c2013-04-03 18:31:38 +0000477/// @brief Dumps each object file in \a Arc;
478static void dumpArchive(const Archive *Arc) {
Mehdi Amini41af4302016-11-11 04:28:40 +0000479 Error Err = Error::success();
Lang Hamesfc209622016-07-14 02:24:01 +0000480 for (auto &Child : Arc->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000481 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
482 if (!ChildOrErr) {
483 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
Sam Cleggc0d76642017-05-10 14:18:11 +0000484 reportError(Arc->getFileName(), ChildOrErr.takeError());
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000485 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000486 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000487 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000488 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000489 dumpObject(Obj);
Saleem Abdulrasool3780b3a2016-08-18 14:32:11 +0000490 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
491 dumpCOFFImportFile(Imp);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000492 else
493 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
494 }
Lang Hamesfc209622016-07-14 02:24:01 +0000495 if (Err)
496 reportError(Arc->getFileName(), std::move(Err));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000497}
498
Rafael Espindola8448a242015-03-24 20:26:55 +0000499/// @brief Dumps each object file in \a MachO Universal Binary;
500static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
501 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000502 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
David Blaikie29ac43c2015-04-13 16:05:49 +0000503 if (ObjOrErr)
504 dumpObject(&*ObjOrErr.get());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000505 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
Sam Cleggc0d76642017-05-10 14:18:11 +0000506 reportError(UBinary->getFileName(), ObjOrErr.takeError());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000507 }
Kevin Enderby42398052016-06-28 23:16:13 +0000508 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
David Blaikie29ac43c2015-04-13 16:05:49 +0000509 dumpArchive(&*AOrErr.get());
Rafael Espindola8448a242015-03-24 20:26:55 +0000510 }
511}
Eric Christopher9cad53c2013-04-03 18:31:38 +0000512
513/// @brief Opens \a File and dumps it.
514static void dumpInput(StringRef File) {
David Meyer2fc34c52012-03-01 01:36:50 +0000515
Eric Christopher9cad53c2013-04-03 18:31:38 +0000516 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000517 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
518 if (!BinaryOrErr)
Sam Cleggc0d76642017-05-10 14:18:11 +0000519 reportError(File, BinaryOrErr.takeError());
Rafael Espindola48af1c22014-08-19 18:44:46 +0000520 Binary &Binary = *BinaryOrErr.get().getBinary();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000521
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000522 if (Archive *Arc = dyn_cast<Archive>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000523 dumpArchive(Arc);
Rafael Espindola8448a242015-03-24 20:26:55 +0000524 else if (MachOUniversalBinary *UBinary =
525 dyn_cast<MachOUniversalBinary>(&Binary))
526 dumpMachOUniversalBinary(UBinary);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000527 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000528 dumpObject(Obj);
Rui Ueyama71ba9bd2015-08-28 07:40:30 +0000529 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
530 dumpCOFFImportFile(Import);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000531 else
532 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000533}
534
Eric Christopher9cad53c2013-04-03 18:31:38 +0000535int main(int argc, const char *argv[]) {
Richard Smith2ad6d482016-06-09 00:53:21 +0000536 sys::PrintStackTraceOnErrorSignal(argv[0]);
David Meyer2fc34c52012-03-01 01:36:50 +0000537 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000538 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000539
Eric Christopher9cad53c2013-04-03 18:31:38 +0000540 // Register the target printer for --version.
541 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000542
Eric Christopher9cad53c2013-04-03 18:31:38 +0000543 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000544
Eric Christopher9cad53c2013-04-03 18:31:38 +0000545 // Default to stdin if no filename is specified.
546 if (opts::InputFilenames.size() == 0)
547 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000548
Eric Christopher9cad53c2013-04-03 18:31:38 +0000549 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
550 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000551
Reid Kleckner0b269742016-05-14 00:02:53 +0000552 if (opts::CodeViewMergedTypes) {
553 ScopedPrinter W(outs());
Reid Kleckner5d577522017-03-24 17:26:38 +0000554 dumpCodeViewMergedTypes(W, CVTypes.IDTable, CVTypes.TypeTable);
Reid Kleckner0b269742016-05-14 00:02:53 +0000555 }
556
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000557 return 0;
David Meyer2fc34c52012-03-01 01:36:50 +0000558}