elf_util.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (C) Paul Mackerras 1997.
  3. *
  4. * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <stdarg.h>
  12. #include <stddef.h>
  13. #include "elf.h"
  14. #include "page.h"
  15. #include "string.h"
  16. #include "stdio.h"
  17. int parse_elf64(void *hdr, struct elf_info *info)
  18. {
  19. Elf64_Ehdr *elf64 = hdr;
  20. Elf64_Phdr *elf64ph;
  21. unsigned int i;
  22. if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 &&
  23. elf64->e_ident[EI_MAG1] == ELFMAG1 &&
  24. elf64->e_ident[EI_MAG2] == ELFMAG2 &&
  25. elf64->e_ident[EI_MAG3] == ELFMAG3 &&
  26. elf64->e_ident[EI_CLASS] == ELFCLASS64 &&
  27. #ifdef __LITTLE_ENDIAN__
  28. elf64->e_ident[EI_DATA] == ELFDATA2LSB &&
  29. #else
  30. elf64->e_ident[EI_DATA] == ELFDATA2MSB &&
  31. #endif
  32. (elf64->e_type == ET_EXEC ||
  33. elf64->e_type == ET_DYN) &&
  34. elf64->e_machine == EM_PPC64))
  35. return 0;
  36. elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
  37. (unsigned long)elf64->e_phoff);
  38. for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
  39. if (elf64ph->p_type == PT_LOAD)
  40. break;
  41. if (i >= (unsigned int)elf64->e_phnum)
  42. return 0;
  43. info->loadsize = (unsigned long)elf64ph->p_filesz;
  44. info->memsize = (unsigned long)elf64ph->p_memsz;
  45. info->elfoffset = (unsigned long)elf64ph->p_offset;
  46. return 1;
  47. }
  48. int parse_elf32(void *hdr, struct elf_info *info)
  49. {
  50. Elf32_Ehdr *elf32 = hdr;
  51. Elf32_Phdr *elf32ph;
  52. unsigned int i;
  53. if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 &&
  54. elf32->e_ident[EI_MAG1] == ELFMAG1 &&
  55. elf32->e_ident[EI_MAG2] == ELFMAG2 &&
  56. elf32->e_ident[EI_MAG3] == ELFMAG3 &&
  57. elf32->e_ident[EI_CLASS] == ELFCLASS32 &&
  58. elf32->e_ident[EI_DATA] == ELFDATA2MSB &&
  59. (elf32->e_type == ET_EXEC ||
  60. elf32->e_type == ET_DYN) &&
  61. elf32->e_machine == EM_PPC))
  62. return 0;
  63. elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
  64. for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
  65. if (elf32ph->p_type == PT_LOAD)
  66. break;
  67. if (i >= elf32->e_phnum)
  68. return 0;
  69. info->loadsize = elf32ph->p_filesz;
  70. info->memsize = elf32ph->p_memsz;
  71. info->elfoffset = elf32ph->p_offset;
  72. return 1;
  73. }