dl-array.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * This file contains the helper routines to run init and fini functions.
  3. *
  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
  7. *
  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.
  16. *
  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.
  28. */
  29. #include "ldso.h"
  30. static void _dl_run_array_forward(unsigned long array, unsigned long size,
  31. DL_LOADADDR_TYPE loadaddr)
  32. {
  33. if (array != 0) {
  34. unsigned int j;
  35. unsigned int jm;
  36. ElfW(Addr) *addrs;
  37. jm = size / sizeof (ElfW(Addr));
  38. addrs = (ElfW(Addr) *) DL_RELOC_ADDR(loadaddr, array);
  39. for (j = 0; j < jm; ++j) {
  40. void (*dl_elf_func) (void);
  41. dl_elf_func = (void (*)(void)) (intptr_t) addrs[j];
  42. DL_CALL_FUNC_AT_ADDR (dl_elf_func, loadaddr, (void (*)(void)));
  43. }
  44. }
  45. }
  46. void _dl_run_init_array(struct elf_resolve *tpnt);
  47. void _dl_run_init_array(struct elf_resolve *tpnt)
  48. {
  49. _dl_run_array_forward(tpnt->dynamic_info[DT_INIT_ARRAY],
  50. tpnt->dynamic_info[DT_INIT_ARRAYSZ],
  51. tpnt->loadaddr);
  52. }
  53. void _dl_app_init_array(void);
  54. void _dl_app_init_array(void)
  55. {
  56. _dl_run_init_array(_dl_loaded_modules);
  57. }
  58. void _dl_run_fini_array(struct elf_resolve *tpnt);
  59. void _dl_run_fini_array(struct elf_resolve *tpnt)
  60. {
  61. if (tpnt->dynamic_info[DT_FINI_ARRAY]) {
  62. ElfW(Addr) *array = (ElfW(Addr) *) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI_ARRAY]);
  63. unsigned int i = (tpnt->dynamic_info[DT_FINI_ARRAYSZ] / sizeof(ElfW(Addr)));
  64. while (i-- > 0) {
  65. void (*dl_elf_func) (void);
  66. dl_elf_func = (void (*)(void)) (intptr_t) array[i];
  67. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void (*)(void)));
  68. }
  69. }
  70. }
  71. void _dl_app_fini_array(void);
  72. void _dl_app_fini_array(void)
  73. {
  74. _dl_run_fini_array(_dl_loaded_modules);
  75. }