elf_machdep.c revision 219533
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: head/sys/sparc64/sparc64/elf_machdep.c 219533 2011-03-11 21:08:02Z marius $");
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,
90100384Speter};
91100384Speter
92100384Speterstatic Elf64_Brandinfo freebsd_brand_info = {
93183322Skib	.brand		= ELFOSABI_FREEBSD,
94183322Skib	.machine	= EM_SPARCV9,
95183322Skib	.compat_3_brand	= "FreeBSD",
96183322Skib	.emul_path	= NULL,
97183322Skib	.interp_path	= "/libexec/ld-elf.so.1",
98183322Skib	.sysvec		= &elf64_freebsd_sysvec,
99183322Skib	.interp_newpath	= NULL,
100189771Sdchagin	.brand_note	= &elf64_freebsd_brandnote,
101190708Sdchagin	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
102183322Skib};
103100384Speter
104197729SbzSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
105219533Smarius    (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_info);
106100384Speter
107123742Speterstatic Elf64_Brandinfo freebsd_brand_oinfo = {
108183322Skib	.brand		= ELFOSABI_FREEBSD,
109183322Skib	.machine	= EM_SPARCV9,
110183322Skib	.compat_3_brand	= "FreeBSD",
111183322Skib	.emul_path	= NULL,
112183322Skib	.interp_path	= "/usr/libexec/ld-elf.so.1",
113183322Skib	.sysvec		= &elf64_freebsd_sysvec,
114183322Skib	.interp_newpath	= NULL,
115189771Sdchagin	.brand_note	= &elf64_freebsd_brandnote,
116190708Sdchagin	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
117183322Skib};
118123742Speter
119123742SpeterSYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
120219533Smarius    (sysinit_cfunc_t)elf64_insert_brand_entry, &freebsd_brand_oinfo);
121123742Speter
122133464Smarcelvoid
123133464Smarcelelf64_dump_thread(struct thread *td __unused, void *dst __unused,
124133464Smarcel    size_t *off __unused)
125133464Smarcel{
126213104Smarius
127133464Smarcel}
128133464Smarcel
12985586Sjake/*
13085586Sjake * The following table holds for each relocation type:
13185586Sjake *	- the width in bits of the memory location the relocation
13285586Sjake *	  applies to (not currently used)
13385586Sjake *	- the number of bits the relocation value must be shifted to the
13485586Sjake *	  right (i.e. discard least significant bits) to fit into
13585586Sjake *	  the appropriate field in the instruction word.
13685586Sjake *	- flags indicating whether
13785586Sjake *		* the relocation involves a symbol
13885586Sjake *		* the relocation is relative to the current position
13985586Sjake *		* the relocation is for a GOT entry
14085586Sjake *		* the relocation is relative to the load address
14185586Sjake *
14285586Sjake */
143219532Smarius#define	_RF_S		0x80000000		/* Resolve symbol */
144219532Smarius#define	_RF_A		0x40000000		/* Use addend */
145219532Smarius#define	_RF_P		0x20000000		/* Location relative */
146219532Smarius#define	_RF_G		0x10000000		/* GOT offset */
147219532Smarius#define	_RF_B		0x08000000		/* Load address relative */
148219532Smarius#define	_RF_U		0x04000000		/* Unaligned */
149212998Skib#define	_RF_X		0x02000000		/* Bare symbols, needs proc */
150219533Smarius#define	_RF_D		0x01000000		/* Use dynamic TLS offset */
151219533Smarius#define	_RF_O		0x00800000		/* Use static TLS offset */
152219533Smarius#define	_RF_I		0x00400000		/* Use TLS object ID */
153219532Smarius#define	_RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
154219532Smarius#define	_RF_RS(s)	( (s) & 0xff)		/* right shift */
155172708Smariusstatic const int reloc_target_flags[] = {
15685586Sjake	0,							/* NONE */
157219532Smarius	_RF_S|_RF_A|		_RF_SZ(8)  | _RF_RS(0),		/* 8 */
158219532Smarius	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* 16 */
159219532Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 32 */
16085586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(8)  | _RF_RS(0),		/* DISP_8 */
16185586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* DISP_16 */
16285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* DISP_32 */
16385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_30 */
16485586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP_22 */
165212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(10),	/* HI22 */
166212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 22 */
167212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 13 */
168212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* LO10 */
16985586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT10 */
17085586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(0),		/* GOT13 */
17185586Sjake	_RF_G|			_RF_SZ(32) | _RF_RS(10),	/* GOT22 */
17285586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC10 */
17385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC22 */
17485586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WPLT30 */
17585586Sjake				_RF_SZ(32) | _RF_RS(0),		/* COPY */
17685586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* GLOB_DAT */
17785586Sjake				_RF_SZ(32) | _RF_RS(0),		/* JMP_SLOT */
178104072Sjake	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/* RELATIVE */
179104072Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(32) | _RF_RS(0),		/* UA_32 */
18085586Sjake
18185586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
18285586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(10),	/* HIPLT22 */
18385586Sjake	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LOPLT10 */
18485586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT32 */
18585586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PCPLT22 */
18685586Sjake	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PCPLT10 */
187212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 10 */
188212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 11 */
189212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(64) | _RF_RS(0),		/* 64 */
19085586Sjake	_RF_S|_RF_A|/*extra*/	_RF_SZ(32) | _RF_RS(0),		/* OLO10 */
191212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(42),	/* HH22 */
192212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(32),	/* HM10 */
193212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(10),	/* LM22 */
19485586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(42),	/* PC_HH22 */
19585586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(32),	/* PC_HM10 */
19685586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(10),	/* PC_LM22 */
19785586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP16 */
19885586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* WDISP19 */
19985586Sjake	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_JMP */
200212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 7 */
201212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 5 */
202212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* 6 */
20385586Sjake	_RF_S|_RF_A|_RF_P|	_RF_SZ(64) | _RF_RS(0),		/* DISP64 */
20485586Sjake	      _RF_A|		_RF_SZ(64) | _RF_RS(0),		/* PLT64 */
205212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(10),	/* HIX22 */
206212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* LOX10 */
207212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(22),	/* H44 */
208212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(12),	/* M44 */
209212998Skib	_RF_S|_RF_A|_RF_X|	_RF_SZ(32) | _RF_RS(0),		/* L44 */
21085586Sjake	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/* REGISTER */
21185586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(64) | _RF_RS(0),		/* UA64 */
21285586Sjake	_RF_S|_RF_A|	_RF_U|	_RF_SZ(16) | _RF_RS(0),		/* UA16 */
213219533Smarius
214219533Smarius#if 0
215219533Smarius	/* TLS */
216219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* GD_HI22 */
217219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GD_LO10 */
218219533Smarius	0,							/* GD_ADD */
219219533Smarius	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* GD_CALL */
220219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LDM_HI22 */
221219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LDM_LO10 */
222219533Smarius	0,							/* LDM_ADD */
223219533Smarius	      _RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(2),		/* LDM_CALL */
224219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* LDO_HIX22 */
225219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* LDO_LOX10 */
226219533Smarius	0,							/* LDO_ADD */
227219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(10),	/* IE_HI22 */
228219533Smarius	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* IE_LO10 */
229219533Smarius	0,							/* IE_LD */
230219533Smarius	0,							/* IE_LDX */
231219533Smarius	0,							/* IE_ADD */
232219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(32) | _RF_RS(10),	/* LE_HIX22 */
233219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(32) | _RF_RS(0),		/* LE_LOX10 */
234219533Smarius	_RF_S|		_RF_I|	_RF_SZ(32) | _RF_RS(0),		/* DTPMOD32 */
235219533Smarius	_RF_S|		_RF_I|	_RF_SZ(64) | _RF_RS(0),		/* DTPMOD64 */
236219533Smarius	_RF_S|_RF_A|	_RF_D|	_RF_SZ(32) | _RF_RS(0),		/* DTPOFF32 */
237219533Smarius	_RF_S|_RF_A|	_RF_D|	_RF_SZ(64) | _RF_RS(0),		/* DTPOFF64 */
238219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(32) | _RF_RS(0),		/* TPOFF32 */
239219533Smarius	_RF_S|_RF_A|	_RF_O|	_RF_SZ(64) | _RF_RS(0)		/* TPOFF64 */
240219533Smarius#endif
24185586Sjake};
24285586Sjake
243104072Sjake#if 0
244213104Smariusstatic const char *const reloc_names[] = {
245219532Smarius	"NONE", "8", "16", "32", "DISP_8", "DISP_16", "DISP_32", "WDISP_30",
246219532Smarius	"WDISP_22", "HI22", "22", "13", "LO10", "GOT10", "GOT13", "GOT22",
247219532Smarius	"PC10", "PC22", "WPLT30", "COPY", "GLOB_DAT", "JMP_SLOT", "RELATIVE",
248219532Smarius	"UA_32", "PLT32", "HIPLT22", "LOPLT10", "LOPLT10", "PCPLT22",
249219532Smarius	"PCPLT32", "10", "11", "64", "OLO10", "HH22", "HM10", "LM22",
250219532Smarius	"PC_HH22", "PC_HM10", "PC_LM22", "WDISP16", "WDISP19", "GLOB_JMP",
251219532Smarius	"7", "5", "6", "DISP64", "PLT64", "HIX22", "LOX10", "H44", "M44",
252219533Smarius	"L44", "REGISTER", "UA64", "UA16", "GD_HI22", "GD_LO10", "GD_ADD",
253219533Smarius	"GD_CALL", "LDM_HI22", "LDMO10", "LDM_ADD", "LDM_CALL", "LDO_HIX22",
254219533Smarius	"LDO_LOX10", "LDO_ADD", "IE_HI22", "IE_LO10", "IE_LD", "IE_LDX",
255219533Smarius	"IE_ADD", "LE_HIX22", "LE_LOX10", "DTPMOD32", "DTPMOD64", "DTPOFF32",
256219533Smarius	"DTPOFF64", "TPOFF32", "TPOFF64"
257104072Sjake};
258104072Sjake#endif
259104072Sjake
260219532Smarius#define	RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
261219532Smarius#define	RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
262219532Smarius#define	RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
263219532Smarius#define	RELOC_UNALIGNED(t)		((reloc_target_flags[t] & _RF_U) != 0)
264219532Smarius#define	RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
265212998Skib#define	RELOC_BARE_SYMBOL(t)		((reloc_target_flags[t] & _RF_X) != 0)
266219533Smarius#define	RELOC_USE_TLS_DOFF(t)		((reloc_target_flags[t] & _RF_D) != 0)
267219533Smarius#define	RELOC_USE_TLS_OFF(t)		((reloc_target_flags[t] & _RF_O) != 0)
268219533Smarius#define	RELOC_USE_TLS_ID(t)		((reloc_target_flags[t] & _RF_I) != 0)
269219532Smarius#define	RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
270219532Smarius#define	RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
27185586Sjake
272172708Smariusstatic const long reloc_target_bitmask[] = {
273219532Smarius#define	_BM(x)	(~(-(1ULL << (x))))
27485586Sjake	0,				/* NONE */
275219532Smarius	_BM(8), _BM(16), _BM(32),	/* 8, 16, 32 */
27685586Sjake	_BM(8), _BM(16), _BM(32),	/* DISP8, DISP16, DISP32 */
27785586Sjake	_BM(30), _BM(22),		/* WDISP30, WDISP22 */
278219532Smarius	_BM(22), _BM(22),		/* HI22, 22 */
279219532Smarius	_BM(13), _BM(10),		/* 13, LO10 */
28085586Sjake	_BM(10), _BM(13), _BM(22),	/* GOT10, GOT13, GOT22 */
281219339Smarius	_BM(10), _BM(22),		/* PC10, PC22 */
282219339Smarius	_BM(30), 0,			/* WPLT30, COPY */
283219339Smarius	_BM(32), _BM(32), _BM(32),	/* GLOB_DAT, JMP_SLOT, RELATIVE */
284219339Smarius	_BM(32), _BM(32),		/* UA32, PLT32 */
285219339Smarius	_BM(22), _BM(10),		/* HIPLT22, LOPLT10 */
286219532Smarius	_BM(32), _BM(22), _BM(10),	/* PCPLT32, PCPLT22, PCPLT10 */
287219339Smarius	_BM(10), _BM(11), -1,		/* 10, 11, 64 */
288219339Smarius	_BM(13), _BM(22),		/* OLO10, HH22 */
289219339Smarius	_BM(10), _BM(22),		/* HM10, LM22 */
290219339Smarius	_BM(22), _BM(10), _BM(22),	/* PC_HH22, PC_HM10, PC_LM22 */
291219339Smarius	_BM(16), _BM(19),		/* WDISP16, WDISP19 */
29285586Sjake	-1,				/* GLOB_JMP */
293219532Smarius	_BM(7), _BM(5), _BM(6),		/* 7, 5, 6 */
29485586Sjake	-1, -1,				/* DISP64, PLT64 */
29585586Sjake	_BM(22), _BM(13),		/* HIX22, LOX10 */
29685586Sjake	_BM(22), _BM(10), _BM(13),	/* H44, M44, L44 */
29785586Sjake	-1, -1, _BM(16),		/* REGISTER, UA64, UA16 */
298219533Smarius#if 0
299219533Smarius	_BM(22), _BM(10), 0, _BM(30),	/* GD_HI22, GD_LO10, GD_ADD, GD_CALL */
300219533Smarius	_BM(22), _BM(10), 0,		/* LDM_HI22, LDMO10, LDM_ADD */
301219533Smarius	_BM(30),			/* LDM_CALL */
302219533Smarius	_BM(22), _BM(10), 0,		/* LDO_HIX22, LDO_LOX10, LDO_ADD */
303219533Smarius	_BM(22), _BM(10), 0, 0,		/* IE_HI22, IE_LO10, IE_LD, IE_LDX */
304219533Smarius	0,				/* IE_ADD */
305219533Smarius	_BM(22), _BM(13),		/* LE_HIX22, LE_LOX10 */
306219533Smarius	_BM(32), -1,			/* DTPMOD32, DTPMOD64 */
307219533Smarius	_BM(32), -1,			/* DTPOFF32, DTPOFF64 */
308219533Smarius	_BM(32), -1			/* TPOFF32, TPOFF64 */
309219533Smarius#endif
31085586Sjake#undef _BM
31185586Sjake};
312219532Smarius#define	RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
31385586Sjake
314109605Sjakeint
315129282Speterelf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
316213104Smarius    int type, elf_lookup_fn lookup __unused)
317109605Sjake{
318109605Sjake	const Elf_Rela *rela;
319109605Sjake	Elf_Addr *where;
320109605Sjake
321109605Sjake	if (type != ELF_RELOC_RELA)
322109605Sjake		return (-1);
323109605Sjake
324109605Sjake	rela = (const Elf_Rela *)data;
325172708Smarius	if (ELF64_R_TYPE_ID(rela->r_info) != R_SPARC_RELATIVE)
326109605Sjake		return (-1);
327109605Sjake
328213104Smarius	where = (Elf_Addr *)(relocbase + rela->r_offset);
329213104Smarius	*where = elf_relocaddr(lf, rela->r_addend + relocbase);
330109605Sjake
331109605Sjake	return (0);
332109605Sjake}
333109605Sjake
33485586Sjake/* Process one elf relocation with addend. */
33580708Sjakeint
336129282Speterelf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
337129282Speter    elf_lookup_fn lookup)
33880708Sjake{
33985586Sjake	const Elf_Rela *rela;
340153504Smarcel	Elf_Word *where32;
34185586Sjake	Elf_Addr *where;
342153504Smarcel	Elf_Size rtype, symidx;
34385586Sjake	Elf_Addr value;
34485586Sjake	Elf_Addr mask;
34598635Smux	Elf_Addr addr;
34685586Sjake
34785586Sjake	if (type != ELF_RELOC_RELA)
34885586Sjake		return (-1);
34985586Sjake
35085586Sjake	rela = (const Elf_Rela *)data;
35185586Sjake	where = (Elf_Addr *)(relocbase + rela->r_offset);
352153504Smarcel	where32 = (Elf_Word *)where;
353172708Smarius	rtype = ELF64_R_TYPE_ID(rela->r_info);
35495410Smarcel	symidx = ELF_R_SYM(rela->r_info);
35585586Sjake
356109605Sjake	if (rtype == R_SPARC_NONE || rtype == R_SPARC_RELATIVE)
35785586Sjake		return (0);
35885586Sjake
35985586Sjake	if (rtype == R_SPARC_JMP_SLOT || rtype == R_SPARC_COPY ||
360172708Smarius	    rtype >= sizeof(reloc_target_bitmask) /
361219533Smarius	    sizeof(*reloc_target_bitmask)) {
362219533Smarius		printf("kldload: unexpected relocation type %ld\n", rtype);
36385586Sjake		return (-1);
364219533Smarius	}
36585586Sjake
366219533Smarius	if (RELOC_UNALIGNED(rtype)) {
367219533Smarius		printf("kldload: unaligned relocation type %ld\n", rtype);
36885586Sjake		return (-1);
369219533Smarius	}
37085586Sjake
37185586Sjake	value = rela->r_addend;
37285586Sjake
37385586Sjake	if (RELOC_RESOLVE_SYMBOL(rtype)) {
374129282Speter		addr = lookup(lf, symidx, 1);
375107517Stmm		if (addr == 0)
376107517Stmm			return (-1);
377219531Smarius		value += addr;
378212998Skib		if (RELOC_BARE_SYMBOL(rtype))
379212998Skib			value = elf_relocaddr(lf, value);
38085586Sjake	}
38185586Sjake
382172708Smarius	if (rtype == R_SPARC_OLO10)
383172708Smarius		value = (value & 0x3ff) + ELF64_R_TYPE_DATA(rela->r_info);
384172708Smarius
385219340Smarius	if (rtype == R_SPARC_HIX22)
386219340Smarius		value ^= 0xffffffffffffffff;
387219340Smarius
38885586Sjake	if (RELOC_PC_RELATIVE(rtype))
38985586Sjake		value -= (Elf_Addr)where;
39085586Sjake
391213104Smarius	if (RELOC_BASE_RELATIVE(rtype))
392194784Sjeff		value = elf_relocaddr(lf, value + relocbase);
39385586Sjake
39485586Sjake	mask = RELOC_VALUE_BITMASK(rtype);
39585586Sjake	value >>= RELOC_VALUE_RIGHTSHIFT(rtype);
39685586Sjake	value &= mask;
39785586Sjake
398219533Smarius	if (rtype == R_SPARC_LOX10)
399219533Smarius		value |= 0x1c00;
400219533Smarius
40185586Sjake	if (RELOC_TARGET_SIZE(rtype) > 32) {
40285586Sjake		*where &= ~mask;
40385586Sjake		*where |= value;
40485586Sjake	} else {
40585586Sjake		*where32 &= ~mask;
40685586Sjake		*where32 |= value;
40785586Sjake	}
40885586Sjake
40980708Sjake	return (0);
41080708Sjake}
411105469Smarcel
412105469Smarcelint
413105469Smarcelelf_cpu_load_file(linker_file_t lf __unused)
414105469Smarcel{
415105469Smarcel
416105469Smarcel	return (0);
417105469Smarcel}
418105469Smarcel
419105469Smarcelint
420105469Smarcelelf_cpu_unload_file(linker_file_t lf __unused)
421105469Smarcel{
422105469Smarcel
423105469Smarcel	return (0);
424105469Smarcel}
425