1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This is a tool similar to readelf, except it works on multiple object file 11 // formats. The main purpose of this tool is to provide detailed output suitable 12 // for FileCheck. 13 // 14 // Flags should be similar to readelf where supported, but the output format 15 // does not need to be identical. The point is to not make users learn yet 16 // another set of flags. 17 // 18 // Output should be specialized for each format where appropriate. 19 // 20 //===----------------------------------------------------------------------===// 21 22 #include "llvm-readobj.h" 23 #include "Error.h" 24 #include "ObjDumper.h" 25 #include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.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/Support/Casting.h" 32 #include "llvm/Support/CommandLine.h" 33 #include "llvm/Support/DataTypes.h" 34 #include "llvm/Support/Debug.h" 35 #include "llvm/Support/FileSystem.h" 36 #include "llvm/Support/ManagedStatic.h" 37 #include "llvm/Support/PrettyStackTrace.h" 38 #include "llvm/Support/ScopedPrinter.h" 39 #include "llvm/Support/Signals.h" 40 #include "llvm/Support/TargetRegistry.h" 41 #include "llvm/Support/TargetSelect.h" 42 #include <string> 43 #include <system_error> 44 45 using namespace llvm; 46 using namespace llvm::object; 47 48 namespace opts { 49 cl::list<std::string> InputFilenames(cl::Positional, 50 cl::desc("<input object files>"), 51 cl::ZeroOrMore); 52 53 // -file-headers, -h 54 cl::opt<bool> FileHeaders("file-headers", 55 cl::desc("Display file headers ")); 56 cl::alias FileHeadersShort("h", 57 cl::desc("Alias for --file-headers"), 58 cl::aliasopt(FileHeaders)); 59 60 // -sections, -s 61 cl::opt<bool> Sections("sections", 62 cl::desc("Display all sections.")); 63 cl::alias SectionsShort("s", 64 cl::desc("Alias for --sections"), 65 cl::aliasopt(Sections)); 66 67 // -section-relocations, -sr 68 cl::opt<bool> SectionRelocations("section-relocations", 69 cl::desc("Display relocations for each section shown.")); 70 cl::alias SectionRelocationsShort("sr", 71 cl::desc("Alias for --section-relocations"), 72 cl::aliasopt(SectionRelocations)); 73 74 // -section-symbols, -st 75 cl::opt<bool> SectionSymbols("section-symbols", 76 cl::desc("Display symbols for each section shown.")); 77 cl::alias SectionSymbolsShort("st", 78 cl::desc("Alias for --section-symbols"), 79 cl::aliasopt(SectionSymbols)); 80 81 // -section-data, -sd 82 cl::opt<bool> SectionData("section-data", 83 cl::desc("Display section data for each section shown.")); 84 cl::alias SectionDataShort("sd", 85 cl::desc("Alias for --section-data"), 86 cl::aliasopt(SectionData)); 87 88 // -relocations, -r 89 cl::opt<bool> Relocations("relocations", 90 cl::desc("Display the relocation entries in the file")); 91 cl::alias RelocationsShort("r", 92 cl::desc("Alias for --relocations"), 93 cl::aliasopt(Relocations)); 94 95 // -dyn-relocations 96 cl::opt<bool> DynRelocs("dyn-relocations", 97 cl::desc("Display the dynamic relocation entries in the file")); 98 99 // -symbols, -t 100 cl::opt<bool> Symbols("symbols", 101 cl::desc("Display the symbol table")); 102 cl::alias SymbolsShort("t", 103 cl::desc("Alias for --symbols"), 104 cl::aliasopt(Symbols)); 105 106 // -dyn-symbols, -dt 107 cl::opt<bool> DynamicSymbols("dyn-symbols", 108 cl::desc("Display the dynamic symbol table")); 109 cl::alias DynamicSymbolsShort("dt", 110 cl::desc("Alias for --dyn-symbols"), 111 cl::aliasopt(DynamicSymbols)); 112 113 // -unwind, -u 114 cl::opt<bool> UnwindInfo("unwind", 115 cl::desc("Display unwind information")); 116 cl::alias UnwindInfoShort("u", 117 cl::desc("Alias for --unwind"), 118 cl::aliasopt(UnwindInfo)); 119 120 // -dynamic-table 121 cl::opt<bool> DynamicTable("dynamic-table", 122 cl::desc("Display the ELF .dynamic section table")); 123 cl::alias DynamicTableShort("d", cl::desc("Alias for --dynamic-table"), 124 cl::aliasopt(DynamicTable)); 125 126 // -needed-libs 127 cl::opt<bool> NeededLibraries("needed-libs", 128 cl::desc("Display the needed libraries")); 129 130 // -program-headers 131 cl::opt<bool> ProgramHeaders("program-headers", 132 cl::desc("Display ELF program headers")); 133 cl::alias ProgramHeadersShort("l", cl::desc("Alias for --program-headers"), 134 cl::aliasopt(ProgramHeaders)); 135 136 // -hash-table 137 cl::opt<bool> HashTable("hash-table", 138 cl::desc("Display ELF hash table")); 139 140 // -gnu-hash-table 141 cl::opt<bool> GnuHashTable("gnu-hash-table", 142 cl::desc("Display ELF .gnu.hash section")); 143 144 // -expand-relocs 145 cl::opt<bool> ExpandRelocs("expand-relocs", 146 cl::desc("Expand each shown relocation to multiple lines")); 147 148 // -codeview 149 cl::opt<bool> CodeView("codeview", 150 cl::desc("Display CodeView debug information")); 151 152 // -codeview-merged-types 153 cl::opt<bool> 154 CodeViewMergedTypes("codeview-merged-types", 155 cl::desc("Display the merged CodeView type stream")); 156 157 // -codeview-subsection-bytes 158 cl::opt<bool> CodeViewSubsectionBytes( 159 "codeview-subsection-bytes", 160 cl::desc("Dump raw contents of codeview debug sections and records")); 161 162 // -arm-attributes, -a 163 cl::opt<bool> ARMAttributes("arm-attributes", 164 cl::desc("Display the ARM attributes section")); 165 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"), 166 cl::aliasopt(ARMAttributes)); 167 168 // -mips-plt-got 169 cl::opt<bool> 170 MipsPLTGOT("mips-plt-got", 171 cl::desc("Display the MIPS GOT and PLT GOT sections")); 172 173 // -mips-abi-flags 174 cl::opt<bool> MipsABIFlags("mips-abi-flags", 175 cl::desc("Display the MIPS.abiflags section")); 176 177 // -mips-reginfo 178 cl::opt<bool> MipsReginfo("mips-reginfo", 179 cl::desc("Display the MIPS .reginfo section")); 180 181 // -mips-options 182 cl::opt<bool> MipsOptions("mips-options", 183 cl::desc("Display the MIPS .MIPS.options section")); 184 185 // -coff-imports 186 cl::opt<bool> 187 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table")); 188 189 // -coff-exports 190 cl::opt<bool> 191 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table")); 192 193 // -coff-directives 194 cl::opt<bool> 195 COFFDirectives("coff-directives", 196 cl::desc("Display the PE/COFF .drectve section")); 197 198 // -coff-basereloc 199 cl::opt<bool> 200 COFFBaseRelocs("coff-basereloc", 201 cl::desc("Display the PE/COFF .reloc section")); 202 203 // -coff-debug-directory 204 cl::opt<bool> 205 COFFDebugDirectory("coff-debug-directory", 206 cl::desc("Display the PE/COFF debug directory")); 207 208 // -macho-data-in-code 209 cl::opt<bool> 210 MachODataInCode("macho-data-in-code", 211 cl::desc("Display MachO Data in Code command")); 212 213 // -macho-indirect-symbols 214 cl::opt<bool> 215 MachOIndirectSymbols("macho-indirect-symbols", 216 cl::desc("Display MachO indirect symbols")); 217 218 // -macho-linker-options 219 cl::opt<bool> 220 MachOLinkerOptions("macho-linker-options", 221 cl::desc("Display MachO linker options")); 222 223 // -macho-segment 224 cl::opt<bool> 225 MachOSegment("macho-segment", 226 cl::desc("Display MachO Segment command")); 227 228 // -macho-version-min 229 cl::opt<bool> 230 MachOVersionMin("macho-version-min", 231 cl::desc("Display MachO version min command")); 232 233 // -macho-dysymtab 234 cl::opt<bool> 235 MachODysymtab("macho-dysymtab", 236 cl::desc("Display MachO Dysymtab command")); 237 238 // -stackmap 239 cl::opt<bool> 240 PrintStackMap("stackmap", 241 cl::desc("Display contents of stackmap section")); 242 243 // -version-info 244 cl::opt<bool> 245 VersionInfo("version-info", 246 cl::desc("Display ELF version sections (if present)")); 247 cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"), 248 cl::aliasopt(VersionInfo)); 249 250 cl::opt<bool> SectionGroups("elf-section-groups", 251 cl::desc("Display ELF section group contents")); 252 cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"), 253 cl::aliasopt(SectionGroups)); 254 cl::opt<bool> HashHistogram( 255 "elf-hash-histogram", 256 cl::desc("Display bucket list histogram for hash sections")); 257 cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"), 258 cl::aliasopt(HashHistogram)); 259 260 cl::opt<OutputStyleTy> 261 Output("elf-output-style", cl::desc("Specify ELF dump style"), 262 cl::values(clEnumVal(LLVM, "LLVM default style"), 263 clEnumVal(GNU, "GNU readelf style"), clEnumValEnd), 264 cl::init(LLVM)); 265 } // namespace opts 266 267 namespace llvm { 268 269 LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { 270 errs() << "\nError reading file: " << Msg << ".\n"; 271 errs().flush(); 272 exit(1); 273 } 274 275 void error(Error EC) { 276 if (!EC) 277 return; 278 handleAllErrors(std::move(EC), 279 [&](const ErrorInfoBase &EI) { reportError(EI.message()); }); 280 } 281 282 void error(std::error_code EC) { 283 if (!EC) 284 return; 285 reportError(EC.message()); 286 } 287 288 bool relocAddressLess(RelocationRef a, RelocationRef b) { 289 return a.getOffset() < b.getOffset(); 290 } 291 292 } // namespace llvm 293 294 static void reportError(StringRef Input, std::error_code EC) { 295 if (Input == "-") 296 Input = "<stdin>"; 297 298 reportError(Twine(Input) + ": " + EC.message()); 299 } 300 301 static void reportError(StringRef Input, StringRef Message) { 302 if (Input == "-") 303 Input = "<stdin>"; 304 305 reportError(Twine(Input) + ": " + Message); 306 } 307 308 static void reportError(StringRef Input, Error Err) { 309 if (Input == "-") 310 Input = "<stdin>"; 311 std::string ErrMsg; 312 { 313 raw_string_ostream ErrStream(ErrMsg); 314 logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": "); 315 } 316 reportError(ErrMsg); 317 } 318 319 static bool isMipsArch(unsigned Arch) { 320 switch (Arch) { 321 case llvm::Triple::mips: 322 case llvm::Triple::mipsel: 323 case llvm::Triple::mips64: 324 case llvm::Triple::mips64el: 325 return true; 326 default: 327 return false; 328 } 329 } 330 331 static llvm::codeview::MemoryTypeTableBuilder CVTypes; 332 333 /// @brief Creates an format-specific object file dumper. 334 static std::error_code createDumper(const ObjectFile *Obj, 335 ScopedPrinter &Writer, 336 std::unique_ptr<ObjDumper> &Result) { 337 if (!Obj) 338 return readobj_error::unsupported_file_format; 339 340 if (Obj->isCOFF()) 341 return createCOFFDumper(Obj, Writer, Result); 342 if (Obj->isELF()) 343 return createELFDumper(Obj, Writer, Result); 344 if (Obj->isMachO()) 345 return createMachODumper(Obj, Writer, Result); 346 347 return readobj_error::unsupported_obj_file_format; 348 } 349 350 /// @brief Dumps the specified object file. 351 static void dumpObject(const ObjectFile *Obj) { 352 ScopedPrinter Writer(outs()); 353 std::unique_ptr<ObjDumper> Dumper; 354 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) 355 reportError(Obj->getFileName(), EC); 356 357 if (opts::Output == opts::LLVM) { 358 outs() << '\n'; 359 outs() << "File: " << Obj->getFileName() << "\n"; 360 outs() << "Format: " << Obj->getFileFormatName() << "\n"; 361 outs() << "Arch: " << Triple::getArchTypeName( 362 (llvm::Triple::ArchType)Obj->getArch()) << "\n"; 363 outs() << "AddressSize: " << (8 * Obj->getBytesInAddress()) << "bit\n"; 364 Dumper->printLoadName(); 365 } 366 367 if (opts::FileHeaders) 368 Dumper->printFileHeaders(); 369 if (opts::Sections) 370 Dumper->printSections(); 371 if (opts::Relocations) 372 Dumper->printRelocations(); 373 if (opts::DynRelocs) 374 Dumper->printDynamicRelocations(); 375 if (opts::Symbols) 376 Dumper->printSymbols(); 377 if (opts::DynamicSymbols) 378 Dumper->printDynamicSymbols(); 379 if (opts::UnwindInfo) 380 Dumper->printUnwindInfo(); 381 if (opts::DynamicTable) 382 Dumper->printDynamicTable(); 383 if (opts::NeededLibraries) 384 Dumper->printNeededLibraries(); 385 if (opts::ProgramHeaders) 386 Dumper->printProgramHeaders(); 387 if (opts::HashTable) 388 Dumper->printHashTable(); 389 if (opts::GnuHashTable) 390 Dumper->printGnuHashTable(); 391 if (opts::VersionInfo) 392 Dumper->printVersionInfo(); 393 if (Obj->isELF()) { 394 if (Obj->getArch() == llvm::Triple::arm) 395 if (opts::ARMAttributes) 396 Dumper->printAttributes(); 397 if (isMipsArch(Obj->getArch())) { 398 if (opts::MipsPLTGOT) 399 Dumper->printMipsPLTGOT(); 400 if (opts::MipsABIFlags) 401 Dumper->printMipsABIFlags(); 402 if (opts::MipsReginfo) 403 Dumper->printMipsReginfo(); 404 if (opts::MipsOptions) 405 Dumper->printMipsOptions(); 406 } 407 if (opts::SectionGroups) 408 Dumper->printGroupSections(); 409 if (opts::HashHistogram) 410 Dumper->printHashHistogram(); 411 } 412 if (Obj->isCOFF()) { 413 if (opts::COFFImports) 414 Dumper->printCOFFImports(); 415 if (opts::COFFExports) 416 Dumper->printCOFFExports(); 417 if (opts::COFFDirectives) 418 Dumper->printCOFFDirectives(); 419 if (opts::COFFBaseRelocs) 420 Dumper->printCOFFBaseReloc(); 421 if (opts::COFFDebugDirectory) 422 Dumper->printCOFFDebugDirectory(); 423 if (opts::CodeView) 424 Dumper->printCodeViewDebugInfo(); 425 if (opts::CodeViewMergedTypes) 426 Dumper->mergeCodeViewTypes(CVTypes); 427 } 428 if (Obj->isMachO()) { 429 if (opts::MachODataInCode) 430 Dumper->printMachODataInCode(); 431 if (opts::MachOIndirectSymbols) 432 Dumper->printMachOIndirectSymbols(); 433 if (opts::MachOLinkerOptions) 434 Dumper->printMachOLinkerOptions(); 435 if (opts::MachOSegment) 436 Dumper->printMachOSegment(); 437 if (opts::MachOVersionMin) 438 Dumper->printMachOVersionMin(); 439 if (opts::MachODysymtab) 440 Dumper->printMachODysymtab(); 441 } 442 if (opts::PrintStackMap) 443 Dumper->printStackMap(); 444 } 445 446 /// @brief Dumps each object file in \a Arc; 447 static void dumpArchive(const Archive *Arc) { 448 Error Err; 449 for (auto &Child : Arc->children(Err)) { 450 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); 451 if (!ChildOrErr) { 452 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) { 453 std::string Buf; 454 raw_string_ostream OS(Buf); 455 logAllUnhandledErrors(ChildOrErr.takeError(), OS, ""); 456 OS.flush(); 457 reportError(Arc->getFileName(), Buf); 458 } 459 continue; 460 } 461 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) 462 dumpObject(Obj); 463 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get())) 464 dumpCOFFImportFile(Imp); 465 else 466 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format); 467 } 468 if (Err) 469 reportError(Arc->getFileName(), std::move(Err)); 470 } 471 472 /// @brief Dumps each object file in \a MachO Universal Binary; 473 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) { 474 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) { 475 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile(); 476 if (ObjOrErr) 477 dumpObject(&*ObjOrErr.get()); 478 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) { 479 std::string Buf; 480 raw_string_ostream OS(Buf); 481 logAllUnhandledErrors(ObjOrErr.takeError(), OS, ""); 482 OS.flush(); 483 reportError(UBinary->getFileName(), Buf); 484 } 485 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive()) 486 dumpArchive(&*AOrErr.get()); 487 } 488 } 489 490 /// @brief Opens \a File and dumps it. 491 static void dumpInput(StringRef File) { 492 493 // Attempt to open the binary. 494 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File); 495 if (!BinaryOrErr) 496 reportError(File, errorToErrorCode(BinaryOrErr.takeError())); 497 Binary &Binary = *BinaryOrErr.get().getBinary(); 498 499 if (Archive *Arc = dyn_cast<Archive>(&Binary)) 500 dumpArchive(Arc); 501 else if (MachOUniversalBinary *UBinary = 502 dyn_cast<MachOUniversalBinary>(&Binary)) 503 dumpMachOUniversalBinary(UBinary); 504 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 505 dumpObject(Obj); 506 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary)) 507 dumpCOFFImportFile(Import); 508 else 509 reportError(File, readobj_error::unrecognized_file_format); 510 } 511 512 int main(int argc, const char *argv[]) { 513 sys::PrintStackTraceOnErrorSignal(argv[0]); 514 PrettyStackTraceProgram X(argc, argv); 515 llvm_shutdown_obj Y; 516 517 // Register the target printer for --version. 518 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 519 520 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n"); 521 522 // Default to stdin if no filename is specified. 523 if (opts::InputFilenames.size() == 0) 524 opts::InputFilenames.push_back("-"); 525 526 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), 527 dumpInput); 528 529 if (opts::CodeViewMergedTypes) { 530 ScopedPrinter W(outs()); 531 dumpCodeViewMergedTypes(W, CVTypes); 532 } 533 534 return 0; 535 } 536