00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <private.h>
00025
00026 #ifndef lint
00027 static const char rcsid[] = "@(#) $Id: 32.newphdr.c,v 1.1.1.1 2005/10/21 19:00:00 marcel Exp $";
00028 #endif
00029
00030 static char*
00031 _elf_newphdr(Elf *elf, size_t count, unsigned cls) {
00032 char *phdr = NULL;
00033 size_t size;
00034
00035 if (!elf) {
00036 return NULL;
00037 }
00038 elf_assert(elf->e_magic == ELF_MAGIC);
00039 if (!elf->e_ehdr && !elf->e_readable) {
00040 seterr(ERROR_NOEHDR);
00041 }
00042 else if (elf->e_kind != ELF_K_ELF) {
00043 seterr(ERROR_NOTELF);
00044 }
00045 else if (elf->e_class != cls) {
00046 seterr(ERROR_CLASSMISMATCH);
00047 }
00048 else if (elf->e_ehdr || _elf_cook(elf)) {
00049 size = _msize(cls, _elf_version, ELF_T_PHDR);
00050 elf_assert(size);
00051 if (count) {
00052 if (!(phdr = (char*)malloc(count * size))) {
00053 seterr(ERROR_MEM_PHDR);
00054 return NULL;
00055 }
00056 memset(phdr, 0, count * size);
00057 }
00058 elf_assert(elf->e_ehdr);
00059 if (cls == ELFCLASS32) {
00060 ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum = elf->e_phnum = count;
00061 }
00062 #if __LIBELF64
00063 else if (cls == ELFCLASS64) {
00064 ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum = elf->e_phnum = count;
00065 }
00066 #endif
00067 else {
00068 seterr(ERROR_UNIMPLEMENTED);
00069 if (phdr) {
00070 free(phdr);
00071 }
00072 return NULL;
00073 }
00074 if (elf->e_free_phdr) {
00075 elf_assert(elf->e_phdr);
00076 free(elf->e_phdr);
00077 }
00078 elf->e_phdr = phdr;
00079 elf->e_free_phdr = phdr ? 1 : 0;
00080 elf->e_phdr_flags |= ELF_F_DIRTY;
00081 elf->e_ehdr_flags |= ELF_F_DIRTY;
00082 return phdr;
00083 }
00084 return NULL;
00085 }
00086
00087 Elf32_Phdr*
00088 elf32_newphdr(Elf *elf, size_t count) {
00089 return (Elf32_Phdr*)_elf_newphdr(elf, count, ELFCLASS32);
00090 }
00091
00092 #if __LIBELF64
00093
00094 Elf64_Phdr*
00095 elf64_newphdr(Elf *elf, size_t count) {
00096 return (Elf64_Phdr*)_elf_newphdr(elf, count, ELFCLASS64);
00097 }
00098
00099 unsigned long
00100 gelf_newphdr(Elf *elf, size_t phnum) {
00101 if (!valid_class(elf->e_class)) {
00102 seterr(ERROR_UNKNOWN_CLASS);
00103 return 0;
00104 }
00105 if (!_elf_newphdr(elf, phnum, elf->e_class)) {
00106 return 0;
00107 }
00108 return 1;
00109 }
00110
00111 #endif