xref: /freebsd/sys/kern/imgact_elf.c (revision 1babcad6bc4093664a10c7ab98b9ebd2d64bcaaf)
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>
57b7924341SAndrew Turner #include <sys/reg.h>
588c64af4fSJohn Polstra #include <sys/resourcevar.h>
5989f6b863SAttilio Rao #include <sys/rwlock.h>
60bd390213SMikolaj Golub #include <sys/sbuf.h>
61da61b9a6SAlan Cox #include <sys/sf_buf.h>
62ee235befSKonstantin Belousov #include <sys/smp.h>
6336240ea5SDoug Rabson #include <sys/systm.h>
64e1743d02SSøren Schmidt #include <sys/signalvar.h>
658c64af4fSJohn Polstra #include <sys/stat.h>
661005a129SJohn Baldwin #include <sys/sx.h>
678c64af4fSJohn Polstra #include <sys/syscall.h>
68e1743d02SSøren Schmidt #include <sys/sysctl.h>
698c64af4fSJohn Polstra #include <sys/sysent.h>
70a794e791SBruce Evans #include <sys/vnode.h>
71e7228204SAlfred Perlstein #include <sys/syslog.h>
72e7228204SAlfred Perlstein #include <sys/eventhandler.h>
73f1fca82eSMikolaj Golub #include <sys/user.h>
74e7228204SAlfred Perlstein 
75e1743d02SSøren Schmidt #include <vm/vm.h>
76e1743d02SSøren Schmidt #include <vm/vm_kern.h>
77e1743d02SSøren Schmidt #include <vm/vm_param.h>
78e1743d02SSøren Schmidt #include <vm/pmap.h>
79e1743d02SSøren Schmidt #include <vm/vm_map.h>
800ff27d31SJohn Polstra #include <vm/vm_object.h>
81e1743d02SSøren Schmidt #include <vm/vm_extern.h>
82e1743d02SSøren Schmidt 
8352c24af7SPeter Wemm #include <machine/elf.h>
84e1743d02SSøren Schmidt #include <machine/md_var.h>
85e1743d02SSøren Schmidt 
861b8388cdSMikolaj Golub #define ELF_NOTE_ROUNDSIZE	4
87c815a20cSDavid E. O'Brien #define OLD_EI_BRAND	8
88c815a20cSDavid E. O'Brien 
893ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr);
9032c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
9109c78d53SEdward Tomasz Napierala     const char *interp, int32_t *osrel, uint32_t *fctl0);
923ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
931699546dSEdward Tomasz Napierala     u_long *entry);
940bbee4cdSKonstantin Belousov static int __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
951699546dSEdward Tomasz Napierala     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot);
963ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
97a95659f7SEd Maste static bool __elfN(freebsd_trans_osrel)(const Elf_Note *note,
9889ffc202SBjoern A. Zeeb     int32_t *osrel);
99a95659f7SEd Maste static bool kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
10019621645SAlex Richardson static bool __elfN(check_note)(struct image_params *imgp,
10119621645SAlex Richardson     Elf_Brandnote *checknote, int32_t *osrel, bool *has_fctl0,
1020cad2aa2SKonstantin Belousov     uint32_t *fctl0);
103ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word);
104ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t);
1056b71405bSJohn Baldwin static size_t __elfN(prepare_register_notes)(struct thread *td,
1066b71405bSJohn Baldwin     struct note_info_list *list, struct thread *target_td);
107e1743d02SSøren Schmidt 
1087029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE),
1097029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
110a360a43dSJake Burkholder     "");
111a360a43dSJake Burkholder 
112e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1;
113e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
114af3b2549SHans Petter Selasky     fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
115a360a43dSJake Burkholder     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
116a360a43dSJake Burkholder 
117551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0;
118a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
1191cbb879dSEd Maste     &elf_legacy_coredump, 0,
1201cbb879dSEd Maste     "include all and only RW pages in core dumps");
121e1743d02SSøren Schmidt 
12262c625fdSKonstantin Belousov int __elfN(nxstack) =
1234d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \
1244bf4b0f1SJohn Baldwin     (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) || \
1254bf4b0f1SJohn Baldwin     defined(__riscv)
12662c625fdSKonstantin Belousov 	1;
12762c625fdSKonstantin Belousov #else
12862c625fdSKonstantin Belousov 	0;
12962c625fdSKonstantin Belousov #endif
130291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
131291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
132291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
133291c06a1SKonstantin Belousov 
134eb029587SKonstantin Belousov #if defined(__amd64__)
135eb029587SKonstantin Belousov static int __elfN(vdso) = 1;
136eb029587SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
137eb029587SKonstantin Belousov     vdso, CTLFLAG_RWTUN, &__elfN(vdso), 0,
138eb029587SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable vdso preloading");
139eb029587SKonstantin Belousov #else
140eb029587SKonstantin Belousov static int __elfN(vdso) = 0;
141eb029587SKonstantin Belousov #endif
142eb029587SKonstantin Belousov 
143eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
144126b36a2SKonstantin Belousov int i386_read_exec = 0;
145126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
146126b36a2SKonstantin Belousov     "enable execution from readable segments");
147126b36a2SKonstantin Belousov #endif
148126b36a2SKonstantin Belousov 
14995aafd69SKonstantin Belousov static u_long __elfN(pie_base) = ET_DYN_LOAD_ADDR;
150f33533daSKonstantin Belousov static int
151f33533daSKonstantin Belousov sysctl_pie_base(SYSCTL_HANDLER_ARGS)
152f33533daSKonstantin Belousov {
153f33533daSKonstantin Belousov 	u_long val;
154f33533daSKonstantin Belousov 	int error;
155f33533daSKonstantin Belousov 
156f33533daSKonstantin Belousov 	val = __elfN(pie_base);
157f33533daSKonstantin Belousov 	error = sysctl_handle_long(oidp, &val, 0, req);
158f33533daSKonstantin Belousov 	if (error != 0 || req->newptr == NULL)
159f33533daSKonstantin Belousov 		return (error);
160f33533daSKonstantin Belousov 	if ((val & PAGE_MASK) != 0)
161f33533daSKonstantin Belousov 		return (EINVAL);
162f33533daSKonstantin Belousov 	__elfN(pie_base) = val;
163f33533daSKonstantin Belousov 	return (0);
164f33533daSKonstantin Belousov }
165f33533daSKonstantin Belousov SYSCTL_PROC(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, pie_base,
166f33533daSKonstantin Belousov     CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0,
167f33533daSKonstantin Belousov     sysctl_pie_base, "LU",
16895aafd69SKonstantin Belousov     "PIE load base without randomization");
16995aafd69SKonstantin Belousov 
1707029da5cSPawel Biernacki SYSCTL_NODE(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, aslr,
1717029da5cSPawel Biernacki     CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
172fa50a355SKonstantin Belousov     "");
173fa50a355SKonstantin Belousov #define	ASLR_NODE_OID	__CONCAT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), _aslr)
174fa50a355SKonstantin Belousov 
175b014e0f1SMarcin Wojtas /*
176b014e0f1SMarcin Wojtas  * While for 64-bit machines ASLR works properly, there are
177b014e0f1SMarcin Wojtas  * still some problems when using 32-bit architectures. For this
178b014e0f1SMarcin Wojtas  * reason ASLR is only enabled by default when running native
179b014e0f1SMarcin Wojtas  * 64-bit non-PIE executables.
180b014e0f1SMarcin Wojtas  */
181b014e0f1SMarcin Wojtas static int __elfN(aslr_enabled) = __ELF_WORD_SIZE == 64;
182fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN,
183fa50a355SKonstantin Belousov     &__elfN(aslr_enabled), 0,
184fa50a355SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
185fa50a355SKonstantin Belousov     ": enable address map randomization");
186fa50a355SKonstantin Belousov 
187b014e0f1SMarcin Wojtas /*
188b014e0f1SMarcin Wojtas  * Enable ASLR only for 64-bit PIE binaries by default.
189b014e0f1SMarcin Wojtas  */
190b014e0f1SMarcin Wojtas static int __elfN(pie_aslr_enabled) = __ELF_WORD_SIZE == 64;
191fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, pie_enable, CTLFLAG_RWTUN,
192fa50a355SKonstantin Belousov     &__elfN(pie_aslr_enabled), 0,
193fa50a355SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
194fa50a355SKonstantin Belousov     ": enable address map randomization for PIE binaries");
195fa50a355SKonstantin Belousov 
196b014e0f1SMarcin Wojtas /*
197b014e0f1SMarcin Wojtas  * Sbrk is now deprecated and it can be assumed, that in most
198b014e0f1SMarcin Wojtas  * cases it will not be used anyway. This setting is valid only
199b014e0f1SMarcin Wojtas  * for the ASLR enabled and allows for utilizing the bss grow region.
200b014e0f1SMarcin Wojtas  */
201b014e0f1SMarcin Wojtas static int __elfN(aslr_honor_sbrk) = 0;
202fa50a355SKonstantin Belousov SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, honor_sbrk, CTLFLAG_RW,
203fa50a355SKonstantin Belousov     &__elfN(aslr_honor_sbrk), 0,
204fa50a355SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": assume sbrk is used");
205fa50a355SKonstantin Belousov 
2061811c1e9SMark Johnston static int __elfN(aslr_stack) = 1;
2071811c1e9SMark Johnston SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, stack, CTLFLAG_RWTUN,
2081811c1e9SMark Johnston     &__elfN(aslr_stack), 0,
209fc83c5a7SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2101811c1e9SMark Johnston     ": enable stack address randomization");
211fc83c5a7SKonstantin Belousov 
212944cf37bSKonstantin Belousov static int __elfN(sigfastblock) = 1;
213944cf37bSKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, sigfastblock,
214944cf37bSKonstantin Belousov     CTLFLAG_RWTUN, &__elfN(sigfastblock), 0,
215944cf37bSKonstantin Belousov     "enable sigfastblock for new processes");
216944cf37bSKonstantin Belousov 
2172e1c94aaSKonstantin Belousov static bool __elfN(allow_wx) = true;
2182e1c94aaSKonstantin Belousov SYSCTL_BOOL(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO, allow_wx,
2192e1c94aaSKonstantin Belousov     CTLFLAG_RWTUN, &__elfN(allow_wx), 0,
2202e1c94aaSKonstantin Belousov     "Allow pages to be mapped simultaneously writable and executable");
2212e1c94aaSKonstantin Belousov 
2223ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
223e1743d02SSøren Schmidt 
224545517f1SEdward Tomasz Napierala #define	aligned(a, t)	(rounddown2((u_long)(a), sizeof(t)) == (u_long)(a))
22593d1c728SKonstantin Belousov 
22632c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = {
22732c01de2SDmitry Chagin 	.hdr.n_namesz	= sizeof(FREEBSD_ABI_VENDOR),
22832c01de2SDmitry Chagin 	.hdr.n_descsz	= sizeof(int32_t),
2294c22b468SEd Maste 	.hdr.n_type	= NT_FREEBSD_ABI_TAG,
23032c01de2SDmitry Chagin 	.vendor		= FREEBSD_ABI_VENDOR,
23189ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
23289ffc202SBjoern A. Zeeb 	.trans_osrel	= __elfN(freebsd_trans_osrel)
23332c01de2SDmitry Chagin };
23432c01de2SDmitry Chagin 
235a95659f7SEd Maste static bool
23689ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
23789ffc202SBjoern A. Zeeb {
23889ffc202SBjoern A. Zeeb 	uintptr_t p;
23989ffc202SBjoern A. Zeeb 
24089ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
2411b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
24289ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
24389ffc202SBjoern A. Zeeb 
244a95659f7SEd Maste 	return (true);
24589ffc202SBjoern A. Zeeb }
24689ffc202SBjoern A. Zeeb 
24789ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU";
24889ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3;
24989ffc202SBjoern A. Zeeb 
25089ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = {
25189ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
25289ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
25389ffc202SBjoern A. Zeeb 	.hdr.n_type	= 1,
25489ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
25589ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
25689ffc202SBjoern A. Zeeb 	.trans_osrel	= kfreebsd_trans_osrel
25789ffc202SBjoern A. Zeeb };
25889ffc202SBjoern A. Zeeb 
259a95659f7SEd Maste static bool
26089ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
26189ffc202SBjoern A. Zeeb {
26289ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
26389ffc202SBjoern A. Zeeb 	uintptr_t p;
26489ffc202SBjoern A. Zeeb 
26589ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
2661b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
26789ffc202SBjoern A. Zeeb 
26889ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
26989ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
270a95659f7SEd Maste 		return (false);
27189ffc202SBjoern A. Zeeb 
27289ffc202SBjoern A. Zeeb 	/*
27389ffc202SBjoern A. Zeeb 	 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
27489ffc202SBjoern A. Zeeb 	 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
27589ffc202SBjoern A. Zeeb 	 */
27689ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
27789ffc202SBjoern A. Zeeb 
278a95659f7SEd Maste 	return (true);
27989ffc202SBjoern A. Zeeb }
28089ffc202SBjoern A. Zeeb 
281e1743d02SSøren Schmidt int
2823ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
283e1743d02SSøren Schmidt {
284e1743d02SSøren Schmidt 	int i;
285e1743d02SSøren Schmidt 
2863ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
287ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == NULL) {
288ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = entry;
289e1743d02SSøren Schmidt 			break;
290e1743d02SSøren Schmidt 		}
291e1743d02SSøren Schmidt 	}
292925c8b5bSBjoern A. Zeeb 	if (i == MAX_BRANDS) {
293925c8b5bSBjoern A. Zeeb 		printf("WARNING: %s: could not insert brandinfo entry: %p\n",
294925c8b5bSBjoern A. Zeeb 			__func__, entry);
295a7cddfedSJake Burkholder 		return (-1);
296925c8b5bSBjoern A. Zeeb 	}
297a7cddfedSJake Burkholder 	return (0);
298e1743d02SSøren Schmidt }
299e1743d02SSøren Schmidt 
300e1743d02SSøren Schmidt int
3013ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
302e1743d02SSøren Schmidt {
303e1743d02SSøren Schmidt 	int i;
304e1743d02SSøren Schmidt 
3053ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
306ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == entry) {
307ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = NULL;
308e1743d02SSøren Schmidt 			break;
309e1743d02SSøren Schmidt 		}
310e1743d02SSøren Schmidt 	}
311ea5a2b2eSSøren Schmidt 	if (i == MAX_BRANDS)
312a7cddfedSJake Burkholder 		return (-1);
313a7cddfedSJake Burkholder 	return (0);
314e1743d02SSøren Schmidt }
315e1743d02SSøren Schmidt 
3164082b189SAlex Richardson bool
3173ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry)
318096977faSMark Newton {
319096977faSMark Newton 	struct proc *p;
3204082b189SAlex Richardson 	bool rval = false;
321096977faSMark Newton 
3221005a129SJohn Baldwin 	sx_slock(&allproc_lock);
3234f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
324553629ebSJake Burkholder 		if (p->p_sysent == entry->sysvec) {
3254082b189SAlex Richardson 			rval = true;
326553629ebSJake Burkholder 			break;
327096977faSMark Newton 		}
328553629ebSJake Burkholder 	}
3291005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
330096977faSMark Newton 
331553629ebSJake Burkholder 	return (rval);
332096977faSMark Newton }
333096977faSMark Newton 
3345fe3ed62SJake Burkholder static Elf_Brandinfo *
33532c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
33609c78d53SEdward Tomasz Napierala     int32_t *osrel, uint32_t *fctl0)
3375fe3ed62SJake Burkholder {
33832c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
339af582aaeSKonstantin Belousov 	Elf_Brandinfo *bi, *bi_m;
34019621645SAlex Richardson 	bool ret, has_fctl0;
34109c78d53SEdward Tomasz Napierala 	int i, interp_name_len;
34209c78d53SEdward Tomasz Napierala 
343be7808dcSKonstantin Belousov 	interp_name_len = interp != NULL ? strlen(interp) + 1 : 0;
3445fe3ed62SJake Burkholder 
3455fe3ed62SJake Burkholder 	/*
34632c01de2SDmitry Chagin 	 * We support four types of branding -- (1) the ELF EI_OSABI field
3475fe3ed62SJake Burkholder 	 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
34832c01de2SDmitry Chagin 	 * branding w/in the ELF header, (3) path of the `interp_path'
34932c01de2SDmitry Chagin 	 * field, and (4) the ".note.ABI-tag" ELF section.
3505fe3ed62SJake Burkholder 	 */
3515fe3ed62SJake Burkholder 
35232c01de2SDmitry Chagin 	/* Look for an ".note.ABI-tag" ELF section */
353af582aaeSKonstantin Belousov 	bi_m = NULL;
35432c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
35532c01de2SDmitry Chagin 		bi = elf_brand_list[i];
356ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
357ecc2fda8SBjoern A. Zeeb 			continue;
3582274ab3dSKonstantin Belousov 		if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)
3591438fe3cSKonstantin Belousov 			continue;
360ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
361ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
3620cad2aa2SKonstantin Belousov 			has_fctl0 = false;
3630cad2aa2SKonstantin Belousov 			*fctl0 = 0;
3640cad2aa2SKonstantin Belousov 			*osrel = 0;
365cefb93f2SKonstantin Belousov 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel,
3660cad2aa2SKonstantin Belousov 			    &has_fctl0, fctl0);
367f19d421aSNathan Whitehorn 			/* Give brand a chance to veto check_note's guess */
3680cad2aa2SKonstantin Belousov 			if (ret && bi->header_supported) {
3690cad2aa2SKonstantin Belousov 				ret = bi->header_supported(imgp, osrel,
3700cad2aa2SKonstantin Belousov 				    has_fctl0 ? fctl0 : NULL);
3710cad2aa2SKonstantin Belousov 			}
372af582aaeSKonstantin Belousov 			/*
373af582aaeSKonstantin Belousov 			 * If note checker claimed the binary, but the
374af582aaeSKonstantin Belousov 			 * interpreter path in the image does not
375af582aaeSKonstantin Belousov 			 * match default one for the brand, try to
376af582aaeSKonstantin Belousov 			 * search for other brands with the same
377af582aaeSKonstantin Belousov 			 * interpreter.  Either there is better brand
378af582aaeSKonstantin Belousov 			 * with the right interpreter, or, failing
379af582aaeSKonstantin Belousov 			 * this, we return first brand which accepted
380af582aaeSKonstantin Belousov 			 * our note and, optionally, header.
381af582aaeSKonstantin Belousov 			 */
3823aeacc55SKonstantin Belousov 			if (ret && bi_m == NULL && interp != NULL &&
3833aeacc55SKonstantin Belousov 			    (bi->interp_path == NULL ||
3843aeacc55SKonstantin Belousov 			    (strlen(bi->interp_path) + 1 != interp_name_len ||
3853aeacc55SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
3863aeacc55SKonstantin Belousov 			    != 0))) {
387af582aaeSKonstantin Belousov 				bi_m = bi;
388af582aaeSKonstantin Belousov 				ret = 0;
389af582aaeSKonstantin Belousov 			}
39032c01de2SDmitry Chagin 			if (ret)
39132c01de2SDmitry Chagin 				return (bi);
39232c01de2SDmitry Chagin 		}
39332c01de2SDmitry Chagin 	}
394af582aaeSKonstantin Belousov 	if (bi_m != NULL)
395af582aaeSKonstantin Belousov 		return (bi_m);
39632c01de2SDmitry Chagin 
3975fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
3985fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3995fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
4001438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
4012274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
402ecc2fda8SBjoern A. Zeeb 			continue;
403ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
4045fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
4050fe98320SEd Schouten 		    (bi->compat_3_brand != NULL &&
4063d560b4bSKonstantin Belousov 		    strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
4070fe98320SEd Schouten 		    bi->compat_3_brand) == 0))) {
408686d2f31SNathan Whitehorn 			/* Looks good, but give brand a chance to veto */
409d722231bSJohn Baldwin 			if (bi->header_supported == NULL ||
4100cad2aa2SKonstantin Belousov 			    bi->header_supported(imgp, NULL, NULL)) {
41115a9aedfSKonstantin Belousov 				/*
41215a9aedfSKonstantin Belousov 				 * Again, prefer strictly matching
41315a9aedfSKonstantin Belousov 				 * interpreter path.
41415a9aedfSKonstantin Belousov 				 */
4157aab7a80SKonstantin Belousov 				if (interp_name_len == 0 &&
4167aab7a80SKonstantin Belousov 				    bi->interp_path == NULL)
4177aab7a80SKonstantin Belousov 					return (bi);
4187aab7a80SKonstantin Belousov 				if (bi->interp_path != NULL &&
4197aab7a80SKonstantin Belousov 				    strlen(bi->interp_path) + 1 ==
42015a9aedfSKonstantin Belousov 				    interp_name_len && strncmp(interp,
42115a9aedfSKonstantin Belousov 				    bi->interp_path, interp_name_len) == 0)
4225fe3ed62SJake Burkholder 					return (bi);
42315a9aedfSKonstantin Belousov 				if (bi_m == NULL)
42415a9aedfSKonstantin Belousov 					bi_m = bi;
4255fe3ed62SJake Burkholder 			}
426686d2f31SNathan Whitehorn 		}
42715a9aedfSKonstantin Belousov 	}
42815a9aedfSKonstantin Belousov 	if (bi_m != NULL)
42915a9aedfSKonstantin Belousov 		return (bi_m);
4305fe3ed62SJake Burkholder 
431817dc004SWarner Losh 	/* No known brand, see if the header is recognized by any brand */
432817dc004SWarner Losh 	for (i = 0; i < MAX_BRANDS; i++) {
433817dc004SWarner Losh 		bi = elf_brand_list[i];
434817dc004SWarner Losh 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
435817dc004SWarner Losh 		    bi->header_supported == NULL)
436817dc004SWarner Losh 			continue;
437817dc004SWarner Losh 		if (hdr->e_machine == bi->machine) {
4380cad2aa2SKonstantin Belousov 			ret = bi->header_supported(imgp, NULL, NULL);
439817dc004SWarner Losh 			if (ret)
440817dc004SWarner Losh 				return (bi);
441817dc004SWarner Losh 		}
442817dc004SWarner Losh 	}
443817dc004SWarner Losh 
4445fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
4455fe3ed62SJake Burkholder 	if (interp != NULL) {
4465fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
4475fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
4482274ab3dSKonstantin Belousov 			if (bi == NULL || (bi->flags &
4492274ab3dSKonstantin Belousov 			    (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC))
4502274ab3dSKonstantin Belousov 			    != 0)
451ecc2fda8SBjoern A. Zeeb 				continue;
452ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
4533aeacc55SKonstantin Belousov 			    bi->interp_path != NULL &&
454d1ae5c83SKonstantin Belousov 			    /* ELF image p_filesz includes terminating zero */
455d1ae5c83SKonstantin Belousov 			    strlen(bi->interp_path) + 1 == interp_name_len &&
456d1ae5c83SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
457d722231bSJohn Baldwin 			    == 0 && (bi->header_supported == NULL ||
4580cad2aa2SKonstantin Belousov 			    bi->header_supported(imgp, NULL, NULL)))
4595fe3ed62SJake Burkholder 				return (bi);
4605fe3ed62SJake Burkholder 		}
4615fe3ed62SJake Burkholder 	}
4625fe3ed62SJake Burkholder 
4635fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
4645fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
4655fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
4661438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
4672274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
468ecc2fda8SBjoern A. Zeeb 			continue;
469ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
470d722231bSJohn Baldwin 		    __elfN(fallback_brand) == bi->brand &&
471d722231bSJohn Baldwin 		    (bi->header_supported == NULL ||
4720cad2aa2SKonstantin Belousov 		    bi->header_supported(imgp, NULL, NULL)))
4735fe3ed62SJake Burkholder 			return (bi);
4745fe3ed62SJake Burkholder 	}
4755fe3ed62SJake Burkholder 	return (NULL);
4765fe3ed62SJake Burkholder }
4775fe3ed62SJake Burkholder 
4787de1bc13SKonstantin Belousov static bool
4797de1bc13SKonstantin Belousov __elfN(phdr_in_zero_page)(const Elf_Ehdr *hdr)
4807de1bc13SKonstantin Belousov {
4817de1bc13SKonstantin Belousov 	return (hdr->e_phoff <= PAGE_SIZE &&
4827de1bc13SKonstantin Belousov 	    (u_int)hdr->e_phentsize * hdr->e_phnum <= PAGE_SIZE - hdr->e_phoff);
4837de1bc13SKonstantin Belousov }
4847de1bc13SKonstantin Belousov 
485e1743d02SSøren Schmidt static int
4863ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
487e1743d02SSøren Schmidt {
488d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
4893ebc1248SPeter Wemm 	int i;
4903ebc1248SPeter Wemm 
49152c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
49252c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
49352c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
4943dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
4953dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
4963dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
497a7cddfedSJake Burkholder 		return (ENOEXEC);
498e1743d02SSøren Schmidt 
4993ebc1248SPeter Wemm 	/*
5003ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
5013ebc1248SPeter Wemm 	 */
5023ebc1248SPeter Wemm 
5033ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
504d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
505d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
5063ebc1248SPeter Wemm 			break;
5073ebc1248SPeter Wemm 	}
5083ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
509a7cddfedSJake Burkholder 		return (ENOEXEC);
510e1743d02SSøren Schmidt 
511a7cddfedSJake Burkholder 	return (0);
512e1743d02SSøren Schmidt }
513e1743d02SSøren Schmidt 
514e1743d02SSøren Schmidt static int
5153ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
516ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
5173ebc1248SPeter Wemm {
518da61b9a6SAlan Cox 	struct sf_buf *sf;
519da61b9a6SAlan Cox 	int error;
5203ebc1248SPeter Wemm 	vm_offset_t off;
5213ebc1248SPeter Wemm 
5223ebc1248SPeter Wemm 	/*
5233ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
5243ebc1248SPeter Wemm 	 */
525aaadc41fSKonstantin Belousov 	vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) -
526aaadc41fSKonstantin Belousov 	    trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL);
5273ebc1248SPeter Wemm 
5283ebc1248SPeter Wemm 	/*
5293ebc1248SPeter Wemm 	 * Find the page from the underlying object.
5303ebc1248SPeter Wemm 	 */
53128e8da65SAlan Cox 	if (object != NULL) {
532da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
533da61b9a6SAlan Cox 		if (sf == NULL)
534da61b9a6SAlan Cox 			return (KERN_FAILURE);
5353ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
536da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
537ca0387efSJake Burkholder 		    end - start);
538be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
539fe0a8a39SKonstantin Belousov 		if (error != 0)
540a7cddfedSJake Burkholder 			return (KERN_FAILURE);
5413ebc1248SPeter Wemm 	}
5423ebc1248SPeter Wemm 
543a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
5443ebc1248SPeter Wemm }
5453ebc1248SPeter Wemm 
5463ebc1248SPeter Wemm static int
547e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object,
548e3d8f8feSKonstantin Belousov     vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot,
549e3d8f8feSKonstantin Belousov     int cow)
5503ebc1248SPeter Wemm {
551da61b9a6SAlan Cox 	struct sf_buf *sf;
552da61b9a6SAlan Cox 	vm_offset_t off;
553a063facbSMarcel Moolenaar 	vm_size_t sz;
554e3d8f8feSKonstantin Belousov 	int error, locked, rv;
5553ebc1248SPeter Wemm 
5563ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
55781f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
558ff6f03c7SAlan Cox 		    round_page(start), prot);
55928e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
560a7cddfedSJake Burkholder 			return (rv);
5613ebc1248SPeter Wemm 		offset += round_page(start) - start;
5623ebc1248SPeter Wemm 		start = round_page(start);
5633ebc1248SPeter Wemm 	}
5643ebc1248SPeter Wemm 	if (end != round_page(end)) {
56581f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
566ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
56728e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
568a7cddfedSJake Burkholder 			return (rv);
5693ebc1248SPeter Wemm 		end = trunc_page(end);
5703ebc1248SPeter Wemm 	}
571e383e820SAlan Cox 	if (start >= end)
572e383e820SAlan Cox 		return (KERN_SUCCESS);
573e383e820SAlan Cox 	if ((offset & PAGE_MASK) != 0) {
5743ebc1248SPeter Wemm 		/*
575e383e820SAlan Cox 		 * The mapping is not page aligned.  This means that we have
576e383e820SAlan Cox 		 * to copy the data.
5773ebc1248SPeter Wemm 		 */
578aaadc41fSKonstantin Belousov 		rv = vm_map_fixed(map, NULL, 0, start, end - start,
579aaadc41fSKonstantin Belousov 		    prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL);
5805420f76bSKonstantin Belousov 		if (rv != KERN_SUCCESS)
581a7cddfedSJake Burkholder 			return (rv);
582da61b9a6SAlan Cox 		if (object == NULL)
583da61b9a6SAlan Cox 			return (KERN_SUCCESS);
584da61b9a6SAlan Cox 		for (; start < end; start += sz) {
585da61b9a6SAlan Cox 			sf = vm_imgact_map_page(object, offset);
586da61b9a6SAlan Cox 			if (sf == NULL)
587da61b9a6SAlan Cox 				return (KERN_FAILURE);
5883ebc1248SPeter Wemm 			off = offset - trunc_page(offset);
5893ebc1248SPeter Wemm 			sz = end - start;
590da61b9a6SAlan Cox 			if (sz > PAGE_SIZE - off)
591da61b9a6SAlan Cox 				sz = PAGE_SIZE - off;
592da61b9a6SAlan Cox 			error = copyout((caddr_t)sf_buf_kva(sf) + off,
5933ebc1248SPeter Wemm 			    (caddr_t)start, sz);
594be996836SAttilio Rao 			vm_imgact_unmap_page(sf);
5955420f76bSKonstantin Belousov 			if (error != 0)
596a7cddfedSJake Burkholder 				return (KERN_FAILURE);
597da61b9a6SAlan Cox 			offset += sz;
5983ebc1248SPeter Wemm 		}
5993ebc1248SPeter Wemm 	} else {
600e5e6093bSAlan Cox 		vm_object_reference(object);
601e383e820SAlan Cox 		rv = vm_map_fixed(map, object, offset, start, end - start,
60278022527SKonstantin Belousov 		    prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL |
60378022527SKonstantin Belousov 		    (object != NULL ? MAP_VN_EXEC : 0));
604e3d8f8feSKonstantin Belousov 		if (rv != KERN_SUCCESS) {
605e3d8f8feSKonstantin Belousov 			locked = VOP_ISLOCKED(imgp->vp);
606b249ce48SMateusz Guzik 			VOP_UNLOCK(imgp->vp);
607e5e6093bSAlan Cox 			vm_object_deallocate(object);
608e3d8f8feSKonstantin Belousov 			vn_lock(imgp->vp, locked | LK_RETRY);
609a7cddfedSJake Burkholder 			return (rv);
61078022527SKonstantin Belousov 		} else if (object != NULL) {
61178022527SKonstantin Belousov 			MPASS(imgp->vp->v_object == object);
61278022527SKonstantin Belousov 			VOP_SET_TEXT_CHECKED(imgp->vp);
6133ebc1248SPeter Wemm 		}
6143ebc1248SPeter Wemm 	}
615e383e820SAlan Cox 	return (KERN_SUCCESS);
616e383e820SAlan Cox }
6173ebc1248SPeter Wemm 
6183ebc1248SPeter Wemm static int
6190bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
6201699546dSEdward Tomasz Napierala     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
621e1743d02SSøren Schmidt {
622da61b9a6SAlan Cox 	struct sf_buf *sf;
623e1743d02SSøren Schmidt 	size_t map_len;
624292177e6SAlan Cox 	vm_map_t map;
625292177e6SAlan Cox 	vm_object_t object;
626e020a35fSMark Johnston 	vm_offset_t map_addr;
627fa7dd9c5SMatthew Dillon 	int error, rv, cow;
628e1743d02SSøren Schmidt 	size_t copy_len;
6290bbee4cdSKonstantin Belousov 	vm_ooffset_t file_addr;
63052c24af7SPeter Wemm 
63125ead034SBrian Feldman 	/*
63225ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
63325ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
63425ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
63525ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
63625ead034SBrian Feldman 	 *
63725ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
63825ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
63925ead034SBrian Feldman 	 */
6409bcf2f2dSKonstantin Belousov 	if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) ||
6419bcf2f2dSKonstantin Belousov 	    filsz > memsz) {
64225ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
64325ead034SBrian Feldman 		return (ENOEXEC);
64425ead034SBrian Feldman 	}
64525ead034SBrian Feldman 
646292177e6SAlan Cox 	object = imgp->object;
647292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
648545517f1SEdward Tomasz Napierala 	map_addr = trunc_page((vm_offset_t)vmaddr);
649545517f1SEdward Tomasz Napierala 	file_addr = trunc_page(offset);
650e1743d02SSøren Schmidt 
651e1743d02SSøren Schmidt 	/*
65252c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
65352c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
65452c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
65528e8da65SAlan Cox 	 * choose the second.
65652c24af7SPeter Wemm 	 */
6579bcf2f2dSKonstantin Belousov 	if (filsz == 0)
6589bcf2f2dSKonstantin Belousov 		map_len = 0;
6599bcf2f2dSKonstantin Belousov 	else if (memsz > filsz)
660545517f1SEdward Tomasz Napierala 		map_len = trunc_page(offset + filsz) - file_addr;
66152c24af7SPeter Wemm 	else
662545517f1SEdward Tomasz Napierala 		map_len = round_page(offset + filsz) - file_addr;
66352c24af7SPeter Wemm 
66452c24af7SPeter Wemm 	if (map_len != 0) {
665fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
666fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
667fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
668fa7dd9c5SMatthew Dillon 
66978022527SKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map, object, file_addr,
67078022527SKonstantin Belousov 		    map_addr, map_addr + map_len, prot, cow);
671e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
672a7cddfedSJake Burkholder 			return (EINVAL);
67352c24af7SPeter Wemm 
67452c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
675973d67c4SKonstantin Belousov 		if (memsz == filsz)
676a7cddfedSJake Burkholder 			return (0);
67752c24af7SPeter Wemm 	}
67852c24af7SPeter Wemm 
67952c24af7SPeter Wemm 	/*
68052c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
68152c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
68252c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
68352c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
684e1743d02SSøren Schmidt 	 */
685545517f1SEdward Tomasz Napierala 	copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page(offset +
686545517f1SEdward Tomasz Napierala 	    filsz);
687545517f1SEdward Tomasz Napierala 	map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
688545517f1SEdward Tomasz Napierala 	map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
689e1743d02SSøren Schmidt 
69052c24af7SPeter Wemm 	/* This had damn well better be true! */
6918191d577SPeter Wemm 	if (map_len != 0) {
692e3d8f8feSKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr,
693c547cbb4SAlan Cox 		    map_addr + map_len, prot, 0);
694973d67c4SKonstantin Belousov 		if (rv != KERN_SUCCESS)
695a7cddfedSJake Burkholder 			return (EINVAL);
6968191d577SPeter Wemm 	}
697e1743d02SSøren Schmidt 
69852c24af7SPeter Wemm 	if (copy_len != 0) {
699da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
700da61b9a6SAlan Cox 		if (sf == NULL)
701da61b9a6SAlan Cox 			return (EIO);
702e1743d02SSøren Schmidt 
70352c24af7SPeter Wemm 		/* send the page fragment to user space */
704e020a35fSMark Johnston 		error = copyout((caddr_t)sf_buf_kva(sf), (caddr_t)map_addr,
705e020a35fSMark Johnston 		    copy_len);
706be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
707973d67c4SKonstantin Belousov 		if (error != 0)
70852c24af7SPeter Wemm 			return (error);
70952c24af7SPeter Wemm 	}
710e1743d02SSøren Schmidt 
711e1743d02SSøren Schmidt 	/*
712c547cbb4SAlan Cox 	 * Remove write access to the page if it was only granted by map_insert
713c547cbb4SAlan Cox 	 * to allow copyout.
714e1743d02SSøren Schmidt 	 */
715c547cbb4SAlan Cox 	if ((prot & VM_PROT_WRITE) == 0)
716292177e6SAlan Cox 		vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
7170659df6fSKonstantin Belousov 		    map_len), prot, 0, VM_MAP_PROTECT_SET_PROT);
7188191d577SPeter Wemm 
719ff6f03c7SAlan Cox 	return (0);
720e1743d02SSøren Schmidt }
721e1743d02SSøren Schmidt 
7229bcd7482SEdward Tomasz Napierala static int
7239bcd7482SEdward Tomasz Napierala __elfN(load_sections)(struct image_params *imgp, const Elf_Ehdr *hdr,
7249bcd7482SEdward Tomasz Napierala     const Elf_Phdr *phdr, u_long rbase, u_long *base_addrp)
7259bcd7482SEdward Tomasz Napierala {
7269bcd7482SEdward Tomasz Napierala 	vm_prot_t prot;
7279bcd7482SEdward Tomasz Napierala 	u_long base_addr;
7289bcd7482SEdward Tomasz Napierala 	bool first;
7299bcd7482SEdward Tomasz Napierala 	int error, i;
7309bcd7482SEdward Tomasz Napierala 
731b65ca345SEdward Tomasz Napierala 	ASSERT_VOP_LOCKED(imgp->vp, __func__);
732b65ca345SEdward Tomasz Napierala 
7339bcd7482SEdward Tomasz Napierala 	base_addr = 0;
7349bcd7482SEdward Tomasz Napierala 	first = true;
7359bcd7482SEdward Tomasz Napierala 
7369bcd7482SEdward Tomasz Napierala 	for (i = 0; i < hdr->e_phnum; i++) {
7379bcd7482SEdward Tomasz Napierala 		if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
7389bcd7482SEdward Tomasz Napierala 			continue;
7399bcd7482SEdward Tomasz Napierala 
7409bcd7482SEdward Tomasz Napierala 		/* Loadable segment */
7419bcd7482SEdward Tomasz Napierala 		prot = __elfN(trans_prot)(phdr[i].p_flags);
7429bcd7482SEdward Tomasz Napierala 		error = __elfN(load_section)(imgp, phdr[i].p_offset,
7439bcd7482SEdward Tomasz Napierala 		    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
7449bcd7482SEdward Tomasz Napierala 		    phdr[i].p_memsz, phdr[i].p_filesz, prot);
7459bcd7482SEdward Tomasz Napierala 		if (error != 0)
7469bcd7482SEdward Tomasz Napierala 			return (error);
7479bcd7482SEdward Tomasz Napierala 
7489bcd7482SEdward Tomasz Napierala 		/*
7499bcd7482SEdward Tomasz Napierala 		 * Establish the base address if this is the first segment.
7509bcd7482SEdward Tomasz Napierala 		 */
7519bcd7482SEdward Tomasz Napierala 		if (first) {
7529bcd7482SEdward Tomasz Napierala   			base_addr = trunc_page(phdr[i].p_vaddr + rbase);
7539bcd7482SEdward Tomasz Napierala 			first = false;
7549bcd7482SEdward Tomasz Napierala 		}
7559bcd7482SEdward Tomasz Napierala 	}
7569bcd7482SEdward Tomasz Napierala 
7579bcd7482SEdward Tomasz Napierala 	if (base_addrp != NULL)
7589bcd7482SEdward Tomasz Napierala 		*base_addrp = base_addr;
7599bcd7482SEdward Tomasz Napierala 
7609bcd7482SEdward Tomasz Napierala 	return (0);
7619bcd7482SEdward Tomasz Napierala }
7629bcd7482SEdward Tomasz Napierala 
763c33fe779SJohn Polstra /*
764c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
765c33fe779SJohn Polstra  * or an executable.
766c33fe779SJohn Polstra  *
767c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
768c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
769c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
770c33fe779SJohn Polstra  * where the file was actually loaded.
771c33fe779SJohn Polstra  *
772c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
773c33fe779SJohn Polstra  * the entry point for the loaded file.
774c33fe779SJohn Polstra  */
775e1743d02SSøren Schmidt static int
7763ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
7771699546dSEdward Tomasz Napierala 	u_long *entry)
778e1743d02SSøren Schmidt {
779911c2be0SMark Peek 	struct {
780911c2be0SMark Peek 		struct nameidata nd;
781911c2be0SMark Peek 		struct vattr attr;
782911c2be0SMark Peek 		struct image_params image_params;
783911c2be0SMark Peek 	} *tempdata;
784d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
785d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
786911c2be0SMark Peek 	struct nameidata *nd;
787911c2be0SMark Peek 	struct vattr *attr;
788911c2be0SMark Peek 	struct image_params *imgp;
78978022527SKonstantin Belousov 	u_long rbase;
790c33fe779SJohn Polstra 	u_long base_addr = 0;
7919bcd7482SEdward Tomasz Napierala 	int error;
792e1743d02SSøren Schmidt 
79312bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
79412bc222eSJonathan Anderson 	/*
79512bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
79612bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
79712bc222eSJonathan Anderson 	 */
79812bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
79912bc222eSJonathan Anderson 		return (ECAPMODE);
80012bc222eSJonathan Anderson #endif
80112bc222eSJonathan Anderson 
8021073d17eSKonstantin Belousov 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK | M_ZERO);
803911c2be0SMark Peek 	nd = &tempdata->nd;
804911c2be0SMark Peek 	attr = &tempdata->attr;
805911c2be0SMark Peek 	imgp = &tempdata->image_params;
806911c2be0SMark Peek 
807c8a79999SPeter Wemm 	/*
808c8a79999SPeter Wemm 	 * Initialize part of the common data
809c8a79999SPeter Wemm 	 */
810c8a79999SPeter Wemm 	imgp->proc = p;
811911c2be0SMark Peek 	imgp->attr = attr;
812c8a79999SPeter Wemm 
813f422bc30SJohn Baldwin 	NDINIT(nd, LOOKUP, ISOPEN | FOLLOW | LOCKSHARED | LOCKLEAF,
8147e1d3eefSMateusz Guzik 	    UIO_SYSSPACE, file);
815911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
816911c2be0SMark Peek 		nd->ni_vp = NULL;
817e1743d02SSøren Schmidt 		goto fail;
818e1743d02SSøren Schmidt 	}
819911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
820911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
821c8a79999SPeter Wemm 
822e1743d02SSøren Schmidt 	/*
823e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
824e1743d02SSøren Schmidt 	 */
825c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
826373d1a3fSAlan Cox 	if (error)
827c8a79999SPeter Wemm 		goto fail;
828e1743d02SSøren Schmidt 
829c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
830373d1a3fSAlan Cox 	if (error)
831373d1a3fSAlan Cox 		goto fail;
832373d1a3fSAlan Cox 
8338516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
834e1743d02SSøren Schmidt 
835d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
8363ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
837e1743d02SSøren Schmidt 		goto fail;
838c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
839c33fe779SJohn Polstra 		rbase = *addr;
840c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
841c33fe779SJohn Polstra 		rbase = 0;
842c33fe779SJohn Polstra 	else {
843c33fe779SJohn Polstra 		error = ENOEXEC;
844c33fe779SJohn Polstra 		goto fail;
845c33fe779SJohn Polstra 	}
846e1743d02SSøren Schmidt 
847c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
8487de1bc13SKonstantin Belousov 	if (!__elfN(phdr_in_zero_page)(hdr)) {
849c8a79999SPeter Wemm 		error = ENOEXEC;
850e1743d02SSøren Schmidt 		goto fail;
851c8a79999SPeter Wemm 	}
852c8a79999SPeter Wemm 
853d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
85493d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
85593d1c728SKonstantin Belousov 		error = ENOEXEC;
85693d1c728SKonstantin Belousov 		goto fail;
85793d1c728SKonstantin Belousov 	}
858e1743d02SSøren Schmidt 
8599bcd7482SEdward Tomasz Napierala 	error = __elfN(load_sections)(imgp, hdr, phdr, rbase, &base_addr);
860292177e6SAlan Cox 	if (error != 0)
861e1743d02SSøren Schmidt 		goto fail;
8629bcd7482SEdward Tomasz Napierala 
863c33fe779SJohn Polstra 	*addr = base_addr;
864c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
865e1743d02SSøren Schmidt 
866e1743d02SSøren Schmidt fail:
867c8a79999SPeter Wemm 	if (imgp->firstpage)
868c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
8690b2ed1aeSJeff Roberson 
87078022527SKonstantin Belousov 	if (nd->ni_vp) {
87178022527SKonstantin Belousov 		if (imgp->textset)
87278022527SKonstantin Belousov 			VOP_UNSET_TEXT_CHECKED(nd->ni_vp);
873373d1a3fSAlan Cox 		vput(nd->ni_vp);
87478022527SKonstantin Belousov 	}
875911c2be0SMark Peek 	free(tempdata, M_TEMP);
876e1743d02SSøren Schmidt 
877a7cddfedSJake Burkholder 	return (error);
878e1743d02SSøren Schmidt }
879e1743d02SSøren Schmidt 
880a4007ae1SKonstantin Belousov /*
881a4007ae1SKonstantin Belousov  * Select randomized valid address in the map map, between minv and
882a4007ae1SKonstantin Belousov  * maxv, with specified alignment.  The [minv, maxv) range must belong
883a4007ae1SKonstantin Belousov  * to the map.  Note that function only allocates the address, it is
884a4007ae1SKonstantin Belousov  * up to caller to clamp maxv in a way that the final allocation
885a4007ae1SKonstantin Belousov  * length fit into the map.
886a4007ae1SKonstantin Belousov  *
887a4007ae1SKonstantin Belousov  * Result is returned in *resp, error code indicates that arguments
888a4007ae1SKonstantin Belousov  * did not pass sanity checks for overflow and range correctness.
889a4007ae1SKonstantin Belousov  */
8909cf78c1cSKonstantin Belousov static int
8919cf78c1cSKonstantin Belousov __CONCAT(rnd_, __elfN(base))(vm_map_t map, u_long minv, u_long maxv,
8929cf78c1cSKonstantin Belousov     u_int align, u_long *resp)
893fa50a355SKonstantin Belousov {
894fa50a355SKonstantin Belousov 	u_long rbase, res;
895fa50a355SKonstantin Belousov 
896fa50a355SKonstantin Belousov 	MPASS(vm_map_min(map) <= minv);
8979cf78c1cSKonstantin Belousov 
8989cf78c1cSKonstantin Belousov 	if (minv >= maxv || minv + align >= maxv || maxv > vm_map_max(map)) {
8999cf78c1cSKonstantin Belousov 		uprintf("Invalid ELF segments layout\n");
9009cf78c1cSKonstantin Belousov 		return (ENOEXEC);
9019cf78c1cSKonstantin Belousov 	}
9029cf78c1cSKonstantin Belousov 
903fa50a355SKonstantin Belousov 	arc4rand(&rbase, sizeof(rbase), 0);
904fa50a355SKonstantin Belousov 	res = roundup(minv, (u_long)align) + rbase % (maxv - minv);
905fa50a355SKonstantin Belousov 	res &= ~((u_long)align - 1);
906fa50a355SKonstantin Belousov 	if (res >= maxv)
907fa50a355SKonstantin Belousov 		res -= align;
9089cf78c1cSKonstantin Belousov 
909fa50a355SKonstantin Belousov 	KASSERT(res >= minv,
910fa50a355SKonstantin Belousov 	    ("res %#lx < minv %#lx, maxv %#lx rbase %#lx",
911fa50a355SKonstantin Belousov 	    res, minv, maxv, rbase));
912fa50a355SKonstantin Belousov 	KASSERT(res < maxv,
913fa50a355SKonstantin Belousov 	    ("res %#lx > maxv %#lx, minv %#lx rbase %#lx",
914fa50a355SKonstantin Belousov 	    res, maxv, minv, rbase));
9159cf78c1cSKonstantin Belousov 
9169cf78c1cSKonstantin Belousov 	*resp = res;
9179cf78c1cSKonstantin Belousov 	return (0);
918fa50a355SKonstantin Belousov }
919fa50a355SKonstantin Belousov 
92020e1174aSEdward Tomasz Napierala static int
92120e1174aSEdward Tomasz Napierala __elfN(enforce_limits)(struct image_params *imgp, const Elf_Ehdr *hdr,
92220e1174aSEdward Tomasz Napierala     const Elf_Phdr *phdr, u_long et_dyn_addr)
92320e1174aSEdward Tomasz Napierala {
92420e1174aSEdward Tomasz Napierala 	struct vmspace *vmspace;
92520e1174aSEdward Tomasz Napierala 	const char *err_str;
92620e1174aSEdward Tomasz Napierala 	u_long text_size, data_size, total_size, text_addr, data_addr;
92720e1174aSEdward Tomasz Napierala 	u_long seg_size, seg_addr;
92820e1174aSEdward Tomasz Napierala 	int i;
92920e1174aSEdward Tomasz Napierala 
93020e1174aSEdward Tomasz Napierala 	err_str = NULL;
93120e1174aSEdward Tomasz Napierala 	text_size = data_size = total_size = text_addr = data_addr = 0;
93220e1174aSEdward Tomasz Napierala 
93320e1174aSEdward Tomasz Napierala 	for (i = 0; i < hdr->e_phnum; i++) {
93420e1174aSEdward Tomasz Napierala 		if (phdr[i].p_type != PT_LOAD || phdr[i].p_memsz == 0)
93520e1174aSEdward Tomasz Napierala 			continue;
93620e1174aSEdward Tomasz Napierala 
93720e1174aSEdward Tomasz Napierala 		seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
93820e1174aSEdward Tomasz Napierala 		seg_size = round_page(phdr[i].p_memsz +
93920e1174aSEdward Tomasz Napierala 		    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
94020e1174aSEdward Tomasz Napierala 
94120e1174aSEdward Tomasz Napierala 		/*
94220e1174aSEdward Tomasz Napierala 		 * Make the largest executable segment the official
94320e1174aSEdward Tomasz Napierala 		 * text segment and all others data.
94420e1174aSEdward Tomasz Napierala 		 *
94520e1174aSEdward Tomasz Napierala 		 * Note that obreak() assumes that data_addr + data_size == end
94620e1174aSEdward Tomasz Napierala 		 * of data load area, and the ELF file format expects segments
94720e1174aSEdward Tomasz Napierala 		 * to be sorted by address.  If multiple data segments exist,
94820e1174aSEdward Tomasz Napierala 		 * the last one will be used.
94920e1174aSEdward Tomasz Napierala 		 */
95020e1174aSEdward Tomasz Napierala 
95120e1174aSEdward Tomasz Napierala 		if ((phdr[i].p_flags & PF_X) != 0 && text_size < seg_size) {
95220e1174aSEdward Tomasz Napierala 			text_size = seg_size;
95320e1174aSEdward Tomasz Napierala 			text_addr = seg_addr;
95420e1174aSEdward Tomasz Napierala 		} else {
95520e1174aSEdward Tomasz Napierala 			data_size = seg_size;
95620e1174aSEdward Tomasz Napierala 			data_addr = seg_addr;
95720e1174aSEdward Tomasz Napierala 		}
95820e1174aSEdward Tomasz Napierala 		total_size += seg_size;
95920e1174aSEdward Tomasz Napierala 	}
96020e1174aSEdward Tomasz Napierala 
96120e1174aSEdward Tomasz Napierala 	if (data_addr == 0 && data_size == 0) {
96220e1174aSEdward Tomasz Napierala 		data_addr = text_addr;
96320e1174aSEdward Tomasz Napierala 		data_size = text_size;
96420e1174aSEdward Tomasz Napierala 	}
96520e1174aSEdward Tomasz Napierala 
96620e1174aSEdward Tomasz Napierala 	/*
96720e1174aSEdward Tomasz Napierala 	 * Check limits.  It should be safe to check the
96820e1174aSEdward Tomasz Napierala 	 * limits after loading the segments since we do
96920e1174aSEdward Tomasz Napierala 	 * not actually fault in all the segments pages.
97020e1174aSEdward Tomasz Napierala 	 */
97120e1174aSEdward Tomasz Napierala 	PROC_LOCK(imgp->proc);
97220e1174aSEdward Tomasz Napierala 	if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
97320e1174aSEdward Tomasz Napierala 		err_str = "Data segment size exceeds process limit";
97420e1174aSEdward Tomasz Napierala 	else if (text_size > maxtsiz)
97520e1174aSEdward Tomasz Napierala 		err_str = "Text segment size exceeds system limit";
97620e1174aSEdward Tomasz Napierala 	else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
97720e1174aSEdward Tomasz Napierala 		err_str = "Total segment size exceeds process limit";
97820e1174aSEdward Tomasz Napierala 	else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
97920e1174aSEdward Tomasz Napierala 		err_str = "Data segment size exceeds resource limit";
98020e1174aSEdward Tomasz Napierala 	else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
98120e1174aSEdward Tomasz Napierala 		err_str = "Total segment size exceeds resource limit";
98220e1174aSEdward Tomasz Napierala 	PROC_UNLOCK(imgp->proc);
98320e1174aSEdward Tomasz Napierala 	if (err_str != NULL) {
98420e1174aSEdward Tomasz Napierala 		uprintf("%s\n", err_str);
98520e1174aSEdward Tomasz Napierala 		return (ENOMEM);
98620e1174aSEdward Tomasz Napierala 	}
98720e1174aSEdward Tomasz Napierala 
98820e1174aSEdward Tomasz Napierala 	vmspace = imgp->proc->p_vmspace;
98920e1174aSEdward Tomasz Napierala 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
99020e1174aSEdward Tomasz Napierala 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
99120e1174aSEdward Tomasz Napierala 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
99220e1174aSEdward Tomasz Napierala 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
99320e1174aSEdward Tomasz Napierala 
99420e1174aSEdward Tomasz Napierala 	return (0);
99520e1174aSEdward Tomasz Napierala }
99620e1174aSEdward Tomasz Napierala 
99709c78d53SEdward Tomasz Napierala static int
99809c78d53SEdward Tomasz Napierala __elfN(get_interp)(struct image_params *imgp, const Elf_Phdr *phdr,
99909c78d53SEdward Tomasz Napierala     char **interpp, bool *free_interpp)
100009c78d53SEdward Tomasz Napierala {
100109c78d53SEdward Tomasz Napierala 	struct thread *td;
100209c78d53SEdward Tomasz Napierala 	char *interp;
100309c78d53SEdward Tomasz Napierala 	int error, interp_name_len;
100409c78d53SEdward Tomasz Napierala 
100509c78d53SEdward Tomasz Napierala 	KASSERT(phdr->p_type == PT_INTERP,
100609c78d53SEdward Tomasz Napierala 	    ("%s: p_type %u != PT_INTERP", __func__, phdr->p_type));
1007b65ca345SEdward Tomasz Napierala 	ASSERT_VOP_LOCKED(imgp->vp, __func__);
100809c78d53SEdward Tomasz Napierala 
100909c78d53SEdward Tomasz Napierala 	td = curthread;
101009c78d53SEdward Tomasz Napierala 
101109c78d53SEdward Tomasz Napierala 	/* Path to interpreter */
101209c78d53SEdward Tomasz Napierala 	if (phdr->p_filesz < 2 || phdr->p_filesz > MAXPATHLEN) {
101309c78d53SEdward Tomasz Napierala 		uprintf("Invalid PT_INTERP\n");
101409c78d53SEdward Tomasz Napierala 		return (ENOEXEC);
101509c78d53SEdward Tomasz Napierala 	}
101609c78d53SEdward Tomasz Napierala 
101709c78d53SEdward Tomasz Napierala 	interp_name_len = phdr->p_filesz;
101809c78d53SEdward Tomasz Napierala 	if (phdr->p_offset > PAGE_SIZE ||
101909c78d53SEdward Tomasz Napierala 	    interp_name_len > PAGE_SIZE - phdr->p_offset) {
10200ddfdc60SKonstantin Belousov 		/*
1021f1f81d3bSKonstantin Belousov 		 * The vnode lock might be needed by the pagedaemon to
10220ddfdc60SKonstantin Belousov 		 * clean pages owned by the vnode.  Do not allow sleep
10230ddfdc60SKonstantin Belousov 		 * waiting for memory with the vnode locked, instead
10240ddfdc60SKonstantin Belousov 		 * try non-sleepable allocation first, and if it
10250ddfdc60SKonstantin Belousov 		 * fails, go to the slow path were we drop the lock
1026f1f81d3bSKonstantin Belousov 		 * and do M_WAITOK.  A text reference prevents
1027f1f81d3bSKonstantin Belousov 		 * modifications to the vnode content.
10280ddfdc60SKonstantin Belousov 		 */
10292d6b8546SKonstantin Belousov 		interp = malloc(interp_name_len + 1, M_TEMP, M_NOWAIT);
10302d6b8546SKonstantin Belousov 		if (interp == NULL) {
1031b249ce48SMateusz Guzik 			VOP_UNLOCK(imgp->vp);
103209c78d53SEdward Tomasz Napierala 			interp = malloc(interp_name_len + 1, M_TEMP, M_WAITOK);
103378022527SKonstantin Belousov 			vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
10342d6b8546SKonstantin Belousov 		}
10350ddfdc60SKonstantin Belousov 
103609c78d53SEdward Tomasz Napierala 		error = vn_rdwr(UIO_READ, imgp->vp, interp,
103709c78d53SEdward Tomasz Napierala 		    interp_name_len, phdr->p_offset,
103809c78d53SEdward Tomasz Napierala 		    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
103909c78d53SEdward Tomasz Napierala 		    NOCRED, NULL, td);
104009c78d53SEdward Tomasz Napierala 		if (error != 0) {
104109c78d53SEdward Tomasz Napierala 			free(interp, M_TEMP);
104209c78d53SEdward Tomasz Napierala 			uprintf("i/o error PT_INTERP %d\n", error);
104309c78d53SEdward Tomasz Napierala 			return (error);
104409c78d53SEdward Tomasz Napierala 		}
104509c78d53SEdward Tomasz Napierala 		interp[interp_name_len] = '\0';
104609c78d53SEdward Tomasz Napierala 
104709c78d53SEdward Tomasz Napierala 		*interpp = interp;
104809c78d53SEdward Tomasz Napierala 		*free_interpp = true;
104909c78d53SEdward Tomasz Napierala 		return (0);
105009c78d53SEdward Tomasz Napierala 	}
105109c78d53SEdward Tomasz Napierala 
105209c78d53SEdward Tomasz Napierala 	interp = __DECONST(char *, imgp->image_header) + phdr->p_offset;
105309c78d53SEdward Tomasz Napierala 	if (interp[interp_name_len - 1] != '\0') {
105409c78d53SEdward Tomasz Napierala 		uprintf("Invalid PT_INTERP\n");
105509c78d53SEdward Tomasz Napierala 		return (ENOEXEC);
105609c78d53SEdward Tomasz Napierala 	}
105709c78d53SEdward Tomasz Napierala 
105809c78d53SEdward Tomasz Napierala 	*interpp = interp;
105909c78d53SEdward Tomasz Napierala 	*free_interpp = false;
106009c78d53SEdward Tomasz Napierala 	return (0);
106109c78d53SEdward Tomasz Napierala }
106209c78d53SEdward Tomasz Napierala 
10639274fb35SEdward Tomasz Napierala static int
10649274fb35SEdward Tomasz Napierala __elfN(load_interp)(struct image_params *imgp, const Elf_Brandinfo *brand_info,
10659274fb35SEdward Tomasz Napierala     const char *interp, u_long *addr, u_long *entry)
10669274fb35SEdward Tomasz Napierala {
10679274fb35SEdward Tomasz Napierala 	char *path;
10689274fb35SEdward Tomasz Napierala 	int error;
10699274fb35SEdward Tomasz Napierala 
10709274fb35SEdward Tomasz Napierala 	if (brand_info->emul_path != NULL &&
10719274fb35SEdward Tomasz Napierala 	    brand_info->emul_path[0] != '\0') {
10729274fb35SEdward Tomasz Napierala 		path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
10739274fb35SEdward Tomasz Napierala 		snprintf(path, MAXPATHLEN, "%s%s",
10749274fb35SEdward Tomasz Napierala 		    brand_info->emul_path, interp);
10759274fb35SEdward Tomasz Napierala 		error = __elfN(load_file)(imgp->proc, path, addr, entry);
10769274fb35SEdward Tomasz Napierala 		free(path, M_TEMP);
10779274fb35SEdward Tomasz Napierala 		if (error == 0)
10789274fb35SEdward Tomasz Napierala 			return (0);
10799274fb35SEdward Tomasz Napierala 	}
10809274fb35SEdward Tomasz Napierala 
10819274fb35SEdward Tomasz Napierala 	if (brand_info->interp_newpath != NULL &&
10829274fb35SEdward Tomasz Napierala 	    (brand_info->interp_path == NULL ||
10839274fb35SEdward Tomasz Napierala 	    strcmp(interp, brand_info->interp_path) == 0)) {
10849274fb35SEdward Tomasz Napierala 		error = __elfN(load_file)(imgp->proc,
10859274fb35SEdward Tomasz Napierala 		    brand_info->interp_newpath, addr, entry);
10869274fb35SEdward Tomasz Napierala 		if (error == 0)
10879274fb35SEdward Tomasz Napierala 			return (0);
10889274fb35SEdward Tomasz Napierala 	}
10899274fb35SEdward Tomasz Napierala 
10909274fb35SEdward Tomasz Napierala 	error = __elfN(load_file)(imgp->proc, interp, addr, entry);
10919274fb35SEdward Tomasz Napierala 	if (error == 0)
10929274fb35SEdward Tomasz Napierala 		return (0);
10939274fb35SEdward Tomasz Napierala 
10949274fb35SEdward Tomasz Napierala 	uprintf("ELF interpreter %s not found, error %d\n", interp, error);
10959274fb35SEdward Tomasz Napierala 	return (error);
10969274fb35SEdward Tomasz Napierala }
10979274fb35SEdward Tomasz Napierala 
1098fa50a355SKonstantin Belousov /*
1099fa50a355SKonstantin Belousov  * Impossible et_dyn_addr initial value indicating that the real base
1100fa50a355SKonstantin Belousov  * must be calculated later with some randomization applied.
1101fa50a355SKonstantin Belousov  */
1102fa50a355SKonstantin Belousov #define	ET_DYN_ADDR_RAND	1
1103fa50a355SKonstantin Belousov 
1104303b270bSEivind Eklund static int
11053ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
1106e1743d02SSøren Schmidt {
11076c775eb6SKonstantin Belousov 	struct thread *td;
11086c775eb6SKonstantin Belousov 	const Elf_Ehdr *hdr;
110932c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
1110e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
11115856e12eSJohn Dyson 	struct vmspace *vmspace;
1112fa50a355SKonstantin Belousov 	vm_map_t map;
11139274fb35SEdward Tomasz Napierala 	char *interp;
1114d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
11155fe3ed62SJake Burkholder 	struct sysentvec *sv;
111620e1174aSEdward Tomasz Napierala 	u_long addr, baddr, et_dyn_addr, entry, proghdr;
1117e499988fSKonstantin Belousov 	u_long maxalign, maxsalign, mapsz, maxv, maxv1, anon_loc;
1118cefb93f2SKonstantin Belousov 	uint32_t fctl0;
11196c775eb6SKonstantin Belousov 	int32_t osrel;
112009c78d53SEdward Tomasz Napierala 	bool free_interp;
11219274fb35SEdward Tomasz Napierala 	int error, i, n;
11226c775eb6SKonstantin Belousov 
11236c775eb6SKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
1124e1743d02SSøren Schmidt 
1125e1743d02SSøren Schmidt 	/*
1126e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
1127900b28f9SMaxim Sobolev 	 *
1128900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
1129900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
1130e1743d02SSøren Schmidt 	 */
1131900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
1132900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
1133a7cddfedSJake Burkholder 		return (-1);
1134e1743d02SSøren Schmidt 
1135e1743d02SSøren Schmidt 	/*
1136e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
1137e1743d02SSøren Schmidt 	 * detected an ELF file.
1138e1743d02SSøren Schmidt 	 */
1139e1743d02SSøren Schmidt 
11407de1bc13SKonstantin Belousov 	if (!__elfN(phdr_in_zero_page)(hdr)) {
11416b16d664SEd Maste 		uprintf("Program headers not in the first page\n");
1142a7cddfedSJake Burkholder 		return (ENOEXEC);
1143e1743d02SSøren Schmidt 	}
114452c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
11456b16d664SEd Maste 	if (!aligned(phdr, Elf_Addr)) {
11466b16d664SEd Maste 		uprintf("Unaligned program headers\n");
114793d1c728SKonstantin Belousov 		return (ENOEXEC);
11486b16d664SEd Maste 	}
11496c775eb6SKonstantin Belousov 
11506c775eb6SKonstantin Belousov 	n = error = 0;
11517564c4adSKonstantin Belousov 	baddr = 0;
11526c775eb6SKonstantin Belousov 	osrel = 0;
1153cefb93f2SKonstantin Belousov 	fctl0 = 0;
11546c775eb6SKonstantin Belousov 	entry = proghdr = 0;
11559274fb35SEdward Tomasz Napierala 	interp = NULL;
115609c78d53SEdward Tomasz Napierala 	free_interp = false;
11576c775eb6SKonstantin Belousov 	td = curthread;
1158714d6d09SKonstantin Belousov 
1159714d6d09SKonstantin Belousov 	/*
1160714d6d09SKonstantin Belousov 	 * Somewhat arbitrary, limit accepted max alignment for the
1161714d6d09SKonstantin Belousov 	 * loadable segment to the max supported superpage size. Too
1162714d6d09SKonstantin Belousov 	 * large alignment requests are not useful and are indicators
1163714d6d09SKonstantin Belousov 	 * of corrupted or outright malicious binary.
1164714d6d09SKonstantin Belousov 	 */
1165fa50a355SKonstantin Belousov 	maxalign = PAGE_SIZE;
1166714d6d09SKonstantin Belousov 	maxsalign = PAGE_SIZE * 1024;
1167714d6d09SKonstantin Belousov 	for (i = MAXPAGESIZES - 1; i > 0; i--) {
1168714d6d09SKonstantin Belousov 		if (pagesizes[i] > maxsalign)
1169714d6d09SKonstantin Belousov 			maxsalign = pagesizes[i];
1170714d6d09SKonstantin Belousov 	}
1171714d6d09SKonstantin Belousov 
1172fa50a355SKonstantin Belousov 	mapsz = 0;
11736c775eb6SKonstantin Belousov 
11745fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
1175291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
1176291c06a1SKonstantin Belousov 		case PT_LOAD:
11777564c4adSKonstantin Belousov 			if (n == 0)
11787564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
117936df8f54SKonstantin Belousov 			if (!powerof2(phdr[i].p_align) ||
118036df8f54SKonstantin Belousov 			    phdr[i].p_align > maxsalign) {
1181714d6d09SKonstantin Belousov 				uprintf("Invalid segment alignment\n");
1182714d6d09SKonstantin Belousov 				error = ENOEXEC;
1183714d6d09SKonstantin Belousov 				goto ret;
1184714d6d09SKonstantin Belousov 			}
1185fa50a355SKonstantin Belousov 			if (phdr[i].p_align > maxalign)
1186fa50a355SKonstantin Belousov 				maxalign = phdr[i].p_align;
1187bf839416SKonstantin Belousov 			if (mapsz + phdr[i].p_memsz < mapsz) {
1188bf839416SKonstantin Belousov 				uprintf("Mapsize overflow\n");
1189bf839416SKonstantin Belousov 				error = ENOEXEC;
1190bf839416SKonstantin Belousov 				goto ret;
1191bf839416SKonstantin Belousov 			}
1192fa50a355SKonstantin Belousov 			mapsz += phdr[i].p_memsz;
11937564c4adSKonstantin Belousov 			n++;
11949bcd7482SEdward Tomasz Napierala 
11959bcd7482SEdward Tomasz Napierala 			/*
11969bcd7482SEdward Tomasz Napierala 			 * If this segment contains the program headers,
11979bcd7482SEdward Tomasz Napierala 			 * remember their virtual address for the AT_PHDR
11989bcd7482SEdward Tomasz Napierala 			 * aux entry. Static binaries don't usually include
11999bcd7482SEdward Tomasz Napierala 			 * a PT_PHDR entry.
12009bcd7482SEdward Tomasz Napierala 			 */
12019bcd7482SEdward Tomasz Napierala 			if (phdr[i].p_offset == 0 &&
120288dd7a0aSKonstantin Belousov 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize <=
120388dd7a0aSKonstantin Belousov 			    phdr[i].p_filesz)
12049bcd7482SEdward Tomasz Napierala 				proghdr = phdr[i].p_vaddr + hdr->e_phoff;
1205291c06a1SKonstantin Belousov 			break;
1206291c06a1SKonstantin Belousov 		case PT_INTERP:
1207e5e6093bSAlan Cox 			/* Path to interpreter */
1208d3ee0a15SJonathan T. Looney 			if (interp != NULL) {
1209d3ee0a15SJonathan T. Looney 				uprintf("Multiple PT_INTERP headers\n");
1210d3ee0a15SJonathan T. Looney 				error = ENOEXEC;
1211d3ee0a15SJonathan T. Looney 				goto ret;
1212d3ee0a15SJonathan T. Looney 			}
121309c78d53SEdward Tomasz Napierala 			error = __elfN(get_interp)(imgp, &phdr[i], &interp,
121409c78d53SEdward Tomasz Napierala 			    &free_interp);
121509c78d53SEdward Tomasz Napierala 			if (error != 0)
12166c775eb6SKonstantin Belousov 				goto ret;
1217291c06a1SKonstantin Belousov 			break;
1218291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
1219291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
1220291c06a1SKonstantin Belousov 				imgp->stack_prot =
1221291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
1222316b3843SKonstantin Belousov 			imgp->stack_sz = phdr[i].p_memsz;
1223291c06a1SKonstantin Belousov 			break;
12249bcd7482SEdward Tomasz Napierala 		case PT_PHDR: 	/* Program header table info */
12259bcd7482SEdward Tomasz Napierala 			proghdr = phdr[i].p_vaddr;
12269bcd7482SEdward Tomasz Napierala 			break;
12273ebc1248SPeter Wemm 		}
12283ebc1248SPeter Wemm 	}
12293ebc1248SPeter Wemm 
123009c78d53SEdward Tomasz Napierala 	brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel, &fctl0);
12315fe3ed62SJake Burkholder 	if (brand_info == NULL) {
12325fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
12335fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
12346c775eb6SKonstantin Belousov 		error = ENOEXEC;
12356c775eb6SKonstantin Belousov 		goto ret;
12363ebc1248SPeter Wemm 	}
1237fa50a355SKonstantin Belousov 	sv = brand_info->sysvec;
123877ebe276SEd Maste 	et_dyn_addr = 0;
1239ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
12406b16d664SEd Maste 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
12416b16d664SEd Maste 			uprintf("Cannot execute shared object\n");
12426c775eb6SKonstantin Belousov 			error = ENOEXEC;
12436c775eb6SKonstantin Belousov 			goto ret;
12446b16d664SEd Maste 		}
12457564c4adSKonstantin Belousov 		/*
12467564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
12477564c4adSKonstantin Belousov 		 * non-zero for some reason.
12487564c4adSKonstantin Belousov 		 */
1249fa50a355SKonstantin Belousov 		if (baddr == 0) {
1250fa50a355SKonstantin Belousov 			if ((sv->sv_flags & SV_ASLR) == 0 ||
1251fa50a355SKonstantin Belousov 			    (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0)
125295aafd69SKonstantin Belousov 				et_dyn_addr = __elfN(pie_base);
1253fa50a355SKonstantin Belousov 			else if ((__elfN(pie_aslr_enabled) &&
1254fa50a355SKonstantin Belousov 			    (imgp->proc->p_flag2 & P2_ASLR_DISABLE) == 0) ||
1255fa50a355SKonstantin Belousov 			    (imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0)
1256fa50a355SKonstantin Belousov 				et_dyn_addr = ET_DYN_ADDR_RAND;
1257fa50a355SKonstantin Belousov 			else
125895aafd69SKonstantin Belousov 				et_dyn_addr = __elfN(pie_base);
125977ebe276SEd Maste 		}
1260fa50a355SKonstantin Belousov 	}
12613ebc1248SPeter Wemm 
126260bb3943SAlan Cox 	/*
126360bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
126460bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
126560bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
126660bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
126760bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
126860bb3943SAlan Cox 	 * v_usecount may become zero.
12691dfab802SAlan Cox 	 *
12701dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
12711dfab802SAlan Cox 	 * the vnode is unlocked.
127260bb3943SAlan Cox 	 */
1273b249ce48SMateusz Guzik 	VOP_UNLOCK(imgp->vp);
127460bb3943SAlan Cox 
1275fa50a355SKonstantin Belousov 	/*
1276fa50a355SKonstantin Belousov 	 * Decide whether to enable randomization of user mappings.
1277fa50a355SKonstantin Belousov 	 * First, reset user preferences for the setid binaries.
1278fa50a355SKonstantin Belousov 	 * Then, account for the support of the randomization by the
1279fa50a355SKonstantin Belousov 	 * ABI, by user preferences, and make special treatment for
1280fa50a355SKonstantin Belousov 	 * PIE binaries.
1281fa50a355SKonstantin Belousov 	 */
1282fa50a355SKonstantin Belousov 	if (imgp->credential_setid) {
1283fa50a355SKonstantin Belousov 		PROC_LOCK(imgp->proc);
1284796a8e1aSKonstantin Belousov 		imgp->proc->p_flag2 &= ~(P2_ASLR_ENABLE | P2_ASLR_DISABLE |
1285796a8e1aSKonstantin Belousov 		    P2_WXORX_DISABLE | P2_WXORX_ENABLE_EXEC);
1286fa50a355SKonstantin Belousov 		PROC_UNLOCK(imgp->proc);
1287fa50a355SKonstantin Belousov 	}
1288fa50a355SKonstantin Belousov 	if ((sv->sv_flags & SV_ASLR) == 0 ||
1289fa50a355SKonstantin Belousov 	    (imgp->proc->p_flag2 & P2_ASLR_DISABLE) != 0 ||
1290fa50a355SKonstantin Belousov 	    (fctl0 & NT_FREEBSD_FCTL_ASLR_DISABLE) != 0) {
1291fa50a355SKonstantin Belousov 		KASSERT(et_dyn_addr != ET_DYN_ADDR_RAND,
1292fa50a355SKonstantin Belousov 		    ("et_dyn_addr == RAND and !ASLR"));
1293fa50a355SKonstantin Belousov 	} else if ((imgp->proc->p_flag2 & P2_ASLR_ENABLE) != 0 ||
1294fa50a355SKonstantin Belousov 	    (__elfN(aslr_enabled) && hdr->e_type == ET_EXEC) ||
1295fa50a355SKonstantin Belousov 	    et_dyn_addr == ET_DYN_ADDR_RAND) {
1296fa50a355SKonstantin Belousov 		imgp->map_flags |= MAP_ASLR;
1297fa50a355SKonstantin Belousov 		/*
1298fa50a355SKonstantin Belousov 		 * If user does not care about sbrk, utilize the bss
1299fa50a355SKonstantin Belousov 		 * grow region for mappings as well.  We can select
1300fa50a355SKonstantin Belousov 		 * the base for the image anywere and still not suffer
1301fa50a355SKonstantin Belousov 		 * from the fragmentation.
1302fa50a355SKonstantin Belousov 		 */
1303fa50a355SKonstantin Belousov 		if (!__elfN(aslr_honor_sbrk) ||
1304fa50a355SKonstantin Belousov 		    (imgp->proc->p_flag2 & P2_ASLR_IGNSTART) != 0)
1305fa50a355SKonstantin Belousov 			imgp->map_flags |= MAP_ASLR_IGNSTART;
13061811c1e9SMark Johnston 		if (__elfN(aslr_stack))
13071811c1e9SMark Johnston 			imgp->map_flags |= MAP_ASLR_STACK;
1308fa50a355SKonstantin Belousov 	}
1309fa50a355SKonstantin Belousov 
1310796a8e1aSKonstantin Belousov 	if ((!__elfN(allow_wx) && (fctl0 & NT_FREEBSD_FCTL_WXNEEDED) == 0 &&
1311796a8e1aSKonstantin Belousov 	    (imgp->proc->p_flag2 & P2_WXORX_DISABLE) == 0) ||
1312796a8e1aSKonstantin Belousov 	    (imgp->proc->p_flag2 & P2_WXORX_ENABLE_EXEC) != 0)
13132e1c94aaSKonstantin Belousov 		imgp->map_flags |= MAP_WXORX;
13142e1c94aaSKonstantin Belousov 
131589b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
1316fa50a355SKonstantin Belousov 
131719059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
1318615f22b2SDmitry Chagin 	imgp->proc->p_elf_brandinfo = brand_info;
1319e1743d02SSøren Schmidt 
13201811c1e9SMark Johnston 	vmspace = imgp->proc->p_vmspace;
13211811c1e9SMark Johnston 	map = &vmspace->vm_map;
13221811c1e9SMark Johnston 	maxv = sv->sv_usrstack;
13231811c1e9SMark Johnston 	if ((imgp->map_flags & MAP_ASLR_STACK) == 0)
13241811c1e9SMark Johnston 		maxv -= lim_max(td, RLIMIT_STACK);
13251811c1e9SMark Johnston 	if (error == 0 && mapsz >= maxv - vm_map_min(map)) {
1326bf839416SKonstantin Belousov 		uprintf("Excessive mapping size\n");
1327bf839416SKonstantin Belousov 		error = ENOEXEC;
1328bf839416SKonstantin Belousov 	}
1329bf839416SKonstantin Belousov 
13309cf78c1cSKonstantin Belousov 	if (error == 0 && et_dyn_addr == ET_DYN_ADDR_RAND) {
1331fa50a355SKonstantin Belousov 		KASSERT((map->flags & MAP_ASLR) != 0,
1332fa50a355SKonstantin Belousov 		    ("ET_DYN_ADDR_RAND but !MAP_ASLR"));
13339cf78c1cSKonstantin Belousov 		error = __CONCAT(rnd_, __elfN(base))(map,
1334fa50a355SKonstantin Belousov 		    vm_map_min(map) + mapsz + lim_max(td, RLIMIT_DATA),
1335fa50a355SKonstantin Belousov 		    /* reserve half of the address space to interpreter */
133636df8f54SKonstantin Belousov 		    maxv / 2, maxalign, &et_dyn_addr);
1337fa50a355SKonstantin Belousov 	}
1338fa50a355SKonstantin Belousov 
133978022527SKonstantin Belousov 	vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
13406c775eb6SKonstantin Belousov 	if (error != 0)
13416c775eb6SKonstantin Belousov 		goto ret;
134260bb3943SAlan Cox 
13439bcd7482SEdward Tomasz Napierala 	error = __elfN(load_sections)(imgp, hdr, phdr, et_dyn_addr, NULL);
1344292177e6SAlan Cox 	if (error != 0)
13456c775eb6SKonstantin Belousov 		goto ret;
1346e1743d02SSøren Schmidt 
134720e1174aSEdward Tomasz Napierala 	error = __elfN(enforce_limits)(imgp, hdr, phdr, et_dyn_addr);
134820e1174aSEdward Tomasz Napierala 	if (error != 0)
134920e1174aSEdward Tomasz Napierala 		goto ret;
1350cac45152SMatthew Dillon 
1351cac45152SMatthew Dillon 	/*
1352c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
1353c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
1354c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
1355c460ac3aSPeter Wemm 	 * its maximum allowed size.
1356c460ac3aSPeter Wemm 	 */
13576c775eb6SKonstantin Belousov 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td,
1358292177e6SAlan Cox 	    RLIMIT_DATA));
1359fa50a355SKonstantin Belousov 	if ((map->flags & MAP_ASLR) != 0) {
1360fa50a355SKonstantin Belousov 		maxv1 = maxv / 2 + addr / 2;
13619cf78c1cSKonstantin Belousov 		error = __CONCAT(rnd_, __elfN(base))(map, addr, maxv1,
136241032835SJason A. Harmening 		    (MAXPAGESIZES > 1 && pagesizes[1] != 0) ?
1363e499988fSKonstantin Belousov 		    pagesizes[1] : pagesizes[0], &anon_loc);
13649cf78c1cSKonstantin Belousov 		if (error != 0)
13659cf78c1cSKonstantin Belousov 			goto ret;
1366e499988fSKonstantin Belousov 		map->anon_loc = anon_loc;
1367fa50a355SKonstantin Belousov 	} else {
1368fa50a355SKonstantin Belousov 		map->anon_loc = addr;
1369fa50a355SKonstantin Belousov 	}
1370e1743d02SSøren Schmidt 
13711811c1e9SMark Johnston 	entry = (u_long)hdr->e_entry + et_dyn_addr;
1372ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
1373ea5a2b2eSSøren Schmidt 
137460bb3943SAlan Cox 	if (interp != NULL) {
1375b249ce48SMateusz Guzik 		VOP_UNLOCK(imgp->vp);
1376fa50a355SKonstantin Belousov 		if ((map->flags & MAP_ASLR) != 0) {
137741032835SJason A. Harmening 			/* Assume that interpreter fits into 1/4 of AS */
1378fa50a355SKonstantin Belousov 			maxv1 = maxv / 2 + addr / 2;
13799cf78c1cSKonstantin Belousov 			error = __CONCAT(rnd_, __elfN(base))(map, addr,
13809cf78c1cSKonstantin Belousov 			    maxv1, PAGE_SIZE, &addr);
1381fa50a355SKonstantin Belousov 		}
13829cf78c1cSKonstantin Belousov 		if (error == 0) {
13839cf78c1cSKonstantin Belousov 			error = __elfN(load_interp)(imgp, brand_info, interp,
13849cf78c1cSKonstantin Belousov 			    &addr, &imgp->entry_addr);
13859cf78c1cSKonstantin Belousov 		}
138678022527SKonstantin Belousov 		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
13879274fb35SEdward Tomasz Napierala 		if (error != 0)
13886c775eb6SKonstantin Belousov 			goto ret;
138995c807cfSRobert Watson 	} else
13907564c4adSKonstantin Belousov 		addr = et_dyn_addr;
1391ea5a2b2eSSøren Schmidt 
13921811c1e9SMark Johnston 	error = exec_map_stack(imgp);
13931811c1e9SMark Johnston 	if (error != 0)
13941811c1e9SMark Johnston 		goto ret;
13951811c1e9SMark Johnston 
1396e1743d02SSøren Schmidt 	/*
1397e3532331SJohn Baldwin 	 * Construct auxargs table (used by the copyout_auxargs routine)
1398e1743d02SSøren Schmidt 	 */
13992d6b8546SKonstantin Belousov 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_NOWAIT);
14002d6b8546SKonstantin Belousov 	if (elf_auxargs == NULL) {
1401b249ce48SMateusz Guzik 		VOP_UNLOCK(imgp->vp);
1402a163d034SWarner Losh 		elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
140378022527SKonstantin Belousov 		vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
14042d6b8546SKonstantin Belousov 	}
1405e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
14069bcd7482SEdward Tomasz Napierala 	elf_auxargs->phdr = proghdr + et_dyn_addr;
1407e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
1408e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
1409e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
1410e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
1411e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
1412e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
1413d36eec69SWarner Losh 	elf_auxargs->hdr_eflags = hdr->e_flags;
1414e1743d02SSøren Schmidt 
1415e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
1416e1743d02SSøren Schmidt 	imgp->interpreted = 0;
1417a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
141832c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
1419cefb93f2SKonstantin Belousov 	imgp->proc->p_fctl0 = fctl0;
1420885f13dcSJohn Baldwin 	imgp->proc->p_elf_flags = hdr->e_flags;
1421f231de47SKonstantin Belousov 
14226c775eb6SKonstantin Belousov ret:
1423b4b20492SKonstantin Belousov 	ASSERT_VOP_LOCKED(imgp->vp, "skipped relock");
142409c78d53SEdward Tomasz Napierala 	if (free_interp)
142509c78d53SEdward Tomasz Napierala 		free(interp, M_TEMP);
1426a7cddfedSJake Burkholder 	return (error);
1427e1743d02SSøren Schmidt }
1428e1743d02SSøren Schmidt 
1429ebf98866SMark Johnston #define	elf_suword __CONCAT(suword, __ELF_WORD_SIZE)
14303ebc1248SPeter Wemm 
143103b0d68cSJohn Baldwin int
1432d8010b11SJohn Baldwin __elfN(freebsd_copyout_auxargs)(struct image_params *imgp, uintptr_t base)
1433e1743d02SSøren Schmidt {
1434ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
14355f77b8a8SBrooks Davis 	Elf_Auxinfo *argarray, *pos;
143603b0d68cSJohn Baldwin 	int error;
1437e1743d02SSøren Schmidt 
14385f77b8a8SBrooks Davis 	argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP,
14395f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
1440e1743d02SSøren Schmidt 
144135c2a5a8SWarner Losh 	if (args->execfd != -1)
1442e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1443e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1444e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1445e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1446e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1447e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1448e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1449e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
1450d36eec69SWarner Losh 	AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
14513ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
1452b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, AT_EXECPATH, imgp->execpathp);
1453b96bd95bSIan Lepore 	AUXARGS_ENTRY(pos, AT_OSRELDATE,
1454b96bd95bSIan Lepore 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
1455ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1456b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, AT_CANARY, imgp->canary);
1457ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1458ee235befSKonstantin Belousov 	}
1459ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1460ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1461b24e6ac8SBrooks Davis 		AUXARGS_ENTRY_PTR(pos, AT_PAGESIZES, imgp->pagesizes);
1462ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1463ee235befSKonstantin Belousov 	}
1464aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1465aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1466aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1467aea81038SKonstantin Belousov 	}
146826d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
146926d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
147026d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1471c2f37b92SJohn Baldwin 	if (imgp->sysent->sv_hwcap != NULL)
1472c2f37b92SJohn Baldwin 		AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap);
1473904d8c49SMichal Meloun 	if (imgp->sysent->sv_hwcap2 != NULL)
1474904d8c49SMichal Meloun 		AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1475944cf37bSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_BSDFLAGS, __elfN(sigfastblock) ?
1476944cf37bSKonstantin Belousov 	    ELF_BSDF_SIGFASTBLK : 0);
14779df1c38bSBrooks Davis 	AUXARGS_ENTRY(pos, AT_ARGC, imgp->args->argc);
14789df1c38bSBrooks Davis 	AUXARGS_ENTRY_PTR(pos, AT_ARGV, imgp->argv);
14799df1c38bSBrooks Davis 	AUXARGS_ENTRY(pos, AT_ENVC, imgp->args->envc);
14809df1c38bSBrooks Davis 	AUXARGS_ENTRY_PTR(pos, AT_ENVV, imgp->envv);
14819df1c38bSBrooks Davis 	AUXARGS_ENTRY_PTR(pos, AT_PS_STRINGS, imgp->ps_strings);
1482f8e8a06dSConrad Meyer 	if (imgp->sysent->sv_fxrng_gen_base != 0)
1483f8e8a06dSConrad Meyer 		AUXARGS_ENTRY(pos, AT_FXRNG, imgp->sysent->sv_fxrng_gen_base);
1484eb029587SKonstantin Belousov 	if (imgp->sysent->sv_vdso_base != 0 && __elfN(vdso) != 0)
148501c77a43SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_KPRELOAD, imgp->sysent->sv_vdso_base);
1486e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1487e1743d02SSøren Schmidt 
1488e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1489e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1490d8b2f079SBrooks Davis 	KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs"));
14915f77b8a8SBrooks Davis 
1492d8010b11SJohn Baldwin 	error = copyout(argarray, (void *)base, sizeof(*argarray) * AT_COUNT);
14935f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
149403b0d68cSJohn Baldwin 	return (error);
1495e3532331SJohn Baldwin }
1496e1743d02SSøren Schmidt 
1497e3532331SJohn Baldwin int
149831174518SJohn Baldwin __elfN(freebsd_fixup)(uintptr_t *stack_base, struct image_params *imgp)
1499e3532331SJohn Baldwin {
1500e3532331SJohn Baldwin 	Elf_Addr *base;
1501e3532331SJohn Baldwin 
1502e3532331SJohn Baldwin 	base = (Elf_Addr *)*stack_base;
15033ebc1248SPeter Wemm 	base--;
1504ebf98866SMark Johnston 	if (elf_suword(base, imgp->args->argc) == -1)
15055f77b8a8SBrooks Davis 		return (EFAULT);
150631174518SJohn Baldwin 	*stack_base = (uintptr_t)base;
1507a7cddfedSJake Burkholder 	return (0);
1508e1743d02SSøren Schmidt }
1509e1743d02SSøren Schmidt 
1510e1743d02SSøren Schmidt /*
15118c64af4fSJohn Polstra  * Code for generating ELF core dumps.
15128c64af4fSJohn Polstra  */
15138c64af4fSJohn Polstra 
15144d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
15150ff27d31SJohn Polstra 
15160ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
15170ff27d31SJohn Polstra struct phdr_closure {
15180ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
15190ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
15200ff27d31SJohn Polstra };
15210ff27d31SJohn Polstra 
1522bd390213SMikolaj Golub struct note_info {
1523bd390213SMikolaj Golub 	int		type;		/* Note type. */
15246b71405bSJohn Baldwin 	struct regset	*regset;	/* Register set. */
1525bd390213SMikolaj Golub 	outfunc_t 	outfunc; 	/* Output function. */
1526bd390213SMikolaj Golub 	void		*outarg;	/* Argument for the output function. */
1527bd390213SMikolaj Golub 	size_t		outsize;	/* Output size. */
1528bd390213SMikolaj Golub 	TAILQ_ENTRY(note_info) link;	/* Link to the next note info. */
1529bd390213SMikolaj Golub };
1530bd390213SMikolaj Golub 
1531bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info);
1532bd390213SMikolaj Golub 
153378f57a9cSMark Johnston extern int compress_user_cores;
153478f57a9cSMark Johnston extern int compress_user_cores_level;
153578f57a9cSMark Johnston 
15364d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
15374d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
15385bc3c617SKonstantin Belousov static void each_dumpable_segment(struct thread *, segment_callback, void *,
15395bc3c617SKonstantin Belousov     int);
1540aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
15415bc3c617SKonstantin Belousov     struct note_info_list *, size_t, int);
1542435754a5SEdward Tomasz Napierala static void __elfN(putnote)(struct thread *td, struct note_info *, struct sbuf *);
1543bd390213SMikolaj Golub 
1544bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1545bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1546bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
154786be94fcSTycho Nightingale static void __elfN(note_ptlwpinfo)(void *, struct sbuf *, size_t *);
1548f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1549f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1550f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1551f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *);
1552f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *);
1553f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1554f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1555f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *);
1556f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
15578c64af4fSJohn Polstra 
1558aa14e9b7SMark Johnston static int
155978f57a9cSMark Johnston core_compressed_write(void *base, size_t len, off_t offset, void *arg)
1560aa14e9b7SMark Johnston {
1561aa14e9b7SMark Johnston 
1562aa14e9b7SMark Johnston 	return (core_write((struct coredump_params *)arg, base, len, offset,
1563f31695ccSMark Johnston 	    UIO_SYSSPACE, NULL));
1564aa14e9b7SMark Johnston }
1565aa14e9b7SMark Johnston 
15668c64af4fSJohn Polstra int
1567e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1568fca666a1SJulian Elischer {
1569247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
157086ffb3d1SKonstantin Belousov 	int compm, error = 0;
15710ff27d31SJohn Polstra 	struct sseg_closure seginfo;
1572bd390213SMikolaj Golub 	struct note_info_list notelst;
1573aa14e9b7SMark Johnston 	struct coredump_params params;
1574bd390213SMikolaj Golub 	struct note_info *ninfo;
1575aa14e9b7SMark Johnston 	void *hdr, *tmpbuf;
1576bd390213SMikolaj Golub 	size_t hdrsize, notesz, coresize;
15778c64af4fSJohn Polstra 
1578e7228204SAlfred Perlstein 	hdr = NULL;
157902d131adSMark Johnston 	tmpbuf = NULL;
1580bd390213SMikolaj Golub 	TAILQ_INIT(&notelst);
1581e7228204SAlfred Perlstein 
15820ff27d31SJohn Polstra 	/* Size the program segments. */
1583905d192dSEdward Tomasz Napierala 	__elfN(size_segments)(td, &seginfo, flags);
15840ff27d31SJohn Polstra 
15850ff27d31SJohn Polstra 	/*
1586bd390213SMikolaj Golub 	 * Collect info about the core file header area.
15870ff27d31SJohn Polstra 	 */
1588bd390213SMikolaj Golub 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1589c17b0bd2SConrad Meyer 	if (seginfo.count + 1 >= PN_XNUM)
1590c17b0bd2SConrad Meyer 		hdrsize += sizeof(Elf_Shdr);
1591435754a5SEdward Tomasz Napierala 	td->td_proc->p_sysent->sv_elf_core_prepare_notes(td, &notelst, &notesz);
1592bd390213SMikolaj Golub 	coresize = round_page(hdrsize + notesz) + seginfo.size;
15930ff27d31SJohn Polstra 
159402d131adSMark Johnston 	/* Set up core dump parameters. */
159502d131adSMark Johnston 	params.offset = 0;
159602d131adSMark Johnston 	params.active_cred = cred;
159702d131adSMark Johnston 	params.file_cred = NOCRED;
159802d131adSMark Johnston 	params.td = td;
159902d131adSMark Johnston 	params.vp = vp;
160078f57a9cSMark Johnston 	params.comp = NULL;
160102d131adSMark Johnston 
1602afcc55f3SEdward Tomasz Napierala #ifdef RACCT
16034b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
16041ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1605bd390213SMikolaj Golub 		error = racct_add(td->td_proc, RACCT_CORE, coresize);
16061ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
16071ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
16081ba5ad42SEdward Tomasz Napierala 			error = EFAULT;
16091ba5ad42SEdward Tomasz Napierala 			goto done;
16101ba5ad42SEdward Tomasz Napierala 		}
16114b5c9cf6SEdward Tomasz Napierala 	}
1612afcc55f3SEdward Tomasz Napierala #endif
1613bd390213SMikolaj Golub 	if (coresize >= limit) {
1614fba6b1afSAlfred Perlstein 		error = EFAULT;
1615fba6b1afSAlfred Perlstein 		goto done;
1616fba6b1afSAlfred Perlstein 	}
16170ff27d31SJohn Polstra 
1618aa14e9b7SMark Johnston 	/* Create a compression stream if necessary. */
161986ffb3d1SKonstantin Belousov 	compm = compress_user_cores;
162086ffb3d1SKonstantin Belousov 	if ((flags & (SVC_PT_COREDUMP | SVC_NOCOMPRESS)) == SVC_PT_COREDUMP &&
162186ffb3d1SKonstantin Belousov 	    compm == 0)
162286ffb3d1SKonstantin Belousov 		compm = COMPRESS_GZIP;
162386ffb3d1SKonstantin Belousov 	if (compm != 0) {
162478f57a9cSMark Johnston 		params.comp = compressor_init(core_compressed_write,
162586ffb3d1SKonstantin Belousov 		    compm, CORE_BUF_SIZE,
162678f57a9cSMark Johnston 		    compress_user_cores_level, &params);
162778f57a9cSMark Johnston 		if (params.comp == NULL) {
1628aa14e9b7SMark Johnston 			error = EFAULT;
1629aa14e9b7SMark Johnston 			goto done;
1630aa14e9b7SMark Johnston 		}
1631aa14e9b7SMark Johnston 		tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1632aa14e9b7SMark Johnston         }
1633aa14e9b7SMark Johnston 
16340ff27d31SJohn Polstra 	/*
16350ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
1636bd390213SMikolaj Golub 	 * and write it out following the notes.
16370ff27d31SJohn Polstra 	 */
1638a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1639aa14e9b7SMark Johnston 	error = __elfN(corehdr)(&params, seginfo.count, hdr, hdrsize, &notelst,
16405bc3c617SKonstantin Belousov 	    notesz, flags);
16410ff27d31SJohn Polstra 
16420ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
16430ff27d31SJohn Polstra 	if (error == 0) {
16440ff27d31SJohn Polstra 		Elf_Phdr *php;
16452b471bc6STim J. Robbins 		off_t offset;
16460ff27d31SJohn Polstra 		int i;
16470ff27d31SJohn Polstra 
16480ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1649bd390213SMikolaj Golub 		offset = round_page(hdrsize + notesz);
16500ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1651f31695ccSMark Johnston 			error = core_output((char *)(uintptr_t)php->p_vaddr,
1652aa14e9b7SMark Johnston 			    php->p_filesz, offset, &params, tmpbuf);
16530ff27d31SJohn Polstra 			if (error != 0)
16542b471bc6STim J. Robbins 				break;
16550ff27d31SJohn Polstra 			offset += php->p_filesz;
16560ff27d31SJohn Polstra 			php++;
16570ff27d31SJohn Polstra 		}
165878f57a9cSMark Johnston 		if (error == 0 && params.comp != NULL)
165978f57a9cSMark Johnston 			error = compressor_flush(params.comp);
16600ff27d31SJohn Polstra 	}
1661e7228204SAlfred Perlstein 	if (error) {
1662e7228204SAlfred Perlstein 		log(LOG_WARNING,
1663e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1664e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1665e7228204SAlfred Perlstein 	}
1666e7228204SAlfred Perlstein 
1667e7228204SAlfred Perlstein done:
1668aa14e9b7SMark Johnston 	free(tmpbuf, M_TEMP);
166978f57a9cSMark Johnston 	if (params.comp != NULL)
167078f57a9cSMark Johnston 		compressor_fini(params.comp);
1671bd390213SMikolaj Golub 	while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1672bd390213SMikolaj Golub 		TAILQ_REMOVE(&notelst, ninfo, link);
1673bd390213SMikolaj Golub 		free(ninfo, M_TEMP);
1674bd390213SMikolaj Golub 	}
1675bd390213SMikolaj Golub 	if (hdr != NULL)
16760ff27d31SJohn Polstra 		free(hdr, M_TEMP);
16770ff27d31SJohn Polstra 
1678a7cddfedSJake Burkholder 	return (error);
16798c64af4fSJohn Polstra }
16808c64af4fSJohn Polstra 
16810ff27d31SJohn Polstra /*
16821005d8afSConrad Meyer  * A callback for each_dumpable_segment() to write out the segment's
16830ff27d31SJohn Polstra  * program header entry.
16840ff27d31SJohn Polstra  */
16850ff27d31SJohn Polstra static void
16865cc6d253SEd Maste cb_put_phdr(vm_map_entry_t entry, void *closure)
16870ff27d31SJohn Polstra {
16880ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
16890ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
16900ff27d31SJohn Polstra 
16910ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
16920ff27d31SJohn Polstra 
16930ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
16940ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
16950ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
16960ff27d31SJohn Polstra 	phdr->p_paddr = 0;
16970ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
16980ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1699ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
17000ff27d31SJohn Polstra 
17010ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
17020ff27d31SJohn Polstra 	phc->phdr++;
17030ff27d31SJohn Polstra }
17040ff27d31SJohn Polstra 
17050ff27d31SJohn Polstra /*
17061005d8afSConrad Meyer  * A callback for each_dumpable_segment() to gather information about
17070ff27d31SJohn Polstra  * the number of segments and their total size.
17080ff27d31SJohn Polstra  */
17090ff27d31SJohn Polstra static void
1710f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure)
17110ff27d31SJohn Polstra {
17120ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
17130ff27d31SJohn Polstra 
17140ff27d31SJohn Polstra 	ssc->count++;
17150ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
17160ff27d31SJohn Polstra }
17170ff27d31SJohn Polstra 
1718905d192dSEdward Tomasz Napierala void
1719905d192dSEdward Tomasz Napierala __elfN(size_segments)(struct thread *td, struct sseg_closure *seginfo,
1720905d192dSEdward Tomasz Napierala     int flags)
1721905d192dSEdward Tomasz Napierala {
1722905d192dSEdward Tomasz Napierala 	seginfo->count = 0;
1723905d192dSEdward Tomasz Napierala 	seginfo->size = 0;
1724905d192dSEdward Tomasz Napierala 
1725905d192dSEdward Tomasz Napierala 	each_dumpable_segment(td, cb_size_segment, seginfo, flags);
1726905d192dSEdward Tomasz Napierala }
1727905d192dSEdward Tomasz Napierala 
17280ff27d31SJohn Polstra /*
17290ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
17300ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
17310ff27d31SJohn Polstra  * caller-supplied data.
17320ff27d31SJohn Polstra  */
17330ff27d31SJohn Polstra static void
17345bc3c617SKonstantin Belousov each_dumpable_segment(struct thread *td, segment_callback func, void *closure,
17355bc3c617SKonstantin Belousov     int flags)
17360ff27d31SJohn Polstra {
1737247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
17380ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
17390ff27d31SJohn Polstra 	vm_map_entry_t entry;
1740976a87a2SAlan Cox 	vm_object_t backing_object, object;
1741fec41f07SMark Johnston 	bool ignore_entry;
17420ff27d31SJohn Polstra 
1743976a87a2SAlan Cox 	vm_map_lock_read(map);
17442288078cSDoug Moore 	VM_MAP_ENTRY_FOREACH(entry, map) {
1745fa7dd9c5SMatthew Dillon 		/*
1746fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1747fa7dd9c5SMatthew Dillon 		 * coredump mode.
1748fa7dd9c5SMatthew Dillon 		 *
1749fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1750fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1751fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1752fa7dd9c5SMatthew Dillon 		 */
175386ffb3d1SKonstantin Belousov 		if ((flags & SVC_ALL) == 0) {
1754fa7dd9c5SMatthew Dillon 			if (elf_legacy_coredump) {
175586ffb3d1SKonstantin Belousov 				if ((entry->protection & VM_PROT_RW) !=
175686ffb3d1SKonstantin Belousov 				    VM_PROT_RW)
17570ff27d31SJohn Polstra 					continue;
1758fa7dd9c5SMatthew Dillon 			} else {
1759fa7dd9c5SMatthew Dillon 				if ((entry->protection & VM_PROT_ALL) == 0)
1760fa7dd9c5SMatthew Dillon 					continue;
1761fa7dd9c5SMatthew Dillon 			}
176286ffb3d1SKonstantin Belousov 		}
17630ff27d31SJohn Polstra 
17649730a5daSPaul Saab 		/*
1765fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1766fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1767fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1768fa7dd9c5SMatthew Dillon 		 * kernel map).
17699730a5daSPaul Saab 		 */
177086ffb3d1SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0)
17719730a5daSPaul Saab 			continue;
177286ffb3d1SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_NOCOREDUMP) != 0 &&
177386ffb3d1SKonstantin Belousov 		    (flags & SVC_ALL) == 0)
177486ffb3d1SKonstantin Belousov 			continue;
1775976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
17760ff27d31SJohn Polstra 			continue;
17770ff27d31SJohn Polstra 
17780ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1779bc403f03SAttilio Rao 		VM_OBJECT_RLOCK(object);
1780976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1781bc403f03SAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1782bc403f03SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1783976a87a2SAlan Cox 			object = backing_object;
1784976a87a2SAlan Cox 		}
1785fec41f07SMark Johnston 		ignore_entry = (object->flags & OBJ_FICTITIOUS) != 0;
1786bc403f03SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1787976a87a2SAlan Cox 		if (ignore_entry)
17880ff27d31SJohn Polstra 			continue;
17890ff27d31SJohn Polstra 
17900ff27d31SJohn Polstra 		(*func)(entry, closure);
17910ff27d31SJohn Polstra 	}
1792976a87a2SAlan Cox 	vm_map_unlock_read(map);
17930ff27d31SJohn Polstra }
17940ff27d31SJohn Polstra 
17950ff27d31SJohn Polstra /*
17960ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
17970ff27d31SJohn Polstra  * the page boundary.
17980ff27d31SJohn Polstra  */
17998c64af4fSJohn Polstra static int
1800aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
18015bc3c617SKonstantin Belousov     size_t hdrsize, struct note_info_list *notelst, size_t notesz,
18025bc3c617SKonstantin Belousov     int flags)
18038c64af4fSJohn Polstra {
1804bd390213SMikolaj Golub 	struct note_info *ninfo;
1805bd390213SMikolaj Golub 	struct sbuf *sb;
1806bd390213SMikolaj Golub 	int error;
18078c64af4fSJohn Polstra 
18088c64af4fSJohn Polstra 	/* Fill in the header. */
18090ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
18105bc3c617SKonstantin Belousov 	__elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz, flags);
18118c64af4fSJohn Polstra 
1812bd390213SMikolaj Golub 	sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1813aa14e9b7SMark Johnston 	sbuf_set_drain(sb, sbuf_drain_core_output, p);
1814bd390213SMikolaj Golub 	sbuf_start_section(sb, NULL);
1815bd390213SMikolaj Golub 	sbuf_bcat(sb, hdr, hdrsize);
1816bd390213SMikolaj Golub 	TAILQ_FOREACH(ninfo, notelst, link)
1817435754a5SEdward Tomasz Napierala 	    __elfN(putnote)(p->td, ninfo, sb);
1818bd390213SMikolaj Golub 	/* Align up to a page boundary for the program segments. */
1819bd390213SMikolaj Golub 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1820bd390213SMikolaj Golub 	error = sbuf_finish(sb);
1821bd390213SMikolaj Golub 	sbuf_delete(sb);
1822bd390213SMikolaj Golub 
1823bd390213SMikolaj Golub 	return (error);
1824e7228204SAlfred Perlstein }
1825bd390213SMikolaj Golub 
1826435754a5SEdward Tomasz Napierala void
1827bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1828bd390213SMikolaj Golub     size_t *sizep)
1829bd390213SMikolaj Golub {
1830bd390213SMikolaj Golub 	struct proc *p;
1831bd390213SMikolaj Golub 	struct thread *thr;
1832bd390213SMikolaj Golub 	size_t size;
1833bd390213SMikolaj Golub 
1834bd390213SMikolaj Golub 	p = td->td_proc;
1835bd390213SMikolaj Golub 	size = 0;
1836bd390213SMikolaj Golub 
18376b71405bSJohn Baldwin 	size += __elfN(register_note)(td, list, NT_PRPSINFO,
18386b71405bSJohn Baldwin 	    __elfN(note_prpsinfo), p);
1839bd390213SMikolaj Golub 
1840bd390213SMikolaj Golub 	/*
1841bd390213SMikolaj Golub 	 * To have the debugger select the right thread (LWP) as the initial
1842bd390213SMikolaj Golub 	 * thread, we dump the state of the thread passed to us in td first.
1843bd390213SMikolaj Golub 	 * This is the thread that causes the core dump and thus likely to
1844bd390213SMikolaj Golub 	 * be the right thread one wants to have selected in the debugger.
1845bd390213SMikolaj Golub 	 */
1846bd390213SMikolaj Golub 	thr = td;
1847bd390213SMikolaj Golub 	while (thr != NULL) {
18486b71405bSJohn Baldwin 		size += __elfN(prepare_register_notes)(td, list, thr);
1849435754a5SEdward Tomasz Napierala 		size += __elfN(register_note)(td, list, NT_THRMISC,
1850bd390213SMikolaj Golub 		    __elfN(note_thrmisc), thr);
1851435754a5SEdward Tomasz Napierala 		size += __elfN(register_note)(td, list, NT_PTLWPINFO,
185286be94fcSTycho Nightingale 		    __elfN(note_ptlwpinfo), thr);
1853435754a5SEdward Tomasz Napierala 		size += __elfN(register_note)(td, list, -1,
1854bd390213SMikolaj Golub 		    __elfN(note_threadmd), thr);
1855bd390213SMikolaj Golub 
185661b4c627SEdward Tomasz Napierala 		thr = thr == td ? TAILQ_FIRST(&p->p_threads) :
1857bd390213SMikolaj Golub 		    TAILQ_NEXT(thr, td_plist);
1858bd390213SMikolaj Golub 		if (thr == td)
1859bd390213SMikolaj Golub 			thr = TAILQ_NEXT(thr, td_plist);
1860dada0278SJohn Polstra 	}
1861dada0278SJohn Polstra 
1862435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_PROC,
1863f1fca82eSMikolaj Golub 	    __elfN(note_procstat_proc), p);
1864435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_FILES,
1865f1fca82eSMikolaj Golub 	    note_procstat_files, p);
1866435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_VMMAP,
1867f1fca82eSMikolaj Golub 	    note_procstat_vmmap, p);
1868435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_GROUPS,
1869f1fca82eSMikolaj Golub 	    note_procstat_groups, p);
1870435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_UMASK,
1871f1fca82eSMikolaj Golub 	    note_procstat_umask, p);
1872435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_RLIMIT,
1873f1fca82eSMikolaj Golub 	    note_procstat_rlimit, p);
1874435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_OSREL,
1875f1fca82eSMikolaj Golub 	    note_procstat_osrel, p);
1876435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_PSSTRINGS,
1877f1fca82eSMikolaj Golub 	    __elfN(note_procstat_psstrings), p);
1878435754a5SEdward Tomasz Napierala 	size += __elfN(register_note)(td, list, NT_PROCSTAT_AUXV,
1879f1fca82eSMikolaj Golub 	    __elfN(note_procstat_auxv), p);
1880f1fca82eSMikolaj Golub 
1881bd390213SMikolaj Golub 	*sizep = size;
1882bd390213SMikolaj Golub }
1883bd390213SMikolaj Golub 
1884905d192dSEdward Tomasz Napierala void
1885bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
18865bc3c617SKonstantin Belousov     size_t notesz, int flags)
1887bd390213SMikolaj Golub {
1888bd390213SMikolaj Golub 	Elf_Ehdr *ehdr;
1889bd390213SMikolaj Golub 	Elf_Phdr *phdr;
1890c17b0bd2SConrad Meyer 	Elf_Shdr *shdr;
1891bd390213SMikolaj Golub 	struct phdr_closure phc;
18925d9f7901SDmitry Chagin 	Elf_Brandinfo *bi;
1893bd390213SMikolaj Golub 
1894bd390213SMikolaj Golub 	ehdr = (Elf_Ehdr *)hdr;
18955d9f7901SDmitry Chagin 	bi = td->td_proc->p_elf_brandinfo;
1896bd390213SMikolaj Golub 
1897bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1898bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1899bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1900bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1901bd390213SMikolaj Golub 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1902bd390213SMikolaj Golub 	ehdr->e_ident[EI_DATA] = ELF_DATA;
1903bd390213SMikolaj Golub 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1904435754a5SEdward Tomasz Napierala 	ehdr->e_ident[EI_OSABI] = td->td_proc->p_sysent->sv_elf_core_osabi;
1905bd390213SMikolaj Golub 	ehdr->e_ident[EI_ABIVERSION] = 0;
1906bd390213SMikolaj Golub 	ehdr->e_ident[EI_PAD] = 0;
1907bd390213SMikolaj Golub 	ehdr->e_type = ET_CORE;
19085d9f7901SDmitry Chagin 	ehdr->e_machine = bi->machine;
1909bd390213SMikolaj Golub 	ehdr->e_version = EV_CURRENT;
1910bd390213SMikolaj Golub 	ehdr->e_entry = 0;
1911bd390213SMikolaj Golub 	ehdr->e_phoff = sizeof(Elf_Ehdr);
1912885f13dcSJohn Baldwin 	ehdr->e_flags = td->td_proc->p_elf_flags;
1913bd390213SMikolaj Golub 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
1914bd390213SMikolaj Golub 	ehdr->e_phentsize = sizeof(Elf_Phdr);
1915bd390213SMikolaj Golub 	ehdr->e_shentsize = sizeof(Elf_Shdr);
1916bd390213SMikolaj Golub 	ehdr->e_shstrndx = SHN_UNDEF;
1917c17b0bd2SConrad Meyer 	if (numsegs + 1 < PN_XNUM) {
1918c17b0bd2SConrad Meyer 		ehdr->e_phnum = numsegs + 1;
1919c17b0bd2SConrad Meyer 		ehdr->e_shnum = 0;
1920c17b0bd2SConrad Meyer 	} else {
1921c17b0bd2SConrad Meyer 		ehdr->e_phnum = PN_XNUM;
1922c17b0bd2SConrad Meyer 		ehdr->e_shnum = 1;
1923c17b0bd2SConrad Meyer 
1924c17b0bd2SConrad Meyer 		ehdr->e_shoff = ehdr->e_phoff +
1925c17b0bd2SConrad Meyer 		    (numsegs + 1) * ehdr->e_phentsize;
1926c17b0bd2SConrad Meyer 		KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr),
1927c17b0bd2SConrad Meyer 		    ("e_shoff: %zu, hdrsize - shdr: %zu",
192807f825e8SConrad Meyer 		     (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr)));
1929c17b0bd2SConrad Meyer 
1930c17b0bd2SConrad Meyer 		shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
1931c17b0bd2SConrad Meyer 		memset(shdr, 0, sizeof(*shdr));
1932c17b0bd2SConrad Meyer 		/*
1933c17b0bd2SConrad Meyer 		 * A special first section is used to hold large segment and
1934c17b0bd2SConrad Meyer 		 * section counts.  This was proposed by Sun Microsystems in
1935c17b0bd2SConrad Meyer 		 * Solaris and has been adopted by Linux; the standard ELF
1936c17b0bd2SConrad Meyer 		 * tools are already familiar with the technique.
1937c17b0bd2SConrad Meyer 		 *
1938c17b0bd2SConrad Meyer 		 * See table 7-7 of the Solaris "Linker and Libraries Guide"
1939c17b0bd2SConrad Meyer 		 * (or 12-7 depending on the version of the document) for more
1940c17b0bd2SConrad Meyer 		 * details.
1941c17b0bd2SConrad Meyer 		 */
1942c17b0bd2SConrad Meyer 		shdr->sh_type = SHT_NULL;
1943c17b0bd2SConrad Meyer 		shdr->sh_size = ehdr->e_shnum;
1944c17b0bd2SConrad Meyer 		shdr->sh_link = ehdr->e_shstrndx;
1945c17b0bd2SConrad Meyer 		shdr->sh_info = numsegs + 1;
1946c17b0bd2SConrad Meyer 	}
1947bd390213SMikolaj Golub 
1948bd390213SMikolaj Golub 	/*
1949bd390213SMikolaj Golub 	 * Fill in the program header entries.
1950bd390213SMikolaj Golub 	 */
1951c17b0bd2SConrad Meyer 	phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
1952bd390213SMikolaj Golub 
1953bd390213SMikolaj Golub 	/* The note segement. */
1954bd390213SMikolaj Golub 	phdr->p_type = PT_NOTE;
1955bd390213SMikolaj Golub 	phdr->p_offset = hdrsize;
1956bd390213SMikolaj Golub 	phdr->p_vaddr = 0;
1957bd390213SMikolaj Golub 	phdr->p_paddr = 0;
1958bd390213SMikolaj Golub 	phdr->p_filesz = notesz;
1959bd390213SMikolaj Golub 	phdr->p_memsz = 0;
1960bd390213SMikolaj Golub 	phdr->p_flags = PF_R;
19611b8388cdSMikolaj Golub 	phdr->p_align = ELF_NOTE_ROUNDSIZE;
1962bd390213SMikolaj Golub 	phdr++;
1963bd390213SMikolaj Golub 
1964bd390213SMikolaj Golub 	/* All the writable segments from the program. */
1965bd390213SMikolaj Golub 	phc.phdr = phdr;
1966bd390213SMikolaj Golub 	phc.offset = round_page(hdrsize + notesz);
19675bc3c617SKonstantin Belousov 	each_dumpable_segment(td, cb_put_phdr, &phc, flags);
1968bd390213SMikolaj Golub }
1969bd390213SMikolaj Golub 
19706b71405bSJohn Baldwin static size_t
19716b71405bSJohn Baldwin __elfN(register_regset_note)(struct thread *td, struct note_info_list *list,
19726b71405bSJohn Baldwin     struct regset *regset, struct thread *target_td)
19736b71405bSJohn Baldwin {
19746b71405bSJohn Baldwin 	const struct sysentvec *sv;
19756b71405bSJohn Baldwin 	struct note_info *ninfo;
19766b71405bSJohn Baldwin 	size_t size, notesize;
19776b71405bSJohn Baldwin 
19786b71405bSJohn Baldwin 	size = 0;
19796b71405bSJohn Baldwin 	if (!regset->get(regset, target_td, NULL, &size) || size == 0)
19806b71405bSJohn Baldwin 		return (0);
19816b71405bSJohn Baldwin 
19826b71405bSJohn Baldwin 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
19836b71405bSJohn Baldwin 	ninfo->type = regset->note;
19846b71405bSJohn Baldwin 	ninfo->regset = regset;
19856b71405bSJohn Baldwin 	ninfo->outarg = target_td;
19866b71405bSJohn Baldwin 	ninfo->outsize = size;
19876b71405bSJohn Baldwin 	TAILQ_INSERT_TAIL(list, ninfo, link);
19886b71405bSJohn Baldwin 
19896b71405bSJohn Baldwin 	sv = td->td_proc->p_sysent;
19906b71405bSJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
19916b71405bSJohn Baldwin 	    roundup2(strlen(sv->sv_elf_core_abi_vendor) + 1, ELF_NOTE_ROUNDSIZE) +
19926b71405bSJohn Baldwin 						/* note name */
19936b71405bSJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
19946b71405bSJohn Baldwin 
19956b71405bSJohn Baldwin 	return (notesize);
19966b71405bSJohn Baldwin }
19976b71405bSJohn Baldwin 
1998435754a5SEdward Tomasz Napierala size_t
1999435754a5SEdward Tomasz Napierala __elfN(register_note)(struct thread *td, struct note_info_list *list,
2000435754a5SEdward Tomasz Napierala     int type, outfunc_t out, void *arg)
2001bd390213SMikolaj Golub {
2002435754a5SEdward Tomasz Napierala 	const struct sysentvec *sv;
2003bd390213SMikolaj Golub 	struct note_info *ninfo;
2004bd390213SMikolaj Golub 	size_t size, notesize;
2005bd390213SMikolaj Golub 
2006435754a5SEdward Tomasz Napierala 	sv = td->td_proc->p_sysent;
2007bd390213SMikolaj Golub 	size = 0;
2008bd390213SMikolaj Golub 	out(arg, NULL, &size);
2009bd390213SMikolaj Golub 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
2010bd390213SMikolaj Golub 	ninfo->type = type;
2011bd390213SMikolaj Golub 	ninfo->outfunc = out;
2012bd390213SMikolaj Golub 	ninfo->outarg = arg;
2013bd390213SMikolaj Golub 	ninfo->outsize = size;
2014bd390213SMikolaj Golub 	TAILQ_INSERT_TAIL(list, ninfo, link);
2015bd390213SMikolaj Golub 
2016bd390213SMikolaj Golub 	if (type == -1)
2017bd390213SMikolaj Golub 		return (size);
2018bd390213SMikolaj Golub 
2019bd390213SMikolaj Golub 	notesize = sizeof(Elf_Note) +		/* note header */
2020435754a5SEdward Tomasz Napierala 	    roundup2(strlen(sv->sv_elf_core_abi_vendor) + 1, ELF_NOTE_ROUNDSIZE) +
2021180e57e5SJohn Baldwin 						/* note name */
2022180e57e5SJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
2023180e57e5SJohn Baldwin 
2024180e57e5SJohn Baldwin 	return (notesize);
2025180e57e5SJohn Baldwin }
2026180e57e5SJohn Baldwin 
2027180e57e5SJohn Baldwin static size_t
2028180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len)
2029180e57e5SJohn Baldwin {
2030180e57e5SJohn Baldwin 	size_t padded_len;
2031180e57e5SJohn Baldwin 
2032180e57e5SJohn Baldwin 	padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
2033180e57e5SJohn Baldwin 	if (dst != NULL) {
2034180e57e5SJohn Baldwin 		bcopy(src, dst, len);
2035180e57e5SJohn Baldwin 		bzero((char *)dst + len, padded_len - len);
2036180e57e5SJohn Baldwin 	}
2037180e57e5SJohn Baldwin 	return (padded_len);
2038180e57e5SJohn Baldwin }
2039180e57e5SJohn Baldwin 
2040180e57e5SJohn Baldwin size_t
2041180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
2042180e57e5SJohn Baldwin {
2043180e57e5SJohn Baldwin 	Elf_Note *note;
2044180e57e5SJohn Baldwin 	char *buf;
2045180e57e5SJohn Baldwin 	size_t notesize;
2046180e57e5SJohn Baldwin 
2047180e57e5SJohn Baldwin 	buf = dst;
2048180e57e5SJohn Baldwin 	if (buf != NULL) {
2049180e57e5SJohn Baldwin 		note = (Elf_Note *)buf;
2050180e57e5SJohn Baldwin 		note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
2051180e57e5SJohn Baldwin 		note->n_descsz = size;
2052180e57e5SJohn Baldwin 		note->n_type = type;
2053180e57e5SJohn Baldwin 		buf += sizeof(*note);
2054180e57e5SJohn Baldwin 		buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
2055180e57e5SJohn Baldwin 		    sizeof(FREEBSD_ABI_VENDOR));
2056180e57e5SJohn Baldwin 		append_note_data(src, buf, size);
2057180e57e5SJohn Baldwin 		if (descp != NULL)
2058180e57e5SJohn Baldwin 			*descp = buf;
2059180e57e5SJohn Baldwin 	}
2060180e57e5SJohn Baldwin 
2061180e57e5SJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
2062180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
2063180e57e5SJohn Baldwin 						/* note name */
20641b8388cdSMikolaj Golub 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
2065bd390213SMikolaj Golub 
2066bd390213SMikolaj Golub 	return (notesize);
2067bd390213SMikolaj Golub }
2068bd390213SMikolaj Golub 
2069bd390213SMikolaj Golub static void
2070435754a5SEdward Tomasz Napierala __elfN(putnote)(struct thread *td, struct note_info *ninfo, struct sbuf *sb)
2071bd390213SMikolaj Golub {
2072bd390213SMikolaj Golub 	Elf_Note note;
2073435754a5SEdward Tomasz Napierala 	const struct sysentvec *sv;
207414bdbaf2SConrad Meyer 	ssize_t old_len, sect_len;
207514bdbaf2SConrad Meyer 	size_t new_len, descsz, i;
2076bd390213SMikolaj Golub 
2077bd390213SMikolaj Golub 	if (ninfo->type == -1) {
2078bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
2079bd390213SMikolaj Golub 		return;
2080bd390213SMikolaj Golub 	}
2081bd390213SMikolaj Golub 
2082435754a5SEdward Tomasz Napierala 	sv = td->td_proc->p_sysent;
2083435754a5SEdward Tomasz Napierala 
2084435754a5SEdward Tomasz Napierala 	note.n_namesz = strlen(sv->sv_elf_core_abi_vendor) + 1;
2085bd390213SMikolaj Golub 	note.n_descsz = ninfo->outsize;
2086bd390213SMikolaj Golub 	note.n_type = ninfo->type;
2087bd390213SMikolaj Golub 
2088bd390213SMikolaj Golub 	sbuf_bcat(sb, &note, sizeof(note));
2089bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
2090435754a5SEdward Tomasz Napierala 	sbuf_bcat(sb, sv->sv_elf_core_abi_vendor,
2091435754a5SEdward Tomasz Napierala 	    strlen(sv->sv_elf_core_abi_vendor) + 1);
20921b8388cdSMikolaj Golub 	sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
2093bd390213SMikolaj Golub 	if (note.n_descsz == 0)
2094bd390213SMikolaj Golub 		return;
2095bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
20966b71405bSJohn Baldwin 	if (ninfo->regset != NULL) {
20976b71405bSJohn Baldwin 		struct regset *regset = ninfo->regset;
20986b71405bSJohn Baldwin 		void *buf;
20996b71405bSJohn Baldwin 
21006b71405bSJohn Baldwin 		buf = malloc(ninfo->outsize, M_TEMP, M_ZERO | M_WAITOK);
21016b71405bSJohn Baldwin 		(void)regset->get(regset, ninfo->outarg, buf, &ninfo->outsize);
21026b71405bSJohn Baldwin 		sbuf_bcat(sb, buf, ninfo->outsize);
21036b71405bSJohn Baldwin 		free(buf, M_TEMP);
21046b71405bSJohn Baldwin 	} else
2105bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
210614bdbaf2SConrad Meyer 	sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
210714bdbaf2SConrad Meyer 	if (sect_len < 0)
210814bdbaf2SConrad Meyer 		return;
210914bdbaf2SConrad Meyer 
211014bdbaf2SConrad Meyer 	new_len = (size_t)sect_len;
211114bdbaf2SConrad Meyer 	descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
211214bdbaf2SConrad Meyer 	if (new_len < descsz) {
211314bdbaf2SConrad Meyer 		/*
211414bdbaf2SConrad Meyer 		 * It is expected that individual note emitters will correctly
211514bdbaf2SConrad Meyer 		 * predict their expected output size and fill up to that size
211614bdbaf2SConrad Meyer 		 * themselves, padding in a format-specific way if needed.
211714bdbaf2SConrad Meyer 		 * However, in case they don't, just do it here with zeros.
211814bdbaf2SConrad Meyer 		 */
211914bdbaf2SConrad Meyer 		for (i = 0; i < descsz - new_len; i++)
212014bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
212114bdbaf2SConrad Meyer 	} else if (new_len > descsz) {
212214bdbaf2SConrad Meyer 		/*
212314bdbaf2SConrad Meyer 		 * We can't always truncate sb -- we may have drained some
212414bdbaf2SConrad Meyer 		 * of it already.
212514bdbaf2SConrad Meyer 		 */
212614bdbaf2SConrad Meyer 		KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
212714bdbaf2SConrad Meyer 		    "read it (%zu > %zu).  Since it is longer than "
212814bdbaf2SConrad Meyer 		    "expected, this coredump's notes are corrupt.  THIS "
212914bdbaf2SConrad Meyer 		    "IS A BUG in the note_procstat routine for type %u.\n",
213014bdbaf2SConrad Meyer 		    __func__, (unsigned)note.n_type, new_len, descsz,
213114bdbaf2SConrad Meyer 		    (unsigned)note.n_type));
213214bdbaf2SConrad Meyer 	}
2133bd390213SMikolaj Golub }
2134bd390213SMikolaj Golub 
2135bd390213SMikolaj Golub /*
2136bd390213SMikolaj Golub  * Miscellaneous note out functions.
2137bd390213SMikolaj Golub  */
2138bd390213SMikolaj Golub 
2139841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2140841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
214151645e83SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
2142841c0c7eSNathan Whitehorn 
214362919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
214462919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
214562919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
214662919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
214762919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
21487f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
2149f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	KERN_PROC_MASK32
2150f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t;
2151f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t;
215262919d78SPeter Wemm #else
215362919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
215462919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
215562919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
215662919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
215762919d78SPeter Wemm typedef gregset_t elf_gregset_t;
21587f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
2159f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	0
2160f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t;
2161f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t;
216262919d78SPeter Wemm #endif
216362919d78SPeter Wemm 
21648c64af4fSJohn Polstra static void
2165bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
21668c64af4fSJohn Polstra {
2167c77547d2SJohn Baldwin 	struct sbuf sbarg;
2168c77547d2SJohn Baldwin 	size_t len;
2169c77547d2SJohn Baldwin 	char *cp, *end;
2170247aba24SMarcel Moolenaar 	struct proc *p;
2171bd390213SMikolaj Golub 	elf_prpsinfo_t *psinfo;
2172c77547d2SJohn Baldwin 	int error;
21738c64af4fSJohn Polstra 
217461b4c627SEdward Tomasz Napierala 	p = arg;
2175bd390213SMikolaj Golub 	if (sb != NULL) {
2176bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
2177bd390213SMikolaj Golub 		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
21788c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
217962919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
2180ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
2181c77547d2SJohn Baldwin 		PROC_LOCK(p);
2182c77547d2SJohn Baldwin 		if (p->p_args != NULL) {
2183c77547d2SJohn Baldwin 			len = sizeof(psinfo->pr_psargs) - 1;
2184c77547d2SJohn Baldwin 			if (len > p->p_args->ar_length)
2185c77547d2SJohn Baldwin 				len = p->p_args->ar_length;
2186c77547d2SJohn Baldwin 			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
2187c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
2188c77547d2SJohn Baldwin 			error = 0;
2189c77547d2SJohn Baldwin 		} else {
2190c77547d2SJohn Baldwin 			_PHOLD(p);
2191c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
2192c77547d2SJohn Baldwin 			sbuf_new(&sbarg, psinfo->pr_psargs,
2193c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
2194c77547d2SJohn Baldwin 			error = proc_getargv(curthread, p, &sbarg);
2195c77547d2SJohn Baldwin 			PRELE(p);
2196c77547d2SJohn Baldwin 			if (sbuf_finish(&sbarg) == 0)
2197c77547d2SJohn Baldwin 				len = sbuf_len(&sbarg) - 1;
2198c77547d2SJohn Baldwin 			else
2199c77547d2SJohn Baldwin 				len = sizeof(psinfo->pr_psargs) - 1;
2200c77547d2SJohn Baldwin 			sbuf_delete(&sbarg);
2201c77547d2SJohn Baldwin 		}
2202c77547d2SJohn Baldwin 		if (error || len == 0)
2203ccd3953eSJohn Baldwin 			strlcpy(psinfo->pr_psargs, p->p_comm,
22048c9b7b2cSMarcel Moolenaar 			    sizeof(psinfo->pr_psargs));
2205c77547d2SJohn Baldwin 		else {
2206c77547d2SJohn Baldwin 			KASSERT(len < sizeof(psinfo->pr_psargs),
2207c77547d2SJohn Baldwin 			    ("len is too long: %zu vs %zu", len,
2208c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs)));
2209c77547d2SJohn Baldwin 			cp = psinfo->pr_psargs;
2210c77547d2SJohn Baldwin 			end = cp + len - 1;
2211c77547d2SJohn Baldwin 			for (;;) {
2212c77547d2SJohn Baldwin 				cp = memchr(cp, '\0', end - cp);
2213c77547d2SJohn Baldwin 				if (cp == NULL)
2214c77547d2SJohn Baldwin 					break;
2215c77547d2SJohn Baldwin 				*cp = ' ';
2216c77547d2SJohn Baldwin 			}
2217c77547d2SJohn Baldwin 		}
2218ccb83afdSJohn Baldwin 		psinfo->pr_pid = p->p_pid;
2219bd390213SMikolaj Golub 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
2220bd390213SMikolaj Golub 		free(psinfo, M_TEMP);
2221bd390213SMikolaj Golub 	}
2222bd390213SMikolaj Golub 	*sizep = sizeof(*psinfo);
2223bd390213SMikolaj Golub }
2224bd390213SMikolaj Golub 
2225548a2ec4SAndrew Turner static bool
2226548a2ec4SAndrew Turner __elfN(get_prstatus)(struct regset *rs, struct thread *td, void *buf,
2227548a2ec4SAndrew Turner     size_t *sizep)
2228bd390213SMikolaj Golub {
2229bd390213SMikolaj Golub 	elf_prstatus_t *status;
2230bd390213SMikolaj Golub 
2231548a2ec4SAndrew Turner 	if (buf != NULL) {
2232548a2ec4SAndrew Turner 		KASSERT(*sizep == sizeof(*status), ("%s: invalid size",
2233548a2ec4SAndrew Turner 		    __func__));
2234548a2ec4SAndrew Turner 		status = buf;
2235*1babcad6SMark Johnston 		memset(status, 0, *sizep);
22368c9b7b2cSMarcel Moolenaar 		status->pr_version = PRSTATUS_VERSION;
223762919d78SPeter Wemm 		status->pr_statussz = sizeof(elf_prstatus_t);
223862919d78SPeter Wemm 		status->pr_gregsetsz = sizeof(elf_gregset_t);
223962919d78SPeter Wemm 		status->pr_fpregsetsz = sizeof(elf_fpregset_t);
22408c9b7b2cSMarcel Moolenaar 		status->pr_osreldate = osreldate;
2241bd390213SMikolaj Golub 		status->pr_cursig = td->td_proc->p_sig;
2242bd390213SMikolaj Golub 		status->pr_pid = td->td_tid;
2243841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2244bd390213SMikolaj Golub 		fill_regs32(td, &status->pr_reg);
224562919d78SPeter Wemm #else
2246bd390213SMikolaj Golub 		fill_regs(td, &status->pr_reg);
224762919d78SPeter Wemm #endif
2248548a2ec4SAndrew Turner 	}
2249548a2ec4SAndrew Turner 	*sizep = sizeof(*status);
2250548a2ec4SAndrew Turner 	return (true);
2251548a2ec4SAndrew Turner }
2252548a2ec4SAndrew Turner 
2253548a2ec4SAndrew Turner static bool
2254548a2ec4SAndrew Turner __elfN(set_prstatus)(struct regset *rs, struct thread *td, void *buf,
2255548a2ec4SAndrew Turner     size_t size)
2256548a2ec4SAndrew Turner {
2257548a2ec4SAndrew Turner 	elf_prstatus_t *status;
2258548a2ec4SAndrew Turner 
2259548a2ec4SAndrew Turner 	KASSERT(size == sizeof(*status), ("%s: invalid size", __func__));
2260548a2ec4SAndrew Turner 	status = buf;
2261548a2ec4SAndrew Turner #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2262548a2ec4SAndrew Turner 	set_regs32(td, &status->pr_reg);
2263548a2ec4SAndrew Turner #else
2264548a2ec4SAndrew Turner 	set_regs(td, &status->pr_reg);
2265548a2ec4SAndrew Turner #endif
2266548a2ec4SAndrew Turner 	return (true);
2267548a2ec4SAndrew Turner }
2268548a2ec4SAndrew Turner 
2269548a2ec4SAndrew Turner static struct regset __elfN(regset_prstatus) = {
2270548a2ec4SAndrew Turner 	.note = NT_PRSTATUS,
2271548a2ec4SAndrew Turner 	.size = sizeof(elf_prstatus_t),
2272548a2ec4SAndrew Turner 	.get = __elfN(get_prstatus),
2273548a2ec4SAndrew Turner 	.set = __elfN(set_prstatus),
2274548a2ec4SAndrew Turner };
2275548a2ec4SAndrew Turner ELF_REGSET(__elfN(regset_prstatus));
2276548a2ec4SAndrew Turner 
2277548a2ec4SAndrew Turner static bool
2278548a2ec4SAndrew Turner __elfN(get_fpregset)(struct regset *rs, struct thread *td, void *buf,
2279548a2ec4SAndrew Turner     size_t *sizep)
2280548a2ec4SAndrew Turner {
2281548a2ec4SAndrew Turner 	elf_prfpregset_t *fpregset;
2282548a2ec4SAndrew Turner 
2283548a2ec4SAndrew Turner 	if (buf != NULL) {
2284548a2ec4SAndrew Turner 		KASSERT(*sizep == sizeof(*fpregset), ("%s: invalid size",
2285548a2ec4SAndrew Turner 		    __func__));
2286548a2ec4SAndrew Turner 		fpregset = buf;
2287548a2ec4SAndrew Turner #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2288548a2ec4SAndrew Turner 		fill_fpregs32(td, fpregset);
2289548a2ec4SAndrew Turner #else
2290548a2ec4SAndrew Turner 		fill_fpregs(td, fpregset);
2291548a2ec4SAndrew Turner #endif
2292548a2ec4SAndrew Turner 	}
22930b25cbc7SJohn Baldwin 	*sizep = sizeof(*fpregset);
2294548a2ec4SAndrew Turner 	return (true);
2295548a2ec4SAndrew Turner }
2296548a2ec4SAndrew Turner 
2297548a2ec4SAndrew Turner static bool
2298548a2ec4SAndrew Turner __elfN(set_fpregset)(struct regset *rs, struct thread *td, void *buf,
2299548a2ec4SAndrew Turner     size_t size)
2300548a2ec4SAndrew Turner {
2301548a2ec4SAndrew Turner 	elf_prfpregset_t *fpregset;
2302548a2ec4SAndrew Turner 
2303548a2ec4SAndrew Turner 	fpregset = buf;
2304548a2ec4SAndrew Turner 	KASSERT(size == sizeof(*fpregset), ("%s: invalid size", __func__));
2305548a2ec4SAndrew Turner #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2306548a2ec4SAndrew Turner 	set_fpregs32(td, fpregset);
2307548a2ec4SAndrew Turner #else
2308548a2ec4SAndrew Turner 	set_fpregs(td, fpregset);
2309548a2ec4SAndrew Turner #endif
2310548a2ec4SAndrew Turner 	return (true);
2311548a2ec4SAndrew Turner }
2312548a2ec4SAndrew Turner 
2313548a2ec4SAndrew Turner static struct regset __elfN(regset_fpregset) = {
2314548a2ec4SAndrew Turner 	.note = NT_FPREGSET,
2315548a2ec4SAndrew Turner 	.size = sizeof(elf_prfpregset_t),
2316548a2ec4SAndrew Turner 	.get = __elfN(get_fpregset),
2317548a2ec4SAndrew Turner 	.set = __elfN(set_fpregset),
2318548a2ec4SAndrew Turner };
2319548a2ec4SAndrew Turner ELF_REGSET(__elfN(regset_fpregset));
2320548a2ec4SAndrew Turner 
23216b71405bSJohn Baldwin static size_t
23226b71405bSJohn Baldwin __elfN(prepare_register_notes)(struct thread *td, struct note_info_list *list,
23236b71405bSJohn Baldwin     struct thread *target_td)
2324bd390213SMikolaj Golub {
23256b71405bSJohn Baldwin 	struct sysentvec *sv = td->td_proc->p_sysent;
23266b71405bSJohn Baldwin 	struct regset **regsetp, **regset_end, *regset;
23276b71405bSJohn Baldwin 	size_t size;
2328bd390213SMikolaj Golub 
23296b71405bSJohn Baldwin 	size = 0;
23306b71405bSJohn Baldwin 
23316b71405bSJohn Baldwin 	/* NT_PRSTATUS must be the first register set note. */
23326b71405bSJohn Baldwin 	size += __elfN(register_regset_note)(td, list, &__elfN(regset_prstatus),
23336b71405bSJohn Baldwin 	    target_td);
23346b71405bSJohn Baldwin 
23356b71405bSJohn Baldwin 	regsetp = sv->sv_regset_begin;
23366b71405bSJohn Baldwin 	if (regsetp == NULL) {
23376b71405bSJohn Baldwin 		/* XXX: This shouldn't be true for any FreeBSD ABIs. */
23386b71405bSJohn Baldwin 		size += __elfN(register_regset_note)(td, list,
23396b71405bSJohn Baldwin 		    &__elfN(regset_fpregset), target_td);
23406b71405bSJohn Baldwin 		return (size);
2341bd390213SMikolaj Golub 	}
23426b71405bSJohn Baldwin 	regset_end = sv->sv_regset_end;
23436b71405bSJohn Baldwin 	MPASS(regset_end != NULL);
23446b71405bSJohn Baldwin 	for (; regsetp < regset_end; regsetp++) {
23456b71405bSJohn Baldwin 		regset = *regsetp;
23466b71405bSJohn Baldwin 		if (regset->note == NT_PRSTATUS)
23476b71405bSJohn Baldwin 			continue;
23486b71405bSJohn Baldwin 		size += __elfN(register_regset_note)(td, list, regset,
23496b71405bSJohn Baldwin 		    target_td);
23506b71405bSJohn Baldwin 	}
23516b71405bSJohn Baldwin 	return (size);
2352bd390213SMikolaj Golub }
2353bd390213SMikolaj Golub 
2354bd390213SMikolaj Golub static void
2355bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
2356bd390213SMikolaj Golub {
2357bd390213SMikolaj Golub 	struct thread *td;
2358bd390213SMikolaj Golub 	elf_thrmisc_t thrmisc;
2359bd390213SMikolaj Golub 
236061b4c627SEdward Tomasz Napierala 	td = arg;
2361bd390213SMikolaj Golub 	if (sb != NULL) {
2362bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
23632e5f9189SEd Maste 		bzero(&thrmisc, sizeof(thrmisc));
2364bd390213SMikolaj Golub 		strcpy(thrmisc.pr_tname, td->td_name);
2365bd390213SMikolaj Golub 		sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
2366bd390213SMikolaj Golub 	}
2367bd390213SMikolaj Golub 	*sizep = sizeof(thrmisc);
2368bd390213SMikolaj Golub }
2369bd390213SMikolaj Golub 
237086be94fcSTycho Nightingale static void
237186be94fcSTycho Nightingale __elfN(note_ptlwpinfo)(void *arg, struct sbuf *sb, size_t *sizep)
237286be94fcSTycho Nightingale {
237386be94fcSTycho Nightingale 	struct thread *td;
237486be94fcSTycho Nightingale 	size_t size;
237586be94fcSTycho Nightingale 	int structsize;
237651645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
237751645e83SJohn Baldwin 	struct ptrace_lwpinfo32 pl;
237851645e83SJohn Baldwin #else
237986be94fcSTycho Nightingale 	struct ptrace_lwpinfo pl;
238051645e83SJohn Baldwin #endif
238186be94fcSTycho Nightingale 
238261b4c627SEdward Tomasz Napierala 	td = arg;
238351645e83SJohn Baldwin 	size = sizeof(structsize) + sizeof(pl);
238486be94fcSTycho Nightingale 	if (sb != NULL) {
238586be94fcSTycho Nightingale 		KASSERT(*sizep == size, ("invalid size"));
238651645e83SJohn Baldwin 		structsize = sizeof(pl);
238786be94fcSTycho Nightingale 		sbuf_bcat(sb, &structsize, sizeof(structsize));
238886be94fcSTycho Nightingale 		bzero(&pl, sizeof(pl));
238986be94fcSTycho Nightingale 		pl.pl_lwpid = td->td_tid;
239086be94fcSTycho Nightingale 		pl.pl_event = PL_EVENT_NONE;
239186be94fcSTycho Nightingale 		pl.pl_sigmask = td->td_sigmask;
239286be94fcSTycho Nightingale 		pl.pl_siglist = td->td_siglist;
239386be94fcSTycho Nightingale 		if (td->td_si.si_signo != 0) {
239486be94fcSTycho Nightingale 			pl.pl_event = PL_EVENT_SIGNAL;
239586be94fcSTycho Nightingale 			pl.pl_flags |= PL_FLAG_SI;
239651645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
239751645e83SJohn Baldwin 			siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo);
239851645e83SJohn Baldwin #else
239986be94fcSTycho Nightingale 			pl.pl_siginfo = td->td_si;
240051645e83SJohn Baldwin #endif
240186be94fcSTycho Nightingale 		}
240286be94fcSTycho Nightingale 		strcpy(pl.pl_tdname, td->td_name);
240386be94fcSTycho Nightingale 		/* XXX TODO: supply more information in struct ptrace_lwpinfo*/
240451645e83SJohn Baldwin 		sbuf_bcat(sb, &pl, sizeof(pl));
240586be94fcSTycho Nightingale 	}
240686be94fcSTycho Nightingale 	*sizep = size;
240786be94fcSTycho Nightingale }
240886be94fcSTycho Nightingale 
24094da47b2fSMarcel Moolenaar /*
24104da47b2fSMarcel Moolenaar  * Allow for MD specific notes, as well as any MD
24114da47b2fSMarcel Moolenaar  * specific preparations for writing MI notes.
24124da47b2fSMarcel Moolenaar  */
24138c64af4fSJohn Polstra static void
2414bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
24158c64af4fSJohn Polstra {
2416bd390213SMikolaj Golub 	struct thread *td;
2417bd390213SMikolaj Golub 	void *buf;
2418bd390213SMikolaj Golub 	size_t size;
24198c64af4fSJohn Polstra 
2420bd390213SMikolaj Golub 	td = (struct thread *)arg;
2421bd390213SMikolaj Golub 	size = *sizep;
2422bd390213SMikolaj Golub 	if (size != 0 && sb != NULL)
2423bd390213SMikolaj Golub 		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
242483a396ceSChristian Brueffer 	else
242583a396ceSChristian Brueffer 		buf = NULL;
2426bd390213SMikolaj Golub 	size = 0;
2427bd390213SMikolaj Golub 	__elfN(dump_thread)(td, buf, &size);
242864779280SKonstantin Belousov 	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
242983a396ceSChristian Brueffer 	if (size != 0 && sb != NULL)
2430bd390213SMikolaj Golub 		sbuf_bcat(sb, buf, size);
2431a1761d73SChristian Brueffer 	free(buf, M_TEMP);
2432bd390213SMikolaj Golub 	*sizep = size;
24338c64af4fSJohn Polstra }
24348c64af4fSJohn Polstra 
2435f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE
2436f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
2437f1fca82eSMikolaj Golub #endif
2438f1fca82eSMikolaj Golub 
2439f1fca82eSMikolaj Golub static void
2440f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
2441f1fca82eSMikolaj Golub {
2442f1fca82eSMikolaj Golub 	struct proc *p;
2443f1fca82eSMikolaj Golub 	size_t size;
2444f1fca82eSMikolaj Golub 	int structsize;
2445f1fca82eSMikolaj Golub 
244661b4c627SEdward Tomasz Napierala 	p = arg;
2447f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_numthreads *
2448f1fca82eSMikolaj Golub 	    sizeof(elf_kinfo_proc_t);
2449f1fca82eSMikolaj Golub 
2450f1fca82eSMikolaj Golub 	if (sb != NULL) {
2451f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2452f1fca82eSMikolaj Golub 		structsize = sizeof(elf_kinfo_proc_t);
2453f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
24544daea938SKonstantin Belousov 		sx_slock(&proctree_lock);
2455f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2456f1fca82eSMikolaj Golub 		kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
24574daea938SKonstantin Belousov 		sx_sunlock(&proctree_lock);
2458f1fca82eSMikolaj Golub 	}
2459f1fca82eSMikolaj Golub 	*sizep = size;
2460f1fca82eSMikolaj Golub }
2461f1fca82eSMikolaj Golub 
2462f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE
2463f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
2464f1fca82eSMikolaj Golub #endif
2465f1fca82eSMikolaj Golub 
2466f1fca82eSMikolaj Golub static void
2467f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
2468f1fca82eSMikolaj Golub {
2469f1fca82eSMikolaj Golub 	struct proc *p;
247014bdbaf2SConrad Meyer 	size_t size, sect_sz, i;
247114bdbaf2SConrad Meyer 	ssize_t start_len, sect_len;
247214bdbaf2SConrad Meyer 	int structsize, filedesc_flags;
247314bdbaf2SConrad Meyer 
2474bcb60d52SConrad Meyer 	if (coredump_pack_fileinfo)
247514bdbaf2SConrad Meyer 		filedesc_flags = KERN_FILEDESC_PACK_KINFO;
247614bdbaf2SConrad Meyer 	else
247714bdbaf2SConrad Meyer 		filedesc_flags = 0;
2478f1fca82eSMikolaj Golub 
247961b4c627SEdward Tomasz Napierala 	p = arg;
248014bdbaf2SConrad Meyer 	structsize = sizeof(struct kinfo_file);
2481f1fca82eSMikolaj Golub 	if (sb == NULL) {
2482f1fca82eSMikolaj Golub 		size = 0;
2483f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
24845c32e9fcSAlexander Motin 		sbuf_set_drain(sb, sbuf_count_drain, &size);
2485f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2486f1fca82eSMikolaj Golub 		PROC_LOCK(p);
248714bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2488f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2489f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2490f1fca82eSMikolaj Golub 		*sizep = size;
2491f1fca82eSMikolaj Golub 	} else {
249214bdbaf2SConrad Meyer 		sbuf_start_section(sb, &start_len);
249314bdbaf2SConrad Meyer 
2494f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2495f1fca82eSMikolaj Golub 		PROC_LOCK(p);
249614bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
249714bdbaf2SConrad Meyer 		    filedesc_flags);
249814bdbaf2SConrad Meyer 
249914bdbaf2SConrad Meyer 		sect_len = sbuf_end_section(sb, start_len, 0, 0);
250014bdbaf2SConrad Meyer 		if (sect_len < 0)
250114bdbaf2SConrad Meyer 			return;
250214bdbaf2SConrad Meyer 		sect_sz = sect_len;
250314bdbaf2SConrad Meyer 
250414bdbaf2SConrad Meyer 		KASSERT(sect_sz <= *sizep,
250514bdbaf2SConrad Meyer 		    ("kern_proc_filedesc_out did not respect maxlen; "
250614bdbaf2SConrad Meyer 		     "requested %zu, got %zu", *sizep - sizeof(structsize),
250714bdbaf2SConrad Meyer 		     sect_sz - sizeof(structsize)));
250814bdbaf2SConrad Meyer 
250914bdbaf2SConrad Meyer 		for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
251014bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
2511f1fca82eSMikolaj Golub 	}
2512f1fca82eSMikolaj Golub }
2513f1fca82eSMikolaj Golub 
2514f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE
2515f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2516f1fca82eSMikolaj Golub #endif
2517f1fca82eSMikolaj Golub 
2518f1fca82eSMikolaj Golub static void
2519f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2520f1fca82eSMikolaj Golub {
2521f1fca82eSMikolaj Golub 	struct proc *p;
2522f1fca82eSMikolaj Golub 	size_t size;
2523e6b95927SConrad Meyer 	int structsize, vmmap_flags;
2524e6b95927SConrad Meyer 
2525e6b95927SConrad Meyer 	if (coredump_pack_vmmapinfo)
2526e6b95927SConrad Meyer 		vmmap_flags = KERN_VMMAP_PACK_KINFO;
2527e6b95927SConrad Meyer 	else
2528e6b95927SConrad Meyer 		vmmap_flags = 0;
2529f1fca82eSMikolaj Golub 
253061b4c627SEdward Tomasz Napierala 	p = arg;
2531e6b95927SConrad Meyer 	structsize = sizeof(struct kinfo_vmentry);
2532f1fca82eSMikolaj Golub 	if (sb == NULL) {
2533f1fca82eSMikolaj Golub 		size = 0;
2534f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
25355c32e9fcSAlexander Motin 		sbuf_set_drain(sb, sbuf_count_drain, &size);
2536f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2537f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2538e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2539f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2540f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2541f1fca82eSMikolaj Golub 		*sizep = size;
2542f1fca82eSMikolaj Golub 	} else {
2543f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2544f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2545e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2546e6b95927SConrad Meyer 		    vmmap_flags);
2547f1fca82eSMikolaj Golub 	}
2548f1fca82eSMikolaj Golub }
2549f1fca82eSMikolaj Golub 
2550f1fca82eSMikolaj Golub static void
2551f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2552f1fca82eSMikolaj Golub {
2553f1fca82eSMikolaj Golub 	struct proc *p;
2554f1fca82eSMikolaj Golub 	size_t size;
2555f1fca82eSMikolaj Golub 	int structsize;
2556f1fca82eSMikolaj Golub 
255761b4c627SEdward Tomasz Napierala 	p = arg;
2558f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
2559f1fca82eSMikolaj Golub 	if (sb != NULL) {
2560f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2561f1fca82eSMikolaj Golub 		structsize = sizeof(gid_t);
2562f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2563f1fca82eSMikolaj Golub 		sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2564f1fca82eSMikolaj Golub 		    sizeof(gid_t));
2565f1fca82eSMikolaj Golub 	}
2566f1fca82eSMikolaj Golub 	*sizep = size;
2567f1fca82eSMikolaj Golub }
2568f1fca82eSMikolaj Golub 
2569f1fca82eSMikolaj Golub static void
2570f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2571f1fca82eSMikolaj Golub {
2572f1fca82eSMikolaj Golub 	struct proc *p;
2573f1fca82eSMikolaj Golub 	size_t size;
2574f1fca82eSMikolaj Golub 	int structsize;
2575f1fca82eSMikolaj Golub 
257661b4c627SEdward Tomasz Napierala 	p = arg;
257785078b85SConrad Meyer 	size = sizeof(structsize) + sizeof(p->p_pd->pd_cmask);
2578f1fca82eSMikolaj Golub 	if (sb != NULL) {
2579f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
258085078b85SConrad Meyer 		structsize = sizeof(p->p_pd->pd_cmask);
2581f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
258285078b85SConrad Meyer 		sbuf_bcat(sb, &p->p_pd->pd_cmask, sizeof(p->p_pd->pd_cmask));
2583f1fca82eSMikolaj Golub 	}
2584f1fca82eSMikolaj Golub 	*sizep = size;
2585f1fca82eSMikolaj Golub }
2586f1fca82eSMikolaj Golub 
2587f1fca82eSMikolaj Golub static void
2588f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2589f1fca82eSMikolaj Golub {
2590f1fca82eSMikolaj Golub 	struct proc *p;
2591f1fca82eSMikolaj Golub 	struct rlimit rlim[RLIM_NLIMITS];
2592f1fca82eSMikolaj Golub 	size_t size;
2593f1fca82eSMikolaj Golub 	int structsize, i;
2594f1fca82eSMikolaj Golub 
259561b4c627SEdward Tomasz Napierala 	p = arg;
2596f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(rlim);
2597f1fca82eSMikolaj Golub 	if (sb != NULL) {
2598f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2599f1fca82eSMikolaj Golub 		structsize = sizeof(rlim);
2600f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2601f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2602f1fca82eSMikolaj Golub 		for (i = 0; i < RLIM_NLIMITS; i++)
2603f6f6d240SMateusz Guzik 			lim_rlimit_proc(p, i, &rlim[i]);
2604f1fca82eSMikolaj Golub 		PROC_UNLOCK(p);
2605f1fca82eSMikolaj Golub 		sbuf_bcat(sb, rlim, sizeof(rlim));
2606f1fca82eSMikolaj Golub 	}
2607f1fca82eSMikolaj Golub 	*sizep = size;
2608f1fca82eSMikolaj Golub }
2609f1fca82eSMikolaj Golub 
2610f1fca82eSMikolaj Golub static void
2611f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2612f1fca82eSMikolaj Golub {
2613f1fca82eSMikolaj Golub 	struct proc *p;
2614f1fca82eSMikolaj Golub 	size_t size;
2615f1fca82eSMikolaj Golub 	int structsize;
2616f1fca82eSMikolaj Golub 
261761b4c627SEdward Tomasz Napierala 	p = arg;
2618f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_osrel);
2619f1fca82eSMikolaj Golub 	if (sb != NULL) {
2620f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2621f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_osrel);
2622f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2623f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2624f1fca82eSMikolaj Golub 	}
2625f1fca82eSMikolaj Golub 	*sizep = size;
2626f1fca82eSMikolaj Golub }
2627f1fca82eSMikolaj Golub 
2628f1fca82eSMikolaj Golub static void
2629f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2630f1fca82eSMikolaj Golub {
2631f1fca82eSMikolaj Golub 	struct proc *p;
2632f1fca82eSMikolaj Golub 	elf_ps_strings_t ps_strings;
2633f1fca82eSMikolaj Golub 	size_t size;
2634f1fca82eSMikolaj Golub 	int structsize;
2635f1fca82eSMikolaj Golub 
263661b4c627SEdward Tomasz Napierala 	p = arg;
2637f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(ps_strings);
2638f1fca82eSMikolaj Golub 	if (sb != NULL) {
2639f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2640f1fca82eSMikolaj Golub 		structsize = sizeof(ps_strings);
2641f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2642706f4a81SMark Johnston 		ps_strings = PTROUT(PROC_PS_STRINGS(p));
2643f1fca82eSMikolaj Golub #else
2644706f4a81SMark Johnston 		ps_strings = PROC_PS_STRINGS(p);
2645f1fca82eSMikolaj Golub #endif
2646f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2647f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2648f1fca82eSMikolaj Golub 	}
2649f1fca82eSMikolaj Golub 	*sizep = size;
2650f1fca82eSMikolaj Golub }
2651f1fca82eSMikolaj Golub 
2652f1fca82eSMikolaj Golub static void
2653f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2654f1fca82eSMikolaj Golub {
2655f1fca82eSMikolaj Golub 	struct proc *p;
2656f1fca82eSMikolaj Golub 	size_t size;
2657f1fca82eSMikolaj Golub 	int structsize;
2658f1fca82eSMikolaj Golub 
265961b4c627SEdward Tomasz Napierala 	p = arg;
2660f1fca82eSMikolaj Golub 	if (sb == NULL) {
2661f1fca82eSMikolaj Golub 		size = 0;
266206250515SEdward Tomasz Napierala 		sb = sbuf_new(NULL, NULL, AT_COUNT * sizeof(Elf_Auxinfo),
266306250515SEdward Tomasz Napierala 		    SBUF_FIXEDLEN);
26645c32e9fcSAlexander Motin 		sbuf_set_drain(sb, sbuf_count_drain, &size);
2665f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2666f1fca82eSMikolaj Golub 		PHOLD(p);
2667f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2668f1fca82eSMikolaj Golub 		PRELE(p);
2669f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2670f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2671f1fca82eSMikolaj Golub 		*sizep = size;
2672f1fca82eSMikolaj Golub 	} else {
2673f1fca82eSMikolaj Golub 		structsize = sizeof(Elf_Auxinfo);
2674f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2675f1fca82eSMikolaj Golub 		PHOLD(p);
2676f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2677f1fca82eSMikolaj Golub 		PRELE(p);
2678f1fca82eSMikolaj Golub 	}
2679f1fca82eSMikolaj Golub }
2680f1fca82eSMikolaj Golub 
268119621645SAlex Richardson static bool
268292328a32SKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Note *checknote,
268392328a32SKonstantin Belousov     const char *note_vendor, const Elf_Phdr *pnote,
268419621645SAlex Richardson     bool (*cb)(const Elf_Note *, void *, bool *), void *cb_arg)
268532c01de2SDmitry Chagin {
2686267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
268732c01de2SDmitry Chagin 	const char *note_name;
26886c775eb6SKonstantin Belousov 	char *buf;
26896c775eb6SKonstantin Belousov 	int i, error;
269019621645SAlex Richardson 	bool res;
269132c01de2SDmitry Chagin 
26926c775eb6SKonstantin Belousov 	/* We need some limit, might as well use PAGE_SIZE. */
26936c775eb6SKonstantin Belousov 	if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
269419621645SAlex Richardson 		return (false);
26956c775eb6SKonstantin Belousov 	ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
26966c775eb6SKonstantin Belousov 	if (pnote->p_offset > PAGE_SIZE ||
26976c775eb6SKonstantin Belousov 	    pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
26982d6b8546SKonstantin Belousov 		buf = malloc(pnote->p_filesz, M_TEMP, M_NOWAIT);
26992d6b8546SKonstantin Belousov 		if (buf == NULL) {
2700b249ce48SMateusz Guzik 			VOP_UNLOCK(imgp->vp);
27016c775eb6SKonstantin Belousov 			buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
270278022527SKonstantin Belousov 			vn_lock(imgp->vp, LK_SHARED | LK_RETRY);
27032d6b8546SKonstantin Belousov 		}
27046c775eb6SKonstantin Belousov 		error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
27056c775eb6SKonstantin Belousov 		    pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
27066c775eb6SKonstantin Belousov 		    curthread->td_ucred, NOCRED, NULL, curthread);
27076c775eb6SKonstantin Belousov 		if (error != 0) {
27086c775eb6SKonstantin Belousov 			uprintf("i/o error PT_NOTE\n");
2709eda8fe63SKonstantin Belousov 			goto retf;
27106c775eb6SKonstantin Belousov 		}
27116c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)buf;
27126c775eb6SKonstantin Belousov 		note_end = (const Elf_Note *)(buf + pnote->p_filesz);
27136c775eb6SKonstantin Belousov 	} else {
27146c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)(imgp->image_header +
27156c775eb6SKonstantin Belousov 		    pnote->p_offset);
271632c01de2SDmitry Chagin 		note_end = (const Elf_Note *)(imgp->image_header +
271732c01de2SDmitry Chagin 		    pnote->p_offset + pnote->p_filesz);
27186c775eb6SKonstantin Belousov 		buf = NULL;
27196c775eb6SKonstantin Belousov 	}
2720267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2721d1ae5c83SKonstantin Belousov 		if (!aligned(note, Elf32_Addr) || (const char *)note_end -
27226c775eb6SKonstantin Belousov 		    (const char *)note < sizeof(Elf_Note)) {
2723eda8fe63SKonstantin Belousov 			goto retf;
27246c775eb6SKonstantin Belousov 		}
272592328a32SKonstantin Belousov 		if (note->n_namesz != checknote->n_namesz ||
272692328a32SKonstantin Belousov 		    note->n_descsz != checknote->n_descsz ||
272792328a32SKonstantin Belousov 		    note->n_type != checknote->n_type)
272832c01de2SDmitry Chagin 			goto nextnote;
272932c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
273092328a32SKonstantin Belousov 		if (note_name + checknote->n_namesz >=
273192328a32SKonstantin Belousov 		    (const char *)note_end || strncmp(note_vendor,
273292328a32SKonstantin Belousov 		    note_name, checknote->n_namesz) != 0)
273332c01de2SDmitry Chagin 			goto nextnote;
273432c01de2SDmitry Chagin 
273592328a32SKonstantin Belousov 		if (cb(note, cb_arg, &res))
27366c775eb6SKonstantin Belousov 			goto ret;
273732c01de2SDmitry Chagin nextnote:
273832c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
27391b8388cdSMikolaj Golub 		    roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
27401b8388cdSMikolaj Golub 		    roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
274132c01de2SDmitry Chagin 	}
2742eda8fe63SKonstantin Belousov retf:
274319621645SAlex Richardson 	res = false;
27446c775eb6SKonstantin Belousov ret:
27456c775eb6SKonstantin Belousov 	free(buf, M_TEMP);
27466c775eb6SKonstantin Belousov 	return (res);
274732c01de2SDmitry Chagin }
274832c01de2SDmitry Chagin 
274992328a32SKonstantin Belousov struct brandnote_cb_arg {
275092328a32SKonstantin Belousov 	Elf_Brandnote *brandnote;
275192328a32SKonstantin Belousov 	int32_t *osrel;
275292328a32SKonstantin Belousov };
275392328a32SKonstantin Belousov 
275419621645SAlex Richardson static bool
275519621645SAlex Richardson brandnote_cb(const Elf_Note *note, void *arg0, bool *res)
275692328a32SKonstantin Belousov {
275792328a32SKonstantin Belousov 	struct brandnote_cb_arg *arg;
275892328a32SKonstantin Belousov 
275992328a32SKonstantin Belousov 	arg = arg0;
276092328a32SKonstantin Belousov 
276192328a32SKonstantin Belousov 	/*
276292328a32SKonstantin Belousov 	 * Fetch the osreldate for binary from the ELF OSABI-note if
276392328a32SKonstantin Belousov 	 * necessary.
276492328a32SKonstantin Belousov 	 */
276592328a32SKonstantin Belousov 	*res = (arg->brandnote->flags & BN_TRANSLATE_OSREL) != 0 &&
276692328a32SKonstantin Belousov 	    arg->brandnote->trans_osrel != NULL ?
276719621645SAlex Richardson 	    arg->brandnote->trans_osrel(note, arg->osrel) : true;
276892328a32SKonstantin Belousov 
276919621645SAlex Richardson 	return (true);
277092328a32SKonstantin Belousov }
277192328a32SKonstantin Belousov 
2772cefb93f2SKonstantin Belousov static Elf_Note fctl_note = {
2773cefb93f2SKonstantin Belousov 	.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
2774cefb93f2SKonstantin Belousov 	.n_descsz = sizeof(uint32_t),
2775cefb93f2SKonstantin Belousov 	.n_type = NT_FREEBSD_FEATURE_CTL,
2776cefb93f2SKonstantin Belousov };
2777cefb93f2SKonstantin Belousov 
2778cefb93f2SKonstantin Belousov struct fctl_cb_arg {
277919621645SAlex Richardson 	bool *has_fctl0;
2780cefb93f2SKonstantin Belousov 	uint32_t *fctl0;
2781cefb93f2SKonstantin Belousov };
2782cefb93f2SKonstantin Belousov 
278319621645SAlex Richardson static bool
278419621645SAlex Richardson note_fctl_cb(const Elf_Note *note, void *arg0, bool *res)
2785cefb93f2SKonstantin Belousov {
2786cefb93f2SKonstantin Belousov 	struct fctl_cb_arg *arg;
2787cefb93f2SKonstantin Belousov 	const Elf32_Word *desc;
2788cefb93f2SKonstantin Belousov 	uintptr_t p;
2789cefb93f2SKonstantin Belousov 
2790cefb93f2SKonstantin Belousov 	arg = arg0;
2791cefb93f2SKonstantin Belousov 	p = (uintptr_t)(note + 1);
2792cefb93f2SKonstantin Belousov 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
2793cefb93f2SKonstantin Belousov 	desc = (const Elf32_Word *)p;
279419621645SAlex Richardson 	*arg->has_fctl0 = true;
2795cefb93f2SKonstantin Belousov 	*arg->fctl0 = desc[0];
279619621645SAlex Richardson 	*res = true;
279719621645SAlex Richardson 	return (true);
2798cefb93f2SKonstantin Belousov }
2799cefb93f2SKonstantin Belousov 
280032c01de2SDmitry Chagin /*
2801cefb93f2SKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote, fetch
2802cefb93f2SKonstantin Belousov  * the osreldate and feature control flags for binary from the ELF
2803cefb93f2SKonstantin Belousov  * OSABI-note.  Only the first page of the image is searched, the same
2804cefb93f2SKonstantin Belousov  * as for headers.
28051a9c7decSKonstantin Belousov  */
280619621645SAlex Richardson static bool
280792328a32SKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *brandnote,
280819621645SAlex Richardson     int32_t *osrel, bool *has_fctl0, uint32_t *fctl0)
28091a9c7decSKonstantin Belousov {
28101a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
28111a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
281292328a32SKonstantin Belousov 	struct brandnote_cb_arg b_arg;
2813cefb93f2SKonstantin Belousov 	struct fctl_cb_arg f_arg;
2814cefb93f2SKonstantin Belousov 	int i, j;
28151a9c7decSKonstantin Belousov 
28161a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
28171a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
281892328a32SKonstantin Belousov 	b_arg.brandnote = brandnote;
281992328a32SKonstantin Belousov 	b_arg.osrel = osrel;
28200cad2aa2SKonstantin Belousov 	f_arg.has_fctl0 = has_fctl0;
2821cefb93f2SKonstantin Belousov 	f_arg.fctl0 = fctl0;
28221a9c7decSKonstantin Belousov 
28231a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
282492328a32SKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp,
282592328a32SKonstantin Belousov 		    &brandnote->hdr, brandnote->vendor, &phdr[i], brandnote_cb,
282692328a32SKonstantin Belousov 		    &b_arg)) {
2827cefb93f2SKonstantin Belousov 			for (j = 0; j < hdr->e_phnum; j++) {
2828cefb93f2SKonstantin Belousov 				if (phdr[j].p_type == PT_NOTE &&
2829cefb93f2SKonstantin Belousov 				    __elfN(parse_notes)(imgp, &fctl_note,
2830cefb93f2SKonstantin Belousov 				    FREEBSD_ABI_VENDOR, &phdr[j],
2831cefb93f2SKonstantin Belousov 				    note_fctl_cb, &f_arg))
2832cefb93f2SKonstantin Belousov 					break;
2833cefb93f2SKonstantin Belousov 			}
283419621645SAlex Richardson 			return (true);
28351a9c7decSKonstantin Belousov 		}
283692328a32SKonstantin Belousov 	}
283719621645SAlex Richardson 	return (false);
28381a9c7decSKonstantin Belousov 
28391a9c7decSKonstantin Belousov }
28401a9c7decSKonstantin Belousov 
28411a9c7decSKonstantin Belousov /*
2842e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
2843e1743d02SSøren Schmidt  */
2844a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
2845b7feabf9SEd Maste 	.ex_imgact = __CONCAT(exec_, __elfN(imgact)),
2846b7feabf9SEd Maste 	.ex_name = __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2847a360a43dSJake Burkholder };
2848a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2849e7228204SAlfred Perlstein 
2850ed167eaaSKonstantin Belousov static vm_prot_t
2851ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
2852ed167eaaSKonstantin Belousov {
2853ed167eaaSKonstantin Belousov 	vm_prot_t prot;
2854ed167eaaSKonstantin Belousov 
2855ed167eaaSKonstantin Belousov 	prot = 0;
2856ed167eaaSKonstantin Belousov 	if (flags & PF_X)
2857ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
2858ed167eaaSKonstantin Belousov 	if (flags & PF_W)
2859ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
2860ed167eaaSKonstantin Belousov 	if (flags & PF_R)
2861ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
2862eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
2863126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
2864676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
2865676eda08SMarcel Moolenaar #endif
2866ed167eaaSKonstantin Belousov 	return (prot);
2867ed167eaaSKonstantin Belousov }
2868ed167eaaSKonstantin Belousov 
2869ed167eaaSKonstantin Belousov static Elf_Word
2870ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
2871ed167eaaSKonstantin Belousov {
2872ed167eaaSKonstantin Belousov 	Elf_Word flags;
2873ed167eaaSKonstantin Belousov 
2874ed167eaaSKonstantin Belousov 	flags = 0;
2875ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
2876ed167eaaSKonstantin Belousov 		flags |= PF_X;
2877ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
2878ed167eaaSKonstantin Belousov 		flags |= PF_R;
2879ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
2880ed167eaaSKonstantin Belousov 		flags |= PF_W;
2881ed167eaaSKonstantin Belousov 	return (flags);
2882ed167eaaSKonstantin Belousov }
2883