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