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