1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
/*
* 64.xlatetof.c - implementation of the elf64_xlateto[fm](3) functions.
* Copyright (C) 1995 - 2006 Michael Riepe
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <private.h>
#include <ext_types.h>
#include <byteswap.h>
#if __LIBELF64
#ifndef lint
static const char rcsid[] = "@(#) $Id: 64.xlatetof.c,v 1.27 2008/05/23 08:15:34 michael Exp $";
#endif /* lint */
/*
* Ugly, ugly
*/
#ifdef _WIN32
# define Cat2(a,b)a##b
# define Cat3(a,b,c)a##b##c
# define Ex1(m1,m2,a,b)m1##m2(a##b)
# define Ex2(m1,m2,a,b,c)m1##m2(a,b##c)
#else /* _WIN32 */
# define x
# if defined/**/x
# define Cat2(a,b)a##b
# define Cat3(a,b,c)a##b##c
# define Ex1(m1,m2,a,b)m1##m2(a##b)
# define Ex2(m1,m2,a,b,c)m1##m2(a,b##c)
# else
# define Cat2(a,b)a/**/b
# define Cat3(a,b,c)a/**/b/**/c
# define Ex1(m1,m2,a,b)m1/**/m2(a/**/b)
# define Ex2(m1,m2,a,b,c)m1/**/m2(a,b/**/c)
# endif
# undef x
#endif /* _WIN32 */
/*
* auxiliary macros for execution order reversal
*/
#define seq_forw(a,b) a b
#define seq_back(a,b) b a
/*
* function instantiator
*/
#define copy_type_e_io(name,e,io,tfrom,tto,copy) \
static size_t \
Cat3(name,_,io)(unsigned char *dst, const unsigned char *src, size_t n) { \
n /= sizeof(tfrom); \
if (n && dst) { \
const tfrom *from = (const tfrom*)src; \
tto *to = (tto*)dst; \
size_t i; \
\
if (sizeof(tfrom) < sizeof(tto)) { \
from += n; \
to += n; \
for (i = 0; i < n; i++) { \
--from; \
--to; \
copy(e,io,seq_back) \
} \
} \
else { \
for (i = 0; i < n; i++) { \
copy(e,io,seq_forw) \
from++; \
to++; \
} \
} \
} \
return n * sizeof(tto); \
}
#define copy_type_e(name,e,type,copy) \
copy_type_e_io(name,e,tom,Cat2(__ext_,type),type,copy) \
copy_type_e_io(name,e,tof,type,Cat2(__ext_,type),copy)
/*
* master function instantiator
*/
#define copy_type(name,version,type,copy) \
copy_type_e(Cat3(name,L,version),L,type,copy) \
copy_type_e(Cat3(name,M,version),M,type,copy)
/*
* scalar copying
*/
#define copy_scalar_tom(type) *to = Cat2(__load_,type)(*from);
#define copy_scalar_tof(type) Cat2(__store_,type)(*to, *from);
/*
* structure member copying
*/
#define copy_tom(mb,type) to->mb = Cat2(__load_,type)(from->mb);
#define copy_tof(mb,type) Cat2(__store_,type)(to->mb, from->mb);
/*
* structure member copying (direction independent)
*/
#define copy_byte(e,io,mb) to->mb = from->mb;
#define copy_addr(e,io,mb) Ex2(copy_,io,mb,u64,e)
#define copy_half(e,io,mb) Ex2(copy_,io,mb,u16,e)
#define copy_off(e,io,mb) Ex2(copy_,io,mb,u64,e)
#define copy_sword(e,io,mb) Ex2(copy_,io,mb,i32,e)
#define copy_word(e,io,mb) Ex2(copy_,io,mb,u32,e)
#define copy_sxword(e,io,mb) Ex2(copy_,io,mb,i64,e)
#define copy_xword(e,io,mb) Ex2(copy_,io,mb,u64,e)
#define copy_arr(e,io,mb) \
array_copy(to->mb, sizeof(to->mb), from->mb, sizeof(from->mb));
/*
* scalar copying (direction independent)
* these macros are used as `copy' arguments to copy_type()
*/
#define copy_addr_11(e,io,seq) Ex1(copy_scalar_,io,u64,e)
#define copy_half_11(e,io,seq) Ex1(copy_scalar_,io,u16,e)
#define copy_off_11(e,io,seq) Ex1(copy_scalar_,io,u64,e)
#define copy_sword_11(e,io,seq) Ex1(copy_scalar_,io,i32,e)
#define copy_word_11(e,io,seq) Ex1(copy_scalar_,io,u32,e)
#define copy_sxword_11(e,io,seq)Ex1(copy_scalar_,io,i64,e)
#define copy_xword_11(e,io,seq) Ex1(copy_scalar_,io,u64,e)
/*
* structure copying (direction independent)
* these macros are used as `copy' arguments to copy_type()
*/
#define copy_dyn_11(e,io,seq) \
seq(copy_xword(e,io,d_tag), \
seq(copy_addr(e,io,d_un.d_ptr), \
nullcopy))
#define copy_ehdr_11(e,io,seq) \
seq(copy_arr(e,io,e_ident), \
seq(copy_half(e,io,e_type), \
seq(copy_half(e,io,e_machine), \
seq(copy_word(e,io,e_version), \
seq(copy_addr(e,io,e_entry), \
seq(copy_off(e,io,e_phoff), \
seq(copy_off(e,io,e_shoff), \
seq(copy_word(e,io,e_flags), \
seq(copy_half(e,io,e_ehsize), \
seq(copy_half(e,io,e_phentsize), \
seq(copy_half(e,io,e_phnum), \
seq(copy_half(e,io,e_shentsize), \
seq(copy_half(e,io,e_shnum), \
seq(copy_half(e,io,e_shstrndx), \
nullcopy))))))))))))))
#define copy_phdr_11(e,io,seq) \
seq(copy_word(e,io,p_type), \
seq(copy_word(e,io,p_flags), \
seq(copy_off(e,io,p_offset), \
seq(copy_addr(e,io,p_vaddr), \
seq(copy_addr(e,io,p_paddr), \
seq(copy_xword(e,io,p_filesz), \
seq(copy_xword(e,io,p_memsz), \
seq(copy_xword(e,io,p_align), \
nullcopy))))))))
#if __LIBELF64_IRIX
#define copy_rela_11(e,io,seq) \
seq(copy_addr(e,io,r_offset), \
seq(copy_word(e,io,r_sym), \
seq(copy_byte(e,io,r_ssym), \
seq(copy_byte(e,io,r_type3), \
seq(copy_byte(e,io,r_type2), \
seq(copy_byte(e,io,r_type), \
seq(copy_sxword(e,io,r_addend), \
nullcopy)))))))
#define copy_rel_11(e,io,seq) \
seq(copy_addr(e,io,r_offset), \
seq(copy_word(e,io,r_sym), \
seq(copy_byte(e,io,r_ssym), \
seq(copy_byte(e,io,r_type3), \
seq(copy_byte(e,io,r_type2), \
seq(copy_byte(e,io,r_type), \
nullcopy))))))
#else /* __LIBELF64_IRIX */
#define copy_rela_11(e,io,seq) \
seq(copy_addr(e,io,r_offset), \
seq(copy_xword(e,io,r_info), \
seq(copy_sxword(e,io,r_addend), \
nullcopy)))
#define copy_rel_11(e,io,seq) \
seq(copy_addr(e,io,r_offset), \
seq(copy_xword(e,io,r_info), \
nullcopy))
#endif /* __LIBELF64_IRIX */
#define copy_shdr_11(e,io,seq) \
seq(copy_word(e,io,sh_name), \
seq(copy_word(e,io,sh_type), \
seq(copy_xword(e,io,sh_flags), \
seq(copy_addr(e,io,sh_addr), \
seq(copy_off(e,io,sh_offset), \
seq(copy_xword(e,io,sh_size), \
seq(copy_word(e,io,sh_link), \
seq(copy_word(e,io,sh_info), \
seq(copy_xword(e,io,sh_addralign), \
seq(copy_xword(e,io,sh_entsize), \
nullcopy))))))))))
#define copy_sym_11(e,io,seq) \
seq(copy_word(e,io,st_name), \
seq(copy_byte(e,io,st_info), \
seq(copy_byte(e,io,st_other), \
seq(copy_half(e,io,st_shndx), \
seq(copy_addr(e,io,st_value), \
seq(copy_xword(e,io,st_size), \
nullcopy))))))
#define nullcopy /**/
static size_t
byte_copy(unsigned char *dst, const unsigned char *src, size_t n) {
if (n && dst && dst != src) {
#if HAVE_BROKEN_MEMMOVE
size_t i;
if (dst >= src + n || dst + n <= src) {
memcpy(dst, src, n);
}
else if (dst < src) {
for (i = 0; i < n; i++) {
dst[i] = src[i];
}
}
else {
for (i = n; --i; ) {
dst[i] = src[i];
}
}
#else /* HAVE_BROKEN_MEMMOVE */
memmove(dst, src, n);
#endif /* HAVE_BROKEN_MEMMOVE */
}
return n;
}
static void
array_copy(unsigned char *dst, size_t dlen, const unsigned char *src, size_t slen) {
byte_copy(dst, src, dlen < slen ? dlen : slen);
if (dlen > slen) {
memset(dst + slen, 0, dlen - slen);
}
}
/*
* instantiate copy functions
*/
copy_type(addr_64,_,Elf64_Addr,copy_addr_11)
copy_type(half_64,_,Elf64_Half,copy_half_11)
copy_type(off_64,_,Elf64_Off,copy_off_11)
copy_type(sword_64,_,Elf64_Sword,copy_sword_11)
copy_type(word_64,_,Elf64_Word,copy_word_11)
copy_type(sxword_64,_,Elf64_Sxword,copy_sxword_11)
copy_type(xword_64,_,Elf64_Xword,copy_xword_11)
copy_type(dyn_64,11,Elf64_Dyn,copy_dyn_11)
copy_type(ehdr_64,11,Elf64_Ehdr,copy_ehdr_11)
copy_type(phdr_64,11,Elf64_Phdr,copy_phdr_11)
copy_type(rela_64,11,Elf64_Rela,copy_rela_11)
copy_type(rel_64,11,Elf64_Rel,copy_rel_11)
copy_type(shdr_64,11,Elf64_Shdr,copy_shdr_11)
copy_type(sym_64,11,Elf64_Sym,copy_sym_11)
typedef size_t (*xlator)(unsigned char*, const unsigned char*, size_t);
typedef xlator xltab[ELF_T_NUM][2];
/*
* translation table (64-bit, version 1 -> version 1)
*/
#if PIC
static xltab
#else /* PIC */
static const xltab
#endif /* PIC */
xlate64_11[/*encoding*/] = {
{
{ byte_copy, byte_copy },
{ addr_64L__tom, addr_64L__tof },
{ dyn_64L11_tom, dyn_64L11_tof },
{ ehdr_64L11_tom, ehdr_64L11_tof },
{ half_64L__tom, half_64L__tof },
{ off_64L__tom, off_64L__tof },
{ phdr_64L11_tom, phdr_64L11_tof },
{ rela_64L11_tom, rela_64L11_tof },
{ rel_64L11_tom, rel_64L11_tof },
{ shdr_64L11_tom, shdr_64L11_tof },
{ sword_64L__tom, sword_64L__tof },
{ sym_64L11_tom, sym_64L11_tof },
{ word_64L__tom, word_64L__tof },
{ sxword_64L__tom, sxword_64L__tof },
{ xword_64L__tom, xword_64L__tof },
#if __LIBELF_SYMBOL_VERSIONS
{ _elf_verdef_64L11_tom, _elf_verdef_64L11_tof },
{ _elf_verneed_64L11_tom, _elf_verneed_64L11_tof },
#else /* __LIBELF_SYMBOL_VERSIONS */
{ 0, 0 },
{ 0, 0 },
#endif /* __LIBELF_SYMBOL_VERSIONS */
},
{
{ byte_copy, byte_copy },
{ addr_64M__tom, addr_64M__tof },
{ dyn_64M11_tom, dyn_64M11_tof },
{ ehdr_64M11_tom, ehdr_64M11_tof },
{ half_64M__tom, half_64M__tof },
{ off_64M__tom, off_64M__tof },
{ phdr_64M11_tom, phdr_64M11_tof },
{ rela_64M11_tom, rela_64M11_tof },
{ rel_64M11_tom, rel_64M11_tof },
{ shdr_64M11_tom, shdr_64M11_tof },
{ sword_64M__tom, sword_64M__tof },
{ sym_64M11_tom, sym_64M11_tof },
{ word_64M__tom, word_64M__tof },
{ sxword_64M__tom, sxword_64M__tof },
{ xword_64M__tom, xword_64M__tof },
#if __LIBELF_SYMBOL_VERSIONS
{ _elf_verdef_64M11_tom, _elf_verdef_64M11_tof },
{ _elf_verneed_64M11_tom, _elf_verneed_64M11_tof },
#else /* __LIBELF_SYMBOL_VERSIONS */
{ 0, 0 },
{ 0, 0 },
#endif /* __LIBELF_SYMBOL_VERSIONS */
},
};
/*
* main translation table (64-bit)
*/
#if PIC
static xltab*
#else /* PIC */
static const xltab *const
#endif /* PIC */
xlate64[EV_CURRENT - EV_NONE][EV_CURRENT - EV_NONE] = {
{ xlate64_11, },
};
#define translator(sv,dv,enc,type,d) \
(xlate64[(sv) - EV_NONE - 1] \
[(dv) - EV_NONE - 1] \
[(enc) - ELFDATA2LSB] \
[(type) - ELF_T_BYTE] \
[d])
/*
* destination buffer size
*/
size_t
_elf64_xltsize(const Elf_Data *src, unsigned dv, unsigned encode, int tof) {
Elf_Type type = src->d_type;
unsigned sv = src->d_version;
xlator op;
if (!valid_version(sv) || !valid_version(dv)) {
seterr(ERROR_UNKNOWN_VERSION);
return (size_t)-1;
}
if (tof) {
/*
* Encoding doesn't really matter (the translator only looks at
* the source, which resides in memory), but we need a proper
* encoding to select a translator...
*/
encode = ELFDATA2LSB;
}
else if (!valid_encoding(encode)) {
seterr(ERROR_UNKNOWN_ENCODING);
return (size_t)-1;
}
if (!valid_type(type)) {
seterr(ERROR_UNKNOWN_TYPE);
return (size_t)-1;
}
if (!(op = translator(sv, dv, encode, type, tof))) {
seterr(ERROR_UNKNOWN_TYPE);
return (size_t)-1;
}
return (*op)(NULL, src->d_buf, src->d_size);
}
/*
* direction-independent translation
*/
static Elf_Data*
elf64_xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof) {
Elf_Type type;
int dv;
int sv;
size_t dsize;
size_t tmp;
xlator op;
if (!src || !dst) {
return NULL;
}
if (!src->d_buf || !dst->d_buf) {
seterr(ERROR_NULLBUF);
return NULL;
}
if (!valid_encoding(encode)) {
seterr(ERROR_UNKNOWN_ENCODING);
return NULL;
}
sv = src->d_version;
dv = dst->d_version;
if (!valid_version(sv) || !valid_version(dv)) {
seterr(ERROR_UNKNOWN_VERSION);
return NULL;
}
type = src->d_type;
if (!valid_type(type)) {
seterr(ERROR_UNKNOWN_TYPE);
return NULL;
}
op = translator(sv, dv, encode, type, tof);
if (!op) {
seterr(ERROR_UNKNOWN_TYPE);
return NULL;
}
dsize = (*op)(NULL, src->d_buf, src->d_size);
if (dsize == (size_t)-1) {
return NULL;
}
if (dst->d_size < dsize) {
seterr(ERROR_DST2SMALL);
return NULL;
}
if (dsize) {
tmp = (*op)(dst->d_buf, src->d_buf, src->d_size);
if (tmp == (size_t)-1) {
return NULL;
}
elf_assert(tmp == dsize);
}
dst->d_size = dsize;
dst->d_type = type;
return dst;
}
/*
* finally, the "official" translation functions
*/
Elf_Data*
elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode) {
return elf64_xlate(dst, src, encode, 0);
}
Elf_Data*
elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode) {
return elf64_xlate(dst, src, encode, 1);
}
Elf_Data*
gelf_xlatetom(Elf *elf, Elf_Data *dst, const Elf_Data *src, unsigned encode) {
if (elf) {
if (elf->e_kind != ELF_K_ELF) {
seterr(ERROR_NOTELF);
}
else if (elf->e_class == ELFCLASS32) {
return elf32_xlatetom(dst, src, encode);
}
else if (elf->e_class == ELFCLASS64) {
return elf64_xlatetom(dst, src, encode);
}
else if (valid_class(elf->e_class)) {
seterr(ERROR_UNIMPLEMENTED);
}
else {
seterr(ERROR_UNKNOWN_CLASS);
}
}
return NULL;
}
Elf_Data*
gelf_xlatetof(Elf *elf, Elf_Data *dst, const Elf_Data *src, unsigned encode) {
if (elf) {
if (elf->e_kind != ELF_K_ELF) {
seterr(ERROR_NOTELF);
}
else if (elf->e_class == ELFCLASS32) {
return elf32_xlatetof(dst, src, encode);
}
else if (elf->e_class == ELFCLASS64) {
return elf64_xlatetof(dst, src, encode);
}
else if (valid_class(elf->e_class)) {
seterr(ERROR_UNIMPLEMENTED);
}
else {
seterr(ERROR_UNKNOWN_CLASS);
}
}
return NULL;
}
#endif /* __LIBELF64__ */
|