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