]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - lib/csu/arm/crt1.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / lib / csu / arm / crt1.c
1 /* LINTLIBRARY */
2 /*-
3  * Copyright 2001 David E. O'Brien.
4  * All rights reserved.
5  * Copyright 1996-1998 John D. Polstra.
6  * All rights reserved.
7  * Copyright (c) 1997 Jason R. Thorpe.
8  * Copyright (c) 1995 Christopher G. Demetriou
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *          This product includes software developed for the
22  *          FreeBSD Project.  See http://www.freebsd.org/ for
23  *          information about FreeBSD.
24  *          This product includes software developed for the
25  *          NetBSD Project.  See http://www.netbsd.org/ for
26  *          information about NetBSD.
27  * 4. The name of the author may not be used to endorse or promote products
28  *    derived from this software without specific prior written permission
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #ifndef lint
46 #ifndef __GNUC__
47 #error "GCC is needed to compile this file"
48 #endif
49 #endif /* lint */
50
51 #include <stdlib.h>
52
53 #include "libc_private.h"
54 #include "crtbrand.c"
55
56 struct Struct_Obj_Entry;
57 struct ps_strings;
58
59 extern int _DYNAMIC;
60 #pragma weak _DYNAMIC
61
62 extern void _fini(void);
63 extern void _init(void);
64 extern int main(int, char **, char **);
65 extern void _start(int, char **, char **, const struct Struct_Obj_Entry *,
66     void (*)(void), struct ps_strings *);
67
68 #ifdef GCRT
69 extern void _mcleanup(void);
70 extern void monstartup(void *, void *);
71 extern int eprol;
72 extern int etext;
73 #endif
74
75 char **environ;
76 const char *__progname = "";
77 struct ps_strings *__ps_strings;
78
79 void __start(int, char **, char **, struct ps_strings *,
80     const struct Struct_Obj_Entry *, void (*)(void));
81
82 /* The entry function. */
83 __asm(" .text                   \n"
84 "       .align  0               \n"
85 "       .globl  _start          \n"
86 "       _start:                 \n"
87 "       mov     r5, r2          /* cleanup */           \n"
88 "       mov     r4, r1          /* obj_main */          \n"
89 "       mov     r3, r0          /* ps_strings */        \n"
90 "       /* Get argc, argv, and envp from stack */       \n"
91 "       ldr     r0, [sp, #0x0000]       \n"
92 "       add     r1, sp, #0x0004         \n"
93 "       add     r2, r1, r0, lsl #2      \n"
94 "       add     r2, r2, #0x0004         \n"
95 "       /* Ensure the stack is properly aligned before calling C code. */\n"
96 "       bic     sp, sp, #7      \n"
97 "       sub     sp, sp, #8      \n"
98 "       str     r5, [sp, #4]    \n"
99 "       str     r4, [sp, #0]    \n"
100 "\n"
101 "       b        __start  ");
102 /* ARGSUSED */
103 void
104 __start(int argc, char **argv, char **env, struct ps_strings *ps_strings,
105     const struct Struct_Obj_Entry *obj __unused, void (*cleanup)(void))
106 {
107         const char *s;
108
109         environ = env;
110
111         if (argc > 0 && argv[0] != NULL) {
112                 __progname = argv[0];
113                 for (s = __progname; *s != '\0'; s++)
114                         if (*s == '/')
115                                 __progname = s + 1;
116         }
117
118         if (ps_strings != (struct ps_strings *)0)
119                 __ps_strings = ps_strings;
120
121         if (&_DYNAMIC != NULL)
122                 atexit(cleanup);
123         else
124                 _init_tls();
125 #ifdef GCRT
126         atexit(_mcleanup);
127 #endif
128         atexit(_fini);
129 #ifdef GCRT
130         monstartup(&eprol, &etext);
131 #endif
132         _init();
133         exit( main(argc, argv, env) );
134 }
135
136 #ifdef GCRT
137 __asm__(".text");
138 __asm__("eprol:");
139 __asm__(".previous");
140 #endif