2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 1996-1998 John D. Polstra.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
34 #define __ELF_WORD_SIZE 32
37 #include <sys/imgact.h>
38 #include <sys/malloc.h>
40 #include <sys/namei.h>
41 #include <sys/fcntl.h>
42 #include <sys/sysent.h>
43 #include <sys/imgact_elf.h>
46 #include <sys/syscall.h>
47 #include <sys/sysctl.h>
48 #include <sys/signalvar.h>
49 #include <sys/vnode.h>
50 #include <sys/linker.h>
53 #include <vm/vm_param.h>
55 #include <machine/altivec.h>
56 #include <machine/cpu.h>
57 #include <machine/fpu.h>
58 #include <machine/elf.h>
59 #include <machine/reg.h>
60 #include <machine/md_var.h>
62 #include <powerpc/powerpc/elf_common.c>
65 #include <compat/freebsd32/freebsd32_proto.h>
66 #include <compat/freebsd32/freebsd32_util.h>
68 extern const char *freebsd32_syscallnames[];
69 static void ppc32_fixlimit(struct rlimit *rl, int which);
71 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW, 0, "32-bit mode");
73 #define PPC32_MAXDSIZ (1024*1024*1024)
74 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
75 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
77 #define PPC32_MAXSSIZ (64*1024*1024)
78 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
79 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
82 static void ppc32_runtime_resolve(void);
85 struct sysentvec elf32_freebsd_sysvec = {
86 .sv_size = SYS_MAXSYSCALL,
88 .sv_table = freebsd32_sysent,
95 .sv_fixup = __elfN(freebsd_fixup),
96 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
97 .sv_sendsig = sendsig,
98 .sv_sigcode = sigcode32,
99 .sv_szsigcode = &szsigcode32,
100 .sv_name = "FreeBSD ELF32",
101 .sv_coredump = __elfN(coredump),
102 .sv_imgact_try = NULL,
103 .sv_minsigstksz = MINSIGSTKSZ,
104 .sv_minuser = VM_MIN_ADDRESS,
105 .sv_stackprot = VM_PROT_ALL,
107 .sv_maxuser = VM_MAXUSER_ADDRESS32,
108 .sv_usrstack = FREEBSD32_USRSTACK,
109 .sv_psstrings = FREEBSD32_PS_STRINGS,
110 .sv_copyout_strings = freebsd32_copyout_strings,
111 .sv_setregs = ppc32_setregs,
112 .sv_syscallnames = freebsd32_syscallnames,
113 .sv_fixlimit = ppc32_fixlimit,
115 .sv_maxuser = VM_MAXUSER_ADDRESS,
116 .sv_usrstack = USRSTACK,
117 .sv_psstrings = PS_STRINGS,
118 .sv_copyout_strings = exec_copyout_strings,
119 .sv_setregs = exec_setregs,
120 .sv_syscallnames = syscallnames,
124 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR,
125 .sv_set_syscall_retval = cpu_set_syscall_retval,
126 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
127 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
128 .sv_shared_page_len = PAGE_SIZE,
129 .sv_schedtail = NULL,
130 .sv_thread_detach = NULL,
132 .sv_hwcap = &cpu_features,
133 .sv_hwcap2 = &cpu_features2,
135 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
137 static Elf32_Brandinfo freebsd_brand_info = {
138 .brand = ELFOSABI_FREEBSD,
140 .compat_3_brand = "FreeBSD",
142 .interp_path = "/libexec/ld-elf.so.1",
143 .sysvec = &elf32_freebsd_sysvec,
145 .interp_newpath = "/libexec/ld-elf32.so.1",
147 .interp_newpath = NULL,
149 .brand_note = &elf32_freebsd_brandnote,
150 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
153 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
154 (sysinit_cfunc_t) elf32_insert_brand_entry,
155 &freebsd_brand_info);
157 static Elf32_Brandinfo freebsd_brand_oinfo = {
158 .brand = ELFOSABI_FREEBSD,
160 .compat_3_brand = "FreeBSD",
162 .interp_path = "/usr/libexec/ld-elf.so.1",
163 .sysvec = &elf32_freebsd_sysvec,
164 .interp_newpath = NULL,
165 .brand_note = &elf32_freebsd_brandnote,
166 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
169 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
170 (sysinit_cfunc_t) elf32_insert_brand_entry,
171 &freebsd_brand_oinfo);
173 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
176 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
187 if (pcb->pcb_flags & PCB_VEC) {
190 len += elf32_populate_note(NT_PPC_VMX,
191 &pcb->pcb_vec, (char *)dst + len,
192 sizeof(pcb->pcb_vec), NULL);
194 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
195 sizeof(pcb->pcb_vec), NULL);
198 if (pcb->pcb_flags & PCB_VSX) {
202 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
203 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
204 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
206 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
207 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
208 vshr[vsr_idx] = *vsr_dw1;
210 len += elf32_populate_note(NT_PPC_VSX,
211 vshr, (char *)dst + len,
214 len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
221 #ifndef __powerpc64__
223 elf_is_ifunc_reloc(Elf_Size r_info __unused)
229 /* Process one elf relocation with addend. */
231 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
232 int type, int local, elf_lookup_fn lookup)
238 Elf_Word rtype, symidx;
239 const Elf_Rela *rela;
244 panic("PPC only supports RELA relocations");
247 rela = (const Elf_Rela *)data;
248 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
249 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
250 addend = rela->r_addend;
251 rtype = ELF_R_TYPE(rela->r_info);
252 symidx = ELF_R_SYM(rela->r_info);
255 panic("elf_reloc: unknown relocation mode %d\n", type);
263 case R_PPC_ADDR32: /* word32 S + A */
264 error = lookup(lf, symidx, 1, &addr);
267 *where = elf_relocaddr(lf, addr + addend);
270 case R_PPC_ADDR16_LO: /* #lo(S) */
271 error = lookup(lf, symidx, 1, &addr);
275 * addend values are sometimes relative to sections
276 * (i.e. .rodata) in rela, where in reality they
277 * are relative to relocbase. Detect this condition.
279 if (addr > relocbase && addr <= (relocbase + addend))
281 addr = elf_relocaddr(lf, addr + addend);
282 *hwhere = addr & 0xffff;
285 case R_PPC_ADDR16_HA: /* #ha(S) */
286 error = lookup(lf, symidx, 1, &addr);
290 * addend values are sometimes relative to sections
291 * (i.e. .rodata) in rela, where in reality they
292 * are relative to relocbase. Detect this condition.
294 if (addr > relocbase && addr <= (relocbase + addend))
296 addr = elf_relocaddr(lf, addr + addend);
297 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
301 case R_PPC_RELATIVE: /* word32 B + A */
302 *where = elf_relocaddr(lf, relocbase + addend);
305 case R_PPC_JMP_SLOT: /* PLT jump slot entry */
307 * We currently only support Secure-PLT jump slots.
308 * Given that we reject BSS-PLT modules during load, we
309 * don't need to check again.
310 * The method we are using here is equivilent to
313 error = lookup(lf, symidx, 1, &addr);
316 *where = elf_relocaddr(lf, addr + addend);
320 printf("kldload: unexpected relocation type %d\n",
328 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
330 Elf_Rela *rela = NULL, *relalim;
335 * Extract the rela/relasz values from the dynamic section
337 for (; dynp->d_tag != DT_NULL; dynp++) {
338 switch (dynp->d_tag) {
340 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
343 relasz = dynp->d_un.d_val;
349 * Relocate these values
351 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
352 for (; rela < relalim; rela++) {
353 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
355 where = (Elf_Addr *)(relocbase + rela->r_offset);
356 *where = (Elf_Addr)(relocbase + rela->r_addend);
361 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
362 elf_lookup_fn lookup)
365 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
369 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
370 int type, elf_lookup_fn lookup)
373 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
377 elf_cpu_load_file(linker_file_t lf)
380 /* Only sync the cache for non-kernel modules */
382 __syncicache(lf->address, lf->size);
387 elf_cpu_unload_file(linker_file_t lf __unused)
394 ppc32_runtime_resolve()
398 * Since we don't support lazy binding, panic immediately if anyone
399 * manages to call the runtime resolver.
401 panic("kldload: Runtime resolver was called unexpectedly!");
405 elf_cpu_parse_dynamic(linker_file_t lf, Elf_Dyn *dynamic)
408 bool has_plt = false;
409 bool secure_plt = false;
412 for (dp = dynamic; dp->d_tag != DT_NULL; dp++) {
416 got = (Elf_Addr *)(lf->address + dp->d_un.d_ptr);
417 /* Install runtime resolver canary. */
418 got[1] = (Elf_Addr)ppc32_runtime_resolve;
419 got[2] = (Elf_Addr)0;
427 if (has_plt && !secure_plt) {
428 printf("kldload: BSS-PLT modules are not supported.\n");
437 ppc32_fixlimit(struct rlimit *rl, int which)
441 if (ppc32_maxdsiz != 0) {
442 if (rl->rlim_cur > ppc32_maxdsiz)
443 rl->rlim_cur = ppc32_maxdsiz;
444 if (rl->rlim_max > ppc32_maxdsiz)
445 rl->rlim_max = ppc32_maxdsiz;
449 if (ppc32_maxssiz != 0) {
450 if (rl->rlim_cur > ppc32_maxssiz)
451 rl->rlim_cur = ppc32_maxssiz;
452 if (rl->rlim_max > ppc32_maxssiz)
453 rl->rlim_max = ppc32_maxssiz;