rtld: Add support for preinit, init, and fini arrays
[dragonfly.git] / lib / csu / i386 / crt1_c.c
CommitLineData
e61eaf36
JS
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 *
e101389c 25 * $FreeBSD: src/lib/csu/i386-elf/crt1.c, svn 200038 2009/12/02 kib $
e61eaf36
JS
26 */
27
28#ifndef __GNUC__
29#error "GCC is needed to compile this file"
30#endif
31
a1eee96a 32#include <machine/tls.h>
e61eaf36
JS
33#include <stddef.h>
34#include <stdlib.h>
2f81c0d4
JM
35
36#include "libc_private.h"
e61eaf36
JS
37#include "crtbrand.c"
38
2f81c0d4
JM
39extern int _DYNAMIC;
40#pragma weak _DYNAMIC
41
e61eaf36
JS
42typedef void (*fptr)(void);
43
44extern void _fini(void);
45extern void _init(void);
46extern int main(int, char **, char **);
47
b28bf640
JM
48extern void (*__preinit_array_start []) (int, char **, char **) __dso_hidden;
49extern void (*__preinit_array_end []) (int, char **, char **) __dso_hidden;
50extern void (*__init_array_start []) (int, char **, char **) __dso_hidden;
51extern void (*__init_array_end []) (int, char **, char **) __dso_hidden;
52extern void (*__fini_array_start []) (void) __dso_hidden;
53extern void (*__fini_array_end []) (void) __dso_hidden;
54
e61eaf36
JS
55#ifdef GCRT
56extern void _mcleanup(void);
57extern void monstartup(void *, void *);
58extern int eprol;
59extern int etext;
60#endif
61
e61eaf36 62char **environ;
2f81c0d4
JM
63const char *__progname = "";
64
e101389c 65void _start1(fptr, int, char *[]) __dead2;
e61eaf36 66
2f81c0d4 67/* The entry function, C part. */
e61eaf36 68void
e101389c 69_start1(fptr cleanup, int argc, char *argv[])
e61eaf36 70{
e61eaf36 71 char **env;
2f81c0d4 72 const char *s;
b28bf640 73 size_t n, array_size;
e61eaf36 74
e61eaf36
JS
75 env = argv + argc + 1;
76 environ = env;
2f81c0d4 77 if (argc > 0 && argv[0] != NULL) {
e61eaf36
JS
78 __progname = argv[0];
79 for (s = __progname; *s != '\0'; s++)
80 if (*s == '/')
81 __progname = s + 1;
82 }
83
a1eee96a
MD
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
e101389c 88 * _rtld_allocate_tls().
a1eee96a
MD
89 */
90 _init_tls();
91 _rtld_call_init();
92
e61eaf36 93 if(&_DYNAMIC != NULL)
e101389c 94 atexit(cleanup);
e61eaf36
JS
95
96#ifdef GCRT
97 atexit(_mcleanup);
98#endif
b28bf640
JM
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 */
e61eaf36 105 atexit(_fini);
b28bf640
JM
106 array_size = __fini_array_end - __fini_array_start;
107 for (n = 0; n < array_size; n++)
108 atexit(*__fini_array_start [n]);
e61eaf36
JS
109#ifdef GCRT
110 monstartup(&eprol, &etext);
e101389c 111__asm__("eprol:");
e61eaf36 112#endif
b28bf640
JM
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 }
e61eaf36 123 _init();
b28bf640
JM
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);
e61eaf36 127 exit( main(argc, argv, env) );
e61eaf36
JS
128}
129
e101389c 130__asm(".hidden _start1");