1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
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.
18 // Output should be specialized for each format where appropriate.
20 //===----------------------------------------------------------------------===//
22 #include "llvm-readobj.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"
41 #include <system_error>
44 using namespace llvm::object;
47 cl::list<std::string> InputFilenames(cl::Positional,
48 cl::desc("<input object files>"),
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));
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));
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));
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));
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));
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));
94 cl::opt<bool> DynRelocs("dyn-relocations",
95 cl::desc("Display the dynamic relocation entries in the file"));
98 cl::opt<bool> Symbols("symbols",
99 cl::desc("Display the symbol table"));
100 cl::alias SymbolsShort("t",
101 cl::desc("Alias for --symbols"),
102 cl::aliasopt(Symbols));
105 cl::opt<bool> DynamicSymbols("dyn-symbols",
106 cl::desc("Display the dynamic symbol table"));
107 cl::alias DynamicSymbolsShort("dt",
108 cl::desc("Alias for --dyn-symbols"),
109 cl::aliasopt(DynamicSymbols));
112 cl::opt<bool> UnwindInfo("unwind",
113 cl::desc("Display unwind information"));
114 cl::alias UnwindInfoShort("u",
115 cl::desc("Alias for --unwind"),
116 cl::aliasopt(UnwindInfo));
119 cl::opt<bool> DynamicTable("dynamic-table",
120 cl::desc("Display the ELF .dynamic section table"));
123 cl::opt<bool> NeededLibraries("needed-libs",
124 cl::desc("Display the needed libraries"));
127 cl::opt<bool> ProgramHeaders("program-headers",
128 cl::desc("Display ELF program headers"));
131 cl::opt<bool> HashTable("hash-table",
132 cl::desc("Display ELF hash table"));
135 cl::opt<bool> ExpandRelocs("expand-relocs",
136 cl::desc("Expand each shown relocation to multiple lines"));
139 cl::opt<bool> CodeView("codeview",
140 cl::desc("Display CodeView debug information"));
142 // -codeview-subsection-bytes
143 cl::opt<bool> CodeViewSubsectionBytes(
144 "codeview-subsection-bytes",
145 cl::desc("Dump raw contents of codeview debug sections and records"));
147 // -arm-attributes, -a
148 cl::opt<bool> ARMAttributes("arm-attributes",
149 cl::desc("Display the ARM attributes section"));
150 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
151 cl::aliasopt(ARMAttributes));
155 MipsPLTGOT("mips-plt-got",
156 cl::desc("Display the MIPS GOT and PLT GOT sections"));
159 cl::opt<bool> MipsABIFlags("mips-abi-flags",
160 cl::desc("Display the MIPS.abiflags section"));
163 cl::opt<bool> MipsReginfo("mips-reginfo",
164 cl::desc("Display the MIPS .reginfo section"));
168 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
172 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
176 COFFDirectives("coff-directives",
177 cl::desc("Display the PE/COFF .drectve section"));
181 COFFBaseRelocs("coff-basereloc",
182 cl::desc("Display the PE/COFF .reloc section"));
186 PrintStackMap("stackmap",
187 cl::desc("Display contents of stackmap section"));
191 static int ReturnValue = EXIT_SUCCESS;
195 bool error(std::error_code EC) {
199 ReturnValue = EXIT_FAILURE;
200 outs() << "\nError reading file: " << EC.message() << ".\n";
205 bool relocAddressLess(RelocationRef a, RelocationRef b) {
206 return a.getOffset() < b.getOffset();
211 static void reportError(StringRef Input, std::error_code EC) {
215 errs() << Input << ": " << EC.message() << "\n";
217 ReturnValue = EXIT_FAILURE;
220 static void reportError(StringRef Input, StringRef Message) {
224 errs() << Input << ": " << Message << "\n";
225 ReturnValue = EXIT_FAILURE;
228 static bool isMipsArch(unsigned Arch) {
230 case llvm::Triple::mips:
231 case llvm::Triple::mipsel:
232 case llvm::Triple::mips64:
233 case llvm::Triple::mips64el:
240 /// @brief Creates an format-specific object file dumper.
241 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
242 std::unique_ptr<ObjDumper> &Result) {
244 return readobj_error::unsupported_file_format;
247 return createCOFFDumper(Obj, Writer, Result);
249 return createELFDumper(Obj, Writer, Result);
251 return createMachODumper(Obj, Writer, Result);
253 return readobj_error::unsupported_obj_file_format;
256 static StringRef getLoadName(const ObjectFile *Obj) {
257 if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
258 return ELF->getLoadName();
259 if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
260 return ELF->getLoadName();
261 if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
262 return ELF->getLoadName();
263 if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
264 return ELF->getLoadName();
265 llvm_unreachable("Not ELF");
268 /// @brief Dumps the specified object file.
269 static void dumpObject(const ObjectFile *Obj) {
270 StreamWriter Writer(outs());
271 std::unique_ptr<ObjDumper> Dumper;
272 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
273 reportError(Obj->getFileName(), EC);
278 outs() << "File: " << Obj->getFileName() << "\n";
279 outs() << "Format: " << Obj->getFileFormatName() << "\n";
281 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
283 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
285 outs() << "LoadName: " << getLoadName(Obj) << "\n";
287 if (opts::FileHeaders)
288 Dumper->printFileHeaders();
290 Dumper->printSections();
291 if (opts::Relocations)
292 Dumper->printRelocations();
294 Dumper->printDynamicRelocations();
296 Dumper->printSymbols();
297 if (opts::DynamicSymbols)
298 Dumper->printDynamicSymbols();
299 if (opts::UnwindInfo)
300 Dumper->printUnwindInfo();
301 if (opts::DynamicTable)
302 Dumper->printDynamicTable();
303 if (opts::NeededLibraries)
304 Dumper->printNeededLibraries();
305 if (opts::ProgramHeaders)
306 Dumper->printProgramHeaders();
308 Dumper->printHashTable();
309 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
310 if (opts::ARMAttributes)
311 Dumper->printAttributes();
312 if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
313 if (opts::MipsPLTGOT)
314 Dumper->printMipsPLTGOT();
315 if (opts::MipsABIFlags)
316 Dumper->printMipsABIFlags();
317 if (opts::MipsReginfo)
318 Dumper->printMipsReginfo();
320 if (opts::COFFImports)
321 Dumper->printCOFFImports();
322 if (opts::COFFExports)
323 Dumper->printCOFFExports();
324 if (opts::COFFDirectives)
325 Dumper->printCOFFDirectives();
326 if (opts::COFFBaseRelocs)
327 Dumper->printCOFFBaseReloc();
329 if (opts::PrintStackMap)
330 Dumper->printStackMap();
333 /// @brief Dumps each object file in \a Arc;
334 static void dumpArchive(const Archive *Arc) {
335 for (Archive::child_iterator ArcI = Arc->child_begin(),
336 ArcE = Arc->child_end();
337 ArcI != ArcE; ++ArcI) {
338 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
339 if (std::error_code EC = ChildOrErr.getError()) {
340 // Ignore non-object files.
341 if (EC != object_error::invalid_file_type)
342 reportError(Arc->getFileName(), EC.message());
346 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
349 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
353 /// @brief Dumps each object file in \a MachO Universal Binary;
354 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
355 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
356 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
358 dumpObject(&*ObjOrErr.get());
359 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
360 dumpArchive(&*AOrErr.get());
362 reportError(UBinary->getFileName(), ObjOrErr.getError().message());
366 /// @brief Opens \a File and dumps it.
367 static void dumpInput(StringRef File) {
368 // If file isn't stdin, check that it exists.
369 if (File != "-" && !sys::fs::exists(File)) {
370 reportError(File, readobj_error::file_not_found);
374 // Attempt to open the binary.
375 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
376 if (std::error_code EC = BinaryOrErr.getError()) {
377 reportError(File, EC);
380 Binary &Binary = *BinaryOrErr.get().getBinary();
382 if (Archive *Arc = dyn_cast<Archive>(&Binary))
384 else if (MachOUniversalBinary *UBinary =
385 dyn_cast<MachOUniversalBinary>(&Binary))
386 dumpMachOUniversalBinary(UBinary);
387 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
390 reportError(File, readobj_error::unrecognized_file_format);
393 int main(int argc, const char *argv[]) {
394 sys::PrintStackTraceOnErrorSignal();
395 PrettyStackTraceProgram X(argc, argv);
398 // Register the target printer for --version.
399 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
401 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
403 // Default to stdin if no filename is specified.
404 if (opts::InputFilenames.size() == 0)
405 opts::InputFilenames.push_back("-");
407 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),