32.newehdr.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * 32.newehdr.c - implementation of the elf{32,64}_newehdr(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.newehdr.c,v 1.16 2008/05/23 08:15:34 michael Exp $";
  22. #endif /* lint */
  23. static char*
  24. _elf_newehdr(Elf *elf, unsigned cls) {
  25. size_t size;
  26. if (!elf) {
  27. return NULL;
  28. }
  29. elf_assert(elf->e_magic == ELF_MAGIC);
  30. if (elf->e_readable) {
  31. return _elf_getehdr(elf, cls);
  32. }
  33. else if (!elf->e_ehdr) {
  34. size = _msize(cls, _elf_version, ELF_T_EHDR);
  35. elf_assert(size);
  36. if ((elf->e_ehdr = (char*)malloc(size))) {
  37. memset(elf->e_ehdr, 0, size);
  38. elf->e_ehdr_flags |= ELF_F_DIRTY;
  39. elf->e_kind = ELF_K_ELF;
  40. elf->e_class = cls;
  41. return elf->e_ehdr;
  42. }
  43. seterr(ERROR_MEM_EHDR);
  44. }
  45. else if (elf->e_class != cls) {
  46. seterr(ERROR_CLASSMISMATCH);
  47. }
  48. else {
  49. elf_assert(elf->e_kind == ELF_K_ELF);
  50. return elf->e_ehdr;
  51. }
  52. return NULL;
  53. }
  54. Elf32_Ehdr*
  55. elf32_newehdr(Elf *elf) {
  56. return (Elf32_Ehdr*)_elf_newehdr(elf, ELFCLASS32);
  57. }
  58. #if __LIBELF64
  59. Elf64_Ehdr*
  60. elf64_newehdr(Elf *elf) {
  61. return (Elf64_Ehdr*)_elf_newehdr(elf, ELFCLASS64);
  62. }
  63. unsigned long
  64. gelf_newehdr(Elf *elf, int cls) {
  65. if (!valid_class(cls) || !_msize(cls, _elf_version, ELF_T_EHDR)) {
  66. seterr(ERROR_UNKNOWN_CLASS);
  67. return 0;
  68. }
  69. return (unsigned long)_elf_newehdr(elf, cls);
  70. }
  71. #endif /* __LIBELF64 */