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