180708Sjake/*- 280708Sjake * Copyright (c) 2001 Jake Burkholder. 385586Sjake * Copyright (c) 2000 Eduardo Horvath. 485586Sjake * Copyright (c) 1999 The NetBSD Foundation, Inc. 580708Sjake * All rights reserved. 680708Sjake * 785586Sjake * This code is derived from software contributed to The NetBSD Foundation 885586Sjake * by Paul Kranenburg. 985586Sjake * 1080708Sjake * Redistribution and use in source and binary forms, with or without 1180708Sjake * modification, are permitted provided that the following conditions 1280708Sjake * are met: 1380708Sjake * 1. Redistributions of source code must retain the above copyright 1480708Sjake * notice, this list of conditions and the following disclaimer. 1580708Sjake * 2. Redistributions in binary form must reproduce the above copyright 1680708Sjake * notice, this list of conditions and the following disclaimer in the 1780708Sjake * documentation and/or other materials provided with the distribution. 1880708Sjake * 1985586Sjake * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2085586Sjake * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2185586Sjake * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2285586Sjake * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2385586Sjake * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2485586Sjake * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2585586Sjake * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2685586Sjake * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2785586Sjake * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2885586Sjake * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2985586Sjake * POSSIBILITY OF SUCH DAMAGE. 3080708Sjake * 31219532Smarius * from: NetBSD: mdreloc.c,v 1.42 2008/04/28 20:23:04 martin Exp 3280708Sjake */ 3380708Sjake 34172708Smarius#include <sys/cdefs.h> 35172708Smarius__FBSDID("$FreeBSD$"); 36172708Smarius 3780708Sjake#include <sys/param.h> 38100384Speter#include <sys/kernel.h> 3980708Sjake#include <sys/systm.h> 40102808Sjake#include <sys/exec.h> 41102808Sjake#include <sys/imgact.h> 4280708Sjake#include <sys/linker.h> 43208453Skib#include <sys/proc.h> 44100384Speter#include <sys/sysent.h> 45100384Speter#include <sys/imgact_elf.h> 46100384Speter#include <sys/syscall.h> 47100384Speter#include <sys/signalvar.h> 48100384Speter#include <sys/vnode.h> 49102808Sjake 50102808Sjake#include <vm/vm.h> 51102808Sjake#include <vm/vm_param.h> 52102808Sjake 5380708Sjake#include <machine/elf.h> 5480708Sjake 5585586Sjake#include "linker_if.h" 5685586Sjake 57172708Smariusstatic struct sysentvec elf64_freebsd_sysvec = { 58183322Skib .sv_size = SYS_MAXSYSCALL, 59183322Skib .sv_table = sysent, 60183322Skib .sv_mask = 0, 61183322Skib .sv_sigsize = 0, 62183322Skib .sv_sigtbl = NULL, 63183322Skib .sv_errsize = 0, 64183322Skib .sv_errtbl = NULL, 65183322Skib .sv_transtrap = NULL, 66183322Skib .sv_fixup = __elfN(freebsd_fixup), 67183322Skib .sv_sendsig = sendsig, 68183322Skib .sv_sigcode = NULL, 69183322Skib .sv_szsigcode = NULL, 70183322Skib .sv_prepsyscall = NULL, 71183322Skib .sv_name = "FreeBSD ELF64", 72183322Skib .sv_coredump = __elfN(coredump), 73183322Skib .sv_imgact_try = NULL, 74183322Skib .sv_minsigstksz = MINSIGSTKSZ, 75183322Skib .sv_pagesize = PAGE_SIZE, 76183322Skib .sv_minuser = VM_MIN_ADDRESS, 77183322Skib .sv_maxuser = VM_MAXUSER_ADDRESS, 78183322Skib .sv_usrstack = USRSTACK, 79183322Skib .sv_psstrings = PS_STRINGS, 80183322Skib .sv_stackprot = VM_PROT_READ | VM_PROT_WRITE, 81183322Skib .sv_copyout_strings = exec_copyout_strings, 82183322Skib .sv_setregs = exec_setregs, 83183322Skib .sv_fixlimit = NULL, 84185169Skib .sv_maxssiz = NULL, 85208453Skib .sv_flags = SV_ABI_FREEBSD | SV_LP64, 86208453Skib .sv_set_syscall_retval = cpu_set_syscall_retval, 87208453Skib .sv_fetch_syscall_args = cpu_fetch_syscall_args, 88208453Skib .sv_syscallnames = syscallnames, 89219405Sdchagin .sv_schedtail = NULL, 90293490Sdchagin .sv_thread_detach = NULL, 91294136Sdchagin .sv_trap = NULL, 92100384Speter}; 93100384Speter 94100384Speterstatic Elf64_Brandinfo freebsd_brand_info = { 95183322Skib .brand = ELFOSABI_FREEBSD, 96183322Skib .machine = EM_SPARCV9, 97183322Skib .compat_3_brand = "FreeBSD", 98183322Skib .emul_path = NULL, 99183322Skib .interp_path = "/libexec/ld-elf.so.1", 100183322Skib .sysvec = &elf64_freebsd_sysvec, 101183322Skib .interp_newpath = NULL, 102189771Sdchagin .brand_note = &elf64_freebsd_brandnote, 103190708Sdchagin .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 104183322Skib}; 105100384Speter 106197729SbzSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST, 107219533Smarius (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info); 108100384Speter 109123742Speterstatic Elf64_Brandinfo freebsd_brand_oinfo = { 110183322Skib .brand = ELFOSABI_FREEBSD, 111183322Skib .machine = EM_SPARCV9, 112183322Skib .compat_3_brand = "FreeBSD", 113183322Skib .emul_path = NULL, 114183322Skib .interp_path = "/usr/libexec/ld-elf.so.1", 115183322Skib .sysvec = &elf64_freebsd_sysvec, 116183322Skib .interp_newpath = NULL, 117189771Sdchagin .brand_note = &elf64_freebsd_brandnote, 118190708Sdchagin .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE 119183322Skib}; 120123742Speter 121123742SpeterSYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY, 122219533Smarius (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo); 123123742Speter 124133464Smarcelvoid 125133464Smarcelelf64_dump_thread(struct thread *td __unused, void *dst __unused, 126133464Smarcel size_t *off __unused) 127133464Smarcel{ 128213104Smarius 129133464Smarcel} 130133464Smarcel 13185586Sjake/* 13285586Sjake * The following table holds for each relocation type: 13385586Sjake * - the width in bits of the memory location the relocation 13485586Sjake * applies to (not currently used) 13585586Sjake * - the number of bits the relocation value must be shifted to the 13685586Sjake * right (i.e. discard least significant bits) to fit into 13785586Sjake * the appropriate field in the instruction word. 13885586Sjake * - flags indicating whether 13985586Sjake * * the relocation involves a symbol 14085586Sjake * * the relocation is relative to the current position 14185586Sjake * * the relocation is for a GOT entry 14285586Sjake * * the relocation is relative to the load address 14385586Sjake * 14485586Sjake */ 145219532Smarius#define _RF_S 0x80000000 /* Resolve symbol */ 146219532Smarius#define _RF_A 0x40000000 /* Use addend */ 147219532Smarius#define _RF_P 0x20000000 /* Location relative */ 148219532Smarius#define _RF_G 0x10000000 /* GOT offset */ 149219532Smarius#define _RF_B 0x08000000 /* Load address relative */ 150219532Smarius#define _RF_U 0x04000000 /* Unaligned */ 151212998Skib#define _RF_X 0x02000000 /* Bare symbols, needs proc */ 152219533Smarius#define _RF_D 0x01000000 /* Use dynamic TLS offset */ 153219533Smarius#define _RF_O 0x00800000 /* Use static TLS offset */ 154219533Smarius#define _RF_I 0x00400000 /* Use TLS object ID */ 155219532Smarius#define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */ 156219532Smarius#define _RF_RS(s) ( (s) & 0xff) /* right shift */ 157172708Smariusstatic const int reloc_target_flags[] = { 15885586Sjake 0, /* NONE */ 159219532Smarius _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* 8 */ 160219532Smarius _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* 16 */ 161219532Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 32 */ 16285586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */ 16385586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */ 16485586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */ 16585586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */ 16685586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */ 167212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(10), /* HI22 */ 168212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 22 */ 169212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 13 */ 170212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* LO10 */ 17185586Sjake _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */ 17285586Sjake _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */ 17385586Sjake _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */ 17485586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */ 17585586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */ 17685586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */ 17785586Sjake _RF_SZ(32) | _RF_RS(0), /* COPY */ 17885586Sjake _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */ 17985586Sjake _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */ 180104072Sjake _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */ 181104072Sjake _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */ 18285586Sjake 18385586Sjake _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */ 18485586Sjake _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */ 18585586Sjake _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */ 18685586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */ 18785586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */ 18885586Sjake _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */ 189212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 10 */ 190212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 11 */ 191212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(64) | _RF_RS(0), /* 64 */ 19285586Sjake _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */ 193212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(42), /* HH22 */ 194212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(32), /* HM10 */ 195212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(10), /* LM22 */ 19685586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */ 19785586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */ 19885586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */ 19985586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */ 20085586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */ 20185586Sjake _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */ 202212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 7 */ 203212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 5 */ 204212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* 6 */ 20585586Sjake _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */ 20685586Sjake _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */ 207212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(10), /* HIX22 */ 208212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* LOX10 */ 209212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(22), /* H44 */ 210212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(12), /* M44 */ 211212998Skib _RF_S|_RF_A|_RF_X| _RF_SZ(32) | _RF_RS(0), /* L44 */ 21285586Sjake _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */ 21385586Sjake _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */ 21485586Sjake _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */ 215219533Smarius 216219533Smarius#if 0 217219533Smarius /* TLS */ 218219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* GD_HI22 */ 219219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GD_LO10 */ 220219533Smarius 0, /* GD_ADD */ 221219533Smarius _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* GD_CALL */ 222219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LDM_HI22 */ 223219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LDM_LO10 */ 224219533Smarius 0, /* LDM_ADD */ 225219533Smarius _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* LDM_CALL */ 226219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LDO_HIX22 */ 227219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LDO_LOX10 */ 228219533Smarius 0, /* LDO_ADD */ 229219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* IE_HI22 */ 230219533Smarius _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* IE_LO10 */ 231219533Smarius 0, /* IE_LD */ 232219533Smarius 0, /* IE_LDX */ 233219533Smarius 0, /* IE_ADD */ 234219533Smarius _RF_S|_RF_A| _RF_O| _RF_SZ(32) | _RF_RS(10), /* LE_HIX22 */ 235219533Smarius _RF_S|_RF_A| _RF_O| _RF_SZ(32) | _RF_RS(0), /* LE_LOX10 */ 236219533Smarius _RF_S| _RF_I| _RF_SZ(32) | _RF_RS(0), /* DTPMOD32 */ 237219533Smarius _RF_S| _RF_I| _RF_SZ(64) | _RF_RS(0), /* DTPMOD64 */ 238219533Smarius _RF_S|_RF_A| _RF_D| _RF_SZ(32) | _RF_RS(0), /* DTPOFF32 */ 239219533Smarius _RF_S|_RF_A| _RF_D| _RF_SZ(64) | _RF_RS(0), /* DTPOFF64 */ 240219533Smarius _RF_S|_RF_A| _RF_O| _RF_SZ(32) | _RF_RS(0), /* TPOFF32 */ 241219533Smarius _RF_S|_RF_A| _RF_O| _RF_SZ(64) | _RF_RS(0) /* TPOFF64 */ 242219533Smarius#endif 24385586Sjake}; 24485586Sjake 245104072Sjake#if 0 246213104Smariusstatic const char *const reloc_names[] = { 247219532Smarius "NONE", "8", "16", "32", "DISP_8", "DISP_16", "DISP_32", "WDISP_30", 248219532Smarius "WDISP_22", "HI22", "22", "13", "LO10", "GOT10", "GOT13", "GOT22", 249219532Smarius "PC10", "PC22", "WPLT30", "COPY", "GLOB_DAT", "JMP_SLOT", "RELATIVE", 250219532Smarius "UA_32", "PLT32", "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22", 251219532Smarius "PCPLT32", "10", "11", "64", "OLO10", "HH22", "HM10", "LM22", 252219532Smarius "PC_HH22", "PC_HM10", "PC_LM22", "WDISP16", "WDISP19", "GLOB_JMP", 253219532Smarius "7", "5", "6", "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44", 254219533Smarius "L44", "REGISTER", "UA64", "UA16", "GD_HI22", "GD_LO10", "GD_ADD", 255219533Smarius "GD_CALL", "LDM_HI22", "LDMO10", "LDM_ADD", "LDM_CALL", "LDO_HIX22", 256219533Smarius "LDO_LOX10", "LDO_ADD", "IE_HI22", "IE_LO10", "IE_LD", "IE_LDX", 257219533Smarius "IE_ADD", "LE_HIX22", "LE_LOX10", "DTPMOD32", "DTPMOD64", "DTPOFF32", 258219533Smarius "DTPOFF64", "TPOFF32", "TPOFF64" 259104072Sjake}; 260104072Sjake#endif 261104072Sjake 262219532Smarius#define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0) 263219532Smarius#define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0) 264219532Smarius#define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0) 265219532Smarius#define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0) 266219532Smarius#define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0) 267212998Skib#define RELOC_BARE_SYMBOL(t) ((reloc_target_flags[t] & _RF_X) != 0) 268219533Smarius#define RELOC_USE_TLS_DOFF(t) ((reloc_target_flags[t] & _RF_D) != 0) 269219533Smarius#define RELOC_USE_TLS_OFF(t) ((reloc_target_flags[t] & _RF_O) != 0) 270219533Smarius#define RELOC_USE_TLS_ID(t) ((reloc_target_flags[t] & _RF_I) != 0) 271219532Smarius#define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff) 272219532Smarius#define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff) 27385586Sjake 274172708Smariusstatic const long reloc_target_bitmask[] = { 275219532Smarius#define _BM(x) (~(-(1ULL << (x)))) 27685586Sjake 0, /* NONE */ 277219532Smarius _BM(8), _BM(16), _BM(32), /* 8, 16, 32 */ 27885586Sjake _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */ 27985586Sjake _BM(30), _BM(22), /* WDISP30, WDISP22 */ 280219532Smarius _BM(22), _BM(22), /* HI22, 22 */ 281219532Smarius _BM(13), _BM(10), /* 13, LO10 */ 28285586Sjake _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */ 283219339Smarius _BM(10), _BM(22), /* PC10, PC22 */ 284219339Smarius _BM(30), 0, /* WPLT30, COPY */ 285219339Smarius _BM(32), _BM(32), _BM(32), /* GLOB_DAT, JMP_SLOT, RELATIVE */ 286219339Smarius _BM(32), _BM(32), /* UA32, PLT32 */ 287219339Smarius _BM(22), _BM(10), /* HIPLT22, LOPLT10 */ 288219532Smarius _BM(32), _BM(22), _BM(10), /* PCPLT32, PCPLT22, PCPLT10 */ 289219339Smarius _BM(10), _BM(11), -1, /* 10, 11, 64 */ 290219339Smarius _BM(13), _BM(22), /* OLO10, HH22 */ 291219339Smarius _BM(10), _BM(22), /* HM10, LM22 */ 292219339Smarius _BM(22), _BM(10), _BM(22), /* PC_HH22, PC_HM10, PC_LM22 */ 293219339Smarius _BM(16), _BM(19), /* WDISP16, WDISP19 */ 29485586Sjake -1, /* GLOB_JMP */ 295219532Smarius _BM(7), _BM(5), _BM(6), /* 7, 5, 6 */ 29685586Sjake -1, -1, /* DISP64, PLT64 */ 29785586Sjake _BM(22), _BM(13), /* HIX22, LOX10 */ 29885586Sjake _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */ 29985586Sjake -1, -1, _BM(16), /* REGISTER, UA64, UA16 */ 300219533Smarius#if 0 301219533Smarius _BM(22), _BM(10), 0, _BM(30), /* GD_HI22, GD_LO10, GD_ADD, GD_CALL */ 302219533Smarius _BM(22), _BM(10), 0, /* LDM_HI22, LDMO10, LDM_ADD */ 303219533Smarius _BM(30), /* LDM_CALL */ 304219533Smarius _BM(22), _BM(10), 0, /* LDO_HIX22, LDO_LOX10, LDO_ADD */ 305219533Smarius _BM(22), _BM(10), 0, 0, /* IE_HI22, IE_LO10, IE_LD, IE_LDX */ 306219533Smarius 0, /* IE_ADD */ 307219533Smarius _BM(22), _BM(13), /* LE_HIX22, LE_LOX10 */ 308219533Smarius _BM(32), -1, /* DTPMOD32, DTPMOD64 */ 309219533Smarius _BM(32), -1, /* DTPOFF32, DTPOFF64 */ 310219533Smarius _BM(32), -1 /* TPOFF32, TPOFF64 */ 311219533Smarius#endif 31285586Sjake#undef _BM 31385586Sjake}; 314219532Smarius#define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t]) 31585586Sjake 316109605Sjakeint 317129282Speterelf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data, 318213104Smarius int type, elf_lookup_fn lookup __unused) 319109605Sjake{ 320109605Sjake const Elf_Rela *rela; 321109605Sjake Elf_Addr *where; 322109605Sjake 323109605Sjake if (type != ELF_RELOC_RELA) 324109605Sjake return (-1); 325109605Sjake 326109605Sjake rela = (const Elf_Rela *)data; 327172708Smarius if (ELF64_R_TYPE_ID(rela->r_info) != R_SPARC_RELATIVE) 328109605Sjake return (-1); 329109605Sjake 330213104Smarius where = (Elf_Addr *)(relocbase + rela->r_offset); 331213104Smarius *where = elf_relocaddr(lf, rela->r_addend + relocbase); 332109605Sjake 333109605Sjake return (0); 334109605Sjake} 335109605Sjake 33685586Sjake/* Process one elf relocation with addend. */ 33780708Sjakeint 338129282Speterelf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type, 339129282Speter elf_lookup_fn lookup) 34080708Sjake{ 34185586Sjake const Elf_Rela *rela; 342153504Smarcel Elf_Word *where32; 34385586Sjake Elf_Addr *where; 344153504Smarcel Elf_Size rtype, symidx; 34585586Sjake Elf_Addr value; 34685586Sjake Elf_Addr mask; 34798635Smux Elf_Addr addr; 348288287Skib int error; 34985586Sjake 35085586Sjake if (type != ELF_RELOC_RELA) 35185586Sjake return (-1); 35285586Sjake 35385586Sjake rela = (const Elf_Rela *)data; 35485586Sjake where = (Elf_Addr *)(relocbase + rela->r_offset); 355153504Smarcel where32 = (Elf_Word *)where; 356172708Smarius rtype = ELF64_R_TYPE_ID(rela->r_info); 35795410Smarcel symidx = ELF_R_SYM(rela->r_info); 35885586Sjake 359109605Sjake if (rtype == R_SPARC_NONE || rtype == R_SPARC_RELATIVE) 36085586Sjake return (0); 36185586Sjake 36285586Sjake if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY || 363172708Smarius rtype >= sizeof(reloc_target_bitmask) / 364219533Smarius sizeof(*reloc_target_bitmask)) { 365219533Smarius printf("kldload: unexpected relocation type %ld\n", rtype); 36685586Sjake return (-1); 367219533Smarius } 36885586Sjake 369219533Smarius if (RELOC_UNALIGNED(rtype)) { 370219533Smarius printf("kldload: unaligned relocation type %ld\n", rtype); 37185586Sjake return (-1); 372219533Smarius } 37385586Sjake 37485586Sjake value = rela->r_addend; 37585586Sjake 37685586Sjake if (RELOC_RESOLVE_SYMBOL(rtype)) { 377288287Skib error = lookup(lf, symidx, 1, &addr); 378288287Skib if (error != 0) 379107517Stmm return (-1); 380219531Smarius value += addr; 381212998Skib if (RELOC_BARE_SYMBOL(rtype)) 382212998Skib value = elf_relocaddr(lf, value); 38385586Sjake } 38485586Sjake 385172708Smarius if (rtype == R_SPARC_OLO10) 386172708Smarius value = (value & 0x3ff) + ELF64_R_TYPE_DATA(rela->r_info); 387172708Smarius 388219340Smarius if (rtype == R_SPARC_HIX22) 389219340Smarius value ^= 0xffffffffffffffff; 390219340Smarius 39185586Sjake if (RELOC_PC_RELATIVE(rtype)) 39285586Sjake value -= (Elf_Addr)where; 39385586Sjake 394213104Smarius if (RELOC_BASE_RELATIVE(rtype)) 395194784Sjeff value = elf_relocaddr(lf, value + relocbase); 39685586Sjake 39785586Sjake mask = RELOC_VALUE_BITMASK(rtype); 39885586Sjake value >>= RELOC_VALUE_RIGHTSHIFT(rtype); 39985586Sjake value &= mask; 40085586Sjake 401219533Smarius if (rtype == R_SPARC_LOX10) 402219533Smarius value |= 0x1c00; 403219533Smarius 40485586Sjake if (RELOC_TARGET_SIZE(rtype) > 32) { 40585586Sjake *where &= ~mask; 40685586Sjake *where |= value; 40785586Sjake } else { 40885586Sjake *where32 &= ~mask; 40985586Sjake *where32 |= value; 41085586Sjake } 41185586Sjake 41280708Sjake return (0); 41380708Sjake} 414105469Smarcel 415105469Smarcelint 416105469Smarcelelf_cpu_load_file(linker_file_t lf __unused) 417105469Smarcel{ 418105469Smarcel 419105469Smarcel return (0); 420105469Smarcel} 421105469Smarcel 422105469Smarcelint 423105469Smarcelelf_cpu_unload_file(linker_file_t lf __unused) 424105469Smarcel{ 425105469Smarcel 426105469Smarcel return (0); 427105469Smarcel} 428