x.elfext.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * x.elfext.c -- handle ELF format extensions
  3. * Copyright (C) 2002 - 2006 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: x.elfext.c,v 1.5 2009/07/07 17:57:43 michael Exp $";
  22. #endif /* lint */
  23. int
  24. elf_getphdrnum(Elf *elf, size_t *resultp) {
  25. if (!elf) {
  26. return -1;
  27. }
  28. elf_assert(elf->e_magic == ELF_MAGIC);
  29. if (elf->e_kind != ELF_K_ELF) {
  30. seterr(ERROR_NOTELF);
  31. return -1;
  32. }
  33. if (!elf->e_ehdr && !_elf_cook(elf)) {
  34. return -1;
  35. }
  36. if (resultp) {
  37. *resultp = elf->e_phnum;
  38. }
  39. return 0;
  40. }
  41. int
  42. elf_getshdrnum(Elf *elf, size_t *resultp) {
  43. size_t num = 0;
  44. Elf_Scn *scn;
  45. if (!elf) {
  46. return -1;
  47. }
  48. elf_assert(elf->e_magic == ELF_MAGIC);
  49. if (elf->e_kind != ELF_K_ELF) {
  50. seterr(ERROR_NOTELF);
  51. return -1;
  52. }
  53. if (!elf->e_ehdr && !_elf_cook(elf)) {
  54. return -1;
  55. }
  56. if ((scn = elf->e_scn_n)) {
  57. num = scn->s_index + 1;
  58. }
  59. if (resultp) {
  60. *resultp = num;
  61. }
  62. return 0;
  63. }
  64. int
  65. elf_getshdrstrndx(Elf *elf, size_t *resultp) {
  66. size_t num = 0;
  67. size_t dummy;
  68. Elf_Scn *scn;
  69. if (!elf) {
  70. return -1;
  71. }
  72. elf_assert(elf->e_magic == ELF_MAGIC);
  73. if (resultp == NULL) {
  74. resultp = &dummy; /* handle NULL pointer gracefully */
  75. }
  76. if (elf->e_kind != ELF_K_ELF) {
  77. seterr(ERROR_NOTELF);
  78. return -1;
  79. }
  80. if (!elf->e_ehdr && !_elf_cook(elf)) {
  81. return -1;
  82. }
  83. if (elf->e_class == ELFCLASS32) {
  84. num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx;
  85. }
  86. #if __LIBELF64
  87. else if (elf->e_class == ELFCLASS64) {
  88. num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx;
  89. }
  90. #endif /* __LIBELF64 */
  91. else {
  92. if (valid_class(elf->e_class)) {
  93. seterr(ERROR_UNIMPLEMENTED);
  94. }
  95. else {
  96. seterr(ERROR_UNKNOWN_CLASS);
  97. }
  98. return -1;
  99. }
  100. if (num != SHN_XINDEX) {
  101. *resultp = num;
  102. return 0;
  103. }
  104. /*
  105. * look at first section header
  106. */
  107. if (!(scn = elf->e_scn_1)) {
  108. seterr(ERROR_NOSUCHSCN);
  109. return -1;
  110. }
  111. elf_assert(scn->s_magic == SCN_MAGIC);
  112. #if __LIBELF64
  113. if (elf->e_class == ELFCLASS64) {
  114. *resultp = scn->s_shdr64.sh_link;
  115. return 0;
  116. }
  117. #endif /* __LIBELF64 */
  118. *resultp = scn->s_shdr32.sh_link;
  119. return 0;
  120. }
  121. int
  122. elf_getphnum(Elf *elf, size_t *resultp) {
  123. return elf_getphdrnum(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS;
  124. }
  125. int
  126. elf_getshnum(Elf *elf, size_t *resultp) {
  127. return elf_getshdrnum(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS;
  128. }
  129. int
  130. elf_getshstrndx(Elf *elf, size_t *resultp) {
  131. return elf_getshdrstrndx(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS;
  132. }
  133. int
  134. elfx_update_shstrndx(Elf *elf, size_t value) {
  135. size_t extvalue = 0;
  136. Elf_Scn *scn;
  137. if (!elf) {
  138. return LIBELF_FAILURE;
  139. }
  140. elf_assert(elf->e_magic == ELF_MAGIC);
  141. if (value >= SHN_LORESERVE) {
  142. extvalue = value;
  143. value = SHN_XINDEX;
  144. }
  145. if (elf->e_kind != ELF_K_ELF) {
  146. seterr(ERROR_NOTELF);
  147. return LIBELF_FAILURE;
  148. }
  149. if (!elf->e_ehdr && !_elf_cook(elf)) {
  150. return LIBELF_FAILURE;
  151. }
  152. if (!(scn = _elf_first_scn(elf))) {
  153. return LIBELF_FAILURE;
  154. }
  155. elf_assert(scn->s_magic == SCN_MAGIC);
  156. if (elf->e_class == ELFCLASS32) {
  157. ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx = value;
  158. scn->s_shdr32.sh_link = extvalue;
  159. }
  160. #if __LIBELF64
  161. else if (elf->e_class == ELFCLASS64) {
  162. ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx = value;
  163. scn->s_shdr64.sh_link = extvalue;
  164. }
  165. #endif /* __LIBELF64 */
  166. else {
  167. if (valid_class(elf->e_class)) {
  168. seterr(ERROR_UNIMPLEMENTED);
  169. }
  170. else {
  171. seterr(ERROR_UNKNOWN_CLASS);
  172. }
  173. return LIBELF_FAILURE;
  174. }
  175. elf->e_ehdr_flags |= ELF_F_DIRTY;
  176. scn->s_shdr_flags |= ELF_F_DIRTY;
  177. return LIBELF_SUCCESS;
  178. }