xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/xlate.m4 (revision b0fe7b8fa79924061f3bdf7f240ea116c2c0b704)
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 2007 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
692elf32_fsize(Elf_Type type, size_t count, unsigned ver)
693{
694	if (--ver >= EV_CURRENT) {
695		_elf_seterr(EREQ_VER, 0);
696		return (0);
697	}
698	if ((unsigned)type >= ELF_T_NUM) {
699		_elf_seterr(EREQ_TYPE, 0);
700		return (0);
701	}
702	return (fmsize[ver][type].s_filesz * count);
703}
704
705
706size_t
707_elf32_msize(Elf_Type type, unsigned ver)
708{
709	return (fmsize[ver - 1][type].s_memsz);
710}
711
712
713Elf_Type
714_elf32_mtype(Elf * elf, Elf32_Word shtype, unsigned ver)
715{
716	Elf32_Ehdr *	ehdr = (Elf32_Ehdr *)elf->ed_ehdr;
717
718	if (shtype < SHT_NUM)
719		return (mtype[ver - 1][shtype]);
720
721	switch (shtype) {
722	case SHT_SUNW_symsort:
723	case SHT_SUNW_tlssort:
724		return (ELF_T_WORD);
725	case SHT_SUNW_LDYNSYM:
726		return (ELF_T_SYM);
727	case SHT_SUNW_dof:
728		return (ELF_T_BYTE);
729	case SHT_SUNW_cap:
730		return (ELF_T_CAP);
731	case SHT_SUNW_SIGNATURE:
732		return (ELF_T_BYTE);
733	case SHT_SUNW_ANNOTATE:
734		return (ELF_T_BYTE);
735	case SHT_SUNW_DEBUGSTR:
736		return (ELF_T_BYTE);
737	case SHT_SUNW_DEBUG:
738		return (ELF_T_BYTE);
739	case SHT_SUNW_move:
740		/*
741		 * 32bit sparc binaries have a padded
742		 * MOVE structure.  So - return the
743		 * appropriate type.
744		 */
745		if ((ehdr->e_machine == EM_SPARC) ||
746		    (ehdr->e_machine == EM_SPARC32PLUS)) {
747			return (ELF_T_MOVEP);
748		}
749
750		return (ELF_T_MOVE);
751	case SHT_SUNW_COMDAT:
752		return (ELF_T_BYTE);
753	case SHT_SUNW_syminfo:
754		return (ELF_T_SYMINFO);
755	case SHT_SUNW_verdef:
756		return (ELF_T_VDEF);
757	case SHT_SUNW_verneed:
758		return (ELF_T_VNEED);
759	case SHT_SUNW_versym:
760		return (ELF_T_HALF);
761	};
762
763	/*
764	 * Check for the sparc specific section types
765	 * below.
766	 */
767	if (((ehdr->e_machine == EM_SPARC) ||
768	    (ehdr->e_machine == EM_SPARC32PLUS) ||
769	    (ehdr->e_machine == EM_SPARCV9)) &&
770	    (shtype == SHT_SPARC_GOTDATA))
771		return (ELF_T_BYTE);
772
773	/*
774	 * Check for the amd64 specific section types
775	 * below.
776	 */
777	if ((ehdr->e_machine == EM_AMD64) &&
778	    (shtype == SHT_AMD64_UNWIND))
779		return (ELF_T_BYTE);
780
781	/*
782	 * And the default is ELF_T_BYTE - but we should
783	 * certainly have caught any sections we know about
784	 * above.  This is for unknown sections to libelf.
785	 */
786	return (ELF_T_BYTE);
787}
788
789
790size_t
791_elf32_entsz(Elf *elf, Elf32_Word shtype, unsigned ver)
792{
793	Elf_Type	ttype;
794
795	ttype = _elf32_mtype(elf, shtype, ver);
796	return ((ttype == ELF_T_BYTE) ? 0 : fmsize[ver - 1][ttype].s_filesz);
797}
798
799
800/*
801 * XX64	This routine is also used to 'version' interactions with Elf64
802 *	applications, but there's no way to figure out if the caller is
803 *	asking Elf32 or Elf64 questions, even though it has Elf32
804 *	dependencies.  Ick.
805 */
806unsigned
807elf_version(unsigned ver)
808{
809	register unsigned	j;
810	union
811	{
812		Elf32_Word	w;
813		unsigned char	c[W_sizeof];
814	} u;
815
816
817
818	if (ver == EV_NONE)
819		return EV_CURRENT;
820	if (ver > EV_CURRENT)
821	{
822		_elf_seterr(EREQ_VER, 0);
823		return EV_NONE;
824	}
825	(void) mutex_lock(&_elf_globals_mutex);
826	if (_elf_work != EV_NONE)
827	{
828		j = _elf_work;
829		_elf_work = ver;
830		(void) mutex_unlock(&_elf_globals_mutex);
831		return j;
832	}
833	_elf_work = ver;
834
835	u.w = 0x10203;
836	/*CONSTANTCONDITION*/
837	if (~(Elf32_Word)0 == -(Elf32_Sword)1
838	&& tomw(u.c, W_L) == 0x10203)
839		_elf_encode = ELFDATA2LSB;
840	/*CONSTANTCONDITION*/
841	else if (~(Elf32_Word)0 == -(Elf32_Sword)1
842	&& tomw(u.c, W_M) == 0x10203)
843		_elf_encode = ELFDATA2MSB;
844
845	(void) mutex_unlock(&_elf_globals_mutex);
846
847	return ver;
848}
849
850
851static Elf_Data *
852xlate(Elf_Data *dst, const Elf_Data *src, unsigned encode, int tof)
853						/* tof !0 -> xlatetof */
854{
855	size_t		cnt, dsz, ssz;
856	unsigned	type;
857	unsigned	dver, sver;
858	void		(*f)();
859	unsigned	_encode;
860
861	if (dst == 0 || src == 0)
862		return (0);
863	if (--encode >= (ELFDATANUM - 1)) {
864		_elf_seterr(EREQ_ENCODE, 0);
865		return (0);
866	}
867	if ((dver = dst->d_version - 1) >= EV_CURRENT ||
868	    (sver = src->d_version - 1) >= EV_CURRENT) {
869		_elf_seterr(EREQ_VER, 0);
870		return (0);
871	}
872	if ((type = src->d_type) >= ELF_T_NUM) {
873		_elf_seterr(EREQ_TYPE, 0);
874		return (0);
875	}
876
877	if (tof) {
878		dsz = fmsize[dver][type].s_filesz;
879		ssz = fmsize[sver][type].s_memsz;
880		f = x32[dver][sver][encode][type].x_tof;
881	} else {
882		dsz = fmsize[dver][type].s_memsz;
883		ssz = fmsize[sver][type].s_filesz;
884		f = x32[dver][sver][encode][type].x_tom;
885	}
886	cnt = src->d_size / ssz;
887	if (dst->d_size < dsz * cnt) {
888		_elf_seterr(EREQ_DSZ, 0);
889		return (0);
890	}
891
892	ELFACCESSDATA(_encode, _elf_encode)
893	if ((_encode == (encode + 1)) && (dsz == ssz)) {
894		/*
895		 *	ld(1) frequently produces empty sections (eg. .dynsym,
896		 *	.dynstr, .symtab, .strtab, etc) so that the initial
897		 *	output image can be created of the correct size.  Later
898		 *	these sections are filled in with the associated data.
899		 *	So that we don't have to pre-allocate buffers for
900		 *	these segments, allow for the src destination to be 0.
901		 */
902		if (src->d_buf && src->d_buf != dst->d_buf)
903			(void) memcpy(dst->d_buf, src->d_buf, src->d_size);
904		dst->d_type = src->d_type;
905		dst->d_size = src->d_size;
906		return (dst);
907	}
908	if (cnt)
909		(*f)(dst->d_buf, src->d_buf, cnt);
910	dst->d_size = dsz * cnt;
911	dst->d_type = src->d_type;
912	return (dst);
913}
914
915
916Elf_Data *
917elf32_xlatetof(Elf_Data *dst, const Elf_Data *src, unsigned encode)
918{
919	return (xlate(dst, src, encode, 1));
920}
921
922
923Elf_Data *
924elf32_xlatetom(Elf_Data *dst, const Elf_Data *src, unsigned encode)
925{
926	return (xlate(dst, src, encode, 0));
927}
928
929
930/*
931 * xlate to file format
932 *
933 *	..._tof(name, data) -- macros
934 *
935 *	Recall that the file format must be no larger than the
936 *	memory format (equal versions).  Use "forward" copy.
937 *	All these routines require non-null, non-zero arguments.
938 */
939
940define(addr_tof, `
941static void
942$1(unsigned char *dst, Elf32_Addr *src, size_t cnt)
943{
944	Elf32_Addr	*end = src + cnt;
945
946	do {
947		tofa(dst, *src, A_$2);
948		dst += A_sizeof;
949	} while (++src < end);
950}')
951
952addr_tof(addr_2L_tof,L)
953addr_tof(addr_2M_tof,M)
954
955
956static void
957byte_to(unsigned char *dst, unsigned char *src, size_t cnt)
958{
959	if (dst != src)
960		(void) memcpy(dst, src, cnt);
961}
962
963
964define(dyn_11_tof, `
965static void
966$1(unsigned char *dst, Elf32_Dyn *src, size_t cnt)
967{
968	Elf32_Dyn	*end = src + cnt;
969
970	do {
971		tofw(dst, src->d_tag, D1_tag_$2);
972		tofo(dst, src->d_un.d_val, D1_val_$2);
973		dst += D1_sizeof;
974	} while (++src < end);
975}')
976
977dyn_11_tof(dyn_2L11_tof,L)
978dyn_11_tof(dyn_2M11_tof,M)
979
980
981define(ehdr_11_tof, `
982static void
983$1(unsigned char *dst, Elf32_Ehdr *src, size_t cnt)
984{
985	Elf32_Ehdr	*end = src + cnt;
986
987	do {
988		if (&dst[E1_ident] != src->e_ident)
989			(void) memcpy(&dst[E1_ident], src->e_ident, E1_Nident);
990		tofh(dst, src->e_type, E1_type_$2);
991		tofh(dst, src->e_machine, E1_machine_$2);
992		tofw(dst, src->e_version, E1_version_$2);
993		tofa(dst, src->e_entry, E1_entry_$2);
994		tofo(dst, src->e_phoff, E1_phoff_$2);
995		tofo(dst, src->e_shoff, E1_shoff_$2);
996		tofw(dst, src->e_flags, E1_flags_$2);
997		tofh(dst, src->e_ehsize, E1_ehsize_$2);
998		tofh(dst, src->e_phentsize, E1_phentsize_$2);
999		tofh(dst, src->e_phnum, E1_phnum_$2);
1000		tofh(dst, src->e_shentsize, E1_shentsize_$2);
1001		tofh(dst, src->e_shnum, E1_shnum_$2);
1002		tofh(dst, src->e_shstrndx, E1_shstrndx_$2);
1003		dst += E1_sizeof;
1004	} while (++src < end);
1005}')
1006
1007ehdr_11_tof(ehdr_2L11_tof,L)
1008ehdr_11_tof(ehdr_2M11_tof,M)
1009
1010
1011define(half_tof, `
1012static void
1013$1(unsigned char *dst, Elf32_Half *src, size_t cnt)
1014{
1015	Elf32_Half	*end = src + cnt;
1016
1017	do {
1018		tofh(dst, *src, H_$2);
1019		dst += H_sizeof;
1020	} while (++src < end);
1021}')
1022
1023half_tof(half_2L_tof,L)
1024half_tof(half_2M_tof,M)
1025
1026
1027define(move_11_tof, `
1028static void
1029$1(unsigned char *dst, Elf32_Move *src, size_t cnt)
1030{
1031	Elf32_Move	*end = src + cnt;
1032
1033	do {
1034		tofl(dst, src->m_value, M1_value_$2);
1035		tofw(dst, src->m_info, M1_info_$2);
1036		tofw(dst, src->m_poffset, M1_poffset_$2);
1037		tofh(dst, src->m_repeat, M1_repeat_$2);
1038		tofh(dst, src->m_stride, M1_stride_$2);
1039		dst += M1_sizeof;
1040	} while (++src < end);
1041}')
1042
1043move_11_tof(move_2L11_tof,L)
1044move_11_tof(move_2M11_tof,M)
1045
1046
1047define(movep_11_tof, `
1048static void
1049$1(unsigned char *dst, Elf32_Move *src, size_t cnt)
1050{
1051	Elf32_Move	*end = src + cnt;
1052
1053	do {
1054		tofl(dst, src->m_value, MP1_value_$2);
1055		tofw(dst, src->m_info, MP1_info_$2);
1056		tofw(dst, src->m_poffset, MP1_poffset_$2);
1057		tofh(dst, src->m_repeat, MP1_repeat_$2);
1058		tofh(dst, src->m_stride, MP1_stride_$2);
1059		dst += MP1_sizeof;
1060	} while (++src < end);
1061}')
1062
1063movep_11_tof(movep_2L11_tof,L)
1064movep_11_tof(movep_2M11_tof,M)
1065
1066
1067define(off_tof, `
1068static void
1069$1(unsigned char *dst, Elf32_Off *src, size_t cnt)
1070{
1071	Elf32_Off	*end = src + cnt;
1072
1073	do {
1074		tofo(dst, *src, O_$2);
1075		dst += O_sizeof;
1076	} while (++src < end);
1077}')
1078
1079off_tof(off_2L_tof,L)
1080off_tof(off_2M_tof,M)
1081
1082
1083define(note_11_tof, `
1084static void
1085$1(unsigned char *dst, Elf32_Nhdr *src, size_t cnt)
1086{
1087	/* LINTED */
1088	Elf32_Nhdr *	end = (Elf32_Nhdr *)((char *)src + cnt);
1089
1090	do {
1091		Elf32_Word	descsz, namesz;
1092
1093		/*
1094		 * cache size of desc & name fields - while rounding
1095		 * up their size.
1096		 */
1097		namesz = S_ROUND(src->n_namesz, sizeof (Elf32_Word));
1098		descsz = src->n_descsz;
1099
1100		/*
1101		 * Copy contents of Elf32_Nhdr
1102		 */
1103		tofw(dst, src->n_namesz, N1_namesz_$2);
1104		tofw(dst, src->n_descsz, N1_descsz_$2);
1105		tofw(dst, src->n_type, N1_type_$2);
1106
1107		/*
1108		 * Copy contents of Name field
1109		 */
1110		dst += N1_sizeof;
1111		src++;
1112		(void)memcpy(dst, src, namesz);
1113
1114		/*
1115		 * Copy contents of desc field
1116		 */
1117		dst += namesz;
1118		src = (Elf32_Nhdr *)((uintptr_t)src + namesz);
1119		(void)memcpy(dst, src, descsz);
1120		descsz = S_ROUND(descsz, sizeof (Elf32_Word));
1121		dst += descsz;
1122		src = (Elf32_Nhdr *)((uintptr_t)src + descsz);
1123	} while (src < end);
1124}')
1125
1126note_11_tof(note_2L11_tof,L)
1127note_11_tof(note_2M11_tof,M)
1128
1129
1130define(phdr_11_tof, `
1131static void
1132$1(unsigned char *dst, Elf32_Phdr *src, size_t cnt)
1133{
1134	Elf32_Phdr	*end = src + cnt;
1135
1136	do {
1137		tofw(dst, src->p_type, P1_type_$2);
1138		tofo(dst, src->p_offset, P1_offset_$2);
1139		tofa(dst, src->p_vaddr, P1_vaddr_$2);
1140		tofa(dst, src->p_paddr, P1_paddr_$2);
1141		tofw(dst, src->p_filesz, P1_filesz_$2);
1142		tofw(dst, src->p_memsz, P1_memsz_$2);
1143		tofw(dst, src->p_flags, P1_flags_$2);
1144		tofw(dst, src->p_align, P1_align_$2);
1145		dst += P1_sizeof;
1146	} while (++src < end);
1147}')
1148
1149phdr_11_tof(phdr_2L11_tof,L)
1150phdr_11_tof(phdr_2M11_tof,M)
1151
1152
1153define(rel_11_tof, `
1154static void
1155$1(unsigned char *dst, Elf32_Rel *src, size_t cnt)
1156{
1157	Elf32_Rel	*end = src + cnt;
1158
1159	do {
1160		tofa(dst, src->r_offset, R1_offset_$2);
1161		tofw(dst, src->r_info, R1_info_$2);
1162		dst += R1_sizeof;
1163	} while (++src < end);
1164}')
1165
1166rel_11_tof(rel_2L11_tof,L)
1167rel_11_tof(rel_2M11_tof,M)
1168
1169
1170define(rela_11_tof, `
1171static void
1172$1(unsigned char *dst, Elf32_Rela *src, size_t cnt)
1173{
1174	Elf32_Rela	*end = src + cnt;
1175
1176	do {
1177		tofa(dst, src->r_offset, RA1_offset_$2);
1178		tofw(dst, src->r_info, RA1_info_$2);
1179		/*CONSTANTCONDITION*/
1180		if (~(Elf32_Word)0 == -(Elf32_Sword)1) {	/* 2s comp */
1181			tofw(dst, src->r_addend, RA1_addend_$2);
1182		} else {
1183			Elf32_Word	w;
1184
1185			if (src->r_addend < 0) {
1186				w = - src->r_addend;
1187				w = ~w + 1;
1188			} else
1189				w = src->r_addend;
1190			tofw(dst, w, RA1_addend_$2);
1191		}
1192		dst += RA1_sizeof;
1193	} while (++src < end);
1194}')
1195
1196rela_11_tof(rela_2L11_tof,L)
1197rela_11_tof(rela_2M11_tof,M)
1198
1199
1200define(shdr_11_tof, `
1201static void
1202$1(unsigned char *dst, Elf32_Shdr *src, size_t cnt)
1203{
1204	Elf32_Shdr	*end = src + cnt;
1205
1206	do {
1207		tofw(dst, src->sh_name, SH1_name_$2);
1208		tofw(dst, src->sh_type, SH1_type_$2);
1209		tofw(dst, src->sh_flags, SH1_flags_$2);
1210		tofa(dst, src->sh_addr, SH1_addr_$2);
1211		tofo(dst, src->sh_offset, SH1_offset_$2);
1212		tofw(dst, src->sh_size, SH1_size_$2);
1213		tofw(dst, src->sh_link, SH1_link_$2);
1214		tofw(dst, src->sh_info, SH1_info_$2);
1215		tofw(dst, src->sh_addralign, SH1_addralign_$2);
1216		tofw(dst, src->sh_entsize, SH1_entsize_$2);
1217		dst += SH1_sizeof;
1218	} while (++src < end);
1219}')
1220
1221shdr_11_tof(shdr_2L11_tof,L)
1222shdr_11_tof(shdr_2M11_tof,M)
1223
1224
1225define(sword_tof, `
1226static void
1227$1(unsigned char *dst, Elf32_Sword *src, size_t cnt)
1228{
1229	Elf32_Sword	*end = src + cnt;
1230
1231	do {
1232		/*CONSTANTCONDITION*/
1233		if (~(Elf32_Word)0 == -(Elf32_Sword)1) {	/* 2s comp */
1234			tofw(dst, *src, W_$2);
1235		} else {
1236			Elf32_Word	w;
1237
1238			if (*src < 0) {
1239				w = - *src;
1240				w = ~w + 1;
1241			} else
1242				w = *src;
1243			tofw(dst, w, W_$2);
1244		}
1245		dst += W_sizeof;
1246	} while (++src < end);
1247}')
1248
1249sword_tof(sword_2L_tof,L)
1250sword_tof(sword_2M_tof,M)
1251
1252
1253define(cap_11_tof, `
1254static void
1255$1(unsigned char *dst, Elf32_Cap *src, size_t cnt)
1256{
1257	Elf32_Cap	*end = src + cnt;
1258
1259	do {
1260		tofw(dst, src->c_tag, C1_tag_$2);
1261		tofw(dst, src->c_un.c_val, C1_val_$2);
1262		dst += C1_sizeof;
1263	} while (++src < end);
1264}')
1265
1266cap_11_tof(cap_2L11_tof,L)
1267cap_11_tof(cap_2M11_tof,M)
1268
1269
1270define(syminfo_11_tof, `
1271static void
1272$1(unsigned char *dst, Elf32_Syminfo *src, size_t cnt)
1273{
1274	Elf32_Syminfo	*end = src + cnt;
1275
1276	do {
1277		tofh(dst, src->si_boundto, SI1_boundto_$2);
1278		tofh(dst, src->si_flags, SI1_flags_$2);
1279		dst += SI1_sizeof;
1280	} while (++src < end);
1281}')
1282
1283syminfo_11_tof(syminfo_2L11_tof,L)
1284syminfo_11_tof(syminfo_2M11_tof,M)
1285
1286
1287define(sym_11_tof, `
1288static void
1289$1(unsigned char *dst, Elf32_Sym *src, size_t cnt)
1290{
1291	Elf32_Sym	*end = src + cnt;
1292
1293	do {
1294		tofw(dst, src->st_name, ST1_name_$2);
1295		tofa(dst, src->st_value, ST1_value_$2);
1296		tofw(dst, src->st_size, ST1_size_$2);
1297		tofb(dst, src->st_info, ST1_info_$2);
1298		tofb(dst, src->st_other, ST1_other_$2);
1299		tofh(dst, src->st_shndx, ST1_shndx_$2);
1300		dst += ST1_sizeof;
1301	} while (++src < end);
1302}')
1303
1304sym_11_tof(sym_2L11_tof,L)
1305sym_11_tof(sym_2M11_tof,M)
1306
1307
1308define(word_tof, `
1309static void
1310$1(unsigned char *dst, Elf32_Word *src, size_t cnt)
1311{
1312	Elf32_Word	*end = src + cnt;
1313
1314	do {
1315		tofw(dst, *src, W_$2);
1316		dst += W_sizeof;
1317	} while (++src < end);
1318}')
1319
1320word_tof(word_2L_tof,L)
1321word_tof(word_2M_tof,M)
1322
1323
1324define(verdef_11_tof, `
1325static void
1326$1(unsigned char *dst, Elf32_Verdef *src, size_t cnt)
1327{
1328	/* LINTED */
1329	Elf32_Verdef	*end = (Elf32_Verdef *)((char *)src + cnt);
1330
1331	do {
1332		Elf32_Verdef	*next_verdef;
1333		Elf32_Verdaux	*vaux;
1334		Elf32_Half	i;
1335		unsigned char	*vaux_dst;
1336		unsigned char	*dst_next;
1337
1338		/* LINTED */
1339		next_verdef = (Elf32_Verdef *)(src->vd_next ?
1340		    (char *)src + src->vd_next : (char *)end);
1341		dst_next = dst + src->vd_next;
1342
1343		/* LINTED */
1344		vaux = (Elf32_Verdaux *)((char *)src + src->vd_aux);
1345		vaux_dst = dst + src->vd_aux;
1346
1347		/*
1348		 * Convert auxilary structures
1349		 */
1350		for (i = 0; i < src->vd_cnt; i++) {
1351			Elf32_Verdaux	*vaux_next;
1352			unsigned char	*vaux_dst_next;
1353
1354			/*
1355			 * because our source and destination can be
1356			 * the same place we need to figure out the next
1357			 * location now.
1358			 */
1359			/* LINTED */
1360			vaux_next = (Elf32_Verdaux *)((char *)vaux +
1361			    vaux->vda_next);
1362			vaux_dst_next = vaux_dst + vaux->vda_next;
1363
1364			tofa(vaux_dst, vaux->vda_name, VDA1_name_$2);
1365			tofw(vaux_dst, vaux->vda_next, VDA1_next_$2);
1366			vaux_dst = vaux_dst_next;
1367			vaux = vaux_next;
1368		}
1369
1370		/*
1371		 * Convert Elf32_Verdef structure.
1372		 */
1373		tofh(dst, src->vd_version, VD1_version_$2);
1374		tofh(dst, src->vd_flags, VD1_flags_$2);
1375		tofh(dst, src->vd_ndx, VD1_ndx_$2);
1376		tofh(dst, src->vd_cnt, VD1_cnt_$2);
1377		tofw(dst, src->vd_hash, VD1_hash_$2);
1378		tofw(dst, src->vd_aux, VD1_aux_$2);
1379		tofw(dst, src->vd_next, VD1_next_$2);
1380		src = next_verdef;
1381		dst = dst_next;
1382	} while (src < end);
1383}')
1384
1385verdef_11_tof(verdef_2L11_tof, L)
1386verdef_11_tof(verdef_2M11_tof, M)
1387
1388define(verneed_11_tof, `
1389static void
1390$1(unsigned char *dst, Elf32_Verneed *src, size_t cnt)
1391{
1392	/* LINTED */
1393	Elf32_Verneed	*end = (Elf32_Verneed *)((char *)src + cnt);
1394
1395	do {
1396		Elf32_Verneed	*next_verneed;
1397		Elf32_Vernaux	*vaux;
1398		Elf32_Half	i;
1399		unsigned char	*vaux_dst;
1400		unsigned char	*dst_next;
1401
1402		/* LINTED */
1403		next_verneed = (Elf32_Verneed *)(src->vn_next ?
1404		    (char *)src + src->vn_next : (char *)end);
1405		dst_next = dst + src->vn_next;
1406
1407		/* LINTED */
1408		vaux = (Elf32_Vernaux *)((char *)src + src->vn_aux);
1409		vaux_dst = dst + src->vn_aux;
1410
1411		/*
1412		 * Convert auxilary structures first
1413		 */
1414		for (i = 0; i < src->vn_cnt; i++) {
1415			Elf32_Vernaux *	vaux_next;
1416			unsigned char *	vaux_dst_next;
1417
1418			/*
1419			 * because our source and destination can be
1420			 * the same place we need to figure out the
1421			 * next location now.
1422			 */
1423			/* LINTED */
1424			vaux_next = (Elf32_Vernaux *)((char *)vaux +
1425			    vaux->vna_next);
1426			vaux_dst_next = vaux_dst + vaux->vna_next;
1427
1428			tofw(vaux_dst, vaux->vna_hash, VNA1_hash_$2);
1429			tofh(vaux_dst, vaux->vna_flags, VNA1_flags_$2);
1430			tofh(vaux_dst, vaux->vna_other, VNA1_other_$2);
1431			tofa(vaux_dst, vaux->vna_name, VNA1_name_$2);
1432			tofw(vaux_dst, vaux->vna_next, VNA1_next_$2);
1433			vaux_dst = vaux_dst_next;
1434			vaux = vaux_next;
1435		}
1436		/*
1437		 * Convert Elf32_Verneed structure.
1438		 */
1439		tofh(dst, src->vn_version, VN1_version_$2);
1440		tofh(dst, src->vn_cnt, VN1_cnt_$2);
1441		tofa(dst, src->vn_file, VN1_file_$2);
1442		tofw(dst, src->vn_aux, VN1_aux_$2);
1443		tofw(dst, src->vn_next, VN1_next_$2);
1444		src = next_verneed;
1445		dst = dst_next;
1446	} while (src < end);
1447}')
1448
1449verneed_11_tof(verneed_2L11_tof, L)
1450verneed_11_tof(verneed_2M11_tof, M)
1451
1452
1453/* xlate to memory format
1454 *
1455 *	..._tom(name, data) -- macros
1456 *
1457 *	Recall that the memory format may be larger than the
1458 *	file format (equal versions).  Use "backward" copy.
1459 *	All these routines require non-null, non-zero arguments.
1460 */
1461
1462
1463define(addr_tom, `
1464static void
1465$1(Elf32_Addr *dst, unsigned char *src, size_t cnt)
1466{
1467	Elf32_Addr	*end = dst;
1468
1469	dst += cnt;
1470	src += cnt * A_sizeof;
1471	while (dst-- > end) {
1472		src -= A_sizeof;
1473		*dst = toma(src, A_$2);
1474	}
1475}')
1476
1477addr_tom(addr_2L_tom,L)
1478addr_tom(addr_2M_tom,M)
1479
1480
1481define(dyn_11_tom, `
1482static void
1483$1(Elf32_Dyn *dst, unsigned char *src, size_t cnt)
1484{
1485	Elf32_Dyn	*end = dst + cnt;
1486
1487	do {
1488		dst->d_tag = tomw(src, D1_tag_$2);
1489		dst->d_un.d_val = tomw(src, D1_val_$2);
1490		src += D1_sizeof;
1491	} while (++dst < end);
1492}')
1493
1494dyn_11_tom(dyn_2L11_tom,L)
1495dyn_11_tom(dyn_2M11_tom,M)
1496
1497
1498define(ehdr_11_tom, `
1499static void
1500$1(Elf32_Ehdr *dst, unsigned char *src, size_t cnt)
1501{
1502	Elf32_Ehdr	*end = dst;
1503
1504	dst += cnt;
1505	src += cnt * E1_sizeof;
1506	while (dst-- > end) {
1507		src -= E1_sizeof;
1508		dst->e_shstrndx = tomh(src, E1_shstrndx_$2);
1509		dst->e_shnum = tomh(src, E1_shnum_$2);
1510		dst->e_shentsize = tomh(src, E1_shentsize_$2);
1511		dst->e_phnum = tomh(src, E1_phnum_$2);
1512		dst->e_phentsize = tomh(src, E1_phentsize_$2);
1513		dst->e_ehsize = tomh(src, E1_ehsize_$2);
1514		dst->e_flags = tomw(src, E1_flags_$2);
1515		dst->e_shoff = tomo(src, E1_shoff_$2);
1516		dst->e_phoff = tomo(src, E1_phoff_$2);
1517		dst->e_entry = toma(src, E1_entry_$2);
1518		dst->e_version = tomw(src, E1_version_$2);
1519		dst->e_machine = tomh(src, E1_machine_$2);
1520		dst->e_type = tomh(src, E1_type_$2);
1521		if (dst->e_ident != &src[E1_ident])
1522			(void) memcpy(dst->e_ident, &src[E1_ident], E1_Nident);
1523	}
1524}')
1525
1526ehdr_11_tom(ehdr_2L11_tom,L)
1527ehdr_11_tom(ehdr_2M11_tom,M)
1528
1529
1530define(half_tom, `
1531static void
1532$1(Elf32_Half *dst, unsigned char *src, size_t cnt)
1533{
1534	Elf32_Half	*end = dst;
1535
1536	dst += cnt;
1537	src += cnt * H_sizeof;
1538	while (dst-- > end) {
1539		src -= H_sizeof;
1540		*dst = tomh(src, H_$2);
1541	}
1542}')
1543
1544half_tom(half_2L_tom,L)
1545half_tom(half_2M_tom,M)
1546
1547
1548define(move_11_tom, `
1549static void
1550$1(Elf32_Move *dst, unsigned char *src, size_t cnt)
1551{
1552	Elf32_Move	*end = dst + cnt;
1553
1554	do {
1555		dst->m_value = toml(src, M1_value_$2);
1556		dst->m_info = tomw(src, M1_info_$2);
1557		dst->m_poffset = tomw(src, M1_poffset_$2);
1558		dst->m_repeat = tomh(src, M1_repeat_$2);
1559		dst->m_stride = tomh(src, M1_stride_$2);
1560		src += M1_sizeof;
1561	} while (++dst < end);
1562}')
1563
1564move_11_tom(move_2L11_tom,L)
1565move_11_tom(move_2M11_tom,M)
1566
1567
1568define(movep_11_tom, `
1569static void
1570$1(Elf32_Move *dst, unsigned char *src, size_t cnt)
1571{
1572	Elf32_Move		*end = dst + cnt;
1573
1574	do
1575	{
1576		dst->m_value = toml(src, MP1_value_$2);
1577		dst->m_info = tomw(src, MP1_info_$2);
1578		dst->m_poffset = tomw(src, MP1_poffset_$2);
1579		dst->m_repeat = tomh(src, MP1_repeat_$2);
1580		dst->m_stride = tomh(src, MP1_stride_$2);
1581		src += MP1_sizeof;
1582	} while (++dst < end);
1583}')
1584
1585movep_11_tom(movep_2L11_tom,L)
1586movep_11_tom(movep_2M11_tom,M)
1587
1588
1589define(note_11_tom, `
1590static void
1591$1(Elf32_Nhdr *dst, unsigned char *src, size_t cnt)
1592{
1593	/* LINTED */
1594	Elf32_Nhdr	*end = (Elf32_Nhdr *)((char *)dst + cnt);
1595
1596	while (dst < end) {
1597		Elf32_Nhdr *	nhdr;
1598		unsigned char *	namestr;
1599		void *		desc;
1600		Elf32_Word	field_sz;
1601
1602		dst->n_namesz = tomw(src, N1_namesz_$2);
1603		dst->n_descsz = tomw(src, N1_descsz_$2);
1604		dst->n_type = tomw(src, N1_type_$2);
1605		nhdr = dst;
1606		/* LINTED */
1607		dst = (Elf32_Nhdr *)((char *)dst + sizeof (Elf32_Nhdr));
1608		namestr = src + N1_sizeof;
1609		field_sz = S_ROUND(nhdr->n_namesz, sizeof (Elf32_Word));
1610		(void)memcpy((void *)dst, namestr, field_sz);
1611		desc = namestr + field_sz;
1612		/* LINTED */
1613		dst = (Elf32_Nhdr *)((char *)dst + field_sz);
1614		field_sz = nhdr->n_descsz;
1615		(void)memcpy(dst, desc, field_sz);
1616		field_sz = S_ROUND(field_sz, sizeof (Elf32_Word));
1617		/* LINTED */
1618		dst = (Elf32_Nhdr *)((char *)dst + field_sz);
1619		src = (unsigned char *)desc + field_sz;
1620	}
1621}')
1622
1623note_11_tom(note_2L11_tom,L)
1624note_11_tom(note_2M11_tom,M)
1625
1626
1627define(off_tom, `
1628static void
1629$1(Elf32_Off *dst, unsigned char *src, size_t cnt)
1630{
1631	Elf32_Off	*end = dst;
1632
1633	dst += cnt;
1634	src += cnt * O_sizeof;
1635	while (dst-- > end) {
1636		src -= O_sizeof;
1637		*dst = tomo(src, O_$2);
1638	}
1639}')
1640
1641off_tom(off_2L_tom,L)
1642off_tom(off_2M_tom,M)
1643
1644
1645define(phdr_11_tom, `
1646static void
1647$1(Elf32_Phdr *dst, unsigned char *src, size_t cnt)
1648{
1649	Elf32_Phdr	*end = dst;
1650
1651	dst += cnt;
1652	src += cnt * P1_sizeof;
1653	while (dst-- > end) {
1654		src -= P1_sizeof;
1655		dst->p_align = tomw(src, P1_align_$2);
1656		dst->p_flags = tomw(src, P1_flags_$2);
1657		dst->p_memsz = tomw(src, P1_memsz_$2);
1658		dst->p_filesz = tomw(src, P1_filesz_$2);
1659		dst->p_paddr = toma(src, P1_paddr_$2);
1660		dst->p_vaddr = toma(src, P1_vaddr_$2);
1661		dst->p_offset = tomo(src, P1_offset_$2);
1662		dst->p_type = tomw(src, P1_type_$2);
1663	}
1664}')
1665
1666phdr_11_tom(phdr_2L11_tom,L)
1667phdr_11_tom(phdr_2M11_tom,M)
1668
1669
1670define(rel_11_tom, `
1671static void
1672$1(Elf32_Rel *dst, unsigned char *src, size_t cnt)
1673{
1674	Elf32_Rel	*end = dst;
1675
1676	dst += cnt;
1677	src += cnt * R1_sizeof;
1678	while (dst-- > end) {
1679		src -= R1_sizeof;
1680		dst->r_info = tomw(src, R1_info_$2);
1681		dst->r_offset = toma(src, R1_offset_$2);
1682	}
1683}')
1684
1685rel_11_tom(rel_2L11_tom,L)
1686rel_11_tom(rel_2M11_tom,M)
1687
1688
1689define(rela_11_tom, `
1690static void
1691$1(Elf32_Rela *dst, unsigned char *src, size_t cnt)
1692{
1693	Elf32_Rela	*end = dst;
1694
1695	dst += cnt;
1696	src += cnt * RA1_sizeof;
1697	while (dst-- > end) {
1698		src -= RA1_sizeof;
1699		/*CONSTANTCONDITION*/
1700		if (~(Elf32_Word)0 == -(Elf32_Sword)1 &&	/* 2s comp */
1701		    ~(~(Elf32_Word)0 >> 1) == HI32) {
1702			dst->r_addend = tomw(src, RA1_addend_$2);
1703		} else {
1704			union {
1705				Elf32_Word w;
1706				Elf32_Sword sw;
1707			} u;
1708
1709			if ((u.w = tomw(src, RA1_addend_$2)) & HI32) {
1710				u.w |= ~(Elf32_Word)LO31;
1711				u.w = ~u.w + 1;
1712				u.sw = -u.w;
1713			}
1714			dst->r_addend = u.sw;
1715		}
1716		dst->r_info = tomw(src, RA1_info_$2);
1717		dst->r_offset = toma(src, RA1_offset_$2);
1718	}
1719}')
1720
1721rela_11_tom(rela_2L11_tom,L)
1722rela_11_tom(rela_2M11_tom,M)
1723
1724
1725define(shdr_11_tom, `
1726static void
1727$1(Elf32_Shdr *dst, unsigned char *src, size_t cnt)
1728{
1729	Elf32_Shdr	*end = dst;
1730
1731	dst += cnt;
1732	src += cnt * SH1_sizeof;
1733	while (dst-- > end) {
1734		src -= SH1_sizeof;
1735		dst->sh_entsize = tomw(src, SH1_entsize_$2);
1736		dst->sh_addralign = tomw(src, SH1_addralign_$2);
1737		dst->sh_info = tomw(src, SH1_info_$2);
1738		dst->sh_link = tomw(src, SH1_link_$2);
1739		dst->sh_size = tomw(src, SH1_size_$2);
1740		dst->sh_offset = tomo(src, SH1_offset_$2);
1741		dst->sh_addr = toma(src, SH1_addr_$2);
1742		dst->sh_flags = tomw(src, SH1_flags_$2);
1743		dst->sh_type = tomw(src, SH1_type_$2);
1744		dst->sh_name = tomw(src, SH1_name_$2);
1745	}
1746}')
1747
1748shdr_11_tom(shdr_2L11_tom,L)
1749shdr_11_tom(shdr_2M11_tom,M)
1750
1751
1752
1753define(sword_tom, `
1754static void
1755$1(Elf32_Sword *dst, unsigned char *src, size_t cnt)
1756{
1757	Elf32_Sword	*end = dst;
1758
1759	dst += cnt;
1760	src += cnt * W_sizeof;
1761	while (dst-- > end) {
1762		src -= W_sizeof;
1763		/*CONSTANTCONDITION*/
1764		if (~(Elf32_Word)0 == -(Elf32_Sword)1 &&	/* 2s comp */
1765		    ~(~(Elf32_Word)0 >> 1) == HI32) {
1766			*dst = tomw(src, W_$2);
1767		} else {
1768			union {
1769				Elf32_Word w;
1770				Elf32_Sword sw;
1771			} u;
1772
1773			if ((u.w = tomw(src, W_$2)) & HI32) {
1774				u.w |= ~(Elf32_Word)LO31;
1775				u.w = ~u.w + 1;
1776				u.sw = -u.w;
1777			}
1778			*dst = u.sw;
1779		}
1780	}
1781}')
1782
1783sword_tom(sword_2L_tom,L)
1784sword_tom(sword_2M_tom,M)
1785
1786
1787define(cap_11_tom, `
1788static void
1789$1(Elf32_Cap *dst, unsigned char *src, size_t cnt)
1790{
1791	Elf32_Cap	*end = dst + cnt;
1792
1793	do {
1794		dst->c_tag = tomw(src, C1_tag_$2);
1795		dst->c_un.c_val = tomw(src, C1_val_$2);
1796		src += C1_sizeof;
1797	} while (++dst < end);
1798}')
1799
1800cap_11_tom(cap_2L11_tom,L)
1801cap_11_tom(cap_2M11_tom,M)
1802
1803
1804define(syminfo_11_tom, `
1805static void
1806$1(Elf32_Syminfo *dst, unsigned char *src, size_t cnt)
1807{
1808	Elf32_Syminfo	*end = dst;
1809
1810	dst += cnt;
1811	src += cnt * SI1_sizeof;
1812	while (dst-- > end) {
1813		src -= SI1_sizeof;
1814		dst->si_boundto = tomh(src, SI1_boundto_$2);
1815		dst->si_flags = tomh(src, SI1_flags_$2);
1816	}
1817}')
1818
1819syminfo_11_tom(syminfo_2L11_tom,L)
1820syminfo_11_tom(syminfo_2M11_tom,M)
1821
1822
1823define(sym_11_tom, `
1824static void
1825$1(Elf32_Sym *dst, unsigned char *src, size_t cnt)
1826{
1827	Elf32_Sym	*end = dst;
1828
1829	dst += cnt;
1830	src += cnt * ST1_sizeof;
1831	while (dst-- > end) {
1832		src -= ST1_sizeof;
1833		dst->st_shndx = tomh(src, ST1_shndx_$2);
1834		dst->st_other = tomb(src, ST1_other_$2);
1835		dst->st_info = tomb(src, ST1_info_$2);
1836		dst->st_size = tomw(src, ST1_size_$2);
1837		dst->st_value = toma(src, ST1_value_$2);
1838		dst->st_name = tomw(src, ST1_name_$2);
1839	}
1840}')
1841
1842sym_11_tom(sym_2L11_tom,L)
1843sym_11_tom(sym_2M11_tom,M)
1844
1845
1846define(word_tom, `
1847static void
1848$1(Elf32_Word *dst, unsigned char *src, size_t cnt)
1849{
1850	Elf32_Word	*end = dst;
1851
1852	dst += cnt;
1853	src += cnt * W_sizeof;
1854	while (dst-- > end) {
1855		src -= W_sizeof;
1856		*dst = tomw(src, W_$2);
1857	}
1858}')
1859
1860word_tom(word_2L_tom,L)
1861word_tom(word_2M_tom,M)
1862
1863
1864define(verdef_11_tom, `
1865static void
1866$1(Elf32_Verdef *dst, unsigned char *src, size_t cnt)
1867{
1868	/* LINTED */
1869	Elf32_Verdef	*end = (Elf32_Verdef *)((char *)dst + cnt);
1870
1871	while (dst < end) {
1872		Elf32_Verdaux	*vaux;
1873		unsigned char	*src_vaux;
1874		Elf32_Half	i;
1875
1876		dst->vd_version = tomh(src, VD1_version_$2);
1877		dst->vd_flags = tomh(src, VD1_flags_$2);
1878		dst->vd_ndx = tomh(src, VD1_ndx_$2);
1879		dst->vd_cnt = tomh(src, VD1_cnt_$2);
1880		dst->vd_hash = tomw(src, VD1_hash_$2);
1881		dst->vd_aux = tomw(src, VD1_aux_$2);
1882		dst->vd_next = tomw(src, VD1_next_$2);
1883
1884		src_vaux = src + dst->vd_aux;
1885		/* LINTED */
1886		vaux = (Elf32_Verdaux*)((char *)dst + dst->vd_aux);
1887		for (i = 0; i < dst->vd_cnt; i++) {
1888			vaux->vda_name = toma(src_vaux, VDA1_name_$2);
1889			vaux->vda_next = toma(src_vaux, VDA1_next_$2);
1890			src_vaux += vaux->vda_next;
1891			/* LINTED */
1892			vaux = (Elf32_Verdaux *)((char *)vaux +
1893			    vaux->vda_next);
1894		}
1895		src += dst->vd_next;
1896		/* LINTED */
1897		dst = (Elf32_Verdef *)(dst->vd_next ?
1898		    (char *)dst + dst->vd_next : (char *)end);
1899	}
1900}')
1901
1902verdef_11_tom(verdef_2L11_tom,L)
1903verdef_11_tom(verdef_2M11_tom,M)
1904
1905
1906define(verneed_11_tom, `
1907static void
1908$1(Elf32_Verneed *dst, unsigned char *src, size_t cnt)
1909{
1910	/* LINTED */
1911	Elf32_Verneed	*end = (Elf32_Verneed *)((char *)dst + cnt);
1912
1913	while (dst < end) {
1914		Elf32_Vernaux *	vaux;
1915		unsigned char *	src_vaux;
1916		Elf32_Half	i;
1917		dst->vn_version = tomh(src, VN1_version_$2);
1918		dst->vn_cnt = tomh(src, VN1_cnt_$2);
1919		dst->vn_file = toma(src, VN1_file_$2);
1920		dst->vn_aux = tomw(src, VN1_aux_$2);
1921		dst->vn_next = tomw(src, VN1_next_$2);
1922
1923		src_vaux = src + dst->vn_aux;
1924		/* LINTED */
1925		vaux = (Elf32_Vernaux *)((char *)dst + dst->vn_aux);
1926		for (i = 0; i < dst->vn_cnt; i++) {
1927			vaux->vna_hash = tomw(src_vaux, VNA1_hash_$2);
1928			vaux->vna_flags = tomh(src_vaux, VNA1_flags_$2);
1929			vaux->vna_other = tomh(src_vaux, VNA1_other_$2);
1930			vaux->vna_name = toma(src_vaux, VNA1_name_$2);
1931			vaux->vna_next = tomw(src_vaux, VNA1_next_$2);
1932			src_vaux += vaux->vna_next;
1933			/* LINTED */
1934			vaux = (Elf32_Vernaux *)((char *)vaux +
1935			    vaux->vna_next);
1936		}
1937		src += dst->vn_next;
1938		/* LINTED */
1939		dst = (Elf32_Verneed *)(dst->vn_next ?
1940		    (char *)dst + dst->vn_next : (char *)end);
1941	}
1942}')
1943
1944verneed_11_tom(verneed_2L11_tom,L)
1945verneed_11_tom(verneed_2M11_tom,M)
1946