gelfehdr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * gelfehdr.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: gelfehdr.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_Ehdr*
  34. gelf_getehdr(Elf *elf, GElf_Ehdr *dst) {
  35. GElf_Ehdr buf;
  36. char *tmp;
  37. if (!elf) {
  38. return NULL;
  39. }
  40. elf_assert(elf->e_magic == ELF_MAGIC);
  41. tmp = _elf_getehdr(elf, elf->e_class);
  42. if (!tmp) {
  43. return NULL;
  44. }
  45. if (!dst) {
  46. dst = &buf;
  47. }
  48. if (elf->e_class == ELFCLASS64) {
  49. *dst = *(Elf64_Ehdr*)tmp;
  50. }
  51. else if (elf->e_class == ELFCLASS32) {
  52. Elf32_Ehdr *src = (Elf32_Ehdr*)tmp;
  53. memcpy(dst->e_ident, src->e_ident, EI_NIDENT);
  54. check_and_copy(GElf_Half, dst, src, e_type, NULL);
  55. check_and_copy(GElf_Half, dst, src, e_machine, NULL);
  56. check_and_copy(GElf_Word, dst, src, e_version, NULL);
  57. check_and_copy(GElf_Addr, dst, src, e_entry, NULL);
  58. check_and_copy(GElf_Off, dst, src, e_phoff, NULL);
  59. check_and_copy(GElf_Off, dst, src, e_shoff, NULL);
  60. check_and_copy(GElf_Word, dst, src, e_flags, NULL);
  61. check_and_copy(GElf_Half, dst, src, e_ehsize, NULL);
  62. check_and_copy(GElf_Half, dst, src, e_phentsize, NULL);
  63. check_and_copy(GElf_Half, dst, src, e_phnum, NULL);
  64. check_and_copy(GElf_Half, dst, src, e_shentsize, NULL);
  65. check_and_copy(GElf_Half, dst, src, e_shnum, NULL);
  66. check_and_copy(GElf_Half, dst, src, e_shstrndx, NULL);
  67. }
  68. else {
  69. if (valid_class(elf->e_class)) {
  70. seterr(ERROR_UNIMPLEMENTED);
  71. }
  72. else {
  73. seterr(ERROR_UNKNOWN_CLASS);
  74. }
  75. return NULL;
  76. }
  77. if (dst == &buf) {
  78. dst = (GElf_Ehdr*)malloc(sizeof(GElf_Ehdr));
  79. if (!dst) {
  80. seterr(ERROR_MEM_EHDR);
  81. return NULL;
  82. }
  83. *dst = buf;
  84. }
  85. return dst;
  86. }
  87. int
  88. gelf_update_ehdr(Elf *elf, GElf_Ehdr *src) {
  89. char *tmp;
  90. if (!elf || !src) {
  91. return 0;
  92. }
  93. elf_assert(elf->e_magic == ELF_MAGIC);
  94. tmp = _elf_getehdr(elf, elf->e_class);
  95. if (!tmp) {
  96. return 0;
  97. }
  98. if (elf->e_class == ELFCLASS64) {
  99. *(Elf64_Ehdr*)tmp = *src;
  100. }
  101. else if (elf->e_class == ELFCLASS32) {
  102. Elf32_Ehdr *dst = (Elf32_Ehdr*)tmp;
  103. memcpy(dst->e_ident, src->e_ident, EI_NIDENT);
  104. check_and_copy(Elf32_Half, dst, src, e_type, 0);
  105. check_and_copy(Elf32_Half, dst, src, e_machine, 0);
  106. check_and_copy(Elf32_Word, dst, src, e_version, 0);
  107. check_and_copy(Elf32_Addr, dst, src, e_entry, 0);
  108. check_and_copy(Elf32_Off, dst, src, e_phoff, 0);
  109. check_and_copy(Elf32_Off, dst, src, e_shoff, 0);
  110. check_and_copy(Elf32_Word, dst, src, e_flags, 0);
  111. check_and_copy(Elf32_Half, dst, src, e_ehsize, 0);
  112. check_and_copy(Elf32_Half, dst, src, e_phentsize, 0);
  113. check_and_copy(Elf32_Half, dst, src, e_phnum, 0);
  114. check_and_copy(Elf32_Half, dst, src, e_shentsize, 0);
  115. check_and_copy(Elf32_Half, dst, src, e_shnum, 0);
  116. check_and_copy(Elf32_Half, dst, src, e_shstrndx, 0);
  117. }
  118. else {
  119. if (valid_class(elf->e_class)) {
  120. seterr(ERROR_UNIMPLEMENTED);
  121. }
  122. else {
  123. seterr(ERROR_UNKNOWN_CLASS);
  124. }
  125. return 0;
  126. }
  127. return 1;
  128. }
  129. #endif /* __LIBELF64 */