[llvm-objcopy] [COFF] Fix warnings abuilt missing field initialization. NFC.
[llvm-complete.git] / tools / llvm-readobj / llvm-readobj.cpp
blobf66564b02fb9a740183fff4d5c7dce8e908abf0d
1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
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 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.
14 // 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.
18 // Output should be specialized for each format where appropriate.
20 //===----------------------------------------------------------------------===//
22 #include "llvm-readobj.h"
23 #include "Error.h"
24 #include "ObjDumper.h"
25 #include "WindowsResourceDumper.h"
26 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
27 #include "llvm/Object/Archive.h"
28 #include "llvm/Object/COFFImportFile.h"
29 #include "llvm/Object/MachOUniversal.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Object/WindowsResource.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/DataTypes.h"
35 #include "llvm/Support/Debug.h"
36 #include "llvm/Support/FileSystem.h"
37 #include "llvm/Support/FormatVariadic.h"
38 #include "llvm/Support/InitLLVM.h"
39 #include "llvm/Support/Path.h"
40 #include "llvm/Support/ScopedPrinter.h"
41 #include "llvm/Support/TargetRegistry.h"
43 using namespace llvm;
44 using namespace llvm::object;
46 namespace opts {
47 cl::list<std::string> InputFilenames(cl::Positional,
48 cl::desc("<input object files>"),
49 cl::ZeroOrMore);
51 // -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));
60 // --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));
68 // -wide, -W
69 cl::opt<bool>
70 WideOutput("wide", cl::desc("Ignored for compatibility with GNU readelf"),
71 cl::Hidden);
72 cl::alias WideOutputShort("W",
73 cl::desc("Alias for --wide"),
74 cl::aliasopt(WideOutput));
76 // -file-headers, -file-header, -h
77 cl::opt<bool> FileHeaders("file-headers",
78 cl::desc("Display file headers "));
79 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));
85 // -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);
95 // -section-relocations, -sr
96 cl::opt<bool> SectionRelocations("section-relocations",
97 cl::desc("Display relocations for each section shown."));
98 cl::alias SectionRelocationsShort("sr",
99 cl::desc("Alias for --section-relocations"),
100 cl::aliasopt(SectionRelocations));
102 // -section-symbols, -st
103 cl::opt<bool> SectionSymbols("section-symbols",
104 cl::desc("Display symbols for each section shown."));
105 cl::alias SectionSymbolsShort("st",
106 cl::desc("Alias for --section-symbols"),
107 cl::aliasopt(SectionSymbols));
109 // -section-data, -sd
110 cl::opt<bool> SectionData("section-data",
111 cl::desc("Display section data for each section shown."));
112 cl::alias SectionDataShort("sd",
113 cl::desc("Alias for --section-data"),
114 cl::aliasopt(SectionData));
116 // -relocations, -relocs, -r
117 cl::opt<bool> Relocations("relocations",
118 cl::desc("Display the relocation entries in the file"));
119 cl::alias RelocationsShort("r", cl::desc("Alias for --relocations"),
120 cl::aliasopt(Relocations), cl::NotHidden);
121 cl::alias RelocationsGNU("relocs", cl::desc("Alias for --relocations"),
122 cl::aliasopt(Relocations));
124 // -notes, -n
125 cl::opt<bool> Notes("notes", cl::desc("Display the ELF notes in the file"));
126 cl::alias NotesShort("n", cl::desc("Alias for --notes"), cl::aliasopt(Notes));
128 // -dyn-relocations
129 cl::opt<bool> DynRelocs("dyn-relocations",
130 cl::desc("Display the dynamic relocation entries in the file"));
132 // -symbols
133 // Also -s in llvm-readelf mode, or -t in llvm-readobj mode.
134 cl::opt<bool> Symbols("symbols",
135 cl::desc("Display the symbol table"));
136 cl::alias SymbolsGNU("syms", cl::desc("Alias for --symbols"),
137 cl::aliasopt(Symbols));
139 // -dyn-symbols, -dyn-syms, -dt
140 cl::opt<bool> DynamicSymbols("dyn-symbols",
141 cl::desc("Display the dynamic symbol table"));
142 cl::alias DynamicSymbolsShort("dt",
143 cl::desc("Alias for --dyn-symbols"),
144 cl::aliasopt(DynamicSymbols));
145 cl::alias DynSymsGNU("dyn-syms", cl::desc("Alias for --dyn-symbols"),
146 cl::aliasopt(DynamicSymbols));
148 // -unwind, -u
149 cl::opt<bool> UnwindInfo("unwind",
150 cl::desc("Display unwind information"));
151 cl::alias UnwindInfoShort("u",
152 cl::desc("Alias for --unwind"),
153 cl::aliasopt(UnwindInfo));
155 // -dynamic-table, -dynamic, -d
156 cl::opt<bool> DynamicTable("dynamic-table",
157 cl::desc("Display the ELF .dynamic section table"));
158 cl::alias DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
159 cl::aliasopt(DynamicTable), cl::NotHidden);
160 cl::alias DynamicTableAlias("dynamic", cl::desc("Alias for --dynamic-table"),
161 cl::aliasopt(DynamicTable));
163 // -needed-libs
164 cl::opt<bool> NeededLibraries("needed-libs",
165 cl::desc("Display the needed libraries"));
167 // -program-headers, -segments, -l
168 cl::opt<bool> ProgramHeaders("program-headers",
169 cl::desc("Display ELF program headers"));
170 cl::alias ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
171 cl::aliasopt(ProgramHeaders), cl::NotHidden);
172 cl::alias SegmentsAlias("segments", cl::desc("Alias for --program-headers"),
173 cl::aliasopt(ProgramHeaders));
175 // -string-dump, -p
176 cl::list<std::string> StringDump("string-dump", cl::desc("<number|name>"),
177 cl::ZeroOrMore);
178 cl::alias StringDumpShort("p", cl::desc("Alias for --string-dump"),
179 cl::aliasopt(StringDump));
181 // -hex-dump, -x
182 cl::list<std::string> HexDump("hex-dump", cl::desc("<number|name>"),
183 cl::ZeroOrMore);
184 cl::alias HexDumpShort("x", cl::desc("Alias for --hex-dump"),
185 cl::aliasopt(HexDump));
187 // -hash-table
188 cl::opt<bool> HashTable("hash-table",
189 cl::desc("Display ELF hash table"));
191 // -gnu-hash-table
192 cl::opt<bool> GnuHashTable("gnu-hash-table",
193 cl::desc("Display ELF .gnu.hash section"));
195 // -expand-relocs
196 cl::opt<bool> ExpandRelocs("expand-relocs",
197 cl::desc("Expand each shown relocation to multiple lines"));
199 // -raw-relr
200 cl::opt<bool> RawRelr("raw-relr",
201 cl::desc("Do not decode relocations in SHT_RELR section, display raw contents"));
203 // -codeview
204 cl::opt<bool> CodeView("codeview",
205 cl::desc("Display CodeView debug information"));
207 // -codeview-merged-types
208 cl::opt<bool>
209 CodeViewMergedTypes("codeview-merged-types",
210 cl::desc("Display the merged CodeView type stream"));
212 // -codeview-subsection-bytes
213 cl::opt<bool> CodeViewSubsectionBytes(
214 "codeview-subsection-bytes",
215 cl::desc("Dump raw contents of codeview debug sections and records"));
217 // -arm-attributes
218 cl::opt<bool> ARMAttributes("arm-attributes",
219 cl::desc("Display the ARM attributes section"));
221 // -mips-plt-got
222 cl::opt<bool>
223 MipsPLTGOT("mips-plt-got",
224 cl::desc("Display the MIPS GOT and PLT GOT sections"));
226 // -mips-abi-flags
227 cl::opt<bool> MipsABIFlags("mips-abi-flags",
228 cl::desc("Display the MIPS.abiflags section"));
230 // -mips-reginfo
231 cl::opt<bool> MipsReginfo("mips-reginfo",
232 cl::desc("Display the MIPS .reginfo section"));
234 // -mips-options
235 cl::opt<bool> MipsOptions("mips-options",
236 cl::desc("Display the MIPS .MIPS.options section"));
238 // -coff-imports
239 cl::opt<bool>
240 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
242 // -coff-exports
243 cl::opt<bool>
244 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
246 // -coff-directives
247 cl::opt<bool>
248 COFFDirectives("coff-directives",
249 cl::desc("Display the PE/COFF .drectve section"));
251 // -coff-basereloc
252 cl::opt<bool>
253 COFFBaseRelocs("coff-basereloc",
254 cl::desc("Display the PE/COFF .reloc section"));
256 // -coff-debug-directory
257 cl::opt<bool>
258 COFFDebugDirectory("coff-debug-directory",
259 cl::desc("Display the PE/COFF debug directory"));
261 // -coff-resources
262 cl::opt<bool> COFFResources("coff-resources",
263 cl::desc("Display the PE/COFF .rsrc section"));
265 // -coff-load-config
266 cl::opt<bool>
267 COFFLoadConfig("coff-load-config",
268 cl::desc("Display the PE/COFF load config"));
270 // -elf-linker-options
271 cl::opt<bool>
272 ELFLinkerOptions("elf-linker-options",
273 cl::desc("Display the ELF .linker-options section"));
275 // -macho-data-in-code
276 cl::opt<bool>
277 MachODataInCode("macho-data-in-code",
278 cl::desc("Display MachO Data in Code command"));
280 // -macho-indirect-symbols
281 cl::opt<bool>
282 MachOIndirectSymbols("macho-indirect-symbols",
283 cl::desc("Display MachO indirect symbols"));
285 // -macho-linker-options
286 cl::opt<bool>
287 MachOLinkerOptions("macho-linker-options",
288 cl::desc("Display MachO linker options"));
290 // -macho-segment
291 cl::opt<bool>
292 MachOSegment("macho-segment",
293 cl::desc("Display MachO Segment command"));
295 // -macho-version-min
296 cl::opt<bool>
297 MachOVersionMin("macho-version-min",
298 cl::desc("Display MachO version min command"));
300 // -macho-dysymtab
301 cl::opt<bool>
302 MachODysymtab("macho-dysymtab",
303 cl::desc("Display MachO Dysymtab command"));
305 // -stackmap
306 cl::opt<bool>
307 PrintStackMap("stackmap",
308 cl::desc("Display contents of stackmap section"));
310 // -version-info, -V
311 cl::opt<bool>
312 VersionInfo("version-info",
313 cl::desc("Display ELF version sections (if present)"));
314 cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
315 cl::aliasopt(VersionInfo));
317 // -elf-section-groups, -section-groups, -g
318 cl::opt<bool> SectionGroups("elf-section-groups",
319 cl::desc("Display ELF section group contents"));
320 cl::alias SectionGroupsAlias("section-groups",
321 cl::desc("Alias for -elf-sections-groups"),
322 cl::aliasopt(SectionGroups));
323 cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
324 cl::aliasopt(SectionGroups));
326 // -elf-hash-histogram, -histogram, -I
327 cl::opt<bool> HashHistogram(
328 "elf-hash-histogram",
329 cl::desc("Display bucket list histogram for hash sections"));
330 cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
331 cl::aliasopt(HashHistogram));
332 cl::alias HistogramAlias("histogram",
333 cl::desc("Alias for --elf-hash-histogram"),
334 cl::aliasopt(HashHistogram));
336 // -elf-cg-profile
337 cl::opt<bool> CGProfile("elf-cg-profile", cl::desc("Display callgraph profile section"));
339 // -addrsig
340 cl::opt<bool> Addrsig("addrsig",
341 cl::desc("Display address-significance table"));
343 // -elf-output-style
344 cl::opt<OutputStyleTy>
345 Output("elf-output-style", cl::desc("Specify ELF dump style"),
346 cl::values(clEnumVal(LLVM, "LLVM default style"),
347 clEnumVal(GNU, "GNU readelf style")),
348 cl::init(LLVM));
349 } // namespace opts
351 namespace llvm {
353 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
354 errs() << "\nError reading file: " << Msg << ".\n";
355 errs().flush();
356 exit(1);
359 void error(Error EC) {
360 if (!EC)
361 return;
362 handleAllErrors(std::move(EC),
363 [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
366 void error(std::error_code EC) {
367 if (!EC)
368 return;
369 reportError(EC.message());
372 bool relocAddressLess(RelocationRef a, RelocationRef b) {
373 return a.getOffset() < b.getOffset();
376 } // namespace llvm
378 static void reportError(StringRef Input, std::error_code EC) {
379 if (Input == "-")
380 Input = "<stdin>";
382 reportError(Twine(Input) + ": " + EC.message());
385 static void reportError(StringRef Input, Error Err) {
386 if (Input == "-")
387 Input = "<stdin>";
388 std::string ErrMsg;
390 raw_string_ostream ErrStream(ErrMsg);
391 logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
393 reportError(ErrMsg);
396 static bool isMipsArch(unsigned Arch) {
397 switch (Arch) {
398 case llvm::Triple::mips:
399 case llvm::Triple::mipsel:
400 case llvm::Triple::mips64:
401 case llvm::Triple::mips64el:
402 return true;
403 default:
404 return false;
407 namespace {
408 struct ReadObjTypeTableBuilder {
409 ReadObjTypeTableBuilder()
410 : Allocator(), IDTable(Allocator), TypeTable(Allocator) {}
412 llvm::BumpPtrAllocator Allocator;
413 llvm::codeview::MergingTypeTableBuilder IDTable;
414 llvm::codeview::MergingTypeTableBuilder TypeTable;
417 static ReadObjTypeTableBuilder CVTypes;
419 /// Creates an format-specific object file dumper.
420 static std::error_code createDumper(const ObjectFile *Obj,
421 ScopedPrinter &Writer,
422 std::unique_ptr<ObjDumper> &Result) {
423 if (!Obj)
424 return readobj_error::unsupported_file_format;
426 if (Obj->isCOFF())
427 return createCOFFDumper(Obj, Writer, Result);
428 if (Obj->isELF())
429 return createELFDumper(Obj, Writer, Result);
430 if (Obj->isMachO())
431 return createMachODumper(Obj, Writer, Result);
432 if (Obj->isWasm())
433 return createWasmDumper(Obj, Writer, Result);
435 return readobj_error::unsupported_obj_file_format;
438 /// Dumps the specified object file.
439 static void dumpObject(const ObjectFile *Obj, ScopedPrinter &Writer) {
440 std::unique_ptr<ObjDumper> Dumper;
441 if (std::error_code EC = createDumper(Obj, Writer, Dumper))
442 reportError(Obj->getFileName(), EC);
444 if (opts::Output == opts::LLVM) {
445 Writer.startLine() << "\n";
446 Writer.printString("File", Obj->getFileName());
447 Writer.printString("Format", Obj->getFileFormatName());
448 Writer.printString("Arch", Triple::getArchTypeName(
449 (llvm::Triple::ArchType)Obj->getArch()));
450 Writer.printString("AddressSize",
451 formatv("{0}bit", 8 * Obj->getBytesInAddress()));
452 Dumper->printLoadName();
455 if (opts::FileHeaders)
456 Dumper->printFileHeaders();
457 if (opts::SectionHeaders)
458 Dumper->printSectionHeaders();
459 if (opts::Relocations)
460 Dumper->printRelocations();
461 if (opts::DynRelocs)
462 Dumper->printDynamicRelocations();
463 if (opts::Symbols)
464 Dumper->printSymbols();
465 if (opts::DynamicSymbols)
466 Dumper->printDynamicSymbols();
467 if (opts::UnwindInfo)
468 Dumper->printUnwindInfo();
469 if (opts::DynamicTable)
470 Dumper->printDynamicTable();
471 if (opts::NeededLibraries)
472 Dumper->printNeededLibraries();
473 if (opts::ProgramHeaders)
474 Dumper->printProgramHeaders();
475 if (!opts::StringDump.empty())
476 llvm::for_each(opts::StringDump, [&Dumper, Obj](StringRef SectionName) {
477 Dumper->printSectionAsString(Obj, SectionName);
479 if (!opts::HexDump.empty())
480 llvm::for_each(opts::HexDump, [&Dumper, Obj](StringRef SectionName) {
481 Dumper->printSectionAsHex(Obj, SectionName);
483 if (opts::HashTable)
484 Dumper->printHashTable();
485 if (opts::GnuHashTable)
486 Dumper->printGnuHashTable();
487 if (opts::VersionInfo)
488 Dumper->printVersionInfo();
489 if (Obj->isELF()) {
490 if (opts::ELFLinkerOptions)
491 Dumper->printELFLinkerOptions();
492 if (Obj->getArch() == llvm::Triple::arm)
493 if (opts::ARMAttributes)
494 Dumper->printAttributes();
495 if (isMipsArch(Obj->getArch())) {
496 if (opts::MipsPLTGOT)
497 Dumper->printMipsPLTGOT();
498 if (opts::MipsABIFlags)
499 Dumper->printMipsABIFlags();
500 if (opts::MipsReginfo)
501 Dumper->printMipsReginfo();
502 if (opts::MipsOptions)
503 Dumper->printMipsOptions();
505 if (opts::SectionGroups)
506 Dumper->printGroupSections();
507 if (opts::HashHistogram)
508 Dumper->printHashHistogram();
509 if (opts::CGProfile)
510 Dumper->printCGProfile();
511 if (opts::Addrsig)
512 Dumper->printAddrsig();
513 if (opts::Notes)
514 Dumper->printNotes();
516 if (Obj->isCOFF()) {
517 if (opts::COFFImports)
518 Dumper->printCOFFImports();
519 if (opts::COFFExports)
520 Dumper->printCOFFExports();
521 if (opts::COFFDirectives)
522 Dumper->printCOFFDirectives();
523 if (opts::COFFBaseRelocs)
524 Dumper->printCOFFBaseReloc();
525 if (opts::COFFDebugDirectory)
526 Dumper->printCOFFDebugDirectory();
527 if (opts::COFFResources)
528 Dumper->printCOFFResources();
529 if (opts::COFFLoadConfig)
530 Dumper->printCOFFLoadConfig();
531 if (opts::Addrsig)
532 Dumper->printAddrsig();
533 if (opts::CodeView)
534 Dumper->printCodeViewDebugInfo();
535 if (opts::CodeViewMergedTypes)
536 Dumper->mergeCodeViewTypes(CVTypes.IDTable, CVTypes.TypeTable);
538 if (Obj->isMachO()) {
539 if (opts::MachODataInCode)
540 Dumper->printMachODataInCode();
541 if (opts::MachOIndirectSymbols)
542 Dumper->printMachOIndirectSymbols();
543 if (opts::MachOLinkerOptions)
544 Dumper->printMachOLinkerOptions();
545 if (opts::MachOSegment)
546 Dumper->printMachOSegment();
547 if (opts::MachOVersionMin)
548 Dumper->printMachOVersionMin();
549 if (opts::MachODysymtab)
550 Dumper->printMachODysymtab();
552 if (opts::PrintStackMap)
553 Dumper->printStackMap();
556 /// Dumps each object file in \a Arc;
557 static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
558 Error Err = Error::success();
559 for (auto &Child : Arc->children(Err)) {
560 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
561 if (!ChildOrErr) {
562 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
563 reportError(Arc->getFileName(), ChildOrErr.takeError());
565 continue;
567 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
568 dumpObject(Obj, Writer);
569 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
570 dumpCOFFImportFile(Imp, Writer);
571 else
572 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
574 if (Err)
575 reportError(Arc->getFileName(), std::move(Err));
578 /// Dumps each object file in \a MachO Universal Binary;
579 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary,
580 ScopedPrinter &Writer) {
581 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
582 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
583 if (ObjOrErr)
584 dumpObject(&*ObjOrErr.get(), Writer);
585 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
586 reportError(UBinary->getFileName(), ObjOrErr.takeError());
588 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
589 dumpArchive(&*AOrErr.get(), Writer);
593 /// Dumps \a WinRes, Windows Resource (.res) file;
594 static void dumpWindowsResourceFile(WindowsResource *WinRes) {
595 ScopedPrinter Printer{outs()};
596 WindowsRes::Dumper Dumper(WinRes, Printer);
597 if (auto Err = Dumper.printData())
598 reportError(WinRes->getFileName(), std::move(Err));
602 /// Opens \a File and dumps it.
603 static void dumpInput(StringRef File) {
604 ScopedPrinter Writer(outs());
606 // Attempt to open the binary.
607 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
608 if (!BinaryOrErr)
609 reportError(File, BinaryOrErr.takeError());
610 Binary &Binary = *BinaryOrErr.get().getBinary();
612 if (Archive *Arc = dyn_cast<Archive>(&Binary))
613 dumpArchive(Arc, Writer);
614 else if (MachOUniversalBinary *UBinary =
615 dyn_cast<MachOUniversalBinary>(&Binary))
616 dumpMachOUniversalBinary(UBinary, Writer);
617 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
618 dumpObject(Obj, Writer);
619 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
620 dumpCOFFImportFile(Import, Writer);
621 else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(&Binary))
622 dumpWindowsResourceFile(WinRes);
623 else
624 reportError(File, readobj_error::unrecognized_file_format);
627 /// Registers aliases that should only be allowed by readobj.
628 static void registerReadobjAliases() {
629 // -s has meant --sections for a very long time in llvm-readobj despite
630 // meaning --symbols in readelf.
631 static cl::alias SectionsShort("s", cl::desc("Alias for --section-headers"),
632 cl::aliasopt(opts::SectionHeaders),
633 cl::NotHidden);
635 // Only register -t in llvm-readobj, as readelf reserves it for
636 // --section-details (not implemented yet).
637 static cl::alias SymbolsShort("t", cl::desc("Alias for --symbols"),
638 cl::aliasopt(opts::Symbols), cl::NotHidden);
641 /// Registers aliases that should only be allowed by readelf.
642 static void registerReadelfAliases() {
643 // -s is here because for readobj it means --sections.
644 static cl::alias SymbolsShort("s", cl::desc("Alias for --symbols"),
645 cl::aliasopt(opts::Symbols), cl::NotHidden);
648 int main(int argc, const char *argv[]) {
649 InitLLVM X(argc, argv);
651 // Register the target printer for --version.
652 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
654 if (sys::path::stem(argv[0]).contains("readelf")) {
655 opts::Output = opts::GNU;
656 registerReadelfAliases();
657 } else {
658 registerReadobjAliases();
661 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
663 if (opts::All) {
664 opts::FileHeaders = true;
665 opts::ProgramHeaders = true;
666 opts::SectionHeaders = true;
667 opts::Symbols = true;
668 opts::Relocations = true;
669 opts::DynamicTable = true;
670 opts::Notes = true;
671 opts::VersionInfo = true;
672 opts::UnwindInfo = true;
673 opts::SectionGroups = true;
674 opts::HashHistogram = true;
677 if (opts::Headers) {
678 opts::FileHeaders = true;
679 opts::ProgramHeaders = true;
680 opts::SectionHeaders = true;
683 // Default to stdin if no filename is specified.
684 if (opts::InputFilenames.empty())
685 opts::InputFilenames.push_back("-");
687 llvm::for_each(opts::InputFilenames, dumpInput);
689 if (opts::CodeViewMergedTypes) {
690 ScopedPrinter W(outs());
691 dumpCodeViewMergedTypes(W, CVTypes.IDTable, CVTypes.TypeTable);
694 return 0;