Benjamin Kramer | 054f422 | 2013-08-09 10:31:14 +0000 | [diff] [blame] | 1 | //===- llvm-readobj.cpp - Dump contents of an Object File -----------------===// |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 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 | // |
Michael J. Spencer | d7e7003 | 2013-02-05 20:27:22 +0000 | [diff] [blame] | 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. |
David Meyer | ae11a78 | 2012-03-02 23:43:51 +0000 | [diff] [blame] | 13 | // |
Michael J. Spencer | d7e7003 | 2013-02-05 20:27:22 +0000 | [diff] [blame] | 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. |
David Meyer | ae11a78 | 2012-03-02 23:43:51 +0000 | [diff] [blame] | 17 | // |
Michael J. Spencer | d7e7003 | 2013-02-05 20:27:22 +0000 | [diff] [blame] | 18 | // Output should be specialized for each format where appropriate. |
David Meyer | ae11a78 | 2012-03-02 23:43:51 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 21 | |
Michael J. Spencer | 6a8746b | 2013-02-20 02:37:12 +0000 | [diff] [blame] | 22 | #include "llvm-readobj.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 23 | #include "Error.h" |
| 24 | #include "ObjDumper.h" |
| 25 | #include "StreamWriter.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 26 | #include "llvm/Object/Archive.h" |
Rui Ueyama | 71ba9bd | 2015-08-28 07:40:30 +0000 | [diff] [blame] | 27 | #include "llvm/Object/COFFImportFile.h" |
Rafael Espindola | 6762231 | 2014-08-08 16:39:22 +0000 | [diff] [blame] | 28 | #include "llvm/Object/ELFObjectFile.h" |
Rafael Espindola | 8448a24 | 2015-03-24 20:26:55 +0000 | [diff] [blame] | 29 | #include "llvm/Object/MachOUniversal.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 30 | #include "llvm/Object/ObjectFile.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Casting.h" |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 32 | #include "llvm/Support/CommandLine.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 33 | #include "llvm/Support/DataTypes.h" |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Debug.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 35 | #include "llvm/Support/FileSystem.h" |
| 36 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 37 | #include "llvm/Support/PrettyStackTrace.h" |
| 38 | #include "llvm/Support/Signals.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 39 | #include "llvm/Support/TargetRegistry.h" |
| 40 | #include "llvm/Support/TargetSelect.h" |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 41 | #include <string> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 42 | #include <system_error> |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 43 | |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 44 | using namespace llvm; |
| 45 | using namespace llvm::object; |
| 46 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 47 | namespace opts { |
| 48 | cl::list<std::string> InputFilenames(cl::Positional, |
| 49 | cl::desc("<input object files>"), |
| 50 | cl::ZeroOrMore); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 51 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 52 | // -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. Spencer | 594c028 | 2015-06-25 21:47:32 +0000 | [diff] [blame] | 94 | // -dyn-relocations |
| 95 | cl::opt<bool> DynRelocs("dyn-relocations", |
| 96 | cl::desc("Display the dynamic relocation entries in the file")); |
| 97 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 98 | // -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 Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 126 | |
Nico Rieck | d6df054 | 2013-04-12 04:07:39 +0000 | [diff] [blame] | 127 | // -program-headers |
| 128 | cl::opt<bool> ProgramHeaders("program-headers", |
| 129 | cl::desc("Display ELF program headers")); |
| 130 | |
Michael J. Spencer | 20546ff | 2015-07-09 22:32:24 +0000 | [diff] [blame] | 131 | // -hash-table |
| 132 | cl::opt<bool> HashTable("hash-table", |
| 133 | cl::desc("Display ELF hash table")); |
| 134 | |
Igor Kudrin | 496fb2f | 2015-10-14 12:11:50 +0000 | [diff] [blame] | 135 | // -gnu-hash-table |
| 136 | cl::opt<bool> GnuHashTable("gnu-hash-table", |
| 137 | cl::desc("Display ELF .gnu.hash section")); |
| 138 | |
Nico Rieck | f3f0b79 | 2013-04-12 04:01:52 +0000 | [diff] [blame] | 139 | // -expand-relocs |
| 140 | cl::opt<bool> ExpandRelocs("expand-relocs", |
| 141 | cl::desc("Expand each shown relocation to multiple lines")); |
Timur Iskhodzhanov | 48703be | 2013-12-19 11:37:14 +0000 | [diff] [blame] | 142 | |
Zachary Turner | 99f0215 | 2015-02-18 19:32:05 +0000 | [diff] [blame] | 143 | // -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 Abdulrasool | 15d16d8 | 2014-01-30 04:46:33 +0000 | [diff] [blame] | 151 | |
| 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 Atanasyan | 8043390 | 2014-06-18 08:47:09 +0000 | [diff] [blame] | 157 | |
| 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 Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 162 | |
Simon Atanasyan | c914de2 | 2015-05-07 15:40:35 +0000 | [diff] [blame] | 163 | // -mips-abi-flags |
| 164 | cl::opt<bool> MipsABIFlags("mips-abi-flags", |
| 165 | cl::desc("Display the MIPS.abiflags section")); |
| 166 | |
Simon Atanasyan | 6e07e93 | 2015-06-16 21:47:43 +0000 | [diff] [blame] | 167 | // -mips-reginfo |
| 168 | cl::opt<bool> MipsReginfo("mips-reginfo", |
| 169 | cl::desc("Display the MIPS .reginfo section")); |
| 170 | |
Rui Ueyama | 1e152d5 | 2014-10-02 17:02:18 +0000 | [diff] [blame] | 171 | // -coff-imports |
| 172 | cl::opt<bool> |
| 173 | COFFImports("coff-imports", cl::desc("Display the PE/COFF import table")); |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 174 | |
Saleem Abdulrasool | ddd9264 | 2015-01-03 21:35:09 +0000 | [diff] [blame] | 175 | // -coff-exports |
| 176 | cl::opt<bool> |
| 177 | COFFExports("coff-exports", cl::desc("Display the PE/COFF export table")); |
| 178 | |
Saleem Abdulrasool | f957863 | 2014-10-07 19:37:52 +0000 | [diff] [blame] | 179 | // -coff-directives |
| 180 | cl::opt<bool> |
| 181 | COFFDirectives("coff-directives", |
Rui Ueyama | 74e8513 | 2014-11-19 00:18:07 +0000 | [diff] [blame] | 182 | cl::desc("Display the PE/COFF .drectve section")); |
| 183 | |
| 184 | // -coff-basereloc |
| 185 | cl::opt<bool> |
| 186 | COFFBaseRelocs("coff-basereloc", |
| 187 | cl::desc("Display the PE/COFF .reloc section")); |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 188 | |
Davide Italiano | 07e7acb | 2015-08-21 20:28:30 +0000 | [diff] [blame] | 189 | // -macho-data-in-code |
| 190 | cl::opt<bool> |
| 191 | MachODataInCode("macho-data-in-code", |
| 192 | cl::desc("Display MachO Data in Code command")); |
| 193 | |
Davide Italiano | 4410b22 | 2015-09-03 18:10:28 +0000 | [diff] [blame] | 194 | // -macho-indirect-symbols |
| 195 | cl::opt<bool> |
| 196 | MachOIndirectSymbols("macho-indirect-symbols", |
| 197 | cl::desc("Display MachO indirect symbols")); |
| 198 | |
Davide Italiano | 9a429b7 | 2015-09-09 00:21:18 +0000 | [diff] [blame] | 199 | // -macho-linker-options |
| 200 | cl::opt<bool> |
| 201 | MachOLinkerOptions("macho-linker-options", |
| 202 | cl::desc("Display MachO linker options")); |
| 203 | |
Davide Italiano | d1f0996 | 2015-09-02 16:24:24 +0000 | [diff] [blame] | 204 | // -macho-segment |
| 205 | cl::opt<bool> |
| 206 | MachOSegment("macho-segment", |
| 207 | cl::desc("Display MachO Segment command")); |
| 208 | |
Davide Italiano | 976f4da | 2015-08-27 15:11:32 +0000 | [diff] [blame] | 209 | // -macho-version-min |
| 210 | cl::opt<bool> |
| 211 | MachOVersionMin("macho-version-min", |
| 212 | cl::desc("Display MachO version min command")); |
Davide Italiano | 35eebe1 | 2015-08-31 19:32:31 +0000 | [diff] [blame] | 213 | |
| 214 | // -macho-dysymtab |
| 215 | cl::opt<bool> |
| 216 | MachODysymtab("macho-dysymtab", |
| 217 | cl::desc("Display MachO Dysymtab command")); |
| 218 | |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 219 | // -stackmap |
| 220 | cl::opt<bool> |
| 221 | PrintStackMap("stackmap", |
| 222 | cl::desc("Display contents of stackmap section")); |
| 223 | |
Davide Italiano | 4f05f32 | 2015-10-16 23:19:01 +0000 | [diff] [blame] | 224 | // -version-info |
| 225 | cl::opt<bool> |
| 226 | VersionInfo("version-info", |
| 227 | cl::desc("Display ELF version sections (if present)")); |
| 228 | cl::alias VersionInfoShort("V", cl::desc("Alias for -version-info"), |
| 229 | cl::aliasopt(VersionInfo)); |
Hemant Kulkarni | ab4a46f | 2016-01-26 19:46:39 +0000 | [diff] [blame] | 230 | |
| 231 | cl::opt<bool> SectionGroups("elf-section-groups", |
| 232 | cl::desc("Display ELF section group contents")); |
| 233 | cl::alias SectionGroupsShort("g", cl::desc("Alias for -elf-sections-groups"), |
| 234 | cl::aliasopt(SectionGroups)); |
Hemant Kulkarni | 9b1b7f0 | 2016-04-11 17:15:30 +0000 | [diff] [blame] | 235 | cl::opt<bool> HashHistogram( |
| 236 | "elf-hash-histogram", |
| 237 | cl::desc("Display bucket list histogram for hash sections")); |
| 238 | cl::alias HashHistogramShort("I", cl::desc("Alias for -elf-hash-histogram"), |
| 239 | cl::aliasopt(HashHistogram)); |
Hemant Kulkarni | ab4a46f | 2016-01-26 19:46:39 +0000 | [diff] [blame] | 240 | |
Tim Northover | d59b23a | 2016-03-01 21:45:22 +0000 | [diff] [blame] | 241 | cl::opt<OutputStyleTy> |
Hemant Kulkarni | d8a985e | 2016-02-10 20:40:55 +0000 | [diff] [blame] | 242 | Output("elf-output-style", cl::desc("Specify ELF dump style"), |
| 243 | cl::values(clEnumVal(LLVM, "LLVM default style"), |
| 244 | clEnumVal(GNU, "GNU readelf style"), clEnumValEnd), |
| 245 | cl::init(LLVM)); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 246 | } // namespace opts |
| 247 | |
Rafael Espindola | 00ddb14 | 2015-07-20 03:38:17 +0000 | [diff] [blame] | 248 | namespace llvm { |
| 249 | |
Davide Italiano | 1eb9234 | 2015-12-04 19:29:49 +0000 | [diff] [blame] | 250 | LLVM_ATTRIBUTE_NORETURN void reportError(Twine Msg) { |
Davide Italiano | 0a07a82 | 2015-12-23 19:29:34 +0000 | [diff] [blame] | 251 | errs() << "\nError reading file: " << Msg << ".\n"; |
| 252 | errs().flush(); |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 253 | exit(1); |
Rafael Espindola | 724d4b4 | 2015-07-20 03:01:49 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 256 | void error(std::error_code EC) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 257 | if (!EC) |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 258 | return; |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 259 | |
Rafael Espindola | 8b3b09f | 2015-08-06 21:54:37 +0000 | [diff] [blame] | 260 | reportError(EC.message()); |
Rafael Espindola | 144af2c | 2012-12-31 16:05:21 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 263 | bool relocAddressLess(RelocationRef a, RelocationRef b) { |
Rafael Espindola | c768930 | 2015-07-06 15:53:43 +0000 | [diff] [blame] | 264 | return a.getOffset() < b.getOffset(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | } // namespace llvm |
| 268 | |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 269 | static void reportError(StringRef Input, std::error_code EC) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 270 | if (Input == "-") |
| 271 | Input = "<stdin>"; |
| 272 | |
Rafael Espindola | 724d4b4 | 2015-07-20 03:01:49 +0000 | [diff] [blame] | 273 | reportError(Twine(Input) + ": " + EC.message()); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static void reportError(StringRef Input, StringRef Message) { |
| 277 | if (Input == "-") |
| 278 | Input = "<stdin>"; |
| 279 | |
Rafael Espindola | 724d4b4 | 2015-07-20 03:01:49 +0000 | [diff] [blame] | 280 | reportError(Twine(Input) + ": " + Message); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Simon Atanasyan | 8043390 | 2014-06-18 08:47:09 +0000 | [diff] [blame] | 283 | static bool isMipsArch(unsigned Arch) { |
| 284 | switch (Arch) { |
| 285 | case llvm::Triple::mips: |
| 286 | case llvm::Triple::mipsel: |
| 287 | case llvm::Triple::mips64: |
| 288 | case llvm::Triple::mips64el: |
| 289 | return true; |
| 290 | default: |
| 291 | return false; |
| 292 | } |
| 293 | } |
| 294 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 295 | /// @brief Creates an format-specific object file dumper. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 296 | static std::error_code createDumper(const ObjectFile *Obj, StreamWriter &Writer, |
| 297 | std::unique_ptr<ObjDumper> &Result) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 298 | if (!Obj) |
| 299 | return readobj_error::unsupported_file_format; |
| 300 | |
| 301 | if (Obj->isCOFF()) |
| 302 | return createCOFFDumper(Obj, Writer, Result); |
| 303 | if (Obj->isELF()) |
| 304 | return createELFDumper(Obj, Writer, Result); |
| 305 | if (Obj->isMachO()) |
| 306 | return createMachODumper(Obj, Writer, Result); |
| 307 | |
| 308 | return readobj_error::unsupported_obj_file_format; |
| 309 | } |
| 310 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 311 | /// @brief Dumps the specified object file. |
| 312 | static void dumpObject(const ObjectFile *Obj) { |
| 313 | StreamWriter Writer(outs()); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 314 | std::unique_ptr<ObjDumper> Dumper; |
Davide Italiano | e66b73f | 2015-12-05 23:36:52 +0000 | [diff] [blame] | 315 | if (std::error_code EC = createDumper(Obj, Writer, Dumper)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 316 | reportError(Obj->getFileName(), EC); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 317 | |
Hemant Kulkarni | d8a985e | 2016-02-10 20:40:55 +0000 | [diff] [blame] | 318 | if (opts::Output == opts::LLVM) { |
| 319 | outs() << '\n'; |
| 320 | outs() << "File: " << Obj->getFileName() << "\n"; |
| 321 | outs() << "Format: " << Obj->getFileFormatName() << "\n"; |
| 322 | outs() << "Arch: " << Triple::getArchTypeName( |
| 323 | (llvm::Triple::ArchType)Obj->getArch()) << "\n"; |
| 324 | outs() << "AddressSize: " << (8 * Obj->getBytesInAddress()) << "bit\n"; |
| 325 | Dumper->printLoadName(); |
| 326 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 327 | |
| 328 | if (opts::FileHeaders) |
| 329 | Dumper->printFileHeaders(); |
| 330 | if (opts::Sections) |
| 331 | Dumper->printSections(); |
| 332 | if (opts::Relocations) |
| 333 | Dumper->printRelocations(); |
Michael J. Spencer | 594c028 | 2015-06-25 21:47:32 +0000 | [diff] [blame] | 334 | if (opts::DynRelocs) |
| 335 | Dumper->printDynamicRelocations(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 336 | if (opts::Symbols) |
| 337 | Dumper->printSymbols(); |
| 338 | if (opts::DynamicSymbols) |
| 339 | Dumper->printDynamicSymbols(); |
| 340 | if (opts::UnwindInfo) |
| 341 | Dumper->printUnwindInfo(); |
| 342 | if (opts::DynamicTable) |
| 343 | Dumper->printDynamicTable(); |
| 344 | if (opts::NeededLibraries) |
| 345 | Dumper->printNeededLibraries(); |
Nico Rieck | d6df054 | 2013-04-12 04:07:39 +0000 | [diff] [blame] | 346 | if (opts::ProgramHeaders) |
| 347 | Dumper->printProgramHeaders(); |
Michael J. Spencer | 20546ff | 2015-07-09 22:32:24 +0000 | [diff] [blame] | 348 | if (opts::HashTable) |
| 349 | Dumper->printHashTable(); |
Igor Kudrin | 496fb2f | 2015-10-14 12:11:50 +0000 | [diff] [blame] | 350 | if (opts::GnuHashTable) |
| 351 | Dumper->printGnuHashTable(); |
Davide Italiano | 4f05f32 | 2015-10-16 23:19:01 +0000 | [diff] [blame] | 352 | if (opts::VersionInfo) |
| 353 | Dumper->printVersionInfo(); |
Hemant Kulkarni | ab4a46f | 2016-01-26 19:46:39 +0000 | [diff] [blame] | 354 | if (Obj->isELF()) { |
| 355 | if (Obj->getArch() == llvm::Triple::arm) |
| 356 | if (opts::ARMAttributes) |
| 357 | Dumper->printAttributes(); |
| 358 | if (isMipsArch(Obj->getArch())) { |
| 359 | if (opts::MipsPLTGOT) |
| 360 | Dumper->printMipsPLTGOT(); |
| 361 | if (opts::MipsABIFlags) |
| 362 | Dumper->printMipsABIFlags(); |
| 363 | if (opts::MipsReginfo) |
| 364 | Dumper->printMipsReginfo(); |
| 365 | } |
| 366 | if (opts::SectionGroups) |
| 367 | Dumper->printGroupSections(); |
Hemant Kulkarni | 9b1b7f0 | 2016-04-11 17:15:30 +0000 | [diff] [blame] | 368 | if (opts::HashHistogram) |
| 369 | Dumper->printHashHistogram(); |
Simon Atanasyan | c914de2 | 2015-05-07 15:40:35 +0000 | [diff] [blame] | 370 | } |
Davide Italiano | cd1b6db | 2015-07-24 02:14:20 +0000 | [diff] [blame] | 371 | if (Obj->isCOFF()) { |
| 372 | if (opts::COFFImports) |
| 373 | Dumper->printCOFFImports(); |
| 374 | if (opts::COFFExports) |
| 375 | Dumper->printCOFFExports(); |
| 376 | if (opts::COFFDirectives) |
| 377 | Dumper->printCOFFDirectives(); |
| 378 | if (opts::COFFBaseRelocs) |
| 379 | Dumper->printCOFFBaseReloc(); |
Reid Kleckner | 83ebad3 | 2015-12-16 18:28:12 +0000 | [diff] [blame] | 380 | if (opts::CodeView) |
| 381 | Dumper->printCodeViewDebugInfo(); |
Davide Italiano | cd1b6db | 2015-07-24 02:14:20 +0000 | [diff] [blame] | 382 | } |
Davide Italiano | 1e12fc4 | 2015-08-31 17:12:23 +0000 | [diff] [blame] | 383 | if (Obj->isMachO()) { |
Davide Italiano | 07e7acb | 2015-08-21 20:28:30 +0000 | [diff] [blame] | 384 | if (opts::MachODataInCode) |
| 385 | Dumper->printMachODataInCode(); |
Davide Italiano | 4410b22 | 2015-09-03 18:10:28 +0000 | [diff] [blame] | 386 | if (opts::MachOIndirectSymbols) |
| 387 | Dumper->printMachOIndirectSymbols(); |
Davide Italiano | 9a429b7 | 2015-09-09 00:21:18 +0000 | [diff] [blame] | 388 | if (opts::MachOLinkerOptions) |
| 389 | Dumper->printMachOLinkerOptions(); |
Davide Italiano | d1f0996 | 2015-09-02 16:24:24 +0000 | [diff] [blame] | 390 | if (opts::MachOSegment) |
| 391 | Dumper->printMachOSegment(); |
Davide Italiano | 976f4da | 2015-08-27 15:11:32 +0000 | [diff] [blame] | 392 | if (opts::MachOVersionMin) |
| 393 | Dumper->printMachOVersionMin(); |
Davide Italiano | 35eebe1 | 2015-08-31 19:32:31 +0000 | [diff] [blame] | 394 | if (opts::MachODysymtab) |
| 395 | Dumper->printMachODysymtab(); |
Davide Italiano | 1e12fc4 | 2015-08-31 17:12:23 +0000 | [diff] [blame] | 396 | } |
Lang Hames | 0000afd | 2015-06-26 23:56:53 +0000 | [diff] [blame] | 397 | if (opts::PrintStackMap) |
| 398 | Dumper->printStackMap(); |
Rafael Espindola | 21bd841 | 2012-12-31 16:29:44 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 401 | /// @brief Dumps each object file in \a Arc; |
| 402 | static void dumpArchive(const Archive *Arc) { |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 403 | for (auto &ErrorOrChild : Arc->children()) { |
| 404 | if (std::error_code EC = ErrorOrChild.getError()) |
| 405 | reportError(Arc->getFileName(), EC.message()); |
| 406 | const auto &Child = *ErrorOrChild; |
Davide Italiano | db8803b | 2015-08-07 00:35:28 +0000 | [diff] [blame] | 407 | ErrorOr<std::unique_ptr<Binary>> ChildOrErr = Child.getAsBinary(); |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 408 | if (std::error_code EC = ChildOrErr.getError()) { |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 409 | // Ignore non-object files. |
| 410 | if (EC != object_error::invalid_file_type) |
| 411 | reportError(Arc->getFileName(), EC.message()); |
| 412 | continue; |
David Meyer | 6c614bf | 2012-03-09 20:59:52 +0000 | [diff] [blame] | 413 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 414 | |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 415 | if (ObjectFile *Obj = dyn_cast<ObjectFile>(&*ChildOrErr.get())) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 416 | dumpObject(Obj); |
| 417 | else |
| 418 | reportError(Arc->getFileName(), readobj_error::unrecognized_file_format); |
| 419 | } |
| 420 | } |
| 421 | |
Rafael Espindola | 8448a24 | 2015-03-24 20:26:55 +0000 | [diff] [blame] | 422 | /// @brief Dumps each object file in \a MachO Universal Binary; |
| 423 | static void dumpMachOUniversalBinary(const MachOUniversalBinary *UBinary) { |
| 424 | for (const MachOUniversalBinary::ObjectForArch &Obj : UBinary->objects()) { |
| 425 | ErrorOr<std::unique_ptr<MachOObjectFile>> ObjOrErr = Obj.getAsObjectFile(); |
David Blaikie | 29ac43c | 2015-04-13 16:05:49 +0000 | [diff] [blame] | 426 | if (ObjOrErr) |
| 427 | dumpObject(&*ObjOrErr.get()); |
| 428 | else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = Obj.getAsArchive()) |
| 429 | dumpArchive(&*AOrErr.get()); |
| 430 | else |
| 431 | reportError(UBinary->getFileName(), ObjOrErr.getError().message()); |
Rafael Espindola | 8448a24 | 2015-03-24 20:26:55 +0000 | [diff] [blame] | 432 | } |
| 433 | } |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 434 | |
| 435 | /// @brief Opens \a File and dumps it. |
| 436 | static void dumpInput(StringRef File) { |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 437 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 438 | // Attempt to open the binary. |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 439 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File); |
| 440 | if (!BinaryOrErr) |
| 441 | reportError(File, errorToErrorCode(BinaryOrErr.takeError())); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 442 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 443 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 444 | if (Archive *Arc = dyn_cast<Archive>(&Binary)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 445 | dumpArchive(Arc); |
Rafael Espindola | 8448a24 | 2015-03-24 20:26:55 +0000 | [diff] [blame] | 446 | else if (MachOUniversalBinary *UBinary = |
| 447 | dyn_cast<MachOUniversalBinary>(&Binary)) |
| 448 | dumpMachOUniversalBinary(UBinary); |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 449 | else if (ObjectFile *Obj = dyn_cast<ObjectFile>(&Binary)) |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 450 | dumpObject(Obj); |
Rui Ueyama | 71ba9bd | 2015-08-28 07:40:30 +0000 | [diff] [blame] | 451 | else if (COFFImportFile *Import = dyn_cast<COFFImportFile>(&Binary)) |
| 452 | dumpCOFFImportFile(Import); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 453 | else |
| 454 | reportError(File, readobj_error::unrecognized_file_format); |
Rafael Espindola | 21bd841 | 2012-12-31 16:29:44 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 457 | int main(int argc, const char *argv[]) { |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 458 | sys::PrintStackTraceOnErrorSignal(); |
| 459 | PrettyStackTraceProgram X(argc, argv); |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 460 | llvm_shutdown_obj Y; |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 461 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 462 | // Register the target printer for --version. |
| 463 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 464 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 465 | cl::ParseCommandLineOptions(argc, argv, "LLVM Object Reader\n"); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 466 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 467 | // Default to stdin if no filename is specified. |
| 468 | if (opts::InputFilenames.size() == 0) |
| 469 | opts::InputFilenames.push_back("-"); |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 470 | |
Eric Christopher | 9cad53c | 2013-04-03 18:31:38 +0000 | [diff] [blame] | 471 | std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(), |
| 472 | dumpInput); |
Rafael Espindola | 278e891 | 2012-12-31 16:53:01 +0000 | [diff] [blame] | 473 | |
Rafael Espindola | fb3acd6 | 2015-07-20 03:23:55 +0000 | [diff] [blame] | 474 | return 0; |
David Meyer | 2fc34c5 | 2012-03-01 01:36:50 +0000 | [diff] [blame] | 475 | } |