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