1
0

x.remscn.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. x.remscn.c - implementation of the elfx_remscn(3) function.
  3. Copyright (C) 1995 - 2001, 2003 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: x.remscn.c,v 1.15 2008/05/23 08:15:35 michael Exp $";
  19. #endif /* lint */
  20. size_t
  21. elfx_remscn(Elf *elf, Elf_Scn *scn) {
  22. Elf_Scn *pscn;
  23. Scn_Data *sd;
  24. Scn_Data *tmp;
  25. size_t index;
  26. if (!elf || !scn) {
  27. return SHN_UNDEF;
  28. }
  29. elf_assert(elf->e_magic == ELF_MAGIC);
  30. if (elf->e_kind != ELF_K_ELF) {
  31. seterr(ERROR_NOTELF);
  32. return SHN_UNDEF;
  33. }
  34. elf_assert(scn->s_magic == SCN_MAGIC);
  35. elf_assert(elf->e_ehdr);
  36. if (scn->s_elf != elf) {
  37. seterr(ERROR_ELFSCNMISMATCH);
  38. return SHN_UNDEF;
  39. }
  40. elf_assert(elf->e_scn_1);
  41. if (scn == elf->e_scn_1) {
  42. seterr(ERROR_NULLSCN);
  43. return SHN_UNDEF;
  44. }
  45. /*
  46. * Find previous section.
  47. */
  48. for (pscn = elf->e_scn_1; pscn->s_link; pscn = pscn->s_link) {
  49. if (pscn->s_link == scn) {
  50. break;
  51. }
  52. }
  53. if (pscn->s_link != scn) {
  54. seterr(ERROR_ELFSCNMISMATCH);
  55. return SHN_UNDEF;
  56. }
  57. /*
  58. * Unlink section.
  59. */
  60. if (elf->e_scn_n == scn) {
  61. elf->e_scn_n = pscn;
  62. }
  63. pscn->s_link = scn->s_link;
  64. index = scn->s_index;
  65. /*
  66. * Free section descriptor and data.
  67. */
  68. for (sd = scn->s_data_1; sd; sd = tmp) {
  69. elf_assert(sd->sd_magic == DATA_MAGIC);
  70. elf_assert(sd->sd_scn == scn);
  71. tmp = sd->sd_link;
  72. if (sd->sd_free_data && sd->sd_memdata) {
  73. free(sd->sd_memdata);
  74. }
  75. if (sd->sd_freeme) {
  76. free(sd);
  77. }
  78. }
  79. if ((sd = scn->s_rawdata)) {
  80. elf_assert(sd->sd_magic == DATA_MAGIC);
  81. elf_assert(sd->sd_scn == scn);
  82. if (sd->sd_free_data && sd->sd_memdata) {
  83. free(sd->sd_memdata);
  84. }
  85. if (sd->sd_freeme) {
  86. free(sd);
  87. }
  88. }
  89. if (scn->s_freeme) {
  90. elf_assert(scn->s_index > 0);
  91. free(scn);
  92. }
  93. /*
  94. * Adjust section indices.
  95. */
  96. for (scn = pscn->s_link; scn; scn = scn->s_link) {
  97. elf_assert(scn->s_index > index);
  98. scn->s_index--;
  99. }
  100. /*
  101. * Adjust section count in ELF header
  102. */
  103. if (_elf_update_shnum(elf, elf->e_scn_n->s_index + 1)) {
  104. return SHN_UNDEF;
  105. }
  106. return index;
  107. }