xref: /freebsd/sys/kern/imgact_elf.c (revision 14bdbaf2e40a827e84f6c2fe27b1f8229767f03c)
1e1743d02SSøren Schmidt /*-
221a3ee0eSDavid E. O'Brien  * Copyright (c) 2000 David O'Brien
39a14aa01SUlrich Spörlein  * Copyright (c) 1995-1996 Søren Schmidt
4e1743d02SSøren Schmidt  * Copyright (c) 1996 Peter Wemm
5e1743d02SSøren Schmidt  * All rights reserved.
6e1743d02SSøren Schmidt  *
7e1743d02SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
8e1743d02SSøren Schmidt  * modification, are permitted provided that the following conditions
9e1743d02SSøren Schmidt  * are met:
10e1743d02SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
11e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
12e1743d02SSøren Schmidt  *    in this position and unchanged.
13e1743d02SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
14e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
15e1743d02SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
16e1743d02SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
1721dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
18e1743d02SSøren Schmidt  *
19e1743d02SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20e1743d02SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21e1743d02SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22e1743d02SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23e1743d02SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24e1743d02SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e1743d02SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e1743d02SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e1743d02SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28e1743d02SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e1743d02SSøren Schmidt  */
30e1743d02SSøren Schmidt 
31677b542eSDavid E. O'Brien #include <sys/cdefs.h>
32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
33677b542eSDavid E. O'Brien 
3412bc222eSJonathan Anderson #include "opt_capsicum.h"
3562919d78SPeter Wemm #include "opt_compat.h"
36aa14e9b7SMark Johnston #include "opt_gzio.h"
3762919d78SPeter Wemm 
38e1743d02SSøren Schmidt #include <sys/param.h>
394a144410SRobert Watson #include <sys/capsicum.h>
40e1743d02SSøren Schmidt #include <sys/exec.h>
418c64af4fSJohn Polstra #include <sys/fcntl.h>
42aa14e9b7SMark Johnston #include <sys/gzio.h>
43e1743d02SSøren Schmidt #include <sys/imgact.h>
44e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
45b96bd95bSIan Lepore #include <sys/jail.h>
46e1743d02SSøren Schmidt #include <sys/kernel.h>
47f34fa851SJohn Baldwin #include <sys/lock.h>
48e1743d02SSøren Schmidt #include <sys/malloc.h>
4968ff2a43SChristian S.J. Peron #include <sys/mount.h>
508c64af4fSJohn Polstra #include <sys/mman.h>
51a794e791SBruce Evans #include <sys/namei.h>
528c64af4fSJohn Polstra #include <sys/pioctl.h>
53a794e791SBruce Evans #include <sys/proc.h>
548c64af4fSJohn Polstra #include <sys/procfs.h>
551ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
568c64af4fSJohn Polstra #include <sys/resourcevar.h>
5789f6b863SAttilio Rao #include <sys/rwlock.h>
58bd390213SMikolaj Golub #include <sys/sbuf.h>
59da61b9a6SAlan Cox #include <sys/sf_buf.h>
60ee235befSKonstantin Belousov #include <sys/smp.h>
6136240ea5SDoug Rabson #include <sys/systm.h>
62e1743d02SSøren Schmidt #include <sys/signalvar.h>
638c64af4fSJohn Polstra #include <sys/stat.h>
641005a129SJohn Baldwin #include <sys/sx.h>
658c64af4fSJohn Polstra #include <sys/syscall.h>
66e1743d02SSøren Schmidt #include <sys/sysctl.h>
678c64af4fSJohn Polstra #include <sys/sysent.h>
68a794e791SBruce Evans #include <sys/vnode.h>
69e7228204SAlfred Perlstein #include <sys/syslog.h>
70e7228204SAlfred Perlstein #include <sys/eventhandler.h>
71f1fca82eSMikolaj Golub #include <sys/user.h>
72e7228204SAlfred Perlstein 
73e1743d02SSøren Schmidt #include <vm/vm.h>
74e1743d02SSøren Schmidt #include <vm/vm_kern.h>
75e1743d02SSøren Schmidt #include <vm/vm_param.h>
76e1743d02SSøren Schmidt #include <vm/pmap.h>
77e1743d02SSøren Schmidt #include <vm/vm_map.h>
780ff27d31SJohn Polstra #include <vm/vm_object.h>
79e1743d02SSøren Schmidt #include <vm/vm_extern.h>
80e1743d02SSøren Schmidt 
8152c24af7SPeter Wemm #include <machine/elf.h>
82e1743d02SSøren Schmidt #include <machine/md_var.h>
83e1743d02SSøren Schmidt 
841b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE	4
85c815a20cSDavid E. O'Brien #define OLD_EI_BRAND	8
86c815a20cSDavid E. O'Brien 
873ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr);
8832c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
89d1ae5c83SKonstantin Belousov     const char *interp, int interp_name_len, int32_t *osrel);
903ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
913ebc1248SPeter Wemm     u_long *entry, size_t pagesize);
92292177e6SAlan Cox static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
93292177e6SAlan Cox     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
94292177e6SAlan Cox     size_t pagesize);
953ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
9689ffc202SBjoern A. Zeeb static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
9789ffc202SBjoern A. Zeeb     int32_t *osrel);
9889ffc202SBjoern A. Zeeb static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
9932c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp,
10032c01de2SDmitry Chagin     Elf_Brandnote *checknote, int32_t *osrel);
101ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word);
102ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t);
103e1743d02SSøren Schmidt 
104a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
105a360a43dSJake Burkholder     "");
106a360a43dSJake Burkholder 
107bd390213SMikolaj Golub #define	CORE_BUF_SIZE	(16 * 1024)
108e7228204SAlfred Perlstein 
109e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1;
110e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
111af3b2549SHans Petter Selasky     fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
112a360a43dSJake Burkholder     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
113a360a43dSJake Burkholder 
114551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0;
115a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
116551d79e1SMarcel Moolenaar     &elf_legacy_coredump, 0, "");
117e1743d02SSøren Schmidt 
11862c625fdSKonstantin Belousov int __elfN(nxstack) =
11962c625fdSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
12062c625fdSKonstantin Belousov 	1;
12162c625fdSKonstantin Belousov #else
12262c625fdSKonstantin Belousov 	0;
12362c625fdSKonstantin Belousov #endif
124291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
125291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
126291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
127291c06a1SKonstantin Belousov 
128126b36a2SKonstantin Belousov #if __ELF_WORD_SIZE == 32
129e7d939bdSMarcel Moolenaar #if defined(__amd64__)
130126b36a2SKonstantin Belousov int i386_read_exec = 0;
131126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
132126b36a2SKonstantin Belousov     "enable execution from readable segments");
133126b36a2SKonstantin Belousov #endif
134126b36a2SKonstantin Belousov #endif
135126b36a2SKonstantin Belousov 
1363ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
137e1743d02SSøren Schmidt 
13893d1c728SKonstantin Belousov #define	trunc_page_ps(va, ps)	((va) & ~(ps - 1))
13993d1c728SKonstantin Belousov #define	round_page_ps(va, ps)	(((va) + (ps - 1)) & ~(ps - 1))
14093d1c728SKonstantin Belousov #define	aligned(a, t)	(trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
14193d1c728SKonstantin Belousov 
14232c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
14332c01de2SDmitry Chagin 
14432c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = {
14532c01de2SDmitry Chagin 	.hdr.n_namesz	= sizeof(FREEBSD_ABI_VENDOR),
14632c01de2SDmitry Chagin 	.hdr.n_descsz	= sizeof(int32_t),
14732c01de2SDmitry Chagin 	.hdr.n_type	= 1,
14832c01de2SDmitry Chagin 	.vendor		= FREEBSD_ABI_VENDOR,
14989ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
15089ffc202SBjoern A. Zeeb 	.trans_osrel	= __elfN(freebsd_trans_osrel)
15132c01de2SDmitry Chagin };
15232c01de2SDmitry Chagin 
15389ffc202SBjoern A. Zeeb static boolean_t
15489ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
15589ffc202SBjoern A. Zeeb {
15689ffc202SBjoern A. Zeeb 	uintptr_t p;
15789ffc202SBjoern A. Zeeb 
15889ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1591b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
16089ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
16189ffc202SBjoern A. Zeeb 
16289ffc202SBjoern A. Zeeb 	return (TRUE);
16389ffc202SBjoern A. Zeeb }
16489ffc202SBjoern A. Zeeb 
16589ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU";
16689ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3;
16789ffc202SBjoern A. Zeeb 
16889ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = {
16989ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
17089ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
17189ffc202SBjoern A. Zeeb 	.hdr.n_type	= 1,
17289ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
17389ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
17489ffc202SBjoern A. Zeeb 	.trans_osrel	= kfreebsd_trans_osrel
17589ffc202SBjoern A. Zeeb };
17689ffc202SBjoern A. Zeeb 
17789ffc202SBjoern A. Zeeb static boolean_t
17889ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
17989ffc202SBjoern A. Zeeb {
18089ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
18189ffc202SBjoern A. Zeeb 	uintptr_t p;
18289ffc202SBjoern A. Zeeb 
18389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1841b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
18589ffc202SBjoern A. Zeeb 
18689ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
18789ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
18889ffc202SBjoern A. Zeeb 		return (FALSE);
18989ffc202SBjoern A. Zeeb 
19089ffc202SBjoern A. Zeeb 	/*
19189ffc202SBjoern A. Zeeb 	 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
19289ffc202SBjoern A. Zeeb 	 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
19389ffc202SBjoern A. Zeeb 	 */
19489ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
19589ffc202SBjoern A. Zeeb 
19689ffc202SBjoern A. Zeeb 	return (TRUE);
19789ffc202SBjoern A. Zeeb }
19889ffc202SBjoern A. Zeeb 
199e1743d02SSøren Schmidt int
2003ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
201e1743d02SSøren Schmidt {
202e1743d02SSøren Schmidt 	int i;
203e1743d02SSøren Schmidt 
2043ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
205ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == NULL) {
206ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = entry;
207e1743d02SSøren Schmidt 			break;
208e1743d02SSøren Schmidt 		}
209e1743d02SSøren Schmidt 	}
210925c8b5bSBjoern A. Zeeb 	if (i == MAX_BRANDS) {
211925c8b5bSBjoern A. Zeeb 		printf("WARNING: %s: could not insert brandinfo entry: %p\n",
212925c8b5bSBjoern A. Zeeb 			__func__, entry);
213a7cddfedSJake Burkholder 		return (-1);
214925c8b5bSBjoern A. Zeeb 	}
215a7cddfedSJake Burkholder 	return (0);
216e1743d02SSøren Schmidt }
217e1743d02SSøren Schmidt 
218e1743d02SSøren Schmidt int
2193ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
220e1743d02SSøren Schmidt {
221e1743d02SSøren Schmidt 	int i;
222e1743d02SSøren Schmidt 
2233ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
224ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == entry) {
225ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = NULL;
226e1743d02SSøren Schmidt 			break;
227e1743d02SSøren Schmidt 		}
228e1743d02SSøren Schmidt 	}
229ea5a2b2eSSøren Schmidt 	if (i == MAX_BRANDS)
230a7cddfedSJake Burkholder 		return (-1);
231a7cddfedSJake Burkholder 	return (0);
232e1743d02SSøren Schmidt }
233e1743d02SSøren Schmidt 
234096977faSMark Newton int
2353ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry)
236096977faSMark Newton {
237096977faSMark Newton 	struct proc *p;
238553629ebSJake Burkholder 	int rval = FALSE;
239096977faSMark Newton 
2401005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2414f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
242553629ebSJake Burkholder 		if (p->p_sysent == entry->sysvec) {
243553629ebSJake Burkholder 			rval = TRUE;
244553629ebSJake Burkholder 			break;
245096977faSMark Newton 		}
246553629ebSJake Burkholder 	}
2471005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
248096977faSMark Newton 
249553629ebSJake Burkholder 	return (rval);
250096977faSMark Newton }
251096977faSMark Newton 
2525fe3ed62SJake Burkholder static Elf_Brandinfo *
25332c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
254d1ae5c83SKonstantin Belousov     int interp_name_len, int32_t *osrel)
2555fe3ed62SJake Burkholder {
25632c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
2575fe3ed62SJake Burkholder 	Elf_Brandinfo *bi;
25832c01de2SDmitry Chagin 	boolean_t ret;
2595fe3ed62SJake Burkholder 	int i;
2605fe3ed62SJake Burkholder 
2615fe3ed62SJake Burkholder 	/*
26232c01de2SDmitry Chagin 	 * We support four types of branding -- (1) the ELF EI_OSABI field
2635fe3ed62SJake Burkholder 	 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
26432c01de2SDmitry Chagin 	 * branding w/in the ELF header, (3) path of the `interp_path'
26532c01de2SDmitry Chagin 	 * field, and (4) the ".note.ABI-tag" ELF section.
2665fe3ed62SJake Burkholder 	 */
2675fe3ed62SJake Burkholder 
26832c01de2SDmitry Chagin 	/* Look for an ".note.ABI-tag" ELF section */
26932c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
27032c01de2SDmitry Chagin 		bi = elf_brand_list[i];
271ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
272ecc2fda8SBjoern A. Zeeb 			continue;
273ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
274ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
27532c01de2SDmitry Chagin 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
27632c01de2SDmitry Chagin 			if (ret)
27732c01de2SDmitry Chagin 				return (bi);
27832c01de2SDmitry Chagin 		}
27932c01de2SDmitry Chagin 	}
28032c01de2SDmitry Chagin 
2815fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
2825fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
2835fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
284ecc2fda8SBjoern A. Zeeb 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
285ecc2fda8SBjoern A. Zeeb 			continue;
286ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
2875fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
2885fe3ed62SJake Burkholder 		    strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
2895fe3ed62SJake Burkholder 		    bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
2905fe3ed62SJake Burkholder 			return (bi);
2915fe3ed62SJake Burkholder 	}
2925fe3ed62SJake Burkholder 
293817dc004SWarner Losh 	/* No known brand, see if the header is recognized by any brand */
294817dc004SWarner Losh 	for (i = 0; i < MAX_BRANDS; i++) {
295817dc004SWarner Losh 		bi = elf_brand_list[i];
296817dc004SWarner Losh 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
297817dc004SWarner Losh 		    bi->header_supported == NULL)
298817dc004SWarner Losh 			continue;
299817dc004SWarner Losh 		if (hdr->e_machine == bi->machine) {
300817dc004SWarner Losh 			ret = bi->header_supported(imgp);
301817dc004SWarner Losh 			if (ret)
302817dc004SWarner Losh 				return (bi);
303817dc004SWarner Losh 		}
304817dc004SWarner Losh 	}
305817dc004SWarner Losh 
3065fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
3075fe3ed62SJake Burkholder 	if (interp != NULL) {
3085fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
3095fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
310ecc2fda8SBjoern A. Zeeb 			if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
311ecc2fda8SBjoern A. Zeeb 				continue;
312ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
313d1ae5c83SKonstantin Belousov 			    /* ELF image p_filesz includes terminating zero */
314d1ae5c83SKonstantin Belousov 			    strlen(bi->interp_path) + 1 == interp_name_len &&
315d1ae5c83SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
316d1ae5c83SKonstantin Belousov 			    == 0)
3175fe3ed62SJake Burkholder 				return (bi);
3185fe3ed62SJake Burkholder 		}
3195fe3ed62SJake Burkholder 	}
3205fe3ed62SJake Burkholder 
3215fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
3225fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3235fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
324ecc2fda8SBjoern A. Zeeb 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
325ecc2fda8SBjoern A. Zeeb 			continue;
326ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
327e548a1d4SJake Burkholder 		    __elfN(fallback_brand) == bi->brand)
3285fe3ed62SJake Burkholder 			return (bi);
3295fe3ed62SJake Burkholder 	}
3305fe3ed62SJake Burkholder 	return (NULL);
3315fe3ed62SJake Burkholder }
3325fe3ed62SJake Burkholder 
333e1743d02SSøren Schmidt static int
3343ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
335e1743d02SSøren Schmidt {
336d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
3373ebc1248SPeter Wemm 	int i;
3383ebc1248SPeter Wemm 
33952c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
34052c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
34152c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
3423dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
3433dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
3443dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
345a7cddfedSJake Burkholder 		return (ENOEXEC);
346e1743d02SSøren Schmidt 
3473ebc1248SPeter Wemm 	/*
3483ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
3493ebc1248SPeter Wemm 	 */
3503ebc1248SPeter Wemm 
3513ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
352d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
353d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
3543ebc1248SPeter Wemm 			break;
3553ebc1248SPeter Wemm 	}
3563ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
357a7cddfedSJake Burkholder 		return (ENOEXEC);
358e1743d02SSøren Schmidt 
359a7cddfedSJake Burkholder 	return (0);
360e1743d02SSøren Schmidt }
361e1743d02SSøren Schmidt 
362e1743d02SSøren Schmidt static int
3633ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
364ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
3653ebc1248SPeter Wemm {
366da61b9a6SAlan Cox 	struct sf_buf *sf;
367da61b9a6SAlan Cox 	int error;
3683ebc1248SPeter Wemm 	vm_offset_t off;
3693ebc1248SPeter Wemm 
3703ebc1248SPeter Wemm 	/*
3713ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
3723ebc1248SPeter Wemm 	 */
3733ebc1248SPeter Wemm 	vm_map_lock(map);
374ff6f03c7SAlan Cox 	vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
375ff6f03c7SAlan Cox 	    VM_PROT_ALL, VM_PROT_ALL, 0);
3763ebc1248SPeter Wemm 	vm_map_unlock(map);
3773ebc1248SPeter Wemm 
3783ebc1248SPeter Wemm 	/*
3793ebc1248SPeter Wemm 	 * Find the page from the underlying object.
3803ebc1248SPeter Wemm 	 */
3813ebc1248SPeter Wemm 	if (object) {
382da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
383da61b9a6SAlan Cox 		if (sf == NULL)
384da61b9a6SAlan Cox 			return (KERN_FAILURE);
3853ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
386da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
387ca0387efSJake Burkholder 		    end - start);
388be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
3893ebc1248SPeter Wemm 		if (error) {
390a7cddfedSJake Burkholder 			return (KERN_FAILURE);
3913ebc1248SPeter Wemm 		}
3923ebc1248SPeter Wemm 	}
3933ebc1248SPeter Wemm 
394a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
3953ebc1248SPeter Wemm }
3963ebc1248SPeter Wemm 
3973ebc1248SPeter Wemm static int
3983ebc1248SPeter Wemm __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
399ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
4003ebc1248SPeter Wemm {
401da61b9a6SAlan Cox 	struct sf_buf *sf;
402da61b9a6SAlan Cox 	vm_offset_t off;
403a063facbSMarcel Moolenaar 	vm_size_t sz;
404a063facbSMarcel Moolenaar 	int error, rv;
4053ebc1248SPeter Wemm 
4063ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
40781f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
408ff6f03c7SAlan Cox 		    round_page(start), prot);
4093ebc1248SPeter Wemm 		if (rv)
410a7cddfedSJake Burkholder 			return (rv);
4113ebc1248SPeter Wemm 		offset += round_page(start) - start;
4123ebc1248SPeter Wemm 		start = round_page(start);
4133ebc1248SPeter Wemm 	}
4143ebc1248SPeter Wemm 	if (end != round_page(end)) {
41581f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
416ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
4173ebc1248SPeter Wemm 		if (rv)
418a7cddfedSJake Burkholder 			return (rv);
4193ebc1248SPeter Wemm 		end = trunc_page(end);
4203ebc1248SPeter Wemm 	}
4213ebc1248SPeter Wemm 	if (end > start) {
4223ebc1248SPeter Wemm 		if (offset & PAGE_MASK) {
4233ebc1248SPeter Wemm 			/*
4243ebc1248SPeter Wemm 			 * The mapping is not page aligned. This means we have
4253ebc1248SPeter Wemm 			 * to copy the data. Sigh.
4263ebc1248SPeter Wemm 			 */
427edb572a3SJohn Baldwin 			rv = vm_map_find(map, NULL, 0, &start, end - start, 0,
428edb572a3SJohn Baldwin 			    VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL,
429edb572a3SJohn Baldwin 			    0);
4303ebc1248SPeter Wemm 			if (rv)
431a7cddfedSJake Burkholder 				return (rv);
432da61b9a6SAlan Cox 			if (object == NULL)
433da61b9a6SAlan Cox 				return (KERN_SUCCESS);
434da61b9a6SAlan Cox 			for (; start < end; start += sz) {
435da61b9a6SAlan Cox 				sf = vm_imgact_map_page(object, offset);
436da61b9a6SAlan Cox 				if (sf == NULL)
437da61b9a6SAlan Cox 					return (KERN_FAILURE);
4383ebc1248SPeter Wemm 				off = offset - trunc_page(offset);
4393ebc1248SPeter Wemm 				sz = end - start;
440da61b9a6SAlan Cox 				if (sz > PAGE_SIZE - off)
441da61b9a6SAlan Cox 					sz = PAGE_SIZE - off;
442da61b9a6SAlan Cox 				error = copyout((caddr_t)sf_buf_kva(sf) + off,
4433ebc1248SPeter Wemm 				    (caddr_t)start, sz);
444be996836SAttilio Rao 				vm_imgact_unmap_page(sf);
4453ebc1248SPeter Wemm 				if (error) {
446a7cddfedSJake Burkholder 					return (KERN_FAILURE);
4473ebc1248SPeter Wemm 				}
448da61b9a6SAlan Cox 				offset += sz;
4493ebc1248SPeter Wemm 			}
4503ebc1248SPeter Wemm 			rv = KERN_SUCCESS;
4513ebc1248SPeter Wemm 		} else {
452e5e6093bSAlan Cox 			vm_object_reference(object);
4533ebc1248SPeter Wemm 			vm_map_lock(map);
4543ebc1248SPeter Wemm 			rv = vm_map_insert(map, object, offset, start, end,
455ff6f03c7SAlan Cox 			    prot, VM_PROT_ALL, cow);
4563ebc1248SPeter Wemm 			vm_map_unlock(map);
457e5e6093bSAlan Cox 			if (rv != KERN_SUCCESS)
458e5e6093bSAlan Cox 				vm_object_deallocate(object);
4593ebc1248SPeter Wemm 		}
460a7cddfedSJake Burkholder 		return (rv);
4613ebc1248SPeter Wemm 	} else {
462a7cddfedSJake Burkholder 		return (KERN_SUCCESS);
4633ebc1248SPeter Wemm 	}
4643ebc1248SPeter Wemm }
4653ebc1248SPeter Wemm 
4663ebc1248SPeter Wemm static int
467292177e6SAlan Cox __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
4683ebc1248SPeter Wemm     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
4693ebc1248SPeter Wemm     size_t pagesize)
470e1743d02SSøren Schmidt {
471da61b9a6SAlan Cox 	struct sf_buf *sf;
472e1743d02SSøren Schmidt 	size_t map_len;
473292177e6SAlan Cox 	vm_map_t map;
474292177e6SAlan Cox 	vm_object_t object;
475e1743d02SSøren Schmidt 	vm_offset_t map_addr;
476fa7dd9c5SMatthew Dillon 	int error, rv, cow;
477e1743d02SSøren Schmidt 	size_t copy_len;
47852c24af7SPeter Wemm 	vm_offset_t file_addr;
47952c24af7SPeter Wemm 
48025ead034SBrian Feldman 	/*
48125ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
48225ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
48325ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
48425ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
48525ead034SBrian Feldman 	 *
48625ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
48725ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
48825ead034SBrian Feldman 	 */
489292177e6SAlan Cox 	if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
49025ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
49125ead034SBrian Feldman 		return (ENOEXEC);
49225ead034SBrian Feldman 	}
49325ead034SBrian Feldman 
494292177e6SAlan Cox 	object = imgp->object;
495292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
4963ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
4973ebc1248SPeter Wemm 	file_addr = trunc_page_ps(offset, pagesize);
498e1743d02SSøren Schmidt 
499e1743d02SSøren Schmidt 	/*
50052c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
50152c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
50252c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
50352c24af7SPeter Wemm 	 * choose the second..
50452c24af7SPeter Wemm 	 */
50552c24af7SPeter Wemm 	if (memsz > filsz)
5063ebc1248SPeter Wemm 		map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
50752c24af7SPeter Wemm 	else
5083ebc1248SPeter Wemm 		map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
50952c24af7SPeter Wemm 
51052c24af7SPeter Wemm 	if (map_len != 0) {
511fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
512fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
513fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
514fa7dd9c5SMatthew Dillon 
515292177e6SAlan Cox 		rv = __elfN(map_insert)(map,
51652c24af7SPeter Wemm 				      object,
51752c24af7SPeter Wemm 				      file_addr,	/* file offset */
51852c24af7SPeter Wemm 				      map_addr,		/* virtual start */
51952c24af7SPeter Wemm 				      map_addr + map_len,/* virtual end */
52052c24af7SPeter Wemm 				      prot,
521fa7dd9c5SMatthew Dillon 				      cow);
522e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
523a7cddfedSJake Burkholder 			return (EINVAL);
52452c24af7SPeter Wemm 
52552c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
52623955314SAlfred Perlstein 		if (memsz == filsz) {
527a7cddfedSJake Burkholder 			return (0);
52852c24af7SPeter Wemm 		}
52923955314SAlfred Perlstein 	}
53052c24af7SPeter Wemm 
53152c24af7SPeter Wemm 
53252c24af7SPeter Wemm 	/*
53352c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
53452c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
53552c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
53652c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
537e1743d02SSøren Schmidt 	 */
5383ebc1248SPeter Wemm 	copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
5393ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
540ca0387efSJake Burkholder 	map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
541ca0387efSJake Burkholder 	    map_addr;
542e1743d02SSøren Schmidt 
54352c24af7SPeter Wemm 	/* This had damn well better be true! */
5448191d577SPeter Wemm 	if (map_len != 0) {
545292177e6SAlan Cox 		rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
546292177e6SAlan Cox 		    map_len, VM_PROT_ALL, 0);
54723955314SAlfred Perlstein 		if (rv != KERN_SUCCESS) {
548a7cddfedSJake Burkholder 			return (EINVAL);
5498191d577SPeter Wemm 		}
55023955314SAlfred Perlstein 	}
551e1743d02SSøren Schmidt 
55252c24af7SPeter Wemm 	if (copy_len != 0) {
5533ebc1248SPeter Wemm 		vm_offset_t off;
554da61b9a6SAlan Cox 
555da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
556da61b9a6SAlan Cox 		if (sf == NULL)
557da61b9a6SAlan Cox 			return (EIO);
558e1743d02SSøren Schmidt 
55952c24af7SPeter Wemm 		/* send the page fragment to user space */
56081f223caSJake Burkholder 		off = trunc_page_ps(offset + filsz, pagesize) -
56181f223caSJake Burkholder 		    trunc_page(offset + filsz);
562da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off,
563da61b9a6SAlan Cox 		    (caddr_t)map_addr, copy_len);
564be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
56523955314SAlfred Perlstein 		if (error) {
56652c24af7SPeter Wemm 			return (error);
56752c24af7SPeter Wemm 		}
56823955314SAlfred Perlstein 	}
569e1743d02SSøren Schmidt 
570e1743d02SSøren Schmidt 	/*
5713ebc1248SPeter Wemm 	 * set it to the specified protection.
5723ebc1248SPeter Wemm 	 * XXX had better undo the damage from pasting over the cracks here!
573e1743d02SSøren Schmidt 	 */
574292177e6SAlan Cox 	vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
575292177e6SAlan Cox 	    map_len), prot, FALSE);
5768191d577SPeter Wemm 
577ff6f03c7SAlan Cox 	return (0);
578e1743d02SSøren Schmidt }
579e1743d02SSøren Schmidt 
580c33fe779SJohn Polstra /*
581c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
582c33fe779SJohn Polstra  * or an executable.
583c33fe779SJohn Polstra  *
584c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
585c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
586c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
587c33fe779SJohn Polstra  * where the file was actually loaded.
588c33fe779SJohn Polstra  *
589c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
590c33fe779SJohn Polstra  * the entry point for the loaded file.
591c33fe779SJohn Polstra  */
592e1743d02SSøren Schmidt static int
5933ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
5943ebc1248SPeter Wemm 	u_long *entry, size_t pagesize)
595e1743d02SSøren Schmidt {
596911c2be0SMark Peek 	struct {
597911c2be0SMark Peek 		struct nameidata nd;
598911c2be0SMark Peek 		struct vattr attr;
599911c2be0SMark Peek 		struct image_params image_params;
600911c2be0SMark Peek 	} *tempdata;
601d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
602d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
603911c2be0SMark Peek 	struct nameidata *nd;
604911c2be0SMark Peek 	struct vattr *attr;
605911c2be0SMark Peek 	struct image_params *imgp;
60652c24af7SPeter Wemm 	vm_prot_t prot;
607c33fe779SJohn Polstra 	u_long rbase;
608c33fe779SJohn Polstra 	u_long base_addr = 0;
6095050aa86SKonstantin Belousov 	int error, i, numsegs;
610e1743d02SSøren Schmidt 
61112bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
61212bc222eSJonathan Anderson 	/*
61312bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
61412bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
61512bc222eSJonathan Anderson 	 */
61612bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
61712bc222eSJonathan Anderson 		return (ECAPMODE);
61812bc222eSJonathan Anderson #endif
61912bc222eSJonathan Anderson 
620a163d034SWarner Losh 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
621911c2be0SMark Peek 	nd = &tempdata->nd;
622911c2be0SMark Peek 	attr = &tempdata->attr;
623911c2be0SMark Peek 	imgp = &tempdata->image_params;
624911c2be0SMark Peek 
625c8a79999SPeter Wemm 	/*
626c8a79999SPeter Wemm 	 * Initialize part of the common data
627c8a79999SPeter Wemm 	 */
628c8a79999SPeter Wemm 	imgp->proc = p;
629911c2be0SMark Peek 	imgp->attr = attr;
630c8a79999SPeter Wemm 	imgp->firstpage = NULL;
63159c8bc40SAlan Cox 	imgp->image_header = NULL;
6320b2ed1aeSJeff Roberson 	imgp->object = NULL;
6336d7bdc8dSRobert Watson 	imgp->execlabel = NULL;
634c8a79999SPeter Wemm 
6355050aa86SKonstantin Belousov 	NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
636911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
637911c2be0SMark Peek 		nd->ni_vp = NULL;
638e1743d02SSøren Schmidt 		goto fail;
639e1743d02SSøren Schmidt 	}
640911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
641911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
642c8a79999SPeter Wemm 
643e1743d02SSøren Schmidt 	/*
644e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
645e1743d02SSøren Schmidt 	 */
646c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
647373d1a3fSAlan Cox 	if (error)
648c8a79999SPeter Wemm 		goto fail;
649e1743d02SSøren Schmidt 
650c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
651373d1a3fSAlan Cox 	if (error)
652373d1a3fSAlan Cox 		goto fail;
653373d1a3fSAlan Cox 
65425ead034SBrian Feldman 	/*
65525ead034SBrian Feldman 	 * Also make certain that the interpreter stays the same, so set
656e6e370a7SJeff Roberson 	 * its VV_TEXT flag, too.
65725ead034SBrian Feldman 	 */
658877d24acSKonstantin Belousov 	VOP_SET_TEXT(nd->ni_vp);
659e6e370a7SJeff Roberson 
6608516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
661e1743d02SSøren Schmidt 
662d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
6633ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
664e1743d02SSøren Schmidt 		goto fail;
665c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
666c33fe779SJohn Polstra 		rbase = *addr;
667c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
668c33fe779SJohn Polstra 		rbase = 0;
669c33fe779SJohn Polstra 	else {
670c33fe779SJohn Polstra 		error = ENOEXEC;
671c33fe779SJohn Polstra 		goto fail;
672c33fe779SJohn Polstra 	}
673e1743d02SSøren Schmidt 
674c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
67552c24af7SPeter Wemm 	if ((hdr->e_phoff > PAGE_SIZE) ||
676d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
677c8a79999SPeter Wemm 		error = ENOEXEC;
678e1743d02SSøren Schmidt 		goto fail;
679c8a79999SPeter Wemm 	}
680c8a79999SPeter Wemm 
681d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
68293d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
68393d1c728SKonstantin Belousov 		error = ENOEXEC;
68493d1c728SKonstantin Belousov 		goto fail;
68593d1c728SKonstantin Belousov 	}
686e1743d02SSøren Schmidt 
687c33fe779SJohn Polstra 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
6885b33842aSKonstantin Belousov 		if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
6895b33842aSKonstantin Belousov 			/* Loadable segment */
690ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
691292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
69281f223caSJake Burkholder 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
693292177e6SAlan Cox 			    phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
694292177e6SAlan Cox 			if (error != 0)
695e1743d02SSøren Schmidt 				goto fail;
696e1743d02SSøren Schmidt 			/*
697c33fe779SJohn Polstra 			 * Establish the base address if this is the
698c33fe779SJohn Polstra 			 * first segment.
699e1743d02SSøren Schmidt 			 */
700c33fe779SJohn Polstra 			if (numsegs == 0)
701ca0387efSJake Burkholder   				base_addr = trunc_page(phdr[i].p_vaddr +
702ca0387efSJake Burkholder 				    rbase);
703c33fe779SJohn Polstra 			numsegs++;
704e1743d02SSøren Schmidt 		}
705e1743d02SSøren Schmidt 	}
706c33fe779SJohn Polstra 	*addr = base_addr;
707c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
708e1743d02SSøren Schmidt 
709e1743d02SSøren Schmidt fail:
710c8a79999SPeter Wemm 	if (imgp->firstpage)
711c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
7120b2ed1aeSJeff Roberson 
713911c2be0SMark Peek 	if (nd->ni_vp)
714373d1a3fSAlan Cox 		vput(nd->ni_vp);
715911c2be0SMark Peek 
716911c2be0SMark Peek 	free(tempdata, M_TEMP);
717e1743d02SSøren Schmidt 
718a7cddfedSJake Burkholder 	return (error);
719e1743d02SSøren Schmidt }
720e1743d02SSøren Schmidt 
721303b270bSEivind Eklund static int
7223ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
723e1743d02SSøren Schmidt {
724ecbb00a2SDoug Rabson 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
72532c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
726e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
7275856e12eSJohn Dyson 	struct vmspace *vmspace;
72852c24af7SPeter Wemm 	vm_prot_t prot;
72921c2d047SMatthew Dillon 	u_long text_size = 0, data_size = 0, total_size = 0;
730e1743d02SSøren Schmidt 	u_long text_addr = 0, data_addr = 0;
731cac45152SMatthew Dillon 	u_long seg_size, seg_addr;
7327564c4adSKonstantin Belousov 	u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
73332c01de2SDmitry Chagin 	int32_t osrel = 0;
734d1ae5c83SKonstantin Belousov 	int error = 0, i, n, interp_name_len = 0;
7356b16d664SEd Maste 	const char *err_str = NULL, *interp = NULL, *newinterp = NULL;
736d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
737911c2be0SMark Peek 	char *path;
7385fe3ed62SJake Burkholder 	struct sysentvec *sv;
739e1743d02SSøren Schmidt 
740e1743d02SSøren Schmidt 	/*
741e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
742900b28f9SMaxim Sobolev 	 *
743900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
744900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
745e1743d02SSøren Schmidt 	 */
746900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
747900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
748a7cddfedSJake Burkholder 		return (-1);
749e1743d02SSøren Schmidt 
750e1743d02SSøren Schmidt 	/*
751e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
752e1743d02SSøren Schmidt 	 * detected an ELF file.
753e1743d02SSøren Schmidt 	 */
754e1743d02SSøren Schmidt 
755e1743d02SSøren Schmidt 	if ((hdr->e_phoff > PAGE_SIZE) ||
756d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
757c8a79999SPeter Wemm 		/* Only support headers in first page for now */
7586b16d664SEd Maste 		uprintf("Program headers not in the first page\n");
759a7cddfedSJake Burkholder 		return (ENOEXEC);
760e1743d02SSøren Schmidt 	}
76152c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
7626b16d664SEd Maste 	if (!aligned(phdr, Elf_Addr)) {
7636b16d664SEd Maste 		uprintf("Unaligned program headers\n");
76493d1c728SKonstantin Belousov 		return (ENOEXEC);
7656b16d664SEd Maste 	}
7667564c4adSKonstantin Belousov 	n = 0;
7677564c4adSKonstantin Belousov 	baddr = 0;
7685fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
769291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
770291c06a1SKonstantin Belousov 		case PT_LOAD:
7717564c4adSKonstantin Belousov 			if (n == 0)
7727564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
7737564c4adSKonstantin Belousov 			n++;
774291c06a1SKonstantin Belousov 			break;
775291c06a1SKonstantin Belousov 		case PT_INTERP:
776e5e6093bSAlan Cox 			/* Path to interpreter */
7775fe3ed62SJake Burkholder 			if (phdr[i].p_filesz > MAXPATHLEN ||
778d19d5bf4STijl Coosemans 			    phdr[i].p_offset > PAGE_SIZE ||
7796b16d664SEd Maste 			    phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset) {
7806b16d664SEd Maste 				uprintf("Invalid PT_INTERP\n");
78160bb3943SAlan Cox 				return (ENOEXEC);
7826b16d664SEd Maste 			}
7835fe3ed62SJake Burkholder 			interp = imgp->image_header + phdr[i].p_offset;
784d1ae5c83SKonstantin Belousov 			interp_name_len = phdr[i].p_filesz;
785291c06a1SKonstantin Belousov 			break;
786291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
787291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
788291c06a1SKonstantin Belousov 				imgp->stack_prot =
789291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
790316b3843SKonstantin Belousov 			imgp->stack_sz = phdr[i].p_memsz;
791291c06a1SKonstantin Belousov 			break;
7923ebc1248SPeter Wemm 		}
7933ebc1248SPeter Wemm 	}
7943ebc1248SPeter Wemm 
795d1ae5c83SKonstantin Belousov 	brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
796d1ae5c83SKonstantin Belousov 	    &osrel);
7975fe3ed62SJake Burkholder 	if (brand_info == NULL) {
7985fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
7995fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
80060bb3943SAlan Cox 		return (ENOEXEC);
8013ebc1248SPeter Wemm 	}
802ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
8036b16d664SEd Maste 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
8046b16d664SEd Maste 			uprintf("Cannot execute shared object\n");
805d49b2109SMaxim Sobolev 			return (ENOEXEC);
8066b16d664SEd Maste 		}
8077564c4adSKonstantin Belousov 		/*
8087564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
8097564c4adSKonstantin Belousov 		 * non-zero for some reason.
8107564c4adSKonstantin Belousov 		 */
8117564c4adSKonstantin Belousov 		if (baddr == 0)
812ab02d85fSKonstantin Belousov 			et_dyn_addr = ET_DYN_LOAD_ADDR;
8137564c4adSKonstantin Belousov 		else
8147564c4adSKonstantin Belousov 			et_dyn_addr = 0;
815ab02d85fSKonstantin Belousov 	} else
816ab02d85fSKonstantin Belousov 		et_dyn_addr = 0;
8175fe3ed62SJake Burkholder 	sv = brand_info->sysvec;
8189b68618dSPeter Wemm 	if (interp != NULL && brand_info->interp_newpath != NULL)
8194113f8d7SPeter Wemm 		newinterp = brand_info->interp_newpath;
8203ebc1248SPeter Wemm 
82160bb3943SAlan Cox 	/*
82260bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
82360bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
82460bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
82560bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
82660bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
82760bb3943SAlan Cox 	 * v_usecount may become zero.
8281dfab802SAlan Cox 	 *
8291dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
8301dfab802SAlan Cox 	 * the vnode is unlocked.
83160bb3943SAlan Cox 	 */
83222db15c0SAttilio Rao 	VOP_UNLOCK(imgp->vp, 0);
83360bb3943SAlan Cox 
83489b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
83519059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
836e1743d02SSøren Schmidt 
837cb05b60aSAttilio Rao 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
83889b57fcfSKonstantin Belousov 	if (error)
83989b57fcfSKonstantin Belousov 		return (error);
84060bb3943SAlan Cox 
841e1743d02SSøren Schmidt 	for (i = 0; i < hdr->e_phnum; i++) {
842e1743d02SSøren Schmidt 		switch (phdr[i].p_type) {
843e1743d02SSøren Schmidt 		case PT_LOAD:	/* Loadable segment */
8445b33842aSKonstantin Belousov 			if (phdr[i].p_memsz == 0)
8455b33842aSKonstantin Belousov 				break;
846ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
847292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
848ab02d85fSKonstantin Belousov 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
84981f223caSJake Burkholder 			    phdr[i].p_memsz, phdr[i].p_filesz, prot,
850292177e6SAlan Cox 			    sv->sv_pagesize);
851292177e6SAlan Cox 			if (error != 0)
85260bb3943SAlan Cox 				return (error);
853e1743d02SSøren Schmidt 
854cfaf7e60SDoug Rabson 			/*
855cfaf7e60SDoug Rabson 			 * If this segment contains the program headers,
856cfaf7e60SDoug Rabson 			 * remember their virtual address for the AT_PHDR
857cfaf7e60SDoug Rabson 			 * aux entry. Static binaries don't usually include
858cfaf7e60SDoug Rabson 			 * a PT_PHDR entry.
859cfaf7e60SDoug Rabson 			 */
860cfaf7e60SDoug Rabson 			if (phdr[i].p_offset == 0 &&
861cfaf7e60SDoug Rabson 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
862cfaf7e60SDoug Rabson 				<= phdr[i].p_filesz)
863ab02d85fSKonstantin Belousov 				proghdr = phdr[i].p_vaddr + hdr->e_phoff +
864ab02d85fSKonstantin Belousov 				    et_dyn_addr;
865cfaf7e60SDoug Rabson 
866ab02d85fSKonstantin Belousov 			seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
867cac45152SMatthew Dillon 			seg_size = round_page(phdr[i].p_memsz +
868ab02d85fSKonstantin Belousov 			    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
869cac45152SMatthew Dillon 
870e1743d02SSøren Schmidt 			/*
871920acedbSNathan Whitehorn 			 * Make the largest executable segment the official
872920acedbSNathan Whitehorn 			 * text segment and all others data.
87321c2d047SMatthew Dillon 			 *
87421c2d047SMatthew Dillon 			 * Note that obreak() assumes that data_addr +
87521c2d047SMatthew Dillon 			 * data_size == end of data load area, and the ELF
87621c2d047SMatthew Dillon 			 * file format expects segments to be sorted by
87721c2d047SMatthew Dillon 			 * address.  If multiple data segments exist, the
87821c2d047SMatthew Dillon 			 * last one will be used.
879e1743d02SSøren Schmidt 			 */
880920acedbSNathan Whitehorn 
881920acedbSNathan Whitehorn 			if (phdr[i].p_flags & PF_X && text_size < seg_size) {
8829782ecbaSPeter Wemm 				text_size = seg_size;
8839782ecbaSPeter Wemm 				text_addr = seg_addr;
8849782ecbaSPeter Wemm 			} else {
88521c2d047SMatthew Dillon 				data_size = seg_size;
886cac45152SMatthew Dillon 				data_addr = seg_addr;
887cac45152SMatthew Dillon 			}
88821c2d047SMatthew Dillon 			total_size += seg_size;
88996725dd0SAlexander Kabaev 			break;
89096725dd0SAlexander Kabaev 		case PT_PHDR: 	/* Program header table info */
891ab02d85fSKonstantin Belousov 			proghdr = phdr[i].p_vaddr + et_dyn_addr;
89296725dd0SAlexander Kabaev 			break;
89396725dd0SAlexander Kabaev 		default:
89496725dd0SAlexander Kabaev 			break;
89596725dd0SAlexander Kabaev 		}
89696725dd0SAlexander Kabaev 	}
89796725dd0SAlexander Kabaev 
89896725dd0SAlexander Kabaev 	if (data_addr == 0 && data_size == 0) {
89996725dd0SAlexander Kabaev 		data_addr = text_addr;
90096725dd0SAlexander Kabaev 		data_size = text_size;
90196725dd0SAlexander Kabaev 	}
902cac45152SMatthew Dillon 
903920acedbSNathan Whitehorn 	entry = (u_long)hdr->e_entry + et_dyn_addr;
904920acedbSNathan Whitehorn 
905cac45152SMatthew Dillon 	/*
906cac45152SMatthew Dillon 	 * Check limits.  It should be safe to check the
90796725dd0SAlexander Kabaev 	 * limits after loading the segments since we do
90896725dd0SAlexander Kabaev 	 * not actually fault in all the segments pages.
909cac45152SMatthew Dillon 	 */
91091d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
911f6f6d240SMateusz Guzik 	if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
9126b16d664SEd Maste 		err_str = "Data segment size exceeds process limit";
9136b16d664SEd Maste 	else if (text_size > maxtsiz)
9146b16d664SEd Maste 		err_str = "Text segment size exceeds system limit";
915f6f6d240SMateusz Guzik 	else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
9166b16d664SEd Maste 		err_str = "Total segment size exceeds process limit";
9176b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
9186b16d664SEd Maste 		err_str = "Data segment size exceeds resource limit";
9196b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
9206b16d664SEd Maste 		err_str = "Total segment size exceeds resource limit";
9216b16d664SEd Maste 	if (err_str != NULL) {
92291d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
9236b16d664SEd Maste 		uprintf("%s\n", err_str);
92460bb3943SAlan Cox 		return (ENOMEM);
925cac45152SMatthew Dillon 	}
926e1743d02SSøren Schmidt 
927292177e6SAlan Cox 	vmspace = imgp->proc->p_vmspace;
928e1743d02SSøren Schmidt 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
9297cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
930e1743d02SSøren Schmidt 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
9317cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
932e1743d02SSøren Schmidt 
933c460ac3aSPeter Wemm 	/*
934c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
935c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
936c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
937c460ac3aSPeter Wemm 	 * its maximum allowed size.
938c460ac3aSPeter Wemm 	 */
939f6f6d240SMateusz Guzik 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(curthread,
940292177e6SAlan Cox 	    RLIMIT_DATA));
94191d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
942e1743d02SSøren Schmidt 
943ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
944ea5a2b2eSSøren Schmidt 
94560bb3943SAlan Cox 	if (interp != NULL) {
9464113f8d7SPeter Wemm 		int have_interp = FALSE;
94722db15c0SAttilio Rao 		VOP_UNLOCK(imgp->vp, 0);
94860bb3943SAlan Cox 		if (brand_info->emul_path != NULL &&
9499b68618dSPeter Wemm 		    brand_info->emul_path[0] != '\0') {
950a163d034SWarner Losh 			path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
95160bb3943SAlan Cox 			snprintf(path, MAXPATHLEN, "%s%s",
95260bb3943SAlan Cox 			    brand_info->emul_path, interp);
9539b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, path, &addr,
9549b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
955911c2be0SMark Peek 			free(path, M_TEMP);
9569b68618dSPeter Wemm 			if (error == 0)
9574113f8d7SPeter Wemm 				have_interp = TRUE;
9589b68618dSPeter Wemm 		}
9594113f8d7SPeter Wemm 		if (!have_interp && newinterp != NULL) {
9604113f8d7SPeter Wemm 			error = __elfN(load_file)(imgp->proc, newinterp, &addr,
9614113f8d7SPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
962387ad998SKonstantin Belousov 			if (error == 0)
9634113f8d7SPeter Wemm 				have_interp = TRUE;
9644113f8d7SPeter Wemm 		}
9654113f8d7SPeter Wemm 		if (!have_interp) {
9669b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, interp, &addr,
9679b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
96860bb3943SAlan Cox 		}
969cb05b60aSAttilio Rao 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
9709b68618dSPeter Wemm 		if (error != 0) {
9719b68618dSPeter Wemm 			uprintf("ELF interpreter %s not found\n", interp);
97260bb3943SAlan Cox 			return (error);
973e1743d02SSøren Schmidt 		}
97495c807cfSRobert Watson 	} else
9757564c4adSKonstantin Belousov 		addr = et_dyn_addr;
976ea5a2b2eSSøren Schmidt 
977e1743d02SSøren Schmidt 	/*
978e1743d02SSøren Schmidt 	 * Construct auxargs table (used by the fixup routine)
979e1743d02SSøren Schmidt 	 */
980a163d034SWarner Losh 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
981e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
982e1743d02SSøren Schmidt 	elf_auxargs->phdr = proghdr;
983e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
984e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
985e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
986e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
987e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
988e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
989d36eec69SWarner Losh 	elf_auxargs->hdr_eflags = hdr->e_flags;
990e1743d02SSøren Schmidt 
991e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
992e1743d02SSøren Schmidt 	imgp->interpreted = 0;
993a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
99432c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
995f231de47SKonstantin Belousov 
996a7cddfedSJake Burkholder 	return (error);
997e1743d02SSøren Schmidt }
998e1743d02SSøren Schmidt 
999a360a43dSJake Burkholder #define	suword __CONCAT(suword, __ELF_WORD_SIZE)
10003ebc1248SPeter Wemm 
10013ebc1248SPeter Wemm int
10023ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
1003e1743d02SSøren Schmidt {
1004ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
1005a360a43dSJake Burkholder 	Elf_Addr *base;
1006a360a43dSJake Burkholder 	Elf_Addr *pos;
1007e1743d02SSøren Schmidt 
1008a360a43dSJake Burkholder 	base = (Elf_Addr *)*stack_base;
1009610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
1010e1743d02SSøren Schmidt 
101135c2a5a8SWarner Losh 	if (args->execfd != -1)
1012e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1013e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1014e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1015e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1016e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1017e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1018e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1019e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
1020ee960398SWarner Losh #ifdef AT_EHDRFLAGS
1021d36eec69SWarner Losh 	AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
1022d36eec69SWarner Losh #endif
10233ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
10243ff06357SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1025b96bd95bSIan Lepore 	AUXARGS_ENTRY(pos, AT_OSRELDATE,
1026b96bd95bSIan Lepore 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
1027ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1028ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1029ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1030ee235befSKonstantin Belousov 	}
1031ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1032ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1033ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1034ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1035ee235befSKonstantin Belousov 	}
1036aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1037aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1038aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1039aea81038SKonstantin Belousov 	}
104026d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
104126d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
104226d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1043e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1044e1743d02SSøren Schmidt 
1045e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1046e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1047e1743d02SSøren Schmidt 
10483ebc1248SPeter Wemm 	base--;
1049610ecfe0SMaxim Sobolev 	suword(base, (long)imgp->args->argc);
10503ebc1248SPeter Wemm 	*stack_base = (register_t *)base;
1051a7cddfedSJake Burkholder 	return (0);
1052e1743d02SSøren Schmidt }
1053e1743d02SSøren Schmidt 
1054e1743d02SSøren Schmidt /*
10558c64af4fSJohn Polstra  * Code for generating ELF core dumps.
10568c64af4fSJohn Polstra  */
10578c64af4fSJohn Polstra 
10584d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
10590ff27d31SJohn Polstra 
10600ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
10610ff27d31SJohn Polstra struct phdr_closure {
10620ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
10630ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
10640ff27d31SJohn Polstra };
10650ff27d31SJohn Polstra 
10660ff27d31SJohn Polstra /* Closure for cb_size_segment(). */
10670ff27d31SJohn Polstra struct sseg_closure {
10680ff27d31SJohn Polstra 	int count;		/* Count of writable segments. */
10690ff27d31SJohn Polstra 	size_t size;		/* Total size of all writable segments. */
10700ff27d31SJohn Polstra };
10710ff27d31SJohn Polstra 
1072bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1073bd390213SMikolaj Golub 
1074bd390213SMikolaj Golub struct note_info {
1075bd390213SMikolaj Golub 	int		type;		/* Note type. */
1076bd390213SMikolaj Golub 	outfunc_t 	outfunc; 	/* Output function. */
1077bd390213SMikolaj Golub 	void		*outarg;	/* Argument for the output function. */
1078bd390213SMikolaj Golub 	size_t		outsize;	/* Output size. */
1079bd390213SMikolaj Golub 	TAILQ_ENTRY(note_info) link;	/* Link to the next note info. */
1080bd390213SMikolaj Golub };
1081bd390213SMikolaj Golub 
1082bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info);
1083bd390213SMikolaj Golub 
1084aa14e9b7SMark Johnston /* Coredump output parameters. */
1085aa14e9b7SMark Johnston struct coredump_params {
1086aa14e9b7SMark Johnston 	off_t		offset;
1087aa14e9b7SMark Johnston 	struct ucred	*active_cred;
1088aa14e9b7SMark Johnston 	struct ucred	*file_cred;
1089aa14e9b7SMark Johnston 	struct thread	*td;
1090aa14e9b7SMark Johnston 	struct vnode	*vp;
1091aa14e9b7SMark Johnston 	struct gzio_stream *gzs;
1092aa14e9b7SMark Johnston };
1093aa14e9b7SMark Johnston 
10944d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
10954d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
1096aa14e9b7SMark Johnston static int core_write(struct coredump_params *, void *, size_t, off_t,
1097aa14e9b7SMark Johnston     enum uio_seg);
1098247aba24SMarcel Moolenaar static void each_writable_segment(struct thread *, segment_callback, void *);
1099aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1100aa14e9b7SMark Johnston     struct note_info_list *, size_t);
1101bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1102bd390213SMikolaj Golub     size_t *);
1103bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1104bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *);
1105bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1106bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int);
1107f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len);
1108bd390213SMikolaj Golub 
1109bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1110bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1111bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1112bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1113bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1114f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1115f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1116f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1117f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *);
1118f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *);
1119f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1120f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1121f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *);
1122f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
11238c64af4fSJohn Polstra 
1124aa14e9b7SMark Johnston #ifdef GZIO
1125e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel;
1126e7228204SAlfred Perlstein 
1127aa14e9b7SMark Johnston /*
1128aa14e9b7SMark Johnston  * Write out a core segment to the compression stream.
1129aa14e9b7SMark Johnston  */
1130e7228204SAlfred Perlstein static int
1131aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
1132aa14e9b7SMark Johnston {
1133aa14e9b7SMark Johnston 	u_int chunk_len;
1134e7228204SAlfred Perlstein 	int error;
1135aa14e9b7SMark Johnston 
1136aa14e9b7SMark Johnston 	while (len > 0) {
1137aa14e9b7SMark Johnston 		chunk_len = MIN(len, CORE_BUF_SIZE);
1138aa14e9b7SMark Johnston 		copyin(base, buf, chunk_len);
1139aa14e9b7SMark Johnston 		error = gzio_write(p->gzs, buf, chunk_len);
1140aa14e9b7SMark Johnston 		if (error != 0)
1141aa14e9b7SMark Johnston 			break;
1142aa14e9b7SMark Johnston 		base += chunk_len;
1143aa14e9b7SMark Johnston 		len -= chunk_len;
1144e7228204SAlfred Perlstein 	}
1145e7228204SAlfred Perlstein 	return (error);
1146e7228204SAlfred Perlstein }
1147e7228204SAlfred Perlstein 
1148aa14e9b7SMark Johnston static int
1149aa14e9b7SMark Johnston core_gz_write(void *base, size_t len, off_t offset, void *arg)
1150aa14e9b7SMark Johnston {
1151aa14e9b7SMark Johnston 
1152aa14e9b7SMark Johnston 	return (core_write((struct coredump_params *)arg, base, len, offset,
1153aa14e9b7SMark Johnston 	    UIO_SYSSPACE));
1154aa14e9b7SMark Johnston }
1155aa14e9b7SMark Johnston #endif /* GZIO */
1156aa14e9b7SMark Johnston 
1157aa14e9b7SMark Johnston static int
1158aa14e9b7SMark Johnston core_write(struct coredump_params *p, void *base, size_t len, off_t offset,
1159aa14e9b7SMark Johnston     enum uio_seg seg)
1160aa14e9b7SMark Johnston {
1161aa14e9b7SMark Johnston 
1162aa14e9b7SMark Johnston 	return (vn_rdwr_inchunks(UIO_WRITE, p->vp, base, len, offset,
1163aa14e9b7SMark Johnston 	    seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
1164aa14e9b7SMark Johnston 	    p->active_cred, p->file_cred, NULL, p->td));
1165aa14e9b7SMark Johnston }
1166aa14e9b7SMark Johnston 
1167aa14e9b7SMark Johnston static int
1168aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p,
1169aa14e9b7SMark Johnston     void *tmpbuf)
1170aa14e9b7SMark Johnston {
1171aa14e9b7SMark Johnston 
1172aa14e9b7SMark Johnston #ifdef GZIO
1173aa14e9b7SMark Johnston 	if (p->gzs != NULL)
1174aa14e9b7SMark Johnston 		return (compress_chunk(p, base, tmpbuf, len));
1175bd390213SMikolaj Golub #endif
1176aa14e9b7SMark Johnston 	return (core_write(p, base, len, offset, UIO_USERSPACE));
1177aa14e9b7SMark Johnston }
1178bd390213SMikolaj Golub 
1179bd390213SMikolaj Golub /*
1180bd390213SMikolaj Golub  * Drain into a core file.
1181bd390213SMikolaj Golub  */
1182bd390213SMikolaj Golub static int
1183bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len)
1184bd390213SMikolaj Golub {
1185aa14e9b7SMark Johnston 	struct coredump_params *p;
1186f1fca82eSMikolaj Golub 	int error, locked;
1187bd390213SMikolaj Golub 
1188aa14e9b7SMark Johnston 	p = (struct coredump_params *)arg;
1189f1fca82eSMikolaj Golub 
1190f1fca82eSMikolaj Golub 	/*
1191f1fca82eSMikolaj Golub 	 * Some kern_proc out routines that print to this sbuf may
1192f1fca82eSMikolaj Golub 	 * call us with the process lock held. Draining with the
1193f1fca82eSMikolaj Golub 	 * non-sleepable lock held is unsafe. The lock is needed for
1194f1fca82eSMikolaj Golub 	 * those routines when dumping a live process. In our case we
1195f1fca82eSMikolaj Golub 	 * can safely release the lock before draining and acquire
1196f1fca82eSMikolaj Golub 	 * again after.
1197f1fca82eSMikolaj Golub 	 */
1198f1fca82eSMikolaj Golub 	locked = PROC_LOCKED(p->td->td_proc);
1199f1fca82eSMikolaj Golub 	if (locked)
1200f1fca82eSMikolaj Golub 		PROC_UNLOCK(p->td->td_proc);
1201aa14e9b7SMark Johnston #ifdef GZIO
1202aa14e9b7SMark Johnston 	if (p->gzs != NULL)
1203aa14e9b7SMark Johnston 		error = gzio_write(p->gzs, __DECONST(char *, data), len);
1204bd390213SMikolaj Golub 	else
1205bd390213SMikolaj Golub #endif
1206aa14e9b7SMark Johnston 		error = core_write(p, __DECONST(void *, data), len, p->offset,
1207aa14e9b7SMark Johnston 		    UIO_SYSSPACE);
1208f1fca82eSMikolaj Golub 	if (locked)
1209f1fca82eSMikolaj Golub 		PROC_LOCK(p->td->td_proc);
1210bd390213SMikolaj Golub 	if (error != 0)
1211bd390213SMikolaj Golub 		return (-error);
1212bd390213SMikolaj Golub 	p->offset += len;
1213bd390213SMikolaj Golub 	return (len);
1214bd390213SMikolaj Golub }
1215bd390213SMikolaj Golub 
1216f1fca82eSMikolaj Golub /*
1217f1fca82eSMikolaj Golub  * Drain into a counter.
1218f1fca82eSMikolaj Golub  */
1219f1fca82eSMikolaj Golub static int
1220f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len)
1221f1fca82eSMikolaj Golub {
1222f1fca82eSMikolaj Golub 	size_t *sizep;
1223f1fca82eSMikolaj Golub 
1224f1fca82eSMikolaj Golub 	sizep = (size_t *)arg;
1225f1fca82eSMikolaj Golub 	*sizep += len;
1226f1fca82eSMikolaj Golub 	return (len);
1227f1fca82eSMikolaj Golub }
1228f1fca82eSMikolaj Golub 
12298c64af4fSJohn Polstra int
1230e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1231fca666a1SJulian Elischer {
1232247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
1233fca666a1SJulian Elischer 	int error = 0;
12340ff27d31SJohn Polstra 	struct sseg_closure seginfo;
1235bd390213SMikolaj Golub 	struct note_info_list notelst;
1236aa14e9b7SMark Johnston 	struct coredump_params params;
1237bd390213SMikolaj Golub 	struct note_info *ninfo;
1238aa14e9b7SMark Johnston 	void *hdr, *tmpbuf;
1239bd390213SMikolaj Golub 	size_t hdrsize, notesz, coresize;
1240aa14e9b7SMark Johnston 	boolean_t compress;
12418c64af4fSJohn Polstra 
1242aa14e9b7SMark Johnston 	compress = (flags & IMGACT_CORE_COMPRESS) != 0;
1243e7228204SAlfred Perlstein 	hdr = NULL;
124402d131adSMark Johnston 	tmpbuf = NULL;
1245bd390213SMikolaj Golub 	TAILQ_INIT(&notelst);
1246e7228204SAlfred Perlstein 
12470ff27d31SJohn Polstra 	/* Size the program segments. */
12480ff27d31SJohn Polstra 	seginfo.count = 0;
12490ff27d31SJohn Polstra 	seginfo.size = 0;
1250247aba24SMarcel Moolenaar 	each_writable_segment(td, cb_size_segment, &seginfo);
12510ff27d31SJohn Polstra 
12520ff27d31SJohn Polstra 	/*
1253bd390213SMikolaj Golub 	 * Collect info about the core file header area.
12540ff27d31SJohn Polstra 	 */
1255bd390213SMikolaj Golub 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1256bd390213SMikolaj Golub 	__elfN(prepare_notes)(td, &notelst, &notesz);
1257bd390213SMikolaj Golub 	coresize = round_page(hdrsize + notesz) + seginfo.size;
12580ff27d31SJohn Polstra 
125902d131adSMark Johnston 	/* Set up core dump parameters. */
126002d131adSMark Johnston 	params.offset = 0;
126102d131adSMark Johnston 	params.active_cred = cred;
126202d131adSMark Johnston 	params.file_cred = NOCRED;
126302d131adSMark Johnston 	params.td = td;
126402d131adSMark Johnston 	params.vp = vp;
126502d131adSMark Johnston 	params.gzs = NULL;
126602d131adSMark Johnston 
1267afcc55f3SEdward Tomasz Napierala #ifdef RACCT
12684b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
12691ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1270bd390213SMikolaj Golub 		error = racct_add(td->td_proc, RACCT_CORE, coresize);
12711ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
12721ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
12731ba5ad42SEdward Tomasz Napierala 			error = EFAULT;
12741ba5ad42SEdward Tomasz Napierala 			goto done;
12751ba5ad42SEdward Tomasz Napierala 		}
12764b5c9cf6SEdward Tomasz Napierala 	}
1277afcc55f3SEdward Tomasz Napierala #endif
1278bd390213SMikolaj Golub 	if (coresize >= limit) {
1279fba6b1afSAlfred Perlstein 		error = EFAULT;
1280fba6b1afSAlfred Perlstein 		goto done;
1281fba6b1afSAlfred Perlstein 	}
12820ff27d31SJohn Polstra 
1283aa14e9b7SMark Johnston #ifdef GZIO
1284aa14e9b7SMark Johnston 	/* Create a compression stream if necessary. */
1285aa14e9b7SMark Johnston 	if (compress) {
1286aa14e9b7SMark Johnston 		params.gzs = gzio_init(core_gz_write, GZIO_DEFLATE,
1287aa14e9b7SMark Johnston 		    CORE_BUF_SIZE, compress_user_cores_gzlevel, &params);
1288aa14e9b7SMark Johnston 		if (params.gzs == NULL) {
1289aa14e9b7SMark Johnston 			error = EFAULT;
1290aa14e9b7SMark Johnston 			goto done;
1291aa14e9b7SMark Johnston 		}
1292aa14e9b7SMark Johnston 		tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1293aa14e9b7SMark Johnston         }
1294aa14e9b7SMark Johnston #endif
1295aa14e9b7SMark Johnston 
12960ff27d31SJohn Polstra 	/*
12970ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
1298bd390213SMikolaj Golub 	 * and write it out following the notes.
12990ff27d31SJohn Polstra 	 */
1300a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
13010ff27d31SJohn Polstra 	if (hdr == NULL) {
1302fba6b1afSAlfred Perlstein 		error = EINVAL;
1303fba6b1afSAlfred Perlstein 		goto done;
13040ff27d31SJohn Polstra 	}
1305aa14e9b7SMark Johnston 	error = __elfN(corehdr)(&params, seginfo.count, hdr, hdrsize, &notelst,
1306aa14e9b7SMark Johnston 	    notesz);
13070ff27d31SJohn Polstra 
13080ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
13090ff27d31SJohn Polstra 	if (error == 0) {
13100ff27d31SJohn Polstra 		Elf_Phdr *php;
13112b471bc6STim J. Robbins 		off_t offset;
13120ff27d31SJohn Polstra 		int i;
13130ff27d31SJohn Polstra 
13140ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1315bd390213SMikolaj Golub 		offset = round_page(hdrsize + notesz);
13160ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1317aa14e9b7SMark Johnston 			error = core_output((caddr_t)(uintptr_t)php->p_vaddr,
1318aa14e9b7SMark Johnston 			    php->p_filesz, offset, &params, tmpbuf);
13190ff27d31SJohn Polstra 			if (error != 0)
13202b471bc6STim J. Robbins 				break;
13210ff27d31SJohn Polstra 			offset += php->p_filesz;
13220ff27d31SJohn Polstra 			php++;
13230ff27d31SJohn Polstra 		}
1324aa14e9b7SMark Johnston #ifdef GZIO
1325aa14e9b7SMark Johnston 		if (error == 0 && compress)
1326aa14e9b7SMark Johnston 			error = gzio_flush(params.gzs);
1327aa14e9b7SMark Johnston #endif
13280ff27d31SJohn Polstra 	}
1329e7228204SAlfred Perlstein 	if (error) {
1330e7228204SAlfred Perlstein 		log(LOG_WARNING,
1331e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1332e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1333e7228204SAlfred Perlstein 	}
1334e7228204SAlfred Perlstein 
1335e7228204SAlfred Perlstein done:
1336aa14e9b7SMark Johnston #ifdef GZIO
1337aa14e9b7SMark Johnston 	if (compress) {
1338aa14e9b7SMark Johnston 		free(tmpbuf, M_TEMP);
133902d131adSMark Johnston 		if (params.gzs != NULL)
1340aa14e9b7SMark Johnston 			gzio_fini(params.gzs);
1341aa14e9b7SMark Johnston 	}
13428b325009SAlfred Perlstein #endif
1343bd390213SMikolaj Golub 	while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1344bd390213SMikolaj Golub 		TAILQ_REMOVE(&notelst, ninfo, link);
1345bd390213SMikolaj Golub 		free(ninfo, M_TEMP);
1346bd390213SMikolaj Golub 	}
1347bd390213SMikolaj Golub 	if (hdr != NULL)
13480ff27d31SJohn Polstra 		free(hdr, M_TEMP);
13490ff27d31SJohn Polstra 
1350a7cddfedSJake Burkholder 	return (error);
13518c64af4fSJohn Polstra }
13528c64af4fSJohn Polstra 
13530ff27d31SJohn Polstra /*
13540ff27d31SJohn Polstra  * A callback for each_writable_segment() to write out the segment's
13550ff27d31SJohn Polstra  * program header entry.
13560ff27d31SJohn Polstra  */
13570ff27d31SJohn Polstra static void
13580ff27d31SJohn Polstra cb_put_phdr(entry, closure)
13590ff27d31SJohn Polstra 	vm_map_entry_t entry;
13600ff27d31SJohn Polstra 	void *closure;
13610ff27d31SJohn Polstra {
13620ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
13630ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
13640ff27d31SJohn Polstra 
13650ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
13660ff27d31SJohn Polstra 
13670ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
13680ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
13690ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
13700ff27d31SJohn Polstra 	phdr->p_paddr = 0;
13710ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
13720ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1373ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
13740ff27d31SJohn Polstra 
13750ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
13760ff27d31SJohn Polstra 	phc->phdr++;
13770ff27d31SJohn Polstra }
13780ff27d31SJohn Polstra 
13790ff27d31SJohn Polstra /*
13800ff27d31SJohn Polstra  * A callback for each_writable_segment() to gather information about
13810ff27d31SJohn Polstra  * the number of segments and their total size.
13820ff27d31SJohn Polstra  */
13830ff27d31SJohn Polstra static void
13840ff27d31SJohn Polstra cb_size_segment(entry, closure)
13850ff27d31SJohn Polstra 	vm_map_entry_t entry;
13860ff27d31SJohn Polstra 	void *closure;
13870ff27d31SJohn Polstra {
13880ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
13890ff27d31SJohn Polstra 
13900ff27d31SJohn Polstra 	ssc->count++;
13910ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
13920ff27d31SJohn Polstra }
13930ff27d31SJohn Polstra 
13940ff27d31SJohn Polstra /*
13950ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
13960ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
13970ff27d31SJohn Polstra  * caller-supplied data.
13980ff27d31SJohn Polstra  */
13990ff27d31SJohn Polstra static void
1400247aba24SMarcel Moolenaar each_writable_segment(td, func, closure)
1401247aba24SMarcel Moolenaar 	struct thread *td;
14020ff27d31SJohn Polstra 	segment_callback func;
14030ff27d31SJohn Polstra 	void *closure;
14040ff27d31SJohn Polstra {
1405247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
14060ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
14070ff27d31SJohn Polstra 	vm_map_entry_t entry;
1408976a87a2SAlan Cox 	vm_object_t backing_object, object;
1409976a87a2SAlan Cox 	boolean_t ignore_entry;
14100ff27d31SJohn Polstra 
1411976a87a2SAlan Cox 	vm_map_lock_read(map);
14120ff27d31SJohn Polstra 	for (entry = map->header.next; entry != &map->header;
14130ff27d31SJohn Polstra 	    entry = entry->next) {
1414fa7dd9c5SMatthew Dillon 		/*
1415fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1416fa7dd9c5SMatthew Dillon 		 * coredump mode.
1417fa7dd9c5SMatthew Dillon 		 *
1418fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1419fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1420fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1421fa7dd9c5SMatthew Dillon 		 */
1422fa7dd9c5SMatthew Dillon 		if (elf_legacy_coredump) {
1423fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
14240ff27d31SJohn Polstra 				continue;
1425fa7dd9c5SMatthew Dillon 		} else {
1426fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_ALL) == 0)
1427fa7dd9c5SMatthew Dillon 				continue;
1428fa7dd9c5SMatthew Dillon 		}
14290ff27d31SJohn Polstra 
14309730a5daSPaul Saab 		/*
1431fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1432fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1433fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1434fa7dd9c5SMatthew Dillon 		 * kernel map).
14359730a5daSPaul Saab 		 */
1436fa7dd9c5SMatthew Dillon 		if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
14379730a5daSPaul Saab 			continue;
14389730a5daSPaul Saab 
1439976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
14400ff27d31SJohn Polstra 			continue;
14410ff27d31SJohn Polstra 
14420ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1443bc403f03SAttilio Rao 		VM_OBJECT_RLOCK(object);
1444976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1445bc403f03SAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1446bc403f03SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1447976a87a2SAlan Cox 			object = backing_object;
1448976a87a2SAlan Cox 		}
1449976a87a2SAlan Cox 		ignore_entry = object->type != OBJT_DEFAULT &&
1450bc411bc2SJohn Baldwin 		    object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
1451bc411bc2SJohn Baldwin 		    object->type != OBJT_PHYS;
1452bc403f03SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1453976a87a2SAlan Cox 		if (ignore_entry)
14540ff27d31SJohn Polstra 			continue;
14550ff27d31SJohn Polstra 
14560ff27d31SJohn Polstra 		(*func)(entry, closure);
14570ff27d31SJohn Polstra 	}
1458976a87a2SAlan Cox 	vm_map_unlock_read(map);
14590ff27d31SJohn Polstra }
14600ff27d31SJohn Polstra 
14610ff27d31SJohn Polstra /*
14620ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
14630ff27d31SJohn Polstra  * the page boundary.
14640ff27d31SJohn Polstra  */
14658c64af4fSJohn Polstra static int
1466aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1467aa14e9b7SMark Johnston     size_t hdrsize, struct note_info_list *notelst, size_t notesz)
14688c64af4fSJohn Polstra {
1469bd390213SMikolaj Golub 	struct note_info *ninfo;
1470bd390213SMikolaj Golub 	struct sbuf *sb;
1471bd390213SMikolaj Golub 	int error;
14728c64af4fSJohn Polstra 
14738c64af4fSJohn Polstra 	/* Fill in the header. */
14740ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
1475aa14e9b7SMark Johnston 	__elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz);
14768c64af4fSJohn Polstra 
1477bd390213SMikolaj Golub 	sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1478aa14e9b7SMark Johnston 	sbuf_set_drain(sb, sbuf_drain_core_output, p);
1479bd390213SMikolaj Golub 	sbuf_start_section(sb, NULL);
1480bd390213SMikolaj Golub 	sbuf_bcat(sb, hdr, hdrsize);
1481bd390213SMikolaj Golub 	TAILQ_FOREACH(ninfo, notelst, link)
1482bd390213SMikolaj Golub 	    __elfN(putnote)(ninfo, sb);
1483bd390213SMikolaj Golub 	/* Align up to a page boundary for the program segments. */
1484bd390213SMikolaj Golub 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1485bd390213SMikolaj Golub 	error = sbuf_finish(sb);
1486bd390213SMikolaj Golub 	sbuf_delete(sb);
1487bd390213SMikolaj Golub 
1488bd390213SMikolaj Golub 	return (error);
1489e7228204SAlfred Perlstein }
1490bd390213SMikolaj Golub 
1491bd390213SMikolaj Golub static void
1492bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1493bd390213SMikolaj Golub     size_t *sizep)
1494bd390213SMikolaj Golub {
1495bd390213SMikolaj Golub 	struct proc *p;
1496bd390213SMikolaj Golub 	struct thread *thr;
1497bd390213SMikolaj Golub 	size_t size;
1498bd390213SMikolaj Golub 
1499bd390213SMikolaj Golub 	p = td->td_proc;
1500bd390213SMikolaj Golub 	size = 0;
1501bd390213SMikolaj Golub 
1502bd390213SMikolaj Golub 	size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1503bd390213SMikolaj Golub 
1504bd390213SMikolaj Golub 	/*
1505bd390213SMikolaj Golub 	 * To have the debugger select the right thread (LWP) as the initial
1506bd390213SMikolaj Golub 	 * thread, we dump the state of the thread passed to us in td first.
1507bd390213SMikolaj Golub 	 * This is the thread that causes the core dump and thus likely to
1508bd390213SMikolaj Golub 	 * be the right thread one wants to have selected in the debugger.
1509bd390213SMikolaj Golub 	 */
1510bd390213SMikolaj Golub 	thr = td;
1511bd390213SMikolaj Golub 	while (thr != NULL) {
1512bd390213SMikolaj Golub 		size += register_note(list, NT_PRSTATUS,
1513bd390213SMikolaj Golub 		    __elfN(note_prstatus), thr);
1514bd390213SMikolaj Golub 		size += register_note(list, NT_FPREGSET,
1515bd390213SMikolaj Golub 		    __elfN(note_fpregset), thr);
1516bd390213SMikolaj Golub 		size += register_note(list, NT_THRMISC,
1517bd390213SMikolaj Golub 		    __elfN(note_thrmisc), thr);
1518bd390213SMikolaj Golub 		size += register_note(list, -1,
1519bd390213SMikolaj Golub 		    __elfN(note_threadmd), thr);
1520bd390213SMikolaj Golub 
1521bd390213SMikolaj Golub 		thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1522bd390213SMikolaj Golub 		    TAILQ_NEXT(thr, td_plist);
1523bd390213SMikolaj Golub 		if (thr == td)
1524bd390213SMikolaj Golub 			thr = TAILQ_NEXT(thr, td_plist);
1525dada0278SJohn Polstra 	}
1526dada0278SJohn Polstra 
1527f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PROC,
1528f1fca82eSMikolaj Golub 	    __elfN(note_procstat_proc), p);
1529f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_FILES,
1530f1fca82eSMikolaj Golub 	    note_procstat_files, p);
1531f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_VMMAP,
1532f1fca82eSMikolaj Golub 	    note_procstat_vmmap, p);
1533f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_GROUPS,
1534f1fca82eSMikolaj Golub 	    note_procstat_groups, p);
1535f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_UMASK,
1536f1fca82eSMikolaj Golub 	    note_procstat_umask, p);
1537f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_RLIMIT,
1538f1fca82eSMikolaj Golub 	    note_procstat_rlimit, p);
1539f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_OSREL,
1540f1fca82eSMikolaj Golub 	    note_procstat_osrel, p);
1541f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1542f1fca82eSMikolaj Golub 	    __elfN(note_procstat_psstrings), p);
1543f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_AUXV,
1544f1fca82eSMikolaj Golub 	    __elfN(note_procstat_auxv), p);
1545f1fca82eSMikolaj Golub 
1546bd390213SMikolaj Golub 	*sizep = size;
1547bd390213SMikolaj Golub }
1548bd390213SMikolaj Golub 
1549bd390213SMikolaj Golub static void
1550bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1551bd390213SMikolaj Golub     size_t notesz)
1552bd390213SMikolaj Golub {
1553bd390213SMikolaj Golub 	Elf_Ehdr *ehdr;
1554bd390213SMikolaj Golub 	Elf_Phdr *phdr;
1555bd390213SMikolaj Golub 	struct phdr_closure phc;
1556bd390213SMikolaj Golub 
1557bd390213SMikolaj Golub 	ehdr = (Elf_Ehdr *)hdr;
1558bd390213SMikolaj Golub 	phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
1559bd390213SMikolaj Golub 
1560bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1561bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1562bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1563bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1564bd390213SMikolaj Golub 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1565bd390213SMikolaj Golub 	ehdr->e_ident[EI_DATA] = ELF_DATA;
1566bd390213SMikolaj Golub 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1567bd390213SMikolaj Golub 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1568bd390213SMikolaj Golub 	ehdr->e_ident[EI_ABIVERSION] = 0;
1569bd390213SMikolaj Golub 	ehdr->e_ident[EI_PAD] = 0;
1570bd390213SMikolaj Golub 	ehdr->e_type = ET_CORE;
1571bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1572bd390213SMikolaj Golub 	ehdr->e_machine = ELF_ARCH32;
1573bd390213SMikolaj Golub #else
1574bd390213SMikolaj Golub 	ehdr->e_machine = ELF_ARCH;
1575bd390213SMikolaj Golub #endif
1576bd390213SMikolaj Golub 	ehdr->e_version = EV_CURRENT;
1577bd390213SMikolaj Golub 	ehdr->e_entry = 0;
1578bd390213SMikolaj Golub 	ehdr->e_phoff = sizeof(Elf_Ehdr);
1579bd390213SMikolaj Golub 	ehdr->e_flags = 0;
1580bd390213SMikolaj Golub 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
1581bd390213SMikolaj Golub 	ehdr->e_phentsize = sizeof(Elf_Phdr);
1582bd390213SMikolaj Golub 	ehdr->e_phnum = numsegs + 1;
1583bd390213SMikolaj Golub 	ehdr->e_shentsize = sizeof(Elf_Shdr);
1584bd390213SMikolaj Golub 	ehdr->e_shnum = 0;
1585bd390213SMikolaj Golub 	ehdr->e_shstrndx = SHN_UNDEF;
1586bd390213SMikolaj Golub 
1587bd390213SMikolaj Golub 	/*
1588bd390213SMikolaj Golub 	 * Fill in the program header entries.
1589bd390213SMikolaj Golub 	 */
1590bd390213SMikolaj Golub 
1591bd390213SMikolaj Golub 	/* The note segement. */
1592bd390213SMikolaj Golub 	phdr->p_type = PT_NOTE;
1593bd390213SMikolaj Golub 	phdr->p_offset = hdrsize;
1594bd390213SMikolaj Golub 	phdr->p_vaddr = 0;
1595bd390213SMikolaj Golub 	phdr->p_paddr = 0;
1596bd390213SMikolaj Golub 	phdr->p_filesz = notesz;
1597bd390213SMikolaj Golub 	phdr->p_memsz = 0;
1598bd390213SMikolaj Golub 	phdr->p_flags = PF_R;
15991b8388cdSMikolaj Golub 	phdr->p_align = ELF_NOTE_ROUNDSIZE;
1600bd390213SMikolaj Golub 	phdr++;
1601bd390213SMikolaj Golub 
1602bd390213SMikolaj Golub 	/* All the writable segments from the program. */
1603bd390213SMikolaj Golub 	phc.phdr = phdr;
1604bd390213SMikolaj Golub 	phc.offset = round_page(hdrsize + notesz);
1605bd390213SMikolaj Golub 	each_writable_segment(td, cb_put_phdr, &phc);
1606bd390213SMikolaj Golub }
1607bd390213SMikolaj Golub 
1608bd390213SMikolaj Golub static size_t
1609bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1610bd390213SMikolaj Golub {
1611bd390213SMikolaj Golub 	struct note_info *ninfo;
1612bd390213SMikolaj Golub 	size_t size, notesize;
1613bd390213SMikolaj Golub 
1614bd390213SMikolaj Golub 	size = 0;
1615bd390213SMikolaj Golub 	out(arg, NULL, &size);
1616bd390213SMikolaj Golub 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1617bd390213SMikolaj Golub 	ninfo->type = type;
1618bd390213SMikolaj Golub 	ninfo->outfunc = out;
1619bd390213SMikolaj Golub 	ninfo->outarg = arg;
1620bd390213SMikolaj Golub 	ninfo->outsize = size;
1621bd390213SMikolaj Golub 	TAILQ_INSERT_TAIL(list, ninfo, link);
1622bd390213SMikolaj Golub 
1623bd390213SMikolaj Golub 	if (type == -1)
1624bd390213SMikolaj Golub 		return (size);
1625bd390213SMikolaj Golub 
1626bd390213SMikolaj Golub 	notesize = sizeof(Elf_Note) +		/* note header */
1627180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1628180e57e5SJohn Baldwin 						/* note name */
1629180e57e5SJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1630180e57e5SJohn Baldwin 
1631180e57e5SJohn Baldwin 	return (notesize);
1632180e57e5SJohn Baldwin }
1633180e57e5SJohn Baldwin 
1634180e57e5SJohn Baldwin static size_t
1635180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len)
1636180e57e5SJohn Baldwin {
1637180e57e5SJohn Baldwin 	size_t padded_len;
1638180e57e5SJohn Baldwin 
1639180e57e5SJohn Baldwin 	padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
1640180e57e5SJohn Baldwin 	if (dst != NULL) {
1641180e57e5SJohn Baldwin 		bcopy(src, dst, len);
1642180e57e5SJohn Baldwin 		bzero((char *)dst + len, padded_len - len);
1643180e57e5SJohn Baldwin 	}
1644180e57e5SJohn Baldwin 	return (padded_len);
1645180e57e5SJohn Baldwin }
1646180e57e5SJohn Baldwin 
1647180e57e5SJohn Baldwin size_t
1648180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
1649180e57e5SJohn Baldwin {
1650180e57e5SJohn Baldwin 	Elf_Note *note;
1651180e57e5SJohn Baldwin 	char *buf;
1652180e57e5SJohn Baldwin 	size_t notesize;
1653180e57e5SJohn Baldwin 
1654180e57e5SJohn Baldwin 	buf = dst;
1655180e57e5SJohn Baldwin 	if (buf != NULL) {
1656180e57e5SJohn Baldwin 		note = (Elf_Note *)buf;
1657180e57e5SJohn Baldwin 		note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1658180e57e5SJohn Baldwin 		note->n_descsz = size;
1659180e57e5SJohn Baldwin 		note->n_type = type;
1660180e57e5SJohn Baldwin 		buf += sizeof(*note);
1661180e57e5SJohn Baldwin 		buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
1662180e57e5SJohn Baldwin 		    sizeof(FREEBSD_ABI_VENDOR));
1663180e57e5SJohn Baldwin 		append_note_data(src, buf, size);
1664180e57e5SJohn Baldwin 		if (descp != NULL)
1665180e57e5SJohn Baldwin 			*descp = buf;
1666180e57e5SJohn Baldwin 	}
1667180e57e5SJohn Baldwin 
1668180e57e5SJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
1669180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1670180e57e5SJohn Baldwin 						/* note name */
16711b8388cdSMikolaj Golub 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1672bd390213SMikolaj Golub 
1673bd390213SMikolaj Golub 	return (notesize);
1674bd390213SMikolaj Golub }
1675bd390213SMikolaj Golub 
1676bd390213SMikolaj Golub static void
1677bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1678bd390213SMikolaj Golub {
1679bd390213SMikolaj Golub 	Elf_Note note;
1680*14bdbaf2SConrad Meyer 	ssize_t old_len, sect_len;
1681*14bdbaf2SConrad Meyer 	size_t new_len, descsz, i;
1682bd390213SMikolaj Golub 
1683bd390213SMikolaj Golub 	if (ninfo->type == -1) {
1684bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1685bd390213SMikolaj Golub 		return;
1686bd390213SMikolaj Golub 	}
1687bd390213SMikolaj Golub 
1688180e57e5SJohn Baldwin 	note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1689bd390213SMikolaj Golub 	note.n_descsz = ninfo->outsize;
1690bd390213SMikolaj Golub 	note.n_type = ninfo->type;
1691bd390213SMikolaj Golub 
1692bd390213SMikolaj Golub 	sbuf_bcat(sb, &note, sizeof(note));
1693bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1694180e57e5SJohn Baldwin 	sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
16951b8388cdSMikolaj Golub 	sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1696bd390213SMikolaj Golub 	if (note.n_descsz == 0)
1697bd390213SMikolaj Golub 		return;
1698bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1699bd390213SMikolaj Golub 	ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1700*14bdbaf2SConrad Meyer 	sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1701*14bdbaf2SConrad Meyer 	if (sect_len < 0)
1702*14bdbaf2SConrad Meyer 		return;
1703*14bdbaf2SConrad Meyer 
1704*14bdbaf2SConrad Meyer 	new_len = (size_t)sect_len;
1705*14bdbaf2SConrad Meyer 	descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
1706*14bdbaf2SConrad Meyer 	if (new_len < descsz) {
1707*14bdbaf2SConrad Meyer 		/*
1708*14bdbaf2SConrad Meyer 		 * It is expected that individual note emitters will correctly
1709*14bdbaf2SConrad Meyer 		 * predict their expected output size and fill up to that size
1710*14bdbaf2SConrad Meyer 		 * themselves, padding in a format-specific way if needed.
1711*14bdbaf2SConrad Meyer 		 * However, in case they don't, just do it here with zeros.
1712*14bdbaf2SConrad Meyer 		 */
1713*14bdbaf2SConrad Meyer 		for (i = 0; i < descsz - new_len; i++)
1714*14bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
1715*14bdbaf2SConrad Meyer 	} else if (new_len > descsz) {
1716*14bdbaf2SConrad Meyer 		/*
1717*14bdbaf2SConrad Meyer 		 * We can't always truncate sb -- we may have drained some
1718*14bdbaf2SConrad Meyer 		 * of it already.
1719*14bdbaf2SConrad Meyer 		 */
1720*14bdbaf2SConrad Meyer 		KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
1721*14bdbaf2SConrad Meyer 		    "read it (%zu > %zu).  Since it is longer than "
1722*14bdbaf2SConrad Meyer 		    "expected, this coredump's notes are corrupt.  THIS "
1723*14bdbaf2SConrad Meyer 		    "IS A BUG in the note_procstat routine for type %u.\n",
1724*14bdbaf2SConrad Meyer 		    __func__, (unsigned)note.n_type, new_len, descsz,
1725*14bdbaf2SConrad Meyer 		    (unsigned)note.n_type));
1726*14bdbaf2SConrad Meyer 	}
1727bd390213SMikolaj Golub }
1728bd390213SMikolaj Golub 
1729bd390213SMikolaj Golub /*
1730bd390213SMikolaj Golub  * Miscellaneous note out functions.
1731bd390213SMikolaj Golub  */
1732bd390213SMikolaj Golub 
1733841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1734841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
1735841c0c7eSNathan Whitehorn 
173662919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
173762919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
173862919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
173962919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
174062919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
17417f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
1742f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	KERN_PROC_MASK32
1743f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t;
1744f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t;
174562919d78SPeter Wemm #else
174662919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
174762919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
174862919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
174962919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
175062919d78SPeter Wemm typedef gregset_t elf_gregset_t;
17517f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
1752f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	0
1753f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t;
1754f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t;
175562919d78SPeter Wemm #endif
175662919d78SPeter Wemm 
17578c64af4fSJohn Polstra static void
1758bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
17598c64af4fSJohn Polstra {
1760247aba24SMarcel Moolenaar 	struct proc *p;
1761bd390213SMikolaj Golub 	elf_prpsinfo_t *psinfo;
17628c64af4fSJohn Polstra 
1763bd390213SMikolaj Golub 	p = (struct proc *)arg;
1764bd390213SMikolaj Golub 	if (sb != NULL) {
1765bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1766bd390213SMikolaj Golub 		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
17678c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
176862919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1769ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
17708c9b7b2cSMarcel Moolenaar 		/*
17718c9b7b2cSMarcel Moolenaar 		 * XXX - We don't fill in the command line arguments properly
17728c9b7b2cSMarcel Moolenaar 		 * yet.
17738c9b7b2cSMarcel Moolenaar 		 */
1774ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_psargs, p->p_comm,
17758c9b7b2cSMarcel Moolenaar 		    sizeof(psinfo->pr_psargs));
17768c9b7b2cSMarcel Moolenaar 
1777bd390213SMikolaj Golub 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1778bd390213SMikolaj Golub 		free(psinfo, M_TEMP);
1779bd390213SMikolaj Golub 	}
1780bd390213SMikolaj Golub 	*sizep = sizeof(*psinfo);
1781bd390213SMikolaj Golub }
1782bd390213SMikolaj Golub 
1783bd390213SMikolaj Golub static void
1784bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1785bd390213SMikolaj Golub {
1786bd390213SMikolaj Golub 	struct thread *td;
1787bd390213SMikolaj Golub 	elf_prstatus_t *status;
1788bd390213SMikolaj Golub 
1789bd390213SMikolaj Golub 	td = (struct thread *)arg;
1790bd390213SMikolaj Golub 	if (sb != NULL) {
1791bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*status), ("invalid size"));
1792bd390213SMikolaj Golub 		status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
17938c9b7b2cSMarcel Moolenaar 		status->pr_version = PRSTATUS_VERSION;
179462919d78SPeter Wemm 		status->pr_statussz = sizeof(elf_prstatus_t);
179562919d78SPeter Wemm 		status->pr_gregsetsz = sizeof(elf_gregset_t);
179662919d78SPeter Wemm 		status->pr_fpregsetsz = sizeof(elf_fpregset_t);
17978c9b7b2cSMarcel Moolenaar 		status->pr_osreldate = osreldate;
1798bd390213SMikolaj Golub 		status->pr_cursig = td->td_proc->p_sig;
1799bd390213SMikolaj Golub 		status->pr_pid = td->td_tid;
1800841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1801bd390213SMikolaj Golub 		fill_regs32(td, &status->pr_reg);
180262919d78SPeter Wemm #else
1803bd390213SMikolaj Golub 		fill_regs(td, &status->pr_reg);
180462919d78SPeter Wemm #endif
1805bd390213SMikolaj Golub 		sbuf_bcat(sb, status, sizeof(*status));
1806bd390213SMikolaj Golub 		free(status, M_TEMP);
18078c9b7b2cSMarcel Moolenaar 	}
1808bd390213SMikolaj Golub 	*sizep = sizeof(*status);
1809bd390213SMikolaj Golub }
1810bd390213SMikolaj Golub 
1811bd390213SMikolaj Golub static void
1812bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1813bd390213SMikolaj Golub {
1814bd390213SMikolaj Golub 	struct thread *td;
1815bd390213SMikolaj Golub 	elf_prfpregset_t *fpregset;
1816bd390213SMikolaj Golub 
1817bd390213SMikolaj Golub 	td = (struct thread *)arg;
1818bd390213SMikolaj Golub 	if (sb != NULL) {
1819bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1820bd390213SMikolaj Golub 		fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1821bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1822bd390213SMikolaj Golub 		fill_fpregs32(td, fpregset);
1823bd390213SMikolaj Golub #else
1824bd390213SMikolaj Golub 		fill_fpregs(td, fpregset);
1825bd390213SMikolaj Golub #endif
1826bd390213SMikolaj Golub 		sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1827bd390213SMikolaj Golub 		free(fpregset, M_TEMP);
1828bd390213SMikolaj Golub 	}
1829bd390213SMikolaj Golub 	*sizep = sizeof(*fpregset);
1830bd390213SMikolaj Golub }
1831bd390213SMikolaj Golub 
1832bd390213SMikolaj Golub static void
1833bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1834bd390213SMikolaj Golub {
1835bd390213SMikolaj Golub 	struct thread *td;
1836bd390213SMikolaj Golub 	elf_thrmisc_t thrmisc;
1837bd390213SMikolaj Golub 
1838bd390213SMikolaj Golub 	td = (struct thread *)arg;
1839bd390213SMikolaj Golub 	if (sb != NULL) {
1840bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1841bd390213SMikolaj Golub 		bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1842bd390213SMikolaj Golub 		strcpy(thrmisc.pr_tname, td->td_name);
1843bd390213SMikolaj Golub 		sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1844bd390213SMikolaj Golub 	}
1845bd390213SMikolaj Golub 	*sizep = sizeof(thrmisc);
1846bd390213SMikolaj Golub }
1847bd390213SMikolaj Golub 
18484da47b2fSMarcel Moolenaar /*
18494da47b2fSMarcel Moolenaar  * Allow for MD specific notes, as well as any MD
18504da47b2fSMarcel Moolenaar  * specific preparations for writing MI notes.
18514da47b2fSMarcel Moolenaar  */
18528c64af4fSJohn Polstra static void
1853bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
18548c64af4fSJohn Polstra {
1855bd390213SMikolaj Golub 	struct thread *td;
1856bd390213SMikolaj Golub 	void *buf;
1857bd390213SMikolaj Golub 	size_t size;
18588c64af4fSJohn Polstra 
1859bd390213SMikolaj Golub 	td = (struct thread *)arg;
1860bd390213SMikolaj Golub 	size = *sizep;
1861bd390213SMikolaj Golub 	if (size != 0 && sb != NULL)
1862bd390213SMikolaj Golub 		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
186383a396ceSChristian Brueffer 	else
186483a396ceSChristian Brueffer 		buf = NULL;
1865bd390213SMikolaj Golub 	size = 0;
1866bd390213SMikolaj Golub 	__elfN(dump_thread)(td, buf, &size);
186764779280SKonstantin Belousov 	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
186883a396ceSChristian Brueffer 	if (size != 0 && sb != NULL)
1869bd390213SMikolaj Golub 		sbuf_bcat(sb, buf, size);
1870a1761d73SChristian Brueffer 	free(buf, M_TEMP);
1871bd390213SMikolaj Golub 	*sizep = size;
18728c64af4fSJohn Polstra }
18738c64af4fSJohn Polstra 
1874f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE
1875f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
1876f1fca82eSMikolaj Golub #endif
1877f1fca82eSMikolaj Golub 
1878f1fca82eSMikolaj Golub static void
1879f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
1880f1fca82eSMikolaj Golub {
1881f1fca82eSMikolaj Golub 	struct proc *p;
1882f1fca82eSMikolaj Golub 	size_t size;
1883f1fca82eSMikolaj Golub 	int structsize;
1884f1fca82eSMikolaj Golub 
1885f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
1886f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_numthreads *
1887f1fca82eSMikolaj Golub 	    sizeof(elf_kinfo_proc_t);
1888f1fca82eSMikolaj Golub 
1889f1fca82eSMikolaj Golub 	if (sb != NULL) {
1890f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
1891f1fca82eSMikolaj Golub 		structsize = sizeof(elf_kinfo_proc_t);
1892f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
18936662ce5aSMateusz Guzik 		sx_slock(&proctree_lock);
1894f1fca82eSMikolaj Golub 		PROC_LOCK(p);
1895f1fca82eSMikolaj Golub 		kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
18966662ce5aSMateusz Guzik 		sx_sunlock(&proctree_lock);
1897f1fca82eSMikolaj Golub 	}
1898f1fca82eSMikolaj Golub 	*sizep = size;
1899f1fca82eSMikolaj Golub }
1900f1fca82eSMikolaj Golub 
1901f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE
1902f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
1903f1fca82eSMikolaj Golub #endif
1904f1fca82eSMikolaj Golub 
1905*14bdbaf2SConrad Meyer static int pack_fileinfo = 1;
1906*14bdbaf2SConrad Meyer SYSCTL_INT(_kern, OID_AUTO, coredump_pack_fileinfo, CTLFLAG_RWTUN,
1907*14bdbaf2SConrad Meyer     &pack_fileinfo, 0,
1908*14bdbaf2SConrad Meyer     "Enable file path packing in 'procstat -f' coredump notes");
1909*14bdbaf2SConrad Meyer 
1910f1fca82eSMikolaj Golub static void
1911f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
1912f1fca82eSMikolaj Golub {
1913f1fca82eSMikolaj Golub 	struct proc *p;
1914*14bdbaf2SConrad Meyer 	size_t size, sect_sz, i;
1915*14bdbaf2SConrad Meyer 	ssize_t start_len, sect_len;
1916*14bdbaf2SConrad Meyer 	int structsize, filedesc_flags;
1917*14bdbaf2SConrad Meyer 
1918*14bdbaf2SConrad Meyer 	if (pack_fileinfo)
1919*14bdbaf2SConrad Meyer 		filedesc_flags = KERN_FILEDESC_PACK_KINFO;
1920*14bdbaf2SConrad Meyer 	else
1921*14bdbaf2SConrad Meyer 		filedesc_flags = 0;
1922f1fca82eSMikolaj Golub 
1923f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
1924*14bdbaf2SConrad Meyer 	structsize = sizeof(struct kinfo_file);
1925f1fca82eSMikolaj Golub 	if (sb == NULL) {
1926f1fca82eSMikolaj Golub 		size = 0;
1927f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1928f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
1929f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
1930f1fca82eSMikolaj Golub 		PROC_LOCK(p);
1931*14bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
1932f1fca82eSMikolaj Golub 		sbuf_finish(sb);
1933f1fca82eSMikolaj Golub 		sbuf_delete(sb);
1934f1fca82eSMikolaj Golub 		*sizep = size;
1935f1fca82eSMikolaj Golub 	} else {
1936*14bdbaf2SConrad Meyer 		sbuf_start_section(sb, &start_len);
1937*14bdbaf2SConrad Meyer 
1938f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
1939f1fca82eSMikolaj Golub 		PROC_LOCK(p);
1940*14bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
1941*14bdbaf2SConrad Meyer 		    filedesc_flags);
1942*14bdbaf2SConrad Meyer 
1943*14bdbaf2SConrad Meyer 		sect_len = sbuf_end_section(sb, start_len, 0, 0);
1944*14bdbaf2SConrad Meyer 		if (sect_len < 0)
1945*14bdbaf2SConrad Meyer 			return;
1946*14bdbaf2SConrad Meyer 		sect_sz = sect_len;
1947*14bdbaf2SConrad Meyer 
1948*14bdbaf2SConrad Meyer 		KASSERT(sect_sz <= *sizep,
1949*14bdbaf2SConrad Meyer 		    ("kern_proc_filedesc_out did not respect maxlen; "
1950*14bdbaf2SConrad Meyer 		     "requested %zu, got %zu", *sizep - sizeof(structsize),
1951*14bdbaf2SConrad Meyer 		     sect_sz - sizeof(structsize)));
1952*14bdbaf2SConrad Meyer 
1953*14bdbaf2SConrad Meyer 		for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
1954*14bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
1955f1fca82eSMikolaj Golub 	}
1956f1fca82eSMikolaj Golub }
1957f1fca82eSMikolaj Golub 
1958f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE
1959f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1960f1fca82eSMikolaj Golub #endif
1961f1fca82eSMikolaj Golub 
1962f1fca82eSMikolaj Golub static void
1963f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
1964f1fca82eSMikolaj Golub {
1965f1fca82eSMikolaj Golub 	struct proc *p;
1966f1fca82eSMikolaj Golub 	size_t size;
1967f1fca82eSMikolaj Golub 	int structsize;
1968f1fca82eSMikolaj Golub 
1969f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
1970f1fca82eSMikolaj Golub 	if (sb == NULL) {
1971f1fca82eSMikolaj Golub 		size = 0;
1972f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1973f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
1974f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
1975f1fca82eSMikolaj Golub 		PROC_LOCK(p);
1976f1fca82eSMikolaj Golub 		kern_proc_vmmap_out(p, sb);
1977f1fca82eSMikolaj Golub 		sbuf_finish(sb);
1978f1fca82eSMikolaj Golub 		sbuf_delete(sb);
1979f1fca82eSMikolaj Golub 		*sizep = size;
1980f1fca82eSMikolaj Golub 	} else {
1981f1fca82eSMikolaj Golub 		structsize = sizeof(struct kinfo_vmentry);
1982f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
1983f1fca82eSMikolaj Golub 		PROC_LOCK(p);
1984f1fca82eSMikolaj Golub 		kern_proc_vmmap_out(p, sb);
1985f1fca82eSMikolaj Golub 	}
1986f1fca82eSMikolaj Golub }
1987f1fca82eSMikolaj Golub 
1988f1fca82eSMikolaj Golub static void
1989f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
1990f1fca82eSMikolaj Golub {
1991f1fca82eSMikolaj Golub 	struct proc *p;
1992f1fca82eSMikolaj Golub 	size_t size;
1993f1fca82eSMikolaj Golub 	int structsize;
1994f1fca82eSMikolaj Golub 
1995f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
1996f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
1997f1fca82eSMikolaj Golub 	if (sb != NULL) {
1998f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
1999f1fca82eSMikolaj Golub 		structsize = sizeof(gid_t);
2000f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2001f1fca82eSMikolaj Golub 		sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2002f1fca82eSMikolaj Golub 		    sizeof(gid_t));
2003f1fca82eSMikolaj Golub 	}
2004f1fca82eSMikolaj Golub 	*sizep = size;
2005f1fca82eSMikolaj Golub }
2006f1fca82eSMikolaj Golub 
2007f1fca82eSMikolaj Golub static void
2008f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2009f1fca82eSMikolaj Golub {
2010f1fca82eSMikolaj Golub 	struct proc *p;
2011f1fca82eSMikolaj Golub 	size_t size;
2012f1fca82eSMikolaj Golub 	int structsize;
2013f1fca82eSMikolaj Golub 
2014f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2015f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
2016f1fca82eSMikolaj Golub 	if (sb != NULL) {
2017f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2018f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_fd->fd_cmask);
2019f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2020f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
2021f1fca82eSMikolaj Golub 	}
2022f1fca82eSMikolaj Golub 	*sizep = size;
2023f1fca82eSMikolaj Golub }
2024f1fca82eSMikolaj Golub 
2025f1fca82eSMikolaj Golub static void
2026f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2027f1fca82eSMikolaj Golub {
2028f1fca82eSMikolaj Golub 	struct proc *p;
2029f1fca82eSMikolaj Golub 	struct rlimit rlim[RLIM_NLIMITS];
2030f1fca82eSMikolaj Golub 	size_t size;
2031f1fca82eSMikolaj Golub 	int structsize, i;
2032f1fca82eSMikolaj Golub 
2033f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2034f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(rlim);
2035f1fca82eSMikolaj Golub 	if (sb != NULL) {
2036f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2037f1fca82eSMikolaj Golub 		structsize = sizeof(rlim);
2038f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2039f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2040f1fca82eSMikolaj Golub 		for (i = 0; i < RLIM_NLIMITS; i++)
2041f6f6d240SMateusz Guzik 			lim_rlimit_proc(p, i, &rlim[i]);
2042f1fca82eSMikolaj Golub 		PROC_UNLOCK(p);
2043f1fca82eSMikolaj Golub 		sbuf_bcat(sb, rlim, sizeof(rlim));
2044f1fca82eSMikolaj Golub 	}
2045f1fca82eSMikolaj Golub 	*sizep = size;
2046f1fca82eSMikolaj Golub }
2047f1fca82eSMikolaj Golub 
2048f1fca82eSMikolaj Golub static void
2049f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2050f1fca82eSMikolaj Golub {
2051f1fca82eSMikolaj Golub 	struct proc *p;
2052f1fca82eSMikolaj Golub 	size_t size;
2053f1fca82eSMikolaj Golub 	int structsize;
2054f1fca82eSMikolaj Golub 
2055f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2056f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_osrel);
2057f1fca82eSMikolaj Golub 	if (sb != NULL) {
2058f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2059f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_osrel);
2060f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2061f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2062f1fca82eSMikolaj Golub 	}
2063f1fca82eSMikolaj Golub 	*sizep = size;
2064f1fca82eSMikolaj Golub }
2065f1fca82eSMikolaj Golub 
2066f1fca82eSMikolaj Golub static void
2067f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2068f1fca82eSMikolaj Golub {
2069f1fca82eSMikolaj Golub 	struct proc *p;
2070f1fca82eSMikolaj Golub 	elf_ps_strings_t ps_strings;
2071f1fca82eSMikolaj Golub 	size_t size;
2072f1fca82eSMikolaj Golub 	int structsize;
2073f1fca82eSMikolaj Golub 
2074f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2075f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(ps_strings);
2076f1fca82eSMikolaj Golub 	if (sb != NULL) {
2077f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2078f1fca82eSMikolaj Golub 		structsize = sizeof(ps_strings);
2079f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2080f1fca82eSMikolaj Golub 		ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2081f1fca82eSMikolaj Golub #else
2082f1fca82eSMikolaj Golub 		ps_strings = p->p_sysent->sv_psstrings;
2083f1fca82eSMikolaj Golub #endif
2084f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2085f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2086f1fca82eSMikolaj Golub 	}
2087f1fca82eSMikolaj Golub 	*sizep = size;
2088f1fca82eSMikolaj Golub }
2089f1fca82eSMikolaj Golub 
2090f1fca82eSMikolaj Golub static void
2091f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2092f1fca82eSMikolaj Golub {
2093f1fca82eSMikolaj Golub 	struct proc *p;
2094f1fca82eSMikolaj Golub 	size_t size;
2095f1fca82eSMikolaj Golub 	int structsize;
2096f1fca82eSMikolaj Golub 
2097f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2098f1fca82eSMikolaj Golub 	if (sb == NULL) {
2099f1fca82eSMikolaj Golub 		size = 0;
2100f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2101f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2102f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2103f1fca82eSMikolaj Golub 		PHOLD(p);
2104f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2105f1fca82eSMikolaj Golub 		PRELE(p);
2106f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2107f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2108f1fca82eSMikolaj Golub 		*sizep = size;
2109f1fca82eSMikolaj Golub 	} else {
2110f1fca82eSMikolaj Golub 		structsize = sizeof(Elf_Auxinfo);
2111f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2112f1fca82eSMikolaj Golub 		PHOLD(p);
2113f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2114f1fca82eSMikolaj Golub 		PRELE(p);
2115f1fca82eSMikolaj Golub 	}
2116f1fca82eSMikolaj Golub }
2117f1fca82eSMikolaj Golub 
211832c01de2SDmitry Chagin static boolean_t
21191a9c7decSKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
21201a9c7decSKonstantin Belousov     int32_t *osrel, const Elf_Phdr *pnote)
212132c01de2SDmitry Chagin {
2122267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
212332c01de2SDmitry Chagin 	const char *note_name;
212432c01de2SDmitry Chagin 	int i;
212532c01de2SDmitry Chagin 
2126d19d5bf4STijl Coosemans 	if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
2127d19d5bf4STijl Coosemans 	    pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
212832c01de2SDmitry Chagin 		return (FALSE);
212932c01de2SDmitry Chagin 
2130267c52fcSKonstantin Belousov 	note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
213132c01de2SDmitry Chagin 	note_end = (const Elf_Note *)(imgp->image_header +
213232c01de2SDmitry Chagin 	    pnote->p_offset + pnote->p_filesz);
2133267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2134d1ae5c83SKonstantin Belousov 		if (!aligned(note, Elf32_Addr) || (const char *)note_end -
2135d1ae5c83SKonstantin Belousov 		    (const char *)note < sizeof(Elf_Note))
2136267c52fcSKonstantin Belousov 			return (FALSE);
213732c01de2SDmitry Chagin 		if (note->n_namesz != checknote->hdr.n_namesz ||
213832c01de2SDmitry Chagin 		    note->n_descsz != checknote->hdr.n_descsz ||
213932c01de2SDmitry Chagin 		    note->n_type != checknote->hdr.n_type)
214032c01de2SDmitry Chagin 			goto nextnote;
214132c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
2142d1ae5c83SKonstantin Belousov 		if (note_name + checknote->hdr.n_namesz >=
2143d1ae5c83SKonstantin Belousov 		    (const char *)note_end || strncmp(checknote->vendor,
2144d1ae5c83SKonstantin Belousov 		    note_name, checknote->hdr.n_namesz) != 0)
214532c01de2SDmitry Chagin 			goto nextnote;
214632c01de2SDmitry Chagin 
214732c01de2SDmitry Chagin 		/*
214832c01de2SDmitry Chagin 		 * Fetch the osreldate for binary
214932c01de2SDmitry Chagin 		 * from the ELF OSABI-note if necessary.
215032c01de2SDmitry Chagin 		 */
215189ffc202SBjoern A. Zeeb 		if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
215289ffc202SBjoern A. Zeeb 		    checknote->trans_osrel != NULL)
215389ffc202SBjoern A. Zeeb 			return (checknote->trans_osrel(note, osrel));
215432c01de2SDmitry Chagin 		return (TRUE);
215532c01de2SDmitry Chagin 
215632c01de2SDmitry Chagin nextnote:
215732c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
21581b8388cdSMikolaj Golub 		    roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
21591b8388cdSMikolaj Golub 		    roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
216032c01de2SDmitry Chagin 	}
216132c01de2SDmitry Chagin 
216232c01de2SDmitry Chagin 	return (FALSE);
216332c01de2SDmitry Chagin }
216432c01de2SDmitry Chagin 
216532c01de2SDmitry Chagin /*
21661a9c7decSKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote,
21671a9c7decSKonstantin Belousov  * fetch the osreldate for binary from the ELF OSABI-note. Only the
21681a9c7decSKonstantin Belousov  * first page of the image is searched, the same as for headers.
21691a9c7decSKonstantin Belousov  */
21701a9c7decSKonstantin Belousov static boolean_t
21711a9c7decSKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
21721a9c7decSKonstantin Belousov     int32_t *osrel)
21731a9c7decSKonstantin Belousov {
21741a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
21751a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
21761a9c7decSKonstantin Belousov 	int i;
21771a9c7decSKonstantin Belousov 
21781a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
21791a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
21801a9c7decSKonstantin Belousov 
21811a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
21821a9c7decSKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE &&
21831a9c7decSKonstantin Belousov 		    __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
21841a9c7decSKonstantin Belousov 			return (TRUE);
21851a9c7decSKonstantin Belousov 	}
21861a9c7decSKonstantin Belousov 	return (FALSE);
21871a9c7decSKonstantin Belousov 
21881a9c7decSKonstantin Belousov }
21891a9c7decSKonstantin Belousov 
21901a9c7decSKonstantin Belousov /*
2191e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
2192e1743d02SSøren Schmidt  */
2193a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
2194a360a43dSJake Burkholder 	__CONCAT(exec_, __elfN(imgact)),
2195a360a43dSJake Burkholder 	__XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2196a360a43dSJake Burkholder };
2197a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2198e7228204SAlfred Perlstein 
2199ed167eaaSKonstantin Belousov static vm_prot_t
2200ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
2201ed167eaaSKonstantin Belousov {
2202ed167eaaSKonstantin Belousov 	vm_prot_t prot;
2203ed167eaaSKonstantin Belousov 
2204ed167eaaSKonstantin Belousov 	prot = 0;
2205ed167eaaSKonstantin Belousov 	if (flags & PF_X)
2206ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
2207ed167eaaSKonstantin Belousov 	if (flags & PF_W)
2208ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
2209ed167eaaSKonstantin Belousov 	if (flags & PF_R)
2210ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
2211676eda08SMarcel Moolenaar #if __ELF_WORD_SIZE == 32
2212e7d939bdSMarcel Moolenaar #if defined(__amd64__)
2213126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
2214676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
2215676eda08SMarcel Moolenaar #endif
2216676eda08SMarcel Moolenaar #endif
2217ed167eaaSKonstantin Belousov 	return (prot);
2218ed167eaaSKonstantin Belousov }
2219ed167eaaSKonstantin Belousov 
2220ed167eaaSKonstantin Belousov static Elf_Word
2221ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
2222ed167eaaSKonstantin Belousov {
2223ed167eaaSKonstantin Belousov 	Elf_Word flags;
2224ed167eaaSKonstantin Belousov 
2225ed167eaaSKonstantin Belousov 	flags = 0;
2226ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
2227ed167eaaSKonstantin Belousov 		flags |= PF_X;
2228ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
2229ed167eaaSKonstantin Belousov 		flags |= PF_R;
2230ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
2231ed167eaaSKonstantin Belousov 		flags |= PF_W;
2232ed167eaaSKonstantin Belousov 	return (flags);
2233ed167eaaSKonstantin Belousov }
2234