2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
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.
28 * Kernel sets up stack frame to look like:
60 * sp-> | mh | address of where the a.out's file offset 0 is in memory
63 * Where arg[i] and env[i] point into the STRING AREA
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
83 __dyld_start_static_picbase:
84 .long L__dyld_start_picbase
85 Lmh: .long ___dso_handle
89 # stable entry points into dyld
90 .globl _stub_binding_helper
92 jmp _stub_binding_helper_interface
96 .globl _dyld_func_lookup
98 jmp __Z18lookupDyldFunctionPKcPm
102 _offset_to_dyld_all_image_infos:
103 .long _dyld_all_image_infos - . + 0x1010
105 # space for future stable entry points
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
118 # call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh)
120 call L__dyld_start_picbase
121 L__dyld_start_picbase:
122 popl %ebx # set %ebx to runtime value of picbase
123 movl Lmh-L__dyld_start_picbase(%ebx), %ecx # ecx = prefered load address
124 movl __dyld_start_static_picbase-L__dyld_start_picbase(%ebx), %eax
125 subl %eax, %ebx # ebx = slide = L__dyld_start_picbase - [__dyld_start_static_picbase]
126 addl %ebx, %ecx # ecx = actual load address
127 pushl %ecx # param5 = actual load address
128 pushl %ebx # param4 = slide
130 pushl %ebx # param3 = argv
132 pushl %ebx # param2 = argc
134 pushl %ebx # param1 = mh
135 call __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_
137 # clean up stack and jump to result
138 movl %ebp,%esp # restore the unaligned stack pointer
139 addl $8,%esp # remove the mh argument, and debugger end
141 movl $0,%ebp # restore ebp back to zero
142 jmp *%eax # jump to the entry point
145 .globl dyld_stub_binding_helper
146 dyld_stub_binding_helper:
149 #endif /* __i386__ */
159 # stable entry points into dyld
162 .globl _stub_binding_helper
163 _stub_binding_helper:
164 jmp _stub_binding_helper_interface
168 .globl _dyld_func_lookup
170 jmp __Z18lookupDyldFunctionPKcPm
174 _offset_to_dyld_all_image_infos:
175 .long _dyld_all_image_infos - . + 0x1010
177 # space for future stable entry points
185 pushq $0 # push a zero for debugger end of frames marker
186 movq %rsp,%rbp # pointer to base of kernel frame
187 andq $-16,%rsp # force SSE alignment
189 # call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh)
190 movq 8(%rbp),%rdi # param1 = mh into %rdi
191 movl 16(%rbp),%esi # param2 = argc into %esi
192 leaq 24(%rbp),%rdx # param3 = &argv[0] into %rdx
193 movq __dyld_start_static(%rip), %r8
194 leaq __dyld_start(%rip), %rcx
195 subq %r8, %rcx # param4 = slide into %rcx
196 leaq ___dso_handle(%rip),%r8 # param5 = dyldsMachHeader
197 call __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_
199 # clean up stack and jump to result
200 movq %rbp,%rsp # restore the unaligned stack pointer
201 addq $16,%rsp # remove the mh argument, and debugger end frame marker
202 movq $0,%rbp # restore ebp back to zero
203 jmp *%rax # jump to the entry point
205 #endif /* __x86_64__ */
208 #if __ppc__ || __ppc64__
209 #include <architecture/ppc/mode_independent_asm.h>
213 __dyld_start_static_picbase:
214 .g_long L__dyld_start_picbase
215 Lmh: .g_long ___dso_handle
223 .set L_argc_offset,8 ; stack is 8-byte aligned and there is a 4-byte hole between argc and argv
224 .set L_argv_offset,16
229 ; stable entry points into dyld
230 .globl _stub_binding_helper
231 _stub_binding_helper:
232 b _stub_binding_helper_interface
234 .globl _dyld_func_lookup
236 b __Z18lookupDyldFunctionPKcPm
238 _offset_to_dyld_all_image_infos:
239 .long _dyld_all_image_infos - . + 0x1010
241 # space for future stable entry points
248 mr r26,r1 ; save original stack pointer into r26
249 subi r1,r1,GPR_BYTES ; make space for linkage
250 clrrgi r1,r1,5 ; align to 32 bytes
251 addi r0,0,0 ; load 0 into r0
252 stg r0,0(r1) ; terminate initial stack frame
253 stgu r1,-SF_MINSIZE(r1); allocate minimal stack frame
255 # call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh)
256 lg r3,L_mh_offset(r26) ; r3 = mach_header
257 lwz r4,L_argc_offset(r26) ; r4 = argc (int == 4 bytes)
258 addi r5,r26,L_argv_offset ; r5 = argv
259 bcl 20,31,L__dyld_start_picbase
260 L__dyld_start_picbase:
261 mflr r31 ; put address of L__dyld_start_picbase in r31
262 addis r6,r31,ha16(__dyld_start_static_picbase-L__dyld_start_picbase)
263 lg r6,lo16(__dyld_start_static_picbase-L__dyld_start_picbase)(r6)
264 subf r6,r6,r31 ; r6 = slide
265 addis r7,r31,ha16(Lmh-L__dyld_start_picbase)
266 lg r7,lo16(Lmh-L__dyld_start_picbase)(r7)
267 add r7,r6,r7 ; r7 = dyld_mh
268 bl __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_
270 ; clean up stack and jump to result
271 mtctr r3 ; Put entry point in count register
272 mr r12,r3 ; also put in r12 for ABI convention.
273 addi r1,r26,GPR_BYTES; Restore the stack pointer and remove the
274 ; mach_header argument.
275 bctr ; jump to the program's entry point
277 .globl dyld_stub_binding_helper
278 dyld_stub_binding_helper:
286 __dyld_start_static_picbase:
287 .long L__dyld_start_picbase
291 .globl _stub_binding_helper
292 _stub_binding_helper:
293 b _stub_binding_helper_interface
296 .globl _dyld_func_lookup
298 b _branch_to_lookupDyldFunction
301 _offset_to_dyld_all_image_infos:
302 .long _dyld_all_image_infos - . + 0x1010
304 # space for future stable entry points
308 // Hack to make ___dso_handle work
309 // Without this local symbol, assembler will error out about in subtraction expression
310 // The real ___dso_handle (non-weak) sythesized by the linker
311 // Since this one is weak, the linker will throw this one away and use the real one instead.
314 .weak_definition ___dso_handle
315 ___dso_handle: .long 0
320 mov r8, sp // save stack pointer
321 sub sp, #8 // make room for outgoing dyld_mh parameter
322 bic sp, sp, #7 // force 8-byte alignment
324 // call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh)
326 ldr r3, L__dyld_start_picbase_ptr
327 L__dyld_start_picbase:
328 sub r0, pc, #8 // load actual PC
329 ldr r3, [r0, r3] // load expected PC
330 sub r3, r0, r3 // r3 = slide
332 ldr r0, [r8] // r0 = mach_header
333 ldr r1, [r8, #4] // r1 = argc
334 add r2, r8, #8 // r2 = argv
337 L3: add r4, r4, pc // r4 = dyld_mh
340 bl __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_
342 // clean up stack and jump to result
343 add sp, r8, #4 // remove the mach_header argument.
344 bx r0 // jump to the program's entry point
348 L__dyld_start_picbase_ptr:
349 .long __dyld_start_static_picbase-L__dyld_start_picbase
350 Lmh: .long ___dso_handle-L3-8
354 _branch_to_lookupDyldFunction:
355 // arm has no "bx label" instruction, so need this island in case lookupDyldFunction() is in thumb
358 L2: .long _lookupDyldFunction_ptr-8-L1
362 _lookupDyldFunction_ptr:
363 .long __Z18lookupDyldFunctionPKcPm
367 .globl dyld_stub_binding_helper
368 dyld_stub_binding_helper:
375 * dyld calls this function to terminate a process.
376 * It has a label so that CrashReporter can distinguish this
377 * termination from a random crash. rdar://problem/4764143
381 .globl _dyld_fatal_error
383 #if __ppc__ || __ppc64__ || __arm__
386 #elif __x86_64__ || __i386__
390 #error unknown architecture
394 // work around for: <rdar://problem/6530727> gdb-1109: notifier in dyld does not work if it is in thumb
397 .globl _gdb_image_notifier
398 .private_extern _gdb_image_notifier