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" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Triple.h" |
Sanjoy Das | 3f1bc3b | 2015-06-23 20:09:03 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/FaultMaps.h" |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmInfo.h" |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCContext.h" |
Benjamin Kramer | f57c197 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCDisassembler/MCDisassembler.h" |
| 29 | #include "llvm/MC/MCDisassembler/MCRelocationInfo.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCInst.h" |
| 31 | #include "llvm/MC/MCInstPrinter.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCInstrAnalysis.h" |
Craig Topper | 54bfde7 | 2012-04-02 06:09:36 +0000 | [diff] [blame] | 33 | #include "llvm/MC/MCInstrInfo.h" |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 34 | #include "llvm/MC/MCObjectFileInfo.h" |
Jim Grosbach | fd93a59 | 2012-03-05 19:33:20 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCRegisterInfo.h" |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 36 | #include "llvm/MC/MCSubtargetInfo.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 37 | #include "llvm/Object/Archive.h" |
| 38 | #include "llvm/Object/COFF.h" |
Benjamin Kramer | f57c197 | 2016-01-26 16:44:37 +0000 | [diff] [blame] | 39 | #include "llvm/Object/ELFObjectFile.h" |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 40 | #include "llvm/Object/MachO.h" |
Chandler Carruth | 4d88a1c | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 41 | #include "llvm/Object/ObjectFile.h" |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Casting.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
| 44 | #include "llvm/Support/Debug.h" |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 45 | #include "llvm/Support/Errc.h" |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 46 | #include "llvm/Support/FileSystem.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Format.h" |
Benjamin Kramer | bf11531 | 2011-07-25 23:04:36 +0000 | [diff] [blame] | 48 | #include "llvm/Support/GraphWriter.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Host.h" |
| 50 | #include "llvm/Support/ManagedStatic.h" |
| 51 | #include "llvm/Support/MemoryBuffer.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 52 | #include "llvm/Support/PrettyStackTrace.h" |
| 53 | #include "llvm/Support/Signals.h" |
| 54 | #include "llvm/Support/SourceMgr.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 55 | #include "llvm/Support/TargetRegistry.h" |
| 56 | #include "llvm/Support/TargetSelect.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 58 | #include <algorithm> |
Benjamin Kramer | a5177e6 | 2012-03-23 11:49:32 +0000 | [diff] [blame] | 59 | #include <cctype> |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 60 | #include <cstring> |
Rafael Espindola | a6e9c3e | 2014-06-12 17:38:55 +0000 | [diff] [blame] | 61 | #include <system_error> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 62 | #include <utility> |
Ahmed Bougacha | 1792647 | 2013-08-21 07:29:02 +0000 | [diff] [blame] | 63 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 64 | using namespace llvm; |
| 65 | using namespace object; |
| 66 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 67 | static cl::list<std::string> |
| 68 | InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 69 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 70 | cl::opt<bool> |
| 71 | llvm::Disassemble("disassemble", |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 72 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 73 | static cl::alias |
| 74 | Disassembled("d", cl::desc("Alias for --disassemble"), |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 75 | cl::aliasopt(Disassemble)); |
| 76 | |
| 77 | cl::opt<bool> |
| 78 | llvm::DisassembleAll("disassemble-all", |
| 79 | cl::desc("Display assembler mnemonics for the machine instructions")); |
| 80 | static cl::alias |
| 81 | DisassembleAlld("D", cl::desc("Alias for --disassemble-all"), |
Colin LeMahieu | f34933e | 2015-07-23 20:58:49 +0000 | [diff] [blame] | 82 | cl::aliasopt(DisassembleAll)); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 83 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 84 | cl::opt<bool> |
| 85 | llvm::Relocations("r", cl::desc("Display the relocation entries in the file")); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 86 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 87 | cl::opt<bool> |
| 88 | llvm::SectionContents("s", cl::desc("Display the content of each section")); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 89 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 90 | cl::opt<bool> |
| 91 | llvm::SymbolTable("t", cl::desc("Display the symbol table")); |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 92 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 93 | cl::opt<bool> |
| 94 | llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols")); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 95 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 96 | cl::opt<bool> |
| 97 | llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info")); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 98 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 99 | cl::opt<bool> |
| 100 | llvm::Bind("bind", cl::desc("Display mach-o binding info")); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 101 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 102 | cl::opt<bool> |
| 103 | llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info")); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 104 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 105 | cl::opt<bool> |
| 106 | llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info")); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 107 | |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 108 | cl::opt<bool> |
| 109 | llvm::RawClangAST("raw-clang-ast", |
| 110 | cl::desc("Dump the raw binary contents of the clang AST section")); |
| 111 | |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 112 | static cl::opt<bool> |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 113 | MachOOpt("macho", cl::desc("Use MachO specific object file parser")); |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 114 | static cl::alias |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 115 | MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachOOpt)); |
Benjamin Kramer | 87ee76c | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 116 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 117 | cl::opt<std::string> |
| 118 | llvm::TripleName("triple", cl::desc("Target triple to disassemble for, " |
| 119 | "see -version for available targets")); |
| 120 | |
| 121 | cl::opt<std::string> |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 122 | llvm::MCPU("mcpu", |
| 123 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 124 | cl::value_desc("cpu-name"), |
| 125 | cl::init("")); |
| 126 | |
| 127 | cl::opt<std::string> |
Kevin Enderby | ef3ad2f | 2014-12-04 23:56:27 +0000 | [diff] [blame] | 128 | llvm::ArchName("arch-name", cl::desc("Target arch to disassemble for, " |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 129 | "see -version for available targets")); |
| 130 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 131 | cl::opt<bool> |
| 132 | llvm::SectionHeaders("section-headers", cl::desc("Display summaries of the " |
| 133 | "headers for each section.")); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 134 | static cl::alias |
| 135 | SectionHeadersShort("headers", cl::desc("Alias for --section-headers"), |
| 136 | cl::aliasopt(SectionHeaders)); |
| 137 | static cl::alias |
| 138 | SectionHeadersShorter("h", cl::desc("Alias for --section-headers"), |
| 139 | cl::aliasopt(SectionHeaders)); |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 140 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 141 | cl::list<std::string> |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 142 | llvm::FilterSections("section", cl::desc("Operate on the specified sections only. " |
| 143 | "With -macho dump segment,section")); |
| 144 | cl::alias |
| 145 | static FilterSectionsj("j", cl::desc("Alias for --section"), |
| 146 | cl::aliasopt(llvm::FilterSections)); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 147 | |
Kevin Enderby | c959562 | 2014-08-06 23:24:41 +0000 | [diff] [blame] | 148 | cl::list<std::string> |
| 149 | llvm::MAttrs("mattr", |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 150 | cl::CommaSeparated, |
| 151 | cl::desc("Target specific attributes"), |
| 152 | cl::value_desc("a1,+a2,-a3,...")); |
| 153 | |
Kevin Enderby | bf246f5 | 2014-09-24 23:08:22 +0000 | [diff] [blame] | 154 | cl::opt<bool> |
| 155 | llvm::NoShowRawInsn("no-show-raw-insn", cl::desc("When disassembling " |
| 156 | "instructions, do not print " |
| 157 | "the instruction bytes.")); |
Eli Bendersky | 3a6808c | 2012-11-20 22:57:02 +0000 | [diff] [blame] | 158 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 159 | cl::opt<bool> |
| 160 | llvm::UnwindInfo("unwind-info", cl::desc("Display unwind information")); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 161 | |
| 162 | static cl::alias |
| 163 | UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), |
| 164 | cl::aliasopt(UnwindInfo)); |
| 165 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 166 | cl::opt<bool> |
| 167 | llvm::PrivateHeaders("private-headers", |
| 168 | cl::desc("Display format specific file headers")); |
Michael J. Spencer | 209565db | 2013-01-06 03:56:49 +0000 | [diff] [blame] | 169 | |
Kevin Enderby | 0ae163f | 2016-01-13 00:25:36 +0000 | [diff] [blame] | 170 | cl::opt<bool> |
| 171 | llvm::FirstPrivateHeader("private-header", |
| 172 | cl::desc("Display only the first format specific file " |
| 173 | "header")); |
| 174 | |
Michael J. Spencer | 209565db | 2013-01-06 03:56:49 +0000 | [diff] [blame] | 175 | static cl::alias |
| 176 | PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), |
| 177 | cl::aliasopt(PrivateHeaders)); |
| 178 | |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 179 | cl::opt<bool> |
| 180 | llvm::PrintImmHex("print-imm-hex", |
Colin LeMahieu | efe3732 | 2016-04-08 18:15:37 +0000 | [diff] [blame] | 181 | cl::desc("Use hex format for immediate values")); |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 182 | |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 183 | cl::opt<bool> PrintFaultMaps("fault-map-section", |
| 184 | cl::desc("Display contents of faultmap section")); |
| 185 | |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 186 | cl::opt<DIDumpType> llvm::DwarfDumpType( |
| 187 | "dwarf", cl::init(DIDT_Null), cl::desc("Dump of dwarf debug sections:"), |
| 188 | cl::values(clEnumValN(DIDT_Frames, "frames", ".debug_frame"), |
| 189 | clEnumValEnd)); |
| 190 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 191 | static StringRef ToolName; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 192 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 193 | namespace { |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 194 | typedef std::function<bool(llvm::object::SectionRef const &)> FilterPredicate; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 195 | |
| 196 | class SectionFilterIterator { |
| 197 | public: |
| 198 | SectionFilterIterator(FilterPredicate P, |
| 199 | llvm::object::section_iterator const &I, |
| 200 | llvm::object::section_iterator const &E) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 201 | : Predicate(std::move(P)), Iterator(I), End(E) { |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 202 | ScanPredicate(); |
| 203 | } |
Benjamin Kramer | ac9257b | 2015-09-24 14:52:52 +0000 | [diff] [blame] | 204 | const llvm::object::SectionRef &operator*() const { return *Iterator; } |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 205 | SectionFilterIterator &operator++() { |
| 206 | ++Iterator; |
| 207 | ScanPredicate(); |
| 208 | return *this; |
| 209 | } |
| 210 | bool operator!=(SectionFilterIterator const &Other) const { |
| 211 | return Iterator != Other.Iterator; |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | void ScanPredicate() { |
Colin LeMahieu | da1723f | 2015-07-29 19:21:13 +0000 | [diff] [blame] | 216 | while (Iterator != End && !Predicate(*Iterator)) { |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 217 | ++Iterator; |
| 218 | } |
| 219 | } |
| 220 | FilterPredicate Predicate; |
| 221 | llvm::object::section_iterator Iterator; |
| 222 | llvm::object::section_iterator End; |
| 223 | }; |
| 224 | |
| 225 | class SectionFilter { |
| 226 | public: |
| 227 | SectionFilter(FilterPredicate P, llvm::object::ObjectFile const &O) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 228 | : Predicate(std::move(P)), Object(O) {} |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 229 | SectionFilterIterator begin() { |
| 230 | return SectionFilterIterator(Predicate, Object.section_begin(), |
| 231 | Object.section_end()); |
| 232 | } |
| 233 | SectionFilterIterator end() { |
| 234 | return SectionFilterIterator(Predicate, Object.section_end(), |
| 235 | Object.section_end()); |
| 236 | } |
| 237 | |
| 238 | private: |
| 239 | FilterPredicate Predicate; |
| 240 | llvm::object::ObjectFile const &Object; |
| 241 | }; |
| 242 | SectionFilter ToolSectionFilter(llvm::object::ObjectFile const &O) { |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 243 | return SectionFilter([](llvm::object::SectionRef const &S) { |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 244 | if(FilterSections.empty()) |
Colin LeMahieu | da1723f | 2015-07-29 19:21:13 +0000 | [diff] [blame] | 245 | return true; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 246 | llvm::StringRef String; |
| 247 | std::error_code error = S.getName(String); |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 248 | if (error) |
Colin LeMahieu | da1723f | 2015-07-29 19:21:13 +0000 | [diff] [blame] | 249 | return false; |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 250 | return std::find(FilterSections.begin(), |
| 251 | FilterSections.end(), |
Colin LeMahieu | da1723f | 2015-07-29 19:21:13 +0000 | [diff] [blame] | 252 | String) != FilterSections.end(); |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 253 | }, |
| 254 | O); |
| 255 | } |
| 256 | } |
| 257 | |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 258 | void llvm::error(std::error_code EC) { |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 259 | if (!EC) |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 260 | return; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 261 | |
Davide Italiano | 140af64 | 2015-12-25 18:16:45 +0000 | [diff] [blame] | 262 | errs() << ToolName << ": error reading file: " << EC.message() << ".\n"; |
| 263 | errs().flush(); |
Davide Italiano | 7f6c301 | 2015-08-06 00:18:52 +0000 | [diff] [blame] | 264 | exit(1); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Davide Italiano | ed9d95b | 2015-12-29 13:41:02 +0000 | [diff] [blame] | 267 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, |
| 268 | std::error_code EC) { |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 269 | assert(EC); |
| 270 | errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n"; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 271 | exit(1); |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 274 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File, |
| 275 | llvm::Error E) { |
| 276 | assert(E); |
| 277 | std::string Buf; |
| 278 | raw_string_ostream OS(Buf); |
| 279 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 280 | OS.flush(); |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 281 | errs() << ToolName << ": '" << File << "': " << Buf; |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 282 | exit(1); |
| 283 | } |
| 284 | |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 285 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, |
| 286 | StringRef FileName, |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 287 | llvm::Error E, |
| 288 | StringRef ArchitectureName) { |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 289 | assert(E); |
| 290 | errs() << ToolName << ": "; |
| 291 | if (ArchiveName != "") |
| 292 | errs() << ArchiveName << "(" << FileName << ")"; |
| 293 | else |
| 294 | errs() << FileName; |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 295 | if (!ArchitectureName.empty()) |
| 296 | errs() << " (for architecture " << ArchitectureName << ")"; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 297 | std::string Buf; |
| 298 | raw_string_ostream OS(Buf); |
| 299 | logAllUnhandledErrors(std::move(E), OS, ""); |
| 300 | OS.flush(); |
| 301 | errs() << " " << Buf; |
| 302 | exit(1); |
| 303 | } |
| 304 | |
| 305 | LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef ArchiveName, |
| 306 | const object::Archive::Child &C, |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 307 | llvm::Error E, |
| 308 | StringRef ArchitectureName) { |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 309 | ErrorOr<StringRef> NameOrErr = C.getName(); |
| 310 | // TODO: if we have a error getting the name then it would be nice to print |
| 311 | // the index of which archive member this is and or its offset in the |
| 312 | // archive instead of "???" as the name. |
| 313 | if (NameOrErr.getError()) |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 314 | llvm::report_error(ArchiveName, "???", std::move(E), ArchitectureName); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 315 | else |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 316 | llvm::report_error(ArchiveName, NameOrErr.get(), std::move(E), |
| 317 | ArchitectureName); |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Craig Topper | e6cb63e | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 320 | static const Target *getTarget(const ObjectFile *Obj = nullptr) { |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 321 | // Figure out the target triple. |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 322 | llvm::Triple TheTriple("unknown-unknown-unknown"); |
Michael J. Spencer | 05350e6d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 323 | if (TripleName.empty()) { |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 324 | if (Obj) { |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 325 | TheTriple.setArch(Triple::ArchType(Obj->getArch())); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 326 | // TheTriple defaults to ELF, and COFF doesn't have an environment: |
| 327 | // the best we can do here is indicate that it is mach-o. |
| 328 | if (Obj->isMachO()) |
Saleem Abdulrasool | 3547633 | 2014-03-06 20:47:11 +0000 | [diff] [blame] | 329 | TheTriple.setObjectFormat(Triple::MachO); |
Saleem Abdulrasool | 98938f1 | 2014-04-17 06:17:23 +0000 | [diff] [blame] | 330 | |
| 331 | if (Obj->isCOFF()) { |
| 332 | const auto COFFObj = dyn_cast<COFFObjectFile>(Obj); |
| 333 | if (COFFObj->getArch() == Triple::thumb) |
| 334 | TheTriple.setTriple("thumbv7-windows"); |
| 335 | } |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 336 | } |
Michael J. Spencer | 05350e6d | 2011-01-20 07:22:04 +0000 | [diff] [blame] | 337 | } else |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 338 | TheTriple.setTriple(Triple::normalize(TripleName)); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 339 | |
| 340 | // Get the target specific parser. |
| 341 | std::string Error; |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 342 | const Target *TheTarget = TargetRegistry::lookupTarget(ArchName, TheTriple, |
| 343 | Error); |
Davide Italiano | bb599e3 | 2015-12-03 22:13:40 +0000 | [diff] [blame] | 344 | if (!TheTarget) |
| 345 | report_fatal_error("can't find target: " + Error); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 346 | |
Kevin Enderby | fe3d005 | 2012-05-08 23:38:45 +0000 | [diff] [blame] | 347 | // Update the triple name and return the found target. |
| 348 | TripleName = TheTriple.getTriple(); |
| 349 | return TheTarget; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 352 | bool llvm::RelocAddressLess(RelocationRef a, RelocationRef b) { |
Rafael Espindola | 704cd84 | 2015-07-06 15:47:43 +0000 | [diff] [blame] | 353 | return a.getOffset() < b.getOffset(); |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Colin LeMahieu | fb76b00 | 2015-05-28 19:07:14 +0000 | [diff] [blame] | 356 | namespace { |
| 357 | class PrettyPrinter { |
| 358 | public: |
Colin LeMahieu | 0b5890d | 2015-05-28 20:59:08 +0000 | [diff] [blame] | 359 | virtual ~PrettyPrinter(){} |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 360 | virtual void printInst(MCInstPrinter &IP, const MCInst *MI, |
Colin LeMahieu | fb76b00 | 2015-05-28 19:07:14 +0000 | [diff] [blame] | 361 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 362 | raw_ostream &OS, StringRef Annot, |
| 363 | MCSubtargetInfo const &STI) { |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 364 | OS << format("%8" PRIx64 ":", Address); |
Colin LeMahieu | fb76b00 | 2015-05-28 19:07:14 +0000 | [diff] [blame] | 365 | if (!NoShowRawInsn) { |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 366 | OS << "\t"; |
| 367 | dumpBytes(Bytes, OS); |
Colin LeMahieu | fb76b00 | 2015-05-28 19:07:14 +0000 | [diff] [blame] | 368 | } |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 369 | if (MI) |
| 370 | IP.printInst(MI, OS, "", STI); |
| 371 | else |
| 372 | OS << " <unknown>"; |
Colin LeMahieu | fb76b00 | 2015-05-28 19:07:14 +0000 | [diff] [blame] | 373 | } |
| 374 | }; |
| 375 | PrettyPrinter PrettyPrinterInst; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 376 | class HexagonPrettyPrinter : public PrettyPrinter { |
| 377 | public: |
| 378 | void printLead(ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 379 | raw_ostream &OS) { |
| 380 | uint32_t opcode = |
| 381 | (Bytes[3] << 24) | (Bytes[2] << 16) | (Bytes[1] << 8) | Bytes[0]; |
| 382 | OS << format("%8" PRIx64 ":", Address); |
| 383 | if (!NoShowRawInsn) { |
| 384 | OS << "\t"; |
| 385 | dumpBytes(Bytes.slice(0, 4), OS); |
| 386 | OS << format("%08" PRIx32, opcode); |
| 387 | } |
| 388 | } |
| 389 | void printInst(MCInstPrinter &IP, const MCInst *MI, |
| 390 | ArrayRef<uint8_t> Bytes, uint64_t Address, |
| 391 | raw_ostream &OS, StringRef Annot, |
| 392 | MCSubtargetInfo const &STI) override { |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 393 | if (!MI) { |
| 394 | printLead(Bytes, Address, OS); |
| 395 | OS << " <unknown>"; |
| 396 | return; |
| 397 | } |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 398 | std::string Buffer; |
| 399 | { |
| 400 | raw_string_ostream TempStream(Buffer); |
| 401 | IP.printInst(MI, TempStream, "", STI); |
| 402 | } |
| 403 | StringRef Contents(Buffer); |
| 404 | // Split off bundle attributes |
| 405 | auto PacketBundle = Contents.rsplit('\n'); |
| 406 | // Split off first instruction from the rest |
| 407 | auto HeadTail = PacketBundle.first.split('\n'); |
| 408 | auto Preamble = " { "; |
| 409 | auto Separator = ""; |
| 410 | while(!HeadTail.first.empty()) { |
| 411 | OS << Separator; |
| 412 | Separator = "\n"; |
| 413 | printLead(Bytes, Address, OS); |
| 414 | OS << Preamble; |
| 415 | Preamble = " "; |
| 416 | StringRef Inst; |
| 417 | auto Duplex = HeadTail.first.split('\v'); |
| 418 | if(!Duplex.second.empty()){ |
| 419 | OS << Duplex.first; |
| 420 | OS << "; "; |
| 421 | Inst = Duplex.second; |
| 422 | } |
| 423 | else |
| 424 | Inst = HeadTail.first; |
| 425 | OS << Inst; |
| 426 | Bytes = Bytes.slice(4); |
| 427 | Address += 4; |
| 428 | HeadTail = HeadTail.second.split('\n'); |
| 429 | } |
| 430 | OS << " } " << PacketBundle.second; |
| 431 | } |
| 432 | }; |
| 433 | HexagonPrettyPrinter HexagonPrettyPrinterInst; |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 434 | |
| 435 | class AMDGCNPrettyPrinter : public PrettyPrinter { |
| 436 | public: |
| 437 | void printInst(MCInstPrinter &IP, |
| 438 | const MCInst *MI, |
| 439 | ArrayRef<uint8_t> Bytes, |
| 440 | uint64_t Address, |
| 441 | raw_ostream &OS, |
| 442 | StringRef Annot, |
| 443 | MCSubtargetInfo const &STI) override { |
Matt Arsenault | 87d80db | 2016-04-22 21:23:41 +0000 | [diff] [blame] | 444 | if (!MI) { |
| 445 | OS << " <unknown>"; |
| 446 | return; |
| 447 | } |
| 448 | |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 449 | SmallString<40> InstStr; |
| 450 | raw_svector_ostream IS(InstStr); |
| 451 | |
| 452 | IP.printInst(MI, IS, "", STI); |
| 453 | |
Valery Pykhtin | 8e79f5b | 2016-04-07 08:38:20 +0000 | [diff] [blame] | 454 | OS << left_justify(IS.str(), 60) << format("// %012" PRIX64 ": ", Address); |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 455 | typedef support::ulittle32_t U32; |
| 456 | for (auto D : makeArrayRef(reinterpret_cast<const U32*>(Bytes.data()), |
| 457 | Bytes.size() / sizeof(U32))) |
| 458 | // D should be explicitly casted to uint32_t here as it is passed |
| 459 | // by format to snprintf as vararg. |
Valery Pykhtin | 8e79f5b | 2016-04-07 08:38:20 +0000 | [diff] [blame] | 460 | OS << format("%08" PRIX32 " ", static_cast<uint32_t>(D)); |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 461 | |
| 462 | if (!Annot.empty()) |
| 463 | OS << "// " << Annot; |
| 464 | } |
| 465 | }; |
| 466 | AMDGCNPrettyPrinter AMDGCNPrettyPrinterInst; |
| 467 | |
Colin LeMahieu | 35436a2 | 2015-05-29 14:48:25 +0000 | [diff] [blame] | 468 | PrettyPrinter &selectPrettyPrinter(Triple const &Triple) { |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 469 | switch(Triple.getArch()) { |
| 470 | default: |
| 471 | return PrettyPrinterInst; |
| 472 | case Triple::hexagon: |
| 473 | return HexagonPrettyPrinterInst; |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 474 | case Triple::amdgcn: |
| 475 | return AMDGCNPrettyPrinterInst; |
Colin LeMahieu | 68d967d | 2015-05-29 14:44:13 +0000 | [diff] [blame] | 476 | } |
Colin LeMahieu | fb76b00 | 2015-05-28 19:07:14 +0000 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 480 | template <class ELFT> |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 481 | static std::error_code getRelocationValueString(const ELFObjectFile<ELFT> *Obj, |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 482 | const RelocationRef &RelRef, |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 483 | SmallVectorImpl<char> &Result) { |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 484 | DataRefImpl Rel = RelRef.getRawDataRefImpl(); |
| 485 | |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 486 | typedef typename ELFObjectFile<ELFT>::Elf_Sym Elf_Sym; |
| 487 | typedef typename ELFObjectFile<ELFT>::Elf_Shdr Elf_Shdr; |
Rafael Espindola | 7f162ec | 2015-07-02 14:21:38 +0000 | [diff] [blame] | 488 | typedef typename ELFObjectFile<ELFT>::Elf_Rela Elf_Rela; |
| 489 | |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 490 | const ELFFile<ELFT> &EF = *Obj->getELFFile(); |
| 491 | |
Rafael Espindola | 6def304 | 2015-07-01 12:56:27 +0000 | [diff] [blame] | 492 | ErrorOr<const Elf_Shdr *> SecOrErr = EF.getSection(Rel.d.a); |
| 493 | if (std::error_code EC = SecOrErr.getError()) |
| 494 | return EC; |
| 495 | const Elf_Shdr *Sec = *SecOrErr; |
| 496 | ErrorOr<const Elf_Shdr *> SymTabOrErr = EF.getSection(Sec->sh_link); |
| 497 | if (std::error_code EC = SymTabOrErr.getError()) |
| 498 | return EC; |
| 499 | const Elf_Shdr *SymTab = *SymTabOrErr; |
Rafael Espindola | 719dc7c | 2015-06-29 12:38:31 +0000 | [diff] [blame] | 500 | assert(SymTab->sh_type == ELF::SHT_SYMTAB || |
| 501 | SymTab->sh_type == ELF::SHT_DYNSYM); |
Rafael Espindola | 6def304 | 2015-07-01 12:56:27 +0000 | [diff] [blame] | 502 | ErrorOr<const Elf_Shdr *> StrTabSec = EF.getSection(SymTab->sh_link); |
| 503 | if (std::error_code EC = StrTabSec.getError()) |
| 504 | return EC; |
| 505 | ErrorOr<StringRef> StrTabOrErr = EF.getStringTable(*StrTabSec); |
Rafael Espindola | 6a1bfb2 | 2015-06-29 14:39:25 +0000 | [diff] [blame] | 506 | if (std::error_code EC = StrTabOrErr.getError()) |
| 507 | return EC; |
| 508 | StringRef StrTab = *StrTabOrErr; |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 509 | uint8_t type = RelRef.getType(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 510 | StringRef res; |
| 511 | int64_t addend = 0; |
Rafael Espindola | 6def304 | 2015-07-01 12:56:27 +0000 | [diff] [blame] | 512 | switch (Sec->sh_type) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 513 | default: |
| 514 | return object_error::parse_failed; |
| 515 | case ELF::SHT_REL: { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 516 | // TODO: Read implicit addend from section data. |
| 517 | break; |
| 518 | } |
| 519 | case ELF::SHT_RELA: { |
Rafael Espindola | 7f162ec | 2015-07-02 14:21:38 +0000 | [diff] [blame] | 520 | const Elf_Rela *ERela = Obj->getRela(Rel); |
Rafael Espindola | 7f162ec | 2015-07-02 14:21:38 +0000 | [diff] [blame] | 521 | addend = ERela->r_addend; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | } |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 525 | symbol_iterator SI = RelRef.getSymbol(); |
| 526 | const Elf_Sym *symb = Obj->getSymbol(SI->getRawDataRefImpl()); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 527 | StringRef Target; |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 528 | if (symb->getType() == ELF::STT_SECTION) { |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 529 | Expected<section_iterator> SymSI = SI->getSection(); |
| 530 | if (!SymSI) |
| 531 | return errorToErrorCode(SymSI.takeError()); |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 532 | const Elf_Shdr *SymSec = Obj->getSection((*SymSI)->getRawDataRefImpl()); |
| 533 | ErrorOr<StringRef> SecName = EF.getSectionName(SymSec); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 534 | if (std::error_code EC = SecName.getError()) |
| 535 | return EC; |
| 536 | Target = *SecName; |
| 537 | } else { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 538 | Expected<StringRef> SymName = symb->getName(StrTab); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 539 | if (!SymName) |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 540 | return errorToErrorCode(SymName.takeError()); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 541 | Target = *SymName; |
| 542 | } |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 543 | switch (EF.getHeader()->e_machine) { |
| 544 | case ELF::EM_X86_64: |
| 545 | switch (type) { |
| 546 | case ELF::R_X86_64_PC8: |
| 547 | case ELF::R_X86_64_PC16: |
| 548 | case ELF::R_X86_64_PC32: { |
| 549 | std::string fmtbuf; |
| 550 | raw_string_ostream fmt(fmtbuf); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 551 | fmt << Target << (addend < 0 ? "" : "+") << addend << "-P"; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 552 | fmt.flush(); |
| 553 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 554 | } break; |
| 555 | case ELF::R_X86_64_8: |
| 556 | case ELF::R_X86_64_16: |
| 557 | case ELF::R_X86_64_32: |
| 558 | case ELF::R_X86_64_32S: |
| 559 | case ELF::R_X86_64_64: { |
| 560 | std::string fmtbuf; |
| 561 | raw_string_ostream fmt(fmtbuf); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 562 | fmt << Target << (addend < 0 ? "" : "+") << addend; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 563 | fmt.flush(); |
| 564 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 565 | } break; |
| 566 | default: |
| 567 | res = "Unknown"; |
| 568 | } |
| 569 | break; |
Jacques Pienaar | ea9f25a | 2016-03-01 21:21:42 +0000 | [diff] [blame] | 570 | case ELF::EM_LANAI: |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 571 | case ELF::EM_AARCH64: { |
| 572 | std::string fmtbuf; |
| 573 | raw_string_ostream fmt(fmtbuf); |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 574 | fmt << Target; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 575 | if (addend != 0) |
| 576 | fmt << (addend < 0 ? "" : "+") << addend; |
| 577 | fmt.flush(); |
| 578 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 579 | break; |
| 580 | } |
| 581 | case ELF::EM_386: |
Michael Kuperstein | a3b79dd | 2015-11-04 11:21:50 +0000 | [diff] [blame] | 582 | case ELF::EM_IAMCU: |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 583 | case ELF::EM_ARM: |
| 584 | case ELF::EM_HEXAGON: |
| 585 | case ELF::EM_MIPS: |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 586 | res = Target; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 587 | break; |
Dan Gohman | 4635017 | 2016-01-12 20:56:01 +0000 | [diff] [blame] | 588 | case ELF::EM_WEBASSEMBLY: |
| 589 | switch (type) { |
| 590 | case ELF::R_WEBASSEMBLY_DATA: { |
| 591 | std::string fmtbuf; |
| 592 | raw_string_ostream fmt(fmtbuf); |
| 593 | fmt << Target << (addend < 0 ? "" : "+") << addend; |
| 594 | fmt.flush(); |
| 595 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
| 596 | break; |
| 597 | } |
| 598 | case ELF::R_WEBASSEMBLY_FUNCTION: |
| 599 | res = Target; |
| 600 | break; |
| 601 | default: |
| 602 | res = "Unknown"; |
| 603 | } |
| 604 | break; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 605 | default: |
| 606 | res = "Unknown"; |
| 607 | } |
| 608 | if (Result.empty()) |
| 609 | Result.append(res.begin(), res.end()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 610 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | static std::error_code getRelocationValueString(const ELFObjectFileBase *Obj, |
Rafael Espindola | a01ff22 | 2015-08-10 20:50:40 +0000 | [diff] [blame] | 614 | const RelocationRef &Rel, |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 615 | SmallVectorImpl<char> &Result) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 616 | if (auto *ELF32LE = dyn_cast<ELF32LEObjectFile>(Obj)) |
| 617 | return getRelocationValueString(ELF32LE, Rel, Result); |
| 618 | if (auto *ELF64LE = dyn_cast<ELF64LEObjectFile>(Obj)) |
| 619 | return getRelocationValueString(ELF64LE, Rel, Result); |
| 620 | if (auto *ELF32BE = dyn_cast<ELF32BEObjectFile>(Obj)) |
| 621 | return getRelocationValueString(ELF32BE, Rel, Result); |
| 622 | auto *ELF64BE = cast<ELF64BEObjectFile>(Obj); |
| 623 | return getRelocationValueString(ELF64BE, Rel, Result); |
| 624 | } |
| 625 | |
| 626 | static std::error_code getRelocationValueString(const COFFObjectFile *Obj, |
| 627 | const RelocationRef &Rel, |
| 628 | SmallVectorImpl<char> &Result) { |
| 629 | symbol_iterator SymI = Rel.getSymbol(); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 630 | Expected<StringRef> SymNameOrErr = SymI->getName(); |
| 631 | if (!SymNameOrErr) |
| 632 | return errorToErrorCode(SymNameOrErr.takeError()); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 633 | StringRef SymName = *SymNameOrErr; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 634 | Result.append(SymName.begin(), SymName.end()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 635 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | static void printRelocationTargetName(const MachOObjectFile *O, |
| 639 | const MachO::any_relocation_info &RE, |
| 640 | raw_string_ostream &fmt) { |
| 641 | bool IsScattered = O->isRelocationScattered(RE); |
| 642 | |
| 643 | // Target of a scattered relocation is an address. In the interest of |
| 644 | // generating pretty output, scan through the symbol table looking for a |
| 645 | // symbol that aligns with that address. If we find one, print it. |
| 646 | // Otherwise, we just print the hex address of the target. |
| 647 | if (IsScattered) { |
| 648 | uint32_t Val = O->getPlainRelocationSymbolNum(RE); |
| 649 | |
| 650 | for (const SymbolRef &Symbol : O->symbols()) { |
| 651 | std::error_code ec; |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 652 | ErrorOr<uint64_t> Addr = Symbol.getAddress(); |
| 653 | if ((ec = Addr.getError())) |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 654 | report_fatal_error(ec.message()); |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 655 | if (*Addr != Val) |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 656 | continue; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 657 | Expected<StringRef> Name = Symbol.getName(); |
| 658 | if (!Name) { |
| 659 | std::string Buf; |
| 660 | raw_string_ostream OS(Buf); |
| 661 | logAllUnhandledErrors(Name.takeError(), OS, ""); |
| 662 | OS.flush(); |
| 663 | report_fatal_error(Buf); |
| 664 | } |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 665 | fmt << *Name; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | |
| 669 | // If we couldn't find a symbol that this relocation refers to, try |
| 670 | // to find a section beginning instead. |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 671 | for (const SectionRef &Section : ToolSectionFilter(*O)) { |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 672 | std::error_code ec; |
| 673 | |
| 674 | StringRef Name; |
| 675 | uint64_t Addr = Section.getAddress(); |
| 676 | if (Addr != Val) |
| 677 | continue; |
| 678 | if ((ec = Section.getName(Name))) |
| 679 | report_fatal_error(ec.message()); |
| 680 | fmt << Name; |
| 681 | return; |
| 682 | } |
| 683 | |
| 684 | fmt << format("0x%x", Val); |
| 685 | return; |
| 686 | } |
| 687 | |
| 688 | StringRef S; |
| 689 | bool isExtern = O->getPlainRelocationExternal(RE); |
| 690 | uint64_t Val = O->getPlainRelocationSymbolNum(RE); |
| 691 | |
| 692 | if (isExtern) { |
| 693 | symbol_iterator SI = O->symbol_begin(); |
| 694 | advance(SI, Val); |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 695 | Expected<StringRef> SOrErr = SI->getName(); |
| 696 | error(errorToErrorCode(SOrErr.takeError())); |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 697 | S = *SOrErr; |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 698 | } else { |
| 699 | section_iterator SI = O->section_begin(); |
| 700 | // Adjust for the fact that sections are 1-indexed. |
| 701 | advance(SI, Val - 1); |
| 702 | SI->getName(S); |
| 703 | } |
| 704 | |
| 705 | fmt << S; |
| 706 | } |
| 707 | |
| 708 | static std::error_code getRelocationValueString(const MachOObjectFile *Obj, |
| 709 | const RelocationRef &RelRef, |
| 710 | SmallVectorImpl<char> &Result) { |
| 711 | DataRefImpl Rel = RelRef.getRawDataRefImpl(); |
| 712 | MachO::any_relocation_info RE = Obj->getRelocation(Rel); |
| 713 | |
| 714 | unsigned Arch = Obj->getArch(); |
| 715 | |
| 716 | std::string fmtbuf; |
| 717 | raw_string_ostream fmt(fmtbuf); |
| 718 | unsigned Type = Obj->getAnyRelocationType(RE); |
| 719 | bool IsPCRel = Obj->getAnyRelocationPCRel(RE); |
| 720 | |
| 721 | // Determine any addends that should be displayed with the relocation. |
| 722 | // These require decoding the relocation type, which is triple-specific. |
| 723 | |
| 724 | // X86_64 has entirely custom relocation types. |
| 725 | if (Arch == Triple::x86_64) { |
| 726 | bool isPCRel = Obj->getAnyRelocationPCRel(RE); |
| 727 | |
| 728 | switch (Type) { |
| 729 | case MachO::X86_64_RELOC_GOT_LOAD: |
| 730 | case MachO::X86_64_RELOC_GOT: { |
| 731 | printRelocationTargetName(Obj, RE, fmt); |
| 732 | fmt << "@GOT"; |
| 733 | if (isPCRel) |
| 734 | fmt << "PCREL"; |
| 735 | break; |
| 736 | } |
| 737 | case MachO::X86_64_RELOC_SUBTRACTOR: { |
| 738 | DataRefImpl RelNext = Rel; |
| 739 | Obj->moveRelocationNext(RelNext); |
| 740 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 741 | |
| 742 | // X86_64_RELOC_SUBTRACTOR must be followed by a relocation of type |
| 743 | // X86_64_RELOC_UNSIGNED. |
| 744 | // NOTE: Scattered relocations don't exist on x86_64. |
| 745 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 746 | if (RType != MachO::X86_64_RELOC_UNSIGNED) |
| 747 | report_fatal_error("Expected X86_64_RELOC_UNSIGNED after " |
| 748 | "X86_64_RELOC_SUBTRACTOR."); |
| 749 | |
| 750 | // The X86_64_RELOC_UNSIGNED contains the minuend symbol; |
| 751 | // X86_64_RELOC_SUBTRACTOR contains the subtrahend. |
| 752 | printRelocationTargetName(Obj, RENext, fmt); |
| 753 | fmt << "-"; |
| 754 | printRelocationTargetName(Obj, RE, fmt); |
| 755 | break; |
| 756 | } |
| 757 | case MachO::X86_64_RELOC_TLV: |
| 758 | printRelocationTargetName(Obj, RE, fmt); |
| 759 | fmt << "@TLV"; |
| 760 | if (isPCRel) |
| 761 | fmt << "P"; |
| 762 | break; |
| 763 | case MachO::X86_64_RELOC_SIGNED_1: |
| 764 | printRelocationTargetName(Obj, RE, fmt); |
| 765 | fmt << "-1"; |
| 766 | break; |
| 767 | case MachO::X86_64_RELOC_SIGNED_2: |
| 768 | printRelocationTargetName(Obj, RE, fmt); |
| 769 | fmt << "-2"; |
| 770 | break; |
| 771 | case MachO::X86_64_RELOC_SIGNED_4: |
| 772 | printRelocationTargetName(Obj, RE, fmt); |
| 773 | fmt << "-4"; |
| 774 | break; |
| 775 | default: |
| 776 | printRelocationTargetName(Obj, RE, fmt); |
| 777 | break; |
| 778 | } |
| 779 | // X86 and ARM share some relocation types in common. |
| 780 | } else if (Arch == Triple::x86 || Arch == Triple::arm || |
| 781 | Arch == Triple::ppc) { |
| 782 | // Generic relocation types... |
| 783 | switch (Type) { |
| 784 | case MachO::GENERIC_RELOC_PAIR: // prints no info |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 785 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 786 | case MachO::GENERIC_RELOC_SECTDIFF: { |
| 787 | DataRefImpl RelNext = Rel; |
| 788 | Obj->moveRelocationNext(RelNext); |
| 789 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 790 | |
| 791 | // X86 sect diff's must be followed by a relocation of type |
| 792 | // GENERIC_RELOC_PAIR. |
| 793 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 794 | |
| 795 | if (RType != MachO::GENERIC_RELOC_PAIR) |
| 796 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 797 | "GENERIC_RELOC_SECTDIFF."); |
| 798 | |
| 799 | printRelocationTargetName(Obj, RE, fmt); |
| 800 | fmt << "-"; |
| 801 | printRelocationTargetName(Obj, RENext, fmt); |
| 802 | break; |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | if (Arch == Triple::x86 || Arch == Triple::ppc) { |
| 807 | switch (Type) { |
| 808 | case MachO::GENERIC_RELOC_LOCAL_SECTDIFF: { |
| 809 | DataRefImpl RelNext = Rel; |
| 810 | Obj->moveRelocationNext(RelNext); |
| 811 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 812 | |
| 813 | // X86 sect diff's must be followed by a relocation of type |
| 814 | // GENERIC_RELOC_PAIR. |
| 815 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 816 | if (RType != MachO::GENERIC_RELOC_PAIR) |
| 817 | report_fatal_error("Expected GENERIC_RELOC_PAIR after " |
| 818 | "GENERIC_RELOC_LOCAL_SECTDIFF."); |
| 819 | |
| 820 | printRelocationTargetName(Obj, RE, fmt); |
| 821 | fmt << "-"; |
| 822 | printRelocationTargetName(Obj, RENext, fmt); |
| 823 | break; |
| 824 | } |
| 825 | case MachO::GENERIC_RELOC_TLV: { |
| 826 | printRelocationTargetName(Obj, RE, fmt); |
| 827 | fmt << "@TLV"; |
| 828 | if (IsPCRel) |
| 829 | fmt << "P"; |
| 830 | break; |
| 831 | } |
| 832 | default: |
| 833 | printRelocationTargetName(Obj, RE, fmt); |
| 834 | } |
| 835 | } else { // ARM-specific relocations |
| 836 | switch (Type) { |
| 837 | case MachO::ARM_RELOC_HALF: |
| 838 | case MachO::ARM_RELOC_HALF_SECTDIFF: { |
| 839 | // Half relocations steal a bit from the length field to encode |
| 840 | // whether this is an upper16 or a lower16 relocation. |
| 841 | bool isUpper = Obj->getAnyRelocationLength(RE) >> 1; |
| 842 | |
| 843 | if (isUpper) |
| 844 | fmt << ":upper16:("; |
| 845 | else |
| 846 | fmt << ":lower16:("; |
| 847 | printRelocationTargetName(Obj, RE, fmt); |
| 848 | |
| 849 | DataRefImpl RelNext = Rel; |
| 850 | Obj->moveRelocationNext(RelNext); |
| 851 | MachO::any_relocation_info RENext = Obj->getRelocation(RelNext); |
| 852 | |
| 853 | // ARM half relocs must be followed by a relocation of type |
| 854 | // ARM_RELOC_PAIR. |
| 855 | unsigned RType = Obj->getAnyRelocationType(RENext); |
| 856 | if (RType != MachO::ARM_RELOC_PAIR) |
| 857 | report_fatal_error("Expected ARM_RELOC_PAIR after " |
| 858 | "ARM_RELOC_HALF"); |
| 859 | |
| 860 | // NOTE: The half of the target virtual address is stashed in the |
| 861 | // address field of the secondary relocation, but we can't reverse |
| 862 | // engineer the constant offset from it without decoding the movw/movt |
| 863 | // instruction to find the other half in its immediate field. |
| 864 | |
| 865 | // ARM_RELOC_HALF_SECTDIFF encodes the second section in the |
| 866 | // symbol/section pointer of the follow-on relocation. |
| 867 | if (Type == MachO::ARM_RELOC_HALF_SECTDIFF) { |
| 868 | fmt << "-"; |
| 869 | printRelocationTargetName(Obj, RENext, fmt); |
| 870 | } |
| 871 | |
| 872 | fmt << ")"; |
| 873 | break; |
| 874 | } |
| 875 | default: { printRelocationTargetName(Obj, RE, fmt); } |
| 876 | } |
| 877 | } |
| 878 | } else |
| 879 | printRelocationTargetName(Obj, RE, fmt); |
| 880 | |
| 881 | fmt.flush(); |
| 882 | Result.append(fmtbuf.begin(), fmtbuf.end()); |
Rui Ueyama | 7d09919 | 2015-06-09 15:20:42 +0000 | [diff] [blame] | 883 | return std::error_code(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | static std::error_code getRelocationValueString(const RelocationRef &Rel, |
| 887 | SmallVectorImpl<char> &Result) { |
Rafael Espindola | 854038e | 2015-06-26 14:51:16 +0000 | [diff] [blame] | 888 | const ObjectFile *Obj = Rel.getObject(); |
Rafael Espindola | 37070a5 | 2015-06-03 04:48:06 +0000 | [diff] [blame] | 889 | if (auto *ELF = dyn_cast<ELFObjectFileBase>(Obj)) |
| 890 | return getRelocationValueString(ELF, Rel, Result); |
| 891 | if (auto *COFF = dyn_cast<COFFObjectFile>(Obj)) |
| 892 | return getRelocationValueString(COFF, Rel, Result); |
| 893 | auto *MachO = cast<MachOObjectFile>(Obj); |
| 894 | return getRelocationValueString(MachO, Rel, Result); |
| 895 | } |
| 896 | |
Rafael Espindola | 0ad71d9 | 2015-06-30 03:41:26 +0000 | [diff] [blame] | 897 | /// @brief Indicates whether this relocation should hidden when listing |
| 898 | /// relocations, usually because it is the trailing part of a multipart |
| 899 | /// relocation that will be printed as part of the leading relocation. |
| 900 | static bool getHidden(RelocationRef RelRef) { |
| 901 | const ObjectFile *Obj = RelRef.getObject(); |
| 902 | auto *MachO = dyn_cast<MachOObjectFile>(Obj); |
| 903 | if (!MachO) |
| 904 | return false; |
| 905 | |
| 906 | unsigned Arch = MachO->getArch(); |
| 907 | DataRefImpl Rel = RelRef.getRawDataRefImpl(); |
| 908 | uint64_t Type = MachO->getRelocationType(Rel); |
| 909 | |
| 910 | // On arches that use the generic relocations, GENERIC_RELOC_PAIR |
| 911 | // is always hidden. |
| 912 | if (Arch == Triple::x86 || Arch == Triple::arm || Arch == Triple::ppc) { |
| 913 | if (Type == MachO::GENERIC_RELOC_PAIR) |
| 914 | return true; |
| 915 | } else if (Arch == Triple::x86_64) { |
| 916 | // On x86_64, X86_64_RELOC_UNSIGNED is hidden only when it follows |
| 917 | // an X86_64_RELOC_SUBTRACTOR. |
| 918 | if (Type == MachO::X86_64_RELOC_UNSIGNED && Rel.d.a > 0) { |
| 919 | DataRefImpl RelPrev = Rel; |
| 920 | RelPrev.d.a--; |
| 921 | uint64_t PrevType = MachO->getRelocationType(RelPrev); |
| 922 | if (PrevType == MachO::X86_64_RELOC_SUBTRACTOR) |
| 923 | return true; |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | return false; |
| 928 | } |
| 929 | |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 930 | static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) { |
Jim Grosbach | af9aec0 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 931 | const Target *TheTarget = getTarget(Obj); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 932 | |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 933 | // Package up features to be passed to target/subtarget |
Daniel Sanders | 1d14864 | 2016-06-16 09:17:03 +0000 | [diff] [blame^] | 934 | SubtargetFeatures Features = Obj->getFeatures(); |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 935 | if (MAttrs.size()) { |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 936 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 937 | Features.AddFeature(MAttrs[i]); |
Jack Carter | 551efd7 | 2012-08-28 19:24:49 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 940 | std::unique_ptr<const MCRegisterInfo> MRI( |
| 941 | TheTarget->createMCRegInfo(TripleName)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 942 | if (!MRI) |
| 943 | report_fatal_error("error: no register info for target " + TripleName); |
Ahmed Bougacha | 0835ca1 | 2013-05-16 21:28:23 +0000 | [diff] [blame] | 944 | |
| 945 | // Set up disassembler. |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 946 | std::unique_ptr<const MCAsmInfo> AsmInfo( |
| 947 | TheTarget->createMCAsmInfo(*MRI, TripleName)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 948 | if (!AsmInfo) |
| 949 | report_fatal_error("error: no assembly info for target " + TripleName); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 950 | std::unique_ptr<const MCSubtargetInfo> STI( |
Daniel Sanders | 1d14864 | 2016-06-16 09:17:03 +0000 | [diff] [blame^] | 951 | TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString())); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 952 | if (!STI) |
| 953 | report_fatal_error("error: no subtarget info for target " + TripleName); |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 954 | std::unique_ptr<const MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 955 | if (!MII) |
| 956 | report_fatal_error("error: no instruction info for target " + TripleName); |
Lang Hames | a1bc0f5 | 2014-04-15 04:40:56 +0000 | [diff] [blame] | 957 | std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo); |
| 958 | MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get()); |
| 959 | |
| 960 | std::unique_ptr<MCDisassembler> DisAsm( |
| 961 | TheTarget->createMCDisassembler(*STI, Ctx)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 962 | if (!DisAsm) |
| 963 | report_fatal_error("error: no disassembler for target " + TripleName); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 964 | |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 965 | std::unique_ptr<const MCInstrAnalysis> MIA( |
| 966 | TheTarget->createMCInstrAnalysis(MII.get())); |
Ahmed Bougacha | aa79068 | 2013-05-24 01:07:04 +0000 | [diff] [blame] | 967 | |
Ahmed Bougacha | 0835ca1 | 2013-05-16 21:28:23 +0000 | [diff] [blame] | 968 | int AsmPrinterVariant = AsmInfo->getAssemblerDialect(); |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 969 | std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter( |
| 970 | Triple(TripleName), AsmPrinterVariant, *AsmInfo, *MII, *MRI)); |
Davide Italiano | 711e495 | 2015-12-17 01:59:50 +0000 | [diff] [blame] | 971 | if (!IP) |
| 972 | report_fatal_error("error: no instruction printer for target " + |
| 973 | TripleName); |
Colin LeMahieu | 14ec76e | 2015-06-07 21:07:17 +0000 | [diff] [blame] | 974 | IP->setPrintImmHex(PrintImmHex); |
Colin LeMahieu | 35436a2 | 2015-05-29 14:48:25 +0000 | [diff] [blame] | 975 | PrettyPrinter &PIP = selectPrettyPrinter(Triple(TripleName)); |
Ahmed Bougacha | 0835ca1 | 2013-05-16 21:28:23 +0000 | [diff] [blame] | 976 | |
Greg Fitzgerald | 1843227 | 2014-03-20 22:55:15 +0000 | [diff] [blame] | 977 | StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " : |
| 978 | "\t\t\t%08" PRIx64 ": "; |
| 979 | |
Mark Seaborn | 0929d3d | 2014-01-25 17:38:19 +0000 | [diff] [blame] | 980 | // Create a mapping, RelocSecs = SectionRelocMap[S], where sections |
| 981 | // in RelocSecs contain the relocations for section S. |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 982 | std::error_code EC; |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 983 | std::map<SectionRef, SmallVector<SectionRef, 1>> SectionRelocMap; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 984 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 985 | section_iterator Sec2 = Section.getRelocatedSection(); |
Rafael Espindola | b5155a5 | 2014-02-10 20:24:04 +0000 | [diff] [blame] | 986 | if (Sec2 != Obj->section_end()) |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 987 | SectionRelocMap[*Sec2].push_back(Section); |
Mark Seaborn | 0929d3d | 2014-01-25 17:38:19 +0000 | [diff] [blame] | 988 | } |
| 989 | |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 990 | // 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] | 991 | // pretty print the symbols while disassembling. |
| 992 | typedef std::vector<std::pair<uint64_t, StringRef>> SectionSymbolsTy; |
| 993 | std::map<SectionRef, SectionSymbolsTy> AllSymbols; |
| 994 | for (const SymbolRef &Symbol : Obj->symbols()) { |
| 995 | ErrorOr<uint64_t> AddressOrErr = Symbol.getAddress(); |
| 996 | error(AddressOrErr.getError()); |
| 997 | uint64_t Address = *AddressOrErr; |
David Majnemer | 2603a8fa | 2015-07-09 18:11:40 +0000 | [diff] [blame] | 998 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 999 | Expected<StringRef> Name = Symbol.getName(); |
| 1000 | error(errorToErrorCode(Name.takeError())); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1001 | if (Name->empty()) |
| 1002 | continue; |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1003 | |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1004 | Expected<section_iterator> SectionOrErr = Symbol.getSection(); |
| 1005 | error(errorToErrorCode(SectionOrErr.takeError())); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1006 | section_iterator SecI = *SectionOrErr; |
| 1007 | if (SecI == Obj->section_end()) |
| 1008 | continue; |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1009 | |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1010 | AllSymbols[*SecI].emplace_back(Address, *Name); |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1013 | // Create a mapping from virtual address to section. |
| 1014 | std::vector<std::pair<uint64_t, SectionRef>> SectionAddresses; |
| 1015 | for (SectionRef Sec : Obj->sections()) |
| 1016 | SectionAddresses.emplace_back(Sec.getAddress(), Sec); |
| 1017 | array_pod_sort(SectionAddresses.begin(), SectionAddresses.end()); |
| 1018 | |
| 1019 | // Linked executables (.exe and .dll files) typically don't include a real |
| 1020 | // symbol table but they might contain an export table. |
| 1021 | if (const auto *COFFObj = dyn_cast<COFFObjectFile>(Obj)) { |
| 1022 | for (const auto &ExportEntry : COFFObj->export_directories()) { |
| 1023 | StringRef Name; |
| 1024 | error(ExportEntry.getSymbolName(Name)); |
| 1025 | if (Name.empty()) |
| 1026 | continue; |
| 1027 | uint32_t RVA; |
| 1028 | error(ExportEntry.getExportRVA(RVA)); |
| 1029 | |
| 1030 | uint64_t VA = COFFObj->getImageBase() + RVA; |
| 1031 | auto Sec = std::upper_bound( |
| 1032 | SectionAddresses.begin(), SectionAddresses.end(), VA, |
| 1033 | [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) { |
| 1034 | return LHS < RHS.first; |
| 1035 | }); |
| 1036 | if (Sec != SectionAddresses.begin()) |
| 1037 | --Sec; |
| 1038 | else |
| 1039 | Sec = SectionAddresses.end(); |
| 1040 | |
| 1041 | if (Sec != SectionAddresses.end()) |
| 1042 | AllSymbols[Sec->second].emplace_back(VA, Name); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | // Sort all the symbols, this allows us to use a simple binary search to find |
| 1047 | // a symbol near an address. |
| 1048 | for (std::pair<const SectionRef, SectionSymbolsTy> &SecSyms : AllSymbols) |
| 1049 | array_pod_sort(SecSyms.second.begin(), SecSyms.second.end()); |
| 1050 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1051 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Colin LeMahieu | f34933e | 2015-07-23 20:58:49 +0000 | [diff] [blame] | 1052 | if (!DisassembleAll && (!Section.isText() || Section.isVirtual())) |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1053 | continue; |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 1054 | |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1055 | uint64_t SectionAddr = Section.getAddress(); |
| 1056 | uint64_t SectSize = Section.getSize(); |
David Majnemer | 185b5b1 | 2014-11-11 09:58:25 +0000 | [diff] [blame] | 1057 | if (!SectSize) |
| 1058 | continue; |
Simon Atanasyan | 2b614e1 | 2014-02-24 22:12:11 +0000 | [diff] [blame] | 1059 | |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1060 | // Get the list of all the symbols in this section. |
| 1061 | SectionSymbolsTy &Symbols = AllSymbols[Section]; |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1062 | std::vector<uint64_t> DataMappingSymsAddr; |
| 1063 | std::vector<uint64_t> TextMappingSymsAddr; |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1064 | if (Obj->isELF() && Obj->getArch() == Triple::aarch64) { |
| 1065 | for (const auto &Symb : Symbols) { |
| 1066 | uint64_t Address = Symb.first; |
| 1067 | StringRef Name = Symb.second; |
| 1068 | if (Name.startswith("$d")) |
David Majnemer | 153722d | 2015-11-18 04:35:32 +0000 | [diff] [blame] | 1069 | DataMappingSymsAddr.push_back(Address - SectionAddr); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1070 | if (Name.startswith("$x")) |
David Majnemer | 153722d | 2015-11-18 04:35:32 +0000 | [diff] [blame] | 1071 | TextMappingSymsAddr.push_back(Address - SectionAddr); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
| 1074 | |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1075 | std::sort(DataMappingSymsAddr.begin(), DataMappingSymsAddr.end()); |
| 1076 | std::sort(TextMappingSymsAddr.begin(), TextMappingSymsAddr.end()); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1077 | |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1078 | // Make a list of all the relocations for this section. |
| 1079 | std::vector<RelocationRef> Rels; |
| 1080 | if (InlineRelocs) { |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1081 | for (const SectionRef &RelocSec : SectionRelocMap[Section]) { |
| 1082 | for (const RelocationRef &Reloc : RelocSec.relocations()) { |
| 1083 | Rels.push_back(Reloc); |
| 1084 | } |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | // Sort relocations by address. |
| 1089 | std::sort(Rels.begin(), Rels.end(), RelocAddressLess); |
| 1090 | |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1091 | StringRef SegmentName = ""; |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1092 | if (const MachOObjectFile *MachO = dyn_cast<const MachOObjectFile>(Obj)) { |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1093 | DataRefImpl DR = Section.getRawDataRefImpl(); |
Rafael Espindola | 56f976f | 2013-04-18 18:08:55 +0000 | [diff] [blame] | 1094 | SegmentName = MachO->getSectionFinalSegmentName(DR); |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1095 | } |
Michael J. Spencer | 1d6167f | 2011-06-25 17:55:23 +0000 | [diff] [blame] | 1096 | StringRef name; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1097 | error(Section.getName(name)); |
Rafael Espindola | a9f810b | 2012-12-21 03:47:03 +0000 | [diff] [blame] | 1098 | outs() << "Disassembly of section "; |
| 1099 | if (!SegmentName.empty()) |
| 1100 | outs() << SegmentName << ","; |
| 1101 | outs() << name << ':'; |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1102 | |
Rafael Espindola | 7884c95 | 2015-06-04 15:01:05 +0000 | [diff] [blame] | 1103 | // If the section has no symbol at the start, just insert a dummy one. |
| 1104 | if (Symbols.empty() || Symbols[0].first != 0) |
David Majnemer | 153722d | 2015-11-18 04:35:32 +0000 | [diff] [blame] | 1105 | Symbols.insert(Symbols.begin(), std::make_pair(SectionAddr, name)); |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 1106 | |
| 1107 | SmallString<40> Comments; |
| 1108 | raw_svector_ostream CommentStream(Comments); |
Ahmed Bougacha | ad1084d | 2013-05-24 00:39:57 +0000 | [diff] [blame] | 1109 | |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 1110 | StringRef BytesStr; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1111 | error(Section.getContents(BytesStr)); |
Aaron Ballman | 106fd7b | 2014-11-12 14:01:17 +0000 | [diff] [blame] | 1112 | ArrayRef<uint8_t> Bytes(reinterpret_cast<const uint8_t *>(BytesStr.data()), |
| 1113 | BytesStr.size()); |
Rafael Espindola | 7fc5b87 | 2014-11-12 02:04:27 +0000 | [diff] [blame] | 1114 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1115 | uint64_t Size; |
| 1116 | uint64_t Index; |
| 1117 | |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1118 | std::vector<RelocationRef>::const_iterator rel_cur = Rels.begin(); |
| 1119 | std::vector<RelocationRef>::const_iterator rel_end = Rels.end(); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1120 | // Disassemble symbol by symbol. |
| 1121 | for (unsigned si = 0, se = Symbols.size(); si != se; ++si) { |
Rafael Espindola | e45c740 | 2014-08-17 16:31:39 +0000 | [diff] [blame] | 1122 | |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1123 | uint64_t Start = Symbols[si].first - SectionAddr; |
| 1124 | // The end is either the section end or the beginning of the next |
| 1125 | // symbol. |
| 1126 | uint64_t End = |
| 1127 | (si == se - 1) ? SectSize : Symbols[si + 1].first - SectionAddr; |
| 1128 | // Don't try to disassemble beyond the end of section contents. |
| 1129 | if (End > SectSize) |
| 1130 | End = SectSize; |
Rafael Espindola | e45c740 | 2014-08-17 16:31:39 +0000 | [diff] [blame] | 1131 | // 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] | 1132 | if (Start >= End) |
Michael J. Spencer | ee84f64 | 2011-10-13 20:37:08 +0000 | [diff] [blame] | 1133 | continue; |
| 1134 | |
Valery Pykhtin | de04805 | 2016-04-07 07:24:01 +0000 | [diff] [blame] | 1135 | if (Obj->isELF() && Obj->getArch() == Triple::amdgcn) { |
| 1136 | // make size 4 bytes folded |
| 1137 | End = Start + ((End - Start) & ~0x3ull); |
| 1138 | Start += 256; // add sizeof(amd_kernel_code_t) |
| 1139 | // cut trailing zeroes - up to 256 bytes (align) |
| 1140 | const uint64_t EndAlign = 256; |
| 1141 | const auto Limit = End - (std::min)(EndAlign, End - Start); |
| 1142 | while (End > Limit && |
| 1143 | *reinterpret_cast<const support::ulittle32_t*>(&Bytes[End - 4]) == 0) |
| 1144 | End -= 4; |
| 1145 | } |
| 1146 | |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1147 | outs() << '\n' << Symbols[si].second << ":\n"; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1148 | |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1149 | #ifndef NDEBUG |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1150 | raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls(); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1151 | #else |
Mark Seaborn | eb03ac5 | 2014-01-25 00:32:01 +0000 | [diff] [blame] | 1152 | raw_ostream &DebugOut = nulls(); |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1153 | #endif |
| 1154 | |
Benjamin Kramer | 43a772e | 2011-09-19 17:56:04 +0000 | [diff] [blame] | 1155 | for (Index = Start; Index < End; Index += Size) { |
| 1156 | MCInst Inst; |
Owen Anderson | a0c3b97 | 2011-09-15 23:38:46 +0000 | [diff] [blame] | 1157 | |
Davide Italiano | f070688 | 2015-10-01 21:57:09 +0000 | [diff] [blame] | 1158 | // AArch64 ELF binaries can interleave data and text in the |
| 1159 | // same section. We rely on the markers introduced to |
| 1160 | // understand what we need to dump. |
| 1161 | if (Obj->isELF() && Obj->getArch() == Triple::aarch64) { |
| 1162 | uint64_t Stride = 0; |
| 1163 | |
| 1164 | auto DAI = std::lower_bound(DataMappingSymsAddr.begin(), |
| 1165 | DataMappingSymsAddr.end(), Index); |
| 1166 | if (DAI != DataMappingSymsAddr.end() && *DAI == Index) { |
| 1167 | // Switch to data. |
| 1168 | while (Index < End) { |
| 1169 | outs() << format("%8" PRIx64 ":", SectionAddr + Index); |
| 1170 | outs() << "\t"; |
| 1171 | if (Index + 4 <= End) { |
| 1172 | Stride = 4; |
| 1173 | dumpBytes(Bytes.slice(Index, 4), outs()); |
| 1174 | outs() << "\t.word"; |
| 1175 | } else if (Index + 2 <= End) { |
| 1176 | Stride = 2; |
| 1177 | dumpBytes(Bytes.slice(Index, 2), outs()); |
| 1178 | outs() << "\t.short"; |
| 1179 | } else { |
| 1180 | Stride = 1; |
| 1181 | dumpBytes(Bytes.slice(Index, 1), outs()); |
| 1182 | outs() << "\t.byte"; |
| 1183 | } |
| 1184 | Index += Stride; |
| 1185 | outs() << "\n"; |
| 1186 | auto TAI = std::lower_bound(TextMappingSymsAddr.begin(), |
| 1187 | TextMappingSymsAddr.end(), Index); |
| 1188 | if (TAI != TextMappingSymsAddr.end() && *TAI == Index) |
| 1189 | break; |
| 1190 | } |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | if (Index >= End) |
| 1195 | break; |
| 1196 | |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1197 | bool Disassembled = DisAsm->getInstruction(Inst, Size, Bytes.slice(Index), |
| 1198 | SectionAddr + Index, DebugOut, |
| 1199 | CommentStream); |
| 1200 | if (Size == 0) |
| 1201 | Size = 1; |
| 1202 | PIP.printInst(*IP, Disassembled ? &Inst : nullptr, |
| 1203 | Bytes.slice(Index, Size), |
| 1204 | SectionAddr + Index, outs(), "", *STI); |
| 1205 | outs() << CommentStream.str(); |
| 1206 | Comments.clear(); |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1207 | |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1208 | // Try to resolve the target of a call, tail call, etc. to a specific |
| 1209 | // symbol. |
| 1210 | if (MIA && (MIA->isCall(Inst) || MIA->isUnconditionalBranch(Inst) || |
| 1211 | MIA->isConditionalBranch(Inst))) { |
| 1212 | uint64_t Target; |
| 1213 | if (MIA->evaluateBranch(Inst, SectionAddr + Index, Size, Target)) { |
| 1214 | // In a relocatable object, the target's section must reside in |
| 1215 | // the same section as the call instruction or it is accessed |
| 1216 | // through a relocation. |
| 1217 | // |
| 1218 | // In a non-relocatable object, the target may be in any section. |
| 1219 | // |
| 1220 | // N.B. We don't walk the relocations in the relocatable case yet. |
| 1221 | auto *TargetSectionSymbols = &Symbols; |
| 1222 | if (!Obj->isRelocatableObject()) { |
| 1223 | auto SectionAddress = std::upper_bound( |
| 1224 | SectionAddresses.begin(), SectionAddresses.end(), Target, |
| 1225 | [](uint64_t LHS, |
| 1226 | const std::pair<uint64_t, SectionRef> &RHS) { |
| 1227 | return LHS < RHS.first; |
| 1228 | }); |
| 1229 | if (SectionAddress != SectionAddresses.begin()) { |
| 1230 | --SectionAddress; |
| 1231 | TargetSectionSymbols = &AllSymbols[SectionAddress->second]; |
| 1232 | } else { |
| 1233 | TargetSectionSymbols = nullptr; |
David Majnemer | fbb1c3a | 2015-11-18 02:49:19 +0000 | [diff] [blame] | 1234 | } |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1235 | } |
David Majnemer | 2603a8fa | 2015-07-09 18:11:40 +0000 | [diff] [blame] | 1236 | |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1237 | // Find the first symbol in the section whose offset is less than |
| 1238 | // or equal to the target. |
| 1239 | if (TargetSectionSymbols) { |
| 1240 | auto TargetSym = std::upper_bound( |
| 1241 | TargetSectionSymbols->begin(), TargetSectionSymbols->end(), |
| 1242 | Target, [](uint64_t LHS, |
| 1243 | const std::pair<uint64_t, StringRef> &RHS) { |
| 1244 | return LHS < RHS.first; |
| 1245 | }); |
| 1246 | if (TargetSym != TargetSectionSymbols->begin()) { |
| 1247 | --TargetSym; |
| 1248 | uint64_t TargetAddress = std::get<0>(*TargetSym); |
| 1249 | StringRef TargetName = std::get<1>(*TargetSym); |
| 1250 | outs() << " <" << TargetName; |
| 1251 | uint64_t Disp = Target - TargetAddress; |
| 1252 | if (Disp) |
| 1253 | outs() << "+0x" << utohexstr(Disp); |
| 1254 | outs() << '>'; |
David Majnemer | 81afca6 | 2015-07-07 22:06:59 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | } |
Benjamin Kramer | e0dda9c | 2011-07-15 18:39:24 +0000 | [diff] [blame] | 1258 | } |
Colin LeMahieu | 307a83d | 2016-03-18 16:26:48 +0000 | [diff] [blame] | 1259 | outs() << "\n"; |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1260 | |
| 1261 | // Print relocation for instruction. |
| 1262 | while (rel_cur != rel_end) { |
Rafael Espindola | 0ad71d9 | 2015-06-30 03:41:26 +0000 | [diff] [blame] | 1263 | bool hidden = getHidden(*rel_cur); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1264 | uint64_t addr = rel_cur->getOffset(); |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1265 | SmallString<16> name; |
| 1266 | SmallString<32> val; |
Owen Anderson | fa3e520 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1267 | |
| 1268 | // If this relocation is hidden, skip it. |
Owen Anderson | fa3e520 | 2011-10-25 20:35:53 +0000 | [diff] [blame] | 1269 | if (hidden) goto skip_print_rel; |
| 1270 | |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1271 | // Stop when rel_cur's address is past the current instruction. |
Owen Anderson | f20e3e5 | 2011-10-25 20:15:39 +0000 | [diff] [blame] | 1272 | if (addr >= Index + Size) break; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1273 | rel_cur->getTypeName(name); |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1274 | error(getRelocationValueString(*rel_cur, val)); |
Greg Fitzgerald | 1843227 | 2014-03-20 22:55:15 +0000 | [diff] [blame] | 1275 | outs() << format(Fmt.data(), SectionAddr + addr) << name |
Benjamin Kramer | 8280311 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 1276 | << "\t" << val << "\n"; |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1277 | |
| 1278 | skip_print_rel: |
| 1279 | ++rel_cur; |
| 1280 | } |
Benjamin Kramer | 87ee76c | 2011-07-20 19:37:35 +0000 | [diff] [blame] | 1281 | } |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1282 | } |
| 1283 | } |
| 1284 | } |
| 1285 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 1286 | void llvm::PrintRelocations(const ObjectFile *Obj) { |
Greg Fitzgerald | 1843227 | 2014-03-20 22:55:15 +0000 | [diff] [blame] | 1287 | StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 : |
| 1288 | "%08" PRIx64; |
Rafael Espindola | 9219fe7 | 2016-03-21 20:59:15 +0000 | [diff] [blame] | 1289 | // Regular objdump doesn't print relocations in non-relocatable object |
| 1290 | // files. |
| 1291 | if (!Obj->isRelocatableObject()) |
| 1292 | return; |
Rafael Espindola | c66d761 | 2014-08-17 19:09:37 +0000 | [diff] [blame] | 1293 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1294 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1295 | if (Section.relocation_begin() == Section.relocation_end()) |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1296 | continue; |
| 1297 | StringRef secname; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1298 | error(Section.getName(secname)); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1299 | outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n"; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1300 | for (const RelocationRef &Reloc : Section.relocations()) { |
Rafael Espindola | 0ad71d9 | 2015-06-30 03:41:26 +0000 | [diff] [blame] | 1301 | bool hidden = getHidden(Reloc); |
Rafael Espindola | 96d071c | 2015-06-29 23:29:12 +0000 | [diff] [blame] | 1302 | uint64_t address = Reloc.getOffset(); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1303 | SmallString<32> relocname; |
| 1304 | SmallString<32> valuestr; |
Alexey Samsonov | aa4d295 | 2014-03-14 14:22:49 +0000 | [diff] [blame] | 1305 | if (hidden) |
| 1306 | continue; |
Rafael Espindola | 41bb432 | 2015-06-30 04:08:37 +0000 | [diff] [blame] | 1307 | Reloc.getTypeName(relocname); |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1308 | error(getRelocationValueString(Reloc, valuestr)); |
Greg Fitzgerald | 1843227 | 2014-03-20 22:55:15 +0000 | [diff] [blame] | 1309 | outs() << format(Fmt.data(), address) << " " << relocname << " " |
| 1310 | << valuestr << "\n"; |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1311 | } |
| 1312 | outs() << "\n"; |
| 1313 | } |
| 1314 | } |
| 1315 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 1316 | void llvm::PrintSectionHeaders(const ObjectFile *Obj) { |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1317 | outs() << "Sections:\n" |
| 1318 | "Idx Name Size Address Type\n"; |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1319 | unsigned i = 0; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1320 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1321 | StringRef Name; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1322 | error(Section.getName(Name)); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1323 | uint64_t Address = Section.getAddress(); |
| 1324 | uint64_t Size = Section.getSize(); |
| 1325 | bool Text = Section.isText(); |
| 1326 | bool Data = Section.isData(); |
| 1327 | bool BSS = Section.isBSS(); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1328 | std::string Type = (std::string(Text ? "TEXT " : "") + |
Michael J. Spencer | 8f67d47 | 2011-10-13 20:37:20 +0000 | [diff] [blame] | 1329 | (Data ? "DATA " : "") + (BSS ? "BSS" : "")); |
Alexey Samsonov | 48803e5 | 2014-03-13 14:37:36 +0000 | [diff] [blame] | 1330 | outs() << format("%3d %-13s %08" PRIx64 " %016" PRIx64 " %s\n", i, |
| 1331 | Name.str().c_str(), Size, Address, Type.c_str()); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1332 | ++i; |
| 1333 | } |
| 1334 | } |
| 1335 | |
Kevin Enderby | 98da613 | 2015-01-20 21:47:46 +0000 | [diff] [blame] | 1336 | void llvm::PrintSectionContents(const ObjectFile *Obj) { |
Rafael Espindola | 4453e4294 | 2014-06-13 03:07:50 +0000 | [diff] [blame] | 1337 | std::error_code EC; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1338 | for (const SectionRef &Section : ToolSectionFilter(*Obj)) { |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1339 | StringRef Name; |
| 1340 | StringRef Contents; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1341 | error(Section.getName(Name)); |
Rafael Espindola | 8029127 | 2014-10-08 15:28:58 +0000 | [diff] [blame] | 1342 | uint64_t BaseAddr = Section.getAddress(); |
David Majnemer | 185b5b1 | 2014-11-11 09:58:25 +0000 | [diff] [blame] | 1343 | uint64_t Size = Section.getSize(); |
| 1344 | if (!Size) |
| 1345 | continue; |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1346 | |
| 1347 | outs() << "Contents of section " << Name << ":\n"; |
David Majnemer | 185b5b1 | 2014-11-11 09:58:25 +0000 | [diff] [blame] | 1348 | if (Section.isBSS()) { |
Alexey Samsonov | 209095c | 2013-04-16 10:53:11 +0000 | [diff] [blame] | 1349 | outs() << format("<skipping contents of bss section at [%04" PRIx64 |
David Majnemer | 8f6b04c | 2014-07-14 16:20:14 +0000 | [diff] [blame] | 1350 | ", %04" PRIx64 ")>\n", |
| 1351 | BaseAddr, BaseAddr + Size); |
Alexey Samsonov | 209095c | 2013-04-16 10:53:11 +0000 | [diff] [blame] | 1352 | continue; |
| 1353 | } |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1354 | |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1355 | error(Section.getContents(Contents)); |
David Majnemer | 8f6b04c | 2014-07-14 16:20:14 +0000 | [diff] [blame] | 1356 | |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1357 | // Dump out the content as hex and printable ascii characters. |
| 1358 | for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) { |
Benjamin Kramer | 8280311 | 2012-03-10 02:04:38 +0000 | [diff] [blame] | 1359 | outs() << format(" %04" PRIx64 " ", BaseAddr + addr); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1360 | // Dump line of hex. |
| 1361 | for (std::size_t i = 0; i < 16; ++i) { |
| 1362 | if (i != 0 && i % 4 == 0) |
| 1363 | outs() << ' '; |
| 1364 | if (addr + i < end) |
| 1365 | outs() << hexdigit((Contents[addr + i] >> 4) & 0xF, true) |
| 1366 | << hexdigit(Contents[addr + i] & 0xF, true); |
| 1367 | else |
| 1368 | outs() << " "; |
| 1369 | } |
| 1370 | // Print ascii. |
| 1371 | outs() << " "; |
| 1372 | for (std::size_t i = 0; i < 16 && addr + i < end; ++i) { |
Guy Benyei | 83c74e9 | 2013-02-12 21:21:59 +0000 | [diff] [blame] | 1373 | if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF)) |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1374 | outs() << Contents[addr + i]; |
| 1375 | else |
| 1376 | outs() << "."; |
| 1377 | } |
| 1378 | outs() << "\n"; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 1383 | void llvm::PrintSymbolTable(const ObjectFile *o, StringRef ArchiveName, |
| 1384 | StringRef ArchitectureName) { |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1385 | outs() << "SYMBOL TABLE:\n"; |
| 1386 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1387 | if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) { |
Davide Italiano | e85abf7 | 2015-12-20 09:54:34 +0000 | [diff] [blame] | 1388 | printCOFFSymbolTable(coff); |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | } |
| 1391 | for (const SymbolRef &Symbol : o->symbols()) { |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 1392 | ErrorOr<uint64_t> AddressOrError = Symbol.getAddress(); |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1393 | error(AddressOrError.getError()); |
Rafael Espindola | ed067c4 | 2015-07-03 18:19:00 +0000 | [diff] [blame] | 1394 | uint64_t Address = *AddressOrError; |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1395 | Expected<SymbolRef::Type> TypeOrError = Symbol.getType(); |
| 1396 | if (!TypeOrError) |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 1397 | report_error(ArchiveName, o->getFileName(), TypeOrError.takeError()); |
Kevin Enderby | 5afbc1c | 2016-03-23 20:27:00 +0000 | [diff] [blame] | 1398 | SymbolRef::Type Type = *TypeOrError; |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1399 | uint32_t Flags = Symbol.getFlags(); |
Kevin Enderby | 7bd8d99 | 2016-05-02 20:28:12 +0000 | [diff] [blame] | 1400 | Expected<section_iterator> SectionOrErr = Symbol.getSection(); |
| 1401 | error(errorToErrorCode(SectionOrErr.takeError())); |
Rafael Espindola | 8bab889 | 2015-08-07 23:27:14 +0000 | [diff] [blame] | 1402 | section_iterator Section = *SectionOrErr; |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 1403 | StringRef Name; |
| 1404 | if (Type == SymbolRef::ST_Debug && Section != o->section_end()) { |
| 1405 | Section->getName(Name); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1406 | } else { |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 1407 | Expected<StringRef> NameOrErr = Symbol.getName(); |
| 1408 | if (!NameOrErr) |
Kevin Enderby | 9acb109 | 2016-05-31 20:35:34 +0000 | [diff] [blame] | 1409 | report_error(ArchiveName, o->getFileName(), NameOrErr.takeError(), |
| 1410 | ArchitectureName); |
Rafael Espindola | 5d0c2ff | 2015-07-02 20:55:21 +0000 | [diff] [blame] | 1411 | Name = *NameOrErr; |
Rafael Espindola | 75d5b54 | 2015-06-03 05:14:22 +0000 | [diff] [blame] | 1412 | } |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1413 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1414 | bool Global = Flags & SymbolRef::SF_Global; |
| 1415 | bool Weak = Flags & SymbolRef::SF_Weak; |
| 1416 | bool Absolute = Flags & SymbolRef::SF_Absolute; |
Colin LeMahieu | bc2f47a | 2015-01-23 20:06:24 +0000 | [diff] [blame] | 1417 | bool Common = Flags & SymbolRef::SF_Common; |
Davide Italiano | cd2514d | 2015-04-30 23:08:53 +0000 | [diff] [blame] | 1418 | bool Hidden = Flags & SymbolRef::SF_Hidden; |
David Meyer | 1df4b84 | 2012-02-28 23:47:53 +0000 | [diff] [blame] | 1419 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1420 | char GlobLoc = ' '; |
| 1421 | if (Type != SymbolRef::ST_Unknown) |
| 1422 | GlobLoc = Global ? 'g' : 'l'; |
| 1423 | char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File) |
| 1424 | ? 'd' : ' '; |
| 1425 | char FileFunc = ' '; |
| 1426 | if (Type == SymbolRef::ST_File) |
| 1427 | FileFunc = 'f'; |
| 1428 | else if (Type == SymbolRef::ST_Function) |
| 1429 | FileFunc = 'F'; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1430 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1431 | const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 : |
| 1432 | "%08" PRIx64; |
Michael J. Spencer | d857c1c | 2013-01-10 22:40:50 +0000 | [diff] [blame] | 1433 | |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1434 | outs() << format(Fmt, Address) << " " |
| 1435 | << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' ' |
| 1436 | << (Weak ? 'w' : ' ') // Weak? |
| 1437 | << ' ' // Constructor. Not supported yet. |
| 1438 | << ' ' // Warning. Not supported yet. |
| 1439 | << ' ' // Indirect reference to another symbol. |
| 1440 | << Debug // Debugging (d) or dynamic (D) symbol. |
| 1441 | << FileFunc // Name of function (F), file (f) or object (O). |
| 1442 | << ' '; |
| 1443 | if (Absolute) { |
| 1444 | outs() << "*ABS*"; |
Colin LeMahieu | bc2f47a | 2015-01-23 20:06:24 +0000 | [diff] [blame] | 1445 | } else if (Common) { |
| 1446 | outs() << "*COM*"; |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1447 | } else if (Section == o->section_end()) { |
| 1448 | outs() << "*UND*"; |
| 1449 | } else { |
| 1450 | if (const MachOObjectFile *MachO = |
| 1451 | dyn_cast<const MachOObjectFile>(o)) { |
| 1452 | DataRefImpl DR = Section->getRawDataRefImpl(); |
| 1453 | StringRef SegmentName = MachO->getSectionFinalSegmentName(DR); |
| 1454 | outs() << SegmentName << ","; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1455 | } |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1456 | StringRef SectionName; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1457 | error(Section->getName(SectionName)); |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1458 | outs() << SectionName; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1459 | } |
Rafael Espindola | 5f7ade2 | 2015-06-23 15:45:38 +0000 | [diff] [blame] | 1460 | |
| 1461 | outs() << '\t'; |
Rafael Espindola | ae3ac08 | 2015-06-23 18:34:25 +0000 | [diff] [blame] | 1462 | if (Common || isa<ELFObjectFileBase>(o)) { |
Rafael Espindola | dbb6bd3 | 2015-06-25 22:10:04 +0000 | [diff] [blame] | 1463 | uint64_t Val = |
| 1464 | Common ? Symbol.getAlignment() : ELFSymbolRef(Symbol).getSize(); |
Rafael Espindola | ae3ac08 | 2015-06-23 18:34:25 +0000 | [diff] [blame] | 1465 | outs() << format("\t %08" PRIx64 " ", Val); |
| 1466 | } |
Rafael Espindola | 5f7ade2 | 2015-06-23 15:45:38 +0000 | [diff] [blame] | 1467 | |
Davide Italiano | cd2514d | 2015-04-30 23:08:53 +0000 | [diff] [blame] | 1468 | if (Hidden) { |
| 1469 | outs() << ".hidden "; |
| 1470 | } |
| 1471 | outs() << Name |
Rui Ueyama | 4e39f71 | 2014-03-18 18:58:51 +0000 | [diff] [blame] | 1472 | << '\n'; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1473 | } |
| 1474 | } |
| 1475 | |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1476 | static void PrintUnwindInfo(const ObjectFile *o) { |
| 1477 | outs() << "Unwind info:\n\n"; |
| 1478 | |
| 1479 | if (const COFFObjectFile *coff = dyn_cast<COFFObjectFile>(o)) { |
| 1480 | printCOFFUnwindInfo(coff); |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1481 | } else if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 1482 | printMachOUnwindInfo(MachO); |
| 1483 | else { |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1484 | // TODO: Extract DWARF dump tool to objdump. |
| 1485 | errs() << "This operation is only currently supported " |
Tim Northover | 4bd286a | 2014-08-01 13:07:19 +0000 | [diff] [blame] | 1486 | "for COFF and MachO object files.\n"; |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1487 | return; |
| 1488 | } |
| 1489 | } |
| 1490 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 1491 | void llvm::printExportsTrie(const ObjectFile *o) { |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1492 | outs() << "Exports trie:\n"; |
| 1493 | if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 1494 | printMachOExportsTrie(MachO); |
| 1495 | else { |
| 1496 | errs() << "This operation is only currently supported " |
| 1497 | "for Mach-O executable files.\n"; |
| 1498 | return; |
| 1499 | } |
| 1500 | } |
| 1501 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 1502 | void llvm::printRebaseTable(const ObjectFile *o) { |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 1503 | outs() << "Rebase table:\n"; |
| 1504 | if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 1505 | printMachORebaseTable(MachO); |
| 1506 | else { |
| 1507 | errs() << "This operation is only currently supported " |
| 1508 | "for Mach-O executable files.\n"; |
| 1509 | return; |
| 1510 | } |
| 1511 | } |
| 1512 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 1513 | void llvm::printBindTable(const ObjectFile *o) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 1514 | outs() << "Bind table:\n"; |
| 1515 | if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 1516 | printMachOBindTable(MachO); |
| 1517 | else { |
| 1518 | errs() << "This operation is only currently supported " |
| 1519 | "for Mach-O executable files.\n"; |
| 1520 | return; |
| 1521 | } |
| 1522 | } |
| 1523 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 1524 | void llvm::printLazyBindTable(const ObjectFile *o) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 1525 | outs() << "Lazy bind table:\n"; |
| 1526 | if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 1527 | printMachOLazyBindTable(MachO); |
| 1528 | else { |
| 1529 | errs() << "This operation is only currently supported " |
| 1530 | "for Mach-O executable files.\n"; |
| 1531 | return; |
| 1532 | } |
| 1533 | } |
| 1534 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 1535 | void llvm::printWeakBindTable(const ObjectFile *o) { |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 1536 | outs() << "Weak bind table:\n"; |
| 1537 | if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) |
| 1538 | printMachOWeakBindTable(MachO); |
| 1539 | else { |
| 1540 | errs() << "This operation is only currently supported " |
| 1541 | "for Mach-O executable files.\n"; |
| 1542 | return; |
| 1543 | } |
| 1544 | } |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 1545 | |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1546 | /// Dump the raw contents of the __clangast section so the output can be piped |
| 1547 | /// into llvm-bcanalyzer. |
| 1548 | void llvm::printRawClangAST(const ObjectFile *Obj) { |
| 1549 | if (outs().is_displayed()) { |
| 1550 | errs() << "The -raw-clang-ast option will dump the raw binary contents of " |
| 1551 | "the clang ast section.\n" |
| 1552 | "Please redirect the output to a file or another program such as " |
| 1553 | "llvm-bcanalyzer.\n"; |
| 1554 | return; |
| 1555 | } |
| 1556 | |
| 1557 | StringRef ClangASTSectionName("__clangast"); |
| 1558 | if (isa<COFFObjectFile>(Obj)) { |
| 1559 | ClangASTSectionName = "clangast"; |
| 1560 | } |
| 1561 | |
| 1562 | Optional<object::SectionRef> ClangASTSection; |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1563 | for (auto Sec : ToolSectionFilter(*Obj)) { |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1564 | StringRef Name; |
| 1565 | Sec.getName(Name); |
| 1566 | if (Name == ClangASTSectionName) { |
| 1567 | ClangASTSection = Sec; |
| 1568 | break; |
| 1569 | } |
| 1570 | } |
| 1571 | if (!ClangASTSection) |
| 1572 | return; |
| 1573 | |
| 1574 | StringRef ClangASTContents; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1575 | error(ClangASTSection.getValue().getContents(ClangASTContents)); |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1576 | outs().write(ClangASTContents.data(), ClangASTContents.size()); |
| 1577 | } |
| 1578 | |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 1579 | static void printFaultMaps(const ObjectFile *Obj) { |
| 1580 | const char *FaultMapSectionName = nullptr; |
| 1581 | |
| 1582 | if (isa<ELFObjectFileBase>(Obj)) { |
| 1583 | FaultMapSectionName = ".llvm_faultmaps"; |
| 1584 | } else if (isa<MachOObjectFile>(Obj)) { |
| 1585 | FaultMapSectionName = "__llvm_faultmaps"; |
| 1586 | } else { |
| 1587 | errs() << "This operation is only currently supported " |
| 1588 | "for ELF and Mach-O executable files.\n"; |
| 1589 | return; |
| 1590 | } |
| 1591 | |
| 1592 | Optional<object::SectionRef> FaultMapSection; |
| 1593 | |
Colin LeMahieu | 77804be | 2015-07-29 15:45:39 +0000 | [diff] [blame] | 1594 | for (auto Sec : ToolSectionFilter(*Obj)) { |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 1595 | StringRef Name; |
| 1596 | Sec.getName(Name); |
| 1597 | if (Name == FaultMapSectionName) { |
| 1598 | FaultMapSection = Sec; |
| 1599 | break; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | outs() << "FaultMap table:\n"; |
| 1604 | |
| 1605 | if (!FaultMapSection.hasValue()) { |
| 1606 | outs() << "<not found>\n"; |
| 1607 | return; |
| 1608 | } |
| 1609 | |
| 1610 | StringRef FaultMapContents; |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1611 | error(FaultMapSection.getValue().getContents(FaultMapContents)); |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 1612 | |
| 1613 | FaultMapParser FMP(FaultMapContents.bytes_begin(), |
| 1614 | FaultMapContents.bytes_end()); |
| 1615 | |
| 1616 | outs() << FMP; |
| 1617 | } |
| 1618 | |
Kevin Enderby | 0ae163f | 2016-01-13 00:25:36 +0000 | [diff] [blame] | 1619 | static void printPrivateFileHeaders(const ObjectFile *o) { |
| 1620 | if (o->isELF()) |
| 1621 | printELFFileHeader(o); |
| 1622 | else if (o->isCOFF()) |
| 1623 | printCOFFFileHeader(o); |
| 1624 | else if (o->isMachO()) { |
| 1625 | printMachOFileHeader(o); |
| 1626 | printMachOLoadCommands(o); |
| 1627 | } else |
| 1628 | report_fatal_error("Invalid/Unsupported object file format"); |
| 1629 | } |
| 1630 | |
| 1631 | static void printFirstPrivateFileHeader(const ObjectFile *o) { |
Davide Italiano | 540e921 | 2015-12-19 22:09:40 +0000 | [diff] [blame] | 1632 | if (o->isELF()) |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1633 | printELFFileHeader(o); |
Davide Italiano | 540e921 | 2015-12-19 22:09:40 +0000 | [diff] [blame] | 1634 | else if (o->isCOFF()) |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1635 | printCOFFFileHeader(o); |
Davide Italiano | 540e921 | 2015-12-19 22:09:40 +0000 | [diff] [blame] | 1636 | else if (o->isMachO()) |
Kevin Enderby | b76d386 | 2014-08-22 20:35:18 +0000 | [diff] [blame] | 1637 | printMachOFileHeader(o); |
Davide Italiano | 540e921 | 2015-12-19 22:09:40 +0000 | [diff] [blame] | 1638 | else |
| 1639 | report_fatal_error("Invalid/Unsupported object file format"); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 1642 | static void DumpObject(const ObjectFile *o, const Archive *a = nullptr) { |
| 1643 | StringRef ArchiveName = a != nullptr ? a->getFileName() : ""; |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1644 | // Avoid other output when using a raw option. |
| 1645 | if (!RawClangAST) { |
| 1646 | outs() << '\n'; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 1647 | if (a) |
| 1648 | outs() << a->getFileName() << "(" << o->getFileName() << ")"; |
| 1649 | else |
| 1650 | outs() << o->getFileName(); |
| 1651 | outs() << ":\tfile format " << o->getFileFormatName() << "\n\n"; |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1652 | } |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1653 | |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1654 | if (Disassemble) |
Michael J. Spencer | 51862b3 | 2011-10-13 22:17:18 +0000 | [diff] [blame] | 1655 | DisassembleObject(o, Relocations); |
| 1656 | if (Relocations && !Disassemble) |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1657 | PrintRelocations(o); |
Nick Lewycky | fcf8462 | 2011-10-10 21:21:34 +0000 | [diff] [blame] | 1658 | if (SectionHeaders) |
| 1659 | PrintSectionHeaders(o); |
Michael J. Spencer | 4e25c02 | 2011-10-17 17:13:22 +0000 | [diff] [blame] | 1660 | if (SectionContents) |
| 1661 | PrintSectionContents(o); |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1662 | if (SymbolTable) |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 1663 | PrintSymbolTable(o, ArchiveName); |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1664 | if (UnwindInfo) |
| 1665 | PrintUnwindInfo(o); |
Rui Ueyama | c2bed42 | 2013-09-27 21:04:00 +0000 | [diff] [blame] | 1666 | if (PrivateHeaders) |
Kevin Enderby | 0ae163f | 2016-01-13 00:25:36 +0000 | [diff] [blame] | 1667 | printPrivateFileHeaders(o); |
| 1668 | if (FirstPrivateHeader) |
| 1669 | printFirstPrivateFileHeader(o); |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1670 | if (ExportsTrie) |
| 1671 | printExportsTrie(o); |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 1672 | if (Rebase) |
| 1673 | printRebaseTable(o); |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 1674 | if (Bind) |
| 1675 | printBindTable(o); |
| 1676 | if (LazyBind) |
| 1677 | printLazyBindTable(o); |
| 1678 | if (WeakBind) |
| 1679 | printWeakBindTable(o); |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1680 | if (RawClangAST) |
| 1681 | printRawClangAST(o); |
Sanjoy Das | 6f567a4 | 2015-06-22 18:03:02 +0000 | [diff] [blame] | 1682 | if (PrintFaultMaps) |
| 1683 | printFaultMaps(o); |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 1684 | if (DwarfDumpType != DIDT_Null) { |
| 1685 | std::unique_ptr<DIContext> DICtx(new DWARFContextInMemory(*o)); |
| 1686 | // Dump the complete DWARF structure. |
| 1687 | DICtx->dump(outs(), DwarfDumpType, true /* DumpEH */); |
| 1688 | } |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
| 1691 | /// @brief Dump each object file in \a a; |
| 1692 | static void DumpArchive(const Archive *a) { |
Kevin Enderby | 7a96942 | 2015-11-05 19:24:56 +0000 | [diff] [blame] | 1693 | for (auto &ErrorOrChild : a->children()) { |
| 1694 | if (std::error_code EC = ErrorOrChild.getError()) |
| 1695 | report_error(a->getFileName(), EC); |
| 1696 | const Archive::Child &C = *ErrorOrChild; |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 1697 | Expected<std::unique_ptr<Binary>> ChildOrErr = C.getAsBinary(); |
| 1698 | if (!ChildOrErr) { |
| 1699 | if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) |
| 1700 | report_error(a->getFileName(), C, std::move(E)); |
| 1701 | continue; |
| 1702 | } |
Rafael Espindola | ae46002 | 2014-06-16 16:08:36 +0000 | [diff] [blame] | 1703 | if (ObjectFile *o = dyn_cast<ObjectFile>(&*ChildOrErr.get())) |
Kevin Enderby | ac9e155 | 2016-05-17 17:10:12 +0000 | [diff] [blame] | 1704 | DumpObject(o, a); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1705 | else |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 1706 | report_error(a->getFileName(), object_error::invalid_file_type); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /// @brief Open file and figure out how to dump it. |
| 1711 | static void DumpInput(StringRef file) { |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1712 | |
Kevin Enderby | e2297dd | 2015-01-07 21:02:18 +0000 | [diff] [blame] | 1713 | // If we are using the Mach-O specific object file parser, then let it parse |
| 1714 | // the file and process the command line options. So the -arch flags can |
| 1715 | // be used to select specific slices, etc. |
| 1716 | if (MachOOpt) { |
| 1717 | ParseInputMachO(file); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1718 | return; |
| 1719 | } |
| 1720 | |
| 1721 | // Attempt to open the binary. |
Kevin Enderby | 3fcdf6a | 2016-04-06 22:14:09 +0000 | [diff] [blame] | 1722 | Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(file); |
| 1723 | if (!BinaryOrErr) |
Kevin Enderby | b34e3a1 | 2016-05-05 17:43:35 +0000 | [diff] [blame] | 1724 | report_error(file, BinaryOrErr.takeError()); |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 1725 | Binary &Binary = *BinaryOrErr.get().getBinary(); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1726 | |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 1727 | if (Archive *a = dyn_cast<Archive>(&Binary)) |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1728 | DumpArchive(a); |
Rafael Espindola | 3f6481d | 2014-08-01 14:31:55 +0000 | [diff] [blame] | 1729 | else if (ObjectFile *o = dyn_cast<ObjectFile>(&Binary)) |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1730 | DumpObject(o); |
Jim Grosbach | af9aec0 | 2012-08-07 17:53:14 +0000 | [diff] [blame] | 1731 | else |
Alexey Samsonov | 50d0fbd | 2015-06-04 18:34:11 +0000 | [diff] [blame] | 1732 | report_error(file, object_error::invalid_file_type); |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1735 | int main(int argc, char **argv) { |
| 1736 | // Print a stack trace if we signal out. |
Richard Smith | 2ad6d48 | 2016-06-09 00:53:21 +0000 | [diff] [blame] | 1737 | sys::PrintStackTraceOnErrorSignal(argv[0]); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1738 | PrettyStackTraceProgram X(argc, argv); |
| 1739 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 1740 | |
| 1741 | // Initialize targets and assembly printers/parsers. |
| 1742 | llvm::InitializeAllTargetInfos(); |
Evan Cheng | 8c886a4 | 2011-07-22 21:58:54 +0000 | [diff] [blame] | 1743 | llvm::InitializeAllTargetMCs(); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1744 | llvm::InitializeAllDisassemblers(); |
| 1745 | |
Pete Cooper | 28fb4fc | 2012-05-03 23:20:10 +0000 | [diff] [blame] | 1746 | // Register the target printer for --version. |
| 1747 | cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); |
| 1748 | |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1749 | cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n"); |
| 1750 | TripleName = Triple::normalize(TripleName); |
| 1751 | |
| 1752 | ToolName = argv[0]; |
| 1753 | |
| 1754 | // Defaults to a.out if no filenames specified. |
| 1755 | if (InputFilenames.size() == 0) |
| 1756 | InputFilenames.push_back("a.out"); |
| 1757 | |
Colin LeMahieu | f34933e | 2015-07-23 20:58:49 +0000 | [diff] [blame] | 1758 | if (DisassembleAll) |
| 1759 | Disassemble = true; |
Michael J. Spencer | bfa0678 | 2011-10-18 19:32:17 +0000 | [diff] [blame] | 1760 | if (!Disassemble |
| 1761 | && !Relocations |
| 1762 | && !SectionHeaders |
| 1763 | && !SectionContents |
Michael J. Spencer | 0c6ec48 | 2012-12-05 20:12:35 +0000 | [diff] [blame] | 1764 | && !SymbolTable |
Michael J. Spencer | 209565db | 2013-01-06 03:56:49 +0000 | [diff] [blame] | 1765 | && !UnwindInfo |
Nick Kledzik | d04bc35 | 2014-08-30 00:20:14 +0000 | [diff] [blame] | 1766 | && !PrivateHeaders |
Kevin Enderby | 0ae163f | 2016-01-13 00:25:36 +0000 | [diff] [blame] | 1767 | && !FirstPrivateHeader |
Nick Kledzik | ac43144 | 2014-09-12 21:34:15 +0000 | [diff] [blame] | 1768 | && !ExportsTrie |
Nick Kledzik | 56ebef4 | 2014-09-16 01:41:51 +0000 | [diff] [blame] | 1769 | && !Rebase |
| 1770 | && !Bind |
| 1771 | && !LazyBind |
Kevin Enderby | 131d177 | 2015-01-09 19:22:37 +0000 | [diff] [blame] | 1772 | && !WeakBind |
Adrian Prantl | 437105a | 2015-07-08 02:04:15 +0000 | [diff] [blame] | 1773 | && !RawClangAST |
Kevin Enderby | 13023a1 | 2015-01-15 23:19:11 +0000 | [diff] [blame] | 1774 | && !(UniversalHeaders && MachOOpt) |
Kevin Enderby | a7bdc7e | 2015-01-22 18:55:27 +0000 | [diff] [blame] | 1775 | && !(ArchiveHeaders && MachOOpt) |
Kevin Enderby | 69fe98d | 2015-01-23 18:52:17 +0000 | [diff] [blame] | 1776 | && !(IndirectSymbols && MachOOpt) |
Kevin Enderby | 9a50944 | 2015-01-27 21:28:24 +0000 | [diff] [blame] | 1777 | && !(DataInCode && MachOOpt) |
Kevin Enderby | f6d2585 | 2015-01-31 00:37:11 +0000 | [diff] [blame] | 1778 | && !(LinkOptHints && MachOOpt) |
Kevin Enderby | cd66be5 | 2015-03-11 22:06:32 +0000 | [diff] [blame] | 1779 | && !(InfoPlist && MachOOpt) |
Kevin Enderby | bc847fa | 2015-03-16 20:08:09 +0000 | [diff] [blame] | 1780 | && !(DylibsUsed && MachOOpt) |
| 1781 | && !(DylibId && MachOOpt) |
Kevin Enderby | 0fc1182 | 2015-04-01 20:57:01 +0000 | [diff] [blame] | 1782 | && !(ObjcMetaData && MachOOpt) |
Colin LeMahieu | fcc3276 | 2015-07-29 19:08:10 +0000 | [diff] [blame] | 1783 | && !(FilterSections.size() != 0 && MachOOpt) |
Igor Laevsky | 03a670c | 2016-01-26 15:09:42 +0000 | [diff] [blame] | 1784 | && !PrintFaultMaps |
| 1785 | && DwarfDumpType == DIDT_Null) { |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1786 | cl::PrintHelpMessage(); |
| 1787 | return 2; |
| 1788 | } |
| 1789 | |
Michael J. Spencer | ba4a362 | 2011-10-08 00:18:30 +0000 | [diff] [blame] | 1790 | std::for_each(InputFilenames.begin(), InputFilenames.end(), |
| 1791 | DumpInput); |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1792 | |
Davide Italiano | ccd53fe | 2015-08-05 07:18:31 +0000 | [diff] [blame] | 1793 | return EXIT_SUCCESS; |
Michael J. Spencer | 2670c25 | 2011-01-20 06:39:06 +0000 | [diff] [blame] | 1794 | } |