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