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: stable/11/sys/sparc64/sparc64/elf_machdep.c 338867 2018-09-21 20:40:37Z markj $");
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_errsize	= 0,
62183322Skib	.sv_errtbl	= NULL,
63183322Skib	.sv_transtrap	= NULL,
64183322Skib	.sv_fixup	= __elfN(freebsd_fixup),
65183322Skib	.sv_sendsig	= sendsig,
66183322Skib	.sv_sigcode	= NULL,
67183322Skib	.sv_szsigcode	= NULL,
68183322Skib	.sv_name	= "FreeBSD ELF64",
69183322Skib	.sv_coredump	= __elfN(coredump),
70183322Skib	.sv_imgact_try	= NULL,
71183322Skib	.sv_minsigstksz	= MINSIGSTKSZ,
72183322Skib	.sv_pagesize	= PAGE_SIZE,
73183322Skib	.sv_minuser	= VM_MIN_ADDRESS,
74183322Skib	.sv_maxuser	= VM_MAXUSER_ADDRESS,
75183322Skib	.sv_usrstack	= USRSTACK,
76183322Skib	.sv_psstrings	= PS_STRINGS,
77183322Skib	.sv_stackprot	= VM_PROT_READ | VM_PROT_WRITE,
78183322Skib	.sv_copyout_strings = exec_copyout_strings,
79183322Skib	.sv_setregs	= exec_setregs,
80183322Skib	.sv_fixlimit	= NULL,
81185169Skib	.sv_maxssiz	= NULL,
82208453Skib	.sv_flags	= SV_ABI_FREEBSD | SV_LP64,
83208453Skib	.sv_set_syscall_retval = cpu_set_syscall_retval,
84208453Skib	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
85208453Skib	.sv_syscallnames = syscallnames,
86219405Sdchagin	.sv_schedtail	= NULL,
87283382Sdchagin	.sv_thread_detach = NULL,
88293613Sdchagin	.sv_trap	= NULL,
89100384Speter};
90100384Speter
91100384Speterstatic Elf64_Brandinfo freebsd_brand_info = {
92183322Skib	.brand		= ELFOSABI_FREEBSD,
93183322Skib	.machine	= EM_SPARCV9,
94183322Skib	.compat_3_brand	= "FreeBSD",
95183322Skib	.emul_path	= NULL,
96183322Skib	.interp_path	= "/libexec/ld-elf.so.1",
97183322Skib	.sysvec		= &elf64_freebsd_sysvec,
98183322Skib	.interp_newpath	= NULL,
99189771Sdchagin	.brand_note	= &elf64_freebsd_brandnote,
100190708Sdchagin	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
101183322Skib};
102100384Speter
103197729SbzSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
104219533Smarius    (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
105100384Speter
106123742Speterstatic Elf64_Brandinfo freebsd_brand_oinfo = {
107183322Skib	.brand		= ELFOSABI_FREEBSD,
108183322Skib	.machine	= EM_SPARCV9,
109183322Skib	.compat_3_brand	= "FreeBSD",
110183322Skib	.emul_path	= NULL,
111183322Skib	.interp_path	= "/usr/libexec/ld-elf.so.1",
112183322Skib	.sysvec		= &elf64_freebsd_sysvec,
113183322Skib	.interp_newpath	= NULL,
114189771Sdchagin	.brand_note	= &elf64_freebsd_brandnote,
115190708Sdchagin	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
116183322Skib};
117123742Speter
118123742SpeterSYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
119219533Smarius    (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo);
120123742Speter
121133464Smarcelvoid
122133464Smarcelelf64_dump_thread(struct thread *td __unused, void *dst __unused,
123133464Smarcel    size_t *off __unused)
124133464Smarcel{
125213104Smarius
126133464Smarcel}
127133464Smarcel
12885586Sjake/*
12985586Sjake * The following table holds for each relocation type:
13085586Sjake *	- the width in bits of the memory location the relocation
13185586Sjake *	  applies to (not currently used)
13285586Sjake *	- the number of bits the relocation value must be shifted to the
13385586Sjake *	  right (i.e. discard least significant bits) to fit into
13485586Sjake *	  the appropriate field in the instruction word.
13585586Sjake *	- flags indicating whether
13685586Sjake *		* the relocation involves a symbol
13785586Sjake *		* the relocation is relative to the current position
13885586Sjake *		* the relocation is for a GOT entry
13985586Sjake *		* the relocation is relative to the load address
14085586Sjake *
14185586Sjake */
142219532Smarius#define	_RF_S		0x80000000		/* Resolve symbol */
143219532Smarius#define	_RF_A		0x40000000		/* Use addend */
144219532Smarius#define	_RF_P		0x20000000		/* Location relative */
145219532Smarius#define	_RF_G		0x10000000		/* GOT offset */
146219532Smarius#define	_RF_B		0x08000000		/* Load address relative */
147219532Smarius#define	_RF_U		0x04000000		/* Unaligned */
148212998Skib#define	_RF_X		0x02000000		/* Bare symbols, needs proc */
149219533Smarius#define	_RF_D		0x01000000		/* Use dynamic TLS offset */
150219533Smarius#define	_RF_O		0x00800000		/* Use static TLS offset */
151219533Smarius#define	_RF_I		0x00400000		/* Use TLS object ID */
152219532Smarius#define	_RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
153219532Smarius#define	_RF_RS(s)	( (s) & 0xff)		/* right shift */
154172708Smariusstatic const int reloc_target_flags[] = {
15585586Sjake	0,							/* NONE */
156219532Smarius	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* 8 */
157219532Smarius	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* 16 */
158219532Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 32 */
15985586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
16085586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
16185586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
16285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
16385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
164212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(10),	/* HI22 */
165212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 22 */
166212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 13 */
167212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* LO10 */
16885586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
16985586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
17085586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
17185586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
17285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
17385586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
17485586Sjake				_RF_SZ(32) | _RF_RS(0),		/* COPY */
17585586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
17685586Sjake				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
177104072Sjake	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
178104072Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
17985586Sjake
18085586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
18185586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
18285586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
18385586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
18485586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
18585586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
186212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 10 */
187212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 11 */
188212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(64) | _RF_RS(0),		/* 64 */
18985586Sjake	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
190212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(42),	/* HH22 */
191212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(32),	/* HM10 */
192212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(10),	/* LM22 */
19385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
19485586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
19585586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
19685586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
19785586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
19885586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
199212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 7 */
200212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 5 */
201212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 6 */
20285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
20385586Sjake	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
204212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
205212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
206212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(22),	/* H44 */
207212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(12),	/* M44 */
208212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* L44 */
20985586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
21085586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
21185586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
212219533Smarius
213219533Smarius#if 0
214219533Smarius	/* TLS */
215219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* GD_HI22 */
216219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GD_LO10 */
217219533Smarius	0,							/* GD_ADD */
218219533Smarius	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* GD_CALL */
219219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LDM_HI22 */
220219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LDM_LO10 */
221219533Smarius	0,							/* LDM_ADD */
222219533Smarius	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* LDM_CALL */
223219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LDO_HIX22 */
224219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LDO_LOX10 */
225219533Smarius	0,							/* LDO_ADD */
226219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* IE_HI22 */
227219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* IE_LO10 */
228219533Smarius	0,							/* IE_LD */
229219533Smarius	0,							/* IE_LDX */
230219533Smarius	0,							/* IE_ADD */
231219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(32) | _RF_RS(10),	/* LE_HIX22 */
232219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(32) | _RF_RS(0),		/* LE_LOX10 */
233219533Smarius	_RF_S|		_RF_I|	_RF_SZ(32) | _RF_RS(0),		/* DTPMOD32 */
234219533Smarius	_RF_S|		_RF_I|	_RF_SZ(64) | _RF_RS(0),		/* DTPMOD64 */
235219533Smarius	_RF_S|_RF_A|	_RF_D|	_RF_SZ(32) | _RF_RS(0),		/* DTPOFF32 */
236219533Smarius	_RF_S|_RF_A|	_RF_D|	_RF_SZ(64) | _RF_RS(0),		/* DTPOFF64 */
237219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(32) | _RF_RS(0),		/* TPOFF32 */
238219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(64) | _RF_RS(0)		/* TPOFF64 */
239219533Smarius#endif
24085586Sjake};
24185586Sjake
242104072Sjake#if 0
243213104Smariusstatic const char *const reloc_names[] = {
244219532Smarius	"NONE", "8", "16", "32", "DISP_8", "DISP_16", "DISP_32", "WDISP_30",
245219532Smarius	"WDISP_22", "HI22", "22", "13", "LO10", "GOT10", "GOT13", "GOT22",
246219532Smarius	"PC10", "PC22", "WPLT30", "COPY", "GLOB_DAT", "JMP_SLOT", "RELATIVE",
247219532Smarius	"UA_32", "PLT32", "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22",
248219532Smarius	"PCPLT32", "10", "11", "64", "OLO10", "HH22", "HM10", "LM22",
249219532Smarius	"PC_HH22", "PC_HM10", "PC_LM22", "WDISP16", "WDISP19", "GLOB_JMP",
250219532Smarius	"7", "5", "6", "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
251219533Smarius	"L44", "REGISTER", "UA64", "UA16", "GD_HI22", "GD_LO10", "GD_ADD",
252219533Smarius	"GD_CALL", "LDM_HI22", "LDMO10", "LDM_ADD", "LDM_CALL", "LDO_HIX22",
253219533Smarius	"LDO_LOX10", "LDO_ADD", "IE_HI22", "IE_LO10", "IE_LD", "IE_LDX",
254219533Smarius	"IE_ADD", "LE_HIX22", "LE_LOX10", "DTPMOD32", "DTPMOD64", "DTPOFF32",
255219533Smarius	"DTPOFF64", "TPOFF32", "TPOFF64"
256104072Sjake};
257104072Sjake#endif
258104072Sjake
259219532Smarius#define	RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
260219532Smarius#define	RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
261219532Smarius#define	RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
262219532Smarius#define	RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
263219532Smarius#define	RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
264212998Skib#define	RELOC_BARE_SYMBOL(t)		((reloc_target_flags[t] & _RF_X) != 0)
265219533Smarius#define	RELOC_USE_TLS_DOFF(t)		((reloc_target_flags[t] & _RF_D) != 0)
266219533Smarius#define	RELOC_USE_TLS_OFF(t)		((reloc_target_flags[t] & _RF_O) != 0)
267219533Smarius#define	RELOC_USE_TLS_ID(t)		((reloc_target_flags[t] & _RF_I) != 0)
268219532Smarius#define	RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
269219532Smarius#define	RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
27085586Sjake
271172708Smariusstatic const long reloc_target_bitmask[] = {
272219532Smarius#define	_BM(x)	(~(-(1ULL << (x))))
27385586Sjake	0,				/* NONE */
274219532Smarius	_BM(8), _BM(16), _BM(32),	/* 8, 16, 32 */
27585586Sjake	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
27685586Sjake	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
277219532Smarius	_BM(22), _BM(22),		/* HI22, 22 */
278219532Smarius	_BM(13), _BM(10),		/* 13, LO10 */
27985586Sjake	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
280219339Smarius	_BM(10), _BM(22),		/* PC10, PC22 */
281219339Smarius	_BM(30), 0,			/* WPLT30, COPY */
282219339Smarius	_BM(32), _BM(32), _BM(32),	/* GLOB_DAT, JMP_SLOT, RELATIVE */
283219339Smarius	_BM(32), _BM(32),		/* UA32, PLT32 */
284219339Smarius	_BM(22), _BM(10),		/* HIPLT22, LOPLT10 */
285219532Smarius	_BM(32), _BM(22), _BM(10),	/* PCPLT32, PCPLT22, PCPLT10 */
286219339Smarius	_BM(10), _BM(11), -1,		/* 10, 11, 64 */
287219339Smarius	_BM(13), _BM(22),		/* OLO10, HH22 */
288219339Smarius	_BM(10), _BM(22),		/* HM10, LM22 */
289219339Smarius	_BM(22), _BM(10), _BM(22),	/* PC_HH22, PC_HM10, PC_LM22 */
290219339Smarius	_BM(16), _BM(19),		/* WDISP16, WDISP19 */
29185586Sjake	-1,				/* GLOB_JMP */
292219532Smarius	_BM(7), _BM(5), _BM(6),		/* 7, 5, 6 */
29385586Sjake	-1, -1,				/* DISP64, PLT64 */
29485586Sjake	_BM(22), _BM(13),		/* HIX22, LOX10 */
29585586Sjake	_BM(22), _BM(10), _BM(13),	/* H44, M44, L44 */
29685586Sjake	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
297219533Smarius#if 0
298219533Smarius	_BM(22), _BM(10), 0, _BM(30),	/* GD_HI22, GD_LO10, GD_ADD, GD_CALL */
299219533Smarius	_BM(22), _BM(10), 0,		/* LDM_HI22, LDMO10, LDM_ADD */
300219533Smarius	_BM(30),			/* LDM_CALL */
301219533Smarius	_BM(22), _BM(10), 0,		/* LDO_HIX22, LDO_LOX10, LDO_ADD */
302219533Smarius	_BM(22), _BM(10), 0, 0,		/* IE_HI22, IE_LO10, IE_LD, IE_LDX */
303219533Smarius	0,				/* IE_ADD */
304219533Smarius	_BM(22), _BM(13),		/* LE_HIX22, LE_LOX10 */
305219533Smarius	_BM(32), -1,			/* DTPMOD32, DTPMOD64 */
306219533Smarius	_BM(32), -1,			/* DTPOFF32, DTPOFF64 */
307219533Smarius	_BM(32), -1			/* TPOFF32, TPOFF64 */
308219533Smarius#endif
30985586Sjake#undef _BM
31085586Sjake};
311219532Smarius#define	RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
31285586Sjake
313338867Smarkjbool
314338867Smarkjelf_is_ifunc_reloc(Elf_Size r_info __unused)
315338867Smarkj{
316338867Smarkj
317338867Smarkj	return (false);
318338867Smarkj}
319338867Smarkj
320109605Sjakeint
321129282Speterelf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
322213104Smarius    int type, elf_lookup_fn lookup __unused)
323109605Sjake{
324109605Sjake	const Elf_Rela *rela;
325109605Sjake	Elf_Addr *where;
326109605Sjake
327109605Sjake	if (type != ELF_RELOC_RELA)
328109605Sjake		return (-1);
329109605Sjake
330109605Sjake	rela = (const Elf_Rela *)data;
331172708Smarius	if (ELF64_R_TYPE_ID(rela->r_info) != R_SPARC_RELATIVE)
332109605Sjake		return (-1);
333109605Sjake
334213104Smarius	where = (Elf_Addr *)(relocbase + rela->r_offset);
335213104Smarius	*where = elf_relocaddr(lf, rela->r_addend + relocbase);
336109605Sjake
337109605Sjake	return (0);
338109605Sjake}
339109605Sjake
34085586Sjake/* Process one elf relocation with addend. */
34180708Sjakeint
342129282Speterelf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
343129282Speter    elf_lookup_fn lookup)
34480708Sjake{
34585586Sjake	const Elf_Rela *rela;
346153504Smarcel	Elf_Word *where32;
34785586Sjake	Elf_Addr *where;
348153504Smarcel	Elf_Size rtype, symidx;
34985586Sjake	Elf_Addr value;
35085586Sjake	Elf_Addr mask;
35198635Smux	Elf_Addr addr;
352288000Skib	int error;
35385586Sjake
35485586Sjake	if (type != ELF_RELOC_RELA)
35585586Sjake		return (-1);
35685586Sjake
35785586Sjake	rela = (const Elf_Rela *)data;
35885586Sjake	where = (Elf_Addr *)(relocbase + rela->r_offset);
359153504Smarcel	where32 = (Elf_Word *)where;
360172708Smarius	rtype = ELF64_R_TYPE_ID(rela->r_info);
36195410Smarcel	symidx = ELF_R_SYM(rela->r_info);
36285586Sjake
363109605Sjake	if (rtype == R_SPARC_NONE || rtype == R_SPARC_RELATIVE)
36485586Sjake		return (0);
36585586Sjake
36685586Sjake	if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY ||
367298352Spfg	    rtype >= nitems(reloc_target_bitmask)) {
368219533Smarius		printf("kldload: unexpected relocation type %ld\n", rtype);
36985586Sjake		return (-1);
370219533Smarius	}
37185586Sjake
372219533Smarius	if (RELOC_UNALIGNED(rtype)) {
373219533Smarius		printf("kldload: unaligned relocation type %ld\n", rtype);
37485586Sjake		return (-1);
375219533Smarius	}
37685586Sjake
37785586Sjake	value = rela->r_addend;
37885586Sjake
37985586Sjake	if (RELOC_RESOLVE_SYMBOL(rtype)) {
380288000Skib		error = lookup(lf, symidx, 1, &addr);
381288000Skib		if (error != 0)
382107517Stmm			return (-1);
383219531Smarius		value += addr;
384212998Skib		if (RELOC_BARE_SYMBOL(rtype))
385212998Skib			value = elf_relocaddr(lf, value);
38685586Sjake	}
38785586Sjake
388172708Smarius	if (rtype == R_SPARC_OLO10)
389172708Smarius		value = (value & 0x3ff) + ELF64_R_TYPE_DATA(rela->r_info);
390172708Smarius
391219340Smarius	if (rtype == R_SPARC_HIX22)
392219340Smarius		value ^= 0xffffffffffffffff;
393219340Smarius
39485586Sjake	if (RELOC_PC_RELATIVE(rtype))
39585586Sjake		value -= (Elf_Addr)where;
39685586Sjake
397213104Smarius	if (RELOC_BASE_RELATIVE(rtype))
398194784Sjeff		value = elf_relocaddr(lf, value + relocbase);
39985586Sjake
40085586Sjake	mask = RELOC_VALUE_BITMASK(rtype);
40185586Sjake	value >>= RELOC_VALUE_RIGHTSHIFT(rtype);
40285586Sjake	value &= mask;
40385586Sjake
404219533Smarius	if (rtype == R_SPARC_LOX10)
405219533Smarius		value |= 0x1c00;
406219533Smarius
40785586Sjake	if (RELOC_TARGET_SIZE(rtype) > 32) {
40885586Sjake		*where &= ~mask;
40985586Sjake		*where |= value;
41085586Sjake	} else {
41185586Sjake		*where32 &= ~mask;
41285586Sjake		*where32 |= value;
41385586Sjake	}
41485586Sjake
41580708Sjake	return (0);
41680708Sjake}
417105469Smarcel
418105469Smarcelint
419105469Smarcelelf_cpu_load_file(linker_file_t lf __unused)
420105469Smarcel{
421105469Smarcel
422105469Smarcel	return (0);
423105469Smarcel}
424105469Smarcel
425105469Smarcelint
426105469Smarcelelf_cpu_unload_file(linker_file_t lf __unused)
427105469Smarcel{
428105469Smarcel
429105469Smarcel	return (0);
430105469Smarcel}
431