]> git.saurik.com Git - apple/ld64.git/blob - src/ld/passes/stubs/stub_arm.hpp
ld64-123.2.tar.gz
[apple/ld64.git] / src / ld / passes / stubs / stub_arm.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 // already in ld::passes::stubs namespace
26 namespace arm {
27
28
29 class FastBindingPointerAtom : public ld::Atom {
30 public:
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); }
37
38 virtual const ld::File* file() const { return NULL; }
39 virtual bool translationUnitSource(const char** dir, const char** nm) const
40 { return false; }
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]; }
48
49 private:
50 ld::Fixup _fixup;
51
52 static ld::Section _s_section;
53 };
54
55 ld::Section FastBindingPointerAtom::_s_section("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer);
56
57
58 class ImageCachePointerAtom : public ld::Atom {
59 public:
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); }
64
65 virtual const ld::File* file() const { return NULL; }
66 virtual bool translationUnitSource(const char** dir, const char** nm) const
67 { return false; }
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) { }
73
74 private:
75
76 static ld::Section _s_section;
77 };
78
79 ld::Section ImageCachePointerAtom::_s_section("__DATA", "__nl_symbol_ptr", ld::Section::typeNonLazyPointer);
80
81
82
83 class StubHelperHelperAtom : public ld::Atom {
84 public:
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); }
98
99 virtual ld::File* file() const { return NULL; }
100 virtual bool translationUnitSource(const char** dir, const char** nm) const
101 { return false; }
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)
118 }
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]; }
122
123 private:
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;
128 }
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;
133 }
134
135 ld::Fixup _fixup1;
136 ld::Fixup _fixup2;
137 ld::Fixup _fixup3;
138 ld::Fixup _fixup4;
139 ld::Fixup _fixup5;
140 ld::Fixup _fixup6;
141 ld::Fixup _fixup7;
142 ld::Fixup _fixup8;
143
144 static ld::Section _s_section;
145 };
146
147 ld::Section StubHelperHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
148
149
150 class StubHelperAtom : public ld::Atom {
151 public:
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)),
157 _stubTo(stubTo),
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) { }
161
162 virtual const ld::File* file() const { return _stubTo.file(); }
163 virtual bool translationUnitSource(const char** dir, const char** nm) const
164 { return false; }
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
172 }
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]; }
176
177 private:
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;
182 }
183 const ld::Atom& _stubTo;
184 ld::Fixup _fixup1;
185 ld::Fixup _fixup2;
186 ld::Fixup _fixup3;
187
188 static ld::Section _s_section;
189 };
190
191 ld::Section StubHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
192
193
194 class ResolverHelperAtom : public ld::Atom {
195 public:
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)),
201 _stubTo(stubTo),
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) { }
207
208 virtual const ld::File* file() const { return _stubTo.file(); }
209 virtual bool translationUnitSource(const char** dir, const char** nm) const
210 { return false; }
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
224 }
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]; }
228
229 private:
230 const ld::Atom& _stubTo;
231 ld::Fixup _fixup1;
232 ld::Fixup _fixup2;
233 ld::Fixup _fixup3;
234 ld::Fixup _fixup4;
235 ld::Fixup _fixup5;
236
237 static ld::Section _s_section;
238 };
239
240 ld::Section ResolverHelperAtom::_s_section("__TEXT", "__stub_helper", ld::Section::typeStubHelper);
241
242
243 class LazyPointerAtom : public ld::Atom {
244 public:
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)),
251 _stubTo(stubTo),
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);
262 }
263
264 virtual const ld::File* file() const { return _stubTo.file(); }
265 virtual bool translationUnitSource(const char** dir, const char** nm) const
266 { return false; }
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]; }
274
275 private:
276 const ld::Atom& _stubTo;
277 StubHelperAtom _helper;
278 ResolverHelperAtom _resolverHelper;
279 ld::Fixup _fixup1;
280 ld::Fixup _fixup2;
281
282 static ld::Section _s_section;
283 static ld::Section _s_sectionClose;
284 };
285
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);
288
289
290
291 class StubPICAtom : public ld::Atom {
292 public:
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)),
298 _stubTo(stubTo),
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); }
305
306 virtual const ld::File* file() const { return _stubTo.file(); }
307 virtual bool translationUnitSource(const char** dir, const char** nm) const
308 { return false; }
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)
317 }
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]; }
321
322 private:
323 const ld::Atom& _stubTo;
324 LazyPointerAtom _lazyPointer;
325 ld::Fixup _fixup1;
326 ld::Fixup _fixup2;
327 ld::Fixup _fixup3;
328 ld::Fixup _fixup4;
329
330 static ld::Section _s_section;
331 };
332
333 ld::Section StubPICAtom::_s_section("__TEXT", "__picsymbolstub4", ld::Section::typeStub);
334
335
336
337 class StubNoPICAtom : public ld::Atom {
338 public:
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)),
344 _stubTo(stubTo),
345 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, false),
346 _fixup(8, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressLittleEndian32, &_lazyPointer)
347 { pass.addAtom(*this); }
348
349 virtual const ld::File* file() const { return _stubTo.file(); }
350 virtual bool translationUnitSource(const char** dir, const char** nm) const
351 { return false; }
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
359 }
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]; }
363
364 private:
365 const ld::Atom& _stubTo;
366 LazyPointerAtom _lazyPointer;
367 ld::Fixup _fixup;
368
369 static ld::Section _s_section;
370 };
371
372 ld::Section StubNoPICAtom::_s_section("__TEXT", "__symbol_stub4", ld::Section::typeStub);
373
374
375
376
377 class StubCloseAtom : public ld::Atom {
378 public:
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)),
384 _stubTo(stubTo),
385 _lazyPointer(pass, stubTo, stubToGlobalWeakDef, stubToResolver, weakImport, true),
386 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindStoreTargetAddressARMLoad12, &_lazyPointer)
387 { pass.addAtom(*this); }
388
389 virtual const ld::File* file() const { return _stubTo.file(); }
390 virtual bool translationUnitSource(const char** dir, const char** nm) const
391 { return false; }
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]
397 }
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]; }
401
402 private:
403 const ld::Atom& _stubTo;
404 LazyPointerAtom _lazyPointer;
405 ld::Fixup _fixup;
406
407 static ld::Section _s_section;
408 };
409
410 ld::Section StubCloseAtom::_s_section("__TEXT", "__symbolstub1", ld::Section::typeStubClose);
411
412
413
414
415 } // namespace arm
416