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/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"
40 #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> Symbols("symbols",
95 cl::desc("Display the symbol table"));
96 cl::alias SymbolsShort("t",
97 cl::desc("Alias for --symbols"),
98 cl::aliasopt(Symbols));
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));
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));
115 cl::opt<bool> DynamicTable("dynamic-table",
116 cl::desc("Display the ELF .dynamic section table"));
119 cl::opt<bool> NeededLibraries("needed-libs",
120 cl::desc("Display the needed libraries"));
123 cl::opt<bool> ProgramHeaders("program-headers",
124 cl::desc("Display ELF program headers"));
127 cl::opt<bool> ExpandRelocs("expand-relocs",
128 cl::desc("Expand each shown relocation to multiple lines"));
130 // -codeview-linetables
131 cl::opt<bool> CodeViewLineTables("codeview-linetables",
132 cl::desc("Display CodeView line table information"));
134 // -arm-attributes, -a
135 cl::opt<bool> ARMAttributes("arm-attributes",
136 cl::desc("Display the ARM attributes section"));
137 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
138 cl::aliasopt(ARMAttributes));
142 MipsPLTGOT("mips-plt-got",
143 cl::desc("Display the MIPS GOT and PLT GOT sections"));
147 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
151 COFFDirectives("coff-directives",
152 cl::desc("Display the PE/COFF .drectve section"));
156 COFFBaseRelocs("coff-basereloc",
157 cl::desc("Display the PE/COFF .reloc section"));
160 static int ReturnValue = EXIT_SUCCESS;
164 bool error(std::error_code EC) {
168 ReturnValue = EXIT_FAILURE;
169 outs() << "\nError reading file: " << EC.message() << ".\n";
174 bool relocAddressLess(RelocationRef a, RelocationRef b) {
175 uint64_t a_addr, b_addr;
176 if (error(a.getOffset(a_addr))) exit(ReturnValue);
177 if (error(b.getOffset(b_addr))) exit(ReturnValue);
178 return a_addr < b_addr;
183 static void reportError(StringRef Input, std::error_code EC) {
187 errs() << Input << ": " << EC.message() << "\n";
189 ReturnValue = EXIT_FAILURE;
192 static void reportError(StringRef Input, StringRef Message) {
196 errs() << Input << ": " << Message << "\n";
197 ReturnValue = EXIT_FAILURE;
200 static bool isMipsArch(unsigned Arch) {
202 case llvm::Triple::mips:
203 case llvm::Triple::mipsel:
204 case llvm::Triple::mips64:
205 case llvm::Triple::mips64el:
212 /// @brief Creates an format-specific object file dumper.
213 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
214 std::unique_ptr<ObjDumper> &Result) {
216 return readobj_error::unsupported_file_format;
219 return createCOFFDumper(Obj, Writer, Result);
221 return createELFDumper(Obj, Writer, Result);
223 return createMachODumper(Obj, Writer, Result);
225 return readobj_error::unsupported_obj_file_format;
228 static StringRef getLoadName(const ObjectFile *Obj) {
229 if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
230 return ELF->getLoadName();
231 if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
232 return ELF->getLoadName();
233 if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
234 return ELF->getLoadName();
235 if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
236 return ELF->getLoadName();
237 llvm_unreachable("Not ELF");
240 /// @brief Dumps the specified object file.
241 static void dumpObject(const ObjectFile *Obj) {
242 StreamWriter Writer(outs());
243 std::unique_ptr<ObjDumper> Dumper;
244 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
245 reportError(Obj->getFileName(), EC);
250 outs() << "File: " << Obj->getFileName() << "\n";
251 outs() << "Format: " << Obj->getFileFormatName() << "\n";
253 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
255 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
257 outs() << "LoadName: " << getLoadName(Obj) << "\n";
259 if (opts::FileHeaders)
260 Dumper->printFileHeaders();
262 Dumper->printSections();
263 if (opts::Relocations)
264 Dumper->printRelocations();
266 Dumper->printSymbols();
267 if (opts::DynamicSymbols)
268 Dumper->printDynamicSymbols();
269 if (opts::UnwindInfo)
270 Dumper->printUnwindInfo();
271 if (opts::DynamicTable)
272 Dumper->printDynamicTable();
273 if (opts::NeededLibraries)
274 Dumper->printNeededLibraries();
275 if (opts::ProgramHeaders)
276 Dumper->printProgramHeaders();
277 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
278 if (opts::ARMAttributes)
279 Dumper->printAttributes();
280 if (isMipsArch(Obj->getArch()) && Obj->isELF())
281 if (opts::MipsPLTGOT)
282 Dumper->printMipsPLTGOT();
283 if (opts::COFFImports)
284 Dumper->printCOFFImports();
285 if (opts::COFFDirectives)
286 Dumper->printCOFFDirectives();
287 if (opts::COFFBaseRelocs)
288 Dumper->printCOFFBaseReloc();
292 /// @brief Dumps each object file in \a Arc;
293 static void dumpArchive(const Archive *Arc) {
294 for (Archive::child_iterator ArcI = Arc->child_begin(),
295 ArcE = Arc->child_end();
296 ArcI != ArcE; ++ArcI) {
297 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
298 if (std::error_code EC = ChildOrErr.getError()) {
299 // Ignore non-object files.
300 if (EC != object_error::invalid_file_type)
301 reportError(Arc->getFileName(), EC.message());
305 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
308 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
313 /// @brief Opens \a File and dumps it.
314 static void dumpInput(StringRef File) {
315 // If file isn't stdin, check that it exists.
316 if (File != "-" && !sys::fs::exists(File)) {
317 reportError(File, readobj_error::file_not_found);
321 // Attempt to open the binary.
322 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
323 if (std::error_code EC = BinaryOrErr.getError()) {
324 reportError(File, EC);
327 Binary &Binary = *BinaryOrErr.get().getBinary();
329 if (Archive *Arc = dyn_cast<Archive>(&Binary))
331 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
334 reportError(File, readobj_error::unrecognized_file_format);
338 int main(int argc, const char *argv[]) {
339 sys::PrintStackTraceOnErrorSignal();
340 PrettyStackTraceProgram X(argc, argv);
343 // Initialize targets.
344 llvm::InitializeAllTargetInfos();
346 // Register the target printer for --version.
347 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
349 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
351 // Default to stdin if no filename is specified.
352 if (opts::InputFilenames.size() == 0)
353 opts::InputFilenames.push_back("-");
355 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),