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@
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(2)),
37 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32,
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 4; }
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", "__nl_symbol_ptr", 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(2)) { 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 4; }
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", "__nl_symbol_ptr", 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(1, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, compressedImageCache(pass)),
96 _fixup2(7, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, compressedFastBinder(pass))
97 { pass.addAtom(*this); }
99 virtual ld::File* file() const { return NULL; }
100 virtual const char* name() const { return "helper helper"; }
101 virtual uint64_t size() const { return 12; }
102 virtual uint64_t objectAddress() const { return 0; }
103 virtual void copyRawContent(uint8_t buffer[]) const {
104 buffer[0] = 0x68; // pushl $dyld_ImageLoaderCache
109 buffer[5] = 0xFF; // jmp *_fast_lazy_bind
115 buffer[11] = 0x90; // nop
117 virtual void setScope(Scope) { }
118 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
119 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
122 static ld::Atom* compressedImageCache(ld::passes::stubs::Pass& pass) {
123 if ( pass.compressedImageCache == NULL )
124 pass.compressedImageCache = new ImageCachePointerAtom(pass);
125 return pass.compressedImageCache;
127 static ld::Atom* compressedFastBinder(ld::passes::stubs::Pass& pass) {
128 if ( pass.compressedFastBinderPointer == NULL )
129 pass.compressedFastBinderPointer = new FastBindingPointerAtom(pass);
130 return pass.compressedFastBinderPointer;
133 mutable ld::Fixup _fixup1;
136 static ld::Section _s_section;
139 ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
143 class StubHelperAtom : public ld::Atom {
145 StubHelperAtom(ld::passes::stubs::Pass& pass, const ld::Atom* lazyPointer,
146 const ld::Atom& stubTo)
147 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
148 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
149 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
151 _fixup1(1, ld::Fixup::k1of2, ld::Fixup::kindSetLazyOffset, lazyPointer),
152 _fixup2(1, ld::Fixup::k2of2, ld::Fixup::kindStoreLittleEndian32),
153 _fixup3(6, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressX86BranchPCRel32, helperHelper(pass)) { }
155 virtual const ld::File* file() const { return _stubTo.file(); }
156 virtual const char* name() const { return _stubTo.name(); }
157 virtual uint64_t size() const { return 10; }
158 virtual uint64_t objectAddress() const { return 0; }
159 virtual void copyRawContent(uint8_t buffer[]) const {
160 buffer[0] = 0x68; // pushl $lazy-info-offset
165 buffer[5] = 0xE9; // jmp helperhelper
171 virtual void setScope(Scope) { }
172 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
173 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
176 static ld::Atom* helperHelper(ld::passes::stubs::Pass& pass) {
177 if ( pass.compressedHelperHelper == NULL )
178 pass.compressedHelperHelper = new StubHelperHelperAtom(pass);
179 return pass.compressedHelperHelper;
182 const ld::Atom& _stubTo;
183 mutable ld::Fixup _fixup1;
187 static ld::Section _s_section;
190 ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
193 class ResolverHelperAtom : public ld::Atom {
195 ResolverHelperAtom(ld::passes::stubs::Pass& pass, const ld::Atom* lazyPointer,
196 const ld::Atom& stubTo)
197 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
198 ld::Atom::scopeLinkageUnit, ld::Atom::typeStubHelper,
199 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
201 _fixup1(4, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressX86BranchPCRel32, &stubTo),
202 _fixup2(9, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, lazyPointer),
203 _fixup3(18, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, lazyPointer) { }
205 virtual const ld::File* file() const { return _stubTo.file(); }
206 virtual const char* name() const { return _stubTo.name(); }
207 virtual uint64_t size() const { return 22; }
208 virtual uint64_t objectAddress() const { return 0; }
209 virtual void copyRawContent(uint8_t buffer[]) const {
210 buffer[ 0] = 0x50; // push %eax
211 buffer[ 1] = 0x51; // push %ecx
212 buffer[ 2] = 0x52; // push %edx
213 buffer[ 3] = 0xE8; // call foo
218 buffer[ 8] = 0xA3; // movl %eax,foo$lazy_ptr
223 buffer[13] = 0x5A; // pop %edx
224 buffer[14] = 0x59; // pop %ecx
225 buffer[15] = 0x58; // pop %eax
226 buffer[16] = 0xFF; // jmp *foo$lazy_ptr
233 virtual void setScope(Scope) { }
234 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
235 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup3)[1]; }
239 const ld::Atom& _stubTo;
240 mutable ld::Fixup _fixup1;
244 static ld::Section _s_section;
247 ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
251 class LazyPointerAtom : public ld::Atom {
253 LazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
254 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport)
255 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
256 ld::Atom::scopeTranslationUnit, ld::Atom::typeLazyPointer,
257 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
259 _helper(pass, this, stubTo),
260 _resolverHelper(pass, this, stubTo),
261 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32,
262 stubToResolver ? &_resolverHelper : (stubToGlobalWeakDef ? &stubTo : &_helper)),
263 _fixup2(0, ld::Fixup::k1of1, ld::Fixup::kindLazyTarget, &stubTo) {
264 _fixup2.weakImport = weakImport; pass.addAtom(*this);
265 if ( stubToResolver )
266 pass.addAtom(_resolverHelper);
267 else if ( !stubToGlobalWeakDef )
268 pass.addAtom(_helper);
271 virtual const ld::File* file() const { return _stubTo.file(); }
272 virtual const char* name() const { return _stubTo.name(); }
273 virtual uint64_t size() const { return 4; }
274 virtual uint64_t objectAddress() const { return 0; }
275 virtual void copyRawContent(uint8_t buffer[]) const { }
276 virtual void setScope(Scope) { }
277 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
278 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup2)[1]; }
281 const ld::Atom& _stubTo;
282 StubHelperAtom _helper;
283 ResolverHelperAtom _resolverHelper;
284 mutable ld::Fixup _fixup1;
287 static ld::Section _s_section;
290 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
293 class StubAtom : public ld::Atom {
295 StubAtom(ld::passes::stubs::Pass& pass, const ld::Atom& stubTo,
296 bool stubToGlobalWeakDef, bool stubToResolver, bool weakImport)
297 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
298 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
299 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
301 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport),
302 _fixup(2, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer) { pass.addAtom(*this); }
304 virtual const ld::File* file() const { return _stubTo.file(); }
305 virtual const char* name() const { return _stubTo.name(); }
306 virtual uint64_t size() const { return 6; }
307 virtual uint64_t objectAddress() const { return 0; }
308 virtual void copyRawContent(uint8_t buffer[]) const {
309 buffer[0] = 0xFF; // jmp *foo$lazy_pointer
316 virtual void setScope(Scope) { }
317 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup; }
318 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
321 const ld::Atom& _stubTo;
322 LazyPointerAtom _lazyPointer;
323 mutable ld::Fixup _fixup;
325 static ld::Section _s_section;
328 ld::Section StubAtom::_s_section("__TEXT", "__symbol_stub", ld::Section::typeStub);