* NEWS: Added 2.2.6 text from 2.2 branch version.
[glibc.git] / elf / dl-init.c
blob74810feff155f492d173e82a45047caf0a874182
1 /* Return the next shared object initializer function not yet run.
2 Copyright (C) 1995, 1996, 1998-2001, 2002 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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. */
20 #include <stddef.h>
21 #include <ldsodefs.h>
24 /* Type of the initializer. */
25 typedef void (*init_t) (int, char **, char **);
27 /* Flag, nonzero during startup phase. */
28 extern int _dl_starting_up;
29 extern int _dl_starting_up_internal attribute_hidden;
32 static void
33 call_init (struct link_map *l, int argc, char **argv, char **env)
35 if (l->l_init_called)
36 /* This object is all done. */
37 return;
39 /* Avoid handling this constructor again in case we have a circular
40 dependency. */
41 l->l_init_called = 1;
43 /* Check for object which constructors we do not run here. */
44 if (__builtin_expect (l->l_name[0], 'a') == '\0'
45 && l->l_type == lt_executable)
46 return;
48 /* Are there any constructors? */
49 if (l->l_info[DT_INIT] == NULL
50 && __builtin_expect (l->l_info[DT_INIT_ARRAY] == NULL, 1))
51 return;
53 /* Print a debug message if wanted. */
54 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
55 INTUSE(_dl_debug_printf) ("\ncalling init: %s\n\n",
56 l->l_name[0] ? l->l_name : rtld_progname);
58 /* Now run the local constructors. There are two forms of them:
59 - the one named by DT_INIT
60 - the others in the DT_INIT_ARRAY.
62 if (l->l_info[DT_INIT] != NULL)
64 init_t init = (init_t) DL_DT_INIT_ADDRESS
65 (l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr);
67 /* Call the function. */
68 init (argc, argv, env);
71 /* Next see whether there is an array with initialization functions. */
72 if (l->l_info[DT_INIT_ARRAY] != NULL)
74 unsigned int j;
75 unsigned int jm;
76 ElfW(Addr) *addrs;
78 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
80 addrs = (ElfW(Addr) *) (l->l_info[DT_INIT_ARRAY]->d_un.d_ptr
81 + l->l_addr);
82 for (j = 0; j < jm; ++j)
83 ((init_t) addrs[j]) (argc, argv, env);
88 void
89 internal_function
90 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
92 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
93 struct r_debug *r;
94 unsigned int i;
96 if (__builtin_expect (GL(dl_initfirst) != NULL, 0))
98 call_init (GL(dl_initfirst), argc, argv, env);
99 GL(dl_initfirst) = NULL;
102 /* Don't do anything if there is no preinit array. */
103 if (__builtin_expect (preinit_array != NULL, 0)
104 && (i = preinit_array->d_un.d_val / sizeof (ElfW(Addr))) > 0)
106 ElfW(Addr) *addrs;
107 unsigned int cnt;
109 if (__builtin_expect (GL(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
110 INTUSE(_dl_debug_printf) ("\ncalling preinit: %s\n\n",
111 main_map->l_name[0]
112 ? main_map->l_name : rtld_progname);
114 addrs = (ElfW(Addr) *) (main_map->l_info[DT_PREINIT_ARRAY]->d_un.d_ptr
115 + main_map->l_addr);
116 for (cnt = 0; cnt < i; ++cnt)
117 ((init_t) addrs[cnt]) (argc, argv, env);
120 /* Notify the debugger we have added some objects. We need to call
121 _dl_debug_initialize in a static program in case dynamic linking has
122 not been used before. */
123 r = _dl_debug_initialize (0);
124 r->r_state = RT_ADD;
125 INTUSE(_dl_debug_state) ();
127 /* Stupid users forced the ELF specification to be changed. It now
128 says that the dynamic loader is responsible for determining the
129 order in which the constructors have to run. The constructors
130 for all dependencies of an object must run before the constructor
131 for the object itself. Circular dependencies are left unspecified.
133 This is highly questionable since it puts the burden on the dynamic
134 loader which has to find the dependencies at runtime instead of
135 letting the user do it right. Stupidity rules! */
137 i = main_map->l_searchlist.r_nlist;
138 while (i-- > 0)
139 call_init (main_map->l_initfini[i], argc, argv, env);
141 /* Notify the debugger all new objects are now ready to go. */
142 r->r_state = RT_CONSISTENT;
143 INTUSE(_dl_debug_state) ();
145 /* Finished starting up. */
146 INTUSE(_dl_starting_up) = 0;
148 INTDEF (_dl_init)