]> git.saurik.com Git - apple/ld64.git/blame - src/ld/passes/stubs/stub_arm64.hpp
ld64-264.3.102.tar.gz
[apple/ld64.git] / src / ld / passes / stubs / stub_arm64.hpp
CommitLineData
f80fe69f
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2010-2013 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26// already in ld::passes::stubs namespace
27namespace arm64 {
28
29
30
31class FastBindingPointerAtom : public ld::Atom {
32public:
33 FastBindingPointerAtom(ld::passes::stubs::Pass& pass)
34 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
35 ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
36 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)),
37 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64,
38 pass.internal()->compressedFastBinderProxy)
39 { pass.addAtom(*this); }
40
41 virtual const ld::File* file() const { return NULL; }
42 virtual const char* name() const { return "fast binder pointer"; }
43 virtual uint64_t size() const { return 8; }
44 virtual uint64_t objectAddress() const { return 0; }
45 virtual void copyRawContent(uint8_t buffer[]) const { }
46 virtual void setScope(Scope) { }
47 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup; }
48 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
49
50private:
51 mutable ld::Fixup _fixup;
52
53 static ld::Section _s_section;
54};
55
56ld::Section FastBindingPointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
57
58
59class ImageCachePointerAtom : public ld::Atom {
60public:
61 ImageCachePointerAtom(ld::passes::stubs::Pass& pass)
62 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
63 ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
64 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)) { pass.addAtom(*this); }
65
66 virtual const ld::File* file() const { return NULL; }
67 virtual const char* name() const { return "image cache pointer"; }
68 virtual uint64_t size() const { return 8; }
69 virtual uint64_t objectAddress() const { return 0; }
70 virtual void copyRawContent(uint8_t buffer[]) const { }
71 virtual void setScope(Scope) { }
72
73private:
74
75 static ld::Section _s_section;
76};
77
78ld::Section ImageCachePointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
79
80
81
82
83
84//
85// The stub-helper-helper is the common code factored out of each helper function.
86// It is in the same section as the stub-helpers.
87// Similar to the PLT0 entry in ELF.
88//
89class StubHelperHelperAtom : public ld::Atom {
90public:
91 StubHelperHelperAtom(ld::passes::stubs::Pass& pass)
92 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
93 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
94 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
95 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, compressedImageCache(pass)),
96 _fixup2(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64PageOff12, compressedImageCache(pass)),
97 _fixup3(12, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, compressedFastBinder(pass)),
9543cb2f
A
98 _fixup4(16, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64PageOff12, compressedFastBinder(pass)),
99 _fixup5(ld::Fixup::kindLinkerOptimizationHint, LOH_ARM64_ADRP_ADD, 0, 4),
100 _fixup6(ld::Fixup::kindLinkerOptimizationHint, LOH_ARM64_ADRP_LDR, 12, 16)
f80fe69f
A
101 { pass.addAtom(*this); }
102
103 virtual ld::File* file() const { return NULL; }
104 virtual const char* name() const { return "helper helper"; }
105 virtual uint64_t size() const { return 24; }
106 virtual uint64_t objectAddress() const { return 0; }
107 virtual void copyRawContent(uint8_t buffer[]) const {
108 OSWriteLittleInt32(&buffer[ 0], 0, 0x90000011); // ADRP X17, dyld_mageLoaderCache@page
109 OSWriteLittleInt32(&buffer[ 4], 0, 0x91000231); // ADD X17, X17, dyld_mageLoaderCache@pageoff
110 OSWriteLittleInt32(&buffer[ 8], 0, 0xA9BF47F0); // STP X16/X17, [SP, #-16]!
111 OSWriteLittleInt32(&buffer[12], 0, 0x90000010); // ADRP X16, _fast_lazy_bind@page
112 OSWriteLittleInt32(&buffer[16], 0, 0xF9400210); // LDR X16, [X16,_fast_lazy_bind@pageoff]
113 OSWriteLittleInt32(&buffer[20], 0, 0xD61F0200); // BR X16
114 }
115 virtual void setScope(Scope) { }
116 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
9543cb2f 117 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup6)[1]; }
f80fe69f
A
118
119private:
120 static ld::Atom* compressedImageCache(ld::passes::stubs::Pass& pass) {
121 if ( pass.compressedImageCache == NULL )
122 pass.compressedImageCache = new ImageCachePointerAtom(pass);
123 return pass.compressedImageCache;
124 }
125 static ld::Atom* compressedFastBinder(ld::passes::stubs::Pass& pass) {
126 if ( pass.compressedFastBinderPointer == NULL )
127 pass.compressedFastBinderPointer = new FastBindingPointerAtom(pass);
128 return pass.compressedFastBinderPointer;
129 }
130
131 mutable ld::Fixup _fixup1;
132 ld::Fixup _fixup2;
133 ld::Fixup _fixup3;
134 ld::Fixup _fixup4;
9543cb2f
A
135 ld::Fixup _fixup5;
136 ld::Fixup _fixup6;
f80fe69f
A
137
138 static ld::Section _s_section;
139};
140
141ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
142
143
144
145class StubHelperAtom : public ld::Atom {
146public:
147 StubHelperAtom(ld::passes::stubs::Pass& pass, const ld::Atom* lazyPointer,
148 const ld::Atom& stubTo)
149 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
150 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
151 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
152 _stubTo(stubTo),
153 _fixup1(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Branch26, helperHelper(pass)),
154 _fixup2(8, ld::Fixup::k1of2, ld::Fixup::kindSetLazyOffset, lazyPointer),
155 _fixup3(8, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32) { }
156
157 virtual const ld::File* file() const { return _stubTo.file(); }
158 virtual const char* name() const { return _stubTo.name(); }
159 virtual uint64_t size() const { return 12; }
160 virtual uint64_t objectAddress() const { return 0; }
161 virtual void copyRawContent(uint8_t buffer[]) const {
162 OSWriteLittleInt32(&buffer[0], 0, 0x18000050); // LDR W16, L0
163 OSWriteLittleInt32(&buffer[4], 0, 0x14000000); // B helperhelper
164 OSWriteLittleInt32(&buffer[8], 0, 0x00000000); // L0: .long 0
165 }
166 virtual void setScope(Scope) { }
167 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
168 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
169
170private:
171 static ld::Atom* helperHelper(ld::passes::stubs::Pass& pass) {
172 if ( pass.compressedHelperHelper == NULL )
173 pass.compressedHelperHelper = new StubHelperHelperAtom(pass);
174 return pass.compressedHelperHelper;
175 }
176
177 const ld::Atom& _stubTo;
178 mutable ld::Fixup _fixup1;
179 ld::Fixup _fixup2;
180 ld::Fixup _fixup3;
181
182 static ld::Section _s_section;
183};
184
185ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
186
187
188class ResolverHelperAtom : public ld::Atom {
189public:
190 ResolverHelperAtom(ld::passes::stubs::Pass& pass, const ld::Atom* lazyPointer,
191 const ld::Atom& stubTo)
192 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
193 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
194 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
195 _stubTo(stubTo),
196 _fixup1(24, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Branch26, &stubTo),
197 _fixup2(28, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, lazyPointer),
198 _fixup3(32, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64PageOff12, lazyPointer) { }
199
200 virtual const ld::File* file() const { return _stubTo.file(); }
201 virtual const char* name() const { return _stubTo.name(); }
202 virtual uint64_t size() const { return 68; }
203 virtual uint64_t objectAddress() const { return 0; }
204 virtual void copyRawContent(uint8_t buffer[]) const {
205 OSWriteLittleInt32(&buffer[ 0], 0, 0xa9bf7bfd); // stp fp, lr, [sp, #-16]!
206 OSWriteLittleInt32(&buffer[ 4], 0, 0x910003fd); // mov fp, sp
207 OSWriteLittleInt32(&buffer[ 8], 0, 0xa9bf03e1); // stp x1, x0, [sp, #-16]!
208 OSWriteLittleInt32(&buffer[12], 0, 0xa9bf0be3); // stp x3, x2, [sp, #-16]!
209 OSWriteLittleInt32(&buffer[16], 0, 0xa9bf13e5); // stp x5, x4, [sp, #-16]!
210 OSWriteLittleInt32(&buffer[20], 0, 0xa9bf1be7); // stp x7, x6, [sp, #-16]!
211 OSWriteLittleInt32(&buffer[24], 0, 0x94000000); // bl _foo
212 OSWriteLittleInt32(&buffer[28], 0, 0x90000010); // adrp x16, lazy_pointer@PAGE
213 OSWriteLittleInt32(&buffer[32], 0, 0x91000210); // add x16, x16, lazy_pointer@PAGEOFF
214 OSWriteLittleInt32(&buffer[36], 0, 0xf9000200); // str x0, [x16]
215 OSWriteLittleInt32(&buffer[40], 0, 0xaa0003f0); // mov x16, x0
216 OSWriteLittleInt32(&buffer[44], 0, 0xa8c11be7); // ldp x7, x6, [sp], #16
217 OSWriteLittleInt32(&buffer[48], 0, 0xa8c113e5); // ldp x5, x4, [sp], #16
218 OSWriteLittleInt32(&buffer[52], 0, 0xa8c10be3); // ldp x3, x2, [sp], #16
219 OSWriteLittleInt32(&buffer[56], 0, 0xa8c103e1); // ldp x1, x0, [sp], #16
220 OSWriteLittleInt32(&buffer[60], 0, 0xa8c17bfd); // ldp fp, lr, [sp], #16
221 OSWriteLittleInt32(&buffer[64], 0, 0xd61f0200); // br x16
222 }
223 virtual void setScope(Scope) { }
224 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
225 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
226
227private:
228
229 const ld::Atom& _stubTo;
230 mutable ld::Fixup _fixup1;
231 ld::Fixup _fixup2;
232 ld::Fixup _fixup3;
233
234 static ld::Section _s_section;
235};
236
237ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
238
239
240
241class LazyPointerAtom : public ld::Atom {
242public:
243 LazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
eaf282aa
A
244 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport, bool dataConstUsed)
245 : ld::Atom(selectSection(stubToGlobalWeakDef, stubToResolver, dataConstUsed),
246 ld::Atom::definitionRegular, ld::Atom::combineNever,
f80fe69f
A
247 ld::Atom::scopeTranslationUnit, ld::Atom::typeLazyPointer,
248 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)),
249 _stubTo(stubTo),
250 _helper(pass, this, stubTo),
251 _resolverHelper(pass, this, stubTo),
252 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64,
253 stubToResolver ? &_resolverHelper :
254 (stubToGlobalWeakDef ? &stubTo : &_helper)),
255 _fixup2(0, ld::Fixup::k1of1, ld::Fixup::kindLazyTarget, &stubTo) {
256 _fixup2.weakImport = weakImport; pass.addAtom(*this);
257 if ( stubToResolver )
258 pass.addAtom(_resolverHelper);
259 else if ( !stubToGlobalWeakDef )
260 pass.addAtom(_helper);
261 }
262
263 virtual const ld::File* file() const { return _stubTo.file(); }
264 virtual const char* name() const { return _stubTo.name(); }
265 virtual uint64_t size() const { return 8; }
266 virtual uint64_t objectAddress() const { return 0; }
267 virtual void copyRawContent(uint8_t buffer[]) const { }
268 virtual void setScope(Scope) { }
269 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
270 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
271
272private:
eaf282aa
A
273 static ld::Section& selectSection(bool stubToGlobalWeakDef, bool stubToResolver, bool dataConstUsed) {
274 if ( stubToGlobalWeakDef && dataConstUsed )
275 return _s_sectionWeak;
276 else if ( stubToResolver && dataConstUsed )
277 return _s_sectionResolver;
278 else
279 return _s_section;
280 }
281
f80fe69f
A
282 const ld::Atom& _stubTo;
283 StubHelperAtom _helper;
284 ResolverHelperAtom _resolverHelper;
285 mutable ld::Fixup _fixup1;
286 ld::Fixup _fixup2;
287
288 static ld::Section _s_section;
eaf282aa
A
289 static ld::Section _s_sectionResolver;
290 static ld::Section _s_sectionWeak;
f80fe69f
A
291};
292
293ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
eaf282aa
A
294ld::Section LazyPointerAtom::_s_sectionResolver("__DATA_DIRTY", "__la_resolver", ld::Section::typeLazyPointer);
295ld::Section LazyPointerAtom::_s_sectionWeak("__DATA", "__la_weak_ptr", ld::Section::typeLazyPointer);
f80fe69f
A
296
297
298class StubAtom : public ld::Atom {
299public:
300 StubAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
eaf282aa 301 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport, bool dataConstUsed)
f80fe69f
A
302 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
303 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
304 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
305 _stubTo(stubTo),
eaf282aa 306 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, dataConstUsed),
f80fe69f 307 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, &_lazyPointer),
9543cb2f
A
308 _fixup2(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64PageOff12, &_lazyPointer),
309 _fixup3(ld::Fixup::kindLinkerOptimizationHint, LOH_ARM64_ADRP_LDR, 0, 4)
310 { pass.addAtom(*this); }
f80fe69f
A
311
312 virtual const ld::File* file() const { return _stubTo.file(); }
313 virtual const char* name() const { return _stubTo.name(); }
314 virtual uint64_t size() const { return 12; }
315 virtual uint64_t objectAddress() const { return 0; }
316 virtual void copyRawContent(uint8_t buffer[]) const {
317 OSWriteLittleInt32(&buffer[0], 0, 0x90000010); // ADRP X16, lazy_pointer@page
318 OSWriteLittleInt32(&buffer[4], 0, 0xF9400210); // LDR X16, [X16, lazy_pointer@pageoff]
319 OSWriteLittleInt32(&buffer[8], 0, 0xD61F0200); // BR X16
320 }
321 virtual void setScope(Scope) { }
322 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
9543cb2f 323 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
f80fe69f
A
324
325private:
326 const ld::Atom& _stubTo;
327 LazyPointerAtom _lazyPointer;
328 mutable ld::Fixup _fixup1;
329 mutable ld::Fixup _fixup2;
9543cb2f 330 mutable ld::Fixup _fixup3;
f80fe69f
A
331
332 static ld::Section _s_section;
333};
334
335ld::Section StubAtom::_s_section("__TEXT", "__stubs", ld::Section::typeStub);
336
337
338
339class NonLazyPointerAtom : public ld::Atom {
340public:
341 NonLazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo)
342 : ld::Atom(_s_section, ld::Atom::definitionRegular,
343 ld::Atom::combineNever, ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
344 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)),
345 _stubTo(stubTo),
346 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, &stubTo) {
347 pass.addAtom(*this);
348 }
349
350 virtual const ld::File* file() const { return _stubTo.file(); }
351 virtual const char* name() const { return _stubTo.name(); }
352 virtual uint64_t size() const { return 8; }
353 virtual uint64_t objectAddress() const { return 0; }
354 virtual void copyRawContent(uint8_t buffer[]) const { }
355 virtual void setScope(Scope) { }
356 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
357 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup1)[1]; }
358
359private:
360 const ld::Atom& _stubTo;
361 ld::Fixup _fixup1;
362
363 static ld::Section _s_section;
364};
365
366ld::Section NonLazyPointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
367
368
369class KextStubAtom : public ld::Atom {
370public:
371 KextStubAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo)
372 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
373 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
374 symbolTableIn, false, false, false, ld::Atom::Alignment(1)),
375 _stubTo(stubTo),
376 _nonLazyPointer(pass, stubTo),
377 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, &_nonLazyPointer),
378 _fixup2(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64PageOff12, &_nonLazyPointer) {
379 pass.addAtom(*this);
380 asprintf((char**)&_name, "%s.stub", _stubTo.name());
381 }
382
383 virtual const ld::File* file() const { return _stubTo.file(); }
384 virtual const char* name() const { return _name; }
385 virtual uint64_t size() const { return 12; }
386 virtual uint64_t objectAddress() const { return 0; }
387 virtual void copyRawContent(uint8_t buffer[]) const {
388 OSWriteLittleInt32(&buffer[0], 0, 0x90000010); // ADRP X16, non_lazy_pointer@page
389 OSWriteLittleInt32(&buffer[4], 0, 0xF9400210); // LDR X16, [X16, non_lazy_pointer@pageoff]
390 OSWriteLittleInt32(&buffer[8], 0, 0xD61F0200); // BR X16
391 }
392 virtual void setScope(Scope) { }
393 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
394 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
395
396private:
397 const ld::Atom& _stubTo;
398 const char* _name;
399 NonLazyPointerAtom _nonLazyPointer;
400 mutable ld::Fixup _fixup1;
401 mutable ld::Fixup _fixup2;
402
403 static ld::Section _s_section;
404};
405
406ld::Section KextStubAtom::_s_section("__TEXT", "__stubs", ld::Section::typeCode);
407
408
409} // namespace x86_64
410