elf_machdep.c revision 105469
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 105469 2002-10-19 19:16:03Z marcel $ 40 */ 41 42#include <sys/param.h> 43#include <sys/kernel.h> 44#include <sys/systm.h> 45#include <sys/exec.h> 46#include <sys/imgact.h> 47#include <sys/linker.h> 48#include <sys/sysent.h> 49#include <sys/imgact_elf.h> 50#include <sys/syscall.h> 51#include <sys/signalvar.h> 52#include <sys/vnode.h> 53 54#include <vm/vm.h> 55#include <vm/vm_param.h> 56 57#include <machine/elf.h> 58 59#include "linker_if.h" 60 61struct sysentvec elf64_freebsd_sysvec = { 62 SYS_MAXSYSCALL, 63 sysent, 64 0, 65 0, 66 NULL, 67 0, 68 NULL, 69 NULL, 70 __elfN(freebsd_fixup), 71 sendsig, 72 NULL, 73 NULL, 74 NULL, 75 "FreeBSD ELF64", 76 __elfN(coredump), 77 NULL, 78 MINSIGSTKSZ, 79 PAGE_SIZE, 80 VM_MIN_ADDRESS, 81 VM_MAXUSER_ADDRESS, 82 USRSTACK, 83 PS_STRINGS, 84 VM_PROT_READ | VM_PROT_WRITE, 85 exec_copyout_strings, 86 exec_setregs 87}; 88 89static Elf64_Brandinfo freebsd_brand_info = { 90 ELFOSABI_FREEBSD, 91 EM_SPARCV9, 92 "FreeBSD", 93 "", 94 "/usr/libexec/ld-elf.so.1", 95 &elf64_freebsd_sysvec 96 }; 97 98SYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_ANY, 99 (sysinit_cfunc_t) elf64_insert_brand_entry, 100 &freebsd_brand_info); 101 102/* 103 * The following table holds for each relocation type: 104 * - the width in bits of the memory location the relocation 105 * applies to (not currently used) 106 * - the number of bits the relocation value must be shifted to the 107 * right (i.e. discard least significant bits) to fit into 108 * the appropriate field in the instruction word. 109 * - flags indicating whether 110 * * the relocation involves a symbol 111 * * the relocation is relative to the current position 112 * * the relocation is for a GOT entry 113 * * the relocation is relative to the load address 114 * 115 */ 116#define _RF_S 0x80000000 /* Resolve symbol */ 117#define _RF_A 0x40000000 /* Use addend */ 118#define _RF_P 0x20000000 /* Location relative */ 119#define _RF_G 0x10000000 /* GOT offset */ 120#define _RF_B 0x08000000 /* Load address relative */ 121#define _RF_U 0x04000000 /* Unaligned */ 122#define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 123#define _RF_RS(s) ( (s) & 0xff) /* right shift */ 124static int reloc_target_flags[] = { 125 0, /* NONE */ 126 _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */ 127 _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */ 128 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */ 129 _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 130 _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 131 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 132 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 133 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 134 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */ 135 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */ 136 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */ 137 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */ 138 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 139 _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 140 _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 141 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 142 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 143 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 144 _RF_SZ(32) | _RF_RS(0), /* COPY */ 145 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */ 146 _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 147 _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */ 148 _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ 149 150 _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 151 _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */ 152 _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */ 153 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */ 154 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */ 155 _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */ 156 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */ 157 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */ 158 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */ 159 _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */ 160 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */ 161 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */ 162 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */ 163 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */ 164 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */ 165 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */ 166 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */ 167 _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */ 168 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */ 169 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */ 170 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */ 171 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */ 172 _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */ 173 _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */ 174 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */ 175 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */ 176 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */ 177 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */ 178 _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */ 179 _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */ 180 _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */ 181 _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */ 182}; 183 184#if 0 185static const char *reloc_names[] = { 186 "NONE", "RELOC_8", "RELOC_16", "RELOC_32", "DISP_8", 187 "DISP_16", "DISP_32", "WDISP_30", "WDISP_22", "HI22", 188 "22", "13", "LO10", "GOT10", "GOT13", 189 "GOT22", "PC10", "PC22", "WPLT30", "COPY", 190 "GLOB_DAT", "JMP_SLOT", "RELATIVE", "UA_32", "PLT32", 191 "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", "PCPLT32", 192 "10", "11", "64", "OLO10", "HH22", 193 "HM10", "LM22", "PC_HH22", "PC_HM10", "PC_LM22", 194 "WDISP16", "WDISP19", "GLOB_JMP", "7", "5", "6", 195 "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44", 196 "L44", "REGISTER", "UA64", "UA16" 197}; 198#endif 199 200#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 201#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 202#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 203#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 204#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 205#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 206#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 207 208static long reloc_target_bitmask[] = { 209#define _BM(x) (~(-(1ULL << (x)))) 210 0, /* NONE */ 211 _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */ 212 _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 213 _BM(30), _BM(22), /* WDISP30, WDISP22 */ 214 _BM(22), _BM(22), /* HI22, _22 */ 215 _BM(13), _BM(10), /* RELOC_13, _LO10 */ 216 _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 217 _BM(10), _BM(22), /* _PC10, _PC22 */ 218 _BM(30), 0, /* _WPLT30, _COPY */ 219 _BM(32), _BM(32), _BM(32), /* _GLOB_DAT, JMP_SLOT, _RELATIVE */ 220 _BM(32), _BM(32), /* _UA32, PLT32 */ 221 _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */ 222 _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */ 223 _BM(10), _BM(11), -1, /* _10, _11, _64 */ 224 _BM(10), _BM(22), /* _OLO10, _HH22 */ 225 _BM(10), _BM(22), /* _HM10, _LM22 */ 226 _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */ 227 _BM(16), _BM(19), /* _WDISP16, _WDISP19 */ 228 -1, /* GLOB_JMP */ 229 _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */ 230 -1, -1, /* DISP64, PLT64 */ 231 _BM(22), _BM(13), /* HIX22, LOX10 */ 232 _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */ 233 -1, -1, _BM(16), /* REGISTER, UA64, UA16 */ 234#undef _BM 235}; 236#define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 237 238/* Process one elf relocation with addend. */ 239int 240elf_reloc(linker_file_t lf, const void *data, int type) 241{ 242 const Elf_Rela *rela; 243 const Elf_Sym *sym; 244 Elf_Addr relocbase; 245 Elf_Half *where32; 246 Elf_Addr *where; 247 Elf_Word rtype, symidx; 248 Elf_Addr value; 249 Elf_Addr mask; 250 Elf_Addr addr; 251 252 if (type != ELF_RELOC_RELA) 253 return (-1); 254 255 relocbase = (Elf_Addr)lf->address; 256 rela = (const Elf_Rela *)data; 257 where = (Elf_Addr *)(relocbase + rela->r_offset); 258 where32 = (Elf_Half *)where; 259 rtype = ELF_R_TYPE(rela->r_info); 260 symidx = ELF_R_SYM(rela->r_info); 261 262 if (rtype == R_SPARC_NONE) 263 return (0); 264 265 if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY || 266 rtype > R_SPARC_UA16) 267 return (-1); 268 269 if (RELOC_UNALIGNED(rtype)) 270 return (-1); 271 272 value = rela->r_addend; 273 274 if (RELOC_RESOLVE_SYMBOL(rtype)) { 275 /* 276 * Work around what appears to be confusion between binutils 277 * and the v9 ABI. LO10 and HI22 relocations are listed as 278 * S + A, but for STB_LOCAL symbols it seems that the value 279 * in the Elf_Sym refered to by the symbol index is wrong, 280 * instead the value is in the addend field of the Elf_Rela 281 * record. So if the symbol is local don't look it up, just 282 * use the addend as its value and add in the relocbase. 283 */ 284 sym = elf_get_sym(lf, symidx); 285 if (ELF_ST_BIND(sym->st_info) == STB_LOCAL) 286 value += relocbase; 287 else { 288 addr = elf_lookup(lf, symidx, 1); 289 if (addr == 0) 290 return (-1); 291 value += addr; 292 } 293 } 294 295 if (RELOC_PC_RELATIVE(rtype)) 296 value -= (Elf_Addr)where; 297 298 if (RELOC_BASE_RELATIVE(rtype)) 299 value += relocbase; 300 301 mask = RELOC_VALUE_BITMASK(rtype); 302 value >>= RELOC_VALUE_RIGHTSHIFT(rtype); 303 value &= mask; 304 305 if (RELOC_TARGET_SIZE(rtype) > 32) { 306 *where &= ~mask; 307 *where |= value; 308 } else { 309 *where32 &= ~mask; 310 *where32 |= value; 311 } 312 313 return (0); 314} 315 316int 317elf_cpu_load_file(linker_file_t lf __unused) 318{ 319 320 return (0); 321} 322 323int 324elf_cpu_unload_file(linker_file_t lf __unused) 325{ 326 327 return (0); 328} 329