1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2009-2010 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@
25 // already in ld::passes::stubs namespace
29 class FastBindingPointerAtom : public ld::Atom {
31 FastBindingPointerAtom(ld::passes::stubs::Pass& pass)
32 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
33 ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
34 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
35 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, pass.internal()->compressedFastBinderProxy)
36 { pass.addAtom(*this); }
38 virtual const ld::File* file() const { return NULL; }
39 virtual bool translationUnitSource(const char** dir, const char** nm) const
41 virtual const char* name() const { return "fast binder pointer"; }
42 virtual uint64_t size() const { return 4; }
43 virtual uint64_t objectAddress() const { return 0; }
44 virtual void copyRawContent(uint8_t buffer[]) const { }
45 virtual void setScope(Scope) { }
46 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup; }
47 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
52 static ld::Section _s_section;
55 ld::Section FastBindingPointerAtom::_s_section("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer);
58 class ImageCachePointerAtom : public ld::Atom {
60 ImageCachePointerAtom(ld::passes::stubs::Pass& pass)
61 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
62 ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
63 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)) { pass.addAtom(*this); }
65 virtual const ld::File* file() const { return NULL; }
66 virtual bool translationUnitSource(const char** dir, const char** nm) const
68 virtual const char* name() const { return "non-lazy pointer"; }
69 virtual uint64_t size() const { return 4; }
70 virtual uint64_t objectAddress() const { return 0; }
71 virtual void copyRawContent(uint8_t buffer[]) const { }
72 virtual void setScope(Scope) { }
76 static ld::Section _s_section;
79 ld::Section ImageCachePointerAtom::_s_section("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer);
83 class StubHelperHelperAtom : public ld::Atom {
85 StubHelperHelperAtom(ld::passes::stubs::Pass& pass)
86 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
87 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
88 ld::Atom::symbolTableIn, false, false, false, ld::Atom::Alignment(2)),
89 _fixup1(28, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, compressedImageCache(pass)),
90 _fixup2(28, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
91 _fixup3(28, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 16),
92 _fixup4(28, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32),
93 _fixup5(32, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, compressedFastBinder(pass)),
94 _fixup6(32, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
95 _fixup7(32, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 28),
96 _fixup8(32, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32)
97 { pass.addAtom(*this); }
99 virtual ld::File* file() const { return NULL; }
100 virtual bool translationUnitSource(const char** dir, const char** nm) const
102 virtual const char* name() const { return " stub helpers"; }
103 virtual uint64_t size() const { return 36; }
104 virtual uint64_t objectAddress() const { return 0; }
105 virtual void copyRawContent(uint8_t buffer[]) const {
106 // push lazy-info-offset
107 OSWriteLittleInt32(&buffer[ 0], 0, 0xe52dc004); // str ip, [sp, #-4]!
108 // push address of dyld_mageLoaderCache
109 OSWriteLittleInt32(&buffer[ 4], 0, 0xe59fc010); // ldr ip, L1
110 OSWriteLittleInt32(&buffer[ 8], 0, 0xe08fc00c); // add ip, pc, ip
111 OSWriteLittleInt32(&buffer[12], 0, 0xe52dc004); // str ip, [sp, #-4]!
112 // jump through _fast_lazy_bind
113 OSWriteLittleInt32(&buffer[16], 0, 0xe59fc008); // ldr ip, L2
114 OSWriteLittleInt32(&buffer[20], 0, 0xe08fc00c); // add ip, pc, ip
115 OSWriteLittleInt32(&buffer[24], 0, 0xe59cf000); // ldr pc, [ip]
116 OSWriteLittleInt32(&buffer[28], 0, 0x00000000); // L1: .long fFastStubGOTAtom - (helperhelper+16)
117 OSWriteLittleInt32(&buffer[32], 0, 0x00000000); // L2: .long _fast_lazy_bind - (helperhelper+28)
119 virtual void setScope(Scope) { }
120 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
121 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup8)[1]; }
124 static ld::Atom* compressedImageCache(ld::passes::stubs::Pass& pass) {
125 if ( pass.compressedImageCache == NULL )
126 pass.compressedImageCache = new ImageCachePointerAtom(pass);
127 return pass.compressedImageCache;
129 static ld::Atom* compressedFastBinder(ld::passes::stubs::Pass& pass) {
130 if ( pass.compressedFastBinderPointer == NULL )
131 pass.compressedFastBinderPointer = new FastBindingPointerAtom(pass);
132 return pass.compressedFastBinderPointer;
144 static ld::Section _s_section;
147 ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
150 class StubHelperAtom : public ld::Atom {
152 StubHelperAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
153 const ld::Atom* lazyPointer)
154 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
155 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
156 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
158 _fixup1(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARMBranch24, helperHelper(pass)),
159 _fixup2(8, ld::Fixup::k1of2, ld::Fixup::kindSetLazyOffset, lazyPointer),
160 _fixup3(8, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32) { }
162 virtual const ld::File* file() const { return _stubTo.file(); }
163 virtual bool translationUnitSource(const char** dir, const char** nm) const
165 virtual const char* name() const { return _stubTo.name(); }
166 virtual uint64_t size() const { return 12; }
167 virtual uint64_t objectAddress() const { return 0; }
168 virtual void copyRawContent(uint8_t buffer[]) const {
169 OSWriteLittleInt32(&buffer[0], 0, 0xe59fc000); // ldr ip, [pc, #0]
170 OSWriteLittleInt32(&buffer[4], 0, 0xea000000); // b _helperhelper
171 OSWriteLittleInt32(&buffer[8], 0, 0); // .long lazy-info-offset
173 virtual void setScope(Scope) { }
174 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
175 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
178 static ld::Atom* helperHelper(ld::passes::stubs::Pass& pass) {
179 if ( pass.compressedHelperHelper == NULL )
180 pass.compressedHelperHelper = new StubHelperHelperAtom(pass);
181 return pass.compressedHelperHelper;
183 const ld::Atom& _stubTo;
188 static ld::Section _s_section;
191 ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
194 class ResolverHelperAtom : public ld::Atom {
196 ResolverHelperAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
197 const ld::Atom* lazyPointer)
198 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
199 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
200 ld::Atom::symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
202 _fixup1( 4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARMBranch24, &stubTo),
203 _fixup2(32, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, lazyPointer),
204 _fixup3(32, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
205 _fixup4(32, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 20),
206 _fixup5(32, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32) { }
208 virtual const ld::File* file() const { return _stubTo.file(); }
209 virtual bool translationUnitSource(const char** dir, const char** nm) const
211 virtual const char* name() const { return _stubTo.name(); }
212 virtual uint64_t size() const { return 36; }
213 virtual uint64_t objectAddress() const { return 0; }
214 virtual void copyRawContent(uint8_t buffer[]) const {
215 OSWriteLittleInt32(&buffer[ 0], 0, 0xe92d400f); // push {r0, r1, r2, r3, lr}
216 OSWriteLittleInt32(&buffer[ 4], 0, 0xebfffffd); // bl _foo
217 OSWriteLittleInt32(&buffer[ 8], 0, 0xe59fc010); // ldr ip, [pc, #16]
218 OSWriteLittleInt32(&buffer[12], 0, 0xe08fc00c); // add ip, pc, ip
219 OSWriteLittleInt32(&buffer[16], 0, 0xe58c0000); // str r0, [ip]
220 OSWriteLittleInt32(&buffer[20], 0, 0xe1a0c000); // mov ip, r0
221 OSWriteLittleInt32(&buffer[24], 0, 0xe8bd400f); // pop {r0, r1, r2, r3, lr}
222 OSWriteLittleInt32(&buffer[28], 0, 0xe12fff1c); // bx ip
223 OSWriteLittleInt32(&buffer[32], 0, 0x00000000); // .long foo$lazyptr - helper + 20
225 virtual void setScope(Scope) { }
226 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
227 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup5)[1]; }
230 const ld::Atom& _stubTo;
237 static ld::Section _s_section;
240 ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
243 class LazyPointerAtom : public ld::Atom {
245 LazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
246 bool stubToGlobalWeakDef, bool stubToResolver,
247 bool weakImport, bool close)
248 : ld::Atom(close ? _s_sectionClose : _s_section, ld::Atom::definitionRegular,
249 ld::Atom::combineNever, ld::Atom::scopeLinkageUnit, ld::Atom::typeLazyPointer,
250 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
252 _helper(pass, stubTo, this),
253 _resolverHelper(pass, stubTo, this),
254 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32,
255 stubToResolver ? &_resolverHelper : (stubToGlobalWeakDef ? &stubTo : &_helper)),
256 _fixup2(0, ld::Fixup::k1of1, ld::Fixup::kindLazyTarget, &stubTo) {
257 _fixup2.weakImport = weakImport; pass.addAtom(*this);
258 if ( stubToResolver )
259 pass.addAtom(_resolverHelper);
260 else if ( !stubToGlobalWeakDef )
261 pass.addAtom(_helper);
264 virtual const ld::File* file() const { return _stubTo.file(); }
265 virtual bool translationUnitSource(const char** dir, const char** nm) const
267 virtual const char* name() const { return _stubTo.name(); }
268 virtual uint64_t size() const { return 4; }
269 virtual uint64_t objectAddress() const { return 0; }
270 virtual void copyRawContent(uint8_t buffer[]) const { }
271 virtual void setScope(Scope) { }
272 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
273 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
276 const ld::Atom& _stubTo;
277 StubHelperAtom _helper;
278 ResolverHelperAtom _resolverHelper;
282 static ld::Section _s_section;
283 static ld::Section _s_sectionClose;
286 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
287 ld::Section LazyPointerAtom::_s_sectionClose("__DATA", "__lazy_symbol", ld::Section::typeLazyPointerClose);
291 class StubPICAtom : public ld::Atom {
293 StubPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
294 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport)
295 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
296 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
297 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
299 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, false),
300 _fixup1(12, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, &_lazyPointer),
301 _fixup2(12, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
302 _fixup3(12, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 12),
303 _fixup4(12, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32)
304 { pass.addAtom(*this); }
306 virtual const ld::File* file() const { return _stubTo.file(); }
307 virtual bool translationUnitSource(const char** dir, const char** nm) const
309 virtual const char* name() const { return _stubTo.name(); }
310 virtual uint64_t size() const { return 16; }
311 virtual uint64_t objectAddress() const { return 0; }
312 virtual void copyRawContent(uint8_t buffer[]) const {
313 OSWriteLittleInt32(&buffer[ 0], 0, 0xe59fc004); // ldr ip, pc + 12
314 OSWriteLittleInt32(&buffer[ 4], 0, 0xe08fc00c); // add ip, pc, ip
315 OSWriteLittleInt32(&buffer[ 8], 0, 0xe59cf000); // ldr pc, [ip]
316 OSWriteLittleInt32(&buffer[12], 0, 0x00000000); // .long L_foo$lazy_ptr - (L1$scv + 8)
318 virtual void setScope(Scope) { }
319 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
320 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup4)[1]; }
323 const ld::Atom& _stubTo;
324 LazyPointerAtom _lazyPointer;
330 static ld::Section _s_section;
333 ld::Section StubPICAtom::_s_section("__TEXT", "__picsymbolstub4", ld::Section::typeStub);
337 class StubNoPICAtom : public ld::Atom {
339 StubNoPICAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
340 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport)
341 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
342 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
343 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
345 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, false),
346 _fixup(8, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer)
347 { pass.addAtom(*this); }
349 virtual const ld::File* file() const { return _stubTo.file(); }
350 virtual bool translationUnitSource(const char** dir, const char** nm) const
352 virtual const char* name() const { return _stubTo.name(); }
353 virtual uint64_t size() const { return 12; }
354 virtual uint64_t objectAddress() const { return 0; }
355 virtual void copyRawContent(uint8_t buffer[]) const {
356 OSWriteLittleInt32(&buffer[ 0], 0, 0xe59fc000); // ldr ip, [pc, #0]
357 OSWriteLittleInt32(&buffer[ 4], 0, 0xe59cf000); // ldr pc, [ip]
358 OSWriteLittleInt32(&buffer[ 8], 0, 0x00000000); // .long L_foo$lazy_ptr
360 virtual void setScope(Scope) { }
361 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup; }
362 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
365 const ld::Atom& _stubTo;
366 LazyPointerAtom _lazyPointer;
369 static ld::Section _s_section;
372 ld::Section StubNoPICAtom::_s_section("__TEXT", "__symbol_stub4", ld::Section::typeStub);
377 class StubCloseAtom : public ld::Atom {
379 StubCloseAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
380 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport)
381 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
382 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
383 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
385 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, true),
386 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARMLoad12, &_lazyPointer)
387 { pass.addAtom(*this); }
389 virtual const ld::File* file() const { return _stubTo.file(); }
390 virtual bool translationUnitSource(const char** dir, const char** nm) const
392 virtual const char* name() const { return _stubTo.name(); }
393 virtual uint64_t size() const { return 4; }
394 virtual uint64_t objectAddress() const { return 0; }
395 virtual void copyRawContent(uint8_t buffer[]) const {
396 OSWriteLittleInt32(&buffer[ 0], 0, 0xE59FF000); // ldr pc, [pc, #foo$lazy_ptr]
398 virtual void setScope(Scope) { }
399 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup; }
400 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
403 const ld::Atom& _stubTo;
404 LazyPointerAtom _lazyPointer;
407 static ld::Section _s_section;
410 ld::Section StubCloseAtom::_s_section("__TEXT", "__symbolstub1", ld::Section::typeStubClose);