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/ELFObjectFile.h" 28 #include "llvm/Object/MachOUniversal.h" 29 #include "llvm/Object/ObjectFile.h" 30 #include "llvm/Support/Casting.h" 31 #include "llvm/Support/CommandLine.h" 32 #include "llvm/Support/DataTypes.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/FileSystem.h" 35 #include "llvm/Support/ManagedStatic.h" 36 #include "llvm/Support/PrettyStackTrace.h" 37 #include "llvm/Support/Signals.h" 38 #include "llvm/Support/TargetRegistry.h" 39 #include "llvm/Support/TargetSelect.h" 40 #include <string> 41 #include <system_error> 42 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 // -symbols, -t 95 cl::opt<bool> Symbols("symbols", 96 cl::desc("Display the symbol table")); 97 cl::alias SymbolsShort("t", 98 cl::desc("Alias for --symbols"), 99 cl::aliasopt(Symbols)); 100 101 // -dyn-symbols, -dt 102 cl::opt<bool> DynamicSymbols("dyn-symbols", 103 cl::desc("Display the dynamic symbol table")); 104 cl::alias DynamicSymbolsShort("dt", 105 cl::desc("Alias for --dyn-symbols"), 106 cl::aliasopt(DynamicSymbols)); 107 108 // -unwind, -u 109 cl::opt<bool> UnwindInfo("unwind", 110 cl::desc("Display unwind information")); 111 cl::alias UnwindInfoShort("u", 112 cl::desc("Alias for --unwind"), 113 cl::aliasopt(UnwindInfo)); 114 115 // -dynamic-table 116 cl::opt<bool> DynamicTable("dynamic-table", 117 cl::desc("Display the ELF .dynamic section table")); 118 119 // -needed-libs 120 cl::opt<bool> NeededLibraries("needed-libs", 121 cl::desc("Display the needed libraries")); 122 123 // -program-headers 124 cl::opt<bool> ProgramHeaders("program-headers", 125 cl::desc("Display ELF program headers")); 126 127 // -expand-relocs 128 cl::opt<bool> ExpandRelocs("expand-relocs", 129 cl::desc("Expand each shown relocation to multiple lines")); 130 131 // -codeview 132 cl::opt<bool> CodeView("codeview", 133 cl::desc("Display CodeView debug information")); 134 135 // -codeview-subsection-bytes 136 cl::opt<bool> CodeViewSubsectionBytes( 137 "codeview-subsection-bytes", 138 cl::desc("Dump raw contents of codeview debug sections and records")); 139 140 // -arm-attributes, -a 141 cl::opt<bool> ARMAttributes("arm-attributes", 142 cl::desc("Display the ARM attributes section")); 143 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"), 144 cl::aliasopt(ARMAttributes)); 145 146 // -mips-plt-got 147 cl::opt<bool> 148 MipsPLTGOT("mips-plt-got", 149 cl::desc("Display the MIPS GOT and PLT GOT sections")); 150 151 // -mips-abi-flags 152 cl::opt<bool> MipsABIFlags("mips-abi-flags", 153 cl::desc("Display the MIPS.abiflags section")); 154 155 // -coff-imports 156 cl::opt<bool> 157 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table")); 158 159 // -coff-exports 160 cl::opt<bool> 161 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table")); 162 163 // -coff-directives 164 cl::opt<bool> 165 COFFDirectives("coff-directives", 166 cl::desc("Display the PE/COFF .drectve section")); 167 168 // -coff-basereloc 169 cl::opt<bool> 170 COFFBaseRelocs("coff-basereloc", 171 cl::desc("Display the PE/COFF .reloc section")); 172 } // namespace opts 173 174 static int ReturnValue = EXIT_SUCCESS; 175 176 namespace llvm { 177 178 bool error(std::error_code EC) { 179 if (!EC) 180 return false; 181 182 ReturnValue = EXIT_FAILURE; 183 outs() << "\nError reading file: " << EC.message() << ".\n"; 184 outs().flush(); 185 return true; 186 } 187 188 bool relocAddressLess(RelocationRef a, RelocationRef b) { 189 uint64_t a_addr, b_addr; 190 if (error(a.getOffset(a_addr))) exit(ReturnValue); 191 if (error(b.getOffset(b_addr))) exit(ReturnValue); 192 return a_addr < b_addr; 193 } 194 195 } // namespace llvm 196 197 static void reportError(StringRef Input, std::error_code EC) { 198 if (Input == "-") 199 Input = "<stdin>"; 200 201 errs() << Input << ": " << EC.message() << "\n"; 202 errs().flush(); 203 ReturnValue = EXIT_FAILURE; 204 } 205 206 static void reportError(StringRef Input, StringRef Message) { 207 if (Input == "-") 208 Input = "<stdin>"; 209 210 errs() << Input << ": " << Message << "\n"; 211 ReturnValue = EXIT_FAILURE; 212 } 213 214 static bool isMipsArch(unsigned Arch) { 215 switch (Arch) { 216 case llvm::Triple::mips: 217 case llvm::Triple::mipsel: 218 case llvm::Triple::mips64: 219 case llvm::Triple::mips64el: 220 return true; 221 default: 222 return false; 223 } 224 } 225 226 /// @brief Creates an format-specific object file dumper. 227 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer, 228 std::unique_ptr<ObjDumper> &Result) { 229 if (!Obj) 230 return readobj_error::unsupported_file_format; 231 232 if (Obj->isCOFF()) 233 return createCOFFDumper(Obj, Writer, Result); 234 if (Obj->isELF()) 235 return createELFDumper(Obj, Writer, Result); 236 if (Obj->isMachO()) 237 return createMachODumper(Obj, Writer, Result); 238 239 return readobj_error::unsupported_obj_file_format; 240 } 241 242 static StringRef getLoadName(const ObjectFile *Obj) { 243 if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj)) 244 return ELF->getLoadName(); 245 if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj)) 246 return ELF->getLoadName(); 247 if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj)) 248 return ELF->getLoadName(); 249 if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj)) 250 return ELF->getLoadName(); 251 llvm_unreachable("Not ELF"); 252 } 253 254 /// @brief Dumps the specified object file. 255 static void dumpObject(const ObjectFile *Obj) { 256 StreamWriter Writer(outs()); 257 std::unique_ptr<ObjDumper> Dumper; 258 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) { 259 reportError(Obj->getFileName(), EC); 260 return; 261 } 262 263 outs() << '\n'; 264 outs() << "File: " << Obj->getFileName() << "\n"; 265 outs() << "Format: " << Obj->getFileFormatName() << "\n"; 266 outs() << "Arch: " 267 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch()) 268 << "\n"; 269 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n"; 270 if (Obj->isELF()) 271 outs() << "LoadName: " << getLoadName(Obj) << "\n"; 272 273 if (opts::FileHeaders) 274 Dumper->printFileHeaders(); 275 if (opts::Sections) 276 Dumper->printSections(); 277 if (opts::Relocations) 278 Dumper->printRelocations(); 279 if (opts::Symbols) 280 Dumper->printSymbols(); 281 if (opts::DynamicSymbols) 282 Dumper->printDynamicSymbols(); 283 if (opts::UnwindInfo) 284 Dumper->printUnwindInfo(); 285 if (opts::DynamicTable) 286 Dumper->printDynamicTable(); 287 if (opts::NeededLibraries) 288 Dumper->printNeededLibraries(); 289 if (opts::ProgramHeaders) 290 Dumper->printProgramHeaders(); 291 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF()) 292 if (opts::ARMAttributes) 293 Dumper->printAttributes(); 294 if (isMipsArch(Obj->getArch()) && Obj->isELF()) { 295 if (opts::MipsPLTGOT) 296 Dumper->printMipsPLTGOT(); 297 if (opts::MipsABIFlags) 298 Dumper->printMipsABIFlags(); 299 } 300 if (opts::COFFImports) 301 Dumper->printCOFFImports(); 302 if (opts::COFFExports) 303 Dumper->printCOFFExports(); 304 if (opts::COFFDirectives) 305 Dumper->printCOFFDirectives(); 306 if (opts::COFFBaseRelocs) 307 Dumper->printCOFFBaseReloc(); 308 } 309 310 311 /// @brief Dumps each object file in \a Arc; 312 static void dumpArchive(const Archive *Arc) { 313 for (Archive::child_iterator ArcI = Arc->child_begin(), 314 ArcE = Arc->child_end(); 315 ArcI != ArcE; ++ArcI) { 316 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary(); 317 if (std::error_code EC = ChildOrErr.getError()) { 318 // Ignore non-object files. 319 if (EC != object_error::invalid_file_type) 320 reportError(Arc->getFileName(), EC.message()); 321 continue; 322 } 323 324 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) 325 dumpObject(Obj); 326 else 327 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format); 328 } 329 } 330 331 /// @brief Dumps each object file in \a MachO Universal Binary; 332 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) { 333 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) { 334 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile(); 335 if (ObjOrErr) 336 dumpObject(&*ObjOrErr.get()); 337 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive()) 338 dumpArchive(&*AOrErr.get()); 339 else 340 reportError(UBinary->getFileName(), ObjOrErr.getError().message()); 341 } 342 } 343 344 /// @brief Opens \a File and dumps it. 345 static void dumpInput(StringRef File) { 346 // If file isn't stdin, check that it exists. 347 if (File != "-" && !sys::fs::exists(File)) { 348 reportError(File, readobj_error::file_not_found); 349 return; 350 } 351 352 // Attempt to open the binary. 353 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File); 354 if (std::error_code EC = BinaryOrErr.getError()) { 355 reportError(File, EC); 356 return; 357 } 358 Binary &Binary = *BinaryOrErr.get().getBinary(); 359 360 if (Archive *Arc = dyn_cast<Archive>(&Binary)) 361 dumpArchive(Arc); 362 else if (MachOUniversalBinary *UBinary = 363 dyn_cast<MachOUniversalBinary>(&Binary)) 364 dumpMachOUniversalBinary(UBinary); 365 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 366 dumpObject(Obj); 367 else 368 reportError(File, readobj_error::unrecognized_file_format); 369 } 370 371 372 int main(int argc, const char *argv[]) { 373 sys::PrintStackTraceOnErrorSignal(); 374 PrettyStackTraceProgram X(argc, argv); 375 llvm_shutdown_obj Y; 376 377 // Initialize targets. 378 llvm::InitializeAllTargetInfos(); 379 380 // Register the target printer for --version. 381 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 382 383 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n"); 384 385 // Default to stdin if no filename is specified. 386 if (opts::InputFilenames.size() == 0) 387 opts::InputFilenames.push_back("-"); 388 389 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), 390 dumpInput); 391 392 return ReturnValue; 393 } 394