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 #define __ELF_WORD_SIZE 32
35 #include <sys/imgact.h>
36 #include <sys/malloc.h>
38 #include <sys/namei.h>
39 #include <sys/fcntl.h>
40 #include <sys/sysent.h>
41 #include <sys/imgact_elf.h>
42 #include <sys/syscall.h>
43 #include <sys/signalvar.h>
44 #include <sys/vnode.h>
45 #include <sys/linker.h>
48 #include <vm/vm_param.h>
50 #include <machine/cpu.h>
51 #include <machine/elf.h>
52 #include <machine/reg.h>
53 #include <machine/md_var.h>
56 #include <compat/freebsd32/freebsd32_proto.h>
57 #include <compat/freebsd32/freebsd32_util.h>
59 extern const char *freebsd32_syscallnames[];
62 struct sysentvec elf32_freebsd_sysvec = {
63 .sv_size = SYS_MAXSYSCALL,
65 .sv_table = freebsd32_sysent,
75 .sv_fixup = __elfN(freebsd_fixup),
76 .sv_sendsig = sendsig,
77 .sv_sigcode = sigcode32,
78 .sv_szsigcode = &szsigcode32,
79 .sv_prepsyscall = NULL,
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,
111 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
113 static Elf32_Brandinfo freebsd_brand_info = {
114 .brand = ELFOSABI_FREEBSD,
116 .compat_3_brand = "FreeBSD",
118 .interp_path = "/libexec/ld-elf.so.1",
119 .sysvec = &elf32_freebsd_sysvec,
121 .interp_newpath = "/libexec/ld-elf32.so.1",
123 .interp_newpath = NULL,
125 .brand_note = &elf32_freebsd_brandnote,
126 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
129 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
130 (sysinit_cfunc_t) elf32_insert_brand_entry,
131 &freebsd_brand_info);
133 static Elf32_Brandinfo freebsd_brand_oinfo = {
134 .brand = ELFOSABI_FREEBSD,
136 .compat_3_brand = "FreeBSD",
138 .interp_path = "/usr/libexec/ld-elf.so.1",
139 .sysvec = &elf32_freebsd_sysvec,
140 .interp_newpath = NULL,
141 .brand_note = &elf32_freebsd_brandnote,
142 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
145 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
146 (sysinit_cfunc_t) elf32_insert_brand_entry,
147 &freebsd_brand_oinfo);
150 elf32_dump_thread(struct thread *td __unused, void *dst __unused,
151 size_t *off __unused)
155 #ifndef __powerpc64__
156 /* Process one elf relocation with addend. */
158 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
159 int type, int local, elf_lookup_fn lookup)
165 Elf_Word rtype, symidx;
166 const Elf_Rela *rela;
171 panic("PPC only supports RELA relocations");
174 rela = (const Elf_Rela *)data;
175 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
176 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
177 addend = rela->r_addend;
178 rtype = ELF_R_TYPE(rela->r_info);
179 symidx = ELF_R_SYM(rela->r_info);
182 panic("elf_reloc: unknown relocation mode %d\n", type);
190 case R_PPC_ADDR32: /* word32 S + A */
191 error = lookup(lf, symidx, 1, &addr);
198 case R_PPC_ADDR16_LO: /* #lo(S) */
199 error = lookup(lf, symidx, 1, &addr);
203 * addend values are sometimes relative to sections
204 * (i.e. .rodata) in rela, where in reality they
205 * are relative to relocbase. Detect this condition.
207 if (addr > relocbase && addr <= (relocbase + addend))
208 addr = relocbase + addend;
211 *hwhere = addr & 0xffff;
214 case R_PPC_ADDR16_HA: /* #ha(S) */
215 error = lookup(lf, symidx, 1, &addr);
219 * addend values are sometimes relative to sections
220 * (i.e. .rodata) in rela, where in reality they
221 * are relative to relocbase. Detect this condition.
223 if (addr > relocbase && addr <= (relocbase + addend))
224 addr = relocbase + addend;
227 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
231 case R_PPC_RELATIVE: /* word32 B + A */
232 *where = elf_relocaddr(lf, relocbase + addend);
236 printf("kldload: unexpected relocation type %d\n",
244 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
245 elf_lookup_fn lookup)
248 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
252 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
253 int type, elf_lookup_fn lookup)
256 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
260 elf_cpu_load_file(linker_file_t lf)
262 /* Only sync the cache for non-kernel modules */
264 __syncicache(lf->address, lf->size);
269 elf_cpu_unload_file(linker_file_t lf __unused)