1 /* $NetBSD: kobj_machdep.c,v 1.4 2010/05/02 11:43:30 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 Jake Burkholder. 5 * Copyright (c) 2000 Eduardo Horvath. 6 * Copyright (c) 1999, 2008 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Paul Kranenburg. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 * POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <sys/cdefs.h> 35 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.4 2010/05/02 11:43:30 martin Exp $"); 36 37 #define ELFSIZE ARCH_ELFSIZE 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kobj.h> 42 #include <sys/exec.h> 43 #include <sys/exec_elf.h> 44 45 /* 46 * The following table holds for each relocation type: 47 * - the width in bits of the memory location the relocation 48 * applies to (not currently used) 49 * - the number of bits the relocation value must be shifted to the 50 * right (i.e. discard least significant bits) to fit into 51 * the appropriate field in the instruction word. 52 * - flags indicating whether 53 * * the relocation involves a symbol 54 * * the relocation is relative to the current position 55 * * the relocation is for a GOT entry 56 * * the relocation is relative to the load address 57 * 58 */ 59 #define _RF_S 0x80000000 /* Resolve symbol */ 60 #define _RF_A 0x40000000 /* Use addend */ 61 #define _RF_P 0x20000000 /* Location relative */ 62 #define _RF_G 0x10000000 /* GOT offset */ 63 #define _RF_B 0x08000000 /* Load address relative */ 64 #define _RF_U 0x04000000 /* Unaligned */ 65 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 66 #define _RF_RS(s) ( (s) & 0xff) /* right shift */ 67 static const int reloc_target_flags[] = { 68 0, /* NONE */ 69 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 70 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 71 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */ 72 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 73 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 74 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 75 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 76 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 77 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */ 78 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */ 79 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */ 80 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */ 81 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 82 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 83 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 84 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 85 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 86 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 87 _RF_SZ(32) | _RF_RS(0), /* COPY */ 88 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */ 89 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 90 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */ 91 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ 92 93 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 94 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */ 95 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */ 96 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */ 97 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */ 98 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */ 99 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */ 100 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */ 101 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */ 102 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */ 103 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */ 104 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */ 105 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */ 106 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */ 107 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */ 108 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */ 109 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */ 110 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */ 111 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */ 112 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */ 113 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */ 114 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */ 115 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */ 116 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */ 117 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */ 118 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */ 119 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */ 120 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */ 121 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */ 122 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */ 123 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */ 124 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */ 125 }; 126 127 #if 0 128 static const char *reloc_names[] = { 129 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8", 130 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22", 131 "22", "13", "LO10", "GOT10", "GOT13", 132 "GOT22", "PC10", "PC22", "WPLT30", "COPY", 133 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32", 134 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32", 135 "10", "11", "64", "OLO10", "HH22", 136 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22", 137 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6", 138 "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44", 139 "L44", "REGISTER", "UA64", "UA16" 140 }; 141 #endif 142 143 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 144 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 145 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 146 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 147 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 148 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 149 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 150 151 static const long reloc_target_bitmask[] = { 152 #define _BM(x) (~(-(1ULL << (x)))) 153 0, /* NONE */ 154 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ 155 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 156 _BM(30), _BM(22), /* WDISP30, WDISP22 */ 157 _BM(22), _BM(22), /* HI22, _22 */ 158 _BM(13), _BM(10), /* RELOC_13, _LO10 */ 159 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 160 _BM(10), _BM(22), /* _PC10, _PC22 */ 161 _BM(30), 0, /* _WPLT30, _COPY */ 162 _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ 163 _BM(32), _BM(32), /* _UA32, PLT32 */ 164 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */ 165 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */ 166 _BM(10), _BM(11), -1, /* _10, _11, _64 */ 167 _BM(10), _BM(22), /* _OLO10, _HH22 */ 168 _BM(10), _BM(22), /* _HM10, _LM22 */ 169 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */ 170 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */ 171 -1, /* GLOB_JMP */ 172 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */ 173 -1, -1, /* DISP64, PLT64 */ 174 _BM(22), _BM(13), /* HIX22, LOX10 */ 175 _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */ 176 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */ 177 #undef _BM 178 }; 179 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 180 181 int 182 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data, 183 bool isrela, bool local) 184 { 185 const Elf_Rela *rela; 186 Elf_Word *where32; 187 Elf_Addr *where; 188 uintptr_t rtype, symidx; 189 Elf_Addr value; 190 Elf_Addr mask; 191 Elf_Addr addr; 192 vaddr_t base; 193 194 if (!isrela) 195 return -1; 196 197 rela = (const Elf_Rela *)data; 198 where = (Elf_Addr *)(relocbase + rela->r_offset); 199 where32 = (Elf_Word *)where; 200 rtype = ELF64_R_TYPE(rela->r_info); 201 symidx = ELF_R_SYM(rela->r_info); 202 203 if (rtype == R_SPARC_NONE) 204 return 0; 205 206 if ((rtype & 0x00ff) == R_SPARC_OLO10) 207 rtype = R_SPARC_OLO10; 208 209 if (rtype == R_SPARC_RELATIVE) { 210 kobj_stat(ko, &base, NULL); 211 value = rela->r_addend + (Elf_Addr)base; 212 where = (Elf_Addr *)((Elf_Addr)base + rela->r_offset); 213 *where = value; 214 return 0; 215 } 216 217 if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY || 218 rtype >= __arraycount(reloc_target_bitmask)) 219 return -1; 220 221 if (RELOC_UNALIGNED(rtype)) 222 return -1; 223 224 value = rela->r_addend; 225 226 if (RELOC_RESOLVE_SYMBOL(rtype)) { 227 addr = kobj_sym_lookup(ko, symidx); 228 if (addr == 0) 229 return -1; 230 value += addr; 231 } 232 233 if (rtype == R_SPARC_OLO10) 234 value = (value & 0x3ff) + (((Elf64_Xword)rela->r_info<<32)>>40); 235 236 if (RELOC_PC_RELATIVE(rtype)) 237 value -= (Elf_Addr)where; 238 239 if (RELOC_BASE_RELATIVE(rtype)) 240 value += relocbase; 241 242 mask = RELOC_VALUE_BITMASK(rtype); 243 value >>= RELOC_VALUE_RIGHTSHIFT(rtype); 244 value &= mask; 245 246 if (RELOC_TARGET_SIZE(rtype) > 32) { 247 *where &= ~mask; 248 *where |= value; 249 } else { 250 *where32 &= ~mask; 251 *where32 |= value; 252 } 253 254 return 0; 255 } 256 257 int 258 kobj_machdep(kobj_t ko, void *base, size_t size, bool load) 259 { 260 261 return 0; 262 } 263