blob: c2964c5f5e1cca714f842f02ef1c09241641b53a [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"
Eric Christopher9cad53c2013-04-03 18:31:38 +000025#include "llvm/Object/Archive.h"
Rui Ueyama71ba9bd2015-08-28 07:40:30 +000026#include "llvm/Object/COFFImportFile.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"
Zachary Turner88bb1632016-05-03 00:28:04 +000037#include "llvm/Support/ScopedPrinter.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000038#include "llvm/Support/Signals.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000039#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Support/TargetSelect.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000041#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000042#include <system_error>
Eric Christopher9cad53c2013-04-03 18:31:38 +000043
David Meyer2fc34c52012-03-01 01:36:50 +000044using namespace llvm;
45using namespace llvm::object;
46
Eric Christopher9cad53c2013-04-03 18:31:38 +000047namespace opts {
48 cl::list<std::string> InputFilenames(cl::Positional,
49 cl::desc("<input object files>"),
50 cl::ZeroOrMore);
David Meyer2fc34c52012-03-01 01:36:50 +000051
Eric Christopher9cad53c2013-04-03 18:31:38 +000052 // -file-headers, -h
53 cl::opt<bool> FileHeaders("file-headers",
54 cl::desc("Display file headers "));
55 cl::alias FileHeadersShort("h",
56 cl::desc("Alias for --file-headers"),
57 cl::aliasopt(FileHeaders));
58
59 // -sections, -s
60 cl::opt<bool> Sections("sections",
61 cl::desc("Display all sections."));
62 cl::alias SectionsShort("s",
63 cl::desc("Alias for --sections"),
64 cl::aliasopt(Sections));
65
66 // -section-relocations, -sr
67 cl::opt<bool> SectionRelocations("section-relocations",
68 cl::desc("Display relocations for each section shown."));
69 cl::alias SectionRelocationsShort("sr",
70 cl::desc("Alias for --section-relocations"),
71 cl::aliasopt(SectionRelocations));
72
73 // -section-symbols, -st
74 cl::opt<bool> SectionSymbols("section-symbols",
75 cl::desc("Display symbols for each section shown."));
76 cl::alias SectionSymbolsShort("st",
77 cl::desc("Alias for --section-symbols"),
78 cl::aliasopt(SectionSymbols));
79
80 // -section-data, -sd
81 cl::opt<bool> SectionData("section-data",
82 cl::desc("Display section data for each section shown."));
83 cl::alias SectionDataShort("sd",
84 cl::desc("Alias for --section-data"),
85 cl::aliasopt(SectionData));
86
87 // -relocations, -r
88 cl::opt<bool> Relocations("relocations",
89 cl::desc("Display the relocation entries in the file"));
90 cl::alias RelocationsShort("r",
91 cl::desc("Alias for --relocations"),
92 cl::aliasopt(Relocations));
93
Michael J. Spencer594c0282015-06-25 21:47:32 +000094 // -dyn-relocations
95 cl::opt<bool> DynRelocs("dyn-relocations",
96 cl::desc("Display the dynamic relocation entries in the file"));
97
Eric Christopher9cad53c2013-04-03 18:31:38 +000098 // -symbols, -t
99 cl::opt<bool> Symbols("symbols",
100 cl::desc("Display the symbol table"));
101 cl::alias SymbolsShort("t",
102 cl::desc("Alias for --symbols"),
103 cl::aliasopt(Symbols));
104
105 // -dyn-symbols, -dt
106 cl::opt<bool> DynamicSymbols("dyn-symbols",
107 cl::desc("Display the dynamic symbol table"));
108 cl::alias DynamicSymbolsShort("dt",
109 cl::desc("Alias for --dyn-symbols"),
110 cl::aliasopt(DynamicSymbols));
111
112 // -unwind, -u
113 cl::opt<bool> UnwindInfo("unwind",
114 cl::desc("Display unwind information"));
115 cl::alias UnwindInfoShort("u",
116 cl::desc("Alias for --unwind"),
117 cl::aliasopt(UnwindInfo));
118
119 // -dynamic-table
120 cl::opt<bool> DynamicTable("dynamic-table",
121 cl::desc("Display the ELF .dynamic section table"));
122
123 // -needed-libs
124 cl::opt<bool> NeededLibraries("needed-libs",
125 cl::desc("Display the needed libraries"));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000126
Nico Rieckd6df0542013-04-12 04:07:39 +0000127 // -program-headers
128 cl::opt<bool> ProgramHeaders("program-headers",
129 cl::desc("Display ELF program headers"));
130
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000131 // -hash-table
132 cl::opt<bool> HashTable("hash-table",
133 cl::desc("Display ELF hash table"));
134
Igor Kudrin496fb2f2015-10-14 12:11:50 +0000135 // -gnu-hash-table
136 cl::opt<bool> GnuHashTable("gnu-hash-table",
137 cl::desc("Display ELF .gnu.hash section"));
138
Nico Rieckf3f0b792013-04-12 04:01:52 +0000139 // -expand-relocs
140 cl::opt<bool> ExpandRelocs("expand-relocs",
141 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000142
Zachary Turner99f02152015-02-18 19:32:05 +0000143 // -codeview
144 cl::opt<bool> CodeView("codeview",
145 cl::desc("Display CodeView debug information"));
146
147 // -codeview-subsection-bytes
148 cl::opt<bool> CodeViewSubsectionBytes(
149 "codeview-subsection-bytes",
150 cl::desc("Dump raw contents of codeview debug sections and records"));
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000151
152 // -arm-attributes, -a
153 cl::opt<bool> ARMAttributes("arm-attributes",
154 cl::desc("Display the ARM attributes section"));
155 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
156 cl::aliasopt(ARMAttributes));
Simon Atanasyan80433902014-06-18 08:47:09 +0000157
158 // -mips-plt-got
159 cl::opt<bool>
160 MipsPLTGOT("mips-plt-got",
161 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Rui Ueyama1e152d52014-10-02 17:02:18 +0000162
Simon Atanasyanc914de22015-05-07 15:40:35 +0000163 // -mips-abi-flags
164 cl::opt<bool> MipsABIFlags("mips-abi-flags",
165 cl::desc("Display the MIPS.abiflags section"));
166
Simon Atanasyan6e07e932015-06-16 21:47:43 +0000167 // -mips-reginfo
168 cl::opt<bool> MipsReginfo("mips-reginfo",
169 cl::desc("Display the MIPS .reginfo section"));
170
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000171 // -mips-options
172 cl::opt<bool> MipsOptions("mips-options",
173 cl::desc("Display the MIPS .MIPS.options section"));
174
Rui Ueyama1e152d52014-10-02 17:02:18 +0000175 // -coff-imports
176 cl::opt<bool>
177 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000178
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +0000179 // -coff-exports
180 cl::opt<bool>
181 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
182
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000183 // -coff-directives
184 cl::opt<bool>
185 COFFDirectives("coff-directives",
Rui Ueyama74e85132014-11-19 00:18:07 +0000186 cl::desc("Display the PE/COFF .drectve section"));
187
188 // -coff-basereloc
189 cl::opt<bool>
190 COFFBaseRelocs("coff-basereloc",
191 cl::desc("Display the PE/COFF .reloc section"));
Lang Hames0000afd2015-06-26 23:56:53 +0000192
Davide Italiano07e7acb2015-08-21 20:28:30 +0000193 // -macho-data-in-code
194 cl::opt<bool>
195 MachODataInCode("macho-data-in-code",
196 cl::desc("Display MachO Data in Code command"));
197
Davide Italiano4410b222015-09-03 18:10:28 +0000198 // -macho-indirect-symbols
199 cl::opt<bool>
200 MachOIndirectSymbols("macho-indirect-symbols",
201 cl::desc("Display MachO indirect symbols"));
202
Davide Italiano9a429b72015-09-09 00:21:18 +0000203 // -macho-linker-options
204 cl::opt<bool>
205 MachOLinkerOptions("macho-linker-options",
206 cl::desc("Display MachO linker options"));
207
Davide Italianod1f09962015-09-02 16:24:24 +0000208 // -macho-segment
209 cl::opt<bool>
210 MachOSegment("macho-segment",
211 cl::desc("Display MachO Segment command"));
212
Davide Italiano976f4da2015-08-27 15:11:32 +0000213 // -macho-version-min
214 cl::opt<bool>
215 MachOVersionMin("macho-version-min",
216 cl::desc("Display MachO version min command"));
Davide Italiano35eebe12015-08-31 19:32:31 +0000217
218 // -macho-dysymtab
219 cl::opt<bool>
220 MachODysymtab("macho-dysymtab",
221 cl::desc("Display MachO Dysymtab command"));
222
Lang Hames0000afd2015-06-26 23:56:53 +0000223 // -stackmap
224 cl::opt<bool>
225 PrintStackMap("stackmap",
226 cl::desc("Display contents of stackmap section"));
227
Davide Italiano4f05f322015-10-16 23:19:01 +0000228 // -version-info
229 cl::opt<bool>
230 VersionInfo("version-info",
231 cl::desc("Display ELF version sections (if present)"));
232 cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
233 cl::aliasopt(VersionInfo));
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000234
235 cl::opt<bool> SectionGroups("elf-section-groups",
236 cl::desc("Display ELF section group contents"));
237 cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
238 cl::aliasopt(SectionGroups));
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000239 cl::opt<bool> HashHistogram(
240 "elf-hash-histogram",
241 cl::desc("Display bucket list histogram for hash sections"));
242 cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
243 cl::aliasopt(HashHistogram));
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000244
Tim Northoverd59b23a2016-03-01 21:45:22 +0000245 cl::opt<OutputStyleTy>
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000246 Output("elf-output-style", cl::desc("Specify ELF dump style"),
247 cl::values(clEnumVal(LLVM, "LLVM default style"),
248 clEnumVal(GNU, "GNU readelf style"), clEnumValEnd),
249 cl::init(LLVM));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000250} // namespace opts
251
Rafael Espindola00ddb142015-07-20 03:38:17 +0000252namespace llvm {
253
Davide Italiano1eb92342015-12-04 19:29:49 +0000254LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
Davide Italiano0a07a822015-12-23 19:29:34 +0000255 errs() << "\nError reading file: " << Msg << ".\n";
256 errs().flush();
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000257 exit(1);
Rafael Espindola724d4b42015-07-20 03:01:49 +0000258}
259
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000260void error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000261 if (!EC)
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000262 return;
Eric Christopher9cad53c2013-04-03 18:31:38 +0000263
Rafael Espindola8b3b09f2015-08-06 21:54:37 +0000264 reportError(EC.message());
Rafael Espindola144af2c2012-12-31 16:05:21 +0000265}
266
Eric Christopher9cad53c2013-04-03 18:31:38 +0000267bool relocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindolac7689302015-07-06 15:53:43 +0000268 return a.getOffset() < b.getOffset();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000269}
270
271} // namespace llvm
272
Rafael Espindola4453e42942014-06-13 03:07:50 +0000273static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000274 if (Input == "-")
275 Input = "<stdin>";
276
Rafael Espindola724d4b42015-07-20 03:01:49 +0000277 reportError(Twine(Input) + ": " + EC.message());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000278}
279
280static void reportError(StringRef Input, StringRef Message) {
281 if (Input == "-")
282 Input = "<stdin>";
283
Rafael Espindola724d4b42015-07-20 03:01:49 +0000284 reportError(Twine(Input) + ": " + Message);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000285}
286
Simon Atanasyan80433902014-06-18 08:47:09 +0000287static bool isMipsArch(unsigned Arch) {
288 switch (Arch) {
289 case llvm::Triple::mips:
290 case llvm::Triple::mipsel:
291 case llvm::Triple::mips64:
292 case llvm::Triple::mips64el:
293 return true;
294 default:
295 return false;
296 }
297}
298
Eric Christopher9cad53c2013-04-03 18:31:38 +0000299/// @brief Creates an format-specific object file dumper.
Zachary Turner88bb1632016-05-03 00:28:04 +0000300static std::error_code createDumper(const ObjectFile *Obj,
301 ScopedPrinter &Writer,
Rafael Espindola4453e42942014-06-13 03:07:50 +0000302 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000303 if (!Obj)
304 return readobj_error::unsupported_file_format;
305
306 if (Obj->isCOFF())
307 return createCOFFDumper(Obj, Writer, Result);
308 if (Obj->isELF())
309 return createELFDumper(Obj, Writer, Result);
310 if (Obj->isMachO())
311 return createMachODumper(Obj, Writer, Result);
312
313 return readobj_error::unsupported_obj_file_format;
314}
315
Eric Christopher9cad53c2013-04-03 18:31:38 +0000316/// @brief Dumps the specified object file.
317static void dumpObject(const ObjectFile *Obj) {
Zachary Turner88bb1632016-05-03 00:28:04 +0000318 ScopedPrinter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000319 std::unique_ptr<ObjDumper> Dumper;
Davide Italianoe66b73f2015-12-05 23:36:52 +0000320 if (std::error_code EC = createDumper(Obj, Writer, Dumper))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000321 reportError(Obj->getFileName(), EC);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000322
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000323 if (opts::Output == opts::LLVM) {
324 outs() << '\n';
325 outs() << "File: " << Obj->getFileName() << "\n";
326 outs() << "Format: " << Obj->getFileFormatName() << "\n";
327 outs() << "Arch: " << Triple::getArchTypeName(
328 (llvm::Triple::ArchType)Obj->getArch()) << "\n";
329 outs() << "AddressSize: " << (8 * Obj->getBytesInAddress()) << "bit\n";
330 Dumper->printLoadName();
331 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000332
333 if (opts::FileHeaders)
334 Dumper->printFileHeaders();
335 if (opts::Sections)
336 Dumper->printSections();
337 if (opts::Relocations)
338 Dumper->printRelocations();
Michael J. Spencer594c0282015-06-25 21:47:32 +0000339 if (opts::DynRelocs)
340 Dumper->printDynamicRelocations();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000341 if (opts::Symbols)
342 Dumper->printSymbols();
343 if (opts::DynamicSymbols)
344 Dumper->printDynamicSymbols();
345 if (opts::UnwindInfo)
346 Dumper->printUnwindInfo();
347 if (opts::DynamicTable)
348 Dumper->printDynamicTable();
349 if (opts::NeededLibraries)
350 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000351 if (opts::ProgramHeaders)
352 Dumper->printProgramHeaders();
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000353 if (opts::HashTable)
354 Dumper->printHashTable();
Igor Kudrin496fb2f2015-10-14 12:11:50 +0000355 if (opts::GnuHashTable)
356 Dumper->printGnuHashTable();
Davide Italiano4f05f322015-10-16 23:19:01 +0000357 if (opts::VersionInfo)
358 Dumper->printVersionInfo();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000359 if (Obj->isELF()) {
360 if (Obj->getArch() == llvm::Triple::arm)
361 if (opts::ARMAttributes)
362 Dumper->printAttributes();
363 if (isMipsArch(Obj->getArch())) {
364 if (opts::MipsPLTGOT)
365 Dumper->printMipsPLTGOT();
366 if (opts::MipsABIFlags)
367 Dumper->printMipsABIFlags();
368 if (opts::MipsReginfo)
369 Dumper->printMipsReginfo();
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000370 if (opts::MipsOptions)
371 Dumper->printMipsOptions();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000372 }
373 if (opts::SectionGroups)
374 Dumper->printGroupSections();
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000375 if (opts::HashHistogram)
376 Dumper->printHashHistogram();
Simon Atanasyanc914de22015-05-07 15:40:35 +0000377 }
Davide Italianocd1b6db2015-07-24 02:14:20 +0000378 if (Obj->isCOFF()) {
379 if (opts::COFFImports)
380 Dumper->printCOFFImports();
381 if (opts::COFFExports)
382 Dumper->printCOFFExports();
383 if (opts::COFFDirectives)
384 Dumper->printCOFFDirectives();
385 if (opts::COFFBaseRelocs)
386 Dumper->printCOFFBaseReloc();
Reid Kleckner83ebad32015-12-16 18:28:12 +0000387 if (opts::CodeView)
388 Dumper->printCodeViewDebugInfo();
Davide Italianocd1b6db2015-07-24 02:14:20 +0000389 }
Davide Italiano1e12fc42015-08-31 17:12:23 +0000390 if (Obj->isMachO()) {
Davide Italiano07e7acb2015-08-21 20:28:30 +0000391 if (opts::MachODataInCode)
392 Dumper->printMachODataInCode();
Davide Italiano4410b222015-09-03 18:10:28 +0000393 if (opts::MachOIndirectSymbols)
394 Dumper->printMachOIndirectSymbols();
Davide Italiano9a429b72015-09-09 00:21:18 +0000395 if (opts::MachOLinkerOptions)
396 Dumper->printMachOLinkerOptions();
Davide Italianod1f09962015-09-02 16:24:24 +0000397 if (opts::MachOSegment)
398 Dumper->printMachOSegment();
Davide Italiano976f4da2015-08-27 15:11:32 +0000399 if (opts::MachOVersionMin)
400 Dumper->printMachOVersionMin();
Davide Italiano35eebe12015-08-31 19:32:31 +0000401 if (opts::MachODysymtab)
402 Dumper->printMachODysymtab();
Davide Italiano1e12fc42015-08-31 17:12:23 +0000403 }
Lang Hames0000afd2015-06-26 23:56:53 +0000404 if (opts::PrintStackMap)
405 Dumper->printStackMap();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000406}
407
Eric Christopher9cad53c2013-04-03 18:31:38 +0000408/// @brief Dumps each object file in \a Arc;
409static void dumpArchive(const Archive *Arc) {
Kevin Enderby7a969422015-11-05 19:24:56 +0000410 for (auto &ErrorOrChild : Arc->children()) {
411 if (std::error_code EC = ErrorOrChild.getError())
412 reportError(Arc->getFileName(), EC.message());
413 const auto &Child = *ErrorOrChild;
Davide Italianodb8803b2015-08-07 00:35:28 +0000414 ErrorOr<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
Rafael Espindolaae460022014-06-16 16:08:36 +0000415 if (std::error_code EC = ChildOrErr.getError()) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000416 // Ignore non-object files.
417 if (EC != object_error::invalid_file_type)
418 reportError(Arc->getFileName(), EC.message());
419 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000420 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000421
Rafael Espindolaae460022014-06-16 16:08:36 +0000422 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000423 dumpObject(Obj);
424 else
425 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
426 }
427}
428
Rafael Espindola8448a242015-03-24 20:26:55 +0000429/// @brief Dumps each object file in \a MachO Universal Binary;
430static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
431 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
432 ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
David Blaikie29ac43c2015-04-13 16:05:49 +0000433 if (ObjOrErr)
434 dumpObject(&*ObjOrErr.get());
435 else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
436 dumpArchive(&*AOrErr.get());
437 else
438 reportError(UBinary->getFileName(), ObjOrErr.getError().message());
Rafael Espindola8448a242015-03-24 20:26:55 +0000439 }
440}
Eric Christopher9cad53c2013-04-03 18:31:38 +0000441
442/// @brief Opens \a File and dumps it.
443static void dumpInput(StringRef File) {
David Meyer2fc34c52012-03-01 01:36:50 +0000444
Eric Christopher9cad53c2013-04-03 18:31:38 +0000445 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000446 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
447 if (!BinaryOrErr)
448 reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
Rafael Espindola48af1c22014-08-19 18:44:46 +0000449 Binary &Binary = *BinaryOrErr.get().getBinary();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000450
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000451 if (Archive *Arc = dyn_cast<Archive>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000452 dumpArchive(Arc);
Rafael Espindola8448a242015-03-24 20:26:55 +0000453 else if (MachOUniversalBinary *UBinary =
454 dyn_cast<MachOUniversalBinary>(&Binary))
455 dumpMachOUniversalBinary(UBinary);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000456 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000457 dumpObject(Obj);
Rui Ueyama71ba9bd2015-08-28 07:40:30 +0000458 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
459 dumpCOFFImportFile(Import);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000460 else
461 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000462}
463
Eric Christopher9cad53c2013-04-03 18:31:38 +0000464int main(int argc, const char *argv[]) {
David Meyer2fc34c52012-03-01 01:36:50 +0000465 sys::PrintStackTraceOnErrorSignal();
466 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000467 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000468
Eric Christopher9cad53c2013-04-03 18:31:38 +0000469 // Register the target printer for --version.
470 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000471
Eric Christopher9cad53c2013-04-03 18:31:38 +0000472 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000473
Eric Christopher9cad53c2013-04-03 18:31:38 +0000474 // Default to stdin if no filename is specified.
475 if (opts::InputFilenames.size() == 0)
476 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000477
Eric Christopher9cad53c2013-04-03 18:31:38 +0000478 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
479 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000480
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000481 return 0;
David Meyer2fc34c52012-03-01 01:36:50 +0000482}