OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [linux/] [uClibc/] [libc/] [misc/] [internals/] [__uClibc_main.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/*
2
 * Manuel Novoa III           Feb 2001
3
 * Erik Andersen              Mar 2002
4
 *
5
 * __uClibc_main is the routine to be called by all the arch-specific
6
 * versions of crt0.S in uClibc.
7
 *
8
 * It is meant to handle any special initialization needed by the library
9
 * such as setting the global variable(s) __environ (environ) and
10
 * initializing the stdio package.  Using weak symbols, the latter is
11
 * avoided in the static library case.
12
 */
13
 
14
#define _ERRNO_H
15
#include <features.h>
16
#include <unistd.h>
17
#include <stdlib.h>
18
#ifdef __UCLIBC_PROPOLICE__
19
extern void __guard_setup(void);
20
#endif
21
 
22
 
23
/*
24
 * Prototypes.
25
 */
26
extern int  main(int argc, char **argv, char **envp);
27
extern void weak_function _stdio_init(void);
28
extern int *weak_const_function __errno_location(void);
29
extern int *weak_const_function __h_errno_location(void);
30
#ifdef __UCLIBC_HAS_LOCALE__
31
extern void weak_function _locale_init(void);
32
#endif
33
#ifdef __UCLIBC_HAS_THREADS__
34
extern void weak_function __pthread_initialize_minimal(void);
35
#endif
36
 
37
 
38
 
39
 
40
 
41
/*
42
 * Declare the __environ global variable and create a weak alias environ.
43
 * Note: Apparently we must initialize __environ to ensure that the weak
44
 * environ symbol is also included.
45
 */
46
 
47
char **__environ = 0;
48
const char *__progname = 0;
49
weak_alias(__environ, environ);
50
 
51
 
52
 
53
/* __uClibc_init completely initialize uClibc so it is ready to use.
54
 *
55
 * On ELF systems (with a dynamic loader) this function must be called
56
 * from the dynamic loader (see TIS and ELF Specification), so that
57
 * constructors of shared libraries (which depend on libc) can use all
58
 * the libc code without restriction.  For this we link the shared
59
 * version of the uClibc with -init __uClibc_init so DT_INIT for
60
 * uClibc is the address of __uClibc_init
61
 *
62
 * In all other cases we call it from the main stub
63
 * __uClibc_start_main.
64
 */
65
 
66
void __uClibc_init(void)
67
{
68
    static int been_there_done_that = 0;
69
 
70
    if (been_there_done_that)
71
        return;
72
    been_there_done_that++;
73
 
74
#ifdef __UCLIBC_HAS_THREADS__
75
    /* Before we start initialzing uClibc we have to call
76
     * __pthread_initialize_minimal so we can use pthread_locks
77
     * whenever they are needed.
78
     */
79
    if (likely(__pthread_initialize_minimal!=NULL))
80
        __pthread_initialize_minimal();
81
#endif
82
 
83
#if 0
84
    /* Some security at this point.  Prevent starting a SUID binary
85
     * where the standard file descriptors are not opened.  We have
86
     * to do this only for statically linked applications since
87
     * otherwise the dynamic loader did the work already.  */
88
    if (unlikely (__libc_enable_secure!=NULL))
89
        __libc_check_standard_fds ();
90
#endif
91
 
92
#ifdef __UCLIBC_HAS_LOCALE__
93
    /* Initialize the global locale structure. */
94
    if (likely(_locale_init!=NULL))
95
        _locale_init();
96
#endif
97
 
98
    /*
99
     * Initialize stdio here.  In the static library case, this will
100
     * be bypassed if not needed because of the weak alias above.
101
     */
102
    if (likely(_stdio_init != NULL))
103
        _stdio_init();
104
 
105
}
106
 
107
#ifdef __UCLIBC_CTOR_DTOR__
108
void (*__app_fini)(void) = NULL;
109
#endif
110
 
111
/* __uClibc_start_main is the new main stub for uClibc. This function is
112
 * called from crt0 (version 0.9.16 or newer), after ALL shared libraries
113
 * are initialized, just before we call the application's main function.
114
 */
115
void __attribute__ ((__noreturn__))
116
__uClibc_start_main(int argc, char **argv, char **envp,
117
        void (*app_init)(void), void (*app_fini)(void))
118
{
119
 
120
    /* If we are dynamically linked the shared lib loader already
121
     * did this for us.  But if we are statically linked, we need
122
     * to do this for ourselves.  */
123
    if (__environ==NULL) {
124
        /* Statically linked. */
125
        __environ = envp;
126
    }
127
 
128
    /* We need to initialize uClibc.  If we are dynamically linked this
129
     * may have already been completed by the shared lib loader.  We call
130
     * __uClibc_init() regardless, to be sure the right thing happens. */
131
    __uClibc_init();
132
 
133
    __progname = *argv;
134
 
135
#ifdef __UCLIBC_CTOR_DTOR__
136
    /* Arrange for the application's dtors to run before we exit.  */
137
        __app_fini = app_fini;
138
 
139
    /* Run all the application's ctors now.  */
140
    if (app_init!=NULL) {
141
        app_init();
142
    }
143
#endif
144
 
145
#ifdef __UCLIBC_PROPOLICE__
146
     __guard_setup ();
147
#endif
148
 
149
    /* Note: It is possible that any initialization done above could
150
     * have resulted in errno being set nonzero, so set it to 0 before
151
     * we call main.
152
     */
153
    if (likely(__errno_location!=NULL))
154
        *(__errno_location()) = 0;
155
 
156
    /* Set h_errno to 0 as well */
157
    if (likely(__h_errno_location!=NULL))
158
        *(__h_errno_location()) = 0;
159
 
160
    /*
161
     * Finally, invoke application's main and then exit.
162
     */
163
    exit(main(argc, argv, envp));
164
}
165
 
166
 
167
/* __uClibc_main is the old main stub of the uClibc. This
168
 * function is called from crt0 (uClibc 0.9.15 and older) after
169
 * ALL shared libraries are initialized, and just before we call
170
 * the application's main() function.
171
 *
172
 * Attention: This stub does not call the .init/.fini sections of
173
 * the application. If you need this, please fix your uClibc port
174
 * so that  __uClibc_start_main is called by your crt0.S with
175
 * _init and _fini properly set.
176
*/
177
void __attribute__ ((__noreturn__))
178
__uClibc_main(int argc, char **argv, char ** envp)
179
{
180
    __uClibc_start_main(argc, argv, envp, NULL, NULL);
181
}
182
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.