* sysdeps/generic/libc-start.c (__libc_start_main): Do
[platform/upstream/glibc.git] / sysdeps / unix / sysv / linux / init-first.c
1 /* Initialization code run first thing by the ELF startup code.  Linux version.
2    Copyright (C) 1995-1999,2000,01,02 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sysdep.h>
25 #include <fpu_control.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <libc-internal.h>
29
30 #include <ldsodefs.h>
31
32 /* The function is called from assembly stubs the compiler can't see.  */
33 static void init (int, char **, char **) __attribute__ ((unused));
34
35 /* Set nonzero if we have to be prepared for more then one libc being
36    used in the process.  Safe assumption if initializer never runs.  */
37 int __libc_multiple_libcs attribute_hidden = 1;
38
39 /* Remember the command line argument and enviroment contents for
40    later calls of initializers for dynamic libraries.  */
41 int __libc_argc attribute_hidden;
42 char **__libc_argv attribute_hidden;
43
44
45 static void
46 init (int argc, char **argv, char **envp)
47 {
48 #ifdef USE_NONOPTION_FLAGS
49   extern void __getopt_clean_environment (char **);
50 #endif
51
52   __libc_multiple_libcs = &_dl_starting_up && !_dl_starting_up;
53
54   /* Make sure we don't initialize twice.  */
55   if (!__libc_multiple_libcs)
56     {
57       /* Set the FPU control word to the proper default value if the
58          kernel would use a different value.  (In a static program we
59          don't have this information.)  */
60 #ifdef SHARED
61       if (__fpu_control != GL(dl_fpu_control))
62 #endif
63         __setfpucw (__fpu_control);
64     }
65
66   /* Save the command-line arguments.  */
67   __libc_argc = argc;
68   __libc_argv = argv;
69   __environ = envp;
70
71 #ifndef SHARED
72   __libc_init_secure ();
73
74   /* First the initialization which normally would be done by the
75      dynamic linker.  */
76   _dl_non_dynamic_init ();
77 #endif
78
79   __init_misc (argc, argv, envp);
80
81 #ifdef USE_NONOPTION_FLAGS
82   /* This is a hack to make the special getopt in GNU libc working.  */
83   __getopt_clean_environment (envp);
84 #endif
85
86 #ifdef SHARED
87   __libc_global_ctors ();
88 #endif
89 }
90
91 #ifdef SHARED
92
93 strong_alias (init, _init);
94
95 extern void __libc_init_first (void);
96
97 void
98 __libc_init_first (void)
99 {
100 }
101
102 #else
103 extern void __libc_init_first (int argc, char **argv, char **envp);
104
105 void
106 __libc_init_first (int argc, char **argv, char **envp)
107 {
108   init (argc, argv, envp);
109 }
110 #endif
111
112
113 /* This function is defined here so that if this file ever gets into
114    ld.so we will get a link error.  Having this file silently included
115    in ld.so causes disaster, because the _init definition above will
116    cause ld.so to gain an init function, which is not a cool thing. */
117
118 extern void _dl_start (void) __attribute__ ((noreturn));
119
120 void
121 _dl_start (void)
122 {
123   abort ();
124 }