1 /* $OpenBSD: exec_elf.c,v 1.114 2015/04/26 05:30:42 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Per Fogelstrom 5 * All rights reserved. 6 * 7 * Copyright (c) 1994 Christos Zoulas 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * 32 */ 33 34 /* 35 * Copyright (c) 2001 Wasabi Systems, Inc. 36 * All rights reserved. 37 * 38 * Written by Jason R. Thorpe for Wasabi Systems, Inc. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed for the NetBSD Project by 51 * Wasabi Systems, Inc. 52 * 4. The name of Wasabi Systems, Inc. may not be used to endorse 53 * or promote products derived from this software without specific prior 54 * written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND 57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/proc.h> 73 #include <sys/malloc.h> 74 #include <sys/pool.h> 75 #include <sys/mount.h> 76 #include <sys/namei.h> 77 #include <sys/vnode.h> 78 #include <sys/core.h> 79 #include <sys/exec.h> 80 #include <sys/exec_elf.h> 81 #include <sys/file.h> 82 #include <sys/ptrace.h> 83 #include <sys/syscall.h> 84 #include <sys/signalvar.h> 85 #include <sys/stat.h> 86 87 #include <sys/mman.h> 88 89 #include <uvm/uvm_extern.h> 90 91 #include <machine/reg.h> 92 #include <machine/exec.h> 93 94 #ifdef COMPAT_LINUX 95 #include <compat/linux/linux_exec.h> 96 #endif 97 98 struct ELFNAME(probe_entry) { 99 int (*func)(struct proc *, struct exec_package *, char *, 100 u_long *); 101 } ELFNAME(probes)[] = { 102 /* XXX - bogus, shouldn't be size independent.. */ 103 #ifdef COMPAT_LINUX 104 { linux_elf_probe }, 105 #endif 106 { NULL } 107 }; 108 109 int ELFNAME(load_file)(struct proc *, char *, struct exec_package *, 110 struct elf_args *, Elf_Addr *); 111 int ELFNAME(check_header)(Elf_Ehdr *); 112 int ELFNAME(read_from)(struct proc *, struct vnode *, u_long, caddr_t, int); 113 void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *, 114 Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int); 115 int ELFNAMEEND(coredump)(struct proc *, void *); 116 117 extern char sigcode[], esigcode[]; 118 #ifdef SYSCALL_DEBUG 119 extern char *syscallnames[]; 120 #endif 121 122 /* round up and down to page boundaries. */ 123 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 124 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 125 126 /* 127 * We limit the number of program headers to 32, this should 128 * be a reasonable limit for ELF, the most we have seen so far is 12 129 */ 130 #define ELF_MAX_VALID_PHDR 32 131 132 /* 133 * This is the basic elf emul. elf_probe_funcs may change to other emuls. 134 */ 135 struct emul ELFNAMEEND(emul) = { 136 "native", 137 NULL, 138 sendsig, 139 SYS_syscall, 140 SYS_MAXSYSCALL, 141 sysent, 142 #ifdef SYSCALL_DEBUG 143 syscallnames, 144 #else 145 NULL, 146 #endif 147 (sizeof(AuxInfo) * ELF_AUX_ENTRIES / sizeof(char *)), 148 ELFNAME(copyargs), 149 setregs, 150 ELFNAME2(exec,fixup), 151 ELFNAMEEND(coredump), 152 sigcode, 153 esigcode, 154 EMUL_ENABLED | EMUL_NATIVE, 155 }; 156 157 /* 158 * Copy arguments onto the stack in the normal way, but add some 159 * space for extra information in case of dynamic binding. 160 */ 161 void * 162 ELFNAME(copyargs)(struct exec_package *pack, struct ps_strings *arginfo, 163 void *stack, void *argp) 164 { 165 stack = copyargs(pack, arginfo, stack, argp); 166 if (!stack) 167 return (NULL); 168 169 /* 170 * Push space for extra arguments on the stack needed by 171 * dynamically linked binaries. 172 */ 173 if (pack->ep_emul_arg != NULL) { 174 pack->ep_emul_argp = stack; 175 stack = (char *)stack + ELF_AUX_ENTRIES * sizeof (AuxInfo); 176 } 177 return (stack); 178 } 179 180 /* 181 * Check header for validity; return 0 for ok, ENOEXEC if error 182 */ 183 int 184 ELFNAME(check_header)(Elf_Ehdr *ehdr) 185 { 186 /* 187 * We need to check magic, class size, endianess, and version before 188 * we look at the rest of the Elf_Ehdr structure. These few elements 189 * are represented in a machine independent fashion. 190 */ 191 if (!IS_ELF(*ehdr) || 192 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 193 ehdr->e_ident[EI_DATA] != ELF_TARG_DATA || 194 ehdr->e_ident[EI_VERSION] != ELF_TARG_VER) 195 return (ENOEXEC); 196 197 /* Now check the machine dependent header */ 198 if (ehdr->e_machine != ELF_TARG_MACH || 199 ehdr->e_version != ELF_TARG_VER) 200 return (ENOEXEC); 201 202 /* Don't allow an insane amount of sections. */ 203 if (ehdr->e_phnum > ELF_MAX_VALID_PHDR) 204 return (ENOEXEC); 205 206 return (0); 207 } 208 209 /* 210 * Load a psection at the appropriate address 211 */ 212 void 213 ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, 214 Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags) 215 { 216 u_long msize, lsize, psize, rm, rf; 217 long diff, offset, bdiff; 218 Elf_Addr base; 219 220 /* 221 * If the user specified an address, then we load there. 222 */ 223 if (*addr != ELFDEFNNAME(NO_ADDR)) { 224 if (ph->p_align > 1) { 225 *addr = ELF_TRUNC(*addr, ph->p_align); 226 diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 227 /* page align vaddr */ 228 base = *addr + trunc_page(ph->p_vaddr) 229 - ELF_TRUNC(ph->p_vaddr, ph->p_align); 230 } else { 231 diff = 0; 232 base = *addr + trunc_page(ph->p_vaddr) - ph->p_vaddr; 233 } 234 } else { 235 *addr = ph->p_vaddr; 236 if (ph->p_align > 1) 237 *addr = ELF_TRUNC(*addr, ph->p_align); 238 base = trunc_page(ph->p_vaddr); 239 diff = ph->p_vaddr - *addr; 240 } 241 bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr); 242 243 *prot |= (ph->p_flags & PF_R) ? PROT_READ : 0; 244 *prot |= (ph->p_flags & PF_W) ? PROT_WRITE : 0; 245 *prot |= (ph->p_flags & PF_X) ? PROT_EXEC : 0; 246 247 msize = ph->p_memsz + diff; 248 offset = ph->p_offset - bdiff; 249 lsize = ph->p_filesz + bdiff; 250 psize = round_page(lsize); 251 252 /* 253 * Because the pagedvn pager can't handle zero fill of the last 254 * data page if it's not page aligned we map the last page readvn. 255 */ 256 if (ph->p_flags & PF_W) { 257 psize = trunc_page(lsize); 258 if (psize > 0) 259 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, 260 offset, *prot, flags); 261 if (psize != lsize) { 262 NEW_VMCMD2(vcset, vmcmd_map_readvn, lsize - psize, 263 base + psize, vp, offset + psize, *prot, flags); 264 } 265 } else { 266 NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, offset, 267 *prot, flags); 268 } 269 270 /* 271 * Check if we need to extend the size of the segment 272 */ 273 rm = round_page(*addr + ph->p_memsz + diff); 274 rf = round_page(*addr + ph->p_filesz + diff); 275 276 if (rm != rf) { 277 NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, 278 *prot, flags); 279 } 280 *size = msize; 281 } 282 283 /* 284 * Read from vnode into buffer at offset. 285 */ 286 int 287 ELFNAME(read_from)(struct proc *p, struct vnode *vp, u_long off, caddr_t buf, 288 int size) 289 { 290 int error; 291 size_t resid; 292 293 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE, 294 0, p->p_ucred, &resid, p)) != 0) 295 return error; 296 /* 297 * See if we got all of it 298 */ 299 if (resid != 0) 300 return (ENOEXEC); 301 return (0); 302 } 303 304 /* 305 * Load a file (interpreter/library) pointed to by path [stolen from 306 * coff_load_shlib()]. Made slightly generic so it might be used externally. 307 */ 308 int 309 ELFNAME(load_file)(struct proc *p, char *path, struct exec_package *epp, 310 struct elf_args *ap, Elf_Addr *last) 311 { 312 int error, i; 313 struct nameidata nd; 314 Elf_Ehdr eh; 315 Elf_Phdr *ph = NULL; 316 u_long phsize; 317 Elf_Addr addr; 318 struct vnode *vp; 319 Elf_Phdr *base_ph = NULL; 320 struct interp_ld_sec { 321 Elf_Addr vaddr; 322 u_long memsz; 323 } loadmap[ELF_MAX_VALID_PHDR]; 324 int nload, idx = 0; 325 Elf_Addr pos = *last; 326 int file_align; 327 int loop; 328 size_t randomizequota = ELF_RANDOMIZE_LIMIT; 329 330 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 331 if ((error = namei(&nd)) != 0) { 332 return (error); 333 } 334 vp = nd.ni_vp; 335 if (vp->v_type != VREG) { 336 error = EACCES; 337 goto bad; 338 } 339 if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0) 340 goto bad; 341 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 342 error = EACCES; 343 goto bad; 344 } 345 if ((error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) != 0) 346 goto bad1; 347 if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, 348 (caddr_t)&eh, sizeof(eh))) != 0) 349 goto bad1; 350 351 if (ELFNAME(check_header)(&eh) || eh.e_type != ET_DYN) { 352 error = ENOEXEC; 353 goto bad1; 354 } 355 356 ph = mallocarray(eh.e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK); 357 phsize = eh.e_phnum * sizeof(Elf_Phdr); 358 359 if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, (caddr_t)ph, 360 phsize)) != 0) 361 goto bad1; 362 363 for (i = 0; i < eh.e_phnum; i++) { 364 if (ph[i].p_type == PT_LOAD) { 365 if (ph[i].p_filesz > ph[i].p_memsz) 366 goto bad1; 367 loadmap[idx].vaddr = trunc_page(ph[i].p_vaddr); 368 loadmap[idx].memsz = round_page (ph[i].p_vaddr + 369 ph[i].p_memsz - loadmap[idx].vaddr); 370 file_align = ph[i].p_align; 371 idx++; 372 } 373 } 374 nload = idx; 375 376 /* 377 * If no position to load the interpreter was set by a probe 378 * function, pick the same address that a non-fixed mmap(0, ..) 379 * would (i.e. something safely out of the way). 380 */ 381 if (pos == ELFDEFNNAME(NO_ADDR)) { 382 pos = uvm_map_hint(p->p_vmspace, PROT_EXEC, 383 VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS); 384 } 385 386 pos = ELF_ROUND(pos, file_align); 387 *last = epp->ep_interp_pos = pos; 388 loop = 0; 389 for (i = 0; i < nload;/**/) { 390 vaddr_t addr; 391 struct uvm_object *uobj; 392 off_t uoff; 393 size_t size; 394 395 #ifdef this_needs_fixing 396 if (i == 0) { 397 uobj = &vp->v_uvm.u_obj; 398 /* need to fix uoff */ 399 } else { 400 #endif 401 uobj = NULL; 402 uoff = 0; 403 #ifdef this_needs_fixing 404 } 405 #endif 406 407 addr = trunc_page(pos + loadmap[i].vaddr); 408 size = round_page(addr + loadmap[i].memsz) - addr; 409 410 /* CRAP - map_findspace does not avoid daddr+BRKSIZ */ 411 if ((addr + size > (vaddr_t)p->p_vmspace->vm_daddr) && 412 (addr < (vaddr_t)p->p_vmspace->vm_daddr + BRKSIZ)) 413 addr = round_page((vaddr_t)p->p_vmspace->vm_daddr + 414 BRKSIZ); 415 416 if (uvm_map_mquery(&p->p_vmspace->vm_map, &addr, size, 417 (i == 0 ? uoff : UVM_UNKNOWN_OFFSET), 0) != 0) { 418 if (loop == 0) { 419 loop = 1; 420 i = 0; 421 *last = epp->ep_interp_pos = pos = 0; 422 continue; 423 } 424 error = ENOMEM; 425 goto bad1; 426 } 427 if (addr != pos + loadmap[i].vaddr) { 428 /* base changed. */ 429 pos = addr - trunc_page(loadmap[i].vaddr); 430 pos = ELF_ROUND(pos,file_align); 431 epp->ep_interp_pos = *last = pos; 432 i = 0; 433 continue; 434 } 435 436 i++; 437 } 438 439 /* 440 * Load all the necessary sections 441 */ 442 for (i = 0; i < eh.e_phnum; i++) { 443 Elf_Addr size = 0; 444 int prot = 0; 445 int flags; 446 447 switch (ph[i].p_type) { 448 case PT_LOAD: 449 if (base_ph == NULL) { 450 flags = VMCMD_BASE; 451 addr = *last; 452 base_ph = &ph[i]; 453 } else { 454 flags = VMCMD_RELATIVE; 455 addr = ph[i].p_vaddr - base_ph->p_vaddr; 456 } 457 ELFNAME(load_psection)(&epp->ep_vmcmds, nd.ni_vp, 458 &ph[i], &addr, &size, &prot, flags); 459 /* If entry is within this section it must be text */ 460 if (eh.e_entry >= ph[i].p_vaddr && 461 eh.e_entry < (ph[i].p_vaddr + size)) { 462 epp->ep_entry = addr + eh.e_entry - 463 ELF_TRUNC(ph[i].p_vaddr,ph[i].p_align); 464 ap->arg_interp = addr; 465 } 466 addr += size; 467 break; 468 469 case PT_DYNAMIC: 470 case PT_PHDR: 471 case PT_NOTE: 472 break; 473 474 case PT_OPENBSD_RANDOMIZE: 475 if (ph[i].p_memsz > randomizequota) { 476 error = ENOMEM; 477 goto bad1; 478 } 479 randomizequota -= ph[i].p_memsz; 480 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_randomize, 481 ph[i].p_memsz, ph[i].p_vaddr + pos, NULLVP, 0, 0); 482 break; 483 484 default: 485 break; 486 } 487 } 488 489 vn_marktext(nd.ni_vp); 490 491 bad1: 492 VOP_CLOSE(nd.ni_vp, FREAD, p->p_ucred, p); 493 bad: 494 free(ph, M_TEMP, phsize); 495 496 *last = addr; 497 vput(nd.ni_vp); 498 return (error); 499 } 500 501 /* 502 * Prepare an Elf binary's exec package 503 * 504 * First, set of the various offsets/lengths in the exec package. 505 * 506 * Then, mark the text image busy (so it can be demand paged) or error out if 507 * this is not possible. Finally, set up vmcmds for the text, data, bss, and 508 * stack segments. 509 */ 510 int 511 ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) 512 { 513 Elf_Ehdr *eh = epp->ep_hdr; 514 Elf_Phdr *ph, *pp, *base_ph = NULL; 515 Elf_Addr phdr = 0, exe_base = 0; 516 int error, i, has_phdr = 0; 517 char *interp = NULL; 518 u_long pos = 0, phsize; 519 size_t randomizequota = ELF_RANDOMIZE_LIMIT; 520 521 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 522 return (ENOEXEC); 523 524 if (ELFNAME(check_header)(eh) || 525 (eh->e_type != ET_EXEC && eh->e_type != ET_DYN)) 526 return (ENOEXEC); 527 528 /* 529 * check if vnode is in open for writing, because we want to demand- 530 * page out of it. if it is, don't do it, for various reasons. 531 */ 532 if (epp->ep_vp->v_writecount != 0) { 533 #ifdef DIAGNOSTIC 534 if (epp->ep_vp->v_flag & VTEXT) 535 panic("exec: a VTEXT vnode has writecount != 0"); 536 #endif 537 return (ETXTBSY); 538 } 539 /* 540 * Allocate space to hold all the program headers, and read them 541 * from the file 542 */ 543 ph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK); 544 phsize = eh->e_phnum * sizeof(Elf_Phdr); 545 546 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph, 547 phsize)) != 0) 548 goto bad; 549 550 epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 551 epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 552 553 for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) { 554 if (pp->p_type == PT_INTERP && !interp) { 555 if (pp->p_filesz >= MAXPATHLEN) 556 goto bad; 557 interp = pool_get(&namei_pool, PR_WAITOK); 558 if ((error = ELFNAME(read_from)(p, epp->ep_vp, 559 pp->p_offset, interp, pp->p_filesz)) != 0) { 560 goto bad; 561 } 562 } else if (pp->p_type == PT_LOAD) { 563 if (pp->p_filesz > pp->p_memsz) { 564 error = EINVAL; 565 goto bad; 566 } 567 if (base_ph == NULL) 568 base_ph = pp; 569 } else if (pp->p_type == PT_PHDR) { 570 has_phdr = 1; 571 } 572 } 573 574 if (eh->e_type == ET_DYN) { 575 /* need phdr and load sections for PIE */ 576 if (!has_phdr || base_ph == NULL) { 577 error = EINVAL; 578 goto bad; 579 } 580 /* randomize exe_base for PIE */ 581 exe_base = uvm_map_pie(base_ph->p_align); 582 } 583 584 /* 585 * OK, we want a slightly different twist of the 586 * standard emulation package for "real" elf. 587 */ 588 epp->ep_emul = &ELFNAMEEND(emul); 589 pos = ELFDEFNNAME(NO_ADDR); 590 591 /* 592 * On the same architecture, we may be emulating different systems. 593 * See which one will accept this executable. 594 * 595 * Probe functions would normally see if the interpreter (if any) 596 * exists. Emulation packages may possibly replace the interpreter in 597 * *interp with a changed path (/emul/xxx/<path>), and also 598 * set the ep_emul field in the exec package structure. 599 */ 600 error = ENOEXEC; 601 if (eh->e_ident[EI_OSABI] != ELFOSABI_OPENBSD && 602 ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "OpenBSD", 8, 4) != 0) { 603 for (i = 0; ELFNAME(probes)[i].func != NULL && error; i++) 604 error = (*ELFNAME(probes)[i].func)(p, epp, interp, &pos); 605 if (error) 606 goto bad; 607 } 608 609 /* 610 * Load all the necessary sections 611 */ 612 for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) { 613 Elf_Addr addr, size = 0; 614 int prot = 0; 615 int flags = 0; 616 617 switch (pp->p_type) { 618 case PT_LOAD: 619 if (exe_base != 0) { 620 if (pp == base_ph) { 621 flags = VMCMD_BASE; 622 addr = exe_base; 623 } else { 624 flags = VMCMD_RELATIVE; 625 addr = pp->p_vaddr - base_ph->p_vaddr; 626 } 627 } else 628 addr = ELFDEFNNAME(NO_ADDR); 629 630 /* 631 * Calculates size of text and data segments 632 * by starting at first and going to end of last. 633 * 'rwx' sections are treated as data. 634 * this is correct for BSS_PLT, but may not be 635 * for DATA_PLT, is fine for TEXT_PLT. 636 */ 637 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 638 pp, &addr, &size, &prot, flags); 639 640 /* 641 * Update exe_base in case alignment was off. 642 * For PIE, addr is relative to exe_base so 643 * adjust it (non PIE exe_base is 0 so no change). 644 */ 645 if (flags == VMCMD_BASE) 646 exe_base = addr; 647 else 648 addr += exe_base; 649 650 /* 651 * Decide whether it's text or data by looking 652 * at the protection of the section 653 */ 654 if (prot & PROT_WRITE) { 655 /* data section */ 656 if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) { 657 epp->ep_daddr = addr; 658 epp->ep_dsize = size; 659 } else { 660 if (addr < epp->ep_daddr) { 661 epp->ep_dsize = 662 epp->ep_dsize + 663 epp->ep_daddr - 664 addr; 665 epp->ep_daddr = addr; 666 } else 667 epp->ep_dsize = addr+size - 668 epp->ep_daddr; 669 } 670 } else if (prot & PROT_EXEC) { 671 /* text section */ 672 if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) { 673 epp->ep_taddr = addr; 674 epp->ep_tsize = size; 675 } else { 676 if (addr < epp->ep_taddr) { 677 epp->ep_tsize = 678 epp->ep_tsize + 679 epp->ep_taddr - 680 addr; 681 epp->ep_taddr = addr; 682 } else 683 epp->ep_tsize = addr+size - 684 epp->ep_taddr; 685 } 686 } 687 break; 688 689 case PT_SHLIB: 690 error = ENOEXEC; 691 goto bad; 692 693 case PT_INTERP: 694 /* Already did this one */ 695 case PT_DYNAMIC: 696 case PT_NOTE: 697 break; 698 699 case PT_PHDR: 700 /* Note address of program headers (in text segment) */ 701 phdr = pp->p_vaddr; 702 break; 703 704 case PT_OPENBSD_RANDOMIZE: 705 if (ph[i].p_memsz > randomizequota) { 706 error = ENOMEM; 707 goto bad; 708 } 709 randomizequota -= ph[i].p_memsz; 710 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_randomize, 711 ph[i].p_memsz, ph[i].p_vaddr + exe_base, NULLVP, 0, 0); 712 break; 713 714 default: 715 /* 716 * Not fatal, we don't need to understand everything 717 * :-) 718 */ 719 break; 720 } 721 } 722 723 phdr += exe_base; 724 725 /* 726 * Strangely some linux programs may have all load sections marked 727 * writeable, in this case, textsize is not -1, but rather 0; 728 */ 729 if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) 730 epp->ep_tsize = 0; 731 /* 732 * Another possibility is that it has all load sections marked 733 * read-only. Fake a zero-sized data segment right after the 734 * text segment. 735 */ 736 if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) { 737 epp->ep_daddr = round_page(epp->ep_taddr + epp->ep_tsize); 738 epp->ep_dsize = 0; 739 } 740 741 epp->ep_interp = interp; 742 epp->ep_entry = eh->e_entry + exe_base; 743 744 /* 745 * Check if we found a dynamically linked binary and arrange to load 746 * its interpreter when the exec file is released. 747 */ 748 if (interp || eh->e_type == ET_DYN) { 749 struct elf_args *ap; 750 751 ap = malloc(sizeof(*ap), M_TEMP, M_WAITOK); 752 753 ap->arg_phaddr = phdr; 754 ap->arg_phentsize = eh->e_phentsize; 755 ap->arg_phnum = eh->e_phnum; 756 ap->arg_entry = eh->e_entry + exe_base; 757 ap->arg_interp = exe_base; 758 759 epp->ep_emul_arg = ap; 760 epp->ep_interp_pos = pos; 761 } 762 763 free(ph, M_TEMP, phsize); 764 vn_marktext(epp->ep_vp); 765 return (exec_setup_stack(p, epp)); 766 767 bad: 768 if (interp) 769 pool_put(&namei_pool, interp); 770 free(ph, M_TEMP, phsize); 771 kill_vmcmds(&epp->ep_vmcmds); 772 if (error == 0) 773 return (ENOEXEC); 774 return (error); 775 } 776 777 /* 778 * Phase II of load. It is now safe to load the interpreter. Info collected 779 * when loading the program is available for setup of the interpreter. 780 */ 781 int 782 ELFNAME2(exec,fixup)(struct proc *p, struct exec_package *epp) 783 { 784 char *interp; 785 int error = 0; 786 struct elf_args *ap; 787 AuxInfo ai[ELF_AUX_ENTRIES], *a; 788 Elf_Addr pos = epp->ep_interp_pos; 789 790 if (epp->ep_emul_arg == NULL) { 791 return (0); 792 } 793 794 interp = epp->ep_interp; 795 ap = epp->ep_emul_arg; 796 797 if (interp && 798 (error = ELFNAME(load_file)(p, interp, epp, ap, &pos)) != 0) { 799 free(ap, M_TEMP, 0); 800 pool_put(&namei_pool, interp); 801 kill_vmcmds(&epp->ep_vmcmds); 802 return (error); 803 } 804 /* 805 * We have to do this ourselves... 806 */ 807 error = exec_process_vmcmds(p, epp); 808 809 /* 810 * Push extra arguments on the stack needed by dynamically 811 * linked binaries 812 */ 813 if (error == 0) { 814 a = ai; 815 816 a->au_id = AUX_phdr; 817 a->au_v = ap->arg_phaddr; 818 a++; 819 820 a->au_id = AUX_phent; 821 a->au_v = ap->arg_phentsize; 822 a++; 823 824 a->au_id = AUX_phnum; 825 a->au_v = ap->arg_phnum; 826 a++; 827 828 a->au_id = AUX_pagesz; 829 a->au_v = PAGE_SIZE; 830 a++; 831 832 a->au_id = AUX_base; 833 a->au_v = ap->arg_interp; 834 a++; 835 836 a->au_id = AUX_flags; 837 a->au_v = 0; 838 a++; 839 840 a->au_id = AUX_entry; 841 a->au_v = ap->arg_entry; 842 a++; 843 844 a->au_id = AUX_null; 845 a->au_v = 0; 846 a++; 847 848 error = copyout(ai, epp->ep_emul_argp, sizeof ai); 849 } 850 free(ap, M_TEMP, 0); 851 if (interp) 852 pool_put(&namei_pool, interp); 853 return (error); 854 } 855 856 /* 857 * Older ELF binaries use EI_ABIVERSION (formerly EI_BRAND) to brand 858 * executables. Newer ELF binaries use EI_OSABI instead. 859 */ 860 char * 861 ELFNAME(check_brand)(Elf_Ehdr *eh) 862 { 863 if (eh->e_ident[EI_ABIVERSION] == '\0') 864 return (NULL); 865 return (&eh->e_ident[EI_ABIVERSION]); 866 } 867 868 int 869 ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, 870 char *os_name, size_t name_size, size_t desc_size) 871 { 872 Elf_Phdr *hph, *ph; 873 Elf_Note *np = NULL; 874 size_t phsize; 875 int error; 876 877 hph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK); 878 phsize = eh->e_phnum * sizeof(Elf_Phdr); 879 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 880 (caddr_t)hph, phsize)) != 0) 881 goto out1; 882 883 for (ph = hph; ph < &hph[eh->e_phnum]; ph++) { 884 if (ph->p_type != PT_NOTE || 885 ph->p_filesz > 1024 || 886 ph->p_filesz < sizeof(Elf_Note) + name_size) 887 continue; 888 889 np = malloc(ph->p_filesz, M_TEMP, M_WAITOK); 890 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset, 891 (caddr_t)np, ph->p_filesz)) != 0) 892 goto out2; 893 894 #if 0 895 if (np->type != ELF_NOTE_TYPE_OSVERSION) { 896 free(np, M_TEMP, ph->p_filesz); 897 np = NULL; 898 continue; 899 } 900 #endif 901 902 /* Check the name and description sizes. */ 903 if (np->namesz != name_size || 904 np->descsz != desc_size) 905 goto out3; 906 907 if (memcmp((np + 1), os_name, name_size)) 908 goto out3; 909 910 /* XXX: We could check for the specific emulation here */ 911 /* All checks succeeded. */ 912 error = 0; 913 goto out2; 914 } 915 916 out3: 917 error = ENOEXEC; 918 out2: 919 free(np, M_TEMP, ph->p_filesz); 920 out1: 921 free(hph, M_TEMP, phsize); 922 return error; 923 } 924 925 struct countsegs_state { 926 int npsections; 927 }; 928 929 int ELFNAMEEND(coredump_countsegs)(struct proc *, void *, 930 struct uvm_coredump_state *); 931 932 struct writesegs_state { 933 Elf_Phdr *psections; 934 off_t secoff; 935 }; 936 937 int ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *, 938 struct uvm_coredump_state *); 939 940 int ELFNAMEEND(coredump_notes)(struct proc *, void *, size_t *); 941 int ELFNAMEEND(coredump_note)(struct proc *, void *, size_t *); 942 int ELFNAMEEND(coredump_writenote)(struct proc *, void *, Elf_Note *, 943 const char *, void *); 944 945 #define ELFROUNDSIZE 4 /* XXX Should it be sizeof(Elf_Word)? */ 946 #define elfround(x) roundup((x), ELFROUNDSIZE) 947 948 int 949 ELFNAMEEND(coredump)(struct proc *p, void *cookie) 950 { 951 #ifdef SMALL_KERNEL 952 return EPERM; 953 #else 954 Elf_Ehdr ehdr; 955 Elf_Phdr *psections = NULL; 956 struct countsegs_state cs; 957 struct writesegs_state ws; 958 off_t notestart, secstart, offset; 959 size_t notesize, psectionslen; 960 int error, i; 961 962 /* 963 * We have to make a total of 3 passes across the map: 964 * 965 * 1. Count the number of map entries (the number of 966 * PT_LOAD sections). 967 * 968 * 2. Write the P-section headers. 969 * 970 * 3. Write the P-sections. 971 */ 972 973 /* Pass 1: count the entries. */ 974 cs.npsections = 0; 975 error = uvm_coredump_walkmap(p, NULL, 976 ELFNAMEEND(coredump_countsegs), &cs); 977 if (error) 978 goto out; 979 980 /* Count the PT_NOTE section. */ 981 cs.npsections++; 982 983 /* Get the size of the notes. */ 984 error = ELFNAMEEND(coredump_notes)(p, NULL, ¬esize); 985 if (error) 986 goto out; 987 988 memset(&ehdr, 0, sizeof(ehdr)); 989 memcpy(ehdr.e_ident, ELFMAG, SELFMAG); 990 ehdr.e_ident[EI_CLASS] = ELF_TARG_CLASS; 991 ehdr.e_ident[EI_DATA] = ELF_TARG_DATA; 992 ehdr.e_ident[EI_VERSION] = EV_CURRENT; 993 /* XXX Should be the OSABI/ABI version of the executable. */ 994 ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV; 995 ehdr.e_ident[EI_ABIVERSION] = 0; 996 ehdr.e_type = ET_CORE; 997 /* XXX This should be the e_machine of the executable. */ 998 ehdr.e_machine = ELF_TARG_MACH; 999 ehdr.e_version = EV_CURRENT; 1000 ehdr.e_entry = 0; 1001 ehdr.e_phoff = sizeof(ehdr); 1002 ehdr.e_shoff = 0; 1003 ehdr.e_flags = 0; 1004 ehdr.e_ehsize = sizeof(ehdr); 1005 ehdr.e_phentsize = sizeof(Elf_Phdr); 1006 ehdr.e_phnum = cs.npsections; 1007 ehdr.e_shentsize = 0; 1008 ehdr.e_shnum = 0; 1009 ehdr.e_shstrndx = 0; 1010 1011 /* Write out the ELF header. */ 1012 error = coredump_write(cookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr)); 1013 if (error) 1014 goto out; 1015 1016 psections = mallocarray(cs.npsections, sizeof(Elf_Phdr), 1017 M_TEMP, M_WAITOK|M_ZERO); 1018 psectionslen = cs.npsections * sizeof(Elf_Phdr); 1019 1020 offset = sizeof(ehdr); 1021 notestart = offset + psectionslen; 1022 secstart = notestart + notesize; 1023 1024 /* Pass 2: now write the P-section headers. */ 1025 ws.secoff = secstart; 1026 ws.psections = psections; 1027 error = uvm_coredump_walkmap(p, cookie, 1028 ELFNAMEEND(coredump_writeseghdrs), &ws); 1029 if (error) 1030 goto out; 1031 1032 /* Write out the PT_NOTE header. */ 1033 ws.psections->p_type = PT_NOTE; 1034 ws.psections->p_offset = notestart; 1035 ws.psections->p_vaddr = 0; 1036 ws.psections->p_paddr = 0; 1037 ws.psections->p_filesz = notesize; 1038 ws.psections->p_memsz = 0; 1039 ws.psections->p_flags = PF_R; 1040 ws.psections->p_align = ELFROUNDSIZE; 1041 1042 error = coredump_write(cookie, UIO_SYSSPACE, psections, psectionslen); 1043 if (error) 1044 goto out; 1045 1046 #ifdef DIAGNOSTIC 1047 offset += psectionslen; 1048 if (offset != notestart) 1049 panic("coredump: offset %lld != notestart %lld", 1050 (long long) offset, (long long) notestart); 1051 #endif 1052 1053 /* Write out the notes. */ 1054 error = ELFNAMEEND(coredump_notes)(p, cookie, ¬esize); 1055 if (error) 1056 goto out; 1057 1058 #ifdef DIAGNOSTIC 1059 offset += notesize; 1060 if (offset != secstart) 1061 panic("coredump: offset %lld != secstart %lld", 1062 (long long) offset, (long long) secstart); 1063 #endif 1064 1065 /* Pass 3: finally, write the sections themselves. */ 1066 for (i = 0; i < cs.npsections - 1; i++) { 1067 if (psections[i].p_filesz == 0) 1068 continue; 1069 1070 #ifdef DIAGNOSTIC 1071 if (offset != psections[i].p_offset) 1072 panic("coredump: offset %lld != p_offset[%d] %lld", 1073 (long long) offset, i, 1074 (long long) psections[i].p_filesz); 1075 #endif 1076 1077 error = coredump_write(cookie, UIO_USERSPACE, 1078 (void *)(vaddr_t)psections[i].p_vaddr, 1079 psections[i].p_filesz); 1080 if (error) 1081 goto out; 1082 1083 coredump_unmap(cookie, (vaddr_t)psections[i].p_vaddr, 1084 (vaddr_t)psections[i].p_vaddr + psections[i].p_filesz); 1085 1086 #ifdef DIAGNOSTIC 1087 offset += psections[i].p_filesz; 1088 #endif 1089 } 1090 1091 out: 1092 free(psections, M_TEMP, psectionslen); 1093 return (error); 1094 #endif 1095 } 1096 1097 int 1098 ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie, 1099 struct uvm_coredump_state *us) 1100 { 1101 #ifndef SMALL_KERNEL 1102 struct countsegs_state *cs = us->cookie; 1103 1104 cs->npsections++; 1105 #endif 1106 return (0); 1107 } 1108 1109 int 1110 ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie, 1111 struct uvm_coredump_state *us) 1112 { 1113 #ifndef SMALL_KERNEL 1114 struct writesegs_state *ws = us->cookie; 1115 Elf_Phdr phdr; 1116 vsize_t size, realsize; 1117 1118 size = us->end - us->start; 1119 realsize = us->realend - us->start; 1120 1121 phdr.p_type = PT_LOAD; 1122 phdr.p_offset = ws->secoff; 1123 phdr.p_vaddr = us->start; 1124 phdr.p_paddr = 0; 1125 phdr.p_filesz = realsize; 1126 phdr.p_memsz = size; 1127 phdr.p_flags = 0; 1128 if (us->prot & PROT_READ) 1129 phdr.p_flags |= PF_R; 1130 if (us->prot & PROT_WRITE) 1131 phdr.p_flags |= PF_W; 1132 if (us->prot & PROT_EXEC) 1133 phdr.p_flags |= PF_X; 1134 phdr.p_align = PAGE_SIZE; 1135 1136 ws->secoff += phdr.p_filesz; 1137 *ws->psections++ = phdr; 1138 #endif 1139 1140 return (0); 1141 } 1142 1143 int 1144 ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep) 1145 { 1146 #ifndef SMALL_KERNEL 1147 struct ps_strings pss; 1148 struct iovec iov; 1149 struct uio uio; 1150 struct elfcore_procinfo cpi; 1151 Elf_Note nhdr; 1152 struct process *pr = p->p_p; 1153 struct proc *q; 1154 size_t size, notesize; 1155 int error; 1156 1157 size = 0; 1158 1159 /* First, write an elfcore_procinfo. */ 1160 notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) + 1161 elfround(sizeof(cpi)); 1162 if (iocookie) { 1163 memset(&cpi, 0, sizeof(cpi)); 1164 1165 cpi.cpi_version = ELFCORE_PROCINFO_VERSION; 1166 cpi.cpi_cpisize = sizeof(cpi); 1167 cpi.cpi_signo = p->p_sisig; 1168 cpi.cpi_sigcode = p->p_sicode; 1169 1170 cpi.cpi_sigpend = p->p_siglist; 1171 cpi.cpi_sigmask = p->p_sigmask; 1172 cpi.cpi_sigignore = pr->ps_sigacts->ps_sigignore; 1173 cpi.cpi_sigcatch = pr->ps_sigacts->ps_sigcatch; 1174 1175 cpi.cpi_pid = pr->ps_pid; 1176 cpi.cpi_ppid = pr->ps_pptr->ps_pid; 1177 cpi.cpi_pgrp = pr->ps_pgid; 1178 if (pr->ps_session->s_leader) 1179 cpi.cpi_sid = pr->ps_session->s_leader->ps_pid; 1180 else 1181 cpi.cpi_sid = 0; 1182 1183 cpi.cpi_ruid = p->p_ucred->cr_ruid; 1184 cpi.cpi_euid = p->p_ucred->cr_uid; 1185 cpi.cpi_svuid = p->p_ucred->cr_svuid; 1186 1187 cpi.cpi_rgid = p->p_ucred->cr_rgid; 1188 cpi.cpi_egid = p->p_ucred->cr_gid; 1189 cpi.cpi_svgid = p->p_ucred->cr_svgid; 1190 1191 (void)strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name)); 1192 1193 nhdr.namesz = sizeof("OpenBSD"); 1194 nhdr.descsz = sizeof(cpi); 1195 nhdr.type = NT_OPENBSD_PROCINFO; 1196 1197 error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, 1198 "OpenBSD", &cpi); 1199 if (error) 1200 return (error); 1201 } 1202 size += notesize; 1203 1204 /* Second, write an NT_OPENBSD_AUXV note. */ 1205 notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) + 1206 elfround(pr->ps_emul->e_arglen * sizeof(char *)); 1207 if (iocookie) { 1208 iov.iov_base = &pss; 1209 iov.iov_len = sizeof(pss); 1210 uio.uio_iov = &iov; 1211 uio.uio_iovcnt = 1; 1212 uio.uio_offset = (off_t)pr->ps_strings; 1213 uio.uio_resid = sizeof(pss); 1214 uio.uio_segflg = UIO_SYSSPACE; 1215 uio.uio_rw = UIO_READ; 1216 uio.uio_procp = NULL; 1217 1218 error = uvm_io(&p->p_vmspace->vm_map, &uio, 0); 1219 if (error) 1220 return (error); 1221 1222 if (pss.ps_envstr == NULL) 1223 return (EIO); 1224 1225 nhdr.namesz = sizeof("OpenBSD"); 1226 nhdr.descsz = pr->ps_emul->e_arglen * sizeof(char *); 1227 nhdr.type = NT_OPENBSD_AUXV; 1228 1229 error = coredump_write(iocookie, UIO_SYSSPACE, 1230 &nhdr, sizeof(nhdr)); 1231 if (error) 1232 return (error); 1233 1234 error = coredump_write(iocookie, UIO_SYSSPACE, 1235 "OpenBSD", elfround(nhdr.namesz)); 1236 if (error) 1237 return (error); 1238 1239 error = coredump_write(iocookie, UIO_USERSPACE, 1240 pss.ps_envstr + pss.ps_nenvstr + 1, nhdr.descsz); 1241 if (error) 1242 return (error); 1243 } 1244 size += notesize; 1245 1246 #ifdef PT_WCOOKIE 1247 notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) + 1248 elfround(sizeof(register_t)); 1249 if (iocookie) { 1250 register_t wcookie; 1251 1252 nhdr.namesz = sizeof("OpenBSD"); 1253 nhdr.descsz = sizeof(register_t); 1254 nhdr.type = NT_OPENBSD_WCOOKIE; 1255 1256 wcookie = process_get_wcookie(p); 1257 error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, 1258 "OpenBSD", &wcookie); 1259 if (error) 1260 return (error); 1261 } 1262 size += notesize; 1263 #endif 1264 1265 /* 1266 * Now write the register info for the thread that caused the 1267 * coredump. 1268 */ 1269 error = ELFNAMEEND(coredump_note)(p, iocookie, ¬esize); 1270 if (error) 1271 return (error); 1272 size += notesize; 1273 1274 /* 1275 * Now, for each thread, write the register info and any other 1276 * per-thread notes. Since we're dumping core, all the other 1277 * threads in the process have been stopped and the list can't 1278 * change. 1279 */ 1280 TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) { 1281 if (q == p) /* we've taken care of this thread */ 1282 continue; 1283 error = ELFNAMEEND(coredump_note)(q, iocookie, ¬esize); 1284 if (error) 1285 return (error); 1286 size += notesize; 1287 } 1288 1289 *sizep = size; 1290 #endif 1291 return (0); 1292 } 1293 1294 int 1295 ELFNAMEEND(coredump_note)(struct proc *p, void *iocookie, size_t *sizep) 1296 { 1297 #ifndef SMALL_KERNEL 1298 Elf_Note nhdr; 1299 int size, notesize, error; 1300 int namesize; 1301 char name[64+ELFROUNDSIZE]; 1302 struct reg intreg; 1303 #ifdef PT_GETFPREGS 1304 struct fpreg freg; 1305 #endif 1306 1307 size = 0; 1308 1309 snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d", 1310 "OpenBSD", p->p_pid); 1311 namesize = strlen(name) + 1; 1312 memset(name + namesize, 0, elfround(namesize) - namesize); 1313 1314 notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg)); 1315 if (iocookie) { 1316 error = process_read_regs(p, &intreg); 1317 if (error) 1318 return (error); 1319 1320 nhdr.namesz = namesize; 1321 nhdr.descsz = sizeof(intreg); 1322 nhdr.type = NT_OPENBSD_REGS; 1323 1324 error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, 1325 name, &intreg); 1326 if (error) 1327 return (error); 1328 1329 } 1330 size += notesize; 1331 1332 #ifdef PT_GETFPREGS 1333 notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg)); 1334 if (iocookie) { 1335 error = process_read_fpregs(p, &freg); 1336 if (error) 1337 return (error); 1338 1339 nhdr.namesz = namesize; 1340 nhdr.descsz = sizeof(freg); 1341 nhdr.type = NT_OPENBSD_FPREGS; 1342 1343 error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr, 1344 name, &freg); 1345 if (error) 1346 return (error); 1347 } 1348 size += notesize; 1349 #endif 1350 1351 *sizep = size; 1352 /* XXX Add hook for machdep per-LWP notes. */ 1353 #endif 1354 return (0); 1355 } 1356 1357 int 1358 ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Note *nhdr, 1359 const char *name, void *data) 1360 { 1361 #ifdef SMALL_KERNEL 1362 return EPERM; 1363 #else 1364 int error; 1365 1366 error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr)); 1367 if (error) 1368 return error; 1369 1370 error = coredump_write(cookie, UIO_SYSSPACE, name, 1371 elfround(nhdr->namesz)); 1372 if (error) 1373 return error; 1374 1375 return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->descsz); 1376 #endif 1377 } 1378