OSDN Git Service

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