2 * gelfehdr.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: gelfehdr.c,v 1.9 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_getehdr(Elf *elf, GElf_Ehdr *dst) {
46 elf_assert(elf->e_magic == ELF_MAGIC);
47 tmp = _elf_getehdr(elf, elf->e_class);
54 if (elf->e_class == ELFCLASS64) {
55 *dst = *(Elf64_Ehdr*)tmp;
57 else if (elf->e_class == ELFCLASS32) {
58 Elf32_Ehdr *src = (Elf32_Ehdr*)tmp;
60 memcpy(dst->e_ident, src->e_ident, EI_NIDENT);
61 check_and_copy(GElf_Half, dst, src, e_type, NULL);
62 check_and_copy(GElf_Half, dst, src, e_machine, NULL);
63 check_and_copy(GElf_Word, dst, src, e_version, NULL);
64 check_and_copy(GElf_Addr, dst, src, e_entry, NULL);
65 check_and_copy(GElf_Off, dst, src, e_phoff, NULL);
66 check_and_copy(GElf_Off, dst, src, e_shoff, NULL);
67 check_and_copy(GElf_Word, dst, src, e_flags, NULL);
68 check_and_copy(GElf_Half, dst, src, e_ehsize, NULL);
69 check_and_copy(GElf_Half, dst, src, e_phentsize, NULL);
70 check_and_copy(GElf_Half, dst, src, e_phnum, NULL);
71 check_and_copy(GElf_Half, dst, src, e_shentsize, NULL);
72 check_and_copy(GElf_Half, dst, src, e_shnum, NULL);
73 check_and_copy(GElf_Half, dst, src, e_shstrndx, NULL);
76 if (valid_class(elf->e_class)) {
77 seterr(ERROR_UNIMPLEMENTED);
80 seterr(ERROR_UNKNOWN_CLASS);
85 dst = (GElf_Ehdr*)malloc(sizeof(GElf_Ehdr));
87 seterr(ERROR_MEM_EHDR);
96 gelf_update_ehdr(Elf *elf, GElf_Ehdr *src) {
102 elf_assert(elf->e_magic == ELF_MAGIC);
103 tmp = _elf_getehdr(elf, elf->e_class);
107 if (elf->e_class == ELFCLASS64) {
108 *(Elf64_Ehdr*)tmp = *src;
110 else if (elf->e_class == ELFCLASS32) {
111 Elf32_Ehdr *dst = (Elf32_Ehdr*)tmp;
113 memcpy(dst->e_ident, src->e_ident, EI_NIDENT);
114 check_and_copy(Elf32_Half, dst, src, e_type, 0);
115 check_and_copy(Elf32_Half, dst, src, e_machine, 0);
116 check_and_copy(Elf32_Word, dst, src, e_version, 0);
117 check_and_copy(Elf32_Addr, dst, src, e_entry, 0);
118 check_and_copy(Elf32_Off, dst, src, e_phoff, 0);
119 check_and_copy(Elf32_Off, dst, src, e_shoff, 0);
120 check_and_copy(Elf32_Word, dst, src, e_flags, 0);
121 check_and_copy(Elf32_Half, dst, src, e_ehsize, 0);
122 check_and_copy(Elf32_Half, dst, src, e_phentsize, 0);
123 check_and_copy(Elf32_Half, dst, src, e_phnum, 0);
124 check_and_copy(Elf32_Half, dst, src, e_shentsize, 0);
125 check_and_copy(Elf32_Half, dst, src, e_shnum, 0);
126 check_and_copy(Elf32_Half, dst, src, e_shstrndx, 0);
129 if (valid_class(elf->e_class)) {
130 seterr(ERROR_UNIMPLEMENTED);
133 seterr(ERROR_UNKNOWN_CLASS);
140 #endif /* __LIBELF64 */