tools/elf4rom/libs/libelf-0.8.10/lib/32.newphdr.c
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2  * 32.newphdr.c - implementation of the elf{32,64}_newphdr(3) functions.
       
     3  * Copyright (C) 1995 - 2006 Michael Riepe
       
     4  * 
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Library General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  * 
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Library General Public License for more details.
       
    14  * 
       
    15  * You should have received a copy of the GNU Library General Public
       
    16  * License along with this library; if not, write to the Free Software
       
    17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    18  */
       
    19 
       
    20 #include <private.h>
       
    21 
       
    22 #ifndef lint
       
    23 static const char rcsid[] = "@(#) $Id: 32.newphdr.c,v 1.15 2006/07/07 22:15:41 michael Exp $";
       
    24 #endif /* lint */
       
    25 
       
    26 static char*
       
    27 _elf_newphdr(Elf *elf, size_t count, unsigned cls) {
       
    28     size_t extcount = 0;
       
    29     Elf_Scn *scn = NULL;
       
    30     char *phdr = NULL;
       
    31     size_t size;
       
    32 
       
    33     if (!elf) {
       
    34 	return NULL;
       
    35     }
       
    36     elf_assert(elf->e_magic == ELF_MAGIC);
       
    37     if (!elf->e_ehdr && !elf->e_readable) {
       
    38 	seterr(ERROR_NOEHDR);
       
    39     }
       
    40     else if (elf->e_kind != ELF_K_ELF) {
       
    41 	seterr(ERROR_NOTELF);
       
    42     }
       
    43     else if (elf->e_class != cls) {
       
    44 	seterr(ERROR_CLASSMISMATCH);
       
    45     }
       
    46     else if (elf->e_ehdr || _elf_cook(elf)) {
       
    47 	size = _msize(cls, _elf_version, ELF_T_PHDR);
       
    48 	elf_assert(size);
       
    49 	if (!(scn = _elf_first_scn(elf))) {
       
    50 	    return NULL;
       
    51 	}
       
    52 	if (count) {
       
    53 	    if (!(phdr = (char*)malloc(count * size))) {
       
    54 		seterr(ERROR_MEM_PHDR);
       
    55 		return NULL;
       
    56 	    }
       
    57 	    memset(phdr, 0, count * size);
       
    58 	}
       
    59 	elf_assert(elf->e_ehdr);
       
    60 	elf->e_phnum = count;
       
    61 	if (count >= PN_XNUM) {
       
    62 	    /*
       
    63 	     * get NULL section (create it if necessary)
       
    64 	     */
       
    65 	    extcount = count;
       
    66 	    count = PN_XNUM;
       
    67 	}
       
    68 	if (cls == ELFCLASS32) {
       
    69 	    ((Elf32_Ehdr*)elf->e_ehdr)->e_phnum = count;
       
    70 	    scn->s_shdr32.sh_info = extcount;
       
    71 	}
       
    72 #if __LIBELF64
       
    73 	else if (cls == ELFCLASS64) {
       
    74 	    ((Elf64_Ehdr*)elf->e_ehdr)->e_phnum = count;
       
    75 	    scn->s_shdr64.sh_info = extcount;
       
    76 	}
       
    77 #endif /* __LIBELF64 */
       
    78 	else {
       
    79 	    seterr(ERROR_UNIMPLEMENTED);
       
    80 	    if (phdr) {
       
    81 		free(phdr);
       
    82 	    }
       
    83 	    return NULL;
       
    84 	}
       
    85 	if (elf->e_phdr) {
       
    86 	    free(elf->e_phdr);
       
    87 	}
       
    88 	elf->e_phdr = phdr;
       
    89 	elf->e_phdr_flags |= ELF_F_DIRTY;
       
    90 	elf->e_ehdr_flags |= ELF_F_DIRTY;
       
    91 	scn->s_scn_flags |= ELF_F_DIRTY;
       
    92 	return phdr;
       
    93     }
       
    94     return NULL;
       
    95 }
       
    96 
       
    97 Elf32_Phdr*
       
    98 elf32_newphdr(Elf *elf, size_t count) {
       
    99     return (Elf32_Phdr*)_elf_newphdr(elf, count, ELFCLASS32);
       
   100 }
       
   101 
       
   102 #if __LIBELF64
       
   103 
       
   104 Elf64_Phdr*
       
   105 elf64_newphdr(Elf *elf, size_t count) {
       
   106     return (Elf64_Phdr*)_elf_newphdr(elf, count, ELFCLASS64);
       
   107 }
       
   108 
       
   109 unsigned long
       
   110 gelf_newphdr(Elf *elf, size_t phnum) {
       
   111     if (!valid_class(elf->e_class)) {
       
   112 	seterr(ERROR_UNKNOWN_CLASS);
       
   113 	return 0;
       
   114     }
       
   115     return (unsigned long)_elf_newphdr(elf, phnum, elf->e_class);
       
   116 }
       
   117 
       
   118 #endif /* __LIBELF64 */