]> git.saurik.com Git - apple/ld64.git/blame - src/ld/passes/stubs/stub_x86.hpp
ld64-409.12.tar.gz
[apple/ld64.git] / src / ld / passes / stubs / stub_x86.hpp
CommitLineData
a645023d
A
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
27namespace x86 {
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(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; }
a645023d
A
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
50private:
51 mutable ld::Fixup _fixup;
52
53 static ld::Section _s_section;
54};
55
56ld::Section FastBindingPointerAtom::_s_section("__DATA", "__nl_symbol_ptr", 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(2)) { pass.addAtom(*this); }
65
66 virtual const ld::File* file() const { return NULL; }
a645023d
A
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
73private:
74
75 static ld::Section _s_section;
76};
77
78ld::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//
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(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; }
a645023d
A
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
121private:
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
139ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
140
141
142
143class StubHelperAtom : public ld::Atom {
144public:
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(); }
a645023d
A
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
175private:
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
190ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
191
192
193class ResolverHelperAtom : public ld::Atom {
194public:
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(); }
a645023d
A
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
237private:
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
247ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
248
249
250
251class LazyPointerAtom : public ld::Atom {
252public:
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(); }
a645023d
A
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
280private:
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
290ld::Section LazyPointerAtom::_s_section("__DATA", "__la_symbol_ptr", ld::Section::typeLazyPointer);
291
292
293class StubAtom : public ld::Atom {
294public:
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(); }
a645023d
A
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
320private:
321 const ld::Atom& _stubTo;
322 LazyPointerAtom _lazyPointer;
323 mutable ld::Fixup _fixup;
324
325 static ld::Section _s_section;
326};
327
328ld::Section StubAtom::_s_section("__TEXT", "__symbol_stub", ld::Section::typeStub);
329
330
e456bf10
A
331class NonLazyPointerAtom : public ld::Atom {
332public:
333 NonLazyPointerAtom(ld::passes::stubs::Pass& pass, const ld::Atom& target,
334 bool weakImport)
335 : ld::Atom(_s_section, ld::Atom::definitionRegular,
336 ld::Atom::combineNever, ld::Atom::scopeLinkageUnit, ld::Atom::typeNonLazyPointer,
337 symbolTableNotIn, false, false, false, ld::Atom::Alignment(2)),
338 _target(target),
339 _fixup1(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &target) {
340 _fixup1.weakImport = weakImport;
341 pass.addAtom(*this);
342 }
343
344 virtual const ld::File* file() const { return _target.file(); }
345 virtual const char* name() const { return _target.name(); }
346 virtual uint64_t size() const { return 4; }
347 virtual uint64_t objectAddress() const { return 0; }
348 virtual void copyRawContent(uint8_t buffer[]) const { }
349 virtual void setScope(Scope) { }
350 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup1; }
351 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup1)[1]; }
352
353private:
354 const ld::Atom& _target;
355 ld::Fixup _fixup1;
356
357 static ld::Section _s_section;
358};
359
360ld::Section NonLazyPointerAtom::_s_section("__DATA", "__got", ld::Section::typeNonLazyPointer);
361
362
363class NonLazyStubAtom : public ld::Atom {
364public:
365 NonLazyStubAtom(ld::passes::stubs::Pass& pass, const ld::Atom& target,
366 bool weakImport)
367 : ld::Atom(_s_section, ld::Atom::definitionRegular, ld::Atom::combineNever,
368 ld::Atom::scopeLinkageUnit, ld::Atom::typeStub,
369 symbolTableNotIn, false, false, false, ld::Atom::Alignment(1)),
370 _target(target),
371 _nonlazyPointer(pass, target, weakImport),
372 _fixup1(8, ld::Fixup::k1of4, ld::Fixup::kindSetTargetAddress, &_nonlazyPointer),
373 _fixup2(8, ld::Fixup::k2of4, ld::Fixup::kindSubtractTargetAddress, this),
374 _fixup3(8, ld::Fixup::k3of4, ld::Fixup::kindSubtractAddend, 5),
375 _fixup4(8, ld::Fixup::k4of4, ld::Fixup::kindStoreLittleEndian32) { pass.addAtom(*this); }
376
377 virtual const ld::File* file() const { return _target.file(); }
378 virtual const char* name() const { return _target.name(); }
379 virtual uint64_t size() const { return 14; }
380 virtual uint64_t objectAddress() const { return 0; }
381 virtual void copyRawContent(uint8_t buffer[]) const {
382 buffer[0] = 0xE8; // call next instruction (picbase)
383 buffer[1] = 0x00;
384 buffer[2] = 0x00;
385 buffer[3] = 0x00;
386 buffer[4] = 0x00;
387 buffer[5] = 0x58; // pop eax
388 buffer[6] = 0x8D; // lea foo$nonlazy_pointer-picbase(eax),eax
389 buffer[7] = 0x80;
390 buffer[8] = 0x00;
391 buffer[9] = 0x00;
392 buffer[10] = 0x00;
393 buffer[11] = 0x00;
394 buffer[12] = 0xFF; // jmp *(eax)
395 buffer[13] = 0x20;
396 }
397 virtual void setScope(Scope) { }
398 virtual ld::Fixup::iterator fixupsBegin() const { return &_fixup1; }
399 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup4)[1]; }
400
401private:
402 const ld::Atom& _target;
403 NonLazyPointerAtom _nonlazyPointer;
404 mutable ld::Fixup _fixup1;
405 ld::Fixup _fixup2;
406 ld::Fixup _fixup3;
407 ld::Fixup _fixup4;
408
409 static ld::Section _s_section;
410};
411
412ld::Section NonLazyStubAtom::_s_section("__TEXT", "__symbol_stub", ld::Section::typeStub);
413
a645023d
A
414
415} // namespace x86
416