]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/dl-init.c
ffd05b78069432d9140df1b0bbecfa3dad116e3d
[thirdparty/glibc.git] / elf / dl-init.c
1 /* Run initializers for newly loaded objects.
2 Copyright (C) 1995-2023 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, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <assert.h>
20 #include <stddef.h>
21 #include <ldsodefs.h>
22 #include <elf-initfini.h>
23
24 struct link_map *_dl_init_called_list;
25
26 static void
27 call_init (struct link_map *l, int argc, char **argv, char **env)
28 {
29 /* Do not run constructors for proxy objects. */
30 if (l != l->l_real)
31 return;
32
33 /* If the object has not been relocated, this is a bug. The
34 function pointers are invalid in this case. (Executables do not
35 need relocation.) */
36 assert (l->l_relocated || l->l_type == lt_executable);
37
38 if (l->l_init_called)
39 /* This object is all done. */
40 return;
41
42 /* Avoid handling this constructor again in case we have a circular
43 dependency. */
44 l->l_init_called = 1;
45
46 /* Help an already-running dlclose: The just-loaded object must not
47 be removed during the current pass. (No effect if no dlclose in
48 progress.) */
49 l->l_map_used = 1;
50
51 /* Record execution before starting any initializers. This way, if
52 the initializers themselves call dlopen, their ELF destructors
53 will eventually be run before this object is destructed, matching
54 that their ELF constructors have run before this object was
55 constructed. _dl_fini uses this list for audit callbacks, so
56 register objects on the list even if they do not have a
57 constructor. */
58 l->l_init_called_next = _dl_init_called_list;
59 _dl_init_called_list = l;
60
61 /* Check for object which constructors we do not run here. */
62 if (__builtin_expect (l->l_name[0], 'a') == '\0'
63 && l->l_type == lt_executable)
64 return;
65
66 /* Print a debug message if wanted. */
67 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
68 _dl_debug_printf ("\ncalling init: %s\n\n",
69 DSO_FILENAME (l->l_name));
70
71 /* Now run the local constructors. There are two forms of them:
72 - the one named by DT_INIT
73 - the others in the DT_INIT_ARRAY.
74 */
75 if (ELF_INITFINI && l->l_info[DT_INIT] != NULL)
76 DL_CALL_DT_INIT(l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr, argc, argv, env);
77
78 /* Next see whether there is an array with initialization functions. */
79 ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY];
80 if (init_array != NULL)
81 {
82 unsigned int j;
83 unsigned int jm;
84 ElfW(Addr) *addrs;
85
86 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
87
88 addrs = (ElfW(Addr) *) (init_array->d_un.d_ptr + l->l_addr);
89 for (j = 0; j < jm; ++j)
90 ((dl_init_t) addrs[j]) (argc, argv, env);
91 }
92 }
93
94
95 void
96 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
97 {
98 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
99 ElfW(Dyn) *preinit_array_size = main_map->l_info[DT_PREINIT_ARRAYSZ];
100 unsigned int i;
101
102 if (__glibc_unlikely (GL(dl_initfirst) != NULL))
103 {
104 call_init (GL(dl_initfirst), argc, argv, env);
105 GL(dl_initfirst) = NULL;
106 }
107
108 /* Don't do anything if there is no preinit array. */
109 if (__builtin_expect (preinit_array != NULL, 0)
110 && preinit_array_size != NULL
111 && (i = preinit_array_size->d_un.d_val / sizeof (ElfW(Addr))) > 0)
112 {
113 ElfW(Addr) *addrs;
114 unsigned int cnt;
115
116 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
117 _dl_debug_printf ("\ncalling preinit: %s\n\n",
118 DSO_FILENAME (main_map->l_name));
119
120 addrs = (ElfW(Addr) *) (preinit_array->d_un.d_ptr + main_map->l_addr);
121 for (cnt = 0; cnt < i; ++cnt)
122 ((dl_init_t) addrs[cnt]) (argc, argv, env);
123 }
124
125 /* Stupid users forced the ELF specification to be changed. It now
126 says that the dynamic loader is responsible for determining the
127 order in which the constructors have to run. The constructors
128 for all dependencies of an object must run before the constructor
129 for the object itself. Circular dependencies are left unspecified.
130
131 This is highly questionable since it puts the burden on the dynamic
132 loader which has to find the dependencies at runtime instead of
133 letting the user do it right. Stupidity rules! */
134
135 i = main_map->l_searchlist.r_nlist;
136 while (i-- > 0)
137 call_init (main_map->l_initfini[i], argc, argv, env);
138
139 #ifndef HAVE_INLINED_SYSCALLS
140 /* Finished starting up. */
141 _dl_starting_up = 0;
142 #endif
143 }