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