xref: /openbsd-src/sys/kern/exec_elf.c (revision 13f0bae737815c2bc645da585a42c3d9a6f218f2)
1 /*	$OpenBSD: exec_elf.c,v 1.179 2023/01/13 23:02:43 kettenis Exp $	*/
2 
3 /*
4  * Copyright (c) 1996 Per Fogelstrom
5  * All rights reserved.
6  *
7  * Copyright (c) 1994 Christos Zoulas
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 /*
35  * Copyright (c) 2001 Wasabi Systems, Inc.
36  * All rights reserved.
37  *
38  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions
42  * are met:
43  * 1. Redistributions of source code must retain the above copyright
44  *    notice, this list of conditions and the following disclaimer.
45  * 2. Redistributions in binary form must reproduce the above copyright
46  *    notice, this list of conditions and the following disclaimer in the
47  *    documentation and/or other materials provided with the distribution.
48  * 3. All advertising materials mentioning features or use of this software
49  *    must display the following acknowledgement:
50  *	This product includes software developed for the NetBSD Project by
51  *	Wasabi Systems, Inc.
52  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
53  *    or promote products derived from this software without specific prior
54  *    written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
57  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
60  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66  * POSSIBILITY OF SUCH DAMAGE.
67  */
68 
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/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 extern vaddr_t sigcode_va;
986 extern vsize_t sigcode_sz;
987 
988 int
989 coredump_elf(struct proc *p, void *cookie)
990 {
991 #ifdef DIAGNOSTIC
992 	off_t offset;
993 #endif
994 	struct writesegs_state ws;
995 	size_t notesize;
996 	int error, i;
997 
998 	ws.p = p;
999 	ws.iocookie = cookie;
1000 	ws.psections = NULL;
1001 
1002 	/*
1003 	 * Walk the map to get all the segment offsets and lengths,
1004 	 * write out the ELF header.
1005 	 */
1006 	error = uvm_coredump_walkmap(p, coredump_setup_elf,
1007 	    coredump_walk_elf, &ws);
1008 	if (error)
1009 		goto out;
1010 
1011 	error = coredump_write(cookie, UIO_SYSSPACE, ws.psections,
1012 	    ws.psectionslen);
1013 	if (error)
1014 		goto out;
1015 
1016 	/* Write out the notes. */
1017 	error = coredump_notes_elf(p, cookie, &notesize);
1018 	if (error)
1019 		goto out;
1020 
1021 #ifdef DIAGNOSTIC
1022 	if (notesize != ws.notesize)
1023 		panic("coredump: notesize changed: %zu != %zu",
1024 		    ws.notesize, notesize);
1025 	offset = ws.notestart + notesize;
1026 	if (offset != ws.secstart)
1027 		panic("coredump: offset %lld != secstart %lld",
1028 		    (long long) offset, (long long) ws.secstart);
1029 #endif
1030 
1031 	/* Pass 3: finally, write the sections themselves. */
1032 	for (i = 0; i < ws.npsections - 1; i++) {
1033 		Elf_Phdr *pent = &ws.psections[i];
1034 		if (pent->p_filesz == 0)
1035 			continue;
1036 
1037 #ifdef DIAGNOSTIC
1038 		if (offset != pent->p_offset)
1039 			panic("coredump: offset %lld != p_offset[%d] %lld",
1040 			    (long long) offset, i,
1041 			    (long long) pent->p_filesz);
1042 #endif
1043 
1044 		/*
1045 		 * Since the sigcode is mapped execute-only, we can't
1046 		 * read it.  So use the kernel mapping for it instead.
1047 		 */
1048 		if (pent->p_vaddr == p->p_p->ps_sigcode &&
1049 		    pent->p_filesz == sigcode_sz) {
1050 			error = coredump_write(cookie, UIO_SYSSPACE,
1051 			    (void *)sigcode_va, sigcode_sz);
1052 		} else {
1053 			error = coredump_write(cookie, UIO_USERSPACE,
1054 			    (void *)(vaddr_t)pent->p_vaddr, pent->p_filesz);
1055 		}
1056 		if (error)
1057 			goto out;
1058 
1059 		coredump_unmap(cookie, (vaddr_t)pent->p_vaddr,
1060 		    (vaddr_t)pent->p_vaddr + pent->p_filesz);
1061 
1062 #ifdef DIAGNOSTIC
1063 		offset += ws.psections[i].p_filesz;
1064 #endif
1065 	}
1066 
1067 out:
1068 	free(ws.psections, M_TEMP, ws.psectionslen);
1069 	return (error);
1070 }
1071 
1072 
1073 /*
1074  * Normally we lay out core files like this:
1075  *	[ELF Header] [Program headers] [Notes] [data for PT_LOAD segments]
1076  *
1077  * However, if there's >= 65535 segments then it overflows the field
1078  * in the ELF header, so the standard specifies putting a magic
1079  * number there and saving the real count in the .sh_info field of
1080  * the first *section* header...which requires generating a section
1081  * header.  To avoid confusing tools, we include an .shstrtab section
1082  * as well so all the indexes look valid.  So in this case we lay
1083  * out the core file like this:
1084  *	[ELF Header] [Section Headers] [.shstrtab] [Program headers] \
1085  *	[Notes] [data for PT_LOAD segments]
1086  *
1087  * The 'shstrtab' structure below is data for the second of the two
1088  * section headers, plus the .shstrtab itself, in one const buffer.
1089  */
1090 static const struct {
1091     Elf_Shdr	shdr;
1092     char	shstrtab[sizeof(ELF_SHSTRTAB) + 1];
1093 } shstrtab = {
1094     .shdr = {
1095 	.sh_name = 1,			/* offset in .shstrtab below */
1096 	.sh_type = SHT_STRTAB,
1097 	.sh_offset = sizeof(Elf_Ehdr) + 2*sizeof(Elf_Shdr),
1098 	.sh_size = sizeof(ELF_SHSTRTAB) + 1,
1099 	.sh_addralign = 1,
1100     },
1101     .shstrtab = "\0" ELF_SHSTRTAB,
1102 };
1103 
1104 int
1105 coredump_setup_elf(int segment_count, void *cookie)
1106 {
1107 	Elf_Ehdr ehdr;
1108 	struct writesegs_state *ws = cookie;
1109 	Elf_Phdr *note;
1110 	int error;
1111 
1112 	/* Get the count of segments, plus one for the PT_NOTE */
1113 	ws->npsections = segment_count + 1;
1114 
1115 	/* Get the size of the notes. */
1116 	error = coredump_notes_elf(ws->p, NULL, &ws->notesize);
1117 	if (error)
1118 		return error;
1119 
1120 	/* Setup the ELF header */
1121 	memset(&ehdr, 0, sizeof(ehdr));
1122 	memcpy(ehdr.e_ident, ELFMAG, SELFMAG);
1123 	ehdr.e_ident[EI_CLASS] = ELF_TARG_CLASS;
1124 	ehdr.e_ident[EI_DATA] = ELF_TARG_DATA;
1125 	ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1126 	/* XXX Should be the OSABI/ABI version of the executable. */
1127 	ehdr.e_ident[EI_OSABI] = ELFOSABI_SYSV;
1128 	ehdr.e_ident[EI_ABIVERSION] = 0;
1129 	ehdr.e_type = ET_CORE;
1130 	/* XXX This should be the e_machine of the executable. */
1131 	ehdr.e_machine = ELF_TARG_MACH;
1132 	ehdr.e_version = EV_CURRENT;
1133 	ehdr.e_entry = 0;
1134 	ehdr.e_flags = 0;
1135 	ehdr.e_ehsize = sizeof(ehdr);
1136 	ehdr.e_phentsize = sizeof(Elf_Phdr);
1137 
1138 	if (ws->npsections < PN_XNUM) {
1139 		ehdr.e_phoff = sizeof(ehdr);
1140 		ehdr.e_shoff = 0;
1141 		ehdr.e_phnum = ws->npsections;
1142 		ehdr.e_shentsize = 0;
1143 		ehdr.e_shnum = 0;
1144 		ehdr.e_shstrndx = 0;
1145 	} else {
1146 		/* too many segments, use extension setup */
1147 		ehdr.e_shoff = sizeof(ehdr);
1148 		ehdr.e_phnum = PN_XNUM;
1149 		ehdr.e_shentsize = sizeof(Elf_Shdr);
1150 		ehdr.e_shnum = 2;
1151 		ehdr.e_shstrndx = 1;
1152 		ehdr.e_phoff = shstrtab.shdr.sh_offset + shstrtab.shdr.sh_size;
1153 	}
1154 
1155 	/* Write out the ELF header. */
1156 	error = coredump_write(ws->iocookie, UIO_SYSSPACE, &ehdr, sizeof(ehdr));
1157 	if (error)
1158 		return error;
1159 
1160 	/*
1161 	 * If an section header is needed to store extension info, write
1162 	 * it out after the ELF header and before the program header.
1163 	 */
1164 	if (ehdr.e_shnum != 0) {
1165 		Elf_Shdr shdr = { .sh_info = ws->npsections };
1166 		error = coredump_write(ws->iocookie, UIO_SYSSPACE, &shdr,
1167 		    sizeof shdr);
1168 		if (error)
1169 			return error;
1170 		error = coredump_write(ws->iocookie, UIO_SYSSPACE, &shstrtab,
1171 		    sizeof(shstrtab.shdr) + sizeof(shstrtab.shstrtab));
1172 		if (error)
1173 			return error;
1174 	}
1175 
1176 	/*
1177 	 * Allocate the segment header array and setup to collect
1178 	 * the section sizes and offsets
1179 	 */
1180 	ws->psections = mallocarray(ws->npsections, sizeof(Elf_Phdr),
1181 	    M_TEMP, M_WAITOK|M_CANFAIL|M_ZERO);
1182 	if (ws->psections == NULL)
1183 		return ENOMEM;
1184 	ws->psectionslen = ws->npsections * sizeof(Elf_Phdr);
1185 
1186 	ws->notestart = ehdr.e_phoff + ws->psectionslen;
1187 	ws->secstart = ws->notestart + ws->notesize;
1188 	ws->secoff = ws->secstart;
1189 
1190 	/* Fill in the PT_NOTE segment header in the last slot */
1191 	note = &ws->psections[ws->npsections - 1];
1192 	note->p_type = PT_NOTE;
1193 	note->p_offset = ws->notestart;
1194 	note->p_vaddr = 0;
1195 	note->p_paddr = 0;
1196 	note->p_filesz = ws->notesize;
1197 	note->p_memsz = 0;
1198 	note->p_flags = PF_R;
1199 	note->p_align = ELFROUNDSIZE;
1200 
1201 	return (0);
1202 }
1203 
1204 int
1205 coredump_walk_elf(vaddr_t start, vaddr_t realend, vaddr_t end, vm_prot_t prot,
1206     int nsegment, void *cookie)
1207 {
1208 	struct writesegs_state *ws = cookie;
1209 	Elf_Phdr phdr;
1210 	vsize_t size, realsize;
1211 
1212 	size = end - start;
1213 	realsize = realend - start;
1214 
1215 	phdr.p_type = PT_LOAD;
1216 	phdr.p_offset = ws->secoff;
1217 	phdr.p_vaddr = start;
1218 	phdr.p_paddr = 0;
1219 	phdr.p_filesz = realsize;
1220 	phdr.p_memsz = size;
1221 	phdr.p_flags = 0;
1222 	if (prot & PROT_READ)
1223 		phdr.p_flags |= PF_R;
1224 	if (prot & PROT_WRITE)
1225 		phdr.p_flags |= PF_W;
1226 	if (prot & PROT_EXEC)
1227 		phdr.p_flags |= PF_X;
1228 	phdr.p_align = PAGE_SIZE;
1229 
1230 	ws->secoff += phdr.p_filesz;
1231 	ws->psections[nsegment] = phdr;
1232 
1233 	return (0);
1234 }
1235 
1236 int
1237 coredump_notes_elf(struct proc *p, void *iocookie, size_t *sizep)
1238 {
1239 	struct elfcore_procinfo cpi;
1240 	Elf_Note nhdr;
1241 	struct process *pr = p->p_p;
1242 	struct proc *q;
1243 	size_t size, notesize;
1244 	int error;
1245 
1246 	KASSERT(!P_HASSIBLING(p) || pr->ps_single != NULL);
1247 	size = 0;
1248 
1249 	/* First, write an elfcore_procinfo. */
1250 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1251 	    elfround(sizeof(cpi));
1252 	if (iocookie) {
1253 		memset(&cpi, 0, sizeof(cpi));
1254 
1255 		cpi.cpi_version = ELFCORE_PROCINFO_VERSION;
1256 		cpi.cpi_cpisize = sizeof(cpi);
1257 		cpi.cpi_signo = p->p_sisig;
1258 		cpi.cpi_sigcode = p->p_sicode;
1259 
1260 		cpi.cpi_sigpend = p->p_siglist | pr->ps_siglist;
1261 		cpi.cpi_sigmask = p->p_sigmask;
1262 		cpi.cpi_sigignore = pr->ps_sigacts->ps_sigignore;
1263 		cpi.cpi_sigcatch = pr->ps_sigacts->ps_sigcatch;
1264 
1265 		cpi.cpi_pid = pr->ps_pid;
1266 		cpi.cpi_ppid = pr->ps_ppid;
1267 		cpi.cpi_pgrp = pr->ps_pgid;
1268 		if (pr->ps_session->s_leader)
1269 			cpi.cpi_sid = pr->ps_session->s_leader->ps_pid;
1270 		else
1271 			cpi.cpi_sid = 0;
1272 
1273 		cpi.cpi_ruid = p->p_ucred->cr_ruid;
1274 		cpi.cpi_euid = p->p_ucred->cr_uid;
1275 		cpi.cpi_svuid = p->p_ucred->cr_svuid;
1276 
1277 		cpi.cpi_rgid = p->p_ucred->cr_rgid;
1278 		cpi.cpi_egid = p->p_ucred->cr_gid;
1279 		cpi.cpi_svgid = p->p_ucred->cr_svgid;
1280 
1281 		(void)strlcpy(cpi.cpi_name, pr->ps_comm, sizeof(cpi.cpi_name));
1282 
1283 		nhdr.namesz = sizeof("OpenBSD");
1284 		nhdr.descsz = sizeof(cpi);
1285 		nhdr.type = NT_OPENBSD_PROCINFO;
1286 
1287 		error = coredump_writenote_elf(p, iocookie, &nhdr,
1288 		    "OpenBSD", &cpi);
1289 		if (error)
1290 			return (error);
1291 	}
1292 	size += notesize;
1293 
1294 	/* Second, write an NT_OPENBSD_AUXV note. */
1295 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1296 	    elfround(ELF_AUX_WORDS * sizeof(char *));
1297 	if (iocookie && pr->ps_auxinfo) {
1298 
1299 		nhdr.namesz = sizeof("OpenBSD");
1300 		nhdr.descsz = ELF_AUX_WORDS * sizeof(char *);
1301 		nhdr.type = NT_OPENBSD_AUXV;
1302 
1303 		error = coredump_write(iocookie, UIO_SYSSPACE,
1304 		    &nhdr, sizeof(nhdr));
1305 		if (error)
1306 			return (error);
1307 
1308 		error = coredump_write(iocookie, UIO_SYSSPACE,
1309 		    "OpenBSD", elfround(nhdr.namesz));
1310 		if (error)
1311 			return (error);
1312 
1313 		error = coredump_write(iocookie, UIO_USERSPACE,
1314 		    (caddr_t)pr->ps_auxinfo, nhdr.descsz);
1315 		if (error)
1316 			return (error);
1317 	}
1318 	size += notesize;
1319 
1320 #ifdef PT_WCOOKIE
1321 	notesize = sizeof(nhdr) + elfround(sizeof("OpenBSD")) +
1322 	    elfround(sizeof(register_t));
1323 	if (iocookie) {
1324 		register_t wcookie;
1325 
1326 		nhdr.namesz = sizeof("OpenBSD");
1327 		nhdr.descsz = sizeof(register_t);
1328 		nhdr.type = NT_OPENBSD_WCOOKIE;
1329 
1330 		wcookie = process_get_wcookie(p);
1331 		error = coredump_writenote_elf(p, iocookie, &nhdr,
1332 		    "OpenBSD", &wcookie);
1333 		if (error)
1334 			return (error);
1335 	}
1336 	size += notesize;
1337 #endif
1338 
1339 	/*
1340 	 * Now write the register info for the thread that caused the
1341 	 * coredump.
1342 	 */
1343 	error = coredump_note_elf(p, iocookie, &notesize);
1344 	if (error)
1345 		return (error);
1346 	size += notesize;
1347 
1348 	/*
1349 	 * Now, for each thread, write the register info and any other
1350 	 * per-thread notes.  Since we're dumping core, all the other
1351 	 * threads in the process have been stopped and the list can't
1352 	 * change.
1353 	 */
1354 	TAILQ_FOREACH(q, &pr->ps_threads, p_thr_link) {
1355 		if (q == p)		/* we've taken care of this thread */
1356 			continue;
1357 		error = coredump_note_elf(q, iocookie, &notesize);
1358 		if (error)
1359 			return (error);
1360 		size += notesize;
1361 	}
1362 
1363 	*sizep = size;
1364 	return (0);
1365 }
1366 
1367 int
1368 coredump_note_elf(struct proc *p, void *iocookie, size_t *sizep)
1369 {
1370 	Elf_Note nhdr;
1371 	int size, notesize, error;
1372 	int namesize;
1373 	char name[64+ELFROUNDSIZE];
1374 	struct reg intreg;
1375 #ifdef PT_GETFPREGS
1376 	struct fpreg freg;
1377 #endif
1378 
1379 	size = 0;
1380 
1381 	snprintf(name, sizeof(name)-ELFROUNDSIZE, "%s@%d",
1382 	    "OpenBSD", p->p_tid + THREAD_PID_OFFSET);
1383 	namesize = strlen(name) + 1;
1384 	memset(name + namesize, 0, elfround(namesize) - namesize);
1385 
1386 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(intreg));
1387 	if (iocookie) {
1388 		error = process_read_regs(p, &intreg);
1389 		if (error)
1390 			return (error);
1391 
1392 		nhdr.namesz = namesize;
1393 		nhdr.descsz = sizeof(intreg);
1394 		nhdr.type = NT_OPENBSD_REGS;
1395 
1396 		error = coredump_writenote_elf(p, iocookie, &nhdr,
1397 		    name, &intreg);
1398 		if (error)
1399 			return (error);
1400 
1401 	}
1402 	size += notesize;
1403 
1404 #ifdef PT_GETFPREGS
1405 	notesize = sizeof(nhdr) + elfround(namesize) + elfround(sizeof(freg));
1406 	if (iocookie) {
1407 		error = process_read_fpregs(p, &freg);
1408 		if (error)
1409 			return (error);
1410 
1411 		nhdr.namesz = namesize;
1412 		nhdr.descsz = sizeof(freg);
1413 		nhdr.type = NT_OPENBSD_FPREGS;
1414 
1415 		error = coredump_writenote_elf(p, iocookie, &nhdr, name, &freg);
1416 		if (error)
1417 			return (error);
1418 	}
1419 	size += notesize;
1420 #endif
1421 
1422 	*sizep = size;
1423 	/* XXX Add hook for machdep per-LWP notes. */
1424 	return (0);
1425 }
1426 
1427 int
1428 coredump_writenote_elf(struct proc *p, void *cookie, Elf_Note *nhdr,
1429     const char *name, void *data)
1430 {
1431 	int error;
1432 
1433 	error = coredump_write(cookie, UIO_SYSSPACE, nhdr, sizeof(*nhdr));
1434 	if (error)
1435 		return error;
1436 
1437 	error = coredump_write(cookie, UIO_SYSSPACE, name,
1438 	    elfround(nhdr->namesz));
1439 	if (error)
1440 		return error;
1441 
1442 	return coredump_write(cookie, UIO_SYSSPACE, data, nhdr->descsz);
1443 }
1444 #endif /* !SMALL_KERNEL */
1445