32.newphdr.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * 32.newphdr.c - implementation of the elf{32,64}_newphdr(3) functions.
  3. * Copyright (C) 1995 - 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. #ifndef lint
  21. static const char rcsid[] = "@(#) $Id: 32.newphdr.c,v 1.16 2008/05/23 08:15:34 michael Exp $";
  22. #endif /* lint */
  23. static char*
  24. _elf_newphdr(Elf *elf, size_t count, unsigned cls) {
  25. size_t extcount = 0;
  26. Elf_Scn *scn = NULL;
  27. char *phdr = NULL;
  28. size_t size;
  29. if (!elf) {
  30. return NULL;
  31. }
  32. elf_assert(elf->e_magic == ELF_MAGIC);
  33. if (!elf->e_ehdr && !elf->e_readable) {
  34. seterr(ERROR_NOEHDR);
  35. }
  36. else if (elf->e_kind != ELF_K_ELF) {
  37. seterr(ERROR_NOTELF);
  38. }
  39. else if (elf->e_class != cls) {
  40. seterr(ERROR_CLASSMISMATCH);
  41. }
  42. else if (elf->e_ehdr || _elf_cook(elf)) {
  43. size = _msize(cls, _elf_version, ELF_T_PHDR);
  44. elf_assert(size);
  45. if (!(scn = _elf_first_scn(elf))) {
  46. return NULL;
  47. }
  48. if (count) {
  49. if (!(phdr = (char*)malloc(count * size))) {
  50. seterr(ERROR_MEM_PHDR);
  51. return NULL;
  52. }
  53. memset(phdr, 0, count * size);
  54. }
  55. elf_assert(elf->e_ehdr);
  56. elf->e_phnum = count;
  57. if (count >= PN_XNUM) {
  58. /*
  59. * get NULL section (create it if necessary)
  60. */
  61. extcount = count;
  62. count = PN_XNUM;
  63. }
  64. if (cls == ELFCLASS32) {
  65. ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum = count;
  66. scn->s_shdr32.sh_info = extcount;
  67. }
  68. #if __LIBELF64
  69. else if (cls == ELFCLASS64) {
  70. ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum = count;
  71. scn->s_shdr64.sh_info = extcount;
  72. }
  73. #endif /* __LIBELF64 */
  74. else {
  75. seterr(ERROR_UNIMPLEMENTED);
  76. if (phdr) {
  77. free(phdr);
  78. }
  79. return NULL;
  80. }
  81. if (elf->e_phdr) {
  82. free(elf->e_phdr);
  83. }
  84. elf->e_phdr = phdr;
  85. elf->e_phdr_flags |= ELF_F_DIRTY;
  86. elf->e_ehdr_flags |= ELF_F_DIRTY;
  87. scn->s_scn_flags |= ELF_F_DIRTY;
  88. return phdr;
  89. }
  90. return NULL;
  91. }
  92. Elf32_Phdr*
  93. elf32_newphdr(Elf *elf, size_t count) {
  94. return (Elf32_Phdr*)_elf_newphdr(elf, count, ELFCLASS32);
  95. }
  96. #if __LIBELF64
  97. Elf64_Phdr*
  98. elf64_newphdr(Elf *elf, size_t count) {
  99. return (Elf64_Phdr*)_elf_newphdr(elf, count, ELFCLASS64);
  100. }
  101. unsigned long
  102. gelf_newphdr(Elf *elf, size_t phnum) {
  103. if (!valid_class(elf->e_class)) {
  104. seterr(ERROR_UNKNOWN_CLASS);
  105. return 0;
  106. }
  107. return (unsigned long)_elf_newphdr(elf, phnum, elf->e_class);
  108. }
  109. #endif /* __LIBELF64 */