Update to LGPL v2.1.
[jlayton/glibc.git] / sysdeps / mips / elf / start.S
1 /* Startup code compliant to the ELF Mips ABI.
2    Copyright (C) 1995, 1997, 2000, 2001 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
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.
9
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.
14
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., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #define __ASSEMBLY__ 1
21 #include <entry.h>
22
23 #ifndef ENTRY_POINT
24 #error ENTRY_POINT needs to be defined for start.S on MIPS/ELF.
25 #endif
26
27 /* This is the canonical entry point, usually the first thing in the text
28    segment.  The SVR4/Mips ABI (pages 3-31, 3-32) says that when the entry
29    point runs, most registers' values are unspecified, except for:
30
31    v0 ($2)      Contains a function pointer to be registered with `atexit'.
32                 This is how the dynamic linker arranges to have DT_FINI
33                 functions called for shared libraries that have been loaded
34                 before this code runs.
35
36    sp ($29)     The stack contains the arguments and environment:
37                 0(%esp)                 argc
38                 4(%esp)                 argv[0]
39                 ...
40                 (4*argc)(%esp)          NULL
41                 (4*(argc+1))(%esp)      envp[0]
42                 ...
43                                         NULL
44    ra ($31)     The return address register is set to zero so that programs
45                 that search backword through stack frames recognize the last
46                 stack frame.
47 */
48
49
50 /* We need to call:
51    __libc_start_main (int (*main) (int, char **, char **), int argc,
52                       char **argv, void (*init) (void), void (*fini) (void),
53                       void (*rtld_fini) (void), void *stack_end)
54 */
55 #ifdef __PIC__
56 /* A macro to (re)initialize gp. We can get the run time address of 0f in
57    ra ($31) by blezal instruction. In this early phase, we can't save gp
58    in stack and .cprestore doesn't work properly. So we set gp by using
59    this macro. */
60 #define SET_GP \
61         .set noreorder; \
62         bltzal $0,0f;   \
63         nop;            \
64 0:      .cpload $31;    \
65         .set reorder;
66 #endif
67
68         .text
69         .globl ENTRY_POINT
70         .type ENTRY_POINT,@function
71 ENTRY_POINT:
72 #ifdef __PIC__
73         SET_GP
74 #endif
75         move $31, $0
76
77         la $4, main             /* main */
78         lw $5, 0($29)           /* argc */
79         addu $6, $29, 4         /* argv  */
80         /* Allocate space on the stack for seven arguments and make sure
81            the stack is aligned to double words (8 bytes).  */
82         and $29, 0xfffffff8
83         subu $29, 32
84         la $7, _init            /* init */
85         la $8, _fini
86         sw $8, 16($29)          /* fini */
87         sw $2, 20($29)          /* rtld_fini */
88         sw $29, 24($29)         /* stack_end */
89         jal __libc_start_main
90 hlt:    b hlt                   /* Crash if somehow it does return.  */
91
92 /* Define a symbol for the first piece of initialized data.  */
93         .data
94         .globl __data_start
95 __data_start:
96         .long 0
97         .weak data_start
98         data_start = __data_start