1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2010-2013 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
26 // already in ld::passes::stubs namespace
31 class FastBindingPointerAtom : public ld::Atom {
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); }
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]; }
51 mutable ld::Fixup _fixup;
53 static ld::Section _s_section;
56 ld::Section FastBindingPointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
59 class ImageCachePointerAtom : public ld::Atom {
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); }
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) { }
75 static ld::Section _s_section;
78 ld::Section ImageCachePointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
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.
89 class StubHelperHelperAtom : public ld::Atom {
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)),
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)
101 { pass.addAtom(*this); }
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
115 virtual void setScope(Scope) { }
116 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
117 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup6)[1]; }
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;
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;
131 mutable ld::Fixup _fixup1;
138 static ld::Section _s_section;
141 ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
145 class StubHelperAtom : public ld::Atom {
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)),
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) { }
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
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]; }
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;
177 const ld::Atom& _stubTo;
178 mutable ld::Fixup _fixup1;
182 static ld::Section _s_section;
185 ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
188 class ResolverHelperAtom : public ld::Atom {
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)),
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) { }
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
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]; }
229 const ld::Atom& _stubTo;
230 mutable ld::Fixup _fixup1;
234 static ld::Section _s_section;
237 ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
241 class LazyPointerAtom : public ld::Atom {
243 LazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
244 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport, bool dataConstUsed)
245 : ld::Atom(selectSection(stubToGlobalWeakDef, stubToResolver, dataConstUsed),
246 ld::Atom::definitionRegular, ld::Atom::combineNever,
247 ld::Atom::scopeTranslationUnit, ld::Atom::typeLazyPointer,
248 symbolTableNotIn, false, false, false, ld::Atom::Alignment(3)),
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);
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]; }
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;
282 const ld::Atom& _stubTo;
283 StubHelperAtom _helper;
284 ResolverHelperAtom _resolverHelper;
285 mutable ld::Fixup _fixup1;
288 static ld::Section _s_section;
289 static ld::Section _s_sectionResolver;
290 static ld::Section _s_sectionWeak;
293 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
294 ld::Section LazyPointerAtom::_s_sectionResolver("__DATA_DIRTY", "__la_resolver", ld::Section::typeLazyPointer);
295 ld::Section LazyPointerAtom::_s_sectionWeak("__DATA", "__la_weak_ptr", ld::Section::typeLazyPointer);
298 class StubAtom : public ld::Atom {
300 StubAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
301 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport, bool dataConstUsed)
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)),
306 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, dataConstUsed),
307 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, &_lazyPointer),
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); }
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
321 virtual void setScope(Scope) { }
322 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
323 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
326 const ld::Atom& _stubTo;
327 LazyPointerAtom _lazyPointer;
328 mutable ld::Fixup _fixup1;
329 mutable ld::Fixup _fixup2;
330 mutable ld::Fixup _fixup3;
332 static ld::Section _s_section;
335 ld::Section StubAtom::_s_section("__TEXT", "__stubs", ld::Section::typeStub);
339 class NonLazyPointerAtom : public ld::Atom {
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)),
346 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian64, &stubTo) {
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]; }
360 const ld::Atom& _stubTo;
363 static ld::Section _s_section;
366 ld::Section NonLazyPointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
369 class KextStubAtom : public ld::Atom {
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)),
376 _nonLazyPointer(pass, stubTo),
377 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64Page21, &_nonLazyPointer),
378 _fixup2(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARM64PageOff12, &_nonLazyPointer) {
380 asprintf((char**)&_name, "%s.stub", _stubTo.name());
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
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]; }
397 const ld::Atom& _stubTo;
399 NonLazyPointerAtom _nonLazyPointer;
400 mutable ld::Fixup _fixup1;
401 mutable ld::Fixup _fixup2;
403 static ld::Section _s_section;
406 ld::Section KextStubAtom::_s_section("__TEXT", "__stubs", ld::Section::typeCode);
409 } // namespace x86_64