xref: /illumos-gate/usr/src/cmd/sgs/include/_machelf.h (revision 08278a5e91755ccdb5850c19d21d42fb2e16b50e)
1ba2be530Sab196087 /*
2ba2be530Sab196087  * CDDL HEADER START
3ba2be530Sab196087  *
4ba2be530Sab196087  * The contents of this file are subject to the terms of the
5ba2be530Sab196087  * Common Development and Distribution License (the "License").
6ba2be530Sab196087  * You may not use this file except in compliance with the License.
7ba2be530Sab196087  *
8ba2be530Sab196087  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9ba2be530Sab196087  * or http://www.opensolaris.org/os/licensing.
10ba2be530Sab196087  * See the License for the specific language governing permissions
11ba2be530Sab196087  * and limitations under the License.
12ba2be530Sab196087  *
13ba2be530Sab196087  * When distributing Covered Code, include this CDDL HEADER in each
14ba2be530Sab196087  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15ba2be530Sab196087  * If applicable, add the following below this CDDL HEADER, with the
16ba2be530Sab196087  * fields enclosed by brackets "[]" replaced with your own identifying
17ba2be530Sab196087  * information: Portions Copyright [yyyy] [name of copyright owner]
18ba2be530Sab196087  *
19ba2be530Sab196087  * CDDL HEADER END
20ba2be530Sab196087  */
21ba2be530Sab196087 
22ba2be530Sab196087 /*
23*08278a5eSRod Evans  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24ba2be530Sab196087  * Use is subject to license terms.
25ba2be530Sab196087  *
26ba2be530Sab196087  * Wrapper around the <sys/machelf.h> header that adds
27ba2be530Sab196087  * definitions used by SGS.
28ba2be530Sab196087  */
29ba2be530Sab196087 
30ba2be530Sab196087 #ifndef	_MACHELF_H
31ba2be530Sab196087 #define	_MACHELF_H
32ba2be530Sab196087 
33ba2be530Sab196087 #include <sys/machelf.h>
34ba2be530Sab196087 #include <string.h>		/* memcpy() */
35ba2be530Sab196087 
36ba2be530Sab196087 /*
37ba2be530Sab196087  * Make machine class dependent functions transparent to the common code
38ba2be530Sab196087  */
39ba2be530Sab196087 
40ba2be530Sab196087 /*
41ba2be530Sab196087  * Note on ELF_R_TYPE: 64-bit sparc relocations require the use of
42ba2be530Sab196087  * ELF64_R_TYPE_ID instead of the ELF64_R_TYPE macro used for all
43ba2be530Sab196087  * other platforms. So our ELF_R_TYPE macro requires the caller to
44ba2be530Sab196087  * supply the machine type.
45ba2be530Sab196087  */
46ba2be530Sab196087 
47ba2be530Sab196087 
48ba2be530Sab196087 #if	defined(_ELF64)
49ba2be530Sab196087 #define	ELF_R_TYPE(_info, _mach)  \
50ba2be530Sab196087 	(((_mach) == EM_SPARCV9) ? ELF64_R_TYPE_ID(_info) : ELF64_R_TYPE(_info))
51ba2be530Sab196087 #define	ELF_R_INFO			ELF64_R_INFO
52ba2be530Sab196087 #define	ELF_R_SYM			ELF64_R_SYM
53ba2be530Sab196087 #define	ELF_R_TYPE_DATA(x)		ELF64_R_TYPE_DATA(x)
54ba2be530Sab196087 #define	ELF_R_TYPE_INFO(xoff, type)	ELF64_R_TYPE_INFO(xoff, type)
55ba2be530Sab196087 #define	ELF_ST_BIND			ELF64_ST_BIND
56ba2be530Sab196087 #define	ELF_ST_TYPE			ELF64_ST_TYPE
57ba2be530Sab196087 #define	ELF_ST_INFO			ELF64_ST_INFO
58ba2be530Sab196087 #define	ELF_ST_VISIBILITY		ELF64_ST_VISIBILITY
59ba2be530Sab196087 #define	ELF_M_SYM			ELF64_M_SYM
60ba2be530Sab196087 #define	ELF_M_SIZE			ELF64_M_SIZE
61ba2be530Sab196087 #define	ELF_M_INFO			ELF64_M_INFO
62*08278a5eSRod Evans #define	ELF_C_SYM			ELF64_C_SYM
63*08278a5eSRod Evans #define	ELF_C_GROUP			ELF64_C_GROUP
64*08278a5eSRod Evans #define	ELF_C_INFO			ELF64_C_INFO
65ba2be530Sab196087 #define	elf_checksum			elf64_checksum
66ba2be530Sab196087 #define	elf_fsize			elf64_fsize
67ba2be530Sab196087 #define	elf_getehdr			elf64_getehdr
68ba2be530Sab196087 #define	elf_getphdr			elf64_getphdr
69ba2be530Sab196087 #define	elf_newehdr			elf64_newehdr
70ba2be530Sab196087 #define	elf_newphdr			elf64_newphdr
71ba2be530Sab196087 #define	elf_getshdr			elf64_getshdr
72ba2be530Sab196087 #define	elf_xlatetof			elf64_xlatetof
73ba2be530Sab196087 #define	elf_xlatetom			elf64_xlatetom
74ba2be530Sab196087 #else /* _ELF64 */
75ba2be530Sab196087 #define	ELF_R_TYPE(_info, _mach)	ELF32_R_TYPE(_info)
76ba2be530Sab196087 #define	ELF_R_INFO			ELF32_R_INFO
77ba2be530Sab196087 #define	ELF_R_SYM			ELF32_R_SYM
78ba2be530Sab196087 /* Elf64 can hide extra offset in r_info */
79ba2be530Sab196087 #define	ELF_R_TYPE_DATA(x)		(0)
80ba2be530Sab196087 #define	ELF_R_TYPE_INFO(xoff, type)	(type)
81ba2be530Sab196087 #define	ELF_ST_BIND			ELF32_ST_BIND
82ba2be530Sab196087 #define	ELF_ST_TYPE			ELF32_ST_TYPE
83ba2be530Sab196087 #define	ELF_ST_INFO			ELF32_ST_INFO
84ba2be530Sab196087 #define	ELF_ST_VISIBILITY		ELF32_ST_VISIBILITY
85ba2be530Sab196087 #define	ELF_M_SYM			ELF32_M_SYM
86ba2be530Sab196087 #define	ELF_M_SIZE			ELF32_M_SIZE
87ba2be530Sab196087 #define	ELF_M_INFO			ELF32_M_INFO
88*08278a5eSRod Evans #define	ELF_C_SYM			ELF32_C_SYM
89*08278a5eSRod Evans #define	ELF_C_GROUP			ELF32_C_GROUP
90*08278a5eSRod Evans #define	ELF_C_INFO			ELF32_C_INFO
91ba2be530Sab196087 #define	elf_checksum			elf32_checksum
92ba2be530Sab196087 #define	elf_fsize			elf32_fsize
93ba2be530Sab196087 #define	elf_getehdr			elf32_getehdr
94ba2be530Sab196087 #define	elf_getphdr			elf32_getphdr
95ba2be530Sab196087 #define	elf_newehdr			elf32_newehdr
96ba2be530Sab196087 #define	elf_newphdr			elf32_newphdr
97ba2be530Sab196087 #define	elf_getshdr			elf32_getshdr
98ba2be530Sab196087 #define	elf_xlatetof			elf32_xlatetof
99ba2be530Sab196087 #define	elf_xlatetom			elf32_xlatetom
100ba2be530Sab196087 #endif	/* _ELF32 */
101ba2be530Sab196087 
102ba2be530Sab196087 
103ba2be530Sab196087 /*
104ba2be530Sab196087  * Macros for swapping bytes. The type of the argument must
105ba2be530Sab196087  * match the type given in the macro name.
106ba2be530Sab196087  */
107ba2be530Sab196087 #define	BSWAP_HALF(_half) \
108ba2be530Sab196087 	(((_half) << 8) | ((_half) >> 8))
109ba2be530Sab196087 
110ba2be530Sab196087 #define	BSWAP_WORD(_word) \
111ba2be530Sab196087 	((((_word) << 24) | (((_word) & 0xff00) << 8) | \
112ba2be530Sab196087 	(((_word) >> 8) & 0xff00) | ((_word) >> 24)))
113ba2be530Sab196087 
114c6c9aed4Sab196087 #define	BSWAP_LWORD(_lword) \
115c6c9aed4Sab196087 	(((_lword) << 56) | \
116c6c9aed4Sab196087 	(((_lword) & 0x0000ff00) << 40) | \
117c6c9aed4Sab196087 	(((_lword) & 0x00ff0000) << 24) | \
118c6c9aed4Sab196087 	(((_lword) & 0xff000000) << 8) | \
119c6c9aed4Sab196087 	(((_lword) >> 8)  & 0xff000000) | \
120c6c9aed4Sab196087 	(((_lword) >> 24) & 0x00ff0000) | \
121c6c9aed4Sab196087 	(((_lword) >> 40) & 0x0000ff00) | \
122c6c9aed4Sab196087 	((_lword) >> 56))	/* Lword is unsigned - 0 bits enter from left */
123c6c9aed4Sab196087 
124c6c9aed4Sab196087 
125ba2be530Sab196087 #if	defined(_ELF64)
126c6c9aed4Sab196087 #define	BSWAP_XWORD(_xword) BSWAP_LWORD(_xword)
127ba2be530Sab196087 #else
128ba2be530Sab196087 #define	BSWAP_XWORD(_xword) BSWAP_WORD(_xword)
129ba2be530Sab196087 #endif
130ba2be530Sab196087 
131ba2be530Sab196087 /*
132ba2be530Sab196087  * Macros for assigning Half/Word/Xword items from one location to
133ba2be530Sab196087  * another that are safe no matter what the data alignment rules of the
134ba2be530Sab196087  * running platform are. Variants exist to swap the data byteorder
135ba2be530Sab196087  * at the same time, or not.
136ba2be530Sab196087  *
137ba2be530Sab196087  * These macros are useful for code that accesses data that is aligned
138ba2be530Sab196087  * for a different system architecture, as occurs in cross linking.
139ba2be530Sab196087  *
140ba2be530Sab196087  * All of these macros assume the arguments are passed as pointers to
141ba2be530Sab196087  * bytes (signed or unsigned).
142ba2be530Sab196087  */
143ba2be530Sab196087 
144ba2be530Sab196087 #define	UL_ASSIGN_HALF(_dst, _src) (void) \
145ba2be530Sab196087 	((_dst)[0] = (_src)[0],	(_dst)[1] = (_src)[1])
146ba2be530Sab196087 #define	UL_ASSIGN_WORD(_dst, _src) (void) \
147ba2be530Sab196087 	((_dst)[0] = (_src)[0],	(_dst)[1] = (_src)[1], \
148ba2be530Sab196087 	(_dst)[2] = (_src)[2], 	(_dst)[3] = (_src)[3])
149c6c9aed4Sab196087 #define	UL_ASSIGN_LWORD(_dst, _src) (void) memcpy(_dst, (_src), sizeof (Lword))
150ba2be530Sab196087 #if	defined(_ELF64)
151c6c9aed4Sab196087 #define	UL_ASSIGN_XWORD(_dst, _src) UL_ASSIGN_LWORD(_dst, _src)
152ba2be530Sab196087 #else
153ba2be530Sab196087 #define	UL_ASSIGN_XWORD(_xword) UL_ASSIGN_WORD(_xword)
154ba2be530Sab196087 #endif
155ba2be530Sab196087 
156ba2be530Sab196087 #define	UL_ASSIGN_BSWAP_HALF(_dst, _src) (void) \
157ba2be530Sab196087 	((_dst)[0] = (_src)[1],	(_dst)[1] = (_src)[0])
158ba2be530Sab196087 #define	UL_ASSIGN_BSWAP_WORD(_dst, _src) (void) \
159ba2be530Sab196087 	((_dst)[0] = (_src)[3],	(_dst)[1] = (_src)[2], \
160ba2be530Sab196087 	(_dst)[2] = (_src)[1],	(_dst)[3] = (_src)[0])
161c6c9aed4Sab196087 #define	UL_ASSIGN_BSWAP_LWORD(_dst, _src) (void) \
162ba2be530Sab196087 	((_dst)[0] = (_src)[7],	(_dst)[1] = (_src)[6], \
163ba2be530Sab196087 	(_dst)[2] = (_src)[5],	(_dst)[3] = (_src)[4], \
164ba2be530Sab196087 	(_dst)[4] = (_src)[3],	(_dst)[5] = (_src)[2], \
165ba2be530Sab196087 	(_dst)[6] = (_src)[1],	(_dst)[7] = (_src)[0])
166c6c9aed4Sab196087 #if	defined(_ELF64)
167c6c9aed4Sab196087 #define	UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_LWORD(_dst, _src)
168ba2be530Sab196087 #else
169ba2be530Sab196087 #define	UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_WORD(_dst, _src)
170ba2be530Sab196087 #endif
171ba2be530Sab196087 
172ba2be530Sab196087 
173ba2be530Sab196087 #endif /* _MACHELF_H */
174