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