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