xref: /openbsd-src/sys/kern/exec_elf.c (revision a78264c4e9c84e56b13e967228e9e8fcf5e087ff)
1 /*	$OpenBSD: exec_elf.c,v 1.109 2015/01/20 19:43:21 kettenis 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 
89 #include <uvm/uvm_extern.h>
90 
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_emul_arg != 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) ? PROT_READ : 0;
248 	*prot |= (ph->p_flags & PF_W) ? PROT_WRITE : 0;
249 	*prot |= (ph->p_flags & PF_X) ? PROT_EXEC : 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 	ph = mallocarray(eh.e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
361 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
362 
363 	if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, (caddr_t)ph,
364 	    phsize)) != 0)
365 		goto bad1;
366 
367 	for (i = 0; i < eh.e_phnum; i++) {
368 		if (ph[i].p_type == PT_LOAD) {
369 			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, PROT_EXEC);
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 	free(ph, M_TEMP, phsize);
496 
497 	*last = addr;
498 	vput(nd.ni_vp);
499 	return (error);
500 }
501 
502 /*
503  * Prepare an Elf binary's exec package
504  *
505  * First, set of the various offsets/lengths in the exec package.
506  *
507  * Then, mark the text image busy (so it can be demand paged) or error out if
508  * this is not possible.  Finally, set up vmcmds for the text, data, bss, and
509  * stack segments.
510  */
511 int
512 ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
513 {
514 	Elf_Ehdr *eh = epp->ep_hdr;
515 	Elf_Phdr *ph, *pp, *base_ph = NULL;
516 	Elf_Addr phdr = 0, exe_base = 0;
517 	int error, i;
518 	char *interp = NULL;
519 	u_long pos = 0, phsize;
520 	size_t randomizequota = ELF_RANDOMIZE_LIMIT;
521 
522 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
523 		return (ENOEXEC);
524 
525 	if (ELFNAME(check_header)(eh) ||
526 	   (eh->e_type != ET_EXEC && eh->e_type != ET_DYN))
527 		return (ENOEXEC);
528 
529 	/*
530 	 * check if vnode is in open for writing, because we want to demand-
531 	 * page out of it.  if it is, don't do it, for various reasons.
532 	 */
533 	if (epp->ep_vp->v_writecount != 0) {
534 #ifdef DIAGNOSTIC
535 		if (epp->ep_vp->v_flag & VTEXT)
536 			panic("exec: a VTEXT vnode has writecount != 0");
537 #endif
538 		return (ETXTBSY);
539 	}
540 	/*
541 	 * Allocate space to hold all the program headers, and read them
542 	 * from the file
543 	 */
544 	ph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
545 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
546 
547 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, (caddr_t)ph,
548 	    phsize)) != 0)
549 		goto bad;
550 
551 	epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
552 	epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
553 
554 	for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) {
555 		if (pp->p_type == PT_INTERP && !interp) {
556 			if (pp->p_filesz >= MAXPATHLEN)
557 				goto bad;
558 			interp = pool_get(&namei_pool, PR_WAITOK);
559 			if ((error = ELFNAME(read_from)(p, epp->ep_vp,
560 			    pp->p_offset, interp, pp->p_filesz)) != 0) {
561 				goto bad;
562 			}
563 		} else if (pp->p_type == PT_LOAD) {
564 			if (base_ph == NULL)
565 				base_ph = pp;
566 		}
567 	}
568 
569 	if (eh->e_type == ET_DYN) {
570 		/* need load sections for PIE */
571 		if (base_ph == NULL)
572 			goto bad;
573 		/* randomize exe_base for PIE */
574 		exe_base = uvm_map_pie(base_ph->p_align);
575 	}
576 
577 	/*
578 	 * OK, we want a slightly different twist of the
579 	 * standard emulation package for "real" elf.
580 	 */
581 	epp->ep_emul = &ELFNAMEEND(emul);
582 	pos = ELFDEFNNAME(NO_ADDR);
583 
584 	/*
585 	 * On the same architecture, we may be emulating different systems.
586 	 * See which one will accept this executable.
587 	 *
588 	 * Probe functions would normally see if the interpreter (if any)
589 	 * exists. Emulation packages may possibly replace the interpreter in
590 	 * *interp with a changed path (/emul/xxx/<path>), and also
591 	 * set the ep_emul field in the exec package structure.
592 	 */
593 	error = ENOEXEC;
594 	if (eh->e_ident[EI_OSABI] != ELFOSABI_OPENBSD &&
595 	    ELFNAME(os_pt_note)(p, epp, epp->ep_hdr, "OpenBSD", 8, 4) != 0) {
596 		for (i = 0; ELFNAME(probes)[i].func != NULL && error; i++)
597 			error = (*ELFNAME(probes)[i].func)(p, epp, interp, &pos);
598 		if (error)
599 			goto bad;
600 	}
601 
602 	/*
603 	 * Load all the necessary sections
604 	 */
605 	for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) {
606 		Elf_Addr addr, size = 0;
607 		int prot = 0;
608 		int flags = 0;
609 
610 		switch (pp->p_type) {
611 		case PT_LOAD:
612 			if (exe_base != 0) {
613 				if (pp == base_ph) {
614 					flags = VMCMD_BASE;
615 					addr = exe_base;
616 				} else {
617 					flags = VMCMD_RELATIVE;
618 					addr = pp->p_vaddr - base_ph->p_vaddr;
619 				}
620 			} else
621 				addr = ELFDEFNNAME(NO_ADDR);
622 
623 			/*
624 			 * Calculates size of text and data segments
625 			 * by starting at first and going to end of last.
626 			 * 'rwx' sections are treated as data.
627 			 * this is correct for BSS_PLT, but may not be
628 			 * for DATA_PLT, is fine for TEXT_PLT.
629 			 */
630 			ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
631 			    pp, &addr, &size, &prot, flags);
632 
633 			/*
634 			 * Update exe_base in case alignment was off.
635 			 * For PIE, addr is relative to exe_base so
636 			 * adjust it (non PIE exe_base is 0 so no change).
637 			 */
638 			if (flags == VMCMD_BASE)
639 				exe_base = addr;
640 			else
641 				addr += exe_base;
642 
643 			/*
644 			 * Decide whether it's text or data by looking
645 			 * at the protection of the section
646 			 */
647 			if (prot & PROT_WRITE) {
648 				/* data section */
649 				if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) {
650 					epp->ep_daddr = addr;
651 					epp->ep_dsize = size;
652 				} else {
653 					if (addr < epp->ep_daddr) {
654 						epp->ep_dsize =
655 						    epp->ep_dsize +
656 						    epp->ep_daddr -
657 						    addr;
658 						epp->ep_daddr = addr;
659 					} else
660 						epp->ep_dsize = addr+size -
661 						    epp->ep_daddr;
662 				}
663 			} else if (prot & PROT_EXEC) {
664 				/* text section */
665 				if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR)) {
666 					epp->ep_taddr = addr;
667 					epp->ep_tsize = size;
668 				} else {
669 					if (addr < epp->ep_taddr) {
670 						epp->ep_tsize =
671 						    epp->ep_tsize +
672 						    epp->ep_taddr -
673 						    addr;
674 						epp->ep_taddr = addr;
675 					} else
676 						epp->ep_tsize = addr+size -
677 						    epp->ep_taddr;
678 				}
679 			}
680 			break;
681 
682 		case PT_SHLIB:
683 			error = ENOEXEC;
684 			goto bad;
685 
686 		case PT_INTERP:
687 			/* Already did this one */
688 		case PT_DYNAMIC:
689 		case PT_NOTE:
690 			break;
691 
692 		case PT_PHDR:
693 			/* Note address of program headers (in text segment) */
694 			phdr = pp->p_vaddr;
695 			break;
696 
697 		case PT_OPENBSD_RANDOMIZE:
698 			if (ph[i].p_memsz > randomizequota) {
699 				error = ENOMEM;
700 				goto bad;
701 			}
702 			randomizequota -= ph[i].p_memsz;
703 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_randomize,
704 			    ph[i].p_memsz, ph[i].p_vaddr + exe_base, NULLVP, 0, 0);
705 			break;
706 
707 		default:
708 			/*
709 			 * Not fatal, we don't need to understand everything
710 			 * :-)
711 			 */
712 			break;
713 		}
714 	}
715 
716 	phdr += exe_base;
717 
718 	/*
719 	 * Strangely some linux programs may have all load sections marked
720 	 * writeable, in this case, textsize is not -1, but rather 0;
721 	 */
722 	if (epp->ep_tsize == ELFDEFNNAME(NO_ADDR))
723 		epp->ep_tsize = 0;
724 	/*
725 	 * Another possibility is that it has all load sections marked
726 	 * read-only.  Fake a zero-sized data segment right after the
727 	 * text segment.
728 	 */
729 	if (epp->ep_dsize == ELFDEFNNAME(NO_ADDR)) {
730 		epp->ep_daddr = round_page(epp->ep_taddr + epp->ep_tsize);
731 		epp->ep_dsize = 0;
732 	}
733 
734 	epp->ep_interp = interp;
735 	epp->ep_entry = eh->e_entry + exe_base;
736 
737 	/*
738 	 * Check if we found a dynamically linked binary and arrange to load
739 	 * its interpreter when the exec file is released.
740 	 */
741 	if (interp || eh->e_type == ET_DYN) {
742 		struct elf_args *ap;
743 
744 		ap = malloc(sizeof(*ap), M_TEMP, M_WAITOK);
745 
746 		ap->arg_phaddr = phdr;
747 		ap->arg_phentsize = eh->e_phentsize;
748 		ap->arg_phnum = eh->e_phnum;
749 		ap->arg_entry = eh->e_entry + exe_base;
750 		ap->arg_interp = exe_base;
751 
752 		epp->ep_emul_arg = ap;
753 		epp->ep_interp_pos = pos;
754 	}
755 
756 	free(ph, M_TEMP, phsize);
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, phsize);
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 = 0;
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_emul_arg == NULL) {
782 		return (0);
783 	}
784 
785 	interp = epp->ep_interp;
786 	ap = epp->ep_emul_arg;
787 
788 	if (interp &&
789 	    (error = ELFNAME(load_file)(p, interp, epp, ap, &pos)) != 0) {
790 		free(ap, M_TEMP, 0);
791 		pool_put(&namei_pool, interp);
792 		kill_vmcmds(&epp->ep_vmcmds);
793 		return (error);
794 	}
795 	/*
796 	 * We have to do this ourselves...
797 	 */
798 	error = exec_process_vmcmds(p, epp);
799 
800 	/*
801 	 * Push extra arguments on the stack needed by dynamically
802 	 * linked binaries
803 	 */
804 	if (error == 0) {
805 		a = ai;
806 
807 		a->au_id = AUX_phdr;
808 		a->au_v = ap->arg_phaddr;
809 		a++;
810 
811 		a->au_id = AUX_phent;
812 		a->au_v = ap->arg_phentsize;
813 		a++;
814 
815 		a->au_id = AUX_phnum;
816 		a->au_v = ap->arg_phnum;
817 		a++;
818 
819 		a->au_id = AUX_pagesz;
820 		a->au_v = PAGE_SIZE;
821 		a++;
822 
823 		a->au_id = AUX_base;
824 		a->au_v = ap->arg_interp;
825 		a++;
826 
827 		a->au_id = AUX_flags;
828 		a->au_v = 0;
829 		a++;
830 
831 		a->au_id = AUX_entry;
832 		a->au_v = ap->arg_entry;
833 		a++;
834 
835 		a->au_id = AUX_null;
836 		a->au_v = 0;
837 		a++;
838 
839 		error = copyout(ai, epp->ep_emul_argp, sizeof ai);
840 	}
841 	free(ap, M_TEMP, 0);
842 	if (interp)
843 		pool_put(&namei_pool, interp);
844 	return (error);
845 }
846 
847 /*
848  * Older ELF binaries use EI_ABIVERSION (formerly EI_BRAND) to brand
849  * executables.  Newer ELF binaries use EI_OSABI instead.
850  */
851 char *
852 ELFNAME(check_brand)(Elf_Ehdr *eh)
853 {
854 	if (eh->e_ident[EI_ABIVERSION] == '\0')
855 		return (NULL);
856 	return (&eh->e_ident[EI_ABIVERSION]);
857 }
858 
859 int
860 ELFNAME(os_pt_note)(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh,
861 	char *os_name, size_t name_size, size_t desc_size)
862 {
863 	Elf_Phdr *hph, *ph;
864 	Elf_Note *np = NULL;
865 	size_t phsize;
866 	int error;
867 
868 	hph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
869 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
870 	if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
871 	    (caddr_t)hph, phsize)) != 0)
872 		goto out1;
873 
874 	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
875 		if (ph->p_type != PT_NOTE ||
876 		    ph->p_filesz > 1024 ||
877 		    ph->p_filesz < sizeof(Elf_Note) + name_size)
878 			continue;
879 
880 		np = malloc(ph->p_filesz, M_TEMP, M_WAITOK);
881 		if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
882 		    (caddr_t)np, ph->p_filesz)) != 0)
883 			goto out2;
884 
885 #if 0
886 		if (np->type != ELF_NOTE_TYPE_OSVERSION) {
887 			free(np, M_TEMP, ph->p_filesz);
888 			np = NULL;
889 			continue;
890 		}
891 #endif
892 
893 		/* Check the name and description sizes. */
894 		if (np->namesz != name_size ||
895 		    np->descsz != desc_size)
896 			goto out3;
897 
898 		if (memcmp((np + 1), os_name, name_size))
899 			goto out3;
900 
901 		/* XXX: We could check for the specific emulation here */
902 		/* All checks succeeded. */
903 		error = 0;
904 		goto out2;
905 	}
906 
907 out3:
908 	error = ENOEXEC;
909 out2:
910 	free(np, M_TEMP, ph->p_filesz);
911 out1:
912 	free(hph, M_TEMP, phsize);
913 	return error;
914 }
915 
916 struct countsegs_state {
917 	int	npsections;
918 };
919 
920 int	ELFNAMEEND(coredump_countsegs)(struct proc *, void *,
921 	    struct uvm_coredump_state *);
922 
923 struct writesegs_state {
924 	Elf_Phdr *psections;
925 	off_t	secoff;
926 };
927 
928 int	ELFNAMEEND(coredump_writeseghdrs)(struct proc *, void *,
929 	    struct uvm_coredump_state *);
930 
931 int	ELFNAMEEND(coredump_notes)(struct proc *, void *, size_t *);
932 int	ELFNAMEEND(coredump_note)(struct proc *, void *, size_t *);
933 int	ELFNAMEEND(coredump_writenote)(struct proc *, void *, Elf_Note *,
934 	    const char *, void *);
935 
936 #define	ELFROUNDSIZE	4	/* XXX Should it be sizeof(Elf_Word)? */
937 #define	elfround(x)	roundup((x), ELFROUNDSIZE)
938 
939 int
940 ELFNAMEEND(coredump)(struct proc *p, void *cookie)
941 {
942 #ifdef SMALL_KERNEL
943 	return EPERM;
944 #else
945 	Elf_Ehdr ehdr;
946 	Elf_Phdr *psections = NULL;
947 	struct countsegs_state cs;
948 	struct writesegs_state ws;
949 	off_t notestart, secstart, offset;
950 	size_t notesize, psectionslen;
951 	int error, i;
952 
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 	psections = mallocarray(cs.npsections, sizeof(Elf_Phdr),
1008 	    M_TEMP, M_WAITOK|M_ZERO);
1009 	psectionslen = cs.npsections * sizeof(Elf_Phdr);
1010 
1011 	offset = sizeof(ehdr);
1012 	notestart = offset + psectionslen;
1013 	secstart = notestart + notesize;
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, psectionslen);
1034 	if (error)
1035 		goto out;
1036 
1037 #ifdef DIAGNOSTIC
1038 	offset += psectionslen;
1039 	if (offset != notestart)
1040 		panic("coredump: offset %lld != notestart %lld",
1041 		    (long long) offset, (long long) notestart);
1042 #endif
1043 
1044 	/* Write out the notes. */
1045 	error = ELFNAMEEND(coredump_notes)(p, cookie, &notesize);
1046 	if (error)
1047 		goto out;
1048 
1049 #ifdef DIAGNOSTIC
1050 	offset += notesize;
1051 	if (offset != secstart)
1052 		panic("coredump: offset %lld != secstart %lld",
1053 		    (long long) offset, (long long) secstart);
1054 #endif
1055 
1056 	/* Pass 3: finally, write the sections themselves. */
1057 	for (i = 0; i < cs.npsections - 1; i++) {
1058 		if (psections[i].p_filesz == 0)
1059 			continue;
1060 
1061 #ifdef DIAGNOSTIC
1062 		if (offset != psections[i].p_offset)
1063 			panic("coredump: offset %lld != p_offset[%d] %lld",
1064 			    (long long) offset, i,
1065 			    (long long) psections[i].p_filesz);
1066 #endif
1067 
1068 		error = coredump_write(cookie, UIO_USERSPACE,
1069 		    (void *)(vaddr_t)psections[i].p_vaddr,
1070 		    psections[i].p_filesz);
1071 		if (error)
1072 			goto out;
1073 
1074 		coredump_unmap(cookie, (vaddr_t)psections[i].p_vaddr,
1075 		    (vaddr_t)psections[i].p_vaddr + psections[i].p_filesz);
1076 
1077 #ifdef DIAGNOSTIC
1078 		offset += psections[i].p_filesz;
1079 #endif
1080 	}
1081 
1082 out:
1083 	free(psections, M_TEMP, psectionslen);
1084 	return (error);
1085 #endif
1086 }
1087 
1088 int
1089 ELFNAMEEND(coredump_countsegs)(struct proc *p, void *iocookie,
1090     struct uvm_coredump_state *us)
1091 {
1092 #ifndef SMALL_KERNEL
1093 	struct countsegs_state *cs = us->cookie;
1094 
1095 	cs->npsections++;
1096 #endif
1097 	return (0);
1098 }
1099 
1100 int
1101 ELFNAMEEND(coredump_writeseghdrs)(struct proc *p, void *iocookie,
1102     struct uvm_coredump_state *us)
1103 {
1104 #ifndef SMALL_KERNEL
1105 	struct writesegs_state *ws = us->cookie;
1106 	Elf_Phdr phdr;
1107 	vsize_t size, realsize;
1108 
1109 	size = us->end - us->start;
1110 	realsize = us->realend - us->start;
1111 
1112 	phdr.p_type = PT_LOAD;
1113 	phdr.p_offset = ws->secoff;
1114 	phdr.p_vaddr = us->start;
1115 	phdr.p_paddr = 0;
1116 	phdr.p_filesz = realsize;
1117 	phdr.p_memsz = size;
1118 	phdr.p_flags = 0;
1119 	if (us->prot & PROT_READ)
1120 		phdr.p_flags |= PF_R;
1121 	if (us->prot & PROT_WRITE)
1122 		phdr.p_flags |= PF_W;
1123 	if (us->prot & PROT_EXEC)
1124 		phdr.p_flags |= PF_X;
1125 	phdr.p_align = PAGE_SIZE;
1126 
1127 	ws->secoff += phdr.p_filesz;
1128 	*ws->psections++ = phdr;
1129 #endif
1130 
1131 	return (0);
1132 }
1133 
1134 int
1135 ELFNAMEEND(coredump_notes)(struct proc *p, void *iocookie, size_t *sizep)
1136 {
1137 #ifndef SMALL_KERNEL
1138 	struct ps_strings pss;
1139 	struct iovec iov;
1140 	struct uio uio;
1141 	struct elfcore_procinfo cpi;
1142 	Elf_Note nhdr;
1143 	struct process *pr = p->p_p;
1144 	struct proc *q;
1145 	size_t size, notesize;
1146 	int error;
1147 
1148 	size = 0;
1149 
1150 	/* First, write an elfcore_procinfo. */
1151 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1152 	    elfround(sizeof(cpi));
1153 	if (iocookie) {
1154 		memset(&cpi, 0, sizeof(cpi));
1155 
1156 		cpi.cpi_version = ELFCORE_PROCINFO_VERSION;
1157 		cpi.cpi_cpisize = sizeof(cpi);
1158 		cpi.cpi_signo = p->p_sisig;
1159 		cpi.cpi_sigcode = p->p_sicode;
1160 
1161 		cpi.cpi_sigpend = p->p_siglist;
1162 		cpi.cpi_sigmask = p->p_sigmask;
1163 		cpi.cpi_sigignore = pr->ps_sigacts->ps_sigignore;
1164 		cpi.cpi_sigcatch = pr->ps_sigacts->ps_sigcatch;
1165 
1166 		cpi.cpi_pid = pr->ps_pid;
1167 		cpi.cpi_ppid = pr->ps_pptr->ps_pid;
1168 		cpi.cpi_pgrp = pr->ps_pgid;
1169 		if (pr->ps_session->s_leader)
1170 			cpi.cpi_sid = pr->ps_session->s_leader->ps_pid;
1171 		else
1172 			cpi.cpi_sid = 0;
1173 
1174 		cpi.cpi_ruid = p->p_ucred->cr_ruid;
1175 		cpi.cpi_euid = p->p_ucred->cr_uid;
1176 		cpi.cpi_svuid = p->p_ucred->cr_svuid;
1177 
1178 		cpi.cpi_rgid = p->p_ucred->cr_rgid;
1179 		cpi.cpi_egid = p->p_ucred->cr_gid;
1180 		cpi.cpi_svgid = p->p_ucred->cr_svgid;
1181 
1182 		(void)strlcpy(cpi.cpi_name, p->p_comm, sizeof(cpi.cpi_name));
1183 
1184 		nhdr.namesz = sizeof("OpenBSD");
1185 		nhdr.descsz = sizeof(cpi);
1186 		nhdr.type = NT_OPENBSD_PROCINFO;
1187 
1188 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
1189 		    "OpenBSD", &cpi);
1190 		if (error)
1191 			return (error);
1192 	}
1193 	size += notesize;
1194 
1195 	/* Second, write an NT_OPENBSD_AUXV note. */
1196 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1197 	    elfround(pr->ps_emul->e_arglen * sizeof(char *));
1198 	if (iocookie) {
1199 		iov.iov_base = &pss;
1200 		iov.iov_len = sizeof(pss);
1201 		uio.uio_iov = &iov;
1202 		uio.uio_iovcnt = 1;
1203 		uio.uio_offset = (off_t)pr->ps_strings;
1204 		uio.uio_resid = sizeof(pss);
1205 		uio.uio_segflg = UIO_SYSSPACE;
1206 		uio.uio_rw = UIO_READ;
1207 		uio.uio_procp = NULL;
1208 
1209 		error = uvm_io(&p->p_vmspace->vm_map, &uio, 0);
1210 		if (error)
1211 			return (error);
1212 
1213 		if (pss.ps_envstr == NULL)
1214 			return (EIO);
1215 
1216 		nhdr.namesz = sizeof("OpenBSD");
1217 		nhdr.descsz = pr->ps_emul->e_arglen * sizeof(char *);
1218 		nhdr.type = NT_OPENBSD_AUXV;
1219 
1220 		error = coredump_write(iocookie, UIO_SYSSPACE,
1221 		    &nhdr, sizeof(nhdr));
1222 		if (error)
1223 			return (error);
1224 
1225 		error = coredump_write(iocookie, UIO_SYSSPACE,
1226 		    "OpenBSD", elfround(nhdr.namesz));
1227 		if (error)
1228 			return (error);
1229 
1230 		error = coredump_write(iocookie, UIO_USERSPACE,
1231 		    pss.ps_envstr + pss.ps_nenvstr + 1, nhdr.descsz);
1232 		if (error)
1233 			return (error);
1234 	}
1235 	size += notesize;
1236 
1237 #ifdef PT_WCOOKIE
1238 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1239 	    elfround(sizeof(register_t));
1240 	if (iocookie) {
1241 		register_t wcookie;
1242 
1243 		nhdr.namesz = sizeof("OpenBSD");
1244 		nhdr.descsz = sizeof(register_t);
1245 		nhdr.type = NT_OPENBSD_WCOOKIE;
1246 
1247 		wcookie = process_get_wcookie(p);
1248 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
1249 		    "OpenBSD", &wcookie);
1250 		if (error)
1251 			return (error);
1252 	}
1253 	size += notesize;
1254 #endif
1255 
1256 	/*
1257 	 * Now write the register info for the thread that caused the
1258 	 * coredump.
1259 	 */
1260 	error = ELFNAMEEND(coredump_note)(p, iocookie, &notesize);
1261 	if (error)
1262 		return (error);
1263 	size += notesize;
1264 
1265 	/*
1266 	 * Now, for each thread, write the register info and any other
1267 	 * per-thread notes.  Since we're dumping core, all the other
1268 	 * threads in the process have been stopped and the list can't
1269 	 * change.
1270 	 */
1271 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
1272 		if (q == p)		/* we've taken care of this thread */
1273 			continue;
1274 		error = ELFNAMEEND(coredump_note)(q, iocookie, &notesize);
1275 		if (error)
1276 			return (error);
1277 		size += notesize;
1278 	}
1279 
1280 	*sizep = size;
1281 #endif
1282 	return (0);
1283 }
1284 
1285 int
1286 ELFNAMEEND(coredump_note)(struct proc *p, void *iocookie, size_t *sizep)
1287 {
1288 #ifndef SMALL_KERNEL
1289 	Elf_Note nhdr;
1290 	int size, notesize, error;
1291 	int namesize;
1292 	char name[64+ELFROUNDSIZE];
1293 	struct reg intreg;
1294 #ifdef PT_GETFPREGS
1295 	struct fpreg freg;
1296 #endif
1297 
1298 	size = 0;
1299 
1300 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
1301 	    "OpenBSD", p->p_pid);
1302 	namesize = strlen(name) + 1;
1303 	memset(name + namesize, 0, elfround(namesize) - namesize);
1304 
1305 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
1306 	if (iocookie) {
1307 		error = process_read_regs(p, &intreg);
1308 		if (error)
1309 			return (error);
1310 
1311 		nhdr.namesz = namesize;
1312 		nhdr.descsz = sizeof(intreg);
1313 		nhdr.type = NT_OPENBSD_REGS;
1314 
1315 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
1316 		    name, &intreg);
1317 		if (error)
1318 			return (error);
1319 
1320 	}
1321 	size += notesize;
1322 
1323 #ifdef PT_GETFPREGS
1324 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
1325 	if (iocookie) {
1326 		error = process_read_fpregs(p, &freg);
1327 		if (error)
1328 			return (error);
1329 
1330 		nhdr.namesz = namesize;
1331 		nhdr.descsz = sizeof(freg);
1332 		nhdr.type = NT_OPENBSD_FPREGS;
1333 
1334 		error = ELFNAMEEND(coredump_writenote)(p, iocookie, &nhdr,
1335 		    name, &freg);
1336 		if (error)
1337 			return (error);
1338 	}
1339 	size += notesize;
1340 #endif
1341 
1342 	*sizep = size;
1343 	/* XXX Add hook for machdep per-LWP notes. */
1344 #endif
1345 	return (0);
1346 }
1347 
1348 int
1349 ELFNAMEEND(coredump_writenote)(struct proc *p, void *cookie, Elf_Note *nhdr,
1350     const char *name, void *data)
1351 {
1352 #ifdef SMALL_KERNEL
1353 	return EPERM;
1354 #else
1355 	int error;
1356 
1357 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
1358 	if (error)
1359 		return error;
1360 
1361 	error = coredump_write(cookie, UIO_SYSSPACE, name,
1362 	    elfround(nhdr->namesz));
1363 	if (error)
1364 		return error;
1365 
1366 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->descsz);
1367 #endif
1368 }
1369