xref: /freebsd/sys/kern/imgact_elf.c (revision 5bc3c61780d775810eea852936671ca4a232b2a8)
1e1743d02SSøren Schmidt /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a36da99SPedro F. Giffuni  *
486be94fcSTycho Nightingale  * Copyright (c) 2017 Dell EMC
5455d3589SDavid E. O'Brien  * Copyright (c) 2000-2001, 2003 David O'Brien
69a14aa01SUlrich Spörlein  * Copyright (c) 1995-1996 Søren Schmidt
7e1743d02SSøren Schmidt  * Copyright (c) 1996 Peter Wemm
8e1743d02SSøren Schmidt  * All rights reserved.
9e1743d02SSøren Schmidt  *
10e1743d02SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
11e1743d02SSøren Schmidt  * modification, are permitted provided that the following conditions
12e1743d02SSøren Schmidt  * are met:
13e1743d02SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
14e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
15e1743d02SSøren Schmidt  *    in this position and unchanged.
16e1743d02SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
17e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
18e1743d02SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
19e1743d02SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
2021dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
21e1743d02SSøren Schmidt  *
22e1743d02SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23e1743d02SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24e1743d02SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25e1743d02SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26e1743d02SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27e1743d02SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28e1743d02SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29e1743d02SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30e1743d02SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31e1743d02SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32e1743d02SSøren Schmidt  */
33e1743d02SSøren Schmidt 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
3712bc222eSJonathan Anderson #include "opt_capsicum.h"
3862919d78SPeter Wemm 
39e1743d02SSøren Schmidt #include <sys/param.h>
404a144410SRobert Watson #include <sys/capsicum.h>
4178f57a9cSMark Johnston #include <sys/compressor.h>
42e1743d02SSøren Schmidt #include <sys/exec.h>
438c64af4fSJohn Polstra #include <sys/fcntl.h>
44e1743d02SSøren Schmidt #include <sys/imgact.h>
45e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
46b96bd95bSIan Lepore #include <sys/jail.h>
47e1743d02SSøren Schmidt #include <sys/kernel.h>
48f34fa851SJohn Baldwin #include <sys/lock.h>
49e1743d02SSøren Schmidt #include <sys/malloc.h>
5068ff2a43SChristian S.J. Peron #include <sys/mount.h>
518c64af4fSJohn Polstra #include <sys/mman.h>
52a794e791SBruce Evans #include <sys/namei.h>
53a794e791SBruce Evans #include <sys/proc.h>
548c64af4fSJohn Polstra #include <sys/procfs.h>
5586be94fcSTycho Nightingale #include <sys/ptrace.h>
561ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
578c64af4fSJohn Polstra #include <sys/resourcevar.h>
5889f6b863SAttilio Rao #include <sys/rwlock.h>
59bd390213SMikolaj Golub #include <sys/sbuf.h>
60da61b9a6SAlan Cox #include <sys/sf_buf.h>
61ee235befSKonstantin Belousov #include <sys/smp.h>
6236240ea5SDoug Rabson #include <sys/systm.h>
63e1743d02SSøren Schmidt #include <sys/signalvar.h>
648c64af4fSJohn Polstra #include <sys/stat.h>
651005a129SJohn Baldwin #include <sys/sx.h>
668c64af4fSJohn Polstra #include <sys/syscall.h>
67e1743d02SSøren Schmidt #include <sys/sysctl.h>
688c64af4fSJohn Polstra #include <sys/sysent.h>
69a794e791SBruce Evans #include <sys/vnode.h>
70e7228204SAlfred Perlstein #include <sys/syslog.h>
71e7228204SAlfred Perlstein #include <sys/eventhandler.h>
72f1fca82eSMikolaj Golub #include <sys/user.h>
73e7228204SAlfred Perlstein 
74e1743d02SSøren Schmidt #include <vm/vm.h>
75e1743d02SSøren Schmidt #include <vm/vm_kern.h>
76e1743d02SSøren Schmidt #include <vm/vm_param.h>
77e1743d02SSøren Schmidt #include <vm/pmap.h>
78e1743d02SSøren Schmidt #include <vm/vm_map.h>
790ff27d31SJohn Polstra #include <vm/vm_object.h>
80e1743d02SSøren Schmidt #include <vm/vm_extern.h>
81e1743d02SSøren Schmidt 
8252c24af7SPeter Wemm #include <machine/elf.h>
83e1743d02SSøren Schmidt #include <machine/md_var.h>
84e1743d02SSøren Schmidt 
851b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE	4
86c815a20cSDavid E. O'Brien #define OLD_EI_BRAND	8
87c815a20cSDavid E. O'Brien 
883ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr);
8932c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
9009c78d53SEdward Tomasz Napierala     const char *interp, int32_t *osrel, uint32_t *fctl0);
913ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
921699546dSEdward Tomasz Napierala     u_long *entry);
930bbee4cdSKonstantin Belousov static int __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
941699546dSEdward Tomasz Napierala     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot);
953ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
96a95659f7SEd Maste static bool __elfN(freebsd_trans_osrel)(const Elf_Note *note,
9789ffc202SBjoern A. Zeeb     int32_t *osrel);
98a95659f7SEd Maste static bool kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
9932c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp,
1000cad2aa2SKonstantin Belousov     Elf_Brandnote *checknote, int32_t *osrel, boolean_t *has_fctl0,
1010cad2aa2SKonstantin Belousov     uint32_t *fctl0);
102ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word);
103ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t);
104e1743d02SSøren Schmidt 
1057029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE),
1067029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
107a360a43dSJake Burkholder     "");
108a360a43dSJake Burkholder 
109bd390213SMikolaj Golub #define	CORE_BUF_SIZE	(16 * 1024)
110e7228204SAlfred Perlstein 
111e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1;
112e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
113af3b2549SHans Petter Selasky     fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
114a360a43dSJake Burkholder     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
115a360a43dSJake Burkholder 
116551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0;
117a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
1181cbb879dSEd Maste     &elf_legacy_coredump, 0,
1191cbb879dSEd Maste     "include all and only RW pages in core dumps");
120e1743d02SSøren Schmidt 
12162c625fdSKonstantin Belousov int __elfN(nxstack) =
1224d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \
1234bf4b0f1SJohn Baldwin     (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) || \
1244bf4b0f1SJohn Baldwin     defined(__riscv)
12562c625fdSKonstantin Belousov 	1;
12662c625fdSKonstantin Belousov #else
12762c625fdSKonstantin Belousov 	0;
12862c625fdSKonstantin Belousov #endif
129291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
130291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
131291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
132291c06a1SKonstantin Belousov 
133eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
134126b36a2SKonstantin Belousov int i386_read_exec = 0;
135126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
136126b36a2SKonstantin Belousov     "enable execution from readable segments");
137126b36a2SKonstantin Belousov #endif
138126b36a2SKonstantin Belousov 
13995aafd69SKonstantin Belousov static u_long __elfN(pie_base) = ET_DYN_LOAD_ADDR;
140f33533daSKonstantin Belousov static int
141f33533daSKonstantin Belousov sysctl_pie_base(SYSCTL_HANDLER_ARGS)
142f33533daSKonstantin Belousov {
143f33533daSKonstantin Belousov 	u_long val;
144f33533daSKonstantin Belousov 	int error;
145f33533daSKonstantin Belousov 
146f33533daSKonstantin Belousov 	val = __elfN(pie_base);
147f33533daSKonstantin Belousov 	error = sysctl_handle_long(oidp, &val, 0, req);
148f33533daSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
149f33533daSKonstantin Belousov 		return (error);
150f33533daSKonstantin Belousov 	if ((val & PAGE_MASK) != 0)
151f33533daSKonstantin Belousov 		return (EINVAL);
152f33533daSKonstantin Belousov 	__elfN(pie_base) = val;
153f33533daSKonstantin Belousov 	return (0);
154f33533daSKonstantin Belousov }
155f33533daSKonstantin Belousov SYSCTL_PROC(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, pie_base,
156f33533daSKonstantin Belousov     CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
157f33533daSKonstantin Belousov     sysctl_pie_base, "LU",
15895aafd69SKonstantin Belousov     "PIE load base without randomization");
15995aafd69SKonstantin Belousov 
1607029da5cSPawel Biernacki SYSCTL_NODE(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, aslr,
1617029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
162fa50a355SKonstantin Belousov     "");
163fa50a355SKonstantin Belousov #define	ASLR_NODE_OID	__CONCAT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), _aslr)
164fa50a355SKonstantin Belousov 
165fa50a355SKonstantin Belousov static int __elfN(aslr_enabled) = 0;
166fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN,
167fa50a355SKonstantin Belousov     &__elfN(aslr_enabled), 0,
168fa50a355SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
169fa50a355SKonstantin Belousov     ": enable address map randomization");
170fa50a355SKonstantin Belousov 
171fa50a355SKonstantin Belousov static int __elfN(pie_aslr_enabled) = 0;
172fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, pie_enable, CTLFLAG_RWTUN,
173fa50a355SKonstantin Belousov     &__elfN(pie_aslr_enabled), 0,
174fa50a355SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
175fa50a355SKonstantin Belousov     ": enable address map randomization for PIE binaries");
176fa50a355SKonstantin Belousov 
177fa50a355SKonstantin Belousov static int __elfN(aslr_honor_sbrk) = 1;
178fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, honor_sbrk, CTLFLAG_RW,
179fa50a355SKonstantin Belousov     &__elfN(aslr_honor_sbrk), 0,
180fa50a355SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": assume sbrk is used");
181fa50a355SKonstantin Belousov 
182fc83c5a7SKonstantin Belousov static int __elfN(aslr_stack_gap) = 3;
183fc83c5a7SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, stack_gap, CTLFLAG_RW,
184fc83c5a7SKonstantin Belousov     &__elfN(aslr_stack_gap), 0,
185fc83c5a7SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
186fc83c5a7SKonstantin Belousov     ": maximum percentage of main stack to waste on a random gap");
187fc83c5a7SKonstantin Belousov 
188944cf37bSKonstantin Belousov static int __elfN(sigfastblock) = 1;
189944cf37bSKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, sigfastblock,
190944cf37bSKonstantin Belousov     CTLFLAG_RWTUN, &__elfN(sigfastblock), 0,
191944cf37bSKonstantin Belousov     "enable sigfastblock for new processes");
192944cf37bSKonstantin Belousov 
1932e1c94aaSKonstantin Belousov static bool __elfN(allow_wx) = true;
1942e1c94aaSKonstantin Belousov SYSCTL_BOOL(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, allow_wx,
1952e1c94aaSKonstantin Belousov     CTLFLAG_RWTUN, &__elfN(allow_wx), 0,
1962e1c94aaSKonstantin Belousov     "Allow pages to be mapped simultaneously writable and executable");
1972e1c94aaSKonstantin Belousov 
1983ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
199e1743d02SSøren Schmidt 
200545517f1SEdward Tomasz Napierala #define	aligned(a, t)	(rounddown2((u_long)(a), sizeof(t)) == (u_long)(a))
20193d1c728SKonstantin Belousov 
20232c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
20332c01de2SDmitry Chagin 
20432c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = {
20532c01de2SDmitry Chagin 	.hdr.n_namesz	= sizeof(FREEBSD_ABI_VENDOR),
20632c01de2SDmitry Chagin 	.hdr.n_descsz	= sizeof(int32_t),
2074c22b468SEd Maste 	.hdr.n_type	= NT_FREEBSD_ABI_TAG,
20832c01de2SDmitry Chagin 	.vendor		= FREEBSD_ABI_VENDOR,
20989ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
21089ffc202SBjoern A. Zeeb 	.trans_osrel	= __elfN(freebsd_trans_osrel)
21132c01de2SDmitry Chagin };
21232c01de2SDmitry Chagin 
213a95659f7SEd Maste static bool
21489ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
21589ffc202SBjoern A. Zeeb {
21689ffc202SBjoern A. Zeeb 	uintptr_t p;
21789ffc202SBjoern A. Zeeb 
21889ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
2191b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
22089ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
22189ffc202SBjoern A. Zeeb 
222a95659f7SEd Maste 	return (true);
22389ffc202SBjoern A. Zeeb }
22489ffc202SBjoern A. Zeeb 
22589ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU";
22689ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3;
22789ffc202SBjoern A. Zeeb 
22889ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = {
22989ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
23089ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
23189ffc202SBjoern A. Zeeb 	.hdr.n_type	= 1,
23289ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
23389ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
23489ffc202SBjoern A. Zeeb 	.trans_osrel	= kfreebsd_trans_osrel
23589ffc202SBjoern A. Zeeb };
23689ffc202SBjoern A. Zeeb 
237a95659f7SEd Maste static bool
23889ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
23989ffc202SBjoern A. Zeeb {
24089ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
24189ffc202SBjoern A. Zeeb 	uintptr_t p;
24289ffc202SBjoern A. Zeeb 
24389ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
2441b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
24589ffc202SBjoern A. Zeeb 
24689ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
24789ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
248a95659f7SEd Maste 		return (false);
24989ffc202SBjoern A. Zeeb 
25089ffc202SBjoern A. Zeeb 	/*
25189ffc202SBjoern A. Zeeb 	 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
25289ffc202SBjoern A. Zeeb 	 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
25389ffc202SBjoern A. Zeeb 	 */
25489ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
25589ffc202SBjoern A. Zeeb 
256a95659f7SEd Maste 	return (true);
25789ffc202SBjoern A. Zeeb }
25889ffc202SBjoern A. Zeeb 
259e1743d02SSøren Schmidt int
2603ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
261e1743d02SSøren Schmidt {
262e1743d02SSøren Schmidt 	int i;
263e1743d02SSøren Schmidt 
2643ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
265ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == NULL) {
266ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = entry;
267e1743d02SSøren Schmidt 			break;
268e1743d02SSøren Schmidt 		}
269e1743d02SSøren Schmidt 	}
270925c8b5bSBjoern A. Zeeb 	if (i == MAX_BRANDS) {
271925c8b5bSBjoern A. Zeeb 		printf("WARNING: %s: could not insert brandinfo entry: %p\n",
272925c8b5bSBjoern A. Zeeb 			__func__, entry);
273a7cddfedSJake Burkholder 		return (-1);
274925c8b5bSBjoern A. Zeeb 	}
275a7cddfedSJake Burkholder 	return (0);
276e1743d02SSøren Schmidt }
277e1743d02SSøren Schmidt 
278e1743d02SSøren Schmidt int
2793ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
280e1743d02SSøren Schmidt {
281e1743d02SSøren Schmidt 	int i;
282e1743d02SSøren Schmidt 
2833ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
284ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == entry) {
285ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = NULL;
286e1743d02SSøren Schmidt 			break;
287e1743d02SSøren Schmidt 		}
288e1743d02SSøren Schmidt 	}
289ea5a2b2eSSøren Schmidt 	if (i == MAX_BRANDS)
290a7cddfedSJake Burkholder 		return (-1);
291a7cddfedSJake Burkholder 	return (0);
292e1743d02SSøren Schmidt }
293e1743d02SSøren Schmidt 
294096977faSMark Newton int
2953ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry)
296096977faSMark Newton {
297096977faSMark Newton 	struct proc *p;
298553629ebSJake Burkholder 	int rval = FALSE;
299096977faSMark Newton 
3001005a129SJohn Baldwin 	sx_slock(&allproc_lock);
3014f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
302553629ebSJake Burkholder 		if (p->p_sysent == entry->sysvec) {
303553629ebSJake Burkholder 			rval = TRUE;
304553629ebSJake Burkholder 			break;
305096977faSMark Newton 		}
306553629ebSJake Burkholder 	}
3071005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
308096977faSMark Newton 
309553629ebSJake Burkholder 	return (rval);
310096977faSMark Newton }
311096977faSMark Newton 
3125fe3ed62SJake Burkholder static Elf_Brandinfo *
31332c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
31409c78d53SEdward Tomasz Napierala     int32_t *osrel, uint32_t *fctl0)
3155fe3ed62SJake Burkholder {
31632c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
317af582aaeSKonstantin Belousov 	Elf_Brandinfo *bi, *bi_m;
3180cad2aa2SKonstantin Belousov 	boolean_t ret, has_fctl0;
31909c78d53SEdward Tomasz Napierala 	int i, interp_name_len;
32009c78d53SEdward Tomasz Napierala 
321be7808dcSKonstantin Belousov 	interp_name_len = interp != NULL ? strlen(interp) + 1 : 0;
3225fe3ed62SJake Burkholder 
3235fe3ed62SJake Burkholder 	/*
32432c01de2SDmitry Chagin 	 * We support four types of branding -- (1) the ELF EI_OSABI field
3255fe3ed62SJake Burkholder 	 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
32632c01de2SDmitry Chagin 	 * branding w/in the ELF header, (3) path of the `interp_path'
32732c01de2SDmitry Chagin 	 * field, and (4) the ".note.ABI-tag" ELF section.
3285fe3ed62SJake Burkholder 	 */
3295fe3ed62SJake Burkholder 
33032c01de2SDmitry Chagin 	/* Look for an ".note.ABI-tag" ELF section */
331af582aaeSKonstantin Belousov 	bi_m = NULL;
33232c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
33332c01de2SDmitry Chagin 		bi = elf_brand_list[i];
334ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
335ecc2fda8SBjoern A. Zeeb 			continue;
3362274ab3dSKonstantin Belousov 		if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)
3371438fe3cSKonstantin Belousov 			continue;
338ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
339ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
3400cad2aa2SKonstantin Belousov 			has_fctl0 = false;
3410cad2aa2SKonstantin Belousov 			*fctl0 = 0;
3420cad2aa2SKonstantin Belousov 			*osrel = 0;
343cefb93f2SKonstantin Belousov 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel,
3440cad2aa2SKonstantin Belousov 			    &has_fctl0, fctl0);
345f19d421aSNathan Whitehorn 			/* Give brand a chance to veto check_note's guess */
3460cad2aa2SKonstantin Belousov 			if (ret && bi->header_supported) {
3470cad2aa2SKonstantin Belousov 				ret = bi->header_supported(imgp, osrel,
3480cad2aa2SKonstantin Belousov 				    has_fctl0 ? fctl0 : NULL);
3490cad2aa2SKonstantin Belousov 			}
350af582aaeSKonstantin Belousov 			/*
351af582aaeSKonstantin Belousov 			 * If note checker claimed the binary, but the
352af582aaeSKonstantin Belousov 			 * interpreter path in the image does not
353af582aaeSKonstantin Belousov 			 * match default one for the brand, try to
354af582aaeSKonstantin Belousov 			 * search for other brands with the same
355af582aaeSKonstantin Belousov 			 * interpreter.  Either there is better brand
356af582aaeSKonstantin Belousov 			 * with the right interpreter, or, failing
357af582aaeSKonstantin Belousov 			 * this, we return first brand which accepted
358af582aaeSKonstantin Belousov 			 * our note and, optionally, header.
359af582aaeSKonstantin Belousov 			 */
3603aeacc55SKonstantin Belousov 			if (ret && bi_m == NULL && interp != NULL &&
3613aeacc55SKonstantin Belousov 			    (bi->interp_path == NULL ||
3623aeacc55SKonstantin Belousov 			    (strlen(bi->interp_path) + 1 != interp_name_len ||
3633aeacc55SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
3643aeacc55SKonstantin Belousov 			    != 0))) {
365af582aaeSKonstantin Belousov 				bi_m = bi;
366af582aaeSKonstantin Belousov 				ret = 0;
367af582aaeSKonstantin Belousov 			}
36832c01de2SDmitry Chagin 			if (ret)
36932c01de2SDmitry Chagin 				return (bi);
37032c01de2SDmitry Chagin 		}
37132c01de2SDmitry Chagin 	}
372af582aaeSKonstantin Belousov 	if (bi_m != NULL)
373af582aaeSKonstantin Belousov 		return (bi_m);
37432c01de2SDmitry Chagin 
3755fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
3765fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3775fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
3781438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
3792274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
380ecc2fda8SBjoern A. Zeeb 			continue;
381ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
3825fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
3830fe98320SEd Schouten 		    (bi->compat_3_brand != NULL &&
3843d560b4bSKonstantin Belousov 		    strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
3850fe98320SEd Schouten 		    bi->compat_3_brand) == 0))) {
386686d2f31SNathan Whitehorn 			/* Looks good, but give brand a chance to veto */
387d722231bSJohn Baldwin 			if (bi->header_supported == NULL ||
3880cad2aa2SKonstantin Belousov 			    bi->header_supported(imgp, NULL, NULL)) {
38915a9aedfSKonstantin Belousov 				/*
39015a9aedfSKonstantin Belousov 				 * Again, prefer strictly matching
39115a9aedfSKonstantin Belousov 				 * interpreter path.
39215a9aedfSKonstantin Belousov 				 */
3937aab7a80SKonstantin Belousov 				if (interp_name_len == 0 &&
3947aab7a80SKonstantin Belousov 				    bi->interp_path == NULL)
3957aab7a80SKonstantin Belousov 					return (bi);
3967aab7a80SKonstantin Belousov 				if (bi->interp_path != NULL &&
3977aab7a80SKonstantin Belousov 				    strlen(bi->interp_path) + 1 ==
39815a9aedfSKonstantin Belousov 				    interp_name_len && strncmp(interp,
39915a9aedfSKonstantin Belousov 				    bi->interp_path, interp_name_len) == 0)
4005fe3ed62SJake Burkholder 					return (bi);
40115a9aedfSKonstantin Belousov 				if (bi_m == NULL)
40215a9aedfSKonstantin Belousov 					bi_m = bi;
4035fe3ed62SJake Burkholder 			}
404686d2f31SNathan Whitehorn 		}
40515a9aedfSKonstantin Belousov 	}
40615a9aedfSKonstantin Belousov 	if (bi_m != NULL)
40715a9aedfSKonstantin Belousov 		return (bi_m);
4085fe3ed62SJake Burkholder 
409817dc004SWarner Losh 	/* No known brand, see if the header is recognized by any brand */
410817dc004SWarner Losh 	for (i = 0; i < MAX_BRANDS; i++) {
411817dc004SWarner Losh 		bi = elf_brand_list[i];
412817dc004SWarner Losh 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
413817dc004SWarner Losh 		    bi->header_supported == NULL)
414817dc004SWarner Losh 			continue;
415817dc004SWarner Losh 		if (hdr->e_machine == bi->machine) {
4160cad2aa2SKonstantin Belousov 			ret = bi->header_supported(imgp, NULL, NULL);
417817dc004SWarner Losh 			if (ret)
418817dc004SWarner Losh 				return (bi);
419817dc004SWarner Losh 		}
420817dc004SWarner Losh 	}
421817dc004SWarner Losh 
4225fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
4235fe3ed62SJake Burkholder 	if (interp != NULL) {
4245fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
4255fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
4262274ab3dSKonstantin Belousov 			if (bi == NULL || (bi->flags &
4272274ab3dSKonstantin Belousov 			    (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC))
4282274ab3dSKonstantin Belousov 			    != 0)
429ecc2fda8SBjoern A. Zeeb 				continue;
430ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
4313aeacc55SKonstantin Belousov 			    bi->interp_path != NULL &&
432d1ae5c83SKonstantin Belousov 			    /* ELF image p_filesz includes terminating zero */
433d1ae5c83SKonstantin Belousov 			    strlen(bi->interp_path) + 1 == interp_name_len &&
434d1ae5c83SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
435d722231bSJohn Baldwin 			    == 0 && (bi->header_supported == NULL ||
4360cad2aa2SKonstantin Belousov 			    bi->header_supported(imgp, NULL, NULL)))
4375fe3ed62SJake Burkholder 				return (bi);
4385fe3ed62SJake Burkholder 		}
4395fe3ed62SJake Burkholder 	}
4405fe3ed62SJake Burkholder 
4415fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
4425fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
4435fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
4441438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
4452274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
446ecc2fda8SBjoern A. Zeeb 			continue;
447ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
448d722231bSJohn Baldwin 		    __elfN(fallback_brand) == bi->brand &&
449d722231bSJohn Baldwin 		    (bi->header_supported == NULL ||
4500cad2aa2SKonstantin Belousov 		    bi->header_supported(imgp, NULL, NULL)))
4515fe3ed62SJake Burkholder 			return (bi);
4525fe3ed62SJake Burkholder 	}
4535fe3ed62SJake Burkholder 	return (NULL);
4545fe3ed62SJake Burkholder }
4555fe3ed62SJake Burkholder 
4567de1bc13SKonstantin Belousov static bool
4577de1bc13SKonstantin Belousov __elfN(phdr_in_zero_page)(const Elf_Ehdr *hdr)
4587de1bc13SKonstantin Belousov {
4597de1bc13SKonstantin Belousov 	return (hdr->e_phoff <= PAGE_SIZE &&
4607de1bc13SKonstantin Belousov 	    (u_int)hdr->e_phentsize * hdr->e_phnum <= PAGE_SIZE - hdr->e_phoff);
4617de1bc13SKonstantin Belousov }
4627de1bc13SKonstantin Belousov 
463e1743d02SSøren Schmidt static int
4643ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
465e1743d02SSøren Schmidt {
466d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
4673ebc1248SPeter Wemm 	int i;
4683ebc1248SPeter Wemm 
46952c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
47052c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
47152c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
4723dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
4733dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
4743dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
475a7cddfedSJake Burkholder 		return (ENOEXEC);
476e1743d02SSøren Schmidt 
4773ebc1248SPeter Wemm 	/*
4783ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
4793ebc1248SPeter Wemm 	 */
4803ebc1248SPeter Wemm 
4813ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
482d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
483d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
4843ebc1248SPeter Wemm 			break;
4853ebc1248SPeter Wemm 	}
4863ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
487a7cddfedSJake Burkholder 		return (ENOEXEC);
488e1743d02SSøren Schmidt 
489a7cddfedSJake Burkholder 	return (0);
490e1743d02SSøren Schmidt }
491e1743d02SSøren Schmidt 
492e1743d02SSøren Schmidt static int
4933ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
494ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
4953ebc1248SPeter Wemm {
496da61b9a6SAlan Cox 	struct sf_buf *sf;
497da61b9a6SAlan Cox 	int error;
4983ebc1248SPeter Wemm 	vm_offset_t off;
4993ebc1248SPeter Wemm 
5003ebc1248SPeter Wemm 	/*
5013ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
5023ebc1248SPeter Wemm 	 */
503aaadc41fSKonstantin Belousov 	vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) -
504aaadc41fSKonstantin Belousov 	    trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL);
5053ebc1248SPeter Wemm 
5063ebc1248SPeter Wemm 	/*
5073ebc1248SPeter Wemm 	 * Find the page from the underlying object.
5083ebc1248SPeter Wemm 	 */
50928e8da65SAlan Cox 	if (object != NULL) {
510da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
511da61b9a6SAlan Cox 		if (sf == NULL)
512da61b9a6SAlan Cox 			return (KERN_FAILURE);
5133ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
514da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
515ca0387efSJake Burkholder 		    end - start);
516be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
517fe0a8a39SKonstantin Belousov 		if (error != 0)
518a7cddfedSJake Burkholder 			return (KERN_FAILURE);
5193ebc1248SPeter Wemm 	}
5203ebc1248SPeter Wemm 
521a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
5223ebc1248SPeter Wemm }
5233ebc1248SPeter Wemm 
5243ebc1248SPeter Wemm static int
525e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object,
526e3d8f8feSKonstantin Belousov     vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot,
527e3d8f8feSKonstantin Belousov     int cow)
5283ebc1248SPeter Wemm {
529da61b9a6SAlan Cox 	struct sf_buf *sf;
530da61b9a6SAlan Cox 	vm_offset_t off;
531a063facbSMarcel Moolenaar 	vm_size_t sz;
532e3d8f8feSKonstantin Belousov 	int error, locked, rv;
5333ebc1248SPeter Wemm 
5343ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
53581f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
536ff6f03c7SAlan Cox 		    round_page(start), prot);
53728e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
538a7cddfedSJake Burkholder 			return (rv);
5393ebc1248SPeter Wemm 		offset += round_page(start) - start;
5403ebc1248SPeter Wemm 		start = round_page(start);
5413ebc1248SPeter Wemm 	}
5423ebc1248SPeter Wemm 	if (end != round_page(end)) {
54381f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
544ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
54528e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
546a7cddfedSJake Burkholder 			return (rv);
5473ebc1248SPeter Wemm 		end = trunc_page(end);
5483ebc1248SPeter Wemm 	}
549e383e820SAlan Cox 	if (start >= end)
550e383e820SAlan Cox 		return (KERN_SUCCESS);
551e383e820SAlan Cox 	if ((offset & PAGE_MASK) != 0) {
5523ebc1248SPeter Wemm 		/*
553e383e820SAlan Cox 		 * The mapping is not page aligned.  This means that we have
554e383e820SAlan Cox 		 * to copy the data.
5553ebc1248SPeter Wemm 		 */
556aaadc41fSKonstantin Belousov 		rv = vm_map_fixed(map, NULL, 0, start, end - start,
557aaadc41fSKonstantin Belousov 		    prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL);
5585420f76bSKonstantin Belousov 		if (rv != KERN_SUCCESS)
559a7cddfedSJake Burkholder 			return (rv);
560da61b9a6SAlan Cox 		if (object == NULL)
561da61b9a6SAlan Cox 			return (KERN_SUCCESS);
562da61b9a6SAlan Cox 		for (; start < end; start += sz) {
563da61b9a6SAlan Cox 			sf = vm_imgact_map_page(object, offset);
564da61b9a6SAlan Cox 			if (sf == NULL)
565da61b9a6SAlan Cox 				return (KERN_FAILURE);
5663ebc1248SPeter Wemm 			off = offset - trunc_page(offset);
5673ebc1248SPeter Wemm 			sz = end - start;
568da61b9a6SAlan Cox 			if (sz > PAGE_SIZE - off)
569da61b9a6SAlan Cox 				sz = PAGE_SIZE - off;
570da61b9a6SAlan Cox 			error = copyout((caddr_t)sf_buf_kva(sf) + off,
5713ebc1248SPeter Wemm 			    (caddr_t)start, sz);
572be996836SAttilio Rao 			vm_imgact_unmap_page(sf);
5735420f76bSKonstantin Belousov 			if (error != 0)
574a7cddfedSJake Burkholder 				return (KERN_FAILURE);
575da61b9a6SAlan Cox 			offset += sz;
5763ebc1248SPeter Wemm 		}
5773ebc1248SPeter Wemm 	} else {
578e5e6093bSAlan Cox 		vm_object_reference(object);
579e383e820SAlan Cox 		rv = vm_map_fixed(map, object, offset, start, end - start,
58078022527SKonstantin Belousov 		    prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL |
58178022527SKonstantin Belousov 		    (object != NULL ? MAP_VN_EXEC : 0));
582e3d8f8feSKonstantin Belousov 		if (rv != KERN_SUCCESS) {
583e3d8f8feSKonstantin Belousov 			locked = VOP_ISLOCKED(imgp->vp);
584b249ce48SMateusz Guzik 			VOP_UNLOCK(imgp->vp);
585e5e6093bSAlan Cox 			vm_object_deallocate(object);
586e3d8f8feSKonstantin Belousov 			vn_lock(imgp->vp, locked | LK_RETRY);
587a7cddfedSJake Burkholder 			return (rv);
58878022527SKonstantin Belousov 		} else if (object != NULL) {
58978022527SKonstantin Belousov 			MPASS(imgp->vp->v_object == object);
59078022527SKonstantin Belousov 			VOP_SET_TEXT_CHECKED(imgp->vp);
5913ebc1248SPeter Wemm 		}
5923ebc1248SPeter Wemm 	}
593e383e820SAlan Cox 	return (KERN_SUCCESS);
594e383e820SAlan Cox }
5953ebc1248SPeter Wemm 
5963ebc1248SPeter Wemm static int
5970bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
5981699546dSEdward Tomasz Napierala     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
599e1743d02SSøren Schmidt {
600da61b9a6SAlan Cox 	struct sf_buf *sf;
601e1743d02SSøren Schmidt 	size_t map_len;
602292177e6SAlan Cox 	vm_map_t map;
603292177e6SAlan Cox 	vm_object_t object;
604e020a35fSMark Johnston 	vm_offset_t map_addr;
605fa7dd9c5SMatthew Dillon 	int error, rv, cow;
606e1743d02SSøren Schmidt 	size_t copy_len;
6070bbee4cdSKonstantin Belousov 	vm_ooffset_t file_addr;
60852c24af7SPeter Wemm 
60925ead034SBrian Feldman 	/*
61025ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
61125ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
61225ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
61325ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
61425ead034SBrian Feldman 	 *
61525ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
61625ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
61725ead034SBrian Feldman 	 */
6189bcf2f2dSKonstantin Belousov 	if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) ||
6199bcf2f2dSKonstantin Belousov 	    filsz > memsz) {
62025ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
62125ead034SBrian Feldman 		return (ENOEXEC);
62225ead034SBrian Feldman 	}
62325ead034SBrian Feldman 
624292177e6SAlan Cox 	object = imgp->object;
625292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
626545517f1SEdward Tomasz Napierala 	map_addr = trunc_page((vm_offset_t)vmaddr);
627545517f1SEdward Tomasz Napierala 	file_addr = trunc_page(offset);
628e1743d02SSøren Schmidt 
629e1743d02SSøren Schmidt 	/*
63052c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
63152c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
63252c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
63328e8da65SAlan Cox 	 * choose the second.
63452c24af7SPeter Wemm 	 */
6359bcf2f2dSKonstantin Belousov 	if (filsz == 0)
6369bcf2f2dSKonstantin Belousov 		map_len = 0;
6379bcf2f2dSKonstantin Belousov 	else if (memsz > filsz)
638545517f1SEdward Tomasz Napierala 		map_len = trunc_page(offset + filsz) - file_addr;
63952c24af7SPeter Wemm 	else
640545517f1SEdward Tomasz Napierala 		map_len = round_page(offset + filsz) - file_addr;
64152c24af7SPeter Wemm 
64252c24af7SPeter Wemm 	if (map_len != 0) {
643fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
644fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
645fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
646fa7dd9c5SMatthew Dillon 
64778022527SKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map, object, file_addr,
64878022527SKonstantin Belousov 		    map_addr, map_addr + map_len, prot, cow);
649e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
650a7cddfedSJake Burkholder 			return (EINVAL);
65152c24af7SPeter Wemm 
65252c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
653973d67c4SKonstantin Belousov 		if (memsz == filsz)
654a7cddfedSJake Burkholder 			return (0);
65552c24af7SPeter Wemm 	}
65652c24af7SPeter Wemm 
65752c24af7SPeter Wemm 	/*
65852c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
65952c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
66052c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
66152c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
662e1743d02SSøren Schmidt 	 */
663545517f1SEdward Tomasz Napierala 	copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page(offset +
664545517f1SEdward Tomasz Napierala 	    filsz);
665545517f1SEdward Tomasz Napierala 	map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
666545517f1SEdward Tomasz Napierala 	map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
667e1743d02SSøren Schmidt 
66852c24af7SPeter Wemm 	/* This had damn well better be true! */
6698191d577SPeter Wemm 	if (map_len != 0) {
670e3d8f8feSKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr,
671c547cbb4SAlan Cox 		    map_addr + map_len, prot, 0);
672973d67c4SKonstantin Belousov 		if (rv != KERN_SUCCESS)
673a7cddfedSJake Burkholder 			return (EINVAL);
6748191d577SPeter Wemm 	}
675e1743d02SSøren Schmidt 
67652c24af7SPeter Wemm 	if (copy_len != 0) {
677da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
678da61b9a6SAlan Cox 		if (sf == NULL)
679da61b9a6SAlan Cox 			return (EIO);
680e1743d02SSøren Schmidt 
68152c24af7SPeter Wemm 		/* send the page fragment to user space */
682e020a35fSMark Johnston 		error = copyout((caddr_t)sf_buf_kva(sf), (caddr_t)map_addr,
683e020a35fSMark Johnston 		    copy_len);
684be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
685973d67c4SKonstantin Belousov 		if (error != 0)
68652c24af7SPeter Wemm 			return (error);
68752c24af7SPeter Wemm 	}
688e1743d02SSøren Schmidt 
689e1743d02SSøren Schmidt 	/*
690c547cbb4SAlan Cox 	 * Remove write access to the page if it was only granted by map_insert
691c547cbb4SAlan Cox 	 * to allow copyout.
692e1743d02SSøren Schmidt 	 */
693c547cbb4SAlan Cox 	if ((prot & VM_PROT_WRITE) == 0)
694292177e6SAlan Cox 		vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
6950659df6fSKonstantin Belousov 		    map_len), prot, 0, VM_MAP_PROTECT_SET_PROT);
6968191d577SPeter Wemm 
697ff6f03c7SAlan Cox 	return (0);
698e1743d02SSøren Schmidt }
699e1743d02SSøren Schmidt 
7009bcd7482SEdward Tomasz Napierala static int
7019bcd7482SEdward Tomasz Napierala __elfN(load_sections)(struct image_params *imgp, const Elf_Ehdr *hdr,
7029bcd7482SEdward Tomasz Napierala     const Elf_Phdr *phdr, u_long rbase, u_long *base_addrp)
7039bcd7482SEdward Tomasz Napierala {
7049bcd7482SEdward Tomasz Napierala 	vm_prot_t prot;
7059bcd7482SEdward Tomasz Napierala 	u_long base_addr;
7069bcd7482SEdward Tomasz Napierala 	bool first;
7079bcd7482SEdward Tomasz Napierala 	int error, i;
7089bcd7482SEdward Tomasz Napierala 
709b65ca345SEdward Tomasz Napierala 	ASSERT_VOP_LOCKED(imgp->vp, __func__);
710b65ca345SEdward Tomasz Napierala 
7119bcd7482SEdward Tomasz Napierala 	base_addr = 0;
7129bcd7482SEdward Tomasz Napierala 	first = true;
7139bcd7482SEdward Tomasz Napierala 
7149bcd7482SEdward Tomasz Napierala 	for (i = 0; i < hdr->e_phnum; i++) {
7159bcd7482SEdward Tomasz Napierala 		if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
7169bcd7482SEdward Tomasz Napierala 			continue;
7179bcd7482SEdward Tomasz Napierala 
7189bcd7482SEdward Tomasz Napierala 		/* Loadable segment */
7199bcd7482SEdward Tomasz Napierala 		prot = __elfN(trans_prot)(phdr[i].p_flags);
7209bcd7482SEdward Tomasz Napierala 		error = __elfN(load_section)(imgp, phdr[i].p_offset,
7219bcd7482SEdward Tomasz Napierala 		    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
7229bcd7482SEdward Tomasz Napierala 		    phdr[i].p_memsz, phdr[i].p_filesz, prot);
7239bcd7482SEdward Tomasz Napierala 		if (error != 0)
7249bcd7482SEdward Tomasz Napierala 			return (error);
7259bcd7482SEdward Tomasz Napierala 
7269bcd7482SEdward Tomasz Napierala 		/*
7279bcd7482SEdward Tomasz Napierala 		 * Establish the base address if this is the first segment.
7289bcd7482SEdward Tomasz Napierala 		 */
7299bcd7482SEdward Tomasz Napierala 		if (first) {
7309bcd7482SEdward Tomasz Napierala   			base_addr = trunc_page(phdr[i].p_vaddr + rbase);
7319bcd7482SEdward Tomasz Napierala 			first = false;
7329bcd7482SEdward Tomasz Napierala 		}
7339bcd7482SEdward Tomasz Napierala 	}
7349bcd7482SEdward Tomasz Napierala 
7359bcd7482SEdward Tomasz Napierala 	if (base_addrp != NULL)
7369bcd7482SEdward Tomasz Napierala 		*base_addrp = base_addr;
7379bcd7482SEdward Tomasz Napierala 
7389bcd7482SEdward Tomasz Napierala 	return (0);
7399bcd7482SEdward Tomasz Napierala }
7409bcd7482SEdward Tomasz Napierala 
741c33fe779SJohn Polstra /*
742c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
743c33fe779SJohn Polstra  * or an executable.
744c33fe779SJohn Polstra  *
745c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
746c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
747c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
748c33fe779SJohn Polstra  * where the file was actually loaded.
749c33fe779SJohn Polstra  *
750c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
751c33fe779SJohn Polstra  * the entry point for the loaded file.
752c33fe779SJohn Polstra  */
753e1743d02SSøren Schmidt static int
7543ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
7551699546dSEdward Tomasz Napierala 	u_long *entry)
756e1743d02SSøren Schmidt {
757911c2be0SMark Peek 	struct {
758911c2be0SMark Peek 		struct nameidata nd;
759911c2be0SMark Peek 		struct vattr attr;
760911c2be0SMark Peek 		struct image_params image_params;
761911c2be0SMark Peek 	} *tempdata;
762d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
763d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
764911c2be0SMark Peek 	struct nameidata *nd;
765911c2be0SMark Peek 	struct vattr *attr;
766911c2be0SMark Peek 	struct image_params *imgp;
76778022527SKonstantin Belousov 	u_long rbase;
768c33fe779SJohn Polstra 	u_long base_addr = 0;
7699bcd7482SEdward Tomasz Napierala 	int error;
770e1743d02SSøren Schmidt 
77112bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
77212bc222eSJonathan Anderson 	/*
77312bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
77412bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
77512bc222eSJonathan Anderson 	 */
77612bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
77712bc222eSJonathan Anderson 		return (ECAPMODE);
77812bc222eSJonathan Anderson #endif
77912bc222eSJonathan Anderson 
7801073d17eSKonstantin Belousov 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK | M_ZERO);
781911c2be0SMark Peek 	nd = &tempdata->nd;
782911c2be0SMark Peek 	attr = &tempdata->attr;
783911c2be0SMark Peek 	imgp = &tempdata->image_params;
784911c2be0SMark Peek 
785c8a79999SPeter Wemm 	/*
786c8a79999SPeter Wemm 	 * Initialize part of the common data
787c8a79999SPeter Wemm 	 */
788c8a79999SPeter Wemm 	imgp->proc = p;
789911c2be0SMark Peek 	imgp->attr = attr;
790c8a79999SPeter Wemm 
791f422bc30SJohn Baldwin 	NDINIT(nd, LOOKUP, ISOPEN | FOLLOW | LOCKSHARED | LOCKLEAF,
792f422bc30SJohn Baldwin 	    UIO_SYSSPACE, file, curthread);
793911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
794911c2be0SMark Peek 		nd->ni_vp = NULL;
795e1743d02SSøren Schmidt 		goto fail;
796e1743d02SSøren Schmidt 	}
797911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
798911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
799c8a79999SPeter Wemm 
800e1743d02SSøren Schmidt 	/*
801e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
802e1743d02SSøren Schmidt 	 */
803c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
804373d1a3fSAlan Cox 	if (error)
805c8a79999SPeter Wemm 		goto fail;
806e1743d02SSøren Schmidt 
807c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
808373d1a3fSAlan Cox 	if (error)
809373d1a3fSAlan Cox 		goto fail;
810373d1a3fSAlan Cox 
8118516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
812e1743d02SSøren Schmidt 
813d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
8143ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
815e1743d02SSøren Schmidt 		goto fail;
816c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
817c33fe779SJohn Polstra 		rbase = *addr;
818c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
819c33fe779SJohn Polstra 		rbase = 0;
820c33fe779SJohn Polstra 	else {
821c33fe779SJohn Polstra 		error = ENOEXEC;
822c33fe779SJohn Polstra 		goto fail;
823c33fe779SJohn Polstra 	}
824e1743d02SSøren Schmidt 
825c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
8267de1bc13SKonstantin Belousov 	if (!__elfN(phdr_in_zero_page)(hdr)) {
827c8a79999SPeter Wemm 		error = ENOEXEC;
828e1743d02SSøren Schmidt 		goto fail;
829c8a79999SPeter Wemm 	}
830c8a79999SPeter Wemm 
831d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
83293d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
83393d1c728SKonstantin Belousov 		error = ENOEXEC;
83493d1c728SKonstantin Belousov 		goto fail;
83593d1c728SKonstantin Belousov 	}
836e1743d02SSøren Schmidt 
8379bcd7482SEdward Tomasz Napierala 	error = __elfN(load_sections)(imgp, hdr, phdr, rbase, &base_addr);
838292177e6SAlan Cox 	if (error != 0)
839e1743d02SSøren Schmidt 		goto fail;
8409bcd7482SEdward Tomasz Napierala 
841c33fe779SJohn Polstra 	*addr = base_addr;
842c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
843e1743d02SSøren Schmidt 
844e1743d02SSøren Schmidt fail:
845c8a79999SPeter Wemm 	if (imgp->firstpage)
846c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
8470b2ed1aeSJeff Roberson 
84878022527SKonstantin Belousov 	if (nd->ni_vp) {
84978022527SKonstantin Belousov 		if (imgp->textset)
85078022527SKonstantin Belousov 			VOP_UNSET_TEXT_CHECKED(nd->ni_vp);
851373d1a3fSAlan Cox 		vput(nd->ni_vp);
85278022527SKonstantin Belousov 	}
853911c2be0SMark Peek 	free(tempdata, M_TEMP);
854e1743d02SSøren Schmidt 
855a7cddfedSJake Burkholder 	return (error);
856e1743d02SSøren Schmidt }
857e1743d02SSøren Schmidt 
858fa50a355SKonstantin Belousov static u_long
859fa50a355SKonstantin Belousov __CONCAT(rnd_, __elfN(base))(vm_map_t map __unused, u_long minv, u_long maxv,
860fa50a355SKonstantin Belousov     u_int align)
861fa50a355SKonstantin Belousov {
862fa50a355SKonstantin Belousov 	u_long rbase, res;
863fa50a355SKonstantin Belousov 
864fa50a355SKonstantin Belousov 	MPASS(vm_map_min(map) <= minv);
865fa50a355SKonstantin Belousov 	MPASS(maxv <= vm_map_max(map));
866fa50a355SKonstantin Belousov 	MPASS(minv < maxv);
867fa50a355SKonstantin Belousov 	MPASS(minv + align < maxv);
868fa50a355SKonstantin Belousov 	arc4rand(&rbase, sizeof(rbase), 0);
869fa50a355SKonstantin Belousov 	res = roundup(minv, (u_long)align) + rbase % (maxv - minv);
870fa50a355SKonstantin Belousov 	res &= ~((u_long)align - 1);
871fa50a355SKonstantin Belousov 	if (res >= maxv)
872fa50a355SKonstantin Belousov 		res -= align;
873fa50a355SKonstantin Belousov 	KASSERT(res >= minv,
874fa50a355SKonstantin Belousov 	    ("res %#lx < minv %#lx, maxv %#lx rbase %#lx",
875fa50a355SKonstantin Belousov 	    res, minv, maxv, rbase));
876fa50a355SKonstantin Belousov 	KASSERT(res < maxv,
877fa50a355SKonstantin Belousov 	    ("res %#lx > maxv %#lx, minv %#lx rbase %#lx",
878fa50a355SKonstantin Belousov 	    res, maxv, minv, rbase));
879fa50a355SKonstantin Belousov 	return (res);
880fa50a355SKonstantin Belousov }
881fa50a355SKonstantin Belousov 
88220e1174aSEdward Tomasz Napierala static int
88320e1174aSEdward Tomasz Napierala __elfN(enforce_limits)(struct image_params *imgp, const Elf_Ehdr *hdr,
88420e1174aSEdward Tomasz Napierala     const Elf_Phdr *phdr, u_long et_dyn_addr)
88520e1174aSEdward Tomasz Napierala {
88620e1174aSEdward Tomasz Napierala 	struct vmspace *vmspace;
88720e1174aSEdward Tomasz Napierala 	const char *err_str;
88820e1174aSEdward Tomasz Napierala 	u_long text_size, data_size, total_size, text_addr, data_addr;
88920e1174aSEdward Tomasz Napierala 	u_long seg_size, seg_addr;
89020e1174aSEdward Tomasz Napierala 	int i;
89120e1174aSEdward Tomasz Napierala 
89220e1174aSEdward Tomasz Napierala 	err_str = NULL;
89320e1174aSEdward Tomasz Napierala 	text_size = data_size = total_size = text_addr = data_addr = 0;
89420e1174aSEdward Tomasz Napierala 
89520e1174aSEdward Tomasz Napierala 	for (i = 0; i < hdr->e_phnum; i++) {
89620e1174aSEdward Tomasz Napierala 		if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
89720e1174aSEdward Tomasz Napierala 			continue;
89820e1174aSEdward Tomasz Napierala 
89920e1174aSEdward Tomasz Napierala 		seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
90020e1174aSEdward Tomasz Napierala 		seg_size = round_page(phdr[i].p_memsz +
90120e1174aSEdward Tomasz Napierala 		    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
90220e1174aSEdward Tomasz Napierala 
90320e1174aSEdward Tomasz Napierala 		/*
90420e1174aSEdward Tomasz Napierala 		 * Make the largest executable segment the official
90520e1174aSEdward Tomasz Napierala 		 * text segment and all others data.
90620e1174aSEdward Tomasz Napierala 		 *
90720e1174aSEdward Tomasz Napierala 		 * Note that obreak() assumes that data_addr + data_size == end
90820e1174aSEdward Tomasz Napierala 		 * of data load area, and the ELF file format expects segments
90920e1174aSEdward Tomasz Napierala 		 * to be sorted by address.  If multiple data segments exist,
91020e1174aSEdward Tomasz Napierala 		 * the last one will be used.
91120e1174aSEdward Tomasz Napierala 		 */
91220e1174aSEdward Tomasz Napierala 
91320e1174aSEdward Tomasz Napierala 		if ((phdr[i].p_flags & PF_X) != 0 && text_size < seg_size) {
91420e1174aSEdward Tomasz Napierala 			text_size = seg_size;
91520e1174aSEdward Tomasz Napierala 			text_addr = seg_addr;
91620e1174aSEdward Tomasz Napierala 		} else {
91720e1174aSEdward Tomasz Napierala 			data_size = seg_size;
91820e1174aSEdward Tomasz Napierala 			data_addr = seg_addr;
91920e1174aSEdward Tomasz Napierala 		}
92020e1174aSEdward Tomasz Napierala 		total_size += seg_size;
92120e1174aSEdward Tomasz Napierala 	}
92220e1174aSEdward Tomasz Napierala 
92320e1174aSEdward Tomasz Napierala 	if (data_addr == 0 && data_size == 0) {
92420e1174aSEdward Tomasz Napierala 		data_addr = text_addr;
92520e1174aSEdward Tomasz Napierala 		data_size = text_size;
92620e1174aSEdward Tomasz Napierala 	}
92720e1174aSEdward Tomasz Napierala 
92820e1174aSEdward Tomasz Napierala 	/*
92920e1174aSEdward Tomasz Napierala 	 * Check limits.  It should be safe to check the
93020e1174aSEdward Tomasz Napierala 	 * limits after loading the segments since we do
93120e1174aSEdward Tomasz Napierala 	 * not actually fault in all the segments pages.
93220e1174aSEdward Tomasz Napierala 	 */
93320e1174aSEdward Tomasz Napierala 	PROC_LOCK(imgp->proc);
93420e1174aSEdward Tomasz Napierala 	if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
93520e1174aSEdward Tomasz Napierala 		err_str = "Data segment size exceeds process limit";
93620e1174aSEdward Tomasz Napierala 	else if (text_size > maxtsiz)
93720e1174aSEdward Tomasz Napierala 		err_str = "Text segment size exceeds system limit";
93820e1174aSEdward Tomasz Napierala 	else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
93920e1174aSEdward Tomasz Napierala 		err_str = "Total segment size exceeds process limit";
94020e1174aSEdward Tomasz Napierala 	else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
94120e1174aSEdward Tomasz Napierala 		err_str = "Data segment size exceeds resource limit";
94220e1174aSEdward Tomasz Napierala 	else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
94320e1174aSEdward Tomasz Napierala 		err_str = "Total segment size exceeds resource limit";
94420e1174aSEdward Tomasz Napierala 	PROC_UNLOCK(imgp->proc);
94520e1174aSEdward Tomasz Napierala 	if (err_str != NULL) {
94620e1174aSEdward Tomasz Napierala 		uprintf("%s\n", err_str);
94720e1174aSEdward Tomasz Napierala 		return (ENOMEM);
94820e1174aSEdward Tomasz Napierala 	}
94920e1174aSEdward Tomasz Napierala 
95020e1174aSEdward Tomasz Napierala 	vmspace = imgp->proc->p_vmspace;
95120e1174aSEdward Tomasz Napierala 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
95220e1174aSEdward Tomasz Napierala 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
95320e1174aSEdward Tomasz Napierala 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
95420e1174aSEdward Tomasz Napierala 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
95520e1174aSEdward Tomasz Napierala 
95620e1174aSEdward Tomasz Napierala 	return (0);
95720e1174aSEdward Tomasz Napierala }
95820e1174aSEdward Tomasz Napierala 
95909c78d53SEdward Tomasz Napierala static int
96009c78d53SEdward Tomasz Napierala __elfN(get_interp)(struct image_params *imgp, const Elf_Phdr *phdr,
96109c78d53SEdward Tomasz Napierala     char **interpp, bool *free_interpp)
96209c78d53SEdward Tomasz Napierala {
96309c78d53SEdward Tomasz Napierala 	struct thread *td;
96409c78d53SEdward Tomasz Napierala 	char *interp;
96509c78d53SEdward Tomasz Napierala 	int error, interp_name_len;
96609c78d53SEdward Tomasz Napierala 
96709c78d53SEdward Tomasz Napierala 	KASSERT(phdr->p_type == PT_INTERP,
96809c78d53SEdward Tomasz Napierala 	    ("%s: p_type %u != PT_INTERP", __func__, phdr->p_type));
969b65ca345SEdward Tomasz Napierala 	ASSERT_VOP_LOCKED(imgp->vp, __func__);
97009c78d53SEdward Tomasz Napierala 
97109c78d53SEdward Tomasz Napierala 	td = curthread;
97209c78d53SEdward Tomasz Napierala 
97309c78d53SEdward Tomasz Napierala 	/* Path to interpreter */
97409c78d53SEdward Tomasz Napierala 	if (phdr->p_filesz < 2 || phdr->p_filesz > MAXPATHLEN) {
97509c78d53SEdward Tomasz Napierala 		uprintf("Invalid PT_INTERP\n");
97609c78d53SEdward Tomasz Napierala 		return (ENOEXEC);
97709c78d53SEdward Tomasz Napierala 	}
97809c78d53SEdward Tomasz Napierala 
97909c78d53SEdward Tomasz Napierala 	interp_name_len = phdr->p_filesz;
98009c78d53SEdward Tomasz Napierala 	if (phdr->p_offset > PAGE_SIZE ||
98109c78d53SEdward Tomasz Napierala 	    interp_name_len > PAGE_SIZE - phdr->p_offset) {
9820ddfdc60SKonstantin Belousov 		/*
983f1f81d3bSKonstantin Belousov 		 * The vnode lock might be needed by the pagedaemon to
9840ddfdc60SKonstantin Belousov 		 * clean pages owned by the vnode.  Do not allow sleep
9850ddfdc60SKonstantin Belousov 		 * waiting for memory with the vnode locked, instead
9860ddfdc60SKonstantin Belousov 		 * try non-sleepable allocation first, and if it
9870ddfdc60SKonstantin Belousov 		 * fails, go to the slow path were we drop the lock
988f1f81d3bSKonstantin Belousov 		 * and do M_WAITOK.  A text reference prevents
989f1f81d3bSKonstantin Belousov 		 * modifications to the vnode content.
9900ddfdc60SKonstantin Belousov 		 */
9912d6b8546SKonstantin Belousov 		interp = malloc(interp_name_len + 1, M_TEMP, M_NOWAIT);
9922d6b8546SKonstantin Belousov 		if (interp == NULL) {
993b249ce48SMateusz Guzik 			VOP_UNLOCK(imgp->vp);
99409c78d53SEdward Tomasz Napierala 			interp = malloc(interp_name_len + 1, M_TEMP, M_WAITOK);
99578022527SKonstantin Belousov 			vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
9962d6b8546SKonstantin Belousov 		}
9970ddfdc60SKonstantin Belousov 
99809c78d53SEdward Tomasz Napierala 		error = vn_rdwr(UIO_READ, imgp->vp, interp,
99909c78d53SEdward Tomasz Napierala 		    interp_name_len, phdr->p_offset,
100009c78d53SEdward Tomasz Napierala 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
100109c78d53SEdward Tomasz Napierala 		    NOCRED, NULL, td);
100209c78d53SEdward Tomasz Napierala 		if (error != 0) {
100309c78d53SEdward Tomasz Napierala 			free(interp, M_TEMP);
100409c78d53SEdward Tomasz Napierala 			uprintf("i/o error PT_INTERP %d\n", error);
100509c78d53SEdward Tomasz Napierala 			return (error);
100609c78d53SEdward Tomasz Napierala 		}
100709c78d53SEdward Tomasz Napierala 		interp[interp_name_len] = '\0';
100809c78d53SEdward Tomasz Napierala 
100909c78d53SEdward Tomasz Napierala 		*interpp = interp;
101009c78d53SEdward Tomasz Napierala 		*free_interpp = true;
101109c78d53SEdward Tomasz Napierala 		return (0);
101209c78d53SEdward Tomasz Napierala 	}
101309c78d53SEdward Tomasz Napierala 
101409c78d53SEdward Tomasz Napierala 	interp = __DECONST(char *, imgp->image_header) + phdr->p_offset;
101509c78d53SEdward Tomasz Napierala 	if (interp[interp_name_len - 1] != '\0') {
101609c78d53SEdward Tomasz Napierala 		uprintf("Invalid PT_INTERP\n");
101709c78d53SEdward Tomasz Napierala 		return (ENOEXEC);
101809c78d53SEdward Tomasz Napierala 	}
101909c78d53SEdward Tomasz Napierala 
102009c78d53SEdward Tomasz Napierala 	*interpp = interp;
102109c78d53SEdward Tomasz Napierala 	*free_interpp = false;
102209c78d53SEdward Tomasz Napierala 	return (0);
102309c78d53SEdward Tomasz Napierala }
102409c78d53SEdward Tomasz Napierala 
10259274fb35SEdward Tomasz Napierala static int
10269274fb35SEdward Tomasz Napierala __elfN(load_interp)(struct image_params *imgp, const Elf_Brandinfo *brand_info,
10279274fb35SEdward Tomasz Napierala     const char *interp, u_long *addr, u_long *entry)
10289274fb35SEdward Tomasz Napierala {
10299274fb35SEdward Tomasz Napierala 	char *path;
10309274fb35SEdward Tomasz Napierala 	int error;
10319274fb35SEdward Tomasz Napierala 
10329274fb35SEdward Tomasz Napierala 	if (brand_info->emul_path != NULL &&
10339274fb35SEdward Tomasz Napierala 	    brand_info->emul_path[0] != '\0') {
10349274fb35SEdward Tomasz Napierala 		path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
10359274fb35SEdward Tomasz Napierala 		snprintf(path, MAXPATHLEN, "%s%s",
10369274fb35SEdward Tomasz Napierala 		    brand_info->emul_path, interp);
10379274fb35SEdward Tomasz Napierala 		error = __elfN(load_file)(imgp->proc, path, addr, entry);
10389274fb35SEdward Tomasz Napierala 		free(path, M_TEMP);
10399274fb35SEdward Tomasz Napierala 		if (error == 0)
10409274fb35SEdward Tomasz Napierala 			return (0);
10419274fb35SEdward Tomasz Napierala 	}
10429274fb35SEdward Tomasz Napierala 
10439274fb35SEdward Tomasz Napierala 	if (brand_info->interp_newpath != NULL &&
10449274fb35SEdward Tomasz Napierala 	    (brand_info->interp_path == NULL ||
10459274fb35SEdward Tomasz Napierala 	    strcmp(interp, brand_info->interp_path) == 0)) {
10469274fb35SEdward Tomasz Napierala 		error = __elfN(load_file)(imgp->proc,
10479274fb35SEdward Tomasz Napierala 		    brand_info->interp_newpath, addr, entry);
10489274fb35SEdward Tomasz Napierala 		if (error == 0)
10499274fb35SEdward Tomasz Napierala 			return (0);
10509274fb35SEdward Tomasz Napierala 	}
10519274fb35SEdward Tomasz Napierala 
10529274fb35SEdward Tomasz Napierala 	error = __elfN(load_file)(imgp->proc, interp, addr, entry);
10539274fb35SEdward Tomasz Napierala 	if (error == 0)
10549274fb35SEdward Tomasz Napierala 		return (0);
10559274fb35SEdward Tomasz Napierala 
10569274fb35SEdward Tomasz Napierala 	uprintf("ELF interpreter %s not found, error %d\n", interp, error);
10579274fb35SEdward Tomasz Napierala 	return (error);
10589274fb35SEdward Tomasz Napierala }
10599274fb35SEdward Tomasz Napierala 
1060fa50a355SKonstantin Belousov /*
1061fa50a355SKonstantin Belousov  * Impossible et_dyn_addr initial value indicating that the real base
1062fa50a355SKonstantin Belousov  * must be calculated later with some randomization applied.
1063fa50a355SKonstantin Belousov  */
1064fa50a355SKonstantin Belousov #define	ET_DYN_ADDR_RAND	1
1065fa50a355SKonstantin Belousov 
1066303b270bSEivind Eklund static int
10673ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
1068e1743d02SSøren Schmidt {
10696c775eb6SKonstantin Belousov 	struct thread *td;
10706c775eb6SKonstantin Belousov 	const Elf_Ehdr *hdr;
107132c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
1072e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
10735856e12eSJohn Dyson 	struct vmspace *vmspace;
1074fa50a355SKonstantin Belousov 	vm_map_t map;
10759274fb35SEdward Tomasz Napierala 	char *interp;
1076d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
10775fe3ed62SJake Burkholder 	struct sysentvec *sv;
107820e1174aSEdward Tomasz Napierala 	u_long addr, baddr, et_dyn_addr, entry, proghdr;
1079fa50a355SKonstantin Belousov 	u_long maxalign, mapsz, maxv, maxv1;
1080cefb93f2SKonstantin Belousov 	uint32_t fctl0;
10816c775eb6SKonstantin Belousov 	int32_t osrel;
108209c78d53SEdward Tomasz Napierala 	bool free_interp;
10839274fb35SEdward Tomasz Napierala 	int error, i, n;
10846c775eb6SKonstantin Belousov 
10856c775eb6SKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
1086e1743d02SSøren Schmidt 
1087e1743d02SSøren Schmidt 	/*
1088e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
1089900b28f9SMaxim Sobolev 	 *
1090900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
1091900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
1092e1743d02SSøren Schmidt 	 */
1093900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
1094900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
1095a7cddfedSJake Burkholder 		return (-1);
1096e1743d02SSøren Schmidt 
1097e1743d02SSøren Schmidt 	/*
1098e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
1099e1743d02SSøren Schmidt 	 * detected an ELF file.
1100e1743d02SSøren Schmidt 	 */
1101e1743d02SSøren Schmidt 
11027de1bc13SKonstantin Belousov 	if (!__elfN(phdr_in_zero_page)(hdr)) {
11036b16d664SEd Maste 		uprintf("Program headers not in the first page\n");
1104a7cddfedSJake Burkholder 		return (ENOEXEC);
1105e1743d02SSøren Schmidt 	}
110652c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
11076b16d664SEd Maste 	if (!aligned(phdr, Elf_Addr)) {
11086b16d664SEd Maste 		uprintf("Unaligned program headers\n");
110993d1c728SKonstantin Belousov 		return (ENOEXEC);
11106b16d664SEd Maste 	}
11116c775eb6SKonstantin Belousov 
11126c775eb6SKonstantin Belousov 	n = error = 0;
11137564c4adSKonstantin Belousov 	baddr = 0;
11146c775eb6SKonstantin Belousov 	osrel = 0;
1115cefb93f2SKonstantin Belousov 	fctl0 = 0;
11166c775eb6SKonstantin Belousov 	entry = proghdr = 0;
11179274fb35SEdward Tomasz Napierala 	interp = NULL;
111809c78d53SEdward Tomasz Napierala 	free_interp = false;
11196c775eb6SKonstantin Belousov 	td = curthread;
1120fa50a355SKonstantin Belousov 	maxalign = PAGE_SIZE;
1121fa50a355SKonstantin Belousov 	mapsz = 0;
11226c775eb6SKonstantin Belousov 
11235fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
1124291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
1125291c06a1SKonstantin Belousov 		case PT_LOAD:
11267564c4adSKonstantin Belousov 			if (n == 0)
11277564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
1128fa50a355SKonstantin Belousov 			if (phdr[i].p_align > maxalign)
1129fa50a355SKonstantin Belousov 				maxalign = phdr[i].p_align;
1130fa50a355SKonstantin Belousov 			mapsz += phdr[i].p_memsz;
11317564c4adSKonstantin Belousov 			n++;
11329bcd7482SEdward Tomasz Napierala 
11339bcd7482SEdward Tomasz Napierala 			/*
11349bcd7482SEdward Tomasz Napierala 			 * If this segment contains the program headers,
11359bcd7482SEdward Tomasz Napierala 			 * remember their virtual address for the AT_PHDR
11369bcd7482SEdward Tomasz Napierala 			 * aux entry. Static binaries don't usually include
11379bcd7482SEdward Tomasz Napierala 			 * a PT_PHDR entry.
11389bcd7482SEdward Tomasz Napierala 			 */
11399bcd7482SEdward Tomasz Napierala 			if (phdr[i].p_offset == 0 &&
11409bcd7482SEdward Tomasz Napierala 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
11419bcd7482SEdward Tomasz Napierala 				<= phdr[i].p_filesz)
11429bcd7482SEdward Tomasz Napierala 				proghdr = phdr[i].p_vaddr + hdr->e_phoff;
1143291c06a1SKonstantin Belousov 			break;
1144291c06a1SKonstantin Belousov 		case PT_INTERP:
1145e5e6093bSAlan Cox 			/* Path to interpreter */
1146d3ee0a15SJonathan T. Looney 			if (interp != NULL) {
1147d3ee0a15SJonathan T. Looney 				uprintf("Multiple PT_INTERP headers\n");
1148d3ee0a15SJonathan T. Looney 				error = ENOEXEC;
1149d3ee0a15SJonathan T. Looney 				goto ret;
1150d3ee0a15SJonathan T. Looney 			}
115109c78d53SEdward Tomasz Napierala 			error = __elfN(get_interp)(imgp, &phdr[i], &interp,
115209c78d53SEdward Tomasz Napierala 			    &free_interp);
115309c78d53SEdward Tomasz Napierala 			if (error != 0)
11546c775eb6SKonstantin Belousov 				goto ret;
1155291c06a1SKonstantin Belousov 			break;
1156291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
1157291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
1158291c06a1SKonstantin Belousov 				imgp->stack_prot =
1159291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
1160316b3843SKonstantin Belousov 			imgp->stack_sz = phdr[i].p_memsz;
1161291c06a1SKonstantin Belousov 			break;
11629bcd7482SEdward Tomasz Napierala 		case PT_PHDR: 	/* Program header table info */
11639bcd7482SEdward Tomasz Napierala 			proghdr = phdr[i].p_vaddr;
11649bcd7482SEdward Tomasz Napierala 			break;
11653ebc1248SPeter Wemm 		}
11663ebc1248SPeter Wemm 	}
11673ebc1248SPeter Wemm 
116809c78d53SEdward Tomasz Napierala 	brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel, &fctl0);
11695fe3ed62SJake Burkholder 	if (brand_info == NULL) {
11705fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
11715fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
11726c775eb6SKonstantin Belousov 		error = ENOEXEC;
11736c775eb6SKonstantin Belousov 		goto ret;
11743ebc1248SPeter Wemm 	}
1175fa50a355SKonstantin Belousov 	sv = brand_info->sysvec;
117677ebe276SEd Maste 	et_dyn_addr = 0;
1177ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
11786b16d664SEd Maste 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
11796b16d664SEd Maste 			uprintf("Cannot execute shared object\n");
11806c775eb6SKonstantin Belousov 			error = ENOEXEC;
11816c775eb6SKonstantin Belousov 			goto ret;
11826b16d664SEd Maste 		}
11837564c4adSKonstantin Belousov 		/*
11847564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
11857564c4adSKonstantin Belousov 		 * non-zero for some reason.
11867564c4adSKonstantin Belousov 		 */
1187fa50a355SKonstantin Belousov 		if (baddr == 0) {
1188fa50a355SKonstantin Belousov 			if ((sv->sv_flags & SV_ASLR) == 0 ||
1189fa50a355SKonstantin Belousov 			    (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0)
119095aafd69SKonstantin Belousov 				et_dyn_addr = __elfN(pie_base);
1191fa50a355SKonstantin Belousov 			else if ((__elfN(pie_aslr_enabled) &&
1192fa50a355SKonstantin Belousov 			    (imgp->proc->p_flag2 & P2_ASLR_DISABLE) == 0) ||
1193fa50a355SKonstantin Belousov 			    (imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0)
1194fa50a355SKonstantin Belousov 				et_dyn_addr = ET_DYN_ADDR_RAND;
1195fa50a355SKonstantin Belousov 			else
119695aafd69SKonstantin Belousov 				et_dyn_addr = __elfN(pie_base);
119777ebe276SEd Maste 		}
1198fa50a355SKonstantin Belousov 	}
11993ebc1248SPeter Wemm 
120060bb3943SAlan Cox 	/*
120160bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
120260bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
120360bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
120460bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
120560bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
120660bb3943SAlan Cox 	 * v_usecount may become zero.
12071dfab802SAlan Cox 	 *
12081dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
12091dfab802SAlan Cox 	 * the vnode is unlocked.
121060bb3943SAlan Cox 	 */
1211b249ce48SMateusz Guzik 	VOP_UNLOCK(imgp->vp);
121260bb3943SAlan Cox 
1213fa50a355SKonstantin Belousov 	/*
1214fa50a355SKonstantin Belousov 	 * Decide whether to enable randomization of user mappings.
1215fa50a355SKonstantin Belousov 	 * First, reset user preferences for the setid binaries.
1216fa50a355SKonstantin Belousov 	 * Then, account for the support of the randomization by the
1217fa50a355SKonstantin Belousov 	 * ABI, by user preferences, and make special treatment for
1218fa50a355SKonstantin Belousov 	 * PIE binaries.
1219fa50a355SKonstantin Belousov 	 */
1220fa50a355SKonstantin Belousov 	if (imgp->credential_setid) {
1221fa50a355SKonstantin Belousov 		PROC_LOCK(imgp->proc);
1222fa50a355SKonstantin Belousov 		imgp->proc->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE);
1223fa50a355SKonstantin Belousov 		PROC_UNLOCK(imgp->proc);
1224fa50a355SKonstantin Belousov 	}
1225fa50a355SKonstantin Belousov 	if ((sv->sv_flags & SV_ASLR) == 0 ||
1226fa50a355SKonstantin Belousov 	    (imgp->proc->p_flag2 & P2_ASLR_DISABLE) != 0 ||
1227fa50a355SKonstantin Belousov 	    (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) {
1228fa50a355SKonstantin Belousov 		KASSERT(et_dyn_addr != ET_DYN_ADDR_RAND,
1229fa50a355SKonstantin Belousov 		    ("et_dyn_addr == RAND and !ASLR"));
1230fa50a355SKonstantin Belousov 	} else if ((imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0 ||
1231fa50a355SKonstantin Belousov 	    (__elfN(aslr_enabled) && hdr->e_type == ET_EXEC) ||
1232fa50a355SKonstantin Belousov 	    et_dyn_addr == ET_DYN_ADDR_RAND) {
1233fa50a355SKonstantin Belousov 		imgp->map_flags |= MAP_ASLR;
1234fa50a355SKonstantin Belousov 		/*
1235fa50a355SKonstantin Belousov 		 * If user does not care about sbrk, utilize the bss
1236fa50a355SKonstantin Belousov 		 * grow region for mappings as well.  We can select
1237fa50a355SKonstantin Belousov 		 * the base for the image anywere and still not suffer
1238fa50a355SKonstantin Belousov 		 * from the fragmentation.
1239fa50a355SKonstantin Belousov 		 */
1240fa50a355SKonstantin Belousov 		if (!__elfN(aslr_honor_sbrk) ||
1241fa50a355SKonstantin Belousov 		    (imgp->proc->p_flag2 & P2_ASLR_IGNSTART) != 0)
1242fa50a355SKonstantin Belousov 			imgp->map_flags |= MAP_ASLR_IGNSTART;
1243fa50a355SKonstantin Belousov 	}
1244fa50a355SKonstantin Belousov 
12452e1c94aaSKonstantin Belousov 	if (!__elfN(allow_wx) && (fctl0 & NT_FREEBSD_FCTL_WXNEEDED) == 0)
12462e1c94aaSKonstantin Belousov 		imgp->map_flags |= MAP_WXORX;
12472e1c94aaSKonstantin Belousov 
124889b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
1249fa50a355SKonstantin Belousov 	vmspace = imgp->proc->p_vmspace;
1250fa50a355SKonstantin Belousov 	map = &vmspace->vm_map;
1251fa50a355SKonstantin Belousov 
125219059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
1253e1743d02SSøren Schmidt 
1254fa50a355SKonstantin Belousov 	maxv = vm_map_max(map) - lim_max(td, RLIMIT_STACK);
1255fa50a355SKonstantin Belousov 	if (et_dyn_addr == ET_DYN_ADDR_RAND) {
1256fa50a355SKonstantin Belousov 		KASSERT((map->flags & MAP_ASLR) != 0,
1257fa50a355SKonstantin Belousov 		    ("ET_DYN_ADDR_RAND but !MAP_ASLR"));
1258fa50a355SKonstantin Belousov 		et_dyn_addr = __CONCAT(rnd_, __elfN(base))(map,
1259fa50a355SKonstantin Belousov 		    vm_map_min(map) + mapsz + lim_max(td, RLIMIT_DATA),
1260fa50a355SKonstantin Belousov 		    /* reserve half of the address space to interpreter */
1261fa50a355SKonstantin Belousov 		    maxv / 2, 1UL << flsl(maxalign));
1262fa50a355SKonstantin Belousov 	}
1263fa50a355SKonstantin Belousov 
126478022527SKonstantin Belousov 	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
12656c775eb6SKonstantin Belousov 	if (error != 0)
12666c775eb6SKonstantin Belousov 		goto ret;
126760bb3943SAlan Cox 
12689bcd7482SEdward Tomasz Napierala 	error = __elfN(load_sections)(imgp, hdr, phdr, et_dyn_addr, NULL);
1269292177e6SAlan Cox 	if (error != 0)
12706c775eb6SKonstantin Belousov 		goto ret;
1271e1743d02SSøren Schmidt 
127220e1174aSEdward Tomasz Napierala 	error = __elfN(enforce_limits)(imgp, hdr, phdr, et_dyn_addr);
127320e1174aSEdward Tomasz Napierala 	if (error != 0)
127420e1174aSEdward Tomasz Napierala 		goto ret;
1275cac45152SMatthew Dillon 
1276920acedbSNathan Whitehorn 	entry = (u_long)hdr->e_entry + et_dyn_addr;
1277920acedbSNathan Whitehorn 
1278cac45152SMatthew Dillon 	/*
1279c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
1280c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
1281c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
1282c460ac3aSPeter Wemm 	 * its maximum allowed size.
1283c460ac3aSPeter Wemm 	 */
12846c775eb6SKonstantin Belousov 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td,
1285292177e6SAlan Cox 	    RLIMIT_DATA));
1286fa50a355SKonstantin Belousov 	if ((map->flags & MAP_ASLR) != 0) {
1287fa50a355SKonstantin Belousov 		maxv1 = maxv / 2 + addr / 2;
1288fa50a355SKonstantin Belousov 		MPASS(maxv1 >= addr);	/* No overflow */
1289fa50a355SKonstantin Belousov 		map->anon_loc = __CONCAT(rnd_, __elfN(base))(map, addr, maxv1,
129041032835SJason A. Harmening 		    (MAXPAGESIZES > 1 && pagesizes[1] != 0) ?
129141032835SJason A. Harmening 		    pagesizes[1] : pagesizes[0]);
1292fa50a355SKonstantin Belousov 	} else {
1293fa50a355SKonstantin Belousov 		map->anon_loc = addr;
1294fa50a355SKonstantin Belousov 	}
1295e1743d02SSøren Schmidt 
1296ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
1297ea5a2b2eSSøren Schmidt 
129860bb3943SAlan Cox 	if (interp != NULL) {
1299b249ce48SMateusz Guzik 		VOP_UNLOCK(imgp->vp);
1300fa50a355SKonstantin Belousov 		if ((map->flags & MAP_ASLR) != 0) {
130141032835SJason A. Harmening 			/* Assume that interpreter fits into 1/4 of AS */
1302fa50a355SKonstantin Belousov 			maxv1 = maxv / 2 + addr / 2;
1303fa50a355SKonstantin Belousov 			MPASS(maxv1 >= addr);	/* No overflow */
1304fa50a355SKonstantin Belousov 			addr = __CONCAT(rnd_, __elfN(base))(map, addr,
1305fa50a355SKonstantin Belousov 			    maxv1, PAGE_SIZE);
1306fa50a355SKonstantin Belousov 		}
13079274fb35SEdward Tomasz Napierala 		error = __elfN(load_interp)(imgp, brand_info, interp, &addr,
13081699546dSEdward Tomasz Napierala 		    &imgp->entry_addr);
130978022527SKonstantin Belousov 		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
13109274fb35SEdward Tomasz Napierala 		if (error != 0)
13116c775eb6SKonstantin Belousov 			goto ret;
131295c807cfSRobert Watson 	} else
13137564c4adSKonstantin Belousov 		addr = et_dyn_addr;
1314ea5a2b2eSSøren Schmidt 
1315e1743d02SSøren Schmidt 	/*
1316e3532331SJohn Baldwin 	 * Construct auxargs table (used by the copyout_auxargs routine)
1317e1743d02SSøren Schmidt 	 */
13182d6b8546SKonstantin Belousov 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_NOWAIT);
13192d6b8546SKonstantin Belousov 	if (elf_auxargs == NULL) {
1320b249ce48SMateusz Guzik 		VOP_UNLOCK(imgp->vp);
1321a163d034SWarner Losh 		elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
132278022527SKonstantin Belousov 		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
13232d6b8546SKonstantin Belousov 	}
1324e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
13259bcd7482SEdward Tomasz Napierala 	elf_auxargs->phdr = proghdr + et_dyn_addr;
1326e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
1327e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
1328e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
1329e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
1330e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
1331e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
1332d36eec69SWarner Losh 	elf_auxargs->hdr_eflags = hdr->e_flags;
1333e1743d02SSøren Schmidt 
1334e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
1335e1743d02SSøren Schmidt 	imgp->interpreted = 0;
1336a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
133732c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
1338cefb93f2SKonstantin Belousov 	imgp->proc->p_fctl0 = fctl0;
1339885f13dcSJohn Baldwin 	imgp->proc->p_elf_machine = hdr->e_machine;
1340885f13dcSJohn Baldwin 	imgp->proc->p_elf_flags = hdr->e_flags;
1341f231de47SKonstantin Belousov 
13426c775eb6SKonstantin Belousov ret:
134309c78d53SEdward Tomasz Napierala 	if (free_interp)
134409c78d53SEdward Tomasz Napierala 		free(interp, M_TEMP);
1345a7cddfedSJake Burkholder 	return (error);
1346e1743d02SSøren Schmidt }
1347e1743d02SSøren Schmidt 
1348a360a43dSJake Burkholder #define	suword __CONCAT(suword, __ELF_WORD_SIZE)
13493ebc1248SPeter Wemm 
135003b0d68cSJohn Baldwin int
1351d8010b11SJohn Baldwin __elfN(freebsd_copyout_auxargs)(struct image_params *imgp, uintptr_t base)
1352e1743d02SSøren Schmidt {
1353ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
13545f77b8a8SBrooks Davis 	Elf_Auxinfo *argarray, *pos;
135503b0d68cSJohn Baldwin 	int error;
1356e1743d02SSøren Schmidt 
13575f77b8a8SBrooks Davis 	argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP,
13585f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
1359e1743d02SSøren Schmidt 
136035c2a5a8SWarner Losh 	if (args->execfd != -1)
1361e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1362e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1363e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1364e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1365e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1366e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1367e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1368e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
1369d36eec69SWarner Losh 	AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
13703ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
1371b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, AT_EXECPATH, imgp->execpathp);
1372b96bd95bSIan Lepore 	AUXARGS_ENTRY(pos, AT_OSRELDATE,
1373b96bd95bSIan Lepore 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
1374ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1375b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, AT_CANARY, imgp->canary);
1376ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1377ee235befSKonstantin Belousov 	}
1378ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1379ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1380b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, AT_PAGESIZES, imgp->pagesizes);
1381ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1382ee235befSKonstantin Belousov 	}
1383aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1384aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1385aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1386aea81038SKonstantin Belousov 	}
138726d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
138826d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
138926d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1390c2f37b92SJohn Baldwin 	if (imgp->sysent->sv_hwcap != NULL)
1391c2f37b92SJohn Baldwin 		AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap);
1392904d8c49SMichal Meloun 	if (imgp->sysent->sv_hwcap2 != NULL)
1393904d8c49SMichal Meloun 		AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1394944cf37bSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_BSDFLAGS, __elfN(sigfastblock) ?
1395944cf37bSKonstantin Belousov 	    ELF_BSDF_SIGFASTBLK : 0);
13969df1c38bSBrooks Davis 	AUXARGS_ENTRY(pos, AT_ARGC, imgp->args->argc);
13979df1c38bSBrooks Davis 	AUXARGS_ENTRY_PTR(pos, AT_ARGV, imgp->argv);
13989df1c38bSBrooks Davis 	AUXARGS_ENTRY(pos, AT_ENVC, imgp->args->envc);
13999df1c38bSBrooks Davis 	AUXARGS_ENTRY_PTR(pos, AT_ENVV, imgp->envv);
14009df1c38bSBrooks Davis 	AUXARGS_ENTRY_PTR(pos, AT_PS_STRINGS, imgp->ps_strings);
1401f8e8a06dSConrad Meyer 	if (imgp->sysent->sv_fxrng_gen_base != 0)
1402f8e8a06dSConrad Meyer 		AUXARGS_ENTRY(pos, AT_FXRNG, imgp->sysent->sv_fxrng_gen_base);
1403e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1404e1743d02SSøren Schmidt 
1405e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1406e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1407d8b2f079SBrooks Davis 	KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs"));
14085f77b8a8SBrooks Davis 
1409d8010b11SJohn Baldwin 	error = copyout(argarray, (void *)base, sizeof(*argarray) * AT_COUNT);
14105f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
141103b0d68cSJohn Baldwin 	return (error);
1412e3532331SJohn Baldwin }
1413e1743d02SSøren Schmidt 
1414e3532331SJohn Baldwin int
141531174518SJohn Baldwin __elfN(freebsd_fixup)(uintptr_t *stack_base, struct image_params *imgp)
1416e3532331SJohn Baldwin {
1417e3532331SJohn Baldwin 	Elf_Addr *base;
1418e3532331SJohn Baldwin 
1419e3532331SJohn Baldwin 	base = (Elf_Addr *)*stack_base;
14203ebc1248SPeter Wemm 	base--;
14215f77b8a8SBrooks Davis 	if (suword(base, imgp->args->argc) == -1)
14225f77b8a8SBrooks Davis 		return (EFAULT);
142331174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
1424a7cddfedSJake Burkholder 	return (0);
1425e1743d02SSøren Schmidt }
1426e1743d02SSøren Schmidt 
1427e1743d02SSøren Schmidt /*
14288c64af4fSJohn Polstra  * Code for generating ELF core dumps.
14298c64af4fSJohn Polstra  */
14308c64af4fSJohn Polstra 
14314d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
14320ff27d31SJohn Polstra 
14330ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
14340ff27d31SJohn Polstra struct phdr_closure {
14350ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
14360ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
14370ff27d31SJohn Polstra };
14380ff27d31SJohn Polstra 
14390ff27d31SJohn Polstra /* Closure for cb_size_segment(). */
14400ff27d31SJohn Polstra struct sseg_closure {
14410ff27d31SJohn Polstra 	int count;		/* Count of writable segments. */
14420ff27d31SJohn Polstra 	size_t size;		/* Total size of all writable segments. */
14430ff27d31SJohn Polstra };
14440ff27d31SJohn Polstra 
1445bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1446bd390213SMikolaj Golub 
1447bd390213SMikolaj Golub struct note_info {
1448bd390213SMikolaj Golub 	int		type;		/* Note type. */
1449bd390213SMikolaj Golub 	outfunc_t 	outfunc; 	/* Output function. */
1450bd390213SMikolaj Golub 	void		*outarg;	/* Argument for the output function. */
1451bd390213SMikolaj Golub 	size_t		outsize;	/* Output size. */
1452bd390213SMikolaj Golub 	TAILQ_ENTRY(note_info) link;	/* Link to the next note info. */
1453bd390213SMikolaj Golub };
1454bd390213SMikolaj Golub 
1455bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info);
1456bd390213SMikolaj Golub 
1457aa14e9b7SMark Johnston /* Coredump output parameters. */
1458aa14e9b7SMark Johnston struct coredump_params {
1459aa14e9b7SMark Johnston 	off_t		offset;
1460aa14e9b7SMark Johnston 	struct ucred	*active_cred;
1461aa14e9b7SMark Johnston 	struct ucred	*file_cred;
1462aa14e9b7SMark Johnston 	struct thread	*td;
1463aa14e9b7SMark Johnston 	struct vnode	*vp;
146478f57a9cSMark Johnston 	struct compressor *comp;
1465aa14e9b7SMark Johnston };
1466aa14e9b7SMark Johnston 
146778f57a9cSMark Johnston extern int compress_user_cores;
146878f57a9cSMark Johnston extern int compress_user_cores_level;
146978f57a9cSMark Johnston 
14704d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
14714d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
1472c468ff88SAndriy Gapon static int core_write(struct coredump_params *, const void *, size_t, off_t,
1473f31695ccSMark Johnston     enum uio_seg, size_t *);
1474*5bc3c617SKonstantin Belousov static void each_dumpable_segment(struct thread *, segment_callback, void *,
1475*5bc3c617SKonstantin Belousov     int);
1476aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1477*5bc3c617SKonstantin Belousov     struct note_info_list *, size_t, int);
1478bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1479bd390213SMikolaj Golub     size_t *);
1480*5bc3c617SKonstantin Belousov static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t, int);
1481bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *);
1482bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1483bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int);
1484bd390213SMikolaj Golub 
1485bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1486bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1487bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1488bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1489bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
149086be94fcSTycho Nightingale static void __elfN(note_ptlwpinfo)(void *, struct sbuf *, size_t *);
1491f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1492f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1493f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1494f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *);
1495f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *);
1496f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1497f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1498f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *);
1499f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
15008c64af4fSJohn Polstra 
1501aa14e9b7SMark Johnston /*
1502aa14e9b7SMark Johnston  * Write out a core segment to the compression stream.
1503aa14e9b7SMark Johnston  */
1504e7228204SAlfred Perlstein static int
1505aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
1506aa14e9b7SMark Johnston {
1507aa14e9b7SMark Johnston 	u_int chunk_len;
1508e7228204SAlfred Perlstein 	int error;
1509aa14e9b7SMark Johnston 
1510aa14e9b7SMark Johnston 	while (len > 0) {
1511aa14e9b7SMark Johnston 		chunk_len = MIN(len, CORE_BUF_SIZE);
1512c468ff88SAndriy Gapon 
1513c468ff88SAndriy Gapon 		/*
1514c468ff88SAndriy Gapon 		 * We can get EFAULT error here.
1515c468ff88SAndriy Gapon 		 * In that case zero out the current chunk of the segment.
1516c468ff88SAndriy Gapon 		 */
1517c468ff88SAndriy Gapon 		error = copyin(base, buf, chunk_len);
1518c468ff88SAndriy Gapon 		if (error != 0)
1519c468ff88SAndriy Gapon 			bzero(buf, chunk_len);
152078f57a9cSMark Johnston 		error = compressor_write(p->comp, buf, chunk_len);
1521aa14e9b7SMark Johnston 		if (error != 0)
1522aa14e9b7SMark Johnston 			break;
1523aa14e9b7SMark Johnston 		base += chunk_len;
1524aa14e9b7SMark Johnston 		len -= chunk_len;
1525e7228204SAlfred Perlstein 	}
1526e7228204SAlfred Perlstein 	return (error);
1527e7228204SAlfred Perlstein }
1528e7228204SAlfred Perlstein 
1529aa14e9b7SMark Johnston static int
153078f57a9cSMark Johnston core_compressed_write(void *base, size_t len, off_t offset, void *arg)
1531aa14e9b7SMark Johnston {
1532aa14e9b7SMark Johnston 
1533aa14e9b7SMark Johnston 	return (core_write((struct coredump_params *)arg, base, len, offset,
1534f31695ccSMark Johnston 	    UIO_SYSSPACE, NULL));
1535aa14e9b7SMark Johnston }
1536aa14e9b7SMark Johnston 
1537aa14e9b7SMark Johnston static int
1538c468ff88SAndriy Gapon core_write(struct coredump_params *p, const void *base, size_t len,
1539f31695ccSMark Johnston     off_t offset, enum uio_seg seg, size_t *resid)
1540aa14e9b7SMark Johnston {
1541aa14e9b7SMark Johnston 
1542c468ff88SAndriy Gapon 	return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base),
1543c468ff88SAndriy Gapon 	    len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
1544f31695ccSMark Johnston 	    p->active_cred, p->file_cred, resid, p->td));
1545aa14e9b7SMark Johnston }
1546aa14e9b7SMark Johnston 
1547aa14e9b7SMark Johnston static int
1548f31695ccSMark Johnston core_output(char *base, size_t len, off_t offset, struct coredump_params *p,
1549aa14e9b7SMark Johnston     void *tmpbuf)
1550aa14e9b7SMark Johnston {
1551f31695ccSMark Johnston 	vm_map_t map;
1552f31695ccSMark Johnston 	struct mount *mp;
1553f31695ccSMark Johnston 	size_t resid, runlen;
1554c468ff88SAndriy Gapon 	int error;
1555f31695ccSMark Johnston 	bool success;
1556f31695ccSMark Johnston 
1557f31695ccSMark Johnston 	KASSERT((uintptr_t)base % PAGE_SIZE == 0,
1558c88285c5SMark Johnston 	    ("%s: user address %p is not page-aligned", __func__, base));
1559aa14e9b7SMark Johnston 
156078f57a9cSMark Johnston 	if (p->comp != NULL)
1561aa14e9b7SMark Johnston 		return (compress_chunk(p, base, tmpbuf, len));
156278f57a9cSMark Johnston 
1563f31695ccSMark Johnston 	map = &p->td->td_proc->p_vmspace->vm_map;
1564f31695ccSMark Johnston 	for (; len > 0; base += runlen, offset += runlen, len -= runlen) {
1565c468ff88SAndriy Gapon 		/*
1566f31695ccSMark Johnston 		 * Attempt to page in all virtual pages in the range.  If a
1567f31695ccSMark Johnston 		 * virtual page is not backed by the pager, it is represented as
1568f31695ccSMark Johnston 		 * a hole in the file.  This can occur with zero-filled
1569f31695ccSMark Johnston 		 * anonymous memory or truncated files, for example.
1570c468ff88SAndriy Gapon 		 */
1571f31695ccSMark Johnston 		for (runlen = 0; runlen < len; runlen += PAGE_SIZE) {
1572f31695ccSMark Johnston 			error = vm_fault(map, (uintptr_t)base + runlen,
1573f31695ccSMark Johnston 			    VM_PROT_READ, VM_FAULT_NOFILL, NULL);
1574f31695ccSMark Johnston 			if (runlen == 0)
1575f31695ccSMark Johnston 				success = error == KERN_SUCCESS;
1576f31695ccSMark Johnston 			else if ((error == KERN_SUCCESS) != success)
1577f31695ccSMark Johnston 				break;
1578f31695ccSMark Johnston 		}
1579f31695ccSMark Johnston 
1580f31695ccSMark Johnston 		if (success) {
1581f31695ccSMark Johnston 			error = core_write(p, base, runlen, offset,
1582f31695ccSMark Johnston 			    UIO_USERSPACE, &resid);
1583f31695ccSMark Johnston 			if (error != 0) {
1584f31695ccSMark Johnston 				if (error != EFAULT)
1585f31695ccSMark Johnston 					break;
1586c468ff88SAndriy Gapon 
1587c468ff88SAndriy Gapon 				/*
1588f31695ccSMark Johnston 				 * EFAULT may be returned if the user mapping
1589f31695ccSMark Johnston 				 * could not be accessed, e.g., because a mapped
1590f31695ccSMark Johnston 				 * file has been truncated.  Skip the page if no
1591f31695ccSMark Johnston 				 * progress was made, to protect against a
1592f31695ccSMark Johnston 				 * hypothetical scenario where vm_fault() was
1593f31695ccSMark Johnston 				 * successful but core_write() returns EFAULT
1594f31695ccSMark Johnston 				 * anyway.
1595c468ff88SAndriy Gapon 				 */
1596f31695ccSMark Johnston 				runlen -= resid;
1597f31695ccSMark Johnston 				if (runlen == 0) {
1598f31695ccSMark Johnston 					success = false;
1599f31695ccSMark Johnston 					runlen = PAGE_SIZE;
1600f31695ccSMark Johnston 				}
1601f31695ccSMark Johnston 			}
1602f31695ccSMark Johnston 		}
1603f31695ccSMark Johnston 		if (!success) {
1604f31695ccSMark Johnston 			error = vn_start_write(p->vp, &mp, V_WAIT);
1605f31695ccSMark Johnston 			if (error != 0)
1606f31695ccSMark Johnston 				break;
1607f31695ccSMark Johnston 			vn_lock(p->vp, LK_EXCLUSIVE | LK_RETRY);
1608f31695ccSMark Johnston 			error = vn_truncate_locked(p->vp, offset + runlen,
1609f31695ccSMark Johnston 			    false, p->td->td_ucred);
1610f31695ccSMark Johnston 			VOP_UNLOCK(p->vp);
1611f31695ccSMark Johnston 			vn_finished_write(mp);
1612f31695ccSMark Johnston 			if (error != 0)
1613f31695ccSMark Johnston 				break;
1614f31695ccSMark Johnston 		}
1615c468ff88SAndriy Gapon 	}
1616c468ff88SAndriy Gapon 	return (error);
1617aa14e9b7SMark Johnston }
1618bd390213SMikolaj Golub 
1619bd390213SMikolaj Golub /*
1620bd390213SMikolaj Golub  * Drain into a core file.
1621bd390213SMikolaj Golub  */
1622bd390213SMikolaj Golub static int
1623bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len)
1624bd390213SMikolaj Golub {
1625aa14e9b7SMark Johnston 	struct coredump_params *p;
1626f1fca82eSMikolaj Golub 	int error, locked;
1627bd390213SMikolaj Golub 
1628aa14e9b7SMark Johnston 	p = (struct coredump_params *)arg;
1629f1fca82eSMikolaj Golub 
1630f1fca82eSMikolaj Golub 	/*
1631f1fca82eSMikolaj Golub 	 * Some kern_proc out routines that print to this sbuf may
1632f1fca82eSMikolaj Golub 	 * call us with the process lock held. Draining with the
1633f1fca82eSMikolaj Golub 	 * non-sleepable lock held is unsafe. The lock is needed for
1634f1fca82eSMikolaj Golub 	 * those routines when dumping a live process. In our case we
1635f1fca82eSMikolaj Golub 	 * can safely release the lock before draining and acquire
1636f1fca82eSMikolaj Golub 	 * again after.
1637f1fca82eSMikolaj Golub 	 */
1638f1fca82eSMikolaj Golub 	locked = PROC_LOCKED(p->td->td_proc);
1639f1fca82eSMikolaj Golub 	if (locked)
1640f1fca82eSMikolaj Golub 		PROC_UNLOCK(p->td->td_proc);
164178f57a9cSMark Johnston 	if (p->comp != NULL)
164278f57a9cSMark Johnston 		error = compressor_write(p->comp, __DECONST(char *, data), len);
1643bd390213SMikolaj Golub 	else
1644aa14e9b7SMark Johnston 		error = core_write(p, __DECONST(void *, data), len, p->offset,
1645f31695ccSMark Johnston 		    UIO_SYSSPACE, NULL);
1646f1fca82eSMikolaj Golub 	if (locked)
1647f1fca82eSMikolaj Golub 		PROC_LOCK(p->td->td_proc);
1648bd390213SMikolaj Golub 	if (error != 0)
1649bd390213SMikolaj Golub 		return (-error);
1650bd390213SMikolaj Golub 	p->offset += len;
1651bd390213SMikolaj Golub 	return (len);
1652bd390213SMikolaj Golub }
1653bd390213SMikolaj Golub 
16548c64af4fSJohn Polstra int
1655e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1656fca666a1SJulian Elischer {
1657247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
1658fca666a1SJulian Elischer 	int error = 0;
16590ff27d31SJohn Polstra 	struct sseg_closure seginfo;
1660bd390213SMikolaj Golub 	struct note_info_list notelst;
1661aa14e9b7SMark Johnston 	struct coredump_params params;
1662bd390213SMikolaj Golub 	struct note_info *ninfo;
1663aa14e9b7SMark Johnston 	void *hdr, *tmpbuf;
1664bd390213SMikolaj Golub 	size_t hdrsize, notesz, coresize;
16658c64af4fSJohn Polstra 
1666e7228204SAlfred Perlstein 	hdr = NULL;
166702d131adSMark Johnston 	tmpbuf = NULL;
1668bd390213SMikolaj Golub 	TAILQ_INIT(&notelst);
1669e7228204SAlfred Perlstein 
16700ff27d31SJohn Polstra 	/* Size the program segments. */
16710ff27d31SJohn Polstra 	seginfo.count = 0;
16720ff27d31SJohn Polstra 	seginfo.size = 0;
1673*5bc3c617SKonstantin Belousov 	each_dumpable_segment(td, cb_size_segment, &seginfo, flags);
16740ff27d31SJohn Polstra 
16750ff27d31SJohn Polstra 	/*
1676bd390213SMikolaj Golub 	 * Collect info about the core file header area.
16770ff27d31SJohn Polstra 	 */
1678bd390213SMikolaj Golub 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1679c17b0bd2SConrad Meyer 	if (seginfo.count + 1 >= PN_XNUM)
1680c17b0bd2SConrad Meyer 		hdrsize += sizeof(Elf_Shdr);
1681bd390213SMikolaj Golub 	__elfN(prepare_notes)(td, &notelst, &notesz);
1682bd390213SMikolaj Golub 	coresize = round_page(hdrsize + notesz) + seginfo.size;
16830ff27d31SJohn Polstra 
168402d131adSMark Johnston 	/* Set up core dump parameters. */
168502d131adSMark Johnston 	params.offset = 0;
168602d131adSMark Johnston 	params.active_cred = cred;
168702d131adSMark Johnston 	params.file_cred = NOCRED;
168802d131adSMark Johnston 	params.td = td;
168902d131adSMark Johnston 	params.vp = vp;
169078f57a9cSMark Johnston 	params.comp = NULL;
169102d131adSMark Johnston 
1692afcc55f3SEdward Tomasz Napierala #ifdef RACCT
16934b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
16941ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1695bd390213SMikolaj Golub 		error = racct_add(td->td_proc, RACCT_CORE, coresize);
16961ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
16971ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
16981ba5ad42SEdward Tomasz Napierala 			error = EFAULT;
16991ba5ad42SEdward Tomasz Napierala 			goto done;
17001ba5ad42SEdward Tomasz Napierala 		}
17014b5c9cf6SEdward Tomasz Napierala 	}
1702afcc55f3SEdward Tomasz Napierala #endif
1703bd390213SMikolaj Golub 	if (coresize >= limit) {
1704fba6b1afSAlfred Perlstein 		error = EFAULT;
1705fba6b1afSAlfred Perlstein 		goto done;
1706fba6b1afSAlfred Perlstein 	}
17070ff27d31SJohn Polstra 
1708aa14e9b7SMark Johnston 	/* Create a compression stream if necessary. */
170978f57a9cSMark Johnston 	if (compress_user_cores != 0) {
171078f57a9cSMark Johnston 		params.comp = compressor_init(core_compressed_write,
171178f57a9cSMark Johnston 		    compress_user_cores, CORE_BUF_SIZE,
171278f57a9cSMark Johnston 		    compress_user_cores_level, &params);
171378f57a9cSMark Johnston 		if (params.comp == NULL) {
1714aa14e9b7SMark Johnston 			error = EFAULT;
1715aa14e9b7SMark Johnston 			goto done;
1716aa14e9b7SMark Johnston 		}
1717aa14e9b7SMark Johnston 		tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1718aa14e9b7SMark Johnston         }
1719aa14e9b7SMark Johnston 
17200ff27d31SJohn Polstra 	/*
17210ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
1722bd390213SMikolaj Golub 	 * and write it out following the notes.
17230ff27d31SJohn Polstra 	 */
1724a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1725aa14e9b7SMark Johnston 	error = __elfN(corehdr)(&params, seginfo.count, hdr, hdrsize, &notelst,
1726*5bc3c617SKonstantin Belousov 	    notesz, flags);
17270ff27d31SJohn Polstra 
17280ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
17290ff27d31SJohn Polstra 	if (error == 0) {
17300ff27d31SJohn Polstra 		Elf_Phdr *php;
17312b471bc6STim J. Robbins 		off_t offset;
17320ff27d31SJohn Polstra 		int i;
17330ff27d31SJohn Polstra 
17340ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1735bd390213SMikolaj Golub 		offset = round_page(hdrsize + notesz);
17360ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1737f31695ccSMark Johnston 			error = core_output((char *)(uintptr_t)php->p_vaddr,
1738aa14e9b7SMark Johnston 			    php->p_filesz, offset, &params, tmpbuf);
17390ff27d31SJohn Polstra 			if (error != 0)
17402b471bc6STim J. Robbins 				break;
17410ff27d31SJohn Polstra 			offset += php->p_filesz;
17420ff27d31SJohn Polstra 			php++;
17430ff27d31SJohn Polstra 		}
174478f57a9cSMark Johnston 		if (error == 0 && params.comp != NULL)
174578f57a9cSMark Johnston 			error = compressor_flush(params.comp);
17460ff27d31SJohn Polstra 	}
1747e7228204SAlfred Perlstein 	if (error) {
1748e7228204SAlfred Perlstein 		log(LOG_WARNING,
1749e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1750e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1751e7228204SAlfred Perlstein 	}
1752e7228204SAlfred Perlstein 
1753e7228204SAlfred Perlstein done:
1754aa14e9b7SMark Johnston 	free(tmpbuf, M_TEMP);
175578f57a9cSMark Johnston 	if (params.comp != NULL)
175678f57a9cSMark Johnston 		compressor_fini(params.comp);
1757bd390213SMikolaj Golub 	while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1758bd390213SMikolaj Golub 		TAILQ_REMOVE(&notelst, ninfo, link);
1759bd390213SMikolaj Golub 		free(ninfo, M_TEMP);
1760bd390213SMikolaj Golub 	}
1761bd390213SMikolaj Golub 	if (hdr != NULL)
17620ff27d31SJohn Polstra 		free(hdr, M_TEMP);
17630ff27d31SJohn Polstra 
1764a7cddfedSJake Burkholder 	return (error);
17658c64af4fSJohn Polstra }
17668c64af4fSJohn Polstra 
17670ff27d31SJohn Polstra /*
17681005d8afSConrad Meyer  * A callback for each_dumpable_segment() to write out the segment's
17690ff27d31SJohn Polstra  * program header entry.
17700ff27d31SJohn Polstra  */
17710ff27d31SJohn Polstra static void
17725cc6d253SEd Maste cb_put_phdr(vm_map_entry_t entry, void *closure)
17730ff27d31SJohn Polstra {
17740ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
17750ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
17760ff27d31SJohn Polstra 
17770ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
17780ff27d31SJohn Polstra 
17790ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
17800ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
17810ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
17820ff27d31SJohn Polstra 	phdr->p_paddr = 0;
17830ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
17840ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1785ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
17860ff27d31SJohn Polstra 
17870ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
17880ff27d31SJohn Polstra 	phc->phdr++;
17890ff27d31SJohn Polstra }
17900ff27d31SJohn Polstra 
17910ff27d31SJohn Polstra /*
17921005d8afSConrad Meyer  * A callback for each_dumpable_segment() to gather information about
17930ff27d31SJohn Polstra  * the number of segments and their total size.
17940ff27d31SJohn Polstra  */
17950ff27d31SJohn Polstra static void
1796f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure)
17970ff27d31SJohn Polstra {
17980ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
17990ff27d31SJohn Polstra 
18000ff27d31SJohn Polstra 	ssc->count++;
18010ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
18020ff27d31SJohn Polstra }
18030ff27d31SJohn Polstra 
18040ff27d31SJohn Polstra /*
18050ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
18060ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
18070ff27d31SJohn Polstra  * caller-supplied data.
18080ff27d31SJohn Polstra  */
18090ff27d31SJohn Polstra static void
1810*5bc3c617SKonstantin Belousov each_dumpable_segment(struct thread *td, segment_callback func, void *closure,
1811*5bc3c617SKonstantin Belousov     int flags)
18120ff27d31SJohn Polstra {
1813247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
18140ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
18150ff27d31SJohn Polstra 	vm_map_entry_t entry;
1816976a87a2SAlan Cox 	vm_object_t backing_object, object;
1817fec41f07SMark Johnston 	bool ignore_entry;
18180ff27d31SJohn Polstra 
1819976a87a2SAlan Cox 	vm_map_lock_read(map);
18202288078cSDoug Moore 	VM_MAP_ENTRY_FOREACH(entry, map) {
1821fa7dd9c5SMatthew Dillon 		/*
1822fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1823fa7dd9c5SMatthew Dillon 		 * coredump mode.
1824fa7dd9c5SMatthew Dillon 		 *
1825fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1826fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1827fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1828fa7dd9c5SMatthew Dillon 		 */
1829fa7dd9c5SMatthew Dillon 		if (elf_legacy_coredump) {
1830fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
18310ff27d31SJohn Polstra 				continue;
1832fa7dd9c5SMatthew Dillon 		} else {
1833fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_ALL) == 0)
1834fa7dd9c5SMatthew Dillon 				continue;
1835fa7dd9c5SMatthew Dillon 		}
18360ff27d31SJohn Polstra 
18379730a5daSPaul Saab 		/*
1838fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1839fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1840fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1841fa7dd9c5SMatthew Dillon 		 * kernel map).
18429730a5daSPaul Saab 		 */
1843fa7dd9c5SMatthew Dillon 		if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
18449730a5daSPaul Saab 			continue;
18459730a5daSPaul Saab 
1846976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
18470ff27d31SJohn Polstra 			continue;
18480ff27d31SJohn Polstra 
18490ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1850bc403f03SAttilio Rao 		VM_OBJECT_RLOCK(object);
1851976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1852bc403f03SAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1853bc403f03SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1854976a87a2SAlan Cox 			object = backing_object;
1855976a87a2SAlan Cox 		}
1856fec41f07SMark Johnston 		ignore_entry = (object->flags & OBJ_FICTITIOUS) != 0;
1857bc403f03SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1858976a87a2SAlan Cox 		if (ignore_entry)
18590ff27d31SJohn Polstra 			continue;
18600ff27d31SJohn Polstra 
18610ff27d31SJohn Polstra 		(*func)(entry, closure);
18620ff27d31SJohn Polstra 	}
1863976a87a2SAlan Cox 	vm_map_unlock_read(map);
18640ff27d31SJohn Polstra }
18650ff27d31SJohn Polstra 
18660ff27d31SJohn Polstra /*
18670ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
18680ff27d31SJohn Polstra  * the page boundary.
18690ff27d31SJohn Polstra  */
18708c64af4fSJohn Polstra static int
1871aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1872*5bc3c617SKonstantin Belousov     size_t hdrsize, struct note_info_list *notelst, size_t notesz,
1873*5bc3c617SKonstantin Belousov     int flags)
18748c64af4fSJohn Polstra {
1875bd390213SMikolaj Golub 	struct note_info *ninfo;
1876bd390213SMikolaj Golub 	struct sbuf *sb;
1877bd390213SMikolaj Golub 	int error;
18788c64af4fSJohn Polstra 
18798c64af4fSJohn Polstra 	/* Fill in the header. */
18800ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
1881*5bc3c617SKonstantin Belousov 	__elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz, flags);
18828c64af4fSJohn Polstra 
1883bd390213SMikolaj Golub 	sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1884aa14e9b7SMark Johnston 	sbuf_set_drain(sb, sbuf_drain_core_output, p);
1885bd390213SMikolaj Golub 	sbuf_start_section(sb, NULL);
1886bd390213SMikolaj Golub 	sbuf_bcat(sb, hdr, hdrsize);
1887bd390213SMikolaj Golub 	TAILQ_FOREACH(ninfo, notelst, link)
1888bd390213SMikolaj Golub 	    __elfN(putnote)(ninfo, sb);
1889bd390213SMikolaj Golub 	/* Align up to a page boundary for the program segments. */
1890bd390213SMikolaj Golub 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1891bd390213SMikolaj Golub 	error = sbuf_finish(sb);
1892bd390213SMikolaj Golub 	sbuf_delete(sb);
1893bd390213SMikolaj Golub 
1894bd390213SMikolaj Golub 	return (error);
1895e7228204SAlfred Perlstein }
1896bd390213SMikolaj Golub 
1897bd390213SMikolaj Golub static void
1898bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1899bd390213SMikolaj Golub     size_t *sizep)
1900bd390213SMikolaj Golub {
1901bd390213SMikolaj Golub 	struct proc *p;
1902bd390213SMikolaj Golub 	struct thread *thr;
1903bd390213SMikolaj Golub 	size_t size;
1904bd390213SMikolaj Golub 
1905bd390213SMikolaj Golub 	p = td->td_proc;
1906bd390213SMikolaj Golub 	size = 0;
1907bd390213SMikolaj Golub 
1908bd390213SMikolaj Golub 	size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1909bd390213SMikolaj Golub 
1910bd390213SMikolaj Golub 	/*
1911bd390213SMikolaj Golub 	 * To have the debugger select the right thread (LWP) as the initial
1912bd390213SMikolaj Golub 	 * thread, we dump the state of the thread passed to us in td first.
1913bd390213SMikolaj Golub 	 * This is the thread that causes the core dump and thus likely to
1914bd390213SMikolaj Golub 	 * be the right thread one wants to have selected in the debugger.
1915bd390213SMikolaj Golub 	 */
1916bd390213SMikolaj Golub 	thr = td;
1917bd390213SMikolaj Golub 	while (thr != NULL) {
1918bd390213SMikolaj Golub 		size += register_note(list, NT_PRSTATUS,
1919bd390213SMikolaj Golub 		    __elfN(note_prstatus), thr);
1920bd390213SMikolaj Golub 		size += register_note(list, NT_FPREGSET,
1921bd390213SMikolaj Golub 		    __elfN(note_fpregset), thr);
1922bd390213SMikolaj Golub 		size += register_note(list, NT_THRMISC,
1923bd390213SMikolaj Golub 		    __elfN(note_thrmisc), thr);
192486be94fcSTycho Nightingale 		size += register_note(list, NT_PTLWPINFO,
192586be94fcSTycho Nightingale 		    __elfN(note_ptlwpinfo), thr);
1926bd390213SMikolaj Golub 		size += register_note(list, -1,
1927bd390213SMikolaj Golub 		    __elfN(note_threadmd), thr);
1928bd390213SMikolaj Golub 
1929bd390213SMikolaj Golub 		thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1930bd390213SMikolaj Golub 		    TAILQ_NEXT(thr, td_plist);
1931bd390213SMikolaj Golub 		if (thr == td)
1932bd390213SMikolaj Golub 			thr = TAILQ_NEXT(thr, td_plist);
1933dada0278SJohn Polstra 	}
1934dada0278SJohn Polstra 
1935f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PROC,
1936f1fca82eSMikolaj Golub 	    __elfN(note_procstat_proc), p);
1937f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_FILES,
1938f1fca82eSMikolaj Golub 	    note_procstat_files, p);
1939f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_VMMAP,
1940f1fca82eSMikolaj Golub 	    note_procstat_vmmap, p);
1941f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_GROUPS,
1942f1fca82eSMikolaj Golub 	    note_procstat_groups, p);
1943f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_UMASK,
1944f1fca82eSMikolaj Golub 	    note_procstat_umask, p);
1945f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_RLIMIT,
1946f1fca82eSMikolaj Golub 	    note_procstat_rlimit, p);
1947f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_OSREL,
1948f1fca82eSMikolaj Golub 	    note_procstat_osrel, p);
1949f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1950f1fca82eSMikolaj Golub 	    __elfN(note_procstat_psstrings), p);
1951f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_AUXV,
1952f1fca82eSMikolaj Golub 	    __elfN(note_procstat_auxv), p);
1953f1fca82eSMikolaj Golub 
1954bd390213SMikolaj Golub 	*sizep = size;
1955bd390213SMikolaj Golub }
1956bd390213SMikolaj Golub 
1957bd390213SMikolaj Golub static void
1958bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1959*5bc3c617SKonstantin Belousov     size_t notesz, int flags)
1960bd390213SMikolaj Golub {
1961bd390213SMikolaj Golub 	Elf_Ehdr *ehdr;
1962bd390213SMikolaj Golub 	Elf_Phdr *phdr;
1963c17b0bd2SConrad Meyer 	Elf_Shdr *shdr;
1964bd390213SMikolaj Golub 	struct phdr_closure phc;
1965bd390213SMikolaj Golub 
1966bd390213SMikolaj Golub 	ehdr = (Elf_Ehdr *)hdr;
1967bd390213SMikolaj Golub 
1968bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1969bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1970bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1971bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1972bd390213SMikolaj Golub 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1973bd390213SMikolaj Golub 	ehdr->e_ident[EI_DATA] = ELF_DATA;
1974bd390213SMikolaj Golub 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1975bd390213SMikolaj Golub 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1976bd390213SMikolaj Golub 	ehdr->e_ident[EI_ABIVERSION] = 0;
1977bd390213SMikolaj Golub 	ehdr->e_ident[EI_PAD] = 0;
1978bd390213SMikolaj Golub 	ehdr->e_type = ET_CORE;
1979885f13dcSJohn Baldwin 	ehdr->e_machine = td->td_proc->p_elf_machine;
1980bd390213SMikolaj Golub 	ehdr->e_version = EV_CURRENT;
1981bd390213SMikolaj Golub 	ehdr->e_entry = 0;
1982bd390213SMikolaj Golub 	ehdr->e_phoff = sizeof(Elf_Ehdr);
1983885f13dcSJohn Baldwin 	ehdr->e_flags = td->td_proc->p_elf_flags;
1984bd390213SMikolaj Golub 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
1985bd390213SMikolaj Golub 	ehdr->e_phentsize = sizeof(Elf_Phdr);
1986bd390213SMikolaj Golub 	ehdr->e_shentsize = sizeof(Elf_Shdr);
1987bd390213SMikolaj Golub 	ehdr->e_shstrndx = SHN_UNDEF;
1988c17b0bd2SConrad Meyer 	if (numsegs + 1 < PN_XNUM) {
1989c17b0bd2SConrad Meyer 		ehdr->e_phnum = numsegs + 1;
1990c17b0bd2SConrad Meyer 		ehdr->e_shnum = 0;
1991c17b0bd2SConrad Meyer 	} else {
1992c17b0bd2SConrad Meyer 		ehdr->e_phnum = PN_XNUM;
1993c17b0bd2SConrad Meyer 		ehdr->e_shnum = 1;
1994c17b0bd2SConrad Meyer 
1995c17b0bd2SConrad Meyer 		ehdr->e_shoff = ehdr->e_phoff +
1996c17b0bd2SConrad Meyer 		    (numsegs + 1) * ehdr->e_phentsize;
1997c17b0bd2SConrad Meyer 		KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr),
1998c17b0bd2SConrad Meyer 		    ("e_shoff: %zu, hdrsize - shdr: %zu",
199907f825e8SConrad Meyer 		     (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr)));
2000c17b0bd2SConrad Meyer 
2001c17b0bd2SConrad Meyer 		shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
2002c17b0bd2SConrad Meyer 		memset(shdr, 0, sizeof(*shdr));
2003c17b0bd2SConrad Meyer 		/*
2004c17b0bd2SConrad Meyer 		 * A special first section is used to hold large segment and
2005c17b0bd2SConrad Meyer 		 * section counts.  This was proposed by Sun Microsystems in
2006c17b0bd2SConrad Meyer 		 * Solaris and has been adopted by Linux; the standard ELF
2007c17b0bd2SConrad Meyer 		 * tools are already familiar with the technique.
2008c17b0bd2SConrad Meyer 		 *
2009c17b0bd2SConrad Meyer 		 * See table 7-7 of the Solaris "Linker and Libraries Guide"
2010c17b0bd2SConrad Meyer 		 * (or 12-7 depending on the version of the document) for more
2011c17b0bd2SConrad Meyer 		 * details.
2012c17b0bd2SConrad Meyer 		 */
2013c17b0bd2SConrad Meyer 		shdr->sh_type = SHT_NULL;
2014c17b0bd2SConrad Meyer 		shdr->sh_size = ehdr->e_shnum;
2015c17b0bd2SConrad Meyer 		shdr->sh_link = ehdr->e_shstrndx;
2016c17b0bd2SConrad Meyer 		shdr->sh_info = numsegs + 1;
2017c17b0bd2SConrad Meyer 	}
2018bd390213SMikolaj Golub 
2019bd390213SMikolaj Golub 	/*
2020bd390213SMikolaj Golub 	 * Fill in the program header entries.
2021bd390213SMikolaj Golub 	 */
2022c17b0bd2SConrad Meyer 	phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
2023bd390213SMikolaj Golub 
2024bd390213SMikolaj Golub 	/* The note segement. */
2025bd390213SMikolaj Golub 	phdr->p_type = PT_NOTE;
2026bd390213SMikolaj Golub 	phdr->p_offset = hdrsize;
2027bd390213SMikolaj Golub 	phdr->p_vaddr = 0;
2028bd390213SMikolaj Golub 	phdr->p_paddr = 0;
2029bd390213SMikolaj Golub 	phdr->p_filesz = notesz;
2030bd390213SMikolaj Golub 	phdr->p_memsz = 0;
2031bd390213SMikolaj Golub 	phdr->p_flags = PF_R;
20321b8388cdSMikolaj Golub 	phdr->p_align = ELF_NOTE_ROUNDSIZE;
2033bd390213SMikolaj Golub 	phdr++;
2034bd390213SMikolaj Golub 
2035bd390213SMikolaj Golub 	/* All the writable segments from the program. */
2036bd390213SMikolaj Golub 	phc.phdr = phdr;
2037bd390213SMikolaj Golub 	phc.offset = round_page(hdrsize + notesz);
2038*5bc3c617SKonstantin Belousov 	each_dumpable_segment(td, cb_put_phdr, &phc, flags);
2039bd390213SMikolaj Golub }
2040bd390213SMikolaj Golub 
2041bd390213SMikolaj Golub static size_t
2042bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
2043bd390213SMikolaj Golub {
2044bd390213SMikolaj Golub 	struct note_info *ninfo;
2045bd390213SMikolaj Golub 	size_t size, notesize;
2046bd390213SMikolaj Golub 
2047bd390213SMikolaj Golub 	size = 0;
2048bd390213SMikolaj Golub 	out(arg, NULL, &size);
2049bd390213SMikolaj Golub 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
2050bd390213SMikolaj Golub 	ninfo->type = type;
2051bd390213SMikolaj Golub 	ninfo->outfunc = out;
2052bd390213SMikolaj Golub 	ninfo->outarg = arg;
2053bd390213SMikolaj Golub 	ninfo->outsize = size;
2054bd390213SMikolaj Golub 	TAILQ_INSERT_TAIL(list, ninfo, link);
2055bd390213SMikolaj Golub 
2056bd390213SMikolaj Golub 	if (type == -1)
2057bd390213SMikolaj Golub 		return (size);
2058bd390213SMikolaj Golub 
2059bd390213SMikolaj Golub 	notesize = sizeof(Elf_Note) +		/* note header */
2060180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
2061180e57e5SJohn Baldwin 						/* note name */
2062180e57e5SJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
2063180e57e5SJohn Baldwin 
2064180e57e5SJohn Baldwin 	return (notesize);
2065180e57e5SJohn Baldwin }
2066180e57e5SJohn Baldwin 
2067180e57e5SJohn Baldwin static size_t
2068180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len)
2069180e57e5SJohn Baldwin {
2070180e57e5SJohn Baldwin 	size_t padded_len;
2071180e57e5SJohn Baldwin 
2072180e57e5SJohn Baldwin 	padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
2073180e57e5SJohn Baldwin 	if (dst != NULL) {
2074180e57e5SJohn Baldwin 		bcopy(src, dst, len);
2075180e57e5SJohn Baldwin 		bzero((char *)dst + len, padded_len - len);
2076180e57e5SJohn Baldwin 	}
2077180e57e5SJohn Baldwin 	return (padded_len);
2078180e57e5SJohn Baldwin }
2079180e57e5SJohn Baldwin 
2080180e57e5SJohn Baldwin size_t
2081180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
2082180e57e5SJohn Baldwin {
2083180e57e5SJohn Baldwin 	Elf_Note *note;
2084180e57e5SJohn Baldwin 	char *buf;
2085180e57e5SJohn Baldwin 	size_t notesize;
2086180e57e5SJohn Baldwin 
2087180e57e5SJohn Baldwin 	buf = dst;
2088180e57e5SJohn Baldwin 	if (buf != NULL) {
2089180e57e5SJohn Baldwin 		note = (Elf_Note *)buf;
2090180e57e5SJohn Baldwin 		note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
2091180e57e5SJohn Baldwin 		note->n_descsz = size;
2092180e57e5SJohn Baldwin 		note->n_type = type;
2093180e57e5SJohn Baldwin 		buf += sizeof(*note);
2094180e57e5SJohn Baldwin 		buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
2095180e57e5SJohn Baldwin 		    sizeof(FREEBSD_ABI_VENDOR));
2096180e57e5SJohn Baldwin 		append_note_data(src, buf, size);
2097180e57e5SJohn Baldwin 		if (descp != NULL)
2098180e57e5SJohn Baldwin 			*descp = buf;
2099180e57e5SJohn Baldwin 	}
2100180e57e5SJohn Baldwin 
2101180e57e5SJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
2102180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
2103180e57e5SJohn Baldwin 						/* note name */
21041b8388cdSMikolaj Golub 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
2105bd390213SMikolaj Golub 
2106bd390213SMikolaj Golub 	return (notesize);
2107bd390213SMikolaj Golub }
2108bd390213SMikolaj Golub 
2109bd390213SMikolaj Golub static void
2110bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
2111bd390213SMikolaj Golub {
2112bd390213SMikolaj Golub 	Elf_Note note;
211314bdbaf2SConrad Meyer 	ssize_t old_len, sect_len;
211414bdbaf2SConrad Meyer 	size_t new_len, descsz, i;
2115bd390213SMikolaj Golub 
2116bd390213SMikolaj Golub 	if (ninfo->type == -1) {
2117bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
2118bd390213SMikolaj Golub 		return;
2119bd390213SMikolaj Golub 	}
2120bd390213SMikolaj Golub 
2121180e57e5SJohn Baldwin 	note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
2122bd390213SMikolaj Golub 	note.n_descsz = ninfo->outsize;
2123bd390213SMikolaj Golub 	note.n_type = ninfo->type;
2124bd390213SMikolaj Golub 
2125bd390213SMikolaj Golub 	sbuf_bcat(sb, &note, sizeof(note));
2126bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
2127180e57e5SJohn Baldwin 	sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
21281b8388cdSMikolaj Golub 	sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
2129bd390213SMikolaj Golub 	if (note.n_descsz == 0)
2130bd390213SMikolaj Golub 		return;
2131bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
2132bd390213SMikolaj Golub 	ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
213314bdbaf2SConrad Meyer 	sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
213414bdbaf2SConrad Meyer 	if (sect_len < 0)
213514bdbaf2SConrad Meyer 		return;
213614bdbaf2SConrad Meyer 
213714bdbaf2SConrad Meyer 	new_len = (size_t)sect_len;
213814bdbaf2SConrad Meyer 	descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
213914bdbaf2SConrad Meyer 	if (new_len < descsz) {
214014bdbaf2SConrad Meyer 		/*
214114bdbaf2SConrad Meyer 		 * It is expected that individual note emitters will correctly
214214bdbaf2SConrad Meyer 		 * predict their expected output size and fill up to that size
214314bdbaf2SConrad Meyer 		 * themselves, padding in a format-specific way if needed.
214414bdbaf2SConrad Meyer 		 * However, in case they don't, just do it here with zeros.
214514bdbaf2SConrad Meyer 		 */
214614bdbaf2SConrad Meyer 		for (i = 0; i < descsz - new_len; i++)
214714bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
214814bdbaf2SConrad Meyer 	} else if (new_len > descsz) {
214914bdbaf2SConrad Meyer 		/*
215014bdbaf2SConrad Meyer 		 * We can't always truncate sb -- we may have drained some
215114bdbaf2SConrad Meyer 		 * of it already.
215214bdbaf2SConrad Meyer 		 */
215314bdbaf2SConrad Meyer 		KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
215414bdbaf2SConrad Meyer 		    "read it (%zu > %zu).  Since it is longer than "
215514bdbaf2SConrad Meyer 		    "expected, this coredump's notes are corrupt.  THIS "
215614bdbaf2SConrad Meyer 		    "IS A BUG in the note_procstat routine for type %u.\n",
215714bdbaf2SConrad Meyer 		    __func__, (unsigned)note.n_type, new_len, descsz,
215814bdbaf2SConrad Meyer 		    (unsigned)note.n_type));
215914bdbaf2SConrad Meyer 	}
2160bd390213SMikolaj Golub }
2161bd390213SMikolaj Golub 
2162bd390213SMikolaj Golub /*
2163bd390213SMikolaj Golub  * Miscellaneous note out functions.
2164bd390213SMikolaj Golub  */
2165bd390213SMikolaj Golub 
2166841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2167841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
216851645e83SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2169841c0c7eSNathan Whitehorn 
217062919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
217162919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
217262919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
217362919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
217462919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
21757f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
2176f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	KERN_PROC_MASK32
2177f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t;
2178f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t;
217962919d78SPeter Wemm #else
218062919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
218162919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
218262919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
218362919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
218462919d78SPeter Wemm typedef gregset_t elf_gregset_t;
21857f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
2186f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	0
2187f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t;
2188f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t;
218962919d78SPeter Wemm #endif
219062919d78SPeter Wemm 
21918c64af4fSJohn Polstra static void
2192bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
21938c64af4fSJohn Polstra {
2194c77547d2SJohn Baldwin 	struct sbuf sbarg;
2195c77547d2SJohn Baldwin 	size_t len;
2196c77547d2SJohn Baldwin 	char *cp, *end;
2197247aba24SMarcel Moolenaar 	struct proc *p;
2198bd390213SMikolaj Golub 	elf_prpsinfo_t *psinfo;
2199c77547d2SJohn Baldwin 	int error;
22008c64af4fSJohn Polstra 
2201bd390213SMikolaj Golub 	p = (struct proc *)arg;
2202bd390213SMikolaj Golub 	if (sb != NULL) {
2203bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
2204bd390213SMikolaj Golub 		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
22058c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
220662919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
2207ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
2208c77547d2SJohn Baldwin 		PROC_LOCK(p);
2209c77547d2SJohn Baldwin 		if (p->p_args != NULL) {
2210c77547d2SJohn Baldwin 			len = sizeof(psinfo->pr_psargs) - 1;
2211c77547d2SJohn Baldwin 			if (len > p->p_args->ar_length)
2212c77547d2SJohn Baldwin 				len = p->p_args->ar_length;
2213c77547d2SJohn Baldwin 			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
2214c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
2215c77547d2SJohn Baldwin 			error = 0;
2216c77547d2SJohn Baldwin 		} else {
2217c77547d2SJohn Baldwin 			_PHOLD(p);
2218c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
2219c77547d2SJohn Baldwin 			sbuf_new(&sbarg, psinfo->pr_psargs,
2220c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
2221c77547d2SJohn Baldwin 			error = proc_getargv(curthread, p, &sbarg);
2222c77547d2SJohn Baldwin 			PRELE(p);
2223c77547d2SJohn Baldwin 			if (sbuf_finish(&sbarg) == 0)
2224c77547d2SJohn Baldwin 				len = sbuf_len(&sbarg) - 1;
2225c77547d2SJohn Baldwin 			else
2226c77547d2SJohn Baldwin 				len = sizeof(psinfo->pr_psargs) - 1;
2227c77547d2SJohn Baldwin 			sbuf_delete(&sbarg);
2228c77547d2SJohn Baldwin 		}
2229c77547d2SJohn Baldwin 		if (error || len == 0)
2230ccd3953eSJohn Baldwin 			strlcpy(psinfo->pr_psargs, p->p_comm,
22318c9b7b2cSMarcel Moolenaar 			    sizeof(psinfo->pr_psargs));
2232c77547d2SJohn Baldwin 		else {
2233c77547d2SJohn Baldwin 			KASSERT(len < sizeof(psinfo->pr_psargs),
2234c77547d2SJohn Baldwin 			    ("len is too long: %zu vs %zu", len,
2235c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs)));
2236c77547d2SJohn Baldwin 			cp = psinfo->pr_psargs;
2237c77547d2SJohn Baldwin 			end = cp + len - 1;
2238c77547d2SJohn Baldwin 			for (;;) {
2239c77547d2SJohn Baldwin 				cp = memchr(cp, '\0', end - cp);
2240c77547d2SJohn Baldwin 				if (cp == NULL)
2241c77547d2SJohn Baldwin 					break;
2242c77547d2SJohn Baldwin 				*cp = ' ';
2243c77547d2SJohn Baldwin 			}
2244c77547d2SJohn Baldwin 		}
2245ccb83afdSJohn Baldwin 		psinfo->pr_pid = p->p_pid;
2246bd390213SMikolaj Golub 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
2247bd390213SMikolaj Golub 		free(psinfo, M_TEMP);
2248bd390213SMikolaj Golub 	}
2249bd390213SMikolaj Golub 	*sizep = sizeof(*psinfo);
2250bd390213SMikolaj Golub }
2251bd390213SMikolaj Golub 
2252bd390213SMikolaj Golub static void
2253bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
2254bd390213SMikolaj Golub {
2255bd390213SMikolaj Golub 	struct thread *td;
2256bd390213SMikolaj Golub 	elf_prstatus_t *status;
2257bd390213SMikolaj Golub 
2258bd390213SMikolaj Golub 	td = (struct thread *)arg;
2259bd390213SMikolaj Golub 	if (sb != NULL) {
2260bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*status), ("invalid size"));
2261bd390213SMikolaj Golub 		status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
22628c9b7b2cSMarcel Moolenaar 		status->pr_version = PRSTATUS_VERSION;
226362919d78SPeter Wemm 		status->pr_statussz = sizeof(elf_prstatus_t);
226462919d78SPeter Wemm 		status->pr_gregsetsz = sizeof(elf_gregset_t);
226562919d78SPeter Wemm 		status->pr_fpregsetsz = sizeof(elf_fpregset_t);
22668c9b7b2cSMarcel Moolenaar 		status->pr_osreldate = osreldate;
2267bd390213SMikolaj Golub 		status->pr_cursig = td->td_proc->p_sig;
2268bd390213SMikolaj Golub 		status->pr_pid = td->td_tid;
2269841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2270bd390213SMikolaj Golub 		fill_regs32(td, &status->pr_reg);
227162919d78SPeter Wemm #else
2272bd390213SMikolaj Golub 		fill_regs(td, &status->pr_reg);
227362919d78SPeter Wemm #endif
2274bd390213SMikolaj Golub 		sbuf_bcat(sb, status, sizeof(*status));
2275bd390213SMikolaj Golub 		free(status, M_TEMP);
22768c9b7b2cSMarcel Moolenaar 	}
2277bd390213SMikolaj Golub 	*sizep = sizeof(*status);
2278bd390213SMikolaj Golub }
2279bd390213SMikolaj Golub 
2280bd390213SMikolaj Golub static void
2281bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
2282bd390213SMikolaj Golub {
2283bd390213SMikolaj Golub 	struct thread *td;
2284bd390213SMikolaj Golub 	elf_prfpregset_t *fpregset;
2285bd390213SMikolaj Golub 
2286bd390213SMikolaj Golub 	td = (struct thread *)arg;
2287bd390213SMikolaj Golub 	if (sb != NULL) {
2288bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
2289bd390213SMikolaj Golub 		fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
2290bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2291bd390213SMikolaj Golub 		fill_fpregs32(td, fpregset);
2292bd390213SMikolaj Golub #else
2293bd390213SMikolaj Golub 		fill_fpregs(td, fpregset);
2294bd390213SMikolaj Golub #endif
2295bd390213SMikolaj Golub 		sbuf_bcat(sb, fpregset, sizeof(*fpregset));
2296bd390213SMikolaj Golub 		free(fpregset, M_TEMP);
2297bd390213SMikolaj Golub 	}
2298bd390213SMikolaj Golub 	*sizep = sizeof(*fpregset);
2299bd390213SMikolaj Golub }
2300bd390213SMikolaj Golub 
2301bd390213SMikolaj Golub static void
2302bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
2303bd390213SMikolaj Golub {
2304bd390213SMikolaj Golub 	struct thread *td;
2305bd390213SMikolaj Golub 	elf_thrmisc_t thrmisc;
2306bd390213SMikolaj Golub 
2307bd390213SMikolaj Golub 	td = (struct thread *)arg;
2308bd390213SMikolaj Golub 	if (sb != NULL) {
2309bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
23102e5f9189SEd Maste 		bzero(&thrmisc, sizeof(thrmisc));
2311bd390213SMikolaj Golub 		strcpy(thrmisc.pr_tname, td->td_name);
2312bd390213SMikolaj Golub 		sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
2313bd390213SMikolaj Golub 	}
2314bd390213SMikolaj Golub 	*sizep = sizeof(thrmisc);
2315bd390213SMikolaj Golub }
2316bd390213SMikolaj Golub 
231786be94fcSTycho Nightingale static void
231886be94fcSTycho Nightingale __elfN(note_ptlwpinfo)(void *arg, struct sbuf *sb, size_t *sizep)
231986be94fcSTycho Nightingale {
232086be94fcSTycho Nightingale 	struct thread *td;
232186be94fcSTycho Nightingale 	size_t size;
232286be94fcSTycho Nightingale 	int structsize;
232351645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
232451645e83SJohn Baldwin 	struct ptrace_lwpinfo32 pl;
232551645e83SJohn Baldwin #else
232686be94fcSTycho Nightingale 	struct ptrace_lwpinfo pl;
232751645e83SJohn Baldwin #endif
232886be94fcSTycho Nightingale 
232986be94fcSTycho Nightingale 	td = (struct thread *)arg;
233051645e83SJohn Baldwin 	size = sizeof(structsize) + sizeof(pl);
233186be94fcSTycho Nightingale 	if (sb != NULL) {
233286be94fcSTycho Nightingale 		KASSERT(*sizep == size, ("invalid size"));
233351645e83SJohn Baldwin 		structsize = sizeof(pl);
233486be94fcSTycho Nightingale 		sbuf_bcat(sb, &structsize, sizeof(structsize));
233586be94fcSTycho Nightingale 		bzero(&pl, sizeof(pl));
233686be94fcSTycho Nightingale 		pl.pl_lwpid = td->td_tid;
233786be94fcSTycho Nightingale 		pl.pl_event = PL_EVENT_NONE;
233886be94fcSTycho Nightingale 		pl.pl_sigmask = td->td_sigmask;
233986be94fcSTycho Nightingale 		pl.pl_siglist = td->td_siglist;
234086be94fcSTycho Nightingale 		if (td->td_si.si_signo != 0) {
234186be94fcSTycho Nightingale 			pl.pl_event = PL_EVENT_SIGNAL;
234286be94fcSTycho Nightingale 			pl.pl_flags |= PL_FLAG_SI;
234351645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
234451645e83SJohn Baldwin 			siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo);
234551645e83SJohn Baldwin #else
234686be94fcSTycho Nightingale 			pl.pl_siginfo = td->td_si;
234751645e83SJohn Baldwin #endif
234886be94fcSTycho Nightingale 		}
234986be94fcSTycho Nightingale 		strcpy(pl.pl_tdname, td->td_name);
235086be94fcSTycho Nightingale 		/* XXX TODO: supply more information in struct ptrace_lwpinfo*/
235151645e83SJohn Baldwin 		sbuf_bcat(sb, &pl, sizeof(pl));
235286be94fcSTycho Nightingale 	}
235386be94fcSTycho Nightingale 	*sizep = size;
235486be94fcSTycho Nightingale }
235586be94fcSTycho Nightingale 
23564da47b2fSMarcel Moolenaar /*
23574da47b2fSMarcel Moolenaar  * Allow for MD specific notes, as well as any MD
23584da47b2fSMarcel Moolenaar  * specific preparations for writing MI notes.
23594da47b2fSMarcel Moolenaar  */
23608c64af4fSJohn Polstra static void
2361bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
23628c64af4fSJohn Polstra {
2363bd390213SMikolaj Golub 	struct thread *td;
2364bd390213SMikolaj Golub 	void *buf;
2365bd390213SMikolaj Golub 	size_t size;
23668c64af4fSJohn Polstra 
2367bd390213SMikolaj Golub 	td = (struct thread *)arg;
2368bd390213SMikolaj Golub 	size = *sizep;
2369bd390213SMikolaj Golub 	if (size != 0 && sb != NULL)
2370bd390213SMikolaj Golub 		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
237183a396ceSChristian Brueffer 	else
237283a396ceSChristian Brueffer 		buf = NULL;
2373bd390213SMikolaj Golub 	size = 0;
2374bd390213SMikolaj Golub 	__elfN(dump_thread)(td, buf, &size);
237564779280SKonstantin Belousov 	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
237683a396ceSChristian Brueffer 	if (size != 0 && sb != NULL)
2377bd390213SMikolaj Golub 		sbuf_bcat(sb, buf, size);
2378a1761d73SChristian Brueffer 	free(buf, M_TEMP);
2379bd390213SMikolaj Golub 	*sizep = size;
23808c64af4fSJohn Polstra }
23818c64af4fSJohn Polstra 
2382f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE
2383f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
2384f1fca82eSMikolaj Golub #endif
2385f1fca82eSMikolaj Golub 
2386f1fca82eSMikolaj Golub static void
2387f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
2388f1fca82eSMikolaj Golub {
2389f1fca82eSMikolaj Golub 	struct proc *p;
2390f1fca82eSMikolaj Golub 	size_t size;
2391f1fca82eSMikolaj Golub 	int structsize;
2392f1fca82eSMikolaj Golub 
2393f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2394f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_numthreads *
2395f1fca82eSMikolaj Golub 	    sizeof(elf_kinfo_proc_t);
2396f1fca82eSMikolaj Golub 
2397f1fca82eSMikolaj Golub 	if (sb != NULL) {
2398f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2399f1fca82eSMikolaj Golub 		structsize = sizeof(elf_kinfo_proc_t);
2400f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
24014daea938SKonstantin Belousov 		sx_slock(&proctree_lock);
2402f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2403f1fca82eSMikolaj Golub 		kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
24044daea938SKonstantin Belousov 		sx_sunlock(&proctree_lock);
2405f1fca82eSMikolaj Golub 	}
2406f1fca82eSMikolaj Golub 	*sizep = size;
2407f1fca82eSMikolaj Golub }
2408f1fca82eSMikolaj Golub 
2409f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE
2410f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
2411f1fca82eSMikolaj Golub #endif
2412f1fca82eSMikolaj Golub 
2413f1fca82eSMikolaj Golub static void
2414f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
2415f1fca82eSMikolaj Golub {
2416f1fca82eSMikolaj Golub 	struct proc *p;
241714bdbaf2SConrad Meyer 	size_t size, sect_sz, i;
241814bdbaf2SConrad Meyer 	ssize_t start_len, sect_len;
241914bdbaf2SConrad Meyer 	int structsize, filedesc_flags;
242014bdbaf2SConrad Meyer 
2421bcb60d52SConrad Meyer 	if (coredump_pack_fileinfo)
242214bdbaf2SConrad Meyer 		filedesc_flags = KERN_FILEDESC_PACK_KINFO;
242314bdbaf2SConrad Meyer 	else
242414bdbaf2SConrad Meyer 		filedesc_flags = 0;
2425f1fca82eSMikolaj Golub 
2426f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
242714bdbaf2SConrad Meyer 	structsize = sizeof(struct kinfo_file);
2428f1fca82eSMikolaj Golub 	if (sb == NULL) {
2429f1fca82eSMikolaj Golub 		size = 0;
2430f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
24315c32e9fcSAlexander Motin 		sbuf_set_drain(sb, sbuf_count_drain, &size);
2432f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2433f1fca82eSMikolaj Golub 		PROC_LOCK(p);
243414bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2435f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2436f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2437f1fca82eSMikolaj Golub 		*sizep = size;
2438f1fca82eSMikolaj Golub 	} else {
243914bdbaf2SConrad Meyer 		sbuf_start_section(sb, &start_len);
244014bdbaf2SConrad Meyer 
2441f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2442f1fca82eSMikolaj Golub 		PROC_LOCK(p);
244314bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
244414bdbaf2SConrad Meyer 		    filedesc_flags);
244514bdbaf2SConrad Meyer 
244614bdbaf2SConrad Meyer 		sect_len = sbuf_end_section(sb, start_len, 0, 0);
244714bdbaf2SConrad Meyer 		if (sect_len < 0)
244814bdbaf2SConrad Meyer 			return;
244914bdbaf2SConrad Meyer 		sect_sz = sect_len;
245014bdbaf2SConrad Meyer 
245114bdbaf2SConrad Meyer 		KASSERT(sect_sz <= *sizep,
245214bdbaf2SConrad Meyer 		    ("kern_proc_filedesc_out did not respect maxlen; "
245314bdbaf2SConrad Meyer 		     "requested %zu, got %zu", *sizep - sizeof(structsize),
245414bdbaf2SConrad Meyer 		     sect_sz - sizeof(structsize)));
245514bdbaf2SConrad Meyer 
245614bdbaf2SConrad Meyer 		for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
245714bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
2458f1fca82eSMikolaj Golub 	}
2459f1fca82eSMikolaj Golub }
2460f1fca82eSMikolaj Golub 
2461f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE
2462f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2463f1fca82eSMikolaj Golub #endif
2464f1fca82eSMikolaj Golub 
2465f1fca82eSMikolaj Golub static void
2466f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2467f1fca82eSMikolaj Golub {
2468f1fca82eSMikolaj Golub 	struct proc *p;
2469f1fca82eSMikolaj Golub 	size_t size;
2470e6b95927SConrad Meyer 	int structsize, vmmap_flags;
2471e6b95927SConrad Meyer 
2472e6b95927SConrad Meyer 	if (coredump_pack_vmmapinfo)
2473e6b95927SConrad Meyer 		vmmap_flags = KERN_VMMAP_PACK_KINFO;
2474e6b95927SConrad Meyer 	else
2475e6b95927SConrad Meyer 		vmmap_flags = 0;
2476f1fca82eSMikolaj Golub 
2477f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2478e6b95927SConrad Meyer 	structsize = sizeof(struct kinfo_vmentry);
2479f1fca82eSMikolaj Golub 	if (sb == NULL) {
2480f1fca82eSMikolaj Golub 		size = 0;
2481f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
24825c32e9fcSAlexander Motin 		sbuf_set_drain(sb, sbuf_count_drain, &size);
2483f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2484f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2485e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2486f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2487f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2488f1fca82eSMikolaj Golub 		*sizep = size;
2489f1fca82eSMikolaj Golub 	} else {
2490f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2491f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2492e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2493e6b95927SConrad Meyer 		    vmmap_flags);
2494f1fca82eSMikolaj Golub 	}
2495f1fca82eSMikolaj Golub }
2496f1fca82eSMikolaj Golub 
2497f1fca82eSMikolaj Golub static void
2498f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2499f1fca82eSMikolaj Golub {
2500f1fca82eSMikolaj Golub 	struct proc *p;
2501f1fca82eSMikolaj Golub 	size_t size;
2502f1fca82eSMikolaj Golub 	int structsize;
2503f1fca82eSMikolaj Golub 
2504f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2505f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
2506f1fca82eSMikolaj Golub 	if (sb != NULL) {
2507f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2508f1fca82eSMikolaj Golub 		structsize = sizeof(gid_t);
2509f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2510f1fca82eSMikolaj Golub 		sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2511f1fca82eSMikolaj Golub 		    sizeof(gid_t));
2512f1fca82eSMikolaj Golub 	}
2513f1fca82eSMikolaj Golub 	*sizep = size;
2514f1fca82eSMikolaj Golub }
2515f1fca82eSMikolaj Golub 
2516f1fca82eSMikolaj Golub static void
2517f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2518f1fca82eSMikolaj Golub {
2519f1fca82eSMikolaj Golub 	struct proc *p;
2520f1fca82eSMikolaj Golub 	size_t size;
2521f1fca82eSMikolaj Golub 	int structsize;
2522f1fca82eSMikolaj Golub 
2523f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
252485078b85SConrad Meyer 	size = sizeof(structsize) + sizeof(p->p_pd->pd_cmask);
2525f1fca82eSMikolaj Golub 	if (sb != NULL) {
2526f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
252785078b85SConrad Meyer 		structsize = sizeof(p->p_pd->pd_cmask);
2528f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
252985078b85SConrad Meyer 		sbuf_bcat(sb, &p->p_pd->pd_cmask, sizeof(p->p_pd->pd_cmask));
2530f1fca82eSMikolaj Golub 	}
2531f1fca82eSMikolaj Golub 	*sizep = size;
2532f1fca82eSMikolaj Golub }
2533f1fca82eSMikolaj Golub 
2534f1fca82eSMikolaj Golub static void
2535f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2536f1fca82eSMikolaj Golub {
2537f1fca82eSMikolaj Golub 	struct proc *p;
2538f1fca82eSMikolaj Golub 	struct rlimit rlim[RLIM_NLIMITS];
2539f1fca82eSMikolaj Golub 	size_t size;
2540f1fca82eSMikolaj Golub 	int structsize, i;
2541f1fca82eSMikolaj Golub 
2542f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2543f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(rlim);
2544f1fca82eSMikolaj Golub 	if (sb != NULL) {
2545f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2546f1fca82eSMikolaj Golub 		structsize = sizeof(rlim);
2547f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2548f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2549f1fca82eSMikolaj Golub 		for (i = 0; i < RLIM_NLIMITS; i++)
2550f6f6d240SMateusz Guzik 			lim_rlimit_proc(p, i, &rlim[i]);
2551f1fca82eSMikolaj Golub 		PROC_UNLOCK(p);
2552f1fca82eSMikolaj Golub 		sbuf_bcat(sb, rlim, sizeof(rlim));
2553f1fca82eSMikolaj Golub 	}
2554f1fca82eSMikolaj Golub 	*sizep = size;
2555f1fca82eSMikolaj Golub }
2556f1fca82eSMikolaj Golub 
2557f1fca82eSMikolaj Golub static void
2558f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2559f1fca82eSMikolaj Golub {
2560f1fca82eSMikolaj Golub 	struct proc *p;
2561f1fca82eSMikolaj Golub 	size_t size;
2562f1fca82eSMikolaj Golub 	int structsize;
2563f1fca82eSMikolaj Golub 
2564f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2565f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_osrel);
2566f1fca82eSMikolaj Golub 	if (sb != NULL) {
2567f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2568f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_osrel);
2569f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2570f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2571f1fca82eSMikolaj Golub 	}
2572f1fca82eSMikolaj Golub 	*sizep = size;
2573f1fca82eSMikolaj Golub }
2574f1fca82eSMikolaj Golub 
2575f1fca82eSMikolaj Golub static void
2576f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2577f1fca82eSMikolaj Golub {
2578f1fca82eSMikolaj Golub 	struct proc *p;
2579f1fca82eSMikolaj Golub 	elf_ps_strings_t ps_strings;
2580f1fca82eSMikolaj Golub 	size_t size;
2581f1fca82eSMikolaj Golub 	int structsize;
2582f1fca82eSMikolaj Golub 
2583f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2584f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(ps_strings);
2585f1fca82eSMikolaj Golub 	if (sb != NULL) {
2586f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2587f1fca82eSMikolaj Golub 		structsize = sizeof(ps_strings);
2588f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2589f1fca82eSMikolaj Golub 		ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2590f1fca82eSMikolaj Golub #else
2591f1fca82eSMikolaj Golub 		ps_strings = p->p_sysent->sv_psstrings;
2592f1fca82eSMikolaj Golub #endif
2593f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2594f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2595f1fca82eSMikolaj Golub 	}
2596f1fca82eSMikolaj Golub 	*sizep = size;
2597f1fca82eSMikolaj Golub }
2598f1fca82eSMikolaj Golub 
2599f1fca82eSMikolaj Golub static void
2600f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2601f1fca82eSMikolaj Golub {
2602f1fca82eSMikolaj Golub 	struct proc *p;
2603f1fca82eSMikolaj Golub 	size_t size;
2604f1fca82eSMikolaj Golub 	int structsize;
2605f1fca82eSMikolaj Golub 
2606f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2607f1fca82eSMikolaj Golub 	if (sb == NULL) {
2608f1fca82eSMikolaj Golub 		size = 0;
2609f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
26105c32e9fcSAlexander Motin 		sbuf_set_drain(sb, sbuf_count_drain, &size);
2611f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2612f1fca82eSMikolaj Golub 		PHOLD(p);
2613f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2614f1fca82eSMikolaj Golub 		PRELE(p);
2615f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2616f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2617f1fca82eSMikolaj Golub 		*sizep = size;
2618f1fca82eSMikolaj Golub 	} else {
2619f1fca82eSMikolaj Golub 		structsize = sizeof(Elf_Auxinfo);
2620f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2621f1fca82eSMikolaj Golub 		PHOLD(p);
2622f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2623f1fca82eSMikolaj Golub 		PRELE(p);
2624f1fca82eSMikolaj Golub 	}
2625f1fca82eSMikolaj Golub }
2626f1fca82eSMikolaj Golub 
262732c01de2SDmitry Chagin static boolean_t
262892328a32SKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Note *checknote,
262992328a32SKonstantin Belousov     const char *note_vendor, const Elf_Phdr *pnote,
263092328a32SKonstantin Belousov     boolean_t (*cb)(const Elf_Note *, void *, boolean_t *), void *cb_arg)
263132c01de2SDmitry Chagin {
2632267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
263332c01de2SDmitry Chagin 	const char *note_name;
26346c775eb6SKonstantin Belousov 	char *buf;
26356c775eb6SKonstantin Belousov 	int i, error;
26366c775eb6SKonstantin Belousov 	boolean_t res;
263732c01de2SDmitry Chagin 
26386c775eb6SKonstantin Belousov 	/* We need some limit, might as well use PAGE_SIZE. */
26396c775eb6SKonstantin Belousov 	if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
264032c01de2SDmitry Chagin 		return (FALSE);
26416c775eb6SKonstantin Belousov 	ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
26426c775eb6SKonstantin Belousov 	if (pnote->p_offset > PAGE_SIZE ||
26436c775eb6SKonstantin Belousov 	    pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
26442d6b8546SKonstantin Belousov 		buf = malloc(pnote->p_filesz, M_TEMP, M_NOWAIT);
26452d6b8546SKonstantin Belousov 		if (buf == NULL) {
2646b249ce48SMateusz Guzik 			VOP_UNLOCK(imgp->vp);
26476c775eb6SKonstantin Belousov 			buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
264878022527SKonstantin Belousov 			vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
26492d6b8546SKonstantin Belousov 		}
26506c775eb6SKonstantin Belousov 		error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
26516c775eb6SKonstantin Belousov 		    pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
26526c775eb6SKonstantin Belousov 		    curthread->td_ucred, NOCRED, NULL, curthread);
26536c775eb6SKonstantin Belousov 		if (error != 0) {
26546c775eb6SKonstantin Belousov 			uprintf("i/o error PT_NOTE\n");
2655eda8fe63SKonstantin Belousov 			goto retf;
26566c775eb6SKonstantin Belousov 		}
26576c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)buf;
26586c775eb6SKonstantin Belousov 		note_end = (const Elf_Note *)(buf + pnote->p_filesz);
26596c775eb6SKonstantin Belousov 	} else {
26606c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)(imgp->image_header +
26616c775eb6SKonstantin Belousov 		    pnote->p_offset);
266232c01de2SDmitry Chagin 		note_end = (const Elf_Note *)(imgp->image_header +
266332c01de2SDmitry Chagin 		    pnote->p_offset + pnote->p_filesz);
26646c775eb6SKonstantin Belousov 		buf = NULL;
26656c775eb6SKonstantin Belousov 	}
2666267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2667d1ae5c83SKonstantin Belousov 		if (!aligned(note, Elf32_Addr) || (const char *)note_end -
26686c775eb6SKonstantin Belousov 		    (const char *)note < sizeof(Elf_Note)) {
2669eda8fe63SKonstantin Belousov 			goto retf;
26706c775eb6SKonstantin Belousov 		}
267192328a32SKonstantin Belousov 		if (note->n_namesz != checknote->n_namesz ||
267292328a32SKonstantin Belousov 		    note->n_descsz != checknote->n_descsz ||
267392328a32SKonstantin Belousov 		    note->n_type != checknote->n_type)
267432c01de2SDmitry Chagin 			goto nextnote;
267532c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
267692328a32SKonstantin Belousov 		if (note_name + checknote->n_namesz >=
267792328a32SKonstantin Belousov 		    (const char *)note_end || strncmp(note_vendor,
267892328a32SKonstantin Belousov 		    note_name, checknote->n_namesz) != 0)
267932c01de2SDmitry Chagin 			goto nextnote;
268032c01de2SDmitry Chagin 
268192328a32SKonstantin Belousov 		if (cb(note, cb_arg, &res))
26826c775eb6SKonstantin Belousov 			goto ret;
268332c01de2SDmitry Chagin nextnote:
268432c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
26851b8388cdSMikolaj Golub 		    roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
26861b8388cdSMikolaj Golub 		    roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
268732c01de2SDmitry Chagin 	}
2688eda8fe63SKonstantin Belousov retf:
26896c775eb6SKonstantin Belousov 	res = FALSE;
26906c775eb6SKonstantin Belousov ret:
26916c775eb6SKonstantin Belousov 	free(buf, M_TEMP);
26926c775eb6SKonstantin Belousov 	return (res);
269332c01de2SDmitry Chagin }
269432c01de2SDmitry Chagin 
269592328a32SKonstantin Belousov struct brandnote_cb_arg {
269692328a32SKonstantin Belousov 	Elf_Brandnote *brandnote;
269792328a32SKonstantin Belousov 	int32_t *osrel;
269892328a32SKonstantin Belousov };
269992328a32SKonstantin Belousov 
270092328a32SKonstantin Belousov static boolean_t
270192328a32SKonstantin Belousov brandnote_cb(const Elf_Note *note, void *arg0, boolean_t *res)
270292328a32SKonstantin Belousov {
270392328a32SKonstantin Belousov 	struct brandnote_cb_arg *arg;
270492328a32SKonstantin Belousov 
270592328a32SKonstantin Belousov 	arg = arg0;
270692328a32SKonstantin Belousov 
270792328a32SKonstantin Belousov 	/*
270892328a32SKonstantin Belousov 	 * Fetch the osreldate for binary from the ELF OSABI-note if
270992328a32SKonstantin Belousov 	 * necessary.
271092328a32SKonstantin Belousov 	 */
271192328a32SKonstantin Belousov 	*res = (arg->brandnote->flags & BN_TRANSLATE_OSREL) != 0 &&
271292328a32SKonstantin Belousov 	    arg->brandnote->trans_osrel != NULL ?
271392328a32SKonstantin Belousov 	    arg->brandnote->trans_osrel(note, arg->osrel) : TRUE;
271492328a32SKonstantin Belousov 
271592328a32SKonstantin Belousov 	return (TRUE);
271692328a32SKonstantin Belousov }
271792328a32SKonstantin Belousov 
2718cefb93f2SKonstantin Belousov static Elf_Note fctl_note = {
2719cefb93f2SKonstantin Belousov 	.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
2720cefb93f2SKonstantin Belousov 	.n_descsz = sizeof(uint32_t),
2721cefb93f2SKonstantin Belousov 	.n_type = NT_FREEBSD_FEATURE_CTL,
2722cefb93f2SKonstantin Belousov };
2723cefb93f2SKonstantin Belousov 
2724cefb93f2SKonstantin Belousov struct fctl_cb_arg {
27250cad2aa2SKonstantin Belousov 	boolean_t *has_fctl0;
2726cefb93f2SKonstantin Belousov 	uint32_t *fctl0;
2727cefb93f2SKonstantin Belousov };
2728cefb93f2SKonstantin Belousov 
2729cefb93f2SKonstantin Belousov static boolean_t
2730cefb93f2SKonstantin Belousov note_fctl_cb(const Elf_Note *note, void *arg0, boolean_t *res)
2731cefb93f2SKonstantin Belousov {
2732cefb93f2SKonstantin Belousov 	struct fctl_cb_arg *arg;
2733cefb93f2SKonstantin Belousov 	const Elf32_Word *desc;
2734cefb93f2SKonstantin Belousov 	uintptr_t p;
2735cefb93f2SKonstantin Belousov 
2736cefb93f2SKonstantin Belousov 	arg = arg0;
2737cefb93f2SKonstantin Belousov 	p = (uintptr_t)(note + 1);
2738cefb93f2SKonstantin Belousov 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
2739cefb93f2SKonstantin Belousov 	desc = (const Elf32_Word *)p;
27400cad2aa2SKonstantin Belousov 	*arg->has_fctl0 = TRUE;
2741cefb93f2SKonstantin Belousov 	*arg->fctl0 = desc[0];
2742409ab7e1SMark Johnston 	*res = TRUE;
2743cefb93f2SKonstantin Belousov 	return (TRUE);
2744cefb93f2SKonstantin Belousov }
2745cefb93f2SKonstantin Belousov 
274632c01de2SDmitry Chagin /*
2747cefb93f2SKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote, fetch
2748cefb93f2SKonstantin Belousov  * the osreldate and feature control flags for binary from the ELF
2749cefb93f2SKonstantin Belousov  * OSABI-note.  Only the first page of the image is searched, the same
2750cefb93f2SKonstantin Belousov  * as for headers.
27511a9c7decSKonstantin Belousov  */
27521a9c7decSKonstantin Belousov static boolean_t
275392328a32SKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *brandnote,
27540cad2aa2SKonstantin Belousov     int32_t *osrel, boolean_t *has_fctl0, uint32_t *fctl0)
27551a9c7decSKonstantin Belousov {
27561a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
27571a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
275892328a32SKonstantin Belousov 	struct brandnote_cb_arg b_arg;
2759cefb93f2SKonstantin Belousov 	struct fctl_cb_arg f_arg;
2760cefb93f2SKonstantin Belousov 	int i, j;
27611a9c7decSKonstantin Belousov 
27621a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
27631a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
276492328a32SKonstantin Belousov 	b_arg.brandnote = brandnote;
276592328a32SKonstantin Belousov 	b_arg.osrel = osrel;
27660cad2aa2SKonstantin Belousov 	f_arg.has_fctl0 = has_fctl0;
2767cefb93f2SKonstantin Belousov 	f_arg.fctl0 = fctl0;
27681a9c7decSKonstantin Belousov 
27691a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
277092328a32SKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp,
277192328a32SKonstantin Belousov 		    &brandnote->hdr, brandnote->vendor, &phdr[i], brandnote_cb,
277292328a32SKonstantin Belousov 		    &b_arg)) {
2773cefb93f2SKonstantin Belousov 			for (j = 0; j < hdr->e_phnum; j++) {
2774cefb93f2SKonstantin Belousov 				if (phdr[j].p_type == PT_NOTE &&
2775cefb93f2SKonstantin Belousov 				    __elfN(parse_notes)(imgp, &fctl_note,
2776cefb93f2SKonstantin Belousov 				    FREEBSD_ABI_VENDOR, &phdr[j],
2777cefb93f2SKonstantin Belousov 				    note_fctl_cb, &f_arg))
2778cefb93f2SKonstantin Belousov 					break;
2779cefb93f2SKonstantin Belousov 			}
27801a9c7decSKonstantin Belousov 			return (TRUE);
27811a9c7decSKonstantin Belousov 		}
278292328a32SKonstantin Belousov 	}
27831a9c7decSKonstantin Belousov 	return (FALSE);
27841a9c7decSKonstantin Belousov 
27851a9c7decSKonstantin Belousov }
27861a9c7decSKonstantin Belousov 
27871a9c7decSKonstantin Belousov /*
2788e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
2789e1743d02SSøren Schmidt  */
2790a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
2791b7feabf9SEd Maste 	.ex_imgact = __CONCAT(exec_, __elfN(imgact)),
2792b7feabf9SEd Maste 	.ex_name = __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2793a360a43dSJake Burkholder };
2794a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2795e7228204SAlfred Perlstein 
2796ed167eaaSKonstantin Belousov static vm_prot_t
2797ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
2798ed167eaaSKonstantin Belousov {
2799ed167eaaSKonstantin Belousov 	vm_prot_t prot;
2800ed167eaaSKonstantin Belousov 
2801ed167eaaSKonstantin Belousov 	prot = 0;
2802ed167eaaSKonstantin Belousov 	if (flags & PF_X)
2803ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
2804ed167eaaSKonstantin Belousov 	if (flags & PF_W)
2805ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
2806ed167eaaSKonstantin Belousov 	if (flags & PF_R)
2807ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
2808eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
2809126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
2810676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
2811676eda08SMarcel Moolenaar #endif
2812ed167eaaSKonstantin Belousov 	return (prot);
2813ed167eaaSKonstantin Belousov }
2814ed167eaaSKonstantin Belousov 
2815ed167eaaSKonstantin Belousov static Elf_Word
2816ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
2817ed167eaaSKonstantin Belousov {
2818ed167eaaSKonstantin Belousov 	Elf_Word flags;
2819ed167eaaSKonstantin Belousov 
2820ed167eaaSKonstantin Belousov 	flags = 0;
2821ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
2822ed167eaaSKonstantin Belousov 		flags |= PF_X;
2823ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
2824ed167eaaSKonstantin Belousov 		flags |= PF_R;
2825ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
2826ed167eaaSKonstantin Belousov 		flags |= PF_W;
2827ed167eaaSKonstantin Belousov 	return (flags);
2828ed167eaaSKonstantin Belousov }
2829fc83c5a7SKonstantin Belousov 
2830fc83c5a7SKonstantin Belousov void
283131174518SJohn Baldwin __elfN(stackgap)(struct image_params *imgp, uintptr_t *stack_base)
2832fc83c5a7SKonstantin Belousov {
283331174518SJohn Baldwin 	uintptr_t range, rbase, gap;
2834fc83c5a7SKonstantin Belousov 	int pct;
2835fc83c5a7SKonstantin Belousov 
2836fc83c5a7SKonstantin Belousov 	pct = __elfN(aslr_stack_gap);
2837fc83c5a7SKonstantin Belousov 	if (pct == 0)
2838fc83c5a7SKonstantin Belousov 		return;
2839fc83c5a7SKonstantin Belousov 	if (pct > 50)
2840fc83c5a7SKonstantin Belousov 		pct = 50;
2841fc83c5a7SKonstantin Belousov 	range = imgp->eff_stack_sz * pct / 100;
2842fc83c5a7SKonstantin Belousov 	arc4rand(&rbase, sizeof(rbase), 0);
2843fc83c5a7SKonstantin Belousov 	gap = rbase % range;
2844fc83c5a7SKonstantin Belousov 	gap &= ~(sizeof(u_long) - 1);
2845fc83c5a7SKonstantin Belousov 	*stack_base -= gap;
2846fc83c5a7SKonstantin Belousov }
2847