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>
41 #include <sys/syscall.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 #include <sys/linker.h>
47 #include <vm/vm_param.h>
49 #include <machine/altivec.h>
50 #include <machine/cpu.h>
51 #include <machine/elf.h>
52 #include <machine/md_var.h>
54 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
57 struct sysentvec elf64_freebsd_sysvec_v1 = {
58 .sv_size = SYS_MAXSYSCALL,
64 .sv_fixup = __elfN(freebsd_fixup),
65 .sv_sendsig = sendsig,
66 .sv_sigcode = sigcode64,
67 .sv_szsigcode = &szsigcode64,
68 .sv_name = "FreeBSD ELF64",
69 .sv_coredump = __elfN(coredump),
70 .sv_imgact_try = NULL,
71 .sv_minsigstksz = MINSIGSTKSZ,
72 .sv_pagesize = PAGE_SIZE,
73 .sv_minuser = VM_MIN_ADDRESS,
74 .sv_maxuser = VM_MAXUSER_ADDRESS,
75 .sv_usrstack = USRSTACK,
76 .sv_psstrings = PS_STRINGS,
77 .sv_stackprot = VM_PROT_ALL,
78 .sv_copyout_strings = exec_copyout_strings,
79 .sv_setregs = exec_setregs_funcdesc,
82 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
83 .sv_set_syscall_retval = cpu_set_syscall_retval,
84 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
85 .sv_syscallnames = syscallnames,
86 .sv_shared_page_base = SHAREDPAGE,
87 .sv_shared_page_len = PAGE_SIZE,
89 .sv_thread_detach = NULL,
92 INIT_SYSENTVEC(elf64_sysvec_v1, &elf64_freebsd_sysvec_v1);
94 struct sysentvec elf64_freebsd_sysvec_v2 = {
95 .sv_size = SYS_MAXSYSCALL,
100 .sv_transtrap = NULL,
101 .sv_fixup = __elfN(freebsd_fixup),
102 .sv_sendsig = sendsig,
103 .sv_sigcode = sigcode64_elfv2,
104 .sv_szsigcode = &szsigcode64_elfv2,
105 .sv_name = "FreeBSD ELF64 V2",
106 .sv_coredump = __elfN(coredump),
107 .sv_imgact_try = NULL,
108 .sv_minsigstksz = MINSIGSTKSZ,
109 .sv_pagesize = PAGE_SIZE,
110 .sv_minuser = VM_MIN_ADDRESS,
111 .sv_maxuser = VM_MAXUSER_ADDRESS,
112 .sv_usrstack = USRSTACK,
113 .sv_psstrings = PS_STRINGS,
114 .sv_stackprot = VM_PROT_ALL,
115 .sv_copyout_strings = exec_copyout_strings,
116 .sv_setregs = exec_setregs,
119 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
120 .sv_set_syscall_retval = cpu_set_syscall_retval,
121 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
122 .sv_syscallnames = syscallnames,
123 .sv_shared_page_base = SHAREDPAGE,
124 .sv_shared_page_len = PAGE_SIZE,
125 .sv_schedtail = NULL,
126 .sv_thread_detach = NULL,
128 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2);
130 static boolean_t ppc64_elfv1_header_match(struct image_params *params);
131 static boolean_t ppc64_elfv2_header_match(struct image_params *params);
133 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
134 .brand = ELFOSABI_FREEBSD,
136 .compat_3_brand = "FreeBSD",
138 .interp_path = "/libexec/ld-elf.so.1",
139 .sysvec = &elf64_freebsd_sysvec_v1,
140 .interp_newpath = NULL,
141 .brand_note = &elf64_freebsd_brandnote,
142 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
143 .header_supported = &ppc64_elfv1_header_match
146 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
147 (sysinit_cfunc_t) elf64_insert_brand_entry,
148 &freebsd_brand_info_elfv1);
150 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
151 .brand = ELFOSABI_FREEBSD,
153 .compat_3_brand = "FreeBSD",
155 .interp_path = "/libexec/ld-elf.so.1",
156 .sysvec = &elf64_freebsd_sysvec_v2,
157 .interp_newpath = NULL,
158 .brand_note = &elf64_freebsd_brandnote,
159 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
160 .header_supported = &ppc64_elfv2_header_match
163 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
164 (sysinit_cfunc_t) elf64_insert_brand_entry,
165 &freebsd_brand_info_elfv2);
167 static Elf64_Brandinfo freebsd_brand_oinfo = {
168 .brand = ELFOSABI_FREEBSD,
170 .compat_3_brand = "FreeBSD",
172 .interp_path = "/usr/libexec/ld-elf.so.1",
173 .sysvec = &elf64_freebsd_sysvec_v1,
174 .interp_newpath = NULL,
175 .brand_note = &elf64_freebsd_brandnote,
176 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
177 .header_supported = &ppc64_elfv1_header_match
180 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
181 (sysinit_cfunc_t) elf64_insert_brand_entry,
182 &freebsd_brand_oinfo);
184 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
187 ppc64_elfv1_header_match(struct image_params *params)
189 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
190 int abi = (hdr->e_flags & 3);
192 return (abi == 0 || abi == 1);
196 ppc64_elfv2_header_match(struct image_params *params)
198 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
199 int abi = (hdr->e_flags & 3);
205 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
208 struct trapframe *tf;
209 register_t entry_desc[3];
212 exec_setregs(td, imgp, stack);
215 * For 64-bit ELFv1, we need to disentangle the function
220 * 2. Environment pointer (r11)
223 (void)copyin((void *)imgp->entry_addr, entry_desc,
225 tf->srr0 = entry_desc[0] + imgp->reloc_base;
226 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
227 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
231 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
238 if (pcb->pcb_flags & PCB_VEC) {
241 len += elf64_populate_note(NT_PPC_VMX,
243 sizeof(pcb->pcb_vec), NULL);
245 len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
246 sizeof(pcb->pcb_vec), NULL);
252 /* Process one elf relocation with addend. */
254 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
255 int type, int local, elf_lookup_fn lookup)
260 Elf_Word rtype, symidx;
261 const Elf_Rela *rela;
266 panic("PPC only supports RELA relocations");
269 rela = (const Elf_Rela *)data;
270 where = (Elf_Addr *) (relocbase + rela->r_offset);
271 addend = rela->r_addend;
272 rtype = ELF_R_TYPE(rela->r_info);
273 symidx = ELF_R_SYM(rela->r_info);
276 panic("elf_reloc: unknown relocation mode %d\n", type);
284 case R_PPC64_ADDR64: /* doubleword64 S + A */
285 error = lookup(lf, symidx, 1, &addr);
292 case R_PPC_RELATIVE: /* doubleword64 B + A */
293 *where = elf_relocaddr(lf, relocbase + addend);
296 case R_PPC_JMP_SLOT: /* function descriptor copy */
297 lookup(lf, symidx, 1, &addr);
298 #if !defined(_CALL_ELF) || _CALL_ELF == 1
299 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
301 memcpy(where, (Elf_Addr *)addr, sizeof(Elf_Addr));
303 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
307 printf("kldload: unexpected relocation type %d\n",
315 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
317 Elf_Rela *rela = NULL, *relalim;
322 * Extract the rela/relasz values from the dynamic section
324 for (; dynp->d_tag != DT_NULL; dynp++) {
325 switch (dynp->d_tag) {
327 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
330 relasz = dynp->d_un.d_val;
336 * Relocate these values
338 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
339 for (; rela < relalim; rela++) {
340 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
342 where = (Elf_Addr *)(relocbase + rela->r_offset);
343 *where = (Elf_Addr)(relocbase + rela->r_addend);
348 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
349 elf_lookup_fn lookup)
352 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
356 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
357 int type, elf_lookup_fn lookup)
360 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
364 elf_cpu_load_file(linker_file_t lf)
366 /* Only sync the cache for non-kernel modules */
368 __syncicache(lf->address, lf->size);
373 elf_cpu_unload_file(linker_file_t lf __unused)