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