2 * Copyright (c) 2006 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/elf32.h>
32 #include <sys/elf64.h>
40 /* WARNING: GENERATED FROM __file__. */
43 * Macros to swap various integral quantities.
46 #define SWAP_HALF(X) do { \
47 uint16_t _x = (uint16_t) (X); \
48 uint16_t _t = _x & 0xFF; \
49 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
52 #define SWAP_WORD(X) do { \
53 uint32_t _x = (uint32_t) (X); \
54 uint32_t _t = _x & 0xFF; \
55 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
56 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
57 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
60 #define SWAP_ADDR32(X) SWAP_WORD(X)
61 #define SWAP_OFF32(X) SWAP_WORD(X)
62 #define SWAP_SWORD(X) SWAP_WORD(X)
63 #define SWAP_WORD64(X) do { \
64 uint64_t _x = (uint64_t) (X); \
65 uint64_t _t = _x & 0xFF; \
66 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
67 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
68 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
69 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
70 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
71 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
72 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
75 #define SWAP_ADDR64(X) SWAP_WORD64(X)
76 #define SWAP_LWORD(X) SWAP_WORD64(X)
77 #define SWAP_OFF64(X) SWAP_WORD64(X)
78 #define SWAP_SXWORD(X) SWAP_WORD64(X)
79 #define SWAP_XWORD(X) SWAP_WORD64(X)
82 * Write out various integral values. The destination pointer could
83 * be unaligned. Values are written out in native byte order. The
84 * destination pointer is incremented after the write.
86 #define WRITE_BYTE(P,X) do { \
87 unsigned char *const _p = (unsigned char *) (P); \
88 _p[0] = (unsigned char) (X); \
91 #define WRITE_HALF(P,X) do { \
93 unsigned char *const _p = (unsigned char *) (P); \
94 unsigned const char *const _q = (unsigned char *) &_t; \
99 #define WRITE_WORD(P,X) do { \
101 unsigned char *const _p = (unsigned char *) (P); \
102 unsigned const char *const _q = (unsigned char *) &_t; \
109 #define WRITE_ADDR32(P,X) WRITE_WORD(P,X)
110 #define WRITE_OFF32(P,X) WRITE_WORD(P,X)
111 #define WRITE_SWORD(P,X) WRITE_WORD(P,X)
112 #define WRITE_WORD64(P,X) do { \
114 unsigned char *const _p = (unsigned char *) (P); \
115 unsigned const char *const _q = (unsigned char *) &_t; \
126 #define WRITE_ADDR64(P,X) WRITE_WORD64(P,X)
127 #define WRITE_LWORD(P,X) WRITE_WORD64(P,X)
128 #define WRITE_OFF64(P,X) WRITE_WORD64(P,X)
129 #define WRITE_SXWORD(P,X) WRITE_WORD64(P,X)
130 #define WRITE_XWORD(P,X) WRITE_WORD64(P,X)
131 #define WRITE_IDENT(P,X) do { \
132 (void) memcpy((P), (X), sizeof((X))); \
133 (P) = (P) + EI_NIDENT; \
137 * Read in various integral values. The source pointer could be
138 * unaligned. Values are read in in native byte order. The source
139 * pointer is incremented appropriately.
142 #define READ_BYTE(P,X) do { \
143 const unsigned char *const _p = \
144 (const unsigned char *) (P); \
148 #define READ_HALF(P,X) do { \
150 unsigned char *const _q = (unsigned char *) &_t; \
151 const unsigned char *const _p = \
152 (const unsigned char *) (P); \
158 #define READ_WORD(P,X) do { \
160 unsigned char *const _q = (unsigned char *) &_t; \
161 const unsigned char *const _p = \
162 (const unsigned char *) (P); \
170 #define READ_ADDR32(P,X) READ_WORD(P,X)
171 #define READ_OFF32(P,X) READ_WORD(P,X)
172 #define READ_SWORD(P,X) READ_WORD(P,X)
173 #define READ_WORD64(P,X) do { \
175 unsigned char *const _q = (unsigned char *) &_t; \
176 const unsigned char *const _p = \
177 (const unsigned char *) (P); \
189 #define READ_ADDR64(P,X) READ_WORD64(P,X)
190 #define READ_LWORD(P,X) READ_WORD64(P,X)
191 #define READ_OFF64(P,X) READ_WORD64(P,X)
192 #define READ_SXWORD(P,X) READ_WORD64(P,X)
193 #define READ_XWORD(P,X) READ_WORD64(P,X)
194 #define READ_IDENT(P,X) do { \
195 (void) memcpy((X), (P), sizeof((X))); \
196 (P) = (P) + EI_NIDENT; \
199 #define ROUNDUP2(V,N) (V) = ((((V) + (N) - 1)) & ~((N) - 1))
204 * Generate conversion routines for converting between in-memory and
205 * file representations of Elf data structures.
207 * `In-memory' representations of an Elf data structure use natural
208 * alignments and native byte ordering. This allows arithmetic and
209 * casting to work as expected. On the other hand the `file'
210 * representation of an ELF data structure could be packed tighter
211 * than its `in-memory' representation, and could be of a differing
212 * byte order. An additional complication is that `ar' only pads data
213 * to even addresses and so ELF archive member data being read from
214 * inside an `ar' archive could end up at misaligned memory addresses.
216 * Consequently, casting the `char *' pointers that point to memory
217 * representations (i.e., source pointers for the *_tof() functions
218 * and the destination pointers for the *_tom() functions), is safe,
219 * as these pointers should be correctly aligned for the memory type
220 * already. However, pointers to file representations have to be
221 * treated as being potentially unaligned and no casting can be done.
224 include(SRCDIR`/elf_types.m4')
227 * `IGNORE'_* flags turn off generation of template code.
231 `define(IGNORE_$1`'32, 1)
232 define(IGNORE_$1`'64, 1)')
237 ifelse(eval(OSRELDATE < 700009),1,
243 define(`IGNORE_LWORD', 1)',
246 define(IGNORE_BYTE, 1) /* 'lator, leave 'em bytes alone */
247 define(IGNORE_NOTE, 1)
248 define(IGNORE_SXWORD32, 1)
249 define(IGNORE_XWORD32, 1)
252 * `BASE'_XXX flags cause class agnostic template functions
256 define(`BASE_BYTE', 1)
257 define(`BASE_HALF', 1)
258 define(`BASE_NOTE', 1)
259 define(`BASE_WORD', 1)
260 define(`BASE_LWORD', 1)
261 define(`BASE_SWORD', 1)
262 define(`BASE_XWORD', 1)
263 define(`BASE_SXWORD', 1)
266 * `SIZEDEP'_XXX flags cause 32/64 bit variants to be generated
267 * for each primitive type.
270 define(`SIZEDEP_ADDR', 1)
271 define(`SIZEDEP_OFF', 1)
274 * `Primitive' ELF types are those that are an alias for an integral
275 * type. They have no internal structure. These can be copied using
276 * a `memcpy()', and byteswapped in straightforward way.
279 * `$1': Name of the ELF type.
280 * `$2': C structure name suffix
281 * `$3': ELF class specifier for symbols, one of [`', `32', `64']
282 * `$4': ELF class specifier for types, one of [`32', `64']
284 define(`MAKEPRIM_TO_F',`
286 libelf_cvt_$1$3_tof(char *dst, char *src, size_t count, int byteswap)
288 Elf$4_$2 t, *s = (Elf$4_$2 *) (uintptr_t) src;
291 if (dst == src && !byteswap)
295 (void) memcpy(dst, src, count * sizeof(*s));
299 for (c = 0; c < count; c++) {
307 define(`MAKEPRIM_TO_M',`
309 libelf_cvt_$1$3_tom(char *dst, char *src, size_t count, int byteswap)
311 Elf$4_$2 t, *d = (Elf$4_$2 *) (uintptr_t) dst;
314 if (dst == src && !byteswap)
318 (void) memcpy(dst, src, count * sizeof(*d));
322 for (c = 0; c < count; c++) {
331 `ifdef(`IGNORE_'$2,`',
337 `SWAP_$2'SZ()`(t.$1);
339 define(`SWAP_MEMBERS',
341 `SWAP_FIELD($1)SWAP_MEMBERS(shift($@))')')
343 define(`SWAP_STRUCT',
344 `pushdef(`SZ',$2)/* Swap an Elf$2_$1 */
345 SWAP_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
347 define(`WRITE_FIELD',
354 `WRITE_$2'SZ()`(dst,t.$1);
356 define(`WRITE_MEMBERS',
358 `WRITE_FIELD($1)WRITE_MEMBERS(shift($@))')')
360 define(`WRITE_STRUCT',
361 `pushdef(`SZ',$2)/* Write an Elf$2_$1 */
362 WRITE_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
371 `READ_$2'SZ()`(s,t.$1);
374 define(`READ_MEMBERS',
376 `READ_FIELD($1)READ_MEMBERS(shift($@))')')
378 define(`READ_STRUCT',
379 `pushdef(`SZ',$2)/* Read an Elf$2_$1 */
380 READ_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
383 * Converters for non-integral ELF data structures.
385 * When converting data to file representation, the source pointer
386 * will be naturally aligned for a data structure's in-memory
387 * representation. When converting data to memory, the destination
388 * pointer will be similarly aligned.
390 * For in-place conversions, when converting to file representations,
391 * the source buffer is large enough to hold `file' data. When
392 * converting from file to memory, we need to be careful to work
393 * `backwards', to avoid overwriting unconverted data.
396 * `$1': Name of the ELF type.
397 * `$2': C structure name suffix.
398 * `$3': ELF class specifier, one of [`', `32', `64']
402 `ifdef(`IGNORE_'$1$3,`',`
404 libelf_cvt$3_$1_tof(char *dst, char *src, size_t count, int byteswap)
409 s = (Elf$3_$2 *) (uintptr_t) src;
410 for (c = 0; c < count; c++) {
421 `ifdef(`IGNORE_'$1$3,`',`
423 libelf_cvt$3_$1_tom(char *dst, char *src, size_t count, int byteswap)
426 unsigned char *s,*s0;
429 fsz = elf$3_fsize(ELF_T_$1, (size_t) 1, EV_CURRENT);
430 d = ((Elf$3_$2 *) (uintptr_t) dst) + (count - 1);
431 s0 = (unsigned char *) src + (count - 1) * fsz;
445 * Make type convertor functions from the type definition
447 * - if the type is a base (i.e., `primitive') type:
448 * - if it is marked as to be ignored (i.e., `IGNORE_'TYPE)
449 * is defined, we skip the code generation step.
450 * - if the type is declared as `SIZEDEP', then 32 and 64 bit
451 * variants of the conversion functions are generated.
452 * - otherwise a 32 bit variant is generated.
453 * - if the type is a structure type, we generate 32 and 64 bit
454 * variants of the conversion functions.
457 define(`MAKE_TYPE_CONVERTER',
459 `ifdef(`IGNORE_'$1,`',
460 `MAKEPRIM_TO_F($1,$2,`',64)
461 MAKEPRIM_TO_M($1,$2,`',64)')',
463 `MAKEPRIM_TO_F($1,$2,32,32)dnl
464 MAKEPRIM_TO_M($1,$2,32,32)dnl
465 MAKEPRIM_TO_F($1,$2,64,64)dnl
466 MAKEPRIM_TO_M($1,$2,64,64)',
467 `MAKE_TO_F($1,$2,32)dnl
468 MAKE_TO_F($1,$2,64)dnl
469 MAKE_TO_M($1,$2,32)dnl
470 MAKE_TO_M($1,$2,64)')')')
472 define(`MAKE_TYPE_CONVERTERS',
474 `MAKE_TYPE_CONVERTER($1)MAKE_TYPE_CONVERTERS(shift($@))')')
479 * Sections of type ELF_T_BYTE are never byteswapped, consequently a
480 * simple memcpy suffices for both directions of conversion.
484 libelf_cvt_BYTE_tox(char *dst, char *src, size_t count, int byteswap)
488 (void) memcpy(dst, src, count);
492 * Elf_Note structures comprise a fixed size header followed by variable
493 * length strings. The fixed size header needs to be byte swapped, but
496 * Argument `count' denotes the total number of bytes to be converted.
499 libelf_cvt_NOTE_tom(char *dst, char *src, size_t count, int byteswap)
501 uint32_t namesz, descsz, type;
505 if (dst == src && !byteswap)
509 (void) memcpy(dst, src, count);
513 while (count > sizeof(Elf_Note)) {
515 READ_WORD(src, namesz);
516 READ_WORD(src, descsz);
517 READ_WORD(src, type);
525 en = (Elf_Note *) (uintptr_t) dst;
526 en->n_namesz = namesz;
527 en->n_descsz = descsz;
530 dst += sizeof(Elf_Note);
535 sz = namesz + descsz;
540 (void) memcpy(dst, src, sz);
549 libelf_cvt_NOTE_tof(char *dst, char *src, size_t count, int byteswap)
551 uint32_t namesz, descsz, type;
555 if (dst == src && !byteswap)
559 (void) memcpy(dst, src, count);
563 while (count > sizeof(Elf_Note)) {
565 en = (Elf_Note *) (uintptr_t) src;
566 namesz = en->n_namesz;
567 descsz = en->n_descsz;
577 WRITE_WORD(dst, namesz);
578 WRITE_WORD(dst, descsz);
579 WRITE_WORD(dst, type);
581 src += sizeof(Elf_Note);
586 sz = namesz + descsz;
591 (void) memcpy(dst, src, sz);
599 MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
602 void (*tof32)(char *dst, char *src, size_t cnt, int byteswap);
603 void (*tom32)(char *dst, char *src, size_t cnt, int byteswap);
604 void (*tof64)(char *dst, char *src, size_t cnt, int byteswap);
605 void (*tom64)(char *dst, char *src, size_t cnt, int byteswap);
610 `ifdef(`IGNORE_'$1$2,
615 `.$3$2 = libelf_cvt_$1_$3')',
617 `.$3$2 = libelf_cvt_$1$2_$3',
618 `.$3$2 = libelf_cvt$2_$1_$3')')')')
620 define(`CONVERTER_NAME',
622 CONV($1,32,tof), CONV($1,32,tom),
623 CONV($1,64,tof), CONV($1,64,tom) },
626 define(`CONVERTER_NAMES',
628 `CONVERTER_NAME($1)CONVERTER_NAMES(shift($@))')')
630 undefine(`IGNORE_BYTE32', `IGNORE_BYTE64')
633 static struct converters cvt[ELF_T_NUM] = {
634 CONVERTER_NAMES(ELF_TYPE_LIST)
637 * Types that needs hand-coded converters follow.
641 .tof32 = libelf_cvt_BYTE_tox,
642 .tom32 = libelf_cvt_BYTE_tox,
643 .tof64 = libelf_cvt_BYTE_tox,
644 .tom64 = libelf_cvt_BYTE_tox
647 .tof32 = libelf_cvt_NOTE_tof,
648 .tom32 = libelf_cvt_NOTE_tom,
649 .tof64 = libelf_cvt_NOTE_tof,
650 .tom64 = libelf_cvt_NOTE_tom
654 void (*_libelf_get_translator(Elf_Type t, int direction, int elfclass))
655 (char *_dst, char *_src, size_t _cnt, int _byteswap)
657 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
658 assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
660 if (t >= ELF_T_NUM ||
661 (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) ||
662 (direction != ELF_TOFILE && direction != ELF_TOMEMORY))
665 return ((elfclass == ELFCLASS32) ?
666 (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) :
667 (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64));