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>
45 using namespace llvm::object;
48 cl::list<std::string> InputFilenames(cl::Positional,
49 cl::desc("<input object files>"),
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));
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));
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));
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));
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));
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));
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));
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));
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));
116 cl::opt<bool> DynamicTable("dynamic-table",
117 cl::desc("Display the ELF .dynamic section table"));
120 cl::opt<bool> NeededLibraries("needed-libs",
121 cl::desc("Display the needed libraries"));
124 cl::opt<bool> ProgramHeaders("program-headers",
125 cl::desc("Display ELF program headers"));
128 cl::opt<bool> ExpandRelocs("expand-relocs",
129 cl::desc("Expand each shown relocation to multiple lines"));
132 cl::opt<bool> CodeView("codeview",
133 cl::desc("Display CodeView debug information"));
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"));
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));
148 MipsPLTGOT("mips-plt-got",
149 cl::desc("Display the MIPS GOT and PLT GOT sections"));
152 cl::opt<bool> MipsABIFlags("mips-abi-flags",
153 cl::desc("Display the MIPS.abiflags section"));
156 cl::opt<bool> MipsReginfo("mips-reginfo",
157 cl::desc("Display the MIPS .reginfo section"));
161 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
165 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
169 COFFDirectives("coff-directives",
170 cl::desc("Display the PE/COFF .drectve section"));
174 COFFBaseRelocs("coff-basereloc",
175 cl::desc("Display the PE/COFF .reloc section"));
178 static int ReturnValue = EXIT_SUCCESS;
182 bool error(std::error_code EC) {
186 ReturnValue = EXIT_FAILURE;
187 outs() << "\nError reading file: " << EC.message() << ".\n";
192 bool relocAddressLess(RelocationRef a, RelocationRef b) {
193 uint64_t a_addr, b_addr;
194 if (error(a.getOffset(a_addr))) exit(ReturnValue);
195 if (error(b.getOffset(b_addr))) exit(ReturnValue);
196 return a_addr < b_addr;
201 static void reportError(StringRef Input, std::error_code EC) {
205 errs() << Input << ": " << EC.message() << "\n";
207 ReturnValue = EXIT_FAILURE;
210 static void reportError(StringRef Input, StringRef Message) {
214 errs() << Input << ": " << Message << "\n";
215 ReturnValue = EXIT_FAILURE;
218 static bool isMipsArch(unsigned Arch) {
220 case llvm::Triple::mips:
221 case llvm::Triple::mipsel:
222 case llvm::Triple::mips64:
223 case llvm::Triple::mips64el:
230 /// @brief Creates an format-specific object file dumper.
231 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
232 std::unique_ptr<ObjDumper> &Result) {
234 return readobj_error::unsupported_file_format;
237 return createCOFFDumper(Obj, Writer, Result);
239 return createELFDumper(Obj, Writer, Result);
241 return createMachODumper(Obj, Writer, Result);
243 return readobj_error::unsupported_obj_file_format;
246 static StringRef getLoadName(const ObjectFile *Obj) {
247 if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
248 return ELF->getLoadName();
249 if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
250 return ELF->getLoadName();
251 if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
252 return ELF->getLoadName();
253 if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
254 return ELF->getLoadName();
255 llvm_unreachable("Not ELF");
258 /// @brief Dumps the specified object file.
259 static void dumpObject(const ObjectFile *Obj) {
260 StreamWriter Writer(outs());
261 std::unique_ptr<ObjDumper> Dumper;
262 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
263 reportError(Obj->getFileName(), EC);
268 outs() << "File: " << Obj->getFileName() << "\n";
269 outs() << "Format: " << Obj->getFileFormatName() << "\n";
271 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
273 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
275 outs() << "LoadName: " << getLoadName(Obj) << "\n";
277 if (opts::FileHeaders)
278 Dumper->printFileHeaders();
280 Dumper->printSections();
281 if (opts::Relocations)
282 Dumper->printRelocations();
284 Dumper->printSymbols();
285 if (opts::DynamicSymbols)
286 Dumper->printDynamicSymbols();
287 if (opts::UnwindInfo)
288 Dumper->printUnwindInfo();
289 if (opts::DynamicTable)
290 Dumper->printDynamicTable();
291 if (opts::NeededLibraries)
292 Dumper->printNeededLibraries();
293 if (opts::ProgramHeaders)
294 Dumper->printProgramHeaders();
295 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
296 if (opts::ARMAttributes)
297 Dumper->printAttributes();
298 if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
299 if (opts::MipsPLTGOT)
300 Dumper->printMipsPLTGOT();
301 if (opts::MipsABIFlags)
302 Dumper->printMipsABIFlags();
303 if (opts::MipsReginfo)
304 Dumper->printMipsReginfo();
306 if (opts::COFFImports)
307 Dumper->printCOFFImports();
308 if (opts::COFFExports)
309 Dumper->printCOFFExports();
310 if (opts::COFFDirectives)
311 Dumper->printCOFFDirectives();
312 if (opts::COFFBaseRelocs)
313 Dumper->printCOFFBaseReloc();
317 /// @brief Dumps each object file in \a Arc;
318 static void dumpArchive(const Archive *Arc) {
319 for (Archive::child_iterator ArcI = Arc->child_begin(),
320 ArcE = Arc->child_end();
321 ArcI != ArcE; ++ArcI) {
322 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
323 if (std::error_code EC = ChildOrErr.getError()) {
324 // Ignore non-object files.
325 if (EC != object_error::invalid_file_type)
326 reportError(Arc->getFileName(), EC.message());
330 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
333 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
337 /// @brief Dumps each object file in \a MachO Universal Binary;
338 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
339 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
340 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
342 dumpObject(&*ObjOrErr.get());
343 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
344 dumpArchive(&*AOrErr.get());
346 reportError(UBinary->getFileName(), ObjOrErr.getError().message());
350 /// @brief Opens \a File and dumps it.
351 static void dumpInput(StringRef File) {
352 // If file isn't stdin, check that it exists.
353 if (File != "-" && !sys::fs::exists(File)) {
354 reportError(File, readobj_error::file_not_found);
358 // Attempt to open the binary.
359 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
360 if (std::error_code EC = BinaryOrErr.getError()) {
361 reportError(File, EC);
364 Binary &Binary = *BinaryOrErr.get().getBinary();
366 if (Archive *Arc = dyn_cast<Archive>(&Binary))
368 else if (MachOUniversalBinary *UBinary =
369 dyn_cast<MachOUniversalBinary>(&Binary))
370 dumpMachOUniversalBinary(UBinary);
371 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
374 reportError(File, readobj_error::unrecognized_file_format);
378 int main(int argc, const char *argv[]) {
379 sys::PrintStackTraceOnErrorSignal();
380 PrettyStackTraceProgram X(argc, argv);
383 // Register the target printer for --version.
384 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
386 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
388 // Default to stdin if no filename is specified.
389 if (opts::InputFilenames.size() == 0)
390 opts::InputFilenames.push_back("-");
392 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),