Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1 | //===-- ObjectFileELF.cpp ------------------------------------- -*- C++ -*-===// |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ObjectFileELF.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include <algorithm> |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 13 | #include <cassert> |
Tamas Berghammer | 9fa1147 | 2015-10-27 10:43:27 +0000 | [diff] [blame] | 14 | #include <unordered_map> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 16 | #include "lldb/Core/FileSpecList.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Module.h" |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 18 | #include "lldb/Core/ModuleSpec.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include "lldb/Core/PluginManager.h" |
| 20 | #include "lldb/Core/Section.h" |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/DWARFCallFrameInfo.h" |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/SymbolContext.h" |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 23 | #include "lldb/Target/SectionLoadList.h" |
Ed Maste | 5480365 | 2013-10-11 17:39:07 +0000 | [diff] [blame] | 24 | #include "lldb/Target/Target.h" |
Pavel Labath | 5f19b90 | 2017-11-13 16:16:33 +0000 | [diff] [blame] | 25 | #include "lldb/Utility/ArchSpec.h" |
Pavel Labath | e2867bc | 2017-12-15 14:23:58 +0000 | [diff] [blame] | 26 | #include "lldb/Utility/DataBufferHeap.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 27 | #include "lldb/Utility/Log.h" |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/Status.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 29 | #include "lldb/Utility/Stream.h" |
Pavel Labath | 38d0632 | 2017-06-29 14:32:17 +0000 | [diff] [blame] | 30 | #include "lldb/Utility/Timer.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 31 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/PointerUnion.h" |
Zachary Turner | 97a14e6 | 2014-08-19 17:18:29 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringRef.h" |
Pavel Labath | e2867bc | 2017-12-15 14:23:58 +0000 | [diff] [blame] | 34 | #include "llvm/Object/Decompressor.h" |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ARMBuildAttributes.h" |
Zachary Turner | 736d4d8 | 2014-06-25 05:42:32 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MemoryBuffer.h" |
Sagar Thakur | ad5b55a | 2016-05-24 14:52:50 +0000 | [diff] [blame] | 38 | #include "llvm/Support/MipsABIFlags.h" |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 39 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | #define CASE_AND_STREAM(s, def, width) \ |
| 41 | case def: \ |
| 42 | s->Printf("%-*s", width, #def); \ |
| 43 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 45 | using namespace lldb; |
| 46 | using namespace lldb_private; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 47 | using namespace elf; |
| 48 | using namespace llvm::ELF; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 50 | namespace { |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 51 | |
| 52 | // ELF note owner definitions |
| 53 | const char *const LLDB_NT_OWNER_FREEBSD = "FreeBSD"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 54 | const char *const LLDB_NT_OWNER_GNU = "GNU"; |
| 55 | const char *const LLDB_NT_OWNER_NETBSD = "NetBSD"; |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 56 | const char *const LLDB_NT_OWNER_OPENBSD = "OpenBSD"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | const char *const LLDB_NT_OWNER_CSR = "csr"; |
Tamas Berghammer | db037d9 | 2015-03-18 10:36:27 +0000 | [diff] [blame] | 58 | const char *const LLDB_NT_OWNER_ANDROID = "Android"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | const char *const LLDB_NT_OWNER_CORE = "CORE"; |
| 60 | const char *const LLDB_NT_OWNER_LINUX = "LINUX"; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 61 | |
| 62 | // ELF note type definitions |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | const elf_word LLDB_NT_FREEBSD_ABI_TAG = 0x01; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 64 | const elf_word LLDB_NT_FREEBSD_ABI_SIZE = 4; |
| 65 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 66 | const elf_word LLDB_NT_GNU_ABI_TAG = 0x01; |
| 67 | const elf_word LLDB_NT_GNU_ABI_SIZE = 16; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 68 | |
| 69 | const elf_word LLDB_NT_GNU_BUILD_ID_TAG = 0x03; |
| 70 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 71 | const elf_word LLDB_NT_NETBSD_ABI_TAG = 0x01; |
| 72 | const elf_word LLDB_NT_NETBSD_ABI_SIZE = 4; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 73 | |
| 74 | // GNU ABI note OS constants |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 75 | const elf_word LLDB_NT_GNU_ABI_OS_LINUX = 0x00; |
| 76 | const elf_word LLDB_NT_GNU_ABI_OS_HURD = 0x01; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 77 | const elf_word LLDB_NT_GNU_ABI_OS_SOLARIS = 0x02; |
| 78 | |
Greg Clayton | b704b69 | 2015-10-28 18:04:38 +0000 | [diff] [blame] | 79 | // LLDB_NT_OWNER_CORE and LLDB_NT_OWNER_LINUX note contants |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | #define NT_PRSTATUS 1 |
| 81 | #define NT_PRFPREG 2 |
| 82 | #define NT_PRPSINFO 3 |
| 83 | #define NT_TASKSTRUCT 4 |
| 84 | #define NT_AUXV 6 |
| 85 | #define NT_SIGINFO 0x53494749 |
| 86 | #define NT_FILE 0x46494c45 |
| 87 | #define NT_PRXFPREG 0x46e62b7f |
| 88 | #define NT_PPC_VMX 0x100 |
| 89 | #define NT_PPC_SPE 0x101 |
| 90 | #define NT_PPC_VSX 0x102 |
| 91 | #define NT_386_TLS 0x200 |
| 92 | #define NT_386_IOPERM 0x201 |
| 93 | #define NT_X86_XSTATE 0x202 |
| 94 | #define NT_S390_HIGH_GPRS 0x300 |
| 95 | #define NT_S390_TIMER 0x301 |
| 96 | #define NT_S390_TODCMP 0x302 |
| 97 | #define NT_S390_TODPREG 0x303 |
| 98 | #define NT_S390_CTRS 0x304 |
| 99 | #define NT_S390_PREFIX 0x305 |
| 100 | #define NT_S390_LAST_BREAK 0x306 |
| 101 | #define NT_S390_SYSTEM_CALL 0x307 |
| 102 | #define NT_S390_TDB 0x308 |
| 103 | #define NT_S390_VXRS_LOW 0x309 |
| 104 | #define NT_S390_VXRS_HIGH 0x30a |
| 105 | #define NT_ARM_VFP 0x400 |
| 106 | #define NT_ARM_TLS 0x401 |
| 107 | #define NT_ARM_HW_BREAK 0x402 |
| 108 | #define NT_ARM_HW_WATCH 0x403 |
| 109 | #define NT_ARM_SYSTEM_CALL 0x404 |
| 110 | #define NT_METAG_CBUF 0x500 |
| 111 | #define NT_METAG_RPIPE 0x501 |
| 112 | #define NT_METAG_TLS 0x502 |
Greg Clayton | b704b69 | 2015-10-28 18:04:38 +0000 | [diff] [blame] | 113 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | /// @class ELFRelocation |
Adrian Prantl | d8f460e | 2018-05-02 16:55:16 +0000 | [diff] [blame] | 116 | /// Generic wrapper for ELFRel and ELFRela. |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 117 | /// |
| 118 | /// This helper class allows us to parse both ELFRel and ELFRela relocation |
| 119 | /// entries in a generic manner. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | class ELFRelocation { |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 121 | public: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | /// Constructs an ELFRelocation entry with a personality as given by @p |
| 123 | /// type. |
| 124 | /// |
| 125 | /// @param type Either DT_REL or DT_RELA. Any other value is invalid. |
| 126 | ELFRelocation(unsigned type); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 127 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | ~ELFRelocation(); |
Ed Maste | 81b4c5f | 2016-01-04 01:43:47 +0000 | [diff] [blame] | 129 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | bool Parse(const lldb_private::DataExtractor &data, lldb::offset_t *offset); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 131 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | static unsigned RelocType32(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | static unsigned RelocType64(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | static unsigned RelocSymbol32(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 137 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 138 | static unsigned RelocSymbol64(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | static unsigned RelocOffset32(const ELFRelocation &rel); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 141 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | static unsigned RelocOffset64(const ELFRelocation &rel); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 143 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | static unsigned RelocAddend32(const ELFRelocation &rel); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 145 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 146 | static unsigned RelocAddend64(const ELFRelocation &rel); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 147 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 148 | private: |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | typedef llvm::PointerUnion<ELFRel *, ELFRela *> RelocUnion; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 150 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | RelocUnion reloc; |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 152 | }; |
| 153 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 154 | ELFRelocation::ELFRelocation(unsigned type) { |
| 155 | if (type == DT_REL || type == SHT_REL) |
| 156 | reloc = new ELFRel(); |
| 157 | else if (type == DT_RELA || type == SHT_RELA) |
| 158 | reloc = new ELFRela(); |
| 159 | else { |
| 160 | assert(false && "unexpected relocation type"); |
| 161 | reloc = static_cast<ELFRel *>(NULL); |
| 162 | } |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | ELFRelocation::~ELFRelocation() { |
| 166 | if (reloc.is<ELFRel *>()) |
| 167 | delete reloc.get<ELFRel *>(); |
| 168 | else |
| 169 | delete reloc.get<ELFRela *>(); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | bool ELFRelocation::Parse(const lldb_private::DataExtractor &data, |
| 173 | lldb::offset_t *offset) { |
| 174 | if (reloc.is<ELFRel *>()) |
| 175 | return reloc.get<ELFRel *>()->Parse(data, offset); |
| 176 | else |
| 177 | return reloc.get<ELFRela *>()->Parse(data, offset); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | unsigned ELFRelocation::RelocType32(const ELFRelocation &rel) { |
| 181 | if (rel.reloc.is<ELFRel *>()) |
| 182 | return ELFRel::RelocType32(*rel.reloc.get<ELFRel *>()); |
| 183 | else |
| 184 | return ELFRela::RelocType32(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 187 | unsigned ELFRelocation::RelocType64(const ELFRelocation &rel) { |
| 188 | if (rel.reloc.is<ELFRel *>()) |
| 189 | return ELFRel::RelocType64(*rel.reloc.get<ELFRel *>()); |
| 190 | else |
| 191 | return ELFRela::RelocType64(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 194 | unsigned ELFRelocation::RelocSymbol32(const ELFRelocation &rel) { |
| 195 | if (rel.reloc.is<ELFRel *>()) |
| 196 | return ELFRel::RelocSymbol32(*rel.reloc.get<ELFRel *>()); |
| 197 | else |
| 198 | return ELFRela::RelocSymbol32(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | unsigned ELFRelocation::RelocSymbol64(const ELFRelocation &rel) { |
| 202 | if (rel.reloc.is<ELFRel *>()) |
| 203 | return ELFRel::RelocSymbol64(*rel.reloc.get<ELFRel *>()); |
| 204 | else |
| 205 | return ELFRela::RelocSymbol64(*rel.reloc.get<ELFRela *>()); |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 208 | unsigned ELFRelocation::RelocOffset32(const ELFRelocation &rel) { |
| 209 | if (rel.reloc.is<ELFRel *>()) |
| 210 | return rel.reloc.get<ELFRel *>()->r_offset; |
| 211 | else |
| 212 | return rel.reloc.get<ELFRela *>()->r_offset; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 215 | unsigned ELFRelocation::RelocOffset64(const ELFRelocation &rel) { |
| 216 | if (rel.reloc.is<ELFRel *>()) |
| 217 | return rel.reloc.get<ELFRel *>()->r_offset; |
| 218 | else |
| 219 | return rel.reloc.get<ELFRela *>()->r_offset; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | unsigned ELFRelocation::RelocAddend32(const ELFRelocation &rel) { |
| 223 | if (rel.reloc.is<ELFRel *>()) |
| 224 | return 0; |
| 225 | else |
| 226 | return rel.reloc.get<ELFRela *>()->r_addend; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 229 | unsigned ELFRelocation::RelocAddend64(const ELFRelocation &rel) { |
| 230 | if (rel.reloc.is<ELFRel *>()) |
| 231 | return 0; |
| 232 | else |
| 233 | return rel.reloc.get<ELFRela *>()->r_addend; |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Stephen Wilson | 499b40e | 2011-03-30 16:07:05 +0000 | [diff] [blame] | 236 | } // end anonymous namespace |
| 237 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 238 | bool ELFNote::Parse(const DataExtractor &data, lldb::offset_t *offset) { |
| 239 | // Read all fields. |
| 240 | if (data.GetU32(offset, &n_namesz, 3) == NULL) |
| 241 | return false; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 242 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 243 | // The name field is required to be nul-terminated, and n_namesz includes the |
| 244 | // terminating nul in observed implementations (contrary to the ELF-64 spec). |
| 245 | // A special case is needed for cores generated by some older Linux versions, |
| 246 | // which write a note named "CORE" without a nul terminator and n_namesz = 4. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | if (n_namesz == 4) { |
| 248 | char buf[4]; |
| 249 | if (data.ExtractBytes(*offset, 4, data.GetByteOrder(), buf) != 4) |
| 250 | return false; |
| 251 | if (strncmp(buf, "CORE", 4) == 0) { |
| 252 | n_name = "CORE"; |
| 253 | *offset += 4; |
| 254 | return true; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 255 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | } |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | const char *cstr = data.GetCStr(offset, llvm::alignTo(n_namesz, 4)); |
| 259 | if (cstr == NULL) { |
| 260 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS)); |
| 261 | if (log) |
| 262 | log->Printf("Failed to parse note name lacking nul terminator"); |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 263 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | n_name = cstr; |
| 267 | return true; |
Ed Maste | c113ff8 | 2013-12-02 17:49:13 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | static uint32_t kalimbaVariantFromElfFlags(const elf::elf_word e_flags) { |
| 271 | const uint32_t dsp_rev = e_flags & 0xFF; |
| 272 | uint32_t kal_arch_variant = LLDB_INVALID_CPUTYPE; |
| 273 | switch (dsp_rev) { |
| 274 | // TODO(mg11) Support more variants |
| 275 | case 10: |
| 276 | kal_arch_variant = llvm::Triple::KalimbaSubArch_v3; |
| 277 | break; |
| 278 | case 14: |
| 279 | kal_arch_variant = llvm::Triple::KalimbaSubArch_v4; |
| 280 | break; |
| 281 | case 17: |
| 282 | case 20: |
| 283 | kal_arch_variant = llvm::Triple::KalimbaSubArch_v5; |
| 284 | break; |
| 285 | default: |
| 286 | break; |
| 287 | } |
| 288 | return kal_arch_variant; |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 291 | static uint32_t mipsVariantFromElfFlags (const elf::ELFHeader &header) { |
| 292 | const uint32_t mips_arch = header.e_flags & llvm::ELF::EF_MIPS_ARCH; |
| 293 | uint32_t endian = header.e_ident[EI_DATA]; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 294 | uint32_t arch_variant = ArchSpec::eMIPSSubType_unknown; |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 295 | uint32_t fileclass = header.e_ident[EI_CLASS]; |
| 296 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 297 | // If there aren't any elf flags available (e.g core elf file) then return |
| 298 | // default |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 299 | // 32 or 64 bit arch (without any architecture revision) based on object file's class. |
| 300 | if (header.e_type == ET_CORE) { |
| 301 | switch (fileclass) { |
| 302 | case llvm::ELF::ELFCLASS32: |
| 303 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el |
| 304 | : ArchSpec::eMIPSSubType_mips32; |
| 305 | case llvm::ELF::ELFCLASS64: |
| 306 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el |
| 307 | : ArchSpec::eMIPSSubType_mips64; |
| 308 | default: |
| 309 | return arch_variant; |
| 310 | } |
| 311 | } |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | switch (mips_arch) { |
| 314 | case llvm::ELF::EF_MIPS_ARCH_1: |
| 315 | case llvm::ELF::EF_MIPS_ARCH_2: |
| 316 | case llvm::ELF::EF_MIPS_ARCH_32: |
| 317 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32el |
| 318 | : ArchSpec::eMIPSSubType_mips32; |
| 319 | case llvm::ELF::EF_MIPS_ARCH_32R2: |
| 320 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r2el |
| 321 | : ArchSpec::eMIPSSubType_mips32r2; |
| 322 | case llvm::ELF::EF_MIPS_ARCH_32R6: |
| 323 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips32r6el |
| 324 | : ArchSpec::eMIPSSubType_mips32r6; |
| 325 | case llvm::ELF::EF_MIPS_ARCH_3: |
| 326 | case llvm::ELF::EF_MIPS_ARCH_4: |
| 327 | case llvm::ELF::EF_MIPS_ARCH_5: |
| 328 | case llvm::ELF::EF_MIPS_ARCH_64: |
| 329 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64el |
| 330 | : ArchSpec::eMIPSSubType_mips64; |
| 331 | case llvm::ELF::EF_MIPS_ARCH_64R2: |
| 332 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r2el |
| 333 | : ArchSpec::eMIPSSubType_mips64r2; |
| 334 | case llvm::ELF::EF_MIPS_ARCH_64R6: |
| 335 | return (endian == ELFDATA2LSB) ? ArchSpec::eMIPSSubType_mips64r6el |
| 336 | : ArchSpec::eMIPSSubType_mips64r6; |
| 337 | default: |
| 338 | break; |
| 339 | } |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 340 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | return arch_variant; |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | static uint32_t subTypeFromElfHeader(const elf::ELFHeader &header) { |
| 345 | if (header.e_machine == llvm::ELF::EM_MIPS) |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 346 | return mipsVariantFromElfFlags(header); |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 347 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 348 | return llvm::ELF::EM_CSR_KALIMBA == header.e_machine |
| 349 | ? kalimbaVariantFromElfFlags(header.e_flags) |
| 350 | : LLDB_INVALID_CPUTYPE; |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Matthew Gardiner | f03e6d84 | 2014-09-29 08:02:24 +0000 | [diff] [blame] | 353 | //! The kalimba toolchain identifies a code section as being |
| 354 | //! one with the SHT_PROGBITS set in the section sh_type and the top |
| 355 | //! bit in the 32-bit address field set. |
| 356 | static lldb::SectionType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | kalimbaSectionType(const elf::ELFHeader &header, |
| 358 | const elf::ELFSectionHeader §_hdr) { |
| 359 | if (llvm::ELF::EM_CSR_KALIMBA != header.e_machine) { |
Matthew Gardiner | 6e7b0a0 | 2014-10-15 08:21:54 +0000 | [diff] [blame] | 360 | return eSectionTypeOther; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | if (llvm::ELF::SHT_NOBITS == sect_hdr.sh_type) { |
| 364 | return eSectionTypeZeroFill; |
| 365 | } |
| 366 | |
| 367 | if (llvm::ELF::SHT_PROGBITS == sect_hdr.sh_type) { |
| 368 | const lldb::addr_t KAL_CODE_BIT = 1 << 31; |
| 369 | return KAL_CODE_BIT & sect_hdr.sh_addr ? eSectionTypeCode |
| 370 | : eSectionTypeData; |
| 371 | } |
| 372 | |
| 373 | return eSectionTypeOther; |
Matthew Gardiner | f03e6d84 | 2014-09-29 08:02:24 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 376 | // Arbitrary constant used as UUID prefix for core files. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 377 | const uint32_t ObjectFileELF::g_core_uuid_magic(0xE210C); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 378 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 379 | //------------------------------------------------------------------ |
| 380 | // Static methods. |
| 381 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 382 | void ObjectFileELF::Initialize() { |
| 383 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 384 | GetPluginDescriptionStatic(), CreateInstance, |
| 385 | CreateMemoryInstance, GetModuleSpecifications); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 388 | void ObjectFileELF::Terminate() { |
| 389 | PluginManager::UnregisterPlugin(CreateInstance); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 392 | lldb_private::ConstString ObjectFileELF::GetPluginNameStatic() { |
| 393 | static ConstString g_name("elf"); |
| 394 | return g_name; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 397 | const char *ObjectFileELF::GetPluginDescriptionStatic() { |
| 398 | return "ELF object file reader."; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 401 | ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp, |
| 402 | DataBufferSP &data_sp, |
| 403 | lldb::offset_t data_offset, |
| 404 | const lldb_private::FileSpec *file, |
| 405 | lldb::offset_t file_offset, |
| 406 | lldb::offset_t length) { |
| 407 | if (!data_sp) { |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 408 | data_sp = MapFileData(*file, length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 409 | if (!data_sp) |
| 410 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | data_offset = 0; |
| 412 | } |
| 413 | |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 414 | assert(data_sp); |
| 415 | |
| 416 | if (data_sp->GetByteSize() <= (llvm::ELF::EI_NIDENT + data_offset)) |
| 417 | return nullptr; |
| 418 | |
| 419 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
| 420 | if (!ELFHeader::MagicBytesMatch(magic)) |
| 421 | return nullptr; |
| 422 | |
| 423 | // Update the data to contain the entire file if it doesn't already |
| 424 | if (data_sp->GetByteSize() < length) { |
Pavel Labath | 50251fc | 2017-12-21 10:54:30 +0000 | [diff] [blame] | 425 | data_sp = MapFileData(*file, length, file_offset); |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 426 | if (!data_sp) |
| 427 | return nullptr; |
| 428 | data_offset = 0; |
| 429 | magic = data_sp->GetBytes(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | } |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 431 | |
| 432 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 433 | if (address_size == 4 || address_size == 8) { |
| 434 | std::unique_ptr<ObjectFileELF> objfile_ap(new ObjectFileELF( |
| 435 | module_sp, data_sp, data_offset, file, file_offset, length)); |
| 436 | ArchSpec spec; |
| 437 | if (objfile_ap->GetArchitecture(spec) && |
| 438 | objfile_ap->SetModulesArchitecture(spec)) |
| 439 | return objfile_ap.release(); |
| 440 | } |
| 441 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 445 | ObjectFile *ObjectFileELF::CreateMemoryInstance( |
| 446 | const lldb::ModuleSP &module_sp, DataBufferSP &data_sp, |
| 447 | const lldb::ProcessSP &process_sp, lldb::addr_t header_addr) { |
| 448 | if (data_sp && data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT)) { |
| 449 | const uint8_t *magic = data_sp->GetBytes(); |
| 450 | if (ELFHeader::MagicBytesMatch(magic)) { |
| 451 | unsigned address_size = ELFHeader::AddressSizeInBytes(magic); |
| 452 | if (address_size == 4 || address_size == 8) { |
Benjamin Kramer | e1a6074 | 2017-09-14 15:01:55 +0000 | [diff] [blame] | 453 | std::unique_ptr<ObjectFileELF> objfile_ap( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | new ObjectFileELF(module_sp, data_sp, process_sp, header_addr)); |
| 455 | ArchSpec spec; |
| 456 | if (objfile_ap->GetArchitecture(spec) && |
| 457 | objfile_ap->SetModulesArchitecture(spec)) |
| 458 | return objfile_ap.release(); |
| 459 | } |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 460 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | } |
| 462 | return NULL; |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 465 | bool ObjectFileELF::MagicBytesMatch(DataBufferSP &data_sp, |
| 466 | lldb::addr_t data_offset, |
| 467 | lldb::addr_t data_length) { |
| 468 | if (data_sp && |
| 469 | data_sp->GetByteSize() > (llvm::ELF::EI_NIDENT + data_offset)) { |
| 470 | const uint8_t *magic = data_sp->GetBytes() + data_offset; |
| 471 | return ELFHeader::MagicBytesMatch(magic); |
| 472 | } |
| 473 | return false; |
Michael Sartain | 9f0013d | 2013-05-17 00:20:21 +0000 | [diff] [blame] | 474 | } |
Greg Clayton | c966054 | 2012-02-05 02:38:54 +0000 | [diff] [blame] | 475 | |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 476 | /* |
| 477 | * crc function from http://svnweb.freebsd.org/base/head/sys/libkern/crc32.c |
| 478 | * |
| 479 | * COPYRIGHT (C) 1986 Gary S. Brown. You may use this program, or |
| 480 | * code or tables extracted from it, as desired without restriction. |
| 481 | */ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 482 | static uint32_t calc_crc32(uint32_t crc, const void *buf, size_t size) { |
| 483 | static const uint32_t g_crc32_tab[] = { |
| 484 | 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, |
| 485 | 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, |
| 486 | 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, |
| 487 | 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, |
| 488 | 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, |
| 489 | 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, |
| 490 | 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, |
| 491 | 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, |
| 492 | 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, |
| 493 | 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, |
| 494 | 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, |
| 495 | 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, |
| 496 | 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, |
| 497 | 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, |
| 498 | 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, |
| 499 | 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, |
| 500 | 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, |
| 501 | 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, |
| 502 | 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, |
| 503 | 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, |
| 504 | 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, |
| 505 | 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, |
| 506 | 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, |
| 507 | 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, |
| 508 | 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, |
| 509 | 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, |
| 510 | 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, |
| 511 | 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, |
| 512 | 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, |
| 513 | 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, |
| 514 | 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, |
| 515 | 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, |
| 516 | 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, |
| 517 | 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, |
| 518 | 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, |
| 519 | 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, |
| 520 | 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, |
| 521 | 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, |
| 522 | 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, |
| 523 | 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, |
| 524 | 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, |
| 525 | 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, |
| 526 | 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d}; |
| 527 | const uint8_t *p = (const uint8_t *)buf; |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 529 | crc = crc ^ ~0U; |
| 530 | while (size--) |
| 531 | crc = g_crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); |
| 532 | return crc ^ ~0U; |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 535 | static uint32_t calc_gnu_debuglink_crc32(const void *buf, size_t size) { |
| 536 | return calc_crc32(0U, buf, size); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 539 | uint32_t ObjectFileELF::CalculateELFNotesSegmentsCRC32( |
| 540 | const ProgramHeaderColl &program_headers, DataExtractor &object_data) { |
| 541 | typedef ProgramHeaderCollConstIter Iter; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 542 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 543 | uint32_t core_notes_crc = 0; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 544 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 545 | for (Iter I = program_headers.begin(); I != program_headers.end(); ++I) { |
| 546 | if (I->p_type == llvm::ELF::PT_NOTE) { |
| 547 | const elf_off ph_offset = I->p_offset; |
| 548 | const size_t ph_size = I->p_filesz; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 549 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 550 | DataExtractor segment_data; |
| 551 | if (segment_data.SetData(object_data, ph_offset, ph_size) != ph_size) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 552 | // The ELF program header contained incorrect data, probably corefile |
| 553 | // is incomplete or corrupted. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 554 | break; |
| 555 | } |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 556 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 557 | core_notes_crc = calc_crc32(core_notes_crc, segment_data.GetDataStart(), |
| 558 | segment_data.GetByteSize()); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 559 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 560 | } |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 561 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 562 | return core_notes_crc; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 565 | static const char *OSABIAsCString(unsigned char osabi_byte) { |
| 566 | #define _MAKE_OSABI_CASE(x) \ |
| 567 | case x: \ |
| 568 | return #x |
| 569 | switch (osabi_byte) { |
| 570 | _MAKE_OSABI_CASE(ELFOSABI_NONE); |
| 571 | _MAKE_OSABI_CASE(ELFOSABI_HPUX); |
| 572 | _MAKE_OSABI_CASE(ELFOSABI_NETBSD); |
| 573 | _MAKE_OSABI_CASE(ELFOSABI_GNU); |
| 574 | _MAKE_OSABI_CASE(ELFOSABI_HURD); |
| 575 | _MAKE_OSABI_CASE(ELFOSABI_SOLARIS); |
| 576 | _MAKE_OSABI_CASE(ELFOSABI_AIX); |
| 577 | _MAKE_OSABI_CASE(ELFOSABI_IRIX); |
| 578 | _MAKE_OSABI_CASE(ELFOSABI_FREEBSD); |
| 579 | _MAKE_OSABI_CASE(ELFOSABI_TRU64); |
| 580 | _MAKE_OSABI_CASE(ELFOSABI_MODESTO); |
| 581 | _MAKE_OSABI_CASE(ELFOSABI_OPENBSD); |
| 582 | _MAKE_OSABI_CASE(ELFOSABI_OPENVMS); |
| 583 | _MAKE_OSABI_CASE(ELFOSABI_NSK); |
| 584 | _MAKE_OSABI_CASE(ELFOSABI_AROS); |
| 585 | _MAKE_OSABI_CASE(ELFOSABI_FENIXOS); |
| 586 | _MAKE_OSABI_CASE(ELFOSABI_C6000_ELFABI); |
| 587 | _MAKE_OSABI_CASE(ELFOSABI_C6000_LINUX); |
| 588 | _MAKE_OSABI_CASE(ELFOSABI_ARM); |
| 589 | _MAKE_OSABI_CASE(ELFOSABI_STANDALONE); |
| 590 | default: |
| 591 | return "<unknown-osabi>"; |
| 592 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 593 | #undef _MAKE_OSABI_CASE |
| 594 | } |
| 595 | |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 596 | // |
| 597 | // WARNING : This function is being deprecated |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 598 | // It's functionality has moved to ArchSpec::SetArchitecture This function is |
| 599 | // only being kept to validate the move. |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 600 | // |
| 601 | // TODO : Remove this function |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | static bool GetOsFromOSABI(unsigned char osabi_byte, |
| 603 | llvm::Triple::OSType &ostype) { |
| 604 | switch (osabi_byte) { |
| 605 | case ELFOSABI_AIX: |
| 606 | ostype = llvm::Triple::OSType::AIX; |
| 607 | break; |
| 608 | case ELFOSABI_FREEBSD: |
| 609 | ostype = llvm::Triple::OSType::FreeBSD; |
| 610 | break; |
| 611 | case ELFOSABI_GNU: |
| 612 | ostype = llvm::Triple::OSType::Linux; |
| 613 | break; |
| 614 | case ELFOSABI_NETBSD: |
| 615 | ostype = llvm::Triple::OSType::NetBSD; |
| 616 | break; |
| 617 | case ELFOSABI_OPENBSD: |
| 618 | ostype = llvm::Triple::OSType::OpenBSD; |
| 619 | break; |
| 620 | case ELFOSABI_SOLARIS: |
| 621 | ostype = llvm::Triple::OSType::Solaris; |
| 622 | break; |
| 623 | default: |
| 624 | ostype = llvm::Triple::OSType::UnknownOS; |
| 625 | } |
| 626 | return ostype != llvm::Triple::OSType::UnknownOS; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | size_t ObjectFileELF::GetModuleSpecifications( |
| 630 | const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, |
| 631 | lldb::offset_t data_offset, lldb::offset_t file_offset, |
| 632 | lldb::offset_t length, lldb_private::ModuleSpecList &specs) { |
| 633 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 634 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 635 | const size_t initial_count = specs.GetSize(); |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 636 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 637 | if (ObjectFileELF::MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) { |
| 638 | DataExtractor data; |
| 639 | data.SetData(data_sp); |
| 640 | elf::ELFHeader header; |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 641 | lldb::offset_t header_offset = data_offset; |
| 642 | if (header.Parse(data, &header_offset)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 643 | if (data_sp) { |
| 644 | ModuleSpec spec(file); |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 645 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 646 | const uint32_t sub_type = subTypeFromElfHeader(header); |
| 647 | spec.GetArchitecture().SetArchitecture( |
| 648 | eArchTypeELF, header.e_machine, sub_type, header.e_ident[EI_OSABI]); |
Matthew Gardiner | 5f67579 | 2014-08-27 12:09:39 +0000 | [diff] [blame] | 649 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 650 | if (spec.GetArchitecture().IsValid()) { |
| 651 | llvm::Triple::OSType ostype; |
| 652 | llvm::Triple::VendorType vendor; |
| 653 | llvm::Triple::OSType spec_ostype = |
| 654 | spec.GetArchitecture().GetTriple().getOS(); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 655 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 656 | if (log) |
| 657 | log->Printf("ObjectFileELF::%s file '%s' module OSABI: %s", |
| 658 | __FUNCTION__, file.GetPath().c_str(), |
| 659 | OSABIAsCString(header.e_ident[EI_OSABI])); |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 660 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | // SetArchitecture should have set the vendor to unknown |
| 662 | vendor = spec.GetArchitecture().GetTriple().getVendor(); |
| 663 | assert(vendor == llvm::Triple::UnknownVendor); |
Hafiz Abid Qadeer | b155431 | 2017-01-20 10:24:03 +0000 | [diff] [blame] | 664 | UNUSED_IF_ASSERT_DISABLED(vendor); |
Ed Maste | f6a1312 | 2015-06-05 13:03:08 +0000 | [diff] [blame] | 665 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 666 | // |
| 667 | // Validate it is ok to remove GetOsFromOSABI |
| 668 | GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); |
| 669 | assert(spec_ostype == ostype); |
| 670 | if (spec_ostype != llvm::Triple::OSType::UnknownOS) { |
| 671 | if (log) |
| 672 | log->Printf("ObjectFileELF::%s file '%s' set ELF module OS type " |
| 673 | "from ELF header OSABI.", |
| 674 | __FUNCTION__, file.GetPath().c_str()); |
| 675 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 676 | |
Pavel Labath | 4f03312 | 2018-02-05 17:25:40 +0000 | [diff] [blame] | 677 | data_sp = MapFileData(file, -1, file_offset); |
| 678 | if (data_sp) |
| 679 | data.SetData(data_sp); |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 680 | // In case there is header extension in the section #0, the header we |
| 681 | // parsed above could have sentinel values for e_phnum, e_shnum, and |
| 682 | // e_shstrndx. In this case we need to reparse the header with a |
| 683 | // bigger data source to get the actual values. |
Pavel Labath | 4f03312 | 2018-02-05 17:25:40 +0000 | [diff] [blame] | 684 | if (header.HasHeaderExtension()) { |
| 685 | lldb::offset_t header_offset = data_offset; |
| 686 | header.Parse(data, &header_offset); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 687 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 689 | uint32_t gnu_debuglink_crc = 0; |
| 690 | std::string gnu_debuglink_file; |
| 691 | SectionHeaderColl section_headers; |
| 692 | lldb_private::UUID &uuid = spec.GetUUID(); |
Michael Sartain | 9f4517a | 2013-07-03 01:52:14 +0000 | [diff] [blame] | 693 | |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 694 | GetSectionHeaderInfo(section_headers, data, header, uuid, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 695 | gnu_debuglink_file, gnu_debuglink_crc, |
| 696 | spec.GetArchitecture()); |
Ravitheja Addepally | 15f89c4 | 2016-01-19 12:55:21 +0000 | [diff] [blame] | 697 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 698 | llvm::Triple &spec_triple = spec.GetArchitecture().GetTriple(); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 699 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 700 | if (log) |
| 701 | log->Printf("ObjectFileELF::%s file '%s' module set to triple: %s " |
| 702 | "(architecture %s)", |
| 703 | __FUNCTION__, file.GetPath().c_str(), |
| 704 | spec_triple.getTriple().c_str(), |
| 705 | spec.GetArchitecture().GetArchitectureName()); |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 706 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 707 | if (!uuid.IsValid()) { |
| 708 | uint32_t core_notes_crc = 0; |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 709 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 710 | if (!gnu_debuglink_crc) { |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 711 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 712 | lldb_private::Timer scoped_timer( |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame] | 713 | func_cat, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 714 | "Calculating module crc32 %s with size %" PRIu64 " KiB", |
| 715 | file.GetLastPathComponent().AsCString(), |
| 716 | (file.GetByteSize() - file_offset) / 1024); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 717 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 718 | // For core files - which usually don't happen to have a |
Zachary Turner | 3f4a4b3 | 2017-02-24 18:56:49 +0000 | [diff] [blame] | 719 | // gnu_debuglink, and are pretty bulky - calculating whole |
| 720 | // contents crc32 would be too much of luxury. Thus we will need |
| 721 | // to fallback to something simpler. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 722 | if (header.e_type == llvm::ELF::ET_CORE) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | ProgramHeaderColl program_headers; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 724 | GetProgramHeaderInfo(program_headers, data, header); |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 725 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 726 | core_notes_crc = |
| 727 | CalculateELFNotesSegmentsCRC32(program_headers, data); |
| 728 | } else { |
Pavel Labath | 4f03312 | 2018-02-05 17:25:40 +0000 | [diff] [blame] | 729 | gnu_debuglink_crc = calc_gnu_debuglink_crc32( |
| 730 | data.GetDataStart(), data.GetByteSize()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 731 | } |
| 732 | } |
| 733 | if (gnu_debuglink_crc) { |
| 734 | // Use 4 bytes of crc from the .gnu_debuglink section. |
| 735 | uint32_t uuidt[4] = {gnu_debuglink_crc, 0, 0, 0}; |
| 736 | uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 737 | } else if (core_notes_crc) { |
| 738 | // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 739 | // it look different form .gnu_debuglink crc followed by 4 bytes |
| 740 | // of note segments crc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 741 | uint32_t uuidt[4] = {g_core_uuid_magic, core_notes_crc, 0, 0}; |
| 742 | uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | specs.Append(spec); |
| 747 | } |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | return specs.GetSize() - initial_count; |
Greg Clayton | f4d6de6 | 2013-04-24 22:29:28 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 755 | //------------------------------------------------------------------ |
| 756 | // PluginInterface protocol |
| 757 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 758 | lldb_private::ConstString ObjectFileELF::GetPluginName() { |
| 759 | return GetPluginNameStatic(); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 760 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 761 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 762 | uint32_t ObjectFileELF::GetPluginVersion() { return m_plugin_version; } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 763 | //------------------------------------------------------------------ |
| 764 | // ObjectFile protocol |
| 765 | //------------------------------------------------------------------ |
| 766 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 767 | ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, |
| 768 | DataBufferSP &data_sp, lldb::offset_t data_offset, |
| 769 | const FileSpec *file, lldb::offset_t file_offset, |
| 770 | lldb::offset_t length) |
| 771 | : ObjectFile(module_sp, file, file_offset, length, data_sp, data_offset), |
| 772 | m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0), |
| 773 | m_program_headers(), m_section_headers(), m_dynamic_symbols(), |
| 774 | m_filespec_ap(), m_entry_point_address(), m_arch_spec() { |
| 775 | if (file) |
| 776 | m_file = *file; |
| 777 | ::memset(&m_header, 0, sizeof(m_header)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 780 | ObjectFileELF::ObjectFileELF(const lldb::ModuleSP &module_sp, |
| 781 | DataBufferSP &header_data_sp, |
| 782 | const lldb::ProcessSP &process_sp, |
| 783 | addr_t header_addr) |
| 784 | : ObjectFile(module_sp, process_sp, header_addr, header_data_sp), |
| 785 | m_header(), m_uuid(), m_gnu_debuglink_file(), m_gnu_debuglink_crc(0), |
| 786 | m_program_headers(), m_section_headers(), m_dynamic_symbols(), |
| 787 | m_filespec_ap(), m_entry_point_address(), m_arch_spec() { |
| 788 | ::memset(&m_header, 0, sizeof(m_header)); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 791 | ObjectFileELF::~ObjectFileELF() {} |
| 792 | |
| 793 | bool ObjectFileELF::IsExecutable() const { |
| 794 | return ((m_header.e_type & ET_EXEC) != 0) || (m_header.e_entry != 0); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 797 | bool ObjectFileELF::SetLoadAddress(Target &target, lldb::addr_t value, |
| 798 | bool value_is_offset) { |
| 799 | ModuleSP module_sp = GetModule(); |
| 800 | if (module_sp) { |
| 801 | size_t num_loaded_sections = 0; |
| 802 | SectionList *section_list = GetSectionList(); |
| 803 | if (section_list) { |
| 804 | if (!value_is_offset) { |
| 805 | bool found_offset = false; |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 806 | for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 807 | const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i); |
| 808 | if (header == nullptr) |
| 809 | continue; |
Jim Ingham | 5aee162 | 2010-08-09 23:31:02 +0000 | [diff] [blame] | 810 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 811 | if (header->p_type != PT_LOAD || header->p_offset != 0) |
| 812 | continue; |
Tamas Berghammer | f256184 | 2015-06-30 10:41:23 +0000 | [diff] [blame] | 813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | value = value - header->p_vaddr; |
| 815 | found_offset = true; |
| 816 | break; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 817 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 818 | if (!found_offset) |
| 819 | return false; |
| 820 | } |
| 821 | |
| 822 | const size_t num_sections = section_list->GetSize(); |
| 823 | size_t sect_idx = 0; |
| 824 | |
| 825 | for (sect_idx = 0; sect_idx < num_sections; ++sect_idx) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 826 | // Iterate through the object file sections to find all of the sections |
| 827 | // that have SHF_ALLOC in their flag bits. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 828 | SectionSP section_sp(section_list->GetSectionAtIndex(sect_idx)); |
| 829 | if (section_sp && section_sp->Test(SHF_ALLOC)) { |
Pavel Labath | ec03d7e | 2018-02-28 20:42:29 +0000 | [diff] [blame] | 830 | lldb::addr_t load_addr = section_sp->GetFileAddress(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 831 | // We don't want to update the load address of a section with type |
| 832 | // eSectionTypeAbsoluteAddress as they already have the absolute load |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 833 | // address already specified |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 834 | if (section_sp->GetType() != eSectionTypeAbsoluteAddress) |
| 835 | load_addr += value; |
| 836 | |
| 837 | // On 32-bit systems the load address have to fit into 4 bytes. The |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 838 | // rest of the bytes are the overflow from the addition. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 839 | if (GetAddressByteSize() == 4) |
| 840 | load_addr &= 0xFFFFFFFF; |
| 841 | |
| 842 | if (target.GetSectionLoadList().SetSectionLoadAddress(section_sp, |
| 843 | load_addr)) |
| 844 | ++num_loaded_sections; |
| 845 | } |
| 846 | } |
| 847 | return num_loaded_sections > 0; |
Steve Pucci | 9e02dac | 2014-02-06 19:02:19 +0000 | [diff] [blame] | 848 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 849 | } |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | ByteOrder ObjectFileELF::GetByteOrder() const { |
| 854 | if (m_header.e_ident[EI_DATA] == ELFDATA2MSB) |
| 855 | return eByteOrderBig; |
| 856 | if (m_header.e_ident[EI_DATA] == ELFDATA2LSB) |
| 857 | return eByteOrderLittle; |
| 858 | return eByteOrderInvalid; |
| 859 | } |
| 860 | |
| 861 | uint32_t ObjectFileELF::GetAddressByteSize() const { |
| 862 | return m_data.GetAddressByteSize(); |
| 863 | } |
| 864 | |
| 865 | AddressClass ObjectFileELF::GetAddressClass(addr_t file_addr) { |
| 866 | Symtab *symtab = GetSymtab(); |
| 867 | if (!symtab) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 868 | return AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 869 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 870 | // The address class is determined based on the symtab. Ask it from the |
| 871 | // object file what contains the symtab information. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 872 | ObjectFile *symtab_objfile = symtab->GetObjectFile(); |
| 873 | if (symtab_objfile != nullptr && symtab_objfile != this) |
| 874 | return symtab_objfile->GetAddressClass(file_addr); |
| 875 | |
| 876 | auto res = ObjectFile::GetAddressClass(file_addr); |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 877 | if (res != AddressClass::eCode) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 878 | return res; |
| 879 | |
| 880 | auto ub = m_address_class_map.upper_bound(file_addr); |
| 881 | if (ub == m_address_class_map.begin()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 882 | // No entry in the address class map before the address. Return default |
| 883 | // address class for an address in a code section. |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 884 | return AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | // Move iterator to the address class entry preceding address |
| 888 | --ub; |
| 889 | |
| 890 | return ub->second; |
| 891 | } |
| 892 | |
| 893 | size_t ObjectFileELF::SectionIndex(const SectionHeaderCollIter &I) { |
| 894 | return std::distance(m_section_headers.begin(), I) + 1u; |
| 895 | } |
| 896 | |
| 897 | size_t ObjectFileELF::SectionIndex(const SectionHeaderCollConstIter &I) const { |
| 898 | return std::distance(m_section_headers.begin(), I) + 1u; |
| 899 | } |
| 900 | |
| 901 | bool ObjectFileELF::ParseHeader() { |
| 902 | lldb::offset_t offset = 0; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 903 | return m_header.Parse(m_data, &offset); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 906 | bool ObjectFileELF::GetUUID(lldb_private::UUID *uuid) { |
| 907 | // Need to parse the section list to get the UUIDs, so make sure that's been |
| 908 | // done. |
| 909 | if (!ParseSectionHeaders() && GetType() != ObjectFile::eTypeCoreFile) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 910 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 911 | |
| 912 | if (m_uuid.IsValid()) { |
| 913 | // We have the full build id uuid. |
| 914 | *uuid = m_uuid; |
| 915 | return true; |
| 916 | } else if (GetType() == ObjectFile::eTypeCoreFile) { |
| 917 | uint32_t core_notes_crc = 0; |
| 918 | |
| 919 | if (!ParseProgramHeaders()) |
| 920 | return false; |
| 921 | |
| 922 | core_notes_crc = CalculateELFNotesSegmentsCRC32(m_program_headers, m_data); |
| 923 | |
| 924 | if (core_notes_crc) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 925 | // Use 8 bytes - first 4 bytes for *magic* prefix, mainly to make it look |
| 926 | // different form .gnu_debuglink crc - followed by 4 bytes of note |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 927 | // segments crc. |
| 928 | uint32_t uuidt[4] = {g_core_uuid_magic, core_notes_crc, 0, 0}; |
| 929 | m_uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 930 | } |
| 931 | } else { |
| 932 | if (!m_gnu_debuglink_crc) |
| 933 | m_gnu_debuglink_crc = |
| 934 | calc_gnu_debuglink_crc32(m_data.GetDataStart(), m_data.GetByteSize()); |
| 935 | if (m_gnu_debuglink_crc) { |
| 936 | // Use 4 bytes of crc from the .gnu_debuglink section. |
| 937 | uint32_t uuidt[4] = {m_gnu_debuglink_crc, 0, 0, 0}; |
| 938 | m_uuid.SetBytes(uuidt, sizeof(uuidt)); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | if (m_uuid.IsValid()) { |
| 943 | *uuid = m_uuid; |
| 944 | return true; |
| 945 | } |
| 946 | |
| 947 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 948 | } |
| 949 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 950 | lldb_private::FileSpecList ObjectFileELF::GetDebugSymbolFilePaths() { |
| 951 | FileSpecList file_spec_list; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 952 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | if (!m_gnu_debuglink_file.empty()) { |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 954 | FileSpec file_spec(m_gnu_debuglink_file, false); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 955 | file_spec_list.Append(file_spec); |
| 956 | } |
| 957 | return file_spec_list; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 960 | uint32_t ObjectFileELF::GetDependentModules(FileSpecList &files) { |
| 961 | size_t num_modules = ParseDependentModules(); |
| 962 | uint32_t num_specs = 0; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 963 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 964 | for (unsigned i = 0; i < num_modules; ++i) { |
| 965 | if (files.AppendIfUnique(m_filespec_ap->GetFileSpecAtIndex(i))) |
| 966 | num_specs++; |
| 967 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 968 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 969 | return num_specs; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 972 | Address ObjectFileELF::GetImageInfoAddress(Target *target) { |
| 973 | if (!ParseDynamicSymbols()) |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 974 | return Address(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 975 | |
| 976 | SectionList *section_list = GetSectionList(); |
| 977 | if (!section_list) |
| 978 | return Address(); |
| 979 | |
| 980 | // Find the SHT_DYNAMIC (.dynamic) section. |
| 981 | SectionSP dynsym_section_sp( |
| 982 | section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true)); |
| 983 | if (!dynsym_section_sp) |
| 984 | return Address(); |
| 985 | assert(dynsym_section_sp->GetObjectFile() == this); |
| 986 | |
| 987 | user_id_t dynsym_id = dynsym_section_sp->GetID(); |
| 988 | const ELFSectionHeaderInfo *dynsym_hdr = GetSectionHeaderByIndex(dynsym_id); |
| 989 | if (!dynsym_hdr) |
| 990 | return Address(); |
| 991 | |
| 992 | for (size_t i = 0; i < m_dynamic_symbols.size(); ++i) { |
| 993 | ELFDynamic &symbol = m_dynamic_symbols[i]; |
| 994 | |
| 995 | if (symbol.d_tag == DT_DEBUG) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 996 | // Compute the offset as the number of previous entries plus the size of |
| 997 | // d_tag. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 998 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 999 | return Address(dynsym_section_sp, offset); |
| 1000 | } |
| 1001 | // MIPS executables uses DT_MIPS_RLD_MAP_REL to support PIE. DT_MIPS_RLD_MAP |
| 1002 | // exists in non-PIE. |
| 1003 | else if ((symbol.d_tag == DT_MIPS_RLD_MAP || |
| 1004 | symbol.d_tag == DT_MIPS_RLD_MAP_REL) && |
| 1005 | target) { |
| 1006 | addr_t offset = i * dynsym_hdr->sh_entsize + GetAddressByteSize(); |
| 1007 | addr_t dyn_base = dynsym_section_sp->GetLoadBaseAddress(target); |
| 1008 | if (dyn_base == LLDB_INVALID_ADDRESS) |
| 1009 | return Address(); |
| 1010 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1011 | Status error; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1012 | if (symbol.d_tag == DT_MIPS_RLD_MAP) { |
| 1013 | // DT_MIPS_RLD_MAP tag stores an absolute address of the debug pointer. |
| 1014 | Address addr; |
| 1015 | if (target->ReadPointerFromMemory(dyn_base + offset, false, error, |
| 1016 | addr)) |
| 1017 | return addr; |
| 1018 | } |
| 1019 | if (symbol.d_tag == DT_MIPS_RLD_MAP_REL) { |
| 1020 | // DT_MIPS_RLD_MAP_REL tag stores the offset to the debug pointer, |
| 1021 | // relative to the address of the tag. |
| 1022 | uint64_t rel_offset; |
| 1023 | rel_offset = target->ReadUnsignedIntegerFromMemory( |
| 1024 | dyn_base + offset, false, GetAddressByteSize(), UINT64_MAX, error); |
| 1025 | if (error.Success() && rel_offset != UINT64_MAX) { |
| 1026 | Address addr; |
| 1027 | addr_t debug_ptr_address = |
| 1028 | dyn_base + (offset - GetAddressByteSize()) + rel_offset; |
| 1029 | addr.SetOffset(debug_ptr_address); |
| 1030 | return addr; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | return Address(); |
Stephen Wilson | 2ab0a58 | 2011-01-15 00:08:44 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1039 | lldb_private::Address ObjectFileELF::GetEntryPointAddress() { |
| 1040 | if (m_entry_point_address.IsValid()) |
Stephen Wilson | d126c8c | 2011-03-08 04:12:15 +0000 | [diff] [blame] | 1041 | return m_entry_point_address; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1042 | |
| 1043 | if (!ParseHeader() || !IsExecutable()) |
| 1044 | return m_entry_point_address; |
| 1045 | |
| 1046 | SectionList *section_list = GetSectionList(); |
| 1047 | addr_t offset = m_header.e_entry; |
| 1048 | |
| 1049 | if (!section_list) |
| 1050 | m_entry_point_address.SetOffset(offset); |
| 1051 | else |
| 1052 | m_entry_point_address.ResolveAddressUsingFileSections(offset, section_list); |
| 1053 | return m_entry_point_address; |
Jim Ingham | 672e6f5 | 2011-03-07 23:44:08 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1056 | //---------------------------------------------------------------------- |
| 1057 | // ParseDependentModules |
| 1058 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1059 | size_t ObjectFileELF::ParseDependentModules() { |
| 1060 | if (m_filespec_ap.get()) |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 1061 | return m_filespec_ap->GetSize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1062 | |
| 1063 | m_filespec_ap.reset(new FileSpecList()); |
| 1064 | |
| 1065 | if (!ParseSectionHeaders()) |
| 1066 | return 0; |
| 1067 | |
| 1068 | SectionList *section_list = GetSectionList(); |
| 1069 | if (!section_list) |
| 1070 | return 0; |
| 1071 | |
| 1072 | // Find the SHT_DYNAMIC section. |
| 1073 | Section *dynsym = |
| 1074 | section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) |
| 1075 | .get(); |
| 1076 | if (!dynsym) |
| 1077 | return 0; |
| 1078 | assert(dynsym->GetObjectFile() == this); |
| 1079 | |
| 1080 | const ELFSectionHeaderInfo *header = GetSectionHeaderByIndex(dynsym->GetID()); |
| 1081 | if (!header) |
| 1082 | return 0; |
| 1083 | // sh_link: section header index of string table used by entries in the |
| 1084 | // section. |
| 1085 | Section *dynstr = section_list->FindSectionByID(header->sh_link + 1).get(); |
| 1086 | if (!dynstr) |
| 1087 | return 0; |
| 1088 | |
| 1089 | DataExtractor dynsym_data; |
| 1090 | DataExtractor dynstr_data; |
| 1091 | if (ReadSectionData(dynsym, dynsym_data) && |
| 1092 | ReadSectionData(dynstr, dynstr_data)) { |
| 1093 | ELFDynamic symbol; |
| 1094 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 1095 | lldb::offset_t offset = 0; |
| 1096 | |
| 1097 | // The only type of entries we are concerned with are tagged DT_NEEDED, |
| 1098 | // yielding the name of a required library. |
| 1099 | while (offset < section_size) { |
| 1100 | if (!symbol.Parse(dynsym_data, &offset)) |
| 1101 | break; |
| 1102 | |
| 1103 | if (symbol.d_tag != DT_NEEDED) |
| 1104 | continue; |
| 1105 | |
| 1106 | uint32_t str_index = static_cast<uint32_t>(symbol.d_val); |
| 1107 | const char *lib_name = dynstr_data.PeekCStr(str_index); |
| 1108 | m_filespec_ap->Append(FileSpec(lib_name, true)); |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | return m_filespec_ap->GetSize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | //---------------------------------------------------------------------- |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 1116 | // GetProgramHeaderInfo |
| 1117 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1118 | size_t ObjectFileELF::GetProgramHeaderInfo(ProgramHeaderColl &program_headers, |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1119 | DataExtractor &object_data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | const ELFHeader &header) { |
| 1121 | // We have already parsed the program headers |
| 1122 | if (!program_headers.empty()) |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 1123 | return program_headers.size(); |
| 1124 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1125 | // If there are no program headers to read we are done. |
| 1126 | if (header.e_phnum == 0) |
| 1127 | return 0; |
| 1128 | |
| 1129 | program_headers.resize(header.e_phnum); |
| 1130 | if (program_headers.size() != header.e_phnum) |
| 1131 | return 0; |
| 1132 | |
| 1133 | const size_t ph_size = header.e_phnum * header.e_phentsize; |
| 1134 | const elf_off ph_offset = header.e_phoff; |
| 1135 | DataExtractor data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1136 | if (data.SetData(object_data, ph_offset, ph_size) != ph_size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1137 | return 0; |
| 1138 | |
| 1139 | uint32_t idx; |
| 1140 | lldb::offset_t offset; |
| 1141 | for (idx = 0, offset = 0; idx < header.e_phnum; ++idx) { |
| 1142 | if (program_headers[idx].Parse(data, &offset) == false) |
| 1143 | break; |
| 1144 | } |
| 1145 | |
| 1146 | if (idx < program_headers.size()) |
| 1147 | program_headers.resize(idx); |
| 1148 | |
| 1149 | return program_headers.size(); |
Todd Fiala | 4339f3a | 2014-03-25 19:29:09 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1153 | // ParseProgramHeaders |
| 1154 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1155 | size_t ObjectFileELF::ParseProgramHeaders() { |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1156 | return GetProgramHeaderInfo(m_program_headers, m_data, m_header); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1159 | lldb_private::Status |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1160 | ObjectFileELF::RefineModuleDetailsFromNote(lldb_private::DataExtractor &data, |
| 1161 | lldb_private::ArchSpec &arch_spec, |
| 1162 | lldb_private::UUID &uuid) { |
| 1163 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1164 | Status error; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1165 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1166 | lldb::offset_t offset = 0; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1168 | while (true) { |
| 1169 | // Parse the note header. If this fails, bail out. |
| 1170 | const lldb::offset_t note_offset = offset; |
| 1171 | ELFNote note = ELFNote(); |
| 1172 | if (!note.Parse(data, &offset)) { |
| 1173 | // We're done. |
| 1174 | return error; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1175 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1177 | if (log) |
| 1178 | log->Printf("ObjectFileELF::%s parsing note name='%s', type=%" PRIu32, |
| 1179 | __FUNCTION__, note.n_name.c_str(), note.n_type); |
| 1180 | |
| 1181 | // Process FreeBSD ELF notes. |
| 1182 | if ((note.n_name == LLDB_NT_OWNER_FREEBSD) && |
| 1183 | (note.n_type == LLDB_NT_FREEBSD_ABI_TAG) && |
| 1184 | (note.n_descsz == LLDB_NT_FREEBSD_ABI_SIZE)) { |
| 1185 | // Pull out the min version info. |
| 1186 | uint32_t version_info; |
| 1187 | if (data.GetU32(&offset, &version_info, 1) == nullptr) { |
| 1188 | error.SetErrorString("failed to read FreeBSD ABI note payload"); |
| 1189 | return error; |
| 1190 | } |
| 1191 | |
| 1192 | // Convert the version info into a major/minor number. |
| 1193 | const uint32_t version_major = version_info / 100000; |
| 1194 | const uint32_t version_minor = (version_info / 1000) % 100; |
| 1195 | |
| 1196 | char os_name[32]; |
| 1197 | snprintf(os_name, sizeof(os_name), "freebsd%" PRIu32 ".%" PRIu32, |
| 1198 | version_major, version_minor); |
| 1199 | |
| 1200 | // Set the elf OS version to FreeBSD. Also clear the vendor. |
| 1201 | arch_spec.GetTriple().setOSName(os_name); |
| 1202 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 1203 | |
| 1204 | if (log) |
| 1205 | log->Printf("ObjectFileELF::%s detected FreeBSD %" PRIu32 ".%" PRIu32 |
| 1206 | ".%" PRIu32, |
| 1207 | __FUNCTION__, version_major, version_minor, |
| 1208 | static_cast<uint32_t>(version_info % 1000)); |
| 1209 | } |
| 1210 | // Process GNU ELF notes. |
| 1211 | else if (note.n_name == LLDB_NT_OWNER_GNU) { |
| 1212 | switch (note.n_type) { |
| 1213 | case LLDB_NT_GNU_ABI_TAG: |
| 1214 | if (note.n_descsz == LLDB_NT_GNU_ABI_SIZE) { |
| 1215 | // Pull out the min OS version supporting the ABI. |
| 1216 | uint32_t version_info[4]; |
| 1217 | if (data.GetU32(&offset, &version_info[0], note.n_descsz / 4) == |
| 1218 | nullptr) { |
| 1219 | error.SetErrorString("failed to read GNU ABI note payload"); |
| 1220 | return error; |
| 1221 | } |
| 1222 | |
| 1223 | // Set the OS per the OS field. |
| 1224 | switch (version_info[0]) { |
| 1225 | case LLDB_NT_GNU_ABI_OS_LINUX: |
| 1226 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1227 | arch_spec.GetTriple().setVendor( |
| 1228 | llvm::Triple::VendorType::UnknownVendor); |
| 1229 | if (log) |
| 1230 | log->Printf( |
| 1231 | "ObjectFileELF::%s detected Linux, min version %" PRIu32 |
| 1232 | ".%" PRIu32 ".%" PRIu32, |
| 1233 | __FUNCTION__, version_info[1], version_info[2], |
| 1234 | version_info[3]); |
| 1235 | // FIXME we have the minimal version number, we could be propagating |
| 1236 | // that. version_info[1] = OS Major, version_info[2] = OS Minor, |
| 1237 | // version_info[3] = Revision. |
| 1238 | break; |
| 1239 | case LLDB_NT_GNU_ABI_OS_HURD: |
| 1240 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); |
| 1241 | arch_spec.GetTriple().setVendor( |
| 1242 | llvm::Triple::VendorType::UnknownVendor); |
| 1243 | if (log) |
| 1244 | log->Printf("ObjectFileELF::%s detected Hurd (unsupported), min " |
| 1245 | "version %" PRIu32 ".%" PRIu32 ".%" PRIu32, |
| 1246 | __FUNCTION__, version_info[1], version_info[2], |
| 1247 | version_info[3]); |
| 1248 | break; |
| 1249 | case LLDB_NT_GNU_ABI_OS_SOLARIS: |
| 1250 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Solaris); |
| 1251 | arch_spec.GetTriple().setVendor( |
| 1252 | llvm::Triple::VendorType::UnknownVendor); |
| 1253 | if (log) |
| 1254 | log->Printf( |
| 1255 | "ObjectFileELF::%s detected Solaris, min version %" PRIu32 |
| 1256 | ".%" PRIu32 ".%" PRIu32, |
| 1257 | __FUNCTION__, version_info[1], version_info[2], |
| 1258 | version_info[3]); |
| 1259 | break; |
| 1260 | default: |
| 1261 | if (log) |
| 1262 | log->Printf( |
| 1263 | "ObjectFileELF::%s unrecognized OS in note, id %" PRIu32 |
| 1264 | ", min version %" PRIu32 ".%" PRIu32 ".%" PRIu32, |
| 1265 | __FUNCTION__, version_info[0], version_info[1], |
| 1266 | version_info[2], version_info[3]); |
| 1267 | break; |
| 1268 | } |
| 1269 | } |
| 1270 | break; |
| 1271 | |
| 1272 | case LLDB_NT_GNU_BUILD_ID_TAG: |
| 1273 | // Only bother processing this if we don't already have the uuid set. |
| 1274 | if (!uuid.IsValid()) { |
| 1275 | // 16 bytes is UUID|MD5, 20 bytes is SHA1. Other linkers may produce a |
| 1276 | // build-id of a different |
| 1277 | // length. Accept it as long as it's at least 4 bytes as it will be |
| 1278 | // better than our own crc32. |
| 1279 | if (note.n_descsz >= 4 && note.n_descsz <= 20) { |
| 1280 | uint8_t uuidbuf[20]; |
| 1281 | if (data.GetU8(&offset, &uuidbuf, note.n_descsz) == nullptr) { |
| 1282 | error.SetErrorString("failed to read GNU_BUILD_ID note payload"); |
| 1283 | return error; |
| 1284 | } |
| 1285 | |
| 1286 | // Save the build id as the UUID for the module. |
| 1287 | uuid.SetBytes(uuidbuf, note.n_descsz); |
| 1288 | } |
| 1289 | } |
| 1290 | break; |
| 1291 | } |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 1292 | if (arch_spec.IsMIPS() && |
| 1293 | arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) |
| 1294 | // The note.n_name == LLDB_NT_OWNER_GNU is valid for Linux platform |
| 1295 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1296 | } |
| 1297 | // Process NetBSD ELF notes. |
| 1298 | else if ((note.n_name == LLDB_NT_OWNER_NETBSD) && |
| 1299 | (note.n_type == LLDB_NT_NETBSD_ABI_TAG) && |
| 1300 | (note.n_descsz == LLDB_NT_NETBSD_ABI_SIZE)) { |
| 1301 | // Pull out the min version info. |
| 1302 | uint32_t version_info; |
| 1303 | if (data.GetU32(&offset, &version_info, 1) == nullptr) { |
| 1304 | error.SetErrorString("failed to read NetBSD ABI note payload"); |
| 1305 | return error; |
| 1306 | } |
| 1307 | |
| 1308 | // Set the elf OS version to NetBSD. Also clear the vendor. |
| 1309 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::NetBSD); |
| 1310 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 1311 | |
| 1312 | if (log) |
| 1313 | log->Printf( |
| 1314 | "ObjectFileELF::%s detected NetBSD, min version constant %" PRIu32, |
| 1315 | __FUNCTION__, version_info); |
| 1316 | } |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 1317 | // Process OpenBSD ELF notes. |
| 1318 | else if (note.n_name == LLDB_NT_OWNER_OPENBSD) { |
| 1319 | // Set the elf OS version to OpenBSD. Also clear the vendor. |
| 1320 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::OpenBSD); |
| 1321 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::UnknownVendor); |
| 1322 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1323 | // Process CSR kalimba notes |
| 1324 | else if ((note.n_type == LLDB_NT_GNU_ABI_TAG) && |
| 1325 | (note.n_name == LLDB_NT_OWNER_CSR)) { |
| 1326 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::UnknownOS); |
| 1327 | arch_spec.GetTriple().setVendor(llvm::Triple::VendorType::CSR); |
| 1328 | |
| 1329 | // TODO At some point the description string could be processed. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1330 | // It could provide a steer towards the kalimba variant which this ELF |
| 1331 | // targets. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1332 | if (note.n_descsz) { |
| 1333 | const char *cstr = |
| 1334 | data.GetCStr(&offset, llvm::alignTo(note.n_descsz, 4)); |
| 1335 | (void)cstr; |
| 1336 | } |
| 1337 | } else if (note.n_name == LLDB_NT_OWNER_ANDROID) { |
| 1338 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1339 | arch_spec.GetTriple().setEnvironment( |
| 1340 | llvm::Triple::EnvironmentType::Android); |
| 1341 | } else if (note.n_name == LLDB_NT_OWNER_LINUX) { |
| 1342 | // This is sometimes found in core files and usually contains extended |
| 1343 | // register info |
| 1344 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1345 | } else if (note.n_name == LLDB_NT_OWNER_CORE) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1346 | // Parse the NT_FILE to look for stuff in paths to shared libraries As |
| 1347 | // the contents look like this in a 64 bit ELF core file: count = |
| 1348 | // 0x000000000000000a (10) page_size = 0x0000000000001000 (4096) Index |
| 1349 | // start end file_ofs path ===== |
| 1350 | // ------------------ ------------------ ------------------ |
| 1351 | // ------------------------------------- [ 0] 0x0000000000400000 |
| 1352 | // 0x0000000000401000 0x0000000000000000 /tmp/a.out [ 1] |
| 1353 | // 0x0000000000600000 0x0000000000601000 0x0000000000000000 /tmp/a.out [ |
| 1354 | // 2] 0x0000000000601000 0x0000000000602000 0x0000000000000001 /tmp/a.out |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1355 | // [ 3] 0x00007fa79c9ed000 0x00007fa79cba8000 0x0000000000000000 |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1356 | // /lib/x86_64-linux-gnu/libc-2.19.so [ 4] 0x00007fa79cba8000 |
| 1357 | // 0x00007fa79cda7000 0x00000000000001bb /lib/x86_64-linux- |
| 1358 | // gnu/libc-2.19.so [ 5] 0x00007fa79cda7000 0x00007fa79cdab000 |
| 1359 | // 0x00000000000001ba /lib/x86_64-linux-gnu/libc-2.19.so [ 6] |
| 1360 | // 0x00007fa79cdab000 0x00007fa79cdad000 0x00000000000001be /lib/x86_64 |
| 1361 | // -linux-gnu/libc-2.19.so [ 7] 0x00007fa79cdb2000 0x00007fa79cdd5000 |
| 1362 | // 0x0000000000000000 /lib/x86_64-linux-gnu/ld-2.19.so [ 8] |
| 1363 | // 0x00007fa79cfd4000 0x00007fa79cfd5000 0x0000000000000022 /lib/x86_64 |
| 1364 | // -linux-gnu/ld-2.19.so [ 9] 0x00007fa79cfd5000 0x00007fa79cfd6000 |
| 1365 | // 0x0000000000000023 /lib/x86_64-linux-gnu/ld-2.19.so In the 32 bit ELFs |
| 1366 | // the count, page_size, start, end, file_ofs are uint32_t For reference: |
| 1367 | // see readelf source code (in binutils). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1368 | if (note.n_type == NT_FILE) { |
| 1369 | uint64_t count = data.GetAddress(&offset); |
| 1370 | const char *cstr; |
| 1371 | data.GetAddress(&offset); // Skip page size |
| 1372 | offset += count * 3 * |
| 1373 | data.GetAddressByteSize(); // Skip all start/end/file_ofs |
| 1374 | for (size_t i = 0; i < count; ++i) { |
| 1375 | cstr = data.GetCStr(&offset); |
| 1376 | if (cstr == nullptr) { |
| 1377 | error.SetErrorStringWithFormat("ObjectFileELF::%s trying to read " |
| 1378 | "at an offset after the end " |
| 1379 | "(GetCStr returned nullptr)", |
| 1380 | __FUNCTION__); |
| 1381 | return error; |
| 1382 | } |
| 1383 | llvm::StringRef path(cstr); |
Richard Chamberlain | a0c82e1 | 2016-10-13 12:11:00 +0000 | [diff] [blame] | 1384 | if (path.contains("/lib/x86_64-linux-gnu") || path.contains("/lib/i386-linux-gnu")) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1385 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
| 1386 | break; |
| 1387 | } |
| 1388 | } |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 1389 | if (arch_spec.IsMIPS() && |
| 1390 | arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1391 | // In case of MIPSR6, the LLDB_NT_OWNER_GNU note is missing for some |
| 1392 | // cases (e.g. compile with -nostdlib) Hence set OS to Linux |
Nitesh Jain | 706c520 | 2017-03-31 11:06:25 +0000 | [diff] [blame] | 1393 | arch_spec.GetTriple().setOS(llvm::Triple::OSType::Linux); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1397 | // Calculate the offset of the next note just in case "offset" has been |
| 1398 | // used to poke at the contents of the note data |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1399 | offset = note_offset + note.GetByteSize(); |
| 1400 | } |
| 1401 | |
| 1402 | return error; |
Michael Sartain | c836ae7 | 2013-05-23 20:57:03 +0000 | [diff] [blame] | 1403 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1404 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1405 | void ObjectFileELF::ParseARMAttributes(DataExtractor &data, uint64_t length, |
| 1406 | ArchSpec &arch_spec) { |
| 1407 | lldb::offset_t Offset = 0; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1408 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1409 | uint8_t FormatVersion = data.GetU8(&Offset); |
| 1410 | if (FormatVersion != llvm::ARMBuildAttrs::Format_Version) |
| 1411 | return; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1412 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1413 | Offset = Offset + sizeof(uint32_t); // Section Length |
| 1414 | llvm::StringRef VendorName = data.GetCStr(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1415 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1416 | if (VendorName != "aeabi") |
| 1417 | return; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1418 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1419 | if (arch_spec.GetTriple().getEnvironment() == |
| 1420 | llvm::Triple::UnknownEnvironment) |
| 1421 | arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1422 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1423 | while (Offset < length) { |
| 1424 | uint8_t Tag = data.GetU8(&Offset); |
| 1425 | uint32_t Size = data.GetU32(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1426 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1427 | if (Tag != llvm::ARMBuildAttrs::File || Size == 0) |
| 1428 | continue; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1430 | while (Offset < length) { |
| 1431 | uint64_t Tag = data.GetULEB128(&Offset); |
| 1432 | switch (Tag) { |
| 1433 | default: |
| 1434 | if (Tag < 32) |
| 1435 | data.GetULEB128(&Offset); |
| 1436 | else if (Tag % 2 == 0) |
| 1437 | data.GetULEB128(&Offset); |
| 1438 | else |
| 1439 | data.GetCStr(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1441 | break; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1442 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1443 | case llvm::ARMBuildAttrs::CPU_raw_name: |
| 1444 | case llvm::ARMBuildAttrs::CPU_name: |
| 1445 | data.GetCStr(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1446 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1447 | break; |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1448 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1449 | case llvm::ARMBuildAttrs::ABI_VFP_args: { |
| 1450 | uint64_t VFPArgs = data.GetULEB128(&Offset); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1451 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1452 | if (VFPArgs == llvm::ARMBuildAttrs::BaseAAPCS) { |
| 1453 | if (arch_spec.GetTriple().getEnvironment() == |
| 1454 | llvm::Triple::UnknownEnvironment || |
| 1455 | arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABIHF) |
| 1456 | arch_spec.GetTriple().setEnvironment(llvm::Triple::EABI); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1457 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1458 | arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); |
| 1459 | } else if (VFPArgs == llvm::ARMBuildAttrs::HardFPAAPCS) { |
| 1460 | if (arch_spec.GetTriple().getEnvironment() == |
| 1461 | llvm::Triple::UnknownEnvironment || |
| 1462 | arch_spec.GetTriple().getEnvironment() == llvm::Triple::EABI) |
| 1463 | arch_spec.GetTriple().setEnvironment(llvm::Triple::EABIHF); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1464 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1465 | arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1466 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1467 | |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1471 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1472 | } |
Saleem Abdulrasool | d2d1504 | 2016-04-23 16:00:15 +0000 | [diff] [blame] | 1473 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1474 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1475 | //---------------------------------------------------------------------- |
| 1476 | // GetSectionHeaderInfo |
| 1477 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1478 | size_t ObjectFileELF::GetSectionHeaderInfo(SectionHeaderColl §ion_headers, |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1479 | DataExtractor &object_data, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1480 | const elf::ELFHeader &header, |
| 1481 | lldb_private::UUID &uuid, |
| 1482 | std::string &gnu_debuglink_file, |
| 1483 | uint32_t &gnu_debuglink_crc, |
| 1484 | ArchSpec &arch_spec) { |
| 1485 | // Don't reparse the section headers if we already did that. |
| 1486 | if (!section_headers.empty()) |
| 1487 | return section_headers.size(); |
Todd Fiala | 6477ea8 | 2014-07-11 15:13:33 +0000 | [diff] [blame] | 1488 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1489 | // Only initialize the arch_spec to okay defaults if they're not already set. |
| 1490 | // We'll refine this with note data as we parse the notes. |
| 1491 | if (arch_spec.GetTriple().getOS() == llvm::Triple::OSType::UnknownOS) { |
| 1492 | llvm::Triple::OSType ostype; |
| 1493 | llvm::Triple::OSType spec_ostype; |
| 1494 | const uint32_t sub_type = subTypeFromElfHeader(header); |
| 1495 | arch_spec.SetArchitecture(eArchTypeELF, header.e_machine, sub_type, |
| 1496 | header.e_ident[EI_OSABI]); |
Alexander Shaposhnikov | a08ba42 | 2016-12-05 18:42:21 +0000 | [diff] [blame] | 1497 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1498 | // Validate if it is ok to remove GetOsFromOSABI. Note, that now the OS is |
| 1499 | // determined based on EI_OSABI flag and the info extracted from ELF notes |
| 1500 | // (see RefineModuleDetailsFromNote). However in some cases that still |
| 1501 | // might be not enough: for example a shared library might not have any |
| 1502 | // notes at all and have EI_OSABI flag set to System V, as result the OS |
| 1503 | // will be set to UnknownOS. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1504 | GetOsFromOSABI(header.e_ident[EI_OSABI], ostype); |
| 1505 | spec_ostype = arch_spec.GetTriple().getOS(); |
| 1506 | assert(spec_ostype == ostype); |
Hafiz Abid Qadeer | b155431 | 2017-01-20 10:24:03 +0000 | [diff] [blame] | 1507 | UNUSED_IF_ASSERT_DISABLED(spec_ostype); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
| 1510 | if (arch_spec.GetMachine() == llvm::Triple::mips || |
| 1511 | arch_spec.GetMachine() == llvm::Triple::mipsel || |
| 1512 | arch_spec.GetMachine() == llvm::Triple::mips64 || |
| 1513 | arch_spec.GetMachine() == llvm::Triple::mips64el) { |
| 1514 | switch (header.e_flags & llvm::ELF::EF_MIPS_ARCH_ASE) { |
| 1515 | case llvm::ELF::EF_MIPS_MICROMIPS: |
| 1516 | arch_spec.SetFlags(ArchSpec::eMIPSAse_micromips); |
| 1517 | break; |
| 1518 | case llvm::ELF::EF_MIPS_ARCH_ASE_M16: |
| 1519 | arch_spec.SetFlags(ArchSpec::eMIPSAse_mips16); |
| 1520 | break; |
| 1521 | case llvm::ELF::EF_MIPS_ARCH_ASE_MDMX: |
| 1522 | arch_spec.SetFlags(ArchSpec::eMIPSAse_mdmx); |
| 1523 | break; |
| 1524 | default: |
| 1525 | break; |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1526 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1527 | } |
Todd Fiala | b91de78 | 2014-06-27 16:52:49 +0000 | [diff] [blame] | 1528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1529 | if (arch_spec.GetMachine() == llvm::Triple::arm || |
| 1530 | arch_spec.GetMachine() == llvm::Triple::thumb) { |
| 1531 | if (header.e_flags & llvm::ELF::EF_ARM_SOFT_FLOAT) |
| 1532 | arch_spec.SetFlags(ArchSpec::eARM_abi_soft_float); |
| 1533 | else if (header.e_flags & llvm::ELF::EF_ARM_VFP_FLOAT) |
| 1534 | arch_spec.SetFlags(ArchSpec::eARM_abi_hard_float); |
| 1535 | } |
Jaydeep Patil | 501a781 | 2015-07-16 03:51:55 +0000 | [diff] [blame] | 1536 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1537 | // If there are no section headers we are done. |
| 1538 | if (header.e_shnum == 0) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1539 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1540 | |
| 1541 | Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES)); |
| 1542 | |
| 1543 | section_headers.resize(header.e_shnum); |
| 1544 | if (section_headers.size() != header.e_shnum) |
| 1545 | return 0; |
| 1546 | |
| 1547 | const size_t sh_size = header.e_shnum * header.e_shentsize; |
| 1548 | const elf_off sh_offset = header.e_shoff; |
| 1549 | DataExtractor sh_data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1550 | if (sh_data.SetData(object_data, sh_offset, sh_size) != sh_size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1551 | return 0; |
| 1552 | |
| 1553 | uint32_t idx; |
| 1554 | lldb::offset_t offset; |
| 1555 | for (idx = 0, offset = 0; idx < header.e_shnum; ++idx) { |
| 1556 | if (section_headers[idx].Parse(sh_data, &offset) == false) |
| 1557 | break; |
| 1558 | } |
| 1559 | if (idx < section_headers.size()) |
| 1560 | section_headers.resize(idx); |
| 1561 | |
| 1562 | const unsigned strtab_idx = header.e_shstrndx; |
| 1563 | if (strtab_idx && strtab_idx < section_headers.size()) { |
| 1564 | const ELFSectionHeaderInfo &sheader = section_headers[strtab_idx]; |
| 1565 | const size_t byte_size = sheader.sh_size; |
| 1566 | const Elf64_Off offset = sheader.sh_offset; |
| 1567 | lldb_private::DataExtractor shstr_data; |
| 1568 | |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1569 | if (shstr_data.SetData(object_data, offset, byte_size) == byte_size) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1570 | for (SectionHeaderCollIter I = section_headers.begin(); |
| 1571 | I != section_headers.end(); ++I) { |
| 1572 | static ConstString g_sect_name_gnu_debuglink(".gnu_debuglink"); |
| 1573 | const ELFSectionHeaderInfo &sheader = *I; |
| 1574 | const uint64_t section_size = |
| 1575 | sheader.sh_type == SHT_NOBITS ? 0 : sheader.sh_size; |
| 1576 | ConstString name(shstr_data.PeekCStr(I->sh_name)); |
| 1577 | |
| 1578 | I->section_name = name; |
| 1579 | |
| 1580 | if (arch_spec.IsMIPS()) { |
| 1581 | uint32_t arch_flags = arch_spec.GetFlags(); |
| 1582 | DataExtractor data; |
| 1583 | if (sheader.sh_type == SHT_MIPS_ABIFLAGS) { |
| 1584 | |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1585 | if (section_size && (data.SetData(object_data, sheader.sh_offset, |
| 1586 | section_size) == section_size)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1587 | // MIPS ASE Mask is at offset 12 in MIPS.abiflags section |
| 1588 | lldb::offset_t offset = 12; // MIPS ABI Flags Version: 0 |
| 1589 | arch_flags |= data.GetU32(&offset); |
| 1590 | |
| 1591 | // The floating point ABI is at offset 7 |
| 1592 | offset = 7; |
| 1593 | switch (data.GetU8(&offset)) { |
| 1594 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_ANY: |
| 1595 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_ANY; |
| 1596 | break; |
| 1597 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_DOUBLE: |
| 1598 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_DOUBLE; |
| 1599 | break; |
| 1600 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_SINGLE: |
| 1601 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SINGLE; |
| 1602 | break; |
| 1603 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_SOFT: |
| 1604 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_SOFT; |
| 1605 | break; |
| 1606 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_OLD_64: |
| 1607 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_OLD_64; |
| 1608 | break; |
| 1609 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_XX: |
| 1610 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_XX; |
| 1611 | break; |
| 1612 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_64: |
| 1613 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64; |
| 1614 | break; |
| 1615 | case llvm::Mips::Val_GNU_MIPS_ABI_FP_64A: |
| 1616 | arch_flags |= lldb_private::ArchSpec::eMIPS_ABI_FP_64A; |
| 1617 | break; |
| 1618 | } |
| 1619 | } |
| 1620 | } |
| 1621 | // Settings appropriate ArchSpec ABI Flags |
| 1622 | switch (header.e_flags & llvm::ELF::EF_MIPS_ABI) { |
| 1623 | case llvm::ELF::EF_MIPS_ABI_O32: |
| 1624 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_O32; |
| 1625 | break; |
| 1626 | case EF_MIPS_ABI_O64: |
| 1627 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_O64; |
| 1628 | break; |
| 1629 | case EF_MIPS_ABI_EABI32: |
| 1630 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI32; |
| 1631 | break; |
| 1632 | case EF_MIPS_ABI_EABI64: |
| 1633 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_EABI64; |
| 1634 | break; |
| 1635 | default: |
| 1636 | // ABI Mask doesn't cover N32 and N64 ABI. |
| 1637 | if (header.e_ident[EI_CLASS] == llvm::ELF::ELFCLASS64) |
| 1638 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_N64; |
Mehdi Amini | d16a6e3 | 2017-06-23 18:20:13 +0000 | [diff] [blame] | 1639 | else if (header.e_flags & llvm::ELF::EF_MIPS_ABI2) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1640 | arch_flags |= lldb_private::ArchSpec::eMIPSABI_N32; |
| 1641 | break; |
| 1642 | } |
| 1643 | arch_spec.SetFlags(arch_flags); |
| 1644 | } |
| 1645 | |
| 1646 | if (arch_spec.GetMachine() == llvm::Triple::arm || |
| 1647 | arch_spec.GetMachine() == llvm::Triple::thumb) { |
| 1648 | DataExtractor data; |
| 1649 | |
| 1650 | if (sheader.sh_type == SHT_ARM_ATTRIBUTES && section_size != 0 && |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1651 | data.SetData(object_data, sheader.sh_offset, section_size) == section_size) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1652 | ParseARMAttributes(data, section_size, arch_spec); |
| 1653 | } |
| 1654 | |
| 1655 | if (name == g_sect_name_gnu_debuglink) { |
| 1656 | DataExtractor data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1657 | if (section_size && (data.SetData(object_data, sheader.sh_offset, |
| 1658 | section_size) == section_size)) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1659 | lldb::offset_t gnu_debuglink_offset = 0; |
| 1660 | gnu_debuglink_file = data.GetCStr(&gnu_debuglink_offset); |
| 1661 | gnu_debuglink_offset = llvm::alignTo(gnu_debuglink_offset, 4); |
| 1662 | data.GetU32(&gnu_debuglink_offset, &gnu_debuglink_crc, 1); |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | // Process ELF note section entries. |
| 1667 | bool is_note_header = (sheader.sh_type == SHT_NOTE); |
| 1668 | |
| 1669 | // The section header ".note.android.ident" is stored as a |
| 1670 | // PROGBITS type header but it is actually a note header. |
| 1671 | static ConstString g_sect_name_android_ident(".note.android.ident"); |
| 1672 | if (!is_note_header && name == g_sect_name_android_ident) |
| 1673 | is_note_header = true; |
| 1674 | |
| 1675 | if (is_note_header) { |
| 1676 | // Allow notes to refine module info. |
| 1677 | DataExtractor data; |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1678 | if (section_size && (data.SetData(object_data, sheader.sh_offset, |
| 1679 | section_size) == section_size)) { |
Zachary Turner | 97206d5 | 2017-05-12 04:51:55 +0000 | [diff] [blame] | 1680 | Status error = RefineModuleDetailsFromNote(data, arch_spec, uuid); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1681 | if (error.Fail()) { |
| 1682 | if (log) |
| 1683 | log->Printf("ObjectFileELF::%s ELF note processing failed: %s", |
| 1684 | __FUNCTION__, error.AsCString()); |
| 1685 | } |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | // Make any unknown triple components to be unspecified unknowns. |
| 1691 | if (arch_spec.GetTriple().getVendor() == llvm::Triple::UnknownVendor) |
| 1692 | arch_spec.GetTriple().setVendorName(llvm::StringRef()); |
| 1693 | if (arch_spec.GetTriple().getOS() == llvm::Triple::UnknownOS) |
| 1694 | arch_spec.GetTriple().setOSName(llvm::StringRef()); |
| 1695 | |
| 1696 | return section_headers.size(); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | section_headers.clear(); |
| 1701 | return 0; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1704 | size_t ObjectFileELF::GetProgramHeaderCount() { return ParseProgramHeaders(); } |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1705 | |
| 1706 | const elf::ELFProgramHeader * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1707 | ObjectFileELF::GetProgramHeaderByIndex(lldb::user_id_t id) { |
| 1708 | if (!id || !ParseProgramHeaders()) |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1709 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1710 | |
| 1711 | if (--id < m_program_headers.size()) |
| 1712 | return &m_program_headers[id]; |
| 1713 | |
| 1714 | return NULL; |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1717 | DataExtractor ObjectFileELF::GetSegmentDataByIndex(lldb::user_id_t id) { |
| 1718 | const elf::ELFProgramHeader *segment_header = GetProgramHeaderByIndex(id); |
| 1719 | if (segment_header == NULL) |
| 1720 | return DataExtractor(); |
| 1721 | return DataExtractor(m_data, segment_header->p_offset, |
| 1722 | segment_header->p_filesz); |
Ashok Thirumurthi | 4822d92 | 2013-07-11 20:39:00 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1725 | llvm::StringRef |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1726 | ObjectFileELF::StripLinkerSymbolAnnotations(llvm::StringRef symbol_name) const { |
| 1727 | size_t pos = symbol_name.find('@'); |
Pavel Labath | 4d35d6b | 2017-05-02 10:17:30 +0000 | [diff] [blame] | 1728 | return symbol_name.substr(0, pos); |
Pavel Labath | c6ae7ea | 2015-03-04 10:25:22 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1731 | //---------------------------------------------------------------------- |
| 1732 | // ParseSectionHeaders |
| 1733 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1734 | size_t ObjectFileELF::ParseSectionHeaders() { |
Pavel Labath | df1a0d1 | 2017-06-20 08:11:47 +0000 | [diff] [blame] | 1735 | return GetSectionHeaderInfo(m_section_headers, m_data, m_header, m_uuid, |
| 1736 | m_gnu_debuglink_file, m_gnu_debuglink_crc, |
| 1737 | m_arch_spec); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1740 | const ObjectFileELF::ELFSectionHeaderInfo * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1741 | ObjectFileELF::GetSectionHeaderByIndex(lldb::user_id_t id) { |
| 1742 | if (!id || !ParseSectionHeaders()) |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1743 | return NULL; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1744 | |
| 1745 | if (--id < m_section_headers.size()) |
| 1746 | return &m_section_headers[id]; |
| 1747 | |
| 1748 | return NULL; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 1749 | } |
| 1750 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1751 | lldb::user_id_t ObjectFileELF::GetSectionIndexByName(const char *name) { |
| 1752 | if (!name || !name[0] || !ParseSectionHeaders()) |
Tamas Berghammer | 85fadd9 | 2015-05-08 09:40:05 +0000 | [diff] [blame] | 1753 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1754 | for (size_t i = 1; i < m_section_headers.size(); ++i) |
| 1755 | if (m_section_headers[i].section_name == ConstString(name)) |
| 1756 | return i; |
| 1757 | return 0; |
Tamas Berghammer | 85fadd9 | 2015-05-08 09:40:05 +0000 | [diff] [blame] | 1758 | } |
| 1759 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1760 | void ObjectFileELF::CreateSections(SectionList &unified_section_list) { |
| 1761 | if (!m_sections_ap.get() && ParseSectionHeaders()) { |
| 1762 | m_sections_ap.reset(new SectionList()); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1763 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 1764 | // Object files frequently have 0 for every section address, meaning we |
| 1765 | // need to compute synthetic addresses in order for "file addresses" from |
| 1766 | // different sections to not overlap |
| 1767 | bool synthaddrs = (CalculateType() == ObjectFile::Type::eTypeObjectFile); |
| 1768 | uint64_t nextaddr = 0; |
| 1769 | |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1770 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1771 | I != m_section_headers.end(); ++I) { |
| 1772 | const ELFSectionHeaderInfo &header = *I; |
| 1773 | |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 1774 | ConstString &name = I->section_name; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1775 | const uint64_t file_size = |
| 1776 | header.sh_type == SHT_NOBITS ? 0 : header.sh_size; |
| 1777 | const uint64_t vm_size = header.sh_flags & SHF_ALLOC ? header.sh_size : 0; |
| 1778 | |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 1779 | static ConstString g_sect_name_text(".text"); |
| 1780 | static ConstString g_sect_name_data(".data"); |
| 1781 | static ConstString g_sect_name_bss(".bss"); |
| 1782 | static ConstString g_sect_name_tdata(".tdata"); |
| 1783 | static ConstString g_sect_name_tbss(".tbss"); |
| 1784 | static ConstString g_sect_name_dwarf_debug_abbrev(".debug_abbrev"); |
| 1785 | static ConstString g_sect_name_dwarf_debug_addr(".debug_addr"); |
| 1786 | static ConstString g_sect_name_dwarf_debug_aranges(".debug_aranges"); |
| 1787 | static ConstString g_sect_name_dwarf_debug_cu_index(".debug_cu_index"); |
| 1788 | static ConstString g_sect_name_dwarf_debug_frame(".debug_frame"); |
| 1789 | static ConstString g_sect_name_dwarf_debug_info(".debug_info"); |
| 1790 | static ConstString g_sect_name_dwarf_debug_line(".debug_line"); |
| 1791 | static ConstString g_sect_name_dwarf_debug_loc(".debug_loc"); |
| 1792 | static ConstString g_sect_name_dwarf_debug_macinfo(".debug_macinfo"); |
| 1793 | static ConstString g_sect_name_dwarf_debug_macro(".debug_macro"); |
Pavel Labath | a041d84 | 2018-06-01 12:06:45 +0000 | [diff] [blame] | 1794 | static ConstString g_sect_name_dwarf_debug_names(".debug_names"); |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 1795 | static ConstString g_sect_name_dwarf_debug_pubnames(".debug_pubnames"); |
| 1796 | static ConstString g_sect_name_dwarf_debug_pubtypes(".debug_pubtypes"); |
| 1797 | static ConstString g_sect_name_dwarf_debug_ranges(".debug_ranges"); |
| 1798 | static ConstString g_sect_name_dwarf_debug_str(".debug_str"); |
| 1799 | static ConstString g_sect_name_dwarf_debug_str_offsets( |
| 1800 | ".debug_str_offsets"); |
| 1801 | static ConstString g_sect_name_dwarf_debug_abbrev_dwo( |
| 1802 | ".debug_abbrev.dwo"); |
| 1803 | static ConstString g_sect_name_dwarf_debug_info_dwo(".debug_info.dwo"); |
| 1804 | static ConstString g_sect_name_dwarf_debug_line_dwo(".debug_line.dwo"); |
| 1805 | static ConstString g_sect_name_dwarf_debug_macro_dwo(".debug_macro.dwo"); |
| 1806 | static ConstString g_sect_name_dwarf_debug_loc_dwo(".debug_loc.dwo"); |
| 1807 | static ConstString g_sect_name_dwarf_debug_str_dwo(".debug_str.dwo"); |
| 1808 | static ConstString g_sect_name_dwarf_debug_str_offsets_dwo( |
| 1809 | ".debug_str_offsets.dwo"); |
| 1810 | static ConstString g_sect_name_dwarf_debug_types(".debug_types"); |
| 1811 | static ConstString g_sect_name_eh_frame(".eh_frame"); |
| 1812 | static ConstString g_sect_name_arm_exidx(".ARM.exidx"); |
| 1813 | static ConstString g_sect_name_arm_extab(".ARM.extab"); |
| 1814 | static ConstString g_sect_name_go_symtab(".gosymtab"); |
| 1815 | static ConstString g_sect_name_dwarf_gnu_debugaltlink(".gnu_debugaltlink"); |
| 1816 | |
| 1817 | SectionType sect_type = eSectionTypeOther; |
| 1818 | |
| 1819 | bool is_thread_specific = false; |
| 1820 | |
| 1821 | if (name == g_sect_name_text) |
| 1822 | sect_type = eSectionTypeCode; |
| 1823 | else if (name == g_sect_name_data) |
| 1824 | sect_type = eSectionTypeData; |
| 1825 | else if (name == g_sect_name_bss) |
| 1826 | sect_type = eSectionTypeZeroFill; |
| 1827 | else if (name == g_sect_name_tdata) { |
| 1828 | sect_type = eSectionTypeData; |
| 1829 | is_thread_specific = true; |
| 1830 | } else if (name == g_sect_name_tbss) { |
| 1831 | sect_type = eSectionTypeZeroFill; |
| 1832 | is_thread_specific = true; |
| 1833 | } |
| 1834 | // .debug_abbrev – Abbreviations used in the .debug_info section |
| 1835 | // .debug_aranges – Lookup table for mapping addresses to compilation |
| 1836 | // units .debug_frame – Call frame information .debug_info – The core |
| 1837 | // DWARF information section .debug_line – Line number information |
| 1838 | // .debug_loc – Location lists used in DW_AT_location attributes |
| 1839 | // .debug_macinfo – Macro information .debug_pubnames – Lookup table |
| 1840 | // for mapping object and function names to compilation units |
| 1841 | // .debug_pubtypes – Lookup table for mapping type names to compilation |
| 1842 | // units .debug_ranges – Address ranges used in DW_AT_ranges attributes |
| 1843 | // .debug_str – String table used in .debug_info MISSING? |
| 1844 | // .gnu_debugdata - "mini debuginfo / MiniDebugInfo" section, |
| 1845 | // http://sourceware.org/gdb/onlinedocs/gdb/MiniDebugInfo.html MISSING? |
| 1846 | // .debug-index - http://src.chromium.org/viewvc/chrome/trunk/src/build |
| 1847 | // /gdb-add-index?pathrev=144644 MISSING? .debug_types - Type |
| 1848 | // descriptions from DWARF 4? See |
| 1849 | // http://gcc.gnu.org/wiki/DwarfSeparateTypeInfo |
| 1850 | else if (name == g_sect_name_dwarf_debug_abbrev) |
| 1851 | sect_type = eSectionTypeDWARFDebugAbbrev; |
| 1852 | else if (name == g_sect_name_dwarf_debug_addr) |
| 1853 | sect_type = eSectionTypeDWARFDebugAddr; |
| 1854 | else if (name == g_sect_name_dwarf_debug_aranges) |
| 1855 | sect_type = eSectionTypeDWARFDebugAranges; |
| 1856 | else if (name == g_sect_name_dwarf_debug_cu_index) |
| 1857 | sect_type = eSectionTypeDWARFDebugCuIndex; |
| 1858 | else if (name == g_sect_name_dwarf_debug_frame) |
| 1859 | sect_type = eSectionTypeDWARFDebugFrame; |
| 1860 | else if (name == g_sect_name_dwarf_debug_info) |
| 1861 | sect_type = eSectionTypeDWARFDebugInfo; |
| 1862 | else if (name == g_sect_name_dwarf_debug_line) |
| 1863 | sect_type = eSectionTypeDWARFDebugLine; |
| 1864 | else if (name == g_sect_name_dwarf_debug_loc) |
| 1865 | sect_type = eSectionTypeDWARFDebugLoc; |
| 1866 | else if (name == g_sect_name_dwarf_debug_macinfo) |
| 1867 | sect_type = eSectionTypeDWARFDebugMacInfo; |
| 1868 | else if (name == g_sect_name_dwarf_debug_macro) |
| 1869 | sect_type = eSectionTypeDWARFDebugMacro; |
Pavel Labath | a041d84 | 2018-06-01 12:06:45 +0000 | [diff] [blame] | 1870 | else if (name == g_sect_name_dwarf_debug_names) |
| 1871 | sect_type = eSectionTypeDWARFDebugNames; |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 1872 | else if (name == g_sect_name_dwarf_debug_pubnames) |
| 1873 | sect_type = eSectionTypeDWARFDebugPubNames; |
| 1874 | else if (name == g_sect_name_dwarf_debug_pubtypes) |
| 1875 | sect_type = eSectionTypeDWARFDebugPubTypes; |
| 1876 | else if (name == g_sect_name_dwarf_debug_ranges) |
| 1877 | sect_type = eSectionTypeDWARFDebugRanges; |
| 1878 | else if (name == g_sect_name_dwarf_debug_str) |
| 1879 | sect_type = eSectionTypeDWARFDebugStr; |
| 1880 | else if (name == g_sect_name_dwarf_debug_types) |
| 1881 | sect_type = eSectionTypeDWARFDebugTypes; |
| 1882 | else if (name == g_sect_name_dwarf_debug_str_offsets) |
| 1883 | sect_type = eSectionTypeDWARFDebugStrOffsets; |
| 1884 | else if (name == g_sect_name_dwarf_debug_abbrev_dwo) |
| 1885 | sect_type = eSectionTypeDWARFDebugAbbrev; |
| 1886 | else if (name == g_sect_name_dwarf_debug_info_dwo) |
| 1887 | sect_type = eSectionTypeDWARFDebugInfo; |
| 1888 | else if (name == g_sect_name_dwarf_debug_line_dwo) |
| 1889 | sect_type = eSectionTypeDWARFDebugLine; |
| 1890 | else if (name == g_sect_name_dwarf_debug_macro_dwo) |
| 1891 | sect_type = eSectionTypeDWARFDebugMacro; |
| 1892 | else if (name == g_sect_name_dwarf_debug_loc_dwo) |
| 1893 | sect_type = eSectionTypeDWARFDebugLoc; |
| 1894 | else if (name == g_sect_name_dwarf_debug_str_dwo) |
| 1895 | sect_type = eSectionTypeDWARFDebugStr; |
| 1896 | else if (name == g_sect_name_dwarf_debug_str_offsets_dwo) |
| 1897 | sect_type = eSectionTypeDWARFDebugStrOffsets; |
| 1898 | else if (name == g_sect_name_eh_frame) |
| 1899 | sect_type = eSectionTypeEHFrame; |
| 1900 | else if (name == g_sect_name_arm_exidx) |
| 1901 | sect_type = eSectionTypeARMexidx; |
| 1902 | else if (name == g_sect_name_arm_extab) |
| 1903 | sect_type = eSectionTypeARMextab; |
| 1904 | else if (name == g_sect_name_go_symtab) |
| 1905 | sect_type = eSectionTypeGoSymtab; |
| 1906 | else if (name == g_sect_name_dwarf_gnu_debugaltlink) |
| 1907 | sect_type = eSectionTypeDWARFGNUDebugAltLink; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1908 | |
| 1909 | const uint32_t permissions = |
Ilia K | 4f730dc | 2016-09-12 05:25:33 +0000 | [diff] [blame] | 1910 | ((header.sh_flags & SHF_ALLOC) ? ePermissionsReadable : 0u) | |
| 1911 | ((header.sh_flags & SHF_WRITE) ? ePermissionsWritable : 0u) | |
| 1912 | ((header.sh_flags & SHF_EXECINSTR) ? ePermissionsExecutable : 0u); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1913 | switch (header.sh_type) { |
| 1914 | case SHT_SYMTAB: |
| 1915 | assert(sect_type == eSectionTypeOther); |
| 1916 | sect_type = eSectionTypeELFSymbolTable; |
| 1917 | break; |
| 1918 | case SHT_DYNSYM: |
| 1919 | assert(sect_type == eSectionTypeOther); |
| 1920 | sect_type = eSectionTypeELFDynamicSymbols; |
| 1921 | break; |
| 1922 | case SHT_RELA: |
| 1923 | case SHT_REL: |
| 1924 | assert(sect_type == eSectionTypeOther); |
| 1925 | sect_type = eSectionTypeELFRelocationEntries; |
| 1926 | break; |
| 1927 | case SHT_DYNAMIC: |
| 1928 | assert(sect_type == eSectionTypeOther); |
| 1929 | sect_type = eSectionTypeELFDynamicLinkInfo; |
| 1930 | break; |
| 1931 | } |
| 1932 | |
| 1933 | if (eSectionTypeOther == sect_type) { |
| 1934 | // the kalimba toolchain assumes that ELF section names are free-form. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1935 | // It does support linkscripts which (can) give rise to various |
| 1936 | // arbitrarily named sections being "Code" or "Data". |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1937 | sect_type = kalimbaSectionType(m_header, header); |
| 1938 | } |
| 1939 | |
Pavel Labath | edb0127 | 2018-04-30 13:23:47 +0000 | [diff] [blame] | 1940 | // In common case ELF code section can have arbitrary name (for example, |
| 1941 | // we can specify it using section attribute for particular function) so |
| 1942 | // assume that section is a code section if it has SHF_EXECINSTR flag set |
| 1943 | // and has SHT_PROGBITS type. |
| 1944 | if (eSectionTypeOther == sect_type && |
| 1945 | llvm::ELF::SHT_PROGBITS == header.sh_type && |
| 1946 | (header.sh_flags & SHF_EXECINSTR)) { |
| 1947 | sect_type = eSectionTypeCode; |
| 1948 | } |
| 1949 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1950 | const uint32_t target_bytes_size = |
| 1951 | (eSectionTypeData == sect_type || eSectionTypeZeroFill == sect_type) |
| 1952 | ? m_arch_spec.GetDataByteSize() |
| 1953 | : eSectionTypeCode == sect_type ? m_arch_spec.GetCodeByteSize() |
| 1954 | : 1; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1955 | elf::elf_xword log2align = |
| 1956 | (header.sh_addralign == 0) ? 0 : llvm::Log2_64(header.sh_addralign); |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 1957 | |
| 1958 | uint64_t addr = header.sh_addr; |
| 1959 | |
| 1960 | if ((header.sh_flags & SHF_ALLOC) && synthaddrs) { |
| 1961 | nextaddr = |
| 1962 | (nextaddr + header.sh_addralign - 1) & ~(header.sh_addralign - 1); |
| 1963 | addr = nextaddr; |
| 1964 | nextaddr += vm_size; |
| 1965 | } |
| 1966 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1967 | SectionSP section_sp(new Section( |
| 1968 | GetModule(), // Module to which this section belongs. |
| 1969 | this, // ObjectFile to which this section belongs and should read |
| 1970 | // section data from. |
| 1971 | SectionIndex(I), // Section ID. |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 1972 | name, // Section name. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1973 | sect_type, // Section type. |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 1974 | addr, // VM address. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1975 | vm_size, // VM size in bytes of this section. |
| 1976 | header.sh_offset, // Offset of this section in the file. |
| 1977 | file_size, // Size of the section as found in the file. |
| 1978 | log2align, // Alignment of the section |
| 1979 | header.sh_flags, // Flags for this section. |
| 1980 | target_bytes_size)); // Number of host bytes per target byte |
| 1981 | |
| 1982 | section_sp->SetPermissions(permissions); |
| 1983 | if (is_thread_specific) |
| 1984 | section_sp->SetIsThreadSpecific(is_thread_specific); |
| 1985 | m_sections_ap->AddSection(section_sp); |
Andrew MacPherson | 17220c1 | 2014-03-05 10:12:43 +0000 | [diff] [blame] | 1986 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Pavel Labath | 3ef4eeb | 2018-03-09 12:30:09 +0000 | [diff] [blame] | 1989 | // For eTypeDebugInfo files, the Symbol Vendor will take care of updating the |
| 1990 | // unified section list. |
| 1991 | if (GetType() != eTypeDebugInfo) |
| 1992 | unified_section_list = *m_sections_ap; |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1995 | // Find the arm/aarch64 mapping symbol character in the given symbol name. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1996 | // Mapping symbols have the form of "$<char>[.<any>]*". Additionally we |
| 1997 | // recognize cases when the mapping symbol prefixed by an arbitrary string |
| 1998 | // because if a symbol prefix added to each symbol in the object file with |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1999 | // objcopy then the mapping symbols are also prefixed. |
| 2000 | static char FindArmAarch64MappingSymbol(const char *symbol_name) { |
| 2001 | if (!symbol_name) |
| 2002 | return '\0'; |
| 2003 | |
| 2004 | const char *dollar_pos = ::strchr(symbol_name, '$'); |
| 2005 | if (!dollar_pos || dollar_pos[1] == '\0') |
| 2006 | return '\0'; |
| 2007 | |
| 2008 | if (dollar_pos[2] == '\0' || dollar_pos[2] == '.') |
| 2009 | return dollar_pos[1]; |
| 2010 | return '\0'; |
| 2011 | } |
| 2012 | |
| 2013 | #define STO_MIPS_ISA (3 << 6) |
| 2014 | #define STO_MICROMIPS (2 << 6) |
| 2015 | #define IS_MICROMIPS(ST_OTHER) (((ST_OTHER)&STO_MIPS_ISA) == STO_MICROMIPS) |
| 2016 | |
| 2017 | // private |
| 2018 | unsigned ObjectFileELF::ParseSymbols(Symtab *symtab, user_id_t start_id, |
| 2019 | SectionList *section_list, |
| 2020 | const size_t num_symbols, |
| 2021 | const DataExtractor &symtab_data, |
| 2022 | const DataExtractor &strtab_data) { |
| 2023 | ELFSymbol symbol; |
| 2024 | lldb::offset_t offset = 0; |
| 2025 | |
| 2026 | static ConstString text_section_name(".text"); |
| 2027 | static ConstString init_section_name(".init"); |
| 2028 | static ConstString fini_section_name(".fini"); |
| 2029 | static ConstString ctors_section_name(".ctors"); |
| 2030 | static ConstString dtors_section_name(".dtors"); |
| 2031 | |
| 2032 | static ConstString data_section_name(".data"); |
| 2033 | static ConstString rodata_section_name(".rodata"); |
| 2034 | static ConstString rodata1_section_name(".rodata1"); |
| 2035 | static ConstString data2_section_name(".data1"); |
| 2036 | static ConstString bss_section_name(".bss"); |
| 2037 | static ConstString opd_section_name(".opd"); // For ppc64 |
| 2038 | |
| 2039 | // On Android the oatdata and the oatexec symbols in the oat and odex files |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2040 | // covers the full .text section what causes issues with displaying unusable |
| 2041 | // symbol name to the user and very slow unwinding speed because the |
| 2042 | // instruction emulation based unwind plans try to emulate all instructions |
| 2043 | // in these symbols. Don't add these symbols to the symbol list as they have |
| 2044 | // no use for the debugger and they are causing a lot of trouble. Filtering |
| 2045 | // can't be restricted to Android because this special object file don't |
| 2046 | // contain the note section specifying the environment to Android but the |
| 2047 | // custom extension and file name makes it highly unlikely that this will |
| 2048 | // collide with anything else. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2049 | ConstString file_extension = m_file.GetFileNameExtension(); |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 2050 | bool skip_oatdata_oatexec = file_extension == ConstString(".oat") || |
| 2051 | file_extension == ConstString(".odex"); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2052 | |
| 2053 | ArchSpec arch; |
| 2054 | GetArchitecture(arch); |
| 2055 | ModuleSP module_sp(GetModule()); |
| 2056 | SectionList *module_section_list = |
| 2057 | module_sp ? module_sp->GetSectionList() : nullptr; |
| 2058 | |
| 2059 | // Local cache to avoid doing a FindSectionByName for each symbol. The "const |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2060 | // char*" key must came from a ConstString object so they can be compared by |
| 2061 | // pointer |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2062 | std::unordered_map<const char *, lldb::SectionSP> section_name_to_section; |
| 2063 | |
| 2064 | unsigned i; |
| 2065 | for (i = 0; i < num_symbols; ++i) { |
| 2066 | if (symbol.Parse(symtab_data, &offset) == false) |
| 2067 | break; |
| 2068 | |
| 2069 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
| 2070 | if (!symbol_name) |
| 2071 | symbol_name = ""; |
| 2072 | |
| 2073 | // No need to add non-section symbols that have no names |
| 2074 | if (symbol.getType() != STT_SECTION && |
| 2075 | (symbol_name == nullptr || symbol_name[0] == '\0')) |
| 2076 | continue; |
| 2077 | |
| 2078 | // Skipping oatdata and oatexec sections if it is requested. See details |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2079 | // above the definition of skip_oatdata_oatexec for the reasons. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2080 | if (skip_oatdata_oatexec && (::strcmp(symbol_name, "oatdata") == 0 || |
| 2081 | ::strcmp(symbol_name, "oatexec") == 0)) |
| 2082 | continue; |
| 2083 | |
| 2084 | SectionSP symbol_section_sp; |
| 2085 | SymbolType symbol_type = eSymbolTypeInvalid; |
| 2086 | Elf64_Half section_idx = symbol.st_shndx; |
| 2087 | |
| 2088 | switch (section_idx) { |
| 2089 | case SHN_ABS: |
| 2090 | symbol_type = eSymbolTypeAbsolute; |
| 2091 | break; |
| 2092 | case SHN_UNDEF: |
| 2093 | symbol_type = eSymbolTypeUndefined; |
| 2094 | break; |
| 2095 | default: |
| 2096 | symbol_section_sp = section_list->GetSectionAtIndex(section_idx); |
| 2097 | break; |
| 2098 | } |
| 2099 | |
| 2100 | // If a symbol is undefined do not process it further even if it has a STT |
| 2101 | // type |
| 2102 | if (symbol_type != eSymbolTypeUndefined) { |
| 2103 | switch (symbol.getType()) { |
| 2104 | default: |
| 2105 | case STT_NOTYPE: |
| 2106 | // The symbol's type is not specified. |
| 2107 | break; |
| 2108 | |
| 2109 | case STT_OBJECT: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2110 | // The symbol is associated with a data object, such as a variable, an |
| 2111 | // array, etc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2112 | symbol_type = eSymbolTypeData; |
| 2113 | break; |
| 2114 | |
| 2115 | case STT_FUNC: |
| 2116 | // The symbol is associated with a function or other executable code. |
| 2117 | symbol_type = eSymbolTypeCode; |
| 2118 | break; |
| 2119 | |
| 2120 | case STT_SECTION: |
| 2121 | // The symbol is associated with a section. Symbol table entries of |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2122 | // this type exist primarily for relocation and normally have STB_LOCAL |
| 2123 | // binding. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2124 | break; |
| 2125 | |
| 2126 | case STT_FILE: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2127 | // Conventionally, the symbol's name gives the name of the source file |
| 2128 | // associated with the object file. A file symbol has STB_LOCAL |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2129 | // binding, its section index is SHN_ABS, and it precedes the other |
| 2130 | // STB_LOCAL symbols for the file, if it is present. |
| 2131 | symbol_type = eSymbolTypeSourceFile; |
| 2132 | break; |
| 2133 | |
| 2134 | case STT_GNU_IFUNC: |
| 2135 | // The symbol is associated with an indirect function. The actual |
| 2136 | // function will be resolved if it is referenced. |
| 2137 | symbol_type = eSymbolTypeResolver; |
| 2138 | break; |
| 2139 | } |
| 2140 | } |
| 2141 | |
| 2142 | if (symbol_type == eSymbolTypeInvalid && symbol.getType() != STT_SECTION) { |
| 2143 | if (symbol_section_sp) { |
| 2144 | const ConstString §_name = symbol_section_sp->GetName(); |
| 2145 | if (sect_name == text_section_name || sect_name == init_section_name || |
| 2146 | sect_name == fini_section_name || sect_name == ctors_section_name || |
| 2147 | sect_name == dtors_section_name) { |
| 2148 | symbol_type = eSymbolTypeCode; |
| 2149 | } else if (sect_name == data_section_name || |
| 2150 | sect_name == data2_section_name || |
| 2151 | sect_name == rodata_section_name || |
| 2152 | sect_name == rodata1_section_name || |
| 2153 | sect_name == bss_section_name) { |
| 2154 | symbol_type = eSymbolTypeData; |
| 2155 | } |
| 2156 | } |
| 2157 | } |
| 2158 | |
| 2159 | int64_t symbol_value_offset = 0; |
| 2160 | uint32_t additional_flags = 0; |
| 2161 | |
| 2162 | if (arch.IsValid()) { |
| 2163 | if (arch.GetMachine() == llvm::Triple::arm) { |
| 2164 | if (symbol.getBinding() == STB_LOCAL) { |
| 2165 | char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); |
| 2166 | if (symbol_type == eSymbolTypeCode) { |
| 2167 | switch (mapping_symbol) { |
| 2168 | case 'a': |
| 2169 | // $a[.<any>]* - marks an ARM instruction sequence |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2170 | m_address_class_map[symbol.st_value] = AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2171 | break; |
| 2172 | case 'b': |
| 2173 | case 't': |
| 2174 | // $b[.<any>]* - marks a THUMB BL instruction sequence |
| 2175 | // $t[.<any>]* - marks a THUMB instruction sequence |
| 2176 | m_address_class_map[symbol.st_value] = |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2177 | AddressClass::eCodeAlternateISA; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2178 | break; |
| 2179 | case 'd': |
| 2180 | // $d[.<any>]* - marks a data item sequence (e.g. lit pool) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2181 | m_address_class_map[symbol.st_value] = AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2182 | break; |
| 2183 | } |
| 2184 | } |
| 2185 | if (mapping_symbol) |
| 2186 | continue; |
| 2187 | } |
| 2188 | } else if (arch.GetMachine() == llvm::Triple::aarch64) { |
| 2189 | if (symbol.getBinding() == STB_LOCAL) { |
| 2190 | char mapping_symbol = FindArmAarch64MappingSymbol(symbol_name); |
| 2191 | if (symbol_type == eSymbolTypeCode) { |
| 2192 | switch (mapping_symbol) { |
| 2193 | case 'x': |
| 2194 | // $x[.<any>]* - marks an A64 instruction sequence |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2195 | m_address_class_map[symbol.st_value] = AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2196 | break; |
| 2197 | case 'd': |
| 2198 | // $d[.<any>]* - marks a data item sequence (e.g. lit pool) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2199 | m_address_class_map[symbol.st_value] = AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2200 | break; |
| 2201 | } |
| 2202 | } |
| 2203 | if (mapping_symbol) |
| 2204 | continue; |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | if (arch.GetMachine() == llvm::Triple::arm) { |
| 2209 | if (symbol_type == eSymbolTypeCode) { |
| 2210 | if (symbol.st_value & 1) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2211 | // Subtracting 1 from the address effectively unsets the low order |
| 2212 | // bit, which results in the address actually pointing to the |
| 2213 | // beginning of the symbol. This delta will be used below in |
| 2214 | // conjunction with symbol.st_value to produce the final |
| 2215 | // symbol_value that we store in the symtab. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2216 | symbol_value_offset = -1; |
| 2217 | m_address_class_map[symbol.st_value ^ 1] = |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2218 | AddressClass::eCodeAlternateISA; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2219 | } else { |
| 2220 | // This address is ARM |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2221 | m_address_class_map[symbol.st_value] = AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2222 | } |
| 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | /* |
| 2227 | * MIPS: |
| 2228 | * The bit #0 of an address is used for ISA mode (1 for microMIPS, 0 for |
| 2229 | * MIPS). |
| 2230 | * This allows processor to switch between microMIPS and MIPS without any |
| 2231 | * need |
| 2232 | * for special mode-control register. However, apart from .debug_line, |
| 2233 | * none of |
| 2234 | * the ELF/DWARF sections set the ISA bit (for symbol or section). Use |
| 2235 | * st_other |
| 2236 | * flag to check whether the symbol is microMIPS and then set the address |
| 2237 | * class |
| 2238 | * accordingly. |
| 2239 | */ |
| 2240 | const llvm::Triple::ArchType llvm_arch = arch.GetMachine(); |
| 2241 | if (llvm_arch == llvm::Triple::mips || |
| 2242 | llvm_arch == llvm::Triple::mipsel || |
| 2243 | llvm_arch == llvm::Triple::mips64 || |
| 2244 | llvm_arch == llvm::Triple::mips64el) { |
| 2245 | if (IS_MICROMIPS(symbol.st_other)) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2246 | m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2247 | else if ((symbol.st_value & 1) && (symbol_type == eSymbolTypeCode)) { |
| 2248 | symbol.st_value = symbol.st_value & (~1ull); |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2249 | m_address_class_map[symbol.st_value] = AddressClass::eCodeAlternateISA; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2250 | } else { |
| 2251 | if (symbol_type == eSymbolTypeCode) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2252 | m_address_class_map[symbol.st_value] = AddressClass::eCode; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2253 | else if (symbol_type == eSymbolTypeData) |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2254 | m_address_class_map[symbol.st_value] = AddressClass::eData; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2255 | else |
Tatyana Krasnukha | 04803b3 | 2018-06-26 13:06:54 +0000 | [diff] [blame^] | 2256 | m_address_class_map[symbol.st_value] = AddressClass::eUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2257 | } |
| 2258 | } |
| 2259 | } |
| 2260 | |
| 2261 | // symbol_value_offset may contain 0 for ARM symbols or -1 for THUMB |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2262 | // symbols. See above for more details. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2263 | uint64_t symbol_value = symbol.st_value + symbol_value_offset; |
| 2264 | |
| 2265 | if (symbol_section_sp == nullptr && section_idx == SHN_ABS && |
| 2266 | symbol.st_size != 0) { |
| 2267 | // We don't have a section for a symbol with non-zero size. Create a new |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2268 | // section for it so the address range covered by the symbol is also |
| 2269 | // covered by the module (represented through the section list). It is |
| 2270 | // needed so module lookup for the addresses covered by this symbol will |
| 2271 | // be successfull. This case happens for absolute symbols. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2272 | ConstString fake_section_name(std::string(".absolute.") + symbol_name); |
| 2273 | symbol_section_sp = |
| 2274 | std::make_shared<Section>(module_sp, this, SHN_ABS, fake_section_name, |
| 2275 | eSectionTypeAbsoluteAddress, symbol_value, |
| 2276 | symbol.st_size, 0, 0, 0, SHF_ALLOC); |
| 2277 | |
| 2278 | module_section_list->AddSection(symbol_section_sp); |
| 2279 | section_list->AddSection(symbol_section_sp); |
| 2280 | } |
| 2281 | |
| 2282 | if (symbol_section_sp && |
| 2283 | CalculateType() != ObjectFile::Type::eTypeObjectFile) |
| 2284 | symbol_value -= symbol_section_sp->GetFileAddress(); |
| 2285 | |
| 2286 | if (symbol_section_sp && module_section_list && |
| 2287 | module_section_list != section_list) { |
| 2288 | const ConstString §_name = symbol_section_sp->GetName(); |
| 2289 | auto section_it = section_name_to_section.find(sect_name.GetCString()); |
| 2290 | if (section_it == section_name_to_section.end()) |
| 2291 | section_it = |
| 2292 | section_name_to_section |
| 2293 | .emplace(sect_name.GetCString(), |
| 2294 | module_section_list->FindSectionByName(sect_name)) |
| 2295 | .first; |
Pavel Labath | efddda3d | 2017-05-02 12:40:31 +0000 | [diff] [blame] | 2296 | if (section_it->second) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2297 | symbol_section_sp = section_it->second; |
| 2298 | } |
| 2299 | |
| 2300 | bool is_global = symbol.getBinding() == STB_GLOBAL; |
| 2301 | uint32_t flags = symbol.st_other << 8 | symbol.st_info | additional_flags; |
| 2302 | bool is_mangled = (symbol_name[0] == '_' && symbol_name[1] == 'Z'); |
| 2303 | |
| 2304 | llvm::StringRef symbol_ref(symbol_name); |
| 2305 | |
| 2306 | // Symbol names may contain @VERSION suffixes. Find those and strip them |
| 2307 | // temporarily. |
| 2308 | size_t version_pos = symbol_ref.find('@'); |
| 2309 | bool has_suffix = version_pos != llvm::StringRef::npos; |
| 2310 | llvm::StringRef symbol_bare = symbol_ref.substr(0, version_pos); |
| 2311 | Mangled mangled(ConstString(symbol_bare), is_mangled); |
| 2312 | |
| 2313 | // Now append the suffix back to mangled and unmangled names. Only do it if |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2314 | // the demangling was successful (string is not empty). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2315 | if (has_suffix) { |
| 2316 | llvm::StringRef suffix = symbol_ref.substr(version_pos); |
| 2317 | |
| 2318 | llvm::StringRef mangled_name = mangled.GetMangledName().GetStringRef(); |
| 2319 | if (!mangled_name.empty()) |
| 2320 | mangled.SetMangledName(ConstString((mangled_name + suffix).str())); |
| 2321 | |
| 2322 | ConstString demangled = |
| 2323 | mangled.GetDemangledName(lldb::eLanguageTypeUnknown); |
| 2324 | llvm::StringRef demangled_name = demangled.GetStringRef(); |
| 2325 | if (!demangled_name.empty()) |
| 2326 | mangled.SetDemangledName(ConstString((demangled_name + suffix).str())); |
| 2327 | } |
| 2328 | |
| 2329 | // In ELF all symbol should have a valid size but it is not true for some |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2330 | // function symbols coming from hand written assembly. As none of the |
| 2331 | // function symbol should have 0 size we try to calculate the size for |
| 2332 | // these symbols in the symtab with saying that their original size is not |
| 2333 | // valid. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2334 | bool symbol_size_valid = |
| 2335 | symbol.st_size != 0 || symbol.getType() != STT_FUNC; |
| 2336 | |
| 2337 | Symbol dc_symbol( |
| 2338 | i + start_id, // ID is the original symbol table index. |
| 2339 | mangled, |
| 2340 | symbol_type, // Type of this symbol |
| 2341 | is_global, // Is this globally visible? |
| 2342 | false, // Is this symbol debug info? |
| 2343 | false, // Is this symbol a trampoline? |
| 2344 | false, // Is this symbol artificial? |
| 2345 | AddressRange(symbol_section_sp, // Section in which this symbol is |
| 2346 | // defined or null. |
| 2347 | symbol_value, // Offset in section or symbol value. |
| 2348 | symbol.st_size), // Size in bytes of this symbol. |
| 2349 | symbol_size_valid, // Symbol size is valid |
| 2350 | has_suffix, // Contains linker annotations? |
| 2351 | flags); // Symbol flags. |
| 2352 | symtab->AddSymbol(dc_symbol); |
| 2353 | } |
| 2354 | return i; |
| 2355 | } |
| 2356 | |
| 2357 | unsigned ObjectFileELF::ParseSymbolTable(Symtab *symbol_table, |
| 2358 | user_id_t start_id, |
| 2359 | lldb_private::Section *symtab) { |
| 2360 | if (symtab->GetObjectFile() != this) { |
| 2361 | // If the symbol table section is owned by a different object file, have it |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2362 | // do the parsing. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2363 | ObjectFileELF *obj_file_elf = |
| 2364 | static_cast<ObjectFileELF *>(symtab->GetObjectFile()); |
| 2365 | return obj_file_elf->ParseSymbolTable(symbol_table, start_id, symtab); |
| 2366 | } |
| 2367 | |
| 2368 | // Get section list for this object file. |
| 2369 | SectionList *section_list = m_sections_ap.get(); |
| 2370 | if (!section_list) |
| 2371 | return 0; |
| 2372 | |
| 2373 | user_id_t symtab_id = symtab->GetID(); |
| 2374 | const ELFSectionHeaderInfo *symtab_hdr = GetSectionHeaderByIndex(symtab_id); |
| 2375 | assert(symtab_hdr->sh_type == SHT_SYMTAB || |
| 2376 | symtab_hdr->sh_type == SHT_DYNSYM); |
| 2377 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2378 | // sh_link: section header index of associated string table. Section ID's are |
| 2379 | // ones based. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2380 | user_id_t strtab_id = symtab_hdr->sh_link + 1; |
| 2381 | Section *strtab = section_list->FindSectionByID(strtab_id).get(); |
| 2382 | |
| 2383 | if (symtab && strtab) { |
| 2384 | assert(symtab->GetObjectFile() == this); |
| 2385 | assert(strtab->GetObjectFile() == this); |
| 2386 | |
| 2387 | DataExtractor symtab_data; |
| 2388 | DataExtractor strtab_data; |
| 2389 | if (ReadSectionData(symtab, symtab_data) && |
| 2390 | ReadSectionData(strtab, strtab_data)) { |
| 2391 | size_t num_symbols = symtab_data.GetByteSize() / symtab_hdr->sh_entsize; |
| 2392 | |
| 2393 | return ParseSymbols(symbol_table, start_id, section_list, num_symbols, |
| 2394 | symtab_data, strtab_data); |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | size_t ObjectFileELF::ParseDynamicSymbols() { |
| 2402 | if (m_dynamic_symbols.size()) |
| 2403 | return m_dynamic_symbols.size(); |
| 2404 | |
| 2405 | SectionList *section_list = GetSectionList(); |
| 2406 | if (!section_list) |
| 2407 | return 0; |
| 2408 | |
| 2409 | // Find the SHT_DYNAMIC section. |
| 2410 | Section *dynsym = |
| 2411 | section_list->FindSectionByType(eSectionTypeELFDynamicLinkInfo, true) |
| 2412 | .get(); |
| 2413 | if (!dynsym) |
| 2414 | return 0; |
| 2415 | assert(dynsym->GetObjectFile() == this); |
| 2416 | |
| 2417 | ELFDynamic symbol; |
| 2418 | DataExtractor dynsym_data; |
| 2419 | if (ReadSectionData(dynsym, dynsym_data)) { |
| 2420 | const lldb::offset_t section_size = dynsym_data.GetByteSize(); |
| 2421 | lldb::offset_t cursor = 0; |
| 2422 | |
| 2423 | while (cursor < section_size) { |
| 2424 | if (!symbol.Parse(dynsym_data, &cursor)) |
| 2425 | break; |
| 2426 | |
| 2427 | m_dynamic_symbols.push_back(symbol); |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | return m_dynamic_symbols.size(); |
| 2432 | } |
| 2433 | |
| 2434 | const ELFDynamic *ObjectFileELF::FindDynamicSymbol(unsigned tag) { |
| 2435 | if (!ParseDynamicSymbols()) |
| 2436 | return NULL; |
| 2437 | |
| 2438 | DynamicSymbolCollIter I = m_dynamic_symbols.begin(); |
| 2439 | DynamicSymbolCollIter E = m_dynamic_symbols.end(); |
| 2440 | for (; I != E; ++I) { |
| 2441 | ELFDynamic *symbol = &*I; |
| 2442 | |
| 2443 | if (symbol->d_tag == tag) |
| 2444 | return symbol; |
| 2445 | } |
| 2446 | |
| 2447 | return NULL; |
| 2448 | } |
| 2449 | |
| 2450 | unsigned ObjectFileELF::PLTRelocationType() { |
| 2451 | // DT_PLTREL |
| 2452 | // This member specifies the type of relocation entry to which the |
| 2453 | // procedure linkage table refers. The d_val member holds DT_REL or |
| 2454 | // DT_RELA, as appropriate. All relocations in a procedure linkage table |
| 2455 | // must use the same relocation. |
| 2456 | const ELFDynamic *symbol = FindDynamicSymbol(DT_PLTREL); |
| 2457 | |
| 2458 | if (symbol) |
| 2459 | return symbol->d_val; |
| 2460 | |
| 2461 | return 0; |
| 2462 | } |
| 2463 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2464 | // Returns the size of the normal plt entries and the offset of the first |
| 2465 | // normal plt entry. The 0th entry in the plt table is usually a resolution |
| 2466 | // entry which have different size in some architectures then the rest of the |
| 2467 | // plt entries. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2468 | static std::pair<uint64_t, uint64_t> |
| 2469 | GetPltEntrySizeAndOffset(const ELFSectionHeader *rel_hdr, |
| 2470 | const ELFSectionHeader *plt_hdr) { |
| 2471 | const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 2472 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2473 | // Clang 3.3 sets entsize to 4 for 32-bit binaries, but the plt entries are |
| 2474 | // 16 bytes. So round the entsize up by the alignment if addralign is set. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2475 | elf_xword plt_entsize = |
| 2476 | plt_hdr->sh_addralign |
| 2477 | ? llvm::alignTo(plt_hdr->sh_entsize, plt_hdr->sh_addralign) |
| 2478 | : plt_hdr->sh_entsize; |
| 2479 | |
| 2480 | // Some linkers e.g ld for arm, fill plt_hdr->sh_entsize field incorrectly. |
| 2481 | // PLT entries relocation code in general requires multiple instruction and |
| 2482 | // should be greater than 4 bytes in most cases. Try to guess correct size |
| 2483 | // just in case. |
| 2484 | if (plt_entsize <= 4) { |
| 2485 | // The linker haven't set the plt_hdr->sh_entsize field. Try to guess the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2486 | // size of the plt entries based on the number of entries and the size of |
| 2487 | // the plt section with the assumption that the size of the 0th entry is at |
| 2488 | // least as big as the size of the normal entries and it isn't much bigger |
| 2489 | // then that. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2490 | if (plt_hdr->sh_addralign) |
| 2491 | plt_entsize = plt_hdr->sh_size / plt_hdr->sh_addralign / |
| 2492 | (num_relocations + 1) * plt_hdr->sh_addralign; |
| 2493 | else |
| 2494 | plt_entsize = plt_hdr->sh_size / (num_relocations + 1); |
| 2495 | } |
| 2496 | |
| 2497 | elf_xword plt_offset = plt_hdr->sh_size - num_relocations * plt_entsize; |
| 2498 | |
| 2499 | return std::make_pair(plt_entsize, plt_offset); |
| 2500 | } |
| 2501 | |
| 2502 | static unsigned ParsePLTRelocations( |
| 2503 | Symtab *symbol_table, user_id_t start_id, unsigned rel_type, |
| 2504 | const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, |
| 2505 | const ELFSectionHeader *plt_hdr, const ELFSectionHeader *sym_hdr, |
| 2506 | const lldb::SectionSP &plt_section_sp, DataExtractor &rel_data, |
| 2507 | DataExtractor &symtab_data, DataExtractor &strtab_data) { |
| 2508 | ELFRelocation rel(rel_type); |
| 2509 | ELFSymbol symbol; |
| 2510 | lldb::offset_t offset = 0; |
| 2511 | |
| 2512 | uint64_t plt_offset, plt_entsize; |
| 2513 | std::tie(plt_entsize, plt_offset) = |
| 2514 | GetPltEntrySizeAndOffset(rel_hdr, plt_hdr); |
| 2515 | const elf_xword num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 2516 | |
| 2517 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 2518 | reloc_info_fn reloc_type; |
| 2519 | reloc_info_fn reloc_symbol; |
| 2520 | |
| 2521 | if (hdr->Is32Bit()) { |
| 2522 | reloc_type = ELFRelocation::RelocType32; |
| 2523 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 2524 | } else { |
| 2525 | reloc_type = ELFRelocation::RelocType64; |
| 2526 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 2527 | } |
| 2528 | |
| 2529 | unsigned slot_type = hdr->GetRelocationJumpSlotType(); |
| 2530 | unsigned i; |
| 2531 | for (i = 0; i < num_relocations; ++i) { |
| 2532 | if (rel.Parse(rel_data, &offset) == false) |
| 2533 | break; |
| 2534 | |
| 2535 | if (reloc_type(rel) != slot_type) |
| 2536 | continue; |
| 2537 | |
| 2538 | lldb::offset_t symbol_offset = reloc_symbol(rel) * sym_hdr->sh_entsize; |
| 2539 | if (!symbol.Parse(symtab_data, &symbol_offset)) |
| 2540 | break; |
| 2541 | |
| 2542 | const char *symbol_name = strtab_data.PeekCStr(symbol.st_name); |
| 2543 | bool is_mangled = |
| 2544 | symbol_name ? (symbol_name[0] == '_' && symbol_name[1] == 'Z') : false; |
| 2545 | uint64_t plt_index = plt_offset + i * plt_entsize; |
| 2546 | |
| 2547 | Symbol jump_symbol( |
| 2548 | i + start_id, // Symbol table index |
| 2549 | symbol_name, // symbol name. |
| 2550 | is_mangled, // is the symbol name mangled? |
| 2551 | eSymbolTypeTrampoline, // Type of this symbol |
| 2552 | false, // Is this globally visible? |
| 2553 | false, // Is this symbol debug info? |
| 2554 | true, // Is this symbol a trampoline? |
| 2555 | true, // Is this symbol artificial? |
| 2556 | plt_section_sp, // Section in which this symbol is defined or null. |
| 2557 | plt_index, // Offset in section or symbol value. |
| 2558 | plt_entsize, // Size in bytes of this symbol. |
| 2559 | true, // Size is valid |
| 2560 | false, // Contains linker annotations? |
| 2561 | 0); // Symbol flags. |
| 2562 | |
| 2563 | symbol_table->AddSymbol(jump_symbol); |
| 2564 | } |
| 2565 | |
| 2566 | return i; |
| 2567 | } |
| 2568 | |
| 2569 | unsigned |
| 2570 | ObjectFileELF::ParseTrampolineSymbols(Symtab *symbol_table, user_id_t start_id, |
| 2571 | const ELFSectionHeaderInfo *rel_hdr, |
| 2572 | user_id_t rel_id) { |
| 2573 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 2574 | |
| 2575 | // The link field points to the associated symbol table. |
| 2576 | user_id_t symtab_id = rel_hdr->sh_link; |
| 2577 | |
| 2578 | // If the link field doesn't point to the appropriate symbol name table then |
| 2579 | // try to find it by name as some compiler don't fill in the link fields. |
| 2580 | if (!symtab_id) |
| 2581 | symtab_id = GetSectionIndexByName(".dynsym"); |
| 2582 | |
| 2583 | // Get PLT section. We cannot use rel_hdr->sh_info, since current linkers |
| 2584 | // point that to the .got.plt or .got section instead of .plt. |
| 2585 | user_id_t plt_id = GetSectionIndexByName(".plt"); |
| 2586 | |
| 2587 | if (!symtab_id || !plt_id) |
| 2588 | return 0; |
| 2589 | |
| 2590 | // Section ID's are ones based; |
| 2591 | symtab_id++; |
| 2592 | plt_id++; |
| 2593 | |
| 2594 | const ELFSectionHeaderInfo *plt_hdr = GetSectionHeaderByIndex(plt_id); |
| 2595 | if (!plt_hdr) |
| 2596 | return 0; |
| 2597 | |
| 2598 | const ELFSectionHeaderInfo *sym_hdr = GetSectionHeaderByIndex(symtab_id); |
| 2599 | if (!sym_hdr) |
| 2600 | return 0; |
| 2601 | |
| 2602 | SectionList *section_list = m_sections_ap.get(); |
| 2603 | if (!section_list) |
| 2604 | return 0; |
| 2605 | |
| 2606 | Section *rel_section = section_list->FindSectionByID(rel_id).get(); |
| 2607 | if (!rel_section) |
| 2608 | return 0; |
| 2609 | |
| 2610 | SectionSP plt_section_sp(section_list->FindSectionByID(plt_id)); |
| 2611 | if (!plt_section_sp) |
| 2612 | return 0; |
| 2613 | |
| 2614 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 2615 | if (!symtab) |
| 2616 | return 0; |
| 2617 | |
| 2618 | // sh_link points to associated string table. |
| 2619 | Section *strtab = section_list->FindSectionByID(sym_hdr->sh_link + 1).get(); |
| 2620 | if (!strtab) |
| 2621 | return 0; |
| 2622 | |
| 2623 | DataExtractor rel_data; |
| 2624 | if (!ReadSectionData(rel_section, rel_data)) |
| 2625 | return 0; |
| 2626 | |
| 2627 | DataExtractor symtab_data; |
| 2628 | if (!ReadSectionData(symtab, symtab_data)) |
| 2629 | return 0; |
| 2630 | |
| 2631 | DataExtractor strtab_data; |
| 2632 | if (!ReadSectionData(strtab, strtab_data)) |
| 2633 | return 0; |
| 2634 | |
| 2635 | unsigned rel_type = PLTRelocationType(); |
| 2636 | if (!rel_type) |
| 2637 | return 0; |
| 2638 | |
| 2639 | return ParsePLTRelocations(symbol_table, start_id, rel_type, &m_header, |
| 2640 | rel_hdr, plt_hdr, sym_hdr, plt_section_sp, |
| 2641 | rel_data, symtab_data, strtab_data); |
| 2642 | } |
| 2643 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2644 | unsigned ObjectFileELF::ApplyRelocations( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2645 | Symtab *symtab, const ELFHeader *hdr, const ELFSectionHeader *rel_hdr, |
| 2646 | const ELFSectionHeader *symtab_hdr, const ELFSectionHeader *debug_hdr, |
| 2647 | DataExtractor &rel_data, DataExtractor &symtab_data, |
| 2648 | DataExtractor &debug_data, Section *rel_section) { |
| 2649 | ELFRelocation rel(rel_hdr->sh_type); |
| 2650 | lldb::addr_t offset = 0; |
| 2651 | const unsigned num_relocations = rel_hdr->sh_size / rel_hdr->sh_entsize; |
| 2652 | typedef unsigned (*reloc_info_fn)(const ELFRelocation &rel); |
| 2653 | reloc_info_fn reloc_type; |
| 2654 | reloc_info_fn reloc_symbol; |
| 2655 | |
| 2656 | if (hdr->Is32Bit()) { |
| 2657 | reloc_type = ELFRelocation::RelocType32; |
| 2658 | reloc_symbol = ELFRelocation::RelocSymbol32; |
| 2659 | } else { |
| 2660 | reloc_type = ELFRelocation::RelocType64; |
| 2661 | reloc_symbol = ELFRelocation::RelocSymbol64; |
| 2662 | } |
| 2663 | |
| 2664 | for (unsigned i = 0; i < num_relocations; ++i) { |
| 2665 | if (rel.Parse(rel_data, &offset) == false) |
| 2666 | break; |
| 2667 | |
| 2668 | Symbol *symbol = NULL; |
| 2669 | |
| 2670 | if (hdr->Is32Bit()) { |
| 2671 | switch (reloc_type(rel)) { |
| 2672 | case R_386_32: |
| 2673 | case R_386_PC32: |
| 2674 | default: |
Zachary Turner | a6d5464 | 2017-12-02 00:15:29 +0000 | [diff] [blame] | 2675 | // FIXME: This asserts with this input: |
| 2676 | // |
| 2677 | // foo.cpp |
| 2678 | // int main(int argc, char **argv) { return 0; } |
| 2679 | // |
| 2680 | // clang++.exe --target=i686-unknown-linux-gnu -g -c foo.cpp -o foo.o |
| 2681 | // |
| 2682 | // and running this on the foo.o module. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2683 | assert(false && "unexpected relocation type"); |
| 2684 | } |
| 2685 | } else { |
| 2686 | switch (reloc_type(rel)) { |
| 2687 | case R_X86_64_64: { |
| 2688 | symbol = symtab->FindSymbolByID(reloc_symbol(rel)); |
| 2689 | if (symbol) { |
| 2690 | addr_t value = symbol->GetAddressRef().GetFileAddress(); |
| 2691 | DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); |
| 2692 | uint64_t *dst = reinterpret_cast<uint64_t *>( |
| 2693 | data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + |
| 2694 | ELFRelocation::RelocOffset64(rel)); |
| 2695 | *dst = value + ELFRelocation::RelocAddend64(rel); |
| 2696 | } |
| 2697 | break; |
| 2698 | } |
| 2699 | case R_X86_64_32: |
| 2700 | case R_X86_64_32S: { |
| 2701 | symbol = symtab->FindSymbolByID(reloc_symbol(rel)); |
| 2702 | if (symbol) { |
| 2703 | addr_t value = symbol->GetAddressRef().GetFileAddress(); |
| 2704 | value += ELFRelocation::RelocAddend32(rel); |
| 2705 | assert( |
| 2706 | (reloc_type(rel) == R_X86_64_32 && (value <= UINT32_MAX)) || |
| 2707 | (reloc_type(rel) == R_X86_64_32S && |
| 2708 | ((int64_t)value <= INT32_MAX && (int64_t)value >= INT32_MIN))); |
| 2709 | uint32_t truncated_addr = (value & 0xFFFFFFFF); |
| 2710 | DataBufferSP &data_buffer_sp = debug_data.GetSharedDataBuffer(); |
| 2711 | uint32_t *dst = reinterpret_cast<uint32_t *>( |
| 2712 | data_buffer_sp->GetBytes() + rel_section->GetFileOffset() + |
| 2713 | ELFRelocation::RelocOffset32(rel)); |
| 2714 | *dst = truncated_addr; |
| 2715 | } |
| 2716 | break; |
| 2717 | } |
| 2718 | case R_X86_64_PC32: |
| 2719 | default: |
| 2720 | assert(false && "unexpected relocation type"); |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | unsigned ObjectFileELF::RelocateDebugSections(const ELFSectionHeader *rel_hdr, |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2729 | user_id_t rel_id, |
| 2730 | lldb_private::Symtab *thetab) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2731 | assert(rel_hdr->sh_type == SHT_RELA || rel_hdr->sh_type == SHT_REL); |
| 2732 | |
| 2733 | // Parse in the section list if needed. |
| 2734 | SectionList *section_list = GetSectionList(); |
| 2735 | if (!section_list) |
| 2736 | return 0; |
| 2737 | |
| 2738 | // Section ID's are ones based. |
| 2739 | user_id_t symtab_id = rel_hdr->sh_link + 1; |
| 2740 | user_id_t debug_id = rel_hdr->sh_info + 1; |
| 2741 | |
| 2742 | const ELFSectionHeader *symtab_hdr = GetSectionHeaderByIndex(symtab_id); |
| 2743 | if (!symtab_hdr) |
| 2744 | return 0; |
| 2745 | |
| 2746 | const ELFSectionHeader *debug_hdr = GetSectionHeaderByIndex(debug_id); |
| 2747 | if (!debug_hdr) |
| 2748 | return 0; |
| 2749 | |
| 2750 | Section *rel = section_list->FindSectionByID(rel_id).get(); |
| 2751 | if (!rel) |
| 2752 | return 0; |
| 2753 | |
| 2754 | Section *symtab = section_list->FindSectionByID(symtab_id).get(); |
| 2755 | if (!symtab) |
| 2756 | return 0; |
| 2757 | |
| 2758 | Section *debug = section_list->FindSectionByID(debug_id).get(); |
| 2759 | if (!debug) |
| 2760 | return 0; |
| 2761 | |
| 2762 | DataExtractor rel_data; |
| 2763 | DataExtractor symtab_data; |
| 2764 | DataExtractor debug_data; |
| 2765 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2766 | if (GetData(rel->GetFileOffset(), rel->GetFileSize(), rel_data) && |
| 2767 | GetData(symtab->GetFileOffset(), symtab->GetFileSize(), symtab_data) && |
| 2768 | GetData(debug->GetFileOffset(), debug->GetFileSize(), debug_data)) { |
| 2769 | ApplyRelocations(thetab, &m_header, rel_hdr, symtab_hdr, debug_hdr, |
| 2770 | rel_data, symtab_data, debug_data, debug); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | return 0; |
| 2774 | } |
| 2775 | |
| 2776 | Symtab *ObjectFileELF::GetSymtab() { |
| 2777 | ModuleSP module_sp(GetModule()); |
| 2778 | if (!module_sp) |
| 2779 | return NULL; |
| 2780 | |
| 2781 | // We always want to use the main object file so we (hopefully) only have one |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2782 | // cached copy of our symtab, dynamic sections, etc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2783 | ObjectFile *module_obj_file = module_sp->GetObjectFile(); |
| 2784 | if (module_obj_file && module_obj_file != this) |
| 2785 | return module_obj_file->GetSymtab(); |
| 2786 | |
| 2787 | if (m_symtab_ap.get() == NULL) { |
| 2788 | SectionList *section_list = module_sp->GetSectionList(); |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 2789 | if (!section_list) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2790 | return NULL; |
Ashok Thirumurthi | 35729bb | 2013-09-24 15:34:13 +0000 | [diff] [blame] | 2791 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2792 | uint64_t symbol_id = 0; |
| 2793 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 2794 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2795 | // Sharable objects and dynamic executables usually have 2 distinct symbol |
| 2796 | // tables, one named ".symtab", and the other ".dynsym". The dynsym is a |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2797 | // smaller version of the symtab that only contains global symbols. The |
| 2798 | // information found in the dynsym is therefore also found in the symtab, |
| 2799 | // while the reverse is not necessarily true. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2800 | Section *symtab = |
| 2801 | section_list->FindSectionByType(eSectionTypeELFSymbolTable, true).get(); |
| 2802 | if (!symtab) { |
| 2803 | // The symtab section is non-allocable and can be stripped, so if it |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2804 | // doesn't exist then use the dynsym section which should always be |
| 2805 | // there. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2806 | symtab = |
| 2807 | section_list->FindSectionByType(eSectionTypeELFDynamicSymbols, true) |
| 2808 | .get(); |
| 2809 | } |
| 2810 | if (symtab) { |
| 2811 | m_symtab_ap.reset(new Symtab(symtab->GetObjectFile())); |
| 2812 | symbol_id += ParseSymbolTable(m_symtab_ap.get(), symbol_id, symtab); |
| 2813 | } |
| 2814 | |
| 2815 | // DT_JMPREL |
| 2816 | // If present, this entry's d_ptr member holds the address of |
| 2817 | // relocation |
| 2818 | // entries associated solely with the procedure linkage table. |
| 2819 | // Separating |
| 2820 | // these relocation entries lets the dynamic linker ignore them during |
| 2821 | // process initialization, if lazy binding is enabled. If this entry is |
| 2822 | // present, the related entries of types DT_PLTRELSZ and DT_PLTREL must |
| 2823 | // also be present. |
| 2824 | const ELFDynamic *symbol = FindDynamicSymbol(DT_JMPREL); |
| 2825 | if (symbol) { |
| 2826 | // Synthesize trampoline symbols to help navigate the PLT. |
| 2827 | addr_t addr = symbol->d_ptr; |
| 2828 | Section *reloc_section = |
| 2829 | section_list->FindSectionContainingFileAddress(addr).get(); |
| 2830 | if (reloc_section) { |
| 2831 | user_id_t reloc_id = reloc_section->GetID(); |
| 2832 | const ELFSectionHeaderInfo *reloc_header = |
| 2833 | GetSectionHeaderByIndex(reloc_id); |
| 2834 | assert(reloc_header); |
| 2835 | |
| 2836 | if (m_symtab_ap == nullptr) |
| 2837 | m_symtab_ap.reset(new Symtab(reloc_section->GetObjectFile())); |
| 2838 | |
| 2839 | ParseTrampolineSymbols(m_symtab_ap.get(), symbol_id, reloc_header, |
| 2840 | reloc_id); |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | DWARFCallFrameInfo *eh_frame = GetUnwindTable().GetEHFrameInfo(); |
| 2845 | if (eh_frame) { |
| 2846 | if (m_symtab_ap == nullptr) |
| 2847 | m_symtab_ap.reset(new Symtab(this)); |
| 2848 | ParseUnwindSymbols(m_symtab_ap.get(), eh_frame); |
| 2849 | } |
| 2850 | |
| 2851 | // If we still don't have any symtab then create an empty instance to avoid |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2852 | // do the section lookup next time. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2853 | if (m_symtab_ap == nullptr) |
| 2854 | m_symtab_ap.reset(new Symtab(this)); |
| 2855 | |
| 2856 | m_symtab_ap->CalculateSymbolSizes(); |
| 2857 | } |
| 2858 | |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2859 | return m_symtab_ap.get(); |
| 2860 | } |
| 2861 | |
| 2862 | void ObjectFileELF::RelocateSection(lldb_private::Section *section) |
| 2863 | { |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 2864 | static const char *debug_prefix = ".debug"; |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2865 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2866 | // Set relocated bit so we stop getting called, regardless of whether we |
| 2867 | // actually relocate. |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2868 | section->SetIsRelocated(true); |
| 2869 | |
| 2870 | // We only relocate in ELF relocatable files |
| 2871 | if (CalculateType() != eTypeObjectFile) |
| 2872 | return; |
| 2873 | |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 2874 | const char *section_name = section->GetName().GetCString(); |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2875 | // Can't relocate that which can't be named |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 2876 | if (section_name == nullptr) |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2877 | return; |
| 2878 | |
| 2879 | // We don't relocate non-debug sections at the moment |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 2880 | if (strncmp(section_name, debug_prefix, strlen(debug_prefix))) |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2881 | return; |
| 2882 | |
| 2883 | // Relocation section names to look for |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 2884 | std::string needle = std::string(".rel") + section_name; |
| 2885 | std::string needlea = std::string(".rela") + section_name; |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2886 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2887 | for (SectionHeaderCollIter I = m_section_headers.begin(); |
| 2888 | I != m_section_headers.end(); ++I) { |
| 2889 | if (I->sh_type == SHT_RELA || I->sh_type == SHT_REL) { |
Ed Maste | d13f691 | 2017-10-02 14:35:07 +0000 | [diff] [blame] | 2890 | const char *hay_name = I->section_name.GetCString(); |
| 2891 | if (hay_name == nullptr) |
| 2892 | continue; |
| 2893 | if (needle == hay_name || needlea == hay_name) { |
| 2894 | const ELFSectionHeader &reloc_header = *I; |
| 2895 | user_id_t reloc_id = SectionIndex(I); |
| 2896 | RelocateDebugSections(&reloc_header, reloc_id, GetSymtab()); |
| 2897 | break; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2898 | } |
| 2899 | } |
| 2900 | } |
Tamas Berghammer | 6b63b14 | 2016-02-18 11:12:18 +0000 | [diff] [blame] | 2901 | } |
Tamas Berghammer | 5bfd4d0 | 2016-02-10 12:10:58 +0000 | [diff] [blame] | 2902 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2903 | void ObjectFileELF::ParseUnwindSymbols(Symtab *symbol_table, |
| 2904 | DWARFCallFrameInfo *eh_frame) { |
| 2905 | SectionList *section_list = GetSectionList(); |
| 2906 | if (!section_list) |
| 2907 | return; |
| 2908 | |
| 2909 | // First we save the new symbols into a separate list and add them to the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2910 | // symbol table after we colleced all symbols we want to add. This is |
Davide Italiano | 1e6a01f | 2018-05-12 01:25:48 +0000 | [diff] [blame] | 2911 | // neccessary because adding a new symbol invalidates the internal index of |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 2912 | // the symtab what causing the next lookup to be slow because it have to |
| 2913 | // recalculate the index first. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2914 | std::vector<Symbol> new_symbols; |
| 2915 | |
| 2916 | eh_frame->ForEachFDEEntries([this, symbol_table, section_list, &new_symbols]( |
| 2917 | lldb::addr_t file_addr, uint32_t size, dw_offset_t) { |
| 2918 | Symbol *symbol = symbol_table->FindSymbolAtFileAddress(file_addr); |
| 2919 | if (symbol) { |
| 2920 | if (!symbol->GetByteSizeIsValid()) { |
| 2921 | symbol->SetByteSize(size); |
| 2922 | symbol->SetSizeIsSynthesized(true); |
| 2923 | } |
| 2924 | } else { |
| 2925 | SectionSP section_sp = |
| 2926 | section_list->FindSectionContainingFileAddress(file_addr); |
| 2927 | if (section_sp) { |
| 2928 | addr_t offset = file_addr - section_sp->GetFileAddress(); |
| 2929 | const char *symbol_name = GetNextSyntheticSymbolName().GetCString(); |
| 2930 | uint64_t symbol_id = symbol_table->GetNumSymbols(); |
| 2931 | Symbol eh_symbol( |
| 2932 | symbol_id, // Symbol table index. |
| 2933 | symbol_name, // Symbol name. |
| 2934 | false, // Is the symbol name mangled? |
| 2935 | eSymbolTypeCode, // Type of this symbol. |
| 2936 | true, // Is this globally visible? |
| 2937 | false, // Is this symbol debug info? |
| 2938 | false, // Is this symbol a trampoline? |
| 2939 | true, // Is this symbol artificial? |
| 2940 | section_sp, // Section in which this symbol is defined or null. |
| 2941 | offset, // Offset in section or symbol value. |
| 2942 | 0, // Size: Don't specify the size as an FDE can |
| 2943 | false, // Size is valid: cover multiple symbols. |
| 2944 | false, // Contains linker annotations? |
| 2945 | 0); // Symbol flags. |
| 2946 | new_symbols.push_back(eh_symbol); |
| 2947 | } |
| 2948 | } |
| 2949 | return true; |
| 2950 | }); |
| 2951 | |
| 2952 | for (const Symbol &s : new_symbols) |
| 2953 | symbol_table->AddSymbol(s); |
| 2954 | } |
| 2955 | |
| 2956 | bool ObjectFileELF::IsStripped() { |
| 2957 | // TODO: determine this for ELF |
| 2958 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2961 | //===----------------------------------------------------------------------===// |
| 2962 | // Dump |
| 2963 | // |
| 2964 | // Dump the specifics of the runtime file container (such as any headers |
| 2965 | // segments, sections, etc). |
| 2966 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2967 | void ObjectFileELF::Dump(Stream *s) { |
| 2968 | ModuleSP module_sp(GetModule()); |
| 2969 | if (!module_sp) { |
| 2970 | return; |
| 2971 | } |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 2972 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2973 | std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); |
| 2974 | s->Printf("%p: ", static_cast<void *>(this)); |
| 2975 | s->Indent(); |
| 2976 | s->PutCString("ObjectFileELF"); |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 2977 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2978 | ArchSpec header_arch; |
| 2979 | GetArchitecture(header_arch); |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 2980 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2981 | *s << ", file = '" << m_file |
| 2982 | << "', arch = " << header_arch.GetArchitectureName() << "\n"; |
Adrian McCarthy | 543725c | 2016-04-04 21:21:49 +0000 | [diff] [blame] | 2983 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 2984 | DumpELFHeader(s, m_header); |
| 2985 | s->EOL(); |
| 2986 | DumpELFProgramHeaders(s); |
| 2987 | s->EOL(); |
| 2988 | DumpELFSectionHeaders(s); |
| 2989 | s->EOL(); |
| 2990 | SectionList *section_list = GetSectionList(); |
| 2991 | if (section_list) |
| 2992 | section_list->Dump(s, NULL, true, UINT32_MAX); |
| 2993 | Symtab *symtab = GetSymtab(); |
| 2994 | if (symtab) |
| 2995 | symtab->Dump(s, NULL, eSortOrderNone); |
| 2996 | s->EOL(); |
| 2997 | DumpDependentModules(s); |
| 2998 | s->EOL(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 2999 | } |
| 3000 | |
| 3001 | //---------------------------------------------------------------------- |
| 3002 | // DumpELFHeader |
| 3003 | // |
| 3004 | // Dump the ELF header to the specified output stream |
| 3005 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3006 | void ObjectFileELF::DumpELFHeader(Stream *s, const ELFHeader &header) { |
| 3007 | s->PutCString("ELF Header\n"); |
| 3008 | s->Printf("e_ident[EI_MAG0 ] = 0x%2.2x\n", header.e_ident[EI_MAG0]); |
| 3009 | s->Printf("e_ident[EI_MAG1 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG1], |
| 3010 | header.e_ident[EI_MAG1]); |
| 3011 | s->Printf("e_ident[EI_MAG2 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG2], |
| 3012 | header.e_ident[EI_MAG2]); |
| 3013 | s->Printf("e_ident[EI_MAG3 ] = 0x%2.2x '%c'\n", header.e_ident[EI_MAG3], |
| 3014 | header.e_ident[EI_MAG3]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3015 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3016 | s->Printf("e_ident[EI_CLASS ] = 0x%2.2x\n", header.e_ident[EI_CLASS]); |
| 3017 | s->Printf("e_ident[EI_DATA ] = 0x%2.2x ", header.e_ident[EI_DATA]); |
| 3018 | DumpELFHeader_e_ident_EI_DATA(s, header.e_ident[EI_DATA]); |
| 3019 | s->Printf("\ne_ident[EI_VERSION] = 0x%2.2x\n", header.e_ident[EI_VERSION]); |
| 3020 | s->Printf("e_ident[EI_PAD ] = 0x%2.2x\n", header.e_ident[EI_PAD]); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3021 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3022 | s->Printf("e_type = 0x%4.4x ", header.e_type); |
| 3023 | DumpELFHeader_e_type(s, header.e_type); |
| 3024 | s->Printf("\ne_machine = 0x%4.4x\n", header.e_machine); |
| 3025 | s->Printf("e_version = 0x%8.8x\n", header.e_version); |
| 3026 | s->Printf("e_entry = 0x%8.8" PRIx64 "\n", header.e_entry); |
| 3027 | s->Printf("e_phoff = 0x%8.8" PRIx64 "\n", header.e_phoff); |
| 3028 | s->Printf("e_shoff = 0x%8.8" PRIx64 "\n", header.e_shoff); |
| 3029 | s->Printf("e_flags = 0x%8.8x\n", header.e_flags); |
| 3030 | s->Printf("e_ehsize = 0x%4.4x\n", header.e_ehsize); |
| 3031 | s->Printf("e_phentsize = 0x%4.4x\n", header.e_phentsize); |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 3032 | s->Printf("e_phnum = 0x%8.8x\n", header.e_phnum); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3033 | s->Printf("e_shentsize = 0x%4.4x\n", header.e_shentsize); |
Pavel Labath | 23ccc29 | 2017-01-31 23:09:46 +0000 | [diff] [blame] | 3034 | s->Printf("e_shnum = 0x%8.8x\n", header.e_shnum); |
| 3035 | s->Printf("e_shstrndx = 0x%8.8x\n", header.e_shstrndx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
| 3038 | //---------------------------------------------------------------------- |
| 3039 | // DumpELFHeader_e_type |
| 3040 | // |
| 3041 | // Dump an token value for the ELF header member e_type |
| 3042 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3043 | void ObjectFileELF::DumpELFHeader_e_type(Stream *s, elf_half e_type) { |
| 3044 | switch (e_type) { |
| 3045 | case ET_NONE: |
| 3046 | *s << "ET_NONE"; |
| 3047 | break; |
| 3048 | case ET_REL: |
| 3049 | *s << "ET_REL"; |
| 3050 | break; |
| 3051 | case ET_EXEC: |
| 3052 | *s << "ET_EXEC"; |
| 3053 | break; |
| 3054 | case ET_DYN: |
| 3055 | *s << "ET_DYN"; |
| 3056 | break; |
| 3057 | case ET_CORE: |
| 3058 | *s << "ET_CORE"; |
| 3059 | break; |
| 3060 | default: |
| 3061 | break; |
| 3062 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3063 | } |
| 3064 | |
| 3065 | //---------------------------------------------------------------------- |
| 3066 | // DumpELFHeader_e_ident_EI_DATA |
| 3067 | // |
| 3068 | // Dump an token value for the ELF header member e_ident[EI_DATA] |
| 3069 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3070 | void ObjectFileELF::DumpELFHeader_e_ident_EI_DATA(Stream *s, |
| 3071 | unsigned char ei_data) { |
| 3072 | switch (ei_data) { |
| 3073 | case ELFDATANONE: |
| 3074 | *s << "ELFDATANONE"; |
| 3075 | break; |
| 3076 | case ELFDATA2LSB: |
| 3077 | *s << "ELFDATA2LSB - Little Endian"; |
| 3078 | break; |
| 3079 | case ELFDATA2MSB: |
| 3080 | *s << "ELFDATA2MSB - Big Endian"; |
| 3081 | break; |
| 3082 | default: |
| 3083 | break; |
| 3084 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3085 | } |
| 3086 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3087 | //---------------------------------------------------------------------- |
| 3088 | // DumpELFProgramHeader |
| 3089 | // |
| 3090 | // Dump a single ELF program header to the specified output stream |
| 3091 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3092 | void ObjectFileELF::DumpELFProgramHeader(Stream *s, |
| 3093 | const ELFProgramHeader &ph) { |
| 3094 | DumpELFProgramHeader_p_type(s, ph.p_type); |
| 3095 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, ph.p_offset, |
| 3096 | ph.p_vaddr, ph.p_paddr); |
| 3097 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64 " %8.8x (", ph.p_filesz, ph.p_memsz, |
| 3098 | ph.p_flags); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3099 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3100 | DumpELFProgramHeader_p_flags(s, ph.p_flags); |
| 3101 | s->Printf(") %8.8" PRIx64, ph.p_align); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | //---------------------------------------------------------------------- |
| 3105 | // DumpELFProgramHeader_p_type |
| 3106 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3107 | // Dump an token value for the ELF program header member p_type which describes |
| 3108 | // the type of the program header |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3109 | // ---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3110 | void ObjectFileELF::DumpELFProgramHeader_p_type(Stream *s, elf_word p_type) { |
| 3111 | const int kStrWidth = 15; |
| 3112 | switch (p_type) { |
| 3113 | CASE_AND_STREAM(s, PT_NULL, kStrWidth); |
| 3114 | CASE_AND_STREAM(s, PT_LOAD, kStrWidth); |
| 3115 | CASE_AND_STREAM(s, PT_DYNAMIC, kStrWidth); |
| 3116 | CASE_AND_STREAM(s, PT_INTERP, kStrWidth); |
| 3117 | CASE_AND_STREAM(s, PT_NOTE, kStrWidth); |
| 3118 | CASE_AND_STREAM(s, PT_SHLIB, kStrWidth); |
| 3119 | CASE_AND_STREAM(s, PT_PHDR, kStrWidth); |
| 3120 | CASE_AND_STREAM(s, PT_TLS, kStrWidth); |
Filipe Cabecinhas | 477d86d | 2013-05-23 23:01:14 +0000 | [diff] [blame] | 3121 | CASE_AND_STREAM(s, PT_GNU_EH_FRAME, kStrWidth); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3122 | default: |
| 3123 | s->Printf("0x%8.8x%*s", p_type, kStrWidth - 10, ""); |
| 3124 | break; |
| 3125 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3126 | } |
| 3127 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3128 | //---------------------------------------------------------------------- |
| 3129 | // DumpELFProgramHeader_p_flags |
| 3130 | // |
| 3131 | // Dump an token value for the ELF program header member p_flags |
| 3132 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3133 | void ObjectFileELF::DumpELFProgramHeader_p_flags(Stream *s, elf_word p_flags) { |
| 3134 | *s << ((p_flags & PF_X) ? "PF_X" : " ") |
| 3135 | << (((p_flags & PF_X) && (p_flags & PF_W)) ? '+' : ' ') |
| 3136 | << ((p_flags & PF_W) ? "PF_W" : " ") |
| 3137 | << (((p_flags & PF_W) && (p_flags & PF_R)) ? '+' : ' ') |
| 3138 | << ((p_flags & PF_R) ? "PF_R" : " "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | //---------------------------------------------------------------------- |
| 3142 | // DumpELFProgramHeaders |
| 3143 | // |
| 3144 | // Dump all of the ELF program header to the specified output stream |
| 3145 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3146 | void ObjectFileELF::DumpELFProgramHeaders(Stream *s) { |
| 3147 | if (!ParseProgramHeaders()) |
| 3148 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3150 | s->PutCString("Program Headers\n"); |
| 3151 | s->PutCString("IDX p_type p_offset p_vaddr p_paddr " |
| 3152 | "p_filesz p_memsz p_flags p_align\n"); |
| 3153 | s->PutCString("==== --------------- -------- -------- -------- " |
| 3154 | "-------- -------- ------------------------- --------\n"); |
Ed Maste | 3a8ab6e | 2015-02-23 15:33:11 +0000 | [diff] [blame] | 3155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3156 | uint32_t idx = 0; |
| 3157 | for (ProgramHeaderCollConstIter I = m_program_headers.begin(); |
| 3158 | I != m_program_headers.end(); ++I, ++idx) { |
| 3159 | s->Printf("[%2u] ", idx); |
| 3160 | ObjectFileELF::DumpELFProgramHeader(s, *I); |
| 3161 | s->EOL(); |
| 3162 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3165 | //---------------------------------------------------------------------- |
| 3166 | // DumpELFSectionHeader |
| 3167 | // |
| 3168 | // Dump a single ELF section header to the specified output stream |
| 3169 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3170 | void ObjectFileELF::DumpELFSectionHeader(Stream *s, |
| 3171 | const ELFSectionHeaderInfo &sh) { |
| 3172 | s->Printf("%8.8x ", sh.sh_name); |
| 3173 | DumpELFSectionHeader_sh_type(s, sh.sh_type); |
| 3174 | s->Printf(" %8.8" PRIx64 " (", sh.sh_flags); |
| 3175 | DumpELFSectionHeader_sh_flags(s, sh.sh_flags); |
| 3176 | s->Printf(") %8.8" PRIx64 " %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addr, |
| 3177 | sh.sh_offset, sh.sh_size); |
| 3178 | s->Printf(" %8.8x %8.8x", sh.sh_link, sh.sh_info); |
| 3179 | s->Printf(" %8.8" PRIx64 " %8.8" PRIx64, sh.sh_addralign, sh.sh_entsize); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | //---------------------------------------------------------------------- |
| 3183 | // DumpELFSectionHeader_sh_type |
| 3184 | // |
| 3185 | // Dump an token value for the ELF section header member sh_type which |
| 3186 | // describes the type of the section |
| 3187 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3188 | void ObjectFileELF::DumpELFSectionHeader_sh_type(Stream *s, elf_word sh_type) { |
| 3189 | const int kStrWidth = 12; |
| 3190 | switch (sh_type) { |
| 3191 | CASE_AND_STREAM(s, SHT_NULL, kStrWidth); |
| 3192 | CASE_AND_STREAM(s, SHT_PROGBITS, kStrWidth); |
| 3193 | CASE_AND_STREAM(s, SHT_SYMTAB, kStrWidth); |
| 3194 | CASE_AND_STREAM(s, SHT_STRTAB, kStrWidth); |
| 3195 | CASE_AND_STREAM(s, SHT_RELA, kStrWidth); |
| 3196 | CASE_AND_STREAM(s, SHT_HASH, kStrWidth); |
| 3197 | CASE_AND_STREAM(s, SHT_DYNAMIC, kStrWidth); |
| 3198 | CASE_AND_STREAM(s, SHT_NOTE, kStrWidth); |
| 3199 | CASE_AND_STREAM(s, SHT_NOBITS, kStrWidth); |
| 3200 | CASE_AND_STREAM(s, SHT_REL, kStrWidth); |
| 3201 | CASE_AND_STREAM(s, SHT_SHLIB, kStrWidth); |
| 3202 | CASE_AND_STREAM(s, SHT_DYNSYM, kStrWidth); |
| 3203 | CASE_AND_STREAM(s, SHT_LOPROC, kStrWidth); |
| 3204 | CASE_AND_STREAM(s, SHT_HIPROC, kStrWidth); |
| 3205 | CASE_AND_STREAM(s, SHT_LOUSER, kStrWidth); |
| 3206 | CASE_AND_STREAM(s, SHT_HIUSER, kStrWidth); |
| 3207 | default: |
| 3208 | s->Printf("0x%8.8x%*s", sh_type, kStrWidth - 10, ""); |
| 3209 | break; |
| 3210 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3211 | } |
| 3212 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3213 | //---------------------------------------------------------------------- |
| 3214 | // DumpELFSectionHeader_sh_flags |
| 3215 | // |
| 3216 | // Dump an token value for the ELF section header member sh_flags |
| 3217 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3218 | void ObjectFileELF::DumpELFSectionHeader_sh_flags(Stream *s, |
| 3219 | elf_xword sh_flags) { |
| 3220 | *s << ((sh_flags & SHF_WRITE) ? "WRITE" : " ") |
| 3221 | << (((sh_flags & SHF_WRITE) && (sh_flags & SHF_ALLOC)) ? '+' : ' ') |
| 3222 | << ((sh_flags & SHF_ALLOC) ? "ALLOC" : " ") |
| 3223 | << (((sh_flags & SHF_ALLOC) && (sh_flags & SHF_EXECINSTR)) ? '+' : ' ') |
| 3224 | << ((sh_flags & SHF_EXECINSTR) ? "EXECINSTR" : " "); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3225 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3226 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3227 | //---------------------------------------------------------------------- |
| 3228 | // DumpELFSectionHeaders |
| 3229 | // |
| 3230 | // Dump all of the ELF section header to the specified output stream |
| 3231 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3232 | void ObjectFileELF::DumpELFSectionHeaders(Stream *s) { |
| 3233 | if (!ParseSectionHeaders()) |
| 3234 | return; |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3235 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3236 | s->PutCString("Section Headers\n"); |
| 3237 | s->PutCString("IDX name type flags " |
| 3238 | "addr offset size link info addralgn " |
| 3239 | "entsize Name\n"); |
| 3240 | s->PutCString("==== -------- ------------ -------------------------------- " |
| 3241 | "-------- -------- -------- -------- -------- -------- " |
| 3242 | "-------- ====================\n"); |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3243 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3244 | uint32_t idx = 0; |
| 3245 | for (SectionHeaderCollConstIter I = m_section_headers.begin(); |
| 3246 | I != m_section_headers.end(); ++I, ++idx) { |
| 3247 | s->Printf("[%2u] ", idx); |
| 3248 | ObjectFileELF::DumpELFSectionHeader(s, *I); |
| 3249 | const char *section_name = I->section_name.AsCString(""); |
| 3250 | if (section_name) |
| 3251 | *s << ' ' << section_name << "\n"; |
| 3252 | } |
Stephen Wilson | f325ba9 | 2010-07-13 23:07:23 +0000 | [diff] [blame] | 3253 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3254 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3255 | void ObjectFileELF::DumpDependentModules(lldb_private::Stream *s) { |
| 3256 | size_t num_modules = ParseDependentModules(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3257 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3258 | if (num_modules > 0) { |
| 3259 | s->PutCString("Dependent Modules:\n"); |
| 3260 | for (unsigned i = 0; i < num_modules; ++i) { |
| 3261 | const FileSpec &spec = m_filespec_ap->GetFileSpecAtIndex(i); |
| 3262 | s->Printf(" %s\n", spec.GetFilename().GetCString()); |
| 3263 | } |
| 3264 | } |
| 3265 | } |
| 3266 | |
| 3267 | bool ObjectFileELF::GetArchitecture(ArchSpec &arch) { |
| 3268 | if (!ParseHeader()) |
| 3269 | return false; |
| 3270 | |
| 3271 | if (m_section_headers.empty()) { |
| 3272 | // Allow elf notes to be parsed which may affect the detected architecture. |
| 3273 | ParseSectionHeaders(); |
| 3274 | } |
| 3275 | |
| 3276 | if (CalculateType() == eTypeCoreFile && |
| 3277 | m_arch_spec.TripleOSIsUnspecifiedUnknown()) { |
| 3278 | // Core files don't have section headers yet they have PT_NOTE program |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3279 | // headers that might shed more light on the architecture |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3280 | if (ParseProgramHeaders()) { |
Kamil Rytarowski | 12801f1 | 2017-03-26 15:34:57 +0000 | [diff] [blame] | 3281 | for (size_t i = 1, count = GetProgramHeaderCount(); i <= count; ++i) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3282 | const elf::ELFProgramHeader *header = GetProgramHeaderByIndex(i); |
| 3283 | if (header && header->p_type == PT_NOTE && header->p_offset != 0 && |
| 3284 | header->p_filesz > 0) { |
| 3285 | DataExtractor data; |
| 3286 | if (data.SetData(m_data, header->p_offset, header->p_filesz) == |
| 3287 | header->p_filesz) { |
| 3288 | lldb_private::UUID uuid; |
| 3289 | RefineModuleDetailsFromNote(data, m_arch_spec, uuid); |
| 3290 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3291 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3292 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3293 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3294 | } |
| 3295 | arch = m_arch_spec; |
| 3296 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3299 | ObjectFile::Type ObjectFileELF::CalculateType() { |
| 3300 | switch (m_header.e_type) { |
| 3301 | case llvm::ELF::ET_NONE: |
| 3302 | // 0 - No file type |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3303 | return eTypeUnknown; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3304 | |
| 3305 | case llvm::ELF::ET_REL: |
| 3306 | // 1 - Relocatable file |
| 3307 | return eTypeObjectFile; |
| 3308 | |
| 3309 | case llvm::ELF::ET_EXEC: |
| 3310 | // 2 - Executable file |
| 3311 | return eTypeExecutable; |
| 3312 | |
| 3313 | case llvm::ELF::ET_DYN: |
| 3314 | // 3 - Shared object file |
| 3315 | return eTypeSharedLibrary; |
| 3316 | |
| 3317 | case ET_CORE: |
| 3318 | // 4 - Core file |
| 3319 | return eTypeCoreFile; |
| 3320 | |
| 3321 | default: |
| 3322 | break; |
| 3323 | } |
| 3324 | return eTypeUnknown; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3327 | ObjectFile::Strata ObjectFileELF::CalculateStrata() { |
| 3328 | switch (m_header.e_type) { |
| 3329 | case llvm::ELF::ET_NONE: |
| 3330 | // 0 - No file type |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3331 | return eStrataUnknown; |
Greg Clayton | 9e00b6a65 | 2011-07-09 00:41:34 +0000 | [diff] [blame] | 3332 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3333 | case llvm::ELF::ET_REL: |
| 3334 | // 1 - Relocatable file |
| 3335 | return eStrataUnknown; |
| 3336 | |
| 3337 | case llvm::ELF::ET_EXEC: |
| 3338 | // 2 - Executable file |
| 3339 | // TODO: is there any way to detect that an executable is a kernel |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3340 | // related executable by inspecting the program headers, section headers, |
| 3341 | // symbols, or any other flag bits??? |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3342 | return eStrataUser; |
| 3343 | |
| 3344 | case llvm::ELF::ET_DYN: |
| 3345 | // 3 - Shared object file |
| 3346 | // TODO: is there any way to detect that an shared library is a kernel |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3347 | // related executable by inspecting the program headers, section headers, |
| 3348 | // symbols, or any other flag bits??? |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3349 | return eStrataUnknown; |
| 3350 | |
| 3351 | case ET_CORE: |
| 3352 | // 4 - Core file |
| 3353 | // TODO: is there any way to detect that an core file is a kernel |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3354 | // related executable by inspecting the program headers, section headers, |
| 3355 | // symbols, or any other flag bits??? |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 3356 | return eStrataUnknown; |
| 3357 | |
| 3358 | default: |
| 3359 | break; |
| 3360 | } |
| 3361 | return eStrataUnknown; |
| 3362 | } |
Pavel Labath | e2867bc | 2017-12-15 14:23:58 +0000 | [diff] [blame] | 3363 | |
| 3364 | size_t ObjectFileELF::ReadSectionData(Section *section, |
| 3365 | lldb::offset_t section_offset, void *dst, |
| 3366 | size_t dst_len) { |
| 3367 | // If some other objectfile owns this data, pass this to them. |
| 3368 | if (section->GetObjectFile() != this) |
| 3369 | return section->GetObjectFile()->ReadSectionData(section, section_offset, |
| 3370 | dst, dst_len); |
| 3371 | |
| 3372 | if (!section->Test(SHF_COMPRESSED)) |
| 3373 | return ObjectFile::ReadSectionData(section, section_offset, dst, dst_len); |
| 3374 | |
| 3375 | // For compressed sections we need to read to full data to be able to |
| 3376 | // decompress. |
| 3377 | DataExtractor data; |
| 3378 | ReadSectionData(section, data); |
| 3379 | return data.CopyData(section_offset, dst_len, dst); |
| 3380 | } |
| 3381 | |
| 3382 | size_t ObjectFileELF::ReadSectionData(Section *section, |
| 3383 | DataExtractor §ion_data) { |
| 3384 | // If some other objectfile owns this data, pass this to them. |
| 3385 | if (section->GetObjectFile() != this) |
| 3386 | return section->GetObjectFile()->ReadSectionData(section, section_data); |
| 3387 | |
| 3388 | Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_MODULES); |
| 3389 | |
| 3390 | size_t result = ObjectFile::ReadSectionData(section, section_data); |
| 3391 | if (result == 0 || !section->Test(SHF_COMPRESSED)) |
| 3392 | return result; |
| 3393 | |
| 3394 | auto Decompressor = llvm::object::Decompressor::create( |
| 3395 | section->GetName().GetStringRef(), |
| 3396 | {reinterpret_cast<const char *>(section_data.GetDataStart()), |
Pavel Labath | 4c2eb8b | 2017-12-15 14:39:12 +0000 | [diff] [blame] | 3397 | size_t(section_data.GetByteSize())}, |
Pavel Labath | e2867bc | 2017-12-15 14:23:58 +0000 | [diff] [blame] | 3398 | GetByteOrder() == eByteOrderLittle, GetAddressByteSize() == 8); |
| 3399 | if (!Decompressor) { |
Pavel Labath | c561a6a | 2018-01-30 12:19:34 +0000 | [diff] [blame] | 3400 | LLDB_LOG_ERROR(log, Decompressor.takeError(), |
| 3401 | "Unable to initialize decompressor for section {0}", |
| 3402 | section->GetName()); |
Pavel Labath | e2867bc | 2017-12-15 14:23:58 +0000 | [diff] [blame] | 3403 | return result; |
| 3404 | } |
| 3405 | auto buffer_sp = |
| 3406 | std::make_shared<DataBufferHeap>(Decompressor->getDecompressedSize(), 0); |
| 3407 | if (auto Error = Decompressor->decompress( |
| 3408 | {reinterpret_cast<char *>(buffer_sp->GetBytes()), |
Pavel Labath | 4c2eb8b | 2017-12-15 14:39:12 +0000 | [diff] [blame] | 3409 | size_t(buffer_sp->GetByteSize())})) { |
Pavel Labath | c561a6a | 2018-01-30 12:19:34 +0000 | [diff] [blame] | 3410 | LLDB_LOG_ERROR(log, std::move(Error), "Decompression of section {0} failed", |
| 3411 | section->GetName()); |
Pavel Labath | e2867bc | 2017-12-15 14:23:58 +0000 | [diff] [blame] | 3412 | return result; |
| 3413 | } |
| 3414 | section_data.SetData(buffer_sp); |
| 3415 | return buffer_sp->GetByteSize(); |
| 3416 | } |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 3417 | |
| 3418 | bool ObjectFileELF::AnySegmentHasPhysicalAddress() { |
| 3419 | size_t header_count = ParseProgramHeaders(); |
| 3420 | for (size_t i = 1; i <= header_count; ++i) { |
| 3421 | auto header = GetProgramHeaderByIndex(i); |
| 3422 | if (header->p_paddr != 0) |
| 3423 | return true; |
| 3424 | } |
| 3425 | return false; |
| 3426 | } |
| 3427 | |
| 3428 | std::vector<ObjectFile::LoadableData> |
| 3429 | ObjectFileELF::GetLoadableData(Target &target) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 3430 | // Create a list of loadable data from loadable segments, using physical |
| 3431 | // addresses if they aren't all null |
Pavel Labath | 16064d3 | 2018-03-20 11:56:24 +0000 | [diff] [blame] | 3432 | std::vector<LoadableData> loadables; |
| 3433 | size_t header_count = ParseProgramHeaders(); |
| 3434 | bool should_use_paddr = AnySegmentHasPhysicalAddress(); |
| 3435 | for (size_t i = 1; i <= header_count; ++i) { |
| 3436 | LoadableData loadable; |
| 3437 | auto header = GetProgramHeaderByIndex(i); |
| 3438 | if (header->p_type != llvm::ELF::PT_LOAD) |
| 3439 | continue; |
| 3440 | loadable.Dest = should_use_paddr ? header->p_paddr : header->p_vaddr; |
| 3441 | if (loadable.Dest == LLDB_INVALID_ADDRESS) |
| 3442 | continue; |
| 3443 | if (header->p_filesz == 0) |
| 3444 | continue; |
| 3445 | auto segment_data = GetSegmentDataByIndex(i); |
| 3446 | loadable.Contents = llvm::ArrayRef<uint8_t>(segment_data.GetDataStart(), |
| 3447 | segment_data.GetByteSize()); |
| 3448 | loadables.push_back(loadable); |
| 3449 | } |
| 3450 | return loadables; |
| 3451 | } |