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