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