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