1
0

gelfphdr.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * gelfphdr.c - gelf_* translation functions.
  3. * Copyright (C) 2000 - 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. #if __LIBELF64
  21. #ifndef lint
  22. static const char rcsid[] = "@(#) $Id: gelfphdr.c,v 1.9 2008/05/23 08:15:34 michael Exp $";
  23. #endif /* lint */
  24. #define check_and_copy(type, d, s, name, eret) \
  25. do { \
  26. if (sizeof((d)->name) < sizeof((s)->name) \
  27. && (type)(s)->name != (s)->name) { \
  28. seterr(ERROR_BADVALUE); \
  29. return (eret); \
  30. } \
  31. (d)->name = (type)(s)->name; \
  32. } while (0)
  33. GElf_Phdr*
  34. gelf_getphdr(Elf *elf, int ndx, GElf_Phdr *dst) {
  35. GElf_Phdr buf;
  36. char *tmp;
  37. size_t n;
  38. if (!elf) {
  39. return NULL;
  40. }
  41. elf_assert(elf->e_magic == ELF_MAGIC);
  42. tmp = _elf_getphdr(elf, elf->e_class);
  43. if (!tmp) {
  44. return NULL;
  45. }
  46. if (ndx < 0 || ndx >= elf->e_phnum) {
  47. seterr(ERROR_BADINDEX);
  48. return NULL;
  49. }
  50. n = _msize(elf->e_class, _elf_version, ELF_T_PHDR);
  51. if (n == 0) {
  52. seterr(ERROR_UNIMPLEMENTED);
  53. return NULL;
  54. }
  55. if (!dst) {
  56. dst = &buf;
  57. }
  58. if (elf->e_class == ELFCLASS64) {
  59. *dst = *(Elf64_Phdr*)(tmp + ndx * n);
  60. }
  61. else if (elf->e_class == ELFCLASS32) {
  62. Elf32_Phdr *src = (Elf32_Phdr*)(tmp + ndx * n);
  63. check_and_copy(GElf_Word, dst, src, p_type, NULL);
  64. check_and_copy(GElf_Word, dst, src, p_flags, NULL);
  65. check_and_copy(GElf_Off, dst, src, p_offset, NULL);
  66. check_and_copy(GElf_Addr, dst, src, p_vaddr, NULL);
  67. check_and_copy(GElf_Addr, dst, src, p_paddr, NULL);
  68. check_and_copy(GElf_Xword, dst, src, p_filesz, NULL);
  69. check_and_copy(GElf_Xword, dst, src, p_memsz, NULL);
  70. check_and_copy(GElf_Xword, dst, src, p_align, NULL);
  71. }
  72. else {
  73. if (valid_class(elf->e_class)) {
  74. seterr(ERROR_UNIMPLEMENTED);
  75. }
  76. else {
  77. seterr(ERROR_UNKNOWN_CLASS);
  78. }
  79. return NULL;
  80. }
  81. if (dst == &buf) {
  82. dst = (GElf_Phdr*)malloc(sizeof(GElf_Phdr));
  83. if (!dst) {
  84. seterr(ERROR_MEM_PHDR);
  85. return NULL;
  86. }
  87. *dst = buf;
  88. }
  89. return dst;
  90. }
  91. int
  92. gelf_update_phdr(Elf *elf, int ndx, GElf_Phdr *src) {
  93. char *tmp;
  94. size_t n;
  95. if (!elf || !src) {
  96. return 0;
  97. }
  98. elf_assert(elf->e_magic == ELF_MAGIC);
  99. tmp = _elf_getphdr(elf, elf->e_class);
  100. if (!tmp) {
  101. return 0;
  102. }
  103. if (ndx < 0 || ndx >= elf->e_phnum) {
  104. seterr(ERROR_BADINDEX);
  105. return 0;
  106. }
  107. n = _msize(elf->e_class, _elf_version, ELF_T_PHDR);
  108. if (n == 0) {
  109. seterr(ERROR_UNIMPLEMENTED);
  110. return 0;
  111. }
  112. if (elf->e_class == ELFCLASS64) {
  113. *(Elf64_Phdr*)(tmp + ndx * n) = *src;
  114. }
  115. else if (elf->e_class == ELFCLASS32) {
  116. Elf32_Phdr *dst = (Elf32_Phdr*)(tmp + ndx * n);
  117. check_and_copy(Elf32_Word, dst, src, p_type, 0);
  118. check_and_copy(Elf32_Off, dst, src, p_offset, 0);
  119. check_and_copy(Elf32_Addr, dst, src, p_vaddr, 0);
  120. check_and_copy(Elf32_Addr, dst, src, p_paddr, 0);
  121. check_and_copy(Elf32_Word, dst, src, p_filesz, 0);
  122. check_and_copy(Elf32_Word, dst, src, p_memsz, 0);
  123. check_and_copy(Elf32_Word, dst, src, p_flags, 0);
  124. check_and_copy(Elf32_Word, dst, src, p_align, 0);
  125. }
  126. else {
  127. if (valid_class(elf->e_class)) {
  128. seterr(ERROR_UNIMPLEMENTED);
  129. }
  130. else {
  131. seterr(ERROR_UNKNOWN_CLASS);
  132. }
  133. return 0;
  134. }
  135. return 1;
  136. }
  137. #endif /* __LIBELF64 */