xref: /titanic_41/usr/src/cmd/sgs/libelf/common/xlate.m4 (revision 1d4b38e0077763e7c9b20768eacb841957e787bc)
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22/*
23 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24 * Use is subject to license terms.
25 */
26
27#pragma ident	"%Z%%M%	%I%	%E% SMI"
28
29#pragma weak elf32_fsize = _elf32_fsize
30#pragma weak elf_version = _elf_version
31#pragma weak elf32_xlatetof = _elf32_xlatetof
32#pragma weak elf32_xlatetom = _elf32_xlatetom
33
34#include "syn.h"
35#include <memory.h>
36#include <libelf.h>
37#include <link.h>
38#include <sys/elf_SPARC.h>
39#include <sys/elf_amd64.h>
40#include <decl.h>
41#include <msg.h>
42#include <sgs.h>
43
44
45/*
46 * fmsize:  Array used to determine what size the the structures
47 *	    are (for memory image & file image).
48 *
49 * x32:  Translation routines - to file & to memory.
50 *
51 * What must be done when adding a new type for conversion:
52 *
53 * The first question is whether you need a new ELF_T_* type
54 * to be created.  If you've introduced a new structure - then
55 * it will need to be described - this is done by:
56 *
57 * o adding a new type ELF_T_* to usr/src/head/libelf.h
58 * o Create a new macro to define the bytes contained in the structure. Take a
59 *   look at the 'Syminfo_1' macro defined below.  The declarations describe
60 *   the structure based off of the field size of each element of the structure.
61 * o Add a entry to the fmsize table for the new ELF_T_* type.
62 * o Create a <newtype>_11_tof macro.  Take a look at 'syminfo_11_tof'.
63 * o Create a <newtype>_11_tom macro.  Take a look at 'syminfo_11_tom'.
64 * o The <newtype>_11_tof & <newtype>_11_tom results in conversion routines
65 *   <newtype>_2L11_tof, <newtype>_2L11_tom, <newtype>_2M11_tof,
66 *   <newtype>_2M11_tom being created in xlate.c.  These routines
67 *   need to be added to the 'x32[]' array.
68 * o Add entries to getdata.c::align32[] and getdata.c::align64[].  These
69 *   tables define what the alignment requirements for a data type are.
70 *
71 * In order to tie a section header type (SHT_*) to a data
72 * structure you need to update elf32_mtype() so that it can
73 * make the association.  If you are introducing a new section built
74 * on a basic datatype (SHT_INIT_ARRAY) then this is all the updating
75 * that needs to be done.
76 *
77 *
78 * ELF translation routines
79 *	These routines make a subtle implicit assumption.
80 *	The file representations of all structures are "packed,"
81 *	meaning no implicit padding bytes occur.  This might not
82 *	be the case for the memory representations.  Consequently,
83 *	the memory representations ALWAYS contain at least as many
84 *	bytes as the file representations.  Otherwise, the memory
85 *	structures would lose information, meaning they're not
86 *	implemented properly.
87 *
88 *	The words above apply to structures with the same members.
89 *	If a future version changes the number of members, the
90 *	relative structure sizes for different version must be
91 *	tested with the compiler.
92 */
93
94#define	HI32	0x80000000UL
95#define	LO31	0x7fffffffUL
96
97/*
98 *	These macros create indexes for accessing the bytes of
99 *	words and halfwords for ELFCLASS32 data representations
100 *	(currently ELFDATA2LSB and ELFDATA2MSB).  In all cases,
101 *
102 *	w = (((((X_3 << 8) + X_2) << 8) + X_1) << 8) + X_0
103 *	h = (X_1 << 8) + X_0
104 *
105 *	These assume the file representations for Addr, Off,
106 *	Sword, and Word use 4 bytes, but the memory def's for
107 *	the types may differ.
108 *
109 *	Naming convention:
110 *		..._L	ELFDATA2LSB
111 *		..._M	ELFDATA2MSB
112 *
113 *	enuma_*(n)	define enum names for addr n
114 *	enumb_*(n)	define enum names for byte n
115 *	enumh_*(n)	define enum names for half n
116 *	enumo_*(n)	define enum names for off n
117 *	enumw_*(n)	define enum names for word n
118 *	enuml_*(n)	define enum names for Lword n
119 *	tofa(d,s,n)	xlate addr n from mem s to file d
120 *	tofb(d,s,n)	xlate byte n from mem s to file d
121 *	tofh(d,s,n)	xlate half n from mem s to file d
122 *	tofo(d,s,n)	xlate off n from mem s to file d
123 *	tofw(d,s,n)	xlate word n from mem s to file d
124 *	tofl(d,s,n)	xlate Lword n from mem s to file d
125 *	toma(s,n)	xlate addr n from file s to expression value
126 *	tomb(s,n)	xlate byte n from file s to expression value
127 *	tomh(s,n)	xlate half n from file s to expression value
128 *	tomo(s,n)	xlate off n from file s to expression value
129 *	tomw(s,n)	xlate word n from file s to expression value
130 *	toml(s,n)	xlate Lword n from file s to expression value
131 *
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.
136 *
137 *	tom*() macros compute an expression value from the source
138 *	without touching the destination; so they're safe.
139 */
140
141define(enuma_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
142define(enuma_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
143define(enumb_L, `$1_L')dnl
144define(enumb_M, `$1_M')dnl
145define(enumh_L, `$1_L0, $1_L1')dnl
146define(enumh_M, `$1_M1, $1_M0')dnl
147define(enumo_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
148define(enumo_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
149define(enumw_L, `$1_L0, $1_L1, $1_L2, $1_L3')dnl
150define(enumw_M, `$1_M3, $1_M2, $1_M1, $1_M0')dnl
151define(enuml_L, `$1_L0, $1_L1, $1_L2, $1_L3, $1_L4, $1_L5, $1_L6, $1_L7')dnl
152define(enuml_M, `$1_M7, $1_M6, $1_M5, $1_M4, $1_M3, $1_M2, $1_M1, $1_M0')dnl
153
154define(tofa, `{ register Elf32_Addr _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
159define(tofb, `($1)[$3] = (unsigned char)($2)')dnl
160define(tofh, `{ register Elf32_Half _t_ = $2;
161		($1)[$3`'0] = (unsigned char)_t_,
162		($1)[$3`'1] = (unsigned char)(_t_>>8); }')dnl
163define(tofo, `{ register Elf32_Off _t_ = $2;
164		($1)[$3`'0] = (unsigned char)_t_,
165		($1)[$3`'1] = (unsigned char)(_t_>>8),
166		($1)[$3`'2] = (unsigned char)(_t_>>16),
167		($1)[$3`'3] = (unsigned char)(_t_>>24); }')dnl
168define(tofw, `{ register Elf32_Word _t_ = $2;
169		($1)[$3`'0] = (unsigned char)_t_,
170		($1)[$3`'1] = (unsigned char)(_t_>>8),
171		($1)[$3`'2] = (unsigned char)(_t_>>16),
172		($1)[$3`'3] = (unsigned char)(_t_>>24); }')dnl
173define(tofl, `{ Elf32_Lword _t_ = $2;
174		($1)[$3`'0] = (Byte)_t_,
175		($1)[$3`'1] = (Byte)(_t_>>8),
176		($1)[$3`'2] = (Byte)(_t_>>16),
177		($1)[$3`'3] = (Byte)(_t_>>24),
178		($1)[$3`'4] = (Byte)(_t_>>32),
179		($1)[$3`'5] = (Byte)(_t_>>40),
180		($1)[$3`'6] = (Byte)(_t_>>48),
181		($1)[$3`'7] = (Byte)(_t_>>56); }')dnl
182
183define(toma, `(((((((Elf32_Addr)($1)[$2`'3]<<8)
184		+($1)[$2`'2])<<8)
185		+($1)[$2`'1])<<8)
186		+($1)[$2`'0])')dnl
187define(tomb, `((unsigned char)($1)[$2])')dnl
188define(tomh, `(((Elf32_Half)($1)[$2`'1]<<8)+($1)[$2`'0])')dnl
189define(tomo, `(((((((Elf32_Off)($1)[$2`'3]<<8)
190		+($1)[$2`'2])<<8)
191		+($1)[$2`'1])<<8)
192		+($1)[$2`'0])')dnl
193define(tomw, `(((((((Elf32_Word)($1)[$2`'3]<<8)
194		+($1)[$2`'2])<<8)
195		+($1)[$2`'1])<<8)
196		+($1)[$2`'0])')dnl
197define(toml, `(((((((((((Elf32_Lword)($1)[$2`'7]<<8)
198		+($1)[$2`'6]<<8)
199		+($1)[$2`'5]<<8)
200		+($1)[$2`'4]<<8)
201		+($1)[$2`'3]<<8)
202		+($1)[$2`'2])<<8)
203		+($1)[$2`'1])<<8)
204		+($1)[$2`'0])')dnl
205
206
207/*
208 * ELF data object indexes
209 *	The enums are broken apart to get around deficiencies
210 *	in some compilers.
211 */
212
213define(Addr, `
214enum
215{
216	enuma_$1(A)`'ifelse(`$2', `', `', `,
217	A_sizeof')
218};')
219
220Addr(L)
221Addr(M,1)
222
223
224define(Half, `
225enum
226{
227	enumh_$1(H)`'ifelse(`$2', `', `', `,
228	H_sizeof')
229};')
230
231Half(L)
232Half(M,1)
233
234define(Lword, `
235enum
236{
237	enuml_$1(L)`'ifelse(`$2', `', `', `,
238	L_sizeof')
239};')
240
241Lword(L)
242Lword(M,1)
243
244
245define(Move_1, `
246enum
247{
248	enuml_$1(M1_value),
249	enumw_$1(M1_info),
250	enumw_$1(M1_poffset),
251	enumh_$1(M1_repeat),
252	enumh_$1(M1_stride)`'ifelse(`$2', `', `', `,
253	M1_sizeof')
254};')
255
256Move_1(L)
257Move_1(M,1)
258
259
260define(MoveP_1, `
261enum
262{
263	enuml_$1(MP1_value),
264	enumw_$1(MP1_info),
265	enumw_$1(MP1_poffset),
266	enumh_$1(MP1_repeat),
267	enumh_$1(MP1_stride),
268	enumw_$1(MP1_padding)`'ifelse(`$2', `', `', `,
269	MP1_sizeof')
270};')
271
272MoveP_1(L)
273MoveP_1(M,1)
274
275
276define(Off, `
277enum
278{
279	enumo_$1(O)`'ifelse(`$2', `', `', `,
280	O_sizeof')
281};')
282
283Off(L)
284Off(M,1)
285
286
287define(Word, `
288enum
289{
290	enumw_$1(W)`'ifelse(`$2', `', `', `,
291	W_sizeof')
292};')
293
294Word(L)
295Word(M,1)
296
297
298define(Dyn_1, `
299enum
300{
301	enumw_$1(D1_tag),
302	enumw_$1(D1_val)`'ifelse(`$2', `', `', `,
303	D1_sizeof')
304};')
305
306Dyn_1(L)
307Dyn_1(M,1)
308
309
310#define	E1_Nident	16
311
312define(Ehdr_1, `
313enum
314{
315	ifelse(`$2', `', `E1_ident, ')E1_ident_$1_Z = E1_Nident - 1,
316	enumh_$1(E1_type),
317	enumh_$1(E1_machine),
318	enumw_$1(E1_version),
319	enuma_$1(E1_entry),
320	enumo_$1(E1_phoff),
321	enumo_$1(E1_shoff),
322	enumw_$1(E1_flags),
323	enumh_$1(E1_ehsize),
324	enumh_$1(E1_phentsize),
325	enumh_$1(E1_phnum),
326	enumh_$1(E1_shentsize),
327	enumh_$1(E1_shnum),
328	enumh_$1(E1_shstrndx)`'ifelse(`$2', `', `', `,
329	E1_sizeof')
330};')
331
332Ehdr_1(L)
333Ehdr_1(M,1)
334
335define(Nhdr_1, `
336enum
337{
338	enumw_$1(N1_namesz),
339	enumw_$1(N1_descsz),
340	enumw_$1(N1_type)`'ifelse(`$2', `', `', `,
341	N1_sizeof')
342};')
343
344Nhdr_1(L)
345Nhdr_1(M,1)
346
347define(Phdr_1, `
348enum
349{
350	enumw_$1(P1_type),
351	enumo_$1(P1_offset),
352	enuma_$1(P1_vaddr),
353	enuma_$1(P1_paddr),
354	enumw_$1(P1_filesz),
355	enumw_$1(P1_memsz),
356	enumw_$1(P1_flags),
357	enumw_$1(P1_align)`'ifelse(`$2', `', `', `,
358	P1_sizeof')
359};')
360
361Phdr_1(L)
362Phdr_1(M,1)
363
364
365define(Rel_1, `
366enum
367{
368	enuma_$1(R1_offset),
369	enumw_$1(R1_info)`'ifelse(`$2', `', `', `,
370	R1_sizeof')
371};')
372
373Rel_1(L)
374Rel_1(M,1)
375
376
377define(Rela_1, `
378enum
379{
380	enuma_$1(RA1_offset),
381	enumw_$1(RA1_info),
382	enumw_$1(RA1_addend)`'ifelse(`$2', `', `', `,
383	RA1_sizeof')
384};')
385
386Rela_1(L)
387Rela_1(M,1)
388
389
390define(Shdr_1, `
391enum
392{
393	enumw_$1(SH1_name),
394	enumw_$1(SH1_type),
395	enumw_$1(SH1_flags),
396	enuma_$1(SH1_addr),
397	enumo_$1(SH1_offset),
398	enumw_$1(SH1_size),
399	enumw_$1(SH1_link),
400	enumw_$1(SH1_info),
401	enumw_$1(SH1_addralign),
402	enumw_$1(SH1_entsize)`'ifelse(`$2', `', `', `,
403	SH1_sizeof')
404};')
405
406Shdr_1(L)
407Shdr_1(M,1)
408
409
410define(Sym_1, `
411enum
412{
413	enumw_$1(ST1_name),
414	enuma_$1(ST1_value),
415	enumw_$1(ST1_size),
416	enumb_$1(ST1_info),
417	enumb_$1(ST1_other),
418	enumh_$1(ST1_shndx)`'ifelse(`$2', `', `', `,
419	ST1_sizeof')
420};')
421
422Sym_1(L)
423Sym_1(M,1)
424
425
426define(Syminfo_1, `
427enum
428{
429	enumh_$1(SI1_boundto),
430	enumh_$1(SI1_flags)`'ifelse(`$2', `', `', `,
431	SI1_sizeof')
432};')
433
434Syminfo_1(L)
435Syminfo_1(M,1)
436
437
438define(Cap_1, `
439enum
440{
441	enumw_$1(C1_tag),
442	enumw_$1(C1_val)`'ifelse(`$2', `', `', `,
443	C1_sizeof')
444};')
445
446Cap_1(L)
447Cap_1(M,1)
448
449
450define(Verdef_1, `
451enum
452{
453	enumh_$1(VD1_version),
454	enumh_$1(VD1_flags),
455	enumh_$1(VD1_ndx),
456	enumh_$1(VD1_cnt),
457	enumw_$1(VD1_hash),
458	enumw_$1(VD1_aux),
459	enumw_$1(VD1_next)`'ifelse(`$2', `', `', `,
460	VD1_sizeof')
461};')
462
463Verdef_1(L)
464Verdef_1(M,1)
465
466
467define(Verdaux_1, `
468enum
469{
470	enuma_$1(VDA1_name),
471	enumw_$1(VDA1_next)`'ifelse(`$2', `', `', `,
472	VDA1_sizeof')
473};')
474
475Verdaux_1(L)
476Verdaux_1(M,1)
477
478
479define(Verneed_1, `
480enum
481{
482	enumh_$1(VN1_version),
483	enumh_$1(VN1_cnt),
484	enuma_$1(VN1_file),
485	enumw_$1(VN1_aux),
486	enumw_$1(VN1_next)`'ifelse(`$2', `', `', `,
487	VN1_sizeof')
488};')
489
490Verneed_1(L)
491Verneed_1(M,1)
492
493
494define(Vernaux_1, `
495enum
496{
497	enumw_$1(VNA1_hash),
498	enumh_$1(VNA1_flags),
499	enumh_$1(VNA1_other),
500	enuma_$1(VNA1_name),
501	enumw_$1(VNA1_next)`'ifelse(`$2', `', `', `,
502	VNA1_sizeof')
503};')
504
505Vernaux_1(L)
506Vernaux_1(M,1)
507
508
509/*
510 *	Translation function declarations.
511 *
512 *		<object>_<data><dver><sver>_tof
513 *		<object>_<data><dver><sver>_tom
514 *	where
515 *		<data>	2L	ELFDATA2LSB
516 *			2M	ELFDATA2MSB
517 */
518
519static void	addr_2L_tof(), addr_2L_tom(),
520		addr_2M_tof(), addr_2M_tom(),
521		byte_to(),
522		dyn_2L11_tof(), dyn_2L11_tom(),
523		dyn_2M11_tof(), dyn_2M11_tom(),
524		ehdr_2L11_tof(), ehdr_2L11_tom(),
525		ehdr_2M11_tof(), ehdr_2M11_tom(),
526		half_2L_tof(), half_2L_tom(),
527		half_2M_tof(), half_2M_tom(),
528		move_2L11_tof(), move_2L11_tom(),
529		move_2M11_tof(), move_2M11_tom(),
530		movep_2L11_tof(), movep_2L11_tom(),
531		movep_2M11_tof(), movep_2M11_tom(),
532		off_2L_tof(), off_2L_tom(),
533		off_2M_tof(), off_2M_tom(),
534		note_2L11_tof(), note_2L11_tom(),
535		note_2M11_tof(), note_2M11_tom(),
536		phdr_2L11_tof(), phdr_2L11_tom(),
537		phdr_2M11_tof(), phdr_2M11_tom(),
538		rel_2L11_tof(), rel_2L11_tom(),
539		rel_2M11_tof(), rel_2M11_tom(),
540		rela_2L11_tof(), rela_2L11_tom(),
541		rela_2M11_tof(), rela_2M11_tom(),
542		shdr_2L11_tof(), shdr_2L11_tom(),
543		shdr_2M11_tof(), shdr_2M11_tom(),
544		sword_2L_tof(), sword_2L_tom(),
545		sword_2M_tof(), sword_2M_tom(),
546		sym_2L11_tof(), sym_2L11_tom(),
547		sym_2M11_tof(), sym_2M11_tom(),
548		syminfo_2L11_tof(), syminfo_2L11_tom(),
549		syminfo_2M11_tof(), syminfo_2M11_tom(),
550		word_2L_tof(), word_2L_tom(),
551		word_2M_tof(), word_2M_tom(),
552		verdef_2L11_tof(), verdef_2L11_tom(),
553		verdef_2M11_tof(), verdef_2M11_tom(),
554		verneed_2L11_tof(), verneed_2L11_tom(),
555		verneed_2M11_tof(), verneed_2M11_tom(),
556		cap_2L11_tof(), cap_2L11_tom(),
557		cap_2M11_tof(), cap_2M11_tom();
558
559
560/*	x32 [dst_version - 1] [src_version - 1] [encode - 1] [type]
561 */
562
563static struct {
564	void	(*x_tof)(),
565		(*x_tom)();
566} x32 [EV_CURRENT] [EV_CURRENT] [ELFDATANUM - 1] [ELF_T_NUM] =
567{
568	{
569		{
570			{			/* [1-1][1-1][2LSB-1][.] */
571/* BYTE */			{ byte_to, byte_to },
572/* ADDR */			{ addr_2L_tof, addr_2L_tom },
573/* DYN */			{ dyn_2L11_tof, dyn_2L11_tom },
574/* EHDR */			{ ehdr_2L11_tof, ehdr_2L11_tom },
575/* HALF */			{ half_2L_tof, half_2L_tom },
576/* OFF */			{ off_2L_tof, off_2L_tom },
577/* PHDR */			{ phdr_2L11_tof, phdr_2L11_tom },
578/* RELA */			{ rela_2L11_tof, rela_2L11_tom },
579/* REL */			{ rel_2L11_tof, rel_2L11_tom },
580/* SHDR */			{ shdr_2L11_tof, shdr_2L11_tom },
581/* SWORD */			{ sword_2L_tof, sword_2L_tom },
582/* SYM */			{ sym_2L11_tof, sym_2L11_tom },
583/* WORD */			{ word_2L_tof, word_2L_tom },
584/* VERDEF */			{ verdef_2L11_tof, verdef_2L11_tom},
585/* VERNEED */			{ verneed_2L11_tof, verneed_2L11_tom},
586/* SXWORD */			{ 0, 0 },	/* illegal 32-bit op */
587/* XWORD */			{ 0, 0 },	/* illegal 32-bit op */
588/* SYMINFO */			{ syminfo_2L11_tof, syminfo_2L11_tom },
589/* NOTE */			{ note_2L11_tof, note_2L11_tom },
590/* MOVE */			{ move_2L11_tof, move_2L11_tom },
591/* MOVEP */			{ movep_2L11_tof, movep_2L11_tom },
592/* CAP */			{ cap_2L11_tof, cap_2L11_tom },
593			},
594			{			/* [1-1][1-1][2MSB-1][.] */
595/* BYTE */			{ byte_to, byte_to },
596/* ADDR */			{ addr_2M_tof, addr_2M_tom },
597/* DYN */			{ dyn_2M11_tof, dyn_2M11_tom },
598/* EHDR */			{ ehdr_2M11_tof, ehdr_2M11_tom },
599/* HALF */			{ half_2M_tof, half_2M_tom },
600/* OFF */			{ off_2M_tof, off_2M_tom },
601/* PHDR */			{ phdr_2M11_tof, phdr_2M11_tom },
602/* RELA */			{ rela_2M11_tof, rela_2M11_tom },
603/* REL */			{ rel_2M11_tof, rel_2M11_tom },
604/* SHDR */			{ shdr_2M11_tof, shdr_2M11_tom },
605/* SWORD */			{ sword_2M_tof, sword_2M_tom },
606/* SYM */			{ sym_2M11_tof, sym_2M11_tom },
607/* WORD */			{ word_2M_tof, word_2M_tom },
608/* VERDEF */			{ verdef_2M11_tof, verdef_2M11_tom},
609/* VERNEED */			{ verneed_2M11_tof, verneed_2M11_tom},
610/* SXWORD */			{ 0, 0 },	/* illegal 32-bit op */
611/* XWORD */			{ 0, 0 },	/* illegal 32-bit op */
612/* SYMINFO */			{ syminfo_2M11_tof, syminfo_2M11_tom },
613/* NOTE */			{ note_2M11_tof, note_2M11_tom },
614/* MOVE */			{ move_2M11_tof, move_2M11_tom },
615/* MOVEP */			{ movep_2M11_tof, movep_2M11_tom },
616/* CAP */			{ cap_2M11_tof, cap_2M11_tom },
617			},
618		},
619	},
620};
621
622
623/*
624 *	size [version - 1] [type]
625 */
626
627static const struct {
628	size_t	s_filesz,
629		s_memsz;
630} fmsize [EV_CURRENT] [ELF_T_NUM] =
631{
632	{					/* [1-1][.] */
633/* BYTE */	{ 1, 1 },
634/* ADDR */	{ A_sizeof, sizeof (Elf32_Addr) },
635/* DYN */	{ D1_sizeof, sizeof (Elf32_Dyn) },
636/* EHDR */	{ E1_sizeof, sizeof (Elf32_Ehdr) },
637/* HALF */	{ H_sizeof, sizeof (Elf32_Half) },
638/* OFF */	{ O_sizeof, sizeof (Elf32_Off) },
639/* PHDR */	{ P1_sizeof, sizeof (Elf32_Phdr) },
640/* RELA */	{ RA1_sizeof, sizeof (Elf32_Rela) },
641/* REL */	{ R1_sizeof, sizeof (Elf32_Rel) },
642/* SHDR */	{ SH1_sizeof, sizeof (Elf32_Shdr) },
643/* SWORD */	{ W_sizeof, sizeof (Elf32_Sword) },
644/* SYM */	{ ST1_sizeof, sizeof (Elf32_Sym) },
645/* WORD */	{ W_sizeof, sizeof (Elf32_Word) },
646/* VERDEF */	{ 1, 1},	/* because bot VERDEF & VERNEED have varying */
647/* VERNEED */	{ 1, 1},	/* sized structures we set their sizes */
648				/* to 1 byte */
649/* SXWORD */			{ 0, 0 },	/* illegal 32-bit op */
650/* XWORD */			{ 0, 0 },	/* illegal 32-bit op */
651/* SYMINFO */	{ SI1_sizeof, sizeof (Elf32_Syminfo) },
652/* NOTE */	{ 1, 1},	/* NOTE has varying sized data we can't */
653				/*  use the usual table magic. */
654/* MOVE */	{ M1_sizeof, sizeof (Elf32_Move) },
655/* MOVEP */	{ MP1_sizeof, sizeof (Elf32_Move) },
656/* CAP */	{ C1_sizeof, sizeof (Elf32_Cap) },
657	},
658};
659
660
661/*
662 *	memory type [version - 1] [section type]
663 */
664
665static const Elf_Type	mtype[EV_CURRENT][SHT_NUM] =
666{
667	{			/* [1-1][.] */
668/* NULL */		ELF_T_BYTE,
669/* PROGBITS */		ELF_T_BYTE,
670/* SYMTAB */		ELF_T_SYM,
671/* STRTAB */		ELF_T_BYTE,
672/* RELA */		ELF_T_RELA,
673/* HASH */		ELF_T_WORD,
674/* DYNAMIC */		ELF_T_DYN,
675/* NOTE */		ELF_T_NOTE,
676/* NOBITS */		ELF_T_BYTE,
677/* REL */		ELF_T_REL,
678/* SHLIB */		ELF_T_BYTE,
679/* DYNSYM */		ELF_T_SYM,
680/* UNKNOWN12 */		ELF_T_BYTE,
681/* UNKNOWN13 */		ELF_T_BYTE,
682/* INIT_ARRAY */	ELF_T_ADDR,
683/* FINI_ARRAY */	ELF_T_ADDR,
684/* PREINIT_ARRAY */	ELF_T_ADDR,
685/* GROUP */		ELF_T_WORD,
686/* SYMTAB_SHNDX */	ELF_T_WORD
687	},
688};
689
690
691size_t
692_elf32_entsz(Elf32_Word shtype, unsigned ver)
693{
694	Elf_Type	ttype;
695
696	if (shtype >= sizeof (mtype[0]) / sizeof (mtype[0][0]) ||
697	    (ttype = mtype[ver - 1][shtype]) == ELF_T_BYTE)
698		return (0);
699	return (fmsize[ver - 1][ttype].s_filesz);
700}
701
702
703size_t
704elf32_fsize(Elf_Type type, size_t count, unsigned ver)
705{
706	if (--ver >= EV_CURRENT) {
707		_elf_seterr(EREQ_VER, 0);
708		return (0);
709	}
710	if ((unsigned)type >= ELF_T_NUM) {
711		_elf_seterr(EREQ_TYPE, 0);
712		return (0);
713	}
714	return (fmsize[ver][type].s_filesz * count);
715}
716
717
718size_t
719_elf32_msize(Elf_Type type, unsigned ver)
720{
721	return (fmsize[ver - 1][type].s_memsz);
722}
723
724
725Elf_Type
726_elf32_mtype(Elf * elf, Elf32_Word shtype, unsigned ver)
727{
728	Elf32_Ehdr *	ehdr = (Elf32_Ehdr *)elf->ed_ehdr;
729
730	if (shtype < SHT_NUM)
731		return (mtype[ver - 1][shtype]);
732
733	switch (shtype) {
734	case SHT_SUNW_dof:
735		return (ELF_T_BYTE);
736	case SHT_SUNW_cap:
737		return (ELF_T_CAP);
738	case SHT_SUNW_SIGNATURE:
739		return (ELF_T_BYTE);
740	case SHT_SUNW_ANNOTATE:
741		return (ELF_T_BYTE);
742	case SHT_SUNW_DEBUGSTR:
743		return (ELF_T_BYTE);
744	case SHT_SUNW_DEBUG:
745		return (ELF_T_BYTE);
746	case SHT_SUNW_move:
747		/*
748		 * 32bit sparc binaries have a padded
749		 * MOVE structure.  So - return the
750		 * appropriate type.
751		 */
752		if ((ehdr->e_machine == EM_SPARC) ||
753		    (ehdr->e_machine == EM_SPARC32PLUS)) {
754			return (ELF_T_MOVEP);
755		}
756
757		return (ELF_T_MOVE);
758	case SHT_SUNW_COMDAT:
759		return (ELF_T_BYTE);
760	case SHT_SUNW_syminfo:
761		return (ELF_T_SYMINFO);
762	case SHT_SUNW_verdef:
763		return (ELF_T_VDEF);
764	case SHT_SUNW_verneed:
765		return (ELF_T_VNEED);
766	case SHT_SUNW_versym:
767		return (ELF_T_HALF);
768	};
769
770	/*
771	 * Check for the sparc specific section types
772	 * below.
773	 */
774	if (((ehdr->e_machine == EM_SPARC) ||
775	    (ehdr->e_machine == EM_SPARC32PLUS) ||
776	    (ehdr->e_machine == EM_SPARCV9)) &&
777	    (shtype == SHT_SPARC_GOTDATA))
778		return (ELF_T_BYTE);
779
780	/*
781	 * Check for the amd64 specific section types
782	 * below.
783	 */
784	if ((ehdr->e_machine == EM_AMD64) &&
785	    (shtype == SHT_AMD64_UNWIND))
786		return (ELF_T_BYTE);
787
788	/*
789	 * And the default is ELF_T_BYTE - but we should
790	 * certainly have caught any sections we know about
791	 * above.  This is for unknown sections to libelf.
792	 */
793	return (ELF_T_BYTE);
794}
795
796
797/*
798 * XX64	This routine is also used to 'version' interactions with Elf64
799 *	applications, but there's no way to figure out if the caller is
800 *	asking Elf32 or Elf64 questions, even though it has Elf32
801 *	dependencies.  Ick.
802 */
803unsigned
804elf_version(unsigned ver)
805{
806	register unsigned	j;
807	union
808	{
809		Elf32_Word	w;
810		unsigned char	c[W_sizeof];
811	} u;
812
813
814
815	if (ver == EV_NONE)
816		return EV_CURRENT;
817	if (ver > EV_CURRENT)
818	{
819		_elf_seterr(EREQ_VER, 0);
820		return EV_NONE;
821	}
822	(void) mutex_lock(&_elf_globals_mutex);
823	if (_elf_work != EV_NONE)
824	{
825		j = _elf_work;
826		_elf_work = ver;
827		(void) mutex_unlock(&_elf_globals_mutex);
828		return j;
829	}
830	_elf_work = ver;
831
832	u.w = 0x10203;
833	/*CONSTANTCONDITION*/
834	if (~(Elf32_Word)0 == -(Elf32_Sword)1
835	&& tomw(u.c, W_L) == 0x10203)
836		_elf_encode = ELFDATA2LSB;
837	/*CONSTANTCONDITION*/
838	else if (~(Elf32_Word)0 == -(Elf32_Sword)1
839	&& tomw(u.c, W_M) == 0x10203)
840		_elf_encode = ELFDATA2MSB;
841
842	(void) mutex_unlock(&_elf_globals_mutex);
843
844	return ver;
845}
846
847
848static Elf_Data *
849xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof)
850						/* tof !0 -> xlatetof */
851{
852	size_t		cnt, dsz, ssz;
853	unsigned	type;
854	unsigned	dver, sver;
855	void		(*f)();
856	unsigned	_encode;
857
858	if (dst == 0 || src == 0)
859		return (0);
860	if (--encode >= (ELFDATANUM - 1)) {
861		_elf_seterr(EREQ_ENCODE, 0);
862		return (0);
863	}
864	if ((dver = dst->d_version - 1) >= EV_CURRENT ||
865	    (sver = src->d_version - 1) >= EV_CURRENT) {
866		_elf_seterr(EREQ_VER, 0);
867		return (0);
868	}
869	if ((type = src->d_type) >= ELF_T_NUM) {
870		_elf_seterr(EREQ_TYPE, 0);
871		return (0);
872	}
873
874	if (tof) {
875		dsz = fmsize[dver][type].s_filesz;
876		ssz = fmsize[sver][type].s_memsz;
877		f = x32[dver][sver][encode][type].x_tof;
878	} else {
879		dsz = fmsize[dver][type].s_memsz;
880		ssz = fmsize[sver][type].s_filesz;
881		f = x32[dver][sver][encode][type].x_tom;
882	}
883	cnt = src->d_size / ssz;
884	if (dst->d_size < dsz * cnt) {
885		_elf_seterr(EREQ_DSZ, 0);
886		return (0);
887	}
888
889	ELFACCESSDATA(_encode, _elf_encode)
890	if ((_encode == (encode + 1)) && (dsz == ssz)) {
891		/*
892		 *	ld(1) frequently produces empty sections (eg. .dynsym,
893		 *	.dynstr, .symtab, .strtab, etc) so that the initial
894		 *	output image can be created of the correct size.  Later
895		 *	these sections are filled in with the associated data.
896		 *	So that we don't have to pre-allocate buffers for
897		 *	these segments, allow for the src destination to be 0.
898		 */
899		if (src->d_buf && src->d_buf != dst->d_buf)
900			(void) memcpy(dst->d_buf, src->d_buf, src->d_size);
901		dst->d_type = src->d_type;
902		dst->d_size = src->d_size;
903		return (dst);
904	}
905	if (cnt)
906		(*f)(dst->d_buf, src->d_buf, cnt);
907	dst->d_size = dsz * cnt;
908	dst->d_type = src->d_type;
909	return (dst);
910}
911
912
913Elf_Data *
914elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode)
915{
916	return (xlate(dst, src, encode, 1));
917}
918
919
920Elf_Data *
921elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode)
922{
923	return (xlate(dst, src, encode, 0));
924}
925
926
927/*
928 * xlate to file format
929 *
930 *	..._tof(name, data) -- macros
931 *
932 *	Recall that the file format must be no larger than the
933 *	memory format (equal versions).  Use "forward" copy.
934 *	All these routines require non-null, non-zero arguments.
935 */
936
937define(addr_tof, `
938static void
939$1(unsigned char *dst, Elf32_Addr *src, size_t cnt)
940{
941	Elf32_Addr	*end = src + cnt;
942
943	do {
944		tofa(dst, *src, A_$2);
945		dst += A_sizeof;
946	} while (++src < end);
947}')
948
949addr_tof(addr_2L_tof,L)
950addr_tof(addr_2M_tof,M)
951
952
953static void
954byte_to(unsigned char *dst, unsigned char *src, size_t cnt)
955{
956	if (dst != src)
957		(void) memcpy(dst, src, cnt);
958}
959
960
961define(dyn_11_tof, `
962static void
963$1(unsigned char *dst, Elf32_Dyn *src, size_t cnt)
964{
965	Elf32_Dyn	*end = src + cnt;
966
967	do {
968		tofw(dst, src->d_tag, D1_tag_$2);
969		tofo(dst, src->d_un.d_val, D1_val_$2);
970		dst += D1_sizeof;
971	} while (++src < end);
972}')
973
974dyn_11_tof(dyn_2L11_tof,L)
975dyn_11_tof(dyn_2M11_tof,M)
976
977
978define(ehdr_11_tof, `
979static void
980$1(unsigned char *dst, Elf32_Ehdr *src, size_t cnt)
981{
982	Elf32_Ehdr	*end = src + cnt;
983
984	do {
985		if (&dst[E1_ident] != src->e_ident)
986			(void) memcpy(&dst[E1_ident], src->e_ident, E1_Nident);
987		tofh(dst, src->e_type, E1_type_$2);
988		tofh(dst, src->e_machine, E1_machine_$2);
989		tofw(dst, src->e_version, E1_version_$2);
990		tofa(dst, src->e_entry, E1_entry_$2);
991		tofo(dst, src->e_phoff, E1_phoff_$2);
992		tofo(dst, src->e_shoff, E1_shoff_$2);
993		tofw(dst, src->e_flags, E1_flags_$2);
994		tofh(dst, src->e_ehsize, E1_ehsize_$2);
995		tofh(dst, src->e_phentsize, E1_phentsize_$2);
996		tofh(dst, src->e_phnum, E1_phnum_$2);
997		tofh(dst, src->e_shentsize, E1_shentsize_$2);
998		tofh(dst, src->e_shnum, E1_shnum_$2);
999		tofh(dst, src->e_shstrndx, E1_shstrndx_$2);
1000		dst += E1_sizeof;
1001	} while (++src < end);
1002}')
1003
1004ehdr_11_tof(ehdr_2L11_tof,L)
1005ehdr_11_tof(ehdr_2M11_tof,M)
1006
1007
1008define(half_tof, `
1009static void
1010$1(unsigned char *dst, Elf32_Half *src, size_t cnt)
1011{
1012	Elf32_Half	*end = src + cnt;
1013
1014	do {
1015		tofh(dst, *src, H_$2);
1016		dst += H_sizeof;
1017	} while (++src < end);
1018}')
1019
1020half_tof(half_2L_tof,L)
1021half_tof(half_2M_tof,M)
1022
1023
1024define(move_11_tof, `
1025static void
1026$1(unsigned char *dst, Elf32_Move *src, size_t cnt)
1027{
1028	Elf32_Move	*end = src + cnt;
1029
1030	do {
1031		tofl(dst, src->m_value, M1_value_$2);
1032		tofw(dst, src->m_info, M1_info_$2);
1033		tofw(dst, src->m_poffset, M1_poffset_$2);
1034		tofh(dst, src->m_repeat, M1_repeat_$2);
1035		tofh(dst, src->m_stride, M1_stride_$2);
1036		dst += M1_sizeof;
1037	} while (++src < end);
1038}')
1039
1040move_11_tof(move_2L11_tof,L)
1041move_11_tof(move_2M11_tof,M)
1042
1043
1044define(movep_11_tof, `
1045static void
1046$1(unsigned char *dst, Elf32_Move *src, size_t cnt)
1047{
1048	Elf32_Move	*end = src + cnt;
1049
1050	do {
1051		tofl(dst, src->m_value, MP1_value_$2);
1052		tofw(dst, src->m_info, MP1_info_$2);
1053		tofw(dst, src->m_poffset, MP1_poffset_$2);
1054		tofh(dst, src->m_repeat, MP1_repeat_$2);
1055		tofh(dst, src->m_stride, MP1_stride_$2);
1056		dst += MP1_sizeof;
1057	} while (++src < end);
1058}')
1059
1060movep_11_tof(movep_2L11_tof,L)
1061movep_11_tof(movep_2M11_tof,M)
1062
1063
1064define(off_tof, `
1065static void
1066$1(unsigned char *dst, Elf32_Off *src, size_t cnt)
1067{
1068	Elf32_Off	*end = src + cnt;
1069
1070	do {
1071		tofo(dst, *src, O_$2);
1072		dst += O_sizeof;
1073	} while (++src < end);
1074}')
1075
1076off_tof(off_2L_tof,L)
1077off_tof(off_2M_tof,M)
1078
1079
1080define(note_11_tof, `
1081static void
1082$1(unsigned char *dst, Elf32_Nhdr *src, size_t cnt)
1083{
1084	/* LINTED */
1085	Elf32_Nhdr *	end = (Elf32_Nhdr *)((char *)src + cnt);
1086
1087	do {
1088		Elf32_Word	descsz, namesz;
1089
1090		/*
1091		 * cache size of desc & name fields - while rounding
1092		 * up their size.
1093		 */
1094		namesz = S_ROUND(src->n_namesz, sizeof (Elf32_Word));
1095		descsz = src->n_descsz;
1096
1097		/*
1098		 * Copy contents of Elf32_Nhdr
1099		 */
1100		tofw(dst, src->n_namesz, N1_namesz_$2);
1101		tofw(dst, src->n_descsz, N1_descsz_$2);
1102		tofw(dst, src->n_type, N1_type_$2);
1103
1104		/*
1105		 * Copy contents of Name field
1106		 */
1107		dst += N1_sizeof;
1108		src++;
1109		(void)memcpy(dst, src, namesz);
1110
1111		/*
1112		 * Copy contents of desc field
1113		 */
1114		dst += namesz;
1115		src = (Elf32_Nhdr *)((uintptr_t)src + namesz);
1116		(void)memcpy(dst, src, descsz);
1117		descsz = S_ROUND(descsz, sizeof (Elf32_Word));
1118		dst += descsz;
1119		src = (Elf32_Nhdr *)((uintptr_t)src + descsz);
1120	} while (src < end);
1121}')
1122
1123note_11_tof(note_2L11_tof,L)
1124note_11_tof(note_2M11_tof,M)
1125
1126
1127define(phdr_11_tof, `
1128static void
1129$1(unsigned char *dst, Elf32_Phdr *src, size_t cnt)
1130{
1131	Elf32_Phdr	*end = src + cnt;
1132
1133	do {
1134		tofw(dst, src->p_type, P1_type_$2);
1135		tofo(dst, src->p_offset, P1_offset_$2);
1136		tofa(dst, src->p_vaddr, P1_vaddr_$2);
1137		tofa(dst, src->p_paddr, P1_paddr_$2);
1138		tofw(dst, src->p_filesz, P1_filesz_$2);
1139		tofw(dst, src->p_memsz, P1_memsz_$2);
1140		tofw(dst, src->p_flags, P1_flags_$2);
1141		tofw(dst, src->p_align, P1_align_$2);
1142		dst += P1_sizeof;
1143	} while (++src < end);
1144}')
1145
1146phdr_11_tof(phdr_2L11_tof,L)
1147phdr_11_tof(phdr_2M11_tof,M)
1148
1149
1150define(rel_11_tof, `
1151static void
1152$1(unsigned char *dst, Elf32_Rel *src, size_t cnt)
1153{
1154	Elf32_Rel	*end = src + cnt;
1155
1156	do {
1157		tofa(dst, src->r_offset, R1_offset_$2);
1158		tofw(dst, src->r_info, R1_info_$2);
1159		dst += R1_sizeof;
1160	} while (++src < end);
1161}')
1162
1163rel_11_tof(rel_2L11_tof,L)
1164rel_11_tof(rel_2M11_tof,M)
1165
1166
1167define(rela_11_tof, `
1168static void
1169$1(unsigned char *dst, Elf32_Rela *src, size_t cnt)
1170{
1171	Elf32_Rela	*end = src + cnt;
1172
1173	do {
1174		tofa(dst, src->r_offset, RA1_offset_$2);
1175		tofw(dst, src->r_info, RA1_info_$2);
1176		/*CONSTANTCONDITION*/
1177		if (~(Elf32_Word)0 == -(Elf32_Sword)1) {	/* 2s comp */
1178			tofw(dst, src->r_addend, RA1_addend_$2);
1179		} else {
1180			Elf32_Word	w;
1181
1182			if (src->r_addend < 0) {
1183				w = - src->r_addend;
1184				w = ~w + 1;
1185			} else
1186				w = src->r_addend;
1187			tofw(dst, w, RA1_addend_$2);
1188		}
1189		dst += RA1_sizeof;
1190	} while (++src < end);
1191}')
1192
1193rela_11_tof(rela_2L11_tof,L)
1194rela_11_tof(rela_2M11_tof,M)
1195
1196
1197define(shdr_11_tof, `
1198static void
1199$1(unsigned char *dst, Elf32_Shdr *src, size_t cnt)
1200{
1201	Elf32_Shdr	*end = src + cnt;
1202
1203	do {
1204		tofw(dst, src->sh_name, SH1_name_$2);
1205		tofw(dst, src->sh_type, SH1_type_$2);
1206		tofw(dst, src->sh_flags, SH1_flags_$2);
1207		tofa(dst, src->sh_addr, SH1_addr_$2);
1208		tofo(dst, src->sh_offset, SH1_offset_$2);
1209		tofw(dst, src->sh_size, SH1_size_$2);
1210		tofw(dst, src->sh_link, SH1_link_$2);
1211		tofw(dst, src->sh_info, SH1_info_$2);
1212		tofw(dst, src->sh_addralign, SH1_addralign_$2);
1213		tofw(dst, src->sh_entsize, SH1_entsize_$2);
1214		dst += SH1_sizeof;
1215	} while (++src < end);
1216}')
1217
1218shdr_11_tof(shdr_2L11_tof,L)
1219shdr_11_tof(shdr_2M11_tof,M)
1220
1221
1222define(sword_tof, `
1223static void
1224$1(unsigned char *dst, Elf32_Sword *src, size_t cnt)
1225{
1226	Elf32_Sword	*end = src + cnt;
1227
1228	do {
1229		/*CONSTANTCONDITION*/
1230		if (~(Elf32_Word)0 == -(Elf32_Sword)1) {	/* 2s comp */
1231			tofw(dst, *src, W_$2);
1232		} else {
1233			Elf32_Word	w;
1234
1235			if (*src < 0) {
1236				w = - *src;
1237				w = ~w + 1;
1238			} else
1239				w = *src;
1240			tofw(dst, w, W_$2);
1241		}
1242		dst += W_sizeof;
1243	} while (++src < end);
1244}')
1245
1246sword_tof(sword_2L_tof,L)
1247sword_tof(sword_2M_tof,M)
1248
1249
1250define(cap_11_tof, `
1251static void
1252$1(unsigned char *dst, Elf32_Cap *src, size_t cnt)
1253{
1254	Elf32_Cap	*end = src + cnt;
1255
1256	do {
1257		tofw(dst, src->c_tag, C1_tag_$2);
1258		tofw(dst, src->c_un.c_val, C1_val_$2);
1259		dst += C1_sizeof;
1260	} while (++src < end);
1261}')
1262
1263cap_11_tof(cap_2L11_tof,L)
1264cap_11_tof(cap_2M11_tof,M)
1265
1266
1267define(syminfo_11_tof, `
1268static void
1269$1(unsigned char *dst, Elf32_Syminfo *src, size_t cnt)
1270{
1271	Elf32_Syminfo	*end = src + cnt;
1272
1273	do {
1274		tofh(dst, src->si_boundto, SI1_boundto_$2);
1275		tofh(dst, src->si_flags, SI1_flags_$2);
1276		dst += SI1_sizeof;
1277	} while (++src < end);
1278}')
1279
1280syminfo_11_tof(syminfo_2L11_tof,L)
1281syminfo_11_tof(syminfo_2M11_tof,M)
1282
1283
1284define(sym_11_tof, `
1285static void
1286$1(unsigned char *dst, Elf32_Sym *src, size_t cnt)
1287{
1288	Elf32_Sym	*end = src + cnt;
1289
1290	do {
1291		tofw(dst, src->st_name, ST1_name_$2);
1292		tofa(dst, src->st_value, ST1_value_$2);
1293		tofw(dst, src->st_size, ST1_size_$2);
1294		tofb(dst, src->st_info, ST1_info_$2);
1295		tofb(dst, src->st_other, ST1_other_$2);
1296		tofh(dst, src->st_shndx, ST1_shndx_$2);
1297		dst += ST1_sizeof;
1298	} while (++src < end);
1299}')
1300
1301sym_11_tof(sym_2L11_tof,L)
1302sym_11_tof(sym_2M11_tof,M)
1303
1304
1305define(word_tof, `
1306static void
1307$1(unsigned char *dst, Elf32_Word *src, size_t cnt)
1308{
1309	Elf32_Word	*end = src + cnt;
1310
1311	do {
1312		tofw(dst, *src, W_$2);
1313		dst += W_sizeof;
1314	} while (++src < end);
1315}')
1316
1317word_tof(word_2L_tof,L)
1318word_tof(word_2M_tof,M)
1319
1320
1321define(verdef_11_tof, `
1322static void
1323$1(unsigned char *dst, Elf32_Verdef *src, size_t cnt)
1324{
1325	/* LINTED */
1326	Elf32_Verdef	*end = (Elf32_Verdef *)((char *)src + cnt);
1327
1328	do {
1329		Elf32_Verdef	*next_verdef;
1330		Elf32_Verdaux	*vaux;
1331		Elf32_Half	i;
1332		unsigned char	*vaux_dst;
1333		unsigned char	*dst_next;
1334
1335		/* LINTED */
1336		next_verdef = (Elf32_Verdef *)(src->vd_next ?
1337		    (char *)src + src->vd_next : (char *)end);
1338		dst_next = dst + src->vd_next;
1339
1340		/* LINTED */
1341		vaux = (Elf32_Verdaux *)((char *)src + src->vd_aux);
1342		vaux_dst = dst + src->vd_aux;
1343
1344		/*
1345		 * Convert auxilary structures
1346		 */
1347		for (i = 0; i < src->vd_cnt; i++) {
1348			Elf32_Verdaux	*vaux_next;
1349			unsigned char	*vaux_dst_next;
1350
1351			/*
1352			 * because our source and destination can be
1353			 * the same place we need to figure out the next
1354			 * location now.
1355			 */
1356			/* LINTED */
1357			vaux_next = (Elf32_Verdaux *)((char *)vaux +
1358			    vaux->vda_next);
1359			vaux_dst_next = vaux_dst + vaux->vda_next;
1360
1361			tofa(vaux_dst, vaux->vda_name, VDA1_name_$2);
1362			tofw(vaux_dst, vaux->vda_next, VDA1_next_$2);
1363			vaux_dst = vaux_dst_next;
1364			vaux = vaux_next;
1365		}
1366
1367		/*
1368		 * Convert Elf32_Verdef structure.
1369		 */
1370		tofh(dst, src->vd_version, VD1_version_$2);
1371		tofh(dst, src->vd_flags, VD1_flags_$2);
1372		tofh(dst, src->vd_ndx, VD1_ndx_$2);
1373		tofh(dst, src->vd_cnt, VD1_cnt_$2);
1374		tofw(dst, src->vd_hash, VD1_hash_$2);
1375		tofw(dst, src->vd_aux, VD1_aux_$2);
1376		tofw(dst, src->vd_next, VD1_next_$2);
1377		src = next_verdef;
1378		dst = dst_next;
1379	} while (src < end);
1380}')
1381
1382verdef_11_tof(verdef_2L11_tof, L)
1383verdef_11_tof(verdef_2M11_tof, M)
1384
1385define(verneed_11_tof, `
1386static void
1387$1(unsigned char *dst, Elf32_Verneed *src, size_t cnt)
1388{
1389	/* LINTED */
1390	Elf32_Verneed	*end = (Elf32_Verneed *)((char *)src + cnt);
1391
1392	do {
1393		Elf32_Verneed	*next_verneed;
1394		Elf32_Vernaux	*vaux;
1395		Elf32_Half	i;
1396		unsigned char	*vaux_dst;
1397		unsigned char	*dst_next;
1398
1399		/* LINTED */
1400		next_verneed = (Elf32_Verneed *)(src->vn_next ?
1401		    (char *)src + src->vn_next : (char *)end);
1402		dst_next = dst + src->vn_next;
1403
1404		/* LINTED */
1405		vaux = (Elf32_Vernaux *)((char *)src + src->vn_aux);
1406		vaux_dst = dst + src->vn_aux;
1407
1408		/*
1409		 * Convert auxilary structures first
1410		 */
1411		for (i = 0; i < src->vn_cnt; i++) {
1412			Elf32_Vernaux *	vaux_next;
1413			unsigned char *	vaux_dst_next;
1414
1415			/*
1416			 * because our source and destination can be
1417			 * the same place we need to figure out the
1418			 * next location now.
1419			 */
1420			/* LINTED */
1421			vaux_next = (Elf32_Vernaux *)((char *)vaux +
1422			    vaux->vna_next);
1423			vaux_dst_next = vaux_dst + vaux->vna_next;
1424
1425			tofw(vaux_dst, vaux->vna_hash, VNA1_hash_$2);
1426			tofh(vaux_dst, vaux->vna_flags, VNA1_flags_$2);
1427			tofh(vaux_dst, vaux->vna_other, VNA1_other_$2);
1428			tofa(vaux_dst, vaux->vna_name, VNA1_name_$2);
1429			tofw(vaux_dst, vaux->vna_next, VNA1_next_$2);
1430			vaux_dst = vaux_dst_next;
1431			vaux = vaux_next;
1432		}
1433		/*
1434		 * Convert Elf32_Verneed structure.
1435		 */
1436		tofh(dst, src->vn_version, VN1_version_$2);
1437		tofh(dst, src->vn_cnt, VN1_cnt_$2);
1438		tofa(dst, src->vn_file, VN1_file_$2);
1439		tofw(dst, src->vn_aux, VN1_aux_$2);
1440		tofw(dst, src->vn_next, VN1_next_$2);
1441		src = next_verneed;
1442		dst = dst_next;
1443	} while (src < end);
1444}')
1445
1446verneed_11_tof(verneed_2L11_tof, L)
1447verneed_11_tof(verneed_2M11_tof, M)
1448
1449
1450/* xlate to memory format
1451 *
1452 *	..._tom(name, data) -- macros
1453 *
1454 *	Recall that the memory format may be larger than the
1455 *	file format (equal versions).  Use "backward" copy.
1456 *	All these routines require non-null, non-zero arguments.
1457 */
1458
1459
1460define(addr_tom, `
1461static void
1462$1(Elf32_Addr *dst, unsigned char *src, size_t cnt)
1463{
1464	Elf32_Addr	*end = dst;
1465
1466	dst += cnt;
1467	src += cnt * A_sizeof;
1468	while (dst-- > end) {
1469		src -= A_sizeof;
1470		*dst = toma(src, A_$2);
1471	}
1472}')
1473
1474addr_tom(addr_2L_tom,L)
1475addr_tom(addr_2M_tom,M)
1476
1477
1478define(dyn_11_tom, `
1479static void
1480$1(Elf32_Dyn *dst, unsigned char *src, size_t cnt)
1481{
1482	Elf32_Dyn	*end = dst + cnt;
1483
1484	do {
1485		dst->d_tag = tomw(src, D1_tag_$2);
1486		dst->d_un.d_val = tomw(src, D1_val_$2);
1487		src += D1_sizeof;
1488	} while (++dst < end);
1489}')
1490
1491dyn_11_tom(dyn_2L11_tom,L)
1492dyn_11_tom(dyn_2M11_tom,M)
1493
1494
1495define(ehdr_11_tom, `
1496static void
1497$1(Elf32_Ehdr *dst, unsigned char *src, size_t cnt)
1498{
1499	Elf32_Ehdr	*end = dst;
1500
1501	dst += cnt;
1502	src += cnt * E1_sizeof;
1503	while (dst-- > end) {
1504		src -= E1_sizeof;
1505		dst->e_shstrndx = tomh(src, E1_shstrndx_$2);
1506		dst->e_shnum = tomh(src, E1_shnum_$2);
1507		dst->e_shentsize = tomh(src, E1_shentsize_$2);
1508		dst->e_phnum = tomh(src, E1_phnum_$2);
1509		dst->e_phentsize = tomh(src, E1_phentsize_$2);
1510		dst->e_ehsize = tomh(src, E1_ehsize_$2);
1511		dst->e_flags = tomw(src, E1_flags_$2);
1512		dst->e_shoff = tomo(src, E1_shoff_$2);
1513		dst->e_phoff = tomo(src, E1_phoff_$2);
1514		dst->e_entry = toma(src, E1_entry_$2);
1515		dst->e_version = tomw(src, E1_version_$2);
1516		dst->e_machine = tomh(src, E1_machine_$2);
1517		dst->e_type = tomh(src, E1_type_$2);
1518		if (dst->e_ident != &src[E1_ident])
1519			(void) memcpy(dst->e_ident, &src[E1_ident], E1_Nident);
1520	}
1521}')
1522
1523ehdr_11_tom(ehdr_2L11_tom,L)
1524ehdr_11_tom(ehdr_2M11_tom,M)
1525
1526
1527define(half_tom, `
1528static void
1529$1(Elf32_Half *dst, unsigned char *src, size_t cnt)
1530{
1531	Elf32_Half	*end = dst;
1532
1533	dst += cnt;
1534	src += cnt * H_sizeof;
1535	while (dst-- > end) {
1536		src -= H_sizeof;
1537		*dst = tomh(src, H_$2);
1538	}
1539}')
1540
1541half_tom(half_2L_tom,L)
1542half_tom(half_2M_tom,M)
1543
1544
1545define(move_11_tom, `
1546static void
1547$1(Elf32_Move *dst, unsigned char *src, size_t cnt)
1548{
1549	Elf32_Move	*end = dst + cnt;
1550
1551	do {
1552		dst->m_value = toml(src, M1_value_$2);
1553		dst->m_info = tomw(src, M1_info_$2);
1554		dst->m_poffset = tomw(src, M1_poffset_$2);
1555		dst->m_repeat = tomh(src, M1_repeat_$2);
1556		dst->m_stride = tomh(src, M1_stride_$2);
1557		src += M1_sizeof;
1558	} while (++dst < end);
1559}')
1560
1561move_11_tom(move_2L11_tom,L)
1562move_11_tom(move_2M11_tom,M)
1563
1564
1565define(movep_11_tom, `
1566static void
1567$1(Elf32_Move *dst, unsigned char *src, size_t cnt)
1568{
1569	Elf32_Move		*end = dst + cnt;
1570
1571	do
1572	{
1573		dst->m_value = toml(src, MP1_value_$2);
1574		dst->m_info = tomw(src, MP1_info_$2);
1575		dst->m_poffset = tomw(src, MP1_poffset_$2);
1576		dst->m_repeat = tomh(src, MP1_repeat_$2);
1577		dst->m_stride = tomh(src, MP1_stride_$2);
1578		src += MP1_sizeof;
1579	} while (++dst < end);
1580}')
1581
1582movep_11_tom(movep_2L11_tom,L)
1583movep_11_tom(movep_2M11_tom,M)
1584
1585
1586define(note_11_tom, `
1587static void
1588$1(Elf32_Nhdr *dst, unsigned char *src, size_t cnt)
1589{
1590	/* LINTED */
1591	Elf32_Nhdr	*end = (Elf32_Nhdr *)((char *)dst + cnt);
1592
1593	while (dst < end) {
1594		Elf32_Nhdr *	nhdr;
1595		unsigned char *	namestr;
1596		void *		desc;
1597		Elf32_Word	field_sz;
1598
1599		dst->n_namesz = tomw(src, N1_namesz_$2);
1600		dst->n_descsz = tomw(src, N1_descsz_$2);
1601		dst->n_type = tomw(src, N1_type_$2);
1602		nhdr = dst;
1603		/* LINTED */
1604		dst = (Elf32_Nhdr *)((char *)dst + sizeof (Elf32_Nhdr));
1605		namestr = src + N1_sizeof;
1606		field_sz = S_ROUND(nhdr->n_namesz, sizeof (Elf32_Word));
1607		(void)memcpy((void *)dst, namestr, field_sz);
1608		desc = namestr + field_sz;
1609		/* LINTED */
1610		dst = (Elf32_Nhdr *)((char *)dst + field_sz);
1611		field_sz = nhdr->n_descsz;
1612		(void)memcpy(dst, desc, field_sz);
1613		field_sz = S_ROUND(field_sz, sizeof (Elf32_Word));
1614		/* LINTED */
1615		dst = (Elf32_Nhdr *)((char *)dst + field_sz);
1616		src = (unsigned char *)desc + field_sz;
1617	}
1618}')
1619
1620note_11_tom(note_2L11_tom,L)
1621note_11_tom(note_2M11_tom,M)
1622
1623
1624define(off_tom, `
1625static void
1626$1(Elf32_Off *dst, unsigned char *src, size_t cnt)
1627{
1628	Elf32_Off	*end = dst;
1629
1630	dst += cnt;
1631	src += cnt * O_sizeof;
1632	while (dst-- > end) {
1633		src -= O_sizeof;
1634		*dst = tomo(src, O_$2);
1635	}
1636}')
1637
1638off_tom(off_2L_tom,L)
1639off_tom(off_2M_tom,M)
1640
1641
1642define(phdr_11_tom, `
1643static void
1644$1(Elf32_Phdr *dst, unsigned char *src, size_t cnt)
1645{
1646	Elf32_Phdr	*end = dst;
1647
1648	dst += cnt;
1649	src += cnt * P1_sizeof;
1650	while (dst-- > end) {
1651		src -= P1_sizeof;
1652		dst->p_align = tomw(src, P1_align_$2);
1653		dst->p_flags = tomw(src, P1_flags_$2);
1654		dst->p_memsz = tomw(src, P1_memsz_$2);
1655		dst->p_filesz = tomw(src, P1_filesz_$2);
1656		dst->p_paddr = toma(src, P1_paddr_$2);
1657		dst->p_vaddr = toma(src, P1_vaddr_$2);
1658		dst->p_offset = tomo(src, P1_offset_$2);
1659		dst->p_type = tomw(src, P1_type_$2);
1660	}
1661}')
1662
1663phdr_11_tom(phdr_2L11_tom,L)
1664phdr_11_tom(phdr_2M11_tom,M)
1665
1666
1667define(rel_11_tom, `
1668static void
1669$1(Elf32_Rel *dst, unsigned char *src, size_t cnt)
1670{
1671	Elf32_Rel	*end = dst;
1672
1673	dst += cnt;
1674	src += cnt * R1_sizeof;
1675	while (dst-- > end) {
1676		src -= R1_sizeof;
1677		dst->r_info = tomw(src, R1_info_$2);
1678		dst->r_offset = toma(src, R1_offset_$2);
1679	}
1680}')
1681
1682rel_11_tom(rel_2L11_tom,L)
1683rel_11_tom(rel_2M11_tom,M)
1684
1685
1686define(rela_11_tom, `
1687static void
1688$1(Elf32_Rela *dst, unsigned char *src, size_t cnt)
1689{
1690	Elf32_Rela	*end = dst;
1691
1692	dst += cnt;
1693	src += cnt * RA1_sizeof;
1694	while (dst-- > end) {
1695		src -= RA1_sizeof;
1696		/*CONSTANTCONDITION*/
1697		if (~(Elf32_Word)0 == -(Elf32_Sword)1 &&	/* 2s comp */
1698		    ~(~(Elf32_Word)0 >> 1) == HI32) {
1699			dst->r_addend = tomw(src, RA1_addend_$2);
1700		} else {
1701			union {
1702				Elf32_Word w;
1703				Elf32_Sword sw;
1704			} u;
1705
1706			if ((u.w = tomw(src, RA1_addend_$2)) & HI32) {
1707				u.w |= ~(Elf32_Word)LO31;
1708				u.w = ~u.w + 1;
1709				u.sw = -u.w;
1710			}
1711			dst->r_addend = u.sw;
1712		}
1713		dst->r_info = tomw(src, RA1_info_$2);
1714		dst->r_offset = toma(src, RA1_offset_$2);
1715	}
1716}')
1717
1718rela_11_tom(rela_2L11_tom,L)
1719rela_11_tom(rela_2M11_tom,M)
1720
1721
1722define(shdr_11_tom, `
1723static void
1724$1(Elf32_Shdr *dst, unsigned char *src, size_t cnt)
1725{
1726	Elf32_Shdr	*end = dst;
1727
1728	dst += cnt;
1729	src += cnt * SH1_sizeof;
1730	while (dst-- > end) {
1731		src -= SH1_sizeof;
1732		dst->sh_entsize = tomw(src, SH1_entsize_$2);
1733		dst->sh_addralign = tomw(src, SH1_addralign_$2);
1734		dst->sh_info = tomw(src, SH1_info_$2);
1735		dst->sh_link = tomw(src, SH1_link_$2);
1736		dst->sh_size = tomw(src, SH1_size_$2);
1737		dst->sh_offset = tomo(src, SH1_offset_$2);
1738		dst->sh_addr = toma(src, SH1_addr_$2);
1739		dst->sh_flags = tomw(src, SH1_flags_$2);
1740		dst->sh_type = tomw(src, SH1_type_$2);
1741		dst->sh_name = tomw(src, SH1_name_$2);
1742	}
1743}')
1744
1745shdr_11_tom(shdr_2L11_tom,L)
1746shdr_11_tom(shdr_2M11_tom,M)
1747
1748
1749
1750define(sword_tom, `
1751static void
1752$1(Elf32_Sword *dst, unsigned char *src, size_t cnt)
1753{
1754	Elf32_Sword	*end = dst;
1755
1756	dst += cnt;
1757	src += cnt * W_sizeof;
1758	while (dst-- > end) {
1759		src -= W_sizeof;
1760		/*CONSTANTCONDITION*/
1761		if (~(Elf32_Word)0 == -(Elf32_Sword)1 &&	/* 2s comp */
1762		    ~(~(Elf32_Word)0 >> 1) == HI32) {
1763			*dst = tomw(src, W_$2);
1764		} else {
1765			union {
1766				Elf32_Word w;
1767				Elf32_Sword sw;
1768			} u;
1769
1770			if ((u.w = tomw(src, W_$2)) & HI32) {
1771				u.w |= ~(Elf32_Word)LO31;
1772				u.w = ~u.w + 1;
1773				u.sw = -u.w;
1774			}
1775			*dst = u.sw;
1776		}
1777	}
1778}')
1779
1780sword_tom(sword_2L_tom,L)
1781sword_tom(sword_2M_tom,M)
1782
1783
1784define(cap_11_tom, `
1785static void
1786$1(Elf32_Cap *dst, unsigned char *src, size_t cnt)
1787{
1788	Elf32_Cap	*end = dst + cnt;
1789
1790	do {
1791		dst->c_tag = tomw(src, C1_tag_$2);
1792		dst->c_un.c_val = tomw(src, C1_val_$2);
1793		src += C1_sizeof;
1794	} while (++dst < end);
1795}')
1796
1797cap_11_tom(cap_2L11_tom,L)
1798cap_11_tom(cap_2M11_tom,M)
1799
1800
1801define(syminfo_11_tom, `
1802static void
1803$1(Elf32_Syminfo *dst, unsigned char *src, size_t cnt)
1804{
1805	Elf32_Syminfo	*end = dst;
1806
1807	dst += cnt;
1808	src += cnt * SI1_sizeof;
1809	while (dst-- > end) {
1810		src -= SI1_sizeof;
1811		dst->si_boundto = tomh(src, SI1_boundto_$2);
1812		dst->si_flags = tomh(src, SI1_flags_$2);
1813	}
1814}')
1815
1816syminfo_11_tom(syminfo_2L11_tom,L)
1817syminfo_11_tom(syminfo_2M11_tom,M)
1818
1819
1820define(sym_11_tom, `
1821static void
1822$1(Elf32_Sym *dst, unsigned char *src, size_t cnt)
1823{
1824	Elf32_Sym	*end = dst;
1825
1826	dst += cnt;
1827	src += cnt * ST1_sizeof;
1828	while (dst-- > end) {
1829		src -= ST1_sizeof;
1830		dst->st_shndx = tomh(src, ST1_shndx_$2);
1831		dst->st_other = tomb(src, ST1_other_$2);
1832		dst->st_info = tomb(src, ST1_info_$2);
1833		dst->st_size = tomw(src, ST1_size_$2);
1834		dst->st_value = toma(src, ST1_value_$2);
1835		dst->st_name = tomw(src, ST1_name_$2);
1836	}
1837}')
1838
1839sym_11_tom(sym_2L11_tom,L)
1840sym_11_tom(sym_2M11_tom,M)
1841
1842
1843define(word_tom, `
1844static void
1845$1(Elf32_Word *dst, unsigned char *src, size_t cnt)
1846{
1847	Elf32_Word	*end = dst;
1848
1849	dst += cnt;
1850	src += cnt * W_sizeof;
1851	while (dst-- > end) {
1852		src -= W_sizeof;
1853		*dst = tomw(src, W_$2);
1854	}
1855}')
1856
1857word_tom(word_2L_tom,L)
1858word_tom(word_2M_tom,M)
1859
1860
1861define(verdef_11_tom, `
1862static void
1863$1(Elf32_Verdef *dst, unsigned char *src, size_t cnt)
1864{
1865	/* LINTED */
1866	Elf32_Verdef	*end = (Elf32_Verdef *)((char *)dst + cnt);
1867
1868	while (dst < end) {
1869		Elf32_Verdaux	*vaux;
1870		unsigned char	*src_vaux;
1871		Elf32_Half	i;
1872
1873		dst->vd_version = tomh(src, VD1_version_$2);
1874		dst->vd_flags = tomh(src, VD1_flags_$2);
1875		dst->vd_ndx = tomh(src, VD1_ndx_$2);
1876		dst->vd_cnt = tomh(src, VD1_cnt_$2);
1877		dst->vd_hash = tomw(src, VD1_hash_$2);
1878		dst->vd_aux = tomw(src, VD1_aux_$2);
1879		dst->vd_next = tomw(src, VD1_next_$2);
1880
1881		src_vaux = src + dst->vd_aux;
1882		/* LINTED */
1883		vaux = (Elf32_Verdaux*)((char *)dst + dst->vd_aux);
1884		for (i = 0; i < dst->vd_cnt; i++) {
1885			vaux->vda_name = toma(src_vaux, VDA1_name_$2);
1886			vaux->vda_next = toma(src_vaux, VDA1_next_$2);
1887			src_vaux += vaux->vda_next;
1888			/* LINTED */
1889			vaux = (Elf32_Verdaux *)((char *)vaux +
1890			    vaux->vda_next);
1891		}
1892		src += dst->vd_next;
1893		/* LINTED */
1894		dst = (Elf32_Verdef *)(dst->vd_next ?
1895		    (char *)dst + dst->vd_next : (char *)end);
1896	}
1897}')
1898
1899verdef_11_tom(verdef_2L11_tom,L)
1900verdef_11_tom(verdef_2M11_tom,M)
1901
1902
1903define(verneed_11_tom, `
1904static void
1905$1(Elf32_Verneed *dst, unsigned char *src, size_t cnt)
1906{
1907	/* LINTED */
1908	Elf32_Verneed	*end = (Elf32_Verneed *)((char *)dst + cnt);
1909
1910	while (dst < end) {
1911		Elf32_Vernaux *	vaux;
1912		unsigned char *	src_vaux;
1913		Elf32_Half	i;
1914		dst->vn_version = tomh(src, VN1_version_$2);
1915		dst->vn_cnt = tomh(src, VN1_cnt_$2);
1916		dst->vn_file = toma(src, VN1_file_$2);
1917		dst->vn_aux = tomw(src, VN1_aux_$2);
1918		dst->vn_next = tomw(src, VN1_next_$2);
1919
1920		src_vaux = src + dst->vn_aux;
1921		/* LINTED */
1922		vaux = (Elf32_Vernaux *)((char *)dst + dst->vn_aux);
1923		for (i = 0; i < dst->vn_cnt; i++) {
1924			vaux->vna_hash = tomw(src_vaux, VNA1_hash_$2);
1925			vaux->vna_flags = tomh(src_vaux, VNA1_flags_$2);
1926			vaux->vna_other = tomh(src_vaux, VNA1_other_$2);
1927			vaux->vna_name = toma(src_vaux, VNA1_name_$2);
1928			vaux->vna_next = tomw(src_vaux, VNA1_next_$2);
1929			src_vaux += vaux->vna_next;
1930			/* LINTED */
1931			vaux = (Elf32_Vernaux *)((char *)vaux +
1932			    vaux->vna_next);
1933		}
1934		src += dst->vn_next;
1935		/* LINTED */
1936		dst = (Elf32_Verneed *)(dst->vn_next ?
1937		    (char *)dst + dst->vn_next : (char *)end);
1938	}
1939}')
1940
1941verneed_11_tom(verneed_2L11_tom,L)
1942verneed_11_tom(verneed_2M11_tom,M)
1943