2002-12-08 Roland McGrath <roland@redhat.com>
[glibc.git] / sysdeps / mips / elf / start.S
blobe85e9f535ef22217b10b8560e558394323bdc35e
1 /* Startup code compliant to the ELF Mips ABI.
2    Copyright (C) 1995, 1997, 2000, 2001, 2002 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., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
20 #define __ASSEMBLY__ 1
21 #include <entry.h>
23 #ifndef ENTRY_POINT
24 #error ENTRY_POINT needs to be defined for start.S on MIPS/ELF.
25 #endif
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:
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.
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.
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)
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
68         .text
69         .globl ENTRY_POINT
70         .type ENTRY_POINT,@function
71 ENTRY_POINT:
72 #ifdef __PIC__
73         SET_GP
74 #else
75         la $28, _gp             /* Setup GP correctly if we're non-PIC.  */
76 #endif
77         move $31, $0
79         la $4, main             /* main */
80         lw $5, 0($29)           /* argc */
81         addu $6, $29, 4         /* argv  */
82         /* Allocate space on the stack for seven arguments and make sure
83            the stack is aligned to double words (8 bytes).  */
84         and $29, 0xfffffff8
85         subu $29, 32
86         la $7, __libc_csu_init          /* init */
87         la $8, __libc_csu_fini
88         sw $8, 16($29)          /* fini */
89         sw $2, 20($29)          /* rtld_fini */
90         sw $29, 24($29)         /* stack_end */
91         jal __libc_start_main
92 hlt:    b hlt                   /* Crash if somehow it does return.  */
94 /* Define a symbol for the first piece of initialized data.  */
95         .data
96         .globl __data_start
97 __data_start:
98         .long 0
99         .weak data_start
100         data_start = __data_start