1 //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===//
2 //
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 //
10 // 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.
13 //
14 // 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.
17 //
18 // Output should be specialized for each format where appropriate.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #include "llvm-readobj.h"
23 #include "Error.h"
24 #include "ObjDumper.h"
25 #include "StreamWriter.h"
26 #include "llvm/Object/Archive.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/DataTypes.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/ManagedStatic.h"
34 #include "llvm/Support/PrettyStackTrace.h"
35 #include "llvm/Support/Signals.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/TargetSelect.h"
38 #include <string>
39 #include <system_error>
40
41
42 using namespace llvm;
43 using namespace llvm::object;
44
45 namespace opts {
46 cl::list<std::string> InputFilenames(cl::Positional,
47 cl::desc("<input object files>"),
48 cl::ZeroOrMore);
49
50 // -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"));
120
121 // -program-headers
122 cl::opt<bool> ProgramHeaders("program-headers",
123 cl::desc("Display ELF program headers"));
124
125 // -expand-relocs
126 cl::opt<bool> ExpandRelocs("expand-relocs",
127 cl::desc("Expand each shown relocation to multiple lines"));
128
129 // -codeview-linetables
130 cl::opt<bool> CodeViewLineTables("codeview-linetables",
131 cl::desc("Display CodeView line table information"));
132
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));
138
139 // -mips-plt-got
140 cl::opt<bool>
141 MipsPLTGOT("mips-plt-got",
142 cl::desc("Display the MIPS GOT and PLT GOT sections"));
143 } // namespace opts
144
145 static int ReturnValue = EXIT_SUCCESS;
146
147 namespace llvm {
148
error(std::error_code EC)149 bool error(std::error_code EC) {
150 if (!EC)
151 return false;
152
153 ReturnValue = EXIT_FAILURE;
154 outs() << "\nError reading file: " << EC.message() << ".\n";
155 outs().flush();
156 return true;
157 }
158
relocAddressLess(RelocationRef a,RelocationRef b)159 bool relocAddressLess(RelocationRef a, RelocationRef b) {
160 uint64_t a_addr, b_addr;
161 if (error(a.getOffset(a_addr))) return false;
162 if (error(b.getOffset(b_addr))) return false;
163 return a_addr < b_addr;
164 }
165
166 } // namespace llvm
167
reportError(StringRef Input,std::error_code EC)168 static void reportError(StringRef Input, std::error_code EC) {
169 if (Input == "-")
170 Input = "<stdin>";
171
172 errs() << Input << ": " << EC.message() << "\n";
173 errs().flush();
174 ReturnValue = EXIT_FAILURE;
175 }
176
reportError(StringRef Input,StringRef Message)177 static void reportError(StringRef Input, StringRef Message) {
178 if (Input == "-")
179 Input = "<stdin>";
180
181 errs() << Input << ": " << Message << "\n";
182 ReturnValue = EXIT_FAILURE;
183 }
184
isMipsArch(unsigned Arch)185 static 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
197 /// @brief Creates an format-specific object file dumper.
createDumper(const ObjectFile * Obj,StreamWriter & Writer,std::unique_ptr<ObjDumper> & Result)198 static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer,
199 std::unique_ptr<ObjDumper> &Result) {
200 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.
dumpObject(const ObjectFile * Obj)215 static void dumpObject(const ObjectFile *Obj) {
216 StreamWriter Writer(outs());
217 std::unique_ptr<ObjDumper> Dumper;
218 if (std::error_code EC = createDumper(Obj, Writer, Dumper)) {
219 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())
228 << "\n";
229 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();
249 if (opts::ProgramHeaders)
250 Dumper->printProgramHeaders();
251 if (Obj->getArch() == llvm::Triple::arm && Obj->isELF())
252 if (opts::ARMAttributes)
253 Dumper->printAttributes();
254 if (isMipsArch(Obj->getArch()) && Obj->isELF())
255 if (opts::MipsPLTGOT)
256 Dumper->printMipsPLTGOT();
257 }
258
259
260 /// @brief Dumps each object file in \a Arc;
dumpArchive(const Archive * Arc)261 static void dumpArchive(const Archive *Arc) {
262 for (Archive::child_iterator ArcI = Arc->child_begin(),
263 ArcE = Arc->child_end();
264 ArcI != ArcE; ++ArcI) {
265 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = ArcI->getAsBinary();
266 if (std::error_code EC = ChildOrErr.getError()) {
267 // Ignore non-object files.
268 if (EC != object_error::invalid_file_type)
269 reportError(Arc->getFileName(), EC.message());
270 continue;
271 }
272
273 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
274 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.
dumpInput(StringRef File)282 static 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;
287 }
288
289 // Attempt to open the binary.
290 ErrorOr<Binary *> BinaryOrErr = createBinary(File);
291 if (std::error_code EC = BinaryOrErr.getError()) {
292 reportError(File, EC);
293 return;
294 }
295 std::unique_ptr<Binary> Binary(BinaryOrErr.get());
296
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);
303 }
304
305
main(int argc,const char * argv[])306 int main(int argc, const char *argv[]) {
307 sys::PrintStackTraceOnErrorSignal();
308 PrettyStackTraceProgram X(argc, argv);
309 llvm_shutdown_obj Y;
310
311 // Initialize targets.
312 llvm::InitializeAllTargetInfos();
313
314 // Register the target printer for --version.
315 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
316
317 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
318
319 // Default to stdin if no filename is specified.
320 if (opts::InputFilenames.size() == 0)
321 opts::InputFilenames.push_back("-");
322
323 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
324 dumpInput);
325
326 return ReturnValue;
327 }
328