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