1 /* vi: set sw=4 ts=4: */
2 /*
3  * This file contains the helper routines to run init and fini functions.
4  *
5  * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
6  * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
7  *				David Engel, Hongjiu Lu and Mitch D'Souza
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. The name of the above contributors may not be
15  *    used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 
32 #include "ldso.h"
33 
_dl_run_array_forward(unsigned long array,unsigned long size,DL_LOADADDR_TYPE loadaddr)34 static void _dl_run_array_forward(unsigned long array, unsigned long size,
35                                   DL_LOADADDR_TYPE loadaddr)
36 {
37 	if (array != 0) {
38 		unsigned int j;
39 		unsigned int jm;
40 		ElfW(Addr) *addrs;
41 		jm = size / sizeof (ElfW(Addr));
42 		addrs = (ElfW(Addr) *) DL_RELOC_ADDR(loadaddr, array);
43 		for (j = 0; j < jm; ++j) {
44 			void (*dl_elf_func) (void);
45 			dl_elf_func = (void (*)(void)) (intptr_t) addrs[j];
46 			DL_CALL_FUNC_AT_ADDR (dl_elf_func, loadaddr, (void (*)(void)));
47 		}
48 	}
49 }
50 
51 void _dl_run_init_array(struct elf_resolve *tpnt);
_dl_run_init_array(struct elf_resolve * tpnt)52 void _dl_run_init_array(struct elf_resolve *tpnt)
53 {
54 	_dl_run_array_forward(tpnt->dynamic_info[DT_INIT_ARRAY],
55 			      tpnt->dynamic_info[DT_INIT_ARRAYSZ],
56 			      tpnt->loadaddr);
57 }
58 
59 void _dl_app_init_array(void);
_dl_app_init_array(void)60 void _dl_app_init_array(void)
61 {
62 	_dl_run_init_array(_dl_loaded_modules);
63 }
64 
65 void _dl_run_fini_array(struct elf_resolve *tpnt);
_dl_run_fini_array(struct elf_resolve * tpnt)66 void _dl_run_fini_array(struct elf_resolve *tpnt)
67 {
68 	if (tpnt->dynamic_info[DT_FINI_ARRAY]) {
69 		ElfW(Addr) *array = (ElfW(Addr) *) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI_ARRAY]);
70 		unsigned int i = (tpnt->dynamic_info[DT_FINI_ARRAYSZ] / sizeof(ElfW(Addr)));
71 		while (i-- > 0) {
72 			void (*dl_elf_func) (void);
73 			dl_elf_func = (void (*)(void)) (intptr_t) array[i];
74 			DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void (*)(void)));
75 		}
76 	}
77 }
78 
79 void _dl_app_fini_array(void);
_dl_app_fini_array(void)80 void _dl_app_fini_array(void)
81 {
82 	_dl_run_fini_array(_dl_loaded_modules);
83 }
84 
85