Deleted Added
full compact
libelf_convert.m4 (177626) libelf_convert.m4 (210338)
1/*-
2 * Copyright (c) 2006,2007 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2006,2007 Joseph Koshy
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 11 unchanged lines hidden (view full) ---

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
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/lib/libelf/libelf_convert.m4 177626 2008-03-26 07:32:08Z brueffer $");
28__FBSDID("$FreeBSD: head/lib/libelf/libelf_convert.m4 210338 2010-07-21 10:25:02Z kaiw $");
29
30#include <sys/types.h>
31#include <sys/elf32.h>
32#include <sys/elf64.h>
33
34#include <assert.h>
35#include <libelf.h>
36#include <osreldate.h>

--- 194 unchanged lines hidden (view full) ---

231define(`IGNORE',
232 `define(IGNORE_$1`'32, 1)
233 define(IGNORE_$1`'64, 1)')
234
235IGNORE(MOVEP)
236IGNORE(NOTE)
237
238define(IGNORE_BYTE, 1) /* 'lator, leave 'em bytes alone */
29
30#include <sys/types.h>
31#include <sys/elf32.h>
32#include <sys/elf64.h>
33
34#include <assert.h>
35#include <libelf.h>
36#include <osreldate.h>

--- 194 unchanged lines hidden (view full) ---

