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 #include <sys/imgact.h>
35 #include <sys/malloc.h>
37 #include <sys/namei.h>
38 #include <sys/fcntl.h>
39 #include <sys/sysent.h>
40 #include <sys/imgact_elf.h>
43 #include <sys/syscall.h>
44 #include <sys/signalvar.h>
45 #include <sys/vnode.h>
46 #include <sys/linker.h>
49 #include <vm/vm_param.h>
51 #include <machine/altivec.h>
52 #include <machine/cpu.h>
53 #include <machine/fpu.h>
54 #include <machine/elf.h>
55 #include <machine/md_var.h>
57 #include <powerpc/powerpc/elf_common.c>
59 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
62 struct sysentvec elf64_freebsd_sysvec_v1 = {
63 .sv_size = SYS_MAXSYSCALL,
68 .sv_fixup = __elfN(freebsd_fixup),
69 .sv_sendsig = sendsig,
70 .sv_sigcode = sigcode64,
71 .sv_szsigcode = &szsigcode64,
72 .sv_name = "FreeBSD ELF64",
73 .sv_coredump = __elfN(coredump),
74 .sv_imgact_try = NULL,
75 .sv_minsigstksz = MINSIGSTKSZ,
76 .sv_minuser = VM_MIN_ADDRESS,
77 .sv_maxuser = VM_MAXUSER_ADDRESS,
78 .sv_usrstack = USRSTACK,
79 .sv_psstrings = PS_STRINGS,
80 .sv_stackprot = VM_PROT_ALL,
81 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
82 .sv_copyout_strings = exec_copyout_strings,
83 .sv_setregs = exec_setregs_funcdesc,
86 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP | SV_ASLR,
87 .sv_set_syscall_retval = cpu_set_syscall_retval,
88 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
89 .sv_syscallnames = syscallnames,
90 .sv_shared_page_base = SHAREDPAGE,
91 .sv_shared_page_len = PAGE_SIZE,
93 .sv_thread_detach = NULL,
95 .sv_hwcap = &cpu_features,
96 .sv_hwcap2 = &cpu_features2,
98 INIT_SYSENTVEC(elf64_sysvec_v1, &elf64_freebsd_sysvec_v1);
100 struct sysentvec elf64_freebsd_sysvec_v2 = {
101 .sv_size = SYS_MAXSYSCALL,
105 .sv_transtrap = NULL,
106 .sv_fixup = __elfN(freebsd_fixup),
107 .sv_sendsig = sendsig,
108 .sv_sigcode = sigcode64_elfv2,
109 .sv_szsigcode = &szsigcode64_elfv2,
110 .sv_name = "FreeBSD ELF64 V2",
111 .sv_coredump = __elfN(coredump),
112 .sv_imgact_try = NULL,
113 .sv_minsigstksz = MINSIGSTKSZ,
114 .sv_minuser = VM_MIN_ADDRESS,
115 .sv_maxuser = VM_MAXUSER_ADDRESS,
116 .sv_usrstack = USRSTACK,
117 .sv_psstrings = PS_STRINGS,
118 .sv_stackprot = VM_PROT_ALL,
119 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
120 .sv_copyout_strings = exec_copyout_strings,
121 .sv_setregs = exec_setregs,
124 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
125 .sv_set_syscall_retval = cpu_set_syscall_retval,
126 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
127 .sv_syscallnames = syscallnames,
128 .sv_shared_page_base = SHAREDPAGE,
129 .sv_shared_page_len = PAGE_SIZE,
130 .sv_schedtail = NULL,
131 .sv_thread_detach = NULL,
133 .sv_hwcap = &cpu_features,
134 .sv_hwcap2 = &cpu_features2,
136 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2);
138 static boolean_t ppc64_elfv1_header_match(struct image_params *params,
139 int32_t *, uint32_t *);
140 static boolean_t ppc64_elfv2_header_match(struct image_params *params,
141 int32_t *, uint32_t *);
143 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
144 .brand = ELFOSABI_FREEBSD,
146 .compat_3_brand = "FreeBSD",
148 .interp_path = "/libexec/ld-elf.so.1",
149 .sysvec = &elf64_freebsd_sysvec_v1,
150 .interp_newpath = NULL,
151 .brand_note = &elf64_freebsd_brandnote,
152 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
153 .header_supported = &ppc64_elfv1_header_match
156 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
157 (sysinit_cfunc_t) elf64_insert_brand_entry,
158 &freebsd_brand_info_elfv1);
160 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
161 .brand = ELFOSABI_FREEBSD,
163 .compat_3_brand = "FreeBSD",
165 .interp_path = "/libexec/ld-elf.so.1",
166 .sysvec = &elf64_freebsd_sysvec_v2,
167 .interp_newpath = NULL,
168 .brand_note = &elf64_freebsd_brandnote,
169 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
170 .header_supported = &ppc64_elfv2_header_match
173 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
174 (sysinit_cfunc_t) elf64_insert_brand_entry,
175 &freebsd_brand_info_elfv2);
177 static Elf64_Brandinfo freebsd_brand_oinfo = {
178 .brand = ELFOSABI_FREEBSD,
180 .compat_3_brand = "FreeBSD",
182 .interp_path = "/usr/libexec/ld-elf.so.1",
183 .sysvec = &elf64_freebsd_sysvec_v1,
184 .interp_newpath = NULL,
185 .brand_note = &elf64_freebsd_brandnote,
186 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
187 .header_supported = &ppc64_elfv1_header_match
190 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
191 (sysinit_cfunc_t) elf64_insert_brand_entry,
192 &freebsd_brand_oinfo);
194 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
197 ppc64_elfv1_header_match(struct image_params *params, int32_t *osrel __unused,
198 uint32_t *fctl0 __unused)
200 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
201 int abi = (hdr->e_flags & 3);
203 return (abi == 0 || abi == 1);
207 ppc64_elfv2_header_match(struct image_params *params, int32_t *osrel __unused,
208 uint32_t *fctl0 __unused)
210 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
211 int abi = (hdr->e_flags & 3);
217 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
220 struct trapframe *tf;
221 register_t entry_desc[3];
224 exec_setregs(td, imgp, stack);
227 * For 64-bit ELFv1, we need to disentangle the function
232 * 2. Environment pointer (r11)
235 (void)copyin((void *)imgp->entry_addr, entry_desc,
237 tf->srr0 = entry_desc[0] + imgp->reloc_base;
238 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
239 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
243 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
254 if (pcb->pcb_flags & PCB_VEC) {
257 len += elf64_populate_note(NT_PPC_VMX,
258 &pcb->pcb_vec, (char *)dst + len,
259 sizeof(pcb->pcb_vec), NULL);
261 len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
262 sizeof(pcb->pcb_vec), NULL);
265 if (pcb->pcb_flags & PCB_VSX) {
269 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
270 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
271 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
273 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
274 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
275 vshr[vsr_idx] = *vsr_dw1;
277 len += elf64_populate_note(NT_PPC_VSX,
278 vshr, (char *)dst + len,
281 len += elf64_populate_note(NT_PPC_VSX, NULL, NULL,
289 elf_is_ifunc_reloc(Elf_Size r_info)
292 return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
295 /* Process one elf relocation with addend. */
297 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
298 int type, int local, elf_lookup_fn lookup)
302 Elf_Addr addend, val;
303 Elf_Word rtype, symidx;
304 const Elf_Rela *rela;
309 panic("PPC only supports RELA relocations");
312 rela = (const Elf_Rela *)data;
313 where = (Elf_Addr *) (relocbase + rela->r_offset);
314 addend = rela->r_addend;
315 rtype = ELF_R_TYPE(rela->r_info);
316 symidx = ELF_R_SYM(rela->r_info);
319 panic("elf_reloc: unknown relocation mode %d\n", type);
327 case R_PPC64_ADDR64: /* doubleword64 S + A */
328 error = lookup(lf, symidx, 1, &addr);
335 case R_PPC_RELATIVE: /* doubleword64 B + A */
336 *where = elf_relocaddr(lf, relocbase + addend);
339 case R_PPC_JMP_SLOT: /* function descriptor copy */
340 lookup(lf, symidx, 1, &addr);
341 #if !defined(_CALL_ELF) || _CALL_ELF == 1
342 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
346 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
349 case R_PPC_IRELATIVE:
350 addr = relocbase + addend;
351 val = ((Elf64_Addr (*)(void))addr)();
357 printf("kldload: unexpected relocation type %d\n",
365 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
367 Elf_Rela *rela = NULL, *relalim;
372 * Extract the rela/relasz values from the dynamic section
374 for (; dynp->d_tag != DT_NULL; dynp++) {
375 switch (dynp->d_tag) {
377 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
380 relasz = dynp->d_un.d_val;
386 * Relocate these values
388 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
389 for (; rela < relalim; rela++) {
390 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
392 where = (Elf_Addr *)(relocbase + rela->r_offset);
393 *where = (Elf_Addr)(relocbase + rela->r_addend);
398 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
399 elf_lookup_fn lookup)
402 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
406 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
407 int type, elf_lookup_fn lookup)
410 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
414 elf_cpu_load_file(linker_file_t lf)
416 /* Only sync the cache for non-kernel modules */
418 __syncicache(lf->address, lf->size);
423 elf_cpu_unload_file(linker_file_t lf __unused)
430 elf_cpu_parse_dynamic(caddr_t loadbase __unused, Elf_Dyn *dynamic __unused)