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