2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 * $FreeBSD: src/lib/csu/i386-elf/crt1.c, svn 200038 2009/12/02 kib $
29 #error "GCC is needed to compile this file"
32 #include <machine/tls.h>
36 #include "libc_private.h"
42 typedef void (*fptr)(void);
44 extern void _fini(void);
45 extern void _init(void);
46 extern int main(int, char **, char **);
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;
56 extern void _mcleanup(void);
57 extern void monstartup(void *, void *);
63 const char *__progname = "";
65 void _start1(fptr, int, char *[]) __dead2;
67 /* The entry function, C part. */
69 _start1(fptr cleanup, int argc, char *argv[])
75 env = argv + argc + 1;
77 if (argc > 0 && argv[0] != NULL) {
79 for (s = __progname; *s != '\0'; s++)
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().
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.
106 array_size = __fini_array_end - __fini_array_start;
107 for (n = 0; n < array_size; n++)
108 atexit(*__fini_array_start [n]);
110 monstartup(&eprol, &etext);
113 if (&_DYNAMIC == NULL) {
115 * For static executables preinit happens right before init.
116 * Dynamic executable preinit arrays are handled by rtld
117 * before any DSOs are initialized.
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);
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) );
130 __asm(".hidden _start1");