blob: fba157b9424961212f15f3d6b9911fceec559bfe [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,
27 RuntimeDyld::SymbolResolver &Resolver)
28 : 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.");
100 } // fall-through
101 case MachO::ARM64_RELOC_PAGEOFF12: {
102 // Verify that the relocation points to one of the expected load / store
103 // or add / sub instructions.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000104 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkab13b52e2014-07-22 21:42:51 +0000105 assert((((*p & 0x3B000000) == 0x39000000) ||
106 ((*p & 0x11C00000) == 0x11000000) ) &&
107 "Expected load / store or add/sub instruction.");
108
109 // Get the 12 bit addend encoded in the instruction.
110 Addend = (*p & 0x003FFC00) >> 10;
111
112 // Check which instruction we are decoding to obtain the implicit shift
113 // factor of the instruction.
114 int ImplicitShift = 0;
115 if ((*p & 0x3B000000) == 0x39000000) { // << load / store
116 // For load / store instructions the size is encoded in bits 31:30.
117 ImplicitShift = ((*p >> 30) & 0x3);
118 if (ImplicitShift == 0) {
119 // Check if this a vector op to get the correct shift value.
120 if ((*p & 0x04800000) == 0x04800000)
121 ImplicitShift = 4;
122 }
123 }
124 // Compensate for implicit shift.
125 Addend <<= ImplicitShift;
126 break;
127 }
128 }
129 return Addend;
130 }
131
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000132 /// Extract the addend encoded in the instruction.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000133 void encodeAddend(uint8_t *LocalAddress, unsigned NumBytes,
134 MachO::RelocationInfoType RelType, int64_t Addend) const {
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000135 // Verify that the relocation has the correct alignment.
136 switch (RelType) {
137 default:
138 llvm_unreachable("Unsupported relocation type!");
139 case MachO::ARM64_RELOC_UNSIGNED:
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000140 assert((NumBytes == 4 || NumBytes == 8) && "Invalid relocation size.");
141 break;
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000142 case MachO::ARM64_RELOC_BRANCH26:
143 case MachO::ARM64_RELOC_PAGE21:
144 case MachO::ARM64_RELOC_PAGEOFF12:
145 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
146 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000147 assert(NumBytes == 4 && "Invalid relocation size.");
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000148 assert((((uintptr_t)LocalAddress & 0x3) == 0) &&
149 "Instruction address is not aligned to 4 bytes.");
150 break;
151 }
152
153 switch (RelType) {
154 default:
155 llvm_unreachable("Unsupported relocation type!");
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000156 case MachO::ARM64_RELOC_UNSIGNED:
157 // This could be an unaligned memory location.
158 if (NumBytes == 4)
159 *reinterpret_cast<support::ulittle32_t *>(LocalAddress) = Addend;
160 else
161 *reinterpret_cast<support::ulittle64_t *>(LocalAddress) = Addend;
162 break;
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000163 case MachO::ARM64_RELOC_BRANCH26: {
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000164 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000165 // Verify that the relocation points to the expected branch instruction.
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000166 assert((*p & 0xFC000000) == 0x14000000 && "Expected branch instruction.");
167
168 // Verify addend value.
169 assert((Addend & 0x3) == 0 && "Branch target is not aligned");
170 assert(isInt<28>(Addend) && "Branch target is out of range.");
171
172 // Encode the addend as 26 bit immediate in the branch instruction.
173 *p = (*p & 0xFC000000) | ((uint32_t)(Addend >> 2) & 0x03FFFFFF);
174 break;
175 }
176 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
177 case MachO::ARM64_RELOC_PAGE21: {
178 // Verify that the relocation points to the expected adrp instruction.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000179 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000180 assert((*p & 0x9F000000) == 0x90000000 && "Expected adrp instruction.");
181
182 // Check that the addend fits into 21 bits (+ 12 lower bits).
183 assert((Addend & 0xFFF) == 0 && "ADRP target is not page aligned.");
184 assert(isInt<33>(Addend) && "Invalid page reloc value.");
185
186 // Encode the addend into the instruction.
Alexey Samsonov7c8a7252015-01-10 00:46:38 +0000187 uint32_t ImmLoValue = ((uint64_t)Addend << 17) & 0x60000000;
188 uint32_t ImmHiValue = ((uint64_t)Addend >> 9) & 0x00FFFFE0;
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000189 *p = (*p & 0x9F00001F) | ImmHiValue | ImmLoValue;
190 break;
191 }
192 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12: {
193 // Verify that the relocation points to one of the expected load / store
194 // instructions.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000195 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000196 assert((*p & 0x3B000000) == 0x39000000 &&
197 "Only expected load / store instructions.");
NAKAMURA Takumiea4a8da2014-07-23 00:17:44 +0000198 (void)p;
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000199 } // fall-through
200 case MachO::ARM64_RELOC_PAGEOFF12: {
201 // Verify that the relocation points to one of the expected load / store
202 // or add / sub instructions.
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000203 auto *p = reinterpret_cast<support::aligned_ulittle32_t *>(LocalAddress);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000204 assert((((*p & 0x3B000000) == 0x39000000) ||
205 ((*p & 0x11C00000) == 0x11000000) ) &&
206 "Expected load / store or add/sub instruction.");
207
208 // Check which instruction we are decoding to obtain the implicit shift
209 // factor of the instruction and verify alignment.
210 int ImplicitShift = 0;
211 if ((*p & 0x3B000000) == 0x39000000) { // << load / store
212 // For load / store instructions the size is encoded in bits 31:30.
213 ImplicitShift = ((*p >> 30) & 0x3);
214 switch (ImplicitShift) {
215 case 0:
216 // Check if this a vector op to get the correct shift value.
217 if ((*p & 0x04800000) == 0x04800000) {
218 ImplicitShift = 4;
219 assert(((Addend & 0xF) == 0) &&
220 "128-bit LDR/STR not 16-byte aligned.");
221 }
222 break;
223 case 1:
224 assert(((Addend & 0x1) == 0) && "16-bit LDR/STR not 2-byte aligned.");
225 break;
226 case 2:
227 assert(((Addend & 0x3) == 0) && "32-bit LDR/STR not 4-byte aligned.");
228 break;
229 case 3:
230 assert(((Addend & 0x7) == 0) && "64-bit LDR/STR not 8-byte aligned.");
231 break;
232 }
233 }
234 // Compensate for implicit shift.
235 Addend >>= ImplicitShift;
236 assert(isUInt<12>(Addend) && "Addend cannot be encoded.");
237
238 // Encode the addend into the instruction.
239 *p = (*p & 0xFFC003FF) | ((uint32_t)(Addend << 10) & 0x003FFC00);
240 break;
241 }
242 }
243 }
244
Lang Hamesa5216882014-07-17 18:54:50 +0000245 relocation_iterator
246 processRelocationRef(unsigned SectionID, relocation_iterator RelI,
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000247 const ObjectFile &BaseObjT,
248 ObjSectionToIDMap &ObjSectionToID,
Lang Hamesa5cd9502014-11-27 05:40:13 +0000249 StubMap &Stubs) override {
Lang Hamesa5216882014-07-17 18:54:50 +0000250 const MachOObjectFile &Obj =
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000251 static_cast<const MachOObjectFile &>(BaseObjT);
Lang Hamesa5216882014-07-17 18:54:50 +0000252 MachO::any_relocation_info RelInfo =
253 Obj.getRelocation(RelI->getRawDataRefImpl());
254
255 assert(!Obj.isRelocationScattered(RelInfo) && "");
256
257 // ARM64 has an ARM64_RELOC_ADDEND relocation type that carries an explicit
258 // addend for the following relocation. If found: (1) store the associated
259 // addend, (2) consume the next relocation, and (3) use the stored addend to
260 // override the addend.
Lang Hamesa5216882014-07-17 18:54:50 +0000261 int64_t ExplicitAddend = 0;
262 if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_ADDEND) {
263 assert(!Obj.getPlainRelocationExternal(RelInfo));
264 assert(!Obj.getAnyRelocationPCRel(RelInfo));
265 assert(Obj.getAnyRelocationLength(RelInfo) == 2);
Lang Hamesa5216882014-07-17 18:54:50 +0000266 int64_t RawAddend = Obj.getPlainRelocationSymbolNum(RelInfo);
267 // Sign-extend the 24-bit to 64-bit.
Juergen Ributzkadd19d332014-07-22 21:42:49 +0000268 ExplicitAddend = SignExtend64(RawAddend, 24);
Lang Hamesa5216882014-07-17 18:54:50 +0000269 ++RelI;
270 RelInfo = Obj.getRelocation(RelI->getRawDataRefImpl());
271 }
272
Lang Hames3db630b2016-01-21 21:59:50 +0000273 if (Obj.getAnyRelocationType(RelInfo) == MachO::ARM64_RELOC_SUBTRACTOR)
274 return processSubtractRelocation(SectionID, RelI, Obj, ObjSectionToID);
275
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000276 RelocationEntry RE(getRelocationEntry(SectionID, Obj, RelI));
Lang Hames25d93092014-08-08 23:12:22 +0000277 RE.Addend = decodeAddend(RE);
Lang Hamesa5216882014-07-17 18:54:50 +0000278
Juergen Ributzkadd19d332014-07-22 21:42:49 +0000279 assert((ExplicitAddend == 0 || RE.Addend == 0) && "Relocation has "\
280 "ARM64_RELOC_ADDEND and embedded addend in the instruction.");
Lang Hames0fd36102015-08-11 06:27:53 +0000281 if (ExplicitAddend)
Lang Hames76774a52014-07-18 20:29:36 +0000282 RE.Addend = ExplicitAddend;
Lang Hames0fd36102015-08-11 06:27:53 +0000283
284 RelocationValueRef Value(
285 getRelocationValueRef(Obj, RelI, RE, ObjSectionToID));
Lang Hamesa5216882014-07-17 18:54:50 +0000286
287 bool IsExtern = Obj.getPlainRelocationExternal(RelInfo);
288 if (!IsExtern && RE.IsPCRel)
Rafael Espindola76ad2322015-07-06 14:55:37 +0000289 makeValueAddendPCRel(Value, RelI, 1 << RE.Size);
Lang Hamesa5216882014-07-17 18:54:50 +0000290
Lang Hamesca279c22014-09-07 04:03:32 +0000291 RE.Addend = Value.Offset;
Lang Hamesa5216882014-07-17 18:54:50 +0000292
293 if (RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGE21 ||
294 RE.RelType == MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12)
295 processGOTRelocation(RE, Value, Stubs);
296 else {
297 if (Value.SymbolName)
298 addRelocationForSymbol(RE, Value.SymbolName);
299 else
300 addRelocationForSection(RE, Value.SectionID);
301 }
302
303 return ++RelI;
304 }
305
Benjamin Kramer8c90fd72014-09-03 11:41:21 +0000306 void resolveRelocation(const RelocationEntry &RE, uint64_t Value) override {
Lang Hamesa5216882014-07-17 18:54:50 +0000307 DEBUG(dumpRelocationToResolve(RE, Value));
308
309 const SectionEntry &Section = Sections[RE.SectionID];
Sanjoy Das277776a2015-11-23 21:47:41 +0000310 uint8_t *LocalAddress = Section.getAddressWithOffset(RE.Offset);
Juergen Ributzkafbd40c32014-07-29 19:57:11 +0000311 MachO::RelocationInfoType RelType =
312 static_cast<MachO::RelocationInfoType>(RE.RelType);
Lang Hamesa5216882014-07-17 18:54:50 +0000313
Juergen Ributzkafbd40c32014-07-29 19:57:11 +0000314 switch (RelType) {
Lang Hamesa5216882014-07-17 18:54:50 +0000315 default:
316 llvm_unreachable("Invalid relocation type!");
317 case MachO::ARM64_RELOC_UNSIGNED: {
318 assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_UNSIGNED not supported");
319 // Mask in the target value a byte at a time (we don't have an alignment
320 // guarantee for the target address, so this is safest).
321 if (RE.Size < 2)
322 llvm_unreachable("Invalid size for ARM64_RELOC_UNSIGNED");
323
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000324 encodeAddend(LocalAddress, 1 << RE.Size, RelType, Value + RE.Addend);
Lang Hamesa5216882014-07-17 18:54:50 +0000325 break;
326 }
327 case MachO::ARM64_RELOC_BRANCH26: {
328 assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_BRANCH26 not supported");
Lang Hamesa5216882014-07-17 18:54:50 +0000329 // Check if branch is in range.
Sanjoy Das277776a2015-11-23 21:47:41 +0000330 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000331 int64_t PCRelVal = Value - FinalAddress + RE.Addend;
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000332 encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
Lang Hamesa5216882014-07-17 18:54:50 +0000333 break;
334 }
335 case MachO::ARM64_RELOC_GOT_LOAD_PAGE21:
336 case MachO::ARM64_RELOC_PAGE21: {
337 assert(RE.IsPCRel && "not PCRel and ARM64_RELOC_PAGE21 not supported");
Lang Hamesa5216882014-07-17 18:54:50 +0000338 // Adjust for PC-relative relocation and offset.
Sanjoy Das277776a2015-11-23 21:47:41 +0000339 uint64_t FinalAddress = Section.getLoadAddressWithOffset(RE.Offset);
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000340 int64_t PCRelVal =
341 ((Value + RE.Addend) & (-4096)) - (FinalAddress & (-4096));
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000342 encodeAddend(LocalAddress, /*Size=*/4, RelType, PCRelVal);
Lang Hamesa5216882014-07-17 18:54:50 +0000343 break;
344 }
345 case MachO::ARM64_RELOC_GOT_LOAD_PAGEOFF12:
346 case MachO::ARM64_RELOC_PAGEOFF12: {
347 assert(!RE.IsPCRel && "PCRel and ARM64_RELOC_PAGEOFF21 not supported");
Lang Hamesa5216882014-07-17 18:54:50 +0000348 // Add the offset from the symbol.
349 Value += RE.Addend;
350 // Mask out the page address and only use the lower 12 bits.
351 Value &= 0xFFF;
Juergen Ributzka0e913b12014-07-29 19:57:15 +0000352 encodeAddend(LocalAddress, /*Size=*/4, RelType, Value);
Lang Hamesa5216882014-07-17 18:54:50 +0000353 break;
354 }
Lang Hames3db630b2016-01-21 21:59:50 +0000355 case MachO::ARM64_RELOC_SUBTRACTOR: {
356 uint64_t SectionABase = Sections[RE.Sections.SectionA].getLoadAddress();
357 uint64_t SectionBBase = Sections[RE.Sections.SectionB].getLoadAddress();
358 assert((Value == SectionABase || Value == SectionBBase) &&
359 "Unexpected SUBTRACTOR relocation value.");
360 Value = SectionABase - SectionBBase + RE.Addend;
361 writeBytesUnaligned(Value, LocalAddress, 1 << RE.Size);
362 break;
363 }
Lang Hamesa5216882014-07-17 18:54:50 +0000364 case MachO::ARM64_RELOC_POINTER_TO_GOT:
365 case MachO::ARM64_RELOC_TLVP_LOAD_PAGE21:
366 case MachO::ARM64_RELOC_TLVP_LOAD_PAGEOFF12:
Juergen Ributzkaf5609282014-07-22 21:42:55 +0000367 llvm_unreachable("Relocation type not yet implemented!");
Lang Hamesa5216882014-07-17 18:54:50 +0000368 case MachO::ARM64_RELOC_ADDEND:
369 llvm_unreachable("ARM64_RELOC_ADDEND should have been handeled by "
370 "processRelocationRef!");
371 }
372 }
373
Lang Hamesb5c7b1f2014-11-26 16:54:40 +0000374 void finalizeSection(const ObjectFile &Obj, unsigned SectionID,
Lang Hamesa5216882014-07-17 18:54:50 +0000375 const SectionRef &Section) {}
376
377private:
378 void processGOTRelocation(const RelocationEntry &RE,
379 RelocationValueRef &Value, StubMap &Stubs) {
380 assert(RE.Size == 2);
381 SectionEntry &Section = Sections[RE.SectionID];
382 StubMap::const_iterator i = Stubs.find(Value);
Lang Hames41d95942014-10-21 23:41:15 +0000383 int64_t Offset;
Lang Hamesa5216882014-07-17 18:54:50 +0000384 if (i != Stubs.end())
Lang Hames41d95942014-10-21 23:41:15 +0000385 Offset = static_cast<int64_t>(i->second);
Lang Hamesa5216882014-07-17 18:54:50 +0000386 else {
387 // FIXME: There must be a better way to do this then to check and fix the
388 // alignment every time!!!
Sanjoy Das277776a2015-11-23 21:47:41 +0000389 uintptr_t BaseAddress = uintptr_t(Section.getAddress());
Lang Hamesa5216882014-07-17 18:54:50 +0000390 uintptr_t StubAlignment = getStubAlignment();
391 uintptr_t StubAddress =
Sanjoy Das277776a2015-11-23 21:47:41 +0000392 (BaseAddress + Section.getStubOffset() + StubAlignment - 1) &
Lang Hamesa5216882014-07-17 18:54:50 +0000393 -StubAlignment;
394 unsigned StubOffset = StubAddress - BaseAddress;
395 Stubs[Value] = StubOffset;
396 assert(((StubAddress % getStubAlignment()) == 0) &&
397 "GOT entry not aligned");
398 RelocationEntry GOTRE(RE.SectionID, StubOffset,
Lang Hamesca279c22014-09-07 04:03:32 +0000399 MachO::ARM64_RELOC_UNSIGNED, Value.Offset,
Lang Hamesa5216882014-07-17 18:54:50 +0000400 /*IsPCRel=*/false, /*Size=*/3);
401 if (Value.SymbolName)
402 addRelocationForSymbol(GOTRE, Value.SymbolName);
403 else
404 addRelocationForSection(GOTRE, Value.SectionID);
Sanjoy Das277776a2015-11-23 21:47:41 +0000405 Section.advanceStubOffset(getMaxStubSize());
Lang Hames41d95942014-10-21 23:41:15 +0000406 Offset = static_cast<int64_t>(StubOffset);
Lang Hamesa5216882014-07-17 18:54:50 +0000407 }
Lang Hames41d95942014-10-21 23:41:15 +0000408 RelocationEntry TargetRE(RE.SectionID, RE.Offset, RE.RelType, Offset,
Lang Hamesa5216882014-07-17 18:54:50 +0000409 RE.IsPCRel, RE.Size);
Lang Hames41d95942014-10-21 23:41:15 +0000410 addRelocationForSection(TargetRE, RE.SectionID);
Lang Hamesa5216882014-07-17 18:54:50 +0000411 }
Lang Hames3db630b2016-01-21 21:59:50 +0000412
413 relocation_iterator
414 processSubtractRelocation(unsigned SectionID, relocation_iterator RelI,
415 const ObjectFile &BaseObjT,
416 ObjSectionToIDMap &ObjSectionToID) {
417 const MachOObjectFile &Obj =
418 static_cast<const MachOObjectFile&>(BaseObjT);
419 MachO::any_relocation_info RE =
420 Obj.getRelocation(RelI->getRawDataRefImpl());
421
422 unsigned Size = Obj.getAnyRelocationLength(RE);
423 uint64_t Offset = RelI->getOffset();
424 uint8_t *LocalAddress = Sections[SectionID].getAddressWithOffset(Offset);
425 unsigned NumBytes = 1 << Size;
426
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000427 Expected<StringRef> SubtrahendNameOrErr = RelI->getSymbol()->getName();
428 if (!SubtrahendNameOrErr) {
429 std::string Buf;
430 raw_string_ostream OS(Buf);
431 logAllUnhandledErrors(SubtrahendNameOrErr.takeError(), OS, "");
432 OS.flush();
433 report_fatal_error(Buf);
434 }
Lang Hames3db630b2016-01-21 21:59:50 +0000435 auto SubtrahendI = GlobalSymbolTable.find(*SubtrahendNameOrErr);
436 unsigned SectionBID = SubtrahendI->second.getSectionID();
437 uint64_t SectionBOffset = SubtrahendI->second.getOffset();
438 int64_t Addend =
439 SignExtend64(readBytesUnaligned(LocalAddress, NumBytes), NumBytes * 8);
440
441 ++RelI;
Kevin Enderby81e8b7d2016-04-20 21:24:34 +0000442 Expected<StringRef> MinuendNameOrErr = RelI->getSymbol()->getName();
443 if (!MinuendNameOrErr) {
444 std::string Buf;
445 raw_string_ostream OS(Buf);
446 logAllUnhandledErrors(MinuendNameOrErr.takeError(), OS, "");
447 OS.flush();
448 report_fatal_error(Buf);
449 }
Lang Hames3db630b2016-01-21 21:59:50 +0000450 auto MinuendI = GlobalSymbolTable.find(*MinuendNameOrErr);
451 unsigned SectionAID = MinuendI->second.getSectionID();
452 uint64_t SectionAOffset = MinuendI->second.getOffset();
453
454 RelocationEntry R(SectionID, Offset, MachO::ARM64_RELOC_SUBTRACTOR, (uint64_t)Addend,
455 SectionAID, SectionAOffset, SectionBID, SectionBOffset,
456 false, Size);
457
458 addRelocationForSection(R, SectionAID);
459
460 return ++RelI;
461 }
462
Lang Hamesa5216882014-07-17 18:54:50 +0000463};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000464}
Lang Hamesa5216882014-07-17 18:54:50 +0000465
466#undef DEBUG_TYPE
467
Benjamin Kramera7c40ef2014-08-13 16:26:38 +0000468#endif