blob: 97cbc153b227b909f94fc18d0a95f38eb95b128a [file] [log] [blame]
Lang Hamesa5216882014-07-17 18:54:50 +00001//===-- 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 Kramera7c40ef2014-08-13 16:26:38 +000010#ifndef LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H
11#define LLVM_LIB_EXECUTIONENGINE_RUNTIMEDYLD_TARGETS_RUNTIMEDYLDMACHOAARCH64_H
Lang Hamesa5216882014-07-17 18:54:50 +000012
13#include "../RuntimeDyldMachO.h"
Juergen Ributzka0e913b12014-07-29 19:57:15 +000014#include "llvm/Support/Endian.h"
Lang Hamesa5216882014-07-17 18:54:50 +000015
16#define DEBUG_TYPE "dyld"
17
18namespace llvm {
19
20class RuntimeDyldMachOAArch64
21 : public RuntimeDyldMachOCRTPBase<RuntimeDyldMachOAArch64> {
22public:
Lang Hameseb195f02014-09-04 04:53:03 +000023
24 typedef uint64_t TargetPtrT;
25
Lang Hames633fe142015-03-30 03:37:06 +000026 RuntimeDyldMachOAArch64(RuntimeDyld::MemoryManager &MM,
Lang Hamesad4a9112016-08-01 20:49:11 +000027 JITSymbolResolver &Resolver)
Lang Hames633fe142015-03-30 03:37:06 +000028 : RuntimeDyldMachOCRTPBase(MM, Resolver) {}
Lang Hamesa5216882014-07-17 18:54:50 +000029
30 unsigned getMaxStubSize() override { return 8; }
31
Lang Hamese5fc8262014-07-17 23:11:30 +000032 unsigned getStubAlignment() override { return 8; }
Lang Hamesa5216882014-07-17 18:54:50 +000033
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000034 /// Extract the addend encoded in the instruction / memory location.
Lang Hames25d93092014-08-08 23:12:22 +000035 int64_t decodeAddend(const RelocationEntry &RE) const {
36 const SectionEntry &Section = Sections[RE.SectionID];
Sanjoy Das277776a2015-11-23 21:47:41 +000037 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
Lang Hames25d93092014-08-08 23:12:22 +000038 unsigned NumBytes = 1 << RE.Size;
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000039 int64_t Addend = 0;
40 // Verify that the relocation has the correct size and alignment.
Lang Hames25d93092014-08-08 23:12:22 +000041 switch (RE.RelType) {
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000042 default:
43 llvm_unreachable("Unsupported relocation type!");
44 case MachO::ARM64_RELOC_UNSIGNED:
Juergen Ributzka0e913b12014-07-29 19:57:15 +000045 assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size.");
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000046 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 Hames25d93092014-08-08 23:12:22 +000058 switch (RE.RelType) {
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000059 default:
60 llvm_unreachable("Unsupported relocation type!");
61 case MachO::ARM64_RELOC_UNSIGNED:
Juergen Ributzka0e913b12014-07-29 19:57:15 +000062 // 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 Ributzkab13b52e2014-07-22 21:42:51 +000067 break;
68 case MachO::ARM64_RELOC_BRANCH26: {
69 // Verify that the relocation points to the expected branch instruction.
Juergen Ributzka0e913b12014-07-29 19:57:15 +000070 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000071 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 Ributzka0e913b12014-07-29 19:57:15 +000083 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000084 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 Ributzka0e913b12014-07-29 19:57:15 +000096 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzka0e957cf2014-07-22 22:02:19 +000097 (void)p;
Juergen Ributzkab13b52e2014-07-22 21:42:51 +000098 assert((*p & 0x3B000000) == 0x39000000 &&
99 "Only expected load / store instructions.");
Justin Bognerb03fd122016-08-17 05:10:15 +0000100 LLVM_FALLTHROUGH;
101 }
Juergen Ributzkab13b52e2014-07-22 21:42:51 +0000102 case MachO::ARM64_RELOC_PAGEOFF12: {
103 // Verify that the relocation points to one of the expected load / store
104 // or add / sub instructions.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000105 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkab13b52e2014-07-22 21:42:51 +0000106 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 Ributzkaf5609282014-07-22 21:42:55 +0000133 /// Extract the addend encoded in the instruction.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000134 void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
135 MachO::RelocationInfoType RelType, int64_t Addend) const {
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000136 // 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 Ributzka0e913b12014-07-29 19:57:15 +0000141 assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size.");
142 break;
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000143 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 Ributzka0e913b12014-07-29 19:57:15 +0000148 assert(NumBytes == 4 && "Invalid relocation size.");
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000149 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 Ributzka0e913b12014-07-29 19:57:15 +0000157 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 Ributzkaf5609282014-07-22 21:42:55 +0000164 case MachO::ARM64_RELOC_BRANCH26: {
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000165 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000166 // Verify that the relocation points to the expected branch instruction.
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000167 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 Ributzka0e913b12014-07-29 19:57:15 +0000180 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000181 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 Samsonov7c8a7252015-01-10 00:46:38 +0000188 uint32_t ImmLoValue = ((uint64_t)Addend << 17) & 0x60000000;
189 uint32_t ImmHiValue = ((uint64_t)Addend >> 9) & 0x00FFFFE0;
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000190 *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 Ributzka0e913b12014-07-29 19:57:15 +0000196 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000197 assert((*p & 0x3B000000) == 0x39000000 &&
198 "Only expected load / store instructions.");
NAKAMURA Takumiea4a8da2014-07-23 00:17:44 +0000199 (void)p;
Justin Bognerb03fd122016-08-17 05:10:15 +0000200 LLVM_FALLTHROUGH;
201 }
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000202 case MachO::ARM64_RELOC_PAGEOFF12: {
203 // Verify that the relocation points to one of the expected load / store
204 // or add / sub instructions.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000205 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000206 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 Hames89595312016-04-27 20:24:48 +0000247 Expected<relocation_iterator>
Lang Hamesa5216882014-07-17 18:54:50 +0000248 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000249 const ObjectFile &BaseObjT,
250 ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000251 StubMap &Stubs) override {
Lang Hamesa5216882014-07-17 18:54:50 +0000252 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000253 static_cast<const MachOObjectFile &>(BaseObjT);
Lang Hamesa5216882014-07-17 18:54:50 +0000254 MachO::any_relocation_info RelInfo =
255 Obj.getRelocation(RelI->getRawDataRefImpl());
256
Lang Hames89595312016-04-27 20:24:48 +0000257 if (Obj.isRelocationScattered(RelInfo))
258 return make_error<RuntimeDyldError>("Scattered relocations not supported "
259 "for MachO AArch64");
Lang Hamesa5216882014-07-17 18:54:50 +0000260
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 Hamesa5216882014-07-17 18:54:50 +0000265 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 Hamesa5216882014-07-17 18:54:50 +0000270 int64_t RawAddend = Obj.getPlainRelocationSymbolNum(RelInfo);
271 // Sign-extend the 24-bit to 64-bit.
Juergen Ributzkadd19d332014-07-22 21:42:49 +0000272 ExplicitAddend = SignExtend64(RawAddend, 24);
Lang Hamesa5216882014-07-17 18:54:50 +0000273 ++RelI;
274 RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());
275 }
276
Lang Hames3db630b2016-01-21 21:59:50 +0000277 if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_SUBTRACTOR)
278 return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
279
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000280 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
Lang Hames25d93092014-08-08 23:12:22 +0000281 RE.Addend = decodeAddend(RE);
Lang Hamesa5216882014-07-17 18:54:50 +0000282
Juergen Ributzkadd19d332014-07-22 21:42:49 +0000283 assert((ExplicitAddend == 0 || RE.Addend == 0) && "Relocation has "\
284 "ARM64_RELOC_ADDEND and embedded addend in the instruction.");
Lang Hames0fd36102015-08-11 06:27:53 +0000285 if (ExplicitAddend)
Lang Hames76774a52014-07-18 20:29:36 +0000286 RE.Addend = ExplicitAddend;
Lang Hames0fd36102015-08-11 06:27:53 +0000287
Lang Hames89595312016-04-27 20:24:48 +0000288 RelocationValueRef Value;
289 if (auto ValueOrErr = getRelocationValueRef(Obj, RelI, RE, ObjSectionToID))
290 Value = *ValueOrErr;
291 else
292 return ValueOrErr.takeError();
Lang Hamesa5216882014-07-17 18:54:50 +0000293
294 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
295 if (!IsExtern && RE.IsPCRel)
Rafael Espindola76ad2322015-07-06 14:55:37 +0000296 makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
Lang Hamesa5216882014-07-17 18:54:50 +0000297
Lang Hamesca279c22014-09-07 04:03:32 +0000298 RE.Addend = Value.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +0000299
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 Kramer8c90fd72014-09-03 11:41:21 +0000313 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
Lang Hamesa5216882014-07-17 18:54:50 +0000314 DEBUG(dumpRelocationToResolve(RE, Value));
315
316 const SectionEntry &Section = Sections[RE.SectionID];
Sanjoy Das277776a2015-11-23 21:47:41 +0000317 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
Juergen Ributzkafbd40c32014-07-29 19:57:11 +0000318 MachO::RelocationInfoType RelType =
319 static_cast<MachO::RelocationInfoType>(RE.RelType);
Lang Hamesa5216882014-07-17 18:54:50 +0000320
Juergen Ributzkafbd40c32014-07-29 19:57:11 +0000321 switch (RelType) {
Lang Hamesa5216882014-07-17 18:54:50 +0000322 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 Ributzka0e913b12014-07-29 19:57:15 +0000331 encodeAddend(LocalAddress, 1 << RE.Size, RelType, Value + RE.Addend);
Lang Hamesa5216882014-07-17 18:54:50 +0000332 break;
333 }
334 case MachO::ARM64_RELOC_BRANCH26: {
335 assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported");
Lang Hamesa5216882014-07-17 18:54:50 +0000336 // Check if branch is in range.
Sanjoy Das277776a2015-11-23 21:47:41 +0000337 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000338 int64_t PCRelVal = Value - FinalAddress + RE.Addend;
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000339 encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
Lang Hamesa5216882014-07-17 18:54:50 +0000340 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 Hamesa5216882014-07-17 18:54:50 +0000345 // Adjust for PC-relative relocation and offset.
Sanjoy Das277776a2015-11-23 21:47:41 +0000346 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000347 int64_t PCRelVal =
348 ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096));
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000349 encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
Lang Hamesa5216882014-07-17 18:54:50 +0000350 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 Hamesa5216882014-07-17 18:54:50 +0000355 // 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 Ributzka0e913b12014-07-29 19:57:15 +0000359 encodeAddend(LocalAddress, /*Size=*/4, RelType, Value);
Lang Hamesa5216882014-07-17 18:54:50 +0000360 break;
361 }
Lang Hames3db630b2016-01-21 21:59:50 +0000362 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 Hamesa5216882014-07-17 18:54:50 +0000371 case MachO::ARM64_RELOC_POINTER_TO_GOT:
372 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
373 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000374 llvm_unreachable("Relocation type not yet implemented!");
Lang Hamesa5216882014-07-17 18:54:50 +0000375 case MachO::ARM64_RELOC_ADDEND:
376 llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by "
377 "processRelocationRef!");
378 }
379 }
380
Lang Hames89595312016-04-27 20:24:48 +0000381 Error finalizeSection(const ObjectFile &Obj, unsigned SectionID,
382 const SectionRef &Section) {
383 return Error::success();
384 }
Lang Hamesa5216882014-07-17 18:54:50 +0000385
386private:
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 Hames41d95942014-10-21 23:41:15 +0000392 int64_t Offset;
Lang Hamesa5216882014-07-17 18:54:50 +0000393 if (i != Stubs.end())
Lang Hames41d95942014-10-21 23:41:15 +0000394 Offset = static_cast<int64_t>(i->second);
Lang Hamesa5216882014-07-17 18:54:50 +0000395 else {
396 // FIXME: There must be a better way to do this then to check and fix the
397 // alignment every time!!!
Sanjoy Das277776a2015-11-23 21:47:41 +0000398 uintptr_t BaseAddress = uintptr_t(Section.getAddress());
Lang Hamesa5216882014-07-17 18:54:50 +0000399 uintptr_t StubAlignment = getStubAlignment();
400 uintptr_t StubAddress =
Sanjoy Das277776a2015-11-23 21:47:41 +0000401 (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
Lang Hamesa5216882014-07-17 18:54:50 +0000402 -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 Hamesca279c22014-09-07 04:03:32 +0000408 MachO::ARM64_RELOC_UNSIGNED, Value.Offset,
Lang Hamesa5216882014-07-17 18:54:50 +0000409 /*IsPCRel=*/false, /*Size=*/3);
410 if (Value.SymbolName)
411 addRelocationForSymbol(GOTRE, Value.SymbolName);
412 else
413 addRelocationForSection(GOTRE, Value.SectionID);
Sanjoy Das277776a2015-11-23 21:47:41 +0000414 Section.advanceStubOffset(getMaxStubSize());
Lang Hames41d95942014-10-21 23:41:15 +0000415 Offset = static_cast<int64_t>(StubOffset);
Lang Hamesa5216882014-07-17 18:54:50 +0000416 }
Lang Hames41d95942014-10-21 23:41:15 +0000417 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset,
Lang Hamesa5216882014-07-17 18:54:50 +0000418 RE.IsPCRel, RE.Size);
Lang Hames41d95942014-10-21 23:41:15 +0000419 addRelocationForSection(TargetRE, RE.SectionID);
Lang Hamesa5216882014-07-17 18:54:50 +0000420 }
Lang Hames3db630b2016-01-21 21:59:50 +0000421
Lang Hames4ce96c52016-05-18 05:31:24 +0000422 Expected<relocation_iterator>
Lang Hames3db630b2016-01-21 21:59:50 +0000423 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 Enderby81e8b7d2016-04-20 21:24:34 +0000436 Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
Lang Hames4ce96c52016-05-18 05:31:24 +0000437 if (!SubtrahendNameOrErr)
438 return SubtrahendNameOrErr.takeError();
Lang Hames3db630b2016-01-21 21:59:50 +0000439 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 Enderby81e8b7d2016-04-20 21:24:34 +0000446 Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
Lang Hames4ce96c52016-05-18 05:31:24 +0000447 if (!MinuendNameOrErr)
448 return MinuendNameOrErr.takeError();
Lang Hames3db630b2016-01-21 21:59:50 +0000449 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 Hamesa5216882014-07-17 18:54:50 +0000462};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000463}
Lang Hamesa5216882014-07-17 18:54:50 +0000464
465#undef DEBUG_TYPE
466
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000467#endif