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 * x64: 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 'x64[]' 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 elf64_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
71 * These routines make a subtle implicit assumption.
72 * The file representations of all structures are "packed,"
73 * meaning no implicit padding bytes occur. This might not
74 * be the case for the memory representations. Consequently,
75 * the memory representations ALWAYS contain at least as many
76 * bytes as the file representations. Otherwise, the memory
77 * structures would lose information, meaning they're not
78 * implemented properly.
80 * The words above apply to structures with the same members.
81 * If a future version changes the number of members, the
82 * relative structure sizes for different version must be
83 * tested with the compiler.
86 #define HI32 0x80000000UL
87 #define LO31 0x7fffffffUL
89 #define HI64 0x8000000000000000ULL
90 #define LO63 0x7fffffffffffffffULL
93 * These macros create indexes for accessing the bytes of
94 * words and halfwords for ELFCLASS64 data representations
95 * (currently ELFDATA2LSB and ELFDATA2MSB). In all cases,
97 * x = ((((((((((((X_7 << 8) + X_6) << 8) + X_5) << 8) + X_4) << 8
98 * + X_3) << 8) + X_2) << 8) + X_1) << 8) + X_0
99 * w = (((((X_3 << 8) + X_2) << 8) + X_1) << 8) + X_0
100 * h = (X_1 << 8) + X_0
102 * These assume the file representations for Addr, Off,
103 * Sword, and Word use 4 bytes, but the memory def's for
104 * the types may differ.
110 * enuma_*(n) define enum names for addr n
111 * enumb_*(n) define enum names for byte n
112 * enumh_*(n) define enum names for half n
113 * enumo_*(n) define enum names for off n
114 * enumw_*(n) define enum names for word n
115 * enumx_*(n) define enum names for xword n
116 * enuml_*(n) define enum names for Lword n
117 * tofa(d,s,n) xlate addr n from mem s to file d
118 * tofb(d,s,n) xlate byte n from mem s to file d
119 * tofh(d,s,n) xlate half n from mem s to file d
120 * tofo(d,s,n) xlate off n from mem s to file d
121 * tofw(d,s,n) xlate word n from mem s to file d
122 * tofx(d,s,n) xlate xword n from mem s to file d
123 * tofl(d,s,n) xlate Lword n from mem s to file d
124 * toma(s,n) xlate addr n from file s to expression value
125 * tomb(s,n) xlate byte n from file s to expression value
126 * tomh(s,n) xlate half n from file s to expression value
127 * tomo(s,n) xlate off n from file s to expression value
128 * tomw(s,n) xlate word n from file s to expression value
129 * tomx(s,n) xlate xword n from file s to expression value
130 * toml(s,n) xlate Lword n from file s to expression value
132 * tof*() macros must move a multi-byte value into a temporary
133 * because ``in place'' conversions are allowed. If a temp is not
134 * used for multi-byte objects, storing an initial destination byte
135 * may clobber a source byte not yet examined.
137 * tom*() macros compute an expression value from the source
138 * without touching the destination; so they're safe.
141 define(enuma_L, `$1_L0, $1_L1, $1_L2, $1_L3, $1_L4, $1_L5, $1_L6, $1_L7')dnl
142 define(enuma_M, `$1_M7, $1_M6, $1_M5, $1_M4, $1_M3, $1_M2, $1_M1, $1_M0')dnl
143 define(enumb_L, `$1_L')dnl
144 define(enumb_M, `$1_M')dnl
145 define(enumh_L, `$1_L0, $1_L1')dnl
146 define(enumh_M, `$1_M1, $1_M0')dnl
147 define(enumo_L, `$1_L0, $1_L1, $1_L2, $1_L3, $1_L4, $1_L5, $1_L6, $1_L7')dnl
148 define(enumo_M, `$1_M7, $1_M6, $1_M5, $1_M4, $1_M3, $1_M2, $1_M1, $1_M0')dnl
149 define(enumw_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
150 define(enumw_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
151 define(enumx_L, `$1_L0, $1_L1, $1_L2, $1_L3, $1_L4, $1_L5, $1_L6, $1_L7')dnl
152 define(enumx_M, `$1_M7, $1_M6, $1_M5, $1_M4, $1_M3, $1_M2, $1_M1, $1_M0')dnl
153 define(enuml_L, `$1_L0, $1_L1, $1_L2, $1_L3, $1_L4, $1_L5, $1_L6, $1_L7')dnl
154 define(enuml_M, `$1_M7, $1_M6, $1_M5, $1_M4, $1_M3, $1_M2, $1_M1, $1_M0')dnl
156 define(tofa, `{ Elf64_Addr _t_ = $2;
157 ($1)[$3`'0] = (Byte)_t_,
158 ($1)[$3`'1] = (Byte)(_t_>>8),
159 ($1)[$3`'2] = (Byte)(_t_>>16),
160 ($1)[$3`'3] = (Byte)(_t_>>24),
161 ($1)[$3`'4] = (Byte)(_t_>>32),
162 ($1)[$3`'5] = (Byte)(_t_>>40),
163 ($1)[$3`'6] = (Byte)(_t_>>48),
164 ($1)[$3`'7] = (Byte)(_t_>>56); }')dnl
165 define(tofb, `($1)[$3] = (Byte)($2)')dnl
166 define(tofh, `{ Elf64_Half _t_ = $2;
167 ($1)[$3`'0] = (Byte)_t_,
168 ($1)[$3`'1] = (Byte)(_t_>>8); }')dnl
169 define(tofo, `{ Elf64_Off _t_ = $2;
170 ($1)[$3`'0] = (Byte)_t_,
171 ($1)[$3`'1] = (Byte)(_t_>>8),
172 ($1)[$3`'2] = (Byte)(_t_>>16),
173 ($1)[$3`'3] = (Byte)(_t_>>24),
174 ($1)[$3`'4] = (Byte)(_t_>>32),
175 ($1)[$3`'5] = (Byte)(_t_>>40),
176 ($1)[$3`'6] = (Byte)(_t_>>48),
177 ($1)[$3`'7] = (Byte)(_t_>>56); }')dnl
178 define(tofw, `{ Elf64_Word _t_ = $2;
179 ($1)[$3`'0] = (Byte)_t_,
180 ($1)[$3`'1] = (Byte)(_t_>>8),
181 ($1)[$3`'2] = (Byte)(_t_>>16),
182 ($1)[$3`'3] = (Byte)(_t_>>24); }')dnl
183 define(tofx, `{ Elf64_Xword _t_ = $2;
184 ($1)[$3`'0] = (Byte)_t_,
185 ($1)[$3`'1] = (Byte)(_t_>>8),
186 ($1)[$3`'2] = (Byte)(_t_>>16),
187 ($1)[$3`'3] = (Byte)(_t_>>24),
188 ($1)[$3`'4] = (Byte)(_t_>>32),
189 ($1)[$3`'5] = (Byte)(_t_>>40),
190 ($1)[$3`'6] = (Byte)(_t_>>48),
191 ($1)[$3`'7] = (Byte)(_t_>>56); }')dnl
192 define(tofl, `{ Elf64_Lword _t_ = $2;
193 ($1)[$3`'0] = (Byte)_t_,
194 ($1)[$3`'1] = (Byte)(_t_>>8),
195 ($1)[$3`'2] = (Byte)(_t_>>16),
196 ($1)[$3`'3] = (Byte)(_t_>>24),
197 ($1)[$3`'4] = (Byte)(_t_>>32),
198 ($1)[$3`'5] = (Byte)(_t_>>40),
199 ($1)[$3`'6] = (Byte)(_t_>>48),
200 ($1)[$3`'7] = (Byte)(_t_>>56); }')dnl
202 define(toma, `(((((((((((Elf64_Addr)($1)[$2`'7]<<8)
210 define(tomb, `((Byte)($1)[$2])')dnl
211 define(tomh, `(((Elf64_Half)($1)[$2`'1]<<8)+($1)[$2`'0])')dnl
212 define(tomo, `(((((((((((Elf64_Off)($1)[$2`'7]<<8)
220 define(tomw, `(((((((Elf64_Word)($1)[$2`'3]<<8)
224 define(tomx, `(((((((((((Elf64_Xword)($1)[$2`'7]<<8)
232 define(toml, `(((((((((((Elf64_Lword)($1)[$2`'7]<<8)
243 * ELF data object indexes
244 * The enums are broken apart to get around deficiencies
251 enuma_$1(A)`'ifelse(`$2', `', `', `,
262 enumh_$1(H)`'ifelse(`$2', `', `', `,
272 enuml_$1(L)`'ifelse(`$2', `', `', `,
284 enumx_$1(M1_poffset),
286 enumh_$1(M1_stride)`'ifelse(`$2', `', `', `,
299 enumx_$1(MP1_poffset),
300 enumh_$1(MP1_repeat),
301 enumh_$1(MP1_stride),
302 enumw_$1(MP1_padding)`'ifelse(`$2', `', `', `,
313 enumo_$1(O)`'ifelse(`$2', `', `', `,
324 enumw_$1(W)`'ifelse(`$2', `', `', `,
335 enumx_$1(X)`'ifelse(`$2',`', `', `,
347 enumx_$1(D1_val)`'ifelse(`$2', `', `', `,
359 ifelse(`$2', `', `E1_ident, ')E1_ident_$1_Z = E1_Nident - 1,
361 enumh_$1(E1_machine),
362 enumw_$1(E1_version),
368 enumh_$1(E1_phentsize),
370 enumh_$1(E1_shentsize),
372 enumh_$1(E1_shstrndx)`'ifelse(`$2', `', `', `,
384 enumw_$1(N1_type)`'ifelse(`$2', `', `', `,
401 enumx_$1(P1_align)`'ifelse(`$2', `', `', `,
413 enumx_$1(R1_info)`'ifelse(`$2', `', `', `,
424 enuma_$1(RA1_offset),
426 enumx_$1(RA1_addend)`'ifelse(`$2', `', `', `,
441 enumo_$1(SH1_offset),
445 enumx_$1(SH1_addralign),
446 enumx_$1(SH1_entsize)`'ifelse(`$2', `', `', `,
462 enumx_$1(ST1_size)`'ifelse(`$2', `', `', `,
473 enumh_$1(SI1_boundto),
474 enumh_$1(SI1_flags)`'ifelse(`$2', `', `', `,
486 enumx_$1(C1_val)`'ifelse(`$2', `', `', `,
497 enumh_$1(VD1_version),
503 enumw_$1(VD1_next)`'ifelse(`$2', `', `', `,
515 enumw_$1(VDA1_next)`'ifelse(`$2', `', `', `,
526 enumh_$1(VN1_version),
530 enumw_$1(VN1_next)`'ifelse(`$2', `', `', `,
542 enumh_$1(VNA1_flags),
543 enumh_$1(VNA1_other),
545 enumw_$1(VNA1_next)`'ifelse(`$2', `', `', `,
554 * Translation function declarations.
556 * <object>_<data><dver><sver>_tof
557 * <object>_<data><dver><sver>_tom
559 * <data> 2L ELFDATA2LSB
563 static void addr_2L_tof(), addr_2L_tom(),
564 addr_2M_tof(), addr_2M_tom(),
566 dyn_2L11_tof(), dyn_2L11_tom(),
567 dyn_2M11_tof(), dyn_2M11_tom(),
568 ehdr_2L11_tof(), ehdr_2L11_tom(),
569 ehdr_2M11_tof(), ehdr_2M11_tom(),
570 half_2L_tof(), half_2L_tom(),
571 half_2M_tof(), half_2M_tom(),
572 move_2L11_tof(), move_2L11_tom(),
573 move_2M11_tof(), move_2M11_tom(),
574 movep_2L11_tof(), movep_2L11_tom(),
575 movep_2M11_tof(), movep_2M11_tom(),
576 off_2L_tof(), off_2L_tom(),
577 off_2M_tof(), off_2M_tom(),
578 note_2L11_tof(), note_2L11_tom(),
579 note_2M11_tof(), note_2M11_tom(),
580 phdr_2L11_tof(), phdr_2L11_tom(),
581 phdr_2M11_tof(), phdr_2M11_tom(),
582 rel_2L11_tof(), rel_2L11_tom(),
583 rel_2M11_tof(), rel_2M11_tom(),
584 rela_2L11_tof(), rela_2L11_tom(),
585 rela_2M11_tof(), rela_2M11_tom(),
586 shdr_2L11_tof(), shdr_2L11_tom(),
587 shdr_2M11_tof(), shdr_2M11_tom(),
588 sword_2L_tof(), sword_2L_tom(),
589 sword_2M_tof(), sword_2M_tom(),
590 sym_2L11_tof(), sym_2L11_tom(),
591 sym_2M11_tof(), sym_2M11_tom(),
592 syminfo_2L11_tof(), syminfo_2L11_tom(),
593 syminfo_2M11_tof(), syminfo_2M11_tom(),
594 word_2L_tof(), word_2L_tom(),
595 word_2M_tof(), word_2M_tom(),
596 verdef_2L11_tof(), verdef_2L11_tom(),
597 verdef_2M11_tof(), verdef_2M11_tom(),
598 verneed_2L11_tof(), verneed_2L11_tom(),
599 verneed_2M11_tof(), verneed_2M11_tom(),
600 sxword_2L_tof(), sxword_2L_tom(),
601 sxword_2M_tof(), sxword_2M_tom(),
602 xword_2L_tof(), xword_2L_tom(),
603 xword_2M_tof(), xword_2M_tom(),
604 cap_2L11_tof(), cap_2L11_tom(),
605 cap_2M11_tof(), cap_2M11_tom();
609 * x64 [dst_version - 1] [src_version - 1] [encode - 1] [type]
615 } x64 [EV_CURRENT] [EV_CURRENT] [ELFDATANUM - 1] [ELF_T_NUM] = {
618 { /* [1-1][1-1][2LSB-1][.] */
619 /* BYTE */ { byte_to, byte_to },
620 /* ADDR */ { addr_2L_tof, addr_2L_tom },
621 /* DYN */ { dyn_2L11_tof, dyn_2L11_tom },
622 /* EHDR */ { ehdr_2L11_tof, ehdr_2L11_tom },
623 /* HALF */ { half_2L_tof, half_2L_tom },
624 /* OFF */ { off_2L_tof, off_2L_tom },
625 /* PHDR */ { phdr_2L11_tof, phdr_2L11_tom },
626 /* RELA */ { rela_2L11_tof, rela_2L11_tom },
627 /* REL */ { rel_2L11_tof, rel_2L11_tom },
628 /* SHDR */ { shdr_2L11_tof, shdr_2L11_tom },
629 /* SWORD */ { sword_2L_tof, sword_2L_tom },
630 /* SYM */ { sym_2L11_tof, sym_2L11_tom },
631 /* WORD */ { word_2L_tof, word_2L_tom },
632 /* VERDEF */ { verdef_2L11_tof, verdef_2L11_tom},
633 /* VERNEED */ { verneed_2L11_tof, verneed_2L11_tom},
634 /* SXWORD */ { sxword_2L_tof, sxword_2L_tom },
635 /* XWORD */ { xword_2L_tof, xword_2L_tom },
636 /* SYMINFO */ { syminfo_2L11_tof, syminfo_2L11_tom },
637 /* NOTE */ { note_2L11_tof, note_2L11_tom },
638 /* MOVE */ { move_2L11_tof, move_2L11_tom },
639 /* MOVEP */ { movep_2L11_tof, movep_2L11_tom },
640 /* CAP */ { cap_2L11_tof, cap_2L11_tom },
642 { /* [1-1][1-1][2MSB-1][.] */
643 /* BYTE */ { byte_to, byte_to },
644 /* ADDR */ { addr_2M_tof, addr_2M_tom },
645 /* DYN */ { dyn_2M11_tof, dyn_2M11_tom },
646 /* EHDR */ { ehdr_2M11_tof, ehdr_2M11_tom },
647 /* HALF */ { half_2M_tof, half_2M_tom },
648 /* OFF */ { off_2M_tof, off_2M_tom },
649 /* PHDR */ { phdr_2M11_tof, phdr_2M11_tom },
650 /* RELA */ { rela_2M11_tof, rela_2M11_tom },
651 /* REL */ { rel_2M11_tof, rel_2M11_tom },
652 /* SHDR */ { shdr_2M11_tof, shdr_2M11_tom },
653 /* SWORD */ { sword_2M_tof, sword_2M_tom },
654 /* SYM */ { sym_2M11_tof, sym_2M11_tom },
655 /* WORD */ { word_2M_tof, word_2M_tom },
656 /* VERDEF */ { verdef_2M11_tof, verdef_2M11_tom},
657 /* VERNEED */ { verneed_2M11_tof, verneed_2M11_tom},
658 /* SXWORD */ { sxword_2M_tof, sxword_2M_tom },
659 /* XWORD */ { xword_2M_tof, xword_2M_tom },
660 /* SYMINFO */ { syminfo_2M11_tof, syminfo_2M11_tom },
661 /* NOTE */ { note_2M11_tof, note_2M11_tom },
662 /* MOVE */ { move_2M11_tof, move_2M11_tom },
663 /* MOVEP */ { movep_2M11_tof, movep_2M11_tom },
664 /* CAP */ { cap_2M11_tof, cap_2M11_tom },
672 * size [version - 1] [type]
675 static const struct {
678 } fmsize [EV_CURRENT] [ELF_T_NUM] =
682 /* ADDR */ { A_sizeof, sizeof (Elf64_Addr) },
683 /* DYN */ { D1_sizeof, sizeof (Elf64_Dyn) },
684 /* EHDR */ { E1_sizeof, sizeof (Elf64_Ehdr) },
685 /* HALF */ { H_sizeof, sizeof (Elf64_Half) },
686 /* OFF */ { O_sizeof, sizeof (Elf64_Off) },
687 /* PHDR */ { P1_sizeof, sizeof (Elf64_Phdr) },
688 /* RELA */ { RA1_sizeof, sizeof (Elf64_Rela) },
689 /* REL */ { R1_sizeof, sizeof (Elf64_Rel) },
690 /* SHDR */ { SH1_sizeof, sizeof (Elf64_Shdr) },
691 /* SWORD */ { W_sizeof, sizeof (Elf64_Sword) },
692 /* SYM */ { ST1_sizeof, sizeof (Elf64_Sym) },
693 /* WORD */ { W_sizeof, sizeof (Elf64_Word) },
694 /* VERDEF */ { 1, 1 }, /* both VERDEF & VERNEED have varying size */
695 /* VERNEED */ { 1, 1 }, /* structures so we set their sizes to 1 */
696 /* SXWORD */ { X_sizeof, sizeof (Elf64_Sxword) },
697 /* XWORD */ { X_sizeof, sizeof (Elf64_Xword) },
698 /* SYMINFO */ { SI1_sizeof, sizeof (Elf64_Syminfo) },
699 /* NOTE */ { 1, 1}, /* NOTE has varying sized data we can't */
700 /* use the usual table magic. */
701 /* MOVE */ { M1_sizeof, sizeof (Elf64_Move) },
702 /* MOVEP */ { MP1_sizeof, sizeof (Elf64_Move) },
703 /* CAP */ { C1_sizeof, sizeof (Elf64_Cap) },
709 * memory type [version - 1] [section type]
712 static const Elf_Type mtype[EV_CURRENT][SHT_NUM] =
715 /* NULL */ ELF_T_BYTE,
716 /* PROGBITS */ ELF_T_BYTE,
717 /* SYMTAB */ ELF_T_SYM,
718 /* STRTAB */ ELF_T_BYTE,
719 /* RELA */ ELF_T_RELA,
720 /* HASH */ ELF_T_WORD,
721 /* DYNAMIC */ ELF_T_DYN,
722 /* NOTE */ ELF_T_NOTE,
723 /* NOBITS */ ELF_T_BYTE,
725 /* SHLIB */ ELF_T_BYTE,
726 /* DYNSYM */ ELF_T_SYM,
727 /* UNKNOWN12 */ ELF_T_BYTE,
728 /* UNKNOWN13 */ ELF_T_BYTE,
729 /* INIT_ARRAY */ ELF_T_ADDR,
730 /* FINI_ARRAY */ ELF_T_ADDR,
731 /* PREINIT_ARRAY */ ELF_T_ADDR,
732 /* GROUP */ ELF_T_WORD,
733 /* SYMTAB_SHNDX */ ELF_T_WORD
739 elf64_fsize(Elf_Type type, size_t count, unsigned ver)
741 if (--ver >= EV_CURRENT) {
742 _elf_seterr(EREQ_VER, 0);
745 if ((unsigned)type >= ELF_T_NUM) {
746 _elf_seterr(EREQ_TYPE, 0);
749 return (fmsize[ver][type].s_filesz * count);
754 _elf64_msize(Elf_Type type, unsigned ver)
756 return (fmsize[ver - 1][type].s_memsz);
762 _elf64_mtype(Elf * elf, Elf64_Word shtype, unsigned ver)
764 Elf64_Ehdr * ehdr = (Elf64_Ehdr *)elf->ed_ehdr;
766 if (shtype < SHT_NUM)
767 return (mtype[ver - 1][shtype]);
770 case SHT_SUNW_symsort:
771 case SHT_SUNW_tlssort:
773 case SHT_SUNW_LDYNSYM:
779 case SHT_SUNW_capchain:
781 case SHT_SUNW_capinfo:
782 return (ELF_T_XWORD);
783 case SHT_SUNW_SIGNATURE:
785 case SHT_SUNW_ANNOTATE:
787 case SHT_SUNW_DEBUGSTR:
793 * Right now - the only 64bit binaries I know
794 * about with a move is SPARC - and SPARC
795 * binaries pad the size of the move.
797 return (ELF_T_MOVEP);
798 case SHT_SUNW_COMDAT:
800 case SHT_SUNW_syminfo:
801 return (ELF_T_SYMINFO);
802 case SHT_SUNW_verdef:
804 case SHT_SUNW_verneed:
805 return (ELF_T_VNEED);
806 case SHT_SUNW_versym:
811 * Check for the sparc specific section types
814 if (((ehdr->e_machine == EM_SPARC) ||
815 (ehdr->e_machine == EM_SPARC32PLUS) ||
816 (ehdr->e_machine == EM_SPARCV9)) &&
817 (shtype == SHT_SPARC_GOTDATA))
821 * Check for the amd64 specific section types
824 if ((ehdr->e_machine == EM_AMD64) &&
825 (shtype == SHT_AMD64_UNWIND))
829 * And the default is ELF_T_BYTE - but we should
830 * certainly have caught any sections we know about
831 * above. This is for unknown sections to libelf.
838 _elf64_entsz(Elf *elf, Elf64_Word shtype, unsigned ver)
842 ttype = _elf64_mtype(elf, shtype, ver);
843 return ((ttype == ELF_T_BYTE) ? 0 : fmsize[ver - 1][ttype].s_filesz);
848 xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof)
851 size_t cnt, dsz, ssz;
857 if (dst == 0 || src == 0)
859 if (--encode >= (ELFDATANUM - 1)) {
860 _elf_seterr(EREQ_ENCODE, 0);
863 if ((dver = dst->d_version - 1) >= EV_CURRENT ||
864 (sver = src->d_version - 1) >= EV_CURRENT) {
865 _elf_seterr(EREQ_VER, 0);
868 if ((type = src->d_type) >= ELF_T_NUM) {
869 _elf_seterr(EREQ_TYPE, 0);
874 dsz = fmsize[dver][type].s_filesz;
875 ssz = fmsize[sver][type].s_memsz;
876 f = x64[dver][sver][encode][type].x_tof;
878 dsz = fmsize[dver][type].s_memsz;
879 ssz = fmsize[sver][type].s_filesz;
880 f = x64[dver][sver][encode][type].x_tom;
882 cnt = src->d_size / ssz;
883 if (dst->d_size < dsz * cnt) {
884 _elf_seterr(EREQ_DSZ, 0);
888 ELFACCESSDATA(_encode, _elf_encode)
889 if ((_encode == (encode + 1)) && (dsz == ssz)) {
891 * ld(1) frequently produces empty sections (eg. .dynsym,
892 * .dynstr, .symtab, .strtab, etc) so that the initial
893 * output image can be created of the correct size. Later
894 * these sections are filled in with the associated data.
895 * So that we don't have to pre-allocate buffers for
896 * these segments, allow for the src destination to be 0.
898 if (src->d_buf && src->d_buf != dst->d_buf)
899 (void) memcpy(dst->d_buf, src->d_buf, src->d_size);
900 dst->d_type = src->d_type;
901 dst->d_size = src->d_size;
905 (*f)(dst->d_buf, src->d_buf, cnt);
906 dst->d_size = dsz * cnt;
907 dst->d_type = src->d_type;
913 elf64_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode)
915 return (xlate(dst, src, encode, 1));
920 elf64_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode)
922 return (xlate(dst, src, encode, 0));
927 * xlate to file format
929 * ..._tof(name, data) -- macros
931 * Recall that the file format must be no larger than the
932 * memory format (equal versions). Use "forward" copy.
933 * All these routines require non-null, non-zero arguments.
938 $1(Byte *dst, Elf64_Addr *src, size_t cnt)
940 Elf64_Addr *end = src + cnt;
943 tofa(dst, *src, A_$2);
945 } while (++src < end);
948 addr_tof(addr_2L_tof,L)
949 addr_tof(addr_2M_tof,M)
953 byte_to(Byte *dst, Byte *src, size_t cnt)
956 (void) memcpy(dst, src, cnt);
962 $1(Byte *dst, Elf64_Dyn *src, size_t cnt)
964 Elf64_Dyn *end = src + cnt;
967 tofx(dst, src->d_tag, D1_tag_$2);
968 tofx(dst, src->d_un.d_val, D1_val_$2);
970 } while (++src < end);
973 dyn_11_tof(dyn_2L11_tof,L)
974 dyn_11_tof(dyn_2M11_tof,M)
977 define(ehdr_11_tof, `
979 $1(Byte *dst, Elf64_Ehdr *src, size_t cnt)
981 Elf64_Ehdr *end = src + cnt;
984 if (&dst[E1_ident] != src->e_ident)
985 (void) memcpy(&dst[E1_ident], src->e_ident, E1_Nident);
986 tofh(dst, src->e_type, E1_type_$2);
987 tofh(dst, src->e_machine, E1_machine_$2);
988 tofw(dst, src->e_version, E1_version_$2);
989 tofa(dst, src->e_entry, E1_entry_$2);
990 tofo(dst, src->e_phoff, E1_phoff_$2);
991 tofo(dst, src->e_shoff, E1_shoff_$2);
992 tofw(dst, src->e_flags, E1_flags_$2);
993 tofh(dst, src->e_ehsize, E1_ehsize_$2);
994 tofh(dst, src->e_phentsize, E1_phentsize_$2);
995 tofh(dst, src->e_phnum, E1_phnum_$2);
996 tofh(dst, src->e_shentsize, E1_shentsize_$2);
997 tofh(dst, src->e_shnum, E1_shnum_$2);
998 tofh(dst, src->e_shstrndx, E1_shstrndx_$2);
1000 } while (++src < end);
1003 ehdr_11_tof(ehdr_2L11_tof,L)
1004 ehdr_11_tof(ehdr_2M11_tof,M)
1009 $1(Byte *dst, Elf64_Half *src, size_t cnt)
1011 Elf64_Half *end = src + cnt;
1014 tofh(dst, *src, H_$2);
1016 } while (++src < end);
1019 half_tof(half_2L_tof,L)
1020 half_tof(half_2M_tof,M)
1023 define(move_11_tof, `
1025 $1(unsigned char *dst, Elf64_Move *src, size_t cnt)
1027 Elf64_Move *end = src + cnt;
1030 tofl(dst, src->m_value, M1_value_$2);
1031 tofw(dst, src->m_info, M1_info_$2);
1032 tofw(dst, src->m_poffset, M1_poffset_$2);
1033 tofh(dst, src->m_repeat, M1_repeat_$2);
1034 tofh(dst, src->m_stride, M1_stride_$2);
1036 } while (++src < end);
1039 move_11_tof(move_2L11_tof,L)
1040 move_11_tof(move_2M11_tof,M)
1043 define(movep_11_tof, `
1045 $1(unsigned char *dst, Elf64_Move *src, size_t cnt)
1047 Elf64_Move *end = src + cnt;
1050 tofl(dst, src->m_value, MP1_value_$2);
1051 tofw(dst, src->m_info, MP1_info_$2);
1052 tofw(dst, src->m_poffset, MP1_poffset_$2);
1053 tofh(dst, src->m_repeat, MP1_repeat_$2);
1054 tofh(dst, src->m_stride, MP1_stride_$2);
1056 } while (++src < end);
1059 movep_11_tof(movep_2L11_tof,L)
1060 movep_11_tof(movep_2M11_tof,M)
1065 $1(Byte *dst, Elf64_Off *src, size_t cnt)
1067 Elf64_Off *end = src + cnt;
1070 tofo(dst, *src, O_$2);
1072 } while (++src < end);
1075 off_tof(off_2L_tof,L)
1076 off_tof(off_2M_tof,M)
1079 define(note_11_tof, `
1081 $1(unsigned char *dst, Elf64_Nhdr *src, size_t cnt)
1084 Elf64_Nhdr *end = (Elf64_Nhdr *)((char *)src + cnt);
1087 * Copy the note data to the source, translating the
1088 * length fields. Clip against the size of the actual buffer
1089 * to guard against corrupt note data.
1092 Elf64_Word descsz, namesz;
1095 * cache size of desc & name fields - while rounding
1098 namesz = S_ROUND(src->n_namesz, sizeof (Elf64_Word));
1099 descsz = src->n_descsz;
1102 * Copy contents of Elf64_Nhdr
1104 if ((offsetof(Elf64_Nhdr, n_namesz) + sizeof(Elf64_Word) +
1105 (char *) src) >= (char *) end)
1107 tofw(dst, src->n_namesz, N1_namesz_$2);
1109 if ((offsetof(Elf64_Nhdr, n_descsz) + sizeof(Elf64_Word) +
1110 (char *) src) >= (char *) end)
1112 tofw(dst, src->n_descsz, N1_descsz_$2);
1114 if ((offsetof(Elf64_Nhdr, n_type) + sizeof(Elf64_Word) +
1115 (char *) src) >= (char *) end)
1117 tofw(dst, src->n_type, N1_type_$2);
1120 * Copy contents of Name field
1124 if ((namesz + (char *) src) > (char *) end) {
1125 namesz = (char *) end - (char *) src;
1129 (void)memcpy(dst, src, namesz);
1132 * Copy contents of desc field
1135 src = (Elf64_Nhdr *)((uintptr_t)src + namesz);
1136 if ((descsz + (char *) src) > (char *) end) {
1137 descsz = (char *) end - (char *) src;
1141 (void)memcpy(dst, src, descsz);
1143 descsz = S_ROUND(descsz, sizeof (Elf64_Word));
1145 src = (Elf64_Nhdr *)((uintptr_t)src + descsz);
1146 } while (src < end);
1149 note_11_tof(note_2L11_tof,L)
1150 note_11_tof(note_2M11_tof,M)
1153 define(phdr_11_tof, `
1155 $1(Byte *dst, Elf64_Phdr *src, size_t cnt)
1157 Elf64_Phdr *end = src + cnt;
1160 tofw(dst, src->p_type, P1_type_$2);
1161 tofw(dst, src->p_flags, P1_flags_$2);
1162 tofo(dst, src->p_offset, P1_offset_$2);
1163 tofa(dst, src->p_vaddr, P1_vaddr_$2);
1164 tofa(dst, src->p_paddr, P1_paddr_$2);
1165 tofx(dst, src->p_filesz, P1_filesz_$2);
1166 tofx(dst, src->p_memsz, P1_memsz_$2);
1167 tofx(dst, src->p_align, P1_align_$2);
1169 } while (++src < end);
1172 phdr_11_tof(phdr_2L11_tof,L)
1173 phdr_11_tof(phdr_2M11_tof,M)
1176 define(rel_11_tof, `
1178 $1(Byte *dst, Elf64_Rel *src, size_t cnt)
1180 Elf64_Rel *end = src + cnt;
1183 tofa(dst, src->r_offset, R1_offset_$2);
1184 tofx(dst, src->r_info, R1_info_$2);
1186 } while (++src < end);
1189 rel_11_tof(rel_2L11_tof,L)
1190 rel_11_tof(rel_2M11_tof,M)
1193 define(rela_11_tof, `
1195 $1(Byte *dst, Elf64_Rela *src, size_t cnt)
1197 Elf64_Rela *end = src + cnt;
1200 tofa(dst, src->r_offset, RA1_offset_$2);
1201 tofx(dst, src->r_info, RA1_info_$2);
1202 /*CONSTANTCONDITION*/
1203 if (~(Elf64_Xword)0 == -(Elf64_Sxword)1) { /* 2s comp */
1204 tofx(dst, src->r_addend, RA1_addend_$2);
1208 if (src->r_addend < 0) {
1209 w = - src->r_addend;
1213 tofx(dst, w, RA1_addend_$2);
1216 } while (++src < end);
1219 rela_11_tof(rela_2L11_tof,L)
1220 rela_11_tof(rela_2M11_tof,M)
1223 define(shdr_11_tof, `
1225 $1(Byte *dst, Elf64_Shdr *src, size_t cnt)
1227 Elf64_Shdr *end = src + cnt;
1230 tofw(dst, src->sh_name, SH1_name_$2);
1231 tofw(dst, src->sh_type, SH1_type_$2);
1232 tofx(dst, src->sh_flags, SH1_flags_$2);
1233 tofa(dst, src->sh_addr, SH1_addr_$2);
1234 tofo(dst, src->sh_offset, SH1_offset_$2);
1235 tofx(dst, src->sh_size, SH1_size_$2);
1236 tofw(dst, src->sh_link, SH1_link_$2);
1237 tofw(dst, src->sh_info, SH1_info_$2);
1238 tofx(dst, src->sh_addralign, SH1_addralign_$2);
1239 tofx(dst, src->sh_entsize, SH1_entsize_$2);
1241 } while (++src < end);
1244 shdr_11_tof(shdr_2L11_tof,L)
1245 shdr_11_tof(shdr_2M11_tof,M)
1250 $1(Byte *dst, Elf64_Sword *src, size_t cnt)
1252 Elf64_Sword *end = src + cnt;
1255 /*CONSTANTCONDITION*/
1256 if (~(Elf64_Word)0 == -(Elf64_Sword)1) { /* 2s comp */
1257 tofw(dst, *src, W_$2);
1269 } while (++src < end);
1272 sword_tof(sword_2L_tof,L)
1273 sword_tof(sword_2M_tof,M)
1276 define(cap_11_tof, `
1278 $1(unsigned char *dst, Elf64_Cap *src, size_t cnt)
1280 Elf64_Cap *end = src + cnt;
1283 tofx(dst, src->c_tag, C1_tag_$2);
1284 tofx(dst, src->c_un.c_val, C1_val_$2);
1286 } while (++src < end);
1289 cap_11_tof(cap_2L11_tof,L)
1290 cap_11_tof(cap_2M11_tof,M)
1293 define(syminfo_11_tof, `
1295 $1(unsigned char *dst, Elf64_Syminfo *src, size_t cnt)
1297 Elf64_Syminfo *end = src + cnt;
1300 tofh(dst, src->si_boundto, SI1_boundto_$2);
1301 tofh(dst, src->si_flags, SI1_flags_$2);
1303 } while (++src < end);
1306 syminfo_11_tof(syminfo_2L11_tof,L)
1307 syminfo_11_tof(syminfo_2M11_tof,M)
1310 define(sym_11_tof, `
1312 $1(Byte *dst, Elf64_Sym *src, size_t cnt)
1314 Elf64_Sym *end = src + cnt;
1317 tofw(dst, src->st_name, ST1_name_$2);
1318 tofb(dst, src->st_info, ST1_info_$2);
1319 tofb(dst, src->st_other, ST1_other_$2);
1320 tofh(dst, src->st_shndx, ST1_shndx_$2);
1321 tofa(dst, src->st_value, ST1_value_$2);
1322 tofx(dst, src->st_size, ST1_size_$2);
1324 } while (++src < end);
1327 sym_11_tof(sym_2L11_tof,L)
1328 sym_11_tof(sym_2M11_tof,M)
1333 $1(Byte *dst, Elf64_Word *src, size_t cnt)
1335 Elf64_Word *end = src + cnt;
1338 tofw(dst, *src, W_$2);
1340 } while (++src < end);
1343 word_tof(word_2L_tof,L)
1344 word_tof(word_2M_tof,M)
1347 define(verdef_11_tof, `
1349 $1(Byte *dst, Elf64_Verdef *src, size_t cnt)
1352 Elf64_Verdef *end = (Elf64_Verdef *)((Byte *)src + cnt);
1355 Elf64_Verdef *next_verdef;
1356 Elf64_Verdaux *vaux;
1362 next_verdef = (Elf64_Verdef *)(src->vd_next ?
1363 (Byte *)src + src->vd_next : (Byte *)end);
1364 dst_next = dst + src->vd_next;
1367 vaux = (Elf64_Verdaux *)((Byte *)src + src->vd_aux);
1368 vaux_dst = dst + src->vd_aux;
1371 * Convert auxilary structures
1373 for (i = 0; i < src->vd_cnt; i++) {
1374 Elf64_Verdaux *vaux_next;
1375 Byte *vaux_dst_next;
1378 * because our source and destination can be
1379 * the same place we need to figure out the next
1383 vaux_next = (Elf64_Verdaux *)((Byte *)vaux +
1385 vaux_dst_next = vaux_dst + vaux->vda_next;
1387 tofw(vaux_dst, vaux->vda_name, VDA1_name_$2);
1388 tofw(vaux_dst, vaux->vda_next, VDA1_next_$2);
1389 vaux_dst = vaux_dst_next;
1394 * Convert Elf64_Verdef structure.
1396 tofh(dst, src->vd_version, VD1_version_$2);
1397 tofh(dst, src->vd_flags, VD1_flags_$2);
1398 tofh(dst, src->vd_ndx, VD1_ndx_$2);
1399 tofh(dst, src->vd_cnt, VD1_cnt_$2);
1400 tofw(dst, src->vd_hash, VD1_hash_$2);
1401 tofw(dst, src->vd_aux, VD1_aux_$2);
1402 tofw(dst, src->vd_next, VD1_next_$2);
1405 } while (src < end);
1408 verdef_11_tof(verdef_2L11_tof, L)
1409 verdef_11_tof(verdef_2M11_tof, M)
1411 define(verneed_11_tof, `
1413 $1(Byte *dst, Elf64_Verneed *src, size_t cnt)
1416 Elf64_Verneed *end = (Elf64_Verneed *)((char *)src + cnt);
1419 Elf64_Verneed * next_verneed;
1420 Elf64_Vernaux * vaux;
1426 next_verneed = (Elf64_Verneed *)(src->vn_next ?
1427 (Byte *)src + src->vn_next : (Byte *)end);
1428 dst_next = dst + src->vn_next;
1431 vaux = (Elf64_Vernaux *)((Byte *)src + src->vn_aux);
1432 vaux_dst = dst + src->vn_aux;
1435 * Convert auxilary structures first
1437 for (i = 0; i < src->vn_cnt; i++) {
1438 Elf64_Vernaux *vaux_next;
1439 Byte *vaux_dst_next;
1442 * because our source and destination can be
1443 * the same place we need to figure out the
1444 * next location now.
1447 vaux_next = (Elf64_Vernaux *)((Byte *)vaux +
1449 vaux_dst_next = vaux_dst + vaux->vna_next;
1451 tofw(vaux_dst, vaux->vna_hash, VNA1_hash_$2);
1452 tofh(vaux_dst, vaux->vna_flags, VNA1_flags_$2);
1453 tofh(vaux_dst, vaux->vna_other, VNA1_other_$2);
1454 tofw(vaux_dst, vaux->vna_name, VNA1_name_$2);
1455 tofw(vaux_dst, vaux->vna_next, VNA1_next_$2);
1456 vaux_dst = vaux_dst_next;
1461 * Convert Elf64_Verneed structure.
1463 tofh(dst, src->vn_version, VN1_version_$2);
1464 tofh(dst, src->vn_cnt, VN1_cnt_$2);
1465 tofw(dst, src->vn_file, VN1_file_$2);
1466 tofw(dst, src->vn_aux, VN1_aux_$2);
1467 tofw(dst, src->vn_next, VN1_next_$2);
1470 } while (src < end);
1473 verneed_11_tof(verneed_2L11_tof, L)
1474 verneed_11_tof(verneed_2M11_tof, M)
1477 define(sxword_tof, `
1479 $1(Byte *dst, Elf64_Sxword *src, size_t cnt)
1481 Elf64_Sxword *end = src + cnt;
1484 /*CONSTANTCONDITION*/
1485 if (~(Elf64_Xword)0 == -(Elf64_Sxword)1) { /* 2s comp */
1486 tofx(dst, *src, X_$2);
1488 else { /* unknown */
1499 } while (++src < end);
1502 sxword_tof(sxword_2L_tof,L)
1503 sxword_tof(sxword_2M_tof,M)
1508 $1(Byte *dst, Elf64_Xword *src, size_t cnt)
1510 Elf64_Xword *end = src + cnt;
1513 tofx(dst, *src, X_$2);
1515 } while (++src < end);
1518 xword_tof(xword_2L_tof,L)
1519 xword_tof(xword_2M_tof,M)
1523 * xlate to memory format
1525 * ..._tom(name, data) -- macros
1527 * Recall that the memory format may be larger than the
1528 * file format (equal versions). Use "backward" copy.
1529 * All these routines require non-null, non-zero arguments.
1535 $1(Elf64_Addr *dst, Byte *src, size_t cnt)
1537 Elf64_Addr *end = dst;
1540 src += cnt * A_sizeof;
1541 while (dst-- > end) {
1543 *dst = toma(src, A_$2);
1547 addr_tom(addr_2L_tom,L)
1548 addr_tom(addr_2M_tom,M)
1551 define(dyn_11_tom, `
1553 $1(Elf64_Dyn *dst, Byte *src, size_t cnt)
1555 Elf64_Dyn *end = dst + cnt;
1558 dst->d_tag = tomx(src, D1_tag_$2);
1559 dst->d_un.d_val = tomx(src, D1_val_$2);
1561 } while (++dst < end);
1564 dyn_11_tom(dyn_2L11_tom,L)
1565 dyn_11_tom(dyn_2M11_tom,M)
1568 define(ehdr_11_tom, `
1570 $1(Elf64_Ehdr *dst, Byte *src, size_t cnt)
1572 Elf64_Ehdr *end = dst;
1575 src += cnt * E1_sizeof;
1576 while (dst-- > end) {
1578 dst->e_shstrndx = tomh(src, E1_shstrndx_$2);
1579 dst->e_shnum = tomh(src, E1_shnum_$2);
1580 dst->e_shentsize = tomh(src, E1_shentsize_$2);
1581 dst->e_phnum = tomh(src, E1_phnum_$2);
1582 dst->e_phentsize = tomh(src, E1_phentsize_$2);
1583 dst->e_ehsize = tomh(src, E1_ehsize_$2);
1584 dst->e_flags = tomw(src, E1_flags_$2);
1585 dst->e_shoff = tomo(src, E1_shoff_$2);
1586 dst->e_phoff = tomo(src, E1_phoff_$2);
1587 dst->e_entry = toma(src, E1_entry_$2);
1588 dst->e_version = tomw(src, E1_version_$2);
1589 dst->e_machine = tomh(src, E1_machine_$2);
1590 dst->e_type = tomh(src, E1_type_$2);
1591 if (dst->e_ident != &src[E1_ident])
1592 (void) memcpy(dst->e_ident, &src[E1_ident], E1_Nident);
1596 ehdr_11_tom(ehdr_2L11_tom,L)
1597 ehdr_11_tom(ehdr_2M11_tom,M)
1602 $1(Elf64_Half *dst, Byte *src, size_t cnt)
1604 Elf64_Half *end = dst;
1607 src += cnt * H_sizeof;
1608 while (dst-- > end) {
1610 *dst = tomh(src, H_$2);
1614 half_tom(half_2L_tom,L)
1615 half_tom(half_2M_tom,M)
1618 define(move_11_tom, `
1620 $1(Elf64_Move *dst, unsigned char *src, size_t cnt)
1622 Elf64_Move *end = dst + cnt;
1625 dst->m_value = toml(src, M1_value_$2);
1626 dst->m_info = tomw(src, M1_info_$2);
1627 dst->m_poffset = tomw(src, M1_poffset_$2);
1628 dst->m_repeat = tomh(src, M1_repeat_$2);
1629 dst->m_stride = tomh(src, M1_stride_$2);
1631 } while (++dst < end);
1634 move_11_tom(move_2L11_tom,L)
1635 move_11_tom(move_2M11_tom,M)
1638 define(movep_11_tom, `
1640 $1(Elf64_Move *dst, unsigned char *src, size_t cnt)
1642 Elf64_Move *end = dst + cnt;
1646 dst->m_value = toml(src, MP1_value_$2);
1647 dst->m_info = tomw(src, MP1_info_$2);
1648 dst->m_poffset = tomw(src, MP1_poffset_$2);
1649 dst->m_repeat = tomh(src, MP1_repeat_$2);
1650 dst->m_stride = tomh(src, MP1_stride_$2);
1652 } while (++dst < end);
1655 movep_11_tom(movep_2L11_tom,L)
1656 movep_11_tom(movep_2M11_tom,M)
1659 define(note_11_tom, `
1661 $1(Elf64_Nhdr *dst, unsigned char *src, size_t cnt)
1664 Elf64_Nhdr *end = (Elf64_Nhdr *)((char *)dst + cnt);
1667 * Copy the note data to the destination, translating the
1668 * length fields. Clip against the size of the actual buffer
1669 * to guard against corrupt note data.
1673 unsigned char *namestr;
1675 Elf64_Word field_sz;
1677 if ((offsetof(Elf64_Nhdr, n_namesz) + sizeof(Elf64_Word) +
1678 (char *) dst) >= (char *) end)
1680 dst->n_namesz = tomw(src, N1_namesz_$2);
1682 if ((offsetof(Elf64_Nhdr, n_descsz) + sizeof(Elf64_Word) +
1683 (char *) dst) >= (char *) end)
1685 dst->n_descsz = tomw(src, N1_descsz_$2);
1687 if ((offsetof(Elf64_Nhdr, n_type) + sizeof(Elf64_Word) +
1688 (char *) dst) >= (char *) end)
1690 dst->n_type = tomw(src, N1_type_$2);
1694 dst = (Elf64_Nhdr *)((char *)dst + sizeof (Elf64_Nhdr));
1695 namestr = src + N1_sizeof;
1696 field_sz = S_ROUND(nhdr->n_namesz, sizeof (Elf64_Word));
1697 if ((field_sz + (char *) dst) > (char *) end) {
1698 field_sz = (char *) end - (char *) dst;
1702 (void)memcpy((void *)dst, namestr, field_sz);
1703 desc = namestr + field_sz;
1706 dst = (Elf64_Nhdr *)((char *)dst + field_sz);
1707 field_sz = nhdr->n_descsz;
1708 if ((field_sz + (char *) dst) > (char *) end) {
1709 field_sz = (char *) end - (char *) dst;
1713 (void)memcpy(dst, desc, field_sz);
1714 field_sz = S_ROUND(field_sz, sizeof (Elf64_Word));
1717 dst = (Elf64_Nhdr *)((char *)dst + field_sz);
1718 src = (unsigned char *)desc + field_sz;
1722 note_11_tom(note_2L11_tom,L)
1723 note_11_tom(note_2M11_tom,M)
1727 $1(Elf64_Off *dst, Byte *src, size_t cnt)
1729 Elf64_Off *end = dst;
1732 src += cnt * O_sizeof;
1733 while (dst-- > end) {
1735 *dst = tomo(src, O_$2);
1739 off_tom(off_2L_tom,L)
1740 off_tom(off_2M_tom,M)
1743 define(phdr_11_tom, `
1745 $1(Elf64_Phdr *dst, Byte *src, size_t cnt)
1747 Elf64_Phdr *end = dst;
1750 src += cnt * P1_sizeof;
1751 while (dst-- > end) {
1753 dst->p_align = tomx(src, P1_align_$2);
1754 dst->p_memsz = tomx(src, P1_memsz_$2);
1755 dst->p_filesz = tomx(src, P1_filesz_$2);
1756 dst->p_paddr = toma(src, P1_paddr_$2);
1757 dst->p_vaddr = toma(src, P1_vaddr_$2);
1758 dst->p_offset = tomo(src, P1_offset_$2);
1759 dst->p_flags = tomw(src, P1_flags_$2);
1760 dst->p_type = tomw(src, P1_type_$2);
1764 phdr_11_tom(phdr_2L11_tom,L)
1765 phdr_11_tom(phdr_2M11_tom,M)
1768 define(rel_11_tom, `
1770 $1(Elf64_Rel *dst, Byte *src, size_t cnt)
1772 Elf64_Rel *end = dst;
1775 src += cnt * R1_sizeof;
1776 while (dst-- > end) {
1778 dst->r_info = tomx(src, R1_info_$2);
1779 dst->r_offset = toma(src, R1_offset_$2);
1783 rel_11_tom(rel_2L11_tom,L)
1784 rel_11_tom(rel_2M11_tom,M)
1787 define(rela_11_tom, `
1789 $1(Elf64_Rela *dst, Byte *src, size_t cnt)
1791 Elf64_Rela *end = dst;
1794 src += cnt * RA1_sizeof;
1795 while (dst-- > end) {
1797 /*CONSTANTCONDITION*/
1798 if (~(Elf64_Xword)0 == -(Elf64_Sxword)1 && /* 2s comp */
1799 ~(~(Elf64_Xword)0 >> 1) == HI64) {
1800 dst->r_addend = tomx(src, RA1_addend_$2);
1807 if ((u.w = tomx(src, RA1_addend_$2)) & HI64) {
1809 u.w |= ~(Elf64_Xword)LO63;
1813 dst->r_addend = u.sw;
1815 dst->r_info = tomx(src, RA1_info_$2);
1816 dst->r_offset = toma(src, RA1_offset_$2);
1820 rela_11_tom(rela_2L11_tom,L)
1821 rela_11_tom(rela_2M11_tom,M)
1824 define(shdr_11_tom, `
1826 $1(Elf64_Shdr *dst, Byte *src, size_t cnt)
1828 Elf64_Shdr *end = dst;
1831 src += cnt * SH1_sizeof;
1832 while (dst-- > end) {
1834 dst->sh_entsize = tomx(src, SH1_entsize_$2);
1835 dst->sh_addralign = tomx(src, SH1_addralign_$2);
1836 dst->sh_info = tomw(src, SH1_info_$2);
1837 dst->sh_link = tomw(src, SH1_link_$2);
1838 dst->sh_size = tomx(src, SH1_size_$2);
1839 dst->sh_offset = tomo(src, SH1_offset_$2);
1840 dst->sh_addr = toma(src, SH1_addr_$2);
1841 dst->sh_flags = tomx(src, SH1_flags_$2);
1842 dst->sh_type = tomw(src, SH1_type_$2);
1843 dst->sh_name = tomw(src, SH1_name_$2);
1847 shdr_11_tom(shdr_2L11_tom,L)
1848 shdr_11_tom(shdr_2M11_tom,M)
1853 $1(Elf64_Sword *dst, Byte *src, size_t cnt)
1855 Elf64_Sword *end = dst;
1858 src += cnt * W_sizeof;
1859 while (dst-- > end) {
1861 /*CONSTANTCONDITION*/
1862 if (~(Elf64_Word)0 == -(Elf64_Sword)1 &&
1863 ~(~(Elf64_Word)0 >> 1) == HI32) { /* 2s comp */
1864 *dst = tomw(src, W_$2);
1871 if ((u.w = tomw(src, W_$2)) & HI32) {
1872 u.w |= ~(Elf64_Word)LO31;
1881 sword_tom(sword_2L_tom,L)
1882 sword_tom(sword_2M_tom,M)
1885 define(cap_11_tom, `
1887 $1(Elf64_Cap *dst, unsigned char *src, size_t cnt)
1889 Elf64_Cap *end = dst + cnt;
1892 dst->c_tag = tomx(src, C1_tag_$2);
1893 dst->c_un.c_val = tomx(src, C1_val_$2);
1895 } while (++dst < end);
1898 cap_11_tom(cap_2L11_tom,L)
1899 cap_11_tom(cap_2M11_tom,M)
1902 define(syminfo_11_tom, `
1904 $1(Elf64_Syminfo *dst, unsigned char *src, size_t cnt)
1906 Elf64_Syminfo *end = dst;
1909 src += cnt * SI1_sizeof;
1913 dst->si_boundto = tomh(src, SI1_boundto_$2);
1914 dst->si_flags = tomh(src, SI1_flags_$2);
1918 syminfo_11_tom(syminfo_2L11_tom,L)
1919 syminfo_11_tom(syminfo_2M11_tom,M)
1922 define(sym_11_tom, `
1924 $1(Elf64_Sym *dst, Byte *src, size_t cnt)
1926 Elf64_Sym *end = dst;
1929 src += cnt * ST1_sizeof;
1930 while (dst-- > end) {
1932 dst->st_size = tomx(src, ST1_size_$2);
1933 dst->st_value = toma(src, ST1_value_$2);
1934 dst->st_shndx = tomh(src, ST1_shndx_$2);
1935 dst->st_other = tomb(src, ST1_other_$2);
1936 dst->st_info = tomb(src, ST1_info_$2);
1937 dst->st_name = tomw(src, ST1_name_$2);
1941 sym_11_tom(sym_2L11_tom,L)
1942 sym_11_tom(sym_2M11_tom,M)
1947 $1(Elf64_Word *dst, Byte *src, size_t cnt)
1949 Elf64_Word *end = dst;
1952 src += cnt * W_sizeof;
1953 while (dst-- > end) {
1955 *dst = tomw(src, W_$2);
1959 word_tom(word_2L_tom,L)
1960 word_tom(word_2M_tom,M)
1963 define(verdef_11_tom, `
1965 $1(Elf64_Verdef *dst, Byte *src, size_t cnt)
1968 Elf64_Verdef *end = (Elf64_Verdef *)((Byte *)dst + cnt);
1971 Elf64_Verdaux *vaux;
1975 dst->vd_version = tomh(src, VD1_version_$2);
1976 dst->vd_flags = tomh(src, VD1_flags_$2);
1977 dst->vd_ndx = tomh(src, VD1_ndx_$2);
1978 dst->vd_cnt = tomh(src, VD1_cnt_$2);
1979 dst->vd_hash = tomw(src, VD1_hash_$2);
1980 dst->vd_aux = tomw(src, VD1_aux_$2);
1981 dst->vd_next = tomw(src, VD1_next_$2);
1983 src_vaux = src + dst->vd_aux;
1985 vaux = (Elf64_Verdaux *)((Byte *)dst + dst->vd_aux);
1986 for (i = 0; i < dst->vd_cnt; i++) {
1987 vaux->vda_name = tomw(src_vaux, VDA1_name_$2);
1988 vaux->vda_next = tomw(src_vaux, VDA1_next_$2);
1989 src_vaux += vaux->vda_next;
1991 vaux = (Elf64_Verdaux *)((Byte *)vaux +
1994 src += dst->vd_next;
1996 dst = (Elf64_Verdef *)(dst->vd_next ?
1997 (Byte *)dst + dst->vd_next : (Byte *)end);
2001 verdef_11_tom(verdef_2L11_tom,L)
2002 verdef_11_tom(verdef_2M11_tom,M)
2005 define(verneed_11_tom, `
2007 $1(Elf64_Verneed *dst, Byte *src, size_t cnt)
2010 Elf64_Verneed *end = (Elf64_Verneed *)((char *)dst + cnt);
2013 Elf64_Vernaux * vaux;
2017 dst->vn_version = tomh(src, VN1_version_$2);
2018 dst->vn_cnt = tomh(src, VN1_cnt_$2);
2019 dst->vn_file = tomw(src, VN1_file_$2);
2020 dst->vn_aux = tomw(src, VN1_aux_$2);
2021 dst->vn_next = tomw(src, VN1_next_$2);
2023 src_vaux = src + dst->vn_aux;
2025 vaux = (Elf64_Vernaux *)((Byte *)dst + dst->vn_aux);
2026 for (i = 0; i < dst->vn_cnt; i++) {
2027 vaux->vna_hash = tomw(src_vaux, VNA1_hash_$2);
2028 vaux->vna_flags = tomh(src_vaux, VNA1_flags_$2);
2029 vaux->vna_other = tomh(src_vaux, VNA1_other_$2);
2030 vaux->vna_name = tomw(src_vaux, VNA1_name_$2);
2031 vaux->vna_next = tomw(src_vaux, VNA1_next_$2);
2032 src_vaux += vaux->vna_next;
2034 vaux = (Elf64_Vernaux *)((Byte *)vaux +
2037 src += dst->vn_next;
2039 dst = (Elf64_Verneed *)(dst->vn_next ?
2040 (Byte *)dst + dst->vn_next : (Byte *)end);
2044 verneed_11_tom(verneed_2L11_tom,L)
2045 verneed_11_tom(verneed_2M11_tom,M)
2048 define(sxword_tom, `
2050 $1(Elf64_Sxword *dst, Byte *src, size_t cnt)
2052 Elf64_Sxword *end = dst;
2055 src += cnt * X_sizeof;
2056 while (dst-- > end) {
2058 /*CONSTANTCONDITION*/
2059 if (~(Elf64_Xword)0 == -(Elf64_Sxword)1 &&
2060 ~(~(Elf64_Xword)0 >> 1) == HI64) { /* 2s comp */
2061 *dst = tomx(src, X_$2);
2062 } else { /* other */
2068 if ((u.w = tomx(src, X_$2)) & HI64) {
2070 u.w |= ~(Elf64_Xword)LO63;
2079 sxword_tom(sxword_2L_tom,L)
2080 sxword_tom(sxword_2M_tom,M)
2085 $1(Elf64_Xword *dst, Byte *src, size_t cnt)
2087 Elf64_Xword *end = dst;
2090 src += cnt * X_sizeof;
2091 while (dst-- > end) {
2093 *dst = tomx(src, X_$2);
2097 xword_tom(xword_2L_tom,L)
2098 xword_tom(xword_2M_tom,M)