Update.
[platform/upstream/glibc.git] / sysdeps / powerpc / elf / start.c
1 /* Startup code compliant to the ELF PowerPC ABI.
2    Copyright (C) 1997 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 Library General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    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    Library General Public License for more details.
14
15    You should have received a copy of the GNU Library General Public
16    License along with the GNU C Library; see the file COPYING.LIB.  If not,
17    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18    Boston, MA 02111-1307, USA.  */
19
20 /* This is SVR4/PPC ABI compliant, and works under Linux when
21    statically linked.  */
22
23 #include <unistd.h>
24 #include <stdlib.h>
25
26 /* Just a little assembler stub before gcc gets its hands on our
27    stack pointer... */
28 asm ("\
29         .text
30         .globl _start
31 _start:
32  # save the stack pointer, in case we're statically linked under Linux
33         mr 8,1
34  # set up an initial stack frame, and clear the LR
35         addi 1,1,-16
36         clrrwi 1,1,4
37         li 0,0
38         stw 0,0(1)
39         mtlr 0
40  # set r13 to point at the 'small data area'
41         lis 13,_SDA_BASE_@ha
42         addi 13,13,_SDA_BASE_@l
43  # and continue below.
44         b __start1
45 ");
46
47 /* Define a symbol for the first piece of initialized data.  */
48 int __data_start = 0;
49 weak_alias (__data_start, data_start)
50
51 /* these probably should go, at least go somewhere else
52    (sysdeps/mach/something?). */
53 void (*_mach_init_routine) (void);
54 void (*_thread_init_routine) (void);
55
56 void __libc_init_first (int argc, char **argv, char **envp);
57 int main (int argc, char **argv, char **envp, void *auxvec);
58 #ifdef HAVE_INITFINI
59 void _init (void);
60 void _fini (void);
61 #endif
62
63
64 static void __start1(int argc, char **argv, char **envp,
65                      void *auxvec, void (*exitfn) (void), char **arguments)
66      __attribute__ ((unused));
67 static void
68 __start1(int argc, char **argv, char **envp,
69          void *auxvec, void (*exitfn) (void),
70          char **arguments)
71 {
72   /* the PPC SVR4 ABI says that the top thing on the stack will
73      be a NULL pointer, so if not we assume that we're being called
74      as a statically-linked program by Linux. */
75   int abi_compliant_startup = *arguments == NULL;
76
77   if (!abi_compliant_startup)
78   {
79     argc = *(int *) arguments;
80     argv = arguments+1;
81     envp = argv+argc+1;
82     auxvec = envp;
83     while (auxvec != NULL)
84       auxvec++;
85     auxvec++;
86     exitfn = NULL;
87   }
88
89   if (exitfn != NULL)
90     atexit (exitfn);
91
92   /* libc init routine, in case we are statically linked
93      (otherwise ld.so will have called it when it loaded libc, but
94      calling it twice doesn't hurt). */
95   __libc_init_first (argc, argv, envp);
96
97 #ifdef HAVE_INITFINI
98   /* ELF constructors/destructors */
99   atexit (_fini);
100   _init ();
101 #endif
102
103   /* Stuff so we can build Mach/Linux executables (like vmlinux).  */
104   if (_mach_init_routine != 0)
105     _mach_init_routine ();
106   if (_thread_init_routine != 0)
107     _thread_init_routine ();
108
109   /* the rest of the program */
110   exit (main (argc, argv, envp, auxvec));
111 }