xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Run initializers for newly loaded objects. |
| 2 | Copyright (C) 1995-2016 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 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <stddef.h> |
| 20 | #include <ldsodefs.h> |
| 21 | |
| 22 | |
| 23 | /* Type of the initializer. */ |
| 24 | typedef void (*init_t) (int, char **, char **); |
| 25 | |
| 26 | |
| 27 | static void |
| 28 | call_init (struct link_map *l, int argc, char **argv, char **env) |
| 29 | { |
| 30 | if (l->l_init_called) |
| 31 | /* This object is all done. */ |
| 32 | return; |
| 33 | |
| 34 | /* Avoid handling this constructor again in case we have a circular |
| 35 | dependency. */ |
| 36 | l->l_init_called = 1; |
| 37 | |
| 38 | /* Check for object which constructors we do not run here. */ |
| 39 | if (__builtin_expect (l->l_name[0], 'a') == '\0' |
| 40 | && l->l_type == lt_executable) |
| 41 | return; |
| 42 | |
| 43 | /* Are there any constructors? */ |
| 44 | if (l->l_info[DT_INIT] == NULL |
| 45 | && __builtin_expect (l->l_info[DT_INIT_ARRAY] == NULL, 1)) |
| 46 | return; |
| 47 | |
| 48 | /* Print a debug message if wanted. */ |
| 49 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS)) |
| 50 | _dl_debug_printf ("\ncalling init: %s\n\n", |
| 51 | DSO_FILENAME (l->l_name)); |
| 52 | |
| 53 | /* Now run the local constructors. There are two forms of them: |
| 54 | - the one named by DT_INIT |
| 55 | - the others in the DT_INIT_ARRAY. |
| 56 | */ |
| 57 | if (l->l_info[DT_INIT] != NULL) |
| 58 | DL_CALL_DT_INIT(l, l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr, argc, argv, env); |
| 59 | |
| 60 | /* Next see whether there is an array with initialization functions. */ |
| 61 | ElfW(Dyn) *init_array = l->l_info[DT_INIT_ARRAY]; |
| 62 | if (init_array != NULL) |
| 63 | { |
| 64 | unsigned int j; |
| 65 | unsigned int jm; |
| 66 | ElfW(Addr) *addrs; |
| 67 | |
| 68 | jm = l->l_info[DT_INIT_ARRAYSZ]->d_un.d_val / sizeof (ElfW(Addr)); |
| 69 | |
| 70 | addrs = (ElfW(Addr) *) (init_array->d_un.d_ptr + l->l_addr); |
| 71 | for (j = 0; j < jm; ++j) |
| 72 | ((init_t) addrs[j]) (argc, argv, env); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void |
| 78 | internal_function |
| 79 | _dl_init (struct link_map *main_map, int argc, char **argv, char **env) |
| 80 | { |
| 81 | ElfW(Dyn) *preinit_array = main_map->l_info[DT_PREINIT_ARRAY]; |
| 82 | ElfW(Dyn) *preinit_array_size = main_map->l_info[DT_PREINIT_ARRAYSZ]; |
| 83 | unsigned int i; |
| 84 | |
| 85 | if (__glibc_unlikely (GL(dl_initfirst) != NULL)) |
| 86 | { |
| 87 | call_init (GL(dl_initfirst), argc, argv, env); |
| 88 | GL(dl_initfirst) = NULL; |
| 89 | } |
| 90 | |
| 91 | /* Don't do anything if there is no preinit array. */ |
| 92 | if (__builtin_expect (preinit_array != NULL, 0) |
| 93 | && preinit_array_size != NULL |
| 94 | && (i = preinit_array_size->d_un.d_val / sizeof (ElfW(Addr))) > 0) |
| 95 | { |
| 96 | ElfW(Addr) *addrs; |
| 97 | unsigned int cnt; |
| 98 | |
| 99 | if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_IMPCALLS)) |
| 100 | _dl_debug_printf ("\ncalling preinit: %s\n\n", |
| 101 | DSO_FILENAME (main_map->l_name)); |
| 102 | |
| 103 | addrs = (ElfW(Addr) *) (preinit_array->d_un.d_ptr + main_map->l_addr); |
| 104 | for (cnt = 0; cnt < i; ++cnt) |
| 105 | ((init_t) addrs[cnt]) (argc, argv, env); |
| 106 | } |
| 107 | |
| 108 | /* Stupid users forced the ELF specification to be changed. It now |
| 109 | says that the dynamic loader is responsible for determining the |
| 110 | order in which the constructors have to run. The constructors |
| 111 | for all dependencies of an object must run before the constructor |
| 112 | for the object itself. Circular dependencies are left unspecified. |
| 113 | |
| 114 | This is highly questionable since it puts the burden on the dynamic |
| 115 | loader which has to find the dependencies at runtime instead of |
| 116 | letting the user do it right. Stupidity rules! */ |
| 117 | |
| 118 | i = main_map->l_searchlist.r_nlist; |
| 119 | while (i-- > 0) |
| 120 | call_init (main_map->l_initfini[i], argc, argv, env); |
| 121 | |
| 122 | #ifndef HAVE_INLINED_SYSCALLS |
| 123 | /* Finished starting up. */ |
| 124 | _dl_starting_up = 0; |
| 125 | #endif |
| 126 | } |