opt.delscn.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. opt.delscn.c - implementation of the elf_delscn(3) function.
  3. Copyright (C) 1995 - 2001 Michael Riepe
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public
  6. License as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  15. */
  16. #include <private.h>
  17. #ifndef lint
  18. static const char rcsid[] = "@(#) $Id: opt.delscn.c,v 1.12 2008/05/23 08:15:35 michael Exp $";
  19. #endif /* lint */
  20. static size_t
  21. _newindex(size_t old, size_t index) {
  22. return old == index ? SHN_UNDEF : (old > index ? old - 1 : old);
  23. }
  24. static void
  25. _elf32_update_shdr(Elf *elf, size_t index) {
  26. Elf32_Shdr *shdr;
  27. Elf_Scn *scn;
  28. ((Elf32_Ehdr*)elf->e_ehdr)->e_shnum = elf->e_scn_n->s_index + 1;
  29. for (scn = elf->e_scn_1; scn; scn = scn->s_link) {
  30. shdr = &scn->s_shdr32;
  31. switch (shdr->sh_type) {
  32. case SHT_REL:
  33. case SHT_RELA:
  34. shdr->sh_info = _newindex(shdr->sh_info, index);
  35. /* fall through */
  36. case SHT_DYNSYM:
  37. case SHT_DYNAMIC:
  38. case SHT_HASH:
  39. case SHT_SYMTAB:
  40. #if __LIBELF_SYMBOL_VERSIONS
  41. #if __LIBELF_SUN_SYMBOL_VERSIONS
  42. case SHT_SUNW_verdef:
  43. case SHT_SUNW_verneed:
  44. case SHT_SUNW_versym:
  45. #else /* __LIBELF_SUN_SYMBOL_VERSIONS */
  46. case SHT_GNU_verdef:
  47. case SHT_GNU_verneed:
  48. case SHT_GNU_versym:
  49. #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */
  50. #endif /* __LIBELF_SYMBOL_VERSIONS */
  51. shdr->sh_link = _newindex(shdr->sh_link, index);
  52. /* fall through */
  53. default:
  54. break;
  55. }
  56. }
  57. }
  58. #if __LIBELF64
  59. static void
  60. _elf64_update_shdr(Elf *elf, size_t index) {
  61. Elf64_Shdr *shdr;
  62. Elf_Scn *scn;
  63. ((Elf64_Ehdr*)elf->e_ehdr)->e_shnum = elf->e_scn_n->s_index + 1;
  64. for (scn = elf->e_scn_1; scn; scn = scn->s_link) {
  65. shdr = &scn->s_shdr64;
  66. switch (shdr->sh_type) {
  67. case SHT_REL:
  68. case SHT_RELA:
  69. shdr->sh_info = _newindex(shdr->sh_info, index);
  70. /* fall through */
  71. case SHT_DYNSYM:
  72. case SHT_DYNAMIC:
  73. case SHT_HASH:
  74. case SHT_SYMTAB:
  75. #if __LIBELF_SYMBOL_VERSIONS
  76. #if __LIBELF_SUN_SYMBOL_VERSIONS
  77. case SHT_SUNW_verdef:
  78. case SHT_SUNW_verneed:
  79. case SHT_SUNW_versym:
  80. #else /* __LIBELF_SUN_SYMBOL_VERSIONS */
  81. case SHT_GNU_verdef:
  82. case SHT_GNU_verneed:
  83. case SHT_GNU_versym:
  84. #endif /* __LIBELF_SUN_SYMBOL_VERSIONS */
  85. #endif /* __LIBELF_SYMBOL_VERSIONS */
  86. shdr->sh_link = _newindex(shdr->sh_link, index);
  87. /* fall through */
  88. default:
  89. break;
  90. }
  91. }
  92. }
  93. #endif /* __LIBELF64 */
  94. size_t
  95. elf_delscn(Elf *elf, Elf_Scn *scn) {
  96. Elf_Scn *pscn;
  97. Scn_Data *sd;
  98. Scn_Data *tmp;
  99. size_t index;
  100. if (!elf || !scn) {
  101. return SHN_UNDEF;
  102. }
  103. elf_assert(elf->e_magic == ELF_MAGIC);
  104. elf_assert(scn->s_magic == SCN_MAGIC);
  105. elf_assert(elf->e_ehdr);
  106. if (scn->s_elf != elf) {
  107. seterr(ERROR_ELFSCNMISMATCH);
  108. return SHN_UNDEF;
  109. }
  110. elf_assert(elf->e_scn_1);
  111. if (scn == elf->e_scn_1) {
  112. seterr(ERROR_NULLSCN);
  113. return SHN_UNDEF;
  114. }
  115. /*
  116. * Find previous section.
  117. */
  118. for (pscn = elf->e_scn_1; pscn->s_link; pscn = pscn->s_link) {
  119. if (pscn->s_link == scn) {
  120. break;
  121. }
  122. }
  123. if (pscn->s_link != scn) {
  124. seterr(ERROR_ELFSCNMISMATCH);
  125. return SHN_UNDEF;
  126. }
  127. /*
  128. * Unlink section.
  129. */
  130. if (elf->e_scn_n == scn) {
  131. elf->e_scn_n = pscn;
  132. }
  133. pscn->s_link = scn->s_link;
  134. index = scn->s_index;
  135. /*
  136. * Free section descriptor and data.
  137. */
  138. for (sd = scn->s_data_1; sd; sd = tmp) {
  139. elf_assert(sd->sd_magic == DATA_MAGIC);
  140. elf_assert(sd->sd_scn == scn);
  141. tmp = sd->sd_link;
  142. if (sd->sd_free_data && sd->sd_memdata) {
  143. free(sd->sd_memdata);
  144. }
  145. if (sd->sd_freeme) {
  146. free(sd);
  147. }
  148. }
  149. if ((sd = scn->s_rawdata)) {
  150. elf_assert(sd->sd_magic == DATA_MAGIC);
  151. elf_assert(sd->sd_scn == scn);
  152. if (sd->sd_free_data && sd->sd_memdata) {
  153. free(sd->sd_memdata);
  154. }
  155. if (sd->sd_freeme) {
  156. free(sd);
  157. }
  158. }
  159. if (scn->s_freeme) {
  160. elf_assert(scn->s_index > 0);
  161. free(scn);
  162. }
  163. /*
  164. * Adjust section indices.
  165. */
  166. for (scn = pscn->s_link; scn; scn = scn->s_link) {
  167. elf_assert(scn->s_index > index);
  168. scn->s_index--;
  169. }
  170. /*
  171. * Adjust ELF header and well-known section headers.
  172. */
  173. if (elf->e_class == ELFCLASS32) {
  174. _elf32_update_shdr(elf, index);
  175. return index;
  176. }
  177. #if __LIBELF64
  178. else if (elf->e_class == ELFCLASS64) {
  179. _elf64_update_shdr(elf, index);
  180. return index;
  181. }
  182. #endif /* __LIBELF64 */
  183. else if (valid_class(elf->e_class)) {
  184. seterr(ERROR_UNIMPLEMENTED);
  185. }
  186. else {
  187. seterr(ERROR_UNKNOWN_CLASS);
  188. }
  189. return SHN_UNDEF;
  190. }