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/jail.h>
45 #include <sys/reg.h>
46 #include <sys/smp.h>
47 #include <sys/syscall.h>
48 #include <sys/sysctl.h>
49 #include <sys/signalvar.h>
50 #include <sys/vnode.h>
51 #include <sys/linker.h>
52
53 #include <vm/vm.h>
54 #include <vm/vm_param.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_map.h>
57
58 #include <machine/altivec.h>
59 #include <machine/cpu.h>
60 #include <machine/fpu.h>
61 #include <machine/elf.h>
62 #include <machine/md_var.h>
63
64 #include <powerpc/powerpc/elf_common.c>
65
66 #ifdef __powerpc64__
67 #include <compat/freebsd32/freebsd32_proto.h>
68 #include <compat/freebsd32/freebsd32_util.h>
69
70 extern const char *freebsd32_syscallnames[];
71 static void ppc32_fixlimit(struct rlimit *rl, int which);
72
73 static SYSCTL_NODE(_compat, OID_AUTO, ppc32, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
74 "32-bit mode");
75
76 #define PPC32_MAXDSIZ (1024*1024*1024)
77 static u_long ppc32_maxdsiz = PPC32_MAXDSIZ;
78 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxdsiz, CTLFLAG_RWTUN, &ppc32_maxdsiz,
79 0, "");
80 #define PPC32_MAXSSIZ (64*1024*1024)
81 u_long ppc32_maxssiz = PPC32_MAXSSIZ;
82 SYSCTL_ULONG(_compat_ppc32, OID_AUTO, maxssiz, CTLFLAG_RWTUN, &ppc32_maxssiz,
83 0, "");
84 #else
85 static void ppc32_runtime_resolve(void);
86 #endif
87
88 struct sysentvec elf32_freebsd_sysvec = {
89 .sv_size = SYS_MAXSYSCALL,
90 #ifdef __powerpc64__
91 .sv_table = freebsd32_sysent,
92 #else
93 .sv_table = sysent,
94 #endif
95 .sv_fixup = __elfN(freebsd_fixup),
96 .sv_copyout_auxargs = __elfN(powerpc_copyout_auxargs),
97 .sv_sendsig = sendsig,
98 .sv_sigcode = sigcode32,
99 .sv_szsigcode = &szsigcode32,
100 .sv_name = "FreeBSD ELF32",
101 .sv_coredump = __elfN(coredump),
102 .sv_elf_core_osabi = ELFOSABI_FREEBSD,
103 .sv_elf_core_abi_vendor = FREEBSD_ABI_VENDOR,
104 .sv_elf_core_prepare_notes = __elfN(prepare_notes),
105 .sv_imgact_try = NULL,
106 .sv_minsigstksz = MINSIGSTKSZ,
107 .sv_minuser = VM_MIN_ADDRESS,
108 .sv_stackprot = VM_PROT_ALL,
109 #ifdef __powerpc64__
110 .sv_maxuser = VM_MAXUSER_ADDRESS32,
111 .sv_usrstack = FREEBSD32_USRSTACK,
112 .sv_psstrings = FREEBSD32_PS_STRINGS,
113 .sv_psstringssz = sizeof(struct freebsd32_ps_strings),
114 .sv_copyout_strings = freebsd32_copyout_strings,
115 .sv_setregs = ppc32_setregs,
116 .sv_syscallnames = freebsd32_syscallnames,
117 .sv_fixlimit = ppc32_fixlimit,
118 #else
119 .sv_maxuser = VM_MAXUSER_ADDRESS,
120 .sv_usrstack = USRSTACK,
121 .sv_psstrings = PS_STRINGS,
122 .sv_psstringssz = sizeof(struct ps_strings),
123 .sv_copyout_strings = exec_copyout_strings,
124 .sv_setregs = exec_setregs,
125 .sv_syscallnames = syscallnames,
126 .sv_fixlimit = NULL,
127 #endif
128 .sv_maxssiz = NULL,
129 .sv_flags = SV_ABI_FREEBSD | SV_ILP32 | SV_SHP | SV_ASLR |
130 SV_TIMEKEEP | SV_RNG_SEED_VER,
131 .sv_set_syscall_retval = cpu_set_syscall_retval,
132 .sv_fetch_syscall_args = cpu_fetch_syscall_args,
133 .sv_shared_page_base = FREEBSD32_SHAREDPAGE,
134 .sv_shared_page_len = PAGE_SIZE,
135 .sv_schedtail = NULL,
136 .sv_thread_detach = NULL,
137 .sv_trap = NULL,
138 .sv_hwcap = &cpu_features,
139 .sv_hwcap2 = &cpu_features2,
140 .sv_onexec_old = exec_onexec_old,
141 .sv_onexit = exit_onexit,
142 .sv_regset_begin = SET_BEGIN(__elfN(regset)),
143 .sv_regset_end = SET_LIMIT(__elfN(regset)),
144 };
145 INIT_SYSENTVEC(elf32_sysvec, &elf32_freebsd_sysvec);
146
147 static Elf32_Brandinfo freebsd_brand_info = {
148 .brand = ELFOSABI_FREEBSD,
149 .machine = EM_PPC,
150 .compat_3_brand = "FreeBSD",
151 .emul_path = NULL,
152 .interp_path = "/libexec/ld-elf.so.1",
153 .sysvec = &elf32_freebsd_sysvec,
154 #ifdef __powerpc64__
155 .interp_newpath = "/libexec/ld-elf32.so.1",
156 #else
157 .interp_newpath = NULL,
158 #endif
159 .brand_note = &elf32_freebsd_brandnote,
160 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
161 };
162
163 SYSINIT(elf32, SI_SUB_EXEC, SI_ORDER_FIRST,
164 (sysinit_cfunc_t) elf32_insert_brand_entry,
165 &freebsd_brand_info);
166
167 static Elf32_Brandinfo freebsd_brand_oinfo = {
168 .brand = ELFOSABI_FREEBSD,
169 .machine = EM_PPC,
170 .compat_3_brand = "FreeBSD",
171 .emul_path = NULL,
172 .interp_path = "/usr/libexec/ld-elf.so.1",
173 .sysvec = &elf32_freebsd_sysvec,
174 .interp_newpath = NULL,
175 .brand_note = &elf32_freebsd_brandnote,
176 .flags = BI_CAN_EXEC_DYN | BI_BRAND_NOTE
177 };
178
179 SYSINIT(oelf32, SI_SUB_EXEC, SI_ORDER_ANY,
180 (sysinit_cfunc_t) elf32_insert_brand_entry,
181 &freebsd_brand_oinfo);
182
183 void elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase);
184
185 void
186 elf32_dump_thread(struct thread *td, void *dst, size_t *off)
187 {
188 size_t len;
189 struct pcb *pcb;
190 uint64_t vshr[32];
191 uint64_t *vsr_dw1;
192 int vsr_idx;
193
194 len = 0;
195 pcb = td->td_pcb;
196
197 if (pcb->pcb_flags & PCB_VEC) {
198 save_vec_nodrop(td);
199 if (dst != NULL) {
200 len += elf32_populate_note(NT_PPC_VMX,
201 &pcb->pcb_vec, (char *)dst + len,
202 sizeof(pcb->pcb_vec), NULL);
203 } else
204 len += elf32_populate_note(NT_PPC_VMX, NULL, NULL,
205 sizeof(pcb->pcb_vec), NULL);
206 }
207
208 if (pcb->pcb_flags & PCB_VSX) {
209 save_fpu_nodrop(td);
210 if (dst != NULL) {
211 /*
212 * Doubleword 0 of VSR0-VSR31 overlap with FPR0-FPR31 and
213 * VSR32-VSR63 overlap with VR0-VR31, so we only copy
214 * the non-overlapping data, which is doubleword 1 of VSR0-VSR31.
215 */
216 for (vsr_idx = 0; vsr_idx < nitems(vshr); vsr_idx++) {
217 vsr_dw1 = (uint64_t *)&pcb->pcb_fpu.fpr[vsr_idx].vsr[2];
218 vshr[vsr_idx] = *vsr_dw1;
219 }
220 len += elf32_populate_note(NT_PPC_VSX,
221 vshr, (char *)dst + len,
222 sizeof(vshr), NULL);
223 } else
224 len += elf32_populate_note(NT_PPC_VSX, NULL, NULL,
225 sizeof(vshr), NULL);
226 }
227
228 *off = len;
229 }
230
231 #ifndef __powerpc64__
232 bool
233 elf_is_ifunc_reloc(Elf_Size r_info)
234 {
235
236 return (ELF_R_TYPE(r_info) == R_PPC_IRELATIVE);
237 }
238
239 /* Process one elf relocation with addend. */
240 static int
241 elf_reloc_internal(linker_file_t lf, Elf_Addr relocbase, const void *data,
242 int type, int local, elf_lookup_fn lookup)
243 {
244 Elf_Addr *where;
245 Elf_Half *hwhere;
246 Elf_Addr addr;
247 Elf_Addr addend, val;
248 Elf_Word rtype, symidx;
249 const Elf_Rela *rela;
250 int error;
251
252 switch (type) {
253 case ELF_RELOC_REL:
254 panic("PPC only supports RELA relocations");
255 break;
256 case ELF_RELOC_RELA:
257 rela = (const Elf_Rela *)data;
258 where = (Elf_Addr *) ((uintptr_t)relocbase + rela->r_offset);
259 hwhere = (Elf_Half *) ((uintptr_t)relocbase + rela->r_offset);
260 addend = rela->r_addend;
261 rtype = ELF_R_TYPE(rela->r_info);
262 symidx = ELF_R_SYM(rela->r_info);
263 break;
264 default:
265 panic("elf_reloc: unknown relocation mode %d\n", type);
266 }
267
268 switch (rtype) {
269 case R_PPC_NONE:
270 break;
271
272 case R_PPC_ADDR32: /* word32 S + A */
273 error = lookup(lf, symidx, 1, &addr);
274 if (error != 0)
275 return (-1);
276 *where = elf_relocaddr(lf, addr + addend);
277 break;
278
279 case R_PPC_ADDR16_LO: /* #lo(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 & 0xffff;
292 break;
293
294 case R_PPC_ADDR16_HA: /* #ha(S) */
295 error = lookup(lf, symidx, 1, &addr);
296 if (error != 0)
297 return (-1);
298 /*
299 * addend values are sometimes relative to sections
300 * (i.e. .rodata) in rela, where in reality they
301 * are relative to relocbase. Detect this condition.
302 */
303 if (addr > relocbase && addr <= (relocbase + addend))
304 addr = relocbase;
305 addr = elf_relocaddr(lf, addr + addend);
306 *hwhere = ((addr >> 16) + ((addr & 0x8000) ? 1 : 0))
307 & 0xffff;
308 break;
309
310 case R_PPC_RELATIVE: /* word32 B + A */
311 *where = elf_relocaddr(lf, relocbase + addend);
312 break;
313
314 case R_PPC_JMP_SLOT: /* PLT jump slot entry */
315 /*
316 * We currently only support Secure-PLT jump slots.
317 * Given that we reject BSS-PLT modules during load, we
318 * don't need to check again.
319 * The method we are using here is equivilent to
320 * LD_BIND_NOW.
321 */
322 error = lookup(lf, symidx, 1, &addr);
323 if (error != 0)
324 return (-1);
325 *where = elf_relocaddr(lf, addr + addend);
326 break;
327
328 case R_PPC_IRELATIVE:
329 addr = relocbase + addend;
330 val = ((Elf32_Addr (*)(void))addr)();
331 if (*where != val)
332 *where = val;
333 break;
334
335 default:
336 printf("kldload: unexpected relocation type %d, "
337 "symbol index %d\n", (int)rtype, symidx);
338 return (-1);
339 }
340 return (0);
341 }
342
343 void
344 elf_reloc_self(Elf_Dyn *dynp, Elf_Addr relocbase)
345 {
346 Elf_Rela *rela = NULL, *relalim;
347 Elf_Addr relasz = 0;
348 Elf_Addr *where;
349
350 /*
351 * Extract the rela/relasz values from the dynamic section
352 */
353 for (; dynp->d_tag != DT_NULL; dynp++) {
354 switch (dynp->d_tag) {
355 case DT_RELA:
356 rela = (Elf_Rela *)(relocbase+dynp->d_un.d_ptr);
357 break;
358 case DT_RELASZ:
359 relasz = dynp->d_un.d_val;
360 break;
361 }
362 }
363
364 /*
365 * Relocate these values
366 */
367 relalim = (Elf_Rela *)((caddr_t)rela + relasz);
368 for (; rela < relalim; rela++) {
369 if (ELF_R_TYPE(rela->r_info) != R_PPC_RELATIVE)
370 continue;
371 where = (Elf_Addr *)(relocbase + rela->r_offset);
372 *where = (Elf_Addr)(relocbase + rela->r_addend);
373 }
374 }
375
376 int
377 elf_reloc(linker_file_t lf, Elf_Addr relocbase, const void *data, int type,
378 elf_lookup_fn lookup)
379 {
380
381 return (elf_reloc_internal(lf, relocbase, data, type, 0, lookup));
382 }
383
384 int
385 elf_reloc_local(linker_file_t lf, Elf_Addr relocbase, const void *data,
386 int type, elf_lookup_fn lookup)
387 {
388
389 return (elf_reloc_internal(lf, relocbase, data, type, 1, lookup));
390 }
391
392 int
393 elf_cpu_load_file(linker_file_t lf)
394 {
395
396 /* Only sync the cache for non-kernel modules */
397 if (lf->id != 1)
398 __syncicache(lf->address, lf->size);
399 return (0);
400 }
401
402 int
403 elf_cpu_unload_file(linker_file_t lf __unused)
404 {
405
406 return (0);
407 }
408
409 static void
410 ppc32_runtime_resolve()
411 {
412
413 /*
414 * Since we don't support lazy binding, panic immediately if anyone
415 * manages to call the runtime resolver.
416 */
417 panic("kldload: Runtime resolver was called unexpectedly!");
418 }
419
420 int
421 elf_cpu_parse_dynamic(caddr_t loadbase, Elf_Dyn *dynamic)
422 {
423 Elf_Dyn *dp;
424 bool has_plt = false;
425 bool secure_plt = false;
426 Elf_Addr *got;
427
428 for (dp = dynamic; dp->d_tag != DT_NULL; dp++) {
429 switch (dp->d_tag) {
430 case DT_PPC_GOT:
431 secure_plt = true;
432 got = (Elf_Addr *)(loadbase + dp->d_un.d_ptr);
433 /* Install runtime resolver canary. */
434 got[1] = (Elf_Addr)ppc32_runtime_resolve;
435 got[2] = (Elf_Addr)0;
436 break;
437 case DT_PLTGOT:
438 has_plt = true;
439 break;
440 }
441 }
442
443 if (has_plt && !secure_plt) {
444 printf("kldload: BSS-PLT modules are not supported.\n");
445 return (-1);
446 }
447 return (0);
448 }
449 #endif
450
451 #ifdef __powerpc64__
452 static void
453 ppc32_fixlimit(struct rlimit *rl, int which)
454 {
455 switch (which) {
456 case RLIMIT_DATA:
457 if (ppc32_maxdsiz != 0) {
458 if (rl->rlim_cur > ppc32_maxdsiz)
459 rl->rlim_cur = ppc32_maxdsiz;
460 if (rl->rlim_max > ppc32_maxdsiz)
461 rl->rlim_max = ppc32_maxdsiz;
462 }
463 break;
464 case RLIMIT_STACK:
465 if (ppc32_maxssiz != 0) {
466 if (rl->rlim_cur > ppc32_maxssiz)
467 rl->rlim_cur = ppc32_maxssiz;
468 if (rl->rlim_max > ppc32_maxssiz)
469 rl->rlim_max = ppc32_maxssiz;
470 }
471 break;
472 }
473 }
474 #endif
Cache object: be5c9737ce366565b5a995b6d91b32d6
|