Update.
[glibc.git] / elf / dl-init.c
blob683b94b9c777b10f4258b33e6c8150c0992ed1e6
1 /* Return the next shared object initializer function not yet run.
2 Copyright (C) 1995, 1996, 1998, 1999, 2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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;
31 void
32 internal_function
33 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
35 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
36 struct r_debug *r;
37 unsigned int i;
39 /* Don't do anything if there is no preinit array. */
40 if (preinit_array != NULL
41 && (i = preinit_array->d_un.d_val / sizeof (ElfW(Addr))) > 0)
43 ElfW(Addr) *addrs;
44 unsigned int cnt;
46 if (__builtin_expect (_dl_debug_impcalls, 0))
47 _dl_debug_message (1, "\ncalling preinit: ",
48 main_map->l_name[0]
49 ? main_map->l_name : _dl_argv[0], "\n\n", NULL);
51 addrs = (ElfW(Addr) *) (main_map->l_info[DT_PREINIT_ARRAY]->d_un.d_ptr
52 + main_map->l_addr);
53 for (cnt = 0; cnt < i; ++cnt)
54 ((init_t) addrs[cnt]) (argc, argv, env);
57 /* Notify the debugger we have added some objects. We need to call
58 _dl_debug_initialize in a static program in case dynamic linking has
59 not been used before. */
60 r = _dl_debug_initialize (0);
61 r->r_state = RT_ADD;
62 _dl_debug_state ();
64 /* Stupid users forces the ELF specification to be changed. It now
65 says that the dynamic loader is responsible for determining the
66 order in which the constructors have to run. The constructors
67 for all dependencies of an object must run before the constructor
68 for the object itself. Circular dependencies are left unspecified.
70 This is highly questionable since it puts the burden on the dynamic
71 loader which has to find the dependencies at runtime instead of
72 letting the user do it right. Stupidity rules! */
74 i = main_map->l_searchlist.r_nlist;
75 while (i-- > 0)
77 struct link_map *l = main_map->l_initfini[i];
78 init_t init;
80 if (l->l_init_called)
81 /* This object is all done. */
82 continue;
84 /* Avoid handling this constructor again in case we have a circular
85 dependency. */
86 l->l_init_called = 1;
88 /* Check for object which constructors we do not run here. */
89 if (l->l_name[0] == '\0' && l->l_type == lt_executable)
90 continue;
92 /* Are there any constructors? */
93 if (l->l_info[DT_INIT] == NULL && l->l_info[DT_INIT_ARRAY] == NULL)
94 continue;
96 /* Print a debug message if wanted. */
97 if (__builtin_expect (_dl_debug_impcalls, 0))
98 _dl_debug_message (1, "\ncalling init: ",
99 l->l_name[0] ? l->l_name : _dl_argv[0],
100 "\n\n", NULL);
102 /* Now run the local constructors. There are two forms of them:
103 - the one named by DT_INIT
104 - the others in the DT_INIT_ARRAY.
106 if (l->l_info[DT_INIT] != NULL)
108 init = (init_t) (l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr);
110 /* Call the function. */
111 init (argc, argv, env);
114 /* Next see whether there is an array with initialization functions. */
115 if (l->l_info[DT_INIT_ARRAY] != NULL)
117 unsigned int j;
118 unsigned int jm;
119 ElfW(Addr) *addrs;
121 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
123 addrs = (ElfW(Addr) *) (l->l_info[DT_INIT_ARRAY]->d_un.d_ptr
124 + l->l_addr);
125 for (j = 0; j < jm; ++j)
126 ((init_t) addrs[j]) (argc, argv, env);
130 /* Notify the debugger all new objects are now ready to go. */
131 r->r_state = RT_CONSISTENT;
132 _dl_debug_state ();
134 /* Finished starting up. */
135 _dl_starting_up = 0;