Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1 | //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This program is a utility that works like binutils "objdump", that is, it |
| 11 | // dumps out a plethora of information about an object file depending on the |
| 12 | // flags. |
| 13 | // |
Michael J. Spencer | d7e7003 | 2013-02-05 20:27:22 +0000 | [diff] [blame] | 14 | // The flags and output of this program should be near identical to those of |
| 15 | // binutils objdump. |
| 16 | // |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 17 | //===----------------------------------------------------------------------===// |
| 18 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 19 | #include "llvm-objdump.h" |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/Optional.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/STLExtras.h" |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/StringExtras.h" |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSet.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Triple.h" |
Sanjoy Das | 3f1bc3b | 2015-06-23 20:09:03 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/FaultMaps.h" |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/Symbolize/Symbolize.h" |
Paul Semel | 007dedb | 2018-07-18 16:39:21 +0000 | [diff] [blame] | 28 | #include "llvm/Demangle/Demangle.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCAsmInfo.h" |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | f57c197 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 32 | #include "llvm/MC/MCDisassembler/MCRelocationInfo.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInst.h" |
| 34 | #include "llvm/MC/MCInstPrinter.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCInstrAnalysis.h" |
Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCInstrInfo.h" |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 37 | #include "llvm/MC/MCObjectFileInfo.h" |
Jim Grosbach | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 38 | #include "llvm/MC/MCRegisterInfo.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 39 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 40 | #include "llvm/Object/Archive.h" |
| 41 | #include "llvm/Object/COFF.h" |
Saleem Abdulrasool | c6bf547 | 2016-08-18 16:39:19 +0000 | [diff] [blame] | 42 | #include "llvm/Object/COFFImportFile.h" |
Benjamin Kramer | f57c197 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 43 | #include "llvm/Object/ELFObjectFile.h" |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 44 | #include "llvm/Object/MachO.h" |
Dave Lee | 3fb120f | 2018-08-03 00:06:38 +0000 | [diff] [blame] | 45 | #include "llvm/Object/MachOUniversal.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 46 | #include "llvm/Object/ObjectFile.h" |
Sam Clegg | 4df5d76 | 2017-06-27 20:40:53 +0000 | [diff] [blame] | 47 | #include "llvm/Object/Wasm.h" |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Casting.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CommandLine.h" |
| 50 | #include "llvm/Support/Debug.h" |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Errc.h" |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 52 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 53 | #include "llvm/Support/Format.h" |
Benjamin Kramer | bf11531 | 2011-07-25 23:04:36 +0000 | [diff] [blame] | 54 | #include "llvm/Support/GraphWriter.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Host.h" |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 56 | #include "llvm/Support/InitLLVM.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 57 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 58 | #include "llvm/Support/SourceMgr.h" |
Joel Galenson | 134cf47 | 2018-08-24 15:21:57 +0000 | [diff] [blame] | 59 | #include "llvm/Support/StringSaver.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 60 | #include "llvm/Support/TargetRegistry.h" |
| 61 | #include "llvm/Support/TargetSelect.h" |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 62 | #include "llvm/Support/WithColor.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 63 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 64 | #include <algorithm> |
Benjamin Kramer | a5177e6 | 2012-03-23 11:49:32 +0000 | [diff] [blame] | 65 | #include <cctype> |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 66 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 67 | #include <system_error> |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 68 | #include <unordered_map> |
Rafael Espindola | c398e67 | 2017-07-19 22:27:28 +0000 | [diff] [blame] | 69 | #include <utility> |
Ahmed Bougacha | 1792647 | 2013-08-21 07:29:02 +0000 | [diff] [blame] | 70 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 71 | using namespace llvm; |
| 72 | using namespace object; |
| 73 | |
Fangrui Song | 8513cd4 | 2018-06-27 20:45:11 +0000 | [diff] [blame] | 74 | cl::opt<bool> |
| 75 | llvm::AllHeaders("all-headers", |
| 76 | cl::desc("Display all available header information")); |
| 77 | static cl::alias AllHeadersShort("x", cl::desc("Alias for --all-headers"), |
| 78 | cl::aliasopt(AllHeaders)); |
| 79 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 80 | static cl::list<std::string> |
| 81 | InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 82 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 83 | cl::opt<bool> |
| 84 | llvm::Disassemble("disassemble", |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 85 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 86 | static cl::alias |
| 87 | Disassembled("d", cl::desc("Alias for --disassemble"), |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 88 | cl::aliasopt(Disassemble)); |
| 89 | |
| 90 | cl::opt<bool> |
| 91 | llvm::DisassembleAll("disassemble-all", |
| 92 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 93 | static cl::alias |
| 94 | DisassembleAlld("D", cl::desc("Alias for --disassemble-all"), |
Colin LeMahieu | f34933e | 2015-07-23 20:58:49 +0000 | [diff] [blame] | 95 | cl::aliasopt(DisassembleAll)); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 96 | |
Zachary Turner | 030ad37 | 2018-08-20 22:18:21 +0000 | [diff] [blame] | 97 | cl::opt<bool> llvm::Demangle("demangle", cl::desc("Demangle symbols names"), |
| 98 | cl::init(false)); |
Paul Semel | 007dedb | 2018-07-18 16:39:21 +0000 | [diff] [blame] | 99 | |
| 100 | static cl::alias DemangleShort("C", cl::desc("Alias for --demangle"), |
Zachary Turner | 030ad37 | 2018-08-20 22:18:21 +0000 | [diff] [blame] | 101 | cl::aliasopt(llvm::Demangle)); |
Paul Semel | 007dedb | 2018-07-18 16:39:21 +0000 | [diff] [blame] | 102 | |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 103 | static cl::list<std::string> |
| 104 | DisassembleFunctions("df", |
| 105 | cl::CommaSeparated, |
| 106 | cl::desc("List of functions to disassemble")); |
| 107 | static StringSet<> DisasmFuncsSet; |
| 108 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 109 | cl::opt<bool> |
Kristina Brooks | 31579e9 | 2018-10-31 09:34:08 +0000 | [diff] [blame] | 110 | llvm::Relocations("reloc", |
| 111 | cl::desc("Display the relocation entries in the file")); |
| 112 | static cl::alias RelocationsShort("r", cl::desc("Alias for --reloc"), |
| 113 | cl::NotHidden, |
| 114 | cl::aliasopt(llvm::Relocations)); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 115 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 116 | cl::opt<bool> |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 117 | llvm::DynamicRelocations("dynamic-reloc", |
| 118 | cl::desc("Display the dynamic relocation entries in the file")); |
| 119 | static cl::alias |
| 120 | DynamicRelocationsd("R", cl::desc("Alias for --dynamic-reloc"), |
| 121 | cl::aliasopt(DynamicRelocations)); |
| 122 | |
| 123 | cl::opt<bool> |
James Henderson | b55b658 | 2018-10-29 10:05:39 +0000 | [diff] [blame] | 124 | llvm::SectionContents("full-contents", |
| 125 | cl::desc("Display the content of each section")); |
| 126 | static cl::alias SectionContentsShort("s", |
| 127 | cl::desc("Alias for --full-contents"), |
| 128 | cl::aliasopt(SectionContents)); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 129 | |
Kristina Brooks | 3baa5f7 | 2018-10-31 05:45:01 +0000 | [diff] [blame] | 130 | cl::opt<bool> llvm::SymbolTable("syms", cl::desc("Display the symbol table")); |
| 131 | static cl::alias SymbolTableShort("t", cl::desc("Alias for --syms"), |
Kristina Brooks | 889356e | 2018-10-31 09:35:25 +0000 | [diff] [blame] | 132 | cl::NotHidden, |
Kristina Brooks | 3baa5f7 | 2018-10-31 05:45:01 +0000 | [diff] [blame] | 133 | cl::aliasopt(llvm::SymbolTable)); |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 134 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 135 | cl::opt<bool> |
| 136 | llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols")); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 137 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 138 | cl::opt<bool> |
| 139 | llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info")); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 140 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 141 | cl::opt<bool> |
| 142 | llvm::Bind("bind", cl::desc("Display mach-o binding info")); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 143 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 144 | cl::opt<bool> |
| 145 | llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info")); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 146 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 147 | cl::opt<bool> |
| 148 | llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info")); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 149 | |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 150 | cl::opt<bool> |
| 151 | llvm::RawClangAST("raw-clang-ast", |
| 152 | cl::desc("Dump the raw binary contents of the clang AST section")); |
| 153 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 154 | static cl::opt<bool> |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 155 | MachOOpt("macho", cl::desc("Use MachO specific object file parser")); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 156 | static cl::alias |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 157 | MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt)); |
Benjamin Kramer | 87ee76c | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 158 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 159 | cl::opt<std::string> |
| 160 | llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " |
| 161 | "see -version for available targets")); |
| 162 | |
| 163 | cl::opt<std::string> |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 164 | llvm::MCPU("mcpu", |
| 165 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 166 | cl::value_desc("cpu-name"), |
| 167 | cl::init("")); |
| 168 | |
| 169 | cl::opt<std::string> |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 170 | llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, " |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 171 | "see -version for available targets")); |
| 172 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 173 | cl::opt<bool> |
| 174 | llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the " |
| 175 | "headers for each section.")); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 176 | static cl::alias |
| 177 | SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), |
| 178 | cl::aliasopt(SectionHeaders)); |
| 179 | static cl::alias |
| 180 | SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), |
| 181 | cl::aliasopt(SectionHeaders)); |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 182 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 183 | cl::list<std::string> |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 184 | llvm::FilterSections("section", cl::desc("Operate on the specified sections only. " |
| 185 | "With -macho dump segment,section")); |
| 186 | cl::alias |
| 187 | static FilterSectionsj("j", cl::desc("Alias for --section"), |
| 188 | cl::aliasopt(llvm::FilterSections)); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 189 | |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 190 | cl::list<std::string> |
| 191 | llvm::MAttrs("mattr", |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 192 | cl::CommaSeparated, |
| 193 | cl::desc("Target specific attributes"), |
| 194 | cl::value_desc("a1,+a2,-a3,...")); |
| 195 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 196 | cl::opt<bool> |
| 197 | llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling " |
| 198 | "instructions, do not print " |
| 199 | "the instruction bytes.")); |
Saleem Abdulrasool | dea14b2 | 2017-02-08 18:11:31 +0000 | [diff] [blame] | 200 | cl::opt<bool> |
| 201 | llvm::NoLeadingAddr("no-leading-addr", cl::desc("Print no leading address")); |
Eli Bendersky | 3a6808c | 2012-11-20 22:57:02 +0000 | [diff] [blame] | 202 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 203 | cl::opt<bool> |
| 204 | llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information")); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 205 | |
| 206 | static cl::alias |
| 207 | UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), |
| 208 | cl::aliasopt(UnwindInfo)); |
| 209 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 210 | cl::opt<bool> |
| 211 | llvm::PrivateHeaders("private-headers", |
| 212 | cl::desc("Display format specific file headers")); |
Michael J. Spencer | 209565db | 2013-01-06 03:56:49 +0000 | [diff] [blame] | 213 | |
Kevin Enderby | 0ae163f | 2016-01-13 00:25:36 +0000 | [diff] [blame] | 214 | cl::opt<bool> |
| 215 | llvm::FirstPrivateHeader("private-header", |
| 216 | cl::desc("Display only the first format specific file " |
| 217 | "header")); |
| 218 | |
Michael J. Spencer | 209565db | 2013-01-06 03:56:49 +0000 | [diff] [blame] | 219 | static cl::alias |
| 220 | PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), |
| 221 | cl::aliasopt(PrivateHeaders)); |
| 222 | |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 223 | cl::opt<bool> llvm::FileHeaders( |
| 224 | "file-headers", |
| 225 | cl::desc("Display the contents of the overall file header")); |
| 226 | |
| 227 | static cl::alias FileHeadersShort("f", cl::desc("Alias for --file-headers"), |
| 228 | cl::aliasopt(FileHeaders)); |
| 229 | |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 230 | cl::opt<bool> |
Paul Semel | 0dc92f6 | 2018-07-05 14:43:29 +0000 | [diff] [blame] | 231 | llvm::ArchiveHeaders("archive-headers", |
| 232 | cl::desc("Display archive header information")); |
| 233 | |
| 234 | cl::alias |
| 235 | ArchiveHeadersShort("a", cl::desc("Alias for --archive-headers"), |
| 236 | cl::aliasopt(ArchiveHeaders)); |
| 237 | |
| 238 | cl::opt<bool> |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 239 | llvm::PrintImmHex("print-imm-hex", |
Colin LeMahieu | efe3732 | 2016-04-08 18:15:37 +0000 | [diff] [blame] | 240 | cl::desc("Use hex format for immediate values")); |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 241 | |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 242 | cl::opt<bool> PrintFaultMaps("fault-map-section", |
| 243 | cl::desc("Display contents of faultmap section")); |
| 244 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 245 | cl::opt<DIDumpType> llvm::DwarfDumpType( |
| 246 | "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"), |
Jonas Devlieghere | c0a758d | 2017-09-18 14:15:57 +0000 | [diff] [blame] | 247 | cl::values(clEnumValN(DIDT_DebugFrame, "frames", ".debug_frame"))); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 248 | |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 249 | cl::opt<bool> PrintSource( |
| 250 | "source", |
| 251 | cl::desc( |
Alex Denisov | a07169e | 2018-02-02 19:20:37 +0000 | [diff] [blame] | 252 | "Display source inlined with disassembly. Implies disassemble object")); |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 253 | |
| 254 | cl::alias PrintSourceShort("S", cl::desc("Alias for -source"), |
| 255 | cl::aliasopt(PrintSource)); |
| 256 | |
| 257 | cl::opt<bool> PrintLines("line-numbers", |
| 258 | cl::desc("Display source line numbers with " |
| 259 | "disassembly. Implies disassemble object")); |
| 260 | |
| 261 | cl::alias PrintLinesShort("l", cl::desc("Alias for -line-numbers"), |
| 262 | cl::aliasopt(PrintLines)); |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 263 | |
| 264 | cl::opt<unsigned long long> |
| 265 | StartAddress("start-address", cl::desc("Disassemble beginning at address"), |
| 266 | cl::value_desc("address"), cl::init(0)); |
| 267 | cl::opt<unsigned long long> |
George Rimar | 70d197d | 2019-01-10 14:55:26 +0000 | [diff] [blame] | 268 | StopAddress("stop-address", |
George Rimar | 3687c3e9 | 2019-01-15 14:03:50 +0000 | [diff] [blame] | 269 | cl::desc("Stop disassembly at address"), |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 270 | cl::value_desc("address"), cl::init(UINT64_MAX)); |
George Rimar | 70d197d | 2019-01-10 14:55:26 +0000 | [diff] [blame] | 271 | |
George Rimar | 3687c3e9 | 2019-01-15 14:03:50 +0000 | [diff] [blame] | 272 | cl::opt<bool> DisassembleZeroes( |
| 273 | "disassemble-zeroes", |
| 274 | cl::desc("Do not skip blocks of zeroes when disassembling")); |
George Rimar | 70d197d | 2019-01-10 14:55:26 +0000 | [diff] [blame] | 275 | cl::alias DisassembleZeroesShort("z", |
| 276 | cl::desc("Alias for --disassemble-zeroes"), |
| 277 | cl::aliasopt(DisassembleZeroes)); |
| 278 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 279 | static StringRef ToolName; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 280 | |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 281 | typedef std::vector<std::tuple<uint64_t, StringRef, uint8_t>> SectionSymbolsTy; |
| 282 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 283 | namespace { |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 284 | typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 285 | |
| 286 | class SectionFilterIterator { |
| 287 | public: |
| 288 | SectionFilterIterator(FilterPredicate P, |
| 289 | llvm::object::section_iterator const &I, |
| 290 | llvm::object::section_iterator const &E) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 291 | : Predicate(std::move(P)), Iterator(I), End(E) { |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 292 | ScanPredicate(); |
| 293 | } |
Benjamin Kramer | ac9257b | 2015-09-24 14:52:52 +0000 | [diff] [blame] | 294 | const llvm::object::SectionRef &operator*() const { return *Iterator; } |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 295 | SectionFilterIterator &operator++() { |
| 296 | ++Iterator; |
| 297 | ScanPredicate(); |
| 298 | return *this; |
| 299 | } |
| 300 | bool operator!=(SectionFilterIterator const &Other) const { |
| 301 | return Iterator != Other.Iterator; |
| 302 | } |
| 303 | |
| 304 | private: |
| 305 | void ScanPredicate() { |
Colin LeMahieu | da1723f | 2015-07-29 19:21:13 +0000 | [diff] [blame] | 306 | while (Iterator != End && !Predicate(*Iterator)) { |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 307 | ++Iterator; |
| 308 | } |
| 309 | } |
| 310 | FilterPredicate Predicate; |
| 311 | llvm::object::section_iterator Iterator; |
| 312 | llvm::object::section_iterator End; |
| 313 | }; |
| 314 | |
| 315 | class SectionFilter { |
| 316 | public: |
| 317 | SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 318 | : Predicate(std::move(P)), Object(O) {} |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 319 | SectionFilterIterator begin() { |
| 320 | return SectionFilterIterator(Predicate, Object.section_begin(), |
| 321 | Object.section_end()); |
| 322 | } |
| 323 | SectionFilterIterator end() { |
| 324 | return SectionFilterIterator(Predicate, Object.section_end(), |
| 325 | Object.section_end()); |
| 326 | } |
| 327 | |
| 328 | private: |
| 329 | FilterPredicate Predicate; |
| 330 | llvm::object::ObjectFile const &Object; |
| 331 | }; |
| 332 | SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) { |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 333 | return SectionFilter( |
| 334 | [](llvm::object::SectionRef const &S) { |
| 335 | if (FilterSections.empty()) |
| 336 | return true; |
| 337 | llvm::StringRef String; |
| 338 | std::error_code error = S.getName(String); |
| 339 | if (error) |
| 340 | return false; |
| 341 | return is_contained(FilterSections, String); |
| 342 | }, |
| 343 | O); |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 347 | void llvm::error(std::error_code EC) { |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 348 | if (!EC) |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 349 | return; |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 350 | WithColor::error(errs(), ToolName) |
| 351 | << "reading file: " << EC.message() << ".\n"; |
Davide Italiano | 140af64 | 2015-12-25 18:16:45 +0000 | [diff] [blame] | 352 | errs().flush(); |
Davide Italiano | 7f6c301 | 2015-08-06 00:18:52 +0000 | [diff] [blame] | 353 | exit(1); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Kevin Enderby | 4239805 | 2016-06-28 23:16:13 +0000 | [diff] [blame] | 356 | LLVM_ATTRIBUTE_NORETURN void llvm::error(Twine Message) { |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 357 | WithColor::error(errs(), ToolName) << Message << ".\n"; |
Kevin Enderby | 4239805 | 2016-06-28 23:16:13 +0000 | [diff] [blame] | 358 | errs().flush(); |
| 359 | exit(1); |
| 360 | } |
| 361 | |
Paul Semel | 007dedb | 2018-07-18 16:39:21 +0000 | [diff] [blame] | 362 | void llvm::warn(StringRef Message) { |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 363 | WithColor::warning(errs(), ToolName) << Message << ".\n"; |
Paul Semel | 007dedb | 2018-07-18 16:39:21 +0000 | [diff] [blame] | 364 | errs().flush(); |
| 365 | } |
| 366 | |
Davide Italiano | ed9d95b | 2015-12-29 13:41:02 +0000 | [diff] [blame] | 367 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 368 | Twine Message) { |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 369 | WithColor::error(errs(), ToolName) |
| 370 | << "'" << File << "': " << Message << ".\n"; |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 371 | exit(1); |
| 372 | } |
| 373 | |
| 374 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, |
Davide Italiano | ed9d95b | 2015-12-29 13:41:02 +0000 | [diff] [blame] | 375 | std::error_code EC) { |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 376 | assert(EC); |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 377 | WithColor::error(errs(), ToolName) |
| 378 | << "'" << File << "': " << EC.message() << ".\n"; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 379 | exit(1); |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 382 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, |
| 383 | llvm::Error E) { |
| 384 | assert(E); |
| 385 | std::string Buf; |
| 386 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 387 | logAllUnhandledErrors(std::move(E), OS); |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 388 | OS.flush(); |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 389 | WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf; |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 390 | exit(1); |
| 391 | } |
| 392 | |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 393 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, |
| 394 | StringRef FileName, |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 395 | llvm::Error E, |
| 396 | StringRef ArchitectureName) { |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 397 | assert(E); |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 398 | WithColor::error(errs(), ToolName); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 399 | if (ArchiveName != "") |
| 400 | errs() << ArchiveName << "(" << FileName << ")"; |
| 401 | else |
Justin Bogner | 31d8b7d | 2016-10-26 22:37:52 +0000 | [diff] [blame] | 402 | errs() << "'" << FileName << "'"; |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 403 | if (!ArchitectureName.empty()) |
| 404 | errs() << " (for architecture " << ArchitectureName << ")"; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 405 | std::string Buf; |
| 406 | raw_string_ostream OS(Buf); |
Jonas Devlieghere | 45eb84f | 2018-11-11 01:46:03 +0000 | [diff] [blame] | 407 | logAllUnhandledErrors(std::move(E), OS); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 408 | OS.flush(); |
Justin Bogner | 31d8b7d | 2016-10-26 22:37:52 +0000 | [diff] [blame] | 409 | errs() << ": " << Buf; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 410 | exit(1); |
| 411 | } |
| 412 | |
| 413 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, |
| 414 | const object::Archive::Child &C, |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 415 | llvm::Error E, |
| 416 | StringRef ArchitectureName) { |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 417 | Expected<StringRef> NameOrErr = C.getName(); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 418 | // TODO: if we have a error getting the name then it would be nice to print |
| 419 | // the index of which archive member this is and or its offset in the |
| 420 | // archive instead of "???" as the name. |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 421 | if (!NameOrErr) { |
| 422 | consumeError(NameOrErr.takeError()); |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 423 | llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName); |
Kevin Enderby | f458603 | 2016-07-29 17:44:13 +0000 | [diff] [blame] | 424 | } else |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 425 | llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E), |
| 426 | ArchitectureName); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 429 | static const Target *getTarget(const ObjectFile *Obj = nullptr) { |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 430 | // Figure out the target triple. |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 431 | llvm::Triple TheTriple("unknown-unknown-unknown"); |
Michael J. Spencer | 05350e6d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 432 | if (TripleName.empty()) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 433 | if (Obj) |
Vlad Tsyrklevich | de62046 | 2017-09-19 02:22:48 +0000 | [diff] [blame] | 434 | TheTriple = Obj->makeTriple(); |
Sam Parker | df7c6ef | 2017-01-18 13:52:12 +0000 | [diff] [blame] | 435 | } else { |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 436 | TheTriple.setTriple(Triple::normalize(TripleName)); |
Vlad Tsyrklevich | de62046 | 2017-09-19 02:22:48 +0000 | [diff] [blame] | 437 | |
Sam Parker | df7c6ef | 2017-01-18 13:52:12 +0000 | [diff] [blame] | 438 | // Use the triple, but also try to combine with ARM build attributes. |
| 439 | if (Obj) { |
| 440 | auto Arch = Obj->getArch(); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 441 | if (Arch == Triple::arm || Arch == Triple::armeb) |
Sam Parker | df7c6ef | 2017-01-18 13:52:12 +0000 | [diff] [blame] | 442 | Obj->setARMSubArch(TheTriple); |
Sam Parker | df7c6ef | 2017-01-18 13:52:12 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 445 | |
| 446 | // Get the target specific parser. |
| 447 | std::string Error; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 448 | const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, |
| 449 | Error); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 450 | if (!TheTarget) { |
| 451 | if (Obj) |
| 452 | report_error(Obj->getFileName(), "can't find target: " + Error); |
| 453 | else |
| 454 | error("can't find target: " + Error); |
| 455 | } |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 456 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 457 | // Update the triple name and return the found target. |
| 458 | TripleName = TheTriple.getTriple(); |
| 459 | return TheTarget; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 460 | } |
| 461 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 462 | bool llvm::isRelocAddressLess(RelocationRef A, RelocationRef B) { |
| 463 | return A.getOffset() < B.getOffset(); |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 466 | template <class ELFT> |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 467 | static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj, |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 468 | const RelocationRef &RelRef, |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 469 | SmallVectorImpl<char> &Result) { |
| 470 | typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym; |
| 471 | typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr; |
Rafael Espindola | 7f162ec | 2015-07-02 14:21:38 +0000 | [diff] [blame] | 472 | typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela; |
| 473 | |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 474 | const ELFFile<ELFT> &EF = *Obj->getELFFile(); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 475 | DataRefImpl Rel = RelRef.getRawDataRefImpl(); |
Davide Italiano | 6cf0926 | 2016-11-16 05:10:28 +0000 | [diff] [blame] | 476 | auto SecOrErr = EF.getSection(Rel.d.a); |
| 477 | if (!SecOrErr) |
| 478 | return errorToErrorCode(SecOrErr.takeError()); |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 479 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 480 | int64_t Addend = 0; |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 481 | // If there is no Symbol associated with the relocation, we set the undef |
| 482 | // boolean value to 'true'. This will prevent us from calling functions that |
| 483 | // requires the relocation to be associated with a symbol. |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 484 | // |
| 485 | // In SHT_REL case we would need to read the addend from section data. |
George Rimar | ce5cafe | 2019-01-17 09:14:33 +0000 | [diff] [blame] | 486 | // GNU objdump does not do that and we just follow for simplicity. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 487 | bool Undef = false; |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 488 | if ((*SecOrErr)->sh_type == ELF::SHT_RELA) { |
Rafael Espindola | 7f162ec | 2015-07-02 14:21:38 +0000 | [diff] [blame] | 489 | const Elf_Rela *ERela = Obj->getRela(Rel); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 490 | Addend = ERela->r_addend; |
| 491 | Undef = ERela->getSymbol(false) == 0; |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 492 | } else if ((*SecOrErr)->sh_type != ELF::SHT_REL) { |
| 493 | return object_error::parse_failed; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 494 | } |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 495 | |
| 496 | // Default scheme is to print Target, as well as "+ <addend>" for nonzero |
| 497 | // addend. Should be acceptable for all normal purposes. |
| 498 | std::string FmtBuf; |
| 499 | raw_string_ostream Fmt(FmtBuf); |
| 500 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 501 | if (!Undef) { |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 502 | symbol_iterator SI = RelRef.getSymbol(); |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 503 | const Elf_Sym *Sym = Obj->getSymbol(SI->getRawDataRefImpl()); |
| 504 | if (Sym->getType() == ELF::STT_SECTION) { |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 505 | Expected<section_iterator> SymSI = SI->getSection(); |
| 506 | if (!SymSI) |
| 507 | return errorToErrorCode(SymSI.takeError()); |
| 508 | const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl()); |
| 509 | auto SecName = EF.getSectionName(SymSec); |
| 510 | if (!SecName) |
| 511 | return errorToErrorCode(SecName.takeError()); |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 512 | Fmt << *SecName; |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 513 | } else { |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 514 | Expected<StringRef> SymName = SI->getName(); |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 515 | if (!SymName) |
| 516 | return errorToErrorCode(SymName.takeError()); |
George Rimar | 7120fb9 | 2018-12-19 10:44:49 +0000 | [diff] [blame] | 517 | if (Demangle) |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 518 | Fmt << demangle(*SymName); |
George Rimar | 7120fb9 | 2018-12-19 10:44:49 +0000 | [diff] [blame] | 519 | else |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 520 | Fmt << *SymName; |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 521 | } |
George Rimar | 6ce6bfa | 2019-01-17 09:13:17 +0000 | [diff] [blame] | 522 | } else { |
| 523 | Fmt << "*ABS*"; |
| 524 | } |
Daniel Cederman | d72b9fd | 2018-06-01 05:31:58 +0000 | [diff] [blame] | 525 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 526 | if (Addend != 0) |
| 527 | Fmt << (Addend < 0 ? "" : "+") << Addend; |
| 528 | Fmt.flush(); |
| 529 | Result.append(FmtBuf.begin(), FmtBuf.end()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 530 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj, |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 534 | const RelocationRef &Rel, |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 535 | SmallVectorImpl<char> &Result) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 536 | if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj)) |
| 537 | return getRelocationValueString(ELF32LE, Rel, Result); |
| 538 | if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj)) |
| 539 | return getRelocationValueString(ELF64LE, Rel, Result); |
| 540 | if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj)) |
| 541 | return getRelocationValueString(ELF32BE, Rel, Result); |
| 542 | auto *ELF64BE = cast<ELF64BEObjectFile>(Obj); |
| 543 | return getRelocationValueString(ELF64BE, Rel, Result); |
| 544 | } |
| 545 | |
| 546 | static std::error_code getRelocationValueString(const COFFObjectFile *Obj, |
| 547 | const RelocationRef &Rel, |
| 548 | SmallVectorImpl<char> &Result) { |
| 549 | symbol_iterator SymI = Rel.getSymbol(); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 550 | Expected<StringRef> SymNameOrErr = SymI->getName(); |
| 551 | if (!SymNameOrErr) |
| 552 | return errorToErrorCode(SymNameOrErr.takeError()); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 553 | StringRef SymName = *SymNameOrErr; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 554 | Result.append(SymName.begin(), SymName.end()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 555 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static void printRelocationTargetName(const MachOObjectFile *O, |
| 559 | const MachO::any_relocation_info &RE, |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 560 | raw_string_ostream &Fmt) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 561 | // Target of a scattered relocation is an address. In the interest of |
| 562 | // generating pretty output, scan through the symbol table looking for a |
| 563 | // symbol that aligns with that address. If we find one, print it. |
| 564 | // Otherwise, we just print the hex address of the target. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 565 | if (O->isRelocationScattered(RE)) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 566 | uint32_t Val = O->getPlainRelocationSymbolNum(RE); |
| 567 | |
| 568 | for (const SymbolRef &Symbol : O->symbols()) { |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 569 | Expected<uint64_t> Addr = Symbol.getAddress(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 570 | if (!Addr) |
| 571 | report_error(O->getFileName(), Addr.takeError()); |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 572 | if (*Addr != Val) |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 573 | continue; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 574 | Expected<StringRef> Name = Symbol.getName(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 575 | if (!Name) |
| 576 | report_error(O->getFileName(), Name.takeError()); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 577 | Fmt << *Name; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | |
| 581 | // If we couldn't find a symbol that this relocation refers to, try |
| 582 | // to find a section beginning instead. |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 583 | for (const SectionRef &Section : ToolSectionFilter(*O)) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 584 | std::error_code ec; |
| 585 | |
| 586 | StringRef Name; |
| 587 | uint64_t Addr = Section.getAddress(); |
| 588 | if (Addr != Val) |
| 589 | continue; |
| 590 | if ((ec = Section.getName(Name))) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 591 | report_error(O->getFileName(), ec); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 592 | Fmt << Name; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 593 | return; |
| 594 | } |
| 595 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 596 | Fmt << format("0x%x", Val); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 597 | return; |
| 598 | } |
| 599 | |
| 600 | StringRef S; |
| 601 | bool isExtern = O->getPlainRelocationExternal(RE); |
| 602 | uint64_t Val = O->getPlainRelocationSymbolNum(RE); |
| 603 | |
Martin Storsjo | 8c0317d | 2017-07-13 17:03:02 +0000 | [diff] [blame] | 604 | if (O->getAnyRelocationType(RE) == MachO::ARM64_RELOC_ADDEND) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 605 | Fmt << format("0x%0" PRIx64, Val); |
Martin Storsjo | 8c0317d | 2017-07-13 17:03:02 +0000 | [diff] [blame] | 606 | return; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | if (isExtern) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 610 | symbol_iterator SI = O->symbol_begin(); |
| 611 | advance(SI, Val); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 612 | Expected<StringRef> SOrErr = SI->getName(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 613 | if (!SOrErr) |
| 614 | report_error(O->getFileName(), SOrErr.takeError()); |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 615 | S = *SOrErr; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 616 | } else { |
| 617 | section_iterator SI = O->section_begin(); |
| 618 | // Adjust for the fact that sections are 1-indexed. |
Kevin Enderby | 3fc9188 | 2017-11-03 21:32:44 +0000 | [diff] [blame] | 619 | if (Val == 0) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 620 | Fmt << "0 (?,?)"; |
Kevin Enderby | 3fc9188 | 2017-11-03 21:32:44 +0000 | [diff] [blame] | 621 | return; |
| 622 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 623 | uint32_t I = Val - 1; |
| 624 | while (I != 0 && SI != O->section_end()) { |
| 625 | --I; |
Kevin Enderby | 3fc9188 | 2017-11-03 21:32:44 +0000 | [diff] [blame] | 626 | advance(SI, 1); |
| 627 | } |
| 628 | if (SI == O->section_end()) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 629 | Fmt << Val << " (?,?)"; |
Kevin Enderby | 3fc9188 | 2017-11-03 21:32:44 +0000 | [diff] [blame] | 630 | else |
| 631 | SI->getName(S); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 632 | } |
| 633 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 634 | Fmt << S; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Sam Clegg | 4df5d76 | 2017-06-27 20:40:53 +0000 | [diff] [blame] | 637 | static std::error_code getRelocationValueString(const WasmObjectFile *Obj, |
| 638 | const RelocationRef &RelRef, |
| 639 | SmallVectorImpl<char> &Result) { |
| 640 | const wasm::WasmRelocation& Rel = Obj->getWasmRelocation(RelRef); |
Sam Clegg | f676cdd | 2018-04-26 16:41:51 +0000 | [diff] [blame] | 641 | symbol_iterator SI = RelRef.getSymbol(); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 642 | std::string FmtBuf; |
| 643 | raw_string_ostream Fmt(FmtBuf); |
Sam Clegg | 8c4b0ce | 2018-04-26 17:05:04 +0000 | [diff] [blame] | 644 | if (SI == Obj->symbol_end()) { |
| 645 | // Not all wasm relocations have symbols associated with them. |
| 646 | // In particular R_WEBASSEMBLY_TYPE_INDEX_LEB. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 647 | Fmt << Rel.Index; |
Sam Clegg | f676cdd | 2018-04-26 16:41:51 +0000 | [diff] [blame] | 648 | } else { |
Sam Clegg | 8c4b0ce | 2018-04-26 17:05:04 +0000 | [diff] [blame] | 649 | Expected<StringRef> SymNameOrErr = SI->getName(); |
| 650 | if (!SymNameOrErr) |
| 651 | return errorToErrorCode(SymNameOrErr.takeError()); |
Sam Clegg | f676cdd | 2018-04-26 16:41:51 +0000 | [diff] [blame] | 652 | StringRef SymName = *SymNameOrErr; |
| 653 | Result.append(SymName.begin(), SymName.end()); |
| 654 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 655 | Fmt << (Rel.Addend < 0 ? "" : "+") << Rel.Addend; |
| 656 | Fmt.flush(); |
| 657 | Result.append(FmtBuf.begin(), FmtBuf.end()); |
Sam Clegg | 4df5d76 | 2017-06-27 20:40:53 +0000 | [diff] [blame] | 658 | return std::error_code(); |
| 659 | } |
| 660 | |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 661 | static std::error_code getRelocationValueString(const MachOObjectFile *Obj, |
| 662 | const RelocationRef &RelRef, |
| 663 | SmallVectorImpl<char> &Result) { |
| 664 | DataRefImpl Rel = RelRef.getRawDataRefImpl(); |
| 665 | MachO::any_relocation_info RE = Obj->getRelocation(Rel); |
| 666 | |
| 667 | unsigned Arch = Obj->getArch(); |
| 668 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 669 | std::string FmtBuf; |
| 670 | raw_string_ostream Fmt(FmtBuf); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 671 | unsigned Type = Obj->getAnyRelocationType(RE); |
| 672 | bool IsPCRel = Obj->getAnyRelocationPCRel(RE); |
| 673 | |
| 674 | // Determine any addends that should be displayed with the relocation. |
| 675 | // These require decoding the relocation type, which is triple-specific. |
| 676 | |
| 677 | // X86_64 has entirely custom relocation types. |
| 678 | if (Arch == Triple::x86_64) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 679 | switch (Type) { |
| 680 | case MachO::X86_64_RELOC_GOT_LOAD: |
| 681 | case MachO::X86_64_RELOC_GOT: { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 682 | printRelocationTargetName(Obj, RE, Fmt); |
| 683 | Fmt << "@GOT"; |
| 684 | if (IsPCRel) |
| 685 | Fmt << "PCREL"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 686 | break; |
| 687 | } |
| 688 | case MachO::X86_64_RELOC_SUBTRACTOR: { |
| 689 | DataRefImpl RelNext = Rel; |
| 690 | Obj->moveRelocationNext(RelNext); |
| 691 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 692 | |
| 693 | // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type |
| 694 | // X86_64_RELOC_UNSIGNED. |
| 695 | // NOTE: Scattered relocations don't exist on x86_64. |
| 696 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 697 | if (RType != MachO::X86_64_RELOC_UNSIGNED) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 698 | report_error(Obj->getFileName(), "Expected X86_64_RELOC_UNSIGNED after " |
| 699 | "X86_64_RELOC_SUBTRACTOR."); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 700 | |
| 701 | // The X86_64_RELOC_UNSIGNED contains the minuend symbol; |
| 702 | // X86_64_RELOC_SUBTRACTOR contains the subtrahend. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 703 | printRelocationTargetName(Obj, RENext, Fmt); |
| 704 | Fmt << "-"; |
| 705 | printRelocationTargetName(Obj, RE, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 706 | break; |
| 707 | } |
| 708 | case MachO::X86_64_RELOC_TLV: |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 709 | printRelocationTargetName(Obj, RE, Fmt); |
| 710 | Fmt << "@TLV"; |
| 711 | if (IsPCRel) |
| 712 | Fmt << "P"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 713 | break; |
| 714 | case MachO::X86_64_RELOC_SIGNED_1: |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 715 | printRelocationTargetName(Obj, RE, Fmt); |
| 716 | Fmt << "-1"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 717 | break; |
| 718 | case MachO::X86_64_RELOC_SIGNED_2: |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 719 | printRelocationTargetName(Obj, RE, Fmt); |
| 720 | Fmt << "-2"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 721 | break; |
| 722 | case MachO::X86_64_RELOC_SIGNED_4: |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 723 | printRelocationTargetName(Obj, RE, Fmt); |
| 724 | Fmt << "-4"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 725 | break; |
| 726 | default: |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 727 | printRelocationTargetName(Obj, RE, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 728 | break; |
| 729 | } |
| 730 | // X86 and ARM share some relocation types in common. |
| 731 | } else if (Arch == Triple::x86 || Arch == Triple::arm || |
| 732 | Arch == Triple::ppc) { |
| 733 | // Generic relocation types... |
| 734 | switch (Type) { |
| 735 | case MachO::GENERIC_RELOC_PAIR: // prints no info |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 736 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 737 | case MachO::GENERIC_RELOC_SECTDIFF: { |
| 738 | DataRefImpl RelNext = Rel; |
| 739 | Obj->moveRelocationNext(RelNext); |
| 740 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 741 | |
| 742 | // X86 sect diff's must be followed by a relocation of type |
| 743 | // GENERIC_RELOC_PAIR. |
| 744 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 745 | |
| 746 | if (RType != MachO::GENERIC_RELOC_PAIR) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 747 | report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " |
| 748 | "GENERIC_RELOC_SECTDIFF."); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 749 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 750 | printRelocationTargetName(Obj, RE, Fmt); |
| 751 | Fmt << "-"; |
| 752 | printRelocationTargetName(Obj, RENext, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | if (Arch == Triple::x86 || Arch == Triple::ppc) { |
| 758 | switch (Type) { |
| 759 | case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { |
| 760 | DataRefImpl RelNext = Rel; |
| 761 | Obj->moveRelocationNext(RelNext); |
| 762 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 763 | |
| 764 | // X86 sect diff's must be followed by a relocation of type |
| 765 | // GENERIC_RELOC_PAIR. |
| 766 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 767 | if (RType != MachO::GENERIC_RELOC_PAIR) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 768 | report_error(Obj->getFileName(), "Expected GENERIC_RELOC_PAIR after " |
| 769 | "GENERIC_RELOC_LOCAL_SECTDIFF."); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 770 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 771 | printRelocationTargetName(Obj, RE, Fmt); |
| 772 | Fmt << "-"; |
| 773 | printRelocationTargetName(Obj, RENext, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 774 | break; |
| 775 | } |
| 776 | case MachO::GENERIC_RELOC_TLV: { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 777 | printRelocationTargetName(Obj, RE, Fmt); |
| 778 | Fmt << "@TLV"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 779 | if (IsPCRel) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 780 | Fmt << "P"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 781 | break; |
| 782 | } |
| 783 | default: |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 784 | printRelocationTargetName(Obj, RE, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 785 | } |
| 786 | } else { // ARM-specific relocations |
| 787 | switch (Type) { |
| 788 | case MachO::ARM_RELOC_HALF: |
| 789 | case MachO::ARM_RELOC_HALF_SECTDIFF: { |
| 790 | // Half relocations steal a bit from the length field to encode |
| 791 | // whether this is an upper16 or a lower16 relocation. |
Martin Storsjo | fa5183b | 2017-07-13 05:54:08 +0000 | [diff] [blame] | 792 | bool isUpper = (Obj->getAnyRelocationLength(RE) & 0x1) == 1; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 793 | |
| 794 | if (isUpper) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 795 | Fmt << ":upper16:("; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 796 | else |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 797 | Fmt << ":lower16:("; |
| 798 | printRelocationTargetName(Obj, RE, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 799 | |
| 800 | DataRefImpl RelNext = Rel; |
| 801 | Obj->moveRelocationNext(RelNext); |
| 802 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 803 | |
| 804 | // ARM half relocs must be followed by a relocation of type |
| 805 | // ARM_RELOC_PAIR. |
| 806 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 807 | if (RType != MachO::ARM_RELOC_PAIR) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 808 | report_error(Obj->getFileName(), "Expected ARM_RELOC_PAIR after " |
| 809 | "ARM_RELOC_HALF"); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 810 | |
| 811 | // NOTE: The half of the target virtual address is stashed in the |
| 812 | // address field of the secondary relocation, but we can't reverse |
| 813 | // engineer the constant offset from it without decoding the movw/movt |
| 814 | // instruction to find the other half in its immediate field. |
| 815 | |
| 816 | // ARM_RELOC_HALF_SECTDIFF encodes the second section in the |
| 817 | // symbol/section pointer of the follow-on relocation. |
| 818 | if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 819 | Fmt << "-"; |
| 820 | printRelocationTargetName(Obj, RENext, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 821 | } |
| 822 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 823 | Fmt << ")"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 824 | break; |
| 825 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 826 | default: { printRelocationTargetName(Obj, RE, Fmt); } |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | } else |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 830 | printRelocationTargetName(Obj, RE, Fmt); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 831 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 832 | Fmt.flush(); |
| 833 | Result.append(FmtBuf.begin(), FmtBuf.end()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 834 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | static std::error_code getRelocationValueString(const RelocationRef &Rel, |
| 838 | SmallVectorImpl<char> &Result) { |
Rafael Espindola | 854038e | 2015-06-26 14:51:16 +0000 | [diff] [blame] | 839 | const ObjectFile *Obj = Rel.getObject(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 840 | if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj)) |
| 841 | return getRelocationValueString(ELF, Rel, Result); |
| 842 | if (auto *COFF = dyn_cast<COFFObjectFile>(Obj)) |
| 843 | return getRelocationValueString(COFF, Rel, Result); |
Sam Clegg | 4df5d76 | 2017-06-27 20:40:53 +0000 | [diff] [blame] | 844 | if (auto *Wasm = dyn_cast<WasmObjectFile>(Obj)) |
| 845 | return getRelocationValueString(Wasm, Rel, Result); |
| 846 | if (auto *MachO = dyn_cast<MachOObjectFile>(Obj)) |
| 847 | return getRelocationValueString(MachO, Rel, Result); |
| 848 | llvm_unreachable("unknown object file format"); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 851 | /// Indicates whether this relocation should hidden when listing |
Rafael Espindola | 0ad71d9 | 2015-06-30 03:41:26 +0000 | [diff] [blame] | 852 | /// relocations, usually because it is the trailing part of a multipart |
| 853 | /// relocation that will be printed as part of the leading relocation. |
| 854 | static bool getHidden(RelocationRef RelRef) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 855 | auto *MachO = dyn_cast<MachOObjectFile>(RelRef.getObject()); |
Rafael Espindola | 0ad71d9 | 2015-06-30 03:41:26 +0000 | [diff] [blame] | 856 | if (!MachO) |
| 857 | return false; |
| 858 | |
| 859 | unsigned Arch = MachO->getArch(); |
| 860 | DataRefImpl Rel = RelRef.getRawDataRefImpl(); |
| 861 | uint64_t Type = MachO->getRelocationType(Rel); |
| 862 | |
| 863 | // On arches that use the generic relocations, GENERIC_RELOC_PAIR |
| 864 | // is always hidden. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 865 | if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) |
| 866 | return Type == MachO::GENERIC_RELOC_PAIR; |
| 867 | |
| 868 | if (Arch == Triple::x86_64) { |
Rafael Espindola | 0ad71d9 | 2015-06-30 03:41:26 +0000 | [diff] [blame] | 869 | // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows |
| 870 | // an X86_64_RELOC_SUBTRACTOR. |
| 871 | if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) { |
| 872 | DataRefImpl RelPrev = Rel; |
| 873 | RelPrev.d.a--; |
| 874 | uint64_t PrevType = MachO->getRelocationType(RelPrev); |
| 875 | if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR) |
| 876 | return true; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | return false; |
| 881 | } |
| 882 | |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 883 | namespace { |
| 884 | class SourcePrinter { |
| 885 | protected: |
| 886 | DILineInfo OldLineInfo; |
| 887 | const ObjectFile *Obj = nullptr; |
| 888 | std::unique_ptr<symbolize::LLVMSymbolizer> Symbolizer; |
| 889 | // File name to file contents of source |
| 890 | std::unordered_map<std::string, std::unique_ptr<MemoryBuffer>> SourceCache; |
| 891 | // Mark the line endings of the cached source |
| 892 | std::unordered_map<std::string, std::vector<StringRef>> LineCache; |
| 893 | |
| 894 | private: |
| 895 | bool cacheSource(const DILineInfo& LineInfoFile); |
| 896 | |
| 897 | public: |
| 898 | SourcePrinter() = default; |
| 899 | SourcePrinter(const ObjectFile *Obj, StringRef DefaultArch) : Obj(Obj) { |
| 900 | symbolize::LLVMSymbolizer::Options SymbolizerOpts( |
| 901 | DILineInfoSpecifier::FunctionNameKind::None, true, false, false, |
| 902 | DefaultArch); |
| 903 | Symbolizer.reset(new symbolize::LLVMSymbolizer(SymbolizerOpts)); |
| 904 | } |
| 905 | virtual ~SourcePrinter() = default; |
| 906 | virtual void printSourceLine(raw_ostream &OS, uint64_t Address, |
| 907 | StringRef Delimiter = "; "); |
| 908 | }; |
| 909 | |
| 910 | bool SourcePrinter::cacheSource(const DILineInfo &LineInfo) { |
| 911 | std::unique_ptr<MemoryBuffer> Buffer; |
| 912 | if (LineInfo.Source) { |
| 913 | Buffer = MemoryBuffer::getMemBuffer(*LineInfo.Source); |
| 914 | } else { |
| 915 | auto BufferOrError = MemoryBuffer::getFile(LineInfo.FileName); |
| 916 | if (!BufferOrError) |
| 917 | return false; |
| 918 | Buffer = std::move(*BufferOrError); |
| 919 | } |
| 920 | // Chomp the file to get lines |
| 921 | size_t BufferSize = Buffer->getBufferSize(); |
| 922 | const char *BufferStart = Buffer->getBufferStart(); |
| 923 | for (const char *Start = BufferStart, *End = BufferStart; |
| 924 | End < BufferStart + BufferSize; End++) |
| 925 | if (*End == '\n' || End == BufferStart + BufferSize - 1 || |
| 926 | (*End == '\r' && *(End + 1) == '\n')) { |
| 927 | LineCache[LineInfo.FileName].push_back(StringRef(Start, End - Start)); |
| 928 | if (*End == '\r') |
| 929 | End++; |
| 930 | Start = End + 1; |
| 931 | } |
| 932 | SourceCache[LineInfo.FileName] = std::move(Buffer); |
| 933 | return true; |
| 934 | } |
| 935 | |
| 936 | void SourcePrinter::printSourceLine(raw_ostream &OS, uint64_t Address, |
| 937 | StringRef Delimiter) { |
| 938 | if (!Symbolizer) |
| 939 | return; |
| 940 | DILineInfo LineInfo = DILineInfo(); |
| 941 | auto ExpectecLineInfo = |
| 942 | Symbolizer->symbolizeCode(Obj->getFileName(), Address); |
| 943 | if (!ExpectecLineInfo) |
| 944 | consumeError(ExpectecLineInfo.takeError()); |
| 945 | else |
| 946 | LineInfo = *ExpectecLineInfo; |
| 947 | |
| 948 | if ((LineInfo.FileName == "<invalid>") || OldLineInfo.Line == LineInfo.Line || |
| 949 | LineInfo.Line == 0) |
| 950 | return; |
| 951 | |
| 952 | if (PrintLines) |
| 953 | OS << Delimiter << LineInfo.FileName << ":" << LineInfo.Line << "\n"; |
| 954 | if (PrintSource) { |
| 955 | if (SourceCache.find(LineInfo.FileName) == SourceCache.end()) |
| 956 | if (!cacheSource(LineInfo)) |
| 957 | return; |
| 958 | auto FileBuffer = SourceCache.find(LineInfo.FileName); |
| 959 | if (FileBuffer != SourceCache.end()) { |
| 960 | auto LineBuffer = LineCache.find(LineInfo.FileName); |
| 961 | if (LineBuffer != LineCache.end()) { |
| 962 | if (LineInfo.Line > LineBuffer->second.size()) |
| 963 | return; |
| 964 | // Vector begins at 0, line numbers are non-zero |
| 965 | OS << Delimiter << LineBuffer->second[LineInfo.Line - 1].ltrim() |
| 966 | << "\n"; |
| 967 | } |
| 968 | } |
| 969 | } |
| 970 | OldLineInfo = LineInfo; |
| 971 | } |
| 972 | |
| 973 | static bool isArmElf(const ObjectFile *Obj) { |
| 974 | return (Obj->isELF() && |
| 975 | (Obj->getArch() == Triple::aarch64 || |
| 976 | Obj->getArch() == Triple::aarch64_be || |
| 977 | Obj->getArch() == Triple::arm || Obj->getArch() == Triple::armeb || |
| 978 | Obj->getArch() == Triple::thumb || |
| 979 | Obj->getArch() == Triple::thumbeb)); |
| 980 | } |
| 981 | |
| 982 | class PrettyPrinter { |
| 983 | public: |
| 984 | virtual ~PrettyPrinter() = default; |
| 985 | virtual void printInst(MCInstPrinter &IP, const MCInst *MI, |
| 986 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 987 | raw_ostream &OS, StringRef Annot, |
| 988 | MCSubtargetInfo const &STI, SourcePrinter *SP, |
| 989 | std::vector<RelocationRef> *Rels = nullptr) { |
| 990 | if (SP && (PrintSource || PrintLines)) |
| 991 | SP->printSourceLine(OS, Address); |
| 992 | if (!NoLeadingAddr) |
| 993 | OS << format("%8" PRIx64 ":", Address); |
| 994 | if (!NoShowRawInsn) { |
| 995 | OS << "\t"; |
| 996 | dumpBytes(Bytes, OS); |
| 997 | } |
| 998 | if (MI) |
| 999 | IP.printInst(MI, OS, "", STI); |
| 1000 | else |
| 1001 | OS << " <unknown>"; |
| 1002 | } |
| 1003 | }; |
| 1004 | PrettyPrinter PrettyPrinterInst; |
| 1005 | class HexagonPrettyPrinter : public PrettyPrinter { |
| 1006 | public: |
| 1007 | void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 1008 | raw_ostream &OS) { |
| 1009 | uint32_t opcode = |
| 1010 | (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0]; |
| 1011 | if (!NoLeadingAddr) |
| 1012 | OS << format("%8" PRIx64 ":", Address); |
| 1013 | if (!NoShowRawInsn) { |
| 1014 | OS << "\t"; |
| 1015 | dumpBytes(Bytes.slice(0, 4), OS); |
| 1016 | OS << format("%08" PRIx32, opcode); |
| 1017 | } |
| 1018 | } |
| 1019 | void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, |
| 1020 | uint64_t Address, raw_ostream &OS, StringRef Annot, |
| 1021 | MCSubtargetInfo const &STI, SourcePrinter *SP, |
| 1022 | std::vector<RelocationRef> *Rels) override { |
| 1023 | if (SP && (PrintSource || PrintLines)) |
| 1024 | SP->printSourceLine(OS, Address, ""); |
| 1025 | if (!MI) { |
| 1026 | printLead(Bytes, Address, OS); |
| 1027 | OS << " <unknown>"; |
| 1028 | return; |
| 1029 | } |
| 1030 | std::string Buffer; |
| 1031 | { |
| 1032 | raw_string_ostream TempStream(Buffer); |
| 1033 | IP.printInst(MI, TempStream, "", STI); |
| 1034 | } |
| 1035 | StringRef Contents(Buffer); |
| 1036 | // Split off bundle attributes |
| 1037 | auto PacketBundle = Contents.rsplit('\n'); |
| 1038 | // Split off first instruction from the rest |
| 1039 | auto HeadTail = PacketBundle.first.split('\n'); |
| 1040 | auto Preamble = " { "; |
| 1041 | auto Separator = ""; |
| 1042 | StringRef Fmt = "\t\t\t%08" PRIx64 ": "; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1043 | std::vector<RelocationRef>::const_iterator RelCur = Rels->begin(); |
| 1044 | std::vector<RelocationRef>::const_iterator RelEnd = Rels->end(); |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1045 | |
| 1046 | // Hexagon's packets require relocations to be inline rather than |
| 1047 | // clustered at the end of the packet. |
| 1048 | auto PrintReloc = [&]() -> void { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1049 | while ((RelCur != RelEnd) && (RelCur->getOffset() <= Address)) { |
| 1050 | if (RelCur->getOffset() == Address) { |
| 1051 | SmallString<16> Name; |
| 1052 | SmallString<32> Val; |
| 1053 | RelCur->getTypeName(Name); |
| 1054 | error(getRelocationValueString(*RelCur, Val)); |
| 1055 | OS << Separator << format(Fmt.data(), Address) << Name << "\t" << Val |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1056 | << "\n"; |
| 1057 | return; |
| 1058 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1059 | ++RelCur; |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1060 | } |
| 1061 | }; |
| 1062 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1063 | while (!HeadTail.first.empty()) { |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1064 | OS << Separator; |
| 1065 | Separator = "\n"; |
| 1066 | if (SP && (PrintSource || PrintLines)) |
| 1067 | SP->printSourceLine(OS, Address, ""); |
| 1068 | printLead(Bytes, Address, OS); |
| 1069 | OS << Preamble; |
| 1070 | Preamble = " "; |
| 1071 | StringRef Inst; |
| 1072 | auto Duplex = HeadTail.first.split('\v'); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1073 | if (!Duplex.second.empty()) { |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1074 | OS << Duplex.first; |
| 1075 | OS << "; "; |
| 1076 | Inst = Duplex.second; |
| 1077 | } |
| 1078 | else |
| 1079 | Inst = HeadTail.first; |
| 1080 | OS << Inst; |
| 1081 | HeadTail = HeadTail.second.split('\n'); |
| 1082 | if (HeadTail.first.empty()) |
| 1083 | OS << " } " << PacketBundle.second; |
| 1084 | PrintReloc(); |
| 1085 | Bytes = Bytes.slice(4); |
| 1086 | Address += 4; |
| 1087 | } |
| 1088 | } |
| 1089 | }; |
| 1090 | HexagonPrettyPrinter HexagonPrettyPrinterInst; |
| 1091 | |
| 1092 | class AMDGCNPrettyPrinter : public PrettyPrinter { |
| 1093 | public: |
| 1094 | void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, |
| 1095 | uint64_t Address, raw_ostream &OS, StringRef Annot, |
| 1096 | MCSubtargetInfo const &STI, SourcePrinter *SP, |
| 1097 | std::vector<RelocationRef> *Rels) override { |
| 1098 | if (SP && (PrintSource || PrintLines)) |
| 1099 | SP->printSourceLine(OS, Address); |
| 1100 | |
| 1101 | typedef support::ulittle32_t U32; |
| 1102 | |
| 1103 | if (MI) { |
| 1104 | SmallString<40> InstStr; |
| 1105 | raw_svector_ostream IS(InstStr); |
| 1106 | |
| 1107 | IP.printInst(MI, IS, "", STI); |
| 1108 | |
| 1109 | OS << left_justify(IS.str(), 60); |
| 1110 | } else { |
| 1111 | // an unrecognized encoding - this is probably data so represent it |
| 1112 | // using the .long directive, or .byte directive if fewer than 4 bytes |
| 1113 | // remaining |
| 1114 | if (Bytes.size() >= 4) { |
| 1115 | OS << format("\t.long 0x%08" PRIx32 " ", |
| 1116 | static_cast<uint32_t>(*reinterpret_cast<const U32*>(Bytes.data()))); |
| 1117 | OS.indent(42); |
| 1118 | } else { |
| 1119 | OS << format("\t.byte 0x%02" PRIx8, Bytes[0]); |
| 1120 | for (unsigned int i = 1; i < Bytes.size(); i++) |
| 1121 | OS << format(", 0x%02" PRIx8, Bytes[i]); |
| 1122 | OS.indent(55 - (6 * Bytes.size())); |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | OS << format("// %012" PRIX64 ": ", Address); |
| 1127 | if (Bytes.size() >=4) { |
| 1128 | for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()), |
| 1129 | Bytes.size() / sizeof(U32))) |
| 1130 | // D should be explicitly casted to uint32_t here as it is passed |
| 1131 | // by format to snprintf as vararg. |
| 1132 | OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D)); |
| 1133 | } else { |
| 1134 | for (unsigned int i = 0; i < Bytes.size(); i++) |
| 1135 | OS << format("%02" PRIX8 " ", Bytes[i]); |
| 1136 | } |
| 1137 | |
| 1138 | if (!Annot.empty()) |
| 1139 | OS << "// " << Annot; |
| 1140 | } |
| 1141 | }; |
| 1142 | AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst; |
| 1143 | |
| 1144 | class BPFPrettyPrinter : public PrettyPrinter { |
| 1145 | public: |
| 1146 | void printInst(MCInstPrinter &IP, const MCInst *MI, ArrayRef<uint8_t> Bytes, |
| 1147 | uint64_t Address, raw_ostream &OS, StringRef Annot, |
| 1148 | MCSubtargetInfo const &STI, SourcePrinter *SP, |
| 1149 | std::vector<RelocationRef> *Rels) override { |
| 1150 | if (SP && (PrintSource || PrintLines)) |
| 1151 | SP->printSourceLine(OS, Address); |
| 1152 | if (!NoLeadingAddr) |
| 1153 | OS << format("%8" PRId64 ":", Address / 8); |
| 1154 | if (!NoShowRawInsn) { |
| 1155 | OS << "\t"; |
| 1156 | dumpBytes(Bytes, OS); |
| 1157 | } |
| 1158 | if (MI) |
| 1159 | IP.printInst(MI, OS, "", STI); |
| 1160 | else |
| 1161 | OS << " <unknown>"; |
| 1162 | } |
| 1163 | }; |
| 1164 | BPFPrettyPrinter BPFPrettyPrinterInst; |
| 1165 | |
| 1166 | PrettyPrinter &selectPrettyPrinter(Triple const &Triple) { |
| 1167 | switch(Triple.getArch()) { |
| 1168 | default: |
| 1169 | return PrettyPrinterInst; |
| 1170 | case Triple::hexagon: |
| 1171 | return HexagonPrettyPrinterInst; |
| 1172 | case Triple::amdgcn: |
| 1173 | return AMDGCNPrettyPrinterInst; |
| 1174 | case Triple::bpfel: |
| 1175 | case Triple::bpfeb: |
| 1176 | return BPFPrettyPrinterInst; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1181 | static uint8_t getElfSymbolType(const ObjectFile *Obj, const SymbolRef &Sym) { |
| 1182 | assert(Obj->isELF()); |
| 1183 | if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj)) |
| 1184 | return Elf32LEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); |
| 1185 | if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj)) |
| 1186 | return Elf64LEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); |
| 1187 | if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj)) |
| 1188 | return Elf32BEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); |
| 1189 | if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj)) |
| 1190 | return Elf64BEObj->getSymbol(Sym.getRawDataRefImpl())->getType(); |
| 1191 | llvm_unreachable("Unsupported binary format"); |
| 1192 | } |
| 1193 | |
Sam Parker | 5fba45a | 2017-02-08 09:44:18 +0000 | [diff] [blame] | 1194 | template <class ELFT> static void |
| 1195 | addDynamicElfSymbols(const ELFObjectFile<ELFT> *Obj, |
| 1196 | std::map<SectionRef, SectionSymbolsTy> &AllSymbols) { |
| 1197 | for (auto Symbol : Obj->getDynamicSymbolIterators()) { |
| 1198 | uint8_t SymbolType = Symbol.getELFType(); |
| 1199 | if (SymbolType != ELF::STT_FUNC || Symbol.getSize() == 0) |
| 1200 | continue; |
| 1201 | |
| 1202 | Expected<uint64_t> AddressOrErr = Symbol.getAddress(); |
| 1203 | if (!AddressOrErr) |
| 1204 | report_error(Obj->getFileName(), AddressOrErr.takeError()); |
Sam Parker | 5fba45a | 2017-02-08 09:44:18 +0000 | [diff] [blame] | 1205 | |
| 1206 | Expected<StringRef> Name = Symbol.getName(); |
| 1207 | if (!Name) |
| 1208 | report_error(Obj->getFileName(), Name.takeError()); |
| 1209 | if (Name->empty()) |
| 1210 | continue; |
| 1211 | |
| 1212 | Expected<section_iterator> SectionOrErr = Symbol.getSection(); |
| 1213 | if (!SectionOrErr) |
| 1214 | report_error(Obj->getFileName(), SectionOrErr.takeError()); |
| 1215 | section_iterator SecI = *SectionOrErr; |
| 1216 | if (SecI == Obj->section_end()) |
| 1217 | continue; |
| 1218 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1219 | AllSymbols[*SecI].emplace_back(*AddressOrErr, *Name, SymbolType); |
Sam Parker | 5fba45a | 2017-02-08 09:44:18 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | static void |
| 1224 | addDynamicElfSymbols(const ObjectFile *Obj, |
| 1225 | std::map<SectionRef, SectionSymbolsTy> &AllSymbols) { |
| 1226 | assert(Obj->isELF()); |
| 1227 | if (auto *Elf32LEObj = dyn_cast<ELF32LEObjectFile>(Obj)) |
| 1228 | addDynamicElfSymbols(Elf32LEObj, AllSymbols); |
| 1229 | else if (auto *Elf64LEObj = dyn_cast<ELF64LEObjectFile>(Obj)) |
| 1230 | addDynamicElfSymbols(Elf64LEObj, AllSymbols); |
| 1231 | else if (auto *Elf32BEObj = dyn_cast<ELF32BEObjectFile>(Obj)) |
| 1232 | addDynamicElfSymbols(Elf32BEObj, AllSymbols); |
| 1233 | else if (auto *Elf64BEObj = cast<ELF64BEObjectFile>(Obj)) |
| 1234 | addDynamicElfSymbols(Elf64BEObj, AllSymbols); |
| 1235 | else |
| 1236 | llvm_unreachable("Unsupported binary format"); |
| 1237 | } |
| 1238 | |
Joel Galenson | 134cf47 | 2018-08-24 15:21:57 +0000 | [diff] [blame] | 1239 | static void addPltEntries(const ObjectFile *Obj, |
| 1240 | std::map<SectionRef, SectionSymbolsTy> &AllSymbols, |
| 1241 | StringSaver &Saver) { |
| 1242 | Optional<SectionRef> Plt = None; |
| 1243 | for (const SectionRef &Section : Obj->sections()) { |
| 1244 | StringRef Name; |
| 1245 | if (Section.getName(Name)) |
| 1246 | continue; |
| 1247 | if (Name == ".plt") |
| 1248 | Plt = Section; |
| 1249 | } |
| 1250 | if (!Plt) |
| 1251 | return; |
| 1252 | if (auto *ElfObj = dyn_cast<ELFObjectFileBase>(Obj)) { |
| 1253 | for (auto PltEntry : ElfObj->getPltAddresses()) { |
| 1254 | SymbolRef Symbol(PltEntry.first, ElfObj); |
Joel Galenson | 134cf47 | 2018-08-24 15:21:57 +0000 | [diff] [blame] | 1255 | uint8_t SymbolType = getElfSymbolType(Obj, Symbol); |
| 1256 | |
| 1257 | Expected<StringRef> NameOrErr = Symbol.getName(); |
| 1258 | if (!NameOrErr) |
| 1259 | report_error(Obj->getFileName(), NameOrErr.takeError()); |
| 1260 | if (NameOrErr->empty()) |
| 1261 | continue; |
| 1262 | StringRef Name = Saver.save((*NameOrErr + "@plt").str()); |
| 1263 | |
| 1264 | AllSymbols[*Plt].emplace_back(PltEntry.second, Name, SymbolType); |
| 1265 | } |
| 1266 | } |
| 1267 | } |
| 1268 | |
George Rimar | 70d197d | 2019-01-10 14:55:26 +0000 | [diff] [blame] | 1269 | // Normally the disassembly output will skip blocks of zeroes. This function |
| 1270 | // returns the number of zero bytes that can be skipped when dumping the |
| 1271 | // disassembly of the instructions in Buf. |
| 1272 | static size_t countSkippableZeroBytes(ArrayRef<uint8_t> Buf) { |
| 1273 | // When -z or --disassemble-zeroes are given we always dissasemble them. |
| 1274 | if (DisassembleZeroes) |
| 1275 | return 0; |
| 1276 | |
| 1277 | // Find the number of leading zeroes. |
| 1278 | size_t N = 0; |
| 1279 | while (N < Buf.size() && !Buf[N]) |
| 1280 | ++N; |
| 1281 | |
| 1282 | // We may want to skip blocks of zero bytes, but unless we see |
| 1283 | // at least 8 of them in a row. |
| 1284 | if (N < 8) |
| 1285 | return 0; |
| 1286 | |
| 1287 | // We skip zeroes in multiples of 4 because do not want to truncate an |
| 1288 | // instruction if it starts with a zero byte. |
| 1289 | return N & ~0x3; |
| 1290 | } |
| 1291 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1292 | static void disassembleObject(const ObjectFile *Obj, bool InlineRelocs) { |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1293 | if (StartAddress > StopAddress) |
| 1294 | error("Start address should be less than stop address"); |
| 1295 | |
Jim Grosbach | af9aec0 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 1296 | const Target *TheTarget = getTarget(Obj); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1297 | |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 1298 | // Package up features to be passed to target/subtarget |
Daniel Sanders | 1d14864 | 2016-06-16 09:17:03 +0000 | [diff] [blame] | 1299 | SubtargetFeatures Features = Obj->getFeatures(); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1300 | if (!MAttrs.empty()) |
| 1301 | for (unsigned I = 0; I != MAttrs.size(); ++I) |
| 1302 | Features.AddFeature(MAttrs[I]); |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 1303 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1304 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 1305 | TheTarget->createMCRegInfo(TripleName)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 1306 | if (!MRI) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1307 | report_error(Obj->getFileName(), "no register info for target " + |
| 1308 | TripleName); |
Ahmed Bougacha | 0835ca1 | 2013-05-16 21:28:23 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1311 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
| 1312 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 1313 | if (!AsmInfo) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1314 | report_error(Obj->getFileName(), "no assembly info for target " + |
| 1315 | TripleName); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1316 | std::unique_ptr<const MCSubtargetInfo> STI( |
Daniel Sanders | 1d14864 | 2016-06-16 09:17:03 +0000 | [diff] [blame] | 1317 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString())); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 1318 | if (!STI) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1319 | report_error(Obj->getFileName(), "no subtarget info for target " + |
| 1320 | TripleName); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1321 | std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 1322 | if (!MII) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1323 | report_error(Obj->getFileName(), "no instruction info for target " + |
| 1324 | TripleName); |
Sam Kolton | 3381d7a | 2016-10-06 13:46:08 +0000 | [diff] [blame] | 1325 | MCObjectFileInfo MOFI; |
| 1326 | MCContext Ctx(AsmInfo.get(), MRI.get(), &MOFI); |
| 1327 | // FIXME: for now initialize MCObjectFileInfo with default values |
Rafael Espindola | 9f92995 | 2017-08-02 20:32:26 +0000 | [diff] [blame] | 1328 | MOFI.InitMCObjectFileInfo(Triple(TripleName), false, Ctx); |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 1329 | |
| 1330 | std::unique_ptr<MCDisassembler> DisAsm( |
| 1331 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 1332 | if (!DisAsm) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1333 | report_error(Obj->getFileName(), "no disassembler for target " + |
| 1334 | TripleName); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 1335 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 1336 | std::unique_ptr<const MCInstrAnalysis> MIA( |
| 1337 | TheTarget->createMCInstrAnalysis(MII.get())); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 1338 | |
Ahmed Bougacha | 0835ca1 | 2013-05-16 21:28:23 +0000 | [diff] [blame] | 1339 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1340 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 1341 | Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 1342 | if (!IP) |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1343 | report_error(Obj->getFileName(), "no instruction printer for target " + |
| 1344 | TripleName); |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 1345 | IP->setPrintImmHex(PrintImmHex); |
Colin LeMahieu | 35436a2 | 2015-05-29 14:48:25 +0000 | [diff] [blame] | 1346 | PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName)); |
Ahmed Bougacha | 0835ca1 | 2013-05-16 21:28:23 +0000 | [diff] [blame] | 1347 | |
Greg Fitzgerald | 1843227 | 2014-03-20 22:55:15 +0000 | [diff] [blame] | 1348 | StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " : |
| 1349 | "\t\t\t%08" PRIx64 ": "; |
| 1350 | |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 1351 | SourcePrinter SP(Obj, TheTarget->getName()); |
| 1352 | |
Mark Seaborn | 0929d3d | 2014-01-25 17:38:19 +0000 | [diff] [blame] | 1353 | // Create a mapping, RelocSecs = SectionRelocMap[S], where sections |
| 1354 | // in RelocSecs contain the relocations for section S. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1355 | std::error_code EC; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1356 | std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1357 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1358 | section_iterator Sec2 = Section.getRelocatedSection(); |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 1359 | if (Sec2 != Obj->section_end()) |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1360 | SectionRelocMap[*Sec2].push_back(Section); |
Mark Seaborn | 0929d3d | 2014-01-25 17:38:19 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1363 | // Create a mapping from virtual address to symbol name. This is used to |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1364 | // pretty print the symbols while disassembling. |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1365 | std::map<SectionRef, SectionSymbolsTy> AllSymbols; |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1366 | SectionSymbolsTy AbsoluteSymbols; |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1367 | for (const SymbolRef &Symbol : Obj->symbols()) { |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 1368 | Expected<uint64_t> AddressOrErr = Symbol.getAddress(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1369 | if (!AddressOrErr) |
| 1370 | report_error(Obj->getFileName(), AddressOrErr.takeError()); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1371 | uint64_t Address = *AddressOrErr; |
David Majnemer | 2603a8fa | 2015-07-09 18:11:40 +0000 | [diff] [blame] | 1372 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1373 | Expected<StringRef> Name = Symbol.getName(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1374 | if (!Name) |
| 1375 | report_error(Obj->getFileName(), Name.takeError()); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1376 | if (Name->empty()) |
| 1377 | continue; |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1378 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1379 | Expected<section_iterator> SectionOrErr = Symbol.getSection(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1380 | if (!SectionOrErr) |
| 1381 | report_error(Obj->getFileName(), SectionOrErr.takeError()); |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1382 | |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1383 | uint8_t SymbolType = ELF::STT_NOTYPE; |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1384 | if (Obj->isELF()) |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1385 | SymbolType = getElfSymbolType(Obj, Symbol); |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1386 | |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1387 | section_iterator SecI = *SectionOrErr; |
| 1388 | if (SecI != Obj->section_end()) |
| 1389 | AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType); |
| 1390 | else |
| 1391 | AbsoluteSymbols.emplace_back(Address, *Name, SymbolType); |
| 1392 | |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1393 | |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1394 | } |
Sam Parker | 5fba45a | 2017-02-08 09:44:18 +0000 | [diff] [blame] | 1395 | if (AllSymbols.empty() && Obj->isELF()) |
| 1396 | addDynamicElfSymbols(Obj, AllSymbols); |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1397 | |
Joel Galenson | 134cf47 | 2018-08-24 15:21:57 +0000 | [diff] [blame] | 1398 | BumpPtrAllocator A; |
| 1399 | StringSaver Saver(A); |
| 1400 | addPltEntries(Obj, AllSymbols, Saver); |
| 1401 | |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1402 | // Create a mapping from virtual address to section. |
| 1403 | std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses; |
| 1404 | for (SectionRef Sec : Obj->sections()) |
| 1405 | SectionAddresses.emplace_back(Sec.getAddress(), Sec); |
| 1406 | array_pod_sort(SectionAddresses.begin(), SectionAddresses.end()); |
| 1407 | |
| 1408 | // Linked executables (.exe and .dll files) typically don't include a real |
| 1409 | // symbol table but they might contain an export table. |
| 1410 | if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) { |
| 1411 | for (const auto &ExportEntry : COFFObj->export_directories()) { |
| 1412 | StringRef Name; |
| 1413 | error(ExportEntry.getSymbolName(Name)); |
| 1414 | if (Name.empty()) |
| 1415 | continue; |
| 1416 | uint32_t RVA; |
| 1417 | error(ExportEntry.getExportRVA(RVA)); |
| 1418 | |
| 1419 | uint64_t VA = COFFObj->getImageBase() + RVA; |
| 1420 | auto Sec = std::upper_bound( |
| 1421 | SectionAddresses.begin(), SectionAddresses.end(), VA, |
| 1422 | [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) { |
| 1423 | return LHS < RHS.first; |
| 1424 | }); |
| 1425 | if (Sec != SectionAddresses.begin()) |
| 1426 | --Sec; |
| 1427 | else |
| 1428 | Sec = SectionAddresses.end(); |
| 1429 | |
| 1430 | if (Sec != SectionAddresses.end()) |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1431 | AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE); |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1432 | else |
| 1433 | AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | // Sort all the symbols, this allows us to use a simple binary search to find |
| 1438 | // a symbol near an address. |
| 1439 | for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols) |
| 1440 | array_pod_sort(SecSyms.second.begin(), SecSyms.second.end()); |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1441 | array_pod_sort(AbsoluteSymbols.begin(), AbsoluteSymbols.end()); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1442 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1443 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Colin LeMahieu | f34933e | 2015-07-23 20:58:49 +0000 | [diff] [blame] | 1444 | if (!DisassembleAll && (!Section.isText() || Section.isVirtual())) |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1445 | continue; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 1446 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1447 | uint64_t SectionAddr = Section.getAddress(); |
| 1448 | uint64_t SectSize = Section.getSize(); |
David Majnemer | 185b5b1 | 2014-11-11 09:58:25 +0000 | [diff] [blame] | 1449 | if (!SectSize) |
| 1450 | continue; |
Simon Atanasyan | 2b614e1 | 2014-02-24 22:12:11 +0000 | [diff] [blame] | 1451 | |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1452 | // Get the list of all the symbols in this section. |
| 1453 | SectionSymbolsTy &Symbols = AllSymbols[Section]; |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1454 | std::vector<uint64_t> DataMappingSymsAddr; |
| 1455 | std::vector<uint64_t> TextMappingSymsAddr; |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1456 | if (isArmElf(Obj)) { |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1457 | for (const auto &Symb : Symbols) { |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1458 | uint64_t Address = std::get<0>(Symb); |
| 1459 | StringRef Name = std::get<1>(Symb); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1460 | if (Name.startswith("$d")) |
David Majnemer | 153722d | 2015-11-18 04:35:32 +0000 | [diff] [blame] | 1461 | DataMappingSymsAddr.push_back(Address - SectionAddr); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1462 | if (Name.startswith("$x")) |
David Majnemer | 153722d | 2015-11-18 04:35:32 +0000 | [diff] [blame] | 1463 | TextMappingSymsAddr.push_back(Address - SectionAddr); |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1464 | if (Name.startswith("$a")) |
| 1465 | TextMappingSymsAddr.push_back(Address - SectionAddr); |
| 1466 | if (Name.startswith("$t")) |
| 1467 | TextMappingSymsAddr.push_back(Address - SectionAddr); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | |
Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 1471 | llvm::sort(DataMappingSymsAddr); |
| 1472 | llvm::sort(TextMappingSymsAddr); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1473 | |
Sam Kolton | 3381d7a | 2016-10-06 13:46:08 +0000 | [diff] [blame] | 1474 | if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) { |
| 1475 | // AMDGPU disassembler uses symbolizer for printing labels |
| 1476 | std::unique_ptr<MCRelocationInfo> RelInfo( |
| 1477 | TheTarget->createMCRelocationInfo(TripleName, Ctx)); |
| 1478 | if (RelInfo) { |
| 1479 | std::unique_ptr<MCSymbolizer> Symbolizer( |
| 1480 | TheTarget->createMCSymbolizer( |
| 1481 | TripleName, nullptr, nullptr, &Symbols, &Ctx, std::move(RelInfo))); |
| 1482 | DisAsm->setSymbolizer(std::move(Symbolizer)); |
| 1483 | } |
| 1484 | } |
| 1485 | |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1486 | // Make a list of all the relocations for this section. |
| 1487 | std::vector<RelocationRef> Rels; |
| 1488 | if (InlineRelocs) { |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1489 | for (const SectionRef &RelocSec : SectionRelocMap[Section]) { |
| 1490 | for (const RelocationRef &Reloc : RelocSec.relocations()) { |
| 1491 | Rels.push_back(Reloc); |
| 1492 | } |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | // Sort relocations by address. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1497 | llvm::sort(Rels, isRelocAddressLess); |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1498 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1499 | StringRef SegmentName = ""; |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1500 | if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1501 | DataRefImpl DR = Section.getRawDataRefImpl(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1502 | SegmentName = MachO->getSectionFinalSegmentName(DR); |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1503 | } |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 1504 | StringRef SectionName; |
| 1505 | error(Section.getName(SectionName)); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1506 | |
Rafael Espindola | 7884c95 | 2015-06-04 15:01:05 +0000 | [diff] [blame] | 1507 | // If the section has no symbol at the start, just insert a dummy one. |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1508 | if (Symbols.empty() || std::get<0>(Symbols[0]) != 0) { |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 1509 | Symbols.insert( |
| 1510 | Symbols.begin(), |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1511 | std::make_tuple(SectionAddr, SectionName, |
| 1512 | Section.isText() ? ELF::STT_FUNC : ELF::STT_OBJECT)); |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1513 | } |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 1514 | |
| 1515 | SmallString<40> Comments; |
| 1516 | raw_svector_ostream CommentStream(Comments); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 1517 | |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 1518 | StringRef BytesStr; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1519 | error(Section.getContents(BytesStr)); |
Aaron Ballman | 106fd7b | 2014-11-12 14:01:17 +0000 | [diff] [blame] | 1520 | ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()), |
| 1521 | BytesStr.size()); |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 1522 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1523 | uint64_t Size; |
| 1524 | uint64_t Index; |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 1525 | bool PrintedSection = false; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1526 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1527 | std::vector<RelocationRef>::const_iterator RelCur = Rels.begin(); |
| 1528 | std::vector<RelocationRef>::const_iterator RelEnd = Rels.end(); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1529 | // Disassemble symbol by symbol. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1530 | for (unsigned SI = 0, SE = Symbols.size(); SI != SE; ++SI) { |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1531 | uint64_t Start = std::get<0>(Symbols[SI]) - SectionAddr; |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1532 | // The end is either the section end or the beginning of the next |
| 1533 | // symbol. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1534 | uint64_t End = (SI == SE - 1) |
| 1535 | ? SectSize |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1536 | : std::get<0>(Symbols[SI + 1]) - SectionAddr; |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1537 | // Don't try to disassemble beyond the end of section contents. |
| 1538 | if (End > SectSize) |
| 1539 | End = SectSize; |
Rafael Espindola | e45c740 | 2014-08-17 16:31:39 +0000 | [diff] [blame] | 1540 | // If this symbol has the same address as the next symbol, then skip it. |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1541 | if (Start >= End) |
Michael J. Spencer | ee84f64 | 2011-10-13 20:37:08 +0000 | [diff] [blame] | 1542 | continue; |
| 1543 | |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1544 | // Check if we need to skip symbol |
| 1545 | // Skip if the symbol's data is not between StartAddress and StopAddress |
| 1546 | if (End + SectionAddr < StartAddress || |
| 1547 | Start + SectionAddr > StopAddress) { |
| 1548 | continue; |
| 1549 | } |
| 1550 | |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 1551 | /// Skip if user requested specific symbols and this is not in the list |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1552 | if (!DisasmFuncsSet.empty() && |
| 1553 | !DisasmFuncsSet.count(std::get<1>(Symbols[SI]))) |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 1554 | continue; |
| 1555 | |
| 1556 | if (!PrintedSection) { |
| 1557 | PrintedSection = true; |
| 1558 | outs() << "Disassembly of section "; |
| 1559 | if (!SegmentName.empty()) |
| 1560 | outs() << SegmentName << ","; |
| 1561 | outs() << SectionName << ':'; |
| 1562 | } |
| 1563 | |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1564 | // Stop disassembly at the stop address specified |
| 1565 | if (End + SectionAddr > StopAddress) |
| 1566 | End = StopAddress - SectionAddr; |
| 1567 | |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 1568 | if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) { |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1569 | if (std::get<2>(Symbols[SI]) == ELF::STT_AMDGPU_HSA_KERNEL) { |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1570 | // skip amd_kernel_code_t at the begining of kernel symbol (256 bytes) |
| 1571 | Start += 256; |
| 1572 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1573 | if (SI == SE - 1 || |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1574 | std::get<2>(Symbols[SI + 1]) == ELF::STT_AMDGPU_HSA_KERNEL) { |
Sam Kolton | c05d778 | 2016-08-17 10:17:57 +0000 | [diff] [blame] | 1575 | // cut trailing zeroes at the end of kernel |
| 1576 | // cut up to 256 bytes |
| 1577 | const uint64_t EndAlign = 256; |
| 1578 | const auto Limit = End - (std::min)(EndAlign, End - Start); |
| 1579 | while (End > Limit && |
| 1580 | *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0) |
| 1581 | End -= 4; |
| 1582 | } |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
George Rimar | 6622d41 | 2018-12-19 10:21:45 +0000 | [diff] [blame] | 1585 | outs() << '\n'; |
George Rimar | 3ba0f3c0 | 2019-01-09 14:43:33 +0000 | [diff] [blame] | 1586 | if (!NoLeadingAddr) |
| 1587 | outs() << format("%016" PRIx64 " ", SectionAddr + Start); |
| 1588 | |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1589 | StringRef SymbolName = std::get<1>(Symbols[SI]); |
George Rimar | 6622d41 | 2018-12-19 10:21:45 +0000 | [diff] [blame] | 1590 | if (Demangle) |
| 1591 | outs() << demangle(SymbolName) << ":\n"; |
| 1592 | else |
| 1593 | outs() << SymbolName << ":\n"; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1594 | |
Francis Visoiu Mistrih | 1834682 | 2018-04-19 17:02:57 +0000 | [diff] [blame] | 1595 | // Don't print raw contents of a virtual section. A virtual section |
| 1596 | // doesn't have any contents in the file. |
| 1597 | if (Section.isVirtual()) { |
| 1598 | outs() << "...\n"; |
| 1599 | continue; |
| 1600 | } |
| 1601 | |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1602 | #ifndef NDEBUG |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1603 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1604 | #else |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1605 | raw_ostream &DebugOut = nulls(); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1606 | #endif |
| 1607 | |
Wouter van Oortmerssen | f3b762a | 2019-01-17 18:14:09 +0000 | [diff] [blame] | 1608 | // Some targets (like WebAssembly) have a special prelude at the start |
| 1609 | // of each symbol. |
| 1610 | DisAsm->onSymbolStart(SymbolName, Size, Bytes.slice(Start, End - Start), |
| 1611 | SectionAddr + Start, DebugOut, CommentStream); |
| 1612 | Start += Size; |
| 1613 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1614 | for (Index = Start; Index < End; Index += Size) { |
| 1615 | MCInst Inst; |
Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 1616 | |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1617 | if (Index + SectionAddr < StartAddress || |
| 1618 | Index + SectionAddr > StopAddress) { |
| 1619 | // skip byte by byte till StartAddress is reached |
| 1620 | Size = 1; |
| 1621 | continue; |
| 1622 | } |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1623 | // AArch64 ELF binaries can interleave data and text in the |
| 1624 | // same section. We rely on the markers introduced to |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1625 | // understand what we need to dump. If the data marker is within a |
| 1626 | // function, it is denoted as a word/short etc |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1627 | if (isArmElf(Obj) && std::get<2>(Symbols[SI]) != ELF::STT_OBJECT && |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1628 | !DisassembleAll) { |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1629 | uint64_t Stride = 0; |
| 1630 | |
| 1631 | auto DAI = std::lower_bound(DataMappingSymsAddr.begin(), |
| 1632 | DataMappingSymsAddr.end(), Index); |
| 1633 | if (DAI != DataMappingSymsAddr.end() && *DAI == Index) { |
| 1634 | // Switch to data. |
| 1635 | while (Index < End) { |
| 1636 | outs() << format("%8" PRIx64 ":", SectionAddr + Index); |
| 1637 | outs() << "\t"; |
| 1638 | if (Index + 4 <= End) { |
| 1639 | Stride = 4; |
| 1640 | dumpBytes(Bytes.slice(Index, 4), outs()); |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1641 | outs() << "\t.word\t"; |
| 1642 | uint32_t Data = 0; |
| 1643 | if (Obj->isLittleEndian()) { |
| 1644 | const auto Word = |
| 1645 | reinterpret_cast<const support::ulittle32_t *>( |
| 1646 | Bytes.data() + Index); |
| 1647 | Data = *Word; |
| 1648 | } else { |
| 1649 | const auto Word = reinterpret_cast<const support::ubig32_t *>( |
| 1650 | Bytes.data() + Index); |
| 1651 | Data = *Word; |
| 1652 | } |
| 1653 | outs() << "0x" << format("%08" PRIx32, Data); |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1654 | } else if (Index + 2 <= End) { |
| 1655 | Stride = 2; |
| 1656 | dumpBytes(Bytes.slice(Index, 2), outs()); |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1657 | outs() << "\t\t.short\t"; |
| 1658 | uint16_t Data = 0; |
| 1659 | if (Obj->isLittleEndian()) { |
| 1660 | const auto Short = |
| 1661 | reinterpret_cast<const support::ulittle16_t *>( |
| 1662 | Bytes.data() + Index); |
| 1663 | Data = *Short; |
| 1664 | } else { |
| 1665 | const auto Short = |
| 1666 | reinterpret_cast<const support::ubig16_t *>(Bytes.data() + |
| 1667 | Index); |
| 1668 | Data = *Short; |
| 1669 | } |
| 1670 | outs() << "0x" << format("%04" PRIx16, Data); |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1671 | } else { |
| 1672 | Stride = 1; |
| 1673 | dumpBytes(Bytes.slice(Index, 1), outs()); |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1674 | outs() << "\t\t.byte\t"; |
| 1675 | outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]); |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1676 | } |
| 1677 | Index += Stride; |
| 1678 | outs() << "\n"; |
| 1679 | auto TAI = std::lower_bound(TextMappingSymsAddr.begin(), |
| 1680 | TextMappingSymsAddr.end(), Index); |
| 1681 | if (TAI != TextMappingSymsAddr.end() && *TAI == Index) |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1687 | // If there is a data symbol inside an ELF text section and we are only |
| 1688 | // disassembling text (applicable all architectures), |
| 1689 | // we are in a situation where we must print the data and not |
| 1690 | // disassemble it. |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1691 | if (Obj->isELF() && std::get<2>(Symbols[SI]) == ELF::STT_OBJECT && |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1692 | !DisassembleAll && Section.isText()) { |
| 1693 | // print out data up to 8 bytes at a time in hex and ascii |
| 1694 | uint8_t AsciiData[9] = {'\0'}; |
| 1695 | uint8_t Byte; |
| 1696 | int NumBytes = 0; |
| 1697 | |
| 1698 | for (Index = Start; Index < End; Index += 1) { |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1699 | if (((SectionAddr + Index) < StartAddress) || |
| 1700 | ((SectionAddr + Index) > StopAddress)) |
| 1701 | continue; |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1702 | if (NumBytes == 0) { |
| 1703 | outs() << format("%8" PRIx64 ":", SectionAddr + Index); |
| 1704 | outs() << "\t"; |
| 1705 | } |
| 1706 | Byte = Bytes.slice(Index)[0]; |
| 1707 | outs() << format(" %02x", Byte); |
Michael Kruse | 6f1da6e | 2018-07-26 15:31:41 +0000 | [diff] [blame] | 1708 | AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.'; |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1709 | |
| 1710 | uint8_t IndentOffset = 0; |
| 1711 | NumBytes++; |
| 1712 | if (Index == End - 1 || NumBytes > 8) { |
| 1713 | // Indent the space for less than 8 bytes data. |
| 1714 | // 2 spaces for byte and one for space between bytes |
| 1715 | IndentOffset = 3 * (8 - NumBytes); |
| 1716 | for (int Excess = 8 - NumBytes; Excess < 8; Excess++) |
| 1717 | AsciiData[Excess] = '\0'; |
| 1718 | NumBytes = 8; |
| 1719 | } |
| 1720 | if (NumBytes == 8) { |
| 1721 | AsciiData[8] = '\0'; |
| 1722 | outs() << std::string(IndentOffset, ' ') << " "; |
| 1723 | outs() << reinterpret_cast<char *>(AsciiData); |
| 1724 | outs() << '\n'; |
| 1725 | NumBytes = 0; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1729 | if (Index >= End) |
| 1730 | break; |
| 1731 | |
George Rimar | 70d197d | 2019-01-10 14:55:26 +0000 | [diff] [blame] | 1732 | if (size_t N = |
| 1733 | countSkippableZeroBytes(Bytes.slice(Index, End - Index))) { |
| 1734 | outs() << "\t\t..." << '\n'; |
| 1735 | Index += N; |
| 1736 | if (Index >= End) |
| 1737 | break; |
| 1738 | } |
| 1739 | |
Hemant Kulkarni | 5b60f63 | 2016-08-25 19:41:08 +0000 | [diff] [blame] | 1740 | // Disassemble a real instruction or a data when disassemble all is |
| 1741 | // provided |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1742 | bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), |
| 1743 | SectionAddr + Index, DebugOut, |
| 1744 | CommentStream); |
| 1745 | if (Size == 0) |
| 1746 | Size = 1; |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 1747 | |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1748 | PIP.printInst(*IP, Disassembled ? &Inst : nullptr, |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 1749 | Bytes.slice(Index, Size), SectionAddr + Index, outs(), "", |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1750 | *STI, &SP, &Rels); |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1751 | outs() << CommentStream.str(); |
| 1752 | Comments.clear(); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1753 | |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1754 | // Try to resolve the target of a call, tail call, etc. to a specific |
| 1755 | // symbol. |
| 1756 | if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) || |
| 1757 | MIA->isConditionalBranch(Inst))) { |
| 1758 | uint64_t Target; |
| 1759 | if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) { |
| 1760 | // In a relocatable object, the target's section must reside in |
| 1761 | // the same section as the call instruction or it is accessed |
| 1762 | // through a relocation. |
| 1763 | // |
| 1764 | // In a non-relocatable object, the target may be in any section. |
| 1765 | // |
| 1766 | // N.B. We don't walk the relocations in the relocatable case yet. |
| 1767 | auto *TargetSectionSymbols = &Symbols; |
| 1768 | if (!Obj->isRelocatableObject()) { |
| 1769 | auto SectionAddress = std::upper_bound( |
| 1770 | SectionAddresses.begin(), SectionAddresses.end(), Target, |
| 1771 | [](uint64_t LHS, |
| 1772 | const std::pair<uint64_t, SectionRef> &RHS) { |
| 1773 | return LHS < RHS.first; |
| 1774 | }); |
| 1775 | if (SectionAddress != SectionAddresses.begin()) { |
| 1776 | --SectionAddress; |
| 1777 | TargetSectionSymbols = &AllSymbols[SectionAddress->second]; |
| 1778 | } else { |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1779 | TargetSectionSymbols = &AbsoluteSymbols; |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1780 | } |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1781 | } |
David Majnemer | 2603a8fa | 2015-07-09 18:11:40 +0000 | [diff] [blame] | 1782 | |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1783 | // Find the first symbol in the section whose offset is less than |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1784 | // or equal to the target. If there isn't a section that contains |
| 1785 | // the target, find the nearest preceding absolute symbol. |
| 1786 | auto TargetSym = std::upper_bound( |
| 1787 | TargetSectionSymbols->begin(), TargetSectionSymbols->end(), |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1788 | Target, [](uint64_t LHS, |
| 1789 | const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { |
| 1790 | return LHS < std::get<0>(RHS); |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1791 | }); |
| 1792 | if (TargetSym == TargetSectionSymbols->begin()) { |
| 1793 | TargetSectionSymbols = &AbsoluteSymbols; |
| 1794 | TargetSym = std::upper_bound( |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1795 | AbsoluteSymbols.begin(), AbsoluteSymbols.end(), |
| 1796 | Target, [](uint64_t LHS, |
| 1797 | const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { |
| 1798 | return LHS < std::get<0>(RHS); |
| 1799 | }); |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1800 | } |
| 1801 | if (TargetSym != TargetSectionSymbols->begin()) { |
| 1802 | --TargetSym; |
Clement Courbet | 2d7d4a3 | 2019-01-18 09:40:19 +0000 | [diff] [blame^] | 1803 | uint64_t TargetAddress = std::get<0>(*TargetSym); |
| 1804 | StringRef TargetName = std::get<1>(*TargetSym); |
| 1805 | outs() << " <" << TargetName; |
| 1806 | uint64_t Disp = Target - TargetAddress; |
Sterling Augustine | bc78b62 | 2018-06-28 18:57:13 +0000 | [diff] [blame] | 1807 | if (Disp) |
| 1808 | outs() << "+0x" << Twine::utohexstr(Disp); |
| 1809 | outs() << '>'; |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1810 | } |
| 1811 | } |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1812 | } |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1813 | outs() << "\n"; |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1814 | |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1815 | // Hexagon does this in pretty printer |
| 1816 | if (Obj->getArch() != Triple::hexagon) |
| 1817 | // Print relocation for instruction. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1818 | while (RelCur != RelEnd) { |
| 1819 | uint64_t Addr = RelCur->getOffset(); |
| 1820 | SmallString<16> Name; |
| 1821 | SmallString<32> Val; |
Owen Anderson | fa3e520 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1822 | |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1823 | // If this relocation is hidden, skip it. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1824 | if (getHidden(*RelCur) || ((SectionAddr + Addr) < StartAddress)) { |
| 1825 | ++RelCur; |
Sid Manning | d9f2873 | 2018-05-14 19:46:08 +0000 | [diff] [blame] | 1826 | continue; |
| 1827 | } |
| 1828 | |
| 1829 | // Stop when rel_cur's address is past the current instruction. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1830 | if (Addr >= Index + Size) |
| 1831 | break; |
| 1832 | RelCur->getTypeName(Name); |
| 1833 | error(getRelocationValueString(*RelCur, Val)); |
| 1834 | outs() << format(Fmt.data(), SectionAddr + Addr) << Name << "\t" |
| 1835 | << Val << "\n"; |
| 1836 | ++RelCur; |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1837 | } |
Benjamin Kramer | 87ee76c | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 1838 | } |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1843 | void llvm::printRelocations(const ObjectFile *Obj) { |
Greg Fitzgerald | 1843227 | 2014-03-20 22:55:15 +0000 | [diff] [blame] | 1844 | StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : |
| 1845 | "%08" PRIx64; |
Rafael Espindola | 9219fe7 | 2016-03-21 20:59:15 +0000 | [diff] [blame] | 1846 | // Regular objdump doesn't print relocations in non-relocatable object |
| 1847 | // files. |
| 1848 | if (!Obj->isRelocatableObject()) |
| 1849 | return; |
Rafael Espindola | c66d761 | 2014-08-17 19:09:37 +0000 | [diff] [blame] | 1850 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1851 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1852 | if (Section.relocation_begin() == Section.relocation_end()) |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1853 | continue; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1854 | StringRef SecName; |
| 1855 | error(Section.getName(SecName)); |
| 1856 | outs() << "RELOCATION RECORDS FOR [" << SecName << "]:\n"; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1857 | for (const RelocationRef &Reloc : Section.relocations()) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1858 | uint64_t Address = Reloc.getOffset(); |
| 1859 | SmallString<32> RelocName; |
| 1860 | SmallString<32> ValueStr; |
| 1861 | if (Address < StartAddress || Address > StopAddress || getHidden(Reloc)) |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1862 | continue; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1863 | Reloc.getTypeName(RelocName); |
| 1864 | error(getRelocationValueString(Reloc, ValueStr)); |
| 1865 | outs() << format(Fmt.data(), Address) << " " << RelocName << " " |
| 1866 | << ValueStr << "\n"; |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1867 | } |
| 1868 | outs() << "\n"; |
| 1869 | } |
| 1870 | } |
| 1871 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1872 | void llvm::printDynamicRelocations(const ObjectFile *Obj) { |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 1873 | // For the moment, this option is for ELF only |
| 1874 | if (!Obj->isELF()) |
| 1875 | return; |
| 1876 | |
| 1877 | const auto *Elf = dyn_cast<ELFObjectFileBase>(Obj); |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 1878 | if (!Elf || Elf->getEType() != ELF::ET_DYN) { |
| 1879 | error("not a dynamic object"); |
| 1880 | return; |
| 1881 | } |
| 1882 | |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 1883 | std::vector<SectionRef> DynRelSec = Obj->dynamic_relocation_sections(); |
| 1884 | if (DynRelSec.empty()) |
| 1885 | return; |
| 1886 | |
| 1887 | outs() << "DYNAMIC RELOCATION RECORDS\n"; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1888 | StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64; |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 1889 | for (const SectionRef &Section : DynRelSec) { |
| 1890 | if (Section.relocation_begin() == Section.relocation_end()) |
| 1891 | continue; |
| 1892 | for (const RelocationRef &Reloc : Section.relocations()) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1893 | uint64_t Address = Reloc.getOffset(); |
| 1894 | SmallString<32> RelocName; |
| 1895 | SmallString<32> ValueStr; |
| 1896 | Reloc.getTypeName(RelocName); |
| 1897 | error(getRelocationValueString(Reloc, ValueStr)); |
| 1898 | outs() << format(Fmt.data(), Address) << " " << RelocName << " " |
| 1899 | << ValueStr << "\n"; |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1904 | void llvm::printSectionHeaders(const ObjectFile *Obj) { |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1905 | outs() << "Sections:\n" |
| 1906 | "Idx Name Size Address Type\n"; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1907 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1908 | StringRef Name; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1909 | error(Section.getName(Name)); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1910 | uint64_t Address = Section.getAddress(); |
| 1911 | uint64_t Size = Section.getSize(); |
| 1912 | bool Text = Section.isText(); |
| 1913 | bool Data = Section.isData(); |
| 1914 | bool BSS = Section.isBSS(); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1915 | std::string Type = (std::string(Text ? "TEXT " : "") + |
Michael J. Spencer | 8f67d47 | 2011-10-13 20:37:20 +0000 | [diff] [blame] | 1916 | (Data ? "DATA " : "") + (BSS ? "BSS" : "")); |
George Rimar | e35e644 | 2018-07-18 08:34:35 +0000 | [diff] [blame] | 1917 | outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", |
George Rimar | c1090da | 2018-07-18 09:25:36 +0000 | [diff] [blame] | 1918 | (unsigned)Section.getIndex(), Name.str().c_str(), Size, |
| 1919 | Address, Type.c_str()); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1920 | } |
Xing GUO | 785edea | 2018-11-17 08:12:48 +0000 | [diff] [blame] | 1921 | outs() << "\n"; |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1924 | void llvm::printSectionContents(const ObjectFile *Obj) { |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1925 | std::error_code EC; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1926 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1927 | StringRef Name; |
| 1928 | StringRef Contents; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1929 | error(Section.getName(Name)); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1930 | uint64_t BaseAddr = Section.getAddress(); |
David Majnemer | 185b5b1 | 2014-11-11 09:58:25 +0000 | [diff] [blame] | 1931 | uint64_t Size = Section.getSize(); |
| 1932 | if (!Size) |
| 1933 | continue; |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1934 | |
| 1935 | outs() << "Contents of section " << Name << ":\n"; |
David Majnemer | 185b5b1 | 2014-11-11 09:58:25 +0000 | [diff] [blame] | 1936 | if (Section.isBSS()) { |
Alexey Samsonov | 209095c | 2013-04-16 10:53:11 +0000 | [diff] [blame] | 1937 | outs() << format("<skipping contents of bss section at [%04" PRIx64 |
David Majnemer | 8f6b04c | 2014-07-14 16:20:14 +0000 | [diff] [blame] | 1938 | ", %04" PRIx64 ")>\n", |
| 1939 | BaseAddr, BaseAddr + Size); |
Alexey Samsonov | 209095c | 2013-04-16 10:53:11 +0000 | [diff] [blame] | 1940 | continue; |
| 1941 | } |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1942 | |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1943 | error(Section.getContents(Contents)); |
David Majnemer | 8f6b04c | 2014-07-14 16:20:14 +0000 | [diff] [blame] | 1944 | |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1945 | // Dump out the content as hex and printable ascii characters. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1946 | for (std::size_t Addr = 0, End = Contents.size(); Addr < End; Addr += 16) { |
| 1947 | outs() << format(" %04" PRIx64 " ", BaseAddr + Addr); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1948 | // Dump line of hex. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1949 | for (std::size_t I = 0; I < 16; ++I) { |
| 1950 | if (I != 0 && I % 4 == 0) |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1951 | outs() << ' '; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1952 | if (Addr + I < End) |
| 1953 | outs() << hexdigit((Contents[Addr + I] >> 4) & 0xF, true) |
| 1954 | << hexdigit(Contents[Addr + I] & 0xF, true); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1955 | else |
| 1956 | outs() << " "; |
| 1957 | } |
| 1958 | // Print ascii. |
| 1959 | outs() << " "; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1960 | for (std::size_t I = 0; I < 16 && Addr + I < End; ++I) { |
| 1961 | if (isPrint(static_cast<unsigned char>(Contents[Addr + I]) & 0xFF)) |
| 1962 | outs() << Contents[Addr + I]; |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1963 | else |
| 1964 | outs() << "."; |
| 1965 | } |
| 1966 | outs() << "\n"; |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1971 | void llvm::printSymbolTable(const ObjectFile *O, StringRef ArchiveName, |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 1972 | StringRef ArchitectureName) { |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1973 | outs() << "SYMBOL TABLE:\n"; |
| 1974 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1975 | if (const COFFObjectFile *Coff = dyn_cast<const COFFObjectFile>(O)) { |
| 1976 | printCOFFSymbolTable(Coff); |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1977 | return; |
| 1978 | } |
George Rimar | 8e0a70b | 2019-01-10 16:24:10 +0000 | [diff] [blame] | 1979 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1980 | for (auto I = O->symbol_begin(), E = O->symbol_end(); I != E; ++I) { |
George Rimar | 8e0a70b | 2019-01-10 16:24:10 +0000 | [diff] [blame] | 1981 | // Skip printing the special zero symbol when dumping an ELF file. |
| 1982 | // This makes the output consistent with the GNU objdump. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1983 | if (I == O->symbol_begin() && isa<ELFObjectFileBase>(O)) |
George Rimar | 8e0a70b | 2019-01-10 16:24:10 +0000 | [diff] [blame] | 1984 | continue; |
| 1985 | |
| 1986 | const SymbolRef &Symbol = *I; |
Kevin Enderby | 931cb65 | 2016-06-24 18:24:42 +0000 | [diff] [blame] | 1987 | Expected<uint64_t> AddressOrError = Symbol.getAddress(); |
| 1988 | if (!AddressOrError) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1989 | report_error(ArchiveName, O->getFileName(), AddressOrError.takeError(), |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1990 | ArchitectureName); |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 1991 | uint64_t Address = *AddressOrError; |
Hemant Kulkarni | aecf9d0 | 2016-09-12 17:08:22 +0000 | [diff] [blame] | 1992 | if ((Address < StartAddress) || (Address > StopAddress)) |
| 1993 | continue; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1994 | Expected<SymbolRef::Type> TypeOrError = Symbol.getType(); |
| 1995 | if (!TypeOrError) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 1996 | report_error(ArchiveName, O->getFileName(), TypeOrError.takeError(), |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 1997 | ArchitectureName); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1998 | SymbolRef::Type Type = *TypeOrError; |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1999 | uint32_t Flags = Symbol.getFlags(); |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 2000 | Expected<section_iterator> SectionOrErr = Symbol.getSection(); |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 2001 | if (!SectionOrErr) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2002 | report_error(ArchiveName, O->getFileName(), SectionOrErr.takeError(), |
Kevin Enderby | 7fa40c9 | 2016-11-16 22:17:38 +0000 | [diff] [blame] | 2003 | ArchitectureName); |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 2004 | section_iterator Section = *SectionOrErr; |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 2005 | StringRef Name; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2006 | if (Type == SymbolRef::ST_Debug && Section != O->section_end()) { |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 2007 | Section->getName(Name); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 2008 | } else { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 2009 | Expected<StringRef> NameOrErr = Symbol.getName(); |
| 2010 | if (!NameOrErr) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2011 | report_error(ArchiveName, O->getFileName(), NameOrErr.takeError(), |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 2012 | ArchitectureName); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 2013 | Name = *NameOrErr; |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 2014 | } |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2015 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2016 | bool Global = Flags & SymbolRef::SF_Global; |
| 2017 | bool Weak = Flags & SymbolRef::SF_Weak; |
| 2018 | bool Absolute = Flags & SymbolRef::SF_Absolute; |
Colin LeMahieu | bc2f47a | 2015-01-23 20:06:24 +0000 | [diff] [blame] | 2019 | bool Common = Flags & SymbolRef::SF_Common; |
Davide Italiano | cd2514d | 2015-04-30 23:08:53 +0000 | [diff] [blame] | 2020 | bool Hidden = Flags & SymbolRef::SF_Hidden; |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 2021 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2022 | char GlobLoc = ' '; |
| 2023 | if (Type != SymbolRef::ST_Unknown) |
| 2024 | GlobLoc = Global ? 'g' : 'l'; |
| 2025 | char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) |
| 2026 | ? 'd' : ' '; |
| 2027 | char FileFunc = ' '; |
| 2028 | if (Type == SymbolRef::ST_File) |
| 2029 | FileFunc = 'f'; |
| 2030 | else if (Type == SymbolRef::ST_Function) |
| 2031 | FileFunc = 'F'; |
Kristina Brooks | 0674f9d | 2018-11-11 17:47:13 +0000 | [diff] [blame] | 2032 | else if (Type == SymbolRef::ST_Data) |
| 2033 | FileFunc = 'O'; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2034 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2035 | const char *Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2036 | "%08" PRIx64; |
Michael J. Spencer | d857c1c | 2013-01-10 22:40:50 +0000 | [diff] [blame] | 2037 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2038 | outs() << format(Fmt, Address) << " " |
| 2039 | << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' |
| 2040 | << (Weak ? 'w' : ' ') // Weak? |
| 2041 | << ' ' // Constructor. Not supported yet. |
| 2042 | << ' ' // Warning. Not supported yet. |
| 2043 | << ' ' // Indirect reference to another symbol. |
| 2044 | << Debug // Debugging (d) or dynamic (D) symbol. |
| 2045 | << FileFunc // Name of function (F), file (f) or object (O). |
| 2046 | << ' '; |
| 2047 | if (Absolute) { |
| 2048 | outs() << "*ABS*"; |
Colin LeMahieu | bc2f47a | 2015-01-23 20:06:24 +0000 | [diff] [blame] | 2049 | } else if (Common) { |
| 2050 | outs() << "*COM*"; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2051 | } else if (Section == O->section_end()) { |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2052 | outs() << "*UND*"; |
| 2053 | } else { |
| 2054 | if (const MachOObjectFile *MachO = |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2055 | dyn_cast<const MachOObjectFile>(O)) { |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2056 | DataRefImpl DR = Section->getRawDataRefImpl(); |
| 2057 | StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); |
| 2058 | outs() << SegmentName << ","; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2059 | } |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2060 | StringRef SectionName; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 2061 | error(Section->getName(SectionName)); |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 2062 | outs() << SectionName; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2063 | } |
Rafael Espindola | 5f7ade2 | 2015-06-23 15:45:38 +0000 | [diff] [blame] | 2064 | |
| 2065 | outs() << '\t'; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2066 | if (Common || isa<ELFObjectFileBase>(O)) { |
Rafael Espindola | dbb6bd3 | 2015-06-25 22:10:04 +0000 | [diff] [blame] | 2067 | uint64_t Val = |
| 2068 | Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize(); |
Rafael Espindola | ae3ac08 | 2015-06-23 18:34:25 +0000 | [diff] [blame] | 2069 | outs() << format("\t %08" PRIx64 " ", Val); |
| 2070 | } |
Rafael Espindola | 5f7ade2 | 2015-06-23 15:45:38 +0000 | [diff] [blame] | 2071 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2072 | if (Hidden) |
Davide Italiano | cd2514d | 2015-04-30 23:08:53 +0000 | [diff] [blame] | 2073 | outs() << ".hidden "; |
George Rimar | 6622d41 | 2018-12-19 10:21:45 +0000 | [diff] [blame] | 2074 | |
| 2075 | if (Demangle) |
| 2076 | outs() << demangle(Name) << '\n'; |
| 2077 | else |
| 2078 | outs() << Name << '\n'; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2079 | } |
| 2080 | } |
| 2081 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2082 | static void printUnwindInfo(const ObjectFile *O) { |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 2083 | outs() << "Unwind info:\n\n"; |
| 2084 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2085 | if (const COFFObjectFile *Coff = dyn_cast<COFFObjectFile>(O)) |
| 2086 | printCOFFUnwindInfo(Coff); |
| 2087 | else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(O)) |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 2088 | printMachOUnwindInfo(MachO); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2089 | else |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 2090 | // TODO: Extract DWARF dump tool to objdump. |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2091 | WithColor::error(errs(), ToolName) |
| 2092 | << "This operation is only currently supported " |
| 2093 | "for COFF and MachO object files.\n"; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 2096 | void llvm::printExportsTrie(const ObjectFile *o) { |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2097 | outs() << "Exports trie:\n"; |
| 2098 | if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 2099 | printMachOExportsTrie(MachO); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2100 | else |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2101 | WithColor::error(errs(), ToolName) |
| 2102 | << "This operation is only currently supported " |
| 2103 | "for Mach-O executable files.\n"; |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2106 | void llvm::printRebaseTable(ObjectFile *o) { |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2107 | outs() << "Rebase table:\n"; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2108 | if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2109 | printMachORebaseTable(MachO); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2110 | else |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2111 | WithColor::error(errs(), ToolName) |
| 2112 | << "This operation is only currently supported " |
| 2113 | "for Mach-O executable files.\n"; |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2114 | } |
| 2115 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2116 | void llvm::printBindTable(ObjectFile *o) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2117 | outs() << "Bind table:\n"; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2118 | if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2119 | printMachOBindTable(MachO); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2120 | else |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2121 | WithColor::error(errs(), ToolName) |
| 2122 | << "This operation is only currently supported " |
| 2123 | "for Mach-O executable files.\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2126 | void llvm::printLazyBindTable(ObjectFile *o) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2127 | outs() << "Lazy bind table:\n"; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2128 | if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2129 | printMachOLazyBindTable(MachO); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2130 | else |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2131 | WithColor::error(errs(), ToolName) |
| 2132 | << "This operation is only currently supported " |
| 2133 | "for Mach-O executable files.\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2136 | void llvm::printWeakBindTable(ObjectFile *o) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2137 | outs() << "Weak bind table:\n"; |
Kevin Enderby | a8d256c | 2017-03-20 19:46:55 +0000 | [diff] [blame] | 2138 | if (MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2139 | printMachOWeakBindTable(MachO); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2140 | else |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2141 | WithColor::error(errs(), ToolName) |
| 2142 | << "This operation is only currently supported " |
| 2143 | "for Mach-O executable files.\n"; |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2144 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2145 | |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2146 | /// Dump the raw contents of the __clangast section so the output can be piped |
| 2147 | /// into llvm-bcanalyzer. |
| 2148 | void llvm::printRawClangAST(const ObjectFile *Obj) { |
| 2149 | if (outs().is_displayed()) { |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2150 | WithColor::error(errs(), ToolName) |
| 2151 | << "The -raw-clang-ast option will dump the raw binary contents of " |
| 2152 | "the clang ast section.\n" |
| 2153 | "Please redirect the output to a file or another program such as " |
| 2154 | "llvm-bcanalyzer.\n"; |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2155 | return; |
| 2156 | } |
| 2157 | |
| 2158 | StringRef ClangASTSectionName("__clangast"); |
| 2159 | if (isa<COFFObjectFile>(Obj)) { |
| 2160 | ClangASTSectionName = "clangast"; |
| 2161 | } |
| 2162 | |
| 2163 | Optional<object::SectionRef> ClangASTSection; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 2164 | for (auto Sec : ToolSectionFilter(*Obj)) { |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2165 | StringRef Name; |
| 2166 | Sec.getName(Name); |
| 2167 | if (Name == ClangASTSectionName) { |
| 2168 | ClangASTSection = Sec; |
| 2169 | break; |
| 2170 | } |
| 2171 | } |
| 2172 | if (!ClangASTSection) |
| 2173 | return; |
| 2174 | |
| 2175 | StringRef ClangASTContents; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 2176 | error(ClangASTSection.getValue().getContents(ClangASTContents)); |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2177 | outs().write(ClangASTContents.data(), ClangASTContents.size()); |
| 2178 | } |
| 2179 | |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 2180 | static void printFaultMaps(const ObjectFile *Obj) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2181 | StringRef FaultMapSectionName; |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 2182 | |
| 2183 | if (isa<ELFObjectFileBase>(Obj)) { |
| 2184 | FaultMapSectionName = ".llvm_faultmaps"; |
| 2185 | } else if (isa<MachOObjectFile>(Obj)) { |
| 2186 | FaultMapSectionName = "__llvm_faultmaps"; |
| 2187 | } else { |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2188 | WithColor::error(errs(), ToolName) |
| 2189 | << "This operation is only currently supported " |
| 2190 | "for ELF and Mach-O executable files.\n"; |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 2191 | return; |
| 2192 | } |
| 2193 | |
| 2194 | Optional<object::SectionRef> FaultMapSection; |
| 2195 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 2196 | for (auto Sec : ToolSectionFilter(*Obj)) { |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 2197 | StringRef Name; |
| 2198 | Sec.getName(Name); |
| 2199 | if (Name == FaultMapSectionName) { |
| 2200 | FaultMapSection = Sec; |
| 2201 | break; |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | outs() << "FaultMap table:\n"; |
| 2206 | |
| 2207 | if (!FaultMapSection.hasValue()) { |
| 2208 | outs() << "<not found>\n"; |
| 2209 | return; |
| 2210 | } |
| 2211 | |
| 2212 | StringRef FaultMapContents; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 2213 | error(FaultMapSection.getValue().getContents(FaultMapContents)); |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 2214 | |
| 2215 | FaultMapParser FMP(FaultMapContents.bytes_begin(), |
| 2216 | FaultMapContents.bytes_end()); |
| 2217 | |
| 2218 | outs() << FMP; |
| 2219 | } |
| 2220 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2221 | static void printPrivateFileHeaders(const ObjectFile *O, bool OnlyFirst) { |
| 2222 | if (O->isELF()) { |
| 2223 | printELFFileHeader(O); |
| 2224 | return printELFDynamicSection(O); |
Paul Semel | 0913dcd | 2018-07-25 11:09:20 +0000 | [diff] [blame] | 2225 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2226 | if (O->isCOFF()) |
| 2227 | return printCOFFFileHeader(O); |
| 2228 | if (O->isWasm()) |
| 2229 | return printWasmFileHeader(O); |
| 2230 | if (O->isMachO()) { |
| 2231 | printMachOFileHeader(O); |
| 2232 | if (!OnlyFirst) |
| 2233 | printMachOLoadCommands(O); |
Davide Italiano | 1bdaa20 | 2016-09-18 04:39:15 +0000 | [diff] [blame] | 2234 | return; |
| 2235 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2236 | report_error(O->getFileName(), "Invalid/Unsupported object file format"); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 2237 | } |
| 2238 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2239 | static void printFileHeaders(const ObjectFile *O) { |
| 2240 | if (!O->isELF() && !O->isCOFF()) |
| 2241 | report_error(O->getFileName(), "Invalid/Unsupported object file format"); |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 2242 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2243 | Triple::ArchType AT = O->getArch(); |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 2244 | outs() << "architecture: " << Triple::getArchTypeName(AT) << "\n"; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2245 | Expected<uint64_t> StartAddrOrErr = O->getStartAddress(); |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 2246 | if (!StartAddrOrErr) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2247 | report_error(O->getFileName(), StartAddrOrErr.takeError()); |
Petar Jovanovic | 8d947ba | 2018-10-19 22:16:49 +0000 | [diff] [blame] | 2248 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2249 | StringRef Fmt = O->getBytesInAddress() > 4 ? "%016" PRIx64 : "%08" PRIx64; |
Petar Jovanovic | 8d947ba | 2018-10-19 22:16:49 +0000 | [diff] [blame] | 2250 | uint64_t Address = StartAddrOrErr.get(); |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 2251 | outs() << "start address: " |
George Rimar | 9b6fe7e | 2019-01-12 12:17:24 +0000 | [diff] [blame] | 2252 | << "0x" << format(Fmt.data(), Address) << "\n\n"; |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
Paul Semel | 0dc92f6 | 2018-07-05 14:43:29 +0000 | [diff] [blame] | 2255 | static void printArchiveChild(StringRef Filename, const Archive::Child &C) { |
| 2256 | Expected<sys::fs::perms> ModeOrErr = C.getAccessMode(); |
| 2257 | if (!ModeOrErr) { |
Jonas Devlieghere | e787efd | 2018-11-11 22:12:04 +0000 | [diff] [blame] | 2258 | WithColor::error(errs(), ToolName) << "ill-formed archive entry.\n"; |
Paul Semel | 0dc92f6 | 2018-07-05 14:43:29 +0000 | [diff] [blame] | 2259 | consumeError(ModeOrErr.takeError()); |
| 2260 | return; |
| 2261 | } |
| 2262 | sys::fs::perms Mode = ModeOrErr.get(); |
| 2263 | outs() << ((Mode & sys::fs::owner_read) ? "r" : "-"); |
| 2264 | outs() << ((Mode & sys::fs::owner_write) ? "w" : "-"); |
| 2265 | outs() << ((Mode & sys::fs::owner_exe) ? "x" : "-"); |
| 2266 | outs() << ((Mode & sys::fs::group_read) ? "r" : "-"); |
| 2267 | outs() << ((Mode & sys::fs::group_write) ? "w" : "-"); |
| 2268 | outs() << ((Mode & sys::fs::group_exe) ? "x" : "-"); |
| 2269 | outs() << ((Mode & sys::fs::others_read) ? "r" : "-"); |
| 2270 | outs() << ((Mode & sys::fs::others_write) ? "w" : "-"); |
| 2271 | outs() << ((Mode & sys::fs::others_exe) ? "x" : "-"); |
| 2272 | |
| 2273 | outs() << " "; |
| 2274 | |
| 2275 | Expected<unsigned> UIDOrErr = C.getUID(); |
| 2276 | if (!UIDOrErr) |
| 2277 | report_error(Filename, UIDOrErr.takeError()); |
| 2278 | unsigned UID = UIDOrErr.get(); |
| 2279 | outs() << format("%d/", UID); |
| 2280 | |
| 2281 | Expected<unsigned> GIDOrErr = C.getGID(); |
| 2282 | if (!GIDOrErr) |
| 2283 | report_error(Filename, GIDOrErr.takeError()); |
| 2284 | unsigned GID = GIDOrErr.get(); |
| 2285 | outs() << format("%-d ", GID); |
| 2286 | |
| 2287 | Expected<uint64_t> Size = C.getRawSize(); |
| 2288 | if (!Size) |
| 2289 | report_error(Filename, Size.takeError()); |
| 2290 | outs() << format("%6" PRId64, Size.get()) << " "; |
| 2291 | |
| 2292 | StringRef RawLastModified = C.getRawLastModified(); |
| 2293 | unsigned Seconds; |
| 2294 | if (RawLastModified.getAsInteger(10, Seconds)) |
| 2295 | outs() << "(date: \"" << RawLastModified |
| 2296 | << "\" contains non-decimal chars) "; |
| 2297 | else { |
| 2298 | // Since ctime(3) returns a 26 character string of the form: |
| 2299 | // "Sun Sep 16 01:03:52 1973\n\0" |
| 2300 | // just print 24 characters. |
| 2301 | time_t t = Seconds; |
| 2302 | outs() << format("%.24s ", ctime(&t)); |
| 2303 | } |
| 2304 | |
| 2305 | StringRef Name = ""; |
| 2306 | Expected<StringRef> NameOrErr = C.getName(); |
| 2307 | if (!NameOrErr) { |
| 2308 | consumeError(NameOrErr.takeError()); |
| 2309 | Expected<StringRef> RawNameOrErr = C.getRawName(); |
| 2310 | if (!RawNameOrErr) |
| 2311 | report_error(Filename, NameOrErr.takeError()); |
| 2312 | Name = RawNameOrErr.get(); |
| 2313 | } else { |
| 2314 | Name = NameOrErr.get(); |
| 2315 | } |
| 2316 | outs() << Name << "\n"; |
| 2317 | } |
| 2318 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2319 | static void dumpObject(ObjectFile *O, const Archive *A = nullptr, |
| 2320 | const Archive::Child *C = nullptr) { |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2321 | // Avoid other output when using a raw option. |
| 2322 | if (!RawClangAST) { |
| 2323 | outs() << '\n'; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2324 | if (A) |
| 2325 | outs() << A->getFileName() << "(" << O->getFileName() << ")"; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 2326 | else |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2327 | outs() << O->getFileName(); |
| 2328 | outs() << ":\tfile format " << O->getFileFormatName() << "\n\n"; |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2329 | } |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 2330 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2331 | StringRef ArchiveName = A ? A->getFileName() : ""; |
George Rimar | 9b6fe7e | 2019-01-12 12:17:24 +0000 | [diff] [blame] | 2332 | if (FileHeaders) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2333 | printFileHeaders(O); |
| 2334 | if (ArchiveHeaders && !MachOOpt && C) |
| 2335 | printArchiveChild(ArchiveName, *C); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2336 | if (Disassemble) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2337 | disassembleObject(O, Relocations); |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 2338 | if (Relocations && !Disassemble) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2339 | printRelocations(O); |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 2340 | if (DynamicRelocations) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2341 | printDynamicRelocations(O); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 2342 | if (SectionHeaders) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2343 | printSectionHeaders(O); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 2344 | if (SectionContents) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2345 | printSectionContents(O); |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2346 | if (SymbolTable) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2347 | printSymbolTable(O, ArchiveName); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 2348 | if (UnwindInfo) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2349 | printUnwindInfo(O); |
Davide Italiano | 1bdaa20 | 2016-09-18 04:39:15 +0000 | [diff] [blame] | 2350 | if (PrivateHeaders || FirstPrivateHeader) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2351 | printPrivateFileHeaders(O, FirstPrivateHeader); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2352 | if (ExportsTrie) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2353 | printExportsTrie(O); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2354 | if (Rebase) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2355 | printRebaseTable(O); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2356 | if (Bind) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2357 | printBindTable(O); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2358 | if (LazyBind) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2359 | printLazyBindTable(O); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2360 | if (WeakBind) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2361 | printWeakBindTable(O); |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2362 | if (RawClangAST) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2363 | printRawClangAST(O); |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 2364 | if (PrintFaultMaps) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2365 | printFaultMaps(O); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 2366 | if (DwarfDumpType != DIDT_Null) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2367 | std::unique_ptr<DIContext> DICtx = DWARFContext::create(*O); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 2368 | // Dump the complete DWARF structure. |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 2369 | DIDumpOptions DumpOpts; |
| 2370 | DumpOpts.DumpType = DwarfDumpType; |
Adrian Prantl | f4bc1f7 | 2017-06-01 18:18:23 +0000 | [diff] [blame] | 2371 | DICtx->dump(outs(), DumpOpts); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 2372 | } |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2375 | static void dumpObject(const COFFImportFile *I, const Archive *A, |
Paul Semel | 0dc92f6 | 2018-07-05 14:43:29 +0000 | [diff] [blame] | 2376 | const Archive::Child *C = nullptr) { |
Saleem Abdulrasool | c6bf547 | 2016-08-18 16:39:19 +0000 | [diff] [blame] | 2377 | StringRef ArchiveName = A ? A->getFileName() : ""; |
| 2378 | |
| 2379 | // Avoid other output when using a raw option. |
| 2380 | if (!RawClangAST) |
| 2381 | outs() << '\n' |
| 2382 | << ArchiveName << "(" << I->getFileName() << ")" |
| 2383 | << ":\tfile format COFF-import-file" |
| 2384 | << "\n\n"; |
| 2385 | |
James Henderson | c1608c9 | 2018-10-29 14:17:08 +0000 | [diff] [blame] | 2386 | if (ArchiveHeaders && !MachOOpt && C) |
| 2387 | printArchiveChild(ArchiveName, *C); |
Saleem Abdulrasool | c6bf547 | 2016-08-18 16:39:19 +0000 | [diff] [blame] | 2388 | if (SymbolTable) |
| 2389 | printCOFFSymbolTable(I); |
| 2390 | } |
| 2391 | |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2392 | /// Dump each object file in \a a; |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2393 | static void dumpArchive(const Archive *A) { |
Mehdi Amini | 41af430 | 2016-11-11 04:28:40 +0000 | [diff] [blame] | 2394 | Error Err = Error::success(); |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2395 | for (auto &C : A->children(Err)) { |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 2396 | Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); |
| 2397 | if (!ChildOrErr) { |
| 2398 | if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2399 | report_error(A->getFileName(), C, std::move(E)); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 2400 | continue; |
| 2401 | } |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2402 | if (ObjectFile *O = dyn_cast<ObjectFile>(&*ChildOrErr.get())) |
| 2403 | dumpObject(O, A, &C); |
Saleem Abdulrasool | c6bf547 | 2016-08-18 16:39:19 +0000 | [diff] [blame] | 2404 | else if (COFFImportFile *I = dyn_cast<COFFImportFile>(&*ChildOrErr.get())) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2405 | dumpObject(I, A, &C); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2406 | else |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2407 | report_error(A->getFileName(), object_error::invalid_file_type); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2408 | } |
Lang Hames | fc20962 | 2016-07-14 02:24:01 +0000 | [diff] [blame] | 2409 | if (Err) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2410 | report_error(A->getFileName(), std::move(Err)); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Adrian Prantl | 4dfcc4a | 2018-05-01 16:10:38 +0000 | [diff] [blame] | 2413 | /// Open file and figure out how to dump it. |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2414 | static void dumpInput(StringRef file) { |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 2415 | // If we are using the Mach-O specific object file parser, then let it parse |
| 2416 | // the file and process the command line options. So the -arch flags can |
| 2417 | // be used to select specific slices, etc. |
| 2418 | if (MachOOpt) { |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2419 | parseInputMachO(file); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2420 | return; |
| 2421 | } |
| 2422 | |
| 2423 | // Attempt to open the binary. |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 2424 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file); |
| 2425 | if (!BinaryOrErr) |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 2426 | report_error(file, BinaryOrErr.takeError()); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 2427 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2428 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2429 | if (Archive *A = dyn_cast<Archive>(&Binary)) |
| 2430 | dumpArchive(A); |
| 2431 | else if (ObjectFile *O = dyn_cast<ObjectFile>(&Binary)) |
| 2432 | dumpObject(O); |
Dave Lee | 3fb120f | 2018-08-03 00:06:38 +0000 | [diff] [blame] | 2433 | else if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(&Binary)) |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2434 | parseInputMachO(UB); |
Jim Grosbach | af9aec0 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 2435 | else |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 2436 | report_error(file, object_error::invalid_file_type); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2439 | int main(int argc, char **argv) { |
Rui Ueyama | 197194b | 2018-04-13 18:26:06 +0000 | [diff] [blame] | 2440 | InitLLVM X(argc, argv); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2441 | |
| 2442 | // Initialize targets and assembly printers/parsers. |
| 2443 | llvm::InitializeAllTargetInfos(); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 2444 | llvm::InitializeAllTargetMCs(); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2445 | llvm::InitializeAllDisassemblers(); |
| 2446 | |
Pete Cooper | 28fb4fc | 2012-05-03 23:20:10 +0000 | [diff] [blame] | 2447 | // Register the target printer for --version. |
| 2448 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 2449 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2450 | cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2451 | |
| 2452 | ToolName = argv[0]; |
| 2453 | |
| 2454 | // Defaults to a.out if no filenames specified. |
Jordan Rupprecht | 16a0de2 | 2018-12-20 00:57:06 +0000 | [diff] [blame] | 2455 | if (InputFilenames.empty()) |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2456 | InputFilenames.push_back("a.out"); |
| 2457 | |
Fangrui Song | 8513cd4 | 2018-06-27 20:45:11 +0000 | [diff] [blame] | 2458 | if (AllHeaders) |
George Rimar | 9b6fe7e | 2019-01-12 12:17:24 +0000 | [diff] [blame] | 2459 | FileHeaders = PrivateHeaders = Relocations = SectionHeaders = SymbolTable = |
| 2460 | true; |
Fangrui Song | 8513cd4 | 2018-06-27 20:45:11 +0000 | [diff] [blame] | 2461 | |
Hemant Kulkarni | 8dfc0b5 | 2016-08-15 19:49:24 +0000 | [diff] [blame] | 2462 | if (DisassembleAll || PrintSource || PrintLines) |
Colin LeMahieu | f34933e | 2015-07-23 20:58:49 +0000 | [diff] [blame] | 2463 | Disassemble = true; |
Paul Semel | 007dedb | 2018-07-18 16:39:21 +0000 | [diff] [blame] | 2464 | |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2465 | if (!Disassemble |
| 2466 | && !Relocations |
Paul Semel | cb0f043 | 2018-06-07 13:30:55 +0000 | [diff] [blame] | 2467 | && !DynamicRelocations |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 2468 | && !SectionHeaders |
| 2469 | && !SectionContents |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 2470 | && !SymbolTable |
Michael J. Spencer | 209565db | 2013-01-06 03:56:49 +0000 | [diff] [blame] | 2471 | && !UnwindInfo |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 2472 | && !PrivateHeaders |
Paul Semel | d2af4d6 | 2018-07-04 15:25:03 +0000 | [diff] [blame] | 2473 | && !FileHeaders |
Kevin Enderby | 0ae163f | 2016-01-13 00:25:36 +0000 | [diff] [blame] | 2474 | && !FirstPrivateHeader |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 2475 | && !ExportsTrie |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 2476 | && !Rebase |
| 2477 | && !Bind |
| 2478 | && !LazyBind |
Kevin Enderby | 131d177 | 2015-01-09 19:22:37 +0000 | [diff] [blame] | 2479 | && !WeakBind |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 2480 | && !RawClangAST |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 2481 | && !(UniversalHeaders && MachOOpt) |
Paul Semel | 0dc92f6 | 2018-07-05 14:43:29 +0000 | [diff] [blame] | 2482 | && !ArchiveHeaders |
Kevin Enderby | 69fe98d | 2015-01-23 18:52:17 +0000 | [diff] [blame] | 2483 | && !(IndirectSymbols && MachOOpt) |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 2484 | && !(DataInCode && MachOOpt) |
Kevin Enderby | f6d2585 | 2015-01-31 00:37:11 +0000 | [diff] [blame] | 2485 | && !(LinkOptHints && MachOOpt) |
Kevin Enderby | cd66be5 | 2015-03-11 22:06:32 +0000 | [diff] [blame] | 2486 | && !(InfoPlist && MachOOpt) |
Kevin Enderby | bc847fa | 2015-03-16 20:08:09 +0000 | [diff] [blame] | 2487 | && !(DylibsUsed && MachOOpt) |
| 2488 | && !(DylibId && MachOOpt) |
Kevin Enderby | 0fc1182 | 2015-04-01 20:57:01 +0000 | [diff] [blame] | 2489 | && !(ObjcMetaData && MachOOpt) |
Jordan Rupprecht | 16a0de2 | 2018-12-20 00:57:06 +0000 | [diff] [blame] | 2490 | && !(!FilterSections.empty() && MachOOpt) |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 2491 | && !PrintFaultMaps |
| 2492 | && DwarfDumpType == DIDT_Null) { |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 2493 | cl::PrintHelpMessage(); |
Dimitry Andric | e4f5d01 | 2017-12-18 19:46:56 +0000 | [diff] [blame] | 2494 | return 2; |
| 2495 | } |
| 2496 | |
Rafael Auler | b0e4b91 | 2018-03-09 19:13:44 +0000 | [diff] [blame] | 2497 | DisasmFuncsSet.insert(DisassembleFunctions.begin(), |
| 2498 | DisassembleFunctions.end()); |
| 2499 | |
George Rimar | 73a2723 | 2019-01-15 09:19:18 +0000 | [diff] [blame] | 2500 | llvm::for_each(InputFilenames, dumpInput); |
Dimitry Andric | e4f5d01 | 2017-12-18 19:46:56 +0000 | [diff] [blame] | 2501 | |
| 2502 | return EXIT_SUCCESS; |
| 2503 | } |