xref: /openbsd-src/sys/kern/exec_elf.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /*	$OpenBSD: exec_elf.c,v 1.178 2022/12/21 07:16:03 deraadt 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/proc.h>
72 #include <sys/malloc.h>
73 #include <sys/pool.h>
74 #include <sys/mount.h>
75 #include <sys/namei.h>
76 #include <sys/vnode.h>
77 #include <sys/core.h>
78 #include <sys/exec.h>
79 #include <sys/exec_elf.h>
80 #include <sys/fcntl.h>
81 #include <sys/ptrace.h>
82 #include <sys/signalvar.h>
83 #include <sys/pledge.h>
84 
85 #include <sys/mman.h>
86 
87 #include <uvm/uvm_extern.h>
88 
89 #include <machine/reg.h>
90 #include <machine/exec.h>
91 
92 int	elf_load_file(struct proc *, char *, struct exec_package *,
93 	    struct elf_args *);
94 int	elf_check_header(Elf_Ehdr *);
95 int	elf_read_from(struct proc *, struct vnode *, u_long, void *, int);
96 void	elf_load_psection(struct exec_vmcmd_set *, struct vnode *,
97 	    Elf_Phdr *, Elf_Addr *, Elf_Addr *, int *, int);
98 int	elf_os_pt_note_name(Elf_Note *);
99 int	elf_os_pt_note(struct proc *, struct exec_package *, Elf_Ehdr *, int *);
100 
101 /* round up and down to page boundaries. */
102 #define ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
103 #define ELF_TRUNC(a, b)		((a) & ~((b) - 1))
104 
105 /*
106  * We limit the number of program headers to 32, this should
107  * be a reasonable limit for ELF, the most we have seen so far is 12
108  */
109 #define ELF_MAX_VALID_PHDR 32
110 
111 #define ELF_NOTE_NAME_OPENBSD	0x01
112 
113 struct elf_note_name {
114 	char *name;
115 	int id;
116 } elf_note_names[] = {
117 	{ "OpenBSD",	ELF_NOTE_NAME_OPENBSD },
118 };
119 
120 #define	ELFROUNDSIZE	sizeof(Elf_Word)
121 #define	elfround(x)	roundup((x), ELFROUNDSIZE)
122 
123 
124 /*
125  * Check header for validity; return 0 for ok, ENOEXEC if error
126  */
127 int
128 elf_check_header(Elf_Ehdr *ehdr)
129 {
130 	/*
131 	 * We need to check magic, class size, endianness, and version before
132 	 * we look at the rest of the Elf_Ehdr structure. These few elements
133 	 * are represented in a machine independent fashion.
134 	 */
135 	if (!IS_ELF(*ehdr) ||
136 	    ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
137 	    ehdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
138 	    ehdr->e_ident[EI_VERSION] != ELF_TARG_VER)
139 		return (ENOEXEC);
140 
141 	/* Now check the machine dependent header */
142 	if (ehdr->e_machine != ELF_TARG_MACH ||
143 	    ehdr->e_version != ELF_TARG_VER)
144 		return (ENOEXEC);
145 
146 	/* Don't allow an insane amount of sections. */
147 	if (ehdr->e_phnum > ELF_MAX_VALID_PHDR)
148 		return (ENOEXEC);
149 
150 	return (0);
151 }
152 
153 /*
154  * Load a psection at the appropriate address
155  */
156 void
157 elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
158     Elf_Phdr *ph, Elf_Addr *addr, Elf_Addr *size, int *prot, int flags)
159 {
160 	u_long msize, lsize, psize, rm, rf;
161 	long diff, offset, bdiff;
162 	Elf_Addr base;
163 
164 	/*
165 	 * If the user specified an address, then we load there.
166 	 */
167 	if (*addr != ELF_NO_ADDR) {
168 		if (ph->p_align > 1) {
169 			*addr = ELF_TRUNC(*addr, ph->p_align);
170 			diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
171 			/* page align vaddr */
172 			base = *addr + trunc_page(ph->p_vaddr)
173 			    - ELF_TRUNC(ph->p_vaddr, ph->p_align);
174 		} else {
175 			diff = 0;
176 			base = *addr + trunc_page(ph->p_vaddr) - ph->p_vaddr;
177 		}
178 	} else {
179 		*addr = ph->p_vaddr;
180 		if (ph->p_align > 1)
181 			*addr = ELF_TRUNC(*addr, ph->p_align);
182 		base = trunc_page(ph->p_vaddr);
183 		diff = ph->p_vaddr - *addr;
184 	}
185 	bdiff = ph->p_vaddr - trunc_page(ph->p_vaddr);
186 
187 	/*
188 	 * Enforce W^X and map W|X segments without X permission
189 	 * initially.  The dynamic linker will make these read-only
190 	 * and add back X permission after relocation processing.
191 	 * Static executables with W|X segments will probably crash.
192 	 */
193 	*prot |= (ph->p_flags & PF_R) ? PROT_READ : 0;
194 	*prot |= (ph->p_flags & PF_W) ? PROT_WRITE : 0;
195 	if ((ph->p_flags & PF_W) == 0)
196 		*prot |= (ph->p_flags & PF_X) ? PROT_EXEC : 0;
197 
198 	/*
199 	 * Apply immutability as much as possible, but not text/rodata
200 	 * segments of textrel binaries, or RELRO or PT_OPENBSD_MUTABLE
201 	 * sections, or LOADS marked PF_OPENBSD_MUTABLE, or LOADS which
202 	 * violate W^X.
203 	 * Userland (meaning crt0 or ld.so) will repair those regions.
204 	 */
205 	if ((ph->p_flags & (PF_X | PF_W)) != (PF_X | PF_W) &&
206 	    ((ph->p_flags & PF_OPENBSD_MUTABLE) == 0))
207 		flags |= VMCMD_IMMUTABLE;
208 	if ((flags & VMCMD_TEXTREL) && (ph->p_flags & PF_W) == 0)
209 		flags &= ~VMCMD_IMMUTABLE;
210 
211 	msize = ph->p_memsz + diff;
212 	offset = ph->p_offset - bdiff;
213 	lsize = ph->p_filesz + bdiff;
214 	psize = round_page(lsize);
215 
216 	/*
217 	 * Because the pagedvn pager can't handle zero fill of the last
218 	 * data page if it's not page aligned we map the last page readvn.
219 	 */
220 	if (ph->p_flags & PF_W) {
221 		psize = trunc_page(lsize);
222 		if (psize > 0)
223 			NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp,
224 			    offset, *prot, flags);
225 		if (psize != lsize) {
226 			NEW_VMCMD2(vcset, vmcmd_map_readvn, lsize - psize,
227 			    base + psize, vp, offset + psize, *prot, flags);
228 		}
229 	} else {
230 		NEW_VMCMD2(vcset, vmcmd_map_pagedvn, psize, base, vp, offset,
231 		    *prot, flags);
232 	}
233 
234 	/*
235 	 * Check if we need to extend the size of the segment
236 	 */
237 	rm = round_page(*addr + ph->p_memsz + diff);
238 	rf = round_page(*addr + ph->p_filesz + diff);
239 
240 	if (rm != rf) {
241 		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0,
242 		    *prot, flags);
243 	}
244 	*size = msize;
245 }
246 
247 /*
248  * Read from vnode into buffer at offset.
249  */
250 int
251 elf_read_from(struct proc *p, struct vnode *vp, u_long off, void *buf,
252     int size)
253 {
254 	int error;
255 	size_t resid;
256 
257 	if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
258 	    0, p->p_ucred, &resid, p)) != 0)
259 		return error;
260 	/*
261 	 * See if we got all of it
262 	 */
263 	if (resid != 0)
264 		return (ENOEXEC);
265 	return (0);
266 }
267 
268 /*
269  * Load a file (interpreter/library) pointed to by path [stolen from
270  * coff_load_shlib()]. Made slightly generic so it might be used externally.
271  */
272 int
273 elf_load_file(struct proc *p, char *path, struct exec_package *epp,
274     struct elf_args *ap)
275 {
276 	int error, i;
277 	struct nameidata nd;
278 	Elf_Ehdr eh;
279 	Elf_Phdr *ph = NULL;
280 	u_long phsize = 0;
281 	Elf_Addr addr;
282 	struct vnode *vp;
283 	Elf_Phdr *base_ph = NULL;
284 	struct interp_ld_sec {
285 		Elf_Addr vaddr;
286 		u_long memsz;
287 	} loadmap[ELF_MAX_VALID_PHDR];
288 	int nload, idx = 0;
289 	Elf_Addr pos;
290 	int file_align;
291 	int loop;
292 	size_t randomizequota = ELF_RANDOMIZE_LIMIT;
293 
294 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
295 	nd.ni_pledge = PLEDGE_RPATH;
296 	nd.ni_unveil = UNVEIL_READ;
297 	if ((error = namei(&nd)) != 0) {
298 		return (error);
299 	}
300 	vp = nd.ni_vp;
301 	if (vp->v_type != VREG) {
302 		error = EACCES;
303 		goto bad;
304 	}
305 	if ((error = VOP_GETATTR(vp, epp->ep_vap, p->p_ucred, p)) != 0)
306 		goto bad;
307 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
308 		error = EACCES;
309 		goto bad;
310 	}
311 	if ((error = VOP_ACCESS(vp, VREAD, p->p_ucred, p)) != 0)
312 		goto bad1;
313 	if ((error = elf_read_from(p, nd.ni_vp, 0, &eh, sizeof(eh))) != 0)
314 		goto bad1;
315 
316 	if (elf_check_header(&eh) || eh.e_type != ET_DYN) {
317 		error = ENOEXEC;
318 		goto bad1;
319 	}
320 
321 	ph = mallocarray(eh.e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
322 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
323 
324 	if ((error = elf_read_from(p, nd.ni_vp, eh.e_phoff, ph, phsize)) != 0)
325 		goto bad1;
326 
327 	for (i = 0; i < eh.e_phnum; i++) {
328 		if (ph[i].p_type == PT_LOAD) {
329 			if (ph[i].p_filesz > ph[i].p_memsz ||
330 			    ph[i].p_memsz == 0) {
331 				error = EINVAL;
332 				goto bad1;
333 			}
334 			loadmap[idx].vaddr = trunc_page(ph[i].p_vaddr);
335 			loadmap[idx].memsz = round_page (ph[i].p_vaddr +
336 			    ph[i].p_memsz - loadmap[idx].vaddr);
337 			file_align = ph[i].p_align;
338 			idx++;
339 		}
340 	}
341 	nload = idx;
342 
343 	/*
344 	 * Load the interpreter where a non-fixed mmap(NULL, ...)
345 	 * would (i.e. something safely out of the way).
346 	 */
347 	pos = uvm_map_hint(p->p_vmspace, PROT_EXEC, VM_MIN_ADDRESS,
348 	    VM_MAXUSER_ADDRESS);
349 	pos = ELF_ROUND(pos, file_align);
350 
351 	loop = 0;
352 	for (i = 0; i < nload;/**/) {
353 		vaddr_t	addr;
354 		struct	uvm_object *uobj;
355 		off_t	uoff;
356 		size_t	size;
357 
358 #ifdef this_needs_fixing
359 		if (i == 0) {
360 			uobj = &vp->v_uvm.u_obj;
361 			/* need to fix uoff */
362 		} else {
363 #endif
364 			uobj = NULL;
365 			uoff = 0;
366 #ifdef this_needs_fixing
367 		}
368 #endif
369 
370 		addr = trunc_page(pos + loadmap[i].vaddr);
371 		size =  round_page(addr + loadmap[i].memsz) - addr;
372 
373 		/* CRAP - map_findspace does not avoid daddr+BRKSIZ */
374 		if ((addr + size > (vaddr_t)p->p_vmspace->vm_daddr) &&
375 		    (addr < (vaddr_t)p->p_vmspace->vm_daddr + BRKSIZ))
376 			addr = round_page((vaddr_t)p->p_vmspace->vm_daddr +
377 			    BRKSIZ);
378 
379 		if (uvm_map_mquery(&p->p_vmspace->vm_map, &addr, size,
380 		    (i == 0 ? uoff : UVM_UNKNOWN_OFFSET), 0) != 0) {
381 			if (loop == 0) {
382 				loop = 1;
383 				i = 0;
384 				pos = 0;
385 				continue;
386 			}
387 			error = ENOMEM;
388 			goto bad1;
389 		}
390 		if (addr != pos + loadmap[i].vaddr) {
391 			/* base changed. */
392 			pos = addr - trunc_page(loadmap[i].vaddr);
393 			pos = ELF_ROUND(pos,file_align);
394 			i = 0;
395 			continue;
396 		}
397 
398 		i++;
399 	}
400 
401 	/*
402 	 * Load all the necessary sections
403 	 */
404 	for (i = 0; i < eh.e_phnum; i++) {
405 		Elf_Addr size = 0;
406 		int prot = 0;
407 		int flags;
408 
409 		switch (ph[i].p_type) {
410 		case PT_LOAD:
411 			if (base_ph == NULL) {
412 				flags = VMCMD_BASE;
413 				addr = pos;
414 				base_ph = &ph[i];
415 			} else {
416 				flags = VMCMD_RELATIVE;
417 				addr = ph[i].p_vaddr - base_ph->p_vaddr;
418 			}
419 			elf_load_psection(&epp->ep_vmcmds, nd.ni_vp,
420 			    &ph[i], &addr, &size, &prot, flags | VMCMD_SYSCALL);
421 			/* If entry is within this section it must be text */
422 			if (eh.e_entry >= ph[i].p_vaddr &&
423 			    eh.e_entry < (ph[i].p_vaddr + size)) {
424  				epp->ep_entry = addr + eh.e_entry -
425 				    ELF_TRUNC(ph[i].p_vaddr,ph[i].p_align);
426 				if (flags == VMCMD_RELATIVE)
427 					epp->ep_entry += pos;
428 				ap->arg_interp = pos;
429 			}
430 			addr += size;
431 			break;
432 
433 		case PT_PHDR:
434 		case PT_NOTE:
435 			break;
436 
437 		case PT_OPENBSD_RANDOMIZE:
438 			if (ph[i].p_memsz > randomizequota) {
439 				error = ENOMEM;
440 				goto bad1;
441 			}
442 			randomizequota -= ph[i].p_memsz;
443 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_randomize,
444 			    ph[i].p_memsz, ph[i].p_vaddr + pos, NULLVP, 0, 0);
445 			break;
446 
447 		case PT_DYNAMIC:
448 #if defined (__mips__)
449 			/* DT_DEBUG is not ready on mips */
450 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_mutable,
451 			    ph[i].p_memsz, ph[i].p_vaddr + pos, NULLVP, 0, 0);
452 #endif
453 			break;
454 		case PT_GNU_RELRO:
455 		case PT_OPENBSD_MUTABLE:
456 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_mutable,
457 			    ph[i].p_memsz, ph[i].p_vaddr + pos, NULLVP, 0, 0);
458 			break;
459 
460 		default:
461 			break;
462 		}
463 	}
464 
465 	vn_marktext(nd.ni_vp);
466 
467 bad1:
468 	VOP_CLOSE(nd.ni_vp, FREAD, p->p_ucred, p);
469 bad:
470 	free(ph, M_TEMP, phsize);
471 
472 	vput(nd.ni_vp);
473 	return (error);
474 }
475 
476 /*
477  * Prepare an Elf binary's exec package
478  *
479  * First, set of the various offsets/lengths in the exec package.
480  *
481  * Then, mark the text image busy (so it can be demand paged) or error out if
482  * this is not possible.  Finally, set up vmcmds for the text, data, bss, and
483  * stack segments.
484  */
485 int
486 exec_elf_makecmds(struct proc *p, struct exec_package *epp)
487 {
488 	Elf_Ehdr *eh = epp->ep_hdr;
489 	Elf_Phdr *ph, *pp, *base_ph = NULL;
490 	Elf_Addr phdr = 0, exe_base = 0;
491 	int error, i, has_phdr = 0, names = 0, textrel = 0;
492 	char *interp = NULL;
493 	u_long phsize;
494 	size_t randomizequota = ELF_RANDOMIZE_LIMIT;
495 
496 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
497 		return (ENOEXEC);
498 
499 	if (elf_check_header(eh) ||
500 	   (eh->e_type != ET_EXEC && eh->e_type != ET_DYN))
501 		return (ENOEXEC);
502 
503 	/*
504 	 * check if vnode is in open for writing, because we want to demand-
505 	 * page out of it.  if it is, don't do it, for various reasons.
506 	 */
507 	if (epp->ep_vp->v_writecount != 0) {
508 #ifdef DIAGNOSTIC
509 		if (epp->ep_vp->v_flag & VTEXT)
510 			panic("exec: a VTEXT vnode has writecount != 0");
511 #endif
512 		return (ETXTBSY);
513 	}
514 	/*
515 	 * Allocate space to hold all the program headers, and read them
516 	 * from the file
517 	 */
518 	ph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
519 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
520 
521 	if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff, ph,
522 	    phsize)) != 0)
523 		goto bad;
524 
525 	epp->ep_tsize = ELF_NO_ADDR;
526 	epp->ep_dsize = ELF_NO_ADDR;
527 
528 	for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) {
529 		if (pp->p_type == PT_INTERP && !interp) {
530 			if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN)
531 				goto bad;
532 			interp = pool_get(&namei_pool, PR_WAITOK);
533 			if ((error = elf_read_from(p, epp->ep_vp,
534 			    pp->p_offset, interp, pp->p_filesz)) != 0) {
535 				goto bad;
536 			}
537 			if (interp[pp->p_filesz - 1] != '\0')
538 				goto bad;
539 		} else if (pp->p_type == PT_LOAD) {
540 			if (pp->p_filesz > pp->p_memsz ||
541 			    pp->p_memsz == 0) {
542 				error = EINVAL;
543 				goto bad;
544 			}
545 			if (base_ph == NULL)
546 				base_ph = pp;
547 		} else if (pp->p_type == PT_PHDR) {
548 			has_phdr = 1;
549 		}
550 	}
551 
552 	/*
553 	 * Verify this is an OpenBSD executable.  If it's marked that way
554 	 * via a PT_NOTE then also check for a PT_OPENBSD_WXNEEDED segment.
555 	 */
556 	if ((error = elf_os_pt_note(p, epp, epp->ep_hdr, &names)) != 0)
557 		goto bad;
558 	if (eh->e_ident[EI_OSABI] == ELFOSABI_OPENBSD)
559 		names |= ELF_NOTE_NAME_OPENBSD;
560 
561 	if (eh->e_type == ET_DYN) {
562 		/* need phdr and load sections for PIE */
563 		if (!has_phdr || base_ph == NULL) {
564 			error = EINVAL;
565 			goto bad;
566 		}
567 		/* randomize exe_base for PIE */
568 		exe_base = uvm_map_pie(base_ph->p_align);
569 
570 		/*
571 		 * Check if DYNAMIC contains DT_TEXTREL
572 		 */
573 		for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) {
574 			Elf_Dyn *dt;
575 			int j;
576 
577 			switch (pp->p_type) {
578 			case PT_DYNAMIC:
579 				if (pp->p_filesz > 64*1024)
580 					break;
581 				dt = malloc(pp->p_filesz, M_TEMP, M_WAITOK);
582 				error = vn_rdwr(UIO_READ, epp->ep_vp,
583 				    (caddr_t)dt, pp->p_filesz, pp->p_offset,
584 				    UIO_SYSSPACE, IO_UNIT, p->p_ucred, NULL, p);
585 				if (error) {
586 					free(dt, M_TEMP, pp->p_filesz);
587 					break;
588 				}
589 				for (j = 0; j < pp->p_filesz / sizeof(*dt); j++) {
590 					if (dt[j].d_tag == DT_TEXTREL) {
591 						textrel = VMCMD_TEXTREL;
592 						break;
593 					}
594 				}
595 				free(dt, M_TEMP, pp->p_filesz);
596 				break;
597 			default:
598 				break;
599 			}
600 		}
601 	}
602 
603 	/*
604 	 * Load all the necessary sections
605 	 */
606 	for (i = 0, pp = ph; i < eh->e_phnum; i++, pp++) {
607 		Elf_Addr addr, size = 0;
608 		int prot = 0, syscall = 0;
609 		int flags = 0;
610 
611 		switch (pp->p_type) {
612 		case PT_LOAD:
613 			if (exe_base != 0) {
614 				if (pp == base_ph) {
615 					flags = VMCMD_BASE;
616 					addr = exe_base;
617 				} else {
618 					flags = VMCMD_RELATIVE;
619 					addr = pp->p_vaddr - base_ph->p_vaddr;
620 				}
621 			} else
622 				addr = ELF_NO_ADDR;
623 
624 			/*
625 			 * Permit system calls in main-text static binaries.
626 			 * Also block the ld.so syscall-grant
627 			 */
628 			if (interp == NULL) {
629 				syscall = VMCMD_SYSCALL;
630 				p->p_vmspace->vm_map.flags |= VM_MAP_SYSCALL_ONCE;
631 			}
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 			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
641 			    pp, &addr, &size, &prot, flags | textrel | syscall);
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 & PROT_WRITE) {
658 				/* data section */
659 				if (epp->ep_dsize == ELF_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 & PROT_EXEC) {
674 				/* text section */
675 				if (epp->ep_tsize == ELF_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_NOTE:
699 			break;
700 
701 		case PT_PHDR:
702 			/* Note address of program headers (in text segment) */
703 			phdr = pp->p_vaddr;
704 			break;
705 
706 		case PT_OPENBSD_RANDOMIZE:
707 			if (ph[i].p_memsz > randomizequota) {
708 				error = ENOMEM;
709 				goto bad;
710 			}
711 			randomizequota -= ph[i].p_memsz;
712 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_randomize,
713 			    ph[i].p_memsz, ph[i].p_vaddr + exe_base, NULLVP, 0, 0);
714 			break;
715 
716 		case PT_DYNAMIC:
717 #if defined (__mips__)
718 			/* DT_DEBUG is not ready on mips */
719 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_mutable,
720 			    ph[i].p_memsz, ph[i].p_vaddr + exe_base, NULLVP, 0, 0);
721 #endif
722 			break;
723 		case PT_GNU_RELRO:
724 		case PT_OPENBSD_MUTABLE:
725 			NEW_VMCMD(&epp->ep_vmcmds, vmcmd_mutable,
726 			    ph[i].p_memsz, ph[i].p_vaddr + exe_base, NULLVP, 0, 0);
727 			break;
728 
729 		default:
730 			/*
731 			 * Not fatal, we don't need to understand everything
732 			 * :-)
733 			 */
734 			break;
735 		}
736 	}
737 
738 	phdr += exe_base;
739 
740 	/*
741 	 * Strangely some linux programs may have all load sections marked
742 	 * writeable, in this case, textsize is not -1, but rather 0;
743 	 */
744 	if (epp->ep_tsize == ELF_NO_ADDR)
745 		epp->ep_tsize = 0;
746 	/*
747 	 * Another possibility is that it has all load sections marked
748 	 * read-only.  Fake a zero-sized data segment right after the
749 	 * text segment.
750 	 */
751 	if (epp->ep_dsize == ELF_NO_ADDR) {
752 		epp->ep_daddr = round_page(epp->ep_taddr + epp->ep_tsize);
753 		epp->ep_dsize = 0;
754 	}
755 
756 	epp->ep_interp = interp;
757 	epp->ep_entry = eh->e_entry + exe_base;
758 
759 	/*
760 	 * Check if we found a dynamically linked binary and arrange to load
761 	 * its interpreter when the exec file is released.
762 	 */
763 	if (interp || eh->e_type == ET_DYN) {
764 		struct elf_args *ap;
765 
766 		ap = malloc(sizeof(*ap), M_TEMP, M_WAITOK);
767 
768 		ap->arg_phaddr = phdr;
769 		ap->arg_phentsize = eh->e_phentsize;
770 		ap->arg_phnum = eh->e_phnum;
771 		ap->arg_entry = eh->e_entry + exe_base;
772 		ap->arg_interp = exe_base;
773 
774 		epp->ep_args = ap;
775 	}
776 
777 	free(ph, M_TEMP, phsize);
778 	vn_marktext(epp->ep_vp);
779 	return (exec_setup_stack(p, epp));
780 
781 bad:
782 	if (interp)
783 		pool_put(&namei_pool, interp);
784 	free(ph, M_TEMP, phsize);
785 	kill_vmcmds(&epp->ep_vmcmds);
786 	if (error == 0)
787 		return (ENOEXEC);
788 	return (error);
789 }
790 
791 /*
792  * Phase II of load. It is now safe to load the interpreter. Info collected
793  * when loading the program is available for setup of the interpreter.
794  */
795 int
796 exec_elf_fixup(struct proc *p, struct exec_package *epp)
797 {
798 	char	*interp;
799 	int	error = 0;
800 	struct	elf_args *ap;
801 	AuxInfo ai[ELF_AUX_ENTRIES], *a;
802 
803 	ap = epp->ep_args;
804 	if (ap == NULL) {
805 		return (0);
806 	}
807 
808 	interp = epp->ep_interp;
809 
810 	if (interp &&
811 	    (error = elf_load_file(p, interp, epp, ap)) != 0) {
812 		uprintf("execve: cannot load %s\n", interp);
813 		free(ap, M_TEMP, sizeof *ap);
814 		pool_put(&namei_pool, interp);
815 		kill_vmcmds(&epp->ep_vmcmds);
816 		return (error);
817 	}
818 	/*
819 	 * We have to do this ourselves...
820 	 */
821 	error = exec_process_vmcmds(p, epp);
822 
823 	/*
824 	 * Push extra arguments on the stack needed by dynamically
825 	 * linked binaries
826 	 */
827 	if (error == 0) {
828 		memset(&ai, 0, sizeof ai);
829 		a = ai;
830 
831 		a->au_id = AUX_phdr;
832 		a->au_v = ap->arg_phaddr;
833 		a++;
834 
835 		a->au_id = AUX_phent;
836 		a->au_v = ap->arg_phentsize;
837 		a++;
838 
839 		a->au_id = AUX_phnum;
840 		a->au_v = ap->arg_phnum;
841 		a++;
842 
843 		a->au_id = AUX_pagesz;
844 		a->au_v = PAGE_SIZE;
845 		a++;
846 
847 		a->au_id = AUX_base;
848 		a->au_v = ap->arg_interp;
849 		a++;
850 
851 		a->au_id = AUX_flags;
852 		a->au_v = 0;
853 		a++;
854 
855 		a->au_id = AUX_entry;
856 		a->au_v = ap->arg_entry;
857 		a++;
858 
859 		a->au_id = AUX_openbsd_timekeep;
860 		a->au_v = p->p_p->ps_timekeep;
861 		a++;
862 
863 		a->au_id = AUX_null;
864 		a->au_v = 0;
865 		a++;
866 
867 		error = copyout(ai, epp->ep_auxinfo, sizeof ai);
868 	}
869 	free(ap, M_TEMP, sizeof *ap);
870 	if (interp)
871 		pool_put(&namei_pool, interp);
872 	return (error);
873 }
874 
875 int
876 elf_os_pt_note_name(Elf_Note *np)
877 {
878 	int i, j;
879 
880 	for (i = 0; i < nitems(elf_note_names); i++) {
881 		size_t namlen = strlen(elf_note_names[i].name);
882 		if (np->namesz < namlen)
883 			continue;
884 		/* verify name padding (after the NUL) is NUL */
885 		for (j = namlen + 1; j < elfround(np->namesz); j++)
886 			if (((char *)(np + 1))[j] != '\0')
887 				continue;
888 		/* verify desc padding is NUL */
889 		for (j = np->descsz; j < elfround(np->descsz); j++)
890 			if (((char *)(np + 1))[j] != '\0')
891 				continue;
892 		if (strcmp((char *)(np + 1), elf_note_names[i].name) == 0)
893 			return elf_note_names[i].id;
894 	}
895 	return (0);
896 }
897 
898 int
899 elf_os_pt_note(struct proc *p, struct exec_package *epp, Elf_Ehdr *eh, int *namesp)
900 {
901 	Elf_Phdr *hph, *ph;
902 	Elf_Note *np = NULL;
903 	size_t phsize, offset, pfilesz = 0, total;
904 	int error, names = 0;
905 
906 	hph = mallocarray(eh->e_phnum, sizeof(Elf_Phdr), M_TEMP, M_WAITOK);
907 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
908 	if ((error = elf_read_from(p, epp->ep_vp, eh->e_phoff,
909 	    hph, phsize)) != 0)
910 		goto out1;
911 
912 	for (ph = hph;  ph < &hph[eh->e_phnum]; ph++) {
913 		if (ph->p_type == PT_OPENBSD_WXNEEDED) {
914 			epp->ep_flags |= EXEC_WXNEEDED;
915 			continue;
916 		}
917 
918 		if (ph->p_type != PT_NOTE || ph->p_filesz > 1024)
919 			continue;
920 
921 		if (np && ph->p_filesz != pfilesz) {
922 			free(np, M_TEMP, pfilesz);
923 			np = NULL;
924 		}
925 		if (!np)
926 			np = malloc(ph->p_filesz, M_TEMP, M_WAITOK);
927 		pfilesz = ph->p_filesz;
928 		if ((error = elf_read_from(p, epp->ep_vp, ph->p_offset,
929 		    np, ph->p_filesz)) != 0)
930 			goto out2;
931 
932 		for (offset = 0; offset < ph->p_filesz; offset += total) {
933 			Elf_Note *np2 = (Elf_Note *)((char *)np + offset);
934 
935 			if (offset + sizeof(Elf_Note) > ph->p_filesz)
936 				break;
937 			total = sizeof(Elf_Note) + elfround(np2->namesz) +
938 			    elfround(np2->descsz);
939 			if (offset + total > ph->p_filesz)
940 				break;
941 			names |= elf_os_pt_note_name(np2);
942 		}
943 	}
944 
945 out2:
946 	free(np, M_TEMP, pfilesz);
947 out1:
948 	free(hph, M_TEMP, phsize);
949 	*namesp = names;
950 	return ((names & ELF_NOTE_NAME_OPENBSD) ? 0 : ENOEXEC);
951 }
952 
953 /*
954  * Start of routines related to dumping core
955  */
956 
957 #ifdef SMALL_KERNEL
958 int
959 coredump_elf(struct proc *p, void *cookie)
960 {
961 	return EPERM;
962 }
963 #else /* !SMALL_KERNEL */
964 
965 struct writesegs_state {
966 	off_t	notestart;
967 	off_t	secstart;
968 	off_t	secoff;
969 	struct	proc *p;
970 	void	*iocookie;
971 	Elf_Phdr *psections;
972 	size_t	psectionslen;
973 	size_t	notesize;
974 	int	npsections;
975 };
976 
977 uvm_coredump_setup_cb	coredump_setup_elf;
978 uvm_coredump_walk_cb	coredump_walk_elf;
979 
980 int	coredump_notes_elf(struct proc *, void *, size_t *);
981 int	coredump_note_elf(struct proc *, void *, size_t *);
982 int	coredump_writenote_elf(struct proc *, void *, Elf_Note *,
983 	    const char *, void *);
984 
985 int
986 coredump_elf(struct proc *p, void *cookie)
987 {
988 #ifdef DIAGNOSTIC
989 	off_t offset;
990 #endif
991 	struct writesegs_state ws;
992 	size_t notesize;
993 	int error, i;
994 
995 	ws.p = p;
996 	ws.iocookie = cookie;
997 	ws.psections = NULL;
998 
999 	/*
1000 	 * Walk the map to get all the segment offsets and lengths,
1001 	 * write out the ELF header.
1002 	 */
1003 	error = uvm_coredump_walkmap(p, coredump_setup_elf,
1004 	    coredump_walk_elf, &ws);
1005 	if (error)
1006 		goto out;
1007 
1008 	error = coredump_write(cookie, UIO_SYSSPACE, ws.psections,
1009 	    ws.psectionslen);
1010 	if (error)
1011 		goto out;
1012 
1013 	/* Write out the notes. */
1014 	error = coredump_notes_elf(p, cookie, &notesize);
1015 	if (error)
1016 		goto out;
1017 
1018 #ifdef DIAGNOSTIC
1019 	if (notesize != ws.notesize)
1020 		panic("coredump: notesize changed: %zu != %zu",
1021 		    ws.notesize, notesize);
1022 	offset = ws.notestart + notesize;
1023 	if (offset != ws.secstart)
1024 		panic("coredump: offset %lld != secstart %lld",
1025 		    (long long) offset, (long long) ws.secstart);
1026 #endif
1027 
1028 	/* Pass 3: finally, write the sections themselves. */
1029 	for (i = 0; i < ws.npsections - 1; i++) {
1030 		Elf_Phdr *pent = &ws.psections[i];
1031 		if (pent->p_filesz == 0)
1032 			continue;
1033 
1034 #ifdef DIAGNOSTIC
1035 		if (offset != pent->p_offset)
1036 			panic("coredump: offset %lld != p_offset[%d] %lld",
1037 			    (long long) offset, i,
1038 			    (long long) pent->p_filesz);
1039 #endif
1040 
1041 		error = coredump_write(cookie, UIO_USERSPACE,
1042 		    (void *)(vaddr_t)pent->p_vaddr, pent->p_filesz);
1043 		if (error)
1044 			goto out;
1045 
1046 		coredump_unmap(cookie, (vaddr_t)pent->p_vaddr,
1047 		    (vaddr_t)pent->p_vaddr + pent->p_filesz);
1048 
1049 #ifdef DIAGNOSTIC
1050 		offset += ws.psections[i].p_filesz;
1051 #endif
1052 	}
1053 
1054 out:
1055 	free(ws.psections, M_TEMP, ws.psectionslen);
1056 	return (error);
1057 }
1058 
1059 
1060 /*
1061  * Normally we lay out core files like this:
1062  *	[ELF Header] [Program headers] [Notes] [data for PT_LOAD segments]
1063  *
1064  * However, if there's >= 65535 segments then it overflows the field
1065  * in the ELF header, so the standard specifies putting a magic
1066  * number there and saving the real count in the .sh_info field of
1067  * the first *section* header...which requires generating a section
1068  * header.  To avoid confusing tools, we include an .shstrtab section
1069  * as well so all the indexes look valid.  So in this case we lay
1070  * out the core file like this:
1071  *	[ELF Header] [Section Headers] [.shstrtab] [Program headers] \
1072  *	[Notes] [data for PT_LOAD segments]
1073  *
1074  * The 'shstrtab' structure below is data for the second of the two
1075  * section headers, plus the .shstrtab itself, in one const buffer.
1076  */
1077 static const struct {
1078     Elf_Shdr	shdr;
1079     char	shstrtab[sizeof(ELF_SHSTRTAB) + 1];
1080 } shstrtab = {
1081     .shdr = {
1082 	.sh_name = 1,			/* offset in .shstrtab below */
1083 	.sh_type = SHT_STRTAB,
1084 	.sh_offset = sizeof(Elf_Ehdr) + 2*sizeof(Elf_Shdr),
1085 	.sh_size = sizeof(ELF_SHSTRTAB) + 1,
1086 	.sh_addralign = 1,
1087     },
1088     .shstrtab = "\0" ELF_SHSTRTAB,
1089 };
1090 
1091 int
1092 coredump_setup_elf(int segment_count, void *cookie)
1093 {
1094 	Elf_Ehdr ehdr;
1095 	struct writesegs_state *ws = cookie;
1096 	Elf_Phdr *note;
1097 	int error;
1098 
1099 	/* Get the count of segments, plus one for the PT_NOTE */
1100 	ws->npsections = segment_count + 1;
1101 
1102 	/* Get the size of the notes. */
1103 	error = coredump_notes_elf(ws->p, NULL, &ws->notesize);
1104 	if (error)
1105 		return error;
1106 
1107 	/* Setup the ELF header */
1108 	memset(&ehdr, 0, sizeof(ehdr));
1109 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
1110 	ehdr.e_ident[EI_CLASS] = ELF_TARG_CLASS;
1111 	ehdr.e_ident[EI_DATA] = ELF_TARG_DATA;
1112 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1113 	/* XXX Should be the OSABI/ABI version of the executable. */
1114 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
1115 	ehdr.e_ident[EI_ABIVERSION] = 0;
1116 	ehdr.e_type = ET_CORE;
1117 	/* XXX This should be the e_machine of the executable. */
1118 	ehdr.e_machine = ELF_TARG_MACH;
1119 	ehdr.e_version = EV_CURRENT;
1120 	ehdr.e_entry = 0;
1121 	ehdr.e_flags = 0;
1122 	ehdr.e_ehsize = sizeof(ehdr);
1123 	ehdr.e_phentsize = sizeof(Elf_Phdr);
1124 
1125 	if (ws->npsections < PN_XNUM) {
1126 		ehdr.e_phoff = sizeof(ehdr);
1127 		ehdr.e_shoff = 0;
1128 		ehdr.e_phnum = ws->npsections;
1129 		ehdr.e_shentsize = 0;
1130 		ehdr.e_shnum = 0;
1131 		ehdr.e_shstrndx = 0;
1132 	} else {
1133 		/* too many segments, use extension setup */
1134 		ehdr.e_shoff = sizeof(ehdr);
1135 		ehdr.e_phnum = PN_XNUM;
1136 		ehdr.e_shentsize = sizeof(Elf_Shdr);
1137 		ehdr.e_shnum = 2;
1138 		ehdr.e_shstrndx = 1;
1139 		ehdr.e_phoff = shstrtab.shdr.sh_offset + shstrtab.shdr.sh_size;
1140 	}
1141 
1142 	/* Write out the ELF header. */
1143 	error = coredump_write(ws->iocookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
1144 	if (error)
1145 		return error;
1146 
1147 	/*
1148 	 * If an section header is needed to store extension info, write
1149 	 * it out after the ELF header and before the program header.
1150 	 */
1151 	if (ehdr.e_shnum != 0) {
1152 		Elf_Shdr shdr = { .sh_info = ws->npsections };
1153 		error = coredump_write(ws->iocookie, UIO_SYSSPACE, &shdr,
1154 		    sizeof shdr);
1155 		if (error)
1156 			return error;
1157 		error = coredump_write(ws->iocookie, UIO_SYSSPACE, &shstrtab,
1158 		    sizeof(shstrtab.shdr) + sizeof(shstrtab.shstrtab));
1159 		if (error)
1160 			return error;
1161 	}
1162 
1163 	/*
1164 	 * Allocate the segment header array and setup to collect
1165 	 * the section sizes and offsets
1166 	 */
1167 	ws->psections = mallocarray(ws->npsections, sizeof(Elf_Phdr),
1168 	    M_TEMP, M_WAITOK|M_CANFAIL|M_ZERO);
1169 	if (ws->psections == NULL)
1170 		return ENOMEM;
1171 	ws->psectionslen = ws->npsections * sizeof(Elf_Phdr);
1172 
1173 	ws->notestart = ehdr.e_phoff + ws->psectionslen;
1174 	ws->secstart = ws->notestart + ws->notesize;
1175 	ws->secoff = ws->secstart;
1176 
1177 	/* Fill in the PT_NOTE segment header in the last slot */
1178 	note = &ws->psections[ws->npsections - 1];
1179 	note->p_type = PT_NOTE;
1180 	note->p_offset = ws->notestart;
1181 	note->p_vaddr = 0;
1182 	note->p_paddr = 0;
1183 	note->p_filesz = ws->notesize;
1184 	note->p_memsz = 0;
1185 	note->p_flags = PF_R;
1186 	note->p_align = ELFROUNDSIZE;
1187 
1188 	return (0);
1189 }
1190 
1191 int
1192 coredump_walk_elf(vaddr_t start, vaddr_t realend, vaddr_t end, vm_prot_t prot,
1193     int nsegment, void *cookie)
1194 {
1195 	struct writesegs_state *ws = cookie;
1196 	Elf_Phdr phdr;
1197 	vsize_t size, realsize;
1198 
1199 	size = end - start;
1200 	realsize = realend - start;
1201 
1202 	phdr.p_type = PT_LOAD;
1203 	phdr.p_offset = ws->secoff;
1204 	phdr.p_vaddr = start;
1205 	phdr.p_paddr = 0;
1206 	phdr.p_filesz = realsize;
1207 	phdr.p_memsz = size;
1208 	phdr.p_flags = 0;
1209 	if (prot & PROT_READ)
1210 		phdr.p_flags |= PF_R;
1211 	if (prot & PROT_WRITE)
1212 		phdr.p_flags |= PF_W;
1213 	if (prot & PROT_EXEC)
1214 		phdr.p_flags |= PF_X;
1215 	phdr.p_align = PAGE_SIZE;
1216 
1217 	ws->secoff += phdr.p_filesz;
1218 	ws->psections[nsegment] = phdr;
1219 
1220 	return (0);
1221 }
1222 
1223 int
1224 coredump_notes_elf(struct proc *p, void *iocookie, size_t *sizep)
1225 {
1226 	struct elfcore_procinfo cpi;
1227 	Elf_Note nhdr;
1228 	struct process *pr = p->p_p;
1229 	struct proc *q;
1230 	size_t size, notesize;
1231 	int error;
1232 
1233 	KASSERT(!P_HASSIBLING(p) || pr->ps_single != NULL);
1234 	size = 0;
1235 
1236 	/* First, write an elfcore_procinfo. */
1237 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1238 	    elfround(sizeof(cpi));
1239 	if (iocookie) {
1240 		memset(&cpi, 0, sizeof(cpi));
1241 
1242 		cpi.cpi_version = ELFCORE_PROCINFO_VERSION;
1243 		cpi.cpi_cpisize = sizeof(cpi);
1244 		cpi.cpi_signo = p->p_sisig;
1245 		cpi.cpi_sigcode = p->p_sicode;
1246 
1247 		cpi.cpi_sigpend = p->p_siglist | pr->ps_siglist;
1248 		cpi.cpi_sigmask = p->p_sigmask;
1249 		cpi.cpi_sigignore = pr->ps_sigacts->ps_sigignore;
1250 		cpi.cpi_sigcatch = pr->ps_sigacts->ps_sigcatch;
1251 
1252 		cpi.cpi_pid = pr->ps_pid;
1253 		cpi.cpi_ppid = pr->ps_ppid;
1254 		cpi.cpi_pgrp = pr->ps_pgid;
1255 		if (pr->ps_session->s_leader)
1256 			cpi.cpi_sid = pr->ps_session->s_leader->ps_pid;
1257 		else
1258 			cpi.cpi_sid = 0;
1259 
1260 		cpi.cpi_ruid = p->p_ucred->cr_ruid;
1261 		cpi.cpi_euid = p->p_ucred->cr_uid;
1262 		cpi.cpi_svuid = p->p_ucred->cr_svuid;
1263 
1264 		cpi.cpi_rgid = p->p_ucred->cr_rgid;
1265 		cpi.cpi_egid = p->p_ucred->cr_gid;
1266 		cpi.cpi_svgid = p->p_ucred->cr_svgid;
1267 
1268 		(void)strlcpy(cpi.cpi_name, pr->ps_comm, sizeof(cpi.cpi_name));
1269 
1270 		nhdr.namesz = sizeof("OpenBSD");
1271 		nhdr.descsz = sizeof(cpi);
1272 		nhdr.type = NT_OPENBSD_PROCINFO;
1273 
1274 		error = coredump_writenote_elf(p, iocookie, &nhdr,
1275 		    "OpenBSD", &cpi);
1276 		if (error)
1277 			return (error);
1278 	}
1279 	size += notesize;
1280 
1281 	/* Second, write an NT_OPENBSD_AUXV note. */
1282 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1283 	    elfround(ELF_AUX_WORDS * sizeof(char *));
1284 	if (iocookie && pr->ps_auxinfo) {
1285 
1286 		nhdr.namesz = sizeof("OpenBSD");
1287 		nhdr.descsz = ELF_AUX_WORDS * sizeof(char *);
1288 		nhdr.type = NT_OPENBSD_AUXV;
1289 
1290 		error = coredump_write(iocookie, UIO_SYSSPACE,
1291 		    &nhdr, sizeof(nhdr));
1292 		if (error)
1293 			return (error);
1294 
1295 		error = coredump_write(iocookie, UIO_SYSSPACE,
1296 		    "OpenBSD", elfround(nhdr.namesz));
1297 		if (error)
1298 			return (error);
1299 
1300 		error = coredump_write(iocookie, UIO_USERSPACE,
1301 		    (caddr_t)pr->ps_auxinfo, nhdr.descsz);
1302 		if (error)
1303 			return (error);
1304 	}
1305 	size += notesize;
1306 
1307 #ifdef PT_WCOOKIE
1308 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1309 	    elfround(sizeof(register_t));
1310 	if (iocookie) {
1311 		register_t wcookie;
1312 
1313 		nhdr.namesz = sizeof("OpenBSD");
1314 		nhdr.descsz = sizeof(register_t);
1315 		nhdr.type = NT_OPENBSD_WCOOKIE;
1316 
1317 		wcookie = process_get_wcookie(p);
1318 		error = coredump_writenote_elf(p, iocookie, &nhdr,
1319 		    "OpenBSD", &wcookie);
1320 		if (error)
1321 			return (error);
1322 	}
1323 	size += notesize;
1324 #endif
1325 
1326 	/*
1327 	 * Now write the register info for the thread that caused the
1328 	 * coredump.
1329 	 */
1330 	error = coredump_note_elf(p, iocookie, &notesize);
1331 	if (error)
1332 		return (error);
1333 	size += notesize;
1334 
1335 	/*
1336 	 * Now, for each thread, write the register info and any other
1337 	 * per-thread notes.  Since we're dumping core, all the other
1338 	 * threads in the process have been stopped and the list can't
1339 	 * change.
1340 	 */
1341 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
1342 		if (q == p)		/* we've taken care of this thread */
1343 			continue;
1344 		error = coredump_note_elf(q, iocookie, &notesize);
1345 		if (error)
1346 			return (error);
1347 		size += notesize;
1348 	}
1349 
1350 	*sizep = size;
1351 	return (0);
1352 }
1353 
1354 int
1355 coredump_note_elf(struct proc *p, void *iocookie, size_t *sizep)
1356 {
1357 	Elf_Note nhdr;
1358 	int size, notesize, error;
1359 	int namesize;
1360 	char name[64+ELFROUNDSIZE];
1361 	struct reg intreg;
1362 #ifdef PT_GETFPREGS
1363 	struct fpreg freg;
1364 #endif
1365 
1366 	size = 0;
1367 
1368 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
1369 	    "OpenBSD", p->p_tid + THREAD_PID_OFFSET);
1370 	namesize = strlen(name) + 1;
1371 	memset(name + namesize, 0, elfround(namesize) - namesize);
1372 
1373 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
1374 	if (iocookie) {
1375 		error = process_read_regs(p, &intreg);
1376 		if (error)
1377 			return (error);
1378 
1379 		nhdr.namesz = namesize;
1380 		nhdr.descsz = sizeof(intreg);
1381 		nhdr.type = NT_OPENBSD_REGS;
1382 
1383 		error = coredump_writenote_elf(p, iocookie, &nhdr,
1384 		    name, &intreg);
1385 		if (error)
1386 			return (error);
1387 
1388 	}
1389 	size += notesize;
1390 
1391 #ifdef PT_GETFPREGS
1392 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
1393 	if (iocookie) {
1394 		error = process_read_fpregs(p, &freg);
1395 		if (error)
1396 			return (error);
1397 
1398 		nhdr.namesz = namesize;
1399 		nhdr.descsz = sizeof(freg);
1400 		nhdr.type = NT_OPENBSD_FPREGS;
1401 
1402 		error = coredump_writenote_elf(p, iocookie, &nhdr, name, &freg);
1403 		if (error)
1404 			return (error);
1405 	}
1406 	size += notesize;
1407 #endif
1408 
1409 	*sizep = size;
1410 	/* XXX Add hook for machdep per-LWP notes. */
1411 	return (0);
1412 }
1413 
1414 int
1415 coredump_writenote_elf(struct proc *p, void *cookie, Elf_Note *nhdr,
1416     const char *name, void *data)
1417 {
1418 	int error;
1419 
1420 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
1421 	if (error)
1422 		return error;
1423 
1424 	error = coredump_write(cookie, UIO_SYSSPACE, name,
1425 	    elfround(nhdr->namesz));
1426 	if (error)
1427 		return error;
1428 
1429 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->descsz);
1430 }
1431 #endif /* !SMALL_KERNEL */
1432