2 * gelfshdr.c - gelf_* translation functions.
3 * Copyright (C) 2000 - 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
25 static const char rcsid[] = "@(#) $Id: gelfshdr.c,v 1.10 2008/05/23 08:15:34 michael Exp $";
28 #define check_and_copy(type, d, s, name, eret) \
30 if (sizeof((d)->name) < sizeof((s)->name) \
31 && (type)(s)->name != (s)->name) { \
32 seterr(ERROR_BADVALUE); \
35 (d)->name = (type)(s)->name; \
39 gelf_getshdr(Elf_Scn *scn, GElf_Shdr *dst) {
45 elf_assert(scn->s_magic == SCN_MAGIC);
46 elf_assert(scn->s_elf);
47 elf_assert(scn->s_elf->e_magic == ELF_MAGIC);
51 if (scn->s_elf->e_class == ELFCLASS64) {
54 else if (scn->s_elf->e_class == ELFCLASS32) {
55 Elf32_Shdr *src = &scn->s_shdr32;
57 check_and_copy(GElf_Word, dst, src, sh_name, NULL);
58 check_and_copy(GElf_Word, dst, src, sh_type, NULL);
59 check_and_copy(GElf_Xword, dst, src, sh_flags, NULL);
60 check_and_copy(GElf_Addr, dst, src, sh_addr, NULL);
61 check_and_copy(GElf_Off, dst, src, sh_offset, NULL);
62 check_and_copy(GElf_Xword, dst, src, sh_size, NULL);
63 check_and_copy(GElf_Word, dst, src, sh_link, NULL);
64 check_and_copy(GElf_Word, dst, src, sh_info, NULL);
65 check_and_copy(GElf_Xword, dst, src, sh_addralign, NULL);
66 check_and_copy(GElf_Xword, dst, src, sh_entsize, NULL);
69 if (valid_class(scn->s_elf->e_class)) {
70 seterr(ERROR_UNIMPLEMENTED);
73 seterr(ERROR_UNKNOWN_CLASS);
78 dst = (GElf_Shdr*)malloc(sizeof(GElf_Shdr));
80 seterr(ERROR_MEM_SHDR);
89 gelf_update_shdr(Elf_Scn *scn, GElf_Shdr *src) {
93 elf_assert(scn->s_magic == SCN_MAGIC);
94 elf_assert(scn->s_elf);
95 elf_assert(scn->s_elf->e_magic == ELF_MAGIC);
96 if (scn->s_elf->e_class == ELFCLASS64) {
99 else if (scn->s_elf->e_class == ELFCLASS32) {
100 Elf32_Shdr *dst = &scn->s_shdr32;
102 check_and_copy(Elf32_Word, dst, src, sh_name, 0);
103 check_and_copy(Elf32_Word, dst, src, sh_type, 0);
104 check_and_copy(Elf32_Word, dst, src, sh_flags, 0);
105 check_and_copy(Elf32_Addr, dst, src, sh_addr, 0);
106 check_and_copy(Elf32_Off, dst, src, sh_offset, 0);
107 check_and_copy(Elf32_Word, dst, src, sh_size, 0);
108 check_and_copy(Elf32_Word, dst, src, sh_link, 0);
109 check_and_copy(Elf32_Word, dst, src, sh_info, 0);
110 check_and_copy(Elf32_Word, dst, src, sh_addralign, 0);
111 check_and_copy(Elf32_Word, dst, src, sh_entsize, 0);
114 if (valid_class(scn->s_elf->e_class)) {
115 seterr(ERROR_UNIMPLEMENTED);
118 seterr(ERROR_UNKNOWN_CLASS);
125 #endif /* __LIBELF64 */