Update copyright notices with scripts/update-copyrights.
[glibc.git] / elf / dl-init.c
blobfe4d2a07dade4c9fab1ecebefeb28ca8f2fa1f71
1 /* Run initializers for newly loaded objects.
2 Copyright (C) 1995-2013 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, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stddef.h>
20 #include <ldsodefs.h>
23 /* Type of the initializer. */
24 typedef void (*init_t) (int, char **, char **);
26 #ifndef HAVE_INLINED_SYSCALLS
27 /* Flag, nonzero during startup phase. */
28 extern int _dl_starting_up;
29 extern int _dl_starting_up_internal attribute_hidden;
30 #endif
33 static void
34 call_init (struct link_map *l, int argc, char **argv, char **env)
36 if (l->l_init_called)
37 /* This object is all done. */
38 return;
40 /* Avoid handling this constructor again in case we have a circular
41 dependency. */
42 l->l_init_called = 1;
44 /* Check for object which constructors we do not run here. */
45 if (__builtin_expect (l->l_name[0], 'a') == '\0'
46 && l->l_type == lt_executable)
47 return;
49 /* Are there any constructors? */
50 if (l->l_info[DT_INIT] == NULL
51 && __builtin_expect (l->l_info[DT_INIT_ARRAY] == NULL, 1))
52 return;
54 /* Print a debug message if wanted. */
55 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
56 _dl_debug_printf ("\ncalling init: %s\n\n",
57 l->l_name[0] ? l->l_name : rtld_progname);
59 /* Now run the local constructors. There are two forms of them:
60 - the one named by DT_INIT
61 - the others in the DT_INIT_ARRAY.
63 if (l->l_info[DT_INIT] != NULL)
65 init_t init = (init_t) DL_DT_INIT_ADDRESS
66 (l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr);
68 /* Call the function. */
69 init (argc, argv, env);
72 /* Next see whether there is an array with initialization functions. */
73 ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY];
74 if (init_array != NULL)
76 unsigned int j;
77 unsigned int jm;
78 ElfW(Addr) *addrs;
80 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
82 addrs = (ElfW(Addr) *) (init_array->d_un.d_ptr + l->l_addr);
83 for (j = 0; j < jm; ++j)
84 ((init_t) addrs[j]) (argc, argv, env);
89 void
90 internal_function
91 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
93 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
94 ElfW(Dyn) *preinit_array_size = main_map->l_info[DT_PREINIT_ARRAYSZ];
95 unsigned int i;
97 if (__builtin_expect (GL(dl_initfirst) != NULL, 0))
99 call_init (GL(dl_initfirst), argc, argv, env);
100 GL(dl_initfirst) = NULL;
103 /* Don't do anything if there is no preinit array. */
104 if (__builtin_expect (preinit_array != NULL, 0)
105 && preinit_array_size != NULL
106 && (i = preinit_array_size->d_un.d_val / sizeof (ElfW(Addr))) > 0)
108 ElfW(Addr) *addrs;
109 unsigned int cnt;
111 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS, 0))
112 _dl_debug_printf ("\ncalling preinit: %s\n\n",
113 main_map->l_name[0]
114 ? main_map->l_name : rtld_progname);
116 addrs = (ElfW(Addr) *) (preinit_array->d_un.d_ptr + main_map->l_addr);
117 for (cnt = 0; cnt < i; ++cnt)
118 ((init_t) addrs[cnt]) (argc, argv, env);
121 /* Stupid users forced the ELF specification to be changed. It now
122 says that the dynamic loader is responsible for determining the
123 order in which the constructors have to run. The constructors
124 for all dependencies of an object must run before the constructor
125 for the object itself. Circular dependencies are left unspecified.
127 This is highly questionable since it puts the burden on the dynamic
128 loader which has to find the dependencies at runtime instead of
129 letting the user do it right. Stupidity rules! */
131 i = main_map->l_searchlist.r_nlist;
132 while (i-- > 0)
133 call_init (main_map->l_initfini[i], argc, argv, env);
135 #ifndef HAVE_INLINED_SYSCALLS
136 /* Finished starting up. */
137 INTUSE(_dl_starting_up) = 0;
138 #endif
140 INTDEF (_dl_init)