elf_machdep.c revision 95410
1/*- 2 * Copyright (c) 2001 Jake Burkholder. 3 * Copyright (c) 2000 Eduardo Horvath. 4 * Copyright (c) 1999 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. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 * 38 * from: NetBSD: mdreloc.c,v 1.5 2001/04/25 12:24:51 kleink Exp 39 * $FreeBSD: head/sys/sparc64/sparc64/elf_machdep.c 95410 2002-04-25 01:22:16Z marcel $ 40 */ 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/linker.h> 45#include <machine/elf.h> 46 47#include "linker_if.h" 48 49/* 50 * The following table holds for each relocation type: 51 * - the width in bits of the memory location the relocation 52 * applies to (not currently used) 53 * - the number of bits the relocation value must be shifted to the 54 * right (i.e. discard least significant bits) to fit into 55 * the appropriate field in the instruction word. 56 * - flags indicating whether 57 * * the relocation involves a symbol 58 * * the relocation is relative to the current position 59 * * the relocation is for a GOT entry 60 * * the relocation is relative to the load address 61 * 62 */ 63#define _RF_S 0x80000000 /* Resolve symbol */ 64#define _RF_A 0x40000000 /* Use addend */ 65#define _RF_P 0x20000000 /* Location relative */ 66#define _RF_G 0x10000000 /* GOT offset */ 67#define _RF_B 0x08000000 /* Load address relative */ 68#define _RF_U 0x04000000 /* Unaligned */ 69#define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 70#define _RF_RS(s) ( (s) & 0xff) /* right shift */ 71static int reloc_target_flags[] = { 72 0, /* NONE */ 73 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 74 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 75 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */ 76 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 77 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 78 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 79 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 80 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 81 _RF_A| _RF_B|_RF_SZ(32) | _RF_RS(10), /* HI22 */ 82 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */ 83 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */ 84 _RF_A| _RF_B|_RF_SZ(32) | _RF_RS(0), /* LO10 */ 85 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 86 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 87 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 88 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 89 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 90 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 91 _RF_SZ(32) | _RF_RS(0), /* COPY */ 92 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */ 93 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 94 _RF_A| _RF_B|_RF_SZ(64) | _RF_RS(0), /* RELATIVE */ 95 _RF_S|_RF_A| _RF_U|_RF_SZ(32) | _RF_RS(0), /* UA_32 */ 96 97 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 98 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */ 99 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */ 100 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */ 101 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */ 102 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */ 103 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */ 104 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */ 105 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */ 106 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */ 107 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */ 108 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */ 109 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */ 110 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */ 111 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */ 112 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */ 113 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */ 114 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */ 115 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */ 116 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */ 117 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */ 118 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */ 119 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */ 120 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */ 121 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */ 122 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */ 123 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */ 124 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */ 125 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */ 126 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */ 127 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */ 128 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */ 129}; 130 131#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 132#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 133#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 134#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 135#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 136#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 137#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 138 139static long reloc_target_bitmask[] = { 140#define _BM(x) (~(-(1ULL << (x)))) 141 0, /* NONE */ 142 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ 143 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 144 _BM(30), _BM(22), /* WDISP30, WDISP22 */ 145 _BM(22), _BM(22), /* HI22, _22 */ 146 _BM(13), _BM(10), /* RELOC_13, _LO10 */ 147 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 148 _BM(10), _BM(22), /* _PC10, _PC22 */ 149 _BM(30), 0, /* _WPLT30, _COPY */ 150 _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ 151 _BM(32), _BM(32), /* _UA32, PLT32 */ 152 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */ 153 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */ 154 _BM(10), _BM(11), -1, /* _10, _11, _64 */ 155 _BM(10), _BM(22), /* _OLO10, _HH22 */ 156 _BM(10), _BM(22), /* _HM10, _LM22 */ 157 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */ 158 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */ 159 -1, /* GLOB_JMP */ 160 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */ 161 -1, -1, /* DISP64, PLT64 */ 162 _BM(22), _BM(13), /* HIX22, LOX10 */ 163 _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */ 164 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */ 165#undef _BM 166}; 167#define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 168 169/* Process one elf relocation with addend. */ 170int 171elf_reloc(linker_file_t lf, const void *data, int type) 172{ 173 const Elf_Rela *rela; 174 Elf_Addr relocbase; 175 Elf_Half *where32; 176 Elf_Addr *where; 177 Elf_Word rtype, symidx; 178 Elf_Addr value; 179 Elf_Addr mask; 180 caddr_t addr; 181 182 if (type != ELF_RELOC_RELA) 183 return (-1); 184 185 relocbase = (Elf_Addr)lf->address; 186 rela = (const Elf_Rela *)data; 187 where = (Elf_Addr *)(relocbase + rela->r_offset); 188 where32 = (Elf_Half *)where; 189 rtype = ELF_R_TYPE(rela->r_info); 190 symidx = ELF_R_SYM(rela->r_info); 191 192 if (rtype == R_SPARC_NONE) 193 return (0); 194 195 if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY || 196 rtype > R_SPARC_UA16) 197 return (-1); 198 199 if (RELOC_UNALIGNED(rtype)) 200 return (-1); 201 202 value = rela->r_addend; 203 204 if (RELOC_RESOLVE_SYMBOL(rtype)) { 205 addr = elf_lookup(lf, symidx, 1); 206 if (addr == 0) 207 return (-1); 208 value += (Elf_Addr)addr; 209 } 210 211 if (RELOC_PC_RELATIVE(rtype)) 212 value -= (Elf_Addr)where; 213 214 if (RELOC_BASE_RELATIVE(rtype)) 215 value += (Elf_Addr)(relocbase); 216 217 mask = RELOC_VALUE_BITMASK(rtype); 218 value >>= RELOC_VALUE_RIGHTSHIFT(rtype); 219 value &= mask; 220 221 if (RELOC_TARGET_SIZE(rtype) > 32) { 222 *where &= ~mask; 223 *where |= value; 224 } else { 225 *where32 &= ~mask; 226 *where32 |= value; 227 } 228 229 return (0); 230} 231