]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-init.c
Replace _dl_debug_* variables with _dl_debug_mask.
[thirdparty/glibc.git] / elf / dl-init.c
1 /* Return the next shared object initializer function not yet run.
2 Copyright (C) 1995,1996,1998,1999,2000,2001 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 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.
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 Library General Public License for more details.
14
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. */
19
20 #include <stddef.h>
21 #include <ldsodefs.h>
22
23
24 /* Type of the initializer. */
25 typedef void (*init_t) (int, char **, char **);
26
27 /* Flag, nonzero during startup phase. */
28 extern int _dl_starting_up;
29
30 /* The object to be initialized first. */
31 extern struct link_map *_dl_initfirst;
32
33
34 static void
35 call_init (struct link_map *l, int argc, char **argv, char **env)
36 {
37 if (l->l_init_called)
38 /* This object is all done. */
39 return;
40
41 /* Avoid handling this constructor again in case we have a circular
42 dependency. */
43 l->l_init_called = 1;
44
45 /* Check for object which constructors we do not run here. */
46 if (__builtin_expect (l->l_name[0], 'a') == '\0'
47 && l->l_type == lt_executable)
48 return;
49
50 /* Are there any constructors? */
51 if (l->l_info[DT_INIT] == NULL
52 && __builtin_expect (l->l_info[DT_INIT_ARRAY] == NULL, 1))
53 return;
54
55 /* Print a debug message if wanted. */
56 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_IMPCALLS, 0))
57 _dl_debug_message (1, "\ncalling init: ",
58 l->l_name[0] ? l->l_name : _dl_argv[0], "\n\n", NULL);
59
60 /* Now run the local constructors. There are two forms of them:
61 - the one named by DT_INIT
62 - the others in the DT_INIT_ARRAY.
63 */
64 if (l->l_info[DT_INIT] != NULL)
65 {
66 init_t init = (init_t) DL_DT_INIT_ADDRESS
67 (l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr);
68
69 /* Call the function. */
70 init (argc, argv, env);
71 }
72
73 /* Next see whether there is an array with initialization functions. */
74 if (l->l_info[DT_INIT_ARRAY] != NULL)
75 {
76 unsigned int j;
77 unsigned int jm;
78 ElfW(Addr) *addrs;
79
80 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
81
82 addrs = (ElfW(Addr) *) (l->l_info[DT_INIT_ARRAY]->d_un.d_ptr
83 + l->l_addr);
84 for (j = 0; j < jm; ++j)
85 ((init_t) addrs[j]) (argc, argv, env);
86 }
87 }
88
89
90 void
91 internal_function
92 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
93 {
94 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
95 struct r_debug *r;
96 unsigned int i;
97
98 if (__builtin_expect (_dl_initfirst != NULL, 0))
99 {
100 call_init (_dl_initfirst, argc, argv, env);
101 _dl_initfirst = NULL;
102 }
103
104 /* Don't do anything if there is no preinit array. */
105 if (__builtin_expect (preinit_array != NULL, 0)
106 && (i = preinit_array->d_un.d_val / sizeof (ElfW(Addr))) > 0)
107 {
108 ElfW(Addr) *addrs;
109 unsigned int cnt;
110
111 if (__builtin_expect (_dl_debug_mask & DL_DEBUG_IMPCALLS, 0))
112 _dl_debug_message (1, "\ncalling preinit: ",
113 main_map->l_name[0]
114 ? main_map->l_name : _dl_argv[0], "\n\n", NULL);
115
116 addrs = (ElfW(Addr) *) (main_map->l_info[DT_PREINIT_ARRAY]->d_un.d_ptr
117 + main_map->l_addr);
118 for (cnt = 0; cnt < i; ++cnt)
119 ((init_t) addrs[cnt]) (argc, argv, env);
120 }
121
122 /* Notify the debugger we have added some objects. We need to call
123 _dl_debug_initialize in a static program in case dynamic linking has
124 not been used before. */
125 r = _dl_debug_initialize (0);
126 r->r_state = RT_ADD;
127 _dl_debug_state ();
128
129 /* Stupid users forced the ELF specification to be changed. It now
130 says that the dynamic loader is responsible for determining the
131 order in which the constructors have to run. The constructors
132 for all dependencies of an object must run before the constructor
133 for the object itself. Circular dependencies are left unspecified.
134
135 This is highly questionable since it puts the burden on the dynamic
136 loader which has to find the dependencies at runtime instead of
137 letting the user do it right. Stupidity rules! */
138
139 i = main_map->l_searchlist.r_nlist;
140 while (i-- > 0)
141 call_init (main_map->l_initfini[i], argc, argv, env);
142
143 /* Notify the debugger all new objects are now ready to go. */
144 r->r_state = RT_CONSISTENT;
145 _dl_debug_state ();
146
147 /* Finished starting up. */
148 _dl_starting_up = 0;
149 }