xref: /onnv-gate/usr/src/cmd/sgs/include/_machelf.h (revision 11827:d7ef53deac3f)
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 /*
23*11827SRod.Evans@Sun.COM  * Copyright 2010 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 #include <sys/machelf.h>
346206Sab196087 #include <string.h>		/* memcpy() */
356206Sab196087 
366206Sab196087 /*
376206Sab196087  * Make machine class dependent functions transparent to the common code
386206Sab196087  */
396206Sab196087 
406206Sab196087 /*
416206Sab196087  * Note on ELF_R_TYPE: 64-bit sparc relocations require the use of
426206Sab196087  * ELF64_R_TYPE_ID instead of the ELF64_R_TYPE macro used for all
436206Sab196087  * other platforms. So our ELF_R_TYPE macro requires the caller to
446206Sab196087  * supply the machine type.
456206Sab196087  */
466206Sab196087 
476206Sab196087 
486206Sab196087 #if	defined(_ELF64)
496206Sab196087 #define	ELF_R_TYPE(_info, _mach)  \
506206Sab196087 	(((_mach) == EM_SPARCV9) ? ELF64_R_TYPE_ID(_info) : ELF64_R_TYPE(_info))
516206Sab196087 #define	ELF_R_INFO			ELF64_R_INFO
526206Sab196087 #define	ELF_R_SYM			ELF64_R_SYM
536206Sab196087 #define	ELF_R_TYPE_DATA(x)		ELF64_R_TYPE_DATA(x)
546206Sab196087 #define	ELF_R_TYPE_INFO(xoff, type)	ELF64_R_TYPE_INFO(xoff, type)
556206Sab196087 #define	ELF_ST_BIND			ELF64_ST_BIND
566206Sab196087 #define	ELF_ST_TYPE			ELF64_ST_TYPE
576206Sab196087 #define	ELF_ST_INFO			ELF64_ST_INFO
586206Sab196087 #define	ELF_ST_VISIBILITY		ELF64_ST_VISIBILITY
596206Sab196087 #define	ELF_M_SYM			ELF64_M_SYM
606206Sab196087 #define	ELF_M_SIZE			ELF64_M_SIZE
616206Sab196087 #define	ELF_M_INFO			ELF64_M_INFO
62*11827SRod.Evans@Sun.COM #define	ELF_C_SYM			ELF64_C_SYM
63*11827SRod.Evans@Sun.COM #define	ELF_C_GROUP			ELF64_C_GROUP
64*11827SRod.Evans@Sun.COM #define	ELF_C_INFO			ELF64_C_INFO
656206Sab196087 #define	elf_checksum			elf64_checksum
666206Sab196087 #define	elf_fsize			elf64_fsize
676206Sab196087 #define	elf_getehdr			elf64_getehdr
686206Sab196087 #define	elf_getphdr			elf64_getphdr
696206Sab196087 #define	elf_newehdr			elf64_newehdr
706206Sab196087 #define	elf_newphdr			elf64_newphdr
716206Sab196087 #define	elf_getshdr			elf64_getshdr
726206Sab196087 #define	elf_xlatetof			elf64_xlatetof
736206Sab196087 #define	elf_xlatetom			elf64_xlatetom
746206Sab196087 #else /* _ELF64 */
756206Sab196087 #define	ELF_R_TYPE(_info, _mach)	ELF32_R_TYPE(_info)
766206Sab196087 #define	ELF_R_INFO			ELF32_R_INFO
776206Sab196087 #define	ELF_R_SYM			ELF32_R_SYM
786206Sab196087 /* Elf64 can hide extra offset in r_info */
796206Sab196087 #define	ELF_R_TYPE_DATA(x)		(0)
806206Sab196087 #define	ELF_R_TYPE_INFO(xoff, type)	(type)
816206Sab196087 #define	ELF_ST_BIND			ELF32_ST_BIND
826206Sab196087 #define	ELF_ST_TYPE			ELF32_ST_TYPE
836206Sab196087 #define	ELF_ST_INFO			ELF32_ST_INFO
846206Sab196087 #define	ELF_ST_VISIBILITY		ELF32_ST_VISIBILITY
856206Sab196087 #define	ELF_M_SYM			ELF32_M_SYM
866206Sab196087 #define	ELF_M_SIZE			ELF32_M_SIZE
876206Sab196087 #define	ELF_M_INFO			ELF32_M_INFO
88*11827SRod.Evans@Sun.COM #define	ELF_C_SYM			ELF32_C_SYM
89*11827SRod.Evans@Sun.COM #define	ELF_C_GROUP			ELF32_C_GROUP
90*11827SRod.Evans@Sun.COM #define	ELF_C_INFO			ELF32_C_INFO
916206Sab196087 #define	elf_checksum			elf32_checksum
926206Sab196087 #define	elf_fsize			elf32_fsize
936206Sab196087 #define	elf_getehdr			elf32_getehdr
946206Sab196087 #define	elf_getphdr			elf32_getphdr
956206Sab196087 #define	elf_newehdr			elf32_newehdr
966206Sab196087 #define	elf_newphdr			elf32_newphdr
976206Sab196087 #define	elf_getshdr			elf32_getshdr
986206Sab196087 #define	elf_xlatetof			elf32_xlatetof
996206Sab196087 #define	elf_xlatetom			elf32_xlatetom
1006206Sab196087 #endif	/* _ELF32 */
1016206Sab196087 
1026206Sab196087 
1036206Sab196087 /*
1046206Sab196087  * Macros for swapping bytes. The type of the argument must
1056206Sab196087  * match the type given in the macro name.
1066206Sab196087  */
1076206Sab196087 #define	BSWAP_HALF(_half) \
1086206Sab196087 	(((_half) << 8) | ((_half) >> 8))
1096206Sab196087 
1106206Sab196087 #define	BSWAP_WORD(_word) \
1116206Sab196087 	((((_word) << 24) | (((_word) & 0xff00) << 8) | \
1126206Sab196087 	(((_word) >> 8) & 0xff00) | ((_word) >> 24)))
1136206Sab196087 
1146635Sab196087 #define	BSWAP_LWORD(_lword) \
1156635Sab196087 	(((_lword) << 56) | \
1166635Sab196087 	(((_lword) & 0x0000ff00) << 40) | \
1176635Sab196087 	(((_lword) & 0x00ff0000) << 24) | \
1186635Sab196087 	(((_lword) & 0xff000000) << 8) | \
1196635Sab196087 	(((_lword) >> 8)  & 0xff000000) | \
1206635Sab196087 	(((_lword) >> 24) & 0x00ff0000) | \
1216635Sab196087 	(((_lword) >> 40) & 0x0000ff00) | \
1226635Sab196087 	((_lword) >> 56))	/* Lword is unsigned - 0 bits enter from left */
1236635Sab196087 
1246635Sab196087 
1256206Sab196087 #if	defined(_ELF64)
1266635Sab196087 #define	BSWAP_XWORD(_xword) BSWAP_LWORD(_xword)
1276206Sab196087 #else
1286206Sab196087 #define	BSWAP_XWORD(_xword) BSWAP_WORD(_xword)
1296206Sab196087 #endif
1306206Sab196087 
1316206Sab196087 /*
1326206Sab196087  * Macros for assigning Half/Word/Xword items from one location to
1336206Sab196087  * another that are safe no matter what the data alignment rules of the
1346206Sab196087  * running platform are. Variants exist to swap the data byteorder
1356206Sab196087  * at the same time, or not.
1366206Sab196087  *
1376206Sab196087  * These macros are useful for code that accesses data that is aligned
1386206Sab196087  * for a different system architecture, as occurs in cross linking.
1396206Sab196087  *
1406206Sab196087  * All of these macros assume the arguments are passed as pointers to
1416206Sab196087  * bytes (signed or unsigned).
1426206Sab196087  */
1436206Sab196087 
1446206Sab196087 #define	UL_ASSIGN_HALF(_dst, _src) (void) \
1456206Sab196087 	((_dst)[0] = (_src)[0],	(_dst)[1] = (_src)[1])
1466206Sab196087 #define	UL_ASSIGN_WORD(_dst, _src) (void) \
1476206Sab196087 	((_dst)[0] = (_src)[0],	(_dst)[1] = (_src)[1], \
1486206Sab196087 	(_dst)[2] = (_src)[2], 	(_dst)[3] = (_src)[3])
1496635Sab196087 #define	UL_ASSIGN_LWORD(_dst, _src) (void) memcpy(_dst, (_src), sizeof (Lword))
1506206Sab196087 #if	defined(_ELF64)
1516635Sab196087 #define	UL_ASSIGN_XWORD(_dst, _src) UL_ASSIGN_LWORD(_dst, _src)
1526206Sab196087 #else
1536206Sab196087 #define	UL_ASSIGN_XWORD(_xword) UL_ASSIGN_WORD(_xword)
1546206Sab196087 #endif
1556206Sab196087 
1566206Sab196087 #define	UL_ASSIGN_BSWAP_HALF(_dst, _src) (void) \
1576206Sab196087 	((_dst)[0] = (_src)[1],	(_dst)[1] = (_src)[0])
1586206Sab196087 #define	UL_ASSIGN_BSWAP_WORD(_dst, _src) (void) \
1596206Sab196087 	((_dst)[0] = (_src)[3],	(_dst)[1] = (_src)[2], \
1606206Sab196087 	(_dst)[2] = (_src)[1],	(_dst)[3] = (_src)[0])
1616635Sab196087 #define	UL_ASSIGN_BSWAP_LWORD(_dst, _src) (void) \
1626206Sab196087 	((_dst)[0] = (_src)[7],	(_dst)[1] = (_src)[6], \
1636206Sab196087 	(_dst)[2] = (_src)[5],	(_dst)[3] = (_src)[4], \
1646206Sab196087 	(_dst)[4] = (_src)[3],	(_dst)[5] = (_src)[2], \
1656206Sab196087 	(_dst)[6] = (_src)[1],	(_dst)[7] = (_src)[0])
1666635Sab196087 #if	defined(_ELF64)
1676635Sab196087 #define	UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_LWORD(_dst, _src)
1686206Sab196087 #else
1696206Sab196087 #define	UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_WORD(_dst, _src)
1706206Sab196087 #endif
1716206Sab196087 
1726206Sab196087 
1736206Sab196087 #endif /* _MACHELF_H */
174