blob: 8d2a997a2312aeba417aca09bbef49a8490639da [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"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/Object/ObjectFile.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000028#include "llvm/Support/Casting.h"
David Meyer2fc34c52012-03-01 01:36:50 +000029#include "llvm/Support/CommandLine.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000030#include "llvm/Support/DataTypes.h"
David Meyer2fc34c52012-03-01 01:36:50 +000031#include "llvm/Support/Debug.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000032#include "llvm/Support/FileSystem.h"
33#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Support/PrettyStackTrace.h"
35#include "llvm/Support/Signals.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000036#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000038#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000039#include <system_error>
Eric Christopher9cad53c2013-04-03 18:31:38 +000040
David Meyer2fc34c52012-03-01 01:36:50 +000041
42using namespace llvm;
43using namespace llvm::object;
44
Eric Christopher9cad53c2013-04-03 18:31:38 +000045namespace opts {
46 cl::list<std::string> InputFilenames(cl::Positional,
47 cl::desc("<input object files>"),
48 cl::ZeroOrMore);
David Meyer2fc34c52012-03-01 01:36:50 +000049
Eric Christopher9cad53c2013-04-03 18:31:38 +000050 // -file-headers, -h
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));
56
57 // -sections, -s
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));
63
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));
70
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));
77
78 // -section-data, -sd
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));
84
85 // -relocations, -r
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));
91
92 // -symbols, -t
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));
98
99 // -dyn-symbols, -dt
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));
105
106 // -unwind, -u
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));
112
113 // -dynamic-table
114 cl::opt<bool> DynamicTable("dynamic-table",
115 cl::desc("Display the ELF .dynamic section table"));
116
117 // -needed-libs
118 cl::opt<bool> NeededLibraries("needed-libs",
119 cl::desc("Display the needed libraries"));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000120
Nico Rieckd6df0542013-04-12 04:07:39 +0000121 // -program-headers
122 cl::opt<bool> ProgramHeaders("program-headers",
123 cl::desc("Display ELF program headers"));
124
Nico Rieckf3f0b792013-04-12 04:01:52 +0000125 // -expand-relocs
126 cl::opt<bool> ExpandRelocs("expand-relocs",
127 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000128
129 // -codeview-linetables
130 cl::opt<bool> CodeViewLineTables("codeview-linetables",
131 cl::desc("Display CodeView line table information"));
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000132
133 // -arm-attributes, -a
134 cl::opt<bool> ARMAttributes("arm-attributes",
135 cl::desc("Display the ARM attributes section"));
136 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
137 cl::aliasopt(ARMAttributes));
Simon Atanasyan80433902014-06-18 08:47:09 +0000138
139 // -mips-plt-got
140 cl::opt<bool>
141 MipsPLTGOT("mips-plt-got",
142 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000143} // namespace opts
144
Michael J. Spencer126973b2013-08-08 22:27:13 +0000145static int ReturnValue = EXIT_SUCCESS;
146
Eric Christopher9cad53c2013-04-03 18:31:38 +0000147namespace llvm {
148
Rafael Espindola4453e42942014-06-13 03:07:50 +0000149bool error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000150 if (!EC)
151 return false;
152
Michael J. Spencer126973b2013-08-08 22:27:13 +0000153 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000154 outs() << "\nError reading file: " << EC.message() << ".\n";
155 outs().flush();
156 return true;
Rafael Espindola144af2c2012-12-31 16:05:21 +0000157}
158
Eric Christopher9cad53c2013-04-03 18:31:38 +0000159bool relocAddressLess(RelocationRef a, RelocationRef b) {
160 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000161 if (error(a.getOffset(a_addr))) return false;
162 if (error(b.getOffset(b_addr))) return false;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000163 return a_addr < b_addr;
164}
165
166} // namespace llvm
167
Rafael Espindola4453e42942014-06-13 03:07:50 +0000168static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000169 if (Input == "-")
170 Input = "<stdin>";
171
172 errs() << Input << ": " << EC.message() << "\n";
173 errs().flush();
Michael J. Spencer126973b2013-08-08 22:27:13 +0000174 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000175}
176
177static void reportError(StringRef Input, StringRef Message) {
178 if (Input == "-")
179 Input = "<stdin>";
180
181 errs() << Input << ": " << Message << "\n";
Michael J. Spencer126973b2013-08-08 22:27:13 +0000182 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000183}
184
Simon Atanasyan80433902014-06-18 08:47:09 +0000185static bool isMipsArch(unsigned Arch) {
186 switch (Arch) {
187 case llvm::Triple::mips:
188 case llvm::Triple::mipsel:
189 case llvm::Triple::mips64:
190 case llvm::Triple::mips64el:
191 return true;
192 default:
193 return false;
194 }
195}
196
Eric Christopher9cad53c2013-04-03 18:31:38 +0000197/// @brief Creates an format-specific object file dumper.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000198static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
199 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000200 if (!Obj)
201 return readobj_error::unsupported_file_format;
202
203 if (Obj->isCOFF())
204 return createCOFFDumper(Obj, Writer, Result);
205 if (Obj->isELF())
206 return createELFDumper(Obj, Writer, Result);
207 if (Obj->isMachO())
208 return createMachODumper(Obj, Writer, Result);
209
210 return readobj_error::unsupported_obj_file_format;
211}
212
213
214/// @brief Dumps the specified object file.
215static void dumpObject(const ObjectFile *Obj) {
216 StreamWriter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000217 std::unique_ptr<ObjDumper> Dumper;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000218 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000219 reportError(Obj->getFileName(), EC);
220 return;
221 }
222
223 outs() << '\n';
224 outs() << "File: " << Obj->getFileName() << "\n";
225 outs() << "Format: " << Obj->getFileFormatName() << "\n";
226 outs() << "Arch: "
227 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
Rafael Espindola21bd8412012-12-31 16:29:44 +0000228 << "\n";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000229 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
230 if (Obj->isELF())
231 outs() << "LoadName: " << Obj->getLoadName() << "\n";
232
233 if (opts::FileHeaders)
234 Dumper->printFileHeaders();
235 if (opts::Sections)
236 Dumper->printSections();
237 if (opts::Relocations)
238 Dumper->printRelocations();
239 if (opts::Symbols)
240 Dumper->printSymbols();
241 if (opts::DynamicSymbols)
242 Dumper->printDynamicSymbols();
243 if (opts::UnwindInfo)
244 Dumper->printUnwindInfo();
245 if (opts::DynamicTable)
246 Dumper->printDynamicTable();
247 if (opts::NeededLibraries)
248 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000249 if (opts::ProgramHeaders)
250 Dumper->printProgramHeaders();
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000251 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
252 if (opts::ARMAttributes)
253 Dumper->printAttributes();
Simon Atanasyan80433902014-06-18 08:47:09 +0000254 if (isMipsArch(Obj->getArch()) && Obj->isELF())
255 if (opts::MipsPLTGOT)
256 Dumper->printMipsPLTGOT();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000257}
258
David Meyer2fc34c52012-03-01 01:36:50 +0000259
Eric Christopher9cad53c2013-04-03 18:31:38 +0000260/// @brief Dumps each object file in \a Arc;
261static void dumpArchive(const Archive *Arc) {
Rafael Espindola23a97502014-01-21 16:09:45 +0000262 for (Archive::child_iterator ArcI = Arc->child_begin(),
263 ArcE = Arc->child_end();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000264 ArcI != ArcE; ++ArcI) {
Rafael Espindolaae460022014-06-16 16:08:36 +0000265 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
266 if (std::error_code EC = ChildOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000267 // Ignore non-object files.
268 if (EC != object_error::invalid_file_type)
269 reportError(Arc->getFileName(), EC.message());
270 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000271 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000272
Rafael Espindolaae460022014-06-16 16:08:36 +0000273 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000274 dumpObject(Obj);
275 else
276 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
277 }
278}
279
280
281/// @brief Opens \a File and dumps it.
282static void dumpInput(StringRef File) {
283 // If file isn't stdin, check that it exists.
284 if (File != "-" && !sys::fs::exists(File)) {
285 reportError(File, readobj_error::file_not_found);
286 return;
Rafael Espindolaae740952012-12-31 15:30:58 +0000287 }
David Meyer2fc34c52012-03-01 01:36:50 +0000288
Eric Christopher9cad53c2013-04-03 18:31:38 +0000289 // Attempt to open the binary.
Rafael Espindola63da2952014-01-15 19:37:43 +0000290 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000291 if (std::error_code EC = BinaryOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000292 reportError(File, EC);
293 return;
Rafael Espindola21bd8412012-12-31 16:29:44 +0000294 }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000295 std::unique_ptr<Binary> Binary(BinaryOrErr.get());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000296
297 if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
298 dumpArchive(Arc);
299 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
300 dumpObject(Obj);
301 else
302 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000303}
304
David Meyerc429b802012-03-01 22:19:54 +0000305
Eric Christopher9cad53c2013-04-03 18:31:38 +0000306int main(int argc, const char *argv[]) {
David Meyer2fc34c52012-03-01 01:36:50 +0000307 sys::PrintStackTraceOnErrorSignal();
308 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000309 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000310
Eric Christopher9cad53c2013-04-03 18:31:38 +0000311 // Initialize targets.
312 llvm::InitializeAllTargetInfos();
David Meyer2fc34c52012-03-01 01:36:50 +0000313
Eric Christopher9cad53c2013-04-03 18:31:38 +0000314 // Register the target printer for --version.
315 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000316
Eric Christopher9cad53c2013-04-03 18:31:38 +0000317 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000318
Eric Christopher9cad53c2013-04-03 18:31:38 +0000319 // Default to stdin if no filename is specified.
320 if (opts::InputFilenames.size() == 0)
321 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000322
Eric Christopher9cad53c2013-04-03 18:31:38 +0000323 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
324 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000325
Michael J. Spencer126973b2013-08-08 22:27:13 +0000326 return ReturnValue;
David Meyer2fc34c52012-03-01 01:36:50 +0000327}