blob: 6d05658c07c8d1a3c865b2c433b35a744c675bc1 [file] [log] [blame]
Benjamin Kramer054f4222013-08-09 10:31:14 +00001//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
David Meyer2fc34c52012-03-01 01:36:50 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Michael J. Spencerd7e70032013-02-05 20:27:22 +000010// 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
12// for FileCheck.
David Meyerae11a782012-03-02 23:43:51 +000013//
Michael J. Spencerd7e70032013-02-05 20:27:22 +000014// 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.
David Meyerae11a782012-03-02 23:43:51 +000017//
Michael J. Spencerd7e70032013-02-05 20:27:22 +000018// Output should be specialized for each format where appropriate.
David Meyerae11a782012-03-02 23:43:51 +000019//
20//===----------------------------------------------------------------------===//
David Meyer2fc34c52012-03-01 01:36:50 +000021
Michael J. Spencer6a8746b2013-02-20 02:37:12 +000022#include "llvm-readobj.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000023#include "Error.h"
24#include "ObjDumper.h"
25#include "StreamWriter.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000026#include "llvm/Object/Archive.h"
Rafael Espindola67622312014-08-08 16:39:22 +000027#include "llvm/Object/ELFObjectFile.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000028#include "llvm/Object/ObjectFile.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000029#include "llvm/Support/Casting.h"
David Meyer2fc34c52012-03-01 01:36:50 +000030#include "llvm/Support/CommandLine.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000031#include "llvm/Support/DataTypes.h"
David Meyer2fc34c52012-03-01 01:36:50 +000032#include "llvm/Support/Debug.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000033#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000035#include "llvm/Support/PrettyStackTrace.h"
36#include "llvm/Support/Signals.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000037#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000039#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000040#include <system_error>
Eric Christopher9cad53c2013-04-03 18:31:38 +000041
David Meyer2fc34c52012-03-01 01:36:50 +000042
43using namespace llvm;
44using namespace llvm::object;
45
Eric Christopher9cad53c2013-04-03 18:31:38 +000046namespace opts {
47 cl::list<std::string> InputFilenames(cl::Positional,
48 cl::desc("<input object files>"),
49 cl::ZeroOrMore);
David Meyer2fc34c52012-03-01 01:36:50 +000050
Eric Christopher9cad53c2013-04-03 18:31:38 +000051 // -file-headers, -h
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));
57
58 // -sections, -s
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));
64
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));
71
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));
78
79 // -section-data, -sd
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));
85
86 // -relocations, -r
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));
92
93 // -symbols, -t
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));
99
100 // -dyn-symbols, -dt
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));
106
107 // -unwind, -u
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));
113
114 // -dynamic-table
115 cl::opt<bool> DynamicTable("dynamic-table",
116 cl::desc("Display the ELF .dynamic section table"));
117
118 // -needed-libs
119 cl::opt<bool> NeededLibraries("needed-libs",
120 cl::desc("Display the needed libraries"));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000121
Nico Rieckd6df0542013-04-12 04:07:39 +0000122 // -program-headers
123 cl::opt<bool> ProgramHeaders("program-headers",
124 cl::desc("Display ELF program headers"));
125
Nico Rieckf3f0b792013-04-12 04:01:52 +0000126 // -expand-relocs
127 cl::opt<bool> ExpandRelocs("expand-relocs",
128 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000129
130 // -codeview-linetables
131 cl::opt<bool> CodeViewLineTables("codeview-linetables",
132 cl::desc("Display CodeView line table information"));
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000133
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));
Simon Atanasyan80433902014-06-18 08:47:09 +0000139
140 // -mips-plt-got
141 cl::opt<bool>
142 MipsPLTGOT("mips-plt-got",
143 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Rui Ueyama1e152d52014-10-02 17:02:18 +0000144
145 // -coff-imports
146 cl::opt<bool>
147 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000148
149 // -coff-directives
150 cl::opt<bool>
151 COFFDirectives("coff-directives",
152 cl::desc("Display the contents PE/COFF .drectve section"));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000153} // namespace opts
154
Michael J. Spencer126973b2013-08-08 22:27:13 +0000155static int ReturnValue = EXIT_SUCCESS;
156
Eric Christopher9cad53c2013-04-03 18:31:38 +0000157namespace llvm {
158
Rafael Espindola4453e42942014-06-13 03:07:50 +0000159bool error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000160 if (!EC)
161 return false;
162
Michael J. Spencer126973b2013-08-08 22:27:13 +0000163 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000164 outs() << "\nError reading file: " << EC.message() << ".\n";
165 outs().flush();
166 return true;
Rafael Espindola144af2c2012-12-31 16:05:21 +0000167}
168
Eric Christopher9cad53c2013-04-03 18:31:38 +0000169bool relocAddressLess(RelocationRef a, RelocationRef b) {
170 uint64_t a_addr, b_addr;
David Majnemere0eb0862014-11-13 07:54:05 +0000171 if (error(a.getOffset(a_addr))) exit(ReturnValue);
172 if (error(b.getOffset(b_addr))) exit(ReturnValue);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000173 return a_addr < b_addr;
174}
175
176} // namespace llvm
177
Rafael Espindola4453e42942014-06-13 03:07:50 +0000178static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000179 if (Input == "-")
180 Input = "<stdin>";
181
182 errs() << Input << ": " << EC.message() << "\n";
183 errs().flush();
Michael J. Spencer126973b2013-08-08 22:27:13 +0000184 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000185}
186
187static void reportError(StringRef Input, StringRef Message) {
188 if (Input == "-")
189 Input = "<stdin>";
190
191 errs() << Input << ": " << Message << "\n";
Michael J. Spencer126973b2013-08-08 22:27:13 +0000192 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000193}
194
Simon Atanasyan80433902014-06-18 08:47:09 +0000195static bool isMipsArch(unsigned Arch) {
196 switch (Arch) {
197 case llvm::Triple::mips:
198 case llvm::Triple::mipsel:
199 case llvm::Triple::mips64:
200 case llvm::Triple::mips64el:
201 return true;
202 default:
203 return false;
204 }
205}
206
Eric Christopher9cad53c2013-04-03 18:31:38 +0000207/// @brief Creates an format-specific object file dumper.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000208static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
209 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000210 if (!Obj)
211 return readobj_error::unsupported_file_format;
212
213 if (Obj->isCOFF())
214 return createCOFFDumper(Obj, Writer, Result);
215 if (Obj->isELF())
216 return createELFDumper(Obj, Writer, Result);
217 if (Obj->isMachO())
218 return createMachODumper(Obj, Writer, Result);
219
220 return readobj_error::unsupported_obj_file_format;
221}
222
Rafael Espindola67622312014-08-08 16:39:22 +0000223static StringRef getLoadName(const ObjectFile *Obj) {
224 if (auto *ELF = dyn_cast<ELF32LEObjectFile>(Obj))
225 return ELF->getLoadName();
226 if (auto *ELF = dyn_cast<ELF64LEObjectFile>(Obj))
227 return ELF->getLoadName();
228 if (auto *ELF = dyn_cast<ELF32BEObjectFile>(Obj))
229 return ELF->getLoadName();
230 if (auto *ELF = dyn_cast<ELF64BEObjectFile>(Obj))
231 return ELF->getLoadName();
232 llvm_unreachable("Not ELF");
233}
Eric Christopher9cad53c2013-04-03 18:31:38 +0000234
235/// @brief Dumps the specified object file.
236static void dumpObject(const ObjectFile *Obj) {
237 StreamWriter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000238 std::unique_ptr<ObjDumper> Dumper;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000239 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000240 reportError(Obj->getFileName(), EC);
241 return;
242 }
243
244 outs() << '\n';
245 outs() << "File: " << Obj->getFileName() << "\n";
246 outs() << "Format: " << Obj->getFileFormatName() << "\n";
247 outs() << "Arch: "
248 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
Rafael Espindola21bd8412012-12-31 16:29:44 +0000249 << "\n";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000250 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
251 if (Obj->isELF())
Rafael Espindola67622312014-08-08 16:39:22 +0000252 outs() << "LoadName: " << getLoadName(Obj) << "\n";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000253
254 if (opts::FileHeaders)
255 Dumper->printFileHeaders();
256 if (opts::Sections)
257 Dumper->printSections();
258 if (opts::Relocations)
259 Dumper->printRelocations();
260 if (opts::Symbols)
261 Dumper->printSymbols();
262 if (opts::DynamicSymbols)
263 Dumper->printDynamicSymbols();
264 if (opts::UnwindInfo)
265 Dumper->printUnwindInfo();
266 if (opts::DynamicTable)
267 Dumper->printDynamicTable();
268 if (opts::NeededLibraries)
269 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000270 if (opts::ProgramHeaders)
271 Dumper->printProgramHeaders();
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000272 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
273 if (opts::ARMAttributes)
274 Dumper->printAttributes();
Simon Atanasyan80433902014-06-18 08:47:09 +0000275 if (isMipsArch(Obj->getArch()) && Obj->isELF())
276 if (opts::MipsPLTGOT)
277 Dumper->printMipsPLTGOT();
Rui Ueyama1e152d52014-10-02 17:02:18 +0000278 if (opts::COFFImports)
279 Dumper->printCOFFImports();
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000280 if (opts::COFFDirectives)
281 Dumper->printCOFFDirectives();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000282}
283
David Meyer2fc34c52012-03-01 01:36:50 +0000284
Eric Christopher9cad53c2013-04-03 18:31:38 +0000285/// @brief Dumps each object file in \a Arc;
286static void dumpArchive(const Archive *Arc) {
Rafael Espindola23a97502014-01-21 16:09:45 +0000287 for (Archive::child_iterator ArcI = Arc->child_begin(),
288 ArcE = Arc->child_end();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000289 ArcI != ArcE; ++ArcI) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000290 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
291 if (std::error_code EC = ChildOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000292 // Ignore non-object files.
293 if (EC != object_error::invalid_file_type)
294 reportError(Arc->getFileName(), EC.message());
295 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000296 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000297
Rafael Espindolaae460022014-06-16 16:08:36 +0000298 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000299 dumpObject(Obj);
300 else
301 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
302 }
303}
304
305
306/// @brief Opens \a File and dumps it.
307static void dumpInput(StringRef File) {
308 // If file isn't stdin, check that it exists.
309 if (File != "-" && !sys::fs::exists(File)) {
310 reportError(File, readobj_error::file_not_found);
311 return;
Rafael Espindolaae740952012-12-31 15:30:58 +0000312 }
David Meyer2fc34c52012-03-01 01:36:50 +0000313
Eric Christopher9cad53c2013-04-03 18:31:38 +0000314 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000315 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000316 if (std::error_code EC = BinaryOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000317 reportError(File, EC);
318 return;
Rafael Espindola21bd8412012-12-31 16:29:44 +0000319 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000320 Binary &Binary = *BinaryOrErr.get().getBinary();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000321
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000322 if (Archive *Arc = dyn_cast<Archive>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000323 dumpArchive(Arc);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000324 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000325 dumpObject(Obj);
326 else
327 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000328}
329
David Meyerc429b802012-03-01 22:19:54 +0000330
Eric Christopher9cad53c2013-04-03 18:31:38 +0000331int main(int argc, const char *argv[]) {
David Meyer2fc34c52012-03-01 01:36:50 +0000332 sys::PrintStackTraceOnErrorSignal();
333 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000334 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000335
Eric Christopher9cad53c2013-04-03 18:31:38 +0000336 // Initialize targets.
337 llvm::InitializeAllTargetInfos();
David Meyer2fc34c52012-03-01 01:36:50 +0000338
Eric Christopher9cad53c2013-04-03 18:31:38 +0000339 // Register the target printer for --version.
340 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000341
Eric Christopher9cad53c2013-04-03 18:31:38 +0000342 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000343
Eric Christopher9cad53c2013-04-03 18:31:38 +0000344 // Default to stdin if no filename is specified.
345 if (opts::InputFilenames.size() == 0)
346 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000347
Eric Christopher9cad53c2013-04-03 18:31:38 +0000348 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
349 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000350
Michael J. Spencer126973b2013-08-08 22:27:13 +0000351 return ReturnValue;
David Meyer2fc34c52012-03-01 01:36:50 +0000352}