231define(`IGNORE',
232 `define(IGNORE_$1`'32, 1)
233 define(IGNORE_$1`'64, 1)')
234
235IGNORE(MOVEP)
236IGNORE(NOTE)
237
238define(IGNORE_BYTE, 1) /* 'lator, leave 'em bytes alone */
239define(IGNORE_GNUHASH, 1)
239define(IGNORE_NOTE, 1)
240define(IGNORE_SXWORD32, 1)
241define(IGNORE_XWORD32, 1)
242
243/*
244 * `BASE'_XXX flags cause class agnostic template functions
245 * to be generated.
246 */

--- 22 unchanged lines hidden (view full) ---

269 *
270 * Macro use:
271 * `$1': Name of the ELF type.
272 * `$2': C structure name suffix
273 * `$3': ELF class specifier for symbols, one of [`', `32', `64']
274 * `$4': ELF class specifier for types, one of [`32', `64']
275 */
276define(`MAKEPRIM_TO_F',`
240define(IGNORE_NOTE, 1)
241define(IGNORE_SXWORD32, 1)
242define(IGNORE_XWORD32, 1)
243
244/*
245 * `BASE'_XXX flags cause class agnostic template functions
246 * to be generated.
247 */

--- 22 unchanged lines hidden (view full) ---

270 *
271 * Macro use:
272 * `$1': Name of the ELF type.
273 * `$2': C structure name suffix
274 * `$3': ELF class specifier for symbols, one of [`', `32', `64']
275 * `$4': ELF class specifier for types, one of [`32', `64']
276 */
277define(`MAKEPRIM_TO_F',`
277static void
278libelf_cvt_$1$3_tof(char *dst, char *src, size_t count, int byteswap)
278static int
279libelf_cvt_$1$3_tof(char *dst, size_t dsz, char *src, size_t count,
280 int byteswap)
279{
280 Elf$4_$2 t, *s = (Elf$4_$2 *) (uintptr_t) src;
281 size_t c;
282
281{
282 Elf$4_$2 t, *s = (Elf$4_$2 *) (uintptr_t) src;
283 size_t c;
284
283 if (dst == src && !byteswap)
284 return;
285 (void) dsz;
285
286 if (!byteswap) {
287 (void) memcpy(dst, src, count * sizeof(*s));
286
287 if (!byteswap) {
288 (void) memcpy(dst, src, count * sizeof(*s));
288 return;
289 return (1);
289 }
290
291 for (c = 0; c < count; c++) {
292 t = *s++;
293 SWAP_$1$3(t);
294 WRITE_$1$3(dst,t);
295 }
290 }
291
292 for (c = 0; c < count; c++) {
293 t = *s++;
294 SWAP_$1$3(t);
295 WRITE_$1$3(dst,t);
296 }
297
298 return (1);
296}
297')
298
299define(`MAKEPRIM_TO_M',`
299}
300')
301
302define(`MAKEPRIM_TO_M',`
300static void
301libelf_cvt_$1$3_tom(char *dst, char *src, size_t count, int byteswap)
303static int
304libelf_cvt_$1$3_tom(char *dst, size_t dsz, char *src, size_t count,
305 int byteswap)
302{
303 Elf$4_$2 t, *d = (Elf$4_$2 *) (uintptr_t) dst;
304 size_t c;
305
306{
307 Elf$4_$2 t, *d = (Elf$4_$2 *) (uintptr_t) dst;
308 size_t c;
309
306 if (dst == src && !byteswap)
307 return;
310 if (dsz < count * sizeof(Elf$4_$2))
311 return (0);
308
309 if (!byteswap) {
310 (void) memcpy(dst, src, count * sizeof(*d));
312
313 if (!byteswap) {
314 (void) memcpy(dst, src, count * sizeof(*d));
311 return;
315 return (1);
312 }
313
314 for (c = 0; c < count; c++) {
315 READ_$1$3(src,t);
316 SWAP_$1$3(t);
317 *d++ = t;
318 }
316 }
317
318 for (c = 0; c < count; c++) {
319 READ_$1$3(src,t);
320 SWAP_$1$3(t);
321 *d++ = t;
322 }
323
324 return (1);
319}
320')
321
322define(`SWAP_FIELD',
323 `ifdef(`IGNORE_'$2,`',
324 `ifelse(BASE_$2,1,
325 `SWAP_$2(t.$1);
326 ',

--- 60 unchanged lines hidden (view full) ---

387 * Macro use:
388 * `$1': Name of the ELF type.
389 * `$2': C structure name suffix.
390 * `$3': ELF class specifier, one of [`', `32', `64']
391 */
392
393define(`MAKE_TO_F',
394 `ifdef(`IGNORE_'$1$3,`',`
325}
326')
327
328define(`SWAP_FIELD',
329 `ifdef(`IGNORE_'$2,`',
330 `ifelse(BASE_$2,1,
331 `SWAP_$2(t.$1);
332 ',

--- 60 unchanged lines hidden (view full) ---

393 * Macro use:
394 * `$1': Name of the ELF type.
395 * `$2': C structure name suffix.
396 * `$3': ELF class specifier, one of [`', `32', `64']
397 */
398
399define(`MAKE_TO_F',
400 `ifdef(`IGNORE_'$1$3,`',`
395static void
396libelf_cvt$3_$1_tof(char *dst, char *src, size_t count, int byteswap)
401static int
402libelf_cvt$3_$1_tof(char *dst, size_t dsz, char *src, size_t count,
403 int byteswap)
397{
398 Elf$3_$2 t, *s;
399 size_t c;
400
404{
405 Elf$3_$2 t, *s;
406 size_t c;
407
408 (void) dsz;
409
401 s = (Elf$3_$2 *) (uintptr_t) src;
402 for (c = 0; c < count; c++) {
403 t = *s++;
404 if (byteswap) {
405 SWAP_STRUCT($2,$3)
406 }
407 WRITE_STRUCT($2,$3)
408 }
410 s = (Elf$3_$2 *) (uintptr_t) src;
411 for (c = 0; c < count; c++) {
412 t = *s++;
413 if (byteswap) {
414 SWAP_STRUCT($2,$3)
415 }
416 WRITE_STRUCT($2,$3)
417 }
418
419 return (1);
409}
410')')
411
412define(`MAKE_TO_M',
413 `ifdef(`IGNORE_'$1$3,`',`
420}
421')')
422
423define(`MAKE_TO_M',
424 `ifdef(`IGNORE_'$1$3,`',`
414static void
415libelf_cvt$3_$1_tom(char *dst, char *src, size_t count, int byteswap)
425static int
426libelf_cvt$3_$1_tom(char *dst, size_t dsz, char *src, size_t count,
427 int byteswap)
416{
417 Elf$3_$2 t, *d;
418 unsigned char *s,*s0;
419 size_t fsz;
420
421 fsz = elf$3_fsize(ELF_T_$1, (size_t) 1, EV_CURRENT);
422 d = ((Elf$3_$2 *) (uintptr_t) dst) + (count - 1);
423 s0 = (unsigned char *) src + (count - 1) * fsz;
424
428{
429 Elf$3_$2 t, *d;
430 unsigned char *s,*s0;
431 size_t fsz;
432
433 fsz = elf$3_fsize(ELF_T_$1, (size_t) 1, EV_CURRENT);
434 d = ((Elf$3_$2 *) (uintptr_t) dst) + (count - 1);
435 s0 = (unsigned char *) src + (count - 1) * fsz;
436
437 if (dsz < count * sizeof(Elf$3_$2))
438 return (0);
439
425 while (count--) {
426 s = s0;
427 READ_STRUCT($2,$3)
428 if (byteswap) {
429 SWAP_STRUCT($2,$3)
430 }
431 *d-- = t; s0 -= fsz;
432 }
440 while (count--) {
441 s = s0;
442 READ_STRUCT($2,$3)
443 if (byteswap) {
444 SWAP_STRUCT($2,$3)
445 }
446 *d-- = t; s0 -= fsz;
447 }
448
449 return (1);
433}
434')')
435
436/*
437 * Make type convertor functions from the type definition
438 * of the ELF type:
439 * - if the type is a base (i.e., `primitive') type:
440 * - if it is marked as to be ignored (i.e., `IGNORE_'TYPE)

--- 29 unchanged lines hidden (view full) ---

470
471divert(0)
472
473/*
474 * Sections of type ELF_T_BYTE are never byteswapped, consequently a
475 * simple memcpy suffices for both directions of conversion.
476 */
477
450}
451')')
452
453/*
454 * Make type convertor functions from the type definition
455 * of the ELF type:
456 * - if the type is a base (i.e., `primitive') type:
457 * - if it is marked as to be ignored (i.e., `IGNORE_'TYPE)

--- 29 unchanged lines hidden (view full) ---

487
488divert(0)
489
490/*
491 * Sections of type ELF_T_BYTE are never byteswapped, consequently a
492 * simple memcpy suffices for both directions of conversion.
493 */
494
478static void
479libelf_cvt_BYTE_tox(char *dst, char *src, size_t count, int byteswap)
495static int
496libelf_cvt_BYTE_tox(char *dst, size_t dsz, char *src, size_t count,
497 int byteswap)
480{
481 (void) byteswap;
498{
499 (void) byteswap;
500 if (dsz < count)
501 return (0);
482 if (dst != src)
483 (void) memcpy(dst, src, count);
502 if (dst != src)
503 (void) memcpy(dst, src, count);
504 return (1);
484}
485
486/*
487 * Elf_Note structures comprise a fixed size header followed by variable
488 * length strings. The fixed size header needs to be byte swapped, but
489 * not the strings.
490 *
491 * Argument `count' denotes the total number of bytes to be converted.
492 */
505}
506
507/*
508 * Elf_Note structures comprise a fixed size header followed by variable
509 * length strings. The fixed size header needs to be byte swapped, but
510 * not the strings.
511 *
512 * Argument `count' denotes the total number of bytes to be converted.
513 */
493static void
494libelf_cvt_NOTE_tom(char *dst, char *src, size_t count, int byteswap)
514static int
515libelf_cvt_NOTE_tom(char *dst, size_t dsz, char *src, size_t count,
516 int byteswap)
495{
496 uint32_t namesz, descsz, type;
497 Elf_Note *en;
517{
518 uint32_t namesz, descsz, type;
519 Elf_Note *en;
498 size_t sz;
520 size_t sz, hdrsz;
499
521
500 if (dst == src && !byteswap)
501 return;
522 if (dsz < count) /* Destination buffer is too small. */
523 return (0);
502
524
525 hdrsz = 3 * sizeof(uint32_t);
526 if (count < hdrsz) /* Source too small. */
527 return (0);
528
503 if (!byteswap) {
504 (void) memcpy(dst, src, count);
529 if (!byteswap) {
530 (void) memcpy(dst, src, count);
505 return;
531 return (1);
506 }
507
532 }
533
508 while (count > sizeof(Elf_Note)) {
509
534 /* Process all notes in the section. */
535 while (count > hdrsz) {
536 /* Read the note header. */
510 READ_WORD(src, namesz);
511 READ_WORD(src, descsz);
512 READ_WORD(src, type);
513
537 READ_WORD(src, namesz);
538 READ_WORD(src, descsz);
539 READ_WORD(src, type);
540
514 if (byteswap) {
515 SWAP_WORD(namesz);
516 SWAP_WORD(descsz);
517 SWAP_WORD(type);
518 }
541 /* Translate. */
542 SWAP_WORD(namesz);
543 SWAP_WORD(descsz);
544 SWAP_WORD(type);
519
545
546 /* Copy out the translated note header. */
520 en = (Elf_Note *) (uintptr_t) dst;
521 en->n_namesz = namesz;
522 en->n_descsz = descsz;
523 en->n_type = type;
524
547 en = (Elf_Note *) (uintptr_t) dst;
548 en->n_namesz = namesz;
549 en->n_descsz = descsz;
550 en->n_type = type;
551
552 dsz -= sizeof(Elf_Note);
525 dst += sizeof(Elf_Note);
553 dst += sizeof(Elf_Note);
554 count -= hdrsz;
526
527 ROUNDUP2(namesz, 4);
528 ROUNDUP2(descsz, 4);
529
530 sz = namesz + descsz;
531
555
556 ROUNDUP2(namesz, 4);
557 ROUNDUP2(descsz, 4);
558
559 sz = namesz + descsz;
560
532 if (count < sz)
533 sz = count;
561 if (count < sz || dsz < sz) /* Buffers are too small. */
562 return (0);
534
535 (void) memcpy(dst, src, sz);
536
537 src += sz;
538 dst += sz;
563
564 (void) memcpy(dst, src, sz);
565
566 src += sz;
567 dst += sz;
568
539 count -= sz;
540 }
569 count -= sz;
570 }
571
572 return (1);
541}
542
573}
574
543static void
544libelf_cvt_NOTE_tof(char *dst, char *src, size_t count, int byteswap)
575static int
576libelf_cvt_NOTE_tof(char *dst, size_t dsz, char *src, size_t count,
577 int byteswap)
545{
546 uint32_t namesz, descsz, type;
547 Elf_Note *en;
548 size_t sz;
549
578{
579 uint32_t namesz, descsz, type;
580 Elf_Note *en;
581 size_t sz;
582
550 if (dst == src && !byteswap)
551 return;
583 if (dsz < count)
584 return (0);
552
553 if (!byteswap) {
554 (void) memcpy(dst, src, count);
585
586 if (!byteswap) {
587 (void) memcpy(dst, src, count);
555 return;
588 return (1);
556 }
557
558 while (count > sizeof(Elf_Note)) {
559
560 en = (Elf_Note *) (uintptr_t) src;
561 namesz = en->n_namesz;
562 descsz = en->n_descsz;
563 type = en->n_type;
564
589 }
590
591 while (count > sizeof(Elf_Note)) {
592
593 en = (Elf_Note *) (uintptr_t) src;
594 namesz = en->n_namesz;
595 descsz = en->n_descsz;
596 type = en->n_type;
597
565 if (byteswap) {
566 SWAP_WORD(namesz);
567 SWAP_WORD(descsz);
568 SWAP_WORD(type);
569 }
598 SWAP_WORD(namesz);
599 SWAP_WORD(descsz);
600 SWAP_WORD(type);
570
601
571
572 WRITE_WORD(dst, namesz);
573 WRITE_WORD(dst, descsz);
574 WRITE_WORD(dst, type);
575
576 src += sizeof(Elf_Note);
577
578 ROUNDUP2(namesz, 4);
579 ROUNDUP2(descsz, 4);

--- 4 unchanged lines hidden (view full) ---

584 sz = count;
585
586 (void) memcpy(dst, src, sz);
587
588 src += sz;
589 dst += sz;
590 count -= sz;
591 }
602 WRITE_WORD(dst, namesz);
603 WRITE_WORD(dst, descsz);
604 WRITE_WORD(dst, type);
605
606 src += sizeof(Elf_Note);
607
608 ROUNDUP2(namesz, 4);
609 ROUNDUP2(descsz, 4);

--- 4 unchanged lines hidden (view full) ---

614 sz = count;
615
616 (void) memcpy(dst, src, sz);
617
618 src += sz;
619 dst += sz;
620 count -= sz;
621 }
622
623 return (1);
592}
593
594MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
595
596struct converters {
624}
625
626MAKE_TYPE_CONVERTERS(ELF_TYPE_LIST)
627
628struct converters {
597 void (*tof32)(char *dst, char *src, size_t cnt, int byteswap);
598 void (*tom32)(char *dst, char *src, size_t cnt, int byteswap);
599 void (*tof64)(char *dst, char *src, size_t cnt, int byteswap);
600 void (*tom64)(char *dst, char *src, size_t cnt, int byteswap);
629 int (*tof32)(char *dst, size_t dsz, char *src, size_t cnt,
630 int byteswap);
631 int (*tom32)(char *dst, size_t dsz, char *src, size_t cnt,
632 int byteswap);
633 int (*tof64)(char *dst, size_t dsz, char *src, size_t cnt,
634 int byteswap);
635 int (*tom64)(char *dst, size_t dsz, char *src, size_t cnt,
636 int byteswap);
601};
602
603divert(-1)
604define(`CONV',
605 `ifdef(`IGNORE_'$1$2,
606 `.$3$2 = NULL',
607 `ifdef(`BASE_'$1,
608 `.$3$2 = libelf_cvt_$1_$3',

--- 33 unchanged lines hidden (view full) ---

642 [ELF_T_NOTE] = {
643 .tof32 = libelf_cvt_NOTE_tof,
644 .tom32 = libelf_cvt_NOTE_tom,
645 .tof64 = libelf_cvt_NOTE_tof,
646 .tom64 = libelf_cvt_NOTE_tom
647 }
648};
649
637};
638
639divert(-1)
640define(`CONV',
641 `ifdef(`IGNORE_'$1$2,
642 `.$3$2 = NULL',
643 `ifdef(`BASE_'$1,
644 `.$3$2 = libelf_cvt_$1_$3',

--- 33 unchanged lines hidden (view full) ---

678 [ELF_T_NOTE] = {
679 .tof32 = libelf_cvt_NOTE_tof,
680 .tom32 = libelf_cvt_NOTE_tom,
681 .tof64 = libelf_cvt_NOTE_tof,
682 .tom64 = libelf_cvt_NOTE_tom
683 }
684};
685
650void (*_libelf_get_translator(Elf_Type t, int direction, int elfclass))
651 (char *_dst, char *_src, size_t _cnt, int _byteswap)
686int (*_libelf_get_translator(Elf_Type t, int direction, int elfclass))
687 (char *_dst, size_t dsz, char *_src, size_t _cnt, int _byteswap)
652{
653 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
654 assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
655
656 if (t >= ELF_T_NUM ||
657 (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) ||
658 (direction != ELF_TOFILE && direction != ELF_TOMEMORY))
659 return (NULL);
660
661 return ((elfclass == ELFCLASS32) ?
662 (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) :
663 (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64));
664}
688{
689 assert(elfclass == ELFCLASS32 || elfclass == ELFCLASS64);
690 assert(direction == ELF_TOFILE || direction == ELF_TOMEMORY);
691
692 if (t >= ELF_T_NUM ||
693 (elfclass != ELFCLASS32 && elfclass != ELFCLASS64) ||
694 (direction != ELF_TOFILE && direction != ELF_TOMEMORY))
695 return (NULL);
696
697 return ((elfclass == ELFCLASS32) ?
698 (direction == ELF_TOFILE ? cvt[t].tof32 : cvt[t].tom32) :
699 (direction == ELF_TOFILE ? cvt[t].tof64 : cvt[t].tom64));
700}