end.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * end.c - implementation of the elf_end(3) function.
  3. * Copyright (C) 1995 - 2004 Michael Riepe
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Library General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This 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.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  18. */
  19. #include <private.h>
  20. #ifndef lint
  21. static const char rcsid[] = "@(#) $Id: end.c,v 1.12 2008/05/23 08:15:34 michael Exp $";
  22. #endif /* lint */
  23. #if HAVE_MMAP
  24. #include <sys/mman.h>
  25. #endif /* HAVE_MMAP */
  26. static void
  27. _elf_free(void *ptr) {
  28. if (ptr) {
  29. free(ptr);
  30. }
  31. }
  32. static void
  33. _elf_free_scns(Elf *elf, Elf_Scn *scn) {
  34. Scn_Data *sd, *tmp;
  35. Elf_Scn *freescn;
  36. for (freescn = NULL; scn; scn = scn->s_link) {
  37. elf_assert(scn->s_magic == SCN_MAGIC);
  38. elf_assert(scn->s_elf == elf);
  39. for (sd = scn->s_data_1; sd; sd = tmp) {
  40. elf_assert(sd->sd_magic == DATA_MAGIC);
  41. elf_assert(sd->sd_scn == scn);
  42. tmp = sd->sd_link;
  43. if (sd->sd_free_data) {
  44. _elf_free(sd->sd_memdata);
  45. }
  46. if (sd->sd_freeme) {
  47. free(sd);
  48. }
  49. }
  50. if ((sd = scn->s_rawdata)) {
  51. elf_assert(sd->sd_magic == DATA_MAGIC);
  52. elf_assert(sd->sd_scn == scn);
  53. if (sd->sd_free_data) {
  54. _elf_free(sd->sd_memdata);
  55. }
  56. if (sd->sd_freeme) {
  57. free(sd);
  58. }
  59. }
  60. if (scn->s_freeme) {
  61. _elf_free(freescn);
  62. freescn = scn;
  63. }
  64. }
  65. _elf_free(freescn);
  66. }
  67. int
  68. elf_end(Elf *elf) {
  69. Elf **siblings;
  70. if (!elf) {
  71. return 0;
  72. }
  73. elf_assert(elf->e_magic == ELF_MAGIC);
  74. if (--elf->e_count) {
  75. return elf->e_count;
  76. }
  77. if (elf->e_parent) {
  78. elf_assert(elf->e_parent->e_magic == ELF_MAGIC);
  79. elf_assert(elf->e_parent->e_kind == ELF_K_AR);
  80. siblings = &elf->e_parent->e_members;
  81. while (*siblings) {
  82. if (*siblings == elf) {
  83. *siblings = elf->e_link;
  84. break;
  85. }
  86. siblings = &(*siblings)->e_link;
  87. }
  88. elf_end(elf->e_parent);
  89. _elf_free(elf->e_arhdr);
  90. }
  91. #if HAVE_MMAP
  92. else if (elf->e_unmap_data) {
  93. munmap(elf->e_data, elf->e_size);
  94. }
  95. #endif /* HAVE_MMAP */
  96. else if (!elf->e_memory) {
  97. _elf_free(elf->e_data);
  98. }
  99. _elf_free_scns(elf, elf->e_scn_1);
  100. if (elf->e_rawdata != elf->e_data) {
  101. _elf_free(elf->e_rawdata);
  102. }
  103. if (elf->e_free_syms) {
  104. _elf_free(elf->e_symtab);
  105. }
  106. _elf_free(elf->e_ehdr);
  107. _elf_free(elf->e_phdr);
  108. free(elf);
  109. return 0;
  110. }