2 * x.elfext.c -- handle ELF format extensions
3 * Copyright (C) 2002 - 2006 Michael Riepe
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 static const char rcsid[] = "@(#) $Id: x.elfext.c,v 1.5 2009/07/07 17:57:43 michael Exp $";
27 elf_getphdrnum(Elf *elf, size_t *resultp) {
31 elf_assert(elf->e_magic == ELF_MAGIC);
32 if (elf->e_kind != ELF_K_ELF) {
36 if (!elf->e_ehdr && !_elf_cook(elf)) {
40 *resultp = elf->e_phnum;
46 elf_getshdrnum(Elf *elf, size_t *resultp) {
53 elf_assert(elf->e_magic == ELF_MAGIC);
54 if (elf->e_kind != ELF_K_ELF) {
58 if (!elf->e_ehdr && !_elf_cook(elf)) {
61 if ((scn = elf->e_scn_n)) {
62 num = scn->s_index + 1;
71 elf_getshdrstrndx(Elf *elf, size_t *resultp) {
79 elf_assert(elf->e_magic == ELF_MAGIC);
80 if (resultp == NULL) {
81 resultp = &dummy; /* handle NULL pointer gracefully */
83 if (elf->e_kind != ELF_K_ELF) {
87 if (!elf->e_ehdr && !_elf_cook(elf)) {
90 if (elf->e_class == ELFCLASS32) {
91 num = ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx;
94 else if (elf->e_class == ELFCLASS64) {
95 num = ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx;
97 #endif /* __LIBELF64 */
99 if (valid_class(elf->e_class)) {
100 seterr(ERROR_UNIMPLEMENTED);
103 seterr(ERROR_UNKNOWN_CLASS);
107 if (num != SHN_XINDEX) {
112 * look at first section header
114 if (!(scn = elf->e_scn_1)) {
115 seterr(ERROR_NOSUCHSCN);
118 elf_assert(scn->s_magic == SCN_MAGIC);
120 if (elf->e_class == ELFCLASS64) {
121 *resultp = scn->s_shdr64.sh_link;
124 #endif /* __LIBELF64 */
125 *resultp = scn->s_shdr32.sh_link;
130 elf_getphnum(Elf *elf, size_t *resultp) {
131 return elf_getphdrnum(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS;
135 elf_getshnum(Elf *elf, size_t *resultp) {
136 return elf_getshdrnum(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS;
140 elf_getshstrndx(Elf *elf, size_t *resultp) {
141 return elf_getshdrstrndx(elf, resultp) ? LIBELF_FAILURE : LIBELF_SUCCESS;
145 elfx_update_shstrndx(Elf *elf, size_t value) {
150 return LIBELF_FAILURE;
152 elf_assert(elf->e_magic == ELF_MAGIC);
153 if (value >= SHN_LORESERVE) {
157 if (elf->e_kind != ELF_K_ELF) {
158 seterr(ERROR_NOTELF);
159 return LIBELF_FAILURE;
161 if (!elf->e_ehdr && !_elf_cook(elf)) {
162 return LIBELF_FAILURE;
164 if (!(scn = _elf_first_scn(elf))) {
165 return LIBELF_FAILURE;
167 elf_assert(scn->s_magic == SCN_MAGIC);
168 if (elf->e_class == ELFCLASS32) {
169 ((Elf32_Ehdr*)elf->e_ehdr)->e_shstrndx = value;
170 scn->s_shdr32.sh_link = extvalue;
173 else if (elf->e_class == ELFCLASS64) {
174 ((Elf64_Ehdr*)elf->e_ehdr)->e_shstrndx = value;
175 scn->s_shdr64.sh_link = extvalue;
177 #endif /* __LIBELF64 */
179 if (valid_class(elf->e_class)) {
180 seterr(ERROR_UNIMPLEMENTED);
183 seterr(ERROR_UNKNOWN_CLASS);
185 return LIBELF_FAILURE;
187 elf->e_ehdr_flags |= ELF_F_DIRTY;
188 scn->s_shdr_flags |= ELF_F_DIRTY;
189 return LIBELF_SUCCESS;