1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright 1996-1998 John D. Polstra.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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.
26 *
27 * $FreeBSD$
28 */
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33
34 #define __ELF_WORD_SIZE 32
35
36 #include <sys/exec.h>
37 #include <sys/imgact.h>
38 #include <sys/malloc.h>
39 #include <sys/proc.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/sysctl.h>
46 #include <sys/signalvar.h>
47 #include <sys/vnode.h>
48 #include <sys/linker.h>
49
50 #include <vm/vm.h>
51 #include <vm/vm_param.h>
52
53 #include <machine/altivec.h>
54 #include <machine/cpu.h>
55 #include <machine/fpu.h>
56 #include <machine/elf.h>
57 #include <machine/reg.h>
58 #include <machine/md_var.h>
59
60 #ifdef __powerpc64__
61 #include <compat/freebsd32/freebsd32_proto.h>
62 #include <compat/freebsd32/freebsd32_util.h>
63
64 extern const char *freebsd32_syscallnames[];
65 static void ppc32_fixlimit(struct rlimit *rl, int which);
66
67 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW, 0, "32-bit mode");
68
69 #define PPC32_MAXDSIZ (1024*1024*1024)
70 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
71 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
72 0, "");
73 #define PPC32_MAXSSIZ (64*1024*1024)
74 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
75 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
76 0, "");
77 #endif
78
79 struct sysentvec elf32_freebsd_sysvec = {
80 .sv_size = SYS_MAXSYSCALL,
81 #ifdef __powerpc64__
82 .sv_table = freebsd32_sysent,
83 #else
84 .sv_table = sysent,
85 #endif
86 .sv_mask = 0,
87 .sv_errsize = 0,
88 .sv_errtbl = NULL,
89 .sv_transtrap = NULL,
90 .sv_fixup = __elfN(freebsd_fixup),
91 .sv_sendsig = sendsig,
92 .sv_sigcode = sigcode32,
93 .sv_szsigcode = &szsigcode32,
94 .sv_name = "FreeBSD ELF32",
95 .sv_coredump = __elfN(coredump),
96 .sv_imgact_try = NULL,
97 .sv_minsigstksz = MINSIGSTKSZ,
98 .sv_minuser = VM_MIN_ADDRESS,
99 .sv_stackprot = VM_PROT_ALL,
100 #ifdef __powerpc64__
101 .sv_maxuser = VM_MAXUSER_ADDRESS32,
102 .sv_usrstack = FREEBSD32_USRSTACK,
103 .sv_psstrings = FREEBSD32_PS_STRINGS,
104 .sv_copyout_strings = freebsd32_copyout_strings,
105 .sv_setregs = ppc32_setregs,
106 .sv_syscallnames = freebsd32_syscallnames,
107 .sv_fixlimit = ppc32_fixlimit,
108 #else
109 .sv_maxuser = VM_MAXUSER_ADDRESS,
110 .sv_usrstack = USRSTACK,
111 .sv_psstrings = PS_STRINGS,
112 .sv_copyout_strings = exec_copyout_strings,
113 .sv_setregs = exec_setregs,
114 .sv_syscallnames = syscallnames,
115 .sv_fixlimit = NULL,
116 #endif
117 .sv_maxssiz = NULL,
118 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR,
119 .sv_set_syscall_retval = cpu_set_syscall_retval,
120 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
121 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
122 .sv_shared_page_len = PAGE_SIZE,
123 .sv_schedtail = NULL,
124 .sv_thread_detach = NULL,
125 .sv_trap = NULL,
126 .sv_hwcap = &cpu_features,
127 .sv_hwcap2 = &cpu_features2,
128 };
129 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
130
131 static Elf32_Brandinfo freebsd_brand_info = {
132 .brand = ELFOSABI_FREEBSD,
133 .machine = EM_PPC,
134 .compat_3_brand = "FreeBSD",
135 .emul_path = NULL,
136 .interp_path = "/libexec/ld-elf.so.1",
137 .sysvec = &elf32_freebsd_sysvec,
138 #ifdef __powerpc64__
139 .interp_newpath = "/libexec/ld-elf32.so.1",
140 #else
141 .interp_newpath = NULL,
142 #endif
143 .brand_note = &elf32_freebsd_brandnote,
144 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
145 };
146
147 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
148 (sysinit_cfunc_t) elf32_insert_brand_entry,
149 &freebsd_brand_info);
150
151 static Elf32_Brandinfo freebsd_brand_oinfo = {
152 .brand = ELFOSABI_FREEBSD,
153 .machine = EM_PPC,
154 .compat_3_brand = "FreeBSD",
155 .emul_path = NULL,
156 .interp_path = "/usr/libexec/ld-elf.so.1",
157 .sysvec = &elf32_freebsd_sysvec,
158 .interp_newpath = NULL,
159 .brand_note = &elf32_freebsd_brandnote,
160 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
161 };
162
163 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
164 (sysinit_cfunc_t) elf32_insert_brand_entry,
165 &freebsd_brand_oinfo);
166
167 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
168
169 void
170 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
171 {
172 size_t len;
173 struct pcb *pcb;
174 uint64_t vshr[32];
175 uint64_t *vsr_dw1;
176 int vsr_idx;
177
178 len = 0;
179 pcb = td->td_pcb;
180
181 if (pcb->pcb_flags & PCB_VEC) {
182 save_vec_nodrop(td);
183 if (dst != NULL) {
184 len += elf32_populate_note(NT_PPC_VMX,
185 &pcb->pcb_vec, (char *)dst + len,
186 sizeof(pcb->pcb_vec), NULL);
187 } else
188 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
189 sizeof(pcb->pcb_vec), NULL);
190 }
191
192 if (pcb->pcb_flags & PCB_VSX) {
193 save_fpu_nodrop(td);
194 if (dst != NULL) {
195 /*
196 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
197 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
198 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
199 */
200 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
201 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
202 vshr[vsr_idx] = *vsr_dw1;
203 }
204 len += elf32_populate_note(NT_PPC_VSX,
205 vshr, (char *)dst + len,
206 sizeof(vshr), NULL);
207 } else
208 len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
209 sizeof(vshr), NULL);
210 }
211
212 *off = len;
213 }
214
215 #ifndef __powerpc64__
216 bool
217 elf_is_ifunc_reloc(Elf_Size r_info __unused)
218 {
219
220 return (false);
221 }
222
223 /* Process one elf relocation with addend. */
224 static int
225 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
226 int type, int local, elf_lookup_fn lookup)
227 {
228 Elf_Addr *where;
229 Elf_Half *hwhere;
230 Elf_Addr addr;
231 Elf_Addr addend;
232 Elf_Word rtype, symidx;
233 const Elf_Rela *rela;
234 int error;
235
236 switch (type) {
237 case ELF_RELOC_REL:
238 panic("PPC only supports RELA relocations");
239 break;
240 case ELF_RELOC_RELA:
241 rela = (const Elf_Rela *)data;
242 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
243 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
244 addend = rela->r_addend;
245 rtype = ELF_R_TYPE(rela->r_info);
246 symidx = ELF_R_SYM(rela->r_info);
247 break;
248 default:
249 panic("elf_reloc: unknown relocation mode %d\n", type);
250 }
251
252 switch (rtype) {
253
254 case R_PPC_NONE:
255 break;
256
257 case R_PPC_ADDR32: /* word32 S + A */
258 error = lookup(lf, symidx, 1, &addr);
259 if (error != 0)
260 return -1;
261 *where = elf_relocaddr(lf, addr + addend);
262 break;
263
264 case R_PPC_ADDR16_LO: /* #lo(S) */
265 error = lookup(lf, symidx, 1, &addr);
266 if (error != 0)
267 return -1;
268 /*
269 * addend values are sometimes relative to sections
270 * (i.e. .rodata) in rela, where in reality they
271 * are relative to relocbase. Detect this condition.
272 */
273 if (addr > relocbase && addr <= (relocbase + addend))
274 addr = relocbase;
275 addr = elf_relocaddr(lf, addr + addend);
276 *hwhere = addr & 0xffff;
277 break;
278
279 case R_PPC_ADDR16_HA: /* #ha(S) */
280 error = lookup(lf, symidx, 1, &addr);
281 if (error != 0)
282 return -1;
283 /*
284 * addend values are sometimes relative to sections
285 * (i.e. .rodata) in rela, where in reality they
286 * are relative to relocbase. Detect this condition.
287 */
288 if (addr > relocbase && addr <= (relocbase + addend))
289 addr = relocbase;
290 addr = elf_relocaddr(lf, addr + addend);
291 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
292 & 0xffff;
293 break;
294
295 case R_PPC_RELATIVE: /* word32 B + A */
296 *where = elf_relocaddr(lf, relocbase + addend);
297 break;
298
299 default:
300 printf("kldload: unexpected relocation type %d\n",
301 (int) rtype);
302 return -1;
303 }
304 return(0);
305 }
306
307 void
308 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
309 {
310 Elf_Rela *rela = NULL, *relalim;
311 Elf_Addr relasz = 0;
312 Elf_Addr *where;
313
314 /*
315 * Extract the rela/relasz values from the dynamic section
316 */
317 for (; dynp->d_tag != DT_NULL; dynp++) {
318 switch (dynp->d_tag) {
319 case DT_RELA:
320 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
321 break;
322 case DT_RELASZ:
323 relasz = dynp->d_un.d_val;
324 break;
325 }
326 }
327
328 /*
329 * Relocate these values
330 */
331 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
332 for (; rela < relalim; rela++) {
333 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
334 continue;
335 where = (Elf_Addr *)(relocbase + rela->r_offset);
336 *where = (Elf_Addr)(relocbase + rela->r_addend);
337 }
338 }
339
340 int
341 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
342 elf_lookup_fn lookup)
343 {
344
345 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
346 }
347
348 int
349 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
350 int type, elf_lookup_fn lookup)
351 {
352
353 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
354 }
355
356 int
357 elf_cpu_load_file(linker_file_t lf)
358 {
359 /* Only sync the cache for non-kernel modules */
360 if (lf->id != 1)
361 __syncicache(lf->address, lf->size);
362 return (0);
363 }
364
365 int
366 elf_cpu_unload_file(linker_file_t lf __unused)
367 {
368
369 return (0);
370 }
371 #endif
372
373 #ifdef __powerpc64__
374 static void
375 ppc32_fixlimit(struct rlimit *rl, int which)
376 {
377 switch (which) {
378 case RLIMIT_DATA:
379 if (ppc32_maxdsiz != 0) {
380 if (rl->rlim_cur > ppc32_maxdsiz)
381 rl->rlim_cur = ppc32_maxdsiz;
382 if (rl->rlim_max > ppc32_maxdsiz)
383 rl->rlim_max = ppc32_maxdsiz;
384 }
385 break;
386 case RLIMIT_STACK:
387 if (ppc32_maxssiz != 0) {
388 if (rl->rlim_cur > ppc32_maxssiz)
389 rl->rlim_cur = ppc32_maxssiz;
390 if (rl->rlim_max > ppc32_maxssiz)
391 rl->rlim_max = ppc32_maxssiz;
392 }
393 break;
394 }
395 }
396 #endif
Cache object: 6eadcb3d6505bcade170f5874ba57b66
|