resetting manifest requested domain to floor
[platform/upstream/libelf0.git] / lib / gelfehdr.c
1 /*
2  * gelfehdr.c - gelf_* translation functions.
3  * Copyright (C) 2000 - 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include <private.h>
21
22 #if __LIBELF64
23
24 #ifndef lint
25 static const char rcsid[] = "@(#) $Id: gelfehdr.c,v 1.9 2008/05/23 08:15:34 michael Exp $";
26 #endif /* lint */
27
28 #define check_and_copy(type, d, s, name, eret)          \
29     do {                                                \
30         if (sizeof((d)->name) < sizeof((s)->name)       \
31          && (type)(s)->name != (s)->name) {             \
32             seterr(ERROR_BADVALUE);                     \
33             return (eret);                              \
34         }                                               \
35         (d)->name = (type)(s)->name;                    \
36     } while (0)
37
38 GElf_Ehdr*
39 gelf_getehdr(Elf *elf, GElf_Ehdr *dst) {
40     GElf_Ehdr buf;
41     char *tmp;
42
43     if (!elf) {
44         return NULL;
45     }
46     elf_assert(elf->e_magic == ELF_MAGIC);
47     tmp = _elf_getehdr(elf, elf->e_class);
48     if (!tmp) {
49         return NULL;
50     }
51     if (!dst) {
52         dst = &buf;
53     }
54     if (elf->e_class == ELFCLASS64) {
55         *dst = *(Elf64_Ehdr*)tmp;
56     }
57     else if (elf->e_class == ELFCLASS32) {
58         Elf32_Ehdr *src = (Elf32_Ehdr*)tmp;
59
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);
74     }
75     else {
76         if (valid_class(elf->e_class)) {
77             seterr(ERROR_UNIMPLEMENTED);
78         }
79         else {
80             seterr(ERROR_UNKNOWN_CLASS);
81         }
82         return NULL;
83     }
84     if (dst == &buf) {
85         dst = (GElf_Ehdr*)malloc(sizeof(GElf_Ehdr));
86         if (!dst) {
87             seterr(ERROR_MEM_EHDR);
88             return NULL;
89         }
90         *dst = buf;
91     }
92     return dst;
93 }
94
95 int
96 gelf_update_ehdr(Elf *elf, GElf_Ehdr *src) {
97     char *tmp;
98
99     if (!elf || !src) {
100         return 0;
101     }
102     elf_assert(elf->e_magic == ELF_MAGIC);
103     tmp = _elf_getehdr(elf, elf->e_class);
104     if (!tmp) {
105         return 0;
106     }
107     if (elf->e_class == ELFCLASS64) {
108         *(Elf64_Ehdr*)tmp = *src;
109     }
110     else if (elf->e_class == ELFCLASS32) {
111         Elf32_Ehdr *dst = (Elf32_Ehdr*)tmp;
112
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);
127     }
128     else {
129         if (valid_class(elf->e_class)) {
130             seterr(ERROR_UNIMPLEMENTED);
131         }
132         else {
133             seterr(ERROR_UNKNOWN_CLASS);
134         }
135         return 0;
136     }
137     return 1;
138 }
139
140 #endif /* __LIBELF64 */