137a3ec6peter/*-
2d754734pfg * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3d754734pfg *
437a3ec6peter * Copyright 1996-1998 John D. Polstra.
537a3ec6peter * All rights reserved.
637a3ec6peter *
737a3ec6peter * Redistribution and use in source and binary forms, with or without
837a3ec6peter * modification, are permitted provided that the following conditions
937a3ec6peter * are met:
1037a3ec6peter * 1. Redistributions of source code must retain the above copyright
1137a3ec6peter *    notice, this list of conditions and the following disclaimer.
1237a3ec6peter * 2. Redistributions in binary form must reproduce the above copyright
1337a3ec6peter *    notice, this list of conditions and the following disclaimer in the
1437a3ec6peter *    documentation and/or other materials provided with the distribution.
1537a3ec6peter *
1637a3ec6peter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1737a3ec6peter * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1837a3ec6peter * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1937a3ec6peter * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2037a3ec6peter * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2137a3ec6peter * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2237a3ec6peter * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2337a3ec6peter * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2437a3ec6peter * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2537a3ec6peter * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2637a3ec6peter */
2737a3ec6peter
2802a4f42obrien#include <sys/cdefs.h>
2902a4f42obrien__FBSDID("$FreeBSD$");
3002a4f42obrien
3137a3ec6peter#include <sys/param.h>
32cc7b2e4peter#include <sys/kernel.h>
3337a3ec6peter#include <sys/systm.h>
34ce650f8jake#include <sys/exec.h>
35ce650f8jake#include <sys/imgact.h>
3637a3ec6peter#include <sys/linker.h>
374208ccbkib#include <sys/proc.h>
38cc7b2e4peter#include <sys/sysent.h>
39cc7b2e4peter#include <sys/imgact_elf.h>
40cc7b2e4peter#include <sys/syscall.h>
41cc7b2e4peter#include <sys/signalvar.h>
42cc7b2e4peter#include <sys/vnode.h>
43ce650f8jake
44ce650f8jake#include <vm/vm.h>
45ce650f8jake#include <vm/pmap.h>
46ce650f8jake#include <vm/vm_param.h>
47ce650f8jake
4837a3ec6peter#include <machine/elf.h>
491671ac9jhb#include <machine/fpu.h>
50cc7b2e4peter#include <machine/md_var.h>
51cc7b2e4peter
5245949ccpeterstruct sysentvec elf64_freebsd_sysvec = {
53c500808kib	.sv_size	= SYS_MAXSYSCALL,
54c500808kib	.sv_table	= sysent,
55c500808kib	.sv_errsize	= 0,
56c500808kib	.sv_errtbl	= NULL,
57c500808kib	.sv_transtrap	= NULL,
58c500808kib	.sv_fixup	= __elfN(freebsd_fixup),
59c500808kib	.sv_sendsig	= sendsig,
60c500808kib	.sv_sigcode	= sigcode,
61c500808kib	.sv_szsigcode	= &szsigcode,
62c500808kib	.sv_name	= "FreeBSD ELF64",
63c500808kib	.sv_coredump	= __elfN(coredump),
64c500808kib	.sv_imgact_try	= NULL,
65c500808kib	.sv_minsigstksz	= MINSIGSTKSZ,
66c500808kib	.sv_minuser	= VM_MIN_ADDRESS,
67c500808kib	.sv_maxuser	= VM_MAXUSER_ADDRESS,
68c500808kib	.sv_usrstack	= USRSTACK,
69c500808kib	.sv_psstrings	= PS_STRINGS,
70c500808kib	.sv_stackprot	= VM_PROT_ALL,
713f50cb7jhb	.sv_copyout_auxargs = __elfN(freebsd_copyout_auxargs),
72c500808kib	.sv_copyout_strings	= exec_copyout_strings,
73c500808kib	.sv_setregs	= exec_setregs,
74c500808kib	.sv_fixlimit	= NULL,
758fad228kib	.sv_maxssiz	= NULL,
7608849e5kib	.sv_flags	= SV_ABI_FREEBSD | SV_ASLR | SV_LP64 | SV_SHP |
7708849e5kib			    SV_TIMEKEEP,
784208ccbkib	.sv_set_syscall_retval = cpu_set_syscall_retval,
794208ccbkib	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
804208ccbkib	.sv_syscallnames = syscallnames,
8106eb8dekib	.sv_shared_page_base = SHAREDPAGE,
8206eb8dekib	.sv_shared_page_len = PAGE_SIZE,
8369b8756dchagin	.sv_schedtail	= NULL,
84ca0fda4dchagin	.sv_thread_detach = NULL,
85e706df7dchagin	.sv_trap	= NULL,
863df0838kib	.sv_stackgap	= elf64_stackgap,
87cc7b2e4peter};
8806eb8dekibINIT_SYSENTVEC(elf64_sysvec, &elf64_freebsd_sysvec);
89cc7b2e4peter
9043c5cd5truckmanvoid
9143c5cd5truckmanamd64_lower_shared_page(struct sysentvec *sv)
9243c5cd5truckman{
9343c5cd5truckman	if (hw_lower_amd64_sharedpage != 0) {
9443c5cd5truckman		sv->sv_maxuser -= PAGE_SIZE;
9543c5cd5truckman		sv->sv_shared_page_base -= PAGE_SIZE;
9643c5cd5truckman		sv->sv_usrstack -= PAGE_SIZE;
9743c5cd5truckman		sv->sv_psstrings -= PAGE_SIZE;
9843c5cd5truckman	}
9943c5cd5truckman}
10043c5cd5truckman
10143c5cd5truckman/*
10243c5cd5truckman * Do this fixup before INIT_SYSENTVEC (SI_ORDER_ANY) because the latter
10343c5cd5truckman * uses the value of sv_shared_page_base.
10443c5cd5truckman */
10543c5cd5truckmanSYSINIT(elf64_sysvec_fixup, SI_SUB_EXEC, SI_ORDER_FIRST,
10643c5cd5truckman	(sysinit_cfunc_t) amd64_lower_shared_page,
10743c5cd5truckman	&elf64_freebsd_sysvec);
10843c5cd5truckman
10945949ccpeterstatic Elf64_Brandinfo freebsd_brand_info = {
110c500808kib	.brand		= ELFOSABI_FREEBSD,
111c500808kib	.machine	= EM_X86_64,
112c500808kib	.compat_3_brand	= "FreeBSD",
113c500808kib	.emul_path	= NULL,
114c500808kib	.interp_path	= "/libexec/ld-elf.so.1",
115c500808kib	.sysvec		= &elf64_freebsd_sysvec,
116c500808kib	.interp_newpath	= NULL,
1172408b71dchagin	.brand_note	= &elf64_freebsd_brandnote,
11801bf63cdchagin	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
119c500808kib};
120cc7b2e4peter
1218e183cdbzSYSINIT(elf64, SI_SUB_EXEC, SI_ORDER_FIRST,
12245949ccpeter	(sysinit_cfunc_t) elf64_insert_brand_entry,
123cc7b2e4peter	&freebsd_brand_info);
12437a3ec6peter
125998b790peterstatic Elf64_Brandinfo freebsd_brand_oinfo = {
126c500808kib	.brand		= ELFOSABI_FREEBSD,
127c500808kib	.machine	= EM_X86_64,
128c500808kib	.compat_3_brand	= "FreeBSD",
129c500808kib	.emul_path	= NULL,
130c500808kib	.interp_path	= "/usr/libexec/ld-elf.so.1",
131c500808kib	.sysvec		= &elf64_freebsd_sysvec,
132c500808kib	.interp_newpath	= NULL,
1332408b71dchagin	.brand_note	= &elf64_freebsd_brandnote,
13401bf63cdchagin	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
135c500808kib};
136998b790peter
137998b790peterSYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
138998b790peter	(sysinit_cfunc_t) elf64_insert_brand_entry,
139998b790peter	&freebsd_brand_oinfo);
140998b790peter
141ba7b3afbzstatic Elf64_Brandinfo kfreebsd_brand_info = {
142ba7b3afbz	.brand		= ELFOSABI_FREEBSD,
143ba7b3afbz	.machine	= EM_X86_64,
144ba7b3afbz	.compat_3_brand	= "FreeBSD",
145ba7b3afbz	.emul_path	= NULL,
146ba7b3afbz	.interp_path	= "/lib/ld-kfreebsd-x86-64.so.1",
147ba7b3afbz	.sysvec		= &elf64_freebsd_sysvec,
148ba7b3afbz	.interp_newpath	= NULL,
149ba7b3afbz	.brand_note	= &elf64_kfreebsd_brandnote,
150840afe3bz	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE_MANDATORY
151ba7b3afbz};
152ba7b3afbz
153ba7b3afbzSYSINIT(kelf64, SI_SUB_EXEC, SI_ORDER_ANY,
154ba7b3afbz	(sysinit_cfunc_t) elf64_insert_brand_entry,
155ba7b3afbz	&kfreebsd_brand_info);
156ba7b3afbz
157fbbaea5marcelvoid
1581671ac9jhbelf64_dump_thread(struct thread *td, void *dst, size_t *off)
159fbbaea5marcel{
1601671ac9jhb	void *buf;
1611671ac9jhb	size_t len;
162fbbaea5marcel
1631671ac9jhb	len = 0;
1641671ac9jhb	if (use_xsave) {
1651671ac9jhb		if (dst != NULL) {
1661671ac9jhb			fpugetregs(td);
1671671ac9jhb			len += elf64_populate_note(NT_X86_XSTATE,
1681671ac9jhb			    get_pcb_user_save_td(td), dst,
1691671ac9jhb			    cpu_max_ext_state_size, &buf);
1701671ac9jhb			*(uint64_t *)((char *)buf + X86_XSTATE_XCR0_OFFSET) =
1711671ac9jhb			    xsave_mask;
1721671ac9jhb		} else
1731671ac9jhb			len += elf64_populate_note(NT_X86_XSTATE, NULL, NULL,
1741671ac9jhb			    cpu_max_ext_state_size, NULL);
1751671ac9jhb	}
1761671ac9jhb	*off = len;
1771671ac9jhb}
178fbbaea5marcel
1791d715a5markjbool
1801d715a5markjelf_is_ifunc_reloc(Elf_Size r_info)
1811d715a5markj{
1821d715a5markj
1831d715a5markj	return (ELF_R_TYPE(r_info) == R_X86_64_IRELATIVE);
1841d715a5markj}
1852a74077kib
186603a840peter/* Process one elf relocation with addend. */
187c1e42ccjakestatic int
188ea4215cpeterelf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
1891d715a5markj    int type, elf_lookup_fn lookup)
19037a3ec6peter{
191ea4215cpeter	Elf64_Addr *where, val;
192ea4215cpeter	Elf32_Addr *where32, val32;
19360d13c7archie	Elf_Addr addr;
194464de37peter	Elf_Addr addend;
1950a081d0marcel	Elf_Size rtype, symidx;
196464de37peter	const Elf_Rel *rel;
197464de37peter	const Elf_Rela *rela;
1985187346kib	int error;
19937a3ec6peter
200464de37peter	switch (type) {
201464de37peter	case ELF_RELOC_REL:
2029328688dillon		rel = (const Elf_Rel *)data;
203464de37peter		where = (Elf_Addr *) (relocbase + rel->r_offset);
204464de37peter		rtype = ELF_R_TYPE(rel->r_info);
20556d6250marcel		symidx = ELF_R_SYM(rel->r_info);
206d0605f2peter		/* Addend is 32 bit on 32 bit relocs */
207d0605f2peter		switch (rtype) {
208d0605f2peter		case R_X86_64_PC32:
209d0605f2peter		case R_X86_64_32S:
210f8eae77glebius		case R_X86_64_PLT32:
211d0605f2peter			addend = *(Elf32_Addr *)where;
212d0605f2peter			break;
213d0605f2peter		default:
214d0605f2peter			addend = *where;
215d0605f2peter			break;
216d0605f2peter		}
217464de37peter		break;
218464de37peter	case ELF_RELOC_RELA:
2199328688dillon		rela = (const Elf_Rela *)data;
220464de37peter		where = (Elf_Addr *) (relocbase + rela->r_offset);
221464de37peter		addend = rela->r_addend;
222464de37peter		rtype = ELF_R_TYPE(rela->r_info);
22356d6250marcel		symidx = ELF_R_SYM(rela->r_info);
224464de37peter		break;
225464de37peter	default:
226464de37peter		panic("unknown reloc type %d\n", type);
227464de37peter	}
228464de37peter
229464de37peter	switch (rtype) {
23045949ccpeter		case R_X86_64_NONE:	/* none */
23137a3ec6peter			break;
23237a3ec6peter
23345949ccpeter		case R_X86_64_64:		/* S + A */
2345187346kib			error = lookup(lf, symidx, 1, &addr);
235ea4215cpeter			val = addr + addend;
2365187346kib			if (error != 0)
23737a3ec6peter				return -1;
238ea4215cpeter			if (*where != val)
239ea4215cpeter				*where = val;
24037a3ec6peter			break;
24137a3ec6peter
24245949ccpeter		case R_X86_64_PC32:	/* S + A - P */
243f8eae77glebius		case R_X86_64_PLT32:	/* L + A - P, L is PLT location for
244f8eae77glebius					   the symbol, which we treat as S */
2455187346kib			error = lookup(lf, symidx, 1, &addr);
246ea4215cpeter			where32 = (Elf32_Addr *)where;
247ea4215cpeter			val32 = (Elf32_Addr)(addr + addend - (Elf_Addr)where);
2485187346kib			if (error != 0)
24937a3ec6peter				return -1;
250ea4215cpeter			if (*where32 != val32)
251ea4215cpeter				*where32 = val32;
252ea4215cpeter			break;
253ea4215cpeter
254ea4215cpeter		case R_X86_64_32S:	/* S + A sign extend */
2555187346kib			error = lookup(lf, symidx, 1, &addr);
256ea4215cpeter			val32 = (Elf32_Addr)(addr + addend);
257ea4215cpeter			where32 = (Elf32_Addr *)where;
2585187346kib			if (error != 0)
259ea4215cpeter				return -1;
260ea4215cpeter			if (*where32 != val32)
261ea4215cpeter				*where32 = val32;
26237a3ec6peter			break;
26337a3ec6peter
26445949ccpeter		case R_X86_64_COPY:	/* none */
26537a3ec6peter			/*
26637a3ec6peter			 * There shouldn't be copy relocations in kernel
26737a3ec6peter			 * objects.
26837a3ec6peter			 */
26937a3ec6peter			printf("kldload: unexpected R_COPY relocation\n");
2700456e20kib			return (-1);
27137a3ec6peter
27245949ccpeter		case R_X86_64_GLOB_DAT:	/* S */
273257c4e0peter		case R_X86_64_JMP_SLOT:	/* XXX need addend + offset */
2745187346kib			error = lookup(lf, symidx, 1, &addr);
2755187346kib			if (error != 0)
27637a3ec6peter				return -1;
27737a3ec6peter			if (*where != addr)
27837a3ec6peter				*where = addr;
27937a3ec6peter			break;
28037a3ec6peter
28145949ccpeter		case R_X86_64_RELATIVE:	/* B + A */
2829d221cbmarkj			addr = elf_relocaddr(lf, relocbase + addend);
283ea4215cpeter			val = addr;
284ea4215cpeter			if (*where != val)
285ea4215cpeter				*where = val;
28637a3ec6peter			break;
28737a3ec6peter
2882a74077kib		case R_X86_64_IRELATIVE:
2892a74077kib			addr = relocbase + addend;
2902a74077kib			val = ((Elf64_Addr (*)(void))addr)();
2912a74077kib			if (*where != val)
2922a74077kib				*where = val;
2932a74077kib			break;
2942a74077kib
29537a3ec6peter		default:
296f6ba718peter			printf("kldload: unexpected relocation type %ld\n",
297464de37peter			       rtype);
2980456e20kib			return (-1);
29937a3ec6peter	}
3000456e20kib	return (0);
30137a3ec6peter}
302d35d608marcel
303d35d608marcelint
304ea4215cpeterelf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
305ea4215cpeter    elf_lookup_fn lookup)
306c1e42ccjake{
307c1e42ccjake
3081d715a5markj	return (elf_reloc_internal(lf, relocbase, data, type, lookup));
309c1e42ccjake}
310c1e42ccjake
311c1e42ccjakeint
312ea4215cpeterelf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
313ea4215cpeter    int type, elf_lookup_fn lookup)
314c1e42ccjake{
315c1e42ccjake
3161d715a5markj	return (elf_reloc_internal(lf, relocbase, data, type, lookup));
317c1e42ccjake}
318c1e42ccjake
319c1e42ccjakeint
320d35d608marcelelf_cpu_load_file(linker_file_t lf __unused)
321d35d608marcel{
322d35d608marcel
323d35d608marcel	return (0);
324d35d608marcel}
325d35d608marcel
326d35d608marcelint
327d35d608marcelelf_cpu_unload_file(linker_file_t lf __unused)
328d35d608marcel{
329d35d608marcel
330d35d608marcel	return (0);
331d35d608marcel}
332cde01c9bdragon
333cde01c9bdragonint
334cde01c9bdragonelf_cpu_parse_dynamic(linker_file_t lf __unused, Elf_Dyn *dynamic __unused)
335cde01c9bdragon{
336cde01c9bdragon
337cde01c9bdragon	return (0);
338cde01c9bdragon}
339