]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/stubs/stub_x86.hpp
ld64-274.2.tar.gz
[apple/ld64.git] / src / ld / passes / stubs / stub_x86.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2009-2010 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
27 namespace x86 {
28
29
30
31 class FastBindingPointerAtom : public ld::Atom {
32 public:
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); }
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 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]; }
49
50 private:
51 mutable ld::Fixup _fixup;
52
53 static ld::Section _s_section;
54 };
55
56 ld::Section FastBindingPointerAtom::_s_section("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer);
57
58
59 class ImageCachePointerAtom : public ld::Atom {
60 public:
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); }
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 4; }
69 virtual uint64_t objectAddress() const { return 0; }
70 virtual void copyRawContent(uint8_t buffer[]) const { }
71 virtual void setScope(Scope) { }
72
73 private:
74
75 static ld::Section _s_section;
76 };
77
78 ld::Section ImageCachePointerAtom::_s_section("__DATA", "__nl_symbol_ptr", 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 //
89 class StubHelperHelperAtom : public ld::Atom {
90 public:
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); }
98
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
105 buffer[1] = 0x00;
106 buffer[2] = 0x00;
107 buffer[3] = 0x00;
108 buffer[4] = 0x00;
109 buffer[5] = 0xFF; // jmp *_fast_lazy_bind
110 buffer[6] = 0x25;
111 buffer[7] = 0x00;
112 buffer[8] = 0x00;
113 buffer[9] = 0x00;
114 buffer[10] = 0x00;
115 buffer[11] = 0x90; // nop
116 }
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]; }
120
121 private:
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;
126 }
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;
131 }
132
133 mutable ld::Fixup _fixup1;
134 ld::Fixup _fixup2;
135
136 static ld::Section _s_section;
137 };
138
139 ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
140
141
142
143 class StubHelperAtom : public ld::Atom {
144 public:
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)),
150 _stubTo(stubTo),
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)) { }
154
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
161 buffer[1] = 0x00;
162 buffer[2] = 0x00;
163 buffer[3] = 0x00;
164 buffer[4] = 0x00;
165 buffer[5] = 0xE9; // jmp helperhelper
166 buffer[6] = 0x00;
167 buffer[7] = 0x00;
168 buffer[8] = 0x00;
169 buffer[9] = 0x00;
170 }
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]; }
174
175 private:
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;
180 }
181
182 const ld::Atom& _stubTo;
183 mutable ld::Fixup _fixup1;
184 ld::Fixup _fixup2;
185 ld::Fixup _fixup3;
186
187 static ld::Section _s_section;
188 };
189
190 ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
191
192
193 class ResolverHelperAtom : public ld::Atom {
194 public:
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)),
200 _stubTo(stubTo),
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) { }
204
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
214 buffer[ 4] = 0x00;
215 buffer[ 5] = 0x00;
216 buffer[ 6] = 0x00;
217 buffer[ 7] = 0x00;
218 buffer[ 8] = 0xA3; // movl %eax,foo$lazy_ptr
219 buffer[ 9] = 0x00;
220 buffer[10] = 0x00;
221 buffer[11] = 0x00;
222 buffer[12] = 0x00;
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
227 buffer[17] = 0x25;
228 buffer[18] = 0x00;
229 buffer[19] = 0x00;
230 buffer[20] = 0x00;
231 buffer[21] = 0x00;
232 }
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]; }
236
237 private:
238
239 const ld::Atom& _stubTo;
240 mutable ld::Fixup _fixup1;
241 ld::Fixup _fixup2;
242 ld::Fixup _fixup3;
243
244 static ld::Section _s_section;
245 };
246
247 ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
248
249
250
251 class LazyPointerAtom : public ld::Atom {
252 public:
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)),
258 _stubTo(stubTo),
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);
269 }
270
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]; }
279
280 private:
281 const ld::Atom& _stubTo;
282 StubHelperAtom _helper;
283 ResolverHelperAtom _resolverHelper;
284 mutable ld::Fixup _fixup1;
285 ld::Fixup _fixup2;
286
287 static ld::Section _s_section;
288 };
289
290 ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
291
292
293 class StubAtom : public ld::Atom {
294 public:
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)),
300 _stubTo(stubTo),
301 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport),
302 _fixup(2, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer) { pass.addAtom(*this); }
303
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
310 buffer[1] = 0x25;
311 buffer[2] = 0x00;
312 buffer[3] = 0x00;
313 buffer[4] = 0x00;
314 buffer[5] = 0x00;
315 }
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]; }
319
320 private:
321 const ld::Atom& _stubTo;
322 LazyPointerAtom _lazyPointer;
323 mutable ld::Fixup _fixup;
324
325 static ld::Section _s_section;
326 };
327
328 ld::Section StubAtom::_s_section("__TEXT", "__symbol_stub", ld::Section::typeStub);
329
330
331
332 } // namespace x86
333