Update.
[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, 2001, 2002 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 "kernel-features.h"
29 #include <libc-internal.h>
30
31 #include <ldsodefs.h>
32 #ifndef SHARED
33 # include "dl-osinfo.h"
34 #endif
35
36 extern void __libc_init (int, char **, char **);
37
38 /* The function is called from assembly stubs the compiler can't see.  */
39 static void init (int, char **, char **) __attribute__ ((unused));
40
41 extern int _dl_starting_up;
42 weak_extern (_dl_starting_up)
43
44 extern int _dl_fpu_control_set;
45
46 /* Set nonzero if we have to be prepared for more then one libc being
47    used in the process.  Safe assumption if initializer never runs.  */
48 int __libc_multiple_libcs = 1;
49
50 /* Remember the command line argument and enviroment contents for
51    later calls of initializers for dynamic libraries.  */
52 int __libc_argc;
53 char **__libc_argv;
54
55
56 static void
57 init (int argc, char **argv, char **envp)
58 {
59 #ifdef USE_NONOPTION_FLAGS
60   extern void __getopt_clean_environment (char **);
61 #endif
62   /* The next variable is only here to work around a bug in gcc <= 2.7.2.2.
63      If the address would be taken inside the expression the optimizer
64      would try to be too smart and throws it away.  Grrr.  */
65   int *dummy_addr = &_dl_starting_up;
66
67   __libc_multiple_libcs = dummy_addr && !_dl_starting_up;
68
69   /* Make sure we don't initialize twice.  */
70   if (!__libc_multiple_libcs)
71     {
72 #ifndef SHARED
73       DL_SYSDEP_OSCHECK (__libc_fatal);
74 #endif
75
76       /* Set the FPU control word to the proper default value if the
77          kernel would use a different value.  (In a static program we
78          don't have this information.)  */
79 #ifdef SHARED
80       if (__fpu_control != GL(dl_fpu_control))
81 #endif
82         __setfpucw (__fpu_control);
83     }
84
85   /* Save the command-line arguments.  */
86   __libc_argc = argc;
87   __libc_argv = argv;
88   __environ = envp;
89
90 #ifndef SHARED
91   __libc_init_secure ();
92 #endif
93
94   __libc_init (argc, argv, envp);
95
96 #ifdef USE_NONOPTION_FLAGS
97   /* This is a hack to make the special getopt in GNU libc working.  */
98   __getopt_clean_environment (envp);
99 #endif
100
101 #ifdef SHARED
102   __libc_global_ctors ();
103 #endif
104 }
105
106 #ifdef SHARED
107
108 strong_alias (init, _init);
109
110 extern void __libc_init_first (void);
111
112 void
113 __libc_init_first (void)
114 {
115 }
116
117 #else
118 extern void __libc_init_first (int argc, char **argv, char **envp);
119
120 void
121 __libc_init_first (int argc, char **argv, char **envp)
122 {
123   init (argc, argv, envp);
124 }
125 #endif
126
127
128 /* This function is defined here so that if this file ever gets into
129    ld.so we will get a link error.  Having this file silently included
130    in ld.so causes disaster, because the _init definition above will
131    cause ld.so to gain an init function, which is not a cool thing. */
132
133 extern void _dl_start (void) __attribute__ ((noreturn));
134
135 void
136 _dl_start (void)
137 {
138   abort ();
139 }