2.9
[glibc/nacl-glibc.git] / sysdeps / powerpc / powerpc32 / dl-start.S
blobc77c4de1987a7003efb4c52531fb98ad84c61e84
1 /* Machine-dependent ELF startup code.  PowerPC version.
2    Copyright (C) 1995-2000, 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA
18    02110-1301 USA.  */
20 #include <sysdep.h>
22 /* Initial entry point code for the dynamic linker.
23    The C function `_dl_start' is the real entry point;
24    its return value is the user program's entry point.  */
25 ENTRY(_start)
26 /* We start with the following on the stack, from top:
27    argc (4 bytes);
28    arguments for program (terminated by NULL);
29    environment variables (terminated by NULL);
30    arguments for the program loader. */
32 /* Call _dl_start with one parameter pointing at argc */
33         mr      r3,r1
34 /* (we have to frob the stack pointer a bit to allow room for
35    _dl_start to save the link register).  */
36         li      r4,0
37         addi    r1,r1,-16
38         stw     r4,0(r1)
39         bl      _dl_start@local
41         /* FALLTHRU */
42 _dl_start_user:
43 /* Now, we do our main work of calling initialisation procedures.
44    The ELF ABI doesn't say anything about parameters for these,
45    so we just pass argc, argv, and the environment.
46    Changing these is strongly discouraged (not least because argc is
47    passed by value!).  */
49 /*  Put our GOT pointer in r31, */
50 #ifdef HAVE_ASM_PPC_REL16
51         bcl     20,31,1f
52 1:      mflr    r31
53         addis   r31,r31,_GLOBAL_OFFSET_TABLE_-1b@ha
54         addi    r31,r31,_GLOBAL_OFFSET_TABLE_-1b@l
55 #else
56         bl      _GLOBAL_OFFSET_TABLE_-4@local
57         mflr    r31
58 #endif
59 /*  the address of _start in r30, */
60         mr      r30,r3
61 /*  &_dl_argc in 29, &_dl_argv in 27, and _dl_loaded in 28.  */
62         lwz     r28,_rtld_global@got(r31)
63         lwz     r29,_dl_argc@got(r31)
64         lwz     r27,_dl_argv@got(r31)
66 /* Call _dl_init (_dl_loaded, _dl_argc, _dl_argv, _dl_argv+_dl_argc+1). */
67         lwz     r3,0(r28)
68         lwz     r4,0(r29)
69         lwz     r5,0(r27)
70         slwi    r6,r4,2
71         add     r6,r5,r6
72         addi    r6,r6,4
73         bl      _dl_init_internal@local
75 /* Now, to conform to the ELF ABI, we have to: */
76 /* Pass argc (actually _dl_argc) in r3; */
77         lwz     r3,0(r29)
78 /* pass argv (actually _dl_argv) in r4; */
79         lwz     r4,0(r27)
80 /* pass envp (actually _dl_argv+_dl_argc+1) in r5; */
81         slwi    r5,r3,2
82         add     r6,r4,r5
83         addi    r5,r6,4
84 /* pass the auxilary vector in r6. This is passed to us just after _envp.  */
85 2:      lwzu    r0,4(r6)
86         cmpwi   r0,0
87         bne     2b
88         addi    r6,r6,4
89 /* Pass a termination function pointer (in this case _dl_fini) in r7.  */
90         lwz     r7,_dl_fini@got(r31)
91 /* Now, call the start function in r30... */
92         mtctr   r30
93 /* Pass the stack pointer in r1 (so far so good), pointing to a NULL value.
94    (This lets our startup code distinguish between a program linked statically,
95    which linux will call with argc on top of the stack which will hopefully
96    never be zero, and a dynamically linked program which will always have
97    a NULL on the top of the stack).
98    Take the opportunity to clear LR, so anyone who accidentally returns
99    from _start gets SEGV.  Also clear the next few words of the stack.  */
101 _dl_main_dispatch:
102         li      r31,0
103         stw     r31,0(r1)
104         mtlr    r31
105         stw     r31,4(r1)
106         stw     r31,8(r1)
107         stw     r31,12(r1)
108 /* Go do it!  */
109         bctr
110 END(_start)