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 #if defined(_ELF64) 111 #define BSWAP_XWORD(_xword) \ 112 (((_xword) << 56) | \ 113 (((_xword) & 0x0000ff00) << 40) | \ 114 (((_xword) & 0x00ff0000) << 24) | \ 115 (((_xword) & 0xff000000) << 8) | \ 116 (((_xword) >> 8) & 0xff000000) | \ 117 (((_xword) >> 24) & 0x00ff0000) | \ 118 (((_xword) >> 40) & 0x0000ff00) | \ 119 ((_xword) >> 56)) /* Xword is unsigned - 0 bits enter from left */ 120 #else 121 #define BSWAP_XWORD(_xword) BSWAP_WORD(_xword) 122 #endif 123 124 /* 125 * Macros for assigning Half/Word/Xword items from one location to 126 * another that are safe no matter what the data alignment rules of the 127 * running platform are. Variants exist to swap the data byteorder 128 * at the same time, or not. 129 * 130 * These macros are useful for code that accesses data that is aligned 131 * for a different system architecture, as occurs in cross linking. 132 * 133 * All of these macros assume the arguments are passed as pointers to 134 * bytes (signed or unsigned). 135 */ 136 137 #define UL_ASSIGN_HALF(_dst, _src) (void) \ 138 ((_dst)[0] = (_src)[0], (_dst)[1] = (_src)[1]) 139 #define UL_ASSIGN_WORD(_dst, _src) (void) \ 140 ((_dst)[0] = (_src)[0], (_dst)[1] = (_src)[1], \ 141 (_dst)[2] = (_src)[2], (_dst)[3] = (_src)[3]) 142 #if defined(_ELF64) 143 #define UL_ASSIGN_XWORD(_dst, _src) (void) memcpy(_dst, (_src), sizeof (Xword)) 144 #else 145 #define UL_ASSIGN_XWORD(_xword) UL_ASSIGN_WORD(_xword) 146 #endif 147 148 #define UL_ASSIGN_BSWAP_HALF(_dst, _src) (void) \ 149 ((_dst)[0] = (_src)[1], (_dst)[1] = (_src)[0]) 150 #define UL_ASSIGN_BSWAP_WORD(_dst, _src) (void) \ 151 ((_dst)[0] = (_src)[3], (_dst)[1] = (_src)[2], \ 152 (_dst)[2] = (_src)[1], (_dst)[3] = (_src)[0]) 153 #if defined(_ELF64) 154 #define UL_ASSIGN_BSWAP_XWORD(_dst, _src) (void) \ 155 ((_dst)[0] = (_src)[7], (_dst)[1] = (_src)[6], \ 156 (_dst)[2] = (_src)[5], (_dst)[3] = (_src)[4], \ 157 (_dst)[4] = (_src)[3], (_dst)[5] = (_src)[2], \ 158 (_dst)[6] = (_src)[1], (_dst)[7] = (_src)[0]) 159 #else 160 #define UL_ASSIGN_BSWAP_XWORD(_dst, _src) UL_ASSIGN_BSWAP_WORD(_dst, _src) 161 #endif 162 163 164 #endif /* _MACHELF_H */ 165