checksum.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. checksum.c - implementation of the elf{32,64}_checksum(3) functions.
  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: checksum.c,v 1.7 2008/05/23 08:15:34 michael Exp $";
  19. #endif /* lint */
  20. /*
  21. * Compatibility note:
  22. *
  23. * The algorithm used in {elf32,elf64,gelf}_checksum() does not seem to
  24. * be documented. I hope I got it right. My implementation does the
  25. * following:
  26. *
  27. * - skip sections that do not have the SHF_ALLOC flag set
  28. * - skip sections of type SHT_NULL, SHT_NOBITS, SHT_DYNSYM and
  29. * SHT_DYNAMIC
  30. * - add all data bytes from the remaining sections, modulo 2**32
  31. * - add upper and lower half of the result
  32. * - subtract 0xffff if the result is > 0xffff
  33. * - if any error occurs, return 0L
  34. */
  35. static int
  36. skip_section(Elf_Scn *scn, unsigned cls) {
  37. if (cls == ELFCLASS32) {
  38. Elf32_Shdr *shdr = &scn->s_shdr32;
  39. if (!(shdr->sh_flags & SHF_ALLOC)) {
  40. return 1;
  41. }
  42. switch (shdr->sh_type) {
  43. case SHT_NULL:
  44. case SHT_NOBITS:
  45. /* Solaris seems to ignore these, too */
  46. case SHT_DYNSYM:
  47. case SHT_DYNAMIC:
  48. return 1;
  49. }
  50. }
  51. #if __LIBELF64
  52. else if (cls == ELFCLASS64) {
  53. Elf64_Shdr *shdr = &scn->s_shdr64;
  54. if (!(shdr->sh_flags & SHF_ALLOC)) {
  55. return 1;
  56. }
  57. switch (shdr->sh_type) {
  58. case SHT_NULL:
  59. case SHT_NOBITS:
  60. /* Solaris seems to ignore these, too */
  61. case SHT_DYNSYM:
  62. case SHT_DYNAMIC:
  63. return 1;
  64. }
  65. }
  66. #endif /* __LIBELF64 */
  67. else {
  68. seterr(ERROR_UNIMPLEMENTED);
  69. }
  70. return 0;
  71. }
  72. static long
  73. add_bytes(unsigned char *ptr, size_t len) {
  74. long csum = 0;
  75. while (len--) {
  76. csum += *ptr++;
  77. }
  78. return csum;
  79. }
  80. static long
  81. _elf_csum(Elf *elf) {
  82. long csum = 0;
  83. Elf_Data *data;
  84. Elf_Scn *scn;
  85. if (!elf->e_ehdr && !_elf_cook(elf)) {
  86. /* propagate errors from _elf_cook */
  87. return 0L;
  88. }
  89. seterr(0);
  90. for (scn = elf->e_scn_1; scn; scn = scn->s_link) {
  91. if (scn->s_index == SHN_UNDEF || skip_section(scn, elf->e_class)) {
  92. continue;
  93. }
  94. data = NULL;
  95. while ((data = elf_getdata(scn, data))) {
  96. if (data->d_size) {
  97. if (data->d_buf == NULL) {
  98. seterr(ERROR_NULLBUF);
  99. return 0L;
  100. }
  101. csum += add_bytes(data->d_buf, data->d_size);
  102. }
  103. }
  104. }
  105. if (_elf_errno) {
  106. return 0L;
  107. }
  108. csum = (csum & 0xffff) + ((csum >> 16) & 0xffff);
  109. if (csum > 0xffff) {
  110. csum -= 0xffff;
  111. }
  112. return csum;
  113. }
  114. long
  115. elf32_checksum(Elf *elf) {
  116. if (elf) {
  117. if (elf->e_kind != ELF_K_ELF) {
  118. seterr(ERROR_NOTELF);
  119. }
  120. else if (elf->e_class != ELFCLASS32) {
  121. seterr(ERROR_CLASSMISMATCH);
  122. }
  123. else {
  124. return _elf_csum(elf);
  125. }
  126. }
  127. return 0L;
  128. }
  129. #if __LIBELF64
  130. long
  131. elf64_checksum(Elf *elf) {
  132. if (elf) {
  133. if (elf->e_kind != ELF_K_ELF) {
  134. seterr(ERROR_NOTELF);
  135. }
  136. else if (elf->e_class != ELFCLASS64) {
  137. seterr(ERROR_CLASSMISMATCH);
  138. }
  139. else {
  140. return _elf_csum(elf);
  141. }
  142. }
  143. return 0L;
  144. }
  145. long
  146. gelf_checksum(Elf *elf) {
  147. if (elf) {
  148. if (elf->e_kind != ELF_K_ELF) {
  149. seterr(ERROR_NOTELF);
  150. }
  151. else if (!valid_class(elf->e_class)) {
  152. seterr(ERROR_UNKNOWN_CLASS);
  153. }
  154. else {
  155. return _elf_csum(elf);
  156. }
  157. }
  158. return 0L;
  159. }
  160. #endif /* __LIBELF64 */