^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) Paul Mackerras 1997.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stddef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "elf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "page.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "string.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "stdio.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int parse_elf64(void *hdr, struct elf_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) Elf64_Ehdr *elf64 = hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) Elf64_Phdr *elf64ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) elf64->e_ident[EI_MAG1] == ELFMAG1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) elf64->e_ident[EI_MAG2] == ELFMAG2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) elf64->e_ident[EI_MAG3] == ELFMAG3 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) elf64->e_ident[EI_CLASS] == ELFCLASS64 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #ifdef __LITTLE_ENDIAN__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) elf64->e_ident[EI_DATA] == ELFDATA2LSB &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) elf64->e_ident[EI_DATA] == ELFDATA2MSB &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) (elf64->e_type == ET_EXEC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) elf64->e_type == ET_DYN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) elf64->e_machine == EM_PPC64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) elf64ph = (Elf64_Phdr *)((unsigned long)elf64 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (unsigned long)elf64->e_phoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (elf64ph->p_type == PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (i >= (unsigned int)elf64->e_phnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) info->loadsize = (unsigned long)elf64ph->p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) info->memsize = (unsigned long)elf64ph->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) info->elfoffset = (unsigned long)elf64ph->p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int parse_elf32(void *hdr, struct elf_info *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) Elf32_Ehdr *elf32 = hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) Elf32_Phdr *elf32ph;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) elf32->e_ident[EI_MAG1] == ELFMAG1 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) elf32->e_ident[EI_MAG2] == ELFMAG2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) elf32->e_ident[EI_MAG3] == ELFMAG3 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) elf32->e_ident[EI_CLASS] == ELFCLASS32 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) elf32->e_ident[EI_DATA] == ELFDATA2MSB &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) (elf32->e_type == ET_EXEC ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) elf32->e_type == ET_DYN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) elf32->e_machine == EM_PPC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for (i = 0; i < elf32->e_phnum; i++, elf32ph++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (elf32ph->p_type == PT_LOAD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (i >= elf32->e_phnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) info->loadsize = elf32ph->p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) info->memsize = elf32ph->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) info->elfoffset = elf32ph->p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }