xref: /freebsd/sys/kern/imgact_elf.c (revision 78f57a9cdef729f94669fe1eb494666f403f9235)
1e1743d02SSøren Schmidt /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a36da99SPedro F. Giffuni  *
486be94fcSTycho Nightingale  * Copyright (c) 2017 Dell EMC
521a3ee0eSDavid E. O'Brien  * Copyright (c) 2000 David O'Brien
69a14aa01SUlrich Spörlein  * Copyright (c) 1995-1996 Søren Schmidt
7e1743d02SSøren Schmidt  * Copyright (c) 1996 Peter Wemm
8e1743d02SSøren Schmidt  * All rights reserved.
9e1743d02SSøren Schmidt  *
10e1743d02SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
11e1743d02SSøren Schmidt  * modification, are permitted provided that the following conditions
12e1743d02SSøren Schmidt  * are met:
13e1743d02SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
14e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
15e1743d02SSøren Schmidt  *    in this position and unchanged.
16e1743d02SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
17e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
18e1743d02SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
19e1743d02SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
2021dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
21e1743d02SSøren Schmidt  *
22e1743d02SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23e1743d02SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24e1743d02SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25e1743d02SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26e1743d02SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27e1743d02SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28e1743d02SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29e1743d02SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30e1743d02SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31e1743d02SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32e1743d02SSøren Schmidt  */
33e1743d02SSøren Schmidt 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
3712bc222eSJonathan Anderson #include "opt_capsicum.h"
3862919d78SPeter Wemm #include "opt_compat.h"
3962919d78SPeter Wemm 
40e1743d02SSøren Schmidt #include <sys/param.h>
414a144410SRobert Watson #include <sys/capsicum.h>
42*78f57a9cSMark Johnston #include <sys/compressor.h>
43e1743d02SSøren Schmidt #include <sys/exec.h>
448c64af4fSJohn Polstra #include <sys/fcntl.h>
45e1743d02SSøren Schmidt #include <sys/imgact.h>
46e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
47b96bd95bSIan Lepore #include <sys/jail.h>
48e1743d02SSøren Schmidt #include <sys/kernel.h>
49f34fa851SJohn Baldwin #include <sys/lock.h>
50e1743d02SSøren Schmidt #include <sys/malloc.h>
5168ff2a43SChristian S.J. Peron #include <sys/mount.h>
528c64af4fSJohn Polstra #include <sys/mman.h>
53a794e791SBruce Evans #include <sys/namei.h>
548c64af4fSJohn Polstra #include <sys/pioctl.h>
55a794e791SBruce Evans #include <sys/proc.h>
568c64af4fSJohn Polstra #include <sys/procfs.h>
5786be94fcSTycho Nightingale #include <sys/ptrace.h>
581ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
598c64af4fSJohn Polstra #include <sys/resourcevar.h>
6089f6b863SAttilio Rao #include <sys/rwlock.h>
61bd390213SMikolaj Golub #include <sys/sbuf.h>
62da61b9a6SAlan Cox #include <sys/sf_buf.h>
63ee235befSKonstantin Belousov #include <sys/smp.h>
6436240ea5SDoug Rabson #include <sys/systm.h>
65e1743d02SSøren Schmidt #include <sys/signalvar.h>
668c64af4fSJohn Polstra #include <sys/stat.h>
671005a129SJohn Baldwin #include <sys/sx.h>
688c64af4fSJohn Polstra #include <sys/syscall.h>
69e1743d02SSøren Schmidt #include <sys/sysctl.h>
708c64af4fSJohn Polstra #include <sys/sysent.h>
71a794e791SBruce Evans #include <sys/vnode.h>
72e7228204SAlfred Perlstein #include <sys/syslog.h>
73e7228204SAlfred Perlstein #include <sys/eventhandler.h>
74f1fca82eSMikolaj Golub #include <sys/user.h>
75e7228204SAlfred Perlstein 
76e1743d02SSøren Schmidt #include <vm/vm.h>
77e1743d02SSøren Schmidt #include <vm/vm_kern.h>
78e1743d02SSøren Schmidt #include <vm/vm_param.h>
79e1743d02SSøren Schmidt #include <vm/pmap.h>
80e1743d02SSøren Schmidt #include <vm/vm_map.h>
810ff27d31SJohn Polstra #include <vm/vm_object.h>
82e1743d02SSøren Schmidt #include <vm/vm_extern.h>
83e1743d02SSøren Schmidt 
8452c24af7SPeter Wemm #include <machine/elf.h>
85e1743d02SSøren Schmidt #include <machine/md_var.h>
86e1743d02SSøren Schmidt 
871b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE	4
88c815a20cSDavid E. O'Brien #define OLD_EI_BRAND	8
89c815a20cSDavid E. O'Brien 
903ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr);
9132c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
92d1ae5c83SKonstantin Belousov     const char *interp, int interp_name_len, int32_t *osrel);
933ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
943ebc1248SPeter Wemm     u_long *entry, size_t pagesize);
950bbee4cdSKonstantin Belousov static int __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
96292177e6SAlan Cox     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
97292177e6SAlan Cox     size_t pagesize);
983ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
9989ffc202SBjoern A. Zeeb static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
10089ffc202SBjoern A. Zeeb     int32_t *osrel);
10189ffc202SBjoern A. Zeeb static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
10232c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp,
10332c01de2SDmitry Chagin     Elf_Brandnote *checknote, int32_t *osrel);
104ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word);
105ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t);
106e1743d02SSøren Schmidt 
107a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
108a360a43dSJake Burkholder     "");
109a360a43dSJake Burkholder 
110bd390213SMikolaj Golub #define	CORE_BUF_SIZE	(16 * 1024)
111e7228204SAlfred Perlstein 
112e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1;
113e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
114af3b2549SHans Petter Selasky     fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
115a360a43dSJake Burkholder     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
116a360a43dSJake Burkholder 
117551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0;
118a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
1191cbb879dSEd Maste     &elf_legacy_coredump, 0,
1201cbb879dSEd Maste     "include all and only RW pages in core dumps");
121e1743d02SSøren Schmidt 
12262c625fdSKonstantin Belousov int __elfN(nxstack) =
1234d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \
1244d22d07aSKonstantin Belousov     (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__)
12562c625fdSKonstantin Belousov 	1;
12662c625fdSKonstantin Belousov #else
12762c625fdSKonstantin Belousov 	0;
12862c625fdSKonstantin Belousov #endif
129291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
130291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
131291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
132291c06a1SKonstantin Belousov 
133126b36a2SKonstantin Belousov #if __ELF_WORD_SIZE == 32
134e7d939bdSMarcel Moolenaar #if defined(__amd64__)
135126b36a2SKonstantin Belousov int i386_read_exec = 0;
136126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
137126b36a2SKonstantin Belousov     "enable execution from readable segments");
138126b36a2SKonstantin Belousov #endif
139126b36a2SKonstantin Belousov #endif
140126b36a2SKonstantin Belousov 
1413ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
142e1743d02SSøren Schmidt 
143d9c9c81cSPedro F. Giffuni #define	trunc_page_ps(va, ps)	rounddown2(va, ps)
144d9c9c81cSPedro F. Giffuni #define	round_page_ps(va, ps)	roundup2(va, ps)
14593d1c728SKonstantin Belousov #define	aligned(a, t)	(trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
14693d1c728SKonstantin Belousov 
14732c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
14832c01de2SDmitry Chagin 
14932c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = {
15032c01de2SDmitry Chagin 	.hdr.n_namesz	= sizeof(FREEBSD_ABI_VENDOR),
15132c01de2SDmitry Chagin 	.hdr.n_descsz	= sizeof(int32_t),
1524c22b468SEd Maste 	.hdr.n_type	= NT_FREEBSD_ABI_TAG,
15332c01de2SDmitry Chagin 	.vendor		= FREEBSD_ABI_VENDOR,
15489ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
15589ffc202SBjoern A. Zeeb 	.trans_osrel	= __elfN(freebsd_trans_osrel)
15632c01de2SDmitry Chagin };
15732c01de2SDmitry Chagin 
15889ffc202SBjoern A. Zeeb static boolean_t
15989ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
16089ffc202SBjoern A. Zeeb {
16189ffc202SBjoern A. Zeeb 	uintptr_t p;
16289ffc202SBjoern A. Zeeb 
16389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1641b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
16589ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
16689ffc202SBjoern A. Zeeb 
16789ffc202SBjoern A. Zeeb 	return (TRUE);
16889ffc202SBjoern A. Zeeb }
16989ffc202SBjoern A. Zeeb 
17089ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU";
17189ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3;
17289ffc202SBjoern A. Zeeb 
17389ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = {
17489ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
17589ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
17689ffc202SBjoern A. Zeeb 	.hdr.n_type	= 1,
17789ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
17889ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
17989ffc202SBjoern A. Zeeb 	.trans_osrel	= kfreebsd_trans_osrel
18089ffc202SBjoern A. Zeeb };
18189ffc202SBjoern A. Zeeb 
18289ffc202SBjoern A. Zeeb static boolean_t
18389ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
18489ffc202SBjoern A. Zeeb {
18589ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
18689ffc202SBjoern A. Zeeb 	uintptr_t p;
18789ffc202SBjoern A. Zeeb 
18889ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1891b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
19089ffc202SBjoern A. Zeeb 
19189ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
19289ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
19389ffc202SBjoern A. Zeeb 		return (FALSE);
19489ffc202SBjoern A. Zeeb 
19589ffc202SBjoern A. Zeeb 	/*
19689ffc202SBjoern A. Zeeb 	 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
19789ffc202SBjoern A. Zeeb 	 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
19889ffc202SBjoern A. Zeeb 	 */
19989ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
20089ffc202SBjoern A. Zeeb 
20189ffc202SBjoern A. Zeeb 	return (TRUE);
20289ffc202SBjoern A. Zeeb }
20389ffc202SBjoern A. Zeeb 
204e1743d02SSøren Schmidt int
2053ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
206e1743d02SSøren Schmidt {
207e1743d02SSøren Schmidt 	int i;
208e1743d02SSøren Schmidt 
2093ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
210ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == NULL) {
211ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = entry;
212e1743d02SSøren Schmidt 			break;
213e1743d02SSøren Schmidt 		}
214e1743d02SSøren Schmidt 	}
215925c8b5bSBjoern A. Zeeb 	if (i == MAX_BRANDS) {
216925c8b5bSBjoern A. Zeeb 		printf("WARNING: %s: could not insert brandinfo entry: %p\n",
217925c8b5bSBjoern A. Zeeb 			__func__, entry);
218a7cddfedSJake Burkholder 		return (-1);
219925c8b5bSBjoern A. Zeeb 	}
220a7cddfedSJake Burkholder 	return (0);
221e1743d02SSøren Schmidt }
222e1743d02SSøren Schmidt 
223e1743d02SSøren Schmidt int
2243ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
225e1743d02SSøren Schmidt {
226e1743d02SSøren Schmidt 	int i;
227e1743d02SSøren Schmidt 
2283ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
229ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == entry) {
230ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = NULL;
231e1743d02SSøren Schmidt 			break;
232e1743d02SSøren Schmidt 		}
233e1743d02SSøren Schmidt 	}
234ea5a2b2eSSøren Schmidt 	if (i == MAX_BRANDS)
235a7cddfedSJake Burkholder 		return (-1);
236a7cddfedSJake Burkholder 	return (0);
237e1743d02SSøren Schmidt }
238e1743d02SSøren Schmidt 
239096977faSMark Newton int
2403ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry)
241096977faSMark Newton {
242096977faSMark Newton 	struct proc *p;
243553629ebSJake Burkholder 	int rval = FALSE;
244096977faSMark Newton 
2451005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2464f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
247553629ebSJake Burkholder 		if (p->p_sysent == entry->sysvec) {
248553629ebSJake Burkholder 			rval = TRUE;
249553629ebSJake Burkholder 			break;
250096977faSMark Newton 		}
251553629ebSJake Burkholder 	}
2521005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
253096977faSMark Newton 
254553629ebSJake Burkholder 	return (rval);
255096977faSMark Newton }
256096977faSMark Newton 
2575fe3ed62SJake Burkholder static Elf_Brandinfo *
25832c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
259d1ae5c83SKonstantin Belousov     int interp_name_len, int32_t *osrel)
2605fe3ed62SJake Burkholder {
26132c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
262af582aaeSKonstantin Belousov 	Elf_Brandinfo *bi, *bi_m;
26332c01de2SDmitry Chagin 	boolean_t ret;
2645fe3ed62SJake Burkholder 	int i;
2655fe3ed62SJake Burkholder 
2665fe3ed62SJake Burkholder 	/*
26732c01de2SDmitry Chagin 	 * We support four types of branding -- (1) the ELF EI_OSABI field
2685fe3ed62SJake Burkholder 	 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
26932c01de2SDmitry Chagin 	 * branding w/in the ELF header, (3) path of the `interp_path'
27032c01de2SDmitry Chagin 	 * field, and (4) the ".note.ABI-tag" ELF section.
2715fe3ed62SJake Burkholder 	 */
2725fe3ed62SJake Burkholder 
27332c01de2SDmitry Chagin 	/* Look for an ".note.ABI-tag" ELF section */
274af582aaeSKonstantin Belousov 	bi_m = NULL;
27532c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
27632c01de2SDmitry Chagin 		bi = elf_brand_list[i];
277ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
278ecc2fda8SBjoern A. Zeeb 			continue;
2792274ab3dSKonstantin Belousov 		if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)
2801438fe3cSKonstantin Belousov 			continue;
281ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
282ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
28332c01de2SDmitry Chagin 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
284f19d421aSNathan Whitehorn 			/* Give brand a chance to veto check_note's guess */
285f19d421aSNathan Whitehorn 			if (ret && bi->header_supported)
286f19d421aSNathan Whitehorn 				ret = bi->header_supported(imgp);
287af582aaeSKonstantin Belousov 			/*
288af582aaeSKonstantin Belousov 			 * If note checker claimed the binary, but the
289af582aaeSKonstantin Belousov 			 * interpreter path in the image does not
290af582aaeSKonstantin Belousov 			 * match default one for the brand, try to
291af582aaeSKonstantin Belousov 			 * search for other brands with the same
292af582aaeSKonstantin Belousov 			 * interpreter.  Either there is better brand
293af582aaeSKonstantin Belousov 			 * with the right interpreter, or, failing
294af582aaeSKonstantin Belousov 			 * this, we return first brand which accepted
295af582aaeSKonstantin Belousov 			 * our note and, optionally, header.
296af582aaeSKonstantin Belousov 			 */
2973aeacc55SKonstantin Belousov 			if (ret && bi_m == NULL && interp != NULL &&
2983aeacc55SKonstantin Belousov 			    (bi->interp_path == NULL ||
2993aeacc55SKonstantin Belousov 			    (strlen(bi->interp_path) + 1 != interp_name_len ||
3003aeacc55SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
3013aeacc55SKonstantin Belousov 			    != 0))) {
302af582aaeSKonstantin Belousov 				bi_m = bi;
303af582aaeSKonstantin Belousov 				ret = 0;
304af582aaeSKonstantin Belousov 			}
30532c01de2SDmitry Chagin 			if (ret)
30632c01de2SDmitry Chagin 				return (bi);
30732c01de2SDmitry Chagin 		}
30832c01de2SDmitry Chagin 	}
309af582aaeSKonstantin Belousov 	if (bi_m != NULL)
310af582aaeSKonstantin Belousov 		return (bi_m);
31132c01de2SDmitry Chagin 
3125fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
3135fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3145fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
3151438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
3162274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
317ecc2fda8SBjoern A. Zeeb 			continue;
318ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
3195fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
3200fe98320SEd Schouten 		    (bi->compat_3_brand != NULL &&
3213d560b4bSKonstantin Belousov 		    strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
3220fe98320SEd Schouten 		    bi->compat_3_brand) == 0))) {
323686d2f31SNathan Whitehorn 			/* Looks good, but give brand a chance to veto */
32415a9aedfSKonstantin Belousov 			if (!bi->header_supported ||
32515a9aedfSKonstantin Belousov 			    bi->header_supported(imgp)) {
32615a9aedfSKonstantin Belousov 				/*
32715a9aedfSKonstantin Belousov 				 * Again, prefer strictly matching
32815a9aedfSKonstantin Belousov 				 * interpreter path.
32915a9aedfSKonstantin Belousov 				 */
3307aab7a80SKonstantin Belousov 				if (interp_name_len == 0 &&
3317aab7a80SKonstantin Belousov 				    bi->interp_path == NULL)
3327aab7a80SKonstantin Belousov 					return (bi);
3337aab7a80SKonstantin Belousov 				if (bi->interp_path != NULL &&
3347aab7a80SKonstantin Belousov 				    strlen(bi->interp_path) + 1 ==
33515a9aedfSKonstantin Belousov 				    interp_name_len && strncmp(interp,
33615a9aedfSKonstantin Belousov 				    bi->interp_path, interp_name_len) == 0)
3375fe3ed62SJake Burkholder 					return (bi);
33815a9aedfSKonstantin Belousov 				if (bi_m == NULL)
33915a9aedfSKonstantin Belousov 					bi_m = bi;
3405fe3ed62SJake Burkholder 			}
341686d2f31SNathan Whitehorn 		}
34215a9aedfSKonstantin Belousov 	}
34315a9aedfSKonstantin Belousov 	if (bi_m != NULL)
34415a9aedfSKonstantin Belousov 		return (bi_m);
3455fe3ed62SJake Burkholder 
346817dc004SWarner Losh 	/* No known brand, see if the header is recognized by any brand */
347817dc004SWarner Losh 	for (i = 0; i < MAX_BRANDS; i++) {
348817dc004SWarner Losh 		bi = elf_brand_list[i];
349817dc004SWarner Losh 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
350817dc004SWarner Losh 		    bi->header_supported == NULL)
351817dc004SWarner Losh 			continue;
352817dc004SWarner Losh 		if (hdr->e_machine == bi->machine) {
353817dc004SWarner Losh 			ret = bi->header_supported(imgp);
354817dc004SWarner Losh 			if (ret)
355817dc004SWarner Losh 				return (bi);
356817dc004SWarner Losh 		}
357817dc004SWarner Losh 	}
358817dc004SWarner Losh 
3595fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
3605fe3ed62SJake Burkholder 	if (interp != NULL) {
3615fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
3625fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
3632274ab3dSKonstantin Belousov 			if (bi == NULL || (bi->flags &
3642274ab3dSKonstantin Belousov 			    (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC))
3652274ab3dSKonstantin Belousov 			    != 0)
366ecc2fda8SBjoern A. Zeeb 				continue;
367ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
3683aeacc55SKonstantin Belousov 			    bi->interp_path != NULL &&
369d1ae5c83SKonstantin Belousov 			    /* ELF image p_filesz includes terminating zero */
370d1ae5c83SKonstantin Belousov 			    strlen(bi->interp_path) + 1 == interp_name_len &&
371d1ae5c83SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
372d1ae5c83SKonstantin Belousov 			    == 0)
3735fe3ed62SJake Burkholder 				return (bi);
3745fe3ed62SJake Burkholder 		}
3755fe3ed62SJake Burkholder 	}
3765fe3ed62SJake Burkholder 
3775fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
3785fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3795fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
3801438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
3812274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
382ecc2fda8SBjoern A. Zeeb 			continue;
383ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
384e548a1d4SJake Burkholder 		    __elfN(fallback_brand) == bi->brand)
3855fe3ed62SJake Burkholder 			return (bi);
3865fe3ed62SJake Burkholder 	}
3875fe3ed62SJake Burkholder 	return (NULL);
3885fe3ed62SJake Burkholder }
3895fe3ed62SJake Burkholder 
390e1743d02SSøren Schmidt static int
3913ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
392e1743d02SSøren Schmidt {
393d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
3943ebc1248SPeter Wemm 	int i;
3953ebc1248SPeter Wemm 
39652c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
39752c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
39852c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
3993dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
4003dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
4013dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
402a7cddfedSJake Burkholder 		return (ENOEXEC);
403e1743d02SSøren Schmidt 
4043ebc1248SPeter Wemm 	/*
4053ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
4063ebc1248SPeter Wemm 	 */
4073ebc1248SPeter Wemm 
4083ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
409d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
410d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
4113ebc1248SPeter Wemm 			break;
4123ebc1248SPeter Wemm 	}
4133ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
414a7cddfedSJake Burkholder 		return (ENOEXEC);
415e1743d02SSøren Schmidt 
416a7cddfedSJake Burkholder 	return (0);
417e1743d02SSøren Schmidt }
418e1743d02SSøren Schmidt 
419e1743d02SSøren Schmidt static int
4203ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
421ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
4223ebc1248SPeter Wemm {
423da61b9a6SAlan Cox 	struct sf_buf *sf;
424da61b9a6SAlan Cox 	int error;
4253ebc1248SPeter Wemm 	vm_offset_t off;
4263ebc1248SPeter Wemm 
4273ebc1248SPeter Wemm 	/*
4283ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
4293ebc1248SPeter Wemm 	 */
430aaadc41fSKonstantin Belousov 	vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) -
431aaadc41fSKonstantin Belousov 	    trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL);
4323ebc1248SPeter Wemm 
4333ebc1248SPeter Wemm 	/*
4343ebc1248SPeter Wemm 	 * Find the page from the underlying object.
4353ebc1248SPeter Wemm 	 */
43628e8da65SAlan Cox 	if (object != NULL) {
437da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
438da61b9a6SAlan Cox 		if (sf == NULL)
439da61b9a6SAlan Cox 			return (KERN_FAILURE);
4403ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
441da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
442ca0387efSJake Burkholder 		    end - start);
443be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
444fe0a8a39SKonstantin Belousov 		if (error != 0)
445a7cddfedSJake Burkholder 			return (KERN_FAILURE);
4463ebc1248SPeter Wemm 	}
4473ebc1248SPeter Wemm 
448a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
4493ebc1248SPeter Wemm }
4503ebc1248SPeter Wemm 
4513ebc1248SPeter Wemm static int
452e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object,
453e3d8f8feSKonstantin Belousov     vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot,
454e3d8f8feSKonstantin Belousov     int cow)
4553ebc1248SPeter Wemm {
456da61b9a6SAlan Cox 	struct sf_buf *sf;
457da61b9a6SAlan Cox 	vm_offset_t off;
458a063facbSMarcel Moolenaar 	vm_size_t sz;
459e3d8f8feSKonstantin Belousov 	int error, locked, rv;
4603ebc1248SPeter Wemm 
4613ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
46281f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
463ff6f03c7SAlan Cox 		    round_page(start), prot);
46428e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
465a7cddfedSJake Burkholder 			return (rv);
4663ebc1248SPeter Wemm 		offset += round_page(start) - start;
4673ebc1248SPeter Wemm 		start = round_page(start);
4683ebc1248SPeter Wemm 	}
4693ebc1248SPeter Wemm 	if (end != round_page(end)) {
47081f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
471ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
47228e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
473a7cddfedSJake Burkholder 			return (rv);
4743ebc1248SPeter Wemm 		end = trunc_page(end);
4753ebc1248SPeter Wemm 	}
476e383e820SAlan Cox 	if (start >= end)
477e383e820SAlan Cox 		return (KERN_SUCCESS);
478e383e820SAlan Cox 	if ((offset & PAGE_MASK) != 0) {
4793ebc1248SPeter Wemm 		/*
480e383e820SAlan Cox 		 * The mapping is not page aligned.  This means that we have
481e383e820SAlan Cox 		 * to copy the data.
4823ebc1248SPeter Wemm 		 */
483aaadc41fSKonstantin Belousov 		rv = vm_map_fixed(map, NULL, 0, start, end - start,
484aaadc41fSKonstantin Belousov 		    prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL);
4855420f76bSKonstantin Belousov 		if (rv != KERN_SUCCESS)
486a7cddfedSJake Burkholder 			return (rv);
487da61b9a6SAlan Cox 		if (object == NULL)
488da61b9a6SAlan Cox 			return (KERN_SUCCESS);
489da61b9a6SAlan Cox 		for (; start < end; start += sz) {
490da61b9a6SAlan Cox 			sf = vm_imgact_map_page(object, offset);
491da61b9a6SAlan Cox 			if (sf == NULL)
492da61b9a6SAlan Cox 				return (KERN_FAILURE);
4933ebc1248SPeter Wemm 			off = offset - trunc_page(offset);
4943ebc1248SPeter Wemm 			sz = end - start;
495da61b9a6SAlan Cox 			if (sz > PAGE_SIZE - off)
496da61b9a6SAlan Cox 				sz = PAGE_SIZE - off;
497da61b9a6SAlan Cox 			error = copyout((caddr_t)sf_buf_kva(sf) + off,
4983ebc1248SPeter Wemm 			    (caddr_t)start, sz);
499be996836SAttilio Rao 			vm_imgact_unmap_page(sf);
5005420f76bSKonstantin Belousov 			if (error != 0)
501a7cddfedSJake Burkholder 				return (KERN_FAILURE);
502da61b9a6SAlan Cox 			offset += sz;
5033ebc1248SPeter Wemm 		}
5043ebc1248SPeter Wemm 	} else {
505e5e6093bSAlan Cox 		vm_object_reference(object);
506e383e820SAlan Cox 		rv = vm_map_fixed(map, object, offset, start, end - start,
507e383e820SAlan Cox 		    prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL);
508e3d8f8feSKonstantin Belousov 		if (rv != KERN_SUCCESS) {
509e3d8f8feSKonstantin Belousov 			locked = VOP_ISLOCKED(imgp->vp);
510e3d8f8feSKonstantin Belousov 			VOP_UNLOCK(imgp->vp, 0);
511e5e6093bSAlan Cox 			vm_object_deallocate(object);
512e3d8f8feSKonstantin Belousov 			vn_lock(imgp->vp, locked | LK_RETRY);
513a7cddfedSJake Burkholder 			return (rv);
5143ebc1248SPeter Wemm 		}
5153ebc1248SPeter Wemm 	}
516e383e820SAlan Cox 	return (KERN_SUCCESS);
517e383e820SAlan Cox }
5183ebc1248SPeter Wemm 
5193ebc1248SPeter Wemm static int
5200bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
5213ebc1248SPeter Wemm     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
5223ebc1248SPeter Wemm     size_t pagesize)
523e1743d02SSøren Schmidt {
524da61b9a6SAlan Cox 	struct sf_buf *sf;
525e1743d02SSøren Schmidt 	size_t map_len;
526292177e6SAlan Cox 	vm_map_t map;
527292177e6SAlan Cox 	vm_object_t object;
528973d67c4SKonstantin Belousov 	vm_offset_t off, map_addr;
529fa7dd9c5SMatthew Dillon 	int error, rv, cow;
530e1743d02SSøren Schmidt 	size_t copy_len;
5310bbee4cdSKonstantin Belousov 	vm_ooffset_t file_addr;
53252c24af7SPeter Wemm 
53325ead034SBrian Feldman 	/*
53425ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
53525ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
53625ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
53725ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
53825ead034SBrian Feldman 	 *
53925ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
54025ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
54125ead034SBrian Feldman 	 */
5429bcf2f2dSKonstantin Belousov 	if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) ||
5439bcf2f2dSKonstantin Belousov 	    filsz > memsz) {
54425ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
54525ead034SBrian Feldman 		return (ENOEXEC);
54625ead034SBrian Feldman 	}
54725ead034SBrian Feldman 
548292177e6SAlan Cox 	object = imgp->object;
549292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
5503ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
5513ebc1248SPeter Wemm 	file_addr = trunc_page_ps(offset, pagesize);
552e1743d02SSøren Schmidt 
553e1743d02SSøren Schmidt 	/*
55452c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
55552c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
55652c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
55728e8da65SAlan Cox 	 * choose the second.
55852c24af7SPeter Wemm 	 */
5599bcf2f2dSKonstantin Belousov 	if (filsz == 0)
5609bcf2f2dSKonstantin Belousov 		map_len = 0;
5619bcf2f2dSKonstantin Belousov 	else if (memsz > filsz)
5623ebc1248SPeter Wemm 		map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
56352c24af7SPeter Wemm 	else
5643ebc1248SPeter Wemm 		map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
56552c24af7SPeter Wemm 
56652c24af7SPeter Wemm 	if (map_len != 0) {
567fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
568fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
569fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
570fa7dd9c5SMatthew Dillon 
571e3d8f8feSKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map,
57252c24af7SPeter Wemm 				      object,
57352c24af7SPeter Wemm 				      file_addr,	/* file offset */
57452c24af7SPeter Wemm 				      map_addr,		/* virtual start */
57552c24af7SPeter Wemm 				      map_addr + map_len,/* virtual end */
57652c24af7SPeter Wemm 				      prot,
577fa7dd9c5SMatthew Dillon 				      cow);
578e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
579a7cddfedSJake Burkholder 			return (EINVAL);
58052c24af7SPeter Wemm 
58152c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
582973d67c4SKonstantin Belousov 		if (memsz == filsz)
583a7cddfedSJake Burkholder 			return (0);
58452c24af7SPeter Wemm 	}
58552c24af7SPeter Wemm 
58652c24af7SPeter Wemm 
58752c24af7SPeter Wemm 	/*
58852c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
58952c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
59052c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
59152c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
592e1743d02SSøren Schmidt 	 */
5939bcf2f2dSKonstantin Belousov 	copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page_ps(offset +
5949bcf2f2dSKonstantin Belousov 	    filsz, pagesize);
5953ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
596ca0387efSJake Burkholder 	map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
597ca0387efSJake Burkholder 	    map_addr;
598e1743d02SSøren Schmidt 
59952c24af7SPeter Wemm 	/* This had damn well better be true! */
6008191d577SPeter Wemm 	if (map_len != 0) {
601e3d8f8feSKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr,
602c547cbb4SAlan Cox 		    map_addr + map_len, prot, 0);
603973d67c4SKonstantin Belousov 		if (rv != KERN_SUCCESS)
604a7cddfedSJake Burkholder 			return (EINVAL);
6058191d577SPeter Wemm 	}
606e1743d02SSøren Schmidt 
60752c24af7SPeter Wemm 	if (copy_len != 0) {
608da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
609da61b9a6SAlan Cox 		if (sf == NULL)
610da61b9a6SAlan Cox 			return (EIO);
611e1743d02SSøren Schmidt 
61252c24af7SPeter Wemm 		/* send the page fragment to user space */
61381f223caSJake Burkholder 		off = trunc_page_ps(offset + filsz, pagesize) -
61481f223caSJake Burkholder 		    trunc_page(offset + filsz);
615da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off,
616da61b9a6SAlan Cox 		    (caddr_t)map_addr, copy_len);
617be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
618973d67c4SKonstantin Belousov 		if (error != 0)
61952c24af7SPeter Wemm 			return (error);
62052c24af7SPeter Wemm 	}
621e1743d02SSøren Schmidt 
622e1743d02SSøren Schmidt 	/*
623c547cbb4SAlan Cox 	 * Remove write access to the page if it was only granted by map_insert
624c547cbb4SAlan Cox 	 * to allow copyout.
625e1743d02SSøren Schmidt 	 */
626c547cbb4SAlan Cox 	if ((prot & VM_PROT_WRITE) == 0)
627292177e6SAlan Cox 		vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
628292177e6SAlan Cox 		    map_len), prot, FALSE);
6298191d577SPeter Wemm 
630ff6f03c7SAlan Cox 	return (0);
631e1743d02SSøren Schmidt }
632e1743d02SSøren Schmidt 
633c33fe779SJohn Polstra /*
634c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
635c33fe779SJohn Polstra  * or an executable.
636c33fe779SJohn Polstra  *
637c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
638c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
639c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
640c33fe779SJohn Polstra  * where the file was actually loaded.
641c33fe779SJohn Polstra  *
642c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
643c33fe779SJohn Polstra  * the entry point for the loaded file.
644c33fe779SJohn Polstra  */
645e1743d02SSøren Schmidt static int
6463ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
6473ebc1248SPeter Wemm 	u_long *entry, size_t pagesize)
648e1743d02SSøren Schmidt {
649911c2be0SMark Peek 	struct {
650911c2be0SMark Peek 		struct nameidata nd;
651911c2be0SMark Peek 		struct vattr attr;
652911c2be0SMark Peek 		struct image_params image_params;
653911c2be0SMark Peek 	} *tempdata;
654d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
655d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
656911c2be0SMark Peek 	struct nameidata *nd;
657911c2be0SMark Peek 	struct vattr *attr;
658911c2be0SMark Peek 	struct image_params *imgp;
65952c24af7SPeter Wemm 	vm_prot_t prot;
660c33fe779SJohn Polstra 	u_long rbase;
661c33fe779SJohn Polstra 	u_long base_addr = 0;
6625050aa86SKonstantin Belousov 	int error, i, numsegs;
663e1743d02SSøren Schmidt 
66412bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
66512bc222eSJonathan Anderson 	/*
66612bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
66712bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
66812bc222eSJonathan Anderson 	 */
66912bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
67012bc222eSJonathan Anderson 		return (ECAPMODE);
67112bc222eSJonathan Anderson #endif
67212bc222eSJonathan Anderson 
673a163d034SWarner Losh 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
674911c2be0SMark Peek 	nd = &tempdata->nd;
675911c2be0SMark Peek 	attr = &tempdata->attr;
676911c2be0SMark Peek 	imgp = &tempdata->image_params;
677911c2be0SMark Peek 
678c8a79999SPeter Wemm 	/*
679c8a79999SPeter Wemm 	 * Initialize part of the common data
680c8a79999SPeter Wemm 	 */
681c8a79999SPeter Wemm 	imgp->proc = p;
682911c2be0SMark Peek 	imgp->attr = attr;
683c8a79999SPeter Wemm 	imgp->firstpage = NULL;
68459c8bc40SAlan Cox 	imgp->image_header = NULL;
6850b2ed1aeSJeff Roberson 	imgp->object = NULL;
6866d7bdc8dSRobert Watson 	imgp->execlabel = NULL;
687c8a79999SPeter Wemm 
6885050aa86SKonstantin Belousov 	NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
689911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
690911c2be0SMark Peek 		nd->ni_vp = NULL;
691e1743d02SSøren Schmidt 		goto fail;
692e1743d02SSøren Schmidt 	}
693911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
694911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
695c8a79999SPeter Wemm 
696e1743d02SSøren Schmidt 	/*
697e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
698e1743d02SSøren Schmidt 	 */
699c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
700373d1a3fSAlan Cox 	if (error)
701c8a79999SPeter Wemm 		goto fail;
702e1743d02SSøren Schmidt 
703c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
704373d1a3fSAlan Cox 	if (error)
705373d1a3fSAlan Cox 		goto fail;
706373d1a3fSAlan Cox 
70725ead034SBrian Feldman 	/*
70825ead034SBrian Feldman 	 * Also make certain that the interpreter stays the same, so set
709e6e370a7SJeff Roberson 	 * its VV_TEXT flag, too.
71025ead034SBrian Feldman 	 */
711877d24acSKonstantin Belousov 	VOP_SET_TEXT(nd->ni_vp);
712e6e370a7SJeff Roberson 
7138516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
714e1743d02SSøren Schmidt 
715d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
7163ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
717e1743d02SSøren Schmidt 		goto fail;
718c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
719c33fe779SJohn Polstra 		rbase = *addr;
720c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
721c33fe779SJohn Polstra 		rbase = 0;
722c33fe779SJohn Polstra 	else {
723c33fe779SJohn Polstra 		error = ENOEXEC;
724c33fe779SJohn Polstra 		goto fail;
725c33fe779SJohn Polstra 	}
726e1743d02SSøren Schmidt 
727c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
72852c24af7SPeter Wemm 	if ((hdr->e_phoff > PAGE_SIZE) ||
729d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
730c8a79999SPeter Wemm 		error = ENOEXEC;
731e1743d02SSøren Schmidt 		goto fail;
732c8a79999SPeter Wemm 	}
733c8a79999SPeter Wemm 
734d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
73593d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
73693d1c728SKonstantin Belousov 		error = ENOEXEC;
73793d1c728SKonstantin Belousov 		goto fail;
73893d1c728SKonstantin Belousov 	}
739e1743d02SSøren Schmidt 
740c33fe779SJohn Polstra 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
7415b33842aSKonstantin Belousov 		if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
7425b33842aSKonstantin Belousov 			/* Loadable segment */
743ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
744292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
74581f223caSJake Burkholder 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
746292177e6SAlan Cox 			    phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
747292177e6SAlan Cox 			if (error != 0)
748e1743d02SSøren Schmidt 				goto fail;
749e1743d02SSøren Schmidt 			/*
750c33fe779SJohn Polstra 			 * Establish the base address if this is the
751c33fe779SJohn Polstra 			 * first segment.
752e1743d02SSøren Schmidt 			 */
753c33fe779SJohn Polstra 			if (numsegs == 0)
754ca0387efSJake Burkholder   				base_addr = trunc_page(phdr[i].p_vaddr +
755ca0387efSJake Burkholder 				    rbase);
756c33fe779SJohn Polstra 			numsegs++;
757e1743d02SSøren Schmidt 		}
758e1743d02SSøren Schmidt 	}
759c33fe779SJohn Polstra 	*addr = base_addr;
760c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
761e1743d02SSøren Schmidt 
762e1743d02SSøren Schmidt fail:
763c8a79999SPeter Wemm 	if (imgp->firstpage)
764c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
7650b2ed1aeSJeff Roberson 
766911c2be0SMark Peek 	if (nd->ni_vp)
767373d1a3fSAlan Cox 		vput(nd->ni_vp);
768911c2be0SMark Peek 
769911c2be0SMark Peek 	free(tempdata, M_TEMP);
770e1743d02SSøren Schmidt 
771a7cddfedSJake Burkholder 	return (error);
772e1743d02SSøren Schmidt }
773e1743d02SSøren Schmidt 
774303b270bSEivind Eklund static int
7753ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
776e1743d02SSøren Schmidt {
7776c775eb6SKonstantin Belousov 	struct thread *td;
7786c775eb6SKonstantin Belousov 	const Elf_Ehdr *hdr;
77932c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
780e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
7815856e12eSJohn Dyson 	struct vmspace *vmspace;
7826c775eb6SKonstantin Belousov 	const char *err_str, *newinterp;
7836c775eb6SKonstantin Belousov 	char *interp, *interp_buf, *path;
784d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
7855fe3ed62SJake Burkholder 	struct sysentvec *sv;
7866c775eb6SKonstantin Belousov 	vm_prot_t prot;
7876c775eb6SKonstantin Belousov 	u_long text_size, data_size, total_size, text_addr, data_addr;
7886c775eb6SKonstantin Belousov 	u_long seg_size, seg_addr, addr, baddr, et_dyn_addr, entry, proghdr;
7896c775eb6SKonstantin Belousov 	int32_t osrel;
7906c775eb6SKonstantin Belousov 	int error, i, n, interp_name_len, have_interp;
7916c775eb6SKonstantin Belousov 
7926c775eb6SKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
793e1743d02SSøren Schmidt 
794e1743d02SSøren Schmidt 	/*
795e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
796900b28f9SMaxim Sobolev 	 *
797900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
798900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
799e1743d02SSøren Schmidt 	 */
800900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
801900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
802a7cddfedSJake Burkholder 		return (-1);
803e1743d02SSøren Schmidt 
804e1743d02SSøren Schmidt 	/*
805e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
806e1743d02SSøren Schmidt 	 * detected an ELF file.
807e1743d02SSøren Schmidt 	 */
808e1743d02SSøren Schmidt 
809e1743d02SSøren Schmidt 	if ((hdr->e_phoff > PAGE_SIZE) ||
810d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
811c8a79999SPeter Wemm 		/* Only support headers in first page for now */
8126b16d664SEd Maste 		uprintf("Program headers not in the first page\n");
813a7cddfedSJake Burkholder 		return (ENOEXEC);
814e1743d02SSøren Schmidt 	}
81552c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
8166b16d664SEd Maste 	if (!aligned(phdr, Elf_Addr)) {
8176b16d664SEd Maste 		uprintf("Unaligned program headers\n");
81893d1c728SKonstantin Belousov 		return (ENOEXEC);
8196b16d664SEd Maste 	}
8206c775eb6SKonstantin Belousov 
8216c775eb6SKonstantin Belousov 	n = error = 0;
8227564c4adSKonstantin Belousov 	baddr = 0;
8236c775eb6SKonstantin Belousov 	osrel = 0;
8246c775eb6SKonstantin Belousov 	text_size = data_size = total_size = text_addr = data_addr = 0;
8256c775eb6SKonstantin Belousov 	entry = proghdr = 0;
8266c775eb6SKonstantin Belousov 	interp_name_len = 0;
8276c775eb6SKonstantin Belousov 	err_str = newinterp = NULL;
8286c775eb6SKonstantin Belousov 	interp = interp_buf = NULL;
8296c775eb6SKonstantin Belousov 	td = curthread;
8306c775eb6SKonstantin Belousov 
8315fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
832291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
833291c06a1SKonstantin Belousov 		case PT_LOAD:
8347564c4adSKonstantin Belousov 			if (n == 0)
8357564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
8367564c4adSKonstantin Belousov 			n++;
837291c06a1SKonstantin Belousov 			break;
838291c06a1SKonstantin Belousov 		case PT_INTERP:
839e5e6093bSAlan Cox 			/* Path to interpreter */
8406c775eb6SKonstantin Belousov 			if (phdr[i].p_filesz > MAXPATHLEN) {
8416b16d664SEd Maste 				uprintf("Invalid PT_INTERP\n");
8426c775eb6SKonstantin Belousov 				error = ENOEXEC;
8436c775eb6SKonstantin Belousov 				goto ret;
8446b16d664SEd Maste 			}
845d3ee0a15SJonathan T. Looney 			if (interp != NULL) {
846d3ee0a15SJonathan T. Looney 				uprintf("Multiple PT_INTERP headers\n");
847d3ee0a15SJonathan T. Looney 				error = ENOEXEC;
848d3ee0a15SJonathan T. Looney 				goto ret;
849d3ee0a15SJonathan T. Looney 			}
850d1ae5c83SKonstantin Belousov 			interp_name_len = phdr[i].p_filesz;
8516c775eb6SKonstantin Belousov 			if (phdr[i].p_offset > PAGE_SIZE ||
8526c775eb6SKonstantin Belousov 			    interp_name_len > PAGE_SIZE - phdr[i].p_offset) {
8536c775eb6SKonstantin Belousov 				VOP_UNLOCK(imgp->vp, 0);
8546c775eb6SKonstantin Belousov 				interp_buf = malloc(interp_name_len + 1, M_TEMP,
8556c775eb6SKonstantin Belousov 				    M_WAITOK);
8566c775eb6SKonstantin Belousov 				vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
8576c775eb6SKonstantin Belousov 				error = vn_rdwr(UIO_READ, imgp->vp, interp_buf,
8586c775eb6SKonstantin Belousov 				    interp_name_len, phdr[i].p_offset,
8596c775eb6SKonstantin Belousov 				    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
8606c775eb6SKonstantin Belousov 				    NOCRED, NULL, td);
8616c775eb6SKonstantin Belousov 				if (error != 0) {
8626c775eb6SKonstantin Belousov 					uprintf("i/o error PT_INTERP\n");
8636c775eb6SKonstantin Belousov 					goto ret;
8646c775eb6SKonstantin Belousov 				}
8656c775eb6SKonstantin Belousov 				interp_buf[interp_name_len] = '\0';
8666c775eb6SKonstantin Belousov 				interp = interp_buf;
8676c775eb6SKonstantin Belousov 			} else {
8686c775eb6SKonstantin Belousov 				interp = __DECONST(char *, imgp->image_header) +
8696c775eb6SKonstantin Belousov 				    phdr[i].p_offset;
8706c775eb6SKonstantin Belousov 			}
871291c06a1SKonstantin Belousov 			break;
872291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
873291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
874291c06a1SKonstantin Belousov 				imgp->stack_prot =
875291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
876316b3843SKonstantin Belousov 			imgp->stack_sz = phdr[i].p_memsz;
877291c06a1SKonstantin Belousov 			break;
8783ebc1248SPeter Wemm 		}
8793ebc1248SPeter Wemm 	}
8803ebc1248SPeter Wemm 
881d1ae5c83SKonstantin Belousov 	brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
882d1ae5c83SKonstantin Belousov 	    &osrel);
8835fe3ed62SJake Burkholder 	if (brand_info == NULL) {
8845fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
8855fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
8866c775eb6SKonstantin Belousov 		error = ENOEXEC;
8876c775eb6SKonstantin Belousov 		goto ret;
8883ebc1248SPeter Wemm 	}
88977ebe276SEd Maste 	et_dyn_addr = 0;
890ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
8916b16d664SEd Maste 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
8926b16d664SEd Maste 			uprintf("Cannot execute shared object\n");
8936c775eb6SKonstantin Belousov 			error = ENOEXEC;
8946c775eb6SKonstantin Belousov 			goto ret;
8956b16d664SEd Maste 		}
8967564c4adSKonstantin Belousov 		/*
8977564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
8987564c4adSKonstantin Belousov 		 * non-zero for some reason.
8997564c4adSKonstantin Belousov 		 */
9007564c4adSKonstantin Belousov 		if (baddr == 0)
901ab02d85fSKonstantin Belousov 			et_dyn_addr = ET_DYN_LOAD_ADDR;
90277ebe276SEd Maste 	}
9035fe3ed62SJake Burkholder 	sv = brand_info->sysvec;
9049b68618dSPeter Wemm 	if (interp != NULL && brand_info->interp_newpath != NULL)
9054113f8d7SPeter Wemm 		newinterp = brand_info->interp_newpath;
9063ebc1248SPeter Wemm 
90760bb3943SAlan Cox 	/*
90860bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
90960bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
91060bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
91160bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
91260bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
91360bb3943SAlan Cox 	 * v_usecount may become zero.
9141dfab802SAlan Cox 	 *
9151dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
9161dfab802SAlan Cox 	 * the vnode is unlocked.
91760bb3943SAlan Cox 	 */
91822db15c0SAttilio Rao 	VOP_UNLOCK(imgp->vp, 0);
91960bb3943SAlan Cox 
92089b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
92119059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
922e1743d02SSøren Schmidt 
923cb05b60aSAttilio Rao 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
9246c775eb6SKonstantin Belousov 	if (error != 0)
9256c775eb6SKonstantin Belousov 		goto ret;
92660bb3943SAlan Cox 
927e1743d02SSøren Schmidt 	for (i = 0; i < hdr->e_phnum; i++) {
928e1743d02SSøren Schmidt 		switch (phdr[i].p_type) {
929e1743d02SSøren Schmidt 		case PT_LOAD:	/* Loadable segment */
9305b33842aSKonstantin Belousov 			if (phdr[i].p_memsz == 0)
9315b33842aSKonstantin Belousov 				break;
932ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
933292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
934ab02d85fSKonstantin Belousov 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
93581f223caSJake Burkholder 			    phdr[i].p_memsz, phdr[i].p_filesz, prot,
936292177e6SAlan Cox 			    sv->sv_pagesize);
937292177e6SAlan Cox 			if (error != 0)
9386c775eb6SKonstantin Belousov 				goto ret;
939e1743d02SSøren Schmidt 
940cfaf7e60SDoug Rabson 			/*
941cfaf7e60SDoug Rabson 			 * If this segment contains the program headers,
942cfaf7e60SDoug Rabson 			 * remember their virtual address for the AT_PHDR
943cfaf7e60SDoug Rabson 			 * aux entry. Static binaries don't usually include
944cfaf7e60SDoug Rabson 			 * a PT_PHDR entry.
945cfaf7e60SDoug Rabson 			 */
946cfaf7e60SDoug Rabson 			if (phdr[i].p_offset == 0 &&
947cfaf7e60SDoug Rabson 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
948cfaf7e60SDoug Rabson 				<= phdr[i].p_filesz)
949ab02d85fSKonstantin Belousov 				proghdr = phdr[i].p_vaddr + hdr->e_phoff +
950ab02d85fSKonstantin Belousov 				    et_dyn_addr;
951cfaf7e60SDoug Rabson 
952ab02d85fSKonstantin Belousov 			seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
953cac45152SMatthew Dillon 			seg_size = round_page(phdr[i].p_memsz +
954ab02d85fSKonstantin Belousov 			    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
955cac45152SMatthew Dillon 
956e1743d02SSøren Schmidt 			/*
957920acedbSNathan Whitehorn 			 * Make the largest executable segment the official
958920acedbSNathan Whitehorn 			 * text segment and all others data.
95921c2d047SMatthew Dillon 			 *
96021c2d047SMatthew Dillon 			 * Note that obreak() assumes that data_addr +
96121c2d047SMatthew Dillon 			 * data_size == end of data load area, and the ELF
96221c2d047SMatthew Dillon 			 * file format expects segments to be sorted by
96321c2d047SMatthew Dillon 			 * address.  If multiple data segments exist, the
96421c2d047SMatthew Dillon 			 * last one will be used.
965e1743d02SSøren Schmidt 			 */
966920acedbSNathan Whitehorn 
967920acedbSNathan Whitehorn 			if (phdr[i].p_flags & PF_X && text_size < seg_size) {
9689782ecbaSPeter Wemm 				text_size = seg_size;
9699782ecbaSPeter Wemm 				text_addr = seg_addr;
9709782ecbaSPeter Wemm 			} else {
97121c2d047SMatthew Dillon 				data_size = seg_size;
972cac45152SMatthew Dillon 				data_addr = seg_addr;
973cac45152SMatthew Dillon 			}
97421c2d047SMatthew Dillon 			total_size += seg_size;
97596725dd0SAlexander Kabaev 			break;
97696725dd0SAlexander Kabaev 		case PT_PHDR: 	/* Program header table info */
977ab02d85fSKonstantin Belousov 			proghdr = phdr[i].p_vaddr + et_dyn_addr;
97896725dd0SAlexander Kabaev 			break;
97996725dd0SAlexander Kabaev 		default:
98096725dd0SAlexander Kabaev 			break;
98196725dd0SAlexander Kabaev 		}
98296725dd0SAlexander Kabaev 	}
98396725dd0SAlexander Kabaev 
98496725dd0SAlexander Kabaev 	if (data_addr == 0 && data_size == 0) {
98596725dd0SAlexander Kabaev 		data_addr = text_addr;
98696725dd0SAlexander Kabaev 		data_size = text_size;
98796725dd0SAlexander Kabaev 	}
988cac45152SMatthew Dillon 
989920acedbSNathan Whitehorn 	entry = (u_long)hdr->e_entry + et_dyn_addr;
990920acedbSNathan Whitehorn 
991cac45152SMatthew Dillon 	/*
992cac45152SMatthew Dillon 	 * Check limits.  It should be safe to check the
99396725dd0SAlexander Kabaev 	 * limits after loading the segments since we do
99496725dd0SAlexander Kabaev 	 * not actually fault in all the segments pages.
995cac45152SMatthew Dillon 	 */
99691d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
997f6f6d240SMateusz Guzik 	if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
9986b16d664SEd Maste 		err_str = "Data segment size exceeds process limit";
9996b16d664SEd Maste 	else if (text_size > maxtsiz)
10006b16d664SEd Maste 		err_str = "Text segment size exceeds system limit";
1001f6f6d240SMateusz Guzik 	else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
10026b16d664SEd Maste 		err_str = "Total segment size exceeds process limit";
10036b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
10046b16d664SEd Maste 		err_str = "Data segment size exceeds resource limit";
10056b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
10066b16d664SEd Maste 		err_str = "Total segment size exceeds resource limit";
10076b16d664SEd Maste 	if (err_str != NULL) {
100891d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
10096b16d664SEd Maste 		uprintf("%s\n", err_str);
10106c775eb6SKonstantin Belousov 		error = ENOMEM;
10116c775eb6SKonstantin Belousov 		goto ret;
1012cac45152SMatthew Dillon 	}
1013e1743d02SSøren Schmidt 
1014292177e6SAlan Cox 	vmspace = imgp->proc->p_vmspace;
1015e1743d02SSøren Schmidt 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
10167cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
1017e1743d02SSøren Schmidt 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
10187cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
1019e1743d02SSøren Schmidt 
1020c460ac3aSPeter Wemm 	/*
1021c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
1022c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
1023c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
1024c460ac3aSPeter Wemm 	 * its maximum allowed size.
1025c460ac3aSPeter Wemm 	 */
10266c775eb6SKonstantin Belousov 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td,
1027292177e6SAlan Cox 	    RLIMIT_DATA));
102891d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
1029e1743d02SSøren Schmidt 
1030ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
1031ea5a2b2eSSøren Schmidt 
103260bb3943SAlan Cox 	if (interp != NULL) {
10336c775eb6SKonstantin Belousov 		have_interp = FALSE;
103422db15c0SAttilio Rao 		VOP_UNLOCK(imgp->vp, 0);
103560bb3943SAlan Cox 		if (brand_info->emul_path != NULL &&
10369b68618dSPeter Wemm 		    brand_info->emul_path[0] != '\0') {
1037a163d034SWarner Losh 			path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
103860bb3943SAlan Cox 			snprintf(path, MAXPATHLEN, "%s%s",
103960bb3943SAlan Cox 			    brand_info->emul_path, interp);
10409b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, path, &addr,
10419b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
1042911c2be0SMark Peek 			free(path, M_TEMP);
10439b68618dSPeter Wemm 			if (error == 0)
10444113f8d7SPeter Wemm 				have_interp = TRUE;
10459b68618dSPeter Wemm 		}
104618995077SKonstantin Belousov 		if (!have_interp && newinterp != NULL &&
104718995077SKonstantin Belousov 		    (brand_info->interp_path == NULL ||
104818995077SKonstantin Belousov 		    strcmp(interp, brand_info->interp_path) == 0)) {
10494113f8d7SPeter Wemm 			error = __elfN(load_file)(imgp->proc, newinterp, &addr,
10504113f8d7SPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
1051387ad998SKonstantin Belousov 			if (error == 0)
10524113f8d7SPeter Wemm 				have_interp = TRUE;
10534113f8d7SPeter Wemm 		}
10544113f8d7SPeter Wemm 		if (!have_interp) {
10559b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, interp, &addr,
10569b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
105760bb3943SAlan Cox 		}
1058cb05b60aSAttilio Rao 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
10599b68618dSPeter Wemm 		if (error != 0) {
1060d943fa35SKonstantin Belousov 			uprintf("ELF interpreter %s not found, error %d\n",
1061d943fa35SKonstantin Belousov 			    interp, error);
10626c775eb6SKonstantin Belousov 			goto ret;
1063e1743d02SSøren Schmidt 		}
106495c807cfSRobert Watson 	} else
10657564c4adSKonstantin Belousov 		addr = et_dyn_addr;
1066ea5a2b2eSSøren Schmidt 
1067e1743d02SSøren Schmidt 	/*
1068e1743d02SSøren Schmidt 	 * Construct auxargs table (used by the fixup routine)
1069e1743d02SSøren Schmidt 	 */
1070a163d034SWarner Losh 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
1071e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
1072e1743d02SSøren Schmidt 	elf_auxargs->phdr = proghdr;
1073e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
1074e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
1075e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
1076e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
1077e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
1078e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
1079d36eec69SWarner Losh 	elf_auxargs->hdr_eflags = hdr->e_flags;
1080e1743d02SSøren Schmidt 
1081e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
1082e1743d02SSøren Schmidt 	imgp->interpreted = 0;
1083a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
108432c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
1085885f13dcSJohn Baldwin 	imgp->proc->p_elf_machine = hdr->e_machine;
1086885f13dcSJohn Baldwin 	imgp->proc->p_elf_flags = hdr->e_flags;
1087f231de47SKonstantin Belousov 
10886c775eb6SKonstantin Belousov ret:
10896c775eb6SKonstantin Belousov 	free(interp_buf, M_TEMP);
1090a7cddfedSJake Burkholder 	return (error);
1091e1743d02SSøren Schmidt }
1092e1743d02SSøren Schmidt 
1093a360a43dSJake Burkholder #define	suword __CONCAT(suword, __ELF_WORD_SIZE)
10943ebc1248SPeter Wemm 
10953ebc1248SPeter Wemm int
10963ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
1097e1743d02SSøren Schmidt {
1098ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
1099a360a43dSJake Burkholder 	Elf_Addr *base;
1100a360a43dSJake Burkholder 	Elf_Addr *pos;
1101e1743d02SSøren Schmidt 
1102a360a43dSJake Burkholder 	base = (Elf_Addr *)*stack_base;
1103610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
1104e1743d02SSøren Schmidt 
110535c2a5a8SWarner Losh 	if (args->execfd != -1)
1106e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1107e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1108e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1109e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1110e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1111e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1112e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1113e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
1114d36eec69SWarner Losh 	AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
11153ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
11163ff06357SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1117b96bd95bSIan Lepore 	AUXARGS_ENTRY(pos, AT_OSRELDATE,
1118b96bd95bSIan Lepore 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
1119ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1120ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1121ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1122ee235befSKonstantin Belousov 	}
1123ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1124ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1125ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1126ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1127ee235befSKonstantin Belousov 	}
1128aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1129aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1130aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1131aea81038SKonstantin Belousov 	}
113226d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
113326d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
113426d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1135c2f37b92SJohn Baldwin 	if (imgp->sysent->sv_hwcap != NULL)
1136c2f37b92SJohn Baldwin 		AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap);
1137904d8c49SMichal Meloun 	if (imgp->sysent->sv_hwcap2 != NULL)
1138904d8c49SMichal Meloun 		AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1139e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1140e1743d02SSøren Schmidt 
1141e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1142e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1143e1743d02SSøren Schmidt 
11443ebc1248SPeter Wemm 	base--;
1145610ecfe0SMaxim Sobolev 	suword(base, (long)imgp->args->argc);
11463ebc1248SPeter Wemm 	*stack_base = (register_t *)base;
1147a7cddfedSJake Burkholder 	return (0);
1148e1743d02SSøren Schmidt }
1149e1743d02SSøren Schmidt 
1150e1743d02SSøren Schmidt /*
11518c64af4fSJohn Polstra  * Code for generating ELF core dumps.
11528c64af4fSJohn Polstra  */
11538c64af4fSJohn Polstra 
11544d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
11550ff27d31SJohn Polstra 
11560ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
11570ff27d31SJohn Polstra struct phdr_closure {
11580ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
11590ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
11600ff27d31SJohn Polstra };
11610ff27d31SJohn Polstra 
11620ff27d31SJohn Polstra /* Closure for cb_size_segment(). */
11630ff27d31SJohn Polstra struct sseg_closure {
11640ff27d31SJohn Polstra 	int count;		/* Count of writable segments. */
11650ff27d31SJohn Polstra 	size_t size;		/* Total size of all writable segments. */
11660ff27d31SJohn Polstra };
11670ff27d31SJohn Polstra 
1168bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1169bd390213SMikolaj Golub 
1170bd390213SMikolaj Golub struct note_info {
1171bd390213SMikolaj Golub 	int		type;		/* Note type. */
1172bd390213SMikolaj Golub 	outfunc_t 	outfunc; 	/* Output function. */
1173bd390213SMikolaj Golub 	void		*outarg;	/* Argument for the output function. */
1174bd390213SMikolaj Golub 	size_t		outsize;	/* Output size. */
1175bd390213SMikolaj Golub 	TAILQ_ENTRY(note_info) link;	/* Link to the next note info. */
1176bd390213SMikolaj Golub };
1177bd390213SMikolaj Golub 
1178bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info);
1179bd390213SMikolaj Golub 
1180aa14e9b7SMark Johnston /* Coredump output parameters. */
1181aa14e9b7SMark Johnston struct coredump_params {
1182aa14e9b7SMark Johnston 	off_t		offset;
1183aa14e9b7SMark Johnston 	struct ucred	*active_cred;
1184aa14e9b7SMark Johnston 	struct ucred	*file_cred;
1185aa14e9b7SMark Johnston 	struct thread	*td;
1186aa14e9b7SMark Johnston 	struct vnode	*vp;
1187*78f57a9cSMark Johnston 	struct compressor *comp;
1188aa14e9b7SMark Johnston };
1189aa14e9b7SMark Johnston 
1190*78f57a9cSMark Johnston extern int compress_user_cores;
1191*78f57a9cSMark Johnston extern int compress_user_cores_level;
1192*78f57a9cSMark Johnston 
11934d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
11944d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
1195c468ff88SAndriy Gapon static int core_write(struct coredump_params *, const void *, size_t, off_t,
1196aa14e9b7SMark Johnston     enum uio_seg);
11971005d8afSConrad Meyer static void each_dumpable_segment(struct thread *, segment_callback, void *);
1198aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1199aa14e9b7SMark Johnston     struct note_info_list *, size_t);
1200bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1201bd390213SMikolaj Golub     size_t *);
1202bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1203bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *);
1204bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1205bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int);
1206f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len);
1207bd390213SMikolaj Golub 
1208bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1209bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1210bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1211bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1212bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
121386be94fcSTycho Nightingale static void __elfN(note_ptlwpinfo)(void *, struct sbuf *, size_t *);
1214f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1215f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1216f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1217f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *);
1218f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *);
1219f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1220f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1221f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *);
1222f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
12238c64af4fSJohn Polstra 
1224aa14e9b7SMark Johnston /*
1225aa14e9b7SMark Johnston  * Write out a core segment to the compression stream.
1226aa14e9b7SMark Johnston  */
1227e7228204SAlfred Perlstein static int
1228aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
1229aa14e9b7SMark Johnston {
1230aa14e9b7SMark Johnston 	u_int chunk_len;
1231e7228204SAlfred Perlstein 	int error;
1232aa14e9b7SMark Johnston 
1233aa14e9b7SMark Johnston 	while (len > 0) {
1234aa14e9b7SMark Johnston 		chunk_len = MIN(len, CORE_BUF_SIZE);
1235c468ff88SAndriy Gapon 
1236c468ff88SAndriy Gapon 		/*
1237c468ff88SAndriy Gapon 		 * We can get EFAULT error here.
1238c468ff88SAndriy Gapon 		 * In that case zero out the current chunk of the segment.
1239c468ff88SAndriy Gapon 		 */
1240c468ff88SAndriy Gapon 		error = copyin(base, buf, chunk_len);
1241c468ff88SAndriy Gapon 		if (error != 0)
1242c468ff88SAndriy Gapon 			bzero(buf, chunk_len);
1243*78f57a9cSMark Johnston 		error = compressor_write(p->comp, buf, chunk_len);
1244aa14e9b7SMark Johnston 		if (error != 0)
1245aa14e9b7SMark Johnston 			break;
1246aa14e9b7SMark Johnston 		base += chunk_len;
1247aa14e9b7SMark Johnston 		len -= chunk_len;
1248e7228204SAlfred Perlstein 	}
1249e7228204SAlfred Perlstein 	return (error);
1250e7228204SAlfred Perlstein }
1251e7228204SAlfred Perlstein 
1252aa14e9b7SMark Johnston static int
1253*78f57a9cSMark Johnston core_compressed_write(void *base, size_t len, off_t offset, void *arg)
1254aa14e9b7SMark Johnston {
1255aa14e9b7SMark Johnston 
1256aa14e9b7SMark Johnston 	return (core_write((struct coredump_params *)arg, base, len, offset,
1257aa14e9b7SMark Johnston 	    UIO_SYSSPACE));
1258aa14e9b7SMark Johnston }
1259aa14e9b7SMark Johnston 
1260aa14e9b7SMark Johnston static int
1261c468ff88SAndriy Gapon core_write(struct coredump_params *p, const void *base, size_t len,
1262c468ff88SAndriy Gapon     off_t offset, enum uio_seg seg)
1263aa14e9b7SMark Johnston {
1264aa14e9b7SMark Johnston 
1265c468ff88SAndriy Gapon 	return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base),
1266c468ff88SAndriy Gapon 	    len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
1267aa14e9b7SMark Johnston 	    p->active_cred, p->file_cred, NULL, p->td));
1268aa14e9b7SMark Johnston }
1269aa14e9b7SMark Johnston 
1270aa14e9b7SMark Johnston static int
1271aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p,
1272aa14e9b7SMark Johnston     void *tmpbuf)
1273aa14e9b7SMark Johnston {
1274c468ff88SAndriy Gapon 	int error;
1275aa14e9b7SMark Johnston 
1276*78f57a9cSMark Johnston 	if (p->comp != NULL)
1277aa14e9b7SMark Johnston 		return (compress_chunk(p, base, tmpbuf, len));
1278*78f57a9cSMark Johnston 
1279c468ff88SAndriy Gapon 	/*
1280c468ff88SAndriy Gapon 	 * EFAULT is a non-fatal error that we can get, for example,
1281c468ff88SAndriy Gapon 	 * if the segment is backed by a file but extends beyond its
1282c468ff88SAndriy Gapon 	 * end.
1283c468ff88SAndriy Gapon 	 */
1284c468ff88SAndriy Gapon 	error = core_write(p, base, len, offset, UIO_USERSPACE);
1285c468ff88SAndriy Gapon 	if (error == EFAULT) {
1286c468ff88SAndriy Gapon 		log(LOG_WARNING, "Failed to fully fault in a core file segment "
1287c468ff88SAndriy Gapon 		    "at VA %p with size 0x%zx to be written at offset 0x%jx "
1288c468ff88SAndriy Gapon 		    "for process %s\n", base, len, offset, curproc->p_comm);
1289c468ff88SAndriy Gapon 
1290c468ff88SAndriy Gapon 		/*
1291c468ff88SAndriy Gapon 		 * Write a "real" zero byte at the end of the target region
1292c468ff88SAndriy Gapon 		 * in the case this is the last segment.
1293c468ff88SAndriy Gapon 		 * The intermediate space will be implicitly zero-filled.
1294c468ff88SAndriy Gapon 		 */
1295c468ff88SAndriy Gapon 		error = core_write(p, zero_region, 1, offset + len - 1,
1296c468ff88SAndriy Gapon 		    UIO_SYSSPACE);
1297c468ff88SAndriy Gapon 	}
1298c468ff88SAndriy Gapon 	return (error);
1299aa14e9b7SMark Johnston }
1300bd390213SMikolaj Golub 
1301bd390213SMikolaj Golub /*
1302bd390213SMikolaj Golub  * Drain into a core file.
1303bd390213SMikolaj Golub  */
1304bd390213SMikolaj Golub static int
1305bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len)
1306bd390213SMikolaj Golub {
1307aa14e9b7SMark Johnston 	struct coredump_params *p;
1308f1fca82eSMikolaj Golub 	int error, locked;
1309bd390213SMikolaj Golub 
1310aa14e9b7SMark Johnston 	p = (struct coredump_params *)arg;
1311f1fca82eSMikolaj Golub 
1312f1fca82eSMikolaj Golub 	/*
1313f1fca82eSMikolaj Golub 	 * Some kern_proc out routines that print to this sbuf may
1314f1fca82eSMikolaj Golub 	 * call us with the process lock held. Draining with the
1315f1fca82eSMikolaj Golub 	 * non-sleepable lock held is unsafe. The lock is needed for
1316f1fca82eSMikolaj Golub 	 * those routines when dumping a live process. In our case we
1317f1fca82eSMikolaj Golub 	 * can safely release the lock before draining and acquire
1318f1fca82eSMikolaj Golub 	 * again after.
1319f1fca82eSMikolaj Golub 	 */
1320f1fca82eSMikolaj Golub 	locked = PROC_LOCKED(p->td->td_proc);
1321f1fca82eSMikolaj Golub 	if (locked)
1322f1fca82eSMikolaj Golub 		PROC_UNLOCK(p->td->td_proc);
1323*78f57a9cSMark Johnston 	if (p->comp != NULL)
1324*78f57a9cSMark Johnston 		error = compressor_write(p->comp, __DECONST(char *, data), len);
1325bd390213SMikolaj Golub 	else
1326aa14e9b7SMark Johnston 		error = core_write(p, __DECONST(void *, data), len, p->offset,
1327aa14e9b7SMark Johnston 		    UIO_SYSSPACE);
1328f1fca82eSMikolaj Golub 	if (locked)
1329f1fca82eSMikolaj Golub 		PROC_LOCK(p->td->td_proc);
1330bd390213SMikolaj Golub 	if (error != 0)
1331bd390213SMikolaj Golub 		return (-error);
1332bd390213SMikolaj Golub 	p->offset += len;
1333bd390213SMikolaj Golub 	return (len);
1334bd390213SMikolaj Golub }
1335bd390213SMikolaj Golub 
1336f1fca82eSMikolaj Golub /*
1337f1fca82eSMikolaj Golub  * Drain into a counter.
1338f1fca82eSMikolaj Golub  */
1339f1fca82eSMikolaj Golub static int
1340f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len)
1341f1fca82eSMikolaj Golub {
1342f1fca82eSMikolaj Golub 	size_t *sizep;
1343f1fca82eSMikolaj Golub 
1344f1fca82eSMikolaj Golub 	sizep = (size_t *)arg;
1345f1fca82eSMikolaj Golub 	*sizep += len;
1346f1fca82eSMikolaj Golub 	return (len);
1347f1fca82eSMikolaj Golub }
1348f1fca82eSMikolaj Golub 
13498c64af4fSJohn Polstra int
1350e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1351fca666a1SJulian Elischer {
1352247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
1353fca666a1SJulian Elischer 	int error = 0;
13540ff27d31SJohn Polstra 	struct sseg_closure seginfo;
1355bd390213SMikolaj Golub 	struct note_info_list notelst;
1356aa14e9b7SMark Johnston 	struct coredump_params params;
1357bd390213SMikolaj Golub 	struct note_info *ninfo;
1358aa14e9b7SMark Johnston 	void *hdr, *tmpbuf;
1359bd390213SMikolaj Golub 	size_t hdrsize, notesz, coresize;
13608c64af4fSJohn Polstra 
1361e7228204SAlfred Perlstein 	hdr = NULL;
136202d131adSMark Johnston 	tmpbuf = NULL;
1363bd390213SMikolaj Golub 	TAILQ_INIT(&notelst);
1364e7228204SAlfred Perlstein 
13650ff27d31SJohn Polstra 	/* Size the program segments. */
13660ff27d31SJohn Polstra 	seginfo.count = 0;
13670ff27d31SJohn Polstra 	seginfo.size = 0;
13681005d8afSConrad Meyer 	each_dumpable_segment(td, cb_size_segment, &seginfo);
13690ff27d31SJohn Polstra 
13700ff27d31SJohn Polstra 	/*
1371bd390213SMikolaj Golub 	 * Collect info about the core file header area.
13720ff27d31SJohn Polstra 	 */
1373bd390213SMikolaj Golub 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1374c17b0bd2SConrad Meyer 	if (seginfo.count + 1 >= PN_XNUM)
1375c17b0bd2SConrad Meyer 		hdrsize += sizeof(Elf_Shdr);
1376bd390213SMikolaj Golub 	__elfN(prepare_notes)(td, &notelst, &notesz);
1377bd390213SMikolaj Golub 	coresize = round_page(hdrsize + notesz) + seginfo.size;
13780ff27d31SJohn Polstra 
137902d131adSMark Johnston 	/* Set up core dump parameters. */
138002d131adSMark Johnston 	params.offset = 0;
138102d131adSMark Johnston 	params.active_cred = cred;
138202d131adSMark Johnston 	params.file_cred = NOCRED;
138302d131adSMark Johnston 	params.td = td;
138402d131adSMark Johnston 	params.vp = vp;
1385*78f57a9cSMark Johnston 	params.comp = NULL;
138602d131adSMark Johnston 
1387afcc55f3SEdward Tomasz Napierala #ifdef RACCT
13884b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
13891ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1390bd390213SMikolaj Golub 		error = racct_add(td->td_proc, RACCT_CORE, coresize);
13911ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
13921ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
13931ba5ad42SEdward Tomasz Napierala 			error = EFAULT;
13941ba5ad42SEdward Tomasz Napierala 			goto done;
13951ba5ad42SEdward Tomasz Napierala 		}
13964b5c9cf6SEdward Tomasz Napierala 	}
1397afcc55f3SEdward Tomasz Napierala #endif
1398bd390213SMikolaj Golub 	if (coresize >= limit) {
1399fba6b1afSAlfred Perlstein 		error = EFAULT;
1400fba6b1afSAlfred Perlstein 		goto done;
1401fba6b1afSAlfred Perlstein 	}
14020ff27d31SJohn Polstra 
1403aa14e9b7SMark Johnston 	/* Create a compression stream if necessary. */
1404*78f57a9cSMark Johnston 	if (compress_user_cores != 0) {
1405*78f57a9cSMark Johnston 		params.comp = compressor_init(core_compressed_write,
1406*78f57a9cSMark Johnston 		    compress_user_cores, CORE_BUF_SIZE,
1407*78f57a9cSMark Johnston 		    compress_user_cores_level, &params);
1408*78f57a9cSMark Johnston 		if (params.comp == NULL) {
1409aa14e9b7SMark Johnston 			error = EFAULT;
1410aa14e9b7SMark Johnston 			goto done;
1411aa14e9b7SMark Johnston 		}
1412aa14e9b7SMark Johnston 		tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1413aa14e9b7SMark Johnston         }
1414aa14e9b7SMark Johnston 
14150ff27d31SJohn Polstra 	/*
14160ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
1417bd390213SMikolaj Golub 	 * and write it out following the notes.
14180ff27d31SJohn Polstra 	 */
1419a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1420aa14e9b7SMark Johnston 	error = __elfN(corehdr)(&params, seginfo.count, hdr, hdrsize, &notelst,
1421aa14e9b7SMark Johnston 	    notesz);
14220ff27d31SJohn Polstra 
14230ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
14240ff27d31SJohn Polstra 	if (error == 0) {
14250ff27d31SJohn Polstra 		Elf_Phdr *php;
14262b471bc6STim J. Robbins 		off_t offset;
14270ff27d31SJohn Polstra 		int i;
14280ff27d31SJohn Polstra 
14290ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1430bd390213SMikolaj Golub 		offset = round_page(hdrsize + notesz);
14310ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1432aa14e9b7SMark Johnston 			error = core_output((caddr_t)(uintptr_t)php->p_vaddr,
1433aa14e9b7SMark Johnston 			    php->p_filesz, offset, &params, tmpbuf);
14340ff27d31SJohn Polstra 			if (error != 0)
14352b471bc6STim J. Robbins 				break;
14360ff27d31SJohn Polstra 			offset += php->p_filesz;
14370ff27d31SJohn Polstra 			php++;
14380ff27d31SJohn Polstra 		}
1439*78f57a9cSMark Johnston 		if (error == 0 && params.comp != NULL)
1440*78f57a9cSMark Johnston 			error = compressor_flush(params.comp);
14410ff27d31SJohn Polstra 	}
1442e7228204SAlfred Perlstein 	if (error) {
1443e7228204SAlfred Perlstein 		log(LOG_WARNING,
1444e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1445e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1446e7228204SAlfred Perlstein 	}
1447e7228204SAlfred Perlstein 
1448e7228204SAlfred Perlstein done:
1449aa14e9b7SMark Johnston 	free(tmpbuf, M_TEMP);
1450*78f57a9cSMark Johnston 	if (params.comp != NULL)
1451*78f57a9cSMark Johnston 		compressor_fini(params.comp);
1452bd390213SMikolaj Golub 	while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1453bd390213SMikolaj Golub 		TAILQ_REMOVE(&notelst, ninfo, link);
1454bd390213SMikolaj Golub 		free(ninfo, M_TEMP);
1455bd390213SMikolaj Golub 	}
1456bd390213SMikolaj Golub 	if (hdr != NULL)
14570ff27d31SJohn Polstra 		free(hdr, M_TEMP);
14580ff27d31SJohn Polstra 
1459a7cddfedSJake Burkholder 	return (error);
14608c64af4fSJohn Polstra }
14618c64af4fSJohn Polstra 
14620ff27d31SJohn Polstra /*
14631005d8afSConrad Meyer  * A callback for each_dumpable_segment() to write out the segment's
14640ff27d31SJohn Polstra  * program header entry.
14650ff27d31SJohn Polstra  */
14660ff27d31SJohn Polstra static void
14670ff27d31SJohn Polstra cb_put_phdr(entry, closure)
14680ff27d31SJohn Polstra 	vm_map_entry_t entry;
14690ff27d31SJohn Polstra 	void *closure;
14700ff27d31SJohn Polstra {
14710ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
14720ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
14730ff27d31SJohn Polstra 
14740ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
14750ff27d31SJohn Polstra 
14760ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
14770ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
14780ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
14790ff27d31SJohn Polstra 	phdr->p_paddr = 0;
14800ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
14810ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1482ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
14830ff27d31SJohn Polstra 
14840ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
14850ff27d31SJohn Polstra 	phc->phdr++;
14860ff27d31SJohn Polstra }
14870ff27d31SJohn Polstra 
14880ff27d31SJohn Polstra /*
14891005d8afSConrad Meyer  * A callback for each_dumpable_segment() to gather information about
14900ff27d31SJohn Polstra  * the number of segments and their total size.
14910ff27d31SJohn Polstra  */
14920ff27d31SJohn Polstra static void
1493f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure)
14940ff27d31SJohn Polstra {
14950ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
14960ff27d31SJohn Polstra 
14970ff27d31SJohn Polstra 	ssc->count++;
14980ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
14990ff27d31SJohn Polstra }
15000ff27d31SJohn Polstra 
15010ff27d31SJohn Polstra /*
15020ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
15030ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
15040ff27d31SJohn Polstra  * caller-supplied data.
15050ff27d31SJohn Polstra  */
15060ff27d31SJohn Polstra static void
15071005d8afSConrad Meyer each_dumpable_segment(struct thread *td, segment_callback func, void *closure)
15080ff27d31SJohn Polstra {
1509247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
15100ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
15110ff27d31SJohn Polstra 	vm_map_entry_t entry;
1512976a87a2SAlan Cox 	vm_object_t backing_object, object;
1513976a87a2SAlan Cox 	boolean_t ignore_entry;
15140ff27d31SJohn Polstra 
1515976a87a2SAlan Cox 	vm_map_lock_read(map);
15160ff27d31SJohn Polstra 	for (entry = map->header.next; entry != &map->header;
15170ff27d31SJohn Polstra 	    entry = entry->next) {
1518fa7dd9c5SMatthew Dillon 		/*
1519fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1520fa7dd9c5SMatthew Dillon 		 * coredump mode.
1521fa7dd9c5SMatthew Dillon 		 *
1522fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1523fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1524fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1525fa7dd9c5SMatthew Dillon 		 */
1526fa7dd9c5SMatthew Dillon 		if (elf_legacy_coredump) {
1527fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
15280ff27d31SJohn Polstra 				continue;
1529fa7dd9c5SMatthew Dillon 		} else {
1530fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_ALL) == 0)
1531fa7dd9c5SMatthew Dillon 				continue;
1532fa7dd9c5SMatthew Dillon 		}
15330ff27d31SJohn Polstra 
15349730a5daSPaul Saab 		/*
1535fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1536fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1537fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1538fa7dd9c5SMatthew Dillon 		 * kernel map).
15399730a5daSPaul Saab 		 */
1540fa7dd9c5SMatthew Dillon 		if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
15419730a5daSPaul Saab 			continue;
15429730a5daSPaul Saab 
1543976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
15440ff27d31SJohn Polstra 			continue;
15450ff27d31SJohn Polstra 
15460ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1547bc403f03SAttilio Rao 		VM_OBJECT_RLOCK(object);
1548976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1549bc403f03SAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1550bc403f03SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1551976a87a2SAlan Cox 			object = backing_object;
1552976a87a2SAlan Cox 		}
1553976a87a2SAlan Cox 		ignore_entry = object->type != OBJT_DEFAULT &&
1554bc411bc2SJohn Baldwin 		    object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
1555bc411bc2SJohn Baldwin 		    object->type != OBJT_PHYS;
1556bc403f03SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1557976a87a2SAlan Cox 		if (ignore_entry)
15580ff27d31SJohn Polstra 			continue;
15590ff27d31SJohn Polstra 
15600ff27d31SJohn Polstra 		(*func)(entry, closure);
15610ff27d31SJohn Polstra 	}
1562976a87a2SAlan Cox 	vm_map_unlock_read(map);
15630ff27d31SJohn Polstra }
15640ff27d31SJohn Polstra 
15650ff27d31SJohn Polstra /*
15660ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
15670ff27d31SJohn Polstra  * the page boundary.
15680ff27d31SJohn Polstra  */
15698c64af4fSJohn Polstra static int
1570aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1571aa14e9b7SMark Johnston     size_t hdrsize, struct note_info_list *notelst, size_t notesz)
15728c64af4fSJohn Polstra {
1573bd390213SMikolaj Golub 	struct note_info *ninfo;
1574bd390213SMikolaj Golub 	struct sbuf *sb;
1575bd390213SMikolaj Golub 	int error;
15768c64af4fSJohn Polstra 
15778c64af4fSJohn Polstra 	/* Fill in the header. */
15780ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
1579aa14e9b7SMark Johnston 	__elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz);
15808c64af4fSJohn Polstra 
1581bd390213SMikolaj Golub 	sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1582aa14e9b7SMark Johnston 	sbuf_set_drain(sb, sbuf_drain_core_output, p);
1583bd390213SMikolaj Golub 	sbuf_start_section(sb, NULL);
1584bd390213SMikolaj Golub 	sbuf_bcat(sb, hdr, hdrsize);
1585bd390213SMikolaj Golub 	TAILQ_FOREACH(ninfo, notelst, link)
1586bd390213SMikolaj Golub 	    __elfN(putnote)(ninfo, sb);
1587bd390213SMikolaj Golub 	/* Align up to a page boundary for the program segments. */
1588bd390213SMikolaj Golub 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1589bd390213SMikolaj Golub 	error = sbuf_finish(sb);
1590bd390213SMikolaj Golub 	sbuf_delete(sb);
1591bd390213SMikolaj Golub 
1592bd390213SMikolaj Golub 	return (error);
1593e7228204SAlfred Perlstein }
1594bd390213SMikolaj Golub 
1595bd390213SMikolaj Golub static void
1596bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1597bd390213SMikolaj Golub     size_t *sizep)
1598bd390213SMikolaj Golub {
1599bd390213SMikolaj Golub 	struct proc *p;
1600bd390213SMikolaj Golub 	struct thread *thr;
1601bd390213SMikolaj Golub 	size_t size;
1602bd390213SMikolaj Golub 
1603bd390213SMikolaj Golub 	p = td->td_proc;
1604bd390213SMikolaj Golub 	size = 0;
1605bd390213SMikolaj Golub 
1606bd390213SMikolaj Golub 	size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1607bd390213SMikolaj Golub 
1608bd390213SMikolaj Golub 	/*
1609bd390213SMikolaj Golub 	 * To have the debugger select the right thread (LWP) as the initial
1610bd390213SMikolaj Golub 	 * thread, we dump the state of the thread passed to us in td first.
1611bd390213SMikolaj Golub 	 * This is the thread that causes the core dump and thus likely to
1612bd390213SMikolaj Golub 	 * be the right thread one wants to have selected in the debugger.
1613bd390213SMikolaj Golub 	 */
1614bd390213SMikolaj Golub 	thr = td;
1615bd390213SMikolaj Golub 	while (thr != NULL) {
1616bd390213SMikolaj Golub 		size += register_note(list, NT_PRSTATUS,
1617bd390213SMikolaj Golub 		    __elfN(note_prstatus), thr);
1618bd390213SMikolaj Golub 		size += register_note(list, NT_FPREGSET,
1619bd390213SMikolaj Golub 		    __elfN(note_fpregset), thr);
1620bd390213SMikolaj Golub 		size += register_note(list, NT_THRMISC,
1621bd390213SMikolaj Golub 		    __elfN(note_thrmisc), thr);
162286be94fcSTycho Nightingale 		size += register_note(list, NT_PTLWPINFO,
162386be94fcSTycho Nightingale 		    __elfN(note_ptlwpinfo), thr);
1624bd390213SMikolaj Golub 		size += register_note(list, -1,
1625bd390213SMikolaj Golub 		    __elfN(note_threadmd), thr);
1626bd390213SMikolaj Golub 
1627bd390213SMikolaj Golub 		thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1628bd390213SMikolaj Golub 		    TAILQ_NEXT(thr, td_plist);
1629bd390213SMikolaj Golub 		if (thr == td)
1630bd390213SMikolaj Golub 			thr = TAILQ_NEXT(thr, td_plist);
1631dada0278SJohn Polstra 	}
1632dada0278SJohn Polstra 
1633f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PROC,
1634f1fca82eSMikolaj Golub 	    __elfN(note_procstat_proc), p);
1635f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_FILES,
1636f1fca82eSMikolaj Golub 	    note_procstat_files, p);
1637f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_VMMAP,
1638f1fca82eSMikolaj Golub 	    note_procstat_vmmap, p);
1639f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_GROUPS,
1640f1fca82eSMikolaj Golub 	    note_procstat_groups, p);
1641f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_UMASK,
1642f1fca82eSMikolaj Golub 	    note_procstat_umask, p);
1643f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_RLIMIT,
1644f1fca82eSMikolaj Golub 	    note_procstat_rlimit, p);
1645f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_OSREL,
1646f1fca82eSMikolaj Golub 	    note_procstat_osrel, p);
1647f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1648f1fca82eSMikolaj Golub 	    __elfN(note_procstat_psstrings), p);
1649f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_AUXV,
1650f1fca82eSMikolaj Golub 	    __elfN(note_procstat_auxv), p);
1651f1fca82eSMikolaj Golub 
1652bd390213SMikolaj Golub 	*sizep = size;
1653bd390213SMikolaj Golub }
1654bd390213SMikolaj Golub 
1655bd390213SMikolaj Golub static void
1656bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1657bd390213SMikolaj Golub     size_t notesz)
1658bd390213SMikolaj Golub {
1659bd390213SMikolaj Golub 	Elf_Ehdr *ehdr;
1660bd390213SMikolaj Golub 	Elf_Phdr *phdr;
1661c17b0bd2SConrad Meyer 	Elf_Shdr *shdr;
1662bd390213SMikolaj Golub 	struct phdr_closure phc;
1663bd390213SMikolaj Golub 
1664bd390213SMikolaj Golub 	ehdr = (Elf_Ehdr *)hdr;
1665bd390213SMikolaj Golub 
1666bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1667bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1668bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1669bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1670bd390213SMikolaj Golub 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1671bd390213SMikolaj Golub 	ehdr->e_ident[EI_DATA] = ELF_DATA;
1672bd390213SMikolaj Golub 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1673bd390213SMikolaj Golub 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1674bd390213SMikolaj Golub 	ehdr->e_ident[EI_ABIVERSION] = 0;
1675bd390213SMikolaj Golub 	ehdr->e_ident[EI_PAD] = 0;
1676bd390213SMikolaj Golub 	ehdr->e_type = ET_CORE;
1677885f13dcSJohn Baldwin 	ehdr->e_machine = td->td_proc->p_elf_machine;
1678bd390213SMikolaj Golub 	ehdr->e_version = EV_CURRENT;
1679bd390213SMikolaj Golub 	ehdr->e_entry = 0;
1680bd390213SMikolaj Golub 	ehdr->e_phoff = sizeof(Elf_Ehdr);
1681885f13dcSJohn Baldwin 	ehdr->e_flags = td->td_proc->p_elf_flags;
1682bd390213SMikolaj Golub 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
1683bd390213SMikolaj Golub 	ehdr->e_phentsize = sizeof(Elf_Phdr);
1684bd390213SMikolaj Golub 	ehdr->e_shentsize = sizeof(Elf_Shdr);
1685bd390213SMikolaj Golub 	ehdr->e_shstrndx = SHN_UNDEF;
1686c17b0bd2SConrad Meyer 	if (numsegs + 1 < PN_XNUM) {
1687c17b0bd2SConrad Meyer 		ehdr->e_phnum = numsegs + 1;
1688c17b0bd2SConrad Meyer 		ehdr->e_shnum = 0;
1689c17b0bd2SConrad Meyer 	} else {
1690c17b0bd2SConrad Meyer 		ehdr->e_phnum = PN_XNUM;
1691c17b0bd2SConrad Meyer 		ehdr->e_shnum = 1;
1692c17b0bd2SConrad Meyer 
1693c17b0bd2SConrad Meyer 		ehdr->e_shoff = ehdr->e_phoff +
1694c17b0bd2SConrad Meyer 		    (numsegs + 1) * ehdr->e_phentsize;
1695c17b0bd2SConrad Meyer 		KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr),
1696c17b0bd2SConrad Meyer 		    ("e_shoff: %zu, hdrsize - shdr: %zu",
169707f825e8SConrad Meyer 		     (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr)));
1698c17b0bd2SConrad Meyer 
1699c17b0bd2SConrad Meyer 		shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
1700c17b0bd2SConrad Meyer 		memset(shdr, 0, sizeof(*shdr));
1701c17b0bd2SConrad Meyer 		/*
1702c17b0bd2SConrad Meyer 		 * A special first section is used to hold large segment and
1703c17b0bd2SConrad Meyer 		 * section counts.  This was proposed by Sun Microsystems in
1704c17b0bd2SConrad Meyer 		 * Solaris and has been adopted by Linux; the standard ELF
1705c17b0bd2SConrad Meyer 		 * tools are already familiar with the technique.
1706c17b0bd2SConrad Meyer 		 *
1707c17b0bd2SConrad Meyer 		 * See table 7-7 of the Solaris "Linker and Libraries Guide"
1708c17b0bd2SConrad Meyer 		 * (or 12-7 depending on the version of the document) for more
1709c17b0bd2SConrad Meyer 		 * details.
1710c17b0bd2SConrad Meyer 		 */
1711c17b0bd2SConrad Meyer 		shdr->sh_type = SHT_NULL;
1712c17b0bd2SConrad Meyer 		shdr->sh_size = ehdr->e_shnum;
1713c17b0bd2SConrad Meyer 		shdr->sh_link = ehdr->e_shstrndx;
1714c17b0bd2SConrad Meyer 		shdr->sh_info = numsegs + 1;
1715c17b0bd2SConrad Meyer 	}
1716bd390213SMikolaj Golub 
1717bd390213SMikolaj Golub 	/*
1718bd390213SMikolaj Golub 	 * Fill in the program header entries.
1719bd390213SMikolaj Golub 	 */
1720c17b0bd2SConrad Meyer 	phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
1721bd390213SMikolaj Golub 
1722bd390213SMikolaj Golub 	/* The note segement. */
1723bd390213SMikolaj Golub 	phdr->p_type = PT_NOTE;
1724bd390213SMikolaj Golub 	phdr->p_offset = hdrsize;
1725bd390213SMikolaj Golub 	phdr->p_vaddr = 0;
1726bd390213SMikolaj Golub 	phdr->p_paddr = 0;
1727bd390213SMikolaj Golub 	phdr->p_filesz = notesz;
1728bd390213SMikolaj Golub 	phdr->p_memsz = 0;
1729bd390213SMikolaj Golub 	phdr->p_flags = PF_R;
17301b8388cdSMikolaj Golub 	phdr->p_align = ELF_NOTE_ROUNDSIZE;
1731bd390213SMikolaj Golub 	phdr++;
1732bd390213SMikolaj Golub 
1733bd390213SMikolaj Golub 	/* All the writable segments from the program. */
1734bd390213SMikolaj Golub 	phc.phdr = phdr;
1735bd390213SMikolaj Golub 	phc.offset = round_page(hdrsize + notesz);
17361005d8afSConrad Meyer 	each_dumpable_segment(td, cb_put_phdr, &phc);
1737bd390213SMikolaj Golub }
1738bd390213SMikolaj Golub 
1739bd390213SMikolaj Golub static size_t
1740bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1741bd390213SMikolaj Golub {
1742bd390213SMikolaj Golub 	struct note_info *ninfo;
1743bd390213SMikolaj Golub 	size_t size, notesize;
1744bd390213SMikolaj Golub 
1745bd390213SMikolaj Golub 	size = 0;
1746bd390213SMikolaj Golub 	out(arg, NULL, &size);
1747bd390213SMikolaj Golub 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1748bd390213SMikolaj Golub 	ninfo->type = type;
1749bd390213SMikolaj Golub 	ninfo->outfunc = out;
1750bd390213SMikolaj Golub 	ninfo->outarg = arg;
1751bd390213SMikolaj Golub 	ninfo->outsize = size;
1752bd390213SMikolaj Golub 	TAILQ_INSERT_TAIL(list, ninfo, link);
1753bd390213SMikolaj Golub 
1754bd390213SMikolaj Golub 	if (type == -1)
1755bd390213SMikolaj Golub 		return (size);
1756bd390213SMikolaj Golub 
1757bd390213SMikolaj Golub 	notesize = sizeof(Elf_Note) +		/* note header */
1758180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1759180e57e5SJohn Baldwin 						/* note name */
1760180e57e5SJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1761180e57e5SJohn Baldwin 
1762180e57e5SJohn Baldwin 	return (notesize);
1763180e57e5SJohn Baldwin }
1764180e57e5SJohn Baldwin 
1765180e57e5SJohn Baldwin static size_t
1766180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len)
1767180e57e5SJohn Baldwin {
1768180e57e5SJohn Baldwin 	size_t padded_len;
1769180e57e5SJohn Baldwin 
1770180e57e5SJohn Baldwin 	padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
1771180e57e5SJohn Baldwin 	if (dst != NULL) {
1772180e57e5SJohn Baldwin 		bcopy(src, dst, len);
1773180e57e5SJohn Baldwin 		bzero((char *)dst + len, padded_len - len);
1774180e57e5SJohn Baldwin 	}
1775180e57e5SJohn Baldwin 	return (padded_len);
1776180e57e5SJohn Baldwin }
1777180e57e5SJohn Baldwin 
1778180e57e5SJohn Baldwin size_t
1779180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
1780180e57e5SJohn Baldwin {
1781180e57e5SJohn Baldwin 	Elf_Note *note;
1782180e57e5SJohn Baldwin 	char *buf;
1783180e57e5SJohn Baldwin 	size_t notesize;
1784180e57e5SJohn Baldwin 
1785180e57e5SJohn Baldwin 	buf = dst;
1786180e57e5SJohn Baldwin 	if (buf != NULL) {
1787180e57e5SJohn Baldwin 		note = (Elf_Note *)buf;
1788180e57e5SJohn Baldwin 		note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1789180e57e5SJohn Baldwin 		note->n_descsz = size;
1790180e57e5SJohn Baldwin 		note->n_type = type;
1791180e57e5SJohn Baldwin 		buf += sizeof(*note);
1792180e57e5SJohn Baldwin 		buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
1793180e57e5SJohn Baldwin 		    sizeof(FREEBSD_ABI_VENDOR));
1794180e57e5SJohn Baldwin 		append_note_data(src, buf, size);
1795180e57e5SJohn Baldwin 		if (descp != NULL)
1796180e57e5SJohn Baldwin 			*descp = buf;
1797180e57e5SJohn Baldwin 	}
1798180e57e5SJohn Baldwin 
1799180e57e5SJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
1800180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1801180e57e5SJohn Baldwin 						/* note name */
18021b8388cdSMikolaj Golub 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1803bd390213SMikolaj Golub 
1804bd390213SMikolaj Golub 	return (notesize);
1805bd390213SMikolaj Golub }
1806bd390213SMikolaj Golub 
1807bd390213SMikolaj Golub static void
1808bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1809bd390213SMikolaj Golub {
1810bd390213SMikolaj Golub 	Elf_Note note;
181114bdbaf2SConrad Meyer 	ssize_t old_len, sect_len;
181214bdbaf2SConrad Meyer 	size_t new_len, descsz, i;
1813bd390213SMikolaj Golub 
1814bd390213SMikolaj Golub 	if (ninfo->type == -1) {
1815bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1816bd390213SMikolaj Golub 		return;
1817bd390213SMikolaj Golub 	}
1818bd390213SMikolaj Golub 
1819180e57e5SJohn Baldwin 	note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1820bd390213SMikolaj Golub 	note.n_descsz = ninfo->outsize;
1821bd390213SMikolaj Golub 	note.n_type = ninfo->type;
1822bd390213SMikolaj Golub 
1823bd390213SMikolaj Golub 	sbuf_bcat(sb, &note, sizeof(note));
1824bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1825180e57e5SJohn Baldwin 	sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
18261b8388cdSMikolaj Golub 	sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1827bd390213SMikolaj Golub 	if (note.n_descsz == 0)
1828bd390213SMikolaj Golub 		return;
1829bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1830bd390213SMikolaj Golub 	ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
183114bdbaf2SConrad Meyer 	sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
183214bdbaf2SConrad Meyer 	if (sect_len < 0)
183314bdbaf2SConrad Meyer 		return;
183414bdbaf2SConrad Meyer 
183514bdbaf2SConrad Meyer 	new_len = (size_t)sect_len;
183614bdbaf2SConrad Meyer 	descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
183714bdbaf2SConrad Meyer 	if (new_len < descsz) {
183814bdbaf2SConrad Meyer 		/*
183914bdbaf2SConrad Meyer 		 * It is expected that individual note emitters will correctly
184014bdbaf2SConrad Meyer 		 * predict their expected output size and fill up to that size
184114bdbaf2SConrad Meyer 		 * themselves, padding in a format-specific way if needed.
184214bdbaf2SConrad Meyer 		 * However, in case they don't, just do it here with zeros.
184314bdbaf2SConrad Meyer 		 */
184414bdbaf2SConrad Meyer 		for (i = 0; i < descsz - new_len; i++)
184514bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
184614bdbaf2SConrad Meyer 	} else if (new_len > descsz) {
184714bdbaf2SConrad Meyer 		/*
184814bdbaf2SConrad Meyer 		 * We can't always truncate sb -- we may have drained some
184914bdbaf2SConrad Meyer 		 * of it already.
185014bdbaf2SConrad Meyer 		 */
185114bdbaf2SConrad Meyer 		KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
185214bdbaf2SConrad Meyer 		    "read it (%zu > %zu).  Since it is longer than "
185314bdbaf2SConrad Meyer 		    "expected, this coredump's notes are corrupt.  THIS "
185414bdbaf2SConrad Meyer 		    "IS A BUG in the note_procstat routine for type %u.\n",
185514bdbaf2SConrad Meyer 		    __func__, (unsigned)note.n_type, new_len, descsz,
185614bdbaf2SConrad Meyer 		    (unsigned)note.n_type));
185714bdbaf2SConrad Meyer 	}
1858bd390213SMikolaj Golub }
1859bd390213SMikolaj Golub 
1860bd390213SMikolaj Golub /*
1861bd390213SMikolaj Golub  * Miscellaneous note out functions.
1862bd390213SMikolaj Golub  */
1863bd390213SMikolaj Golub 
1864841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1865841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
186651645e83SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
1867841c0c7eSNathan Whitehorn 
186862919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
186962919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
187062919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
187162919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
187262919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
18737f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
1874f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	KERN_PROC_MASK32
1875f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t;
1876f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t;
187762919d78SPeter Wemm #else
187862919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
187962919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
188062919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
188162919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
188262919d78SPeter Wemm typedef gregset_t elf_gregset_t;
18837f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
1884f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	0
1885f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t;
1886f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t;
188762919d78SPeter Wemm #endif
188862919d78SPeter Wemm 
18898c64af4fSJohn Polstra static void
1890bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
18918c64af4fSJohn Polstra {
1892c77547d2SJohn Baldwin 	struct sbuf sbarg;
1893c77547d2SJohn Baldwin 	size_t len;
1894c77547d2SJohn Baldwin 	char *cp, *end;
1895247aba24SMarcel Moolenaar 	struct proc *p;
1896bd390213SMikolaj Golub 	elf_prpsinfo_t *psinfo;
1897c77547d2SJohn Baldwin 	int error;
18988c64af4fSJohn Polstra 
1899bd390213SMikolaj Golub 	p = (struct proc *)arg;
1900bd390213SMikolaj Golub 	if (sb != NULL) {
1901bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1902bd390213SMikolaj Golub 		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
19038c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
190462919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1905ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1906c77547d2SJohn Baldwin 		PROC_LOCK(p);
1907c77547d2SJohn Baldwin 		if (p->p_args != NULL) {
1908c77547d2SJohn Baldwin 			len = sizeof(psinfo->pr_psargs) - 1;
1909c77547d2SJohn Baldwin 			if (len > p->p_args->ar_length)
1910c77547d2SJohn Baldwin 				len = p->p_args->ar_length;
1911c77547d2SJohn Baldwin 			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
1912c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
1913c77547d2SJohn Baldwin 			error = 0;
1914c77547d2SJohn Baldwin 		} else {
1915c77547d2SJohn Baldwin 			_PHOLD(p);
1916c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
1917c77547d2SJohn Baldwin 			sbuf_new(&sbarg, psinfo->pr_psargs,
1918c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
1919c77547d2SJohn Baldwin 			error = proc_getargv(curthread, p, &sbarg);
1920c77547d2SJohn Baldwin 			PRELE(p);
1921c77547d2SJohn Baldwin 			if (sbuf_finish(&sbarg) == 0)
1922c77547d2SJohn Baldwin 				len = sbuf_len(&sbarg) - 1;
1923c77547d2SJohn Baldwin 			else
1924c77547d2SJohn Baldwin 				len = sizeof(psinfo->pr_psargs) - 1;
1925c77547d2SJohn Baldwin 			sbuf_delete(&sbarg);
1926c77547d2SJohn Baldwin 		}
1927c77547d2SJohn Baldwin 		if (error || len == 0)
1928ccd3953eSJohn Baldwin 			strlcpy(psinfo->pr_psargs, p->p_comm,
19298c9b7b2cSMarcel Moolenaar 			    sizeof(psinfo->pr_psargs));
1930c77547d2SJohn Baldwin 		else {
1931c77547d2SJohn Baldwin 			KASSERT(len < sizeof(psinfo->pr_psargs),
1932c77547d2SJohn Baldwin 			    ("len is too long: %zu vs %zu", len,
1933c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs)));
1934c77547d2SJohn Baldwin 			cp = psinfo->pr_psargs;
1935c77547d2SJohn Baldwin 			end = cp + len - 1;
1936c77547d2SJohn Baldwin 			for (;;) {
1937c77547d2SJohn Baldwin 				cp = memchr(cp, '\0', end - cp);
1938c77547d2SJohn Baldwin 				if (cp == NULL)
1939c77547d2SJohn Baldwin 					break;
1940c77547d2SJohn Baldwin 				*cp = ' ';
1941c77547d2SJohn Baldwin 			}
1942c77547d2SJohn Baldwin 		}
1943ccb83afdSJohn Baldwin 		psinfo->pr_pid = p->p_pid;
1944bd390213SMikolaj Golub 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1945bd390213SMikolaj Golub 		free(psinfo, M_TEMP);
1946bd390213SMikolaj Golub 	}
1947bd390213SMikolaj Golub 	*sizep = sizeof(*psinfo);
1948bd390213SMikolaj Golub }
1949bd390213SMikolaj Golub 
1950bd390213SMikolaj Golub static void
1951bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1952bd390213SMikolaj Golub {
1953bd390213SMikolaj Golub 	struct thread *td;
1954bd390213SMikolaj Golub 	elf_prstatus_t *status;
1955bd390213SMikolaj Golub 
1956bd390213SMikolaj Golub 	td = (struct thread *)arg;
1957bd390213SMikolaj Golub 	if (sb != NULL) {
1958bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*status), ("invalid size"));
1959bd390213SMikolaj Golub 		status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
19608c9b7b2cSMarcel Moolenaar 		status->pr_version = PRSTATUS_VERSION;
196162919d78SPeter Wemm 		status->pr_statussz = sizeof(elf_prstatus_t);
196262919d78SPeter Wemm 		status->pr_gregsetsz = sizeof(elf_gregset_t);
196362919d78SPeter Wemm 		status->pr_fpregsetsz = sizeof(elf_fpregset_t);
19648c9b7b2cSMarcel Moolenaar 		status->pr_osreldate = osreldate;
1965bd390213SMikolaj Golub 		status->pr_cursig = td->td_proc->p_sig;
1966bd390213SMikolaj Golub 		status->pr_pid = td->td_tid;
1967841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1968bd390213SMikolaj Golub 		fill_regs32(td, &status->pr_reg);
196962919d78SPeter Wemm #else
1970bd390213SMikolaj Golub 		fill_regs(td, &status->pr_reg);
197162919d78SPeter Wemm #endif
1972bd390213SMikolaj Golub 		sbuf_bcat(sb, status, sizeof(*status));
1973bd390213SMikolaj Golub 		free(status, M_TEMP);
19748c9b7b2cSMarcel Moolenaar 	}
1975bd390213SMikolaj Golub 	*sizep = sizeof(*status);
1976bd390213SMikolaj Golub }
1977bd390213SMikolaj Golub 
1978bd390213SMikolaj Golub static void
1979bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1980bd390213SMikolaj Golub {
1981bd390213SMikolaj Golub 	struct thread *td;
1982bd390213SMikolaj Golub 	elf_prfpregset_t *fpregset;
1983bd390213SMikolaj Golub 
1984bd390213SMikolaj Golub 	td = (struct thread *)arg;
1985bd390213SMikolaj Golub 	if (sb != NULL) {
1986bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1987bd390213SMikolaj Golub 		fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1988bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1989bd390213SMikolaj Golub 		fill_fpregs32(td, fpregset);
1990bd390213SMikolaj Golub #else
1991bd390213SMikolaj Golub 		fill_fpregs(td, fpregset);
1992bd390213SMikolaj Golub #endif
1993bd390213SMikolaj Golub 		sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1994bd390213SMikolaj Golub 		free(fpregset, M_TEMP);
1995bd390213SMikolaj Golub 	}
1996bd390213SMikolaj Golub 	*sizep = sizeof(*fpregset);
1997bd390213SMikolaj Golub }
1998bd390213SMikolaj Golub 
1999bd390213SMikolaj Golub static void
2000bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
2001bd390213SMikolaj Golub {
2002bd390213SMikolaj Golub 	struct thread *td;
2003bd390213SMikolaj Golub 	elf_thrmisc_t thrmisc;
2004bd390213SMikolaj Golub 
2005bd390213SMikolaj Golub 	td = (struct thread *)arg;
2006bd390213SMikolaj Golub 	if (sb != NULL) {
2007bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
2008bd390213SMikolaj Golub 		bzero(&thrmisc._pad, sizeof(thrmisc._pad));
2009bd390213SMikolaj Golub 		strcpy(thrmisc.pr_tname, td->td_name);
2010bd390213SMikolaj Golub 		sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
2011bd390213SMikolaj Golub 	}
2012bd390213SMikolaj Golub 	*sizep = sizeof(thrmisc);
2013bd390213SMikolaj Golub }
2014bd390213SMikolaj Golub 
201586be94fcSTycho Nightingale static void
201686be94fcSTycho Nightingale __elfN(note_ptlwpinfo)(void *arg, struct sbuf *sb, size_t *sizep)
201786be94fcSTycho Nightingale {
201886be94fcSTycho Nightingale 	struct thread *td;
201986be94fcSTycho Nightingale 	size_t size;
202086be94fcSTycho Nightingale 	int structsize;
202151645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
202251645e83SJohn Baldwin 	struct ptrace_lwpinfo32 pl;
202351645e83SJohn Baldwin #else
202486be94fcSTycho Nightingale 	struct ptrace_lwpinfo pl;
202551645e83SJohn Baldwin #endif
202686be94fcSTycho Nightingale 
202786be94fcSTycho Nightingale 	td = (struct thread *)arg;
202851645e83SJohn Baldwin 	size = sizeof(structsize) + sizeof(pl);
202986be94fcSTycho Nightingale 	if (sb != NULL) {
203086be94fcSTycho Nightingale 		KASSERT(*sizep == size, ("invalid size"));
203151645e83SJohn Baldwin 		structsize = sizeof(pl);
203286be94fcSTycho Nightingale 		sbuf_bcat(sb, &structsize, sizeof(structsize));
203386be94fcSTycho Nightingale 		bzero(&pl, sizeof(pl));
203486be94fcSTycho Nightingale 		pl.pl_lwpid = td->td_tid;
203586be94fcSTycho Nightingale 		pl.pl_event = PL_EVENT_NONE;
203686be94fcSTycho Nightingale 		pl.pl_sigmask = td->td_sigmask;
203786be94fcSTycho Nightingale 		pl.pl_siglist = td->td_siglist;
203886be94fcSTycho Nightingale 		if (td->td_si.si_signo != 0) {
203986be94fcSTycho Nightingale 			pl.pl_event = PL_EVENT_SIGNAL;
204086be94fcSTycho Nightingale 			pl.pl_flags |= PL_FLAG_SI;
204151645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
204251645e83SJohn Baldwin 			siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo);
204351645e83SJohn Baldwin #else
204486be94fcSTycho Nightingale 			pl.pl_siginfo = td->td_si;
204551645e83SJohn Baldwin #endif
204686be94fcSTycho Nightingale 		}
204786be94fcSTycho Nightingale 		strcpy(pl.pl_tdname, td->td_name);
204886be94fcSTycho Nightingale 		/* XXX TODO: supply more information in struct ptrace_lwpinfo*/
204951645e83SJohn Baldwin 		sbuf_bcat(sb, &pl, sizeof(pl));
205086be94fcSTycho Nightingale 	}
205186be94fcSTycho Nightingale 	*sizep = size;
205286be94fcSTycho Nightingale }
205386be94fcSTycho Nightingale 
20544da47b2fSMarcel Moolenaar /*
20554da47b2fSMarcel Moolenaar  * Allow for MD specific notes, as well as any MD
20564da47b2fSMarcel Moolenaar  * specific preparations for writing MI notes.
20574da47b2fSMarcel Moolenaar  */
20588c64af4fSJohn Polstra static void
2059bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
20608c64af4fSJohn Polstra {
2061bd390213SMikolaj Golub 	struct thread *td;
2062bd390213SMikolaj Golub 	void *buf;
2063bd390213SMikolaj Golub 	size_t size;
20648c64af4fSJohn Polstra 
2065bd390213SMikolaj Golub 	td = (struct thread *)arg;
2066bd390213SMikolaj Golub 	size = *sizep;
2067bd390213SMikolaj Golub 	if (size != 0 && sb != NULL)
2068bd390213SMikolaj Golub 		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
206983a396ceSChristian Brueffer 	else
207083a396ceSChristian Brueffer 		buf = NULL;
2071bd390213SMikolaj Golub 	size = 0;
2072bd390213SMikolaj Golub 	__elfN(dump_thread)(td, buf, &size);
207364779280SKonstantin Belousov 	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
207483a396ceSChristian Brueffer 	if (size != 0 && sb != NULL)
2075bd390213SMikolaj Golub 		sbuf_bcat(sb, buf, size);
2076a1761d73SChristian Brueffer 	free(buf, M_TEMP);
2077bd390213SMikolaj Golub 	*sizep = size;
20788c64af4fSJohn Polstra }
20798c64af4fSJohn Polstra 
2080f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE
2081f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
2082f1fca82eSMikolaj Golub #endif
2083f1fca82eSMikolaj Golub 
2084f1fca82eSMikolaj Golub static void
2085f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
2086f1fca82eSMikolaj Golub {
2087f1fca82eSMikolaj Golub 	struct proc *p;
2088f1fca82eSMikolaj Golub 	size_t size;
2089f1fca82eSMikolaj Golub 	int structsize;
2090f1fca82eSMikolaj Golub 
2091f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2092f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_numthreads *
2093f1fca82eSMikolaj Golub 	    sizeof(elf_kinfo_proc_t);
2094f1fca82eSMikolaj Golub 
2095f1fca82eSMikolaj Golub 	if (sb != NULL) {
2096f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2097f1fca82eSMikolaj Golub 		structsize = sizeof(elf_kinfo_proc_t);
2098f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
20996662ce5aSMateusz Guzik 		sx_slock(&proctree_lock);
2100f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2101f1fca82eSMikolaj Golub 		kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
21026662ce5aSMateusz Guzik 		sx_sunlock(&proctree_lock);
2103f1fca82eSMikolaj Golub 	}
2104f1fca82eSMikolaj Golub 	*sizep = size;
2105f1fca82eSMikolaj Golub }
2106f1fca82eSMikolaj Golub 
2107f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE
2108f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
2109f1fca82eSMikolaj Golub #endif
2110f1fca82eSMikolaj Golub 
2111f1fca82eSMikolaj Golub static void
2112f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
2113f1fca82eSMikolaj Golub {
2114f1fca82eSMikolaj Golub 	struct proc *p;
211514bdbaf2SConrad Meyer 	size_t size, sect_sz, i;
211614bdbaf2SConrad Meyer 	ssize_t start_len, sect_len;
211714bdbaf2SConrad Meyer 	int structsize, filedesc_flags;
211814bdbaf2SConrad Meyer 
2119bcb60d52SConrad Meyer 	if (coredump_pack_fileinfo)
212014bdbaf2SConrad Meyer 		filedesc_flags = KERN_FILEDESC_PACK_KINFO;
212114bdbaf2SConrad Meyer 	else
212214bdbaf2SConrad Meyer 		filedesc_flags = 0;
2123f1fca82eSMikolaj Golub 
2124f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
212514bdbaf2SConrad Meyer 	structsize = sizeof(struct kinfo_file);
2126f1fca82eSMikolaj Golub 	if (sb == NULL) {
2127f1fca82eSMikolaj Golub 		size = 0;
2128f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2129f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2130f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2131f1fca82eSMikolaj Golub 		PROC_LOCK(p);
213214bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2133f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2134f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2135f1fca82eSMikolaj Golub 		*sizep = size;
2136f1fca82eSMikolaj Golub 	} else {
213714bdbaf2SConrad Meyer 		sbuf_start_section(sb, &start_len);
213814bdbaf2SConrad Meyer 
2139f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2140f1fca82eSMikolaj Golub 		PROC_LOCK(p);
214114bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
214214bdbaf2SConrad Meyer 		    filedesc_flags);
214314bdbaf2SConrad Meyer 
214414bdbaf2SConrad Meyer 		sect_len = sbuf_end_section(sb, start_len, 0, 0);
214514bdbaf2SConrad Meyer 		if (sect_len < 0)
214614bdbaf2SConrad Meyer 			return;
214714bdbaf2SConrad Meyer 		sect_sz = sect_len;
214814bdbaf2SConrad Meyer 
214914bdbaf2SConrad Meyer 		KASSERT(sect_sz <= *sizep,
215014bdbaf2SConrad Meyer 		    ("kern_proc_filedesc_out did not respect maxlen; "
215114bdbaf2SConrad Meyer 		     "requested %zu, got %zu", *sizep - sizeof(structsize),
215214bdbaf2SConrad Meyer 		     sect_sz - sizeof(structsize)));
215314bdbaf2SConrad Meyer 
215414bdbaf2SConrad Meyer 		for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
215514bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
2156f1fca82eSMikolaj Golub 	}
2157f1fca82eSMikolaj Golub }
2158f1fca82eSMikolaj Golub 
2159f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE
2160f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2161f1fca82eSMikolaj Golub #endif
2162f1fca82eSMikolaj Golub 
2163f1fca82eSMikolaj Golub static void
2164f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2165f1fca82eSMikolaj Golub {
2166f1fca82eSMikolaj Golub 	struct proc *p;
2167f1fca82eSMikolaj Golub 	size_t size;
2168e6b95927SConrad Meyer 	int structsize, vmmap_flags;
2169e6b95927SConrad Meyer 
2170e6b95927SConrad Meyer 	if (coredump_pack_vmmapinfo)
2171e6b95927SConrad Meyer 		vmmap_flags = KERN_VMMAP_PACK_KINFO;
2172e6b95927SConrad Meyer 	else
2173e6b95927SConrad Meyer 		vmmap_flags = 0;
2174f1fca82eSMikolaj Golub 
2175f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2176e6b95927SConrad Meyer 	structsize = sizeof(struct kinfo_vmentry);
2177f1fca82eSMikolaj Golub 	if (sb == NULL) {
2178f1fca82eSMikolaj Golub 		size = 0;
2179f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2180f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2181f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2182f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2183e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2184f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2185f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2186f1fca82eSMikolaj Golub 		*sizep = size;
2187f1fca82eSMikolaj Golub 	} else {
2188f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2189f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2190e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2191e6b95927SConrad Meyer 		    vmmap_flags);
2192f1fca82eSMikolaj Golub 	}
2193f1fca82eSMikolaj Golub }
2194f1fca82eSMikolaj Golub 
2195f1fca82eSMikolaj Golub static void
2196f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2197f1fca82eSMikolaj Golub {
2198f1fca82eSMikolaj Golub 	struct proc *p;
2199f1fca82eSMikolaj Golub 	size_t size;
2200f1fca82eSMikolaj Golub 	int structsize;
2201f1fca82eSMikolaj Golub 
2202f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2203f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
2204f1fca82eSMikolaj Golub 	if (sb != NULL) {
2205f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2206f1fca82eSMikolaj Golub 		structsize = sizeof(gid_t);
2207f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2208f1fca82eSMikolaj Golub 		sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2209f1fca82eSMikolaj Golub 		    sizeof(gid_t));
2210f1fca82eSMikolaj Golub 	}
2211f1fca82eSMikolaj Golub 	*sizep = size;
2212f1fca82eSMikolaj Golub }
2213f1fca82eSMikolaj Golub 
2214f1fca82eSMikolaj Golub static void
2215f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2216f1fca82eSMikolaj Golub {
2217f1fca82eSMikolaj Golub 	struct proc *p;
2218f1fca82eSMikolaj Golub 	size_t size;
2219f1fca82eSMikolaj Golub 	int structsize;
2220f1fca82eSMikolaj Golub 
2221f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2222f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
2223f1fca82eSMikolaj Golub 	if (sb != NULL) {
2224f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2225f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_fd->fd_cmask);
2226f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2227f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
2228f1fca82eSMikolaj Golub 	}
2229f1fca82eSMikolaj Golub 	*sizep = size;
2230f1fca82eSMikolaj Golub }
2231f1fca82eSMikolaj Golub 
2232f1fca82eSMikolaj Golub static void
2233f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2234f1fca82eSMikolaj Golub {
2235f1fca82eSMikolaj Golub 	struct proc *p;
2236f1fca82eSMikolaj Golub 	struct rlimit rlim[RLIM_NLIMITS];
2237f1fca82eSMikolaj Golub 	size_t size;
2238f1fca82eSMikolaj Golub 	int structsize, i;
2239f1fca82eSMikolaj Golub 
2240f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2241f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(rlim);
2242f1fca82eSMikolaj Golub 	if (sb != NULL) {
2243f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2244f1fca82eSMikolaj Golub 		structsize = sizeof(rlim);
2245f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2246f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2247f1fca82eSMikolaj Golub 		for (i = 0; i < RLIM_NLIMITS; i++)
2248f6f6d240SMateusz Guzik 			lim_rlimit_proc(p, i, &rlim[i]);
2249f1fca82eSMikolaj Golub 		PROC_UNLOCK(p);
2250f1fca82eSMikolaj Golub 		sbuf_bcat(sb, rlim, sizeof(rlim));
2251f1fca82eSMikolaj Golub 	}
2252f1fca82eSMikolaj Golub 	*sizep = size;
2253f1fca82eSMikolaj Golub }
2254f1fca82eSMikolaj Golub 
2255f1fca82eSMikolaj Golub static void
2256f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2257f1fca82eSMikolaj Golub {
2258f1fca82eSMikolaj Golub 	struct proc *p;
2259f1fca82eSMikolaj Golub 	size_t size;
2260f1fca82eSMikolaj Golub 	int structsize;
2261f1fca82eSMikolaj Golub 
2262f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2263f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_osrel);
2264f1fca82eSMikolaj Golub 	if (sb != NULL) {
2265f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2266f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_osrel);
2267f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2268f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2269f1fca82eSMikolaj Golub 	}
2270f1fca82eSMikolaj Golub 	*sizep = size;
2271f1fca82eSMikolaj Golub }
2272f1fca82eSMikolaj Golub 
2273f1fca82eSMikolaj Golub static void
2274f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2275f1fca82eSMikolaj Golub {
2276f1fca82eSMikolaj Golub 	struct proc *p;
2277f1fca82eSMikolaj Golub 	elf_ps_strings_t ps_strings;
2278f1fca82eSMikolaj Golub 	size_t size;
2279f1fca82eSMikolaj Golub 	int structsize;
2280f1fca82eSMikolaj Golub 
2281f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2282f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(ps_strings);
2283f1fca82eSMikolaj Golub 	if (sb != NULL) {
2284f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2285f1fca82eSMikolaj Golub 		structsize = sizeof(ps_strings);
2286f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2287f1fca82eSMikolaj Golub 		ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2288f1fca82eSMikolaj Golub #else
2289f1fca82eSMikolaj Golub 		ps_strings = p->p_sysent->sv_psstrings;
2290f1fca82eSMikolaj Golub #endif
2291f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2292f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2293f1fca82eSMikolaj Golub 	}
2294f1fca82eSMikolaj Golub 	*sizep = size;
2295f1fca82eSMikolaj Golub }
2296f1fca82eSMikolaj Golub 
2297f1fca82eSMikolaj Golub static void
2298f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2299f1fca82eSMikolaj Golub {
2300f1fca82eSMikolaj Golub 	struct proc *p;
2301f1fca82eSMikolaj Golub 	size_t size;
2302f1fca82eSMikolaj Golub 	int structsize;
2303f1fca82eSMikolaj Golub 
2304f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2305f1fca82eSMikolaj Golub 	if (sb == NULL) {
2306f1fca82eSMikolaj Golub 		size = 0;
2307f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2308f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2309f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2310f1fca82eSMikolaj Golub 		PHOLD(p);
2311f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2312f1fca82eSMikolaj Golub 		PRELE(p);
2313f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2314f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2315f1fca82eSMikolaj Golub 		*sizep = size;
2316f1fca82eSMikolaj Golub 	} else {
2317f1fca82eSMikolaj Golub 		structsize = sizeof(Elf_Auxinfo);
2318f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2319f1fca82eSMikolaj Golub 		PHOLD(p);
2320f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2321f1fca82eSMikolaj Golub 		PRELE(p);
2322f1fca82eSMikolaj Golub 	}
2323f1fca82eSMikolaj Golub }
2324f1fca82eSMikolaj Golub 
232532c01de2SDmitry Chagin static boolean_t
23261a9c7decSKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
23271a9c7decSKonstantin Belousov     int32_t *osrel, const Elf_Phdr *pnote)
232832c01de2SDmitry Chagin {
2329267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
233032c01de2SDmitry Chagin 	const char *note_name;
23316c775eb6SKonstantin Belousov 	char *buf;
23326c775eb6SKonstantin Belousov 	int i, error;
23336c775eb6SKonstantin Belousov 	boolean_t res;
233432c01de2SDmitry Chagin 
23356c775eb6SKonstantin Belousov 	/* We need some limit, might as well use PAGE_SIZE. */
23366c775eb6SKonstantin Belousov 	if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
233732c01de2SDmitry Chagin 		return (FALSE);
23386c775eb6SKonstantin Belousov 	ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
23396c775eb6SKonstantin Belousov 	if (pnote->p_offset > PAGE_SIZE ||
23406c775eb6SKonstantin Belousov 	    pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
23416c775eb6SKonstantin Belousov 		VOP_UNLOCK(imgp->vp, 0);
23426c775eb6SKonstantin Belousov 		buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
23436c775eb6SKonstantin Belousov 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
23446c775eb6SKonstantin Belousov 		error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
23456c775eb6SKonstantin Belousov 		    pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
23466c775eb6SKonstantin Belousov 		    curthread->td_ucred, NOCRED, NULL, curthread);
23476c775eb6SKonstantin Belousov 		if (error != 0) {
23486c775eb6SKonstantin Belousov 			uprintf("i/o error PT_NOTE\n");
23496c775eb6SKonstantin Belousov 			res = FALSE;
23506c775eb6SKonstantin Belousov 			goto ret;
23516c775eb6SKonstantin Belousov 		}
23526c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)buf;
23536c775eb6SKonstantin Belousov 		note_end = (const Elf_Note *)(buf + pnote->p_filesz);
23546c775eb6SKonstantin Belousov 	} else {
23556c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)(imgp->image_header +
23566c775eb6SKonstantin Belousov 		    pnote->p_offset);
235732c01de2SDmitry Chagin 		note_end = (const Elf_Note *)(imgp->image_header +
235832c01de2SDmitry Chagin 		    pnote->p_offset + pnote->p_filesz);
23596c775eb6SKonstantin Belousov 		buf = NULL;
23606c775eb6SKonstantin Belousov 	}
2361267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2362d1ae5c83SKonstantin Belousov 		if (!aligned(note, Elf32_Addr) || (const char *)note_end -
23636c775eb6SKonstantin Belousov 		    (const char *)note < sizeof(Elf_Note)) {
23646c775eb6SKonstantin Belousov 			res = FALSE;
23656c775eb6SKonstantin Belousov 			goto ret;
23666c775eb6SKonstantin Belousov 		}
236732c01de2SDmitry Chagin 		if (note->n_namesz != checknote->hdr.n_namesz ||
236832c01de2SDmitry Chagin 		    note->n_descsz != checknote->hdr.n_descsz ||
236932c01de2SDmitry Chagin 		    note->n_type != checknote->hdr.n_type)
237032c01de2SDmitry Chagin 			goto nextnote;
237132c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
2372d1ae5c83SKonstantin Belousov 		if (note_name + checknote->hdr.n_namesz >=
2373d1ae5c83SKonstantin Belousov 		    (const char *)note_end || strncmp(checknote->vendor,
2374d1ae5c83SKonstantin Belousov 		    note_name, checknote->hdr.n_namesz) != 0)
237532c01de2SDmitry Chagin 			goto nextnote;
237632c01de2SDmitry Chagin 
237732c01de2SDmitry Chagin 		/*
237832c01de2SDmitry Chagin 		 * Fetch the osreldate for binary
237932c01de2SDmitry Chagin 		 * from the ELF OSABI-note if necessary.
238032c01de2SDmitry Chagin 		 */
238189ffc202SBjoern A. Zeeb 		if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
23826c775eb6SKonstantin Belousov 		    checknote->trans_osrel != NULL) {
23836c775eb6SKonstantin Belousov 			res = checknote->trans_osrel(note, osrel);
23846c775eb6SKonstantin Belousov 			goto ret;
23856c775eb6SKonstantin Belousov 		}
23866c775eb6SKonstantin Belousov 		res = TRUE;
23876c775eb6SKonstantin Belousov 		goto ret;
238832c01de2SDmitry Chagin nextnote:
238932c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
23901b8388cdSMikolaj Golub 		    roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
23911b8388cdSMikolaj Golub 		    roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
239232c01de2SDmitry Chagin 	}
23936c775eb6SKonstantin Belousov 	res = FALSE;
23946c775eb6SKonstantin Belousov ret:
23956c775eb6SKonstantin Belousov 	free(buf, M_TEMP);
23966c775eb6SKonstantin Belousov 	return (res);
239732c01de2SDmitry Chagin }
239832c01de2SDmitry Chagin 
239932c01de2SDmitry Chagin /*
24001a9c7decSKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote,
24011a9c7decSKonstantin Belousov  * fetch the osreldate for binary from the ELF OSABI-note. Only the
24021a9c7decSKonstantin Belousov  * first page of the image is searched, the same as for headers.
24031a9c7decSKonstantin Belousov  */
24041a9c7decSKonstantin Belousov static boolean_t
24051a9c7decSKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
24061a9c7decSKonstantin Belousov     int32_t *osrel)
24071a9c7decSKonstantin Belousov {
24081a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
24091a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
24101a9c7decSKonstantin Belousov 	int i;
24111a9c7decSKonstantin Belousov 
24121a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
24131a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
24141a9c7decSKonstantin Belousov 
24151a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
24161a9c7decSKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE &&
24171a9c7decSKonstantin Belousov 		    __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
24181a9c7decSKonstantin Belousov 			return (TRUE);
24191a9c7decSKonstantin Belousov 	}
24201a9c7decSKonstantin Belousov 	return (FALSE);
24211a9c7decSKonstantin Belousov 
24221a9c7decSKonstantin Belousov }
24231a9c7decSKonstantin Belousov 
24241a9c7decSKonstantin Belousov /*
2425e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
2426e1743d02SSøren Schmidt  */
2427a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
2428a360a43dSJake Burkholder 	__CONCAT(exec_, __elfN(imgact)),
2429a360a43dSJake Burkholder 	__XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2430a360a43dSJake Burkholder };
2431a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2432e7228204SAlfred Perlstein 
2433ed167eaaSKonstantin Belousov static vm_prot_t
2434ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
2435ed167eaaSKonstantin Belousov {
2436ed167eaaSKonstantin Belousov 	vm_prot_t prot;
2437ed167eaaSKonstantin Belousov 
2438ed167eaaSKonstantin Belousov 	prot = 0;
2439ed167eaaSKonstantin Belousov 	if (flags & PF_X)
2440ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
2441ed167eaaSKonstantin Belousov 	if (flags & PF_W)
2442ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
2443ed167eaaSKonstantin Belousov 	if (flags & PF_R)
2444ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
2445676eda08SMarcel Moolenaar #if __ELF_WORD_SIZE == 32
2446e7d939bdSMarcel Moolenaar #if defined(__amd64__)
2447126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
2448676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
2449676eda08SMarcel Moolenaar #endif
2450676eda08SMarcel Moolenaar #endif
2451ed167eaaSKonstantin Belousov 	return (prot);
2452ed167eaaSKonstantin Belousov }
2453ed167eaaSKonstantin Belousov 
2454ed167eaaSKonstantin Belousov static Elf_Word
2455ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
2456ed167eaaSKonstantin Belousov {
2457ed167eaaSKonstantin Belousov 	Elf_Word flags;
2458ed167eaaSKonstantin Belousov 
2459ed167eaaSKonstantin Belousov 	flags = 0;
2460ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
2461ed167eaaSKonstantin Belousov 		flags |= PF_X;
2462ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
2463ed167eaaSKonstantin Belousov 		flags |= PF_R;
2464ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
2465ed167eaaSKonstantin Belousov 		flags |= PF_W;
2466ed167eaaSKonstantin Belousov 	return (flags);
2467ed167eaaSKonstantin Belousov }
2468