32.fsize.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. 32.fsize.c - implementation of the elf{32,64}_fsize(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. #include <ext_types.h>
  18. #ifndef lint
  19. static const char rcsid[] = "@(#) $Id: 32.fsize.c,v 1.13 2008/05/23 08:15:33 michael Exp $";
  20. #endif /* lint */
  21. const size_t
  22. _elf_fmsize[2][EV_CURRENT - EV_NONE][ELF_T_NUM][2] = {
  23. /* ELFCLASS32 */
  24. {
  25. /* version 1 */
  26. {
  27. { sizeof(unsigned char), sizeof(unsigned char) },
  28. { sizeof(Elf32_Addr), sizeof(__ext_Elf32_Addr) },
  29. { sizeof(Elf32_Dyn), sizeof(__ext_Elf32_Dyn) },
  30. { sizeof(Elf32_Ehdr), sizeof(__ext_Elf32_Ehdr) },
  31. { sizeof(Elf32_Half), sizeof(__ext_Elf32_Half) },
  32. { sizeof(Elf32_Off), sizeof(__ext_Elf32_Off) },
  33. { sizeof(Elf32_Phdr), sizeof(__ext_Elf32_Phdr) },
  34. { sizeof(Elf32_Rela), sizeof(__ext_Elf32_Rela) },
  35. { sizeof(Elf32_Rel), sizeof(__ext_Elf32_Rel) },
  36. { sizeof(Elf32_Shdr), sizeof(__ext_Elf32_Shdr) },
  37. { sizeof(Elf32_Sword), sizeof(__ext_Elf32_Sword) },
  38. { sizeof(Elf32_Sym), sizeof(__ext_Elf32_Sym) },
  39. { sizeof(Elf32_Word), sizeof(__ext_Elf32_Word) },
  40. { 0, 0 }, /* there is no Elf32_Sxword */
  41. { 0, 0 }, /* there is no Elf32_Xword */
  42. /* XXX: check Solaris values */
  43. { 0, 0 }, /* Elf32_Verdef/Verdaux size varies */
  44. { 0, 0 }, /* Elf32_Verneed/Vernaux size varies */
  45. },
  46. },
  47. #if __LIBELF64
  48. /* ELFCLASS64 */
  49. {
  50. /* version 1 */
  51. {
  52. { sizeof(unsigned char), sizeof(unsigned char) },
  53. { sizeof(Elf64_Addr), sizeof(__ext_Elf64_Addr) },
  54. { sizeof(Elf64_Dyn), sizeof(__ext_Elf64_Dyn) },
  55. { sizeof(Elf64_Ehdr), sizeof(__ext_Elf64_Ehdr) },
  56. { sizeof(Elf64_Half), sizeof(__ext_Elf64_Half) },
  57. { sizeof(Elf64_Off), sizeof(__ext_Elf64_Off) },
  58. { sizeof(Elf64_Phdr), sizeof(__ext_Elf64_Phdr) },
  59. { sizeof(Elf64_Rela), sizeof(__ext_Elf64_Rela) },
  60. { sizeof(Elf64_Rel), sizeof(__ext_Elf64_Rel) },
  61. { sizeof(Elf64_Shdr), sizeof(__ext_Elf64_Shdr) },
  62. { sizeof(Elf64_Sword), sizeof(__ext_Elf64_Sword) },
  63. { sizeof(Elf64_Sym), sizeof(__ext_Elf64_Sym) },
  64. { sizeof(Elf64_Word), sizeof(__ext_Elf64_Word) },
  65. { sizeof(Elf64_Sxword), sizeof(__ext_Elf64_Sxword) },
  66. { sizeof(Elf64_Xword), sizeof(__ext_Elf64_Xword) },
  67. /* XXX: check Solaris values */
  68. { 0, 0 }, /* Elf64_Verdef/Verdaux size varies */
  69. { 0, 0 }, /* Elf64_Verneed/Vernaux size varies */
  70. },
  71. },
  72. #endif /* __LIBELF64 */
  73. };
  74. static size_t
  75. _elf_fsize(unsigned cls, Elf_Type type, unsigned ver) {
  76. size_t n = 0;
  77. if (!valid_version(ver)) {
  78. seterr(ERROR_UNKNOWN_VERSION);
  79. }
  80. else if (!valid_type(type)) {
  81. seterr(ERROR_UNKNOWN_TYPE);
  82. }
  83. else if (!(n = _fsize(cls, ver, type))) {
  84. seterr(ERROR_UNKNOWN_TYPE);
  85. }
  86. return n;
  87. }
  88. size_t
  89. elf32_fsize(Elf_Type type, size_t count, unsigned ver) {
  90. return count * _elf_fsize(ELFCLASS32, type, ver);
  91. }
  92. #if __LIBELF64
  93. size_t
  94. elf64_fsize(Elf_Type type, size_t count, unsigned ver) {
  95. return count * _elf_fsize(ELFCLASS64, type, ver);
  96. }
  97. size_t
  98. gelf_fsize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
  99. if (elf) {
  100. if (elf->e_kind != ELF_K_ELF) {
  101. seterr(ERROR_NOTELF);
  102. }
  103. else if (valid_class(elf->e_class)) {
  104. return count * _elf_fsize(elf->e_class, type, ver);
  105. }
  106. else {
  107. seterr(ERROR_UNKNOWN_CLASS);
  108. }
  109. }
  110. return 0;
  111. }
  112. /*
  113. * Extension: report memory size
  114. */
  115. size_t
  116. gelf_msize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
  117. size_t n;
  118. if (elf) {
  119. if (elf->e_kind != ELF_K_ELF) {
  120. seterr(ERROR_NOTELF);
  121. }
  122. else if (!valid_class(elf->e_class)) {
  123. seterr(ERROR_UNKNOWN_CLASS);
  124. }
  125. else if (!valid_version(ver)) {
  126. seterr(ERROR_UNKNOWN_VERSION);
  127. }
  128. else if (!valid_type(type)) {
  129. seterr(ERROR_UNKNOWN_TYPE);
  130. }
  131. else if (!(n = _msize(elf->e_class, ver, type))) {
  132. seterr(ERROR_UNKNOWN_TYPE);
  133. }
  134. else {
  135. return count * n;
  136. }
  137. }
  138. return 0;
  139. }
  140. #endif /* __LIBELF64 */