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 | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 26 | RuntimeDyldMachOAArch64(RTDyldMemoryManager *MM) |
| 27 | : RuntimeDyldMachOCRTPBase(MM) {} |
| 28 | |
| 29 | unsigned getMaxStubSize() override { return 8; } |
| 30 | |
Lang Hames | e5fc826 | 2014-07-17 23:11:30 +0000 | [diff] [blame] | 31 | unsigned getStubAlignment() override { return 8; } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 32 | |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 33 | /// Extract the addend encoded in the instruction / memory location. |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 34 | int64_t decodeAddend(const RelocationEntry &RE) const { |
| 35 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 36 | uint8_t *LocalAddress = Section.Address + RE.Offset; |
| 37 | unsigned NumBytes = 1 << RE.Size; |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 38 | int64_t Addend = 0; |
| 39 | // Verify that the relocation has the correct size and alignment. |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 40 | switch (RE.RelType) { |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 41 | default: |
| 42 | llvm_unreachable("Unsupported relocation type!"); |
| 43 | case MachO::ARM64_RELOC_UNSIGNED: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 44 | assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size."); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 45 | break; |
| 46 | case MachO::ARM64_RELOC_BRANCH26: |
| 47 | case MachO::ARM64_RELOC_PAGE21: |
| 48 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 49 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 50 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
| 51 | assert(NumBytes == 4 && "Invalid relocation size."); |
| 52 | assert((((uintptr_t)LocalAddress & 0x3) == 0) && |
| 53 | "Instruction address is not aligned to 4 bytes."); |
| 54 | break; |
| 55 | } |
| 56 | |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 57 | switch (RE.RelType) { |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 58 | default: |
| 59 | llvm_unreachable("Unsupported relocation type!"); |
| 60 | case MachO::ARM64_RELOC_UNSIGNED: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 61 | // This could be an unaligned memory location. |
| 62 | if (NumBytes == 4) |
| 63 | Addend = *reinterpret_cast<support::ulittle32_t *>(LocalAddress); |
| 64 | else |
| 65 | Addend = *reinterpret_cast<support::ulittle64_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 66 | break; |
| 67 | case MachO::ARM64_RELOC_BRANCH26: { |
| 68 | // Verify that the relocation points to the expected branch instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 69 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 70 | assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction."); |
| 71 | |
| 72 | // Get the 26 bit addend encoded in the branch instruction and sign-extend |
| 73 | // to 64 bit. The lower 2 bits are always zeros and are therefore implicit |
| 74 | // (<< 2). |
| 75 | Addend = (*p & 0x03FFFFFF) << 2; |
| 76 | Addend = SignExtend64(Addend, 28); |
| 77 | break; |
| 78 | } |
| 79 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 80 | case MachO::ARM64_RELOC_PAGE21: { |
| 81 | // Verify that the relocation points to the expected adrp instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 82 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 83 | assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction."); |
| 84 | |
| 85 | // Get the 21 bit addend encoded in the adrp instruction and sign-extend |
| 86 | // to 64 bit. The lower 12 bits (4096 byte page) are always zeros and are |
| 87 | // therefore implicit (<< 12). |
| 88 | Addend = ((*p & 0x60000000) >> 29) | ((*p & 0x01FFFFE0) >> 3) << 12; |
| 89 | Addend = SignExtend64(Addend, 33); |
| 90 | break; |
| 91 | } |
| 92 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: { |
| 93 | // Verify that the relocation points to one of the expected load / store |
| 94 | // instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 95 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | 0e957cf | 2014-07-22 22:02:19 +0000 | [diff] [blame] | 96 | (void)p; |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 97 | assert((*p & 0x3B000000) == 0x39000000 && |
| 98 | "Only expected load / store instructions."); |
| 99 | } // fall-through |
| 100 | case MachO::ARM64_RELOC_PAGEOFF12: { |
| 101 | // Verify that the relocation points to one of the expected load / store |
| 102 | // or add / sub instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 103 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | b13b52e | 2014-07-22 21:42:51 +0000 | [diff] [blame] | 104 | assert((((*p & 0x3B000000) == 0x39000000) || |
| 105 | ((*p & 0x11C00000) == 0x11000000) ) && |
| 106 | "Expected load / store or add/sub instruction."); |
| 107 | |
| 108 | // Get the 12 bit addend encoded in the instruction. |
| 109 | Addend = (*p & 0x003FFC00) >> 10; |
| 110 | |
| 111 | // Check which instruction we are decoding to obtain the implicit shift |
| 112 | // factor of the instruction. |
| 113 | int ImplicitShift = 0; |
| 114 | if ((*p & 0x3B000000) == 0x39000000) { // << load / store |
| 115 | // For load / store instructions the size is encoded in bits 31:30. |
| 116 | ImplicitShift = ((*p >> 30) & 0x3); |
| 117 | if (ImplicitShift == 0) { |
| 118 | // Check if this a vector op to get the correct shift value. |
| 119 | if ((*p & 0x04800000) == 0x04800000) |
| 120 | ImplicitShift = 4; |
| 121 | } |
| 122 | } |
| 123 | // Compensate for implicit shift. |
| 124 | Addend <<= ImplicitShift; |
| 125 | break; |
| 126 | } |
| 127 | } |
| 128 | return Addend; |
| 129 | } |
| 130 | |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 131 | /// Extract the addend encoded in the instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 132 | void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes, |
| 133 | MachO::RelocationInfoType RelType, int64_t Addend) const { |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 134 | // Verify that the relocation has the correct alignment. |
| 135 | switch (RelType) { |
| 136 | default: |
| 137 | llvm_unreachable("Unsupported relocation type!"); |
| 138 | case MachO::ARM64_RELOC_UNSIGNED: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 139 | assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size."); |
| 140 | break; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 141 | case MachO::ARM64_RELOC_BRANCH26: |
| 142 | case MachO::ARM64_RELOC_PAGE21: |
| 143 | case MachO::ARM64_RELOC_PAGEOFF12: |
| 144 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 145 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 146 | assert(NumBytes == 4 && "Invalid relocation size."); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 147 | assert((((uintptr_t)LocalAddress & 0x3) == 0) && |
| 148 | "Instruction address is not aligned to 4 bytes."); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | switch (RelType) { |
| 153 | default: |
| 154 | llvm_unreachable("Unsupported relocation type!"); |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 155 | case MachO::ARM64_RELOC_UNSIGNED: |
| 156 | // This could be an unaligned memory location. |
| 157 | if (NumBytes == 4) |
| 158 | *reinterpret_cast<support::ulittle32_t *>(LocalAddress) = Addend; |
| 159 | else |
| 160 | *reinterpret_cast<support::ulittle64_t *>(LocalAddress) = Addend; |
| 161 | break; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 162 | case MachO::ARM64_RELOC_BRANCH26: { |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 163 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 164 | // Verify that the relocation points to the expected branch instruction. |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 165 | assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction."); |
| 166 | |
| 167 | // Verify addend value. |
| 168 | assert((Addend & 0x3) == 0 && "Branch target is not aligned"); |
| 169 | assert(isInt<28>(Addend) && "Branch target is out of range."); |
| 170 | |
| 171 | // Encode the addend as 26 bit immediate in the branch instruction. |
| 172 | *p = (*p & 0xFC000000) | ((uint32_t)(Addend >> 2) & 0x03FFFFFF); |
| 173 | break; |
| 174 | } |
| 175 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 176 | case MachO::ARM64_RELOC_PAGE21: { |
| 177 | // Verify that the relocation points to the expected adrp instruction. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 178 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 179 | assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction."); |
| 180 | |
| 181 | // Check that the addend fits into 21 bits (+ 12 lower bits). |
| 182 | assert((Addend & 0xFFF) == 0 && "ADRP target is not page aligned."); |
| 183 | assert(isInt<33>(Addend) && "Invalid page reloc value."); |
| 184 | |
| 185 | // Encode the addend into the instruction. |
| 186 | uint32_t ImmLoValue = (uint32_t)(Addend << 17) & 0x60000000; |
| 187 | uint32_t ImmHiValue = (uint32_t)(Addend >> 9) & 0x00FFFFE0; |
| 188 | *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue; |
| 189 | break; |
| 190 | } |
| 191 | case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: { |
| 192 | // Verify that the relocation points to one of the expected load / store |
| 193 | // instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 194 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 195 | assert((*p & 0x3B000000) == 0x39000000 && |
| 196 | "Only expected load / store instructions."); |
NAKAMURA Takumi | ea4a8da | 2014-07-23 00:17:44 +0000 | [diff] [blame] | 197 | (void)p; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 198 | } // fall-through |
| 199 | case MachO::ARM64_RELOC_PAGEOFF12: { |
| 200 | // Verify that the relocation points to one of the expected load / store |
| 201 | // or add / sub instructions. |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 202 | auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress); |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 203 | assert((((*p & 0x3B000000) == 0x39000000) || |
| 204 | ((*p & 0x11C00000) == 0x11000000) ) && |
| 205 | "Expected load / store or add/sub instruction."); |
| 206 | |
| 207 | // Check which instruction we are decoding to obtain the implicit shift |
| 208 | // factor of the instruction and verify alignment. |
| 209 | int ImplicitShift = 0; |
| 210 | if ((*p & 0x3B000000) == 0x39000000) { // << load / store |
| 211 | // For load / store instructions the size is encoded in bits 31:30. |
| 212 | ImplicitShift = ((*p >> 30) & 0x3); |
| 213 | switch (ImplicitShift) { |
| 214 | case 0: |
| 215 | // Check if this a vector op to get the correct shift value. |
| 216 | if ((*p & 0x04800000) == 0x04800000) { |
| 217 | ImplicitShift = 4; |
| 218 | assert(((Addend & 0xF) == 0) && |
| 219 | "128-bit LDR/STR not 16-byte aligned."); |
| 220 | } |
| 221 | break; |
| 222 | case 1: |
| 223 | assert(((Addend & 0x1) == 0) && "16-bit LDR/STR not 2-byte aligned."); |
| 224 | break; |
| 225 | case 2: |
| 226 | assert(((Addend & 0x3) == 0) && "32-bit LDR/STR not 4-byte aligned."); |
| 227 | break; |
| 228 | case 3: |
| 229 | assert(((Addend & 0x7) == 0) && "64-bit LDR/STR not 8-byte aligned."); |
| 230 | break; |
| 231 | } |
| 232 | } |
| 233 | // Compensate for implicit shift. |
| 234 | Addend >>= ImplicitShift; |
| 235 | assert(isUInt<12>(Addend) && "Addend cannot be encoded."); |
| 236 | |
| 237 | // Encode the addend into the instruction. |
| 238 | *p = (*p & 0xFFC003FF) | ((uint32_t)(Addend << 10) & 0x003FFC00); |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 244 | relocation_iterator |
| 245 | processRelocationRef(unsigned SectionID, relocation_iterator RelI, |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame^] | 246 | const ObjectFile &BaseObjT, |
| 247 | ObjSectionToIDMap &ObjSectionToID, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 248 | const SymbolTableMap &Symbols, StubMap &Stubs) override { |
| 249 | const MachOObjectFile &Obj = |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame^] | 250 | static_cast<const MachOObjectFile &>(BaseObjT); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 251 | MachO::any_relocation_info RelInfo = |
| 252 | Obj.getRelocation(RelI->getRawDataRefImpl()); |
| 253 | |
| 254 | assert(!Obj.isRelocationScattered(RelInfo) && ""); |
| 255 | |
| 256 | // ARM64 has an ARM64_RELOC_ADDEND relocation type that carries an explicit |
| 257 | // addend for the following relocation. If found: (1) store the associated |
| 258 | // addend, (2) consume the next relocation, and (3) use the stored addend to |
| 259 | // override the addend. |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 260 | int64_t ExplicitAddend = 0; |
| 261 | if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_ADDEND) { |
| 262 | assert(!Obj.getPlainRelocationExternal(RelInfo)); |
| 263 | assert(!Obj.getAnyRelocationPCRel(RelInfo)); |
| 264 | assert(Obj.getAnyRelocationLength(RelInfo) == 2); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 265 | int64_t RawAddend = Obj.getPlainRelocationSymbolNum(RelInfo); |
| 266 | // Sign-extend the 24-bit to 64-bit. |
Juergen Ributzka | dd19d33 | 2014-07-22 21:42:49 +0000 | [diff] [blame] | 267 | ExplicitAddend = SignExtend64(RawAddend, 24); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 268 | ++RelI; |
| 269 | RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl()); |
| 270 | } |
| 271 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame^] | 272 | RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI)); |
Lang Hames | 25d9309 | 2014-08-08 23:12:22 +0000 | [diff] [blame] | 273 | RE.Addend = decodeAddend(RE); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 274 | RelocationValueRef Value( |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame^] | 275 | getRelocationValueRef(Obj, RelI, RE, ObjSectionToID, Symbols)); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 276 | |
Juergen Ributzka | dd19d33 | 2014-07-22 21:42:49 +0000 | [diff] [blame] | 277 | assert((ExplicitAddend == 0 || RE.Addend == 0) && "Relocation has "\ |
| 278 | "ARM64_RELOC_ADDEND and embedded addend in the instruction."); |
| 279 | if (ExplicitAddend) { |
Lang Hames | 76774a5 | 2014-07-18 20:29:36 +0000 | [diff] [blame] | 280 | RE.Addend = ExplicitAddend; |
Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame] | 281 | Value.Offset = ExplicitAddend; |
Lang Hames | 76774a5 | 2014-07-18 20:29:36 +0000 | [diff] [blame] | 282 | } |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 283 | |
| 284 | bool IsExtern = Obj.getPlainRelocationExternal(RelInfo); |
| 285 | if (!IsExtern && RE.IsPCRel) |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame^] | 286 | makeValueAddendPCRel(Value, Obj, RelI, 1 << RE.Size); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 287 | |
Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame] | 288 | RE.Addend = Value.Offset; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 289 | |
| 290 | if (RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 || |
| 291 | RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12) |
| 292 | processGOTRelocation(RE, Value, Stubs); |
| 293 | else { |
| 294 | if (Value.SymbolName) |
| 295 | addRelocationForSymbol(RE, Value.SymbolName); |
| 296 | else |
| 297 | addRelocationForSection(RE, Value.SectionID); |
| 298 | } |
| 299 | |
| 300 | return ++RelI; |
| 301 | } |
| 302 | |
Benjamin Kramer | 8c90fd7 | 2014-09-03 11:41:21 +0000 | [diff] [blame] | 303 | void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 304 | DEBUG(dumpRelocationToResolve(RE, Value)); |
| 305 | |
| 306 | const SectionEntry &Section = Sections[RE.SectionID]; |
| 307 | uint8_t *LocalAddress = Section.Address + RE.Offset; |
Juergen Ributzka | fbd40c3 | 2014-07-29 19:57:11 +0000 | [diff] [blame] | 308 | MachO::RelocationInfoType RelType = |
| 309 | static_cast<MachO::RelocationInfoType>(RE.RelType); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 310 | |
Juergen Ributzka | fbd40c3 | 2014-07-29 19:57:11 +0000 | [diff] [blame] | 311 | switch (RelType) { |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 312 | default: |
| 313 | llvm_unreachable("Invalid relocation type!"); |
| 314 | case MachO::ARM64_RELOC_UNSIGNED: { |
| 315 | assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_UNSIGNED not supported"); |
| 316 | // Mask in the target value a byte at a time (we don't have an alignment |
| 317 | // guarantee for the target address, so this is safest). |
| 318 | if (RE.Size < 2) |
| 319 | llvm_unreachable("Invalid size for ARM64_RELOC_UNSIGNED"); |
| 320 | |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 321 | encodeAddend(LocalAddress, 1 << RE.Size, RelType, Value + RE.Addend); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | case MachO::ARM64_RELOC_BRANCH26: { |
| 325 | assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 326 | // Check if branch is in range. |
| 327 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 328 | int64_t PCRelVal = Value - FinalAddress + RE.Addend; |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 329 | encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 330 | break; |
| 331 | } |
| 332 | case MachO::ARM64_RELOC_GOT_LOAD_PAGE21: |
| 333 | case MachO::ARM64_RELOC_PAGE21: { |
| 334 | assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 335 | // Adjust for PC-relative relocation and offset. |
| 336 | uint64_t FinalAddress = Section.LoadAddress + RE.Offset; |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 337 | int64_t PCRelVal = |
| 338 | ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096)); |
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_PAGEOFF12: |
| 343 | case MachO::ARM64_RELOC_PAGEOFF12: { |
| 344 | assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_PAGEOFF21 not supported"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 345 | // Add the offset from the symbol. |
| 346 | Value += RE.Addend; |
| 347 | // Mask out the page address and only use the lower 12 bits. |
| 348 | Value &= 0xFFF; |
Juergen Ributzka | 0e913b1 | 2014-07-29 19:57:15 +0000 | [diff] [blame] | 349 | encodeAddend(LocalAddress, /*Size=*/4, RelType, Value); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 350 | break; |
| 351 | } |
| 352 | case MachO::ARM64_RELOC_SUBTRACTOR: |
| 353 | case MachO::ARM64_RELOC_POINTER_TO_GOT: |
| 354 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21: |
| 355 | case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12: |
Juergen Ributzka | f560928 | 2014-07-22 21:42:55 +0000 | [diff] [blame] | 356 | llvm_unreachable("Relocation type not yet implemented!"); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 357 | case MachO::ARM64_RELOC_ADDEND: |
| 358 | llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by " |
| 359 | "processRelocationRef!"); |
| 360 | } |
| 361 | } |
| 362 | |
Lang Hames | b5c7b1f | 2014-11-26 16:54:40 +0000 | [diff] [blame^] | 363 | void finalizeSection(const ObjectFile &Obj, unsigned SectionID, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 364 | const SectionRef &Section) {} |
| 365 | |
| 366 | private: |
| 367 | void processGOTRelocation(const RelocationEntry &RE, |
| 368 | RelocationValueRef &Value, StubMap &Stubs) { |
| 369 | assert(RE.Size == 2); |
| 370 | SectionEntry &Section = Sections[RE.SectionID]; |
| 371 | StubMap::const_iterator i = Stubs.find(Value); |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 372 | int64_t Offset; |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 373 | if (i != Stubs.end()) |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 374 | Offset = static_cast<int64_t>(i->second); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 375 | else { |
| 376 | // FIXME: There must be a better way to do this then to check and fix the |
| 377 | // alignment every time!!! |
| 378 | uintptr_t BaseAddress = uintptr_t(Section.Address); |
| 379 | uintptr_t StubAlignment = getStubAlignment(); |
| 380 | uintptr_t StubAddress = |
| 381 | (BaseAddress + Section.StubOffset + StubAlignment - 1) & |
| 382 | -StubAlignment; |
| 383 | unsigned StubOffset = StubAddress - BaseAddress; |
| 384 | Stubs[Value] = StubOffset; |
| 385 | assert(((StubAddress % getStubAlignment()) == 0) && |
| 386 | "GOT entry not aligned"); |
| 387 | RelocationEntry GOTRE(RE.SectionID, StubOffset, |
Lang Hames | ca279c2 | 2014-09-07 04:03:32 +0000 | [diff] [blame] | 388 | MachO::ARM64_RELOC_UNSIGNED, Value.Offset, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 389 | /*IsPCRel=*/false, /*Size=*/3); |
| 390 | if (Value.SymbolName) |
| 391 | addRelocationForSymbol(GOTRE, Value.SymbolName); |
| 392 | else |
| 393 | addRelocationForSection(GOTRE, Value.SectionID); |
| 394 | Section.StubOffset = StubOffset + getMaxStubSize(); |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 395 | Offset = static_cast<int64_t>(StubOffset); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 396 | } |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 397 | RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset, |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 398 | RE.IsPCRel, RE.Size); |
Lang Hames | 41d9594 | 2014-10-21 23:41:15 +0000 | [diff] [blame] | 399 | addRelocationForSection(TargetRE, RE.SectionID); |
Lang Hames | a521688 | 2014-07-17 18:54:50 +0000 | [diff] [blame] | 400 | } |
| 401 | }; |
| 402 | } |
| 403 | |
| 404 | #undef DEBUG_TYPE |
| 405 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 406 | #endif |