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/COFFImportFile.h"
28 #include "llvm/Object/ELFObjectFile.h"
29 #include "llvm/Object/MachOUniversal.h"
30 #include "llvm/Object/ObjectFile.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/DataTypes.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/ManagedStatic.h"
37 #include "llvm/Support/PrettyStackTrace.h"
38 #include "llvm/Support/Signals.h"
39 #include "llvm/Support/TargetRegistry.h"
40 #include "llvm/Support/TargetSelect.h"
42 #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> DynRelocs("dyn-relocations",
96 cl::desc("Display the dynamic relocation entries in the file"));
99 cl::opt<bool> Symbols("symbols",
100 cl::desc("Display the symbol table"));
101 cl::alias SymbolsShort("t",
102 cl::desc("Alias for --symbols"),
103 cl::aliasopt(Symbols));
106 cl::opt<bool> DynamicSymbols("dyn-symbols",
107 cl::desc("Display the dynamic symbol table"));
108 cl::alias DynamicSymbolsShort("dt",
109 cl::desc("Alias for --dyn-symbols"),
110 cl::aliasopt(DynamicSymbols));
113 cl::opt<bool> UnwindInfo("unwind",
114 cl::desc("Display unwind information"));
115 cl::alias UnwindInfoShort("u",
116 cl::desc("Alias for --unwind"),
117 cl::aliasopt(UnwindInfo));
120 cl::opt<bool> DynamicTable("dynamic-table",
121 cl::desc("Display the ELF .dynamic section table"));
124 cl::opt<bool> NeededLibraries("needed-libs",
125 cl::desc("Display the needed libraries"));
128 cl::opt<bool> ProgramHeaders("program-headers",
129 cl::desc("Display ELF program headers"));
132 cl::opt<bool> HashTable("hash-table",
133 cl::desc("Display ELF hash table"));
136 cl::opt<bool> ExpandRelocs("expand-relocs",
137 cl::desc("Expand each shown relocation to multiple lines"));
140 cl::opt<bool> CodeView("codeview",
141 cl::desc("Display CodeView debug information"));
143 // -codeview-subsection-bytes
144 cl::opt<bool> CodeViewSubsectionBytes(
145 "codeview-subsection-bytes",
146 cl::desc("Dump raw contents of codeview debug sections and records"));
148 // -arm-attributes, -a
149 cl::opt<bool> ARMAttributes("arm-attributes",
150 cl::desc("Display the ARM attributes section"));
151 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
152 cl::aliasopt(ARMAttributes));
156 MipsPLTGOT("mips-plt-got",
157 cl::desc("Display the MIPS GOT and PLT GOT sections"));
160 cl::opt<bool> MipsABIFlags("mips-abi-flags",
161 cl::desc("Display the MIPS.abiflags section"));
164 cl::opt<bool> MipsReginfo("mips-reginfo",
165 cl::desc("Display the MIPS .reginfo section"));
169 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
173 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
177 COFFDirectives("coff-directives",
178 cl::desc("Display the PE/COFF .drectve section"));
182 COFFBaseRelocs("coff-basereloc",
183 cl::desc("Display the PE/COFF .reloc section"));
185 // -macho-data-in-code
187 MachODataInCode("macho-data-in-code",
188 cl::desc("Display MachO Data in Code command"));
190 // -macho-version-min
192 MachOVersionMin("macho-version-min",
193 cl::desc("Display MachO version min command"));
196 PrintStackMap("stackmap",
197 cl::desc("Display contents of stackmap section"));
203 void reportError(Twine Msg) {
204 outs() << "\nError reading file: " << Msg << ".\n";
209 void error(std::error_code EC) {
213 reportError(EC.message());
216 bool relocAddressLess(RelocationRef a, RelocationRef b) {
217 return a.getOffset() < b.getOffset();
222 static void reportError(StringRef Input, std::error_code EC) {
226 reportError(Twine(Input) + ": " + EC.message());
229 static void reportError(StringRef Input, StringRef Message) {
233 reportError(Twine(Input) + ": " + Message);
236 static bool isMipsArch(unsigned Arch) {
238 case llvm::Triple::mips:
239 case llvm::Triple::mipsel:
240 case llvm::Triple::mips64:
241 case llvm::Triple::mips64el:
248 /// @brief Creates an format-specific object file dumper.
249 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
250 std::unique_ptr<ObjDumper> &Result) {
252 return readobj_error::unsupported_file_format;
255 return createCOFFDumper(Obj, Writer, Result);
257 return createELFDumper(Obj, Writer, Result);
259 return createMachODumper(Obj, Writer, Result);
261 return readobj_error::unsupported_obj_file_format;
264 /// @brief Dumps the specified object file.
265 static void dumpObject(const ObjectFile *Obj) {
266 StreamWriter Writer(outs());
267 std::unique_ptr<ObjDumper> Dumper;
268 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
269 reportError(Obj->getFileName(), EC);
274 outs() << "File: " << Obj->getFileName() << "\n";
275 outs() << "Format: " << Obj->getFileFormatName() << "\n";
277 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
279 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
280 Dumper->printLoadName();
282 if (opts::FileHeaders)
283 Dumper->printFileHeaders();
285 Dumper->printSections();
286 if (opts::Relocations)
287 Dumper->printRelocations();
289 Dumper->printDynamicRelocations();
291 Dumper->printSymbols();
292 if (opts::DynamicSymbols)
293 Dumper->printDynamicSymbols();
294 if (opts::UnwindInfo)
295 Dumper->printUnwindInfo();
296 if (opts::DynamicTable)
297 Dumper->printDynamicTable();
298 if (opts::NeededLibraries)
299 Dumper->printNeededLibraries();
300 if (opts::ProgramHeaders)
301 Dumper->printProgramHeaders();
303 Dumper->printHashTable();
304 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
305 if (opts::ARMAttributes)
306 Dumper->printAttributes();
307 if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
308 if (opts::MipsPLTGOT)
309 Dumper->printMipsPLTGOT();
310 if (opts::MipsABIFlags)
311 Dumper->printMipsABIFlags();
312 if (opts::MipsReginfo)
313 Dumper->printMipsReginfo();
316 if (opts::COFFImports)
317 Dumper->printCOFFImports();
318 if (opts::COFFExports)
319 Dumper->printCOFFExports();
320 if (opts::COFFDirectives)
321 Dumper->printCOFFDirectives();
322 if (opts::COFFBaseRelocs)
323 Dumper->printCOFFBaseReloc();
326 if (opts::MachODataInCode)
327 Dumper->printMachODataInCode();
328 if (opts::MachOVersionMin)
329 Dumper->printMachOVersionMin();
330 if (opts::PrintStackMap)
331 Dumper->printStackMap();
334 static void dumpCOFFImportFile(const COFFImportFile *File) {
336 outs() << "File: " << File->getFileName() << "\n";
337 outs() << "Format: COFF-import-file\n";
340 /// @brief Dumps each object file in \a Arc;
341 static void dumpArchive(const Archive *Arc) {
342 for (const auto &Child : Arc->children()) {
343 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
344 if (std::error_code EC = ChildOrErr.getError()) {
345 // Ignore non-object files.
346 if (EC != object_error::invalid_file_type)
347 reportError(Arc->getFileName(), EC.message());
351 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
354 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
358 /// @brief Dumps each object file in \a MachO Universal Binary;
359 static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
360 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
361 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
363 dumpObject(&*ObjOrErr.get());
364 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
365 dumpArchive(&*AOrErr.get());
367 reportError(UBinary->getFileName(), ObjOrErr.getError().message());
371 /// @brief Opens \a File and dumps it.
372 static void dumpInput(StringRef File) {
373 // If file isn't stdin, check that it exists.
374 if (File != "-" && !sys::fs::exists(File)) {
375 reportError(File, readobj_error::file_not_found);
379 // Attempt to open the binary.
380 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
381 if (std::error_code EC = BinaryOrErr.getError()) {
382 reportError(File, EC);
385 Binary &Binary = *BinaryOrErr.get().getBinary();
387 if (Archive *Arc = dyn_cast<Archive>(&Binary))
389 else if (MachOUniversalBinary *UBinary =
390 dyn_cast<MachOUniversalBinary>(&Binary))
391 dumpMachOUniversalBinary(UBinary);
392 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
394 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
395 dumpCOFFImportFile(Import);
397 reportError(File, readobj_error::unrecognized_file_format);
400 int main(int argc, const char *argv[]) {
401 sys::PrintStackTraceOnErrorSignal();
402 PrettyStackTraceProgram X(argc, argv);
405 // Register the target printer for --version.
406 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
408 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
410 // Default to stdin if no filename is specified.
411 if (opts::InputFilenames.size() == 0)
412 opts::InputFilenames.push_back("-");
414 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),