1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This is a tool similar to readelf, except it works on multiple object file
10 // formats. The main purpose of this tool is to provide detailed output suitable
13 // Flags should be similar to readelf where supported, but the output format
14 // does not need to be identical. The point is to not make users learn yet
15 // another set of flags.
17 // Output should be specialized for each format where appropriate.
19 //===----------------------------------------------------------------------===//
21 #include "llvm-readobj.h"
23 #include "ObjDumper.h"
24 #include "WindowsResourceDumper.h"
25 #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.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"
42 #include "llvm/Support/WithColor.h"
45 using namespace llvm::object
;
48 cl::list
<std::string
> InputFilenames(cl::Positional
,
49 cl::desc("<input object files>"),
55 cl::desc("Equivalent to setting: --file-headers, --program-headers, "
56 "--section-headers, --symbols, --relocations, "
57 "--dynamic-table, --notes, --version-info, --unwind, "
58 "--section-groups and --elf-hash-histogram."));
59 cl::alias
AllShort("a", cl::desc("Alias for --all"), cl::aliasopt(All
));
64 cl::desc("Equivalent to setting: --file-headers, --program-headers, "
65 "--section-headers"));
66 cl::alias
HeadersShort("e", cl::desc("Alias for --headers"),
67 cl::aliasopt(Headers
));
71 WideOutput("wide", cl::desc("Ignored for compatibility with GNU readelf"),
73 cl::alias
WideOutputShort("W",
74 cl::desc("Alias for --wide"),
75 cl::aliasopt(WideOutput
));
77 // --file-headers, --file-header, -h
78 cl::opt
<bool> FileHeaders("file-headers",
79 cl::desc("Display file headers "));
80 cl::alias
FileHeadersShort("h", cl::desc("Alias for --file-headers"),
81 cl::aliasopt(FileHeaders
), cl::NotHidden
);
82 cl::alias
FileHeadersSingular("file-header",
83 cl::desc("Alias for --file-headers"),
84 cl::aliasopt(FileHeaders
));
86 // --section-headers, --sections, -S
87 // Also -s in llvm-readobj mode.
88 cl::opt
<bool> SectionHeaders("section-headers",
89 cl::desc("Display all section headers."));
90 cl::alias
SectionsShortUpper("S", cl::desc("Alias for --section-headers"),
91 cl::aliasopt(SectionHeaders
), cl::NotHidden
);
92 cl::alias
SectionHeadersAlias("sections",
93 cl::desc("Alias for --section-headers"),
94 cl::aliasopt(SectionHeaders
), cl::NotHidden
);
96 // --section-relocations
97 // Also --sr in llvm-readobj mode.
98 cl::opt
<bool> SectionRelocations("section-relocations",
99 cl::desc("Display relocations for each section shown."));
102 // Also --st in llvm-readobj mode.
103 cl::opt
<bool> SectionSymbols("section-symbols",
104 cl::desc("Display symbols for each section shown."));
107 // Also --sd in llvm-readobj mode.
108 cl::opt
<bool> SectionData("section-data",
109 cl::desc("Display section data for each section shown."));
112 cl::opt
<cl::boolOrDefault
>
113 SectionMapping("section-mapping",
114 cl::desc("Display the section to segment mapping."));
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
));
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
));
129 cl::opt
<bool> DynRelocs("dyn-relocations",
130 cl::desc("Display the dynamic relocation entries in the file"));
133 // Also -s in llvm-readelf mode, or -t in llvm-readobj mode.
136 cl::desc("Display the symbol table. Also display the dynamic "
137 "symbol table when using GNU output style for ELF"));
138 cl::alias
SymbolsGNU("syms", cl::desc("Alias for --symbols"),
139 cl::aliasopt(Symbols
));
141 // --dyn-symbols, --dyn-syms
142 // Also --dt in llvm-readobj mode.
143 cl::opt
<bool> DynamicSymbols("dyn-symbols",
144 cl::desc("Display the dynamic symbol table"));
145 cl::alias
DynSymsGNU("dyn-syms", cl::desc("Alias for --dyn-symbols"),
146 cl::aliasopt(DynamicSymbols
));
149 cl::opt
<bool> HashSymbols(
151 cl::desc("Display the dynamic symbols derived from the hash section"));
154 cl::opt
<bool> UnwindInfo("unwind",
155 cl::desc("Display unwind information"));
156 cl::alias
UnwindInfoShort("u",
157 cl::desc("Alias for --unwind"),
158 cl::aliasopt(UnwindInfo
));
160 // --dynamic-table, --dynamic, -d
161 cl::opt
<bool> DynamicTable("dynamic-table",
162 cl::desc("Display the ELF .dynamic section table"));
163 cl::alias
DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
164 cl::aliasopt(DynamicTable
), cl::NotHidden
);
165 cl::alias
DynamicTableAlias("dynamic", cl::desc("Alias for --dynamic-table"),
166 cl::aliasopt(DynamicTable
));
169 cl::opt
<bool> NeededLibraries("needed-libs",
170 cl::desc("Display the needed libraries"));
172 // --program-headers, --segments, -l
173 cl::opt
<bool> ProgramHeaders("program-headers",
174 cl::desc("Display ELF program headers"));
175 cl::alias
ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
176 cl::aliasopt(ProgramHeaders
), cl::NotHidden
);
177 cl::alias
SegmentsAlias("segments", cl::desc("Alias for --program-headers"),
178 cl::aliasopt(ProgramHeaders
));
181 cl::list
<std::string
> StringDump("string-dump", cl::desc("<number|name>"),
183 cl::alias
StringDumpShort("p", cl::desc("Alias for --string-dump"),
184 cl::aliasopt(StringDump
), cl::Prefix
);
187 cl::list
<std::string
> HexDump("hex-dump", cl::desc("<number|name>"),
189 cl::alias
HexDumpShort("x", cl::desc("Alias for --hex-dump"),
190 cl::aliasopt(HexDump
), cl::Prefix
);
193 cl::opt
<bool> Demangle("demangle",
194 cl::desc("Demangle symbol names in output"));
195 cl::alias
DemangleShort("C", cl::desc("Alias for --demangle"),
196 cl::aliasopt(Demangle
), cl::NotHidden
);
199 cl::opt
<bool> HashTable("hash-table",
200 cl::desc("Display ELF hash table"));
203 cl::opt
<bool> GnuHashTable("gnu-hash-table",
204 cl::desc("Display ELF .gnu.hash section"));
207 cl::opt
<bool> ExpandRelocs("expand-relocs",
208 cl::desc("Expand each shown relocation to multiple lines"));
211 cl::opt
<bool> RawRelr("raw-relr",
212 cl::desc("Do not decode relocations in SHT_RELR section, display raw contents"));
215 cl::opt
<bool> CodeView("codeview",
216 cl::desc("Display CodeView debug information"));
218 // --codeview-merged-types
220 CodeViewMergedTypes("codeview-merged-types",
221 cl::desc("Display the merged CodeView type stream"));
224 cl::opt
<bool> CodeViewEnableGHash(
227 "Enable global hashing for CodeView type stream de-duplication"));
229 // --codeview-subsection-bytes
230 cl::opt
<bool> CodeViewSubsectionBytes(
231 "codeview-subsection-bytes",
232 cl::desc("Dump raw contents of codeview debug sections and records"));
235 cl::opt
<bool> ARMAttributes("arm-attributes",
236 cl::desc("Display the ARM attributes section"));
240 MipsPLTGOT("mips-plt-got",
241 cl::desc("Display the MIPS GOT and PLT GOT sections"));
244 cl::opt
<bool> MipsABIFlags("mips-abi-flags",
245 cl::desc("Display the MIPS.abiflags section"));
248 cl::opt
<bool> MipsReginfo("mips-reginfo",
249 cl::desc("Display the MIPS .reginfo section"));
252 cl::opt
<bool> MipsOptions("mips-options",
253 cl::desc("Display the MIPS .MIPS.options section"));
257 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
261 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
265 COFFDirectives("coff-directives",
266 cl::desc("Display the PE/COFF .drectve section"));
270 COFFBaseRelocs("coff-basereloc",
271 cl::desc("Display the PE/COFF .reloc section"));
273 // --coff-debug-directory
275 COFFDebugDirectory("coff-debug-directory",
276 cl::desc("Display the PE/COFF debug directory"));
279 cl::opt
<bool> COFFResources("coff-resources",
280 cl::desc("Display the PE/COFF .rsrc section"));
282 // --coff-load-config
284 COFFLoadConfig("coff-load-config",
285 cl::desc("Display the PE/COFF load config"));
287 // --elf-linker-options
289 ELFLinkerOptions("elf-linker-options",
290 cl::desc("Display the ELF .linker-options section"));
292 // --macho-data-in-code
294 MachODataInCode("macho-data-in-code",
295 cl::desc("Display MachO Data in Code command"));
297 // --macho-indirect-symbols
299 MachOIndirectSymbols("macho-indirect-symbols",
300 cl::desc("Display MachO indirect symbols"));
302 // --macho-linker-options
304 MachOLinkerOptions("macho-linker-options",
305 cl::desc("Display MachO linker options"));
309 MachOSegment("macho-segment",
310 cl::desc("Display MachO Segment command"));
312 // --macho-version-min
314 MachOVersionMin("macho-version-min",
315 cl::desc("Display MachO version min command"));
319 MachODysymtab("macho-dysymtab",
320 cl::desc("Display MachO Dysymtab command"));
324 PrintStackMap("stackmap",
325 cl::desc("Display contents of stackmap section"));
329 PrintStackSizes("stack-sizes",
330 cl::desc("Display contents of all stack sizes sections"));
332 // --version-info, -V
334 VersionInfo("version-info",
335 cl::desc("Display ELF version sections (if present)"));
336 cl::alias
VersionInfoShort("V", cl::desc("Alias for -version-info"),
337 cl::aliasopt(VersionInfo
));
339 // --elf-section-groups, --section-groups, -g
340 cl::opt
<bool> SectionGroups("elf-section-groups",
341 cl::desc("Display ELF section group contents"));
342 cl::alias
SectionGroupsAlias("section-groups",
343 cl::desc("Alias for -elf-sections-groups"),
344 cl::aliasopt(SectionGroups
));
345 cl::alias
SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
346 cl::aliasopt(SectionGroups
));
348 // --elf-hash-histogram, --histogram, -I
349 cl::opt
<bool> HashHistogram(
350 "elf-hash-histogram",
351 cl::desc("Display bucket list histogram for hash sections"));
352 cl::alias
HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
353 cl::aliasopt(HashHistogram
));
354 cl::alias
HistogramAlias("histogram",
355 cl::desc("Alias for --elf-hash-histogram"),
356 cl::aliasopt(HashHistogram
));
359 cl::opt
<bool> CGProfile("elf-cg-profile", cl::desc("Display callgraph profile section"));
362 cl::opt
<bool> Addrsig("addrsig",
363 cl::desc("Display address-significance table"));
366 cl::opt
<OutputStyleTy
>
367 Output("elf-output-style", cl::desc("Specify ELF dump style"),
368 cl::values(clEnumVal(LLVM
, "LLVM default style"),
369 clEnumVal(GNU
, "GNU readelf style")),
373 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
378 LLVM_ATTRIBUTE_NORETURN
void reportError(Twine Msg
) {
381 WithColor::error(errs()) << Msg
<< "\n";
385 void reportError(StringRef Input
, Error Err
) {
388 error(createFileError(Input
, std::move(Err
)));
391 void reportWarning(Twine Msg
) {
394 WithColor::warning(errs()) << Msg
<< "\n";
397 void reportWarning(StringRef Input
, Error Err
) {
400 warn(createFileError(Input
, std::move(Err
)));
403 void warn(Error Err
) {
404 handleAllErrors(std::move(Err
), [&](const ErrorInfoBase
&EI
) {
405 reportWarning(EI
.message());
409 void error(Error EC
) {
412 handleAllErrors(std::move(EC
),
413 [&](const ErrorInfoBase
&EI
) { reportError(EI
.message()); });
416 void error(std::error_code EC
) {
419 reportError(EC
.message());
424 static void reportError(StringRef Input
, std::error_code EC
) {
425 reportError(Input
, errorCodeToError(EC
));
428 static bool isMipsArch(unsigned Arch
) {
430 case llvm::Triple::mips
:
431 case llvm::Triple::mipsel
:
432 case llvm::Triple::mips64
:
433 case llvm::Triple::mips64el
:
440 struct ReadObjTypeTableBuilder
{
441 ReadObjTypeTableBuilder()
442 : Allocator(), IDTable(Allocator
), TypeTable(Allocator
),
443 GlobalIDTable(Allocator
), GlobalTypeTable(Allocator
) {}
445 llvm::BumpPtrAllocator Allocator
;
446 llvm::codeview::MergingTypeTableBuilder IDTable
;
447 llvm::codeview::MergingTypeTableBuilder TypeTable
;
448 llvm::codeview::GlobalTypeTableBuilder GlobalIDTable
;
449 llvm::codeview::GlobalTypeTableBuilder GlobalTypeTable
;
450 std::vector
<OwningBinary
<Binary
>> Binaries
;
453 static ReadObjTypeTableBuilder CVTypes
;
455 /// Creates an format-specific object file dumper.
456 static std::error_code
createDumper(const ObjectFile
*Obj
,
457 ScopedPrinter
&Writer
,
458 std::unique_ptr
<ObjDumper
> &Result
) {
460 return readobj_error::unsupported_file_format
;
463 return createCOFFDumper(Obj
, Writer
, Result
);
465 return createELFDumper(Obj
, Writer
, Result
);
467 return createMachODumper(Obj
, Writer
, Result
);
469 return createWasmDumper(Obj
, Writer
, Result
);
471 return createXCOFFDumper(Obj
, Writer
, Result
);
473 return readobj_error::unsupported_obj_file_format
;
476 /// Dumps the specified object file.
477 static void dumpObject(const ObjectFile
*Obj
, ScopedPrinter
&Writer
,
478 const Archive
*A
= nullptr) {
479 std::string FileStr
=
480 A
? Twine(A
->getFileName() + "(" + Obj
->getFileName() + ")").str()
481 : Obj
->getFileName().str();
483 std::unique_ptr
<ObjDumper
> Dumper
;
484 if (std::error_code EC
= createDumper(Obj
, Writer
, Dumper
))
485 reportError(FileStr
, EC
);
487 Writer
.startLine() << "\n";
488 if (opts::Output
== opts::LLVM
) {
489 Writer
.printString("File", FileStr
);
490 Writer
.printString("Format", Obj
->getFileFormatName());
491 Writer
.printString("Arch", Triple::getArchTypeName(
492 (llvm::Triple::ArchType
)Obj
->getArch()));
493 Writer
.printString("AddressSize",
494 formatv("{0}bit", 8 * Obj
->getBytesInAddress()));
495 Dumper
->printLoadName();
496 } else if (opts::Output
== opts::GNU
&& A
) {
497 Writer
.printString("File", FileStr
);
500 if (opts::FileHeaders
)
501 Dumper
->printFileHeaders();
502 if (opts::SectionHeaders
)
503 Dumper
->printSectionHeaders();
504 if (opts::Relocations
)
505 Dumper
->printRelocations();
507 Dumper
->printDynamicRelocations();
508 if (opts::Symbols
|| opts::DynamicSymbols
)
509 Dumper
->printSymbols(opts::Symbols
, opts::DynamicSymbols
);
510 if (opts::HashSymbols
)
511 Dumper
->printHashSymbols();
512 if (opts::UnwindInfo
)
513 Dumper
->printUnwindInfo();
514 if (opts::DynamicTable
)
515 Dumper
->printDynamicTable();
516 if (opts::NeededLibraries
)
517 Dumper
->printNeededLibraries();
518 if (opts::ProgramHeaders
|| opts::SectionMapping
== cl::BOU_TRUE
)
519 Dumper
->printProgramHeaders(opts::ProgramHeaders
, opts::SectionMapping
);
520 if (!opts::StringDump
.empty())
521 Dumper
->printSectionsAsString(Obj
, opts::StringDump
);
522 if (!opts::HexDump
.empty())
523 Dumper
->printSectionsAsHex(Obj
, opts::HexDump
);
525 Dumper
->printHashTable();
526 if (opts::GnuHashTable
)
527 Dumper
->printGnuHashTable();
528 if (opts::VersionInfo
)
529 Dumper
->printVersionInfo();
531 if (opts::ELFLinkerOptions
)
532 Dumper
->printELFLinkerOptions();
533 if (Obj
->getArch() == llvm::Triple::arm
)
534 if (opts::ARMAttributes
)
535 Dumper
->printAttributes();
536 if (isMipsArch(Obj
->getArch())) {
537 if (opts::MipsPLTGOT
)
538 Dumper
->printMipsPLTGOT();
539 if (opts::MipsABIFlags
)
540 Dumper
->printMipsABIFlags();
541 if (opts::MipsReginfo
)
542 Dumper
->printMipsReginfo();
543 if (opts::MipsOptions
)
544 Dumper
->printMipsOptions();
546 if (opts::SectionGroups
)
547 Dumper
->printGroupSections();
548 if (opts::HashHistogram
)
549 Dumper
->printHashHistogram();
551 Dumper
->printCGProfile();
553 Dumper
->printAddrsig();
555 Dumper
->printNotes();
558 if (opts::COFFImports
)
559 Dumper
->printCOFFImports();
560 if (opts::COFFExports
)
561 Dumper
->printCOFFExports();
562 if (opts::COFFDirectives
)
563 Dumper
->printCOFFDirectives();
564 if (opts::COFFBaseRelocs
)
565 Dumper
->printCOFFBaseReloc();
566 if (opts::COFFDebugDirectory
)
567 Dumper
->printCOFFDebugDirectory();
568 if (opts::COFFResources
)
569 Dumper
->printCOFFResources();
570 if (opts::COFFLoadConfig
)
571 Dumper
->printCOFFLoadConfig();
573 Dumper
->printAddrsig();
575 Dumper
->printCodeViewDebugInfo();
576 if (opts::CodeViewMergedTypes
)
577 Dumper
->mergeCodeViewTypes(CVTypes
.IDTable
, CVTypes
.TypeTable
,
578 CVTypes
.GlobalIDTable
, CVTypes
.GlobalTypeTable
,
579 opts::CodeViewEnableGHash
);
581 if (Obj
->isMachO()) {
582 if (opts::MachODataInCode
)
583 Dumper
->printMachODataInCode();
584 if (opts::MachOIndirectSymbols
)
585 Dumper
->printMachOIndirectSymbols();
586 if (opts::MachOLinkerOptions
)
587 Dumper
->printMachOLinkerOptions();
588 if (opts::MachOSegment
)
589 Dumper
->printMachOSegment();
590 if (opts::MachOVersionMin
)
591 Dumper
->printMachOVersionMin();
592 if (opts::MachODysymtab
)
593 Dumper
->printMachODysymtab();
595 if (opts::PrintStackMap
)
596 Dumper
->printStackMap();
597 if (opts::PrintStackSizes
)
598 Dumper
->printStackSizes();
601 /// Dumps each object file in \a Arc;
602 static void dumpArchive(const Archive
*Arc
, ScopedPrinter
&Writer
) {
603 Error Err
= Error::success();
604 for (auto &Child
: Arc
->children(Err
)) {
605 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= Child
.getAsBinary();
607 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError())) {
608 reportError(Arc
->getFileName(), std::move(E
));
612 if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(&*ChildOrErr
.get()))
613 dumpObject(Obj
, Writer
, Arc
);
614 else if (COFFImportFile
*Imp
= dyn_cast
<COFFImportFile
>(&*ChildOrErr
.get()))
615 dumpCOFFImportFile(Imp
, Writer
);
617 reportError(Arc
->getFileName(), readobj_error::unrecognized_file_format
);
620 reportError(Arc
->getFileName(), std::move(Err
));
623 /// Dumps each object file in \a MachO Universal Binary;
624 static void dumpMachOUniversalBinary(const MachOUniversalBinary
*UBinary
,
625 ScopedPrinter
&Writer
) {
626 for (const MachOUniversalBinary::ObjectForArch
&Obj
: UBinary
->objects()) {
627 Expected
<std::unique_ptr
<MachOObjectFile
>> ObjOrErr
= Obj
.getAsObjectFile();
629 dumpObject(&*ObjOrErr
.get(), Writer
);
630 else if (auto E
= isNotObjectErrorInvalidFileType(ObjOrErr
.takeError())) {
631 reportError(UBinary
->getFileName(), ObjOrErr
.takeError());
633 else if (Expected
<std::unique_ptr
<Archive
>> AOrErr
= Obj
.getAsArchive())
634 dumpArchive(&*AOrErr
.get(), Writer
);
638 /// Dumps \a WinRes, Windows Resource (.res) file;
639 static void dumpWindowsResourceFile(WindowsResource
*WinRes
,
640 ScopedPrinter
&Printer
) {
641 WindowsRes::Dumper
Dumper(WinRes
, Printer
);
642 if (auto Err
= Dumper
.printData())
643 reportError(WinRes
->getFileName(), std::move(Err
));
647 /// Opens \a File and dumps it.
648 static void dumpInput(StringRef File
, ScopedPrinter
&Writer
) {
649 // Attempt to open the binary.
650 Expected
<OwningBinary
<Binary
>> BinaryOrErr
= createBinary(File
);
652 reportError(File
, BinaryOrErr
.takeError());
653 Binary
&Binary
= *BinaryOrErr
.get().getBinary();
655 if (Archive
*Arc
= dyn_cast
<Archive
>(&Binary
))
656 dumpArchive(Arc
, Writer
);
657 else if (MachOUniversalBinary
*UBinary
=
658 dyn_cast
<MachOUniversalBinary
>(&Binary
))
659 dumpMachOUniversalBinary(UBinary
, Writer
);
660 else if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(&Binary
))
661 dumpObject(Obj
, Writer
);
662 else if (COFFImportFile
*Import
= dyn_cast
<COFFImportFile
>(&Binary
))
663 dumpCOFFImportFile(Import
, Writer
);
664 else if (WindowsResource
*WinRes
= dyn_cast
<WindowsResource
>(&Binary
))
665 dumpWindowsResourceFile(WinRes
, Writer
);
667 reportError(File
, readobj_error::unrecognized_file_format
);
669 CVTypes
.Binaries
.push_back(std::move(*BinaryOrErr
));
672 /// Registers aliases that should only be allowed by readobj.
673 static void registerReadobjAliases() {
674 // -s has meant --sections for a very long time in llvm-readobj despite
675 // meaning --symbols in readelf.
676 static cl::alias
SectionsShort("s", cl::desc("Alias for --section-headers"),
677 cl::aliasopt(opts::SectionHeaders
),
680 // Only register -t in llvm-readobj, as readelf reserves it for
681 // --section-details (not implemented yet).
682 static cl::alias
SymbolsShort("t", cl::desc("Alias for --symbols"),
683 cl::aliasopt(opts::Symbols
), cl::NotHidden
);
685 // The following two-letter aliases are only provided for readobj, as readelf
686 // allows single-letter args to be grouped together.
687 static cl::alias
SectionRelocationsShort(
688 "sr", cl::desc("Alias for --section-relocations"),
689 cl::aliasopt(opts::SectionRelocations
));
690 static cl::alias
SectionDataShort("sd", cl::desc("Alias for --section-data"),
691 cl::aliasopt(opts::SectionData
));
692 static cl::alias
SectionSymbolsShort("st",
693 cl::desc("Alias for --section-symbols"),
694 cl::aliasopt(opts::SectionSymbols
));
695 static cl::alias
DynamicSymbolsShort("dt",
696 cl::desc("Alias for --dyn-symbols"),
697 cl::aliasopt(opts::DynamicSymbols
));
700 /// Registers aliases that should only be allowed by readelf.
701 static void registerReadelfAliases() {
702 // -s is here because for readobj it means --sections.
703 static cl::alias
SymbolsShort("s", cl::desc("Alias for --symbols"),
704 cl::aliasopt(opts::Symbols
), cl::NotHidden
,
707 // Allow all single letter flags to be grouped together.
708 for (auto &OptEntry
: cl::getRegisteredOptions()) {
709 StringRef ArgName
= OptEntry
.getKey();
710 cl::Option
*Option
= OptEntry
.getValue();
711 if (ArgName
.size() == 1)
712 apply(Option
, cl::Grouping
);
716 int main(int argc
, const char *argv
[]) {
717 InitLLVM
X(argc
, argv
);
719 // Register the target printer for --version.
720 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion
);
722 if (sys::path::stem(argv
[0]).contains("readelf")) {
723 opts::Output
= opts::GNU
;
724 registerReadelfAliases();
726 registerReadobjAliases();
729 cl::ParseCommandLineOptions(argc
, argv
, "LLVM Object Reader\n");
732 opts::FileHeaders
= true;
733 opts::ProgramHeaders
= true;
734 opts::SectionHeaders
= true;
735 opts::Symbols
= true;
736 opts::Relocations
= true;
737 opts::DynamicTable
= true;
739 opts::VersionInfo
= true;
740 opts::UnwindInfo
= true;
741 opts::SectionGroups
= true;
742 opts::HashHistogram
= true;
743 // FIXME: As soon as we implement LLVM-style printing of the .stack_size
744 // section, we will enable it with --all (only for LLVM-style).
745 if (opts::Output
== opts::LLVM
)
746 opts::PrintStackSizes
= false;
750 opts::FileHeaders
= true;
751 opts::ProgramHeaders
= true;
752 opts::SectionHeaders
= true;
755 // Default to stdin if no filename is specified.
756 if (opts::InputFilenames
.empty())
757 opts::InputFilenames
.push_back("-");
759 ScopedPrinter
Writer(fouts());
760 for (const std::string
&I
: opts::InputFilenames
)
761 dumpInput(I
, Writer
);
763 if (opts::CodeViewMergedTypes
) {
764 if (opts::CodeViewEnableGHash
)
765 dumpCodeViewMergedTypes(Writer
, CVTypes
.GlobalIDTable
.records(),
766 CVTypes
.GlobalTypeTable
.records());
768 dumpCodeViewMergedTypes(Writer
, CVTypes
.IDTable
.records(),
769 CVTypes
.TypeTable
.records());