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 // -coff-imports 152 cl::opt<bool> 153 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table")); 154 155 // -coff-exports 156 cl::opt<bool> 157 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table")); 158 159 // -coff-directives 160 cl::opt<bool> 161 COFFDirectives("coff-directives", 162 cl::desc("Display the PE/COFF .drectve section")); 163 164 // -coff-basereloc 165 cl::opt<bool> 166 COFFBaseRelocs("coff-basereloc", 167 cl::desc("Display the PE/COFF .reloc section")); 168 } // namespace opts 169 170 static int ReturnValue = EXIT_SUCCESS; 171 172 namespace llvm { 173 174 bool error(std::error_code EC) { 175 if (!EC) 176 return false; 177 178 ReturnValue = EXIT_FAILURE; 179 outs() << "\nError reading file: " << EC.message() << ".\n"; 180 outs().flush(); 181 return true; 182 } 183 184 bool relocAddressLess(RelocationRef a, RelocationRef b) { 185 uint64_t a_addr, b_addr; 186 if (error(a.getOffset(a_addr))) exit(ReturnValue); 187 if (error(b.getOffset(b_addr))) exit(ReturnValue); 188 return a_addr < b_addr; 189 } 190 191 } // namespace llvm 192 193 static void reportError(StringRef Input, std::error_code EC) { 194 if (Input == "-") 195 Input = "<stdin>"; 196 197 errs() << Input << ": " << EC.message() << "\n"; 198 errs().flush(); 199 ReturnValue = EXIT_FAILURE; 200 } 201 202 static void reportError(StringRef Input, StringRef Message) { 203 if (Input == "-") 204 Input = "<stdin>"; 205 206 errs() << Input << ": " << Message << "\n"; 207 ReturnValue = EXIT_FAILURE; 208 } 209 210 static bool isMipsArch(unsigned Arch) { 211 switch (Arch) { 212 case llvm::Triple::mips: 213 case llvm::Triple::mipsel: 214 case llvm::Triple::mips64: 215 case llvm::Triple::mips64el: 216 return true; 217 default: 218 return false; 219 } 220 } 221 222 /// @brief Creates an format-specific object file dumper. 223 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer, 224 std::unique_ptr<ObjDumper> &Result) { 225 if (!Obj) 226 return readobj_error::unsupported_file_format; 227 228 if (Obj->isCOFF()) 229 return createCOFFDumper(Obj, Writer, Result); 230 if (Obj->isELF()) 231 return createELFDumper(Obj, Writer, Result); 232 if (Obj->isMachO()) 233 return createMachODumper(Obj, Writer, Result); 234 235 return readobj_error::unsupported_obj_file_format; 236 } 237 238 static StringRef getLoadName(const ObjectFile *Obj) { 239 if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj)) 240 return ELF->getLoadName(); 241 if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj)) 242 return ELF->getLoadName(); 243 if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj)) 244 return ELF->getLoadName(); 245 if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj)) 246 return ELF->getLoadName(); 247 llvm_unreachable("Not ELF"); 248 } 249 250 /// @brief Dumps the specified object file. 251 static void dumpObject(const ObjectFile *Obj) { 252 StreamWriter Writer(outs()); 253 std::unique_ptr<ObjDumper> Dumper; 254 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) { 255 reportError(Obj->getFileName(), EC); 256 return; 257 } 258 259 outs() << '\n'; 260 outs() << "File: " << Obj->getFileName() << "\n"; 261 outs() << "Format: " << Obj->getFileFormatName() << "\n"; 262 outs() << "Arch: " 263 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch()) 264 << "\n"; 265 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n"; 266 if (Obj->isELF()) 267 outs() << "LoadName: " << getLoadName(Obj) << "\n"; 268 269 if (opts::FileHeaders) 270 Dumper->printFileHeaders(); 271 if (opts::Sections) 272 Dumper->printSections(); 273 if (opts::Relocations) 274 Dumper->printRelocations(); 275 if (opts::Symbols) 276 Dumper->printSymbols(); 277 if (opts::DynamicSymbols) 278 Dumper->printDynamicSymbols(); 279 if (opts::UnwindInfo) 280 Dumper->printUnwindInfo(); 281 if (opts::DynamicTable) 282 Dumper->printDynamicTable(); 283 if (opts::NeededLibraries) 284 Dumper->printNeededLibraries(); 285 if (opts::ProgramHeaders) 286 Dumper->printProgramHeaders(); 287 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF()) 288 if (opts::ARMAttributes) 289 Dumper->printAttributes(); 290 if (isMipsArch(Obj->getArch()) && Obj->isELF()) 291 if (opts::MipsPLTGOT) 292 Dumper->printMipsPLTGOT(); 293 if (opts::COFFImports) 294 Dumper->printCOFFImports(); 295 if (opts::COFFExports) 296 Dumper->printCOFFExports(); 297 if (opts::COFFDirectives) 298 Dumper->printCOFFDirectives(); 299 if (opts::COFFBaseRelocs) 300 Dumper->printCOFFBaseReloc(); 301 } 302 303 304 /// @brief Dumps each object file in \a Arc; 305 static void dumpArchive(const Archive *Arc) { 306 for (Archive::child_iterator ArcI = Arc->child_begin(), 307 ArcE = Arc->child_end(); 308 ArcI != ArcE; ++ArcI) { 309 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary(); 310 if (std::error_code EC = ChildOrErr.getError()) { 311 // Ignore non-object files. 312 if (EC != object_error::invalid_file_type) 313 reportError(Arc->getFileName(), EC.message()); 314 continue; 315 } 316 317 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) 318 dumpObject(Obj); 319 else 320 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format); 321 } 322 } 323 324 /// @brief Dumps each object file in \a MachO Universal Binary; 325 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) { 326 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) { 327 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile(); 328 if (ObjOrErr) 329 dumpObject(&*ObjOrErr.get()); 330 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive()) 331 dumpArchive(&*AOrErr.get()); 332 else 333 reportError(UBinary->getFileName(), ObjOrErr.getError().message()); 334 } 335 } 336 337 /// @brief Opens \a File and dumps it. 338 static void dumpInput(StringRef File) { 339 // If file isn't stdin, check that it exists. 340 if (File != "-" && !sys::fs::exists(File)) { 341 reportError(File, readobj_error::file_not_found); 342 return; 343 } 344 345 // Attempt to open the binary. 346 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File); 347 if (std::error_code EC = BinaryOrErr.getError()) { 348 reportError(File, EC); 349 return; 350 } 351 Binary &Binary = *BinaryOrErr.get().getBinary(); 352 353 if (Archive *Arc = dyn_cast<Archive>(&Binary)) 354 dumpArchive(Arc); 355 else if (MachOUniversalBinary *UBinary = 356 dyn_cast<MachOUniversalBinary>(&Binary)) 357 dumpMachOUniversalBinary(UBinary); 358 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) 359 dumpObject(Obj); 360 else 361 reportError(File, readobj_error::unrecognized_file_format); 362 } 363 364 365 int main(int argc, const char *argv[]) { 366 sys::PrintStackTraceOnErrorSignal(); 367 PrettyStackTraceProgram X(argc, argv); 368 llvm_shutdown_obj Y; 369 370 // Initialize targets. 371 llvm::InitializeAllTargetInfos(); 372 373 // Register the target printer for --version. 374 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); 375 376 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n"); 377 378 // Default to stdin if no filename is specified. 379 if (opts::InputFilenames.size() == 0) 380 opts::InputFilenames.push_back("-"); 381 382 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), 383 dumpInput); 384 385 return ReturnValue; 386 } 387