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