|
1 /* |
|
2 * gelfphdr.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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 */ |
|
19 |
|
20 #include <private.h> |
|
21 |
|
22 #if __LIBELF64 |
|
23 |
|
24 #ifndef lint |
|
25 static const char rcsid[] = "@(#) $Id: gelfphdr.c,v 1.8 2006/07/07 22:16:43 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_Phdr* |
|
39 gelf_getphdr(Elf *elf, int ndx, GElf_Phdr *dst) { |
|
40 GElf_Phdr buf; |
|
41 char *tmp; |
|
42 size_t n; |
|
43 |
|
44 if (!elf) { |
|
45 return NULL; |
|
46 } |
|
47 elf_assert(elf->e_magic == ELF_MAGIC); |
|
48 tmp = _elf_getphdr(elf, elf->e_class); |
|
49 if (!tmp) { |
|
50 return NULL; |
|
51 } |
|
52 if (ndx < 0 || ndx >= elf->e_phnum) { |
|
53 seterr(ERROR_BADINDEX); |
|
54 return NULL; |
|
55 } |
|
56 n = _msize(elf->e_class, _elf_version, ELF_T_PHDR); |
|
57 if (n == 0) { |
|
58 seterr(ERROR_UNIMPLEMENTED); |
|
59 return NULL; |
|
60 } |
|
61 if (!dst) { |
|
62 dst = &buf; |
|
63 } |
|
64 if (elf->e_class == ELFCLASS64) { |
|
65 *dst = *(Elf64_Phdr*)(tmp + ndx * n); |
|
66 } |
|
67 else if (elf->e_class == ELFCLASS32) { |
|
68 Elf32_Phdr *src = (Elf32_Phdr*)(tmp + ndx * n); |
|
69 |
|
70 check_and_copy(GElf_Word, dst, src, p_type, NULL); |
|
71 check_and_copy(GElf_Word, dst, src, p_flags, NULL); |
|
72 check_and_copy(GElf_Off, dst, src, p_offset, NULL); |
|
73 check_and_copy(GElf_Addr, dst, src, p_vaddr, NULL); |
|
74 check_and_copy(GElf_Addr, dst, src, p_paddr, NULL); |
|
75 check_and_copy(GElf_Xword, dst, src, p_filesz, NULL); |
|
76 check_and_copy(GElf_Xword, dst, src, p_memsz, NULL); |
|
77 check_and_copy(GElf_Xword, dst, src, p_align, NULL); |
|
78 } |
|
79 else { |
|
80 if (valid_class(elf->e_class)) { |
|
81 seterr(ERROR_UNIMPLEMENTED); |
|
82 } |
|
83 else { |
|
84 seterr(ERROR_UNKNOWN_CLASS); |
|
85 } |
|
86 return NULL; |
|
87 } |
|
88 if (dst == &buf) { |
|
89 dst = (GElf_Phdr*)malloc(sizeof(GElf_Phdr)); |
|
90 if (!dst) { |
|
91 seterr(ERROR_MEM_PHDR); |
|
92 return NULL; |
|
93 } |
|
94 *dst = buf; |
|
95 } |
|
96 return dst; |
|
97 } |
|
98 |
|
99 int |
|
100 gelf_update_phdr(Elf *elf, int ndx, GElf_Phdr *src) { |
|
101 char *tmp; |
|
102 size_t n; |
|
103 |
|
104 if (!elf || !src) { |
|
105 return 0; |
|
106 } |
|
107 elf_assert(elf->e_magic == ELF_MAGIC); |
|
108 tmp = _elf_getphdr(elf, elf->e_class); |
|
109 if (!tmp) { |
|
110 return 0; |
|
111 } |
|
112 if (ndx < 0 || ndx >= elf->e_phnum) { |
|
113 seterr(ERROR_BADINDEX); |
|
114 return 0; |
|
115 } |
|
116 n = _msize(elf->e_class, _elf_version, ELF_T_PHDR); |
|
117 if (n == 0) { |
|
118 seterr(ERROR_UNIMPLEMENTED); |
|
119 return 0; |
|
120 } |
|
121 if (elf->e_class == ELFCLASS64) { |
|
122 *(Elf64_Phdr*)(tmp + ndx * n) = *src; |
|
123 } |
|
124 else if (elf->e_class == ELFCLASS32) { |
|
125 Elf32_Phdr *dst = (Elf32_Phdr*)(tmp + ndx * n); |
|
126 |
|
127 check_and_copy(Elf32_Word, dst, src, p_type, 0); |
|
128 check_and_copy(Elf32_Off, dst, src, p_offset, 0); |
|
129 check_and_copy(Elf32_Addr, dst, src, p_vaddr, 0); |
|
130 check_and_copy(Elf32_Addr, dst, src, p_paddr, 0); |
|
131 check_and_copy(Elf32_Word, dst, src, p_filesz, 0); |
|
132 check_and_copy(Elf32_Word, dst, src, p_memsz, 0); |
|
133 check_and_copy(Elf32_Word, dst, src, p_flags, 0); |
|
134 check_and_copy(Elf32_Word, dst, src, p_align, 0); |
|
135 } |
|
136 else { |
|
137 if (valid_class(elf->e_class)) { |
|
138 seterr(ERROR_UNIMPLEMENTED); |
|
139 } |
|
140 else { |
|
141 seterr(ERROR_UNKNOWN_CLASS); |
|
142 } |
|
143 return 0; |
|
144 } |
|
145 return 1; |
|
146 } |
|
147 |
|
148 #endif /* __LIBELF64 */ |