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