Relocate hppa from ports to libc.
[glibc.git] / elf / dl-init.c
blob598df3b4aad5d01ccaf1967e439f979ad29c7094
1 /* Run initializers for newly loaded objects.
2 Copyright (C) 1995-2014 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 (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
56 _dl_debug_printf ("\ncalling init: %s\n\n",
57 DSO_FILENAME (l->l_name));
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)
64 DL_CALL_DT_INIT(l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr, argc, argv, env);
66 /* Next see whether there is an array with initialization functions. */
67 ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY];
68 if (init_array != NULL)
70 unsigned int j;
71 unsigned int jm;
72 ElfW(Addr) *addrs;
74 jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr));
76 addrs = (ElfW(Addr) *) (init_array->d_un.d_ptr + l->l_addr);
77 for (j = 0; j < jm; ++j)
78 ((init_t) addrs[j]) (argc, argv, env);
83 void
84 internal_function
85 _dl_init (struct link_map *main_map, int argc, char **argv, char **env)
87 ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY];
88 ElfW(Dyn) *preinit_array_size = main_map->l_info[DT_PREINIT_ARRAYSZ];
89 unsigned int i;
91 if (__glibc_unlikely (GL(dl_initfirst) != NULL))
93 call_init (GL(dl_initfirst), argc, argv, env);
94 GL(dl_initfirst) = NULL;
97 /* Don't do anything if there is no preinit array. */
98 if (__builtin_expect (preinit_array != NULL, 0)
99 && preinit_array_size != NULL
100 && (i = preinit_array_size->d_un.d_val / sizeof (ElfW(Addr))) > 0)
102 ElfW(Addr) *addrs;
103 unsigned int cnt;
105 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS))
106 _dl_debug_printf ("\ncalling preinit: %s\n\n",
107 DSO_FILENAME (main_map->l_name));
109 addrs = (ElfW(Addr) *) (preinit_array->d_un.d_ptr + main_map->l_addr);
110 for (cnt = 0; cnt < i; ++cnt)
111 ((init_t) addrs[cnt]) (argc, argv, env);
114 /* Stupid users forced the ELF specification to be changed. It now
115 says that the dynamic loader is responsible for determining the
116 order in which the constructors have to run. The constructors
117 for all dependencies of an object must run before the constructor
118 for the object itself. Circular dependencies are left unspecified.
120 This is highly questionable since it puts the burden on the dynamic
121 loader which has to find the dependencies at runtime instead of
122 letting the user do it right. Stupidity rules! */
124 i = main_map->l_searchlist.r_nlist;
125 while (i-- > 0)
126 call_init (main_map->l_initfini[i], argc, argv, env);
128 #ifndef HAVE_INLINED_SYSCALLS
129 /* Finished starting up. */
130 INTUSE(_dl_starting_up) = 0;
131 #endif
133 INTDEF (_dl_init)