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