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>
44 #include <sys/syscall.h>
45 #include <sys/signalvar.h>
46 #include <sys/vnode.h>
47 #include <sys/linker.h>
50 #include <vm/vm_param.h>
52 #include <machine/altivec.h>
53 #include <machine/cpu.h>
54 #include <machine/elf.h>
55 #include <machine/reg.h>
56 #include <machine/md_var.h>
59 #include <compat/freebsd32/freebsd32_proto.h>
60 #include <compat/freebsd32/freebsd32_util.h>
62 extern const char *freebsd32_syscallnames[];
65 struct sysentvec elf32_freebsd_sysvec = {
66 .sv_size = SYS_MAXSYSCALL,
68 .sv_table = freebsd32_sysent,
76 .sv_fixup = __elfN(freebsd_fixup),
77 .sv_sendsig = sendsig,
78 .sv_sigcode = sigcode32,
79 .sv_szsigcode = &szsigcode32,
80 .sv_name = "FreeBSD ELF32",
81 .sv_coredump = __elfN(coredump),
82 .sv_imgact_try = NULL,
83 .sv_minsigstksz = MINSIGSTKSZ,
84 .sv_pagesize = PAGE_SIZE,
85 .sv_minuser = VM_MIN_ADDRESS,
86 .sv_stackprot = VM_PROT_ALL,
88 .sv_maxuser = VM_MAXUSER_ADDRESS,
89 .sv_usrstack = FREEBSD32_USRSTACK,
90 .sv_psstrings = FREEBSD32_PS_STRINGS,
91 .sv_copyout_strings = freebsd32_copyout_strings,
92 .sv_setregs = ppc32_setregs,
93 .sv_syscallnames = freebsd32_syscallnames,
95 .sv_maxuser = VM_MAXUSER_ADDRESS,
96 .sv_usrstack = USRSTACK,
97 .sv_psstrings = PS_STRINGS,
98 .sv_copyout_strings = exec_copyout_strings,
99 .sv_setregs = exec_setregs,
100 .sv_syscallnames = syscallnames,
104 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP,
105 .sv_set_syscall_retval = cpu_set_syscall_retval,
106 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
107 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
108 .sv_shared_page_len = PAGE_SIZE,
109 .sv_schedtail = NULL,
110 .sv_thread_detach = NULL,
113 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
115 static Elf32_Brandinfo freebsd_brand_info = {
116 .brand = ELFOSABI_FREEBSD,
118 .compat_3_brand = "FreeBSD",
120 .interp_path = "/libexec/ld-elf.so.1",
121 .sysvec = &elf32_freebsd_sysvec,
123 .interp_newpath = "/libexec/ld-elf32.so.1",
125 .interp_newpath = NULL,
127 .brand_note = &elf32_freebsd_brandnote,
128 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
131 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
132 (sysinit_cfunc_t) elf32_insert_brand_entry,
133 &freebsd_brand_info);
135 static Elf32_Brandinfo freebsd_brand_oinfo = {
136 .brand = ELFOSABI_FREEBSD,
138 .compat_3_brand = "FreeBSD",
140 .interp_path = "/usr/libexec/ld-elf.so.1",
141 .sysvec = &elf32_freebsd_sysvec,
142 .interp_newpath = NULL,
143 .brand_note = &elf32_freebsd_brandnote,
144 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
147 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
148 (sysinit_cfunc_t) elf32_insert_brand_entry,
149 &freebsd_brand_oinfo);
151 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
154 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
161 if (pcb->pcb_flags & PCB_VEC) {
164 len += elf32_populate_note(NT_PPC_VMX,
166 sizeof(pcb->pcb_vec), NULL);
168 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
169 sizeof(pcb->pcb_vec), NULL);
174 #ifndef __powerpc64__
175 /* Process one elf relocation with addend. */
177 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
178 int type, int local, elf_lookup_fn lookup)
184 Elf_Word rtype, symidx;
185 const Elf_Rela *rela;
190 panic("PPC only supports RELA relocations");
193 rela = (const Elf_Rela *)data;
194 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
195 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
196 addend = rela->r_addend;
197 rtype = ELF_R_TYPE(rela->r_info);
198 symidx = ELF_R_SYM(rela->r_info);
201 panic("elf_reloc: unknown relocation mode %d\n", type);
209 case R_PPC_ADDR32: /* word32 S + A */
210 error = lookup(lf, symidx, 1, &addr);
213 *where = elf_relocaddr(lf, addr + addend);
216 case R_PPC_ADDR16_LO: /* #lo(S) */
217 error = lookup(lf, symidx, 1, &addr);
221 * addend values are sometimes relative to sections
222 * (i.e. .rodata) in rela, where in reality they
223 * are relative to relocbase. Detect this condition.
225 if (addr > relocbase && addr <= (relocbase + addend))
227 addr = elf_relocaddr(lf, addr + addend);
228 *hwhere = addr & 0xffff;
231 case R_PPC_ADDR16_HA: /* #ha(S) */
232 error = lookup(lf, symidx, 1, &addr);
236 * addend values are sometimes relative to sections
237 * (i.e. .rodata) in rela, where in reality they
238 * are relative to relocbase. Detect this condition.
240 if (addr > relocbase && addr <= (relocbase + addend))
242 addr = elf_relocaddr(lf, addr + addend);
243 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
247 case R_PPC_RELATIVE: /* word32 B + A */
248 *where = elf_relocaddr(lf, relocbase + addend);
252 printf("kldload: unexpected relocation type %d\n",
260 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
262 Elf_Rela *rela = NULL, *relalim;
267 * Extract the rela/relasz values from the dynamic section
269 for (; dynp->d_tag != DT_NULL; dynp++) {
270 switch (dynp->d_tag) {
272 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
275 relasz = dynp->d_un.d_val;
281 * Relocate these values
283 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
284 for (; rela < relalim; rela++) {
285 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
287 where = (Elf_Addr *)(relocbase + rela->r_offset);
288 *where = (Elf_Addr)(relocbase + rela->r_addend);
293 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
294 elf_lookup_fn lookup)
297 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
301 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
302 int type, elf_lookup_fn lookup)
305 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
309 elf_cpu_load_file(linker_file_t lf)
311 /* Only sync the cache for non-kernel modules */
313 __syncicache(lf->address, lf->size);
318 elf_cpu_unload_file(linker_file_t lf __unused)