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