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