4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 #include <sys/elf_SPARC.h>
30 #include <sys/elf_amd64.h>
37 * fmsize: Array used to determine what size the the structures
38 * are (for memory image & file image).
40 * x32: Translation routines - to file & to memory.
42 * What must be done when adding a new type for conversion:
44 * The first question is whether you need a new ELF_T_* type
45 * to be created. If you've introduced a new structure - then
46 * it will need to be described - this is done by:
48 * o adding a new type ELF_T_* to usr/src/head/libelf.h
49 * o Create a new macro to define the bytes contained in the structure. Take a
50 * look at the 'Syminfo_1' macro defined below. The declarations describe
51 * the structure based off of the field size of each element of the structure.
52 * o Add a entry to the fmsize table for the new ELF_T_* type.
53 * o Create a <newtype>_11_tof macro. Take a look at 'syminfo_11_tof'.
54 * o Create a <newtype>_11_tom macro. Take a look at 'syminfo_11_tom'.
55 * o The <newtype>_11_tof & <newtype>_11_tom results in conversion routines
56 * <newtype>_2L11_tof, <newtype>_2L11_tom, <newtype>_2M11_tof,
57 * <newtype>_2M11_tom being created in xlate.c. These routines
58 * need to be added to the 'x32[]' array.
59 * o Add entries to getdata.c::align32[] and getdata.c::align64[]. These
60 * tables define what the alignment requirements for a data type are.
62 * In order to tie a section header type (SHT_*) to a data
63 * structure you need to update elf32_mtype() so that it can
64 * make the association. If you are introducing a new section built
65 * on a basic datatype (SHT_INIT_ARRAY) then this is all the updating
66 * that needs to be done.
69 * ELF translation routines
70 * These routines make a subtle implicit assumption.
71 * The file representations of all structures are "packed,"
72 * meaning no implicit padding bytes occur. This might not
73 * be the case for the memory representations. Consequently,
74 * the memory representations ALWAYS contain at least as many
75 * bytes as the file representations. Otherwise, the memory
76 * structures would lose information, meaning they're not
77 * implemented properly.
79 * The words above apply to structures with the same members.
80 * If a future version changes the number of members, the
81 * relative structure sizes for different version must be
82 * tested with the compiler.
85 #define HI32 0x80000000UL
86 #define LO31 0x7fffffffUL
89 * These macros create indexes for accessing the bytes of
90 * words and halfwords for ELFCLASS32 data representations
91 * (currently ELFDATA2LSB and ELFDATA2MSB). In all cases,
93 * w = (((((X_3 << 8) + X_2) << 8) + X_1) << 8) + X_0
94 * h = (X_1 << 8) + X_0
96 * These assume the file representations for Addr, Off,
97 * Sword, and Word use 4 bytes, but the memory def's for
98 * the types may differ.
104 * enuma_*(n) define enum names for addr n
105 * enumb_*(n) define enum names for byte n
106 * enumh_*(n) define enum names for half n
107 * enumo_*(n) define enum names for off n
108 * enumw_*(n) define enum names for word n
109 * enuml_*(n) define enum names for Lword n
110 * tofa(d,s,n) xlate addr n from mem s to file d
111 * tofb(d,s,n) xlate byte n from mem s to file d
112 * tofh(d,s,n) xlate half n from mem s to file d
113 * tofo(d,s,n) xlate off n from mem s to file d
114 * tofw(d,s,n) xlate word n from mem s to file d
115 * tofl(d,s,n) xlate Lword n from mem s to file d
116 * toma(s,n) xlate addr n from file s to expression value
117 * tomb(s,n) xlate byte n from file s to expression value
118 * tomh(s,n) xlate half n from file s to expression value
119 * tomo(s,n) xlate off n from file s to expression value
120 * tomw(s,n) xlate word n from file s to expression value
121 * toml(s,n) xlate Lword n from file s to expression value
123 * tof*() macros must move a multi-byte value into a temporary
124 * because ``in place'' conversions are allowed. If a temp is not
125 * used for multi-byte objects, storing an initial destination byte
126 * may clobber a source byte not yet examined.
128 * tom*() macros compute an expression value from the source
129 * without touching the destination; so they're safe.
132 define(enuma_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
133 define(enuma_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
134 define(enumb_L, `$1_L')dnl
135 define(enumb_M, `$1_M')dnl
136 define(enumh_L, `$1_L0, $1_L1')dnl
137 define(enumh_M, `$1_M1, $1_M0')dnl
138 define(enumo_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
139 define(enumo_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
140 define(enumw_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
141 define(enumw_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
142 define(enuml_L, `$1_L0, $1_L1, $1_L2, $1_L3, $1_L4, $1_L5, $1_L6, $1_L7')dnl
143 define(enuml_M, `$1_M7, $1_M6, $1_M5, $1_M4, $1_M3, $1_M2, $1_M1, $1_M0')dnl
145 define(tofa, `{ register Elf32_Addr _t_ = $2;
146 ($1)[$3`'0] = (unsigned char)_t_,
147 ($1)[$3`'1] = (unsigned char)(_t_>>8),
148 ($1)[$3`'2] = (unsigned char)(_t_>>16),
149 ($1)[$3`'3] = (unsigned char)(_t_>>24); }')dnl
150 define(tofb, `($1)[$3] = (unsigned char)($2)')dnl
151 define(tofh, `{ register Elf32_Half _t_ = $2;
152 ($1)[$3`'0] = (unsigned char)_t_,
153 ($1)[$3`'1] = (unsigned char)(_t_>>8); }')dnl
154 define(tofo, `{ register Elf32_Off _t_ = $2;
155 ($1)[$3`'0] = (unsigned char)_t_,
156 ($1)[$3`'1] = (unsigned char)(_t_>>8),
157 ($1)[$3`'2] = (unsigned char)(_t_>>16),
158 ($1)[$3`'3] = (unsigned char)(_t_>>24); }')dnl
159 define(tofw, `{ register Elf32_Word _t_ = $2;
160 ($1)[$3`'0] = (unsigned char)_t_,
161 ($1)[$3`'1] = (unsigned char)(_t_>>8),
162 ($1)[$3`'2] = (unsigned char)(_t_>>16),
163 ($1)[$3`'3] = (unsigned char)(_t_>>24); }')dnl
164 define(tofl, `{ Elf32_Lword _t_ = $2;
165 ($1)[$3`'0] = (Byte)_t_,
166 ($1)[$3`'1] = (Byte)(_t_>>8),
167 ($1)[$3`'2] = (Byte)(_t_>>16),
168 ($1)[$3`'3] = (Byte)(_t_>>24),
169 ($1)[$3`'4] = (Byte)(_t_>>32),
170 ($1)[$3`'5] = (Byte)(_t_>>40),
171 ($1)[$3`'6] = (Byte)(_t_>>48),
172 ($1)[$3`'7] = (Byte)(_t_>>56); }')dnl
174 define(toma, `(((((((Elf32_Addr)($1)[$2`'3]<<8)
178 define(tomb, `((unsigned char)($1)[$2])')dnl
179 define(tomh, `(((Elf32_Half)($1)[$2`'1]<<8)+($1)[$2`'0])')dnl
180 define(tomo, `(((((((Elf32_Off)($1)[$2`'3]<<8)
184 define(tomw, `(((((((Elf32_Word)($1)[$2`'3]<<8)
188 define(toml, `(((((((((((Elf32_Lword)($1)[$2`'7]<<8)
199 * ELF data object indexes
200 * The enums are broken apart to get around deficiencies
207 enuma_$1(A)`'ifelse(`$2', `', `', `,
218 enumh_$1(H)`'ifelse(`$2', `', `', `,
228 enuml_$1(L)`'ifelse(`$2', `', `', `,
241 enumw_$1(M1_poffset),
243 enumh_$1(M1_stride)`'ifelse(`$2', `', `', `,
256 enumw_$1(MP1_poffset),
257 enumh_$1(MP1_repeat),
258 enumh_$1(MP1_stride),
259 enumw_$1(MP1_padding)`'ifelse(`$2', `', `', `,
270 enumo_$1(O)`'ifelse(`$2', `', `', `,
281 enumw_$1(W)`'ifelse(`$2', `', `', `,
293 enumw_$1(D1_val)`'ifelse(`$2', `', `', `,
306 ifelse(`$2', `', `E1_ident, ')E1_ident_$1_Z = E1_Nident - 1,
308 enumh_$1(E1_machine),
309 enumw_$1(E1_version),
315 enumh_$1(E1_phentsize),
317 enumh_$1(E1_shentsize),
319 enumh_$1(E1_shstrndx)`'ifelse(`$2', `', `', `,
331 enumw_$1(N1_type)`'ifelse(`$2', `', `', `,
348 enumw_$1(P1_align)`'ifelse(`$2', `', `', `,
360 enumw_$1(R1_info)`'ifelse(`$2', `', `', `,
371 enuma_$1(RA1_offset),
373 enumw_$1(RA1_addend)`'ifelse(`$2', `', `', `,
388 enumo_$1(SH1_offset),
392 enumw_$1(SH1_addralign),
393 enumw_$1(SH1_entsize)`'ifelse(`$2', `', `', `,
409 enumh_$1(ST1_shndx)`'ifelse(`$2', `', `', `,
420 enumh_$1(SI1_boundto),
421 enumh_$1(SI1_flags)`'ifelse(`$2', `', `', `,
433 enumw_$1(C1_val)`'ifelse(`$2', `', `', `,
444 enumh_$1(VD1_version),
450 enumw_$1(VD1_next)`'ifelse(`$2', `', `', `,
462 enumw_$1(VDA1_next)`'ifelse(`$2', `', `', `,
473 enumh_$1(VN1_version),
477 enumw_$1(VN1_next)`'ifelse(`$2', `', `', `,
489 enumh_$1(VNA1_flags),
490 enumh_$1(VNA1_other),
492 enumw_$1(VNA1_next)`'ifelse(`$2', `', `', `,
501 * Translation function declarations.
503 * <object>_<data><dver><sver>_tof
504 * <object>_<data><dver><sver>_tom
506 * <data> 2L ELFDATA2LSB
510 static void addr_2L_tof(), addr_2L_tom(),
511 addr_2M_tof(), addr_2M_tom(),
513 dyn_2L11_tof(), dyn_2L11_tom(),
514 dyn_2M11_tof(), dyn_2M11_tom(),
515 ehdr_2L11_tof(), ehdr_2L11_tom(),
516 ehdr_2M11_tof(), ehdr_2M11_tom(),
517 half_2L_tof(), half_2L_tom(),
518 half_2M_tof(), half_2M_tom(),
519 move_2L11_tof(), move_2L11_tom(),
520 move_2M11_tof(), move_2M11_tom(),
521 movep_2L11_tof(), movep_2L11_tom(),
522 movep_2M11_tof(), movep_2M11_tom(),
523 off_2L_tof(), off_2L_tom(),
524 off_2M_tof(), off_2M_tom(),
525 note_2L11_tof(), note_2L11_tom(),
526 note_2M11_tof(), note_2M11_tom(),
527 phdr_2L11_tof(), phdr_2L11_tom(),
528 phdr_2M11_tof(), phdr_2M11_tom(),
529 rel_2L11_tof(), rel_2L11_tom(),
530 rel_2M11_tof(), rel_2M11_tom(),
531 rela_2L11_tof(), rela_2L11_tom(),
532 rela_2M11_tof(), rela_2M11_tom(),
533 shdr_2L11_tof(), shdr_2L11_tom(),
534 shdr_2M11_tof(), shdr_2M11_tom(),
535 sword_2L_tof(), sword_2L_tom(),
536 sword_2M_tof(), sword_2M_tom(),
537 sym_2L11_tof(), sym_2L11_tom(),
538 sym_2M11_tof(), sym_2M11_tom(),
539 syminfo_2L11_tof(), syminfo_2L11_tom(),
540 syminfo_2M11_tof(), syminfo_2M11_tom(),
541 word_2L_tof(), word_2L_tom(),
542 word_2M_tof(), word_2M_tom(),
543 verdef_2L11_tof(), verdef_2L11_tom(),
544 verdef_2M11_tof(), verdef_2M11_tom(),
545 verneed_2L11_tof(), verneed_2L11_tom(),
546 verneed_2M11_tof(), verneed_2M11_tom(),
547 cap_2L11_tof(), cap_2L11_tom(),
548 cap_2M11_tof(), cap_2M11_tom();
551 /* x32 [dst_version - 1] [src_version - 1] [encode - 1] [type]
557 } x32 [EV_CURRENT] [EV_CURRENT] [ELFDATANUM - 1] [ELF_T_NUM] =
561 { /* [1-1][1-1][2LSB-1][.] */
562 /* BYTE */ { byte_to, byte_to },
563 /* ADDR */ { addr_2L_tof, addr_2L_tom },
564 /* DYN */ { dyn_2L11_tof, dyn_2L11_tom },
565 /* EHDR */ { ehdr_2L11_tof, ehdr_2L11_tom },
566 /* HALF */ { half_2L_tof, half_2L_tom },
567 /* OFF */ { off_2L_tof, off_2L_tom },
568 /* PHDR */ { phdr_2L11_tof, phdr_2L11_tom },
569 /* RELA */ { rela_2L11_tof, rela_2L11_tom },
570 /* REL */ { rel_2L11_tof, rel_2L11_tom },
571 /* SHDR */ { shdr_2L11_tof, shdr_2L11_tom },
572 /* SWORD */ { sword_2L_tof, sword_2L_tom },
573 /* SYM */ { sym_2L11_tof, sym_2L11_tom },
574 /* WORD */ { word_2L_tof, word_2L_tom },
575 /* VERDEF */ { verdef_2L11_tof, verdef_2L11_tom},
576 /* VERNEED */ { verneed_2L11_tof, verneed_2L11_tom},
577 /* SXWORD */ { 0, 0 }, /* illegal 32-bit op */
578 /* XWORD */ { 0, 0 }, /* illegal 32-bit op */
579 /* SYMINFO */ { syminfo_2L11_tof, syminfo_2L11_tom },
580 /* NOTE */ { note_2L11_tof, note_2L11_tom },
581 /* MOVE */ { move_2L11_tof, move_2L11_tom },
582 /* MOVEP */ { movep_2L11_tof, movep_2L11_tom },
583 /* CAP */ { cap_2L11_tof, cap_2L11_tom },
585 { /* [1-1][1-1][2MSB-1][.] */
586 /* BYTE */ { byte_to, byte_to },
587 /* ADDR */ { addr_2M_tof, addr_2M_tom },
588 /* DYN */ { dyn_2M11_tof, dyn_2M11_tom },
589 /* EHDR */ { ehdr_2M11_tof, ehdr_2M11_tom },
590 /* HALF */ { half_2M_tof, half_2M_tom },
591 /* OFF */ { off_2M_tof, off_2M_tom },
592 /* PHDR */ { phdr_2M11_tof, phdr_2M11_tom },
593 /* RELA */ { rela_2M11_tof, rela_2M11_tom },
594 /* REL */ { rel_2M11_tof, rel_2M11_tom },
595 /* SHDR */ { shdr_2M11_tof, shdr_2M11_tom },
596 /* SWORD */ { sword_2M_tof, sword_2M_tom },
597 /* SYM */ { sym_2M11_tof, sym_2M11_tom },
598 /* WORD */ { word_2M_tof, word_2M_tom },
599 /* VERDEF */ { verdef_2M11_tof, verdef_2M11_tom},
600 /* VERNEED */ { verneed_2M11_tof, verneed_2M11_tom},
601 /* SXWORD */ { 0, 0 }, /* illegal 32-bit op */
602 /* XWORD */ { 0, 0 }, /* illegal 32-bit op */
603 /* SYMINFO */ { syminfo_2M11_tof, syminfo_2M11_tom },
604 /* NOTE */ { note_2M11_tof, note_2M11_tom },
605 /* MOVE */ { move_2M11_tof, move_2M11_tom },
606 /* MOVEP */ { movep_2M11_tof, movep_2M11_tom },
607 /* CAP */ { cap_2M11_tof, cap_2M11_tom },
615 * size [version - 1] [type]
618 static const struct {
621 } fmsize [EV_CURRENT] [ELF_T_NUM] =
625 /* ADDR */ { A_sizeof, sizeof (Elf32_Addr) },
626 /* DYN */ { D1_sizeof, sizeof (Elf32_Dyn) },
627 /* EHDR */ { E1_sizeof, sizeof (Elf32_Ehdr) },
628 /* HALF */ { H_sizeof, sizeof (Elf32_Half) },
629 /* OFF */ { O_sizeof, sizeof (Elf32_Off) },
630 /* PHDR */ { P1_sizeof, sizeof (Elf32_Phdr) },
631 /* RELA */ { RA1_sizeof, sizeof (Elf32_Rela) },
632 /* REL */ { R1_sizeof, sizeof (Elf32_Rel) },
633 /* SHDR */ { SH1_sizeof, sizeof (Elf32_Shdr) },
634 /* SWORD */ { W_sizeof, sizeof (Elf32_Sword) },
635 /* SYM */ { ST1_sizeof, sizeof (Elf32_Sym) },
636 /* WORD */ { W_sizeof, sizeof (Elf32_Word) },
637 /* VERDEF */ { 1, 1}, /* because bot VERDEF & VERNEED have varying */
638 /* VERNEED */ { 1, 1}, /* sized structures we set their sizes */
640 /* SXWORD */ { 0, 0 }, /* illegal 32-bit op */
641 /* XWORD */ { 0, 0 }, /* illegal 32-bit op */
642 /* SYMINFO */ { SI1_sizeof, sizeof (Elf32_Syminfo) },
643 /* NOTE */ { 1, 1}, /* NOTE has varying sized data we can't */
644 /* use the usual table magic. */
645 /* MOVE */ { M1_sizeof, sizeof (Elf32_Move) },
646 /* MOVEP */ { MP1_sizeof, sizeof (Elf32_Move) },
647 /* CAP */ { C1_sizeof, sizeof (Elf32_Cap) },
653 * memory type [version - 1] [section type]
656 static const Elf_Type mtype[EV_CURRENT][SHT_NUM] =
659 /* NULL */ ELF_T_BYTE,
660 /* PROGBITS */ ELF_T_BYTE,
661 /* SYMTAB */ ELF_T_SYM,
662 /* STRTAB */ ELF_T_BYTE,
663 /* RELA */ ELF_T_RELA,
664 /* HASH */ ELF_T_WORD,
665 /* DYNAMIC */ ELF_T_DYN,
666 /* NOTE */ ELF_T_NOTE,
667 /* NOBITS */ ELF_T_BYTE,
669 /* SHLIB */ ELF_T_BYTE,
670 /* DYNSYM */ ELF_T_SYM,
671 /* UNKNOWN12 */ ELF_T_BYTE,
672 /* UNKNOWN13 */ ELF_T_BYTE,
673 /* INIT_ARRAY */ ELF_T_ADDR,
674 /* FINI_ARRAY */ ELF_T_ADDR,
675 /* PREINIT_ARRAY */ ELF_T_ADDR,
676 /* GROUP */ ELF_T_WORD,
677 /* SYMTAB_SHNDX */ ELF_T_WORD
683 elf32_fsize(Elf_Type type, size_t count, unsigned ver)
685 if (--ver >= EV_CURRENT) {
686 _elf_seterr(EREQ_VER, 0);
689 if ((unsigned)type >= ELF_T_NUM) {
690 _elf_seterr(EREQ_TYPE, 0);
693 return (fmsize[ver][type].s_filesz * count);
698 _elf32_msize(Elf_Type type, unsigned ver)
700 return (fmsize[ver - 1][type].s_memsz);
705 _elf32_mtype(Elf * elf, Elf32_Word shtype, unsigned ver)
707 Elf32_Ehdr * ehdr = (Elf32_Ehdr *)elf->ed_ehdr;
709 if (shtype < SHT_NUM)
710 return (mtype[ver - 1][shtype]);
713 case SHT_SUNW_symsort:
714 case SHT_SUNW_tlssort:
716 case SHT_SUNW_LDYNSYM:
722 case SHT_SUNW_capchain:
724 case SHT_SUNW_capinfo:
726 case SHT_SUNW_SIGNATURE:
728 case SHT_SUNW_ANNOTATE:
730 case SHT_SUNW_DEBUGSTR:
736 * 32bit sparc binaries have a padded
737 * MOVE structure. So - return the
740 if ((ehdr->e_machine == EM_SPARC) ||
741 (ehdr->e_machine == EM_SPARC32PLUS)) {
742 return (ELF_T_MOVEP);
746 case SHT_SUNW_COMDAT:
748 case SHT_SUNW_syminfo:
749 return (ELF_T_SYMINFO);
750 case SHT_SUNW_verdef:
752 case SHT_SUNW_verneed:
753 return (ELF_T_VNEED);
754 case SHT_SUNW_versym:
759 * Check for the sparc specific section types
762 if (((ehdr->e_machine == EM_SPARC) ||
763 (ehdr->e_machine == EM_SPARC32PLUS) ||
764 (ehdr->e_machine == EM_SPARCV9)) &&
765 (shtype == SHT_SPARC_GOTDATA))
769 * Check for the amd64 specific section types
772 if ((ehdr->e_machine == EM_AMD64) &&
773 (shtype == SHT_AMD64_UNWIND))
777 * And the default is ELF_T_BYTE - but we should
778 * certainly have caught any sections we know about
779 * above. This is for unknown sections to libelf.
786 _elf32_entsz(Elf *elf, Elf32_Word shtype, unsigned ver)
790 ttype = _elf32_mtype(elf, shtype, ver);
791 return ((ttype == ELF_T_BYTE) ? 0 : fmsize[ver - 1][ttype].s_filesz);
796 * Determine the data encoding used by the current system.
799 _elf_sys_encoding(void)
803 unsigned char c[W_sizeof];
807 /*CONSTANTCONDITION*/
808 if (~(Elf32_Word)0 == -(Elf32_Sword)1 && tomw(u.c, W_L) == 0x10203)
809 return (ELFDATA2LSB);
811 /*CONSTANTCONDITION*/
812 if (~(Elf32_Word)0 == -(Elf32_Sword)1 && tomw(u.c, W_M) == 0x10203)
813 return (ELFDATA2MSB);
815 /* Not expected to occur */
816 return (ELFDATANONE);
821 * XX64 This routine is also used to 'version' interactions with Elf64
822 * applications, but there's no way to figure out if the caller is
823 * asking Elf32 or Elf64 questions, even though it has Elf32
827 elf_version(unsigned ver)
833 if (ver > EV_CURRENT)
835 _elf_seterr(EREQ_VER, 0);
838 (void) mutex_lock(&_elf_globals_mutex);
839 if (_elf_work != EV_NONE)
843 (void) mutex_unlock(&_elf_globals_mutex);
848 _elf_encode = _elf_sys_encoding();
850 (void) mutex_unlock(&_elf_globals_mutex);
857 xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof)
858 /* tof !0 -> xlatetof */
860 size_t cnt, dsz, ssz;
866 if (dst == 0 || src == 0)
868 if (--encode >= (ELFDATANUM - 1)) {
869 _elf_seterr(EREQ_ENCODE, 0);
872 if ((dver = dst->d_version - 1) >= EV_CURRENT ||
873 (sver = src->d_version - 1) >= EV_CURRENT) {
874 _elf_seterr(EREQ_VER, 0);
877 if ((type = src->d_type) >= ELF_T_NUM) {
878 _elf_seterr(EREQ_TYPE, 0);
883 dsz = fmsize[dver][type].s_filesz;
884 ssz = fmsize[sver][type].s_memsz;
885 f = x32[dver][sver][encode][type].x_tof;
887 dsz = fmsize[dver][type].s_memsz;
888 ssz = fmsize[sver][type].s_filesz;
889 f = x32[dver][sver][encode][type].x_tom;
891 cnt = src->d_size / ssz;
892 if (dst->d_size < dsz * cnt) {
893 _elf_seterr(EREQ_DSZ, 0);
897 ELFACCESSDATA(_encode, _elf_encode)
898 if ((_encode == (encode + 1)) && (dsz == ssz)) {
900 * ld(1) frequently produces empty sections (eg. .dynsym,
901 * .dynstr, .symtab, .strtab, etc) so that the initial
902 * output image can be created of the correct size. Later
903 * these sections are filled in with the associated data.
904 * So that we don't have to pre-allocate buffers for
905 * these segments, allow for the src destination to be 0.
907 if (src->d_buf && src->d_buf != dst->d_buf)
908 (void) memcpy(dst->d_buf, src->d_buf, src->d_size);
909 dst->d_type = src->d_type;
910 dst->d_size = src->d_size;
914 (*f)(dst->d_buf, src->d_buf, cnt);
915 dst->d_size = dsz * cnt;
916 dst->d_type = src->d_type;
922 elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode)
924 return (xlate(dst, src, encode, 1));
929 elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode)
931 return (xlate(dst, src, encode, 0));
936 * xlate to file format
938 * ..._tof(name, data) -- macros
940 * Recall that the file format must be no larger than the
941 * memory format (equal versions). Use "forward" copy.
942 * All these routines require non-null, non-zero arguments.
947 $1(unsigned char *dst, Elf32_Addr *src, size_t cnt)
949 Elf32_Addr *end = src + cnt;
952 tofa(dst, *src, A_$2);
954 } while (++src < end);
957 addr_tof(addr_2L_tof,L)
958 addr_tof(addr_2M_tof,M)
962 byte_to(unsigned char *dst, unsigned char *src, size_t cnt)
965 (void) memcpy(dst, src, cnt);
971 $1(unsigned char *dst, Elf32_Dyn *src, size_t cnt)
973 Elf32_Dyn *end = src + cnt;
976 tofw(dst, src->d_tag, D1_tag_$2);
977 tofo(dst, src->d_un.d_val, D1_val_$2);
979 } while (++src < end);
982 dyn_11_tof(dyn_2L11_tof,L)
983 dyn_11_tof(dyn_2M11_tof,M)
986 define(ehdr_11_tof, `
988 $1(unsigned char *dst, Elf32_Ehdr *src, size_t cnt)
990 Elf32_Ehdr *end = src + cnt;
993 if (&dst[E1_ident] != src->e_ident)
994 (void) memcpy(&dst[E1_ident], src->e_ident, E1_Nident);
995 tofh(dst, src->e_type, E1_type_$2);
996 tofh(dst, src->e_machine, E1_machine_$2);
997 tofw(dst, src->e_version, E1_version_$2);
998 tofa(dst, src->e_entry, E1_entry_$2);
999 tofo(dst, src->e_phoff, E1_phoff_$2);
1000 tofo(dst, src->e_shoff, E1_shoff_$2);
1001 tofw(dst, src->e_flags, E1_flags_$2);
1002 tofh(dst, src->e_ehsize, E1_ehsize_$2);
1003 tofh(dst, src->e_phentsize, E1_phentsize_$2);
1004 tofh(dst, src->e_phnum, E1_phnum_$2);
1005 tofh(dst, src->e_shentsize, E1_shentsize_$2);
1006 tofh(dst, src->e_shnum, E1_shnum_$2);
1007 tofh(dst, src->e_shstrndx, E1_shstrndx_$2);
1009 } while (++src < end);
1012 ehdr_11_tof(ehdr_2L11_tof,L)
1013 ehdr_11_tof(ehdr_2M11_tof,M)
1018 $1(unsigned char *dst, Elf32_Half *src, size_t cnt)
1020 Elf32_Half *end = src + cnt;
1023 tofh(dst, *src, H_$2);
1025 } while (++src < end);
1028 half_tof(half_2L_tof,L)
1029 half_tof(half_2M_tof,M)
1032 define(move_11_tof, `
1034 $1(unsigned char *dst, Elf32_Move *src, size_t cnt)
1036 Elf32_Move *end = src + cnt;
1039 tofl(dst, src->m_value, M1_value_$2);
1040 tofw(dst, src->m_info, M1_info_$2);
1041 tofw(dst, src->m_poffset, M1_poffset_$2);
1042 tofh(dst, src->m_repeat, M1_repeat_$2);
1043 tofh(dst, src->m_stride, M1_stride_$2);
1045 } while (++src < end);
1048 move_11_tof(move_2L11_tof,L)
1049 move_11_tof(move_2M11_tof,M)
1052 define(movep_11_tof, `
1054 $1(unsigned char *dst, Elf32_Move *src, size_t cnt)
1056 Elf32_Move *end = src + cnt;
1059 tofl(dst, src->m_value, MP1_value_$2);
1060 tofw(dst, src->m_info, MP1_info_$2);
1061 tofw(dst, src->m_poffset, MP1_poffset_$2);
1062 tofh(dst, src->m_repeat, MP1_repeat_$2);
1063 tofh(dst, src->m_stride, MP1_stride_$2);
1065 } while (++src < end);
1068 movep_11_tof(movep_2L11_tof,L)
1069 movep_11_tof(movep_2M11_tof,M)
1074 $1(unsigned char *dst, Elf32_Off *src, size_t cnt)
1076 Elf32_Off *end = src + cnt;
1079 tofo(dst, *src, O_$2);
1081 } while (++src < end);
1084 off_tof(off_2L_tof,L)
1085 off_tof(off_2M_tof,M)
1088 define(note_11_tof, `
1090 $1(unsigned char *dst, Elf32_Nhdr *src, size_t cnt)
1093 Elf32_Nhdr *end = (Elf32_Nhdr *)((char *)src + cnt);
1096 * Copy the note data to the source, translating the
1097 * length fields. Clip against the size of the actual buffer
1098 * to guard against corrupt note data.
1101 Elf32_Word descsz, namesz;
1104 * cache size of desc & name fields - while rounding
1107 namesz = S_ROUND(src->n_namesz, sizeof (Elf32_Word));
1108 descsz = src->n_descsz;
1111 * Copy contents of Elf32_Nhdr
1113 if ((offsetof(Elf32_Nhdr, n_namesz) + sizeof(Elf32_Word) +
1114 (char *) src) >= (char *) end)
1116 tofw(dst, src->n_namesz, N1_namesz_$2);
1118 if ((offsetof(Elf32_Nhdr, n_descsz) + sizeof(Elf32_Word) +
1119 (char *) src) >= (char *) end)
1121 tofw(dst, src->n_descsz, N1_descsz_$2);
1123 if ((offsetof(Elf32_Nhdr, n_type) + sizeof(Elf32_Word) +
1124 (char *) src) >= (char *) end)
1126 tofw(dst, src->n_type, N1_type_$2);
1129 * Copy contents of Name field
1133 if ((namesz + (char *) src) > (char *) end) {
1134 namesz = (char *) end - (char *) src;
1138 (void)memcpy(dst, src, namesz);
1141 * Copy contents of desc field
1144 src = (Elf32_Nhdr *)((uintptr_t)src + namesz);
1145 if ((descsz + (char *) src) > (char *) end) {
1146 descsz = (char *) end - (char *) src;
1150 (void)memcpy(dst, src, descsz);
1152 descsz = S_ROUND(descsz, sizeof (Elf32_Word));
1154 src = (Elf32_Nhdr *)((uintptr_t)src + descsz);
1155 } while (src < end);
1158 note_11_tof(note_2L11_tof,L)
1159 note_11_tof(note_2M11_tof,M)
1162 define(phdr_11_tof, `
1164 $1(unsigned char *dst, Elf32_Phdr *src, size_t cnt)
1166 Elf32_Phdr *end = src + cnt;
1169 tofw(dst, src->p_type, P1_type_$2);
1170 tofo(dst, src->p_offset, P1_offset_$2);
1171 tofa(dst, src->p_vaddr, P1_vaddr_$2);
1172 tofa(dst, src->p_paddr, P1_paddr_$2);
1173 tofw(dst, src->p_filesz, P1_filesz_$2);
1174 tofw(dst, src->p_memsz, P1_memsz_$2);
1175 tofw(dst, src->p_flags, P1_flags_$2);
1176 tofw(dst, src->p_align, P1_align_$2);
1178 } while (++src < end);
1181 phdr_11_tof(phdr_2L11_tof,L)
1182 phdr_11_tof(phdr_2M11_tof,M)
1185 define(rel_11_tof, `
1187 $1(unsigned char *dst, Elf32_Rel *src, size_t cnt)
1189 Elf32_Rel *end = src + cnt;
1192 tofa(dst, src->r_offset, R1_offset_$2);
1193 tofw(dst, src->r_info, R1_info_$2);
1195 } while (++src < end);
1198 rel_11_tof(rel_2L11_tof,L)
1199 rel_11_tof(rel_2M11_tof,M)
1202 define(rela_11_tof, `
1204 $1(unsigned char *dst, Elf32_Rela *src, size_t cnt)
1206 Elf32_Rela *end = src + cnt;
1209 tofa(dst, src->r_offset, RA1_offset_$2);
1210 tofw(dst, src->r_info, RA1_info_$2);
1211 /*CONSTANTCONDITION*/
1212 if (~(Elf32_Word)0 == -(Elf32_Sword)1) { /* 2s comp */
1213 tofw(dst, src->r_addend, RA1_addend_$2);
1217 if (src->r_addend < 0) {
1218 w = - src->r_addend;
1222 tofw(dst, w, RA1_addend_$2);
1225 } while (++src < end);
1228 rela_11_tof(rela_2L11_tof,L)
1229 rela_11_tof(rela_2M11_tof,M)
1232 define(shdr_11_tof, `
1234 $1(unsigned char *dst, Elf32_Shdr *src, size_t cnt)
1236 Elf32_Shdr *end = src + cnt;
1239 tofw(dst, src->sh_name, SH1_name_$2);
1240 tofw(dst, src->sh_type, SH1_type_$2);
1241 tofw(dst, src->sh_flags, SH1_flags_$2);
1242 tofa(dst, src->sh_addr, SH1_addr_$2);
1243 tofo(dst, src->sh_offset, SH1_offset_$2);
1244 tofw(dst, src->sh_size, SH1_size_$2);
1245 tofw(dst, src->sh_link, SH1_link_$2);
1246 tofw(dst, src->sh_info, SH1_info_$2);
1247 tofw(dst, src->sh_addralign, SH1_addralign_$2);
1248 tofw(dst, src->sh_entsize, SH1_entsize_$2);
1250 } while (++src < end);
1253 shdr_11_tof(shdr_2L11_tof,L)
1254 shdr_11_tof(shdr_2M11_tof,M)
1259 $1(unsigned char *dst, Elf32_Sword *src, size_t cnt)
1261 Elf32_Sword *end = src + cnt;
1264 /*CONSTANTCONDITION*/
1265 if (~(Elf32_Word)0 == -(Elf32_Sword)1) { /* 2s comp */
1266 tofw(dst, *src, W_$2);
1278 } while (++src < end);
1281 sword_tof(sword_2L_tof,L)
1282 sword_tof(sword_2M_tof,M)
1285 define(cap_11_tof, `
1287 $1(unsigned char *dst, Elf32_Cap *src, size_t cnt)
1289 Elf32_Cap *end = src + cnt;
1292 tofw(dst, src->c_tag, C1_tag_$2);
1293 tofw(dst, src->c_un.c_val, C1_val_$2);
1295 } while (++src < end);
1298 cap_11_tof(cap_2L11_tof,L)
1299 cap_11_tof(cap_2M11_tof,M)
1302 define(syminfo_11_tof, `
1304 $1(unsigned char *dst, Elf32_Syminfo *src, size_t cnt)
1306 Elf32_Syminfo *end = src + cnt;
1309 tofh(dst, src->si_boundto, SI1_boundto_$2);
1310 tofh(dst, src->si_flags, SI1_flags_$2);
1312 } while (++src < end);
1315 syminfo_11_tof(syminfo_2L11_tof,L)
1316 syminfo_11_tof(syminfo_2M11_tof,M)
1319 define(sym_11_tof, `
1321 $1(unsigned char *dst, Elf32_Sym *src, size_t cnt)
1323 Elf32_Sym *end = src + cnt;
1326 tofw(dst, src->st_name, ST1_name_$2);
1327 tofa(dst, src->st_value, ST1_value_$2);
1328 tofw(dst, src->st_size, ST1_size_$2);
1329 tofb(dst, src->st_info, ST1_info_$2);
1330 tofb(dst, src->st_other, ST1_other_$2);
1331 tofh(dst, src->st_shndx, ST1_shndx_$2);
1333 } while (++src < end);
1336 sym_11_tof(sym_2L11_tof,L)
1337 sym_11_tof(sym_2M11_tof,M)
1342 $1(unsigned char *dst, Elf32_Word *src, size_t cnt)
1344 Elf32_Word *end = src + cnt;
1347 tofw(dst, *src, W_$2);
1349 } while (++src < end);
1352 word_tof(word_2L_tof,L)
1353 word_tof(word_2M_tof,M)
1356 define(verdef_11_tof, `
1358 $1(unsigned char *dst, Elf32_Verdef *src, size_t cnt)
1361 Elf32_Verdef *end = (Elf32_Verdef *)((char *)src + cnt);
1364 Elf32_Verdef *next_verdef;
1365 Elf32_Verdaux *vaux;
1367 unsigned char *vaux_dst;
1368 unsigned char *dst_next;
1371 next_verdef = (Elf32_Verdef *)(src->vd_next ?
1372 (char *)src + src->vd_next : (char *)end);
1373 dst_next = dst + src->vd_next;
1376 vaux = (Elf32_Verdaux *)((char *)src + src->vd_aux);
1377 vaux_dst = dst + src->vd_aux;
1380 * Convert auxilary structures
1382 for (i = 0; i < src->vd_cnt; i++) {
1383 Elf32_Verdaux *vaux_next;
1384 unsigned char *vaux_dst_next;
1387 * because our source and destination can be
1388 * the same place we need to figure out the next
1392 vaux_next = (Elf32_Verdaux *)((char *)vaux +
1394 vaux_dst_next = vaux_dst + vaux->vda_next;
1396 tofa(vaux_dst, vaux->vda_name, VDA1_name_$2);
1397 tofw(vaux_dst, vaux->vda_next, VDA1_next_$2);
1398 vaux_dst = vaux_dst_next;
1403 * Convert Elf32_Verdef structure.
1405 tofh(dst, src->vd_version, VD1_version_$2);
1406 tofh(dst, src->vd_flags, VD1_flags_$2);
1407 tofh(dst, src->vd_ndx, VD1_ndx_$2);
1408 tofh(dst, src->vd_cnt, VD1_cnt_$2);
1409 tofw(dst, src->vd_hash, VD1_hash_$2);
1410 tofw(dst, src->vd_aux, VD1_aux_$2);
1411 tofw(dst, src->vd_next, VD1_next_$2);
1414 } while (src < end);
1417 verdef_11_tof(verdef_2L11_tof, L)
1418 verdef_11_tof(verdef_2M11_tof, M)
1420 define(verneed_11_tof, `
1422 $1(unsigned char *dst, Elf32_Verneed *src, size_t cnt)
1425 Elf32_Verneed *end = (Elf32_Verneed *)((char *)src + cnt);
1428 Elf32_Verneed *next_verneed;
1429 Elf32_Vernaux *vaux;
1431 unsigned char *vaux_dst;
1432 unsigned char *dst_next;
1435 next_verneed = (Elf32_Verneed *)(src->vn_next ?
1436 (char *)src + src->vn_next : (char *)end);
1437 dst_next = dst + src->vn_next;
1440 vaux = (Elf32_Vernaux *)((char *)src + src->vn_aux);
1441 vaux_dst = dst + src->vn_aux;
1444 * Convert auxilary structures first
1446 for (i = 0; i < src->vn_cnt; i++) {
1447 Elf32_Vernaux * vaux_next;
1448 unsigned char * vaux_dst_next;
1451 * because our source and destination can be
1452 * the same place we need to figure out the
1453 * next location now.
1456 vaux_next = (Elf32_Vernaux *)((char *)vaux +
1458 vaux_dst_next = vaux_dst + vaux->vna_next;
1460 tofw(vaux_dst, vaux->vna_hash, VNA1_hash_$2);
1461 tofh(vaux_dst, vaux->vna_flags, VNA1_flags_$2);
1462 tofh(vaux_dst, vaux->vna_other, VNA1_other_$2);
1463 tofa(vaux_dst, vaux->vna_name, VNA1_name_$2);
1464 tofw(vaux_dst, vaux->vna_next, VNA1_next_$2);
1465 vaux_dst = vaux_dst_next;
1469 * Convert Elf32_Verneed structure.
1471 tofh(dst, src->vn_version, VN1_version_$2);
1472 tofh(dst, src->vn_cnt, VN1_cnt_$2);
1473 tofa(dst, src->vn_file, VN1_file_$2);
1474 tofw(dst, src->vn_aux, VN1_aux_$2);
1475 tofw(dst, src->vn_next, VN1_next_$2);
1478 } while (src < end);
1481 verneed_11_tof(verneed_2L11_tof, L)
1482 verneed_11_tof(verneed_2M11_tof, M)
1485 /* xlate to memory format
1487 * ..._tom(name, data) -- macros
1489 * Recall that the memory format may be larger than the
1490 * file format (equal versions). Use "backward" copy.
1491 * All these routines require non-null, non-zero arguments.
1497 $1(Elf32_Addr *dst, unsigned char *src, size_t cnt)
1499 Elf32_Addr *end = dst;
1502 src += cnt * A_sizeof;
1503 while (dst-- > end) {
1505 *dst = toma(src, A_$2);
1509 addr_tom(addr_2L_tom,L)
1510 addr_tom(addr_2M_tom,M)
1513 define(dyn_11_tom, `
1515 $1(Elf32_Dyn *dst, unsigned char *src, size_t cnt)
1517 Elf32_Dyn *end = dst + cnt;
1520 dst->d_tag = tomw(src, D1_tag_$2);
1521 dst->d_un.d_val = tomw(src, D1_val_$2);
1523 } while (++dst < end);
1526 dyn_11_tom(dyn_2L11_tom,L)
1527 dyn_11_tom(dyn_2M11_tom,M)
1530 define(ehdr_11_tom, `
1532 $1(Elf32_Ehdr *dst, unsigned char *src, size_t cnt)
1534 Elf32_Ehdr *end = dst;
1537 src += cnt * E1_sizeof;
1538 while (dst-- > end) {
1540 dst->e_shstrndx = tomh(src, E1_shstrndx_$2);
1541 dst->e_shnum = tomh(src, E1_shnum_$2);
1542 dst->e_shentsize = tomh(src, E1_shentsize_$2);
1543 dst->e_phnum = tomh(src, E1_phnum_$2);
1544 dst->e_phentsize = tomh(src, E1_phentsize_$2);
1545 dst->e_ehsize = tomh(src, E1_ehsize_$2);
1546 dst->e_flags = tomw(src, E1_flags_$2);
1547 dst->e_shoff = tomo(src, E1_shoff_$2);
1548 dst->e_phoff = tomo(src, E1_phoff_$2);
1549 dst->e_entry = toma(src, E1_entry_$2);
1550 dst->e_version = tomw(src, E1_version_$2);
1551 dst->e_machine = tomh(src, E1_machine_$2);
1552 dst->e_type = tomh(src, E1_type_$2);
1553 if (dst->e_ident != &src[E1_ident])
1554 (void) memcpy(dst->e_ident, &src[E1_ident], E1_Nident);
1558 ehdr_11_tom(ehdr_2L11_tom,L)
1559 ehdr_11_tom(ehdr_2M11_tom,M)
1564 $1(Elf32_Half *dst, unsigned char *src, size_t cnt)
1566 Elf32_Half *end = dst;
1569 src += cnt * H_sizeof;
1570 while (dst-- > end) {
1572 *dst = tomh(src, H_$2);
1576 half_tom(half_2L_tom,L)
1577 half_tom(half_2M_tom,M)
1580 define(move_11_tom, `
1582 $1(Elf32_Move *dst, unsigned char *src, size_t cnt)
1584 Elf32_Move *end = dst + cnt;
1587 dst->m_value = toml(src, M1_value_$2);
1588 dst->m_info = tomw(src, M1_info_$2);
1589 dst->m_poffset = tomw(src, M1_poffset_$2);
1590 dst->m_repeat = tomh(src, M1_repeat_$2);
1591 dst->m_stride = tomh(src, M1_stride_$2);
1593 } while (++dst < end);
1596 move_11_tom(move_2L11_tom,L)
1597 move_11_tom(move_2M11_tom,M)
1600 define(movep_11_tom, `
1602 $1(Elf32_Move *dst, unsigned char *src, size_t cnt)
1604 Elf32_Move *end = dst + cnt;
1608 dst->m_value = toml(src, MP1_value_$2);
1609 dst->m_info = tomw(src, MP1_info_$2);
1610 dst->m_poffset = tomw(src, MP1_poffset_$2);
1611 dst->m_repeat = tomh(src, MP1_repeat_$2);
1612 dst->m_stride = tomh(src, MP1_stride_$2);
1614 } while (++dst < end);
1617 movep_11_tom(movep_2L11_tom,L)
1618 movep_11_tom(movep_2M11_tom,M)
1621 define(note_11_tom, `
1623 $1(Elf32_Nhdr *dst, unsigned char *src, size_t cnt)
1626 Elf32_Nhdr *end = (Elf32_Nhdr *)((char *)dst + cnt);
1629 * Copy the note data to the destination, translating the
1630 * length fields. Clip against the size of the actual buffer
1631 * to guard against corrupt note data.
1635 unsigned char *namestr;
1637 Elf32_Word field_sz;
1639 if ((offsetof(Elf32_Nhdr, n_namesz) + sizeof(Elf32_Word) +
1640 (char *) dst) >= (char *) end)
1642 dst->n_namesz = tomw(src, N1_namesz_$2);
1644 if ((offsetof(Elf32_Nhdr, n_descsz) + sizeof(Elf32_Word) +
1645 (char *) dst) >= (char *) end)
1647 dst->n_descsz = tomw(src, N1_descsz_$2);
1649 if ((offsetof(Elf32_Nhdr, n_type) + sizeof(Elf32_Word) +
1650 (char *) dst) >= (char *) end)
1652 dst->n_type = tomw(src, N1_type_$2);
1656 dst = (Elf32_Nhdr *)((char *)dst + sizeof (Elf32_Nhdr));
1657 namestr = src + N1_sizeof;
1658 field_sz = S_ROUND(nhdr->n_namesz, sizeof (Elf32_Word));
1659 if ((field_sz + (char *) dst) > (char *) end) {
1660 field_sz = (char *) end - (char *) dst;
1664 (void)memcpy((void *)dst, namestr, field_sz);
1665 desc = namestr + field_sz;
1668 dst = (Elf32_Nhdr *)((char *)dst + field_sz);
1669 field_sz = nhdr->n_descsz;
1670 if ((field_sz + (char *) dst) > (char *) end) {
1671 field_sz = (char *) end - (char *) dst;
1675 (void)memcpy(dst, desc, field_sz);
1676 field_sz = S_ROUND(field_sz, sizeof (Elf32_Word));
1679 dst = (Elf32_Nhdr *)((char *)dst + field_sz);
1680 src = (unsigned char *)desc + field_sz;
1684 note_11_tom(note_2L11_tom,L)
1685 note_11_tom(note_2M11_tom,M)
1690 $1(Elf32_Off *dst, unsigned char *src, size_t cnt)
1692 Elf32_Off *end = dst;
1695 src += cnt * O_sizeof;
1696 while (dst-- > end) {
1698 *dst = tomo(src, O_$2);
1702 off_tom(off_2L_tom,L)
1703 off_tom(off_2M_tom,M)
1706 define(phdr_11_tom, `
1708 $1(Elf32_Phdr *dst, unsigned char *src, size_t cnt)
1710 Elf32_Phdr *end = dst;
1713 src += cnt * P1_sizeof;
1714 while (dst-- > end) {
1716 dst->p_align = tomw(src, P1_align_$2);
1717 dst->p_flags = tomw(src, P1_flags_$2);
1718 dst->p_memsz = tomw(src, P1_memsz_$2);
1719 dst->p_filesz = tomw(src, P1_filesz_$2);
1720 dst->p_paddr = toma(src, P1_paddr_$2);
1721 dst->p_vaddr = toma(src, P1_vaddr_$2);
1722 dst->p_offset = tomo(src, P1_offset_$2);
1723 dst->p_type = tomw(src, P1_type_$2);
1727 phdr_11_tom(phdr_2L11_tom,L)
1728 phdr_11_tom(phdr_2M11_tom,M)
1731 define(rel_11_tom, `
1733 $1(Elf32_Rel *dst, unsigned char *src, size_t cnt)
1735 Elf32_Rel *end = dst;
1738 src += cnt * R1_sizeof;
1739 while (dst-- > end) {
1741 dst->r_info = tomw(src, R1_info_$2);
1742 dst->r_offset = toma(src, R1_offset_$2);
1746 rel_11_tom(rel_2L11_tom,L)
1747 rel_11_tom(rel_2M11_tom,M)
1750 define(rela_11_tom, `
1752 $1(Elf32_Rela *dst, unsigned char *src, size_t cnt)
1754 Elf32_Rela *end = dst;
1757 src += cnt * RA1_sizeof;
1758 while (dst-- > end) {
1760 /*CONSTANTCONDITION*/
1761 if (~(Elf32_Word)0 == -(Elf32_Sword)1 && /* 2s comp */
1762 ~(~(Elf32_Word)0 >> 1) == HI32) {
1763 dst->r_addend = tomw(src, RA1_addend_$2);
1770 if ((u.w = tomw(src, RA1_addend_$2)) & HI32) {
1771 u.w |= ~(Elf32_Word)LO31;
1775 dst->r_addend = u.sw;
1777 dst->r_info = tomw(src, RA1_info_$2);
1778 dst->r_offset = toma(src, RA1_offset_$2);
1782 rela_11_tom(rela_2L11_tom,L)
1783 rela_11_tom(rela_2M11_tom,M)
1786 define(shdr_11_tom, `
1788 $1(Elf32_Shdr *dst, unsigned char *src, size_t cnt)
1790 Elf32_Shdr *end = dst;
1793 src += cnt * SH1_sizeof;
1794 while (dst-- > end) {
1796 dst->sh_entsize = tomw(src, SH1_entsize_$2);
1797 dst->sh_addralign = tomw(src, SH1_addralign_$2);
1798 dst->sh_info = tomw(src, SH1_info_$2);
1799 dst->sh_link = tomw(src, SH1_link_$2);
1800 dst->sh_size = tomw(src, SH1_size_$2);
1801 dst->sh_offset = tomo(src, SH1_offset_$2);
1802 dst->sh_addr = toma(src, SH1_addr_$2);
1803 dst->sh_flags = tomw(src, SH1_flags_$2);
1804 dst->sh_type = tomw(src, SH1_type_$2);
1805 dst->sh_name = tomw(src, SH1_name_$2);
1809 shdr_11_tom(shdr_2L11_tom,L)
1810 shdr_11_tom(shdr_2M11_tom,M)
1816 $1(Elf32_Sword *dst, unsigned char *src, size_t cnt)
1818 Elf32_Sword *end = dst;
1821 src += cnt * W_sizeof;
1822 while (dst-- > end) {
1824 /*CONSTANTCONDITION*/
1825 if (~(Elf32_Word)0 == -(Elf32_Sword)1 && /* 2s comp */
1826 ~(~(Elf32_Word)0 >> 1) == HI32) {
1827 *dst = tomw(src, W_$2);
1834 if ((u.w = tomw(src, W_$2)) & HI32) {
1835 u.w |= ~(Elf32_Word)LO31;
1844 sword_tom(sword_2L_tom,L)
1845 sword_tom(sword_2M_tom,M)
1848 define(cap_11_tom, `
1850 $1(Elf32_Cap *dst, unsigned char *src, size_t cnt)
1852 Elf32_Cap *end = dst + cnt;
1855 dst->c_tag = tomw(src, C1_tag_$2);
1856 dst->c_un.c_val = tomw(src, C1_val_$2);
1858 } while (++dst < end);
1861 cap_11_tom(cap_2L11_tom,L)
1862 cap_11_tom(cap_2M11_tom,M)
1865 define(syminfo_11_tom, `
1867 $1(Elf32_Syminfo *dst, unsigned char *src, size_t cnt)
1869 Elf32_Syminfo *end = dst;
1872 src += cnt * SI1_sizeof;
1873 while (dst-- > end) {
1875 dst->si_boundto = tomh(src, SI1_boundto_$2);
1876 dst->si_flags = tomh(src, SI1_flags_$2);
1880 syminfo_11_tom(syminfo_2L11_tom,L)
1881 syminfo_11_tom(syminfo_2M11_tom,M)
1884 define(sym_11_tom, `
1886 $1(Elf32_Sym *dst, unsigned char *src, size_t cnt)
1888 Elf32_Sym *end = dst;
1891 src += cnt * ST1_sizeof;
1892 while (dst-- > end) {
1894 dst->st_shndx = tomh(src, ST1_shndx_$2);
1895 dst->st_other = tomb(src, ST1_other_$2);
1896 dst->st_info = tomb(src, ST1_info_$2);
1897 dst->st_size = tomw(src, ST1_size_$2);
1898 dst->st_value = toma(src, ST1_value_$2);
1899 dst->st_name = tomw(src, ST1_name_$2);
1903 sym_11_tom(sym_2L11_tom,L)
1904 sym_11_tom(sym_2M11_tom,M)
1909 $1(Elf32_Word *dst, unsigned char *src, size_t cnt)
1911 Elf32_Word *end = dst;
1914 src += cnt * W_sizeof;
1915 while (dst-- > end) {
1917 *dst = tomw(src, W_$2);
1921 word_tom(word_2L_tom,L)
1922 word_tom(word_2M_tom,M)
1925 define(verdef_11_tom, `
1927 $1(Elf32_Verdef *dst, unsigned char *src, size_t cnt)
1930 Elf32_Verdef *end = (Elf32_Verdef *)((char *)dst + cnt);
1933 Elf32_Verdaux *vaux;
1934 unsigned char *src_vaux;
1937 dst->vd_version = tomh(src, VD1_version_$2);
1938 dst->vd_flags = tomh(src, VD1_flags_$2);
1939 dst->vd_ndx = tomh(src, VD1_ndx_$2);
1940 dst->vd_cnt = tomh(src, VD1_cnt_$2);
1941 dst->vd_hash = tomw(src, VD1_hash_$2);
1942 dst->vd_aux = tomw(src, VD1_aux_$2);
1943 dst->vd_next = tomw(src, VD1_next_$2);
1945 src_vaux = src + dst->vd_aux;
1947 vaux = (Elf32_Verdaux*)((char *)dst + dst->vd_aux);
1948 for (i = 0; i < dst->vd_cnt; i++) {
1949 vaux->vda_name = toma(src_vaux, VDA1_name_$2);
1950 vaux->vda_next = toma(src_vaux, VDA1_next_$2);
1951 src_vaux += vaux->vda_next;
1953 vaux = (Elf32_Verdaux *)((char *)vaux +
1956 src += dst->vd_next;
1958 dst = (Elf32_Verdef *)(dst->vd_next ?
1959 (char *)dst + dst->vd_next : (char *)end);
1963 verdef_11_tom(verdef_2L11_tom,L)
1964 verdef_11_tom(verdef_2M11_tom,M)
1967 define(verneed_11_tom, `
1969 $1(Elf32_Verneed *dst, unsigned char *src, size_t cnt)
1972 Elf32_Verneed *end = (Elf32_Verneed *)((char *)dst + cnt);
1975 Elf32_Vernaux * vaux;
1976 unsigned char * src_vaux;
1978 dst->vn_version = tomh(src, VN1_version_$2);
1979 dst->vn_cnt = tomh(src, VN1_cnt_$2);
1980 dst->vn_file = toma(src, VN1_file_$2);
1981 dst->vn_aux = tomw(src, VN1_aux_$2);
1982 dst->vn_next = tomw(src, VN1_next_$2);
1984 src_vaux = src + dst->vn_aux;
1986 vaux = (Elf32_Vernaux *)((char *)dst + dst->vn_aux);
1987 for (i = 0; i < dst->vn_cnt; i++) {
1988 vaux->vna_hash = tomw(src_vaux, VNA1_hash_$2);
1989 vaux->vna_flags = tomh(src_vaux, VNA1_flags_$2);
1990 vaux->vna_other = tomh(src_vaux, VNA1_other_$2);
1991 vaux->vna_name = toma(src_vaux, VNA1_name_$2);
1992 vaux->vna_next = tomw(src_vaux, VNA1_next_$2);
1993 src_vaux += vaux->vna_next;
1995 vaux = (Elf32_Vernaux *)((char *)vaux +
1998 src += dst->vn_next;
2000 dst = (Elf32_Verneed *)(dst->vn_next ?
2001 (char *)dst + dst->vn_next : (char *)end);
2005 verneed_11_tom(verneed_2L11_tom,L)
2006 verneed_11_tom(verneed_2M11_tom,M)