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/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" 46 47 using namespace llvm; 48 using namespace llvm::object; 49 50 namespace opts { 51 cl::list<std::string> InputFilenames(cl::Positional, 52 cl::desc("<input object files>"), 53 cl::ZeroOrMore); 54 55 // --all, -a 56 cl::opt<bool> 57 All("all", 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)); 63 64 // --dependent-libraries 65 cl::opt<bool> 66 DependentLibraries("dependent-libraries", 67 cl::desc("Display the dependent libraries section")); 68 69 // --headers, -e 70 cl::opt<bool> 71 Headers("headers", 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)); 76 77 // --wide, -W 78 cl::opt<bool> 79 WideOutput("wide", cl::desc("Ignored for compatibility with GNU readelf"), 80 cl::Hidden); 81 cl::alias WideOutputShort("W", 82 cl::desc("Alias for --wide"), 83 cl::aliasopt(WideOutput)); 84 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)); 93 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); 103 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.")); 108 109 // --section-symbols 110 // Also --st in llvm-readobj mode. 111 cl::opt<bool> SectionSymbols("section-symbols", 112 cl::desc("Display symbols for each section shown.")); 113 114 // --section-data 115 // Also --sd in llvm-readobj mode. 116 cl::opt<bool> SectionData("section-data", 117 cl::desc("Display section data for each section shown.")); 118 119 // --section-mapping 120 cl::opt<cl::boolOrDefault> 121 SectionMapping("section-mapping", 122 cl::desc("Display the section to segment mapping.")); 123 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)); 131 132 // --notes, -n 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)); 135 136 // --dyn-relocations 137 cl::opt<bool> DynRelocs("dyn-relocations", 138 cl::desc("Display the dynamic relocation entries in the file")); 139 140 // --section-details 141 // Also -t in llvm-readelf mode. 142 cl::opt<bool> SectionDetails("section-details", 143 cl::desc("Display the section details")); 144 145 // --symbols 146 // Also -s in llvm-readelf mode, or -t in llvm-readobj mode. 147 cl::opt<bool> 148 Symbols("symbols", 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)); 153 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)); 160 161 // --hash-symbols 162 cl::opt<bool> HashSymbols( 163 "hash-symbols", 164 cl::desc("Display the dynamic symbols derived from the hash section")); 165 166 // --unwind, -u 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)); 172 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)); 180 181 // --needed-libs 182 cl::opt<bool> NeededLibraries("needed-libs", 183 cl::desc("Display the needed libraries")); 184 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)); 192 193 // --string-dump, -p 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"), 197 cl::ZeroOrMore); 198 cl::alias StringDumpShort("p", cl::desc("Alias for --string-dump"), 199 cl::aliasopt(StringDump), cl::Prefix); 200 201 // --hex-dump, -x 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"), 205 cl::ZeroOrMore); 206 cl::alias HexDumpShort("x", cl::desc("Alias for --hex-dump"), 207 cl::aliasopt(HexDump), cl::Prefix); 208 209 // --demangle, -C 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); 214 215 // --hash-table 216 cl::opt<bool> HashTable("hash-table", 217 cl::desc("Display ELF hash table")); 218 219 // --gnu-hash-table 220 cl::opt<bool> GnuHashTable("gnu-hash-table", 221 cl::desc("Display ELF .gnu.hash section")); 222 223 // --expand-relocs 224 cl::opt<bool> ExpandRelocs("expand-relocs", 225 cl::desc("Expand each shown relocation to multiple lines")); 226 227 // --raw-relr 228 cl::opt<bool> RawRelr("raw-relr", 229 cl::desc("Do not decode relocations in SHT_RELR section, display raw contents")); 230 231 // --codeview 232 cl::opt<bool> CodeView("codeview", 233 cl::desc("Display CodeView debug information")); 234 235 // --codeview-merged-types 236 cl::opt<bool> 237 CodeViewMergedTypes("codeview-merged-types", 238 cl::desc("Display the merged CodeView type stream")); 239 240 // --codeview-ghash 241 cl::opt<bool> CodeViewEnableGHash( 242 "codeview-ghash", 243 cl::desc( 244 "Enable global hashing for CodeView type stream de-duplication")); 245 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")); 250 251 // --arch-specific 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); 256 257 // --coff-imports 258 cl::opt<bool> 259 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table")); 260 261 // --coff-exports 262 cl::opt<bool> 263 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table")); 264 265 // --coff-directives 266 cl::opt<bool> 267 COFFDirectives("coff-directives", 268 cl::desc("Display the PE/COFF .drectve section")); 269 270 // --coff-basereloc 271 cl::opt<bool> 272 COFFBaseRelocs("coff-basereloc", 273 cl::desc("Display the PE/COFF .reloc section")); 274 275 // --coff-debug-directory 276 cl::opt<bool> 277 COFFDebugDirectory("coff-debug-directory", 278 cl::desc("Display the PE/COFF debug directory")); 279 280 // --coff-tls-directory 281 cl::opt<bool> COFFTLSDirectory("coff-tls-directory", 282 cl::desc("Display the PE/COFF TLS directory")); 283 284 // --coff-resources 285 cl::opt<bool> COFFResources("coff-resources", 286 cl::desc("Display the PE/COFF .rsrc section")); 287 288 // --coff-load-config 289 cl::opt<bool> 290 COFFLoadConfig("coff-load-config", 291 cl::desc("Display the PE/COFF load config")); 292 293 // --elf-linker-options 294 cl::opt<bool> 295 ELFLinkerOptions("elf-linker-options", 296 cl::desc("Display the ELF .linker-options section")); 297 298 // --macho-data-in-code 299 cl::opt<bool> 300 MachODataInCode("macho-data-in-code", 301 cl::desc("Display MachO Data in Code command")); 302 303 // --macho-indirect-symbols 304 cl::opt<bool> 305 MachOIndirectSymbols("macho-indirect-symbols", 306 cl::desc("Display MachO indirect symbols")); 307 308 // --macho-linker-options 309 cl::opt<bool> 310 MachOLinkerOptions("macho-linker-options", 311 cl::desc("Display MachO linker options")); 312 313 // --macho-segment 314 cl::opt<bool> 315 MachOSegment("macho-segment", 316 cl::desc("Display MachO Segment command")); 317 318 // --macho-version-min 319 cl::opt<bool> 320 MachOVersionMin("macho-version-min", 321 cl::desc("Display MachO version min command")); 322 323 // --macho-dysymtab 324 cl::opt<bool> 325 MachODysymtab("macho-dysymtab", 326 cl::desc("Display MachO Dysymtab command")); 327 328 // --stackmap 329 cl::opt<bool> 330 PrintStackMap("stackmap", 331 cl::desc("Display contents of stackmap section")); 332 333 // --stack-sizes 334 cl::opt<bool> 335 PrintStackSizes("stack-sizes", 336 cl::desc("Display contents of all stack sizes sections")); 337 338 // --version-info, -V 339 cl::opt<bool> 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)); 344 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)); 353 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)); 363 364 // --cg-profile 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)); 369 370 // -addrsig 371 cl::opt<bool> Addrsig("addrsig", 372 cl::desc("Display address-significance table")); 373 374 // -elf-output-style 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")), 379 cl::init(LLVM)); 380 381 cl::extrahelp 382 HelpResponse("\nPass @FILE as argument to read options from FILE.\n"); 383 } // namespace opts 384 385 static StringRef ToolName; 386 387 namespace llvm { 388 389 LLVM_ATTRIBUTE_NORETURN static void error(Twine Msg) { 390 // Flush the standard output to print the error at a 391 // proper place. 392 fouts().flush(); 393 WithColor::error(errs(), ToolName) << Msg << "\n"; 394 exit(1); 395 } 396 397 LLVM_ATTRIBUTE_NORETURN void reportError(Error Err, StringRef Input) { 398 assert(Err); 399 if (Input == "-") 400 Input = "<stdin>"; 401 handleAllErrors(createFileError(Input, std::move(Err)), 402 [&](const ErrorInfoBase &EI) { error(EI.message()); }); 403 llvm_unreachable("error() call should never return"); 404 } 405 406 void reportWarning(Error Err, StringRef Input) { 407 assert(Err); 408 if (Input == "-") 409 Input = "<stdin>"; 410 411 // Flush the standard output to print the warning at a 412 // proper place. 413 fouts().flush(); 414 handleAllErrors( 415 createFileError(Input, std::move(Err)), [&](const ErrorInfoBase &EI) { 416 WithColor::warning(errs(), ToolName) << EI.message() << "\n"; 417 }); 418 } 419 420 } // namespace llvm 421 422 namespace { 423 struct ReadObjTypeTableBuilder { 424 ReadObjTypeTableBuilder() 425 : Allocator(), IDTable(Allocator), TypeTable(Allocator), 426 GlobalIDTable(Allocator), GlobalTypeTable(Allocator) {} 427 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; 434 }; 435 } // namespace 436 static ReadObjTypeTableBuilder CVTypes; 437 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); 443 444 if (const ELFObjectFileBase *ELFObj = dyn_cast<ELFObjectFileBase>(&Obj)) 445 return createELFDumper(*ELFObj, Writer); 446 447 if (const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(&Obj)) 448 return createMachODumper(*MachOObj, Writer); 449 450 if (const WasmObjectFile *WasmObj = dyn_cast<WasmObjectFile>(&Obj)) 451 return createWasmDumper(*WasmObj, Writer); 452 453 if (const XCOFFObjectFile *XObj = dyn_cast<XCOFFObjectFile>(&Obj)) 454 return createXCOFFDumper(*XObj, Writer); 455 456 return createStringError(errc::invalid_argument, 457 "unsupported object file format"); 458 } 459 460 /// Dumps the specified object file. 461 static void dumpObject(const 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(); 466 467 ObjDumper *Dumper; 468 Expected<std::unique_ptr<ObjDumper>> DumperOrErr = createDumper(Obj, Writer); 469 if (!DumperOrErr) 470 reportError(DumperOrErr.takeError(), FileStr); 471 Dumper = (*DumperOrErr).get(); 472 473 if (opts::Output == opts::LLVM || opts::InputFilenames.size() > 1 || A) { 474 Writer.startLine() << "\n"; 475 Writer.printString("File", FileStr); 476 } 477 if (opts::Output == opts::LLVM) { 478 Writer.printString("Format", Obj.getFileFormatName()); 479 Writer.printString("Arch", Triple::getArchTypeName(Obj.getArch())); 480 Writer.printString( 481 "AddressSize", 482 std::string(formatv("{0}bit", 8 * Obj.getBytesInAddress()))); 483 Dumper->printLoadName(); 484 } 485 486 if (opts::FileHeaders) 487 Dumper->printFileHeaders(); 488 489 if (opts::SectionDetails || opts::SectionHeaders) { 490 if (opts::Output == opts::GNU && opts::SectionDetails) 491 Dumper->printSectionDetails(); 492 else 493 Dumper->printSectionHeaders(); 494 } 495 496 if (opts::HashSymbols) 497 Dumper->printHashSymbols(); 498 if (opts::ProgramHeaders || opts::SectionMapping == cl::BOU_TRUE) 499 Dumper->printProgramHeaders(opts::ProgramHeaders, opts::SectionMapping); 500 if (opts::DynamicTable) 501 Dumper->printDynamicTable(); 502 if (opts::NeededLibraries) 503 Dumper->printNeededLibraries(); 504 if (opts::Relocations) 505 Dumper->printRelocations(); 506 if (opts::DynRelocs) 507 Dumper->printDynamicRelocations(); 508 if (opts::UnwindInfo) 509 Dumper->printUnwindInfo(); 510 if (opts::Symbols || opts::DynamicSymbols) 511 Dumper->printSymbols(opts::Symbols, opts::DynamicSymbols); 512 if (!opts::StringDump.empty()) 513 Dumper->printSectionsAsString(Obj, opts::StringDump); 514 if (!opts::HexDump.empty()) 515 Dumper->printSectionsAsHex(Obj, opts::HexDump); 516 if (opts::HashTable) 517 Dumper->printHashTable(); 518 if (opts::GnuHashTable) 519 Dumper->printGnuHashTable(); 520 if (opts::VersionInfo) 521 Dumper->printVersionInfo(); 522 if (Obj.isELF()) { 523 if (opts::DependentLibraries) 524 Dumper->printDependentLibs(); 525 if (opts::ELFLinkerOptions) 526 Dumper->printELFLinkerOptions(); 527 if (opts::ArchSpecificInfo) 528 Dumper->printArchSpecificInfo(); 529 if (opts::SectionGroups) 530 Dumper->printGroupSections(); 531 if (opts::HashHistogram) 532 Dumper->printHashHistograms(); 533 if (opts::CGProfile) 534 Dumper->printCGProfile(); 535 if (opts::Addrsig) 536 Dumper->printAddrsig(); 537 if (opts::Notes) 538 Dumper->printNotes(); 539 } 540 if (Obj.isCOFF()) { 541 if (opts::COFFImports) 542 Dumper->printCOFFImports(); 543 if (opts::COFFExports) 544 Dumper->printCOFFExports(); 545 if (opts::COFFDirectives) 546 Dumper->printCOFFDirectives(); 547 if (opts::COFFBaseRelocs) 548 Dumper->printCOFFBaseReloc(); 549 if (opts::COFFDebugDirectory) 550 Dumper->printCOFFDebugDirectory(); 551 if (opts::COFFTLSDirectory) 552 Dumper->printCOFFTLSDirectory(); 553 if (opts::COFFResources) 554 Dumper->printCOFFResources(); 555 if (opts::COFFLoadConfig) 556 Dumper->printCOFFLoadConfig(); 557 if (opts::CGProfile) 558 Dumper->printCGProfile(); 559 if (opts::Addrsig) 560 Dumper->printAddrsig(); 561 if (opts::CodeView) 562 Dumper->printCodeViewDebugInfo(); 563 if (opts::CodeViewMergedTypes) 564 Dumper->mergeCodeViewTypes(CVTypes.IDTable, CVTypes.TypeTable, 565 CVTypes.GlobalIDTable, CVTypes.GlobalTypeTable, 566 opts::CodeViewEnableGHash); 567 } 568 if (Obj.isMachO()) { 569 if (opts::MachODataInCode) 570 Dumper->printMachODataInCode(); 571 if (opts::MachOIndirectSymbols) 572 Dumper->printMachOIndirectSymbols(); 573 if (opts::MachOLinkerOptions) 574 Dumper->printMachOLinkerOptions(); 575 if (opts::MachOSegment) 576 Dumper->printMachOSegment(); 577 if (opts::MachOVersionMin) 578 Dumper->printMachOVersionMin(); 579 if (opts::MachODysymtab) 580 Dumper->printMachODysymtab(); 581 } 582 if (opts::PrintStackMap) 583 Dumper->printStackMap(); 584 if (opts::PrintStackSizes) 585 Dumper->printStackSizes(); 586 } 587 588 /// Dumps each object file in \a Arc; 589 static void dumpArchive(const Archive *Arc, ScopedPrinter &Writer) { 590 Error Err = Error::success(); 591 for (auto &Child : Arc->children(Err)) { 592 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); 593 if (!ChildOrErr) { 594 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) 595 reportError(std::move(E), Arc->getFileName()); 596 continue; 597 } 598 599 Binary *Bin = ChildOrErr->get(); 600 if (ObjectFile *Obj = dyn_cast<ObjectFile>(Bin)) 601 dumpObject(*Obj, Writer, Arc); 602 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(Bin)) 603 dumpCOFFImportFile(Imp, Writer); 604 else 605 reportWarning(createStringError(errc::invalid_argument, 606 Bin->getFileName() + 607 " has an unsupported file type"), 608 Arc->getFileName()); 609 } 610 if (Err) 611 reportError(std::move(Err), Arc->getFileName()); 612 } 613 614 /// Dumps each object file in \a MachO Universal Binary; 615 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary, 616 ScopedPrinter &Writer) { 617 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) { 618 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile(); 619 if (ObjOrErr) 620 dumpObject(*ObjOrErr.get(), Writer); 621 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) 622 reportError(ObjOrErr.takeError(), UBinary->getFileName()); 623 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive()) 624 dumpArchive(&*AOrErr.get(), Writer); 625 } 626 } 627 628 /// Dumps \a WinRes, Windows Resource (.res) file; 629 static void dumpWindowsResourceFile(WindowsResource *WinRes, 630 ScopedPrinter &Printer) { 631 WindowsRes::Dumper Dumper(WinRes, Printer); 632 if (auto Err = Dumper.printData()) 633 reportError(std::move(Err), WinRes->getFileName()); 634 } 635 636 637 /// Opens \a File and dumps it. 638 static void dumpInput(StringRef File, ScopedPrinter &Writer) { 639 // Attempt to open the binary. 640 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File); 641 if (!BinaryOrErr) 642 reportError(BinaryOrErr.takeError(), File); 643 Binary &Binary = *BinaryOrErr.get().getBinary(); 644 645 if (Archive *Arc = dyn_cast<Archive>(&Binary)) 646 dumpArchive(Arc, Writer); 647 else if (MachOUniversalBinary *UBinary = 648 dyn_cast<MachOUniversalBinary>(&Binary)) 649 dumpMachOUniversalBinary(UBinary, Writer); 650 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 651 dumpObject(*Obj, Writer); 652 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary)) 653 dumpCOFFImportFile(Import, Writer); 654 else if (WindowsResource *WinRes = dyn_cast<WindowsResource>(&Binary)) 655 dumpWindowsResourceFile(WinRes, Writer); 656 else 657 llvm_unreachable("unrecognized file type"); 658 659 CVTypes.Binaries.push_back(std::move(*BinaryOrErr)); 660 } 661 662 /// Registers aliases that should only be allowed by readobj. 663 static void registerReadobjAliases() { 664 // -s has meant --sections for a very long time in llvm-readobj despite 665 // meaning --symbols in readelf. 666 static cl::alias SectionsShort("s", cl::desc("Alias for --section-headers"), 667 cl::aliasopt(opts::SectionHeaders), 668 cl::NotHidden); 669 670 // llvm-readelf reserves it for --section-details. 671 static cl::alias SymbolsShort("t", cl::desc("Alias for --symbols"), 672 cl::aliasopt(opts::Symbols), cl::NotHidden); 673 674 // The following two-letter aliases are only provided for readobj, as readelf 675 // allows single-letter args to be grouped together. 676 static cl::alias SectionRelocationsShort( 677 "sr", cl::desc("Alias for --section-relocations"), 678 cl::aliasopt(opts::SectionRelocations)); 679 static cl::alias SectionDataShort("sd", cl::desc("Alias for --section-data"), 680 cl::aliasopt(opts::SectionData)); 681 static cl::alias SectionSymbolsShort("st", 682 cl::desc("Alias for --section-symbols"), 683 cl::aliasopt(opts::SectionSymbols)); 684 static cl::alias DynamicSymbolsShort("dt", 685 cl::desc("Alias for --dyn-symbols"), 686 cl::aliasopt(opts::DynamicSymbols)); 687 } 688 689 /// Registers aliases that should only be allowed by readelf. 690 static void registerReadelfAliases() { 691 // -s is here because for readobj it means --sections. 692 static cl::alias SymbolsShort("s", cl::desc("Alias for --symbols"), 693 cl::aliasopt(opts::Symbols), cl::NotHidden, 694 cl::Grouping); 695 696 // -t is here because for readobj it is an alias for --symbols. 697 static cl::alias SectionDetailsShort( 698 "t", cl::desc("Alias for --section-details"), 699 cl::aliasopt(opts::SectionDetails), cl::NotHidden); 700 701 // Allow all single letter flags to be grouped together. 702 for (auto &OptEntry : cl::getRegisteredOptions()) { 703 StringRef ArgName = OptEntry.getKey(); 704 cl::Option *Option = OptEntry.getValue(); 705 if (ArgName.size() == 1) 706 apply(Option, cl::Grouping); 707 } 708 } 709 710 int main(int argc, const char *argv[]) { 711 InitLLVM X(argc, argv); 712 ToolName = argv[0]; 713 714 // Register the target printer for --version. 715 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 716 717 if (sys::path::stem(argv[0]).contains("readelf")) { 718 opts::Output = opts::GNU; 719 registerReadelfAliases(); 720 } else { 721 registerReadobjAliases(); 722 } 723 724 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n"); 725 726 // Default to print error if no filename is specified. 727 if (opts::InputFilenames.empty()) { 728 error("no input files specified"); 729 } 730 731 if (opts::All) { 732 opts::FileHeaders = true; 733 opts::ProgramHeaders = true; 734 opts::SectionHeaders = true; 735 opts::Symbols = true; 736 opts::Relocations = true; 737 opts::DynamicTable = true; 738 opts::Notes = true; 739 opts::VersionInfo = true; 740 opts::UnwindInfo = true; 741 opts::SectionGroups = true; 742 opts::HashHistogram = true; 743 if (opts::Output == opts::LLVM) { 744 opts::Addrsig = true; 745 opts::PrintStackSizes = true; 746 } 747 } 748 749 if (opts::Headers) { 750 opts::FileHeaders = true; 751 opts::ProgramHeaders = true; 752 opts::SectionHeaders = true; 753 } 754 755 ScopedPrinter Writer(fouts()); 756 for (const std::string &I : opts::InputFilenames) 757 dumpInput(I, Writer); 758 759 if (opts::CodeViewMergedTypes) { 760 if (opts::CodeViewEnableGHash) 761 dumpCodeViewMergedTypes(Writer, CVTypes.GlobalIDTable.records(), 762 CVTypes.GlobalTypeTable.records()); 763 else 764 dumpCodeViewMergedTypes(Writer, CVTypes.IDTable.records(), 765 CVTypes.TypeTable.records()); 766 } 767 768 return 0; 769 } 770