]> git.saurik.com Git - apple/dyld.git/blob - src/dyldStartup.s
dyld-132.13.tar.gz
[apple/dyld.git] / src / dyldStartup.s
1 /*
2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * C runtime startup for i386 and ppc interface to the dynamic linker.
25 * This is the same as the entry point in crt0.o with the addition of the
26 * address of the mach header passed as the an extra first argument.
27 *
28 * Kernel sets up stack frame to look like:
29 *
30 * | STRING AREA |
31 * +-------------+
32 * | 0 |
33 * +-------------+
34 * | apple[n] |
35 * +-------------+
36 * :
37 * +-------------+
38 * | apple[0] |
39 * +-------------+
40 * | 0 |
41 * +-------------+
42 * | env[n] |
43 * +-------------+
44 * :
45 * :
46 * +-------------+
47 * | env[0] |
48 * +-------------+
49 * | 0 |
50 * +-------------+
51 * | arg[argc-1] |
52 * +-------------+
53 * :
54 * :
55 * +-------------+
56 * | arg[0] |
57 * +-------------+
58 * | argc |
59 * +-------------+
60 * sp-> | mh | address of where the a.out's file offset 0 is in memory
61 * +-------------+
62 *
63 * Where arg[i] and env[i] point into the STRING AREA
64 */
65
66
67
68 // Hack to make _offset_to_dyld_all_image_infos work
69 // Without this local symbol, assembler will error out about in subtraction expression
70 // The real _dyld_all_image_infos (non-weak) _dyld_all_image_infos is defined in dyld_gdb.o
71 // and the linker with throw this one away and use the real one instead.
72 .section __DATA,__datacoal_nt,coalesced
73 .globl _dyld_all_image_infos
74 .weak_definition _dyld_all_image_infos
75 _dyld_all_image_infos: .long 0
76
77
78
79 .globl __dyld_start
80
81 #ifdef __i386__
82 .data
83 __dyld_start_static_picbase:
84 .long L__dyld_start_picbase
85
86
87 .text
88 .align 2
89 # stable entry points into dyld
90 .globl _stub_binding_helper
91 _stub_binding_helper:
92 jmp _stub_binding_helper_interface
93 nop
94 nop
95 nop
96 .globl _dyld_func_lookup
97 _dyld_func_lookup:
98 jmp __Z18lookupDyldFunctionPKcPm
99 nop
100 nop
101 nop
102 _offset_to_dyld_all_image_infos:
103 .long _dyld_all_image_infos - . + 0x1010
104 .long 0
105 # space for future stable entry points
106 .space 16
107
108
109
110 .text
111 .align 4, 0x90
112 .globl __dyld_start
113 __dyld_start:
114 pushl $0 # push a zero for debugger end of frames marker
115 movl %esp,%ebp # pointer to base of kernel frame
116 andl $-16,%esp # force SSE alignment
117
118 # call dyldbootstrap::start(app_mh, argc, argv, slide)
119 call L__dyld_start_picbase
120 L__dyld_start_picbase:
121 popl %ebx # set %ebx to runtime value of picbase
122 movl __dyld_start_static_picbase-L__dyld_start_picbase(%ebx), %eax
123 subl %eax, %ebx # slide = L__dyld_start_picbase - [__dyld_start_static_picbase]
124 pushl %ebx # param4 = slide
125 lea 12(%ebp),%ebx
126 pushl %ebx # param3 = argv
127 movl 8(%ebp),%ebx
128 pushl %ebx # param2 = argc
129 movl 4(%ebp),%ebx
130 pushl %ebx # param1 = mh
131 call __ZN13dyldbootstrap5startEPK12macho_headeriPPKcl
132
133 # clean up stack and jump to result
134 movl %ebp,%esp # restore the unaligned stack pointer
135 addl $8,%esp # remove the mh argument, and debugger end
136 # frame marker
137 movl $0,%ebp # restore ebp back to zero
138 jmp *%eax # jump to the entry point
139
140
141 .globl dyld_stub_binding_helper
142 dyld_stub_binding_helper:
143 hlt
144 L_end:
145 #endif /* __i386__ */
146
147
148
149 #if __x86_64__
150 .data
151 .align 3
152 __dyld_start_static:
153 .quad __dyld_start
154
155 # stable entry points into dyld
156 .text
157 .align 2
158 .globl _stub_binding_helper
159 _stub_binding_helper:
160 jmp _stub_binding_helper_interface
161 nop
162 nop
163 nop
164 .globl _dyld_func_lookup
165 _dyld_func_lookup:
166 jmp __Z18lookupDyldFunctionPKcPm
167 nop
168 nop
169 nop
170 _offset_to_dyld_all_image_infos:
171 .long _dyld_all_image_infos - . + 0x1010
172 .long 0
173 # space for future stable entry points
174 .space 16
175
176
177 .text
178 .align 2,0x90
179 .globl __dyld_start
180 __dyld_start:
181 pushq $0 # push a zero for debugger end of frames marker
182 movq %rsp,%rbp # pointer to base of kernel frame
183 andq $-16,%rsp # force SSE alignment
184
185 # call dyldbootstrap::start(app_mh, argc, argv, slide)
186 movq 8(%rbp),%rdi # param1 = mh into %rdi
187 movl 16(%rbp),%esi # param2 = argc into %esi
188 leaq 24(%rbp),%rdx # param3 = &argv[0] into %rdx
189 movq __dyld_start_static(%rip), %r8
190 leaq __dyld_start(%rip), %rcx
191 subq %r8, %rcx # param4 = slide into %rcx
192 call __ZN13dyldbootstrap5startEPK12macho_headeriPPKcl
193
194 # clean up stack and jump to result
195 movq %rbp,%rsp # restore the unaligned stack pointer
196 addq $16,%rsp # remove the mh argument, and debugger end frame marker
197 movq $0,%rbp # restore ebp back to zero
198 jmp *%rax # jump to the entry point
199
200 #endif /* __x86_64__ */
201
202
203 #if __ppc__ || __ppc64__
204 #include <architecture/ppc/mode_independent_asm.h>
205
206 .data
207 .align 2
208 __dyld_start_static_picbase:
209 .g_long L__dyld_start_picbase
210
211 #if __ppc__
212 .set L_mh_offset,0
213 .set L_argc_offset,4
214 .set L_argv_offset,8
215 #else
216 .set L_mh_offset,0
217 .set L_argc_offset,8 ; stack is 8-byte aligned and there is a 4-byte hole between argc and argv
218 .set L_argv_offset,16
219 #endif
220
221 .text
222 .align 2
223 ; stable entry points into dyld
224 .globl _stub_binding_helper
225 _stub_binding_helper:
226 b _stub_binding_helper_interface
227 nop
228 .globl _dyld_func_lookup
229 _dyld_func_lookup:
230 b __Z18lookupDyldFunctionPKcPm
231 nop
232 _offset_to_dyld_all_image_infos:
233 .long _dyld_all_image_infos - . + 0x1010
234 .long 0
235 # space for future stable entry points
236 .space 16
237
238
239 .text
240 .align 2
241 __dyld_start:
242 mr r26,r1 ; save original stack pointer into r26
243 subi r1,r1,GPR_BYTES ; make space for linkage
244 clrrgi r1,r1,5 ; align to 32 bytes
245 addi r0,0,0 ; load 0 into r0
246 stg r0,0(r1) ; terminate initial stack frame
247 stgu r1,-SF_MINSIZE(r1); allocate minimal stack frame
248
249 ; call dyldbootstrap::start(app_mh, argc, argv, slide)
250 lg r3,L_mh_offset(r26) ; r3 = mach_header
251 lwz r4,L_argc_offset(r26) ; r4 = argc (int == 4 bytes)
252 addi r5,r26,L_argv_offset ; r5 = argv
253 bcl 20,31,L__dyld_start_picbase
254 L__dyld_start_picbase:
255 mflr r31 ; put address of L__dyld_start_picbase in r31
256 addis r6,r31,ha16(__dyld_start_static_picbase-L__dyld_start_picbase)
257 lg r6,lo16(__dyld_start_static_picbase-L__dyld_start_picbase)(r6)
258 subf r6,r6,r31 ; r6 = slide
259 bl __ZN13dyldbootstrap5startEPK12macho_headeriPPKcl
260
261 ; clean up stack and jump to result
262 mtctr r3 ; Put entry point in count register
263 mr r12,r3 ; also put in r12 for ABI convention.
264 addi r1,r26,GPR_BYTES; Restore the stack pointer and remove the
265 ; mach_header argument.
266 bctr ; jump to the program's entry point
267
268 .globl dyld_stub_binding_helper
269 dyld_stub_binding_helper:
270 trap
271 L_end:
272 #endif /* __ppc__ */
273
274 #if __arm__
275 .data
276 .align 2
277 __dyld_start_static_picbase:
278 .long L__dyld_start_picbase
279
280 .text
281 .align 2
282 .globl _stub_binding_helper
283 _stub_binding_helper:
284 b _stub_binding_helper_interface
285 nop
286
287 .globl _dyld_func_lookup
288 _dyld_func_lookup:
289 b _branch_to_lookupDyldFunction
290 nop
291
292 _offset_to_dyld_all_image_infos:
293 .long _dyld_all_image_infos - . + 0x1010
294 .long 0
295 # space for future stable entry points
296 .space 16
297
298
299 .text
300 .align 2
301 __dyld_start:
302 // call dyldbootstrap::start(app_mh, argc, argv, slide)
303
304 ldr r3, L__dyld_start_picbase_ptr
305 L__dyld_start_picbase:
306 sub r0, pc, #8 // load actual PC
307 ldr r3, [r0, r3] // load expected PC
308 sub r3, r0, r3 // r3 = slide
309
310 ldr r0, [sp] // r0 = mach_header
311 ldr r1, [sp, #4] // r1 = argc
312 add r2, sp, #8 // r2 = argv
313
314 mov r8, sp // save stack pointer
315 bic sp, sp, #7 // force 8-byte alignment
316
317 bl __ZN13dyldbootstrap5startEPK12macho_headeriPPKcl
318
319 // clean up stack and jump to result
320 add sp, r8, #4 // remove the mach_header argument.
321 bx r0 // jump to the program's entry point
322
323 .align 2
324 L__dyld_start_picbase_ptr:
325 .long __dyld_start_static_picbase-L__dyld_start_picbase
326
327
328 .text
329 .align 2
330 _branch_to_lookupDyldFunction:
331 // arm has no "bx label" instruction, so need this island in case lookupDyldFunction() is in thumb
332 ldr ip, L2
333 L1: ldr pc, [pc, ip]
334 L2: .long _lookupDyldFunction_ptr-8-L1
335
336 .data
337 .align 2
338 _lookupDyldFunction_ptr:
339 .long __Z18lookupDyldFunctionPKcPm
340
341
342 .text
343 .globl dyld_stub_binding_helper
344 dyld_stub_binding_helper:
345 trap
346
347 L_end:
348 #endif /* __arm__ */
349
350 /*
351 * dyld calls this function to terminate a process.
352 * It has a label so that CrashReporter can distinguish this
353 * termination from a random crash. rdar://problem/4764143
354 */
355 .text
356 .align 2
357 .globl _dyld_fatal_error
358 _dyld_fatal_error:
359 #if __ppc__ || __ppc64__ || __arm__
360 trap
361 nop
362 #elif __x86_64__ || __i386__
363 int3
364 nop
365 #else
366 #error unknown architecture
367 #endif
368
369
370
371
372