blob: 67c9a98f40f3ae5c1ecb14047c8d8fce1dd80562 [file] [log] [blame]
David Meyeree37b6e2012-03-02 23:43:51 +00001//===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
David Meyer5c2b4ea2012-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. Spencerdd3aa9e2013-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 Meyeree37b6e2012-03-02 23:43:51 +000013//
Michael J. Spencerdd3aa9e2013-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 Meyeree37b6e2012-03-02 23:43:51 +000017//
Michael J. Spencerdd3aa9e2013-02-05 20:27:22 +000018// Output should be specialized for each format where appropriate.
David Meyeree37b6e2012-03-02 23:43:51 +000019//
20//===----------------------------------------------------------------------===//
David Meyer5c2b4ea2012-03-01 01:36:50 +000021
Michael J. Spencerd326d052013-02-20 02:37:12 +000022#include "llvm-readobj.h"
23
Eric Christopher76e70f32013-04-03 18:31:38 +000024#include "Error.h"
25#include "ObjDumper.h"
26#include "StreamWriter.h"
27
28#include "llvm/Object/Archive.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000029#include "llvm/Object/ObjectFile.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000030#include "llvm/Support/Casting.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000031#include "llvm/Support/CommandLine.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000032#include "llvm/Support/DataTypes.h"
David Meyer5c2b4ea2012-03-01 01:36:50 +000033#include "llvm/Support/Debug.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000034#include "llvm/Support/FileSystem.h"
35#include "llvm/Support/ManagedStatic.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000036#include "llvm/Support/PrettyStackTrace.h"
37#include "llvm/Support/Signals.h"
Eric Christopher76e70f32013-04-03 18:31:38 +000038#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Support/TargetSelect.h"
40#include "llvm/Support/system_error.h"
41
42#include <string>
43
David Meyer5c2b4ea2012-03-01 01:36:50 +000044
45using namespace llvm;
46using namespace llvm::object;
47
Eric Christopher76e70f32013-04-03 18:31:38 +000048namespace opts {
49 cl::list<std::string> InputFilenames(cl::Positional,
50 cl::desc("<input object files>"),
51 cl::ZeroOrMore);
David Meyer5c2b4ea2012-03-01 01:36:50 +000052
Eric Christopher76e70f32013-04-03 18:31:38 +000053 // -file-headers, -h
54 cl::opt<bool> FileHeaders("file-headers",
55 cl::desc("Display file headers "));
56 cl::alias FileHeadersShort("h",
57 cl::desc("Alias for --file-headers"),
58 cl::aliasopt(FileHeaders));
59
60 // -sections, -s
61 cl::opt<bool> Sections("sections",
62 cl::desc("Display all sections."));
63 cl::alias SectionsShort("s",
64 cl::desc("Alias for --sections"),
65 cl::aliasopt(Sections));
66
67 // -section-relocations, -sr
68 cl::opt<bool> SectionRelocations("section-relocations",
69 cl::desc("Display relocations for each section shown."));
70 cl::alias SectionRelocationsShort("sr",
71 cl::desc("Alias for --section-relocations"),
72 cl::aliasopt(SectionRelocations));
73
74 // -section-symbols, -st
75 cl::opt<bool> SectionSymbols("section-symbols",
76 cl::desc("Display symbols for each section shown."));
77 cl::alias SectionSymbolsShort("st",
78 cl::desc("Alias for --section-symbols"),
79 cl::aliasopt(SectionSymbols));
80
81 // -section-data, -sd
82 cl::opt<bool> SectionData("section-data",
83 cl::desc("Display section data for each section shown."));
84 cl::alias SectionDataShort("sd",
85 cl::desc("Alias for --section-data"),
86 cl::aliasopt(SectionData));
87
88 // -relocations, -r
89 cl::opt<bool> Relocations("relocations",
90 cl::desc("Display the relocation entries in the file"));
91 cl::alias RelocationsShort("r",
92 cl::desc("Alias for --relocations"),
93 cl::aliasopt(Relocations));
94
95 // -symbols, -t
96 cl::opt<bool> Symbols("symbols",
97 cl::desc("Display the symbol table"));
98 cl::alias SymbolsShort("t",
99 cl::desc("Alias for --symbols"),
100 cl::aliasopt(Symbols));
101
102 // -dyn-symbols, -dt
103 cl::opt<bool> DynamicSymbols("dyn-symbols",
104 cl::desc("Display the dynamic symbol table"));
105 cl::alias DynamicSymbolsShort("dt",
106 cl::desc("Alias for --dyn-symbols"),
107 cl::aliasopt(DynamicSymbols));
108
109 // -unwind, -u
110 cl::opt<bool> UnwindInfo("unwind",
111 cl::desc("Display unwind information"));
112 cl::alias UnwindInfoShort("u",
113 cl::desc("Alias for --unwind"),
114 cl::aliasopt(UnwindInfo));
115
116 // -dynamic-table
117 cl::opt<bool> DynamicTable("dynamic-table",
118 cl::desc("Display the ELF .dynamic section table"));
119
120 // -needed-libs
121 cl::opt<bool> NeededLibraries("needed-libs",
122 cl::desc("Display the needed libraries"));
123} // namespace opts
124
125namespace llvm {
126
127bool error(error_code EC) {
128 if (!EC)
129 return false;
130
131 outs() << "\nError reading file: " << EC.message() << ".\n";
132 outs().flush();
133 return true;
Rafael Espindola148ee4f2012-12-31 16:05:21 +0000134}
135
Eric Christopher76e70f32013-04-03 18:31:38 +0000136bool relocAddressLess(RelocationRef a, RelocationRef b) {
137 uint64_t a_addr, b_addr;
138 if (error(a.getAddress(a_addr))) return false;
139 if (error(b.getAddress(b_addr))) return false;
140 return a_addr < b_addr;
141}
142
143} // namespace llvm
144
145
146static void reportError(StringRef Input, error_code EC) {
147 if (Input == "-")
148 Input = "<stdin>";
149
150 errs() << Input << ": " << EC.message() << "\n";
151 errs().flush();
152}
153
154static void reportError(StringRef Input, StringRef Message) {
155 if (Input == "-")
156 Input = "<stdin>";
157
158 errs() << Input << ": " << Message << "\n";
159}
160
161/// @brief Creates an format-specific object file dumper.
162static error_code createDumper(const ObjectFile *Obj,
163 StreamWriter &Writer,
164 OwningPtr<ObjDumper> &Result) {
165 if (!Obj)
166 return readobj_error::unsupported_file_format;
167
168 if (Obj->isCOFF())
169 return createCOFFDumper(Obj, Writer, Result);
170 if (Obj->isELF())
171 return createELFDumper(Obj, Writer, Result);
172 if (Obj->isMachO())
173 return createMachODumper(Obj, Writer, Result);
174
175 return readobj_error::unsupported_obj_file_format;
176}
177
178
179/// @brief Dumps the specified object file.
180static void dumpObject(const ObjectFile *Obj) {
181 StreamWriter Writer(outs());
182 OwningPtr<ObjDumper> Dumper;
183 if (error_code EC = createDumper(Obj, Writer, Dumper)) {
184 reportError(Obj->getFileName(), EC);
185 return;
186 }
187
188 outs() << '\n';
189 outs() << "File: " << Obj->getFileName() << "\n";
190 outs() << "Format: " << Obj->getFileFormatName() << "\n";
191 outs() << "Arch: "
192 << Triple::getArchTypeName((llvm::Triple::ArchType)Obj->getArch())
Rafael Espindolafc738472012-12-31 16:29:44 +0000193 << "\n";
Eric Christopher76e70f32013-04-03 18:31:38 +0000194 outs() << "AddressSize: " << (8*Obj->getBytesInAddress()) << "bit\n";
195 if (Obj->isELF())
196 outs() << "LoadName: " << Obj->getLoadName() << "\n";
197
198 if (opts::FileHeaders)
199 Dumper->printFileHeaders();
200 if (opts::Sections)
201 Dumper->printSections();
202 if (opts::Relocations)
203 Dumper->printRelocations();
204 if (opts::Symbols)
205 Dumper->printSymbols();
206 if (opts::DynamicSymbols)
207 Dumper->printDynamicSymbols();
208 if (opts::UnwindInfo)
209 Dumper->printUnwindInfo();
210 if (opts::DynamicTable)
211 Dumper->printDynamicTable();
212 if (opts::NeededLibraries)
213 Dumper->printNeededLibraries();
Rafael Espindolafc738472012-12-31 16:29:44 +0000214}
215
David Meyer5c2b4ea2012-03-01 01:36:50 +0000216
Eric Christopher76e70f32013-04-03 18:31:38 +0000217/// @brief Dumps each object file in \a Arc;
218static void dumpArchive(const Archive *Arc) {
219 for (Archive::child_iterator ArcI = Arc->begin_children(),
220 ArcE = Arc->end_children();
221 ArcI != ArcE; ++ArcI) {
222 OwningPtr<Binary> child;
223 if (error_code EC = ArcI->getAsBinary(child)) {
224 // Ignore non-object files.
225 if (EC != object_error::invalid_file_type)
226 reportError(Arc->getFileName(), EC.message());
227 continue;
David Meyer2d70e262012-03-09 20:59:52 +0000228 }
Eric Christopher76e70f32013-04-03 18:31:38 +0000229
230 if (ObjectFile *Obj = dyn_cast<ObjectFile>(child.get()))
231 dumpObject(Obj);
232 else
233 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
234 }
235}
236
237
238/// @brief Opens \a File and dumps it.
239static void dumpInput(StringRef File) {
240 // If file isn't stdin, check that it exists.
241 if (File != "-" && !sys::fs::exists(File)) {
242 reportError(File, readobj_error::file_not_found);
243 return;
Rafael Espindola1318e142012-12-31 15:30:58 +0000244 }
David Meyer5c2b4ea2012-03-01 01:36:50 +0000245
Eric Christopher76e70f32013-04-03 18:31:38 +0000246 // Attempt to open the binary.
247 OwningPtr<Binary> Binary;
248 if (error_code EC = createBinary(File, Binary)) {
249 reportError(File, EC);
250 return;
Rafael Espindolafc738472012-12-31 16:29:44 +0000251 }
Eric Christopher76e70f32013-04-03 18:31:38 +0000252
253 if (Archive *Arc = dyn_cast<Archive>(Binary.get()))
254 dumpArchive(Arc);
255 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(Binary.get()))
256 dumpObject(Obj);
257 else
258 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindolafc738472012-12-31 16:29:44 +0000259}
260
David Meyer97f77872012-03-01 22:19:54 +0000261
Eric Christopher76e70f32013-04-03 18:31:38 +0000262int main(int argc, const char *argv[]) {
David Meyer5c2b4ea2012-03-01 01:36:50 +0000263 sys::PrintStackTraceOnErrorSignal();
264 PrettyStackTraceProgram X(argc, argv);
Eric Christopher76e70f32013-04-03 18:31:38 +0000265 llvm_shutdown_obj Y;
David Meyer5c2b4ea2012-03-01 01:36:50 +0000266
Eric Christopher76e70f32013-04-03 18:31:38 +0000267 // Initialize targets.
268 llvm::InitializeAllTargetInfos();
David Meyer5c2b4ea2012-03-01 01:36:50 +0000269
Eric Christopher76e70f32013-04-03 18:31:38 +0000270 // Register the target printer for --version.
271 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer5c2b4ea2012-03-01 01:36:50 +0000272
Eric Christopher76e70f32013-04-03 18:31:38 +0000273 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer5c2b4ea2012-03-01 01:36:50 +0000274
Eric Christopher76e70f32013-04-03 18:31:38 +0000275 // Default to stdin if no filename is specified.
276 if (opts::InputFilenames.size() == 0)
277 opts::InputFilenames.push_back("-");
David Meyer5c2b4ea2012-03-01 01:36:50 +0000278
Eric Christopher76e70f32013-04-03 18:31:38 +0000279 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
280 dumpInput);
Rafael Espindola87b47cc2012-12-31 16:53:01 +0000281
David Meyer5c2b4ea2012-03-01 01:36:50 +0000282 return 0;
283}