dl-array.c 2.9 KB

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