|
1 /* |
|
2 * verdef.h - copy versioning information. |
|
3 * Copyright (C) 2001 - 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 #ifndef lint |
|
21 static const char verdef_h_rcsid[] = "@(#) $Id: verdef.h,v 1.12 2006/07/27 22:56:11 michael Exp $"; |
|
22 #endif /* lint */ |
|
23 |
|
24 #if VER_DEF_CURRENT != 1 |
|
25 #error libelf currently does not support VER_DEF_CURRENT != 1 |
|
26 #endif /* VER_DEF_CURRENT != 1 */ |
|
27 |
|
28 #if TOFILE |
|
29 |
|
30 static void |
|
31 __store_verdaux(verdaux_ftype *dst, const verdaux_mtype *src, unsigned enc) { |
|
32 if (enc == ELFDATA2LSB) { |
|
33 __store_u32L(dst->vda_name, src->vda_name); |
|
34 __store_u32L(dst->vda_next, src->vda_next); |
|
35 } |
|
36 else { |
|
37 __store_u32M(dst->vda_name, src->vda_name); |
|
38 __store_u32M(dst->vda_next, src->vda_next); |
|
39 } |
|
40 } |
|
41 |
|
42 static void |
|
43 __store_verdef(verdef_ftype *dst, const verdef_mtype *src, unsigned enc) { |
|
44 if (enc == ELFDATA2LSB) { |
|
45 __store_u16L(dst->vd_version, src->vd_version); |
|
46 __store_u16L(dst->vd_flags, src->vd_flags); |
|
47 __store_u16L(dst->vd_ndx, src->vd_ndx); |
|
48 __store_u16L(dst->vd_cnt, src->vd_cnt); |
|
49 __store_u32L(dst->vd_hash, src->vd_hash); |
|
50 __store_u32L(dst->vd_aux, src->vd_aux); |
|
51 __store_u32L(dst->vd_next, src->vd_next); |
|
52 } |
|
53 else { |
|
54 __store_u16M(dst->vd_version, src->vd_version); |
|
55 __store_u16M(dst->vd_flags, src->vd_flags); |
|
56 __store_u16M(dst->vd_ndx, src->vd_ndx); |
|
57 __store_u16M(dst->vd_cnt, src->vd_cnt); |
|
58 __store_u32M(dst->vd_hash, src->vd_hash); |
|
59 __store_u32M(dst->vd_aux, src->vd_aux); |
|
60 __store_u32M(dst->vd_next, src->vd_next); |
|
61 } |
|
62 } |
|
63 |
|
64 typedef verdaux_mtype verdaux_stype; |
|
65 typedef verdaux_ftype verdaux_dtype; |
|
66 typedef verdef_mtype verdef_stype; |
|
67 typedef verdef_ftype verdef_dtype; |
|
68 typedef align_mtype verdef_atype; |
|
69 |
|
70 #define copy_verdaux_srctotmp(d, s, e) (*(d) = *(s)) |
|
71 #define copy_verdaux_tmptodst(d, s, e) __store_verdaux((d), (s), (e)) |
|
72 #define copy_verdef_srctotmp(d, s, e) (*(d) = *(s)) |
|
73 #define copy_verdef_tmptodst(d, s, e) __store_verdef((d), (s), (e)) |
|
74 |
|
75 #define translator_suffix _tof |
|
76 |
|
77 #else /* TOFILE */ |
|
78 |
|
79 static void |
|
80 __load_verdaux(verdaux_mtype *dst, const verdaux_ftype *src, unsigned enc) { |
|
81 if (enc == ELFDATA2LSB) { |
|
82 dst->vda_name = __load_u32L(src->vda_name); |
|
83 dst->vda_next = __load_u32L(src->vda_next); |
|
84 } |
|
85 else { |
|
86 dst->vda_name = __load_u32M(src->vda_name); |
|
87 dst->vda_next = __load_u32M(src->vda_next); |
|
88 } |
|
89 } |
|
90 |
|
91 static void |
|
92 __load_verdef(verdef_mtype *dst, const verdef_ftype *src, unsigned enc) { |
|
93 if (enc == ELFDATA2LSB) { |
|
94 dst->vd_version = __load_u16L(src->vd_version); |
|
95 dst->vd_flags = __load_u16L(src->vd_flags); |
|
96 dst->vd_ndx = __load_u16L(src->vd_ndx); |
|
97 dst->vd_cnt = __load_u16L(src->vd_cnt); |
|
98 dst->vd_hash = __load_u32L(src->vd_hash); |
|
99 dst->vd_aux = __load_u32L(src->vd_aux); |
|
100 dst->vd_next = __load_u32L(src->vd_next); |
|
101 } |
|
102 else { |
|
103 dst->vd_version = __load_u16M(src->vd_version); |
|
104 dst->vd_flags = __load_u16M(src->vd_flags); |
|
105 dst->vd_ndx = __load_u16M(src->vd_ndx); |
|
106 dst->vd_cnt = __load_u16M(src->vd_cnt); |
|
107 dst->vd_hash = __load_u32M(src->vd_hash); |
|
108 dst->vd_aux = __load_u32M(src->vd_aux); |
|
109 dst->vd_next = __load_u32M(src->vd_next); |
|
110 } |
|
111 } |
|
112 |
|
113 typedef verdaux_ftype verdaux_stype; |
|
114 typedef verdaux_mtype verdaux_dtype; |
|
115 typedef verdef_ftype verdef_stype; |
|
116 typedef verdef_mtype verdef_dtype; |
|
117 typedef align_ftype verdef_atype; |
|
118 |
|
119 #define copy_verdaux_srctotmp(d, s, e) __load_verdaux((d), (s), (e)) |
|
120 #define copy_verdaux_tmptodst(d, s, e) (*(d) = *(s)) |
|
121 #define copy_verdef_srctotmp(d, s, e) __load_verdef((d), (s), (e)) |
|
122 #define copy_verdef_tmptodst(d, s, e) (*(d) = *(s)) |
|
123 |
|
124 #define translator_suffix _tom |
|
125 |
|
126 #endif /* TOFILE */ |
|
127 |
|
128 #define cat3(a,b,c) a##b##c |
|
129 #define xlt3(p,e,s) cat3(p,e,s) |
|
130 #define xltprefix(x) xlt3(x,_,class_suffix) |
|
131 #define translator(x,e) xlt3(xltprefix(_elf_##x),e,translator_suffix) |
|
132 |
|
133 static size_t |
|
134 xlt_verdef(unsigned char *dst, const unsigned char *src, size_t n, unsigned enc) { |
|
135 size_t off; |
|
136 |
|
137 if (sizeof(verdef_stype) != sizeof(verdef_dtype) |
|
138 || sizeof(verdaux_stype) != sizeof(verdaux_dtype)) { |
|
139 /* never happens for ELF v1 and Verneed v1 */ |
|
140 seterr(ERROR_UNIMPLEMENTED); |
|
141 return (size_t)-1; |
|
142 } |
|
143 /* size translation shortcut */ |
|
144 if (dst == NULL) { |
|
145 return n; |
|
146 } |
|
147 if (src == NULL) { |
|
148 seterr(ERROR_NULLBUF); |
|
149 return (size_t)-1; |
|
150 } |
|
151 off = 0; |
|
152 while (off + sizeof(verdef_stype) <= n) { |
|
153 const verdef_stype *svd; |
|
154 verdef_dtype *dvd; |
|
155 verdef_mtype vd; |
|
156 size_t acount; |
|
157 size_t aoff; |
|
158 |
|
159 /* |
|
160 * check for proper alignment |
|
161 */ |
|
162 if (off % sizeof(verdef_atype)) { |
|
163 seterr(ERROR_VERDEF_FORMAT); |
|
164 return (size_t)-1; |
|
165 } |
|
166 /* |
|
167 * copy and check src |
|
168 */ |
|
169 svd = (verdef_stype*)(src + off); |
|
170 dvd = (verdef_dtype*)(dst + off); |
|
171 copy_verdef_srctotmp(&vd, svd, enc); |
|
172 if (vd.vd_version < 1 |
|
173 || vd.vd_version > VER_DEF_CURRENT) { |
|
174 seterr(ERROR_VERDEF_VERSION); |
|
175 return (size_t)-1; |
|
176 } |
|
177 if (vd.vd_cnt < 1 |
|
178 || vd.vd_aux == 0) { |
|
179 seterr(ERROR_VERDEF_FORMAT); |
|
180 return (size_t)-1; |
|
181 } |
|
182 copy_verdef_tmptodst(dvd, &vd, enc); |
|
183 /* |
|
184 * copy aux array |
|
185 */ |
|
186 aoff = off + vd.vd_aux; |
|
187 for (acount = 0; acount < vd.vd_cnt; acount++) { |
|
188 const verdaux_stype *svda; |
|
189 verdaux_dtype *dvda; |
|
190 verdaux_mtype vda; |
|
191 |
|
192 /* |
|
193 * are we still inside the buffer limits? |
|
194 */ |
|
195 if (aoff + sizeof(verdaux_stype) > n) { |
|
196 break; |
|
197 } |
|
198 /* |
|
199 * check for proper alignment |
|
200 */ |
|
201 if (aoff % sizeof(verdef_atype)) { |
|
202 seterr(ERROR_VERDEF_FORMAT); |
|
203 return (size_t)-1; |
|
204 } |
|
205 /* |
|
206 * copy and check src |
|
207 */ |
|
208 svda = (verdaux_stype*)(src + aoff); |
|
209 dvda = (verdaux_dtype*)(dst + aoff); |
|
210 copy_verdaux_srctotmp(&vda, svda, enc); |
|
211 copy_verdaux_tmptodst(dvda, &vda, enc); |
|
212 /* |
|
213 * advance to next verdaux |
|
214 */ |
|
215 if (vda.vda_next == 0) { |
|
216 /* end of list */ |
|
217 break; |
|
218 } |
|
219 aoff += vda.vda_next; |
|
220 } |
|
221 /* |
|
222 * advance to next verdef |
|
223 */ |
|
224 if (vd.vd_next == 0) { |
|
225 /* end of list */ |
|
226 break; |
|
227 } |
|
228 off += vd.vd_next; |
|
229 } |
|
230 return n; |
|
231 } |
|
232 |
|
233 size_t |
|
234 translator(verdef,L11)(unsigned char *dst, const unsigned char *src, size_t n) { |
|
235 return xlt_verdef(dst, src, n, ELFDATA2LSB); |
|
236 } |
|
237 |
|
238 size_t |
|
239 translator(verdef,M11)(unsigned char *dst, const unsigned char *src, size_t n) { |
|
240 return xlt_verdef(dst, src, n, ELFDATA2MSB); |
|
241 } |