xref: /freebsd/sys/kern/imgact_elf.c (revision 5420f76b59977eab4d2e8a9f1e10d342e76dca59)
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,
1161cbb879dSEd Maste     &elf_legacy_coredump, 0,
1171cbb879dSEd Maste     "include all and only RW pages in core dumps");
118e1743d02SSøren Schmidt 
11962c625fdSKonstantin Belousov int __elfN(nxstack) =
1204d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \
1214d22d07aSKonstantin Belousov     (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__)
12262c625fdSKonstantin Belousov 	1;
12362c625fdSKonstantin Belousov #else
12462c625fdSKonstantin Belousov 	0;
12562c625fdSKonstantin Belousov #endif
126291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
127291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
128291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
129291c06a1SKonstantin Belousov 
130126b36a2SKonstantin Belousov #if __ELF_WORD_SIZE == 32
131e7d939bdSMarcel Moolenaar #if defined(__amd64__)
132126b36a2SKonstantin Belousov int i386_read_exec = 0;
133126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
134126b36a2SKonstantin Belousov     "enable execution from readable segments");
135126b36a2SKonstantin Belousov #endif
136126b36a2SKonstantin Belousov #endif
137126b36a2SKonstantin Belousov 
1383ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
139e1743d02SSøren Schmidt 
140d9c9c81cSPedro F. Giffuni #define	trunc_page_ps(va, ps)	rounddown2(va, ps)
141d9c9c81cSPedro F. Giffuni #define	round_page_ps(va, ps)	roundup2(va, ps)
14293d1c728SKonstantin Belousov #define	aligned(a, t)	(trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
14393d1c728SKonstantin Belousov 
14432c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
14532c01de2SDmitry Chagin 
14632c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = {
14732c01de2SDmitry Chagin 	.hdr.n_namesz	= sizeof(FREEBSD_ABI_VENDOR),
14832c01de2SDmitry Chagin 	.hdr.n_descsz	= sizeof(int32_t),
1494c22b468SEd Maste 	.hdr.n_type	= NT_FREEBSD_ABI_TAG,
15032c01de2SDmitry Chagin 	.vendor		= FREEBSD_ABI_VENDOR,
15189ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
15289ffc202SBjoern A. Zeeb 	.trans_osrel	= __elfN(freebsd_trans_osrel)
15332c01de2SDmitry Chagin };
15432c01de2SDmitry Chagin 
15589ffc202SBjoern A. Zeeb static boolean_t
15689ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
15789ffc202SBjoern A. Zeeb {
15889ffc202SBjoern A. Zeeb 	uintptr_t p;
15989ffc202SBjoern A. Zeeb 
16089ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1611b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
16289ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
16389ffc202SBjoern A. Zeeb 
16489ffc202SBjoern A. Zeeb 	return (TRUE);
16589ffc202SBjoern A. Zeeb }
16689ffc202SBjoern A. Zeeb 
16789ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU";
16889ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3;
16989ffc202SBjoern A. Zeeb 
17089ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = {
17189ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
17289ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
17389ffc202SBjoern A. Zeeb 	.hdr.n_type	= 1,
17489ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
17589ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
17689ffc202SBjoern A. Zeeb 	.trans_osrel	= kfreebsd_trans_osrel
17789ffc202SBjoern A. Zeeb };
17889ffc202SBjoern A. Zeeb 
17989ffc202SBjoern A. Zeeb static boolean_t
18089ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
18189ffc202SBjoern A. Zeeb {
18289ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
18389ffc202SBjoern A. Zeeb 	uintptr_t p;
18489ffc202SBjoern A. Zeeb 
18589ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1861b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
18789ffc202SBjoern A. Zeeb 
18889ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
18989ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
19089ffc202SBjoern A. Zeeb 		return (FALSE);
19189ffc202SBjoern A. Zeeb 
19289ffc202SBjoern A. Zeeb 	/*
19389ffc202SBjoern A. Zeeb 	 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
19489ffc202SBjoern A. Zeeb 	 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
19589ffc202SBjoern A. Zeeb 	 */
19689ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
19789ffc202SBjoern A. Zeeb 
19889ffc202SBjoern A. Zeeb 	return (TRUE);
19989ffc202SBjoern A. Zeeb }
20089ffc202SBjoern A. Zeeb 
201e1743d02SSøren Schmidt int
2023ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
203e1743d02SSøren Schmidt {
204e1743d02SSøren Schmidt 	int i;
205e1743d02SSøren Schmidt 
2063ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
207ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == NULL) {
208ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = entry;
209e1743d02SSøren Schmidt 			break;
210e1743d02SSøren Schmidt 		}
211e1743d02SSøren Schmidt 	}
212925c8b5bSBjoern A. Zeeb 	if (i == MAX_BRANDS) {
213925c8b5bSBjoern A. Zeeb 		printf("WARNING: %s: could not insert brandinfo entry: %p\n",
214925c8b5bSBjoern A. Zeeb 			__func__, entry);
215a7cddfedSJake Burkholder 		return (-1);
216925c8b5bSBjoern A. Zeeb 	}
217a7cddfedSJake Burkholder 	return (0);
218e1743d02SSøren Schmidt }
219e1743d02SSøren Schmidt 
220e1743d02SSøren Schmidt int
2213ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
222e1743d02SSøren Schmidt {
223e1743d02SSøren Schmidt 	int i;
224e1743d02SSøren Schmidt 
2253ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
226ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == entry) {
227ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = NULL;
228e1743d02SSøren Schmidt 			break;
229e1743d02SSøren Schmidt 		}
230e1743d02SSøren Schmidt 	}
231ea5a2b2eSSøren Schmidt 	if (i == MAX_BRANDS)
232a7cddfedSJake Burkholder 		return (-1);
233a7cddfedSJake Burkholder 	return (0);
234e1743d02SSøren Schmidt }
235e1743d02SSøren Schmidt 
236096977faSMark Newton int
2373ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry)
238096977faSMark Newton {
239096977faSMark Newton 	struct proc *p;
240553629ebSJake Burkholder 	int rval = FALSE;
241096977faSMark Newton 
2421005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2434f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
244553629ebSJake Burkholder 		if (p->p_sysent == entry->sysvec) {
245553629ebSJake Burkholder 			rval = TRUE;
246553629ebSJake Burkholder 			break;
247096977faSMark Newton 		}
248553629ebSJake Burkholder 	}
2491005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
250096977faSMark Newton 
251553629ebSJake Burkholder 	return (rval);
252096977faSMark Newton }
253096977faSMark Newton 
2545fe3ed62SJake Burkholder static Elf_Brandinfo *
25532c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
256d1ae5c83SKonstantin Belousov     int interp_name_len, int32_t *osrel)
2575fe3ed62SJake Burkholder {
25832c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
259af582aaeSKonstantin Belousov 	Elf_Brandinfo *bi, *bi_m;
26032c01de2SDmitry Chagin 	boolean_t ret;
2615fe3ed62SJake Burkholder 	int i;
2625fe3ed62SJake Burkholder 
2635fe3ed62SJake Burkholder 	/*
26432c01de2SDmitry Chagin 	 * We support four types of branding -- (1) the ELF EI_OSABI field
2655fe3ed62SJake Burkholder 	 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
26632c01de2SDmitry Chagin 	 * branding w/in the ELF header, (3) path of the `interp_path'
26732c01de2SDmitry Chagin 	 * field, and (4) the ".note.ABI-tag" ELF section.
2685fe3ed62SJake Burkholder 	 */
2695fe3ed62SJake Burkholder 
27032c01de2SDmitry Chagin 	/* Look for an ".note.ABI-tag" ELF section */
271af582aaeSKonstantin Belousov 	bi_m = NULL;
27232c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
27332c01de2SDmitry Chagin 		bi = elf_brand_list[i];
274ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
275ecc2fda8SBjoern A. Zeeb 			continue;
276ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
277ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
27832c01de2SDmitry Chagin 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
279f19d421aSNathan Whitehorn 			/* Give brand a chance to veto check_note's guess */
280f19d421aSNathan Whitehorn 			if (ret && bi->header_supported)
281f19d421aSNathan Whitehorn 				ret = bi->header_supported(imgp);
282af582aaeSKonstantin Belousov 			/*
283af582aaeSKonstantin Belousov 			 * If note checker claimed the binary, but the
284af582aaeSKonstantin Belousov 			 * interpreter path in the image does not
285af582aaeSKonstantin Belousov 			 * match default one for the brand, try to
286af582aaeSKonstantin Belousov 			 * search for other brands with the same
287af582aaeSKonstantin Belousov 			 * interpreter.  Either there is better brand
288af582aaeSKonstantin Belousov 			 * with the right interpreter, or, failing
289af582aaeSKonstantin Belousov 			 * this, we return first brand which accepted
290af582aaeSKonstantin Belousov 			 * our note and, optionally, header.
291af582aaeSKonstantin Belousov 			 */
292af582aaeSKonstantin Belousov 			if (ret && bi_m == NULL && (strlen(bi->interp_path) +
293af582aaeSKonstantin Belousov 			    1 != interp_name_len || strncmp(interp,
294af582aaeSKonstantin Belousov 			    bi->interp_path, interp_name_len) != 0)) {
295af582aaeSKonstantin Belousov 				bi_m = bi;
296af582aaeSKonstantin Belousov 				ret = 0;
297af582aaeSKonstantin Belousov 			}
29832c01de2SDmitry Chagin 			if (ret)
29932c01de2SDmitry Chagin 				return (bi);
30032c01de2SDmitry Chagin 		}
30132c01de2SDmitry Chagin 	}
302af582aaeSKonstantin Belousov 	if (bi_m != NULL)
303af582aaeSKonstantin Belousov 		return (bi_m);
30432c01de2SDmitry Chagin 
3055fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
3065fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3075fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
308ecc2fda8SBjoern A. Zeeb 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
309ecc2fda8SBjoern A. Zeeb 			continue;
310ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
3115fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
3125fe3ed62SJake Burkholder 		    strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
313686d2f31SNathan Whitehorn 		    bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0)) {
314686d2f31SNathan Whitehorn 			/* Looks good, but give brand a chance to veto */
315686d2f31SNathan Whitehorn 			if (!bi->header_supported || bi->header_supported(imgp))
3165fe3ed62SJake Burkholder 				return (bi);
3175fe3ed62SJake Burkholder 		}
318686d2f31SNathan Whitehorn 	}
3195fe3ed62SJake Burkholder 
320817dc004SWarner Losh 	/* No known brand, see if the header is recognized by any brand */
321817dc004SWarner Losh 	for (i = 0; i < MAX_BRANDS; i++) {
322817dc004SWarner Losh 		bi = elf_brand_list[i];
323817dc004SWarner Losh 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
324817dc004SWarner Losh 		    bi->header_supported == NULL)
325817dc004SWarner Losh 			continue;
326817dc004SWarner Losh 		if (hdr->e_machine == bi->machine) {
327817dc004SWarner Losh 			ret = bi->header_supported(imgp);
328817dc004SWarner Losh 			if (ret)
329817dc004SWarner Losh 				return (bi);
330817dc004SWarner Losh 		}
331817dc004SWarner Losh 	}
332817dc004SWarner Losh 
3335fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
3345fe3ed62SJake Burkholder 	if (interp != NULL) {
3355fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
3365fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
337ecc2fda8SBjoern A. Zeeb 			if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
338ecc2fda8SBjoern A. Zeeb 				continue;
339ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
340d1ae5c83SKonstantin Belousov 			    /* ELF image p_filesz includes terminating zero */
341d1ae5c83SKonstantin Belousov 			    strlen(bi->interp_path) + 1 == interp_name_len &&
342d1ae5c83SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
343d1ae5c83SKonstantin Belousov 			    == 0)
3445fe3ed62SJake Burkholder 				return (bi);
3455fe3ed62SJake Burkholder 		}
3465fe3ed62SJake Burkholder 	}
3475fe3ed62SJake Burkholder 
3485fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
3495fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3505fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
351ecc2fda8SBjoern A. Zeeb 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
352ecc2fda8SBjoern A. Zeeb 			continue;
353ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
354e548a1d4SJake Burkholder 		    __elfN(fallback_brand) == bi->brand)
3555fe3ed62SJake Burkholder 			return (bi);
3565fe3ed62SJake Burkholder 	}
3575fe3ed62SJake Burkholder 	return (NULL);
3585fe3ed62SJake Burkholder }
3595fe3ed62SJake Burkholder 
360e1743d02SSøren Schmidt static int
3613ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
362e1743d02SSøren Schmidt {
363d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
3643ebc1248SPeter Wemm 	int i;
3653ebc1248SPeter Wemm 
36652c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
36752c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
36852c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
3693dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
3703dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
3713dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
372a7cddfedSJake Burkholder 		return (ENOEXEC);
373e1743d02SSøren Schmidt 
3743ebc1248SPeter Wemm 	/*
3753ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
3763ebc1248SPeter Wemm 	 */
3773ebc1248SPeter Wemm 
3783ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
379d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
380d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
3813ebc1248SPeter Wemm 			break;
3823ebc1248SPeter Wemm 	}
3833ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
384a7cddfedSJake Burkholder 		return (ENOEXEC);
385e1743d02SSøren Schmidt 
386a7cddfedSJake Burkholder 	return (0);
387e1743d02SSøren Schmidt }
388e1743d02SSøren Schmidt 
389e1743d02SSøren Schmidt static int
3903ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
391ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
3923ebc1248SPeter Wemm {
393da61b9a6SAlan Cox 	struct sf_buf *sf;
394da61b9a6SAlan Cox 	int error;
3953ebc1248SPeter Wemm 	vm_offset_t off;
3963ebc1248SPeter Wemm 
3973ebc1248SPeter Wemm 	/*
3983ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
3993ebc1248SPeter Wemm 	 */
4003ebc1248SPeter Wemm 	vm_map_lock(map);
401ff6f03c7SAlan Cox 	vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
402ff6f03c7SAlan Cox 	    VM_PROT_ALL, VM_PROT_ALL, 0);
4033ebc1248SPeter Wemm 	vm_map_unlock(map);
4043ebc1248SPeter Wemm 
4053ebc1248SPeter Wemm 	/*
4063ebc1248SPeter Wemm 	 * Find the page from the underlying object.
4073ebc1248SPeter Wemm 	 */
4083ebc1248SPeter Wemm 	if (object) {
409da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
410da61b9a6SAlan Cox 		if (sf == NULL)
411da61b9a6SAlan Cox 			return (KERN_FAILURE);
4123ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
413da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
414ca0387efSJake Burkholder 		    end - start);
415be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
4163ebc1248SPeter Wemm 		if (error) {
417a7cddfedSJake Burkholder 			return (KERN_FAILURE);
4183ebc1248SPeter Wemm 		}
4193ebc1248SPeter Wemm 	}
4203ebc1248SPeter Wemm 
421a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
4223ebc1248SPeter Wemm }
4233ebc1248SPeter Wemm 
4243ebc1248SPeter Wemm static int
4253ebc1248SPeter Wemm __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
426ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
4273ebc1248SPeter Wemm {
428da61b9a6SAlan Cox 	struct sf_buf *sf;
429da61b9a6SAlan Cox 	vm_offset_t off;
430a063facbSMarcel Moolenaar 	vm_size_t sz;
431a063facbSMarcel Moolenaar 	int error, rv;
4323ebc1248SPeter Wemm 
4333ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
43481f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
435ff6f03c7SAlan Cox 		    round_page(start), prot);
4363ebc1248SPeter Wemm 		if (rv)
437a7cddfedSJake Burkholder 			return (rv);
4383ebc1248SPeter Wemm 		offset += round_page(start) - start;
4393ebc1248SPeter Wemm 		start = round_page(start);
4403ebc1248SPeter Wemm 	}
4413ebc1248SPeter Wemm 	if (end != round_page(end)) {
44281f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
443ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
4443ebc1248SPeter Wemm 		if (rv)
445a7cddfedSJake Burkholder 			return (rv);
4463ebc1248SPeter Wemm 		end = trunc_page(end);
4473ebc1248SPeter Wemm 	}
4483ebc1248SPeter Wemm 	if (end > start) {
4493ebc1248SPeter Wemm 		if (offset & PAGE_MASK) {
4503ebc1248SPeter Wemm 			/*
4513ebc1248SPeter Wemm 			 * The mapping is not page aligned. This means we have
4523ebc1248SPeter Wemm 			 * to copy the data. Sigh.
4533ebc1248SPeter Wemm 			 */
454edb572a3SJohn Baldwin 			rv = vm_map_find(map, NULL, 0, &start, end - start, 0,
455edb572a3SJohn Baldwin 			    VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL,
456edb572a3SJohn Baldwin 			    0);
457*5420f76bSKonstantin Belousov 			if (rv != KERN_SUCCESS)
458a7cddfedSJake Burkholder 				return (rv);
459da61b9a6SAlan Cox 			if (object == NULL)
460da61b9a6SAlan Cox 				return (KERN_SUCCESS);
461da61b9a6SAlan Cox 			for (; start < end; start += sz) {
462da61b9a6SAlan Cox 				sf = vm_imgact_map_page(object, offset);
463da61b9a6SAlan Cox 				if (sf == NULL)
464da61b9a6SAlan Cox 					return (KERN_FAILURE);
4653ebc1248SPeter Wemm 				off = offset - trunc_page(offset);
4663ebc1248SPeter Wemm 				sz = end - start;
467da61b9a6SAlan Cox 				if (sz > PAGE_SIZE - off)
468da61b9a6SAlan Cox 					sz = PAGE_SIZE - off;
469da61b9a6SAlan Cox 				error = copyout((caddr_t)sf_buf_kva(sf) + off,
4703ebc1248SPeter Wemm 				    (caddr_t)start, sz);
471be996836SAttilio Rao 				vm_imgact_unmap_page(sf);
472*5420f76bSKonstantin Belousov 				if (error != 0)
473a7cddfedSJake Burkholder 					return (KERN_FAILURE);
474da61b9a6SAlan Cox 				offset += sz;
4753ebc1248SPeter Wemm 			}
4763ebc1248SPeter Wemm 			rv = KERN_SUCCESS;
4773ebc1248SPeter Wemm 		} else {
478e5e6093bSAlan Cox 			vm_object_reference(object);
4793ebc1248SPeter Wemm 			vm_map_lock(map);
4803ebc1248SPeter Wemm 			rv = vm_map_insert(map, object, offset, start, end,
481ff6f03c7SAlan Cox 			    prot, VM_PROT_ALL, cow);
4823ebc1248SPeter Wemm 			vm_map_unlock(map);
483e5e6093bSAlan Cox 			if (rv != KERN_SUCCESS)
484e5e6093bSAlan Cox 				vm_object_deallocate(object);
4853ebc1248SPeter Wemm 		}
486a7cddfedSJake Burkholder 		return (rv);
4873ebc1248SPeter Wemm 	} else {
488a7cddfedSJake Burkholder 		return (KERN_SUCCESS);
4893ebc1248SPeter Wemm 	}
4903ebc1248SPeter Wemm }
4913ebc1248SPeter Wemm 
4923ebc1248SPeter Wemm static int
493292177e6SAlan Cox __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
4943ebc1248SPeter Wemm     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
4953ebc1248SPeter Wemm     size_t pagesize)
496e1743d02SSøren Schmidt {
497da61b9a6SAlan Cox 	struct sf_buf *sf;
498e1743d02SSøren Schmidt 	size_t map_len;
499292177e6SAlan Cox 	vm_map_t map;
500292177e6SAlan Cox 	vm_object_t object;
501e1743d02SSøren Schmidt 	vm_offset_t map_addr;
502fa7dd9c5SMatthew Dillon 	int error, rv, cow;
503e1743d02SSøren Schmidt 	size_t copy_len;
50452c24af7SPeter Wemm 	vm_offset_t file_addr;
50552c24af7SPeter Wemm 
50625ead034SBrian Feldman 	/*
50725ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
50825ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
50925ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
51025ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
51125ead034SBrian Feldman 	 *
51225ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
51325ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
51425ead034SBrian Feldman 	 */
515292177e6SAlan Cox 	if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
51625ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
51725ead034SBrian Feldman 		return (ENOEXEC);
51825ead034SBrian Feldman 	}
51925ead034SBrian Feldman 
520292177e6SAlan Cox 	object = imgp->object;
521292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
5223ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
5233ebc1248SPeter Wemm 	file_addr = trunc_page_ps(offset, pagesize);
524e1743d02SSøren Schmidt 
525e1743d02SSøren Schmidt 	/*
52652c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
52752c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
52852c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
52952c24af7SPeter Wemm 	 * choose the second..
53052c24af7SPeter Wemm 	 */
53152c24af7SPeter Wemm 	if (memsz > filsz)
5323ebc1248SPeter Wemm 		map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
53352c24af7SPeter Wemm 	else
5343ebc1248SPeter Wemm 		map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
53552c24af7SPeter Wemm 
53652c24af7SPeter Wemm 	if (map_len != 0) {
537fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
538fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
539fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
540fa7dd9c5SMatthew Dillon 
541292177e6SAlan Cox 		rv = __elfN(map_insert)(map,
54252c24af7SPeter Wemm 				      object,
54352c24af7SPeter Wemm 				      file_addr,	/* file offset */
54452c24af7SPeter Wemm 				      map_addr,		/* virtual start */
54552c24af7SPeter Wemm 				      map_addr + map_len,/* virtual end */
54652c24af7SPeter Wemm 				      prot,
547fa7dd9c5SMatthew Dillon 				      cow);
548e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
549a7cddfedSJake Burkholder 			return (EINVAL);
55052c24af7SPeter Wemm 
55152c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
55223955314SAlfred Perlstein 		if (memsz == filsz) {
553a7cddfedSJake Burkholder 			return (0);
55452c24af7SPeter Wemm 		}
55523955314SAlfred Perlstein 	}
55652c24af7SPeter Wemm 
55752c24af7SPeter Wemm 
55852c24af7SPeter Wemm 	/*
55952c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
56052c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
56152c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
56252c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
563e1743d02SSøren Schmidt 	 */
5643ebc1248SPeter Wemm 	copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
5653ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
566ca0387efSJake Burkholder 	map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
567ca0387efSJake Burkholder 	    map_addr;
568e1743d02SSøren Schmidt 
56952c24af7SPeter Wemm 	/* This had damn well better be true! */
5708191d577SPeter Wemm 	if (map_len != 0) {
571292177e6SAlan Cox 		rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
572292177e6SAlan Cox 		    map_len, VM_PROT_ALL, 0);
57323955314SAlfred Perlstein 		if (rv != KERN_SUCCESS) {
574a7cddfedSJake Burkholder 			return (EINVAL);
5758191d577SPeter Wemm 		}
57623955314SAlfred Perlstein 	}
577e1743d02SSøren Schmidt 
57852c24af7SPeter Wemm 	if (copy_len != 0) {
5793ebc1248SPeter Wemm 		vm_offset_t off;
580da61b9a6SAlan Cox 
581da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
582da61b9a6SAlan Cox 		if (sf == NULL)
583da61b9a6SAlan Cox 			return (EIO);
584e1743d02SSøren Schmidt 
58552c24af7SPeter Wemm 		/* send the page fragment to user space */
58681f223caSJake Burkholder 		off = trunc_page_ps(offset + filsz, pagesize) -
58781f223caSJake Burkholder 		    trunc_page(offset + filsz);
588da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off,
589da61b9a6SAlan Cox 		    (caddr_t)map_addr, copy_len);
590be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
59123955314SAlfred Perlstein 		if (error) {
59252c24af7SPeter Wemm 			return (error);
59352c24af7SPeter Wemm 		}
59423955314SAlfred Perlstein 	}
595e1743d02SSøren Schmidt 
596e1743d02SSøren Schmidt 	/*
5973ebc1248SPeter Wemm 	 * set it to the specified protection.
5983ebc1248SPeter Wemm 	 * XXX had better undo the damage from pasting over the cracks here!
599e1743d02SSøren Schmidt 	 */
600292177e6SAlan Cox 	vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
601292177e6SAlan Cox 	    map_len), prot, FALSE);
6028191d577SPeter Wemm 
603ff6f03c7SAlan Cox 	return (0);
604e1743d02SSøren Schmidt }
605e1743d02SSøren Schmidt 
606c33fe779SJohn Polstra /*
607c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
608c33fe779SJohn Polstra  * or an executable.
609c33fe779SJohn Polstra  *
610c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
611c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
612c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
613c33fe779SJohn Polstra  * where the file was actually loaded.
614c33fe779SJohn Polstra  *
615c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
616c33fe779SJohn Polstra  * the entry point for the loaded file.
617c33fe779SJohn Polstra  */
618e1743d02SSøren Schmidt static int
6193ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
6203ebc1248SPeter Wemm 	u_long *entry, size_t pagesize)
621e1743d02SSøren Schmidt {
622911c2be0SMark Peek 	struct {
623911c2be0SMark Peek 		struct nameidata nd;
624911c2be0SMark Peek 		struct vattr attr;
625911c2be0SMark Peek 		struct image_params image_params;
626911c2be0SMark Peek 	} *tempdata;
627d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
628d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
629911c2be0SMark Peek 	struct nameidata *nd;
630911c2be0SMark Peek 	struct vattr *attr;
631911c2be0SMark Peek 	struct image_params *imgp;
63252c24af7SPeter Wemm 	vm_prot_t prot;
633c33fe779SJohn Polstra 	u_long rbase;
634c33fe779SJohn Polstra 	u_long base_addr = 0;
6355050aa86SKonstantin Belousov 	int error, i, numsegs;
636e1743d02SSøren Schmidt 
63712bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
63812bc222eSJonathan Anderson 	/*
63912bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
64012bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
64112bc222eSJonathan Anderson 	 */
64212bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
64312bc222eSJonathan Anderson 		return (ECAPMODE);
64412bc222eSJonathan Anderson #endif
64512bc222eSJonathan Anderson 
646a163d034SWarner Losh 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
647911c2be0SMark Peek 	nd = &tempdata->nd;
648911c2be0SMark Peek 	attr = &tempdata->attr;
649911c2be0SMark Peek 	imgp = &tempdata->image_params;
650911c2be0SMark Peek 
651c8a79999SPeter Wemm 	/*
652c8a79999SPeter Wemm 	 * Initialize part of the common data
653c8a79999SPeter Wemm 	 */
654c8a79999SPeter Wemm 	imgp->proc = p;
655911c2be0SMark Peek 	imgp->attr = attr;
656c8a79999SPeter Wemm 	imgp->firstpage = NULL;
65759c8bc40SAlan Cox 	imgp->image_header = NULL;
6580b2ed1aeSJeff Roberson 	imgp->object = NULL;
6596d7bdc8dSRobert Watson 	imgp->execlabel = NULL;
660c8a79999SPeter Wemm 
6615050aa86SKonstantin Belousov 	NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
662911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
663911c2be0SMark Peek 		nd->ni_vp = NULL;
664e1743d02SSøren Schmidt 		goto fail;
665e1743d02SSøren Schmidt 	}
666911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
667911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
668c8a79999SPeter Wemm 
669e1743d02SSøren Schmidt 	/*
670e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
671e1743d02SSøren Schmidt 	 */
672c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
673373d1a3fSAlan Cox 	if (error)
674c8a79999SPeter Wemm 		goto fail;
675e1743d02SSøren Schmidt 
676c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
677373d1a3fSAlan Cox 	if (error)
678373d1a3fSAlan Cox 		goto fail;
679373d1a3fSAlan Cox 
68025ead034SBrian Feldman 	/*
68125ead034SBrian Feldman 	 * Also make certain that the interpreter stays the same, so set
682e6e370a7SJeff Roberson 	 * its VV_TEXT flag, too.
68325ead034SBrian Feldman 	 */
684877d24acSKonstantin Belousov 	VOP_SET_TEXT(nd->ni_vp);
685e6e370a7SJeff Roberson 
6868516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
687e1743d02SSøren Schmidt 
688d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
6893ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
690e1743d02SSøren Schmidt 		goto fail;
691c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
692c33fe779SJohn Polstra 		rbase = *addr;
693c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
694c33fe779SJohn Polstra 		rbase = 0;
695c33fe779SJohn Polstra 	else {
696c33fe779SJohn Polstra 		error = ENOEXEC;
697c33fe779SJohn Polstra 		goto fail;
698c33fe779SJohn Polstra 	}
699e1743d02SSøren Schmidt 
700c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
70152c24af7SPeter Wemm 	if ((hdr->e_phoff > PAGE_SIZE) ||
702d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
703c8a79999SPeter Wemm 		error = ENOEXEC;
704e1743d02SSøren Schmidt 		goto fail;
705c8a79999SPeter Wemm 	}
706c8a79999SPeter Wemm 
707d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
70893d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
70993d1c728SKonstantin Belousov 		error = ENOEXEC;
71093d1c728SKonstantin Belousov 		goto fail;
71193d1c728SKonstantin Belousov 	}
712e1743d02SSøren Schmidt 
713c33fe779SJohn Polstra 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
7145b33842aSKonstantin Belousov 		if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
7155b33842aSKonstantin Belousov 			/* Loadable segment */
716ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
717292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
71881f223caSJake Burkholder 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
719292177e6SAlan Cox 			    phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
720292177e6SAlan Cox 			if (error != 0)
721e1743d02SSøren Schmidt 				goto fail;
722e1743d02SSøren Schmidt 			/*
723c33fe779SJohn Polstra 			 * Establish the base address if this is the
724c33fe779SJohn Polstra 			 * first segment.
725e1743d02SSøren Schmidt 			 */
726c33fe779SJohn Polstra 			if (numsegs == 0)
727ca0387efSJake Burkholder   				base_addr = trunc_page(phdr[i].p_vaddr +
728ca0387efSJake Burkholder 				    rbase);
729c33fe779SJohn Polstra 			numsegs++;
730e1743d02SSøren Schmidt 		}
731e1743d02SSøren Schmidt 	}
732c33fe779SJohn Polstra 	*addr = base_addr;
733c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
734e1743d02SSøren Schmidt 
735e1743d02SSøren Schmidt fail:
736c8a79999SPeter Wemm 	if (imgp->firstpage)
737c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
7380b2ed1aeSJeff Roberson 
739911c2be0SMark Peek 	if (nd->ni_vp)
740373d1a3fSAlan Cox 		vput(nd->ni_vp);
741911c2be0SMark Peek 
742911c2be0SMark Peek 	free(tempdata, M_TEMP);
743e1743d02SSøren Schmidt 
744a7cddfedSJake Burkholder 	return (error);
745e1743d02SSøren Schmidt }
746e1743d02SSøren Schmidt 
747303b270bSEivind Eklund static int
7483ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
749e1743d02SSøren Schmidt {
7506c775eb6SKonstantin Belousov 	struct thread *td;
7516c775eb6SKonstantin Belousov 	const Elf_Ehdr *hdr;
75232c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
753e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
7545856e12eSJohn Dyson 	struct vmspace *vmspace;
7556c775eb6SKonstantin Belousov 	const char *err_str, *newinterp;
7566c775eb6SKonstantin Belousov 	char *interp, *interp_buf, *path;
757d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
7585fe3ed62SJake Burkholder 	struct sysentvec *sv;
7596c775eb6SKonstantin Belousov 	vm_prot_t prot;
7606c775eb6SKonstantin Belousov 	u_long text_size, data_size, total_size, text_addr, data_addr;
7616c775eb6SKonstantin Belousov 	u_long seg_size, seg_addr, addr, baddr, et_dyn_addr, entry, proghdr;
7626c775eb6SKonstantin Belousov 	int32_t osrel;
7636c775eb6SKonstantin Belousov 	int error, i, n, interp_name_len, have_interp;
7646c775eb6SKonstantin Belousov 
7656c775eb6SKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
766e1743d02SSøren Schmidt 
767e1743d02SSøren Schmidt 	/*
768e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
769900b28f9SMaxim Sobolev 	 *
770900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
771900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
772e1743d02SSøren Schmidt 	 */
773900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
774900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
775a7cddfedSJake Burkholder 		return (-1);
776e1743d02SSøren Schmidt 
777e1743d02SSøren Schmidt 	/*
778e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
779e1743d02SSøren Schmidt 	 * detected an ELF file.
780e1743d02SSøren Schmidt 	 */
781e1743d02SSøren Schmidt 
782e1743d02SSøren Schmidt 	if ((hdr->e_phoff > PAGE_SIZE) ||
783d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
784c8a79999SPeter Wemm 		/* Only support headers in first page for now */
7856b16d664SEd Maste 		uprintf("Program headers not in the first page\n");
786a7cddfedSJake Burkholder 		return (ENOEXEC);
787e1743d02SSøren Schmidt 	}
78852c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
7896b16d664SEd Maste 	if (!aligned(phdr, Elf_Addr)) {
7906b16d664SEd Maste 		uprintf("Unaligned program headers\n");
79193d1c728SKonstantin Belousov 		return (ENOEXEC);
7926b16d664SEd Maste 	}
7936c775eb6SKonstantin Belousov 
7946c775eb6SKonstantin Belousov 	n = error = 0;
7957564c4adSKonstantin Belousov 	baddr = 0;
7966c775eb6SKonstantin Belousov 	osrel = 0;
7976c775eb6SKonstantin Belousov 	text_size = data_size = total_size = text_addr = data_addr = 0;
7986c775eb6SKonstantin Belousov 	entry = proghdr = 0;
7996c775eb6SKonstantin Belousov 	interp_name_len = 0;
8006c775eb6SKonstantin Belousov 	err_str = newinterp = NULL;
8016c775eb6SKonstantin Belousov 	interp = interp_buf = NULL;
8026c775eb6SKonstantin Belousov 	td = curthread;
8036c775eb6SKonstantin Belousov 
8045fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
805291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
806291c06a1SKonstantin Belousov 		case PT_LOAD:
8077564c4adSKonstantin Belousov 			if (n == 0)
8087564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
8097564c4adSKonstantin Belousov 			n++;
810291c06a1SKonstantin Belousov 			break;
811291c06a1SKonstantin Belousov 		case PT_INTERP:
812e5e6093bSAlan Cox 			/* Path to interpreter */
8136c775eb6SKonstantin Belousov 			if (phdr[i].p_filesz > MAXPATHLEN) {
8146b16d664SEd Maste 				uprintf("Invalid PT_INTERP\n");
8156c775eb6SKonstantin Belousov 				error = ENOEXEC;
8166c775eb6SKonstantin Belousov 				goto ret;
8176b16d664SEd Maste 			}
818d3ee0a15SJonathan T. Looney 			if (interp != NULL) {
819d3ee0a15SJonathan T. Looney 				uprintf("Multiple PT_INTERP headers\n");
820d3ee0a15SJonathan T. Looney 				error = ENOEXEC;
821d3ee0a15SJonathan T. Looney 				goto ret;
822d3ee0a15SJonathan T. Looney 			}
823d1ae5c83SKonstantin Belousov 			interp_name_len = phdr[i].p_filesz;
8246c775eb6SKonstantin Belousov 			if (phdr[i].p_offset > PAGE_SIZE ||
8256c775eb6SKonstantin Belousov 			    interp_name_len > PAGE_SIZE - phdr[i].p_offset) {
8266c775eb6SKonstantin Belousov 				VOP_UNLOCK(imgp->vp, 0);
8276c775eb6SKonstantin Belousov 				interp_buf = malloc(interp_name_len + 1, M_TEMP,
8286c775eb6SKonstantin Belousov 				    M_WAITOK);
8296c775eb6SKonstantin Belousov 				vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
8306c775eb6SKonstantin Belousov 				error = vn_rdwr(UIO_READ, imgp->vp, interp_buf,
8316c775eb6SKonstantin Belousov 				    interp_name_len, phdr[i].p_offset,
8326c775eb6SKonstantin Belousov 				    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
8336c775eb6SKonstantin Belousov 				    NOCRED, NULL, td);
8346c775eb6SKonstantin Belousov 				if (error != 0) {
8356c775eb6SKonstantin Belousov 					uprintf("i/o error PT_INTERP\n");
8366c775eb6SKonstantin Belousov 					goto ret;
8376c775eb6SKonstantin Belousov 				}
8386c775eb6SKonstantin Belousov 				interp_buf[interp_name_len] = '\0';
8396c775eb6SKonstantin Belousov 				interp = interp_buf;
8406c775eb6SKonstantin Belousov 			} else {
8416c775eb6SKonstantin Belousov 				interp = __DECONST(char *, imgp->image_header) +
8426c775eb6SKonstantin Belousov 				    phdr[i].p_offset;
8436c775eb6SKonstantin Belousov 			}
844291c06a1SKonstantin Belousov 			break;
845291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
846291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
847291c06a1SKonstantin Belousov 				imgp->stack_prot =
848291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
849316b3843SKonstantin Belousov 			imgp->stack_sz = phdr[i].p_memsz;
850291c06a1SKonstantin Belousov 			break;
8513ebc1248SPeter Wemm 		}
8523ebc1248SPeter Wemm 	}
8533ebc1248SPeter Wemm 
854d1ae5c83SKonstantin Belousov 	brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
855d1ae5c83SKonstantin Belousov 	    &osrel);
8565fe3ed62SJake Burkholder 	if (brand_info == NULL) {
8575fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
8585fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
8596c775eb6SKonstantin Belousov 		error = ENOEXEC;
8606c775eb6SKonstantin Belousov 		goto ret;
8613ebc1248SPeter Wemm 	}
862ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
8636b16d664SEd Maste 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
8646b16d664SEd Maste 			uprintf("Cannot execute shared object\n");
8656c775eb6SKonstantin Belousov 			error = ENOEXEC;
8666c775eb6SKonstantin Belousov 			goto ret;
8676b16d664SEd Maste 		}
8687564c4adSKonstantin Belousov 		/*
8697564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
8707564c4adSKonstantin Belousov 		 * non-zero for some reason.
8717564c4adSKonstantin Belousov 		 */
8727564c4adSKonstantin Belousov 		if (baddr == 0)
873ab02d85fSKonstantin Belousov 			et_dyn_addr = ET_DYN_LOAD_ADDR;
8747564c4adSKonstantin Belousov 		else
8757564c4adSKonstantin Belousov 			et_dyn_addr = 0;
876ab02d85fSKonstantin Belousov 	} else
877ab02d85fSKonstantin Belousov 		et_dyn_addr = 0;
8785fe3ed62SJake Burkholder 	sv = brand_info->sysvec;
8799b68618dSPeter Wemm 	if (interp != NULL && brand_info->interp_newpath != NULL)
8804113f8d7SPeter Wemm 		newinterp = brand_info->interp_newpath;
8813ebc1248SPeter Wemm 
88260bb3943SAlan Cox 	/*
88360bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
88460bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
88560bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
88660bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
88760bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
88860bb3943SAlan Cox 	 * v_usecount may become zero.
8891dfab802SAlan Cox 	 *
8901dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
8911dfab802SAlan Cox 	 * the vnode is unlocked.
89260bb3943SAlan Cox 	 */
89322db15c0SAttilio Rao 	VOP_UNLOCK(imgp->vp, 0);
89460bb3943SAlan Cox 
89589b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
89619059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
897e1743d02SSøren Schmidt 
898cb05b60aSAttilio Rao 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
8996c775eb6SKonstantin Belousov 	if (error != 0)
9006c775eb6SKonstantin Belousov 		goto ret;
90160bb3943SAlan Cox 
902e1743d02SSøren Schmidt 	for (i = 0; i < hdr->e_phnum; i++) {
903e1743d02SSøren Schmidt 		switch (phdr[i].p_type) {
904e1743d02SSøren Schmidt 		case PT_LOAD:	/* Loadable segment */
9055b33842aSKonstantin Belousov 			if (phdr[i].p_memsz == 0)
9065b33842aSKonstantin Belousov 				break;
907ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
908292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
909ab02d85fSKonstantin Belousov 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
91081f223caSJake Burkholder 			    phdr[i].p_memsz, phdr[i].p_filesz, prot,
911292177e6SAlan Cox 			    sv->sv_pagesize);
912292177e6SAlan Cox 			if (error != 0)
9136c775eb6SKonstantin Belousov 				goto ret;
914e1743d02SSøren Schmidt 
915cfaf7e60SDoug Rabson 			/*
916cfaf7e60SDoug Rabson 			 * If this segment contains the program headers,
917cfaf7e60SDoug Rabson 			 * remember their virtual address for the AT_PHDR
918cfaf7e60SDoug Rabson 			 * aux entry. Static binaries don't usually include
919cfaf7e60SDoug Rabson 			 * a PT_PHDR entry.
920cfaf7e60SDoug Rabson 			 */
921cfaf7e60SDoug Rabson 			if (phdr[i].p_offset == 0 &&
922cfaf7e60SDoug Rabson 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
923cfaf7e60SDoug Rabson 				<= phdr[i].p_filesz)
924ab02d85fSKonstantin Belousov 				proghdr = phdr[i].p_vaddr + hdr->e_phoff +
925ab02d85fSKonstantin Belousov 				    et_dyn_addr;
926cfaf7e60SDoug Rabson 
927ab02d85fSKonstantin Belousov 			seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
928cac45152SMatthew Dillon 			seg_size = round_page(phdr[i].p_memsz +
929ab02d85fSKonstantin Belousov 			    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
930cac45152SMatthew Dillon 
931e1743d02SSøren Schmidt 			/*
932920acedbSNathan Whitehorn 			 * Make the largest executable segment the official
933920acedbSNathan Whitehorn 			 * text segment and all others data.
93421c2d047SMatthew Dillon 			 *
93521c2d047SMatthew Dillon 			 * Note that obreak() assumes that data_addr +
93621c2d047SMatthew Dillon 			 * data_size == end of data load area, and the ELF
93721c2d047SMatthew Dillon 			 * file format expects segments to be sorted by
93821c2d047SMatthew Dillon 			 * address.  If multiple data segments exist, the
93921c2d047SMatthew Dillon 			 * last one will be used.
940e1743d02SSøren Schmidt 			 */
941920acedbSNathan Whitehorn 
942920acedbSNathan Whitehorn 			if (phdr[i].p_flags & PF_X && text_size < seg_size) {
9439782ecbaSPeter Wemm 				text_size = seg_size;
9449782ecbaSPeter Wemm 				text_addr = seg_addr;
9459782ecbaSPeter Wemm 			} else {
94621c2d047SMatthew Dillon 				data_size = seg_size;
947cac45152SMatthew Dillon 				data_addr = seg_addr;
948cac45152SMatthew Dillon 			}
94921c2d047SMatthew Dillon 			total_size += seg_size;
95096725dd0SAlexander Kabaev 			break;
95196725dd0SAlexander Kabaev 		case PT_PHDR: 	/* Program header table info */
952ab02d85fSKonstantin Belousov 			proghdr = phdr[i].p_vaddr + et_dyn_addr;
95396725dd0SAlexander Kabaev 			break;
95496725dd0SAlexander Kabaev 		default:
95596725dd0SAlexander Kabaev 			break;
95696725dd0SAlexander Kabaev 		}
95796725dd0SAlexander Kabaev 	}
95896725dd0SAlexander Kabaev 
95996725dd0SAlexander Kabaev 	if (data_addr == 0 && data_size == 0) {
96096725dd0SAlexander Kabaev 		data_addr = text_addr;
96196725dd0SAlexander Kabaev 		data_size = text_size;
96296725dd0SAlexander Kabaev 	}
963cac45152SMatthew Dillon 
964920acedbSNathan Whitehorn 	entry = (u_long)hdr->e_entry + et_dyn_addr;
965920acedbSNathan Whitehorn 
966cac45152SMatthew Dillon 	/*
967cac45152SMatthew Dillon 	 * Check limits.  It should be safe to check the
96896725dd0SAlexander Kabaev 	 * limits after loading the segments since we do
96996725dd0SAlexander Kabaev 	 * not actually fault in all the segments pages.
970cac45152SMatthew Dillon 	 */
97191d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
972f6f6d240SMateusz Guzik 	if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
9736b16d664SEd Maste 		err_str = "Data segment size exceeds process limit";
9746b16d664SEd Maste 	else if (text_size > maxtsiz)
9756b16d664SEd Maste 		err_str = "Text segment size exceeds system limit";
976f6f6d240SMateusz Guzik 	else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
9776b16d664SEd Maste 		err_str = "Total segment size exceeds process limit";
9786b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
9796b16d664SEd Maste 		err_str = "Data segment size exceeds resource limit";
9806b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
9816b16d664SEd Maste 		err_str = "Total segment size exceeds resource limit";
9826b16d664SEd Maste 	if (err_str != NULL) {
98391d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
9846b16d664SEd Maste 		uprintf("%s\n", err_str);
9856c775eb6SKonstantin Belousov 		error = ENOMEM;
9866c775eb6SKonstantin Belousov 		goto ret;
987cac45152SMatthew Dillon 	}
988e1743d02SSøren Schmidt 
989292177e6SAlan Cox 	vmspace = imgp->proc->p_vmspace;
990e1743d02SSøren Schmidt 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
9917cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
992e1743d02SSøren Schmidt 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
9937cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
994e1743d02SSøren Schmidt 
995c460ac3aSPeter Wemm 	/*
996c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
997c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
998c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
999c460ac3aSPeter Wemm 	 * its maximum allowed size.
1000c460ac3aSPeter Wemm 	 */
10016c775eb6SKonstantin Belousov 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td,
1002292177e6SAlan Cox 	    RLIMIT_DATA));
100391d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
1004e1743d02SSøren Schmidt 
1005ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
1006ea5a2b2eSSøren Schmidt 
100760bb3943SAlan Cox 	if (interp != NULL) {
10086c775eb6SKonstantin Belousov 		have_interp = FALSE;
100922db15c0SAttilio Rao 		VOP_UNLOCK(imgp->vp, 0);
101060bb3943SAlan Cox 		if (brand_info->emul_path != NULL &&
10119b68618dSPeter Wemm 		    brand_info->emul_path[0] != '\0') {
1012a163d034SWarner Losh 			path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
101360bb3943SAlan Cox 			snprintf(path, MAXPATHLEN, "%s%s",
101460bb3943SAlan Cox 			    brand_info->emul_path, interp);
10159b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, path, &addr,
10169b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
1017911c2be0SMark Peek 			free(path, M_TEMP);
10189b68618dSPeter Wemm 			if (error == 0)
10194113f8d7SPeter Wemm 				have_interp = TRUE;
10209b68618dSPeter Wemm 		}
102118995077SKonstantin Belousov 		if (!have_interp && newinterp != NULL &&
102218995077SKonstantin Belousov 		    (brand_info->interp_path == NULL ||
102318995077SKonstantin Belousov 		    strcmp(interp, brand_info->interp_path) == 0)) {
10244113f8d7SPeter Wemm 			error = __elfN(load_file)(imgp->proc, newinterp, &addr,
10254113f8d7SPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
1026387ad998SKonstantin Belousov 			if (error == 0)
10274113f8d7SPeter Wemm 				have_interp = TRUE;
10284113f8d7SPeter Wemm 		}
10294113f8d7SPeter Wemm 		if (!have_interp) {
10309b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, interp, &addr,
10319b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
103260bb3943SAlan Cox 		}
1033cb05b60aSAttilio Rao 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
10349b68618dSPeter Wemm 		if (error != 0) {
1035d943fa35SKonstantin Belousov 			uprintf("ELF interpreter %s not found, error %d\n",
1036d943fa35SKonstantin Belousov 			    interp, error);
10376c775eb6SKonstantin Belousov 			goto ret;
1038e1743d02SSøren Schmidt 		}
103995c807cfSRobert Watson 	} else
10407564c4adSKonstantin Belousov 		addr = et_dyn_addr;
1041ea5a2b2eSSøren Schmidt 
1042e1743d02SSøren Schmidt 	/*
1043e1743d02SSøren Schmidt 	 * Construct auxargs table (used by the fixup routine)
1044e1743d02SSøren Schmidt 	 */
1045a163d034SWarner Losh 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
1046e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
1047e1743d02SSøren Schmidt 	elf_auxargs->phdr = proghdr;
1048e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
1049e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
1050e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
1051e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
1052e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
1053e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
1054d36eec69SWarner Losh 	elf_auxargs->hdr_eflags = hdr->e_flags;
1055e1743d02SSøren Schmidt 
1056e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
1057e1743d02SSøren Schmidt 	imgp->interpreted = 0;
1058a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
105932c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
1060f231de47SKonstantin Belousov 
10616c775eb6SKonstantin Belousov  ret:
10626c775eb6SKonstantin Belousov 	free(interp_buf, M_TEMP);
1063a7cddfedSJake Burkholder 	return (error);
1064e1743d02SSøren Schmidt }
1065e1743d02SSøren Schmidt 
1066a360a43dSJake Burkholder #define	suword __CONCAT(suword, __ELF_WORD_SIZE)
10673ebc1248SPeter Wemm 
10683ebc1248SPeter Wemm int
10693ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
1070e1743d02SSøren Schmidt {
1071ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
1072a360a43dSJake Burkholder 	Elf_Addr *base;
1073a360a43dSJake Burkholder 	Elf_Addr *pos;
1074e1743d02SSøren Schmidt 
1075a360a43dSJake Burkholder 	base = (Elf_Addr *)*stack_base;
1076610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
1077e1743d02SSøren Schmidt 
107835c2a5a8SWarner Losh 	if (args->execfd != -1)
1079e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1080e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1081e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1082e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1083e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1084e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1085e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1086e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
1087ee960398SWarner Losh #ifdef AT_EHDRFLAGS
1088d36eec69SWarner Losh 	AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
1089d36eec69SWarner Losh #endif
10903ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
10913ff06357SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1092b96bd95bSIan Lepore 	AUXARGS_ENTRY(pos, AT_OSRELDATE,
1093b96bd95bSIan Lepore 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
1094ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1095ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1096ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1097ee235befSKonstantin Belousov 	}
1098ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1099ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1100ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1101ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1102ee235befSKonstantin Belousov 	}
1103aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1104aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1105aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1106aea81038SKonstantin Belousov 	}
110726d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
110826d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
110926d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1110e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1111e1743d02SSøren Schmidt 
1112e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1113e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1114e1743d02SSøren Schmidt 
11153ebc1248SPeter Wemm 	base--;
1116610ecfe0SMaxim Sobolev 	suword(base, (long)imgp->args->argc);
11173ebc1248SPeter Wemm 	*stack_base = (register_t *)base;
1118a7cddfedSJake Burkholder 	return (0);
1119e1743d02SSøren Schmidt }
1120e1743d02SSøren Schmidt 
1121e1743d02SSøren Schmidt /*
11228c64af4fSJohn Polstra  * Code for generating ELF core dumps.
11238c64af4fSJohn Polstra  */
11248c64af4fSJohn Polstra 
11254d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
11260ff27d31SJohn Polstra 
11270ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
11280ff27d31SJohn Polstra struct phdr_closure {
11290ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
11300ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
11310ff27d31SJohn Polstra };
11320ff27d31SJohn Polstra 
11330ff27d31SJohn Polstra /* Closure for cb_size_segment(). */
11340ff27d31SJohn Polstra struct sseg_closure {
11350ff27d31SJohn Polstra 	int count;		/* Count of writable segments. */
11360ff27d31SJohn Polstra 	size_t size;		/* Total size of all writable segments. */
11370ff27d31SJohn Polstra };
11380ff27d31SJohn Polstra 
1139bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1140bd390213SMikolaj Golub 
1141bd390213SMikolaj Golub struct note_info {
1142bd390213SMikolaj Golub 	int		type;		/* Note type. */
1143bd390213SMikolaj Golub 	outfunc_t 	outfunc; 	/* Output function. */
1144bd390213SMikolaj Golub 	void		*outarg;	/* Argument for the output function. */
1145bd390213SMikolaj Golub 	size_t		outsize;	/* Output size. */
1146bd390213SMikolaj Golub 	TAILQ_ENTRY(note_info) link;	/* Link to the next note info. */
1147bd390213SMikolaj Golub };
1148bd390213SMikolaj Golub 
1149bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info);
1150bd390213SMikolaj Golub 
1151aa14e9b7SMark Johnston /* Coredump output parameters. */
1152aa14e9b7SMark Johnston struct coredump_params {
1153aa14e9b7SMark Johnston 	off_t		offset;
1154aa14e9b7SMark Johnston 	struct ucred	*active_cred;
1155aa14e9b7SMark Johnston 	struct ucred	*file_cred;
1156aa14e9b7SMark Johnston 	struct thread	*td;
1157aa14e9b7SMark Johnston 	struct vnode	*vp;
1158aa14e9b7SMark Johnston 	struct gzio_stream *gzs;
1159aa14e9b7SMark Johnston };
1160aa14e9b7SMark Johnston 
11614d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
11624d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
1163aa14e9b7SMark Johnston static int core_write(struct coredump_params *, void *, size_t, off_t,
1164aa14e9b7SMark Johnston     enum uio_seg);
11651005d8afSConrad Meyer static void each_dumpable_segment(struct thread *, segment_callback, void *);
1166aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1167aa14e9b7SMark Johnston     struct note_info_list *, size_t);
1168bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1169bd390213SMikolaj Golub     size_t *);
1170bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1171bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *);
1172bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1173bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int);
1174f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len);
1175bd390213SMikolaj Golub 
1176bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1177bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1178bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1179bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1180bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1181f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1182f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1183f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1184f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *);
1185f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *);
1186f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1187f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1188f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *);
1189f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
11908c64af4fSJohn Polstra 
1191aa14e9b7SMark Johnston #ifdef GZIO
1192e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel;
1193e7228204SAlfred Perlstein 
1194aa14e9b7SMark Johnston /*
1195aa14e9b7SMark Johnston  * Write out a core segment to the compression stream.
1196aa14e9b7SMark Johnston  */
1197e7228204SAlfred Perlstein static int
1198aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
1199aa14e9b7SMark Johnston {
1200aa14e9b7SMark Johnston 	u_int chunk_len;
1201e7228204SAlfred Perlstein 	int error;
1202aa14e9b7SMark Johnston 
1203aa14e9b7SMark Johnston 	while (len > 0) {
1204aa14e9b7SMark Johnston 		chunk_len = MIN(len, CORE_BUF_SIZE);
1205aa14e9b7SMark Johnston 		copyin(base, buf, chunk_len);
1206aa14e9b7SMark Johnston 		error = gzio_write(p->gzs, buf, chunk_len);
1207aa14e9b7SMark Johnston 		if (error != 0)
1208aa14e9b7SMark Johnston 			break;
1209aa14e9b7SMark Johnston 		base += chunk_len;
1210aa14e9b7SMark Johnston 		len -= chunk_len;
1211e7228204SAlfred Perlstein 	}
1212e7228204SAlfred Perlstein 	return (error);
1213e7228204SAlfred Perlstein }
1214e7228204SAlfred Perlstein 
1215aa14e9b7SMark Johnston static int
1216aa14e9b7SMark Johnston core_gz_write(void *base, size_t len, off_t offset, void *arg)
1217aa14e9b7SMark Johnston {
1218aa14e9b7SMark Johnston 
1219aa14e9b7SMark Johnston 	return (core_write((struct coredump_params *)arg, base, len, offset,
1220aa14e9b7SMark Johnston 	    UIO_SYSSPACE));
1221aa14e9b7SMark Johnston }
1222aa14e9b7SMark Johnston #endif /* GZIO */
1223aa14e9b7SMark Johnston 
1224aa14e9b7SMark Johnston static int
1225aa14e9b7SMark Johnston core_write(struct coredump_params *p, void *base, size_t len, off_t offset,
1226aa14e9b7SMark Johnston     enum uio_seg seg)
1227aa14e9b7SMark Johnston {
1228aa14e9b7SMark Johnston 
1229aa14e9b7SMark Johnston 	return (vn_rdwr_inchunks(UIO_WRITE, p->vp, base, len, offset,
1230aa14e9b7SMark Johnston 	    seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
1231aa14e9b7SMark Johnston 	    p->active_cred, p->file_cred, NULL, p->td));
1232aa14e9b7SMark Johnston }
1233aa14e9b7SMark Johnston 
1234aa14e9b7SMark Johnston static int
1235aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p,
1236aa14e9b7SMark Johnston     void *tmpbuf)
1237aa14e9b7SMark Johnston {
1238aa14e9b7SMark Johnston 
1239aa14e9b7SMark Johnston #ifdef GZIO
1240aa14e9b7SMark Johnston 	if (p->gzs != NULL)
1241aa14e9b7SMark Johnston 		return (compress_chunk(p, base, tmpbuf, len));
1242bd390213SMikolaj Golub #endif
1243aa14e9b7SMark Johnston 	return (core_write(p, base, len, offset, UIO_USERSPACE));
1244aa14e9b7SMark Johnston }
1245bd390213SMikolaj Golub 
1246bd390213SMikolaj Golub /*
1247bd390213SMikolaj Golub  * Drain into a core file.
1248bd390213SMikolaj Golub  */
1249bd390213SMikolaj Golub static int
1250bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len)
1251bd390213SMikolaj Golub {
1252aa14e9b7SMark Johnston 	struct coredump_params *p;
1253f1fca82eSMikolaj Golub 	int error, locked;
1254bd390213SMikolaj Golub 
1255aa14e9b7SMark Johnston 	p = (struct coredump_params *)arg;
1256f1fca82eSMikolaj Golub 
1257f1fca82eSMikolaj Golub 	/*
1258f1fca82eSMikolaj Golub 	 * Some kern_proc out routines that print to this sbuf may
1259f1fca82eSMikolaj Golub 	 * call us with the process lock held. Draining with the
1260f1fca82eSMikolaj Golub 	 * non-sleepable lock held is unsafe. The lock is needed for
1261f1fca82eSMikolaj Golub 	 * those routines when dumping a live process. In our case we
1262f1fca82eSMikolaj Golub 	 * can safely release the lock before draining and acquire
1263f1fca82eSMikolaj Golub 	 * again after.
1264f1fca82eSMikolaj Golub 	 */
1265f1fca82eSMikolaj Golub 	locked = PROC_LOCKED(p->td->td_proc);
1266f1fca82eSMikolaj Golub 	if (locked)
1267f1fca82eSMikolaj Golub 		PROC_UNLOCK(p->td->td_proc);
1268aa14e9b7SMark Johnston #ifdef GZIO
1269aa14e9b7SMark Johnston 	if (p->gzs != NULL)
1270aa14e9b7SMark Johnston 		error = gzio_write(p->gzs, __DECONST(char *, data), len);
1271bd390213SMikolaj Golub 	else
1272bd390213SMikolaj Golub #endif
1273aa14e9b7SMark Johnston 		error = core_write(p, __DECONST(void *, data), len, p->offset,
1274aa14e9b7SMark Johnston 		    UIO_SYSSPACE);
1275f1fca82eSMikolaj Golub 	if (locked)
1276f1fca82eSMikolaj Golub 		PROC_LOCK(p->td->td_proc);
1277bd390213SMikolaj Golub 	if (error != 0)
1278bd390213SMikolaj Golub 		return (-error);
1279bd390213SMikolaj Golub 	p->offset += len;
1280bd390213SMikolaj Golub 	return (len);
1281bd390213SMikolaj Golub }
1282bd390213SMikolaj Golub 
1283f1fca82eSMikolaj Golub /*
1284f1fca82eSMikolaj Golub  * Drain into a counter.
1285f1fca82eSMikolaj Golub  */
1286f1fca82eSMikolaj Golub static int
1287f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len)
1288f1fca82eSMikolaj Golub {
1289f1fca82eSMikolaj Golub 	size_t *sizep;
1290f1fca82eSMikolaj Golub 
1291f1fca82eSMikolaj Golub 	sizep = (size_t *)arg;
1292f1fca82eSMikolaj Golub 	*sizep += len;
1293f1fca82eSMikolaj Golub 	return (len);
1294f1fca82eSMikolaj Golub }
1295f1fca82eSMikolaj Golub 
12968c64af4fSJohn Polstra int
1297e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1298fca666a1SJulian Elischer {
1299247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
1300fca666a1SJulian Elischer 	int error = 0;
13010ff27d31SJohn Polstra 	struct sseg_closure seginfo;
1302bd390213SMikolaj Golub 	struct note_info_list notelst;
1303aa14e9b7SMark Johnston 	struct coredump_params params;
1304bd390213SMikolaj Golub 	struct note_info *ninfo;
1305aa14e9b7SMark Johnston 	void *hdr, *tmpbuf;
1306bd390213SMikolaj Golub 	size_t hdrsize, notesz, coresize;
13079a12e282SEnji Cooper #ifdef GZIO
1308aa14e9b7SMark Johnston 	boolean_t compress;
13098c64af4fSJohn Polstra 
1310aa14e9b7SMark Johnston 	compress = (flags & IMGACT_CORE_COMPRESS) != 0;
13119a12e282SEnji Cooper #endif
1312e7228204SAlfred Perlstein 	hdr = NULL;
131302d131adSMark Johnston 	tmpbuf = NULL;
1314bd390213SMikolaj Golub 	TAILQ_INIT(&notelst);
1315e7228204SAlfred Perlstein 
13160ff27d31SJohn Polstra 	/* Size the program segments. */
13170ff27d31SJohn Polstra 	seginfo.count = 0;
13180ff27d31SJohn Polstra 	seginfo.size = 0;
13191005d8afSConrad Meyer 	each_dumpable_segment(td, cb_size_segment, &seginfo);
13200ff27d31SJohn Polstra 
13210ff27d31SJohn Polstra 	/*
1322bd390213SMikolaj Golub 	 * Collect info about the core file header area.
13230ff27d31SJohn Polstra 	 */
1324bd390213SMikolaj Golub 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1325c17b0bd2SConrad Meyer 	if (seginfo.count + 1 >= PN_XNUM)
1326c17b0bd2SConrad Meyer 		hdrsize += sizeof(Elf_Shdr);
1327bd390213SMikolaj Golub 	__elfN(prepare_notes)(td, &notelst, &notesz);
1328bd390213SMikolaj Golub 	coresize = round_page(hdrsize + notesz) + seginfo.size;
13290ff27d31SJohn Polstra 
133002d131adSMark Johnston 	/* Set up core dump parameters. */
133102d131adSMark Johnston 	params.offset = 0;
133202d131adSMark Johnston 	params.active_cred = cred;
133302d131adSMark Johnston 	params.file_cred = NOCRED;
133402d131adSMark Johnston 	params.td = td;
133502d131adSMark Johnston 	params.vp = vp;
133602d131adSMark Johnston 	params.gzs = NULL;
133702d131adSMark Johnston 
1338afcc55f3SEdward Tomasz Napierala #ifdef RACCT
13394b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
13401ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1341bd390213SMikolaj Golub 		error = racct_add(td->td_proc, RACCT_CORE, coresize);
13421ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
13431ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
13441ba5ad42SEdward Tomasz Napierala 			error = EFAULT;
13451ba5ad42SEdward Tomasz Napierala 			goto done;
13461ba5ad42SEdward Tomasz Napierala 		}
13474b5c9cf6SEdward Tomasz Napierala 	}
1348afcc55f3SEdward Tomasz Napierala #endif
1349bd390213SMikolaj Golub 	if (coresize >= limit) {
1350fba6b1afSAlfred Perlstein 		error = EFAULT;
1351fba6b1afSAlfred Perlstein 		goto done;
1352fba6b1afSAlfred Perlstein 	}
13530ff27d31SJohn Polstra 
1354aa14e9b7SMark Johnston #ifdef GZIO
1355aa14e9b7SMark Johnston 	/* Create a compression stream if necessary. */
1356aa14e9b7SMark Johnston 	if (compress) {
1357aa14e9b7SMark Johnston 		params.gzs = gzio_init(core_gz_write, GZIO_DEFLATE,
1358aa14e9b7SMark Johnston 		    CORE_BUF_SIZE, compress_user_cores_gzlevel, &params);
1359aa14e9b7SMark Johnston 		if (params.gzs == NULL) {
1360aa14e9b7SMark Johnston 			error = EFAULT;
1361aa14e9b7SMark Johnston 			goto done;
1362aa14e9b7SMark Johnston 		}
1363aa14e9b7SMark Johnston 		tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1364aa14e9b7SMark Johnston         }
1365aa14e9b7SMark Johnston #endif
1366aa14e9b7SMark Johnston 
13670ff27d31SJohn Polstra 	/*
13680ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
1369bd390213SMikolaj Golub 	 * and write it out following the notes.
13700ff27d31SJohn Polstra 	 */
1371a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1372aa14e9b7SMark Johnston 	error = __elfN(corehdr)(&params, seginfo.count, hdr, hdrsize, &notelst,
1373aa14e9b7SMark Johnston 	    notesz);
13740ff27d31SJohn Polstra 
13750ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
13760ff27d31SJohn Polstra 	if (error == 0) {
13770ff27d31SJohn Polstra 		Elf_Phdr *php;
13782b471bc6STim J. Robbins 		off_t offset;
13790ff27d31SJohn Polstra 		int i;
13800ff27d31SJohn Polstra 
13810ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1382bd390213SMikolaj Golub 		offset = round_page(hdrsize + notesz);
13830ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1384aa14e9b7SMark Johnston 			error = core_output((caddr_t)(uintptr_t)php->p_vaddr,
1385aa14e9b7SMark Johnston 			    php->p_filesz, offset, &params, tmpbuf);
13860ff27d31SJohn Polstra 			if (error != 0)
13872b471bc6STim J. Robbins 				break;
13880ff27d31SJohn Polstra 			offset += php->p_filesz;
13890ff27d31SJohn Polstra 			php++;
13900ff27d31SJohn Polstra 		}
1391aa14e9b7SMark Johnston #ifdef GZIO
1392aa14e9b7SMark Johnston 		if (error == 0 && compress)
1393aa14e9b7SMark Johnston 			error = gzio_flush(params.gzs);
1394aa14e9b7SMark Johnston #endif
13950ff27d31SJohn Polstra 	}
1396e7228204SAlfred Perlstein 	if (error) {
1397e7228204SAlfred Perlstein 		log(LOG_WARNING,
1398e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1399e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1400e7228204SAlfred Perlstein 	}
1401e7228204SAlfred Perlstein 
1402e7228204SAlfred Perlstein done:
1403aa14e9b7SMark Johnston #ifdef GZIO
1404aa14e9b7SMark Johnston 	if (compress) {
1405aa14e9b7SMark Johnston 		free(tmpbuf, M_TEMP);
140602d131adSMark Johnston 		if (params.gzs != NULL)
1407aa14e9b7SMark Johnston 			gzio_fini(params.gzs);
1408aa14e9b7SMark Johnston 	}
14098b325009SAlfred Perlstein #endif
1410bd390213SMikolaj Golub 	while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1411bd390213SMikolaj Golub 		TAILQ_REMOVE(&notelst, ninfo, link);
1412bd390213SMikolaj Golub 		free(ninfo, M_TEMP);
1413bd390213SMikolaj Golub 	}
1414bd390213SMikolaj Golub 	if (hdr != NULL)
14150ff27d31SJohn Polstra 		free(hdr, M_TEMP);
14160ff27d31SJohn Polstra 
1417a7cddfedSJake Burkholder 	return (error);
14188c64af4fSJohn Polstra }
14198c64af4fSJohn Polstra 
14200ff27d31SJohn Polstra /*
14211005d8afSConrad Meyer  * A callback for each_dumpable_segment() to write out the segment's
14220ff27d31SJohn Polstra  * program header entry.
14230ff27d31SJohn Polstra  */
14240ff27d31SJohn Polstra static void
14250ff27d31SJohn Polstra cb_put_phdr(entry, closure)
14260ff27d31SJohn Polstra 	vm_map_entry_t entry;
14270ff27d31SJohn Polstra 	void *closure;
14280ff27d31SJohn Polstra {
14290ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
14300ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
14310ff27d31SJohn Polstra 
14320ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
14330ff27d31SJohn Polstra 
14340ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
14350ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
14360ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
14370ff27d31SJohn Polstra 	phdr->p_paddr = 0;
14380ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
14390ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1440ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
14410ff27d31SJohn Polstra 
14420ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
14430ff27d31SJohn Polstra 	phc->phdr++;
14440ff27d31SJohn Polstra }
14450ff27d31SJohn Polstra 
14460ff27d31SJohn Polstra /*
14471005d8afSConrad Meyer  * A callback for each_dumpable_segment() to gather information about
14480ff27d31SJohn Polstra  * the number of segments and their total size.
14490ff27d31SJohn Polstra  */
14500ff27d31SJohn Polstra static void
1451f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure)
14520ff27d31SJohn Polstra {
14530ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
14540ff27d31SJohn Polstra 
14550ff27d31SJohn Polstra 	ssc->count++;
14560ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
14570ff27d31SJohn Polstra }
14580ff27d31SJohn Polstra 
14590ff27d31SJohn Polstra /*
14600ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
14610ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
14620ff27d31SJohn Polstra  * caller-supplied data.
14630ff27d31SJohn Polstra  */
14640ff27d31SJohn Polstra static void
14651005d8afSConrad Meyer each_dumpable_segment(struct thread *td, segment_callback func, void *closure)
14660ff27d31SJohn Polstra {
1467247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
14680ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
14690ff27d31SJohn Polstra 	vm_map_entry_t entry;
1470976a87a2SAlan Cox 	vm_object_t backing_object, object;
1471976a87a2SAlan Cox 	boolean_t ignore_entry;
14720ff27d31SJohn Polstra 
1473976a87a2SAlan Cox 	vm_map_lock_read(map);
14740ff27d31SJohn Polstra 	for (entry = map->header.next; entry != &map->header;
14750ff27d31SJohn Polstra 	    entry = entry->next) {
1476fa7dd9c5SMatthew Dillon 		/*
1477fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1478fa7dd9c5SMatthew Dillon 		 * coredump mode.
1479fa7dd9c5SMatthew Dillon 		 *
1480fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1481fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1482fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1483fa7dd9c5SMatthew Dillon 		 */
1484fa7dd9c5SMatthew Dillon 		if (elf_legacy_coredump) {
1485fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
14860ff27d31SJohn Polstra 				continue;
1487fa7dd9c5SMatthew Dillon 		} else {
1488fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_ALL) == 0)
1489fa7dd9c5SMatthew Dillon 				continue;
1490fa7dd9c5SMatthew Dillon 		}
14910ff27d31SJohn Polstra 
14929730a5daSPaul Saab 		/*
1493fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1494fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1495fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1496fa7dd9c5SMatthew Dillon 		 * kernel map).
14979730a5daSPaul Saab 		 */
1498fa7dd9c5SMatthew Dillon 		if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
14999730a5daSPaul Saab 			continue;
15009730a5daSPaul Saab 
1501976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
15020ff27d31SJohn Polstra 			continue;
15030ff27d31SJohn Polstra 
15040ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1505bc403f03SAttilio Rao 		VM_OBJECT_RLOCK(object);
1506976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1507bc403f03SAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1508bc403f03SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1509976a87a2SAlan Cox 			object = backing_object;
1510976a87a2SAlan Cox 		}
1511976a87a2SAlan Cox 		ignore_entry = object->type != OBJT_DEFAULT &&
1512bc411bc2SJohn Baldwin 		    object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
1513bc411bc2SJohn Baldwin 		    object->type != OBJT_PHYS;
1514bc403f03SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1515976a87a2SAlan Cox 		if (ignore_entry)
15160ff27d31SJohn Polstra 			continue;
15170ff27d31SJohn Polstra 
15180ff27d31SJohn Polstra 		(*func)(entry, closure);
15190ff27d31SJohn Polstra 	}
1520976a87a2SAlan Cox 	vm_map_unlock_read(map);
15210ff27d31SJohn Polstra }
15220ff27d31SJohn Polstra 
15230ff27d31SJohn Polstra /*
15240ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
15250ff27d31SJohn Polstra  * the page boundary.
15260ff27d31SJohn Polstra  */
15278c64af4fSJohn Polstra static int
1528aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1529aa14e9b7SMark Johnston     size_t hdrsize, struct note_info_list *notelst, size_t notesz)
15308c64af4fSJohn Polstra {
1531bd390213SMikolaj Golub 	struct note_info *ninfo;
1532bd390213SMikolaj Golub 	struct sbuf *sb;
1533bd390213SMikolaj Golub 	int error;
15348c64af4fSJohn Polstra 
15358c64af4fSJohn Polstra 	/* Fill in the header. */
15360ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
1537aa14e9b7SMark Johnston 	__elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz);
15388c64af4fSJohn Polstra 
1539bd390213SMikolaj Golub 	sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1540aa14e9b7SMark Johnston 	sbuf_set_drain(sb, sbuf_drain_core_output, p);
1541bd390213SMikolaj Golub 	sbuf_start_section(sb, NULL);
1542bd390213SMikolaj Golub 	sbuf_bcat(sb, hdr, hdrsize);
1543bd390213SMikolaj Golub 	TAILQ_FOREACH(ninfo, notelst, link)
1544bd390213SMikolaj Golub 	    __elfN(putnote)(ninfo, sb);
1545bd390213SMikolaj Golub 	/* Align up to a page boundary for the program segments. */
1546bd390213SMikolaj Golub 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1547bd390213SMikolaj Golub 	error = sbuf_finish(sb);
1548bd390213SMikolaj Golub 	sbuf_delete(sb);
1549bd390213SMikolaj Golub 
1550bd390213SMikolaj Golub 	return (error);
1551e7228204SAlfred Perlstein }
1552bd390213SMikolaj Golub 
1553bd390213SMikolaj Golub static void
1554bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1555bd390213SMikolaj Golub     size_t *sizep)
1556bd390213SMikolaj Golub {
1557bd390213SMikolaj Golub 	struct proc *p;
1558bd390213SMikolaj Golub 	struct thread *thr;
1559bd390213SMikolaj Golub 	size_t size;
1560bd390213SMikolaj Golub 
1561bd390213SMikolaj Golub 	p = td->td_proc;
1562bd390213SMikolaj Golub 	size = 0;
1563bd390213SMikolaj Golub 
1564bd390213SMikolaj Golub 	size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1565bd390213SMikolaj Golub 
1566bd390213SMikolaj Golub 	/*
1567bd390213SMikolaj Golub 	 * To have the debugger select the right thread (LWP) as the initial
1568bd390213SMikolaj Golub 	 * thread, we dump the state of the thread passed to us in td first.
1569bd390213SMikolaj Golub 	 * This is the thread that causes the core dump and thus likely to
1570bd390213SMikolaj Golub 	 * be the right thread one wants to have selected in the debugger.
1571bd390213SMikolaj Golub 	 */
1572bd390213SMikolaj Golub 	thr = td;
1573bd390213SMikolaj Golub 	while (thr != NULL) {
1574bd390213SMikolaj Golub 		size += register_note(list, NT_PRSTATUS,
1575bd390213SMikolaj Golub 		    __elfN(note_prstatus), thr);
1576bd390213SMikolaj Golub 		size += register_note(list, NT_FPREGSET,
1577bd390213SMikolaj Golub 		    __elfN(note_fpregset), thr);
1578bd390213SMikolaj Golub 		size += register_note(list, NT_THRMISC,
1579bd390213SMikolaj Golub 		    __elfN(note_thrmisc), thr);
1580bd390213SMikolaj Golub 		size += register_note(list, -1,
1581bd390213SMikolaj Golub 		    __elfN(note_threadmd), thr);
1582bd390213SMikolaj Golub 
1583bd390213SMikolaj Golub 		thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1584bd390213SMikolaj Golub 		    TAILQ_NEXT(thr, td_plist);
1585bd390213SMikolaj Golub 		if (thr == td)
1586bd390213SMikolaj Golub 			thr = TAILQ_NEXT(thr, td_plist);
1587dada0278SJohn Polstra 	}
1588dada0278SJohn Polstra 
1589f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PROC,
1590f1fca82eSMikolaj Golub 	    __elfN(note_procstat_proc), p);
1591f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_FILES,
1592f1fca82eSMikolaj Golub 	    note_procstat_files, p);
1593f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_VMMAP,
1594f1fca82eSMikolaj Golub 	    note_procstat_vmmap, p);
1595f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_GROUPS,
1596f1fca82eSMikolaj Golub 	    note_procstat_groups, p);
1597f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_UMASK,
1598f1fca82eSMikolaj Golub 	    note_procstat_umask, p);
1599f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_RLIMIT,
1600f1fca82eSMikolaj Golub 	    note_procstat_rlimit, p);
1601f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_OSREL,
1602f1fca82eSMikolaj Golub 	    note_procstat_osrel, p);
1603f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1604f1fca82eSMikolaj Golub 	    __elfN(note_procstat_psstrings), p);
1605f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_AUXV,
1606f1fca82eSMikolaj Golub 	    __elfN(note_procstat_auxv), p);
1607f1fca82eSMikolaj Golub 
1608bd390213SMikolaj Golub 	*sizep = size;
1609bd390213SMikolaj Golub }
1610bd390213SMikolaj Golub 
1611bd390213SMikolaj Golub static void
1612bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1613bd390213SMikolaj Golub     size_t notesz)
1614bd390213SMikolaj Golub {
1615bd390213SMikolaj Golub 	Elf_Ehdr *ehdr;
1616bd390213SMikolaj Golub 	Elf_Phdr *phdr;
1617c17b0bd2SConrad Meyer 	Elf_Shdr *shdr;
1618bd390213SMikolaj Golub 	struct phdr_closure phc;
1619bd390213SMikolaj Golub 
1620bd390213SMikolaj Golub 	ehdr = (Elf_Ehdr *)hdr;
1621bd390213SMikolaj Golub 
1622bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1623bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1624bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1625bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1626bd390213SMikolaj Golub 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1627bd390213SMikolaj Golub 	ehdr->e_ident[EI_DATA] = ELF_DATA;
1628bd390213SMikolaj Golub 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1629bd390213SMikolaj Golub 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1630bd390213SMikolaj Golub 	ehdr->e_ident[EI_ABIVERSION] = 0;
1631bd390213SMikolaj Golub 	ehdr->e_ident[EI_PAD] = 0;
1632bd390213SMikolaj Golub 	ehdr->e_type = ET_CORE;
1633bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1634bd390213SMikolaj Golub 	ehdr->e_machine = ELF_ARCH32;
1635bd390213SMikolaj Golub #else
1636bd390213SMikolaj Golub 	ehdr->e_machine = ELF_ARCH;
1637bd390213SMikolaj Golub #endif
1638bd390213SMikolaj Golub 	ehdr->e_version = EV_CURRENT;
1639bd390213SMikolaj Golub 	ehdr->e_entry = 0;
1640bd390213SMikolaj Golub 	ehdr->e_phoff = sizeof(Elf_Ehdr);
1641bd390213SMikolaj Golub 	ehdr->e_flags = 0;
1642bd390213SMikolaj Golub 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
1643bd390213SMikolaj Golub 	ehdr->e_phentsize = sizeof(Elf_Phdr);
1644bd390213SMikolaj Golub 	ehdr->e_shentsize = sizeof(Elf_Shdr);
1645bd390213SMikolaj Golub 	ehdr->e_shstrndx = SHN_UNDEF;
1646c17b0bd2SConrad Meyer 	if (numsegs + 1 < PN_XNUM) {
1647c17b0bd2SConrad Meyer 		ehdr->e_phnum = numsegs + 1;
1648c17b0bd2SConrad Meyer 		ehdr->e_shnum = 0;
1649c17b0bd2SConrad Meyer 	} else {
1650c17b0bd2SConrad Meyer 		ehdr->e_phnum = PN_XNUM;
1651c17b0bd2SConrad Meyer 		ehdr->e_shnum = 1;
1652c17b0bd2SConrad Meyer 
1653c17b0bd2SConrad Meyer 		ehdr->e_shoff = ehdr->e_phoff +
1654c17b0bd2SConrad Meyer 		    (numsegs + 1) * ehdr->e_phentsize;
1655c17b0bd2SConrad Meyer 		KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr),
1656c17b0bd2SConrad Meyer 		    ("e_shoff: %zu, hdrsize - shdr: %zu",
165707f825e8SConrad Meyer 		     (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr)));
1658c17b0bd2SConrad Meyer 
1659c17b0bd2SConrad Meyer 		shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
1660c17b0bd2SConrad Meyer 		memset(shdr, 0, sizeof(*shdr));
1661c17b0bd2SConrad Meyer 		/*
1662c17b0bd2SConrad Meyer 		 * A special first section is used to hold large segment and
1663c17b0bd2SConrad Meyer 		 * section counts.  This was proposed by Sun Microsystems in
1664c17b0bd2SConrad Meyer 		 * Solaris and has been adopted by Linux; the standard ELF
1665c17b0bd2SConrad Meyer 		 * tools are already familiar with the technique.
1666c17b0bd2SConrad Meyer 		 *
1667c17b0bd2SConrad Meyer 		 * See table 7-7 of the Solaris "Linker and Libraries Guide"
1668c17b0bd2SConrad Meyer 		 * (or 12-7 depending on the version of the document) for more
1669c17b0bd2SConrad Meyer 		 * details.
1670c17b0bd2SConrad Meyer 		 */
1671c17b0bd2SConrad Meyer 		shdr->sh_type = SHT_NULL;
1672c17b0bd2SConrad Meyer 		shdr->sh_size = ehdr->e_shnum;
1673c17b0bd2SConrad Meyer 		shdr->sh_link = ehdr->e_shstrndx;
1674c17b0bd2SConrad Meyer 		shdr->sh_info = numsegs + 1;
1675c17b0bd2SConrad Meyer 	}
1676bd390213SMikolaj Golub 
1677bd390213SMikolaj Golub 	/*
1678bd390213SMikolaj Golub 	 * Fill in the program header entries.
1679bd390213SMikolaj Golub 	 */
1680c17b0bd2SConrad Meyer 	phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
1681bd390213SMikolaj Golub 
1682bd390213SMikolaj Golub 	/* The note segement. */
1683bd390213SMikolaj Golub 	phdr->p_type = PT_NOTE;
1684bd390213SMikolaj Golub 	phdr->p_offset = hdrsize;
1685bd390213SMikolaj Golub 	phdr->p_vaddr = 0;
1686bd390213SMikolaj Golub 	phdr->p_paddr = 0;
1687bd390213SMikolaj Golub 	phdr->p_filesz = notesz;
1688bd390213SMikolaj Golub 	phdr->p_memsz = 0;
1689bd390213SMikolaj Golub 	phdr->p_flags = PF_R;
16901b8388cdSMikolaj Golub 	phdr->p_align = ELF_NOTE_ROUNDSIZE;
1691bd390213SMikolaj Golub 	phdr++;
1692bd390213SMikolaj Golub 
1693bd390213SMikolaj Golub 	/* All the writable segments from the program. */
1694bd390213SMikolaj Golub 	phc.phdr = phdr;
1695bd390213SMikolaj Golub 	phc.offset = round_page(hdrsize + notesz);
16961005d8afSConrad Meyer 	each_dumpable_segment(td, cb_put_phdr, &phc);
1697bd390213SMikolaj Golub }
1698bd390213SMikolaj Golub 
1699bd390213SMikolaj Golub static size_t
1700bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1701bd390213SMikolaj Golub {
1702bd390213SMikolaj Golub 	struct note_info *ninfo;
1703bd390213SMikolaj Golub 	size_t size, notesize;
1704bd390213SMikolaj Golub 
1705bd390213SMikolaj Golub 	size = 0;
1706bd390213SMikolaj Golub 	out(arg, NULL, &size);
1707bd390213SMikolaj Golub 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1708bd390213SMikolaj Golub 	ninfo->type = type;
1709bd390213SMikolaj Golub 	ninfo->outfunc = out;
1710bd390213SMikolaj Golub 	ninfo->outarg = arg;
1711bd390213SMikolaj Golub 	ninfo->outsize = size;
1712bd390213SMikolaj Golub 	TAILQ_INSERT_TAIL(list, ninfo, link);
1713bd390213SMikolaj Golub 
1714bd390213SMikolaj Golub 	if (type == -1)
1715bd390213SMikolaj Golub 		return (size);
1716bd390213SMikolaj Golub 
1717bd390213SMikolaj Golub 	notesize = sizeof(Elf_Note) +		/* note header */
1718180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1719180e57e5SJohn Baldwin 						/* note name */
1720180e57e5SJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1721180e57e5SJohn Baldwin 
1722180e57e5SJohn Baldwin 	return (notesize);
1723180e57e5SJohn Baldwin }
1724180e57e5SJohn Baldwin 
1725180e57e5SJohn Baldwin static size_t
1726180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len)
1727180e57e5SJohn Baldwin {
1728180e57e5SJohn Baldwin 	size_t padded_len;
1729180e57e5SJohn Baldwin 
1730180e57e5SJohn Baldwin 	padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
1731180e57e5SJohn Baldwin 	if (dst != NULL) {
1732180e57e5SJohn Baldwin 		bcopy(src, dst, len);
1733180e57e5SJohn Baldwin 		bzero((char *)dst + len, padded_len - len);
1734180e57e5SJohn Baldwin 	}
1735180e57e5SJohn Baldwin 	return (padded_len);
1736180e57e5SJohn Baldwin }
1737180e57e5SJohn Baldwin 
1738180e57e5SJohn Baldwin size_t
1739180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
1740180e57e5SJohn Baldwin {
1741180e57e5SJohn Baldwin 	Elf_Note *note;
1742180e57e5SJohn Baldwin 	char *buf;
1743180e57e5SJohn Baldwin 	size_t notesize;
1744180e57e5SJohn Baldwin 
1745180e57e5SJohn Baldwin 	buf = dst;
1746180e57e5SJohn Baldwin 	if (buf != NULL) {
1747180e57e5SJohn Baldwin 		note = (Elf_Note *)buf;
1748180e57e5SJohn Baldwin 		note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1749180e57e5SJohn Baldwin 		note->n_descsz = size;
1750180e57e5SJohn Baldwin 		note->n_type = type;
1751180e57e5SJohn Baldwin 		buf += sizeof(*note);
1752180e57e5SJohn Baldwin 		buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
1753180e57e5SJohn Baldwin 		    sizeof(FREEBSD_ABI_VENDOR));
1754180e57e5SJohn Baldwin 		append_note_data(src, buf, size);
1755180e57e5SJohn Baldwin 		if (descp != NULL)
1756180e57e5SJohn Baldwin 			*descp = buf;
1757180e57e5SJohn Baldwin 	}
1758180e57e5SJohn Baldwin 
1759180e57e5SJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
1760180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1761180e57e5SJohn Baldwin 						/* note name */
17621b8388cdSMikolaj Golub 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1763bd390213SMikolaj Golub 
1764bd390213SMikolaj Golub 	return (notesize);
1765bd390213SMikolaj Golub }
1766bd390213SMikolaj Golub 
1767bd390213SMikolaj Golub static void
1768bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1769bd390213SMikolaj Golub {
1770bd390213SMikolaj Golub 	Elf_Note note;
177114bdbaf2SConrad Meyer 	ssize_t old_len, sect_len;
177214bdbaf2SConrad Meyer 	size_t new_len, descsz, i;
1773bd390213SMikolaj Golub 
1774bd390213SMikolaj Golub 	if (ninfo->type == -1) {
1775bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1776bd390213SMikolaj Golub 		return;
1777bd390213SMikolaj Golub 	}
1778bd390213SMikolaj Golub 
1779180e57e5SJohn Baldwin 	note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1780bd390213SMikolaj Golub 	note.n_descsz = ninfo->outsize;
1781bd390213SMikolaj Golub 	note.n_type = ninfo->type;
1782bd390213SMikolaj Golub 
1783bd390213SMikolaj Golub 	sbuf_bcat(sb, &note, sizeof(note));
1784bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1785180e57e5SJohn Baldwin 	sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
17861b8388cdSMikolaj Golub 	sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1787bd390213SMikolaj Golub 	if (note.n_descsz == 0)
1788bd390213SMikolaj Golub 		return;
1789bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1790bd390213SMikolaj Golub 	ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
179114bdbaf2SConrad Meyer 	sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
179214bdbaf2SConrad Meyer 	if (sect_len < 0)
179314bdbaf2SConrad Meyer 		return;
179414bdbaf2SConrad Meyer 
179514bdbaf2SConrad Meyer 	new_len = (size_t)sect_len;
179614bdbaf2SConrad Meyer 	descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
179714bdbaf2SConrad Meyer 	if (new_len < descsz) {
179814bdbaf2SConrad Meyer 		/*
179914bdbaf2SConrad Meyer 		 * It is expected that individual note emitters will correctly
180014bdbaf2SConrad Meyer 		 * predict their expected output size and fill up to that size
180114bdbaf2SConrad Meyer 		 * themselves, padding in a format-specific way if needed.
180214bdbaf2SConrad Meyer 		 * However, in case they don't, just do it here with zeros.
180314bdbaf2SConrad Meyer 		 */
180414bdbaf2SConrad Meyer 		for (i = 0; i < descsz - new_len; i++)
180514bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
180614bdbaf2SConrad Meyer 	} else if (new_len > descsz) {
180714bdbaf2SConrad Meyer 		/*
180814bdbaf2SConrad Meyer 		 * We can't always truncate sb -- we may have drained some
180914bdbaf2SConrad Meyer 		 * of it already.
181014bdbaf2SConrad Meyer 		 */
181114bdbaf2SConrad Meyer 		KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
181214bdbaf2SConrad Meyer 		    "read it (%zu > %zu).  Since it is longer than "
181314bdbaf2SConrad Meyer 		    "expected, this coredump's notes are corrupt.  THIS "
181414bdbaf2SConrad Meyer 		    "IS A BUG in the note_procstat routine for type %u.\n",
181514bdbaf2SConrad Meyer 		    __func__, (unsigned)note.n_type, new_len, descsz,
181614bdbaf2SConrad Meyer 		    (unsigned)note.n_type));
181714bdbaf2SConrad Meyer 	}
1818bd390213SMikolaj Golub }
1819bd390213SMikolaj Golub 
1820bd390213SMikolaj Golub /*
1821bd390213SMikolaj Golub  * Miscellaneous note out functions.
1822bd390213SMikolaj Golub  */
1823bd390213SMikolaj Golub 
1824841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1825841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
1826841c0c7eSNathan Whitehorn 
182762919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
182862919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
182962919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
183062919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
183162919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
18327f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
1833f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	KERN_PROC_MASK32
1834f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t;
1835f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t;
183662919d78SPeter Wemm #else
183762919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
183862919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
183962919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
184062919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
184162919d78SPeter Wemm typedef gregset_t elf_gregset_t;
18427f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
1843f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	0
1844f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t;
1845f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t;
184662919d78SPeter Wemm #endif
184762919d78SPeter Wemm 
18488c64af4fSJohn Polstra static void
1849bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
18508c64af4fSJohn Polstra {
1851c77547d2SJohn Baldwin 	struct sbuf sbarg;
1852c77547d2SJohn Baldwin 	size_t len;
1853c77547d2SJohn Baldwin 	char *cp, *end;
1854247aba24SMarcel Moolenaar 	struct proc *p;
1855bd390213SMikolaj Golub 	elf_prpsinfo_t *psinfo;
1856c77547d2SJohn Baldwin 	int error;
18578c64af4fSJohn Polstra 
1858bd390213SMikolaj Golub 	p = (struct proc *)arg;
1859bd390213SMikolaj Golub 	if (sb != NULL) {
1860bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1861bd390213SMikolaj Golub 		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
18628c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
186362919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1864ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1865c77547d2SJohn Baldwin 		PROC_LOCK(p);
1866c77547d2SJohn Baldwin 		if (p->p_args != NULL) {
1867c77547d2SJohn Baldwin 			len = sizeof(psinfo->pr_psargs) - 1;
1868c77547d2SJohn Baldwin 			if (len > p->p_args->ar_length)
1869c77547d2SJohn Baldwin 				len = p->p_args->ar_length;
1870c77547d2SJohn Baldwin 			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
1871c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
1872c77547d2SJohn Baldwin 			error = 0;
1873c77547d2SJohn Baldwin 		} else {
1874c77547d2SJohn Baldwin 			_PHOLD(p);
1875c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
1876c77547d2SJohn Baldwin 			sbuf_new(&sbarg, psinfo->pr_psargs,
1877c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
1878c77547d2SJohn Baldwin 			error = proc_getargv(curthread, p, &sbarg);
1879c77547d2SJohn Baldwin 			PRELE(p);
1880c77547d2SJohn Baldwin 			if (sbuf_finish(&sbarg) == 0)
1881c77547d2SJohn Baldwin 				len = sbuf_len(&sbarg) - 1;
1882c77547d2SJohn Baldwin 			else
1883c77547d2SJohn Baldwin 				len = sizeof(psinfo->pr_psargs) - 1;
1884c77547d2SJohn Baldwin 			sbuf_delete(&sbarg);
1885c77547d2SJohn Baldwin 		}
1886c77547d2SJohn Baldwin 		if (error || len == 0)
1887ccd3953eSJohn Baldwin 			strlcpy(psinfo->pr_psargs, p->p_comm,
18888c9b7b2cSMarcel Moolenaar 			    sizeof(psinfo->pr_psargs));
1889c77547d2SJohn Baldwin 		else {
1890c77547d2SJohn Baldwin 			KASSERT(len < sizeof(psinfo->pr_psargs),
1891c77547d2SJohn Baldwin 			    ("len is too long: %zu vs %zu", len,
1892c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs)));
1893c77547d2SJohn Baldwin 			cp = psinfo->pr_psargs;
1894c77547d2SJohn Baldwin 			end = cp + len - 1;
1895c77547d2SJohn Baldwin 			for (;;) {
1896c77547d2SJohn Baldwin 				cp = memchr(cp, '\0', end - cp);
1897c77547d2SJohn Baldwin 				if (cp == NULL)
1898c77547d2SJohn Baldwin 					break;
1899c77547d2SJohn Baldwin 				*cp = ' ';
1900c77547d2SJohn Baldwin 			}
1901c77547d2SJohn Baldwin 		}
1902ccb83afdSJohn Baldwin 		psinfo->pr_pid = p->p_pid;
1903bd390213SMikolaj Golub 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1904bd390213SMikolaj Golub 		free(psinfo, M_TEMP);
1905bd390213SMikolaj Golub 	}
1906bd390213SMikolaj Golub 	*sizep = sizeof(*psinfo);
1907bd390213SMikolaj Golub }
1908bd390213SMikolaj Golub 
1909bd390213SMikolaj Golub static void
1910bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1911bd390213SMikolaj Golub {
1912bd390213SMikolaj Golub 	struct thread *td;
1913bd390213SMikolaj Golub 	elf_prstatus_t *status;
1914bd390213SMikolaj Golub 
1915bd390213SMikolaj Golub 	td = (struct thread *)arg;
1916bd390213SMikolaj Golub 	if (sb != NULL) {
1917bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*status), ("invalid size"));
1918bd390213SMikolaj Golub 		status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
19198c9b7b2cSMarcel Moolenaar 		status->pr_version = PRSTATUS_VERSION;
192062919d78SPeter Wemm 		status->pr_statussz = sizeof(elf_prstatus_t);
192162919d78SPeter Wemm 		status->pr_gregsetsz = sizeof(elf_gregset_t);
192262919d78SPeter Wemm 		status->pr_fpregsetsz = sizeof(elf_fpregset_t);
19238c9b7b2cSMarcel Moolenaar 		status->pr_osreldate = osreldate;
1924bd390213SMikolaj Golub 		status->pr_cursig = td->td_proc->p_sig;
1925bd390213SMikolaj Golub 		status->pr_pid = td->td_tid;
1926841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1927bd390213SMikolaj Golub 		fill_regs32(td, &status->pr_reg);
192862919d78SPeter Wemm #else
1929bd390213SMikolaj Golub 		fill_regs(td, &status->pr_reg);
193062919d78SPeter Wemm #endif
1931bd390213SMikolaj Golub 		sbuf_bcat(sb, status, sizeof(*status));
1932bd390213SMikolaj Golub 		free(status, M_TEMP);
19338c9b7b2cSMarcel Moolenaar 	}
1934bd390213SMikolaj Golub 	*sizep = sizeof(*status);
1935bd390213SMikolaj Golub }
1936bd390213SMikolaj Golub 
1937bd390213SMikolaj Golub static void
1938bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1939bd390213SMikolaj Golub {
1940bd390213SMikolaj Golub 	struct thread *td;
1941bd390213SMikolaj Golub 	elf_prfpregset_t *fpregset;
1942bd390213SMikolaj Golub 
1943bd390213SMikolaj Golub 	td = (struct thread *)arg;
1944bd390213SMikolaj Golub 	if (sb != NULL) {
1945bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1946bd390213SMikolaj Golub 		fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1947bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1948bd390213SMikolaj Golub 		fill_fpregs32(td, fpregset);
1949bd390213SMikolaj Golub #else
1950bd390213SMikolaj Golub 		fill_fpregs(td, fpregset);
1951bd390213SMikolaj Golub #endif
1952bd390213SMikolaj Golub 		sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1953bd390213SMikolaj Golub 		free(fpregset, M_TEMP);
1954bd390213SMikolaj Golub 	}
1955bd390213SMikolaj Golub 	*sizep = sizeof(*fpregset);
1956bd390213SMikolaj Golub }
1957bd390213SMikolaj Golub 
1958bd390213SMikolaj Golub static void
1959bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1960bd390213SMikolaj Golub {
1961bd390213SMikolaj Golub 	struct thread *td;
1962bd390213SMikolaj Golub 	elf_thrmisc_t thrmisc;
1963bd390213SMikolaj Golub 
1964bd390213SMikolaj Golub 	td = (struct thread *)arg;
1965bd390213SMikolaj Golub 	if (sb != NULL) {
1966bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1967bd390213SMikolaj Golub 		bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1968bd390213SMikolaj Golub 		strcpy(thrmisc.pr_tname, td->td_name);
1969bd390213SMikolaj Golub 		sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1970bd390213SMikolaj Golub 	}
1971bd390213SMikolaj Golub 	*sizep = sizeof(thrmisc);
1972bd390213SMikolaj Golub }
1973bd390213SMikolaj Golub 
19744da47b2fSMarcel Moolenaar /*
19754da47b2fSMarcel Moolenaar  * Allow for MD specific notes, as well as any MD
19764da47b2fSMarcel Moolenaar  * specific preparations for writing MI notes.
19774da47b2fSMarcel Moolenaar  */
19788c64af4fSJohn Polstra static void
1979bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
19808c64af4fSJohn Polstra {
1981bd390213SMikolaj Golub 	struct thread *td;
1982bd390213SMikolaj Golub 	void *buf;
1983bd390213SMikolaj Golub 	size_t size;
19848c64af4fSJohn Polstra 
1985bd390213SMikolaj Golub 	td = (struct thread *)arg;
1986bd390213SMikolaj Golub 	size = *sizep;
1987bd390213SMikolaj Golub 	if (size != 0 && sb != NULL)
1988bd390213SMikolaj Golub 		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
198983a396ceSChristian Brueffer 	else
199083a396ceSChristian Brueffer 		buf = NULL;
1991bd390213SMikolaj Golub 	size = 0;
1992bd390213SMikolaj Golub 	__elfN(dump_thread)(td, buf, &size);
199364779280SKonstantin Belousov 	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
199483a396ceSChristian Brueffer 	if (size != 0 && sb != NULL)
1995bd390213SMikolaj Golub 		sbuf_bcat(sb, buf, size);
1996a1761d73SChristian Brueffer 	free(buf, M_TEMP);
1997bd390213SMikolaj Golub 	*sizep = size;
19988c64af4fSJohn Polstra }
19998c64af4fSJohn Polstra 
2000f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE
2001f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
2002f1fca82eSMikolaj Golub #endif
2003f1fca82eSMikolaj Golub 
2004f1fca82eSMikolaj Golub static void
2005f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
2006f1fca82eSMikolaj Golub {
2007f1fca82eSMikolaj Golub 	struct proc *p;
2008f1fca82eSMikolaj Golub 	size_t size;
2009f1fca82eSMikolaj Golub 	int structsize;
2010f1fca82eSMikolaj Golub 
2011f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2012f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_numthreads *
2013f1fca82eSMikolaj Golub 	    sizeof(elf_kinfo_proc_t);
2014f1fca82eSMikolaj Golub 
2015f1fca82eSMikolaj Golub 	if (sb != NULL) {
2016f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2017f1fca82eSMikolaj Golub 		structsize = sizeof(elf_kinfo_proc_t);
2018f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
20196662ce5aSMateusz Guzik 		sx_slock(&proctree_lock);
2020f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2021f1fca82eSMikolaj Golub 		kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
20226662ce5aSMateusz Guzik 		sx_sunlock(&proctree_lock);
2023f1fca82eSMikolaj Golub 	}
2024f1fca82eSMikolaj Golub 	*sizep = size;
2025f1fca82eSMikolaj Golub }
2026f1fca82eSMikolaj Golub 
2027f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE
2028f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
2029f1fca82eSMikolaj Golub #endif
2030f1fca82eSMikolaj Golub 
2031f1fca82eSMikolaj Golub static void
2032f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
2033f1fca82eSMikolaj Golub {
2034f1fca82eSMikolaj Golub 	struct proc *p;
203514bdbaf2SConrad Meyer 	size_t size, sect_sz, i;
203614bdbaf2SConrad Meyer 	ssize_t start_len, sect_len;
203714bdbaf2SConrad Meyer 	int structsize, filedesc_flags;
203814bdbaf2SConrad Meyer 
2039bcb60d52SConrad Meyer 	if (coredump_pack_fileinfo)
204014bdbaf2SConrad Meyer 		filedesc_flags = KERN_FILEDESC_PACK_KINFO;
204114bdbaf2SConrad Meyer 	else
204214bdbaf2SConrad Meyer 		filedesc_flags = 0;
2043f1fca82eSMikolaj Golub 
2044f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
204514bdbaf2SConrad Meyer 	structsize = sizeof(struct kinfo_file);
2046f1fca82eSMikolaj Golub 	if (sb == NULL) {
2047f1fca82eSMikolaj Golub 		size = 0;
2048f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2049f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2050f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2051f1fca82eSMikolaj Golub 		PROC_LOCK(p);
205214bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2053f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2054f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2055f1fca82eSMikolaj Golub 		*sizep = size;
2056f1fca82eSMikolaj Golub 	} else {
205714bdbaf2SConrad Meyer 		sbuf_start_section(sb, &start_len);
205814bdbaf2SConrad Meyer 
2059f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2060f1fca82eSMikolaj Golub 		PROC_LOCK(p);
206114bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
206214bdbaf2SConrad Meyer 		    filedesc_flags);
206314bdbaf2SConrad Meyer 
206414bdbaf2SConrad Meyer 		sect_len = sbuf_end_section(sb, start_len, 0, 0);
206514bdbaf2SConrad Meyer 		if (sect_len < 0)
206614bdbaf2SConrad Meyer 			return;
206714bdbaf2SConrad Meyer 		sect_sz = sect_len;
206814bdbaf2SConrad Meyer 
206914bdbaf2SConrad Meyer 		KASSERT(sect_sz <= *sizep,
207014bdbaf2SConrad Meyer 		    ("kern_proc_filedesc_out did not respect maxlen; "
207114bdbaf2SConrad Meyer 		     "requested %zu, got %zu", *sizep - sizeof(structsize),
207214bdbaf2SConrad Meyer 		     sect_sz - sizeof(structsize)));
207314bdbaf2SConrad Meyer 
207414bdbaf2SConrad Meyer 		for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
207514bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
2076f1fca82eSMikolaj Golub 	}
2077f1fca82eSMikolaj Golub }
2078f1fca82eSMikolaj Golub 
2079f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE
2080f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2081f1fca82eSMikolaj Golub #endif
2082f1fca82eSMikolaj Golub 
2083f1fca82eSMikolaj Golub static void
2084f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2085f1fca82eSMikolaj Golub {
2086f1fca82eSMikolaj Golub 	struct proc *p;
2087f1fca82eSMikolaj Golub 	size_t size;
2088e6b95927SConrad Meyer 	int structsize, vmmap_flags;
2089e6b95927SConrad Meyer 
2090e6b95927SConrad Meyer 	if (coredump_pack_vmmapinfo)
2091e6b95927SConrad Meyer 		vmmap_flags = KERN_VMMAP_PACK_KINFO;
2092e6b95927SConrad Meyer 	else
2093e6b95927SConrad Meyer 		vmmap_flags = 0;
2094f1fca82eSMikolaj Golub 
2095f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2096e6b95927SConrad Meyer 	structsize = sizeof(struct kinfo_vmentry);
2097f1fca82eSMikolaj Golub 	if (sb == NULL) {
2098f1fca82eSMikolaj Golub 		size = 0;
2099f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2100f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2101f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2102f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2103e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2104f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2105f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2106f1fca82eSMikolaj Golub 		*sizep = size;
2107f1fca82eSMikolaj Golub 	} else {
2108f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2109f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2110e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2111e6b95927SConrad Meyer 		    vmmap_flags);
2112f1fca82eSMikolaj Golub 	}
2113f1fca82eSMikolaj Golub }
2114f1fca82eSMikolaj Golub 
2115f1fca82eSMikolaj Golub static void
2116f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2117f1fca82eSMikolaj Golub {
2118f1fca82eSMikolaj Golub 	struct proc *p;
2119f1fca82eSMikolaj Golub 	size_t size;
2120f1fca82eSMikolaj Golub 	int structsize;
2121f1fca82eSMikolaj Golub 
2122f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2123f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
2124f1fca82eSMikolaj Golub 	if (sb != NULL) {
2125f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2126f1fca82eSMikolaj Golub 		structsize = sizeof(gid_t);
2127f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2128f1fca82eSMikolaj Golub 		sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2129f1fca82eSMikolaj Golub 		    sizeof(gid_t));
2130f1fca82eSMikolaj Golub 	}
2131f1fca82eSMikolaj Golub 	*sizep = size;
2132f1fca82eSMikolaj Golub }
2133f1fca82eSMikolaj Golub 
2134f1fca82eSMikolaj Golub static void
2135f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2136f1fca82eSMikolaj Golub {
2137f1fca82eSMikolaj Golub 	struct proc *p;
2138f1fca82eSMikolaj Golub 	size_t size;
2139f1fca82eSMikolaj Golub 	int structsize;
2140f1fca82eSMikolaj Golub 
2141f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2142f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
2143f1fca82eSMikolaj Golub 	if (sb != NULL) {
2144f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2145f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_fd->fd_cmask);
2146f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2147f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
2148f1fca82eSMikolaj Golub 	}
2149f1fca82eSMikolaj Golub 	*sizep = size;
2150f1fca82eSMikolaj Golub }
2151f1fca82eSMikolaj Golub 
2152f1fca82eSMikolaj Golub static void
2153f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2154f1fca82eSMikolaj Golub {
2155f1fca82eSMikolaj Golub 	struct proc *p;
2156f1fca82eSMikolaj Golub 	struct rlimit rlim[RLIM_NLIMITS];
2157f1fca82eSMikolaj Golub 	size_t size;
2158f1fca82eSMikolaj Golub 	int structsize, i;
2159f1fca82eSMikolaj Golub 
2160f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2161f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(rlim);
2162f1fca82eSMikolaj Golub 	if (sb != NULL) {
2163f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2164f1fca82eSMikolaj Golub 		structsize = sizeof(rlim);
2165f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2166f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2167f1fca82eSMikolaj Golub 		for (i = 0; i < RLIM_NLIMITS; i++)
2168f6f6d240SMateusz Guzik 			lim_rlimit_proc(p, i, &rlim[i]);
2169f1fca82eSMikolaj Golub 		PROC_UNLOCK(p);
2170f1fca82eSMikolaj Golub 		sbuf_bcat(sb, rlim, sizeof(rlim));
2171f1fca82eSMikolaj Golub 	}
2172f1fca82eSMikolaj Golub 	*sizep = size;
2173f1fca82eSMikolaj Golub }
2174f1fca82eSMikolaj Golub 
2175f1fca82eSMikolaj Golub static void
2176f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2177f1fca82eSMikolaj Golub {
2178f1fca82eSMikolaj Golub 	struct proc *p;
2179f1fca82eSMikolaj Golub 	size_t size;
2180f1fca82eSMikolaj Golub 	int structsize;
2181f1fca82eSMikolaj Golub 
2182f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2183f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_osrel);
2184f1fca82eSMikolaj Golub 	if (sb != NULL) {
2185f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2186f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_osrel);
2187f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2188f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2189f1fca82eSMikolaj Golub 	}
2190f1fca82eSMikolaj Golub 	*sizep = size;
2191f1fca82eSMikolaj Golub }
2192f1fca82eSMikolaj Golub 
2193f1fca82eSMikolaj Golub static void
2194f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2195f1fca82eSMikolaj Golub {
2196f1fca82eSMikolaj Golub 	struct proc *p;
2197f1fca82eSMikolaj Golub 	elf_ps_strings_t ps_strings;
2198f1fca82eSMikolaj Golub 	size_t size;
2199f1fca82eSMikolaj Golub 	int structsize;
2200f1fca82eSMikolaj Golub 
2201f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2202f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(ps_strings);
2203f1fca82eSMikolaj Golub 	if (sb != NULL) {
2204f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2205f1fca82eSMikolaj Golub 		structsize = sizeof(ps_strings);
2206f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2207f1fca82eSMikolaj Golub 		ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2208f1fca82eSMikolaj Golub #else
2209f1fca82eSMikolaj Golub 		ps_strings = p->p_sysent->sv_psstrings;
2210f1fca82eSMikolaj Golub #endif
2211f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2212f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2213f1fca82eSMikolaj Golub 	}
2214f1fca82eSMikolaj Golub 	*sizep = size;
2215f1fca82eSMikolaj Golub }
2216f1fca82eSMikolaj Golub 
2217f1fca82eSMikolaj Golub static void
2218f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2219f1fca82eSMikolaj Golub {
2220f1fca82eSMikolaj Golub 	struct proc *p;
2221f1fca82eSMikolaj Golub 	size_t size;
2222f1fca82eSMikolaj Golub 	int structsize;
2223f1fca82eSMikolaj Golub 
2224f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2225f1fca82eSMikolaj Golub 	if (sb == NULL) {
2226f1fca82eSMikolaj Golub 		size = 0;
2227f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2228f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2229f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2230f1fca82eSMikolaj Golub 		PHOLD(p);
2231f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2232f1fca82eSMikolaj Golub 		PRELE(p);
2233f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2234f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2235f1fca82eSMikolaj Golub 		*sizep = size;
2236f1fca82eSMikolaj Golub 	} else {
2237f1fca82eSMikolaj Golub 		structsize = sizeof(Elf_Auxinfo);
2238f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2239f1fca82eSMikolaj Golub 		PHOLD(p);
2240f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2241f1fca82eSMikolaj Golub 		PRELE(p);
2242f1fca82eSMikolaj Golub 	}
2243f1fca82eSMikolaj Golub }
2244f1fca82eSMikolaj Golub 
224532c01de2SDmitry Chagin static boolean_t
22461a9c7decSKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
22471a9c7decSKonstantin Belousov     int32_t *osrel, const Elf_Phdr *pnote)
224832c01de2SDmitry Chagin {
2249267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
225032c01de2SDmitry Chagin 	const char *note_name;
22516c775eb6SKonstantin Belousov 	char *buf;
22526c775eb6SKonstantin Belousov 	int i, error;
22536c775eb6SKonstantin Belousov 	boolean_t res;
225432c01de2SDmitry Chagin 
22556c775eb6SKonstantin Belousov 	/* We need some limit, might as well use PAGE_SIZE. */
22566c775eb6SKonstantin Belousov 	if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
225732c01de2SDmitry Chagin 		return (FALSE);
22586c775eb6SKonstantin Belousov 	ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
22596c775eb6SKonstantin Belousov 	if (pnote->p_offset > PAGE_SIZE ||
22606c775eb6SKonstantin Belousov 	    pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
22616c775eb6SKonstantin Belousov 		VOP_UNLOCK(imgp->vp, 0);
22626c775eb6SKonstantin Belousov 		buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
22636c775eb6SKonstantin Belousov 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
22646c775eb6SKonstantin Belousov 		error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
22656c775eb6SKonstantin Belousov 		    pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
22666c775eb6SKonstantin Belousov 		    curthread->td_ucred, NOCRED, NULL, curthread);
22676c775eb6SKonstantin Belousov 		if (error != 0) {
22686c775eb6SKonstantin Belousov 			uprintf("i/o error PT_NOTE\n");
22696c775eb6SKonstantin Belousov 			res = FALSE;
22706c775eb6SKonstantin Belousov 			goto ret;
22716c775eb6SKonstantin Belousov 		}
22726c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)buf;
22736c775eb6SKonstantin Belousov 		note_end = (const Elf_Note *)(buf + pnote->p_filesz);
22746c775eb6SKonstantin Belousov 	} else {
22756c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)(imgp->image_header +
22766c775eb6SKonstantin Belousov 		    pnote->p_offset);
227732c01de2SDmitry Chagin 		note_end = (const Elf_Note *)(imgp->image_header +
227832c01de2SDmitry Chagin 		    pnote->p_offset + pnote->p_filesz);
22796c775eb6SKonstantin Belousov 		buf = NULL;
22806c775eb6SKonstantin Belousov 	}
2281267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2282d1ae5c83SKonstantin Belousov 		if (!aligned(note, Elf32_Addr) || (const char *)note_end -
22836c775eb6SKonstantin Belousov 		    (const char *)note < sizeof(Elf_Note)) {
22846c775eb6SKonstantin Belousov 			res = FALSE;
22856c775eb6SKonstantin Belousov 			goto ret;
22866c775eb6SKonstantin Belousov 		}
228732c01de2SDmitry Chagin 		if (note->n_namesz != checknote->hdr.n_namesz ||
228832c01de2SDmitry Chagin 		    note->n_descsz != checknote->hdr.n_descsz ||
228932c01de2SDmitry Chagin 		    note->n_type != checknote->hdr.n_type)
229032c01de2SDmitry Chagin 			goto nextnote;
229132c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
2292d1ae5c83SKonstantin Belousov 		if (note_name + checknote->hdr.n_namesz >=
2293d1ae5c83SKonstantin Belousov 		    (const char *)note_end || strncmp(checknote->vendor,
2294d1ae5c83SKonstantin Belousov 		    note_name, checknote->hdr.n_namesz) != 0)
229532c01de2SDmitry Chagin 			goto nextnote;
229632c01de2SDmitry Chagin 
229732c01de2SDmitry Chagin 		/*
229832c01de2SDmitry Chagin 		 * Fetch the osreldate for binary
229932c01de2SDmitry Chagin 		 * from the ELF OSABI-note if necessary.
230032c01de2SDmitry Chagin 		 */
230189ffc202SBjoern A. Zeeb 		if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
23026c775eb6SKonstantin Belousov 		    checknote->trans_osrel != NULL) {
23036c775eb6SKonstantin Belousov 			res = checknote->trans_osrel(note, osrel);
23046c775eb6SKonstantin Belousov 			goto ret;
23056c775eb6SKonstantin Belousov 		}
23066c775eb6SKonstantin Belousov 		res = TRUE;
23076c775eb6SKonstantin Belousov 		goto ret;
230832c01de2SDmitry Chagin nextnote:
230932c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
23101b8388cdSMikolaj Golub 		    roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
23111b8388cdSMikolaj Golub 		    roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
231232c01de2SDmitry Chagin 	}
23136c775eb6SKonstantin Belousov 	res = FALSE;
23146c775eb6SKonstantin Belousov ret:
23156c775eb6SKonstantin Belousov 	free(buf, M_TEMP);
23166c775eb6SKonstantin Belousov 	return (res);
231732c01de2SDmitry Chagin }
231832c01de2SDmitry Chagin 
231932c01de2SDmitry Chagin /*
23201a9c7decSKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote,
23211a9c7decSKonstantin Belousov  * fetch the osreldate for binary from the ELF OSABI-note. Only the
23221a9c7decSKonstantin Belousov  * first page of the image is searched, the same as for headers.
23231a9c7decSKonstantin Belousov  */
23241a9c7decSKonstantin Belousov static boolean_t
23251a9c7decSKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
23261a9c7decSKonstantin Belousov     int32_t *osrel)
23271a9c7decSKonstantin Belousov {
23281a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
23291a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
23301a9c7decSKonstantin Belousov 	int i;
23311a9c7decSKonstantin Belousov 
23321a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
23331a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
23341a9c7decSKonstantin Belousov 
23351a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
23361a9c7decSKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE &&
23371a9c7decSKonstantin Belousov 		    __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
23381a9c7decSKonstantin Belousov 			return (TRUE);
23391a9c7decSKonstantin Belousov 	}
23401a9c7decSKonstantin Belousov 	return (FALSE);
23411a9c7decSKonstantin Belousov 
23421a9c7decSKonstantin Belousov }
23431a9c7decSKonstantin Belousov 
23441a9c7decSKonstantin Belousov /*
2345e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
2346e1743d02SSøren Schmidt  */
2347a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
2348a360a43dSJake Burkholder 	__CONCAT(exec_, __elfN(imgact)),
2349a360a43dSJake Burkholder 	__XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2350a360a43dSJake Burkholder };
2351a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2352e7228204SAlfred Perlstein 
2353ed167eaaSKonstantin Belousov static vm_prot_t
2354ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
2355ed167eaaSKonstantin Belousov {
2356ed167eaaSKonstantin Belousov 	vm_prot_t prot;
2357ed167eaaSKonstantin Belousov 
2358ed167eaaSKonstantin Belousov 	prot = 0;
2359ed167eaaSKonstantin Belousov 	if (flags & PF_X)
2360ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
2361ed167eaaSKonstantin Belousov 	if (flags & PF_W)
2362ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
2363ed167eaaSKonstantin Belousov 	if (flags & PF_R)
2364ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
2365676eda08SMarcel Moolenaar #if __ELF_WORD_SIZE == 32
2366e7d939bdSMarcel Moolenaar #if defined(__amd64__)
2367126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
2368676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
2369676eda08SMarcel Moolenaar #endif
2370676eda08SMarcel Moolenaar #endif
2371ed167eaaSKonstantin Belousov 	return (prot);
2372ed167eaaSKonstantin Belousov }
2373ed167eaaSKonstantin Belousov 
2374ed167eaaSKonstantin Belousov static Elf_Word
2375ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
2376ed167eaaSKonstantin Belousov {
2377ed167eaaSKonstantin Belousov 	Elf_Word flags;
2378ed167eaaSKonstantin Belousov 
2379ed167eaaSKonstantin Belousov 	flags = 0;
2380ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
2381ed167eaaSKonstantin Belousov 		flags |= PF_X;
2382ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
2383ed167eaaSKonstantin Belousov 		flags |= PF_R;
2384ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
2385ed167eaaSKonstantin Belousov 		flags |= PF_W;
2386ed167eaaSKonstantin Belousov 	return (flags);
2387ed167eaaSKonstantin Belousov }
2388