Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 1 | //===-- RuntimeDyldMachOAArch64.h -- MachO/AArch64 specific code. -*- C++ -*-=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 10 | #ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H |
| 11 | #define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 12 | |
| 13 | #include "../RuntimeDyldMachO.h" |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Endian.h" |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 15 | |
| 16 | #define DEBUG_TYPE "dyld" |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
| 20 | class RuntimeDyldMachOAArch64 |
| 21 | : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOAArch64> { |
| 22 | public: |
Lang Hames | eb195f0 | 2014-09-04 04:53:03 +0000 | [diff] [blame] | 23 | |
| 24 | typedef uint64_t TargetPtrT; |
| 25 | |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 26 | RuntimeDyldMachOAArch64(RuntimeDyld::MemoryManager &MM, |
Lang Hames | ad4a911 | 2016-08-01 20:49:11 +0000 | [diff] [blame] | 27 | JITSymbolResolver &Resolver) |
Lang Hames | 633fe14 | 2015-03-30 03:37:06 +0000 | [diff] [blame] | 28 | : RuntimeDyldMachOCRTPBase(MM, Resolver) {} |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 29 | |
| 30 | unsigned getMaxStubSize() override { return 8; } |
| 31 | |
Lang Hames | e5fc826 | 2014-07-17 23:11:30 +0000 | [diff] [blame] | 32 | unsigned getStubAlignment() override { return 8; } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 33 | |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 34 | /// Extract the addend encoded in the instruction / memory location. |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 35 | int64_t decodeAddend(const RelocationEntry &RE) const { |
| 36 | const SectionEntry &Section = Sections[RE.SectionID]; |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 37 | uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 38 | unsigned NumBytes = 1 << RE.Size; |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 39 | int64_t Addend = 0; |
| 40 | // Verify that the relocation has the correct size and alignment. |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 41 | switch (RE.RelType) { |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 42 | default: |
| 43 | llvm_unreachable("Unsupported relocation type!"); |
| 44 | case MachO::ARM64_RELOC_UNSIGNED: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 45 | assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size."); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 46 | break; |
| 47 | case MachO::ARM64_RELOC_BRANCH26: |
| 48 | case MachO::ARM64_RELOC_PAGE21: |
| 49 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 50 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 51 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
| 52 | assert(NumBytes == 4 && "Invalid relocation size."); |
| 53 | assert((((uintptr_t)LocalAddress & 0x3) == 0) && |
| 54 | "Instruction address is not aligned to 4 bytes."); |
| 55 | break; |
| 56 | } |
| 57 | |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 58 | switch (RE.RelType) { |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 59 | default: |
| 60 | llvm_unreachable("Unsupported relocation type!"); |
| 61 | case MachO::ARM64_RELOC_UNSIGNED: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 62 | // This could be an unaligned memory location. |
| 63 | if (NumBytes == 4) |
| 64 | Addend = *reinterpret_cast<support::ulittle32_t *>(LocalAddress); |
| 65 | else |
| 66 | Addend = *reinterpret_cast<support::ulittle64_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 67 | break; |
| 68 | case MachO::ARM64_RELOC_BRANCH26: { |
| 69 | // Verify that the relocation points to the expected branch instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 70 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 71 | assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction."); |
| 72 | |
| 73 | // Get the 26 bit addend encoded in the branch instruction and sign-extend |
| 74 | // to 64 bit. The lower 2 bits are always zeros and are therefore implicit |
| 75 | // (<< 2). |
| 76 | Addend = (*p & 0x03FFFFFF) << 2; |
| 77 | Addend = SignExtend64(Addend, 28); |
| 78 | break; |
| 79 | } |
| 80 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 81 | case MachO::ARM64_RELOC_PAGE21: { |
| 82 | // Verify that the relocation points to the expected adrp instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 83 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 84 | assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction."); |
| 85 | |
| 86 | // Get the 21 bit addend encoded in the adrp instruction and sign-extend |
| 87 | // to 64 bit. The lower 12 bits (4096 byte page) are always zeros and are |
| 88 | // therefore implicit (<< 12). |
| 89 | Addend = ((*p & 0x60000000) >> 29) | ((*p & 0x01FFFFE0) >> 3) << 12; |
| 90 | Addend = SignExtend64(Addend, 33); |
| 91 | break; |
| 92 | } |
| 93 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: { |
| 94 | // Verify that the relocation points to one of the expected load / store |
| 95 | // instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 96 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | 0e957cf | 2014-07-22 22:02:19 +0000 | [diff] [blame] | 97 | (void)p; |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 98 | assert((*p & 0x3B000000) == 0x39000000 && |
| 99 | "Only expected load / store instructions."); |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 100 | LLVM_FALLTHROUGH; |
| 101 | } |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 102 | case MachO::ARM64_RELOC_PAGEOFF12: { |
| 103 | // Verify that the relocation points to one of the expected load / store |
| 104 | // or add / sub instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 105 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 106 | assert((((*p & 0x3B000000) == 0x39000000) || |
| 107 | ((*p & 0x11C00000) == 0x11000000) ) && |
| 108 | "Expected load / store or add/sub instruction."); |
| 109 | |
| 110 | // Get the 12 bit addend encoded in the instruction. |
| 111 | Addend = (*p & 0x003FFC00) >> 10; |
| 112 | |
| 113 | // Check which instruction we are decoding to obtain the implicit shift |
| 114 | // factor of the instruction. |
| 115 | int ImplicitShift = 0; |
| 116 | if ((*p & 0x3B000000) == 0x39000000) { // << load / store |
| 117 | // For load / store instructions the size is encoded in bits 31:30. |
| 118 | ImplicitShift = ((*p >> 30) & 0x3); |
| 119 | if (ImplicitShift == 0) { |
| 120 | // Check if this a vector op to get the correct shift value. |
| 121 | if ((*p & 0x04800000) == 0x04800000) |
| 122 | ImplicitShift = 4; |
| 123 | } |
| 124 | } |
| 125 | // Compensate for implicit shift. |
| 126 | Addend <<= ImplicitShift; |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | return Addend; |
| 131 | } |
| 132 | |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 133 | /// Extract the addend encoded in the instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 134 | void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes, |
| 135 | MachO::RelocationInfoType RelType, int64_t Addend) const { |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 136 | // Verify that the relocation has the correct alignment. |
| 137 | switch (RelType) { |
| 138 | default: |
| 139 | llvm_unreachable("Unsupported relocation type!"); |
| 140 | case MachO::ARM64_RELOC_UNSIGNED: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 141 | assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size."); |
| 142 | break; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 143 | case MachO::ARM64_RELOC_BRANCH26: |
| 144 | case MachO::ARM64_RELOC_PAGE21: |
| 145 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 146 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 147 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 148 | assert(NumBytes == 4 && "Invalid relocation size."); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 149 | assert((((uintptr_t)LocalAddress & 0x3) == 0) && |
| 150 | "Instruction address is not aligned to 4 bytes."); |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | switch (RelType) { |
| 155 | default: |
| 156 | llvm_unreachable("Unsupported relocation type!"); |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 157 | case MachO::ARM64_RELOC_UNSIGNED: |
| 158 | // This could be an unaligned memory location. |
| 159 | if (NumBytes == 4) |
| 160 | *reinterpret_cast<support::ulittle32_t *>(LocalAddress) = Addend; |
| 161 | else |
| 162 | *reinterpret_cast<support::ulittle64_t *>(LocalAddress) = Addend; |
| 163 | break; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 164 | case MachO::ARM64_RELOC_BRANCH26: { |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 165 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 166 | // Verify that the relocation points to the expected branch instruction. |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 167 | assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction."); |
| 168 | |
| 169 | // Verify addend value. |
| 170 | assert((Addend & 0x3) == 0 && "Branch target is not aligned"); |
| 171 | assert(isInt<28>(Addend) && "Branch target is out of range."); |
| 172 | |
| 173 | // Encode the addend as 26 bit immediate in the branch instruction. |
| 174 | *p = (*p & 0xFC000000) | ((uint32_t)(Addend >> 2) & 0x03FFFFFF); |
| 175 | break; |
| 176 | } |
| 177 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 178 | case MachO::ARM64_RELOC_PAGE21: { |
| 179 | // Verify that the relocation points to the expected adrp instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 180 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 181 | assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction."); |
| 182 | |
| 183 | // Check that the addend fits into 21 bits (+ 12 lower bits). |
| 184 | assert((Addend & 0xFFF) == 0 && "ADRP target is not page aligned."); |
| 185 | assert(isInt<33>(Addend) && "Invalid page reloc value."); |
| 186 | |
| 187 | // Encode the addend into the instruction. |
Alexey Samsonov | 7c8a725 | 2015-01-10 00:46:38 +0000 | [diff] [blame] | 188 | uint32_t ImmLoValue = ((uint64_t)Addend << 17) & 0x60000000; |
| 189 | uint32_t ImmHiValue = ((uint64_t)Addend >> 9) & 0x00FFFFE0; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 190 | *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue; |
| 191 | break; |
| 192 | } |
| 193 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: { |
| 194 | // Verify that the relocation points to one of the expected load / store |
| 195 | // instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 196 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 197 | assert((*p & 0x3B000000) == 0x39000000 && |
| 198 | "Only expected load / store instructions."); |
NAKAMURA Takumi | ea4a8da | 2014-07-23 00:17:44 +0000 | [diff] [blame] | 199 | (void)p; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 200 | LLVM_FALLTHROUGH; |
| 201 | } |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 202 | case MachO::ARM64_RELOC_PAGEOFF12: { |
| 203 | // Verify that the relocation points to one of the expected load / store |
| 204 | // or add / sub instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 205 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 206 | assert((((*p & 0x3B000000) == 0x39000000) || |
| 207 | ((*p & 0x11C00000) == 0x11000000) ) && |
| 208 | "Expected load / store or add/sub instruction."); |
| 209 | |
| 210 | // Check which instruction we are decoding to obtain the implicit shift |
| 211 | // factor of the instruction and verify alignment. |
| 212 | int ImplicitShift = 0; |
| 213 | if ((*p & 0x3B000000) == 0x39000000) { // << load / store |
| 214 | // For load / store instructions the size is encoded in bits 31:30. |
| 215 | ImplicitShift = ((*p >> 30) & 0x3); |
| 216 | switch (ImplicitShift) { |
| 217 | case 0: |
| 218 | // Check if this a vector op to get the correct shift value. |
| 219 | if ((*p & 0x04800000) == 0x04800000) { |
| 220 | ImplicitShift = 4; |
| 221 | assert(((Addend & 0xF) == 0) && |
| 222 | "128-bit LDR/STR not 16-byte aligned."); |
| 223 | } |
| 224 | break; |
| 225 | case 1: |
| 226 | assert(((Addend & 0x1) == 0) && "16-bit LDR/STR not 2-byte aligned."); |
| 227 | break; |
| 228 | case 2: |
| 229 | assert(((Addend & 0x3) == 0) && "32-bit LDR/STR not 4-byte aligned."); |
| 230 | break; |
| 231 | case 3: |
| 232 | assert(((Addend & 0x7) == 0) && "64-bit LDR/STR not 8-byte aligned."); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | // Compensate for implicit shift. |
| 237 | Addend >>= ImplicitShift; |
| 238 | assert(isUInt<12>(Addend) && "Addend cannot be encoded."); |
| 239 | |
| 240 | // Encode the addend into the instruction. |
| 241 | *p = (*p & 0xFFC003FF) | ((uint32_t)(Addend << 10) & 0x003FFC00); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 247 | Expected<relocation_iterator> |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 248 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 249 | const ObjectFile &BaseObjT, |
| 250 | ObjSectionToIDMap &ObjSectionToID, |
Lang Hames | a5cd950 | 2014-11-27 05:40:13 +0000 | [diff] [blame] | 251 | StubMap &Stubs) override { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 252 | const MachOObjectFile &Obj = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 253 | static_cast<const MachOObjectFile &>(BaseObjT); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 254 | MachO::any_relocation_info RelInfo = |
| 255 | Obj.getRelocation(RelI->getRawDataRefImpl()); |
| 256 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 257 | if (Obj.isRelocationScattered(RelInfo)) |
| 258 | return make_error<RuntimeDyldError>("Scattered relocations not supported " |
| 259 | "for MachO AArch64"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 260 | |
| 261 | // ARM64 has an ARM64_RELOC_ADDEND relocation type that carries an explicit |
| 262 | // addend for the following relocation. If found: (1) store the associated |
| 263 | // addend, (2) consume the next relocation, and (3) use the stored addend to |
| 264 | // override the addend. |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 265 | int64_t ExplicitAddend = 0; |
| 266 | if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_ADDEND) { |
| 267 | assert(!Obj.getPlainRelocationExternal(RelInfo)); |
| 268 | assert(!Obj.getAnyRelocationPCRel(RelInfo)); |
| 269 | assert(Obj.getAnyRelocationLength(RelInfo) == 2); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 270 | int64_t RawAddend = Obj.getPlainRelocationSymbolNum(RelInfo); |
| 271 | // Sign-extend the 24-bit to 64-bit. |
Juergen Ributzka | dd19d33 | 2014-07-22 21:42:49 +0000 | [diff] [blame] | 272 | ExplicitAddend = SignExtend64(RawAddend, 24); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 273 | ++RelI; |
| 274 | RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl()); |
| 275 | } |
| 276 | |
Lang Hames | 3db630b | 2016-01-21 21:59:50 +0000 | [diff] [blame] | 277 | if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_SUBTRACTOR) |
| 278 | return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID); |
| 279 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame] | 280 | RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI)); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 281 | RE.Addend = decodeAddend(RE); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 282 | |
Juergen Ributzka | dd19d33 | 2014-07-22 21:42:49 +0000 | [diff] [blame] | 283 | assert((ExplicitAddend == 0 || RE.Addend == 0) && "Relocation has "\ |
| 284 | "ARM64_RELOC_ADDEND and embedded addend in the instruction."); |
Lang Hames | 0fd3610 | 2015-08-11 06:27:53 +0000 | [diff] [blame] | 285 | if (ExplicitAddend) |
Lang Hames | 76774a5 | 2014-07-18 20:29:36 +0000 | [diff] [blame] | 286 | RE.Addend = ExplicitAddend; |
Lang Hames | 0fd3610 | 2015-08-11 06:27:53 +0000 | [diff] [blame] | 287 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 288 | RelocationValueRef Value; |
| 289 | if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID)) |
| 290 | Value = *ValueOrErr; |
| 291 | else |
| 292 | return ValueOrErr.takeError(); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 293 | |
| 294 | bool IsExtern = Obj.getPlainRelocationExternal(RelInfo); |
| 295 | if (!IsExtern && RE.IsPCRel) |
Rafael Espindola | 76ad232 | 2015-07-06 14:55:37 +0000 | [diff] [blame] | 296 | makeValueAddendPCRel(Value, RelI, 1 << RE.Size); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 297 | |
Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame] | 298 | RE.Addend = Value.Offset; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 299 | |
| 300 | if (RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 || |
| 301 | RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12) |
| 302 | processGOTRelocation(RE, Value, Stubs); |
| 303 | else { |
| 304 | if (Value.SymbolName) |
| 305 | addRelocationForSymbol(RE, Value.SymbolName); |
| 306 | else |
| 307 | addRelocationForSection(RE, Value.SectionID); |
| 308 | } |
| 309 | |
| 310 | return ++RelI; |
| 311 | } |
| 312 | |
Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 313 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 314 | DEBUG(dumpRelocationToResolve(RE, Value)); |
| 315 | |
| 316 | const SectionEntry &Section = Sections[RE.SectionID]; |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 317 | uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset); |
Juergen Ributzka | fbd40c3 | 2014-07-29 19:57:11 +0000 | [diff] [blame] | 318 | MachO::RelocationInfoType RelType = |
| 319 | static_cast<MachO::RelocationInfoType>(RE.RelType); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 320 | |
Juergen Ributzka | fbd40c3 | 2014-07-29 19:57:11 +0000 | [diff] [blame] | 321 | switch (RelType) { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 322 | default: |
| 323 | llvm_unreachable("Invalid relocation type!"); |
| 324 | case MachO::ARM64_RELOC_UNSIGNED: { |
| 325 | assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_UNSIGNED not supported"); |
| 326 | // Mask in the target value a byte at a time (we don't have an alignment |
| 327 | // guarantee for the target address, so this is safest). |
| 328 | if (RE.Size < 2) |
| 329 | llvm_unreachable("Invalid size for ARM64_RELOC_UNSIGNED"); |
| 330 | |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 331 | encodeAddend(LocalAddress, 1 << RE.Size, RelType, Value + RE.Addend); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | case MachO::ARM64_RELOC_BRANCH26: { |
| 335 | assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 336 | // Check if branch is in range. |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 337 | uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 338 | int64_t PCRelVal = Value - FinalAddress + RE.Addend; |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 339 | encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 340 | break; |
| 341 | } |
| 342 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 343 | case MachO::ARM64_RELOC_PAGE21: { |
| 344 | assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 345 | // Adjust for PC-relative relocation and offset. |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 346 | uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 347 | int64_t PCRelVal = |
| 348 | ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096)); |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 349 | encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
| 353 | case MachO::ARM64_RELOC_PAGEOFF12: { |
| 354 | assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_PAGEOFF21 not supported"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 355 | // Add the offset from the symbol. |
| 356 | Value += RE.Addend; |
| 357 | // Mask out the page address and only use the lower 12 bits. |
| 358 | Value &= 0xFFF; |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 359 | encodeAddend(LocalAddress, /*Size=*/4, RelType, Value); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 360 | break; |
| 361 | } |
Lang Hames | 3db630b | 2016-01-21 21:59:50 +0000 | [diff] [blame] | 362 | case MachO::ARM64_RELOC_SUBTRACTOR: { |
| 363 | uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress(); |
| 364 | uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress(); |
| 365 | assert((Value == SectionABase || Value == SectionBBase) && |
| 366 | "Unexpected SUBTRACTOR relocation value."); |
| 367 | Value = SectionABase - SectionBBase + RE.Addend; |
| 368 | writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size); |
| 369 | break; |
| 370 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 371 | case MachO::ARM64_RELOC_POINTER_TO_GOT: |
| 372 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: |
| 373 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 374 | llvm_unreachable("Relocation type not yet implemented!"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 375 | case MachO::ARM64_RELOC_ADDEND: |
| 376 | llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by " |
| 377 | "processRelocationRef!"); |
| 378 | } |
| 379 | } |
| 380 | |
Lang Hames | 8959531 | 2016-04-27 20:24:48 +0000 | [diff] [blame] | 381 | Error finalizeSection(const ObjectFile &Obj, unsigned SectionID, |
| 382 | const SectionRef &Section) { |
| 383 | return Error::success(); |
| 384 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 385 | |
| 386 | private: |
| 387 | void processGOTRelocation(const RelocationEntry &RE, |
| 388 | RelocationValueRef &Value, StubMap &Stubs) { |
| 389 | assert(RE.Size == 2); |
| 390 | SectionEntry &Section = Sections[RE.SectionID]; |
| 391 | StubMap::const_iterator i = Stubs.find(Value); |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 392 | int64_t Offset; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 393 | if (i != Stubs.end()) |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 394 | Offset = static_cast<int64_t>(i->second); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 395 | else { |
| 396 | // FIXME: There must be a better way to do this then to check and fix the |
| 397 | // alignment every time!!! |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 398 | uintptr_t BaseAddress = uintptr_t(Section.getAddress()); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 399 | uintptr_t StubAlignment = getStubAlignment(); |
| 400 | uintptr_t StubAddress = |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 401 | (BaseAddress + Section.getStubOffset() + StubAlignment - 1) & |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 402 | -StubAlignment; |
| 403 | unsigned StubOffset = StubAddress - BaseAddress; |
| 404 | Stubs[Value] = StubOffset; |
| 405 | assert(((StubAddress % getStubAlignment()) == 0) && |
| 406 | "GOT entry not aligned"); |
| 407 | RelocationEntry GOTRE(RE.SectionID, StubOffset, |
Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame] | 408 | MachO::ARM64_RELOC_UNSIGNED, Value.Offset, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 409 | /*IsPCRel=*/false, /*Size=*/3); |
| 410 | if (Value.SymbolName) |
| 411 | addRelocationForSymbol(GOTRE, Value.SymbolName); |
| 412 | else |
| 413 | addRelocationForSection(GOTRE, Value.SectionID); |
Sanjoy Das | 277776a | 2015-11-23 21:47:41 +0000 | [diff] [blame] | 414 | Section.advanceStubOffset(getMaxStubSize()); |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 415 | Offset = static_cast<int64_t>(StubOffset); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 416 | } |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 417 | RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 418 | RE.IsPCRel, RE.Size); |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 419 | addRelocationForSection(TargetRE, RE.SectionID); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 420 | } |
Lang Hames | 3db630b | 2016-01-21 21:59:50 +0000 | [diff] [blame] | 421 | |
Lang Hames | 4ce96c5 | 2016-05-18 05:31:24 +0000 | [diff] [blame] | 422 | Expected<relocation_iterator> |
Lang Hames | 3db630b | 2016-01-21 21:59:50 +0000 | [diff] [blame] | 423 | processSubtractRelocation(unsigned SectionID, relocation_iterator RelI, |
| 424 | const ObjectFile &BaseObjT, |
| 425 | ObjSectionToIDMap &ObjSectionToID) { |
| 426 | const MachOObjectFile &Obj = |
| 427 | static_cast<const MachOObjectFile&>(BaseObjT); |
| 428 | MachO::any_relocation_info RE = |
| 429 | Obj.getRelocation(RelI->getRawDataRefImpl()); |
| 430 | |
| 431 | unsigned Size = Obj.getAnyRelocationLength(RE); |
| 432 | uint64_t Offset = RelI->getOffset(); |
| 433 | uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset); |
| 434 | unsigned NumBytes = 1 << Size; |
| 435 | |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 436 | Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName(); |
Lang Hames | 4ce96c5 | 2016-05-18 05:31:24 +0000 | [diff] [blame] | 437 | if (!SubtrahendNameOrErr) |
| 438 | return SubtrahendNameOrErr.takeError(); |
Lang Hames | 3db630b | 2016-01-21 21:59:50 +0000 | [diff] [blame] | 439 | auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr); |
| 440 | unsigned SectionBID = SubtrahendI->second.getSectionID(); |
| 441 | uint64_t SectionBOffset = SubtrahendI->second.getOffset(); |
| 442 | int64_t Addend = |
| 443 | SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8); |
| 444 | |
| 445 | ++RelI; |
Kevin Enderby | 81e8b7d | 2016-04-20 21:24:34 +0000 | [diff] [blame] | 446 | Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName(); |
Lang Hames | 4ce96c5 | 2016-05-18 05:31:24 +0000 | [diff] [blame] | 447 | if (!MinuendNameOrErr) |
| 448 | return MinuendNameOrErr.takeError(); |
Lang Hames | 3db630b | 2016-01-21 21:59:50 +0000 | [diff] [blame] | 449 | auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr); |
| 450 | unsigned SectionAID = MinuendI->second.getSectionID(); |
| 451 | uint64_t SectionAOffset = MinuendI->second.getOffset(); |
| 452 | |
| 453 | RelocationEntry R(SectionID, Offset, MachO::ARM64_RELOC_SUBTRACTOR, (uint64_t)Addend, |
| 454 | SectionAID, SectionAOffset, SectionBID, SectionBOffset, |
| 455 | false, Size); |
| 456 | |
| 457 | addRelocationForSection(R, SectionAID); |
| 458 | |
| 459 | return ++RelI; |
| 460 | } |
| 461 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 462 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 463 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 464 | |
| 465 | #undef DEBUG_TYPE |
| 466 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 467 | #endif |