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/ObjectFile.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/DataTypes.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include "llvm/Support/Signals.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/TargetSelect.h"
38 #include "llvm/Support/system_error.h"
43 using namespace llvm::object;
46 cl::list<std::string> InputFilenames(cl::Positional,
47 cl::desc("<input object files>"),
51 cl::opt<bool> FileHeaders("file-headers",
52 cl::desc("Display file headers "));
53 cl::alias FileHeadersShort("h",
54 cl::desc("Alias for --file-headers"),
55 cl::aliasopt(FileHeaders));
58 cl::opt<bool> Sections("sections",
59 cl::desc("Display all sections."));
60 cl::alias SectionsShort("s",
61 cl::desc("Alias for --sections"),
62 cl::aliasopt(Sections));
64 // -section-relocations, -sr
65 cl::opt<bool> SectionRelocations("section-relocations",
66 cl::desc("Display relocations for each section shown."));
67 cl::alias SectionRelocationsShort("sr",
68 cl::desc("Alias for --section-relocations"),
69 cl::aliasopt(SectionRelocations));
71 // -section-symbols, -st
72 cl::opt<bool> SectionSymbols("section-symbols",
73 cl::desc("Display symbols for each section shown."));
74 cl::alias SectionSymbolsShort("st",
75 cl::desc("Alias for --section-symbols"),
76 cl::aliasopt(SectionSymbols));
79 cl::opt<bool> SectionData("section-data",
80 cl::desc("Display section data for each section shown."));
81 cl::alias SectionDataShort("sd",
82 cl::desc("Alias for --section-data"),
83 cl::aliasopt(SectionData));
86 cl::opt<bool> Relocations("relocations",
87 cl::desc("Display the relocation entries in the file"));
88 cl::alias RelocationsShort("r",
89 cl::desc("Alias for --relocations"),
90 cl::aliasopt(Relocations));
93 cl::opt<bool> Symbols("symbols",
94 cl::desc("Display the symbol table"));
95 cl::alias SymbolsShort("t",
96 cl::desc("Alias for --symbols"),
97 cl::aliasopt(Symbols));
100 cl::opt<bool> DynamicSymbols("dyn-symbols",
101 cl::desc("Display the dynamic symbol table"));
102 cl::alias DynamicSymbolsShort("dt",
103 cl::desc("Alias for --dyn-symbols"),
104 cl::aliasopt(DynamicSymbols));
107 cl::opt<bool> UnwindInfo("unwind",
108 cl::desc("Display unwind information"));
109 cl::alias UnwindInfoShort("u",
110 cl::desc("Alias for --unwind"),
111 cl::aliasopt(UnwindInfo));
114 cl::opt<bool> DynamicTable("dynamic-table",
115 cl::desc("Display the ELF .dynamic section table"));
118 cl::opt<bool> NeededLibraries("needed-libs",
119 cl::desc("Display the needed libraries"));
122 cl::opt<bool> ProgramHeaders("program-headers",
123 cl::desc("Display ELF program headers"));
126 cl::opt<bool> ExpandRelocs("expand-relocs",
127 cl::desc("Expand each shown relocation to multiple lines"));
129 // -codeview-linetables
130 cl::opt<bool> CodeViewLineTables("codeview-linetables",
131 cl::desc("Display CodeView line table information"));
134 static int ReturnValue = EXIT_SUCCESS;
138 bool error(error_code EC) {
142 ReturnValue = EXIT_FAILURE;
143 outs() << "\nError reading file: " << EC.message() << ".\n";
148 bool relocAddressLess(RelocationRef a, RelocationRef b) {
149 uint64_t a_addr, b_addr;
150 if (error(a.getOffset(a_addr))) return false;
151 if (error(b.getOffset(b_addr))) return false;
152 return a_addr < b_addr;
158 static void reportError(StringRef Input, error_code EC) {
162 errs() << Input << ": " << EC.message() << "\n";
164 ReturnValue = EXIT_FAILURE;
167 static void reportError(StringRef Input, StringRef Message) {
171 errs() << Input << ": " << Message << "\n";
172 ReturnValue = EXIT_FAILURE;
175 /// @brief Creates an format-specific object file dumper.
176 static error_code createDumper(const ObjectFile *Obj,
177 StreamWriter &Writer,
178 OwningPtr<ObjDumper> &Result) {
180 return readobj_error::unsupported_file_format;
183 return createCOFFDumper(Obj, Writer, Result);
185 return createELFDumper(Obj, Writer, Result);
187 return createMachODumper(Obj, Writer, Result);
189 return readobj_error::unsupported_obj_file_format;
193 /// @brief Dumps the specified object file.
194 static void dumpObject(const ObjectFile *Obj) {
195 StreamWriter Writer(outs());
196 OwningPtr<ObjDumper> Dumper;
197 if (error_code EC = createDumper(Obj, Writer, Dumper)) {
198 reportError(Obj->getFileName(), EC);
203 outs() << "File: " << Obj->getFileName() << "\n";
204 outs() << "Format: " << Obj->getFileFormatName() << "\n";
206 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
208 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
210 outs() << "LoadName: " << Obj->getLoadName() << "\n";
212 if (opts::FileHeaders)
213 Dumper->printFileHeaders();
215 Dumper->printSections();
216 if (opts::Relocations)
217 Dumper->printRelocations();
219 Dumper->printSymbols();
220 if (opts::DynamicSymbols)
221 Dumper->printDynamicSymbols();
222 if (opts::UnwindInfo)
223 Dumper->printUnwindInfo();
224 if (opts::DynamicTable)
225 Dumper->printDynamicTable();
226 if (opts::NeededLibraries)
227 Dumper->printNeededLibraries();
228 if (opts::ProgramHeaders)
229 Dumper->printProgramHeaders();
233 /// @brief Dumps each object file in \a Arc;
234 static void dumpArchive(const Archive *Arc) {
235 for (Archive::child_iterator ArcI = Arc->begin_children(),
236 ArcE = Arc->end_children();
237 ArcI != ArcE; ++ArcI) {
238 OwningPtr<Binary> child;
239 if (error_code EC = ArcI->getAsBinary(child)) {
240 // Ignore non-object files.
241 if (EC != object_error::invalid_file_type)
242 reportError(Arc->getFileName(), EC.message());
246 if (ObjectFile *Obj = dyn_cast<ObjectFile>(child.get()))
249 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
254 /// @brief Opens \a File and dumps it.
255 static void dumpInput(StringRef File) {
256 // If file isn't stdin, check that it exists.
257 if (File != "-" && !sys::fs::exists(File)) {
258 reportError(File, readobj_error::file_not_found);
262 // Attempt to open the binary.
263 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
264 if (error_code EC = BinaryOrErr.getError()) {
265 reportError(File, EC);
268 OwningPtr<Binary> Binary(BinaryOrErr.get());
270 if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
272 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
275 reportError(File, readobj_error::unrecognized_file_format);
279 int main(int argc, const char *argv[]) {
280 sys::PrintStackTraceOnErrorSignal();
281 PrettyStackTraceProgram X(argc, argv);
284 // Initialize targets.
285 llvm::InitializeAllTargetInfos();
287 // Register the target printer for --version.
288 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
290 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
292 // Default to stdin if no filename is specified.
293 if (opts::InputFilenames.size() == 0)
294 opts::InputFilenames.push_back("-");
296 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),