forked from skaht/Csu-85
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyld_glue.s
302 lines (271 loc) · 7.3 KB
/
dyld_glue.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* Copyright (c) 2006-2008 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#if __ppc__ && __PIC__
//
// Force stub section next to __text section to minimize chance that
// a bl to a stub will be out of range.
//
.text
.section __TEXT,__picsymbolstub1,symbol_stubs,pure_instructions,32
#endif
////////////////////////////////////////////////////////////////////
//
// The dyld_stub_binding_helper adds the mach_header parameter
// and then jumps into dyld via a pointer in __dyld section
//
////////////////////////////////////////////////////////////////////
.text
.private_extern dyld_stub_binding_helper
.align 2
dyld_stub_binding_helper:
#if __ppc__
// for ppc the mach_header parameter is place in r12
// and the lazy_pointer is already in r11
#if __PIC__
mflr r0
bcl 20,31,L1
L1: mflr r12
mtlr r0
mr r0,r12
addis r12,r12,ha16(Ldyld_content_lazy_binder-L1)
lwz r12,lo16(Ldyld_content_lazy_binder-L1)(r12)
mtctr r12
mr r12,r0
addis r12,r12,ha16(dyld__mach_header-L1)
lwz r12,lo16(dyld__mach_header-L1)(r12)
bctr
#else
lis r12,ha16(Ldyld_content_lazy_binder)
lwz r12,lo16(Ldyld_content_lazy_binder)(r12)
mtctr r12
lis r12,ha16(___dso_handle)
la r12,lo16(___dso_handle)(r12)
bctr
#endif
#elif __ppc64__
// for ppc the mach_header parameter is place in r12
// and the lazy_pointer is already in r11
// always use PIC code so we can have 4GB zero page
mflr r0
bcl 20,31,L1
L1: mflr r12
mtlr r0
mr r0,r12
addis r12,r12,ha16(Ldyld_content_lazy_binder-L1)
ld r12,lo16(Ldyld_content_lazy_binder-L1)(r12)
mtctr r12
mr r12,r0
addis r12,r12,ha16(dyld__mach_header-L1)
ld r12,lo16(dyld__mach_header-L1)(r12)
bctr
#elif __i386__
// for i386 the mach_header parameter is pushed on the stack
// and the lazy_pointer is already on the stack
#if __PIC__
subl $8,%esp
pushl %eax
pushl %ecx
call L1
L1: popl %eax
movl dyld__mach_header-L1(%eax),%ecx
movl %ecx,12(%esp)
movl Ldyld_content_lazy_binder-L1(%eax),%ecx
movl %ecx,8(%esp)
popl %ecx
popl %eax
ret // jumps into dyld with lp and mh on the stack
#else
pushl $___dso_handle
jmpl *Ldyld_content_lazy_binder
#endif
#elif __x86_64__
// for x86_64 the mach_header parameter is pushed on the stack
// and the lazy_pointer was in r11 and is pushed on the stack
pushq %r11
leaq ___dso_handle(%rip), %r11
pushq %r11
jmp *Ldyld_content_lazy_binder(%rip)
#elif __arm__
str ip, [sp, #-4]! // push address of lazy pointer
ldr ip, Ldyld__mach_header_pointer
#if __PIC__
Ldyld__mach_header_pointer_base:
ldr ip, [pc, ip]
#endif
str ip, [sp, #-4]! // push address of mach header
ldr ip, Ldyld_lazy_symbol_binding_entry_point
#if __PIC__
Ldyld_lazy_symbol_binding_entry_point_base:
ldr pc, [pc, ip] // jump to dyld_lazy_symbol_binding_entry_point
#else
ldr pc, [ip]
#endif
Ldyld__mach_header_pointer:
#if __PIC__
.long dyld__mach_header - (Ldyld__mach_header_pointer_base + 8)
#else
.long ___dso_handle
#endif
Ldyld_lazy_symbol_binding_entry_point:
#if __PIC__
.long Ldyld_content_lazy_binder - (Ldyld_lazy_symbol_binding_entry_point_base + 8)
#else
.long Ldyld_content_lazy_binder
#endif
#endif
////////////////////////////////////////////////////////////////////
//
// cfm_stub_binding_helper
//
// only needed by ppc dylibs which support CFM clients
//
////////////////////////////////////////////////////////////////////
#if __ppc__ && CFM_GLUE
.text
.align 2
.private_extern cfm_stub_binding_helper
cfm_stub_binding_helper:
mr r11, r12 ; The TVector address is the binding pointer address.
b dyld_stub_binding_helper ; Let the normal code handle the rest.
#endif
////////////////////////////////////////////////////////////////////
//
// __dyld_func_lookup(const char*, void**)
//
// jumps into dyld via a pointer in __dyld section
//
////////////////////////////////////////////////////////////////////
.text
.private_extern __dyld_func_lookup
.align 2
__dyld_func_lookup:
#if __ppc__
#if __PIC__
mflr r0
bcl 20,31,L2
L2: mflr r11
mtlr r0
addis r11,r11,ha16(Ldyld_content_func_lookup-L2)
lwz r11,lo16(Ldyld_content_func_lookup-L2)(r11)
mtctr r11
bctr
#else
lis r11,ha16(Ldyld_content_func_lookup)
lwz r11,lo16(Ldyld_content_func_lookup)(r11)
mtctr r11
bctr
#endif
#elif __ppc64__
mflr r0
bcl 20,31,L2
L2: mflr r11
mtlr r0
addis r11,r11,ha16(Ldyld_content_func_lookup-L2)
ld r11,lo16(Ldyld_content_func_lookup-L2)(r11)
mtctr r11
bctr
#elif __i386__
#if __PIC__
call L2
L2: popl %eax
movl Ldyld_content_func_lookup-L2(%eax),%eax
jmpl *%eax
#else
jmpl *Ldyld_content_func_lookup
#endif
#elif __x86_64__
jmp *Ldyld_content_func_lookup(%rip)
#elif __arm__
ldr ip, L__dyld_func_lookup_pointer
#if __PIC__
L__dyld_func_lookup_pointer_base:
ldr pc, [pc, ip]
#else
ldr pc, [ip]
#endif
L__dyld_func_lookup_pointer:
#if __PIC__
.long Ldyld_content_func_lookup - (L__dyld_func_lookup_pointer_base + 8)
#else
.long Ldyld_content_func_lookup
#endif
#endif
#if __LP64__
#define align_pointer align 3
#define pointer quad
#else
#define align_pointer align 2
#define pointer long
#endif
#if __ppc64__ || ((__i386__ || __ppc__ || __arm__) && __PIC__)
////////////////////////////////////////////////////////////////////
//
// dyld__mach_header
// contains a pointer to the mach_header for this linkage unit
// only needed for some code models
//
////////////////////////////////////////////////////////////////////
.data
.align_pointer
dyld__mach_header:
.pointer ___dso_handle
#endif // __x86_64__
////////////////////////////////////////////////////////////////////
//
// __dyld section content
//
// 0: pointer to lazy symbol binder in dyld
// 1: pointer to dyld_func_lookup implementation in dyld
//
////////////////////////////////////////////////////////////////////
#if __ppc__
Ldyld_base_addr = 0x8fe00000
#elif __ppc64__
Ldyld_base_addr = 0x00007fff5fc00000
#elif __i386__
Ldyld_base_addr = 0x8fe00000
#elif __x86_64__
Ldyld_base_addr = 0x00007fff5fc00000
#elif __arm__
Ldyld_base_addr = 0x2fe00000
#elif __arm64__
#else
#error unknown architecture
#endif
#if !__arm64__
.dyld
.align_pointer
Ldyld_content_lazy_binder:
.pointer Ldyld_base_addr + 0x1000
Ldyld_content_func_lookup:
.pointer Ldyld_base_addr + 0x1008
#if CRT && !OLD_LIBSYSTEM_SUPPORT
.pointer ___dso_handle
.pointer _NXArgc
.pointer _NXArgv
.pointer _environ
.pointer ___progname
#endif
#endif
// This code has be written to allow dead code stripping
.subsections_via_symbols