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"
22 #include "ObjDumper.h"
23 #include "WindowsResourceDumper.h"
24 #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
25 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
26 #include "llvm/Object/Archive.h"
27 #include "llvm/Object/COFFImportFile.h"
28 #include "llvm/Object/ELFObjectFile.h"
29 #include "llvm/Object/MachOUniversal.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Object/Wasm.h"
32 #include "llvm/Object/WindowsResource.h"
33 #include "llvm/Object/XCOFFObjectFile.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/CommandLine.h"
36 #include "llvm/Support/DataTypes.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/Errc.h"
39 #include "llvm/Support/FileSystem.h"
40 #include "llvm/Support/FormatVariadic.h"
41 #include "llvm/Support/InitLLVM.h"
42 #include "llvm/Support/Path.h"
43 #include "llvm/Support/ScopedPrinter.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/WithColor.h"
48 using namespace llvm::object
;
51 cl::list
<std::string
> InputFilenames(cl::Positional
,
52 cl::desc("<input object files>"),
58 cl::desc("Equivalent to setting: --file-headers, --program-headers, "
59 "--section-headers, --symbols, --relocations, "
60 "--dynamic-table, --notes, --version-info, --unwind, "
61 "--section-groups and --elf-hash-histogram."));
62 cl::alias
AllShort("a", cl::desc("Alias for --all"), cl::aliasopt(All
));
64 // --dependent-libraries
66 DependentLibraries("dependent-libraries",
67 cl::desc("Display the dependent libraries section"));
72 cl::desc("Equivalent to setting: --file-headers, --program-headers, "
73 "--section-headers"));
74 cl::alias
HeadersShort("e", cl::desc("Alias for --headers"),
75 cl::aliasopt(Headers
));
79 WideOutput("wide", cl::desc("Ignored for compatibility with GNU readelf"),
81 cl::alias
WideOutputShort("W",
82 cl::desc("Alias for --wide"),
83 cl::aliasopt(WideOutput
));
85 // --file-headers, --file-header, -h
86 cl::opt
<bool> FileHeaders("file-headers",
87 cl::desc("Display file headers "));
88 cl::alias
FileHeadersShort("h", cl::desc("Alias for --file-headers"),
89 cl::aliasopt(FileHeaders
), cl::NotHidden
);
90 cl::alias
FileHeadersSingular("file-header",
91 cl::desc("Alias for --file-headers"),
92 cl::aliasopt(FileHeaders
));
94 // --section-headers, --sections, -S
95 // Also -s in llvm-readobj mode.
96 cl::opt
<bool> SectionHeaders("section-headers",
97 cl::desc("Display all section headers."));
98 cl::alias
SectionsShortUpper("S", cl::desc("Alias for --section-headers"),
99 cl::aliasopt(SectionHeaders
), cl::NotHidden
);
100 cl::alias
SectionHeadersAlias("sections",
101 cl::desc("Alias for --section-headers"),
102 cl::aliasopt(SectionHeaders
), cl::NotHidden
);
104 // --section-relocations
105 // Also --sr in llvm-readobj mode.
106 cl::opt
<bool> SectionRelocations("section-relocations",
107 cl::desc("Display relocations for each section shown."));
110 // Also --st in llvm-readobj mode.
111 cl::opt
<bool> SectionSymbols("section-symbols",
112 cl::desc("Display symbols for each section shown."));
115 // Also --sd in llvm-readobj mode.
116 cl::opt
<bool> SectionData("section-data",
117 cl::desc("Display section data for each section shown."));
120 cl::opt
<cl::boolOrDefault
>
121 SectionMapping("section-mapping",
122 cl::desc("Display the section to segment mapping."));
124 // --relocations, --relocs, -r
125 cl::opt
<bool> Relocations("relocations",
126 cl::desc("Display the relocation entries in the file"));
127 cl::alias
RelocationsShort("r", cl::desc("Alias for --relocations"),
128 cl::aliasopt(Relocations
), cl::NotHidden
);
129 cl::alias
RelocationsGNU("relocs", cl::desc("Alias for --relocations"),
130 cl::aliasopt(Relocations
));
133 cl::opt
<bool> Notes("notes", cl::desc("Display the ELF notes in the file"));
134 cl::alias
NotesShort("n", cl::desc("Alias for --notes"), cl::aliasopt(Notes
));
137 cl::opt
<bool> DynRelocs("dyn-relocations",
138 cl::desc("Display the dynamic relocation entries in the file"));
141 // Also -t in llvm-readelf mode.
142 cl::opt
<bool> SectionDetails("section-details",
143 cl::desc("Display the section details"));
146 // Also -s in llvm-readelf mode, or -t in llvm-readobj mode.
149 cl::desc("Display the symbol table. Also display the dynamic "
150 "symbol table when using GNU output style for ELF"));
151 cl::alias
SymbolsGNU("syms", cl::desc("Alias for --symbols"),
152 cl::aliasopt(Symbols
));
154 // --dyn-symbols, --dyn-syms
155 // Also --dt in llvm-readobj mode.
156 cl::opt
<bool> DynamicSymbols("dyn-symbols",
157 cl::desc("Display the dynamic symbol table"));
158 cl::alias
DynSymsGNU("dyn-syms", cl::desc("Alias for --dyn-symbols"),
159 cl::aliasopt(DynamicSymbols
));
162 cl::opt
<bool> HashSymbols(
164 cl::desc("Display the dynamic symbols derived from the hash section"));
167 cl::opt
<bool> UnwindInfo("unwind",
168 cl::desc("Display unwind information"));
169 cl::alias
UnwindInfoShort("u",
170 cl::desc("Alias for --unwind"),
171 cl::aliasopt(UnwindInfo
));
173 // --dynamic-table, --dynamic, -d
174 cl::opt
<bool> DynamicTable("dynamic-table",
175 cl::desc("Display the ELF .dynamic section table"));
176 cl::alias
DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
177 cl::aliasopt(DynamicTable
), cl::NotHidden
);
178 cl::alias
DynamicTableAlias("dynamic", cl::desc("Alias for --dynamic-table"),
179 cl::aliasopt(DynamicTable
));
182 cl::opt
<bool> NeededLibraries("needed-libs",
183 cl::desc("Display the needed libraries"));
185 // --program-headers, --segments, -l
186 cl::opt
<bool> ProgramHeaders("program-headers",
187 cl::desc("Display ELF program headers"));
188 cl::alias
ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
189 cl::aliasopt(ProgramHeaders
), cl::NotHidden
);
190 cl::alias
SegmentsAlias("segments", cl::desc("Alias for --program-headers"),
191 cl::aliasopt(ProgramHeaders
));
194 cl::list
<std::string
> StringDump(
195 "string-dump", cl::value_desc("number|name"),
196 cl::desc("Display the specified section(s) as a list of strings"),
198 cl::alias
StringDumpShort("p", cl::desc("Alias for --string-dump"),
199 cl::aliasopt(StringDump
), cl::Prefix
);
202 cl::list
<std::string
>
203 HexDump("hex-dump", cl::value_desc("number|name"),
204 cl::desc("Display the specified section(s) as hexadecimal bytes"),
206 cl::alias
HexDumpShort("x", cl::desc("Alias for --hex-dump"),
207 cl::aliasopt(HexDump
), cl::Prefix
);
210 cl::opt
<bool> Demangle("demangle",
211 cl::desc("Demangle symbol names in output"));
212 cl::alias
DemangleShort("C", cl::desc("Alias for --demangle"),
213 cl::aliasopt(Demangle
), cl::NotHidden
);
216 cl::opt
<bool> HashTable("hash-table",
217 cl::desc("Display ELF hash table"));
220 cl::opt
<bool> GnuHashTable("gnu-hash-table",
221 cl::desc("Display ELF .gnu.hash section"));
224 cl::opt
<bool> ExpandRelocs("expand-relocs",
225 cl::desc("Expand each shown relocation to multiple lines"));
228 cl::opt
<bool> RawRelr("raw-relr",
229 cl::desc("Do not decode relocations in SHT_RELR section, display raw contents"));
232 cl::opt
<bool> CodeView("codeview",
233 cl::desc("Display CodeView debug information"));
235 // --codeview-merged-types
237 CodeViewMergedTypes("codeview-merged-types",
238 cl::desc("Display the merged CodeView type stream"));
241 cl::opt
<bool> CodeViewEnableGHash(
244 "Enable global hashing for CodeView type stream de-duplication"));
246 // --codeview-subsection-bytes
247 cl::opt
<bool> CodeViewSubsectionBytes(
248 "codeview-subsection-bytes",
249 cl::desc("Dump raw contents of codeview debug sections and records"));
252 cl::opt
<bool> ArchSpecificInfo("arch-specific",
253 cl::desc("Displays architecture-specific information, if there is any."));
254 cl::alias
ArchSpecifcInfoShort("A", cl::desc("Alias for --arch-specific"),
255 cl::aliasopt(ArchSpecificInfo
), cl::NotHidden
);
259 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
263 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
267 COFFDirectives("coff-directives",
268 cl::desc("Display the PE/COFF .drectve section"));
272 COFFBaseRelocs("coff-basereloc",
273 cl::desc("Display the PE/COFF .reloc section"));
275 // --coff-debug-directory
277 COFFDebugDirectory("coff-debug-directory",
278 cl::desc("Display the PE/COFF debug directory"));
280 // --coff-tls-directory
281 cl::opt
<bool> COFFTLSDirectory("coff-tls-directory",
282 cl::desc("Display the PE/COFF TLS directory"));
285 cl::opt
<bool> COFFResources("coff-resources",
286 cl::desc("Display the PE/COFF .rsrc section"));
288 // --coff-load-config
290 COFFLoadConfig("coff-load-config",
291 cl::desc("Display the PE/COFF load config"));
293 // --elf-linker-options
295 ELFLinkerOptions("elf-linker-options",
296 cl::desc("Display the ELF .linker-options section"));
298 // --macho-data-in-code
300 MachODataInCode("macho-data-in-code",
301 cl::desc("Display MachO Data in Code command"));
303 // --macho-indirect-symbols
305 MachOIndirectSymbols("macho-indirect-symbols",
306 cl::desc("Display MachO indirect symbols"));
308 // --macho-linker-options
310 MachOLinkerOptions("macho-linker-options",
311 cl::desc("Display MachO linker options"));
315 MachOSegment("macho-segment",
316 cl::desc("Display MachO Segment command"));
318 // --macho-version-min
320 MachOVersionMin("macho-version-min",
321 cl::desc("Display MachO version min command"));
325 MachODysymtab("macho-dysymtab",
326 cl::desc("Display MachO Dysymtab command"));
330 PrintStackMap("stackmap",
331 cl::desc("Display contents of stackmap section"));
335 PrintStackSizes("stack-sizes",
336 cl::desc("Display contents of all stack sizes sections"));
338 // --version-info, -V
340 VersionInfo("version-info",
341 cl::desc("Display ELF version sections (if present)"));
342 cl::alias
VersionInfoShort("V", cl::desc("Alias for -version-info"),
343 cl::aliasopt(VersionInfo
));
345 // --elf-section-groups, --section-groups, -g
346 cl::opt
<bool> SectionGroups("elf-section-groups",
347 cl::desc("Display ELF section group contents"));
348 cl::alias
SectionGroupsAlias("section-groups",
349 cl::desc("Alias for -elf-sections-groups"),
350 cl::aliasopt(SectionGroups
));
351 cl::alias
SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
352 cl::aliasopt(SectionGroups
));
354 // --elf-hash-histogram, --histogram, -I
355 cl::opt
<bool> HashHistogram(
356 "elf-hash-histogram",
357 cl::desc("Display bucket list histogram for hash sections"));
358 cl::alias
HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
359 cl::aliasopt(HashHistogram
));
360 cl::alias
HistogramAlias("histogram",
361 cl::desc("Alias for --elf-hash-histogram"),
362 cl::aliasopt(HashHistogram
));
365 cl::opt
<bool> CGProfile("cg-profile",
366 cl::desc("Display callgraph profile section"));
367 cl::alias
ELFCGProfile("elf-cg-profile", cl::desc("Alias for --cg-profile"),
368 cl::aliasopt(CGProfile
));
371 cl::opt
<bool> Addrsig("addrsig",
372 cl::desc("Display address-significance table"));
375 cl::opt
<OutputStyleTy
>
376 Output("elf-output-style", cl::desc("Specify ELF dump style"),
377 cl::values(clEnumVal(LLVM
, "LLVM default style"),
378 clEnumVal(GNU
, "GNU readelf style")),
382 HelpResponse("\nPass @FILE as argument to read options from FILE.\n");
385 static StringRef ToolName
;
389 LLVM_ATTRIBUTE_NORETURN
static void error(Twine Msg
) {
390 // Flush the standard output to print the error at a
393 WithColor::error(errs(), ToolName
) << Msg
<< "\n";
397 LLVM_ATTRIBUTE_NORETURN
void reportError(Error Err
, StringRef Input
) {
401 handleAllErrors(createFileError(Input
, std::move(Err
)),
402 [&](const ErrorInfoBase
&EI
) { error(EI
.message()); });
403 llvm_unreachable("error() call should never return");
406 void reportWarning(Error Err
, StringRef Input
) {
411 // Flush the standard output to print the warning at a
415 createFileError(Input
, std::move(Err
)), [&](const ErrorInfoBase
&EI
) {
416 WithColor::warning(errs(), ToolName
) << EI
.message() << "\n";
423 struct ReadObjTypeTableBuilder
{
424 ReadObjTypeTableBuilder()
425 : Allocator(), IDTable(Allocator
), TypeTable(Allocator
),
426 GlobalIDTable(Allocator
), GlobalTypeTable(Allocator
) {}
428 llvm::BumpPtrAllocator Allocator
;
429 llvm::codeview::MergingTypeTableBuilder IDTable
;
430 llvm::codeview::MergingTypeTableBuilder TypeTable
;
431 llvm::codeview::GlobalTypeTableBuilder GlobalIDTable
;
432 llvm::codeview::GlobalTypeTableBuilder GlobalTypeTable
;
433 std::vector
<OwningBinary
<Binary
>> Binaries
;
436 static ReadObjTypeTableBuilder CVTypes
;
438 /// Creates an format-specific object file dumper.
439 static Expected
<std::unique_ptr
<ObjDumper
>>
440 createDumper(const ObjectFile
&Obj
, ScopedPrinter
&Writer
) {
441 if (const COFFObjectFile
*COFFObj
= dyn_cast
<COFFObjectFile
>(&Obj
))
442 return createCOFFDumper(*COFFObj
, Writer
);
444 if (const ELFObjectFileBase
*ELFObj
= dyn_cast
<ELFObjectFileBase
>(&Obj
))
445 return createELFDumper(*ELFObj
, Writer
);
447 if (const MachOObjectFile
*MachOObj
= dyn_cast
<MachOObjectFile
>(&Obj
))
448 return createMachODumper(*MachOObj
, Writer
);
450 if (const WasmObjectFile
*WasmObj
= dyn_cast
<WasmObjectFile
>(&Obj
))
451 return createWasmDumper(*WasmObj
, Writer
);
453 if (const XCOFFObjectFile
*XObj
= dyn_cast
<XCOFFObjectFile
>(&Obj
))
454 return createXCOFFDumper(*XObj
, Writer
);
456 return createStringError(errc::invalid_argument
,
457 "unsupported object file format");
460 /// Dumps the specified object file.
461 static void dumpObject(ObjectFile
&Obj
, ScopedPrinter
&Writer
,
462 const Archive
*A
= nullptr) {
463 std::string FileStr
=
464 A
? Twine(A
->getFileName() + "(" + Obj
.getFileName() + ")").str()
465 : Obj
.getFileName().str();
467 std::string ContentErrString
;
468 if (Error ContentErr
= Obj
.initContent())
469 ContentErrString
= "unable to continue dumping, the file is corrupt: " +
470 toString(std::move(ContentErr
));
473 Expected
<std::unique_ptr
<ObjDumper
>> DumperOrErr
= createDumper(Obj
, Writer
);
475 reportError(DumperOrErr
.takeError(), FileStr
);
476 Dumper
= (*DumperOrErr
).get();
478 if (opts::Output
== opts::LLVM
|| opts::InputFilenames
.size() > 1 || A
) {
479 Writer
.startLine() << "\n";
480 Writer
.printString("File", FileStr
);
482 if (opts::Output
== opts::LLVM
) {
483 Writer
.printString("Format", Obj
.getFileFormatName());
484 Writer
.printString("Arch", Triple::getArchTypeName(Obj
.getArch()));
487 std::string(formatv("{0}bit", 8 * Obj
.getBytesInAddress())));
488 Dumper
->printLoadName();
491 if (opts::FileHeaders
)
492 Dumper
->printFileHeaders();
494 // This is only used for ELF currently. In some cases, when an object is
495 // corrupt (e.g. truncated), we can't dump anything except the file header.
496 if (!ContentErrString
.empty())
497 reportError(createError(ContentErrString
), FileStr
);
499 if (opts::SectionDetails
|| opts::SectionHeaders
) {
500 if (opts::Output
== opts::GNU
&& opts::SectionDetails
)
501 Dumper
->printSectionDetails();
503 Dumper
->printSectionHeaders();
506 if (opts::HashSymbols
)
507 Dumper
->printHashSymbols();
508 if (opts::ProgramHeaders
|| opts::SectionMapping
== cl::BOU_TRUE
)
509 Dumper
->printProgramHeaders(opts::ProgramHeaders
, opts::SectionMapping
);
510 if (opts::DynamicTable
)
511 Dumper
->printDynamicTable();
512 if (opts::NeededLibraries
)
513 Dumper
->printNeededLibraries();
514 if (opts::Relocations
)
515 Dumper
->printRelocations();
517 Dumper
->printDynamicRelocations();
518 if (opts::UnwindInfo
)
519 Dumper
->printUnwindInfo();
520 if (opts::Symbols
|| opts::DynamicSymbols
)
521 Dumper
->printSymbols(opts::Symbols
, opts::DynamicSymbols
);
522 if (!opts::StringDump
.empty())
523 Dumper
->printSectionsAsString(Obj
, opts::StringDump
);
524 if (!opts::HexDump
.empty())
525 Dumper
->printSectionsAsHex(Obj
, opts::HexDump
);
527 Dumper
->printHashTable();
528 if (opts::GnuHashTable
)
529 Dumper
->printGnuHashTable();
530 if (opts::VersionInfo
)
531 Dumper
->printVersionInfo();
533 if (opts::DependentLibraries
)
534 Dumper
->printDependentLibs();
535 if (opts::ELFLinkerOptions
)
536 Dumper
->printELFLinkerOptions();
537 if (opts::ArchSpecificInfo
)
538 Dumper
->printArchSpecificInfo();
539 if (opts::SectionGroups
)
540 Dumper
->printGroupSections();
541 if (opts::HashHistogram
)
542 Dumper
->printHashHistograms();
544 Dumper
->printCGProfile();
546 Dumper
->printAddrsig();
548 Dumper
->printNotes();
551 if (opts::COFFImports
)
552 Dumper
->printCOFFImports();
553 if (opts::COFFExports
)
554 Dumper
->printCOFFExports();
555 if (opts::COFFDirectives
)
556 Dumper
->printCOFFDirectives();
557 if (opts::COFFBaseRelocs
)
558 Dumper
->printCOFFBaseReloc();
559 if (opts::COFFDebugDirectory
)
560 Dumper
->printCOFFDebugDirectory();
561 if (opts::COFFTLSDirectory
)
562 Dumper
->printCOFFTLSDirectory();
563 if (opts::COFFResources
)
564 Dumper
->printCOFFResources();
565 if (opts::COFFLoadConfig
)
566 Dumper
->printCOFFLoadConfig();
568 Dumper
->printCGProfile();
570 Dumper
->printAddrsig();
572 Dumper
->printCodeViewDebugInfo();
573 if (opts::CodeViewMergedTypes
)
574 Dumper
->mergeCodeViewTypes(CVTypes
.IDTable
, CVTypes
.TypeTable
,
575 CVTypes
.GlobalIDTable
, CVTypes
.GlobalTypeTable
,
576 opts::CodeViewEnableGHash
);
579 if (opts::MachODataInCode
)
580 Dumper
->printMachODataInCode();
581 if (opts::MachOIndirectSymbols
)
582 Dumper
->printMachOIndirectSymbols();
583 if (opts::MachOLinkerOptions
)
584 Dumper
->printMachOLinkerOptions();
585 if (opts::MachOSegment
)
586 Dumper
->printMachOSegment();
587 if (opts::MachOVersionMin
)
588 Dumper
->printMachOVersionMin();
589 if (opts::MachODysymtab
)
590 Dumper
->printMachODysymtab();
592 if (opts::PrintStackMap
)
593 Dumper
->printStackMap();
594 if (opts::PrintStackSizes
)
595 Dumper
->printStackSizes();
598 /// Dumps each object file in \a Arc;
599 static void dumpArchive(const Archive
*Arc
, ScopedPrinter
&Writer
) {
600 Error Err
= Error::success();
601 for (auto &Child
: Arc
->children(Err
)) {
602 Expected
<std::unique_ptr
<Binary
>> ChildOrErr
= Child
.getAsBinary();
604 if (auto E
= isNotObjectErrorInvalidFileType(ChildOrErr
.takeError()))
605 reportError(std::move(E
), Arc
->getFileName());
609 Binary
*Bin
= ChildOrErr
->get();
610 if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(Bin
))
611 dumpObject(*Obj
, Writer
, Arc
);
612 else if (COFFImportFile
*Imp
= dyn_cast
<COFFImportFile
>(Bin
))
613 dumpCOFFImportFile(Imp
, Writer
);
615 reportWarning(createStringError(errc::invalid_argument
,
617 " has an unsupported file type"),
621 reportError(std::move(Err
), Arc
->getFileName());
624 /// Dumps each object file in \a MachO Universal Binary;
625 static void dumpMachOUniversalBinary(const MachOUniversalBinary
*UBinary
,
626 ScopedPrinter
&Writer
) {
627 for (const MachOUniversalBinary::ObjectForArch
&Obj
: UBinary
->objects()) {
628 Expected
<std::unique_ptr
<MachOObjectFile
>> ObjOrErr
= Obj
.getAsObjectFile();
630 dumpObject(*ObjOrErr
.get(), Writer
);
631 else if (auto E
= isNotObjectErrorInvalidFileType(ObjOrErr
.takeError()))
632 reportError(ObjOrErr
.takeError(), UBinary
->getFileName());
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(std::move(Err
), WinRes
->getFileName());
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
=
651 createBinary(File
, /*Context=*/nullptr, /*InitContent=*/false);
653 reportError(BinaryOrErr
.takeError(), File
);
654 Binary
&Binary
= *BinaryOrErr
.get().getBinary();
656 if (Archive
*Arc
= dyn_cast
<Archive
>(&Binary
))
657 dumpArchive(Arc
, Writer
);
658 else if (MachOUniversalBinary
*UBinary
=
659 dyn_cast
<MachOUniversalBinary
>(&Binary
))
660 dumpMachOUniversalBinary(UBinary
, Writer
);
661 else if (ObjectFile
*Obj
= dyn_cast
<ObjectFile
>(&Binary
))
662 dumpObject(*Obj
, Writer
);
663 else if (COFFImportFile
*Import
= dyn_cast
<COFFImportFile
>(&Binary
))
664 dumpCOFFImportFile(Import
, Writer
);
665 else if (WindowsResource
*WinRes
= dyn_cast
<WindowsResource
>(&Binary
))
666 dumpWindowsResourceFile(WinRes
, Writer
);
668 llvm_unreachable("unrecognized file type");
670 CVTypes
.Binaries
.push_back(std::move(*BinaryOrErr
));
673 /// Registers aliases that should only be allowed by readobj.
674 static void registerReadobjAliases() {
675 // -s has meant --sections for a very long time in llvm-readobj despite
676 // meaning --symbols in readelf.
677 static cl::alias
SectionsShort("s", cl::desc("Alias for --section-headers"),
678 cl::aliasopt(opts::SectionHeaders
),
681 // llvm-readelf reserves it for --section-details.
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 // -t is here because for readobj it is an alias for --symbols.
708 static cl::alias
SectionDetailsShort(
709 "t", cl::desc("Alias for --section-details"),
710 cl::aliasopt(opts::SectionDetails
), cl::NotHidden
);
712 // Allow all single letter flags to be grouped together.
713 for (auto &OptEntry
: cl::getRegisteredOptions()) {
714 StringRef ArgName
= OptEntry
.getKey();
715 cl::Option
*Option
= OptEntry
.getValue();
716 if (ArgName
.size() == 1)
717 apply(Option
, cl::Grouping
);
721 int main(int argc
, const char *argv
[]) {
722 InitLLVM
X(argc
, argv
);
725 // Register the target printer for --version.
726 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion
);
728 if (sys::path::stem(argv
[0]).contains("readelf")) {
729 opts::Output
= opts::GNU
;
730 registerReadelfAliases();
732 registerReadobjAliases();
735 cl::ParseCommandLineOptions(argc
, argv
, "LLVM Object Reader\n");
737 // Default to print error if no filename is specified.
738 if (opts::InputFilenames
.empty()) {
739 error("no input files specified");
743 opts::FileHeaders
= true;
744 opts::ProgramHeaders
= true;
745 opts::SectionHeaders
= true;
746 opts::Symbols
= true;
747 opts::Relocations
= true;
748 opts::DynamicTable
= true;
750 opts::VersionInfo
= true;
751 opts::UnwindInfo
= true;
752 opts::SectionGroups
= true;
753 opts::HashHistogram
= true;
754 if (opts::Output
== opts::LLVM
) {
755 opts::Addrsig
= true;
756 opts::PrintStackSizes
= true;
761 opts::FileHeaders
= true;
762 opts::ProgramHeaders
= true;
763 opts::SectionHeaders
= true;
766 ScopedPrinter
Writer(fouts());
767 for (const std::string
&I
: opts::InputFilenames
)
768 dumpInput(I
, Writer
);
770 if (opts::CodeViewMergedTypes
) {
771 if (opts::CodeViewEnableGHash
)
772 dumpCodeViewMergedTypes(Writer
, CVTypes
.GlobalIDTable
.records(),
773 CVTypes
.GlobalTypeTable
.records());
775 dumpCodeViewMergedTypes(Writer
, CVTypes
.IDTable
.records(),
776 CVTypes
.TypeTable
.records());