2 * Copyright 1996-1998 John D. Polstra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/param.h>
29 #include <sys/kernel.h>
30 #include <sys/systm.h>
32 #include <sys/imgact.h>
33 #include <sys/malloc.h>
35 #include <sys/namei.h>
36 #include <sys/fcntl.h>
37 #include <sys/sysent.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/syscall.h>
40 #include <sys/signalvar.h>
41 #include <sys/vnode.h>
42 #include <sys/linker.h>
45 #include <vm/vm_param.h>
47 #include <machine/altivec.h>
48 #include <machine/cpu.h>
49 #include <machine/elf.h>
50 #include <machine/md_var.h>
52 static void exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
55 struct sysentvec elf64_freebsd_sysvec_v1 = {
56 .sv_size = SYS_MAXSYSCALL,
62 .sv_fixup = __elfN(freebsd_fixup),
63 .sv_sendsig = sendsig,
64 .sv_sigcode = sigcode64,
65 .sv_szsigcode = &szsigcode64,
66 .sv_name = "FreeBSD ELF64",
67 .sv_coredump = __elfN(coredump),
68 .sv_imgact_try = NULL,
69 .sv_minsigstksz = MINSIGSTKSZ,
70 .sv_pagesize = PAGE_SIZE,
71 .sv_minuser = VM_MIN_ADDRESS,
72 .sv_maxuser = VM_MAXUSER_ADDRESS,
73 .sv_usrstack = USRSTACK,
74 .sv_psstrings = PS_STRINGS,
75 .sv_stackprot = VM_PROT_ALL,
76 .sv_copyout_strings = exec_copyout_strings,
77 .sv_setregs = exec_setregs_funcdesc,
80 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
81 .sv_set_syscall_retval = cpu_set_syscall_retval,
82 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
83 .sv_syscallnames = syscallnames,
84 .sv_shared_page_base = SHAREDPAGE,
85 .sv_shared_page_len = PAGE_SIZE,
87 .sv_thread_detach = NULL,
90 INIT_SYSENTVEC(elf64_sysvec_v1, &elf64_freebsd_sysvec_v1);
92 struct sysentvec elf64_freebsd_sysvec_v2 = {
93 .sv_size = SYS_MAXSYSCALL,
99 .sv_fixup = __elfN(freebsd_fixup),
100 .sv_sendsig = sendsig,
101 .sv_sigcode = sigcode64_elfv2,
102 .sv_szsigcode = &szsigcode64_elfv2,
103 .sv_name = "FreeBSD ELF64 V2",
104 .sv_coredump = __elfN(coredump),
105 .sv_imgact_try = NULL,
106 .sv_minsigstksz = MINSIGSTKSZ,
107 .sv_pagesize = PAGE_SIZE,
108 .sv_minuser = VM_MIN_ADDRESS,
109 .sv_maxuser = VM_MAXUSER_ADDRESS,
110 .sv_usrstack = USRSTACK,
111 .sv_psstrings = PS_STRINGS,
112 .sv_stackprot = VM_PROT_ALL,
113 .sv_copyout_strings = exec_copyout_strings,
114 .sv_setregs = exec_setregs,
117 .sv_flags = SV_ABI_FREEBSD | SV_LP64 | SV_SHP,
118 .sv_set_syscall_retval = cpu_set_syscall_retval,
119 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
120 .sv_syscallnames = syscallnames,
121 .sv_shared_page_base = SHAREDPAGE,
122 .sv_shared_page_len = PAGE_SIZE,
123 .sv_schedtail = NULL,
124 .sv_thread_detach = NULL,
126 INIT_SYSENTVEC(elf64_sysvec_v2, &elf64_freebsd_sysvec_v2);
128 static boolean_t ppc64_elfv1_header_match(struct image_params *params);
129 static boolean_t ppc64_elfv2_header_match(struct image_params *params);
131 static Elf64_Brandinfo freebsd_brand_info_elfv1 = {
132 .brand = ELFOSABI_FREEBSD,
134 .compat_3_brand = "FreeBSD",
136 .interp_path = "/libexec/ld-elf.so.1",
137 .sysvec = &elf64_freebsd_sysvec_v1,
138 .interp_newpath = NULL,
139 .brand_note = &elf64_freebsd_brandnote,
140 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
141 .header_supported = &ppc64_elfv1_header_match
144 SYSINIT(elf64v1, SI_SUB_EXEC, SI_ORDER_ANY,
145 (sysinit_cfunc_t) elf64_insert_brand_entry,
146 &freebsd_brand_info_elfv1);
148 static Elf64_Brandinfo freebsd_brand_info_elfv2 = {
149 .brand = ELFOSABI_FREEBSD,
151 .compat_3_brand = "FreeBSD",
153 .interp_path = "/libexec/ld-elf.so.1",
154 .sysvec = &elf64_freebsd_sysvec_v2,
155 .interp_newpath = NULL,
156 .brand_note = &elf64_freebsd_brandnote,
157 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
158 .header_supported = &ppc64_elfv2_header_match
161 SYSINIT(elf64v2, SI_SUB_EXEC, SI_ORDER_ANY,
162 (sysinit_cfunc_t) elf64_insert_brand_entry,
163 &freebsd_brand_info_elfv2);
165 static Elf64_Brandinfo freebsd_brand_oinfo = {
166 .brand = ELFOSABI_FREEBSD,
168 .compat_3_brand = "FreeBSD",
170 .interp_path = "/usr/libexec/ld-elf.so.1",
171 .sysvec = &elf64_freebsd_sysvec_v1,
172 .interp_newpath = NULL,
173 .brand_note = &elf64_freebsd_brandnote,
174 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE,
175 .header_supported = &ppc64_elfv1_header_match
178 SYSINIT(oelf64, SI_SUB_EXEC, SI_ORDER_ANY,
179 (sysinit_cfunc_t) elf64_insert_brand_entry,
180 &freebsd_brand_oinfo);
182 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
185 ppc64_elfv1_header_match(struct image_params *params)
187 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
188 int abi = (hdr->e_flags & 3);
190 return (abi == 0 || abi == 1);
194 ppc64_elfv2_header_match(struct image_params *params)
196 const Elf64_Ehdr *hdr = (const Elf64_Ehdr *)params->image_header;
197 int abi = (hdr->e_flags & 3);
203 exec_setregs_funcdesc(struct thread *td, struct image_params *imgp,
206 struct trapframe *tf;
207 register_t entry_desc[3];
210 exec_setregs(td, imgp, stack);
213 * For 64-bit ELFv1, we need to disentangle the function
218 * 2. Environment pointer (r11)
221 (void)copyin((void *)imgp->entry_addr, entry_desc,
223 tf->srr0 = entry_desc[0] + imgp->reloc_base;
224 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
225 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
229 elf64_dump_thread(struct thread *td, void *dst, size_t *off)
236 if (pcb->pcb_flags & PCB_VEC) {
239 len += elf64_populate_note(NT_PPC_VMX,
241 sizeof(pcb->pcb_vec), NULL);
243 len += elf64_populate_note(NT_PPC_VMX, NULL, NULL,
244 sizeof(pcb->pcb_vec), NULL);
250 /* Process one elf relocation with addend. */
252 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
253 int type, int local, elf_lookup_fn lookup)
258 Elf_Word rtype, symidx;
259 const Elf_Rela *rela;
264 panic("PPC only supports RELA relocations");
267 rela = (const Elf_Rela *)data;
268 where = (Elf_Addr *) (relocbase + rela->r_offset);
269 addend = rela->r_addend;
270 rtype = ELF_R_TYPE(rela->r_info);
271 symidx = ELF_R_SYM(rela->r_info);
274 panic("elf_reloc: unknown relocation mode %d\n", type);
282 case R_PPC64_ADDR64: /* doubleword64 S + A */
283 error = lookup(lf, symidx, 1, &addr);
290 case R_PPC_RELATIVE: /* doubleword64 B + A */
291 *where = elf_relocaddr(lf, relocbase + addend);
294 case R_PPC_JMP_SLOT: /* function descriptor copy */
295 lookup(lf, symidx, 1, &addr);
296 #if !defined(_CALL_ELF) || _CALL_ELF == 1
297 memcpy(where, (Elf_Addr *)addr, 3*sizeof(Elf_Addr));
299 memcpy(where, (Elf_Addr *)addr, sizeof(Elf_Addr));
301 __asm __volatile("dcbst 0,%0; sync" :: "r"(where) : "memory");
305 printf("kldload: unexpected relocation type %d\n",
313 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
315 Elf_Rela *rela = NULL, *relalim;
320 * Extract the rela/relasz values from the dynamic section
322 for (; dynp->d_tag != DT_NULL; dynp++) {
323 switch (dynp->d_tag) {
325 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
328 relasz = dynp->d_un.d_val;
334 * Relocate these values
336 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
337 for (; rela < relalim; rela++) {
338 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
340 where = (Elf_Addr *)(relocbase + rela->r_offset);
341 *where = (Elf_Addr)(relocbase + rela->r_addend);
346 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
347 elf_lookup_fn lookup)
350 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
354 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
355 int type, elf_lookup_fn lookup)
358 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
362 elf_cpu_load_file(linker_file_t lf)
364 /* Only sync the cache for non-kernel modules */
366 __syncicache(lf->address, lf->size);
371 elf_cpu_unload_file(linker_file_t lf __unused)