OSDN Git Service

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