Merge remote-tracking branch 'crater/master'
[dragonfly.git] / lib / csu / i386 / crt1_c.c
1 /*-
2  * Copyright 1996-1998 John D. Polstra.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD: src/lib/csu/i386-elf/crt1.c, svn 200038 2009/12/02 kib $
26  */
27
28 #ifndef __GNUC__
29 #error "GCC is needed to compile this file"
30 #endif
31
32 #include <machine/tls.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35
36 #include "libc_private.h"
37 #include "crtbrand.c"
38
39 extern int _DYNAMIC;
40 #pragma weak _DYNAMIC
41
42 typedef void (*fptr)(void);
43
44 extern void _fini(void);
45 extern void _init(void);
46 extern int main(int, char **, char **);
47
48 extern void (*__preinit_array_start []) (int, char **, char **) __dso_hidden;
49 extern void (*__preinit_array_end   []) (int, char **, char **) __dso_hidden;
50 extern void (*__init_array_start    []) (int, char **, char **) __dso_hidden;
51 extern void (*__init_array_end      []) (int, char **, char **) __dso_hidden;
52 extern void (*__fini_array_start    []) (void) __dso_hidden;
53 extern void (*__fini_array_end      []) (void) __dso_hidden;
54
55 #ifdef GCRT
56 extern void _mcleanup(void);
57 extern void monstartup(void *, void *);
58 extern int eprol;
59 extern int etext;
60 #endif
61
62 char **environ;
63 const char *__progname = "";
64
65 void _start1(fptr, int, char *[]) __dead2;
66
67 /* The entry function, C part. */
68 void
69 _start1(fptr cleanup, int argc, char *argv[])
70 {
71     char **env;
72     const char *s;
73     size_t n, array_size;
74
75     env = argv + argc + 1;
76     environ = env;
77     if (argc > 0 && argv[0] != NULL) {
78         __progname = argv[0];
79         for (s = __progname; *s != '\0'; s++)
80             if (*s == '/')
81                 __progname = s + 1;
82     }
83
84     /*
85      * Setup the initial TLS space.  The RTLD does not set up our TLS
86      * (it can't, it doesn't know how our errno is declared).  It simply
87      * does all the groundwork required so that we can call
88      * _rtld_allocate_tls().
89      */
90     _init_tls();
91     _rtld_call_init();
92
93     if(&_DYNAMIC != NULL)
94         atexit(cleanup);
95
96 #ifdef GCRT
97     atexit(_mcleanup);
98 #endif
99     /*
100      * The fini_array needs to be executed in the opposite order of its
101      * definition.  However, atexit works like a LIFO stack, so by
102      * pushing functions in array order, they will be executed in the
103      * reverse order as required.
104      */
105     atexit(_fini);
106     array_size = __fini_array_end - __fini_array_start;
107     for (n = 0; n < array_size; n++)
108         atexit(*__fini_array_start [n]);
109 #ifdef GCRT
110     monstartup(&eprol, &etext);
111 __asm__("eprol:");
112 #endif
113     if (&_DYNAMIC == NULL) {
114         /*
115          * For static executables preinit happens right before init.
116          * Dynamic executable preinit arrays are handled by rtld
117          * before any DSOs are initialized.
118          */
119         array_size = __preinit_array_end - __preinit_array_start;
120         for (n = 0; n < array_size; n++)
121                 (*__preinit_array_start [n])(argc, argv, env);
122     }
123     _init();
124     array_size = __init_array_end - __init_array_start;
125     for (n = 0; n < array_size; n++)
126         (*__init_array_start [n])(argc, argv, env);
127     exit( main(argc, argv, env) );
128 }
129
130 __asm(".hidden  _start1");