]> sourceware.org Git - glibc.git/blob - elf/dl-init.c
Update.
[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 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
31 void
32 internal_function
33 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
34 {
35 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
36 struct r_debug *r;
37 unsigned int i;
38
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)
42 {
43 ElfW(Addr) *addrs;
44 unsigned int cnt;
45
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);
50
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);
55 }
56
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 ();
63
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.
69
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! */
73
74 i = main_map->l_searchlist.r_nlist;
75 while (i-- > 0)
76 {
77 struct link_map *l = main_map->l_initfini[i];
78 init_t init;
79
80 if (l->l_init_called)
81 /* This object is all done. */
82 continue;
83
84 /* Avoid handling this constructor again in case we have a circular
85 dependency. */
86 l->l_init_called = 1;
87
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;
91
92 /* Are there any constructors? */
93 if (l->l_info[DT_INIT] == NULL && l->l_info[DT_INIT_ARRAY] == NULL)
94 continue;
95
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);
101
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.
105 */
106 if (l->l_info[DT_INIT] != NULL)
107 {
108 init = (init_t) DL_DT_INIT_ADDRESS
109 (l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr);
110
111 /* Call the function. */
112 init (argc, argv, env);
113 }
114
115 /* Next see whether there is an array with initialization functions. */
116 if (l->l_info[DT_INIT_ARRAY] != NULL)
117 {
118 unsigned int j;
119 unsigned int jm;
120 ElfW(Addr) *addrs;
121
122 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
123
124 addrs = (ElfW(Addr) *) (l->l_info[DT_INIT_ARRAY]->d_un.d_ptr
125 + l->l_addr);
126 for (j = 0; j < jm; ++j)
127 ((init_t) addrs[j]) (argc, argv, env);
128 }
129 }
130
131 /* Notify the debugger all new objects are now ready to go. */
132 r->r_state = RT_CONSISTENT;
133 _dl_debug_state ();
134
135 /* Finished starting up. */
136 _dl_starting_up = 0;
137 }
This page took 0.048979 seconds and 6 git commands to generate.