gelfshdr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * gelfshdr.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: gelfshdr.c,v 1.10 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_Shdr*
  34. gelf_getshdr(Elf_Scn *scn, GElf_Shdr *dst) {
  35. GElf_Shdr buf;
  36. if (!scn) {
  37. return NULL;
  38. }
  39. elf_assert(scn->s_magic == SCN_MAGIC);
  40. elf_assert(scn->s_elf);
  41. elf_assert(scn->s_elf->e_magic == ELF_MAGIC);
  42. if (!dst) {
  43. dst = &buf;
  44. }
  45. if (scn->s_elf->e_class == ELFCLASS64) {
  46. *dst = scn->s_shdr64;
  47. }
  48. else if (scn->s_elf->e_class == ELFCLASS32) {
  49. Elf32_Shdr *src = &scn->s_shdr32;
  50. check_and_copy(GElf_Word, dst, src, sh_name, NULL);
  51. check_and_copy(GElf_Word, dst, src, sh_type, NULL);
  52. check_and_copy(GElf_Xword, dst, src, sh_flags, NULL);
  53. check_and_copy(GElf_Addr, dst, src, sh_addr, NULL);
  54. check_and_copy(GElf_Off, dst, src, sh_offset, NULL);
  55. check_and_copy(GElf_Xword, dst, src, sh_size, NULL);
  56. check_and_copy(GElf_Word, dst, src, sh_link, NULL);
  57. check_and_copy(GElf_Word, dst, src, sh_info, NULL);
  58. check_and_copy(GElf_Xword, dst, src, sh_addralign, NULL);
  59. check_and_copy(GElf_Xword, dst, src, sh_entsize, NULL);
  60. }
  61. else {
  62. if (valid_class(scn->s_elf->e_class)) {
  63. seterr(ERROR_UNIMPLEMENTED);
  64. }
  65. else {
  66. seterr(ERROR_UNKNOWN_CLASS);
  67. }
  68. return NULL;
  69. }
  70. if (dst == &buf) {
  71. dst = (GElf_Shdr*)malloc(sizeof(GElf_Shdr));
  72. if (!dst) {
  73. seterr(ERROR_MEM_SHDR);
  74. return NULL;
  75. }
  76. *dst = buf;
  77. }
  78. return dst;
  79. }
  80. int
  81. gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *src) {
  82. if (!scn || !src) {
  83. return 0;
  84. }
  85. elf_assert(scn->s_magic == SCN_MAGIC);
  86. elf_assert(scn->s_elf);
  87. elf_assert(scn->s_elf->e_magic == ELF_MAGIC);
  88. if (scn->s_elf->e_class == ELFCLASS64) {
  89. scn->s_shdr64 = *src;
  90. }
  91. else if (scn->s_elf->e_class == ELFCLASS32) {
  92. Elf32_Shdr *dst = &scn->s_shdr32;
  93. check_and_copy(Elf32_Word, dst, src, sh_name, 0);
  94. check_and_copy(Elf32_Word, dst, src, sh_type, 0);
  95. check_and_copy(Elf32_Word, dst, src, sh_flags, 0);
  96. check_and_copy(Elf32_Addr, dst, src, sh_addr, 0);
  97. check_and_copy(Elf32_Off, dst, src, sh_offset, 0);
  98. check_and_copy(Elf32_Word, dst, src, sh_size, 0);
  99. check_and_copy(Elf32_Word, dst, src, sh_link, 0);
  100. check_and_copy(Elf32_Word, dst, src, sh_info, 0);
  101. check_and_copy(Elf32_Word, dst, src, sh_addralign, 0);
  102. check_and_copy(Elf32_Word, dst, src, sh_entsize, 0);
  103. }
  104. else {
  105. if (valid_class(scn->s_elf->e_class)) {
  106. seterr(ERROR_UNIMPLEMENTED);
  107. }
  108. else {
  109. seterr(ERROR_UNKNOWN_CLASS);
  110. }
  111. return 0;
  112. }
  113. return 1;
  114. }
  115. #endif /* __LIBELF64 */