xref: /freebsd/sys/powerpc/powerpc/elf32_machdep.c (revision b82b4ae7)
1c3e289e1SNathan Whitehorn /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
371e3c308SPedro F. Giffuni  *
4c3e289e1SNathan Whitehorn  * Copyright 1996-1998 John D. Polstra.
5c3e289e1SNathan Whitehorn  * All rights reserved.
6c3e289e1SNathan Whitehorn  *
7c3e289e1SNathan Whitehorn  * Redistribution and use in source and binary forms, with or without
8c3e289e1SNathan Whitehorn  * modification, are permitted provided that the following conditions
9c3e289e1SNathan Whitehorn  * are met:
10c3e289e1SNathan Whitehorn  * 1. Redistributions of source code must retain the above copyright
11c3e289e1SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer.
12c3e289e1SNathan Whitehorn  * 2. Redistributions in binary form must reproduce the above copyright
13c3e289e1SNathan Whitehorn  *    notice, this list of conditions and the following disclaimer in the
14c3e289e1SNathan Whitehorn  *    documentation and/or other materials provided with the distribution.
15c3e289e1SNathan Whitehorn  *
16c3e289e1SNathan Whitehorn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17c3e289e1SNathan Whitehorn  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18c3e289e1SNathan Whitehorn  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19c3e289e1SNathan Whitehorn  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20c3e289e1SNathan Whitehorn  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21c3e289e1SNathan Whitehorn  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22c3e289e1SNathan Whitehorn  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23c3e289e1SNathan Whitehorn  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24c3e289e1SNathan Whitehorn  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25c3e289e1SNathan Whitehorn  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26c3e289e1SNathan Whitehorn  */
27c3e289e1SNathan Whitehorn 
28c3e289e1SNathan Whitehorn #include <sys/param.h>
29c3e289e1SNathan Whitehorn #include <sys/kernel.h>
30c3e289e1SNathan Whitehorn #include <sys/systm.h>
31c3e289e1SNathan Whitehorn 
32c3e289e1SNathan Whitehorn #define __ELF_WORD_SIZE 32
33c3e289e1SNathan Whitehorn 
34c3e289e1SNathan Whitehorn #include <sys/exec.h>
35c3e289e1SNathan Whitehorn #include <sys/imgact.h>
36c3e289e1SNathan Whitehorn #include <sys/malloc.h>
37c3e289e1SNathan Whitehorn #include <sys/proc.h>
38c3e289e1SNathan Whitehorn #include <sys/namei.h>
39c3e289e1SNathan Whitehorn #include <sys/fcntl.h>
40c3e289e1SNathan Whitehorn #include <sys/sysent.h>
41c3e289e1SNathan Whitehorn #include <sys/imgact_elf.h>
42741dfd86SJustin Hibbits #include <sys/jail.h>
43b7924341SAndrew Turner #include <sys/reg.h>
44741dfd86SJustin Hibbits #include <sys/smp.h>
45c3e289e1SNathan Whitehorn #include <sys/syscall.h>
467e792cb8SAndreas Tobler #include <sys/sysctl.h>
47c3e289e1SNathan Whitehorn #include <sys/signalvar.h>
48c3e289e1SNathan Whitehorn #include <sys/vnode.h>
49c3e289e1SNathan Whitehorn #include <sys/linker.h>
50c3e289e1SNathan Whitehorn 
51c3e289e1SNathan Whitehorn #include <vm/vm.h>
52c3e289e1SNathan Whitehorn #include <vm/vm_param.h>
53361971fbSKornel Dulęba #include <vm/pmap.h>
54361971fbSKornel Dulęba #include <vm/vm_map.h>
55c3e289e1SNathan Whitehorn 
567bde2664SJustin Hibbits #include <machine/altivec.h>
57c3e289e1SNathan Whitehorn #include <machine/cpu.h>
585167f178SJustin Hibbits #include <machine/fpu.h>
59c3e289e1SNathan Whitehorn #include <machine/elf.h>
60c3e289e1SNathan Whitehorn #include <machine/md_var.h>
61c3e289e1SNathan Whitehorn 
62741dfd86SJustin Hibbits #include <powerpc/powerpc/elf_common.c>
63741dfd86SJustin Hibbits 
64c3e289e1SNathan Whitehorn #ifdef __powerpc64__
65c3e289e1SNathan Whitehorn #include <compat/freebsd32/freebsd32_proto.h>
66c3e289e1SNathan Whitehorn #include <compat/freebsd32/freebsd32_util.h>
67c3e289e1SNathan Whitehorn 
68c3e289e1SNathan Whitehorn extern const char *freebsd32_syscallnames[];
697e792cb8SAndreas Tobler static void ppc32_fixlimit(struct rlimit *rl, int which);
707e792cb8SAndreas Tobler 
717029da5cSPawel Biernacki static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
727029da5cSPawel Biernacki     "32-bit mode");
737e792cb8SAndreas Tobler 
747e792cb8SAndreas Tobler #define PPC32_MAXDSIZ (1024*1024*1024)
757e792cb8SAndreas Tobler static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
767e792cb8SAndreas Tobler SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
777e792cb8SAndreas Tobler              0, "");
787e792cb8SAndreas Tobler #define PPC32_MAXSSIZ (64*1024*1024)
797e792cb8SAndreas Tobler u_long ppc32_maxssiz = PPC32_MAXSSIZ;
807e792cb8SAndreas Tobler SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
817e792cb8SAndreas Tobler              0, "");
82e211e5feSJustin Hibbits #else
83e211e5feSJustin Hibbits static void ppc32_runtime_resolve(void);
84c3e289e1SNathan Whitehorn #endif
85c3e289e1SNathan Whitehorn 
86c3e289e1SNathan Whitehorn struct sysentvec elf32_freebsd_sysvec = {
87c3e289e1SNathan Whitehorn 	.sv_size	= SYS_MAXSYSCALL,
88c3e289e1SNathan Whitehorn #ifdef __powerpc64__
89c3e289e1SNathan Whitehorn 	.sv_table	= freebsd32_sysent,
90c3e289e1SNathan Whitehorn #else
91c3e289e1SNathan Whitehorn 	.sv_table	= sysent,
92c3e289e1SNathan Whitehorn #endif
93c3e289e1SNathan Whitehorn 	.sv_fixup	= __elfN(freebsd_fixup),
94741dfd86SJustin Hibbits 	.sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
95c3e289e1SNathan Whitehorn 	.sv_sendsig	= sendsig,
96c3e289e1SNathan Whitehorn 	.sv_sigcode	= sigcode32,
97c3e289e1SNathan Whitehorn 	.sv_szsigcode	= &szsigcode32,
98c3e289e1SNathan Whitehorn 	.sv_name	= "FreeBSD ELF32",
99c3e289e1SNathan Whitehorn 	.sv_coredump	= __elfN(coredump),
100435754a5SEdward Tomasz Napierala 	.sv_elf_core_osabi = ELFOSABI_FREEBSD,
101435754a5SEdward Tomasz Napierala 	.sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
102435754a5SEdward Tomasz Napierala 	.sv_elf_core_prepare_notes = __elfN(prepare_notes),
103c3e289e1SNathan Whitehorn 	.sv_minsigstksz	= MINSIGSTKSZ,
104c3e289e1SNathan Whitehorn 	.sv_minuser	= VM_MIN_ADDRESS,
105c3e289e1SNathan Whitehorn 	.sv_stackprot	= VM_PROT_ALL,
106c3e289e1SNathan Whitehorn #ifdef __powerpc64__
107b51bfc30SJustin Hibbits 	.sv_maxuser	= VM_MAXUSER_ADDRESS32,
108c3e289e1SNathan Whitehorn 	.sv_usrstack	= FREEBSD32_USRSTACK,
109c3e289e1SNathan Whitehorn 	.sv_psstrings	= FREEBSD32_PS_STRINGS,
1103fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct freebsd32_ps_strings),
111c3e289e1SNathan Whitehorn 	.sv_copyout_strings = freebsd32_copyout_strings,
112c3e289e1SNathan Whitehorn 	.sv_setregs	= ppc32_setregs,
113c3e289e1SNathan Whitehorn 	.sv_syscallnames = freebsd32_syscallnames,
1147e792cb8SAndreas Tobler 	.sv_fixlimit	= ppc32_fixlimit,
115c3e289e1SNathan Whitehorn #else
116c3e289e1SNathan Whitehorn 	.sv_maxuser	= VM_MAXUSER_ADDRESS,
117c3e289e1SNathan Whitehorn 	.sv_usrstack	= USRSTACK,
118c3e289e1SNathan Whitehorn 	.sv_psstrings	= PS_STRINGS,
1193fc21fddSMark Johnston 	.sv_psstringssz	= sizeof(struct ps_strings),
120c3e289e1SNathan Whitehorn 	.sv_copyout_strings = exec_copyout_strings,
121c3e289e1SNathan Whitehorn 	.sv_setregs	= exec_setregs,
122c3e289e1SNathan Whitehorn 	.sv_syscallnames = syscallnames,
123c3e289e1SNathan Whitehorn 	.sv_fixlimit	= NULL,
1247e792cb8SAndreas Tobler #endif
125c3e289e1SNathan Whitehorn 	.sv_maxssiz	= NULL,
126328b5f25SBrandon Bergren 	.sv_flags	= SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR |
127b82b4ae7SKonstantin Belousov 			    SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS,
128c3e289e1SNathan Whitehorn 	.sv_set_syscall_retval = cpu_set_syscall_retval,
129c3e289e1SNathan Whitehorn 	.sv_fetch_syscall_args = cpu_fetch_syscall_args,
130640c77e1SKonstantin Belousov 	.sv_shared_page_base = FREEBSD32_SHAREDPAGE,
131640c77e1SKonstantin Belousov 	.sv_shared_page_len = PAGE_SIZE,
132e5d81ef1SDmitry Chagin 	.sv_schedtail	= NULL,
13391d1786fSDmitry Chagin 	.sv_thread_detach = NULL,
134038c7205SDmitry Chagin 	.sv_trap	= NULL,
135611c02b1SJustin Hibbits 	.sv_hwcap	= &cpu_features,
136611c02b1SJustin Hibbits 	.sv_hwcap2	= &cpu_features2,
13728a66fc3SKonstantin Belousov 	.sv_onexec_old	= exec_onexec_old,
13828a66fc3SKonstantin Belousov 	.sv_onexit	= exit_onexit,
139548a2ec4SAndrew Turner 	.sv_regset_begin = SET_BEGIN(__elfN(regset)),
140548a2ec4SAndrew Turner 	.sv_regset_end  = SET_LIMIT(__elfN(regset)),
141c3e289e1SNathan Whitehorn };
142640c77e1SKonstantin Belousov INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
143c3e289e1SNathan Whitehorn 
144c3e289e1SNathan Whitehorn static Elf32_Brandinfo freebsd_brand_info = {
145c3e289e1SNathan Whitehorn 	.brand		= ELFOSABI_FREEBSD,
146c3e289e1SNathan Whitehorn 	.machine	= EM_PPC,
147c3e289e1SNathan Whitehorn 	.compat_3_brand	= "FreeBSD",
148c3e289e1SNathan Whitehorn 	.interp_path	= "/libexec/ld-elf.so.1",
149c3e289e1SNathan Whitehorn 	.sysvec		= &elf32_freebsd_sysvec,
150c3e289e1SNathan Whitehorn #ifdef __powerpc64__
151c3e289e1SNathan Whitehorn 	.interp_newpath	= "/libexec/ld-elf32.so.1",
152c3e289e1SNathan Whitehorn #else
153c3e289e1SNathan Whitehorn 	.interp_newpath	= NULL,
154c3e289e1SNathan Whitehorn #endif
155c3e289e1SNathan Whitehorn 	.brand_note	= &elf32_freebsd_brandnote,
156c3e289e1SNathan Whitehorn 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
157c3e289e1SNathan Whitehorn };
158c3e289e1SNathan Whitehorn 
159c3e289e1SNathan Whitehorn SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
160c3e289e1SNathan Whitehorn     (sysinit_cfunc_t) elf32_insert_brand_entry,
161c3e289e1SNathan Whitehorn     &freebsd_brand_info);
162c3e289e1SNathan Whitehorn 
163c3e289e1SNathan Whitehorn static Elf32_Brandinfo freebsd_brand_oinfo = {
164c3e289e1SNathan Whitehorn 	.brand		= ELFOSABI_FREEBSD,
165c3e289e1SNathan Whitehorn 	.machine	= EM_PPC,
166c3e289e1SNathan Whitehorn 	.compat_3_brand	= "FreeBSD",
167c3e289e1SNathan Whitehorn 	.interp_path	= "/usr/libexec/ld-elf.so.1",
168c3e289e1SNathan Whitehorn 	.sysvec		= &elf32_freebsd_sysvec,
169c3e289e1SNathan Whitehorn 	.interp_newpath	= NULL,
170c3e289e1SNathan Whitehorn 	.brand_note	= &elf32_freebsd_brandnote,
171c3e289e1SNathan Whitehorn 	.flags		= BI_CAN_EXEC_DYN | BI_BRAND_NOTE
172c3e289e1SNathan Whitehorn };
173c3e289e1SNathan Whitehorn 
174c3e289e1SNathan Whitehorn SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
175c3e289e1SNathan Whitehorn 	(sysinit_cfunc_t) elf32_insert_brand_entry,
176c3e289e1SNathan Whitehorn 	&freebsd_brand_oinfo);
177c3e289e1SNathan Whitehorn 
1785c845fdeSNathan Whitehorn void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
1795c845fdeSNathan Whitehorn 
180c3e289e1SNathan Whitehorn void
elf32_dump_thread(struct thread * td,void * dst,size_t * off)1817bde2664SJustin Hibbits elf32_dump_thread(struct thread *td, void *dst, size_t *off)
182c3e289e1SNathan Whitehorn {
1837bde2664SJustin Hibbits 	size_t len;
1847bde2664SJustin Hibbits 	struct pcb *pcb;
1855167f178SJustin Hibbits 	uint64_t vshr[32];
1865167f178SJustin Hibbits 	uint64_t *vsr_dw1;
1875167f178SJustin Hibbits 	int vsr_idx;
1887bde2664SJustin Hibbits 
1897bde2664SJustin Hibbits 	len = 0;
1907bde2664SJustin Hibbits 	pcb = td->td_pcb;
1915167f178SJustin Hibbits 
1927bde2664SJustin Hibbits 	if (pcb->pcb_flags & PCB_VEC) {
1937bde2664SJustin Hibbits 		save_vec_nodrop(td);
1947bde2664SJustin Hibbits 		if (dst != NULL) {
1957bde2664SJustin Hibbits 			len += elf32_populate_note(NT_PPC_VMX,
1965167f178SJustin Hibbits 			    &pcb->pcb_vec, (char *)dst + len,
1977bde2664SJustin Hibbits 			    sizeof(pcb->pcb_vec), NULL);
1987bde2664SJustin Hibbits 		} else
1997bde2664SJustin Hibbits 			len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
2007bde2664SJustin Hibbits 			    sizeof(pcb->pcb_vec), NULL);
2017bde2664SJustin Hibbits 	}
2025167f178SJustin Hibbits 
2035167f178SJustin Hibbits 	if (pcb->pcb_flags & PCB_VSX) {
2045167f178SJustin Hibbits 		save_fpu_nodrop(td);
2055167f178SJustin Hibbits 		if (dst != NULL) {
2065167f178SJustin Hibbits 			/*
2075167f178SJustin Hibbits 			 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
2085167f178SJustin Hibbits 			 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
2095167f178SJustin Hibbits 			 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
2105167f178SJustin Hibbits 			 */
2115167f178SJustin Hibbits 			for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
2125167f178SJustin Hibbits 				vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
2135167f178SJustin Hibbits 				vshr[vsr_idx] = *vsr_dw1;
2145167f178SJustin Hibbits 			}
2155167f178SJustin Hibbits 			len += elf32_populate_note(NT_PPC_VSX,
2165167f178SJustin Hibbits 			    vshr, (char *)dst + len,
2175167f178SJustin Hibbits 			    sizeof(vshr), NULL);
2185167f178SJustin Hibbits 		} else
2195167f178SJustin Hibbits 			len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
2205167f178SJustin Hibbits 			    sizeof(vshr), NULL);
2215167f178SJustin Hibbits 	}
2225167f178SJustin Hibbits 
2237bde2664SJustin Hibbits 	*off = len;
224c3e289e1SNathan Whitehorn }
225c3e289e1SNathan Whitehorn 
226c3e289e1SNathan Whitehorn #ifndef __powerpc64__
22736716fe2SMark Johnston bool
elf_is_ifunc_reloc(Elf_Size r_info)2289411e24dSBrandon Bergren elf_is_ifunc_reloc(Elf_Size r_info)
22936716fe2SMark Johnston {
23036716fe2SMark Johnston 
2319411e24dSBrandon Bergren 	return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
23236716fe2SMark Johnston }
23336716fe2SMark Johnston 
234c3e289e1SNathan Whitehorn /* Process one elf relocation with addend. */
235c3e289e1SNathan Whitehorn static int
elf_reloc_internal(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,int local,elf_lookup_fn lookup)236c3e289e1SNathan Whitehorn elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
237c3e289e1SNathan Whitehorn     int type, int local, elf_lookup_fn lookup)
238c3e289e1SNathan Whitehorn {
239c3e289e1SNathan Whitehorn 	Elf_Addr *where;
240c3e289e1SNathan Whitehorn 	Elf_Half *hwhere;
241c3e289e1SNathan Whitehorn 	Elf_Addr addr;
2429411e24dSBrandon Bergren 	Elf_Addr addend, val;
243c3e289e1SNathan Whitehorn 	Elf_Word rtype, symidx;
244c3e289e1SNathan Whitehorn 	const Elf_Rela *rela;
245cff8c6f2SKonstantin Belousov 	int error;
246c3e289e1SNathan Whitehorn 
247c3e289e1SNathan Whitehorn 	switch (type) {
248c3e289e1SNathan Whitehorn 	case ELF_RELOC_REL:
249c3e289e1SNathan Whitehorn 		panic("PPC only supports RELA relocations");
250c3e289e1SNathan Whitehorn 		break;
251c3e289e1SNathan Whitehorn 	case ELF_RELOC_RELA:
252c3e289e1SNathan Whitehorn 		rela = (const Elf_Rela *)data;
253c3e289e1SNathan Whitehorn 		where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
254c3e289e1SNathan Whitehorn 		hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
255c3e289e1SNathan Whitehorn 		addend = rela->r_addend;
256c3e289e1SNathan Whitehorn 		rtype = ELF_R_TYPE(rela->r_info);
257c3e289e1SNathan Whitehorn 		symidx = ELF_R_SYM(rela->r_info);
258c3e289e1SNathan Whitehorn 		break;
259c3e289e1SNathan Whitehorn 	default:
260c3e289e1SNathan Whitehorn 		panic("elf_reloc: unknown relocation mode %d\n", type);
261c3e289e1SNathan Whitehorn 	}
262c3e289e1SNathan Whitehorn 
263c3e289e1SNathan Whitehorn 	switch (rtype) {
264c3e289e1SNathan Whitehorn 	case R_PPC_NONE:
265c3e289e1SNathan Whitehorn 		break;
266c3e289e1SNathan Whitehorn 
267c3e289e1SNathan Whitehorn 	case R_PPC_ADDR32: /* word32 S + A */
268cff8c6f2SKonstantin Belousov 		error = lookup(lf, symidx, 1, &addr);
269cff8c6f2SKonstantin Belousov 		if (error != 0)
2708481aab1SMitchell Horne 			return (-1);
2718b4f33c5SMarcel Moolenaar 		*where = elf_relocaddr(lf, addr + addend);
272c3e289e1SNathan Whitehorn 			break;
273c3e289e1SNathan Whitehorn 
274c3e289e1SNathan Whitehorn 	case R_PPC_ADDR16_LO: /* #lo(S) */
275cff8c6f2SKonstantin Belousov 		error = lookup(lf, symidx, 1, &addr);
276cff8c6f2SKonstantin Belousov 		if (error != 0)
2778481aab1SMitchell Horne 			return (-1);
278c3e289e1SNathan Whitehorn 		/*
279c3e289e1SNathan Whitehorn 		 * addend values are sometimes relative to sections
280c3e289e1SNathan Whitehorn 		 * (i.e. .rodata) in rela, where in reality they
281c3e289e1SNathan Whitehorn 		 * are relative to relocbase. Detect this condition.
282c3e289e1SNathan Whitehorn 		 */
283c3e289e1SNathan Whitehorn 		if (addr > relocbase && addr <= (relocbase + addend))
2848b4f33c5SMarcel Moolenaar 			addr = relocbase;
2858b4f33c5SMarcel Moolenaar 		addr = elf_relocaddr(lf, addr + addend);
286c3e289e1SNathan Whitehorn 		*hwhere = addr & 0xffff;
287c3e289e1SNathan Whitehorn 		break;
288c3e289e1SNathan Whitehorn 
289c3e289e1SNathan Whitehorn 	case R_PPC_ADDR16_HA: /* #ha(S) */
290cff8c6f2SKonstantin Belousov 		error = lookup(lf, symidx, 1, &addr);
291cff8c6f2SKonstantin Belousov 		if (error != 0)
2928481aab1SMitchell Horne 			return (-1);
293c3e289e1SNathan Whitehorn 		/*
294c3e289e1SNathan Whitehorn 		 * addend values are sometimes relative to sections
295c3e289e1SNathan Whitehorn 		 * (i.e. .rodata) in rela, where in reality they
296c3e289e1SNathan Whitehorn 		 * are relative to relocbase. Detect this condition.
297c3e289e1SNathan Whitehorn 		 */
298c3e289e1SNathan Whitehorn 		if (addr > relocbase && addr <= (relocbase + addend))
2998b4f33c5SMarcel Moolenaar 			addr = relocbase;
3008b4f33c5SMarcel Moolenaar 		addr = elf_relocaddr(lf, addr + addend);
301c3e289e1SNathan Whitehorn 		*hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
302c3e289e1SNathan Whitehorn 		    & 0xffff;
303c3e289e1SNathan Whitehorn 		break;
304c3e289e1SNathan Whitehorn 
305c3e289e1SNathan Whitehorn 	case R_PPC_RELATIVE: /* word32 B + A */
306c3e289e1SNathan Whitehorn 		*where = elf_relocaddr(lf, relocbase + addend);
307c3e289e1SNathan Whitehorn 		break;
308c3e289e1SNathan Whitehorn 
3097821a820SBrandon Bergren 	case R_PPC_JMP_SLOT: /* PLT jump slot entry */
3107821a820SBrandon Bergren 		/*
3117821a820SBrandon Bergren 		 * We currently only support Secure-PLT jump slots.
3127821a820SBrandon Bergren 		 * Given that we reject BSS-PLT modules during load, we
3137821a820SBrandon Bergren 		 * don't need to check again.
3147821a820SBrandon Bergren 		 * The method we are using here is equivilent to
3157821a820SBrandon Bergren 		 * LD_BIND_NOW.
3167821a820SBrandon Bergren 		 */
3177821a820SBrandon Bergren 		error = lookup(lf, symidx, 1, &addr);
3187821a820SBrandon Bergren 		if (error != 0)
3198481aab1SMitchell Horne 			return (-1);
3207821a820SBrandon Bergren 		*where = elf_relocaddr(lf, addr + addend);
3217821a820SBrandon Bergren 		break;
3227821a820SBrandon Bergren 
3239411e24dSBrandon Bergren 	case R_PPC_IRELATIVE:
3249411e24dSBrandon Bergren 		addr = relocbase + addend;
3259411e24dSBrandon Bergren 		val = ((Elf32_Addr (*)(void))addr)();
3269411e24dSBrandon Bergren 		if (*where != val)
3279411e24dSBrandon Bergren 			*where = val;
3289411e24dSBrandon Bergren 		break;
3299411e24dSBrandon Bergren 
330c3e289e1SNathan Whitehorn 	default:
3318481aab1SMitchell Horne 		printf("kldload: unexpected relocation type %d, "
3328481aab1SMitchell Horne 		    "symbol index %d\n", (int)rtype, symidx);
3338481aab1SMitchell Horne 		return (-1);
334c3e289e1SNathan Whitehorn 	}
335c3e289e1SNathan Whitehorn 	return (0);
336c3e289e1SNathan Whitehorn }
337c3e289e1SNathan Whitehorn 
3385c845fdeSNathan Whitehorn void
elf_reloc_self(Elf_Dyn * dynp,Elf_Addr relocbase)3395c845fdeSNathan Whitehorn elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
3405c845fdeSNathan Whitehorn {
34126f0e234SPedro F. Giffuni 	Elf_Rela *rela = NULL, *relalim;
3425c845fdeSNathan Whitehorn 	Elf_Addr relasz = 0;
3435c845fdeSNathan Whitehorn 	Elf_Addr *where;
3445c845fdeSNathan Whitehorn 
3455c845fdeSNathan Whitehorn 	/*
3465c845fdeSNathan Whitehorn 	 * Extract the rela/relasz values from the dynamic section
3475c845fdeSNathan Whitehorn 	 */
3485c845fdeSNathan Whitehorn 	for (; dynp->d_tag != DT_NULL; dynp++) {
3495c845fdeSNathan Whitehorn 		switch (dynp->d_tag) {
3505c845fdeSNathan Whitehorn 		case DT_RELA:
3515c845fdeSNathan Whitehorn 			rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
3525c845fdeSNathan Whitehorn 			break;
3535c845fdeSNathan Whitehorn 		case DT_RELASZ:
3545c845fdeSNathan Whitehorn 			relasz = dynp->d_un.d_val;
3555c845fdeSNathan Whitehorn 			break;
3565c845fdeSNathan Whitehorn 		}
3575c845fdeSNathan Whitehorn 	}
3585c845fdeSNathan Whitehorn 
3595c845fdeSNathan Whitehorn 	/*
3605c845fdeSNathan Whitehorn 	 * Relocate these values
3615c845fdeSNathan Whitehorn 	 */
3625c845fdeSNathan Whitehorn 	relalim = (Elf_Rela *)((caddr_t)rela + relasz);
3635c845fdeSNathan Whitehorn 	for (; rela < relalim; rela++) {
3645c845fdeSNathan Whitehorn 		if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
3655c845fdeSNathan Whitehorn 			continue;
3665c845fdeSNathan Whitehorn 		where = (Elf_Addr *)(relocbase + rela->r_offset);
3675c845fdeSNathan Whitehorn 		*where = (Elf_Addr)(relocbase + rela->r_addend);
3685c845fdeSNathan Whitehorn 	}
3695c845fdeSNathan Whitehorn }
3705c845fdeSNathan Whitehorn 
371c3e289e1SNathan Whitehorn int
elf_reloc(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)372c3e289e1SNathan Whitehorn elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
373c3e289e1SNathan Whitehorn     elf_lookup_fn lookup)
374c3e289e1SNathan Whitehorn {
375c3e289e1SNathan Whitehorn 
376c3e289e1SNathan Whitehorn 	return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
377c3e289e1SNathan Whitehorn }
378c3e289e1SNathan Whitehorn 
379c3e289e1SNathan Whitehorn int
elf_reloc_local(linker_file_t lf,Elf_Addr relocbase,const void * data,int type,elf_lookup_fn lookup)380c3e289e1SNathan Whitehorn elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
381c3e289e1SNathan Whitehorn     int type, elf_lookup_fn lookup)
382c3e289e1SNathan Whitehorn {
383c3e289e1SNathan Whitehorn 
384c3e289e1SNathan Whitehorn 	return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
385c3e289e1SNathan Whitehorn }
386c3e289e1SNathan Whitehorn 
387c3e289e1SNathan Whitehorn int
elf_cpu_load_file(linker_file_t lf)388c3e289e1SNathan Whitehorn elf_cpu_load_file(linker_file_t lf)
389c3e289e1SNathan Whitehorn {
3907821a820SBrandon Bergren 
391c3e289e1SNathan Whitehorn 	/* Only sync the cache for non-kernel modules */
392c3e289e1SNathan Whitehorn 	if (lf->id != 1)
393c3e289e1SNathan Whitehorn 		__syncicache(lf->address, lf->size);
394c3e289e1SNathan Whitehorn 	return (0);
395c3e289e1SNathan Whitehorn }
396c3e289e1SNathan Whitehorn 
397c3e289e1SNathan Whitehorn int
elf_cpu_unload_file(linker_file_t lf __unused)398c3e289e1SNathan Whitehorn elf_cpu_unload_file(linker_file_t lf __unused)
399c3e289e1SNathan Whitehorn {
400c3e289e1SNathan Whitehorn 
401c3e289e1SNathan Whitehorn 	return (0);
402c3e289e1SNathan Whitehorn }
4037821a820SBrandon Bergren 
4047821a820SBrandon Bergren static void
ppc32_runtime_resolve(void)4059b02f2daSJohn Baldwin ppc32_runtime_resolve(void)
4067821a820SBrandon Bergren {
4077821a820SBrandon Bergren 
4087821a820SBrandon Bergren 	/*
4097821a820SBrandon Bergren 	 * Since we don't support lazy binding, panic immediately if anyone
4107821a820SBrandon Bergren 	 * manages to call the runtime resolver.
4117821a820SBrandon Bergren 	 */
4127821a820SBrandon Bergren 	panic("kldload: Runtime resolver was called unexpectedly!");
4137821a820SBrandon Bergren }
4147821a820SBrandon Bergren 
4157821a820SBrandon Bergren int
elf_cpu_parse_dynamic(caddr_t loadbase,Elf_Dyn * dynamic)41630693808SBrandon Bergren elf_cpu_parse_dynamic(caddr_t loadbase, Elf_Dyn *dynamic)
4177821a820SBrandon Bergren {
4187821a820SBrandon Bergren 	Elf_Dyn *dp;
4197821a820SBrandon Bergren 	bool has_plt = false;
4207821a820SBrandon Bergren 	bool secure_plt = false;
4217821a820SBrandon Bergren 	Elf_Addr *got;
4227821a820SBrandon Bergren 
4237821a820SBrandon Bergren 	for (dp = dynamic; dp->d_tag != DT_NULL; dp++) {
4247821a820SBrandon Bergren 		switch (dp->d_tag) {
4257821a820SBrandon Bergren 		case DT_PPC_GOT:
4267821a820SBrandon Bergren 			secure_plt = true;
42730693808SBrandon Bergren 			got = (Elf_Addr *)(loadbase + dp->d_un.d_ptr);
4287821a820SBrandon Bergren 			/* Install runtime resolver canary. */
4297821a820SBrandon Bergren 			got[1] = (Elf_Addr)ppc32_runtime_resolve;
4307821a820SBrandon Bergren 			got[2] = (Elf_Addr)0;
4317821a820SBrandon Bergren 			break;
4327821a820SBrandon Bergren 		case DT_PLTGOT:
4337821a820SBrandon Bergren 			has_plt = true;
4347821a820SBrandon Bergren 			break;
4357821a820SBrandon Bergren 		}
4367821a820SBrandon Bergren 	}
4377821a820SBrandon Bergren 
4387821a820SBrandon Bergren 	if (has_plt && !secure_plt) {
4397821a820SBrandon Bergren 		printf("kldload: BSS-PLT modules are not supported.\n");
4407821a820SBrandon Bergren 		return (-1);
4417821a820SBrandon Bergren 	}
4427821a820SBrandon Bergren 	return (0);
4437821a820SBrandon Bergren }
444c3e289e1SNathan Whitehorn #endif
4457e792cb8SAndreas Tobler 
4467e792cb8SAndreas Tobler #ifdef __powerpc64__
4477e792cb8SAndreas Tobler static void
ppc32_fixlimit(struct rlimit * rl,int which)4487e792cb8SAndreas Tobler ppc32_fixlimit(struct rlimit *rl, int which)
4497e792cb8SAndreas Tobler {
4507e792cb8SAndreas Tobler 	switch (which) {
4517e792cb8SAndreas Tobler 	case RLIMIT_DATA:
4527e792cb8SAndreas Tobler 		if (ppc32_maxdsiz != 0) {
4537e792cb8SAndreas Tobler 			if (rl->rlim_cur > ppc32_maxdsiz)
4547e792cb8SAndreas Tobler 				rl->rlim_cur = ppc32_maxdsiz;
4557e792cb8SAndreas Tobler 			if (rl->rlim_max > ppc32_maxdsiz)
4567e792cb8SAndreas Tobler 				rl->rlim_max = ppc32_maxdsiz;
4577e792cb8SAndreas Tobler 		}
4587e792cb8SAndreas Tobler 		break;
4597e792cb8SAndreas Tobler 	case RLIMIT_STACK:
4607e792cb8SAndreas Tobler 		if (ppc32_maxssiz != 0) {
4617e792cb8SAndreas Tobler 			if (rl->rlim_cur > ppc32_maxssiz)
4627e792cb8SAndreas Tobler 				rl->rlim_cur = ppc32_maxssiz;
4637e792cb8SAndreas Tobler 			if (rl->rlim_max > ppc32_maxssiz)
4647e792cb8SAndreas Tobler 				rl->rlim_max = ppc32_maxssiz;
4657e792cb8SAndreas Tobler 		}
4667e792cb8SAndreas Tobler 		break;
4677e792cb8SAndreas Tobler 	}
4687e792cb8SAndreas Tobler }
4697e792cb8SAndreas Tobler #endif
470