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