1//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
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
11// for FileCheck.
12//
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.
16//
17// Output should be specialized for each format where appropriate.
18//
19//===----------------------------------------------------------------------===//
20
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/MC/TargetRegistry.h"
27#include "llvm/Object/Archive.h"
28#include "llvm/Object/COFFImportFile.h"
29#include "llvm/Object/ELFObjectFile.h"
30#include "llvm/Object/MachOUniversal.h"
31#include "llvm/Object/ObjectFile.h"
32#include "llvm/Object/Wasm.h"
33#include "llvm/Object/WindowsResource.h"
34#include "llvm/Object/XCOFFObjectFile.h"
35#include "llvm/Option/Arg.h"
36#include "llvm/Option/ArgList.h"
37#include "llvm/Option/Option.h"
38#include "llvm/Support/Casting.h"
39#include "llvm/Support/CommandLine.h"
40#include "llvm/Support/DataTypes.h"
41#include "llvm/Support/Debug.h"
42#include "llvm/Support/Errc.h"
43#include "llvm/Support/FileSystem.h"
44#include "llvm/Support/FormatVariadic.h"
45#include "llvm/Support/LLVMDriver.h"
46#include "llvm/Support/Path.h"
47#include "llvm/Support/ScopedPrinter.h"
48#include "llvm/Support/WithColor.h"
49
50using namespace llvm;
51using namespace llvm::object;
52
53namespace {
54using namespace llvm::opt; // for HelpHidden in Opts.inc
55enum ID {
56 OPT_INVALID = 0, // This is not an option ID.
57#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
58#include "Opts.inc"
59#undef OPTION
60};
61
62#define OPTTABLE_STR_TABLE_CODE
63#include "Opts.inc"
64#undef OPTTABLE_STR_TABLE_CODE
65
66#define OPTTABLE_PREFIXES_TABLE_CODE
67#include "Opts.inc"
68#undef OPTTABLE_PREFIXES_TABLE_CODE
69
70static constexpr opt::OptTable::Info InfoTable[] = {
71#define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
72#include "Opts.inc"
73#undef OPTION
74};
75
76class ReadobjOptTable : public opt::GenericOptTable {
77public:
78 ReadobjOptTable()
79 : opt::GenericOptTable(OptionStrTable, OptionPrefixesTable, InfoTable) {
80 setGroupedShortOptions(true);
81 }
82};
83
84enum OutputFormatTy { bsd, sysv, posix, darwin, just_symbols };
85
86enum SortSymbolKeyTy {
87 NAME = 0,
88 TYPE = 1,
89 UNKNOWN = 100,
90 // TODO: add ADDRESS, SIZE as needed.
91};
92
93} // namespace
94
95namespace opts {
96static bool Addrsig;
97static bool All;
98static bool ArchSpecificInfo;
99static bool BBAddrMap;
100static bool PrettyPGOAnalysisMap;
101bool ExpandRelocs;
102static bool CallGraphInfo;
103static bool CGProfile;
104static bool Decompress;
105bool Demangle;
106static bool DependentLibraries;
107static bool DynRelocs;
108static bool DynamicSymbols;
109static bool ExtraSymInfo;
110static bool FileHeaders;
111static bool Headers;
112static std::vector<std::string> HexDump;
113static bool PrettyPrint;
114static bool PrintStackMap;
115static bool PrintStackSizes;
116static bool Relocations;
117bool SectionData;
118static bool SectionDetails;
119static bool SectionHeaders;
120bool SectionRelocations;
121bool SectionSymbols;
122static std::vector<std::string> StringDump;
123static bool StringTable;
124static bool Symbols;
125static bool UnwindInfo;
126static cl::boolOrDefault SectionMapping;
127static SmallVector<SortSymbolKeyTy> SortKeys;
128
129// ELF specific options.
130static bool DynamicTable;
131static bool ELFLinkerOptions;
132static bool GnuHashTable;
133static bool HashSymbols;
134static bool HashTable;
135static bool HashHistogram;
136static bool Memtag;
137static bool NeededLibraries;
138static bool Notes;
139static bool Offloading;
140static bool ProgramHeaders;
141static bool SectionGroups;
142static std::vector<std::string> SFrame;
143static bool VersionInfo;
144
145// Mach-O specific options.
146static bool MachODataInCode;
147static bool MachODysymtab;
148static bool MachOIndirectSymbols;
149static bool MachOLinkerOptions;
150static bool MachOSegment;
151static bool MachOVersionMin;
152
153// PE/COFF specific options.
154static bool CodeView;
155static bool CodeViewEnableGHash;
156static bool CodeViewMergedTypes;
157bool CodeViewSubsectionBytes;
158static bool COFFBaseRelocs;
159static bool COFFPseudoRelocs;
160static bool COFFDebugDirectory;
161static bool COFFDirectives;
162static bool COFFExports;
163static bool COFFImports;
164static bool COFFLoadConfig;
165static bool COFFResources;
166static bool COFFTLSDirectory;
167
168// XCOFF specific options.
169static bool XCOFFAuxiliaryHeader;
170static bool XCOFFLoaderSectionHeader;
171static bool XCOFFLoaderSectionSymbol;
172static bool XCOFFLoaderSectionRelocation;
173static bool XCOFFExceptionSection;
174
175OutputStyleTy Output = OutputStyleTy::LLVM;
176static std::vector<std::string> InputFilenames;
177} // namespace opts
178
179static StringRef ToolName;
180
181namespace llvm {
182
183[[noreturn]] static void error(Twine Msg) {
184 // Flush the standard output to print the error at a
185 // proper place.
186 fouts().flush();
187 WithColor::error(OS&: errs(), Prefix: ToolName) << Msg << "\n";
188 exit(status: 1);
189}
190
191[[noreturn]] void reportError(Error Err, StringRef Input) {
192 assert(Err);
193 if (Input == "-")
194 Input = "<stdin>";
195 handleAllErrors(E: createFileError(F: Input, E: std::move(Err)),
196 Handlers: [&](const ErrorInfoBase &EI) { error(Msg: EI.message()); });
197 llvm_unreachable("error() call should never return");
198}
199
200void reportWarning(Error Err, StringRef Input) {
201 assert(Err);
202 if (Input == "-")
203 Input = "<stdin>";
204
205 // Flush the standard output to print the warning at a
206 // proper place.
207 fouts().flush();
208 handleAllErrors(
209 E: createFileError(F: Input, E: std::move(Err)), Handlers: [&](const ErrorInfoBase &EI) {
210 WithColor::warning(OS&: errs(), Prefix: ToolName) << EI.message() << "\n";
211 });
212}
213
214} // namespace llvm
215
216static void parseOptions(const opt::InputArgList &Args) {
217 opts::Addrsig = Args.hasArg(Ids: OPT_addrsig);
218 opts::All = Args.hasArg(Ids: OPT_all);
219 opts::ArchSpecificInfo = Args.hasArg(Ids: OPT_arch_specific);
220 opts::BBAddrMap = Args.hasArg(Ids: OPT_bb_addr_map);
221 opts::PrettyPGOAnalysisMap = Args.hasArg(Ids: OPT_pretty_pgo_analysis_map);
222 if (opts::PrettyPGOAnalysisMap && !opts::BBAddrMap)
223 WithColor::warning(OS&: errs(), Prefix: ToolName)
224 << "--bb-addr-map must be enabled for --pretty-pgo-analysis-map to "
225 "have an effect\n";
226 opts::CallGraphInfo = Args.hasArg(Ids: OPT_call_graph_info);
227 opts::CGProfile = Args.hasArg(Ids: OPT_cg_profile);
228 opts::Decompress = Args.hasArg(Ids: OPT_decompress);
229 opts::Demangle = Args.hasFlag(Pos: OPT_demangle, Neg: OPT_no_demangle, Default: false);
230 opts::DependentLibraries = Args.hasArg(Ids: OPT_dependent_libraries);
231 opts::DynRelocs = Args.hasArg(Ids: OPT_dyn_relocations);
232 opts::DynamicSymbols = Args.hasArg(Ids: OPT_dyn_syms);
233 opts::ExpandRelocs = Args.hasArg(Ids: OPT_expand_relocs);
234 opts::ExtraSymInfo = Args.hasArg(Ids: OPT_extra_sym_info);
235 opts::FileHeaders = Args.hasArg(Ids: OPT_file_header);
236 opts::Headers = Args.hasArg(Ids: OPT_headers);
237 opts::HexDump = Args.getAllArgValues(Id: OPT_hex_dump_EQ);
238 opts::Relocations = Args.hasArg(Ids: OPT_relocs);
239 opts::SectionData = Args.hasArg(Ids: OPT_section_data);
240 opts::SectionDetails = Args.hasArg(Ids: OPT_section_details);
241 opts::SectionHeaders = Args.hasArg(Ids: OPT_section_headers);
242 opts::SectionRelocations = Args.hasArg(Ids: OPT_section_relocations);
243 opts::SectionSymbols = Args.hasArg(Ids: OPT_section_symbols);
244 if (Args.hasArg(Ids: OPT_section_mapping))
245 opts::SectionMapping = cl::BOU_TRUE;
246 else if (Args.hasArg(Ids: OPT_section_mapping_EQ_false))
247 opts::SectionMapping = cl::BOU_FALSE;
248 else
249 opts::SectionMapping = cl::BOU_UNSET;
250 opts::PrintStackSizes = Args.hasArg(Ids: OPT_stack_sizes);
251 opts::PrintStackMap = Args.hasArg(Ids: OPT_stackmap);
252 opts::StringDump = Args.getAllArgValues(Id: OPT_string_dump_EQ);
253 opts::StringTable = Args.hasArg(Ids: OPT_string_table);
254 opts::Symbols = Args.hasArg(Ids: OPT_symbols);
255 opts::UnwindInfo = Args.hasArg(Ids: OPT_unwind);
256
257 // ELF specific options.
258 opts::DynamicTable = Args.hasArg(Ids: OPT_dynamic_table);
259 opts::ELFLinkerOptions = Args.hasArg(Ids: OPT_elf_linker_options);
260 if (Arg *A = Args.getLastArg(Ids: OPT_elf_output_style_EQ)) {
261 std::string OutputStyleChoice = A->getValue();
262 opts::Output = StringSwitch<opts::OutputStyleTy>(OutputStyleChoice)
263 .Case(S: "LLVM", Value: opts::OutputStyleTy::LLVM)
264 .Case(S: "GNU", Value: opts::OutputStyleTy::GNU)
265 .Case(S: "JSON", Value: opts::OutputStyleTy::JSON)
266 .Default(Value: opts::OutputStyleTy::UNKNOWN);
267 if (opts::Output == opts::OutputStyleTy::UNKNOWN) {
268 error(Msg: "--elf-output-style value should be either 'LLVM', 'GNU', or "
269 "'JSON', but was '" +
270 OutputStyleChoice + "'");
271 }
272 }
273 opts::GnuHashTable = Args.hasArg(Ids: OPT_gnu_hash_table);
274 opts::HashSymbols = Args.hasArg(Ids: OPT_hash_symbols);
275 opts::HashTable = Args.hasArg(Ids: OPT_hash_table);
276 opts::HashHistogram = Args.hasArg(Ids: OPT_histogram);
277 opts::Memtag = Args.hasArg(Ids: OPT_memtag);
278 opts::NeededLibraries = Args.hasArg(Ids: OPT_needed_libs);
279 opts::Notes = Args.hasArg(Ids: OPT_notes);
280 opts::Offloading = Args.hasArg(Ids: OPT_offloading);
281 opts::PrettyPrint = Args.hasArg(Ids: OPT_pretty_print);
282 opts::ProgramHeaders = Args.hasArg(Ids: OPT_program_headers);
283 opts::SectionGroups = Args.hasArg(Ids: OPT_section_groups);
284 opts::SFrame = Args.getAllArgValues(Id: OPT_sframe_EQ);
285 if (Arg *A = Args.getLastArg(Ids: OPT_sort_symbols_EQ)) {
286 for (StringRef KeyStr : llvm::split(Str: A->getValue(), Separator: ",")) {
287 SortSymbolKeyTy KeyType = StringSwitch<SortSymbolKeyTy>(KeyStr)
288 .Case(S: "name", Value: SortSymbolKeyTy::NAME)
289 .Case(S: "type", Value: SortSymbolKeyTy::TYPE)
290 .Default(Value: SortSymbolKeyTy::UNKNOWN);
291 if (KeyType == SortSymbolKeyTy::UNKNOWN)
292 error(Msg: "--sort-symbols value should be 'name' or 'type', but was '" +
293 Twine(KeyStr) + "'");
294 opts::SortKeys.push_back(Elt: KeyType);
295 }
296 }
297 opts::VersionInfo = Args.hasArg(Ids: OPT_version_info);
298
299 // Mach-O specific options.
300 opts::MachODataInCode = Args.hasArg(Ids: OPT_macho_data_in_code);
301 opts::MachODysymtab = Args.hasArg(Ids: OPT_macho_dysymtab);
302 opts::MachOIndirectSymbols = Args.hasArg(Ids: OPT_macho_indirect_symbols);
303 opts::MachOLinkerOptions = Args.hasArg(Ids: OPT_macho_linker_options);
304 opts::MachOSegment = Args.hasArg(Ids: OPT_macho_segment);
305 opts::MachOVersionMin = Args.hasArg(Ids: OPT_macho_version_min);
306
307 // PE/COFF specific options.
308 opts::CodeView = Args.hasArg(Ids: OPT_codeview);
309 opts::CodeViewEnableGHash = Args.hasArg(Ids: OPT_codeview_ghash);
310 opts::CodeViewMergedTypes = Args.hasArg(Ids: OPT_codeview_merged_types);
311 opts::CodeViewSubsectionBytes = Args.hasArg(Ids: OPT_codeview_subsection_bytes);
312 opts::COFFBaseRelocs = Args.hasArg(Ids: OPT_coff_basereloc);
313 opts::COFFPseudoRelocs = Args.hasArg(Ids: OPT_coff_pseudoreloc);
314 opts::COFFDebugDirectory = Args.hasArg(Ids: OPT_coff_debug_directory);
315 opts::COFFDirectives = Args.hasArg(Ids: OPT_coff_directives);
316 opts::COFFExports = Args.hasArg(Ids: OPT_coff_exports);
317 opts::COFFImports = Args.hasArg(Ids: OPT_coff_imports);
318 opts::COFFLoadConfig = Args.hasArg(Ids: OPT_coff_load_config);
319 opts::COFFResources = Args.hasArg(Ids: OPT_coff_resources);
320 opts::COFFTLSDirectory = Args.hasArg(Ids: OPT_coff_tls_directory);
321
322 // XCOFF specific options.
323 opts::XCOFFAuxiliaryHeader = Args.hasArg(Ids: OPT_auxiliary_header);
324 opts::XCOFFLoaderSectionHeader = Args.hasArg(Ids: OPT_loader_section_header);
325 opts::XCOFFLoaderSectionSymbol = Args.hasArg(Ids: OPT_loader_section_symbols);
326 opts::XCOFFLoaderSectionRelocation =
327 Args.hasArg(Ids: OPT_loader_section_relocations);
328 opts::XCOFFExceptionSection = Args.hasArg(Ids: OPT_exception_section);
329
330 opts::InputFilenames = Args.getAllArgValues(Id: OPT_INPUT);
331}
332
333namespace {
334struct ReadObjTypeTableBuilder {
335 ReadObjTypeTableBuilder()
336 : IDTable(Allocator), TypeTable(Allocator), GlobalIDTable(Allocator),
337 GlobalTypeTable(Allocator) {}
338
339 llvm::BumpPtrAllocator Allocator;
340 llvm::codeview::MergingTypeTableBuilder IDTable;
341 llvm::codeview::MergingTypeTableBuilder TypeTable;
342 llvm::codeview::GlobalTypeTableBuilder GlobalIDTable;
343 llvm::codeview::GlobalTypeTableBuilder GlobalTypeTable;
344 std::vector<OwningBinary<Binary>> Binaries;
345};
346} // namespace
347static ReadObjTypeTableBuilder CVTypes;
348
349/// Creates an format-specific object file dumper.
350static Expected<std::unique_ptr<ObjDumper>>
351createDumper(const ObjectFile &Obj, ScopedPrinter &Writer) {
352 if (const COFFObjectFile *COFFObj = dyn_cast<COFFObjectFile>(Val: &Obj))
353 return createCOFFDumper(Obj: *COFFObj, Writer);
354
355 if (const ELFObjectFileBase *ELFObj = dyn_cast<ELFObjectFileBase>(Val: &Obj))
356 return createELFDumper(Obj: *ELFObj, Writer);
357
358 if (const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Val: &Obj))
359 return createMachODumper(Obj: *MachOObj, Writer);
360
361 if (const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(Val: &Obj))
362 return createWasmDumper(Obj: *WasmObj, Writer);
363
364 if (const XCOFFObjectFile *XObj = dyn_cast<XCOFFObjectFile>(Val: &Obj))
365 return createXCOFFDumper(Obj: *XObj, Writer);
366
367 return createStringError(EC: errc::invalid_argument,
368 S: "unsupported object file format");
369}
370
371/// Dumps the specified object file.
372static void dumpObject(ObjectFile &Obj, ScopedPrinter &Writer,
373 const Archive *A = nullptr) {
374 std::string FileStr =
375 A ? Twine(A->getFileName() + "(" + Obj.getFileName() + ")").str()
376 : Obj.getFileName().str();
377
378 std::string ContentErrString;
379 if (Error ContentErr = Obj.initContent())
380 ContentErrString = "unable to continue dumping, the file is corrupt: " +
381 toString(E: std::move(ContentErr));
382
383 ObjDumper *Dumper;
384 std::optional<SymbolComparator> SymComp;
385 Expected<std::unique_ptr<ObjDumper>> DumperOrErr = createDumper(Obj, Writer);
386 if (!DumperOrErr)
387 reportError(Err: DumperOrErr.takeError(), Input: FileStr);
388 Dumper = (*DumperOrErr).get();
389
390 if (!opts::SortKeys.empty()) {
391 if (Dumper->canCompareSymbols()) {
392 SymComp = SymbolComparator();
393 for (SortSymbolKeyTy Key : opts::SortKeys) {
394 switch (Key) {
395 case NAME:
396 SymComp->addPredicate(Pred: [Dumper](SymbolRef LHS, SymbolRef RHS) {
397 return Dumper->compareSymbolsByName(LHS, RHS);
398 });
399 break;
400 case TYPE:
401 SymComp->addPredicate(Pred: [Dumper](SymbolRef LHS, SymbolRef RHS) {
402 return Dumper->compareSymbolsByType(LHS, RHS);
403 });
404 break;
405 case UNKNOWN:
406 llvm_unreachable("Unsupported sort key");
407 }
408 }
409
410 } else {
411 reportWarning(Err: createStringError(
412 EC: errc::invalid_argument,
413 S: "--sort-symbols is not supported yet for this format"),
414 Input: FileStr);
415 }
416 }
417 Dumper->printFileSummary(FileStr, Obj, InputFilenames: opts::InputFilenames, A);
418
419 if (opts::FileHeaders)
420 Dumper->printFileHeaders();
421
422 // Auxiliary header in XOCFF is right after the file header, so print the data
423 // here.
424 if (Obj.isXCOFF() && opts::XCOFFAuxiliaryHeader)
425 Dumper->printAuxiliaryHeader();
426
427 // This is only used for ELF currently. In some cases, when an object is
428 // corrupt (e.g. truncated), we can't dump anything except the file header.
429 if (!ContentErrString.empty())
430 reportError(Err: createError(Err: ContentErrString), Input: FileStr);
431
432 if (opts::SectionDetails || opts::SectionHeaders) {
433 if (opts::Output == opts::GNU && opts::SectionDetails)
434 Dumper->printSectionDetails();
435 else
436 Dumper->printSectionHeaders();
437 }
438
439 if (opts::HashSymbols)
440 Dumper->printHashSymbols();
441 if (opts::ProgramHeaders || opts::SectionMapping == cl::BOU_TRUE)
442 Dumper->printProgramHeaders(PrintProgramHeaders: opts::ProgramHeaders, PrintSectionMapping: opts::SectionMapping);
443 if (opts::DynamicTable)
444 Dumper->printDynamicTable();
445 if (opts::NeededLibraries)
446 Dumper->printNeededLibraries();
447 if (opts::Relocations)
448 Dumper->printRelocations();
449 if (opts::DynRelocs)
450 Dumper->printDynamicRelocations();
451 if (opts::UnwindInfo)
452 Dumper->printUnwindInfo();
453 if (opts::Symbols || opts::DynamicSymbols)
454 Dumper->printSymbols(PrintSymbols: opts::Symbols, PrintDynamicSymbols: opts::DynamicSymbols,
455 ExtraSymInfo: opts::ExtraSymInfo, SymComp);
456 if (!opts::StringDump.empty())
457 Dumper->printSectionsAsString(Obj, Sections: opts::StringDump, Decompress: opts::Decompress);
458 if (!opts::HexDump.empty())
459 Dumper->printSectionsAsHex(Obj, Sections: opts::HexDump, Decompress: opts::Decompress);
460 if (opts::HashTable)
461 Dumper->printHashTable();
462 if (opts::GnuHashTable)
463 Dumper->printGnuHashTable();
464 if (opts::VersionInfo)
465 Dumper->printVersionInfo();
466 if (opts::Offloading)
467 Dumper->printOffloading(Obj);
468 if (opts::StringTable)
469 Dumper->printStringTable();
470 if (Obj.isELF()) {
471 if (opts::DependentLibraries)
472 Dumper->printDependentLibs();
473 if (opts::ELFLinkerOptions)
474 Dumper->printELFLinkerOptions();
475 if (opts::ArchSpecificInfo)
476 Dumper->printArchSpecificInfo();
477 if (opts::SectionGroups)
478 Dumper->printGroupSections();
479 if (opts::HashHistogram)
480 Dumper->printHashHistograms();
481 if (opts::CGProfile)
482 Dumper->printCGProfile();
483 if (opts::CallGraphInfo)
484 Dumper->printCallGraphInfo();
485 if (opts::BBAddrMap)
486 Dumper->printBBAddrMaps(PrettyPGOAnalysis: opts::PrettyPGOAnalysisMap);
487 if (opts::Addrsig)
488 Dumper->printAddrsig();
489 if (opts::Notes)
490 Dumper->printNotes();
491 if (opts::Memtag)
492 Dumper->printMemtag();
493 if (!opts::SFrame.empty())
494 Dumper->printSectionsAsSFrame(Sections: opts::SFrame);
495 }
496 if (Obj.isCOFF()) {
497 if (opts::COFFImports)
498 Dumper->printCOFFImports();
499 if (opts::COFFExports)
500 Dumper->printCOFFExports();
501 if (opts::COFFDirectives)
502 Dumper->printCOFFDirectives();
503 if (opts::COFFBaseRelocs)
504 Dumper->printCOFFBaseReloc();
505 if (opts::COFFPseudoRelocs)
506 Dumper->printCOFFPseudoReloc();
507 if (opts::COFFDebugDirectory)
508 Dumper->printCOFFDebugDirectory();
509 if (opts::COFFTLSDirectory)
510 Dumper->printCOFFTLSDirectory();
511 if (opts::COFFResources)
512 Dumper->printCOFFResources();
513 if (opts::COFFLoadConfig)
514 Dumper->printCOFFLoadConfig();
515 if (opts::CGProfile)
516 Dumper->printCGProfile();
517 if (opts::Addrsig)
518 Dumper->printAddrsig();
519 if (opts::CodeView)
520 Dumper->printCodeViewDebugInfo();
521 if (opts::CodeViewMergedTypes)
522 Dumper->mergeCodeViewTypes(CVIDs&: CVTypes.IDTable, CVTypes&: CVTypes.TypeTable,
523 GlobalCVIDs&: CVTypes.GlobalIDTable, GlobalCVTypes&: CVTypes.GlobalTypeTable,
524 GHash: opts::CodeViewEnableGHash);
525 }
526 if (Obj.isMachO()) {
527 if (opts::MachODataInCode)
528 Dumper->printMachODataInCode();
529 if (opts::MachOIndirectSymbols)
530 Dumper->printMachOIndirectSymbols();
531 if (opts::MachOLinkerOptions)
532 Dumper->printMachOLinkerOptions();
533 if (opts::MachOSegment)
534 Dumper->printMachOSegment();
535 if (opts::MachOVersionMin)
536 Dumper->printMachOVersionMin();
537 if (opts::MachODysymtab)
538 Dumper->printMachODysymtab();
539 if (opts::CGProfile)
540 Dumper->printCGProfile();
541 }
542
543 if (Obj.isXCOFF()) {
544 if (opts::XCOFFLoaderSectionHeader || opts::XCOFFLoaderSectionSymbol ||
545 opts::XCOFFLoaderSectionRelocation)
546 Dumper->printLoaderSection(PrintHeader: opts::XCOFFLoaderSectionHeader,
547 PrintSymbols: opts::XCOFFLoaderSectionSymbol,
548 PrintRelocations: opts::XCOFFLoaderSectionRelocation);
549
550 if (opts::XCOFFExceptionSection)
551 Dumper->printExceptionSection();
552 }
553
554 if (opts::PrintStackMap)
555 Dumper->printStackMap();
556 if (opts::PrintStackSizes)
557 Dumper->printStackSizes();
558}
559
560/// Dumps each object file in \a Arc;
561static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) {
562 Error Err = Error::success();
563 for (auto &Child : Arc->children(Err)) {
564 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
565 if (!ChildOrErr) {
566 if (auto E = isNotObjectErrorInvalidFileType(Err: ChildOrErr.takeError()))
567 reportError(Err: std::move(E), Input: Arc->getFileName());
568 continue;
569 }
570
571 Binary *Bin = ChildOrErr->get();
572 if (ObjectFile *Obj = dyn_cast<ObjectFile>(Val: Bin))
573 dumpObject(Obj&: *Obj, Writer, A: Arc);
574 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(Val: Bin))
575 dumpCOFFImportFile(File: Imp, Writer);
576 else
577 reportWarning(Err: createStringError(EC: errc::invalid_argument,
578 S: Bin->getFileName() +
579 " has an unsupported file type"),
580 Input: Arc->getFileName());
581 }
582 if (Err)
583 reportError(Err: std::move(Err), Input: Arc->getFileName());
584}
585
586/// Dumps each object file in \a MachO Universal Binary;
587static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary,
588 ScopedPrinter &Writer) {
589 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
590 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
591 if (ObjOrErr)
592 dumpObject(Obj&: *ObjOrErr.get(), Writer);
593 else if (auto E = isNotObjectErrorInvalidFileType(Err: ObjOrErr.takeError()))
594 reportError(Err: ObjOrErr.takeError(), Input: UBinary->getFileName());
595 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
596 dumpArchive(Arc: &*AOrErr.get(), Writer);
597 }
598}
599
600/// Dumps \a COFF file;
601static void dumpCOFFObject(COFFObjectFile *Obj, ScopedPrinter &Writer) {
602 dumpObject(Obj&: *Obj, Writer);
603
604 // Dump a hybrid object when available.
605 std::unique_ptr<MemoryBuffer> HybridView = Obj->getHybridObjectView();
606 if (!HybridView)
607 return;
608 Expected<std::unique_ptr<COFFObjectFile>> HybridObjOrErr =
609 COFFObjectFile::create(Object: *HybridView);
610 if (!HybridObjOrErr)
611 reportError(Err: HybridObjOrErr.takeError(), Input: Obj->getFileName().str());
612 DictScope D(Writer, "HybridObject");
613 dumpObject(Obj&: **HybridObjOrErr, Writer);
614}
615
616/// Dumps \a WinRes, Windows Resource (.res) file;
617static void dumpWindowsResourceFile(WindowsResource *WinRes,
618 ScopedPrinter &Printer) {
619 WindowsRes::Dumper Dumper(WinRes, Printer);
620 if (auto Err = Dumper.printData())
621 reportError(Err: std::move(Err), Input: WinRes->getFileName());
622}
623
624
625/// Opens \a File and dumps it.
626static void dumpInput(StringRef File, ScopedPrinter &Writer) {
627 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
628 MemoryBuffer::getFileOrSTDIN(Filename: File, /*IsText=*/false,
629 /*RequiresNullTerminator=*/false);
630 if (std::error_code EC = FileOrErr.getError())
631 return reportError(Err: errorCodeToError(EC), Input: File);
632
633 std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get();
634 file_magic Type = identify_magic(magic: Buffer->getBuffer());
635 if (Type == file_magic::bitcode) {
636 reportWarning(Err: createStringError(EC: errc::invalid_argument,
637 S: "bitcode files are not supported"),
638 Input: File);
639 return;
640 }
641
642 Expected<std::unique_ptr<Binary>> BinaryOrErr = createBinary(
643 Source: Buffer->getMemBufferRef(), /*Context=*/nullptr, /*InitContent=*/false);
644 if (!BinaryOrErr)
645 reportError(Err: BinaryOrErr.takeError(), Input: File);
646
647 std::unique_ptr<Binary> Bin = std::move(*BinaryOrErr);
648 if (Archive *Arc = dyn_cast<Archive>(Val: Bin.get()))
649 dumpArchive(Arc, Writer);
650 else if (MachOUniversalBinary *UBinary =
651 dyn_cast<MachOUniversalBinary>(Val: Bin.get()))
652 dumpMachOUniversalBinary(UBinary, Writer);
653 else if (COFFObjectFile *Obj = dyn_cast<COFFObjectFile>(Val: Bin.get()))
654 dumpCOFFObject(Obj, Writer);
655 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Val: Bin.get()))
656 dumpObject(Obj&: *Obj, Writer);
657 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(Val: Bin.get()))
658 dumpCOFFImportFile(File: Import, Writer);
659 else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(Val: Bin.get()))
660 dumpWindowsResourceFile(WinRes, Printer&: Writer);
661 else
662 llvm_unreachable("unrecognized file type");
663
664 CVTypes.Binaries.push_back(
665 x: OwningBinary<Binary>(std::move(Bin), std::move(Buffer)));
666}
667
668std::unique_ptr<ScopedPrinter> createWriter() {
669 if (opts::Output == opts::JSON)
670 return std::make_unique<JSONScopedPrinter>(
671 args&: fouts(), args: opts::PrettyPrint ? 2 : 0, args: std::make_unique<ListScope>());
672 return std::make_unique<ScopedPrinter>(args&: fouts());
673}
674
675int llvm_readobj_main(int argc, char **argv, const llvm::ToolContext &) {
676 BumpPtrAllocator A;
677 StringSaver Saver(A);
678 ReadobjOptTable Tbl;
679 ToolName = argv[0];
680 opt::InputArgList Args =
681 Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
682 error(Msg);
683 exit(status: 1);
684 });
685 if (Args.hasArg(Ids: OPT_help)) {
686 Tbl.printHelp(
687 OS&: outs(),
688 Usage: (Twine(ToolName) + " [options] <input object files>").str().c_str(),
689 Title: "LLVM Object Reader");
690 // TODO Replace this with OptTable API once it adds extrahelp support.
691 outs() << "\nPass @FILE as argument to read options from FILE.\n";
692 return 0;
693 }
694 if (Args.hasArg(Ids: OPT_version)) {
695 cl::PrintVersionMessage();
696 return 0;
697 }
698
699 if (sys::path::stem(path: argv[0]).contains(Other: "readelf"))
700 opts::Output = opts::GNU;
701 parseOptions(Args);
702
703 // Default to print error if no filename is specified.
704 if (opts::InputFilenames.empty()) {
705 error(Msg: "no input files specified");
706 }
707
708 if (opts::All) {
709 opts::FileHeaders = true;
710 opts::XCOFFAuxiliaryHeader = true;
711 opts::ProgramHeaders = true;
712 opts::SectionHeaders = true;
713 opts::Symbols = true;
714 opts::Relocations = true;
715 opts::DynamicTable = true;
716 opts::Notes = true;
717 opts::VersionInfo = true;
718 opts::Offloading = true;
719 opts::UnwindInfo = true;
720 opts::SectionGroups = true;
721 opts::HashHistogram = true;
722 if (opts::Output == opts::LLVM) {
723 opts::Addrsig = true;
724 opts::PrintStackSizes = true;
725 }
726 opts::Memtag = true;
727 }
728
729 if (opts::Headers) {
730 opts::FileHeaders = true;
731 opts::XCOFFAuxiliaryHeader = true;
732 opts::ProgramHeaders = true;
733 opts::SectionHeaders = true;
734 }
735
736 std::unique_ptr<ScopedPrinter> Writer = createWriter();
737
738 for (const std::string &I : opts::InputFilenames)
739 dumpInput(File: I, Writer&: *Writer);
740
741 if (opts::CodeViewMergedTypes) {
742 if (opts::CodeViewEnableGHash)
743 dumpCodeViewMergedTypes(Writer&: *Writer, IpiRecords: CVTypes.GlobalIDTable.records(),
744 TpiRecords: CVTypes.GlobalTypeTable.records());
745 else
746 dumpCodeViewMergedTypes(Writer&: *Writer, IpiRecords: CVTypes.IDTable.records(),
747 TpiRecords: CVTypes.TypeTable.records());
748 }
749
750 return 0;
751}
752