xref: /onnv-gate/usr/src/cmd/sgs/include/_machelf.h (revision 6635:8de60a34b2e5)
16206Sab196087 /*
26206Sab196087  * CDDL HEADER START
36206Sab196087  *
46206Sab196087  * The contents of this file are subject to the terms of the
56206Sab196087  * Common Development and Distribution License (the "License").
66206Sab196087  * You may not use this file except in compliance with the License.
76206Sab196087  *
86206Sab196087  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
96206Sab196087  * or http://www.opensolaris.org/os/licensing.
106206Sab196087  * See the License for the specific language governing permissions
116206Sab196087  * and limitations under the License.
126206Sab196087  *
136206Sab196087  * When distributing Covered Code, include this CDDL HEADER in each
146206Sab196087  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
156206Sab196087  * If applicable, add the following below this CDDL HEADER, with the
166206Sab196087  * fields enclosed by brackets "[]" replaced with your own identifying
176206Sab196087  * information: Portions Copyright [yyyy] [name of copyright owner]
186206Sab196087  *
196206Sab196087  * CDDL HEADER END
206206Sab196087  */
216206Sab196087 
226206Sab196087 /*
236206Sab196087  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
246206Sab196087  * Use is subject to license terms.
256206Sab196087  *
266206Sab196087  * Wrapper around the <sys/machelf.h> header that adds
276206Sab196087  * definitions used by SGS.
286206Sab196087  */
296206Sab196087 
306206Sab196087 #ifndef	_MACHELF_H
316206Sab196087 #define	_MACHELF_H
326206Sab196087 
336206Sab196087 #pragma ident	"%Z%%M%	%I%	%E% SMI"
346206Sab196087 
356206Sab196087 #include <sys/machelf.h>
366206Sab196087 #include <string.h>		/* memcpy() */
376206Sab196087 
386206Sab196087 /*
396206Sab196087  * Make machine class dependent functions transparent to the common code
406206Sab196087  */
416206Sab196087 
426206Sab196087 /*
436206Sab196087  * Note on ELF_R_TYPE: 64-bit sparc relocations require the use of
446206Sab196087  * ELF64_R_TYPE_ID instead of the ELF64_R_TYPE macro used for all
456206Sab196087  * other platforms. So our ELF_R_TYPE macro requires the caller to
466206Sab196087  * supply the machine type.
476206Sab196087  */
486206Sab196087 
496206Sab196087 
506206Sab196087 #if	defined(_ELF64)
516206Sab196087 #define	ELF_R_TYPE(_info, _mach)  \
526206Sab196087 	(((_mach) == EM_SPARCV9) ? ELF64_R_TYPE_ID(_info) : ELF64_R_TYPE(_info))
536206Sab196087 #define	ELF_R_INFO			ELF64_R_INFO
546206Sab196087 #define	ELF_R_SYM			ELF64_R_SYM
556206Sab196087 #define	ELF_R_TYPE_DATA(x)		ELF64_R_TYPE_DATA(x)
566206Sab196087 #define	ELF_R_TYPE_INFO(xoff, type)	ELF64_R_TYPE_INFO(xoff, type)
576206Sab196087 #define	ELF_ST_BIND			ELF64_ST_BIND
586206Sab196087 #define	ELF_ST_TYPE			ELF64_ST_TYPE
596206Sab196087 #define	ELF_ST_INFO			ELF64_ST_INFO
606206Sab196087 #define	ELF_ST_VISIBILITY		ELF64_ST_VISIBILITY
616206Sab196087 #define	ELF_M_SYM			ELF64_M_SYM
626206Sab196087 #define	ELF_M_SIZE			ELF64_M_SIZE
636206Sab196087 #define	ELF_M_INFO			ELF64_M_INFO
646206Sab196087 #define	elf_checksum			elf64_checksum
656206Sab196087 #define	elf_fsize			elf64_fsize
666206Sab196087 #define	elf_getehdr			elf64_getehdr
676206Sab196087 #define	elf_getphdr			elf64_getphdr
686206Sab196087 #define	elf_newehdr			elf64_newehdr
696206Sab196087 #define	elf_newphdr			elf64_newphdr
706206Sab196087 #define	elf_getshdr			elf64_getshdr
716206Sab196087 #define	elf_xlatetof			elf64_xlatetof
726206Sab196087 #define	elf_xlatetom			elf64_xlatetom
736206Sab196087 #else /* _ELF64 */
746206Sab196087 #define	ELF_R_TYPE(_info, _mach)	ELF32_R_TYPE(_info)
756206Sab196087 #define	ELF_R_INFO			ELF32_R_INFO
766206Sab196087 #define	ELF_R_SYM			ELF32_R_SYM
776206Sab196087 /* Elf64 can hide extra offset in r_info */
786206Sab196087 #define	ELF_R_TYPE_DATA(x)		(0)
796206Sab196087 #define	ELF_R_TYPE_INFO(xoff, type)	(type)
806206Sab196087 #define	ELF_ST_BIND			ELF32_ST_BIND
816206Sab196087 #define	ELF_ST_TYPE			ELF32_ST_TYPE
826206Sab196087 #define	ELF_ST_INFO			ELF32_ST_INFO
836206Sab196087 #define	ELF_ST_VISIBILITY		ELF32_ST_VISIBILITY
846206Sab196087 #define	ELF_M_SYM			ELF32_M_SYM
856206Sab196087 #define	ELF_M_SIZE			ELF32_M_SIZE
866206Sab196087 #define	ELF_M_INFO			ELF32_M_INFO
876206Sab196087 #define	elf_checksum			elf32_checksum
886206Sab196087 #define	elf_fsize			elf32_fsize
896206Sab196087 #define	elf_getehdr			elf32_getehdr
906206Sab196087 #define	elf_getphdr			elf32_getphdr
916206Sab196087 #define	elf_newehdr			elf32_newehdr
926206Sab196087 #define	elf_newphdr			elf32_newphdr
936206Sab196087 #define	elf_getshdr			elf32_getshdr
946206Sab196087 #define	elf_xlatetof			elf32_xlatetof
956206Sab196087 #define	elf_xlatetom			elf32_xlatetom
966206Sab196087 #endif	/* _ELF32 */
976206Sab196087 
986206Sab196087 
996206Sab196087 /*
1006206Sab196087  * Macros for swapping bytes. The type of the argument must
1016206Sab196087  * match the type given in the macro name.
1026206Sab196087  */
1036206Sab196087 #define	BSWAP_HALF(_half) \
1046206Sab196087 	(((_half) << 8) | ((_half) >> 8))
1056206Sab196087 
1066206Sab196087 #define	BSWAP_WORD(_word) \
1076206Sab196087 	((((_word) << 24) | (((_word) & 0xff00) << 8) | \
1086206Sab196087 	(((_word) >> 8) & 0xff00) | ((_word) >> 24)))
1096206Sab196087 
110*6635Sab196087 #define	BSWAP_LWORD(_lword) \
111*6635Sab196087 	(((_lword) << 56) | \
112*6635Sab196087 	(((_lword) & 0x0000ff00) << 40) | \
113*6635Sab196087 	(((_lword) & 0x00ff0000) << 24) | \
114*6635Sab196087 	(((_lword) & 0xff000000) << 8) | \
115*6635Sab196087 	(((_lword) >> 8)  & 0xff000000) | \
116*6635Sab196087 	(((_lword) >> 24) & 0x00ff0000) | \
117*6635Sab196087 	(((_lword) >> 40) & 0x0000ff00) | \
118*6635Sab196087 	((_lword) >> 56))	/* Lword is unsigned - 0 bits enter from left */
119*6635Sab196087 
120*6635Sab196087 
1216206Sab196087 #if	defined(_ELF64)
122*6635Sab196087 #define	BSWAP_XWORD(_xword) BSWAP_LWORD(_xword)
1236206Sab196087 #else
1246206Sab196087 #define	BSWAP_XWORD(_xword) BSWAP_WORD(_xword)
1256206Sab196087 #endif
1266206Sab196087 
1276206Sab196087 /*
1286206Sab196087  * Macros for assigning Half/Word/Xword items from one location to
1296206Sab196087  * another that are safe no matter what the data alignment rules of the
1306206Sab196087  * running platform are. Variants exist to swap the data byteorder
1316206Sab196087  * at the same time, or not.
1326206Sab196087  *
1336206Sab196087  * These macros are useful for code that accesses data that is aligned
1346206Sab196087  * for a different system architecture, as occurs in cross linking.
1356206Sab196087  *
1366206Sab196087  * All of these macros assume the arguments are passed as pointers to
1376206Sab196087  * bytes (signed or unsigned).
1386206Sab196087  */
1396206Sab196087 
1406206Sab196087 #define	UL_ASSIGN_HALF(_dst, _src) (void) \
1416206Sab196087 	((_dst)[0] = (_src)[0],	(_dst)[1] = (_src)[1])
1426206Sab196087 #define	UL_ASSIGN_WORD(_dst, _src) (void) \
1436206Sab196087 	((_dst)[0] = (_src)[0],	(_dst)[1] = (_src)[1], \
1446206Sab196087 	(_dst)[2] = (_src)[2], 	(_dst)[3] = (_src)[3])
145*6635Sab196087 #define	UL_ASSIGN_LWORD(_dst, _src) (void) memcpy(_dst, (_src), sizeof (Lword))
1466206Sab196087 #if	defined(_ELF64)
147*6635Sab196087 #define	UL_ASSIGN_XWORD(_dst, _src) UL_ASSIGN_LWORD(_dst, _src)
1486206Sab196087 #else
1496206Sab196087 #define	UL_ASSIGN_XWORD(_xword) UL_ASSIGN_WORD(_xword)
1506206Sab196087 #endif
1516206Sab196087 
1526206Sab196087 #define	UL_ASSIGN_BSWAP_HALF(_dst, _src) (void) \
1536206Sab196087 	((_dst)[0] = (_src)[1],	(_dst)[1] = (_src)[0])
1546206Sab196087 #define	UL_ASSIGN_BSWAP_WORD(_dst, _src) (void) \
1556206Sab196087 	((_dst)[0] = (_src)[3],	(_dst)[1] = (_src)[2], \
1566206Sab196087 	(_dst)[2] = (_src)[1],	(_dst)[3] = (_src)[0])
157*6635Sab196087 #define	UL_ASSIGN_BSWAP_LWORD(_dst, _src) (void) \
1586206Sab196087 	((_dst)[0] = (_src)[7],	(_dst)[1] = (_src)[6], \
1596206Sab196087 	(_dst)[2] = (_src)[5],	(_dst)[3] = (_src)[4], \
1606206Sab196087 	(_dst)[4] = (_src)[3],	(_dst)[5] = (_src)[2], \
1616206Sab196087 	(_dst)[6] = (_src)[1],	(_dst)[7] = (_src)[0])
162*6635Sab196087 #if	defined(_ELF64)
163*6635Sab196087 #define	UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_LWORD(_dst, _src)
1646206Sab196087 #else
1656206Sab196087 #define	UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_WORD(_dst, _src)
1666206Sab196087 #endif
1676206Sab196087 
1686206Sab196087 
1696206Sab196087 #endif /* _MACHELF_H */
170