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