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>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/linker.h>
54 #include <vm/vm_param.h>
56 #include <vm/vm_map.h>
58 #include <machine/altivec.h>
59 #include <machine/cpu.h>
60 #include <machine/fpu.h>
61 #include <machine/elf.h>
62 #include <machine/md_var.h>
64 #include <powerpc/powerpc/elf_common.c>
67 #include <compat/freebsd32/freebsd32_proto.h>
68 #include <compat/freebsd32/freebsd32_util.h>
70 extern const char *freebsd32_syscallnames[];
71 static void ppc32_fixlimit(struct rlimit *rl, int which);
73 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
76 #define PPC32_MAXDSIZ (1024*1024*1024)
77 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
78 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
80 #define PPC32_MAXSSIZ (64*1024*1024)
81 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
82 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
85 static void ppc32_runtime_resolve(void);
88 struct sysentvec elf32_freebsd_sysvec = {
89 .sv_size = SYS_MAXSYSCALL,
91 .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_elf_core_osabi = ELFOSABI_FREEBSD,
103 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
104 .sv_elf_core_prepare_notes = __elfN(prepare_notes),
105 .sv_imgact_try = NULL,
106 .sv_minsigstksz = MINSIGSTKSZ,
107 .sv_minuser = VM_MIN_ADDRESS,
108 .sv_stackprot = VM_PROT_ALL,
110 .sv_maxuser = VM_MAXUSER_ADDRESS32,
111 .sv_usrstack = FREEBSD32_USRSTACK,
112 .sv_psstrings = FREEBSD32_PS_STRINGS,
113 .sv_psstringssz = sizeof(struct freebsd32_ps_strings),
114 .sv_copyout_strings = freebsd32_copyout_strings,
115 .sv_setregs = ppc32_setregs,
116 .sv_syscallnames = freebsd32_syscallnames,
117 .sv_fixlimit = ppc32_fixlimit,
119 .sv_maxuser = VM_MAXUSER_ADDRESS,
120 .sv_usrstack = USRSTACK,
121 .sv_psstrings = PS_STRINGS,
122 .sv_psstringssz = sizeof(struct ps_strings),
123 .sv_copyout_strings = exec_copyout_strings,
124 .sv_setregs = exec_setregs,
125 .sv_syscallnames = syscallnames,
129 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR |
130 SV_TIMEKEEP | SV_RNG_SEED_VER,
131 .sv_set_syscall_retval = cpu_set_syscall_retval,
132 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
133 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
134 .sv_shared_page_len = PAGE_SIZE,
135 .sv_schedtail = NULL,
136 .sv_thread_detach = NULL,
138 .sv_hwcap = &cpu_features,
139 .sv_hwcap2 = &cpu_features2,
140 .sv_onexec_old = exec_onexec_old,
141 .sv_onexit = exit_onexit,
142 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
143 .sv_regset_end = SET_LIMIT(__elfN(regset)),
145 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
147 static Elf32_Brandinfo freebsd_brand_info = {
148 .brand = ELFOSABI_FREEBSD,
150 .compat_3_brand = "FreeBSD",
152 .interp_path = "/libexec/ld-elf.so.1",
153 .sysvec = &elf32_freebsd_sysvec,
155 .interp_newpath = "/libexec/ld-elf32.so.1",
157 .interp_newpath = NULL,
159 .brand_note = &elf32_freebsd_brandnote,
160 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
163 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
164 (sysinit_cfunc_t) elf32_insert_brand_entry,
165 &freebsd_brand_info);
167 static Elf32_Brandinfo freebsd_brand_oinfo = {
168 .brand = ELFOSABI_FREEBSD,
170 .compat_3_brand = "FreeBSD",
172 .interp_path = "/usr/libexec/ld-elf.so.1",
173 .sysvec = &elf32_freebsd_sysvec,
174 .interp_newpath = NULL,
175 .brand_note = &elf32_freebsd_brandnote,
176 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
179 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
180 (sysinit_cfunc_t) elf32_insert_brand_entry,
181 &freebsd_brand_oinfo);
183 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
186 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
197 if (pcb->pcb_flags & PCB_VEC) {
200 len += elf32_populate_note(NT_PPC_VMX,
201 &pcb->pcb_vec, (char *)dst + len,
202 sizeof(pcb->pcb_vec), NULL);
204 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
205 sizeof(pcb->pcb_vec), NULL);
208 if (pcb->pcb_flags & PCB_VSX) {
212 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
213 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
214 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
216 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
217 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
218 vshr[vsr_idx] = *vsr_dw1;
220 len += elf32_populate_note(NT_PPC_VSX,
221 vshr, (char *)dst + len,
224 len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
231 #ifndef __powerpc64__
233 elf_is_ifunc_reloc(Elf_Size r_info)
236 return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
239 /* Process one elf relocation with addend. */
241 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
242 int type, int local, elf_lookup_fn lookup)
247 Elf_Addr addend, val;
248 Elf_Word rtype, symidx;
249 const Elf_Rela *rela;
254 panic("PPC only supports RELA relocations");
257 rela = (const Elf_Rela *)data;
258 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
259 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
260 addend = rela->r_addend;
261 rtype = ELF_R_TYPE(rela->r_info);
262 symidx = ELF_R_SYM(rela->r_info);
265 panic("elf_reloc: unknown relocation mode %d\n", type);
272 case R_PPC_ADDR32: /* word32 S + A */
273 error = lookup(lf, symidx, 1, &addr);
276 *where = elf_relocaddr(lf, addr + addend);
279 case R_PPC_ADDR16_LO: /* #lo(S) */
280 error = lookup(lf, symidx, 1, &addr);
284 * addend values are sometimes relative to sections
285 * (i.e. .rodata) in rela, where in reality they
286 * are relative to relocbase. Detect this condition.
288 if (addr > relocbase && addr <= (relocbase + addend))
290 addr = elf_relocaddr(lf, addr + addend);
291 *hwhere = addr & 0xffff;
294 case R_PPC_ADDR16_HA: /* #ha(S) */
295 error = lookup(lf, symidx, 1, &addr);
299 * addend values are sometimes relative to sections
300 * (i.e. .rodata) in rela, where in reality they
301 * are relative to relocbase. Detect this condition.
303 if (addr > relocbase && addr <= (relocbase + addend))
305 addr = elf_relocaddr(lf, addr + addend);
306 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
310 case R_PPC_RELATIVE: /* word32 B + A */
311 *where = elf_relocaddr(lf, relocbase + addend);
314 case R_PPC_JMP_SLOT: /* PLT jump slot entry */
316 * We currently only support Secure-PLT jump slots.
317 * Given that we reject BSS-PLT modules during load, we
318 * don't need to check again.
319 * The method we are using here is equivilent to
322 error = lookup(lf, symidx, 1, &addr);
325 *where = elf_relocaddr(lf, addr + addend);
328 case R_PPC_IRELATIVE:
329 addr = relocbase + addend;
330 val = ((Elf32_Addr (*)(void))addr)();
336 printf("kldload: unexpected relocation type %d, "
337 "symbol index %d\n", (int)rtype, symidx);
344 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
346 Elf_Rela *rela = NULL, *relalim;
351 * Extract the rela/relasz values from the dynamic section
353 for (; dynp->d_tag != DT_NULL; dynp++) {
354 switch (dynp->d_tag) {
356 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
359 relasz = dynp->d_un.d_val;
365 * Relocate these values
367 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
368 for (; rela < relalim; rela++) {
369 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
371 where = (Elf_Addr *)(relocbase + rela->r_offset);
372 *where = (Elf_Addr)(relocbase + rela->r_addend);
377 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
378 elf_lookup_fn lookup)
381 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
385 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
386 int type, elf_lookup_fn lookup)
389 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
393 elf_cpu_load_file(linker_file_t lf)
396 /* Only sync the cache for non-kernel modules */
398 __syncicache(lf->address, lf->size);
403 elf_cpu_unload_file(linker_file_t lf __unused)
410 ppc32_runtime_resolve()
414 * Since we don't support lazy binding, panic immediately if anyone
415 * manages to call the runtime resolver.
417 panic("kldload: Runtime resolver was called unexpectedly!");
421 elf_cpu_parse_dynamic(caddr_t loadbase, Elf_Dyn *dynamic)
424 bool has_plt = false;
425 bool secure_plt = false;
428 for (dp = dynamic; dp->d_tag != DT_NULL; dp++) {
432 got = (Elf_Addr *)(loadbase + dp->d_un.d_ptr);
433 /* Install runtime resolver canary. */
434 got[1] = (Elf_Addr)ppc32_runtime_resolve;
435 got[2] = (Elf_Addr)0;
443 if (has_plt && !secure_plt) {
444 printf("kldload: BSS-PLT modules are not supported.\n");
453 ppc32_fixlimit(struct rlimit *rl, int which)
457 if (ppc32_maxdsiz != 0) {
458 if (rl->rlim_cur > ppc32_maxdsiz)
459 rl->rlim_cur = ppc32_maxdsiz;
460 if (rl->rlim_max > ppc32_maxdsiz)
461 rl->rlim_max = ppc32_maxdsiz;
465 if (ppc32_maxssiz != 0) {
466 if (rl->rlim_cur > ppc32_maxssiz)
467 rl->rlim_cur = ppc32_maxssiz;
468 if (rl->rlim_max > ppc32_maxssiz)
469 rl->rlim_max = ppc32_maxssiz;