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