2 * Copyright (c) 2006-2008 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>
36 #include <osreldate.h>
41 /* WARNING: GENERATED FROM __file__. */
44 * Macros to swap various integral quantities.
47 #define SWAP_HALF(X) do { \
48 uint16_t _x = (uint16_t) (X); \
49 uint16_t _t = _x & 0xFF; \
50 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
53 #define SWAP_WORD(X) do { \
54 uint32_t _x = (uint32_t) (X); \
55 uint32_t _t = _x & 0xFF; \
56 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
57 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
58 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
61 #define SWAP_ADDR32(X) SWAP_WORD(X)
62 #define SWAP_OFF32(X) SWAP_WORD(X)
63 #define SWAP_SWORD(X) SWAP_WORD(X)
64 #define SWAP_WORD64(X) do { \
65 uint64_t _x = (uint64_t) (X); \
66 uint64_t _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; \
73 _t <<= 8; _x >>= 8; _t |= _x & 0xFF; \
76 #define SWAP_ADDR64(X) SWAP_WORD64(X)
77 #define SWAP_LWORD(X) SWAP_WORD64(X)
78 #define SWAP_OFF64(X) SWAP_WORD64(X)
79 #define SWAP_SXWORD(X) SWAP_WORD64(X)
80 #define SWAP_XWORD(X) SWAP_WORD64(X)
83 * Write out various integral values. The destination pointer could
84 * be unaligned. Values are written out in native byte order. The
85 * destination pointer is incremented after the write.
87 #define WRITE_BYTE(P,X) do { \
88 char *const _p = (char *) (P); \
92 #define WRITE_HALF(P,X) do { \
94 char *const _p = (char *) (P); \
95 const char *const _q = (char *) &_t; \
100 #define WRITE_WORD(P,X) do { \
102 char *const _p = (char *) (P); \
103 const char *const _q = (char *) &_t; \
110 #define WRITE_ADDR32(P,X) WRITE_WORD(P,X)
111 #define WRITE_OFF32(P,X) WRITE_WORD(P,X)
112 #define WRITE_SWORD(P,X) WRITE_WORD(P,X)
113 #define WRITE_WORD64(P,X) do { \
115 char *const _p = (char *) (P); \
116 const char *const _q = (char *) &_t; \
127 #define WRITE_ADDR64(P,X) WRITE_WORD64(P,X)
128 #define WRITE_LWORD(P,X) WRITE_WORD64(P,X)
129 #define WRITE_OFF64(P,X) WRITE_WORD64(P,X)
130 #define WRITE_SXWORD(P,X) WRITE_WORD64(P,X)
131 #define WRITE_XWORD(P,X) WRITE_WORD64(P,X)
132 #define WRITE_IDENT(P,X) do { \
133 (void) memcpy((P), (X), sizeof((X))); \
134 (P) = (P) + EI_NIDENT; \
138 * Read in various integral values. The source pointer could be
139 * unaligned. Values are read in native byte order. The source
140 * pointer is incremented appropriately.
143 #define READ_BYTE(P,X) do { \
144 const char *const _p = \
145 (const char *) (P); \
149 #define READ_HALF(P,X) do { \
151 char *const _q = (char *) &_t; \
152 const char *const _p = \
153 (const char *) (P); \
159 #define READ_WORD(P,X) do { \
161 char *const _q = (char *) &_t; \
162 const char *const _p = \
163 (const char *) (P); \
171 #define READ_ADDR32(P,X) READ_WORD(P,X)
172 #define READ_OFF32(P,X) READ_WORD(P,X)
173 #define READ_SWORD(P,X) READ_WORD(P,X)
174 #define READ_WORD64(P,X) do { \
176 char *const _q = (char *) &_t; \
177 const char *const _p = \
178 (const char *) (P); \
190 #define READ_ADDR64(P,X) READ_WORD64(P,X)
191 #define READ_LWORD(P,X) READ_WORD64(P,X)
192 #define READ_OFF64(P,X) READ_WORD64(P,X)
193 #define READ_SXWORD(P,X) READ_WORD64(P,X)
194 #define READ_XWORD(P,X) READ_WORD64(P,X)
195 #define READ_IDENT(P,X) do { \
196 (void) memcpy((X), (P), sizeof((X))); \
197 (P) = (P) + EI_NIDENT; \
200 #define ROUNDUP2(V,N) (V) = ((((V) + (N) - 1)) & ~((N) - 1))
205 * Generate conversion routines for converting between in-memory and
206 * file representations of Elf data structures.
208 * `In-memory' representations of an Elf data structure use natural
209 * alignments and native byte ordering. This allows arithmetic and
210 * casting to work as expected. On the other hand the `file'
211 * representation of an ELF data structure could be packed tighter
212 * than its `in-memory' representation, and could be of a differing
213 * byte order. An additional complication is that `ar' only pads data
214 * to even addresses and so ELF archive member data being read from
215 * inside an `ar' archive could end up at misaligned memory addresses.
217 * Consequently, casting the `char *' pointers that point to memory
218 * representations (i.e., source pointers for the *_tof() functions
219 * and the destination pointers for the *_tom() functions), is safe,
220 * as these pointers should be correctly aligned for the memory type
221 * already. However, pointers to file representations have to be
222 * treated as being potentially unaligned and no casting can be done.
225 include(SRCDIR`/elf_types.m4')
228 * `IGNORE'_* flags turn off generation of template code.
232 `define(IGNORE_$1`'32, 1)
233 define(IGNORE_$1`'64, 1)')
239 define(IGNORE_BYTE, 1) /* 'lator, leave 'em bytes alone */
240 define(IGNORE_GNUHASH, 1)
241 define(IGNORE_NOTE, 1)
242 define(IGNORE_SXWORD32, 1)
243 define(IGNORE_XWORD32, 1)
246 * `BASE'_XXX flags cause class agnostic template functions
250 define(`BASE_BYTE', 1)
251 define(`BASE_HALF', 1)
252 define(`BASE_NOTE', 1)
253 define(`BASE_WORD', 1)
254 define(`BASE_LWORD', 1)
255 define(`BASE_SWORD', 1)
256 define(`BASE_XWORD', 1)
257 define(`BASE_SXWORD', 1)
260 * `SIZEDEP'_XXX flags cause 32/64 bit variants to be generated
261 * for each primitive type.
264 define(`SIZEDEP_ADDR', 1)
265 define(`SIZEDEP_OFF', 1)
268 * `Primitive' ELF types are those that are an alias for an integral
269 * type. They have no internal structure. These can be copied using
270 * a `memcpy()', and byteswapped in straightforward way.
273 * `$1': Name of the ELF type.
274 * `$2': C structure name suffix
275 * `$3': ELF class specifier for symbols, one of [`', `32', `64']
276 * `$4': ELF class specifier for types, one of [`32', `64']
278 define(`MAKEPRIM_TO_F',`
280 libelf_cvt_$1$3_tof(char *dst, size_t dsz, char *src, size_t count,
283 Elf$4_$2 t, *s = (Elf$4_$2 *) (uintptr_t) src;
289 (void) memcpy(dst, src, count * sizeof(*s));
293 for (c = 0; c < count; c++) {
303 define(`MAKEPRIM_TO_M',`
305 libelf_cvt_$1$3_tom(char *dst, size_t dsz, char *src, size_t count,
308 Elf$4_$2 t, *d = (Elf$4_$2 *) (uintptr_t) dst;
311 if (dsz < count * sizeof(Elf$4_$2))
315 (void) memcpy(dst, src, count * sizeof(*d));
319 for (c = 0; c < count; c++) {
330 `ifdef(`IGNORE_'$2,`',
336 `SWAP_$2'SZ()`(t.$1);
338 define(`SWAP_MEMBERS',
340 `SWAP_FIELD($1)SWAP_MEMBERS(shift($@))')')
342 define(`SWAP_STRUCT',
343 `pushdef(`SZ',$2)/* Swap an Elf$2_$1 */
344 SWAP_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
346 define(`WRITE_FIELD',
353 `WRITE_$2'SZ()`(dst,t.$1);
355 define(`WRITE_MEMBERS',
357 `WRITE_FIELD($1)WRITE_MEMBERS(shift($@))')')
359 define(`WRITE_STRUCT',
360 `pushdef(`SZ',$2)/* Write an Elf$2_$1 */
361 WRITE_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
370 `READ_$2'SZ()`(s,t.$1);
373 define(`READ_MEMBERS',
375 `READ_FIELD($1)READ_MEMBERS(shift($@))')')
377 define(`READ_STRUCT',
378 `pushdef(`SZ',$2)/* Read an Elf$2_$1 */
379 READ_MEMBERS(Elf$2_$1_DEF)popdef(`SZ')')
382 * Converters for non-integral ELF data structures.
384 * When converting data to file representation, the source pointer
385 * will be naturally aligned for a data structure's in-memory
386 * representation. When converting data to memory, the destination
387 * pointer will be similarly aligned.
389 * For in-place conversions, when converting to file representations,
390 * the source buffer is large enough to hold `file' data. When
391 * converting from file to memory, we need to be careful to work
392 * `backwards', to avoid overwriting unconverted data.
395 * `$1': Name of the ELF type.
396 * `$2': C structure name suffix.
397 * `$3': ELF class specifier, one of [`', `32', `64']
401 `ifdef(`IGNORE_'$1$3,`',`
403 libelf_cvt$3_$1_tof(char *dst, size_t dsz, char *src, size_t count,
411 s = (Elf$3_$2 *) (uintptr_t) src;
412 for (c = 0; c < count; c++) {
425 `ifdef(`IGNORE_'$1$3,`',`
427 libelf_cvt$3_$1_tom(char *dst, size_t dsz, char *src, size_t count,
434 fsz = elf$3_fsize(ELF_T_$1, (size_t) 1, EV_CURRENT);
435 d = ((Elf$3_$2 *) (uintptr_t) dst) + (count - 1);
436 s0 = (char *) src + (count - 1) * fsz;
438 if (dsz < count * sizeof(Elf$3_$2))
455 * Make type convertor functions from the type definition
457 * - if the type is a base (i.e., `primitive') type:
458 * - if it is marked as to be ignored (i.e., `IGNORE_'TYPE)
459 * is defined, we skip the code generation step.
460 * - if the type is declared as `SIZEDEP', then 32 and 64 bit
461 * variants of the conversion functions are generated.
462 * - otherwise a 32 bit variant is generated.
463 * - if the type is a structure type, we generate 32 and 64 bit
464 * variants of the conversion functions.
467 define(`MAKE_TYPE_CONVERTER',
468 `#if __FreeBSD_version >= $3 /* $1 */
470 `ifdef(`IGNORE_'$1,`',
471 `MAKEPRIM_TO_F($1,$2,`',64)
472 MAKEPRIM_TO_M($1,$2,`',64)')',
474 `MAKEPRIM_TO_F($1,$2,32,32)dnl
475 MAKEPRIM_TO_M($1,$2,32,32)dnl
476 MAKEPRIM_TO_F($1,$2,64,64)dnl
477 MAKEPRIM_TO_M($1,$2,64,64)',
478 `MAKE_TO_F($1,$2,32)dnl
479 MAKE_TO_F($1,$2,64)dnl
480 MAKE_TO_M($1,$2,32)dnl
481 MAKE_TO_M($1,$2,64)')')
485 define(`MAKE_TYPE_CONVERTERS',
487 `MAKE_TYPE_CONVERTER($1)MAKE_TYPE_CONVERTERS(shift($@))')')
492 * Sections of type ELF_T_BYTE are never byteswapped, consequently a
493 * simple memcpy suffices for both directions of conversion.
497 libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count,
504 (void) memcpy(dst, src, count);
508 MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
510 #if __FreeBSD_version >= 800062
512 * Sections of type ELF_T_GNUHASH start with a header containing 4 32-bit
513 * words. Bloom filter data comes next, followed by hash buckets and the
516 * Bloom filter words are 64 bit wide on ELFCLASS64 objects and are 32 bit
517 * wide on ELFCLASS32 objects. The other objects in this section are 32
520 * Argument `srcsz' denotes the number of bytes to be converted. In the
521 * 32-bit case we need to translate `srcsz' to a count of 32-bit words.
525 libelf_cvt32_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz,
528 return (libelf_cvt_WORD_tom(dst, dsz, src, srcsz / sizeof(uint32_t),
533 libelf_cvt32_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz,
536 return (libelf_cvt_WORD_tof(dst, dsz, src, srcsz / sizeof(uint32_t),
541 libelf_cvt64_GNUHASH_tom(char *dst, size_t dsz, char *src, size_t srcsz,
545 uint64_t t64, *bloom64;
546 Elf_GNU_Hash_Header *gh;
547 uint32_t n, nbuckets, nchains, maskwords, shift2, symndx, t32;
548 uint32_t *buckets, *chains;
550 sz = 4 * sizeof(uint32_t); /* File header is 4 words long. */
551 if (dsz < sizeof(Elf_GNU_Hash_Header) || srcsz < sz)
554 /* Read in the section header and byteswap if needed. */
555 READ_WORD(src, nbuckets);
556 READ_WORD(src, symndx);
557 READ_WORD(src, maskwords);
558 READ_WORD(src, shift2);
565 SWAP_WORD(maskwords);
569 /* Check source buffer and destination buffer sizes. */
570 sz = nbuckets * sizeof(uint32_t) + maskwords * sizeof(uint64_t);
571 if (srcsz < sz || dsz < sz + sizeof(Elf_GNU_Hash_Header))
574 gh = (Elf_GNU_Hash_Header *) (uintptr_t) dst;
575 gh->gh_nbuckets = nbuckets;
576 gh->gh_symndx = symndx;
577 gh->gh_maskwords = maskwords;
578 gh->gh_shift2 = shift2;
580 dsz -= sizeof(Elf_GNU_Hash_Header);
581 dst += sizeof(Elf_GNU_Hash_Header);
583 bloom64 = (uint64_t *) (uintptr_t) dst;
585 /* Copy bloom filter data. */
586 for (n = 0; n < maskwords; n++) {
587 READ_XWORD(src, t64);
593 /* The hash buckets follows the bloom filter. */
594 dst += maskwords * sizeof(uint64_t);
595 buckets = (uint32_t *) (uintptr_t) dst;
597 for (n = 0; n < nbuckets; n++) {
604 dst += nbuckets * sizeof(uint32_t);
606 /* The hash chain follows the hash buckets. */
610 if (dsz < srcsz) /* Destination lacks space. */
613 nchains = srcsz / sizeof(uint32_t);
614 chains = (uint32_t *) (uintptr_t) dst;
616 for (n = 0; n < nchains; n++) {
627 libelf_cvt64_GNUHASH_tof(char *dst, size_t dsz, char *src, size_t srcsz,
633 Elf_GNU_Hash_Header *gh;
634 uint32_t maskwords, n, nbuckets, nchains, t0, t1, t2, t3, t32;
636 hdrsz = 4 * sizeof(uint32_t); /* Header is 4x32 bits. */
637 if (dsz < hdrsz || srcsz < sizeof(Elf_GNU_Hash_Header))
640 gh = (Elf_GNU_Hash_Header *) (uintptr_t) src;
642 t0 = nbuckets = gh->gh_nbuckets;
644 t2 = maskwords = gh->gh_maskwords;
647 src += sizeof(Elf_GNU_Hash_Header);
648 srcsz -= sizeof(Elf_GNU_Hash_Header);
651 sz = gh->gh_nbuckets * sizeof(uint32_t) + gh->gh_maskwords *
654 if (srcsz < sz || dsz < sz)
657 /* Write out the header. */
670 /* Copy the bloom filter and the hash table. */
671 s64 = (uint64_t *) (uintptr_t) src;
672 for (n = 0; n < maskwords; n++) {
676 WRITE_WORD64(dst, t64);
679 s32 = (uint32_t *) s64;
680 for (n = 0; n < nbuckets; n++) {
684 WRITE_WORD(dst, t32);
690 /* Copy out the hash chains. */
694 nchains = srcsz / sizeof(uint32_t);
695 for (n = 0; n < nchains; n++) {
699 WRITE_WORD(dst, t32);
707 * Elf_Note structures comprise a fixed size header followed by variable
708 * length strings. The fixed size header needs to be byte swapped, but
711 * Argument `count' denotes the total number of bytes to be converted.
712 * The destination buffer needs to be at least `count' bytes in size.
715 libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count,
718 uint32_t namesz, descsz, type;
722 if (dsz < count) /* Destination buffer is too small. */
725 hdrsz = 3 * sizeof(uint32_t);
726 if (count < hdrsz) /* Source too small. */
730 (void) memcpy(dst, src, count);
734 /* Process all notes in the section. */
735 while (count > hdrsz) {
736 /* Read the note header. */
737 READ_WORD(src, namesz);
738 READ_WORD(src, descsz);
739 READ_WORD(src, type);
746 /* Copy out the translated note header. */
747 en = (Elf_Note *) (uintptr_t) dst;
748 en->n_namesz = namesz;
749 en->n_descsz = descsz;
752 dsz -= sizeof(Elf_Note);
753 dst += sizeof(Elf_Note);
759 sz = namesz + descsz;
761 if (count < sz || dsz < sz) /* Buffers are too small. */
764 (void) memcpy(dst, src, sz);
777 libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count,
780 uint32_t namesz, descsz, type;
788 (void) memcpy(dst, src, count);
792 while (count > sizeof(Elf_Note)) {
794 en = (Elf_Note *) (uintptr_t) src;
795 namesz = en->n_namesz;
796 descsz = en->n_descsz;
803 WRITE_WORD(dst, namesz);
804 WRITE_WORD(dst, descsz);
805 WRITE_WORD(dst, type);
807 src += sizeof(Elf_Note);
812 sz = namesz + descsz;
817 (void) memcpy(dst, src, sz);
828 int (*tof32)(char *dst, size_t dsz, char *src, size_t cnt,
830 int (*tom32)(char *dst, size_t dsz, char *src, size_t cnt,
832 int (*tof64)(char *dst, size_t dsz, char *src, size_t cnt,
834 int (*tom64)(char *dst, size_t dsz, char *src, size_t cnt,
840 `ifdef(`IGNORE_'$1$2,
843 `.$3$2 = libelf_cvt_$1_$3',
845 `.$3$2 = libelf_cvt_$1$2_$3',
846 `.$3$2 = libelf_cvt$2_$1_$3')')')')
848 define(`CONVERTER_NAME',
849 `ifdef(`IGNORE_'$1,`',
850 `#if __FreeBSD_version >= $3
852 CONV($1,32,tof), CONV($1,32,tom),
853 CONV($1,64,tof), CONV($1,64,tom) },
857 define(`CONVERTER_NAMES',
859 `CONVERTER_NAME($1)CONVERTER_NAMES(shift($@))')')
861 undefine(`IGNORE_BYTE32', `IGNORE_BYTE64')
864 static struct converters cvt[ELF_T_NUM] = {
865 CONVERTER_NAMES(ELF_TYPE_LIST)
868 * Types that needs hand-coded converters follow.
872 .tof32 = libelf_cvt_BYTE_tox,
873 .tom32 = libelf_cvt_BYTE_tox,
874 .tof64 = libelf_cvt_BYTE_tox,
875 .tom64 = libelf_cvt_BYTE_tox
878 #if __FreeBSD_version >= 800062
880 .tof32 = libelf_cvt32_GNUHASH_tof,
881 .tom32 = libelf_cvt32_GNUHASH_tom,
882 .tof64 = libelf_cvt64_GNUHASH_tof,
883 .tom64 = libelf_cvt64_GNUHASH_tom
888 .tof32 = libelf_cvt_NOTE_tof,
889 .tom32 = libelf_cvt_NOTE_tom,
890 .tof64 = libelf_cvt_NOTE_tof,
891 .tom64 = libelf_cvt_NOTE_tom
895 int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass))
896 (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap)
898 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
899 assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
901 if (t >= ELF_T_NUM ||
902 (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) ||
903 (direction != ELF_TOFILE && direction != ELF_TOMEMORY))
906 return ((elfclass == ELFCLASS32) ?
907 (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) :
908 (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64));