1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 288e68731SMark A. Greer /* 388e68731SMark A. Greer * Copyright (C) Paul Mackerras 1997. 488e68731SMark A. Greer * 588e68731SMark A. Greer * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner. 688e68731SMark A. Greer */ 788e68731SMark A. Greer #include <stdarg.h> 888e68731SMark A. Greer #include <stddef.h> 988e68731SMark A. Greer #include "elf.h" 1088e68731SMark A. Greer #include "page.h" 1188e68731SMark A. Greer #include "string.h" 1288e68731SMark A. Greer #include "stdio.h" 1388e68731SMark A. Greer parse_elf64(void * hdr,struct elf_info * info)1488e68731SMark A. Greerint parse_elf64(void *hdr, struct elf_info *info) 1588e68731SMark A. Greer { 1688e68731SMark A. Greer Elf64_Ehdr *elf64 = hdr; 1788e68731SMark A. Greer Elf64_Phdr *elf64ph; 1888e68731SMark A. Greer unsigned int i; 1988e68731SMark A. Greer 2088e68731SMark A. Greer if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 && 2188e68731SMark A. Greer elf64->e_ident[EI_MAG1] == ELFMAG1 && 2288e68731SMark A. Greer elf64->e_ident[EI_MAG2] == ELFMAG2 && 2388e68731SMark A. Greer elf64->e_ident[EI_MAG3] == ELFMAG3 && 2488e68731SMark A. Greer elf64->e_ident[EI_CLASS] == ELFCLASS64 && 25002c39dbSCédric Le Goater #ifdef __LITTLE_ENDIAN__ 26002c39dbSCédric Le Goater elf64->e_ident[EI_DATA] == ELFDATA2LSB && 27002c39dbSCédric Le Goater #else 2888e68731SMark A. Greer elf64->e_ident[EI_DATA] == ELFDATA2MSB && 29002c39dbSCédric Le Goater #endif 30549e8152SPaul Mackerras (elf64->e_type == ET_EXEC || 31549e8152SPaul Mackerras elf64->e_type == ET_DYN) && 3288e68731SMark A. Greer elf64->e_machine == EM_PPC64)) 3388e68731SMark A. Greer return 0; 3488e68731SMark A. Greer 3588e68731SMark A. Greer elf64ph = (Elf64_Phdr *)((unsigned long)elf64 + 3688e68731SMark A. Greer (unsigned long)elf64->e_phoff); 3788e68731SMark A. Greer for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++) 3888e68731SMark A. Greer if (elf64ph->p_type == PT_LOAD) 3988e68731SMark A. Greer break; 4088e68731SMark A. Greer if (i >= (unsigned int)elf64->e_phnum) 4188e68731SMark A. Greer return 0; 4288e68731SMark A. Greer 4388e68731SMark A. Greer info->loadsize = (unsigned long)elf64ph->p_filesz; 4488e68731SMark A. Greer info->memsize = (unsigned long)elf64ph->p_memsz; 4588e68731SMark A. Greer info->elfoffset = (unsigned long)elf64ph->p_offset; 4688e68731SMark A. Greer 4788e68731SMark A. Greer return 1; 4888e68731SMark A. Greer } 4988e68731SMark A. Greer parse_elf32(void * hdr,struct elf_info * info)5088e68731SMark A. Greerint parse_elf32(void *hdr, struct elf_info *info) 5188e68731SMark A. Greer { 5288e68731SMark A. Greer Elf32_Ehdr *elf32 = hdr; 5388e68731SMark A. Greer Elf32_Phdr *elf32ph; 5488e68731SMark A. Greer unsigned int i; 5588e68731SMark A. Greer 5688e68731SMark A. Greer if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 && 5788e68731SMark A. Greer elf32->e_ident[EI_MAG1] == ELFMAG1 && 5888e68731SMark A. Greer elf32->e_ident[EI_MAG2] == ELFMAG2 && 5988e68731SMark A. Greer elf32->e_ident[EI_MAG3] == ELFMAG3 && 6088e68731SMark A. Greer elf32->e_ident[EI_CLASS] == ELFCLASS32 && 6188e68731SMark A. Greer elf32->e_ident[EI_DATA] == ELFDATA2MSB && 62549e8152SPaul Mackerras (elf32->e_type == ET_EXEC || 63549e8152SPaul Mackerras elf32->e_type == ET_DYN) && 6488e68731SMark A. Greer elf32->e_machine == EM_PPC)) 6588e68731SMark A. Greer return 0; 6688e68731SMark A. Greer 6788e68731SMark A. Greer elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff); 6888e68731SMark A. Greer for (i = 0; i < elf32->e_phnum; i++, elf32ph++) 6988e68731SMark A. Greer if (elf32ph->p_type == PT_LOAD) 7088e68731SMark A. Greer break; 7188e68731SMark A. Greer if (i >= elf32->e_phnum) 7288e68731SMark A. Greer return 0; 7388e68731SMark A. Greer 7488e68731SMark A. Greer info->loadsize = elf32ph->p_filesz; 7588e68731SMark A. Greer info->memsize = elf32ph->p_memsz; 7688e68731SMark A. Greer info->elfoffset = elf32ph->p_offset; 7788e68731SMark A. Greer return 1; 7888e68731SMark A. Greer } 79