OSDN Git Service

b166aaaa702107ae056f28678310b5a20ad13c17
[uclinux-h8/uClibc.git] / libc / misc / internals / __uClibc_main.c
1 /*
2  * Copyright (C) Feb 2001 Manuel Novoa III
3  * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
4  *
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  *
7  * __uClibc_main is the routine to be called by all the arch-specific
8  * versions of crt1.S in uClibc.
9  *
10  * It is meant to handle any special initialization needed by the library
11  * such as setting the global variable(s) __environ (environ) and
12  * initializing the stdio package.  Using weak symbols, the latter is
13  * avoided in the static library case.
14  */
15
16 #define _ERRNO_H
17 #include <features.h>
18 #include <unistd.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <elf.h>
22 #include <link.h>
23 #include <bits/uClibc_page.h>
24 #include <paths.h>
25 #include <asm/errno.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/sysmacros.h>
29
30
31 #ifndef SHARED
32 void *__libc_stack_end = NULL;
33
34 # ifdef __UCLIBC_HAS_SSP__
35 #  include <dl-osinfo.h>
36 #  ifndef THREAD_SET_STACK_GUARD
37 /* Only exported for architectures that don't store the stack guard canary
38  * in thread local area. */
39 #   include <stdint.h>
40 uintptr_t stack_chk_guard;
41 /* for gcc-4.1 non-TLS */
42 uintptr_t __stack_chk_guard attribute_relro;
43 /* for gcc-3.x + Etoh ssp */
44 #   ifdef __UCLIBC_HAS_SSP_COMPAT__
45 #    ifdef __HAVE_SHARED__
46 strong_alias(__stack_chk_guard,__guard)
47 #    else
48 uintptr_t __guard attribute_relro;
49 #    endif
50 #   endif
51 #  elif defined __UCLIBC_HAS_SSP_COMPAT__
52 uintptr_t __guard attribute_relro;
53 #  endif
54 # endif
55
56 /*
57  * Needed to initialize _dl_phdr when statically linked
58  */
59
60 void internal_function _dl_aux_init (ElfW(auxv_t) *av);
61 #endif /* !SHARED */
62
63 /*
64  * Prototypes.
65  */
66 extern int *weak_const_function __errno_location(void);
67 libc_hidden_proto(__errno_location)
68 extern int *weak_const_function __h_errno_location(void);
69 libc_hidden_proto(__h_errno_location)
70
71 extern void weak_function _stdio_init(void) attribute_hidden;
72 #ifdef __UCLIBC_HAS_LOCALE__
73 extern void weak_function _locale_init(void) attribute_hidden;
74 #endif
75 #ifdef __UCLIBC_HAS_THREADS__
76 extern void weak_function __pthread_initialize_minimal(void);
77 #endif
78
79 /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation and finalisation
80  * is handled by the routines passed to __uClibc_main().  */
81 #if defined (__UCLIBC_CTOR_DTOR__) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
82 extern void _dl_app_init_array(void);
83 extern void _dl_app_fini_array(void);
84 # ifndef SHARED
85 /* These magic symbols are provided by the linker.  */
86 extern void (*__preinit_array_start []) (void) attribute_hidden;
87 extern void (*__preinit_array_end []) (void) attribute_hidden;
88 extern void (*__init_array_start []) (void) attribute_hidden;
89 extern void (*__init_array_end []) (void) attribute_hidden;
90 extern void (*__fini_array_start []) (void) attribute_hidden;
91 extern void (*__fini_array_end []) (void) attribute_hidden;
92 # endif
93 #endif
94
95 attribute_hidden const char *__uclibc_progname = "";
96 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
97 const char *program_invocation_short_name = "";
98 const char *program_invocation_name = "";
99 #endif
100 #ifdef __UCLIBC_HAS___PROGNAME__
101 weak_alias (program_invocation_short_name, __progname)
102 weak_alias (program_invocation_name, __progname_full)
103 #endif
104
105 /*
106  * Declare the __environ global variable and create a weak alias environ.
107  * This must be initialized; we cannot have a weak alias into bss.
108  */
109 char **__environ = 0;
110 weak_alias(__environ, environ)
111
112 /* TODO: don't export __pagesize; we cant now because libpthread uses it */
113 size_t __pagesize = 0;
114
115 #ifndef O_NOFOLLOW
116 # define O_NOFOLLOW     0
117 #endif
118
119 #ifndef __ARCH_HAS_NO_LDSO__
120 static void __check_one_fd(int fd, int mode)
121 {
122     /* Check if the specified fd is already open */
123     if (fcntl(fd, F_GETFD) == -1)
124     {
125         /* The descriptor is probably not open, so try to use /dev/null */
126         int nullfd = open(_PATH_DEVNULL, mode);
127         /* /dev/null is major=1 minor=3.  Make absolutely certain
128          * that is in fact the device that we have opened and not
129          * some other weird file... [removed in uclibc] */
130         if (nullfd!=fd)
131         {
132                 abort();
133         }
134     }
135 }
136
137 static int __check_suid(void)
138 {
139     uid_t uid, euid;
140     gid_t gid, egid;
141
142     uid  = getuid();
143     euid = geteuid();
144     if (uid != euid)
145         return 1;
146     gid  = getgid();
147     egid = getegid();
148     if (gid != egid)
149         return 1;
150     return 0; /* we are not suid */
151 }
152 #endif
153
154 /* __uClibc_init completely initialize uClibc so it is ready to use.
155  *
156  * On ELF systems (with a dynamic loader) this function must be called
157  * from the dynamic loader (see TIS and ELF Specification), so that
158  * constructors of shared libraries (which depend on libc) can use all
159  * the libc code without restriction.  For this we link the shared
160  * version of the uClibc with -init __uClibc_init so DT_INIT for
161  * uClibc is the address of __uClibc_init
162  *
163  * In all other cases we call it from the main stub
164  * __uClibc_main.
165  */
166
167 extern void __uClibc_init(void);
168 libc_hidden_proto(__uClibc_init)
169 void __uClibc_init(void)
170 {
171     static smallint been_there_done_that;
172
173     if (been_there_done_that)
174         return;
175     been_there_done_that++;
176
177     /* Setup an initial value.  This may not be perfect, but is
178      * better than  malloc using __pagesize=0 for atexit, ctors, etc.  */
179     __pagesize = PAGE_SIZE;
180
181 #ifdef __UCLIBC_HAS_THREADS__
182     /* Before we start initializing uClibc we have to call
183      * __pthread_initialize_minimal so we can use pthread_locks
184      * whenever they are needed.
185      */
186     if (likely(__pthread_initialize_minimal!=NULL))
187         __pthread_initialize_minimal();
188 #endif
189
190 #ifndef SHARED
191 # ifdef __UCLIBC_HAS_SSP__
192     /* Set up the stack checker's canary.  */
193     stack_chk_guard = _dl_setup_stack_chk_guard();
194 #  ifdef THREAD_SET_STACK_GUARD
195     THREAD_SET_STACK_GUARD (stack_chk_guard);
196 #   ifdef __UCLIBC_HAS_SSP_COMPAT__
197     __guard = stack_chk_guard;
198 #   endif
199 #  else
200     __stack_chk_guard = stack_chk_guard;
201 #   if !defined __HAVE_SHARED__ && defined __UCLIBC_HAS_SSP_COMPAT__
202      __guard = stack_chk_guard;
203 #   endif
204 #  endif
205 # endif
206 #endif
207
208 #ifdef __UCLIBC_HAS_LOCALE__
209     /* Initialize the global locale structure. */
210     if (likely(_locale_init!=NULL))
211         _locale_init();
212 #endif
213
214     /*
215      * Initialize stdio here.  In the static library case, this will
216      * be bypassed if not needed because of the weak alias above.
217      * Thus we get a nice size savings because the stdio functions
218      * won't be pulled into the final static binary unless used.
219      */
220     if (likely(_stdio_init != NULL))
221         _stdio_init();
222
223 }
224 libc_hidden_def(__uClibc_init)
225
226 #ifdef __UCLIBC_CTOR_DTOR__
227 void attribute_hidden (*__app_fini)(void) = NULL;
228 #endif
229
230 void attribute_hidden (*__rtld_fini)(void) = NULL;
231
232 extern void __uClibc_fini(void);
233 libc_hidden_proto(__uClibc_fini)
234 void __uClibc_fini(void)
235 {
236 #ifdef __UCLIBC_CTOR_DTOR__
237     /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array finalisation is handled
238      * by __app_fini.  */
239 # ifdef SHARED
240     _dl_app_fini_array();
241 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
242     size_t i = __fini_array_end - __fini_array_start;
243     while (i-- > 0)
244         (*__fini_array_start [i]) ();
245 # endif
246     if (__app_fini != NULL)
247         (__app_fini)();
248 #endif
249     if (__rtld_fini != NULL)
250         (__rtld_fini)();
251 }
252 libc_hidden_def(__uClibc_fini)
253
254 /* __uClibc_main is the new main stub for uClibc. This function is
255  * called from crt1 (version 0.9.28 or newer), after ALL shared libraries
256  * are initialized, just before we call the application's main function.
257  */
258 void __uClibc_main(int (*main)(int, char **, char **), int argc,
259                     char **argv, void (*app_init)(void), void (*app_fini)(void),
260                     void (*rtld_fini)(void), void *stack_end) attribute_noreturn;
261 void __uClibc_main(int (*main)(int, char **, char **), int argc,
262                     char **argv, void (*app_init)(void), void (*app_fini)(void),
263                     void (*rtld_fini)(void), void *stack_end)
264 {
265 #ifndef __ARCH_HAS_NO_LDSO__
266     unsigned long *aux_dat;
267     ElfW(auxv_t) auxvt[AT_EGID + 1];
268 #endif
269
270 #ifndef SHARED
271     __libc_stack_end = stack_end;
272 #endif
273
274     __rtld_fini = rtld_fini;
275
276     /* The environment begins right after argv.  */
277     __environ = &argv[argc + 1];
278
279     /* If the first thing after argv is the arguments
280      * the the environment is empty. */
281     if ((char *) __environ == *argv) {
282         /* Make __environ point to the NULL at argv[argc] */
283         __environ = &argv[argc];
284     }
285
286 #ifndef __ARCH_HAS_NO_LDSO__
287     /* Pull stuff from the ELF header when possible */
288     memset(auxvt, 0x00, sizeof(auxvt));
289     aux_dat = (unsigned long*)__environ;
290     while (*aux_dat) {
291         aux_dat++;
292     }
293     aux_dat++;
294     while (*aux_dat) {
295         ElfW(auxv_t) *auxv_entry = (ElfW(auxv_t) *) aux_dat;
296         if (auxv_entry->a_type <= AT_EGID) {
297             memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(ElfW(auxv_t)));
298         }
299         aux_dat += 2;
300     }
301 #ifndef SHARED
302     /* Get the program headers (_dl_phdr) from the aux vector
303        It will be used into __libc_setup_tls. */
304
305     _dl_aux_init (auxvt);
306 #endif
307 #endif
308
309     /* We need to initialize uClibc.  If we are dynamically linked this
310      * may have already been completed by the shared lib loader.  We call
311      * __uClibc_init() regardless, to be sure the right thing happens. */
312     __uClibc_init();
313
314 #ifndef __ARCH_HAS_NO_LDSO__
315     /* Make certain getpagesize() gives the correct answer */
316     __pagesize = (auxvt[AT_PAGESZ].a_un.a_val)? auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
317
318     /* Prevent starting SUID binaries where the stdin. stdout, and
319      * stderr file descriptors are not already opened. */
320     if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && __check_suid()) ||
321             (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
322             (auxvt[AT_UID].a_un.a_val != auxvt[AT_EUID].a_un.a_val ||
323              auxvt[AT_GID].a_un.a_val != auxvt[AT_EGID].a_un.a_val)))
324     {
325         __check_one_fd (STDIN_FILENO, O_RDONLY | O_NOFOLLOW);
326         __check_one_fd (STDOUT_FILENO, O_RDWR | O_NOFOLLOW);
327         __check_one_fd (STDERR_FILENO, O_RDWR | O_NOFOLLOW);
328     }
329 #endif
330
331     __uclibc_progname = *argv;
332 #ifdef __UCLIBC_HAS_PROGRAM_INVOCATION_NAME__
333     if (*argv != NULL) {
334         program_invocation_name = *argv;
335         program_invocation_short_name = strrchr(*argv, '/');
336         if (program_invocation_short_name != NULL)
337             ++program_invocation_short_name;
338         else
339             program_invocation_short_name = program_invocation_name;
340     }
341 #endif
342
343 #ifdef __UCLIBC_CTOR_DTOR__
344     /* Arrange for the application's dtors to run before we exit.  */
345     __app_fini = app_fini;
346
347     /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
348      * by __app_init.  */
349 # if !defined (SHARED) && !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
350     /* For dynamically linked executables the preinit array is executed by
351        the dynamic linker (before initializing any shared object).
352        For static executables, preinit happens rights before init.  */
353     {
354         const size_t size = __preinit_array_end - __preinit_array_start;
355         size_t i;
356         for (i = 0; i < size; i++)
357             (*__preinit_array_start [i]) ();
358     }
359 # endif
360     /* Run all the application's ctors now.  */
361     if (app_init!=NULL) {
362         app_init();
363     }
364     /* If __UCLIBC_FORMAT_SHARED_FLAT__, all array initialisation is handled
365      * by __app_init.  */
366 # ifdef SHARED
367     _dl_app_init_array();
368 # elif !defined (__UCLIBC_FORMAT_SHARED_FLAT__)
369     {
370         const size_t size = __init_array_end - __init_array_start;
371         size_t i;
372         for (i = 0; i < size; i++)
373             (*__init_array_start [i]) ();
374     }
375 # endif
376 #endif
377
378     /* Note: It is possible that any initialization done above could
379      * have resulted in errno being set nonzero, so set it to 0 before
380      * we call main.
381      */
382     if (likely(__errno_location!=NULL))
383         *(__errno_location()) = 0;
384
385     /* Set h_errno to 0 as well */
386     if (likely(__h_errno_location!=NULL))
387         *(__h_errno_location()) = 0;
388
389     /*
390      * Finally, invoke application's main and then exit.
391      */
392     exit(main(argc, argv, __environ));
393 }
394
395 #if defined(__UCLIBC_HAS_THREADS__) && !defined(SHARED)
396 /* Weaks for internal library use only.
397  *
398  * We need to define weaks here to cover all the pthread functions that
399  * libc itself will use so that we aren't forced to link libc against
400  * libpthread.  This file is only used in libc.a and since we have
401  * weaks here, they will be automatically overridden by libpthread.a
402  * if it gets linked in.
403  */
404
405 static int __pthread_return_0 (void) { return 0; }
406 static void __pthread_return_void (void) { return; }
407
408 weak_alias (__pthread_return_0, __pthread_mutex_init)
409 weak_alias (__pthread_return_0, __pthread_mutex_lock)
410 weak_alias (__pthread_return_0, __pthread_mutex_trylock)
411 weak_alias (__pthread_return_0, __pthread_mutex_unlock)
412 weak_alias (__pthread_return_void, _pthread_cleanup_push_defer)
413 weak_alias (__pthread_return_void, _pthread_cleanup_pop_restore)
414 # ifdef __UCLIBC_HAS_THREADS_NATIVE__
415 weak_alias (__pthread_return_0, __pthread_mutexattr_init)
416 weak_alias (__pthread_return_0, __pthread_mutexattr_destroy)
417 weak_alias (__pthread_return_0, __pthread_mutexattr_settype)
418 # endif
419 #endif