blob: d66c02f4130b4d0a04d8d7655c643a1ae7a39ad1 [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));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000138} // namespace opts
139
Michael J. Spencer126973b2013-08-08 22:27:13 +0000140static int ReturnValue = EXIT_SUCCESS;
141
Eric Christopher9cad53c2013-04-03 18:31:38 +0000142namespace llvm {
143
Rafael Espindola4453e42942014-06-13 03:07:50 +0000144bool error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000145 if (!EC)
146 return false;
147
Michael J. Spencer126973b2013-08-08 22:27:13 +0000148 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000149 outs() << "\nError reading file: " << EC.message() << ".\n";
150 outs().flush();
151 return true;
Rafael Espindola144af2c2012-12-31 16:05:21 +0000152}
153
Eric Christopher9cad53c2013-04-03 18:31:38 +0000154bool relocAddressLess(RelocationRef a, RelocationRef b) {
155 uint64_t a_addr, b_addr;
Rafael Espindola1e483872013-04-25 12:28:45 +0000156 if (error(a.getOffset(a_addr))) return false;
157 if (error(b.getOffset(b_addr))) return false;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000158 return a_addr < b_addr;
159}
160
161} // namespace llvm
162
Rafael Espindola4453e42942014-06-13 03:07:50 +0000163static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000164 if (Input == "-")
165 Input = "<stdin>";
166
167 errs() << Input << ": " << EC.message() << "\n";
168 errs().flush();
Michael J. Spencer126973b2013-08-08 22:27:13 +0000169 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000170}
171
172static void reportError(StringRef Input, StringRef Message) {
173 if (Input == "-")
174 Input = "<stdin>";
175
176 errs() << Input << ": " << Message << "\n";
Michael J. Spencer126973b2013-08-08 22:27:13 +0000177 ReturnValue = EXIT_FAILURE;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000178}
179
180/// @brief Creates an format-specific object file dumper.
Rafael Espindola4453e42942014-06-13 03:07:50 +0000181static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
182 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000183 if (!Obj)
184 return readobj_error::unsupported_file_format;
185
186 if (Obj->isCOFF())
187 return createCOFFDumper(Obj, Writer, Result);
188 if (Obj->isELF())
189 return createELFDumper(Obj, Writer, Result);
190 if (Obj->isMachO())
191 return createMachODumper(Obj, Writer, Result);
192
193 return readobj_error::unsupported_obj_file_format;
194}
195
196
197/// @brief Dumps the specified object file.
198static void dumpObject(const ObjectFile *Obj) {
199 StreamWriter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000200 std::unique_ptr<ObjDumper> Dumper;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000201 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000202 reportError(Obj->getFileName(), EC);
203 return;
204 }
205
206 outs() << '\n';
207 outs() << "File: " << Obj->getFileName() << "\n";
208 outs() << "Format: " << Obj->getFileFormatName() << "\n";
209 outs() << "Arch: "
210 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
Rafael Espindola21bd8412012-12-31 16:29:44 +0000211 << "\n";
Eric Christopher9cad53c2013-04-03 18:31:38 +0000212 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
213 if (Obj->isELF())
214 outs() << "LoadName: " << Obj->getLoadName() << "\n";
215
216 if (opts::FileHeaders)
217 Dumper->printFileHeaders();
218 if (opts::Sections)
219 Dumper->printSections();
220 if (opts::Relocations)
221 Dumper->printRelocations();
222 if (opts::Symbols)
223 Dumper->printSymbols();
224 if (opts::DynamicSymbols)
225 Dumper->printDynamicSymbols();
226 if (opts::UnwindInfo)
227 Dumper->printUnwindInfo();
228 if (opts::DynamicTable)
229 Dumper->printDynamicTable();
230 if (opts::NeededLibraries)
231 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000232 if (opts::ProgramHeaders)
233 Dumper->printProgramHeaders();
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000234 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
235 if (opts::ARMAttributes)
236 Dumper->printAttributes();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000237}
238
David Meyer2fc34c52012-03-01 01:36:50 +0000239
Eric Christopher9cad53c2013-04-03 18:31:38 +0000240/// @brief Dumps each object file in \a Arc;
241static void dumpArchive(const Archive *Arc) {
Rafael Espindola23a97502014-01-21 16:09:45 +0000242 for (Archive::child_iterator ArcI = Arc->child_begin(),
243 ArcE = Arc->child_end();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000244 ArcI != ArcE; ++ArcI) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000245 std::unique_ptr<Binary> child;
Rafael Espindola4453e42942014-06-13 03:07:50 +0000246 if (std::error_code EC = ArcI->getAsBinary(child)) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000247 // Ignore non-object files.
248 if (EC != object_error::invalid_file_type)
249 reportError(Arc->getFileName(), EC.message());
250 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000251 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000252
253 if (ObjectFile *Obj = dyn_cast<ObjectFile>(child.get()))
254 dumpObject(Obj);
255 else
256 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
257 }
258}
259
260
261/// @brief Opens \a File and dumps it.
262static void dumpInput(StringRef File) {
263 // If file isn't stdin, check that it exists.
264 if (File != "-" && !sys::fs::exists(File)) {
265 reportError(File, readobj_error::file_not_found);
266 return;
Rafael Espindolaae740952012-12-31 15:30:58 +0000267 }
David Meyer2fc34c52012-03-01 01:36:50 +0000268
Eric Christopher9cad53c2013-04-03 18:31:38 +0000269 // Attempt to open the binary.
Rafael Espindola63da2952014-01-15 19:37:43 +0000270 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
Rafael Espindola4453e42942014-06-13 03:07:50 +0000271 if (std::error_code EC = BinaryOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000272 reportError(File, EC);
273 return;
Rafael Espindola21bd8412012-12-31 16:29:44 +0000274 }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000275 std::unique_ptr<Binary> Binary(BinaryOrErr.get());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000276
277 if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
278 dumpArchive(Arc);
279 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
280 dumpObject(Obj);
281 else
282 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000283}
284
David Meyerc429b802012-03-01 22:19:54 +0000285
Eric Christopher9cad53c2013-04-03 18:31:38 +0000286int main(int argc, const char *argv[]) {
David Meyer2fc34c52012-03-01 01:36:50 +0000287 sys::PrintStackTraceOnErrorSignal();
288 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000289 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000290
Eric Christopher9cad53c2013-04-03 18:31:38 +0000291 // Initialize targets.
292 llvm::InitializeAllTargetInfos();
David Meyer2fc34c52012-03-01 01:36:50 +0000293
Eric Christopher9cad53c2013-04-03 18:31:38 +0000294 // Register the target printer for --version.
295 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000296
Eric Christopher9cad53c2013-04-03 18:31:38 +0000297 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000298
Eric Christopher9cad53c2013-04-03 18:31:38 +0000299 // Default to stdin if no filename is specified.
300 if (opts::InputFilenames.size() == 0)
301 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000302
Eric Christopher9cad53c2013-04-03 18:31:38 +0000303 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
304 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000305
Michael J. Spencer126973b2013-08-08 22:27:13 +0000306 return ReturnValue;
David Meyer2fc34c52012-03-01 01:36:50 +0000307}