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