blob: d41dcc3ab4f129d736942593a28b7e33bbbfa50a [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"
Rafael Espindola8448a242015-03-24 20:26:55 +000028#include "llvm/Object/MachOUniversal.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Object/ObjectFile.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000030#include "llvm/Support/Casting.h"
David Meyer2fc34c52012-03-01 01:36:50 +000031#include "llvm/Support/CommandLine.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000032#include "llvm/Support/DataTypes.h"
David Meyer2fc34c52012-03-01 01:36:50 +000033#include "llvm/Support/Debug.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000034#include "llvm/Support/FileSystem.h"
35#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000036#include "llvm/Support/PrettyStackTrace.h"
37#include "llvm/Support/Signals.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000040#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000041#include <system_error>
Eric Christopher9cad53c2013-04-03 18:31:38 +000042
David Meyer2fc34c52012-03-01 01:36:50 +000043using 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
Michael J. Spencer594c0282015-06-25 21:47:32 +000093 // -dyn-relocations
94 cl::opt<bool> DynRelocs("dyn-relocations",
95 cl::desc("Display the dynamic relocation entries in the file"));
96
Eric Christopher9cad53c2013-04-03 18:31:38 +000097 // -symbols, -t
98 cl::opt<bool> Symbols("symbols",
99 cl::desc("Display the symbol table"));
100 cl::alias SymbolsShort("t",
101 cl::desc("Alias for --symbols"),
102 cl::aliasopt(Symbols));
103
104 // -dyn-symbols, -dt
105 cl::opt<bool> DynamicSymbols("dyn-symbols",
106 cl::desc("Display the dynamic symbol table"));
107 cl::alias DynamicSymbolsShort("dt",
108 cl::desc("Alias for --dyn-symbols"),
109 cl::aliasopt(DynamicSymbols));
110
111 // -unwind, -u
112 cl::opt<bool> UnwindInfo("unwind",
113 cl::desc("Display unwind information"));
114 cl::alias UnwindInfoShort("u",
115 cl::desc("Alias for --unwind"),
116 cl::aliasopt(UnwindInfo));
117
118 // -dynamic-table
119 cl::opt<bool> DynamicTable("dynamic-table",
120 cl::desc("Display the ELF .dynamic section table"));
121
122 // -needed-libs
123 cl::opt<bool> NeededLibraries("needed-libs",
124 cl::desc("Display the needed libraries"));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000125
Nico Rieckd6df0542013-04-12 04:07:39 +0000126 // -program-headers
127 cl::opt<bool> ProgramHeaders("program-headers",
128 cl::desc("Display ELF program headers"));
129
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000130 // -hash-table
131 cl::opt<bool> HashTable("hash-table",
132 cl::desc("Display ELF hash table"));
133
Nico Rieckf3f0b792013-04-12 04:01:52 +0000134 // -expand-relocs
135 cl::opt<bool> ExpandRelocs("expand-relocs",
136 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000137
Zachary Turner99f02152015-02-18 19:32:05 +0000138 // -codeview
139 cl::opt<bool> CodeView("codeview",
140 cl::desc("Display CodeView debug information"));
141
142 // -codeview-subsection-bytes
143 cl::opt<bool> CodeViewSubsectionBytes(
144 "codeview-subsection-bytes",
145 cl::desc("Dump raw contents of codeview debug sections and records"));
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000146
147 // -arm-attributes, -a
148 cl::opt<bool> ARMAttributes("arm-attributes",
149 cl::desc("Display the ARM attributes section"));
150 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
151 cl::aliasopt(ARMAttributes));
Simon Atanasyan80433902014-06-18 08:47:09 +0000152
153 // -mips-plt-got
154 cl::opt<bool>
155 MipsPLTGOT("mips-plt-got",
156 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Rui Ueyama1e152d52014-10-02 17:02:18 +0000157
Simon Atanasyanc914de22015-05-07 15:40:35 +0000158 // -mips-abi-flags
159 cl::opt<bool> MipsABIFlags("mips-abi-flags",
160 cl::desc("Display the MIPS.abiflags section"));
161
Simon Atanasyan6e07e932015-06-16 21:47:43 +0000162 // -mips-reginfo
163 cl::opt<bool> MipsReginfo("mips-reginfo",
164 cl::desc("Display the MIPS .reginfo section"));
165
Rui Ueyama1e152d52014-10-02 17:02:18 +0000166 // -coff-imports
167 cl::opt<bool>
168 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000169
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +0000170 // -coff-exports
171 cl::opt<bool>
172 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
173
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000174 // -coff-directives
175 cl::opt<bool>
176 COFFDirectives("coff-directives",
Rui Ueyama74e85132014-11-19 00:18:07 +0000177 cl::desc("Display the PE/COFF .drectve section"));
178
179 // -coff-basereloc
180 cl::opt<bool>
181 COFFBaseRelocs("coff-basereloc",
182 cl::desc("Display the PE/COFF .reloc section"));
Lang Hames0000afd2015-06-26 23:56:53 +0000183
Davide Italiano07e7acb2015-08-21 20:28:30 +0000184 // -macho-data-in-code
185 cl::opt<bool>
186 MachODataInCode("macho-data-in-code",
187 cl::desc("Display MachO Data in Code command"));
188
Davide Italiano976f4da2015-08-27 15:11:32 +0000189 // -macho-version-min
190 cl::opt<bool>
191 MachOVersionMin("macho-version-min",
192 cl::desc("Display MachO version min command"));
Lang Hames0000afd2015-06-26 23:56:53 +0000193 // -stackmap
194 cl::opt<bool>
195 PrintStackMap("stackmap",
196 cl::desc("Display contents of stackmap section"));
197
Eric Christopher9cad53c2013-04-03 18:31:38 +0000198} // namespace opts
199
Rafael Espindola00ddb142015-07-20 03:38:17 +0000200namespace llvm {
201
202void reportError(Twine Msg) {
Rafael Espindola8b3b09f2015-08-06 21:54:37 +0000203 outs() << "\nError reading file: " << Msg << ".\n";
Rafael Espindola724d4b42015-07-20 03:01:49 +0000204 outs().flush();
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000205 exit(1);
Rafael Espindola724d4b42015-07-20 03:01:49 +0000206}
207
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000208void error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000209 if (!EC)
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000210 return;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000211
Rafael Espindola8b3b09f2015-08-06 21:54:37 +0000212 reportError(EC.message());
Rafael Espindola144af2c2012-12-31 16:05:21 +0000213}
214
Eric Christopher9cad53c2013-04-03 18:31:38 +0000215bool relocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindolac7689302015-07-06 15:53:43 +0000216 return a.getOffset() < b.getOffset();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000217}
218
219} // namespace llvm
220
Rafael Espindola4453e42942014-06-13 03:07:50 +0000221static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000222 if (Input == "-")
223 Input = "<stdin>";
224
Rafael Espindola724d4b42015-07-20 03:01:49 +0000225 reportError(Twine(Input) + ": " + EC.message());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000226}
227
228static void reportError(StringRef Input, StringRef Message) {
229 if (Input == "-")
230 Input = "<stdin>";
231
Rafael Espindola724d4b42015-07-20 03:01:49 +0000232 reportError(Twine(Input) + ": " + Message);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000233}
234
Simon Atanasyan80433902014-06-18 08:47:09 +0000235static bool isMipsArch(unsigned Arch) {
236 switch (Arch) {
237 case llvm::Triple::mips:
238 case llvm::Triple::mipsel:
239 case llvm::Triple::mips64:
240 case llvm::Triple::mips64el:
241 return true;
242 default:
243 return false;
244 }
245}
246
Eric Christopher9cad53c2013-04-03 18:31:38 +0000247/// @brief Creates an format-specific object file dumper.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000248static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
249 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000250 if (!Obj)
251 return readobj_error::unsupported_file_format;
252
253 if (Obj->isCOFF())
254 return createCOFFDumper(Obj, Writer, Result);
255 if (Obj->isELF())
256 return createELFDumper(Obj, Writer, Result);
257 if (Obj->isMachO())
258 return createMachODumper(Obj, Writer, Result);
259
260 return readobj_error::unsupported_obj_file_format;
261}
262
Eric Christopher9cad53c2013-04-03 18:31:38 +0000263/// @brief Dumps the specified object file.
264static void dumpObject(const ObjectFile *Obj) {
265 StreamWriter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000266 std::unique_ptr<ObjDumper> Dumper;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000267 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000268 reportError(Obj->getFileName(), EC);
269 return;
270 }
271
272 outs() << '\n';
273 outs() << "File: " << Obj->getFileName() << "\n";
274 outs() << "Format: " << Obj->getFileFormatName() << "\n";
275 outs() << "Arch: "
276 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
Rafael Espindola21bd8412012-12-31 16:29:44 +0000277 << "\n";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000278 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
Rafael Espindola33e746f22015-07-21 13:48:41 +0000279 Dumper->printLoadName();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000280
281 if (opts::FileHeaders)
282 Dumper->printFileHeaders();
283 if (opts::Sections)
284 Dumper->printSections();
285 if (opts::Relocations)
286 Dumper->printRelocations();
Michael J. Spencer594c0282015-06-25 21:47:32 +0000287 if (opts::DynRelocs)
288 Dumper->printDynamicRelocations();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000289 if (opts::Symbols)
290 Dumper->printSymbols();
291 if (opts::DynamicSymbols)
292 Dumper->printDynamicSymbols();
293 if (opts::UnwindInfo)
294 Dumper->printUnwindInfo();
295 if (opts::DynamicTable)
296 Dumper->printDynamicTable();
297 if (opts::NeededLibraries)
298 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000299 if (opts::ProgramHeaders)
300 Dumper->printProgramHeaders();
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000301 if (opts::HashTable)
302 Dumper->printHashTable();
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000303 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
304 if (opts::ARMAttributes)
305 Dumper->printAttributes();
Simon Atanasyanc914de22015-05-07 15:40:35 +0000306 if (isMipsArch(Obj->getArch()) && Obj->isELF()) {
Simon Atanasyan80433902014-06-18 08:47:09 +0000307 if (opts::MipsPLTGOT)
308 Dumper->printMipsPLTGOT();
Simon Atanasyanc914de22015-05-07 15:40:35 +0000309 if (opts::MipsABIFlags)
310 Dumper->printMipsABIFlags();
Simon Atanasyan6e07e932015-06-16 21:47:43 +0000311 if (opts::MipsReginfo)
312 Dumper->printMipsReginfo();
Simon Atanasyanc914de22015-05-07 15:40:35 +0000313 }
Davide Italianocd1b6db2015-07-24 02:14:20 +0000314 if (Obj->isCOFF()) {
315 if (opts::COFFImports)
316 Dumper->printCOFFImports();
317 if (opts::COFFExports)
318 Dumper->printCOFFExports();
319 if (opts::COFFDirectives)
320 Dumper->printCOFFDirectives();
321 if (opts::COFFBaseRelocs)
322 Dumper->printCOFFBaseReloc();
323 }
Davide Italiano07e7acb2015-08-21 20:28:30 +0000324 if (Obj->isMachO())
325 if (opts::MachODataInCode)
326 Dumper->printMachODataInCode();
Davide Italiano976f4da2015-08-27 15:11:32 +0000327 if (opts::MachOVersionMin)
328 Dumper->printMachOVersionMin();
Lang Hames0000afd2015-06-26 23:56:53 +0000329 if (opts::PrintStackMap)
330 Dumper->printStackMap();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000331}
332
Eric Christopher9cad53c2013-04-03 18:31:38 +0000333/// @brief Dumps each object file in \a Arc;
334static void dumpArchive(const Archive *Arc) {
Davide Italianodb8803b2015-08-07 00:35:28 +0000335 for (const auto &Child : Arc->children()) {
336 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
Rafael Espindolaae460022014-06-16 16:08:36 +0000337 if (std::error_code EC = ChildOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000338 // Ignore non-object files.
339 if (EC != object_error::invalid_file_type)
340 reportError(Arc->getFileName(), EC.message());
341 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000342 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000343
Rafael Espindolaae460022014-06-16 16:08:36 +0000344 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000345 dumpObject(Obj);
346 else
347 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
348 }
349}
350
Rafael Espindola8448a242015-03-24 20:26:55 +0000351/// @brief Dumps each object file in \a MachO Universal Binary;
352static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
353 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
354 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
David Blaikie29ac43c2015-04-13 16:05:49 +0000355 if (ObjOrErr)
356 dumpObject(&*ObjOrErr.get());
357 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
358 dumpArchive(&*AOrErr.get());
359 else
360 reportError(UBinary->getFileName(), ObjOrErr.getError().message());
Rafael Espindola8448a242015-03-24 20:26:55 +0000361 }
362}
Eric Christopher9cad53c2013-04-03 18:31:38 +0000363
364/// @brief Opens \a File and dumps it.
365static void dumpInput(StringRef File) {
366 // If file isn't stdin, check that it exists.
367 if (File != "-" && !sys::fs::exists(File)) {
368 reportError(File, readobj_error::file_not_found);
369 return;
Rafael Espindolaae740952012-12-31 15:30:58 +0000370 }
David Meyer2fc34c52012-03-01 01:36:50 +0000371
Eric Christopher9cad53c2013-04-03 18:31:38 +0000372 // Attempt to open the binary.
Rafael Espindola48af1c22014-08-19 18:44:46 +0000373 ErrorOr<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000374 if (std::error_code EC = BinaryOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000375 reportError(File, EC);
376 return;
Rafael Espindola21bd8412012-12-31 16:29:44 +0000377 }
Rafael Espindola48af1c22014-08-19 18:44:46 +0000378 Binary &Binary = *BinaryOrErr.get().getBinary();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000379
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000380 if (Archive *Arc = dyn_cast<Archive>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000381 dumpArchive(Arc);
Rafael Espindola8448a242015-03-24 20:26:55 +0000382 else if (MachOUniversalBinary *UBinary =
383 dyn_cast<MachOUniversalBinary>(&Binary))
384 dumpMachOUniversalBinary(UBinary);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000385 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000386 dumpObject(Obj);
387 else
388 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000389}
390
Eric Christopher9cad53c2013-04-03 18:31:38 +0000391int main(int argc, const char *argv[]) {
David Meyer2fc34c52012-03-01 01:36:50 +0000392 sys::PrintStackTraceOnErrorSignal();
393 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000394 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000395
Eric Christopher9cad53c2013-04-03 18:31:38 +0000396 // Register the target printer for --version.
397 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000398
Eric Christopher9cad53c2013-04-03 18:31:38 +0000399 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000400
Eric Christopher9cad53c2013-04-03 18:31:38 +0000401 // Default to stdin if no filename is specified.
402 if (opts::InputFilenames.size() == 0)
403 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000404
Eric Christopher9cad53c2013-04-03 18:31:38 +0000405 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
406 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000407
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000408 return 0;
David Meyer2fc34c52012-03-01 01:36:50 +0000409}