tools/elf4rom/libs/libelf-0.8.10/lib/x.elfext.c
changeset 34 92d87f2e53c2
equal deleted inserted replaced
33:1af5c1be89f8 34:92d87f2e53c2
       
     1 /*
       
     2  * x.elfext.c -- handle ELF format extensions
       
     3  * Copyright (C) 2002 - 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: x.elfext.c,v 1.3 2006/07/07 22:17:50 michael Exp $";
       
    24 #endif /* lint */
       
    25 
       
    26 int
       
    27 elf_getphnum(Elf *elf, size_t *resultp) {
       
    28     if (!elf) {
       
    29 	return LIBELF_FAILURE;
       
    30     }
       
    31     elf_assert(elf->e_magic == ELF_MAGIC);
       
    32     if (elf->e_kind != ELF_K_ELF) {
       
    33 	seterr(ERROR_NOTELF);
       
    34 	return LIBELF_FAILURE;
       
    35     }
       
    36     if (!elf->e_ehdr && !_elf_cook(elf)) {
       
    37 	return LIBELF_FAILURE;
       
    38     }
       
    39     if (resultp) {
       
    40 	*resultp = elf->e_phnum;
       
    41     }
       
    42     return LIBELF_SUCCESS;
       
    43 }
       
    44 
       
    45 int
       
    46 elf_getshnum(Elf *elf, size_t *resultp) {
       
    47     size_t num = 0;
       
    48     Elf_Scn *scn;
       
    49 
       
    50     if (!elf) {
       
    51 	return LIBELF_FAILURE;
       
    52     }
       
    53     elf_assert(elf->e_magic == ELF_MAGIC);
       
    54     if (elf->e_kind != ELF_K_ELF) {
       
    55 	seterr(ERROR_NOTELF);
       
    56 	return LIBELF_FAILURE;
       
    57     }
       
    58     if (!elf->e_ehdr && !_elf_cook(elf)) {
       
    59 	return LIBELF_FAILURE;
       
    60     }
       
    61     if ((scn = elf->e_scn_n)) {
       
    62 	num = scn->s_index + 1;
       
    63     }
       
    64     if (resultp) {
       
    65 	*resultp = num;
       
    66     }
       
    67     return LIBELF_SUCCESS;
       
    68 }
       
    69 
       
    70 int
       
    71 elf_getshstrndx(Elf *elf, size_t *resultp) {
       
    72     size_t num = 0;
       
    73     size_t dummy;
       
    74     Elf_Scn *scn;
       
    75 
       
    76     if (!elf) {
       
    77 	return LIBELF_FAILURE;
       
    78     }
       
    79     elf_assert(elf->e_magic == ELF_MAGIC);
       
    80     if (resultp == NULL) {
       
    81 	resultp = &dummy;	/* handle NULL pointer gracefully */
       
    82     }
       
    83     if (elf->e_kind != ELF_K_ELF) {
       
    84 	seterr(ERROR_NOTELF);
       
    85 	return LIBELF_FAILURE;
       
    86     }
       
    87     if (!elf->e_ehdr && !_elf_cook(elf)) {
       
    88 	return LIBELF_FAILURE;
       
    89     }
       
    90     if (elf->e_class == ELFCLASS32) {
       
    91 	num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx;
       
    92     }
       
    93 #if __LIBELF64
       
    94     else if (elf->e_class == ELFCLASS64) {
       
    95 	num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx;
       
    96     }
       
    97 #endif /* __LIBELF64 */
       
    98     else {
       
    99 	if (valid_class(elf->e_class)) {
       
   100 	    seterr(ERROR_UNIMPLEMENTED);
       
   101 	}
       
   102 	else {
       
   103 	    seterr(ERROR_UNKNOWN_CLASS);
       
   104 	}
       
   105 	return LIBELF_FAILURE;
       
   106     }
       
   107     if (num != SHN_XINDEX) {
       
   108 	*resultp = num;
       
   109 	return LIBELF_SUCCESS;
       
   110     }
       
   111     /*
       
   112      * look at first section header
       
   113      */
       
   114     if (!(scn = elf->e_scn_1)) {
       
   115 	seterr(ERROR_NOSUCHSCN);
       
   116 	return LIBELF_FAILURE;
       
   117     }
       
   118     elf_assert(scn->s_magic == SCN_MAGIC);
       
   119 #if __LIBELF64
       
   120     if (elf->e_class == ELFCLASS64) {
       
   121 	*resultp = scn->s_shdr64.sh_link;
       
   122 	return LIBELF_SUCCESS;
       
   123     }
       
   124 #endif /* __LIBELF64 */
       
   125     *resultp = scn->s_shdr32.sh_link;
       
   126     return LIBELF_SUCCESS;
       
   127 }
       
   128 
       
   129 int
       
   130 elfx_update_shstrndx(Elf *elf, size_t value) {
       
   131     size_t extvalue = 0;
       
   132     Elf_Scn *scn;
       
   133 
       
   134     if (!elf) {
       
   135 	return LIBELF_FAILURE;
       
   136     }
       
   137     elf_assert(elf->e_magic == ELF_MAGIC);
       
   138     if (value >= SHN_LORESERVE) {
       
   139 	extvalue = value;
       
   140 	value = SHN_XINDEX;
       
   141     }
       
   142     if (elf->e_kind != ELF_K_ELF) {
       
   143 	seterr(ERROR_NOTELF);
       
   144 	return LIBELF_FAILURE;
       
   145     }
       
   146     if (!elf->e_ehdr && !_elf_cook(elf)) {
       
   147 	return LIBELF_FAILURE;
       
   148     }
       
   149     if (!(scn = _elf_first_scn(elf))) {
       
   150 	return LIBELF_FAILURE;
       
   151     }
       
   152     elf_assert(scn->s_magic == SCN_MAGIC);
       
   153     if (elf->e_class == ELFCLASS32) {
       
   154 	((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx = value;
       
   155 	scn->s_shdr32.sh_link = extvalue;
       
   156     }
       
   157 #if __LIBELF64
       
   158     else if (elf->e_class == ELFCLASS64) {
       
   159 	((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx = value;
       
   160 	scn->s_shdr64.sh_link = extvalue;
       
   161     }
       
   162 #endif /* __LIBELF64 */
       
   163     else {
       
   164 	if (valid_class(elf->e_class)) {
       
   165 	    seterr(ERROR_UNIMPLEMENTED);
       
   166 	}
       
   167 	else {
       
   168 	    seterr(ERROR_UNKNOWN_CLASS);
       
   169 	}
       
   170 	return LIBELF_FAILURE;
       
   171     }
       
   172     elf->e_ehdr_flags |= ELF_F_DIRTY;
       
   173     scn->s_shdr_flags |= ELF_F_DIRTY;
       
   174     return LIBELF_SUCCESS;
       
   175 }