strptr.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * strptr.c - implementation of the elf_strptr(3) function.
  3. * Copyright (C) 1995 - 2007 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: strptr.c,v 1.12 2008/05/23 08:15:35 michael Exp $";
  22. #endif /* lint */
  23. char*
  24. elf_strptr(Elf *elf, size_t section, size_t offset) {
  25. Elf_Data *data;
  26. Elf_Scn *scn;
  27. size_t n;
  28. char *s;
  29. if (!elf) {
  30. return NULL;
  31. }
  32. elf_assert(elf->e_magic == ELF_MAGIC);
  33. if (!(scn = elf_getscn(elf, section))) {
  34. return NULL;
  35. }
  36. if (scn->s_index == SHN_UNDEF) {
  37. seterr(ERROR_NOSTRTAB);
  38. return NULL;
  39. }
  40. /*
  41. * checking the section header is more appropriate
  42. */
  43. if (elf->e_class == ELFCLASS32) {
  44. if (scn->s_shdr32.sh_type != SHT_STRTAB) {
  45. seterr(ERROR_NOSTRTAB);
  46. return NULL;
  47. }
  48. }
  49. #if __LIBELF64
  50. else if (elf->e_class == ELFCLASS64) {
  51. if (scn->s_shdr64.sh_type != SHT_STRTAB) {
  52. seterr(ERROR_NOSTRTAB);
  53. return NULL;
  54. }
  55. }
  56. #endif /* __LIBELF64 */
  57. else if (valid_class(elf->e_class)) {
  58. seterr(ERROR_UNIMPLEMENTED);
  59. return NULL;
  60. }
  61. else {
  62. seterr(ERROR_UNKNOWN_CLASS);
  63. return NULL;
  64. }
  65. /*
  66. * Find matching buffer
  67. */
  68. n = 0;
  69. data = NULL;
  70. if (elf->e_elf_flags & ELF_F_LAYOUT) {
  71. /*
  72. * Programmer is responsible for d_off
  73. * Note: buffers may be in any order!
  74. */
  75. while ((data = elf_getdata(scn, data))) {
  76. n = data->d_off;
  77. if (offset >= n && offset - n < data->d_size) {
  78. /*
  79. * Found it
  80. */
  81. break;
  82. }
  83. }
  84. }
  85. else {
  86. /*
  87. * Calculate offsets myself
  88. */
  89. while ((data = elf_getdata(scn, data))) {
  90. if (data->d_align > 1) {
  91. n += data->d_align - 1;
  92. n -= n % data->d_align;
  93. }
  94. if (offset < n) {
  95. /*
  96. * Invalid offset: points into a hole
  97. */
  98. seterr(ERROR_BADSTROFF);
  99. return NULL;
  100. }
  101. if (offset - n < data->d_size) {
  102. /*
  103. * Found it
  104. */
  105. break;
  106. }
  107. n += data->d_size;
  108. }
  109. }
  110. if (data == NULL) {
  111. /*
  112. * Not found
  113. */
  114. seterr(ERROR_BADSTROFF);
  115. return NULL;
  116. }
  117. if (data->d_buf == NULL) {
  118. /*
  119. * Buffer is NULL (usually the programmers' fault)
  120. */
  121. seterr(ERROR_NULLBUF);
  122. return NULL;
  123. }
  124. offset -= n;
  125. s = (char*)data->d_buf;
  126. if (!(_elf_sanity_checks & SANITY_CHECK_STRPTR)) {
  127. return s + offset;
  128. }
  129. /*
  130. * Perform extra sanity check
  131. */
  132. for (n = offset; n < data->d_size; n++) {
  133. if (s[n] == '\0') {
  134. /*
  135. * Return properly NUL terminated string
  136. */
  137. return s + offset;
  138. }
  139. }
  140. /*
  141. * String is not NUL terminated
  142. * Return error to avoid SEGV in application
  143. */
  144. seterr(ERROR_UNTERM);
  145. return NULL;
  146. }