2 * Copyright (c) 1999-2011 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 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
67 #include <TargetConditionals.h>
73 #if !TARGET_IPHONE_SIMULATOR
78 popl %edx # edx = mh of app
79 pushl $0 # push a zero for debugger end of frames marker
80 movl %esp,%ebp # pointer to base of kernel frame
81 andl $-16,%esp # force SSE alignment
82 subl $32,%esp # room for locals and outgoing parameters
84 call L__dyld_start_picbase
85 L__dyld_start_picbase:
86 popl %ebx # set %ebx to runtime value of picbase
88 movl Lmh-L__dyld_start_picbase(%ebx), %ecx # ecx = prefered load address
89 movl __dyld_start_static_picbase-L__dyld_start_picbase(%ebx), %eax
90 subl %eax, %ebx # ebx = slide = L__dyld_start_picbase - [__dyld_start_static_picbase]
91 addl %ebx, %ecx # ecx = actual load address
92 # call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh, &startGlue)
93 movl %edx,(%esp) # param1 = app_mh
95 movl %eax,4(%esp) # param2 = argc
97 movl %eax,8(%esp) # param3 = argv
98 movl %ebx,12(%esp) # param4 = slide
99 movl %ecx,16(%esp) # param5 = actual load address
101 movl %eax,20(%esp) # param6 = &startGlue
102 call __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
107 # clean up stack and jump to "start" in main executable
108 movl %ebp,%esp # restore the unaligned stack pointer
109 addl $4,%esp # remove debugger end frame marker
110 movl $0,%ebp # restore ebp back to zero
111 jmp *%eax # jump to the entry point
113 # LC_MAIN case, set up stack for call to main()
114 Lnew: movl 4(%ebp),%ebx
115 movl %ebx,(%esp) # main param1 = argc
117 movl %ecx,4(%esp) # main param2 = argv
118 leal 0x4(%ecx,%ebx,4),%ebx
119 movl %ebx,8(%esp) # main param3 = env
120 Lapple: movl (%ebx),%ecx # look for NULL ending env[] array
123 jne Lapple # once found, next pointer is "apple" parameter now in %ebx
124 movl %ebx,12(%esp) # main param4 = apple
125 pushl %edx # simulate return address into _start in libdyld
126 jmp *%eax # jump to main(argc,argv,env,apple) with return address set to _start
129 #if !TARGET_IPHONE_SIMULATOR
131 __dyld_start_static_picbase:
132 .long L__dyld_start_picbase
133 Lmh: .long ___dso_handle
137 #endif /* __i386__ */
142 #if !TARGET_IPHONE_SIMULATOR
150 #if !TARGET_IPHONE_SIMULATOR
155 popq %rdi # param1 = mh of app
156 pushq $0 # push a zero for debugger end of frames marker
157 movq %rsp,%rbp # pointer to base of kernel frame
158 andq $-16,%rsp # force SSE alignment
159 subq $16,%rsp # room for local variables
161 # call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh, &startGlue)
162 movl 8(%rbp),%esi # param2 = argc into %esi
163 leaq 16(%rbp),%rdx # param3 = &argv[0] into %rdx
164 movq __dyld_start_static(%rip), %r8
165 leaq __dyld_start(%rip), %rcx
166 subq %r8, %rcx # param4 = slide into %rcx
167 leaq ___dso_handle(%rip),%r8 # param5 = dyldsMachHeader
169 call __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
174 # clean up stack and jump to "start" in main executable
175 movq %rbp,%rsp # restore the unaligned stack pointer
176 addq $8,%rsp # remove the mh argument, and debugger end frame marker
177 movq $0,%rbp # restore ebp back to zero
178 jmp *%rax # jump to the entry point
180 # LC_MAIN case, set up stack for call to main()
181 Lnew: addq $16,%rsp # remove local variables
182 pushq %rdi # simulate return address into _start in libdyld
183 movq 8(%rbp),%rdi # main param1 = argc into %rdi
184 leaq 16(%rbp),%rsi # main param2 = &argv[0] into %rsi
185 leaq 0x8(%rsi,%rdi,8),%rdx # main param3 = &env[0] into %rdx
187 Lapple: movq (%rcx),%r8
189 testq %r8,%r8 # look for NULL ending env[] array
190 jne Lapple # main param4 = apple into %rcx
191 jmp *%rax # jump to main(argc,argv,env,apple) with return address set to _start
193 #endif /* TARGET_IPHONE_SIMULATOR */
194 #endif /* __x86_64__ */
202 __dyld_start_static_picbase:
203 .long L__dyld_start_picbase
206 // Hack to make ___dso_handle work
207 // Without this local symbol, assembler will error out about in subtraction expression
208 // The real ___dso_handle (non-weak) sythesized by the linker
209 // Since this one is weak, the linker will throw this one away and use the real one instead.
212 .weak_definition ___dso_handle
213 ___dso_handle: .long 0
218 mov r8, sp // save stack pointer
219 sub sp, #16 // make room for outgoing parameters
220 bic sp, sp, #15 // force 16-byte alignment
222 // call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh, &startGlue)
224 ldr r3, L__dyld_start_picbase_ptr
225 L__dyld_start_picbase:
226 sub r0, pc, #8 // load actual PC
227 ldr r3, [r0, r3] // load expected PC
228 sub r3, r0, r3 // r3 = slide
230 ldr r0, [r8] // r0 = mach_header
231 ldr r1, [r8, #4] // r1 = argc
232 add r2, r8, #8 // r2 = argv
236 str r4, [sp, #0] // [sp] = dyld_mh
238 str r4, [sp, #4] // [sp+4] = &startGlue
240 bl __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
245 // traditional case, clean up stack and jump to result
246 add sp, r8, #4 // remove the mach_header argument.
247 bx r0 // jump to the program's entry point
249 // LC_MAIN case, set up stack for call to main()
250 Lnew: mov lr, r5 // simulate return address into _start in libdyld
251 mov r5, r0 // save address of main() for later use
252 ldr r0, [r8, #4] // main param1 = argc
253 add r1, r8, #8 // main param2 = argv
254 add r2, r1, r0, lsl #2
255 add r2, r2, #4 // main param3 = &env[0]
260 bne Lapple // main param4 = apple
264 L__dyld_start_picbase_ptr:
265 .long __dyld_start_static_picbase-L__dyld_start_picbase
266 Lmh: .long ___dso_handle-L3-8
284 and sp, x28, #~15 // force 16-byte alignment of stack
287 stp x1, x0, [sp, #-16]! // make aligned terminating frame
288 mov fp, sp // set up fp to point to terminating frame
289 sub sp, sp, #16 // make room for local variables
290 ldr x0, [x28] // get app's mh into x0
291 ldr x1, [x28, #8] // get argc into x1 (kernel passes 32-bit int argc as 64-bits on stack to keep alignment)
292 add x2, x28, #16 // get argv into x2
293 adrp x4,___dso_handle@page
294 add x4,x4,___dso_handle@pageoff // get dyld's mh in to x4
295 adrp x3,__dso_static@page
296 ldr x3,[x3,__dso_static@pageoff] // get unslid start of dyld
297 sub x3,x4,x3 // x3 now has slide of dyld
298 mov x5,sp // x5 has &startGlue
300 // call dyldbootstrap::start(app_mh, argc, argv, slide, dyld_mh, &startGlue)
301 bl __ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
302 mov x16,x0 // save entry point address in x16
307 // LC_UNIXTHREAD way, clean up stack and jump to result
308 add sp, x28, #8 // restore unaligned stack pointer without app mh
309 br x16 // jump to the program's entry point
311 // LC_MAIN case, set up stack for call to main()
312 Lnew: mov lr, x1 // simulate return address into _start in libdyld.dylib
313 ldr x0, [x28, #8] // main param1 = argc
314 add x1, x28, #16 // main param2 = argv
315 add x2, x1, x0, lsl #3
316 add x2, x2, #8 // main param3 = &env[0]
321 b.ne Lapple // main param4 = apple
327 // When iOS 10.0 simulator runs on 10.11, abort_with_payload() does not exist,
328 // so it falls back and uses dyld_fatal_error().
329 #if TARGET_IPHONE_SIMULATOR
332 .globl _dyld_fatal_error