Update.
[glibc.git] / elf / dl-init.c
blobc4b1e2215200de4e5a52b4561de7a8daefd9e34d
1 /* Return the next shared object initializer function not yet run.
2 Copyright (C) 1995, 1996, 1998, 1999 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 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.
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.
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. */
20 #include <stddef.h>
21 #include <elf/ldsodefs.h>
24 /* Run initializers for MAP and its dependencies, in inverse dependency
25 order (that is, leaf nodes first). */
27 ElfW(Addr)
28 internal_function
29 _dl_init_next (struct r_scope_elem *searchlist)
31 unsigned int i;
33 /* The search list for symbol lookup is a flat list in top-down
34 dependency order, so processing that list from back to front gets us
35 breadth-first leaf-to-root order. */
37 i = searchlist->r_nlist;
38 while (i-- > 0)
40 struct link_map *l = searchlist->r_list[i];
41 ElfW(Addr) *array;
43 if (l->l_init_called)
44 /* This object is all done. */
45 continue;
47 /* Check for object which constructors we do not run here.
48 XXX Maybe this should be pre-computed, but where? */
49 if (l->l_name[0] == '\0' && l->l_type == lt_executable)
51 l->l_init_called = 1;
52 continue;
55 /* Account for running next constructor. */
56 ++l->l_runcount;
58 if (l->l_runcount == 1)
60 /* Try running the DT_INIT constructor. */
61 if (l->l_info[DT_INIT])
63 /* Print a debug message if wanted. */
64 if (_dl_debug_impcalls)
65 _dl_debug_message (1, "\ncalling init: ",
66 l->l_name[0] ? l->l_name : _dl_argv[0],
67 "\n\n", NULL);
69 return l->l_addr + l->l_info[DT_INIT]->d_un.d_ptr;
72 /* No DT_INIT, so go on with the array. */
73 ++l->l_runcount;
76 if (l->l_runcount > l->l_initcount)
78 /* That were all of the constructors. */
79 l->l_runcount = 0;
80 l->l_init_called = 1;
81 continue;
84 /* Print a debug message if wanted. */
85 if (_dl_debug_impcalls && l->l_info[DT_INIT] == NULL
86 && l->l_runcount == 2)
87 _dl_debug_message (1, "\ncalling init: ",
88 l->l_name[0] ? l->l_name : _dl_argv[0],
89 "\n\n", NULL);
91 array = (ElfW(Addr) *) l->l_info[DT_INIT_ARRAY]->d_un.d_ptr;
92 return l->l_addr + array[l->l_runcount - 2];
93 /* NOTREACHED */
97 /* Notify the debugger all new objects are now ready to go. */
98 _r_debug.r_state = RT_CONSISTENT;
99 _dl_debug_state ();
101 return 0;