blob: d811ea995ba892ae12c0e13c7bed13d4b8a59705 [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"
Reid Kleckner0b269742016-05-14 00:02:53 +000025#include "llvm/DebugInfo/CodeView/MemoryTypeTableBuilder.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000026#include "llvm/Object/Archive.h"
Rui Ueyama71ba9bd2015-08-28 07:40:30 +000027#include "llvm/Object/COFFImportFile.h"
Rafael Espindola67622312014-08-08 16:39:22 +000028#include "llvm/Object/ELFObjectFile.h"
Rafael Espindola8448a242015-03-24 20:26:55 +000029#include "llvm/Object/MachOUniversal.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000030#include "llvm/Object/ObjectFile.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000031#include "llvm/Support/Casting.h"
David Meyer2fc34c52012-03-01 01:36:50 +000032#include "llvm/Support/CommandLine.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000033#include "llvm/Support/DataTypes.h"
David Meyer2fc34c52012-03-01 01:36:50 +000034#include "llvm/Support/Debug.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000035#include "llvm/Support/FileSystem.h"
36#include "llvm/Support/ManagedStatic.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000037#include "llvm/Support/PrettyStackTrace.h"
Zachary Turner88bb1632016-05-03 00:28:04 +000038#include "llvm/Support/ScopedPrinter.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000039#include "llvm/Support/Signals.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000040#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Support/TargetSelect.h"
Eric Christopher9cad53c2013-04-03 18:31:38 +000042#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000043#include <system_error>
Eric Christopher9cad53c2013-04-03 18:31:38 +000044
David Meyer2fc34c52012-03-01 01:36:50 +000045using namespace llvm;
46using namespace llvm::object;
47
Eric Christopher9cad53c2013-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 Meyer2fc34c52012-03-01 01:36:50 +000052
Eric Christopher9cad53c2013-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
Michael J. Spencer594c0282015-06-25 21:47:32 +000095 // -dyn-relocations
96 cl::opt<bool> DynRelocs("dyn-relocations",
97 cl::desc("Display the dynamic relocation entries in the file"));
98
Eric Christopher9cad53c2013-04-03 18:31:38 +000099 // -symbols, -t
100 cl::opt<bool> Symbols("symbols",
101 cl::desc("Display the symbol table"));
102 cl::alias SymbolsShort("t",
103 cl::desc("Alias for --symbols"),
104 cl::aliasopt(Symbols));
105
106 // -dyn-symbols, -dt
107 cl::opt<bool> DynamicSymbols("dyn-symbols",
108 cl::desc("Display the dynamic symbol table"));
109 cl::alias DynamicSymbolsShort("dt",
110 cl::desc("Alias for --dyn-symbols"),
111 cl::aliasopt(DynamicSymbols));
112
113 // -unwind, -u
114 cl::opt<bool> UnwindInfo("unwind",
115 cl::desc("Display unwind information"));
116 cl::alias UnwindInfoShort("u",
117 cl::desc("Alias for --unwind"),
118 cl::aliasopt(UnwindInfo));
119
120 // -dynamic-table
121 cl::opt<bool> DynamicTable("dynamic-table",
122 cl::desc("Display the ELF .dynamic section table"));
Saleem Abdulrasool6d9ca182016-07-20 01:16:28 +0000123 cl::alias DynamicTableShort("d", cl::desc("Alias for --dynamic-table"),
124 cl::aliasopt(DynamicTable));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000125
126 // -needed-libs
127 cl::opt<bool> NeededLibraries("needed-libs",
128 cl::desc("Display the needed libraries"));
Nico Rieckf3f0b792013-04-12 04:01:52 +0000129
Nico Rieckd6df0542013-04-12 04:07:39 +0000130 // -program-headers
131 cl::opt<bool> ProgramHeaders("program-headers",
132 cl::desc("Display ELF program headers"));
Saleem Abdulrasool6d9ca182016-07-20 01:16:28 +0000133 cl::alias ProgramHeadersShort("l", cl::desc("Alias for --program-headers"),
134 cl::aliasopt(ProgramHeaders));
Nico Rieckd6df0542013-04-12 04:07:39 +0000135
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000136 // -hash-table
137 cl::opt<bool> HashTable("hash-table",
138 cl::desc("Display ELF hash table"));
139
Igor Kudrin496fb2f2015-10-14 12:11:50 +0000140 // -gnu-hash-table
141 cl::opt<bool> GnuHashTable("gnu-hash-table",
142 cl::desc("Display ELF .gnu.hash section"));
143
Nico Rieckf3f0b792013-04-12 04:01:52 +0000144 // -expand-relocs
145 cl::opt<bool> ExpandRelocs("expand-relocs",
146 cl::desc("Expand each shown relocation to multiple lines"));
Timur Iskhodzhanov48703be2013-12-19 11:37:14 +0000147
Zachary Turner99f02152015-02-18 19:32:05 +0000148 // -codeview
149 cl::opt<bool> CodeView("codeview",
150 cl::desc("Display CodeView debug information"));
151
Reid Kleckner0b269742016-05-14 00:02:53 +0000152 // -codeview-merged-types
153 cl::opt<bool>
154 CodeViewMergedTypes("codeview-merged-types",
155 cl::desc("Display the merged CodeView type stream"));
156
Zachary Turner99f02152015-02-18 19:32:05 +0000157 // -codeview-subsection-bytes
158 cl::opt<bool> CodeViewSubsectionBytes(
159 "codeview-subsection-bytes",
160 cl::desc("Dump raw contents of codeview debug sections and records"));
Saleem Abdulrasool15d16d82014-01-30 04:46:33 +0000161
162 // -arm-attributes, -a
163 cl::opt<bool> ARMAttributes("arm-attributes",
164 cl::desc("Display the ARM attributes section"));
165 cl::alias ARMAttributesShort("-a", cl::desc("Alias for --arm-attributes"),
166 cl::aliasopt(ARMAttributes));
Simon Atanasyan80433902014-06-18 08:47:09 +0000167
168 // -mips-plt-got
169 cl::opt<bool>
170 MipsPLTGOT("mips-plt-got",
171 cl::desc("Display the MIPS GOT and PLT GOT sections"));
Rui Ueyama1e152d52014-10-02 17:02:18 +0000172
Simon Atanasyanc914de22015-05-07 15:40:35 +0000173 // -mips-abi-flags
174 cl::opt<bool> MipsABIFlags("mips-abi-flags",
175 cl::desc("Display the MIPS.abiflags section"));
176
Simon Atanasyan6e07e932015-06-16 21:47:43 +0000177 // -mips-reginfo
178 cl::opt<bool> MipsReginfo("mips-reginfo",
179 cl::desc("Display the MIPS .reginfo section"));
180
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000181 // -mips-options
182 cl::opt<bool> MipsOptions("mips-options",
183 cl::desc("Display the MIPS .MIPS.options section"));
184
Rui Ueyama1e152d52014-10-02 17:02:18 +0000185 // -coff-imports
186 cl::opt<bool>
187 COFFImports("coff-imports", cl::desc("Display the PE/COFF import table"));
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000188
Saleem Abdulrasoolddd92642015-01-03 21:35:09 +0000189 // -coff-exports
190 cl::opt<bool>
191 COFFExports("coff-exports", cl::desc("Display the PE/COFF export table"));
192
Saleem Abdulrasoolf9578632014-10-07 19:37:52 +0000193 // -coff-directives
194 cl::opt<bool>
195 COFFDirectives("coff-directives",
Rui Ueyama74e85132014-11-19 00:18:07 +0000196 cl::desc("Display the PE/COFF .drectve section"));
197
198 // -coff-basereloc
199 cl::opt<bool>
200 COFFBaseRelocs("coff-basereloc",
201 cl::desc("Display the PE/COFF .reloc section"));
Lang Hames0000afd2015-06-26 23:56:53 +0000202
Reid Kleckner2da433e2016-06-02 17:10:43 +0000203 // -coff-debug-directory
204 cl::opt<bool>
205 COFFDebugDirectory("coff-debug-directory",
206 cl::desc("Display the PE/COFF debug directory"));
207
Davide Italiano07e7acb2015-08-21 20:28:30 +0000208 // -macho-data-in-code
209 cl::opt<bool>
210 MachODataInCode("macho-data-in-code",
211 cl::desc("Display MachO Data in Code command"));
212
Davide Italiano4410b222015-09-03 18:10:28 +0000213 // -macho-indirect-symbols
214 cl::opt<bool>
215 MachOIndirectSymbols("macho-indirect-symbols",
216 cl::desc("Display MachO indirect symbols"));
217
Davide Italiano9a429b72015-09-09 00:21:18 +0000218 // -macho-linker-options
219 cl::opt<bool>
220 MachOLinkerOptions("macho-linker-options",
221 cl::desc("Display MachO linker options"));
222
Davide Italianod1f09962015-09-02 16:24:24 +0000223 // -macho-segment
224 cl::opt<bool>
225 MachOSegment("macho-segment",
226 cl::desc("Display MachO Segment command"));
227
Davide Italiano976f4da2015-08-27 15:11:32 +0000228 // -macho-version-min
229 cl::opt<bool>
230 MachOVersionMin("macho-version-min",
231 cl::desc("Display MachO version min command"));
Davide Italiano35eebe12015-08-31 19:32:31 +0000232
233 // -macho-dysymtab
234 cl::opt<bool>
235 MachODysymtab("macho-dysymtab",
236 cl::desc("Display MachO Dysymtab command"));
237
Lang Hames0000afd2015-06-26 23:56:53 +0000238 // -stackmap
239 cl::opt<bool>
240 PrintStackMap("stackmap",
241 cl::desc("Display contents of stackmap section"));
242
Davide Italiano4f05f322015-10-16 23:19:01 +0000243 // -version-info
244 cl::opt<bool>
245 VersionInfo("version-info",
246 cl::desc("Display ELF version sections (if present)"));
247 cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"),
248 cl::aliasopt(VersionInfo));
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000249
250 cl::opt<bool> SectionGroups("elf-section-groups",
251 cl::desc("Display ELF section group contents"));
252 cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"),
253 cl::aliasopt(SectionGroups));
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000254 cl::opt<bool> HashHistogram(
255 "elf-hash-histogram",
256 cl::desc("Display bucket list histogram for hash sections"));
257 cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"),
258 cl::aliasopt(HashHistogram));
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000259
Tim Northoverd59b23a2016-03-01 21:45:22 +0000260 cl::opt<OutputStyleTy>
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000261 Output("elf-output-style", cl::desc("Specify ELF dump style"),
262 cl::values(clEnumVal(LLVM, "LLVM default style"),
263 clEnumVal(GNU, "GNU readelf style"), clEnumValEnd),
264 cl::init(LLVM));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000265} // namespace opts
266
Rafael Espindola00ddb142015-07-20 03:38:17 +0000267namespace llvm {
268
Davide Italiano1eb92342015-12-04 19:29:49 +0000269LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) {
Davide Italiano0a07a822015-12-23 19:29:34 +0000270 errs() << "\nError reading file: " << Msg << ".\n";
271 errs().flush();
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000272 exit(1);
Rafael Espindola724d4b42015-07-20 03:01:49 +0000273}
274
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000275void error(Error EC) {
Zachary Turner660230e2016-08-04 19:39:55 +0000276 if (!EC)
277 return;
Zachary Turner660230e2016-08-04 19:39:55 +0000278 handleAllErrors(std::move(EC),
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000279 [&](const ErrorInfoBase &EI) { reportError(EI.message()); });
Zachary Turner660230e2016-08-04 19:39:55 +0000280}
281
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000282void error(std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000283 if (!EC)
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000284 return;
Rafael Espindola8b3b09f2015-08-06 21:54:37 +0000285 reportError(EC.message());
Rafael Espindola144af2c2012-12-31 16:05:21 +0000286}
287
Eric Christopher9cad53c2013-04-03 18:31:38 +0000288bool relocAddressLess(RelocationRef a, RelocationRef b) {
Rafael Espindolac7689302015-07-06 15:53:43 +0000289 return a.getOffset() < b.getOffset();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000290}
291
292} // namespace llvm
293
Rafael Espindola4453e42942014-06-13 03:07:50 +0000294static void reportError(StringRef Input, std::error_code EC) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000295 if (Input == "-")
296 Input = "<stdin>";
297
Rafael Espindola724d4b42015-07-20 03:01:49 +0000298 reportError(Twine(Input) + ": " + EC.message());
Eric Christopher9cad53c2013-04-03 18:31:38 +0000299}
300
301static void reportError(StringRef Input, StringRef Message) {
302 if (Input == "-")
303 Input = "<stdin>";
304
Rafael Espindola724d4b42015-07-20 03:01:49 +0000305 reportError(Twine(Input) + ": " + Message);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000306}
307
Lang Hamesfc209622016-07-14 02:24:01 +0000308static void reportError(StringRef Input, Error Err) {
309 if (Input == "-")
310 Input = "<stdin>";
311 std::string ErrMsg;
312 {
313 raw_string_ostream ErrStream(ErrMsg);
314 logAllUnhandledErrors(std::move(Err), ErrStream, Input + ": ");
315 }
316 reportError(ErrMsg);
317}
318
Simon Atanasyan80433902014-06-18 08:47:09 +0000319static bool isMipsArch(unsigned Arch) {
320 switch (Arch) {
321 case llvm::Triple::mips:
322 case llvm::Triple::mipsel:
323 case llvm::Triple::mips64:
324 case llvm::Triple::mips64el:
325 return true;
326 default:
327 return false;
328 }
329}
330
Reid Kleckner0b269742016-05-14 00:02:53 +0000331static llvm::codeview::MemoryTypeTableBuilder CVTypes;
332
Eric Christopher9cad53c2013-04-03 18:31:38 +0000333/// @brief Creates an format-specific object file dumper.
Zachary Turner88bb1632016-05-03 00:28:04 +0000334static std::error_code createDumper(const ObjectFile *Obj,
335 ScopedPrinter &Writer,
Rafael Espindola4453e42942014-06-13 03:07:50 +0000336 std::unique_ptr<ObjDumper> &Result) {
Eric Christopher9cad53c2013-04-03 18:31:38 +0000337 if (!Obj)
338 return readobj_error::unsupported_file_format;
339
340 if (Obj->isCOFF())
341 return createCOFFDumper(Obj, Writer, Result);
342 if (Obj->isELF())
343 return createELFDumper(Obj, Writer, Result);
344 if (Obj->isMachO())
345 return createMachODumper(Obj, Writer, Result);
346
347 return readobj_error::unsupported_obj_file_format;
348}
349
Eric Christopher9cad53c2013-04-03 18:31:38 +0000350/// @brief Dumps the specified object file.
351static void dumpObject(const ObjectFile *Obj) {
Zachary Turner88bb1632016-05-03 00:28:04 +0000352 ScopedPrinter Writer(outs());
Ahmed Charles56440fd2014-03-06 05:51:42 +0000353 std::unique_ptr<ObjDumper> Dumper;
Davide Italianoe66b73f2015-12-05 23:36:52 +0000354 if (std::error_code EC = createDumper(Obj, Writer, Dumper))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000355 reportError(Obj->getFileName(), EC);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000356
Hemant Kulkarnid8a985e2016-02-10 20:40:55 +0000357 if (opts::Output == opts::LLVM) {
358 outs() << '\n';
359 outs() << "File: " << Obj->getFileName() << "\n";
360 outs() << "Format: " << Obj->getFileFormatName() << "\n";
361 outs() << "Arch: " << Triple::getArchTypeName(
362 (llvm::Triple::ArchType)Obj->getArch()) << "\n";
363 outs() << "AddressSize: " << (8 * Obj->getBytesInAddress()) << "bit\n";
364 Dumper->printLoadName();
365 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000366
367 if (opts::FileHeaders)
368 Dumper->printFileHeaders();
369 if (opts::Sections)
370 Dumper->printSections();
371 if (opts::Relocations)
372 Dumper->printRelocations();
Michael J. Spencer594c0282015-06-25 21:47:32 +0000373 if (opts::DynRelocs)
374 Dumper->printDynamicRelocations();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000375 if (opts::Symbols)
376 Dumper->printSymbols();
377 if (opts::DynamicSymbols)
378 Dumper->printDynamicSymbols();
379 if (opts::UnwindInfo)
380 Dumper->printUnwindInfo();
381 if (opts::DynamicTable)
382 Dumper->printDynamicTable();
383 if (opts::NeededLibraries)
384 Dumper->printNeededLibraries();
Nico Rieckd6df0542013-04-12 04:07:39 +0000385 if (opts::ProgramHeaders)
386 Dumper->printProgramHeaders();
Michael J. Spencer20546ff2015-07-09 22:32:24 +0000387 if (opts::HashTable)
388 Dumper->printHashTable();
Igor Kudrin496fb2f2015-10-14 12:11:50 +0000389 if (opts::GnuHashTable)
390 Dumper->printGnuHashTable();
Davide Italiano4f05f322015-10-16 23:19:01 +0000391 if (opts::VersionInfo)
392 Dumper->printVersionInfo();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000393 if (Obj->isELF()) {
394 if (Obj->getArch() == llvm::Triple::arm)
395 if (opts::ARMAttributes)
396 Dumper->printAttributes();
397 if (isMipsArch(Obj->getArch())) {
398 if (opts::MipsPLTGOT)
399 Dumper->printMipsPLTGOT();
400 if (opts::MipsABIFlags)
401 Dumper->printMipsABIFlags();
402 if (opts::MipsReginfo)
403 Dumper->printMipsReginfo();
Simon Atanasyan8a71b532016-05-04 05:58:57 +0000404 if (opts::MipsOptions)
405 Dumper->printMipsOptions();
Hemant Kulkarniab4a46f2016-01-26 19:46:39 +0000406 }
407 if (opts::SectionGroups)
408 Dumper->printGroupSections();
Hemant Kulkarni9b1b7f02016-04-11 17:15:30 +0000409 if (opts::HashHistogram)
410 Dumper->printHashHistogram();
Simon Atanasyanc914de22015-05-07 15:40:35 +0000411 }
Davide Italianocd1b6db2015-07-24 02:14:20 +0000412 if (Obj->isCOFF()) {
413 if (opts::COFFImports)
414 Dumper->printCOFFImports();
415 if (opts::COFFExports)
416 Dumper->printCOFFExports();
417 if (opts::COFFDirectives)
418 Dumper->printCOFFDirectives();
419 if (opts::COFFBaseRelocs)
420 Dumper->printCOFFBaseReloc();
Reid Kleckner2da433e2016-06-02 17:10:43 +0000421 if (opts::COFFDebugDirectory)
422 Dumper->printCOFFDebugDirectory();
Reid Kleckner83ebad32015-12-16 18:28:12 +0000423 if (opts::CodeView)
424 Dumper->printCodeViewDebugInfo();
Reid Kleckner0b269742016-05-14 00:02:53 +0000425 if (opts::CodeViewMergedTypes)
426 Dumper->mergeCodeViewTypes(CVTypes);
Davide Italianocd1b6db2015-07-24 02:14:20 +0000427 }
Davide Italiano1e12fc42015-08-31 17:12:23 +0000428 if (Obj->isMachO()) {
Davide Italiano07e7acb2015-08-21 20:28:30 +0000429 if (opts::MachODataInCode)
430 Dumper->printMachODataInCode();
Davide Italiano4410b222015-09-03 18:10:28 +0000431 if (opts::MachOIndirectSymbols)
432 Dumper->printMachOIndirectSymbols();
Davide Italiano9a429b72015-09-09 00:21:18 +0000433 if (opts::MachOLinkerOptions)
434 Dumper->printMachOLinkerOptions();
Davide Italianod1f09962015-09-02 16:24:24 +0000435 if (opts::MachOSegment)
436 Dumper->printMachOSegment();
Davide Italiano976f4da2015-08-27 15:11:32 +0000437 if (opts::MachOVersionMin)
438 Dumper->printMachOVersionMin();
Davide Italiano35eebe12015-08-31 19:32:31 +0000439 if (opts::MachODysymtab)
440 Dumper->printMachODysymtab();
Davide Italiano1e12fc42015-08-31 17:12:23 +0000441 }
Lang Hames0000afd2015-06-26 23:56:53 +0000442 if (opts::PrintStackMap)
443 Dumper->printStackMap();
Rafael Espindola21bd8412012-12-31 16:29:44 +0000444}
445
Eric Christopher9cad53c2013-04-03 18:31:38 +0000446/// @brief Dumps each object file in \a Arc;
447static void dumpArchive(const Archive *Arc) {
Lang Hamesfc209622016-07-14 02:24:01 +0000448 Error Err;
449 for (auto &Child : Arc->children(Err)) {
Kevin Enderbyac9e1552016-05-17 17:10:12 +0000450 Expected<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary();
451 if (!ChildOrErr) {
452 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
453 std::string Buf;
454 raw_string_ostream OS(Buf);
455 logAllUnhandledErrors(ChildOrErr.takeError(), OS, "");
456 OS.flush();
457 reportError(Arc->getFileName(), Buf);
458 }
Eric Christopher9cad53c2013-04-03 18:31:38 +0000459 continue;
David Meyer6c614bf2012-03-09 20:59:52 +0000460 }
Rafael Espindolaae460022014-06-16 16:08:36 +0000461 if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get()))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000462 dumpObject(Obj);
Saleem Abdulrasool3780b3a2016-08-18 14:32:11 +0000463 else if (COFFImportFile *Imp = dyn_cast<COFFImportFile>(&*ChildOrErr.get()))
464 dumpCOFFImportFile(Imp);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000465 else
466 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
467 }
Lang Hamesfc209622016-07-14 02:24:01 +0000468 if (Err)
469 reportError(Arc->getFileName(), std::move(Err));
Eric Christopher9cad53c2013-04-03 18:31:38 +0000470}
471
Rafael Espindola8448a242015-03-24 20:26:55 +0000472/// @brief Dumps each object file in \a MachO Universal Binary;
473static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) {
474 for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) {
Kevin Enderby9acb1092016-05-31 20:35:34 +0000475 Expected<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile();
David Blaikie29ac43c2015-04-13 16:05:49 +0000476 if (ObjOrErr)
477 dumpObject(&*ObjOrErr.get());
Kevin Enderby9acb1092016-05-31 20:35:34 +0000478 else if (auto E = isNotObjectErrorInvalidFileType(ObjOrErr.takeError())) {
479 std::string Buf;
480 raw_string_ostream OS(Buf);
481 logAllUnhandledErrors(ObjOrErr.takeError(), OS, "");
482 OS.flush();
483 reportError(UBinary->getFileName(), Buf);
484 }
Kevin Enderby42398052016-06-28 23:16:13 +0000485 else if (Expected<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive())
David Blaikie29ac43c2015-04-13 16:05:49 +0000486 dumpArchive(&*AOrErr.get());
Rafael Espindola8448a242015-03-24 20:26:55 +0000487 }
488}
Eric Christopher9cad53c2013-04-03 18:31:38 +0000489
490/// @brief Opens \a File and dumps it.
491static void dumpInput(StringRef File) {
David Meyer2fc34c52012-03-01 01:36:50 +0000492
Eric Christopher9cad53c2013-04-03 18:31:38 +0000493 // Attempt to open the binary.
Kevin Enderby3fcdf6a2016-04-06 22:14:09 +0000494 Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
495 if (!BinaryOrErr)
496 reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
Rafael Espindola48af1c22014-08-19 18:44:46 +0000497 Binary &Binary = *BinaryOrErr.get().getBinary();
Eric Christopher9cad53c2013-04-03 18:31:38 +0000498
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000499 if (Archive *Arc = dyn_cast<Archive>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000500 dumpArchive(Arc);
Rafael Espindola8448a242015-03-24 20:26:55 +0000501 else if (MachOUniversalBinary *UBinary =
502 dyn_cast<MachOUniversalBinary>(&Binary))
503 dumpMachOUniversalBinary(UBinary);
Rafael Espindola3f6481d2014-08-01 14:31:55 +0000504 else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary))
Eric Christopher9cad53c2013-04-03 18:31:38 +0000505 dumpObject(Obj);
Rui Ueyama71ba9bd2015-08-28 07:40:30 +0000506 else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary))
507 dumpCOFFImportFile(Import);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000508 else
509 reportError(File, readobj_error::unrecognized_file_format);
Rafael Espindola21bd8412012-12-31 16:29:44 +0000510}
511
Eric Christopher9cad53c2013-04-03 18:31:38 +0000512int main(int argc, const char *argv[]) {
Richard Smith2ad6d482016-06-09 00:53:21 +0000513 sys::PrintStackTraceOnErrorSignal(argv[0]);
David Meyer2fc34c52012-03-01 01:36:50 +0000514 PrettyStackTraceProgram X(argc, argv);
Eric Christopher9cad53c2013-04-03 18:31:38 +0000515 llvm_shutdown_obj Y;
David Meyer2fc34c52012-03-01 01:36:50 +0000516
Eric Christopher9cad53c2013-04-03 18:31:38 +0000517 // Register the target printer for --version.
518 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
David Meyer2fc34c52012-03-01 01:36:50 +0000519
Eric Christopher9cad53c2013-04-03 18:31:38 +0000520 cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n");
David Meyer2fc34c52012-03-01 01:36:50 +0000521
Eric Christopher9cad53c2013-04-03 18:31:38 +0000522 // Default to stdin if no filename is specified.
523 if (opts::InputFilenames.size() == 0)
524 opts::InputFilenames.push_back("-");
David Meyer2fc34c52012-03-01 01:36:50 +0000525
Eric Christopher9cad53c2013-04-03 18:31:38 +0000526 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
527 dumpInput);
Rafael Espindola278e8912012-12-31 16:53:01 +0000528
Reid Kleckner0b269742016-05-14 00:02:53 +0000529 if (opts::CodeViewMergedTypes) {
530 ScopedPrinter W(outs());
531 dumpCodeViewMergedTypes(W, CVTypes);
532 }
533
Rafael Espindolafb3acd62015-07-20 03:23:55 +0000534 return 0;
David Meyer2fc34c52012-03-01 01:36:50 +0000535}