1 /* $NetBSD: kobj_machdep.c,v 1.2 2008/04/28 20:23:36 martin Exp $ */ 2 3 /*- 4 * Copyright (c) 1999, 2002, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Paul Kranenburg and by Charles M. Hannum. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #define ELFSIZE ARCH_ELFSIZE 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/kobj.h> 37 #include <sys/exec.h> 38 #include <sys/exec_elf.h> 39 40 /* 41 * The following table holds for each relocation type: 42 * - the width in bits of the memory location the relocation 43 * applies to (not currently used) 44 * - the number of bits the relocation value must be shifted to the 45 * right (i.e. discard least significant bits) to fit into 46 * the appropriate field in the instruction word. 47 * - flags indicating whether 48 * * the relocation involves a symbol 49 * * the relocation is relative to the current position 50 * * the relocation is for a GOT entry 51 * * the relocation is relative to the load address 52 * 53 */ 54 #define _RF_S 0x80000000 /* Resolve symbol */ 55 #define _RF_A 0x40000000 /* Use addend */ 56 #define _RF_P 0x20000000 /* Location relative */ 57 #define _RF_G 0x10000000 /* GOT offset */ 58 #define _RF_B 0x08000000 /* Load address relative */ 59 #define _RF_U 0x04000000 /* Unaligned */ 60 #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 61 #define _RF_RS(s) ( (s) & 0xff) /* right shift */ 62 static const int reloc_target_flags[] = { 63 0, /* NONE */ 64 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 65 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 66 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */ 67 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 68 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 69 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 70 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 71 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 72 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */ 73 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */ 74 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */ 75 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */ 76 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 77 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 78 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 79 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 80 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 81 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 82 _RF_SZ(32) | _RF_RS(0), /* COPY */ 83 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_DAT */ 84 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 85 _RF_A| _RF_B| _RF_SZ(32) | _RF_RS(0), /* RELATIVE */ 86 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ 87 }; 88 89 #ifdef RTLD_DEBUG_RELOC 90 static const char *reloc_names[] = { 91 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8", 92 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22", 93 "22", "13", "LO10", "GOT10", "GOT13", 94 "GOT22", "PC10", "PC22", "WPLT30", "COPY", 95 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32" 96 }; 97 #endif 98 99 #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 100 #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 101 #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 102 #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 103 #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 104 #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 105 #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 106 107 static const int reloc_target_bitmask[] = { 108 #define _BM(x) (~(-(1ULL << (x)))) 109 0, /* NONE */ 110 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ 111 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 112 _BM(30), _BM(22), /* WDISP30, WDISP22 */ 113 _BM(22), _BM(22), /* HI22, _22 */ 114 _BM(13), _BM(10), /* RELOC_13, _LO10 */ 115 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 116 _BM(10), _BM(22), /* _PC10, _PC22 */ 117 _BM(30), 0, /* _WPLT30, _COPY */ 118 -1, -1, -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ 119 _BM(32) /* _UA32 */ 120 #undef _BM 121 }; 122 #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 123 124 int 125 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data, 126 bool isrela, bool local) 127 { 128 const Elf_Rela *rela; 129 Elf_Addr *where; 130 Elf_Word value, mask; 131 uintptr_t tmp, addr; 132 u_int symidx, type; 133 134 rela = data; 135 where = (Elf_Addr *) (relocbase + rela->r_offset); 136 symidx = ELF_R_SYM(rela->r_info); 137 type = ELF_R_TYPE(rela->r_info); 138 value = rela->r_addend; 139 140 if (type == R_TYPE(NONE)) 141 return 0; 142 143 if (!isrela) { 144 printf("kobj_reloc: only support RELA relocations\n"); 145 return -1; 146 } 147 148 if (type == R_TYPE(JMP_SLOT) || type == R_TYPE(COPY) || 149 type > R_TYPE(6)) { 150 printf("kobj_reloc: unexpected reloc type %d\n", type); 151 return -1; 152 } 153 154 if (type == R_TYPE(RELATIVE)) { 155 *where += (Elf_Addr)(relocbase + value); 156 return 0; 157 } 158 159 if (RELOC_RESOLVE_SYMBOL(type)) { 160 addr = kobj_sym_lookup(ko, symidx); 161 if (addr == 0) 162 return -1; 163 value += (Elf_Word)(relocbase + addr); 164 } 165 166 if (RELOC_PC_RELATIVE(type)) { 167 value -= (Elf_Word)where; 168 } 169 170 if (RELOC_BASE_RELATIVE(type)) { 171 value += (Elf_Word)(relocbase + *where); 172 } 173 174 mask = RELOC_VALUE_BITMASK(type); 175 value >>= RELOC_VALUE_RIGHTSHIFT(type); 176 value &= mask; 177 178 if (RELOC_UNALIGNED(type)) { 179 /* Handle unaligned relocations. */ 180 char *ptr = (char *)where; 181 int i, size = RELOC_TARGET_SIZE(type)/8; 182 183 /* Read it in one byte at a time. */ 184 for (i = 0, tmp = 0; i < size; i++) 185 tmp = (tmp << 8) | ptr[i]; 186 187 tmp &= ~mask; 188 tmp |= value; 189 190 /* Write it back out. */ 191 for (i=0; i<size; i++) 192 ptr[i] = ((tmp >> (8*i)) & 0xff); 193 194 } else { 195 *where &= ~mask; 196 *where |= value; 197 } 198 199 return 0; 200 } 201 202 int 203 kobj_machdep(kobj_t ko, void *base, size_t size, bool load) 204 { 205 206 return 0; 207 } 208