xref: /freebsd/sys/kern/imgact_elf.c (revision eb785fab3bf49fc0599ee52ac972b81b568c6076)
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>
538c64af4fSJohn Polstra #include <sys/pioctl.h>
54a794e791SBruce Evans #include <sys/proc.h>
558c64af4fSJohn Polstra #include <sys/procfs.h>
5686be94fcSTycho Nightingale #include <sys/ptrace.h>
571ba5ad42SEdward Tomasz Napierala #include <sys/racct.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,
91cefb93f2SKonstantin Belousov     const char *interp, int interp_name_len, int32_t *osrel, uint32_t *fctl0);
923ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
933ebc1248SPeter Wemm     u_long *entry, size_t pagesize);
940bbee4cdSKonstantin Belousov static int __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
95292177e6SAlan Cox     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
96292177e6SAlan Cox     size_t pagesize);
973ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
98a95659f7SEd Maste static bool __elfN(freebsd_trans_osrel)(const Elf_Note *note,
9989ffc202SBjoern A. Zeeb     int32_t *osrel);
100a95659f7SEd Maste static bool kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
10132c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp,
102cefb93f2SKonstantin Belousov     Elf_Brandnote *checknote, int32_t *osrel, 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);
105e1743d02SSøren Schmidt 
106a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
107a360a43dSJake Burkholder     "");
108a360a43dSJake Burkholder 
109bd390213SMikolaj Golub #define	CORE_BUF_SIZE	(16 * 1024)
110e7228204SAlfred Perlstein 
111e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1;
112e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
113af3b2549SHans Petter Selasky     fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
114a360a43dSJake Burkholder     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
115a360a43dSJake Burkholder 
116551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0;
117a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
1181cbb879dSEd Maste     &elf_legacy_coredump, 0,
1191cbb879dSEd Maste     "include all and only RW pages in core dumps");
120e1743d02SSøren Schmidt 
12162c625fdSKonstantin Belousov int __elfN(nxstack) =
1224d22d07aSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */ || \
1234bf4b0f1SJohn Baldwin     (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) || \
1244bf4b0f1SJohn Baldwin     defined(__riscv)
12562c625fdSKonstantin Belousov 	1;
12662c625fdSKonstantin Belousov #else
12762c625fdSKonstantin Belousov 	0;
12862c625fdSKonstantin Belousov #endif
129291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
130291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
131291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
132291c06a1SKonstantin Belousov 
133*eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
134126b36a2SKonstantin Belousov int i386_read_exec = 0;
135126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
136126b36a2SKonstantin Belousov     "enable execution from readable segments");
137126b36a2SKonstantin Belousov #endif
138126b36a2SKonstantin Belousov 
1393ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
140e1743d02SSøren Schmidt 
141d9c9c81cSPedro F. Giffuni #define	trunc_page_ps(va, ps)	rounddown2(va, ps)
142d9c9c81cSPedro F. Giffuni #define	round_page_ps(va, ps)	roundup2(va, ps)
14393d1c728SKonstantin Belousov #define	aligned(a, t)	(trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
14493d1c728SKonstantin Belousov 
14532c01de2SDmitry Chagin static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
14632c01de2SDmitry Chagin 
14732c01de2SDmitry Chagin Elf_Brandnote __elfN(freebsd_brandnote) = {
14832c01de2SDmitry Chagin 	.hdr.n_namesz	= sizeof(FREEBSD_ABI_VENDOR),
14932c01de2SDmitry Chagin 	.hdr.n_descsz	= sizeof(int32_t),
1504c22b468SEd Maste 	.hdr.n_type	= NT_FREEBSD_ABI_TAG,
15132c01de2SDmitry Chagin 	.vendor		= FREEBSD_ABI_VENDOR,
15289ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
15389ffc202SBjoern A. Zeeb 	.trans_osrel	= __elfN(freebsd_trans_osrel)
15432c01de2SDmitry Chagin };
15532c01de2SDmitry Chagin 
156a95659f7SEd Maste static bool
15789ffc202SBjoern A. Zeeb __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
15889ffc202SBjoern A. Zeeb {
15989ffc202SBjoern A. Zeeb 	uintptr_t p;
16089ffc202SBjoern A. Zeeb 
16189ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1621b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
16389ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
16489ffc202SBjoern A. Zeeb 
165a95659f7SEd Maste 	return (true);
16689ffc202SBjoern A. Zeeb }
16789ffc202SBjoern A. Zeeb 
16889ffc202SBjoern A. Zeeb static const char GNU_ABI_VENDOR[] = "GNU";
16989ffc202SBjoern A. Zeeb static int GNU_KFREEBSD_ABI_DESC = 3;
17089ffc202SBjoern A. Zeeb 
17189ffc202SBjoern A. Zeeb Elf_Brandnote __elfN(kfreebsd_brandnote) = {
17289ffc202SBjoern A. Zeeb 	.hdr.n_namesz	= sizeof(GNU_ABI_VENDOR),
17389ffc202SBjoern A. Zeeb 	.hdr.n_descsz	= 16,	/* XXX at least 16 */
17489ffc202SBjoern A. Zeeb 	.hdr.n_type	= 1,
17589ffc202SBjoern A. Zeeb 	.vendor		= GNU_ABI_VENDOR,
17689ffc202SBjoern A. Zeeb 	.flags		= BN_TRANSLATE_OSREL,
17789ffc202SBjoern A. Zeeb 	.trans_osrel	= kfreebsd_trans_osrel
17889ffc202SBjoern A. Zeeb };
17989ffc202SBjoern A. Zeeb 
180a95659f7SEd Maste static bool
18189ffc202SBjoern A. Zeeb kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
18289ffc202SBjoern A. Zeeb {
18389ffc202SBjoern A. Zeeb 	const Elf32_Word *desc;
18489ffc202SBjoern A. Zeeb 	uintptr_t p;
18589ffc202SBjoern A. Zeeb 
18689ffc202SBjoern A. Zeeb 	p = (uintptr_t)(note + 1);
1871b8388cdSMikolaj Golub 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
18889ffc202SBjoern A. Zeeb 
18989ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
19089ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
191a95659f7SEd Maste 		return (false);
19289ffc202SBjoern A. Zeeb 
19389ffc202SBjoern A. Zeeb 	/*
19489ffc202SBjoern A. Zeeb 	 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
19589ffc202SBjoern A. Zeeb 	 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
19689ffc202SBjoern A. Zeeb 	 */
19789ffc202SBjoern A. Zeeb 	*osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
19889ffc202SBjoern A. Zeeb 
199a95659f7SEd Maste 	return (true);
20089ffc202SBjoern A. Zeeb }
20189ffc202SBjoern A. Zeeb 
202e1743d02SSøren Schmidt int
2033ebc1248SPeter Wemm __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
204e1743d02SSøren Schmidt {
205e1743d02SSøren Schmidt 	int i;
206e1743d02SSøren Schmidt 
2073ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
208ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == NULL) {
209ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = entry;
210e1743d02SSøren Schmidt 			break;
211e1743d02SSøren Schmidt 		}
212e1743d02SSøren Schmidt 	}
213925c8b5bSBjoern A. Zeeb 	if (i == MAX_BRANDS) {
214925c8b5bSBjoern A. Zeeb 		printf("WARNING: %s: could not insert brandinfo entry: %p\n",
215925c8b5bSBjoern A. Zeeb 			__func__, entry);
216a7cddfedSJake Burkholder 		return (-1);
217925c8b5bSBjoern A. Zeeb 	}
218a7cddfedSJake Burkholder 	return (0);
219e1743d02SSøren Schmidt }
220e1743d02SSøren Schmidt 
221e1743d02SSøren Schmidt int
2223ebc1248SPeter Wemm __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
223e1743d02SSøren Schmidt {
224e1743d02SSøren Schmidt 	int i;
225e1743d02SSøren Schmidt 
2263ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
227ea5a2b2eSSøren Schmidt 		if (elf_brand_list[i] == entry) {
228ea5a2b2eSSøren Schmidt 			elf_brand_list[i] = NULL;
229e1743d02SSøren Schmidt 			break;
230e1743d02SSøren Schmidt 		}
231e1743d02SSøren Schmidt 	}
232ea5a2b2eSSøren Schmidt 	if (i == MAX_BRANDS)
233a7cddfedSJake Burkholder 		return (-1);
234a7cddfedSJake Burkholder 	return (0);
235e1743d02SSøren Schmidt }
236e1743d02SSøren Schmidt 
237096977faSMark Newton int
2383ebc1248SPeter Wemm __elfN(brand_inuse)(Elf_Brandinfo *entry)
239096977faSMark Newton {
240096977faSMark Newton 	struct proc *p;
241553629ebSJake Burkholder 	int rval = FALSE;
242096977faSMark Newton 
2431005a129SJohn Baldwin 	sx_slock(&allproc_lock);
2444f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
245553629ebSJake Burkholder 		if (p->p_sysent == entry->sysvec) {
246553629ebSJake Burkholder 			rval = TRUE;
247553629ebSJake Burkholder 			break;
248096977faSMark Newton 		}
249553629ebSJake Burkholder 	}
2501005a129SJohn Baldwin 	sx_sunlock(&allproc_lock);
251096977faSMark Newton 
252553629ebSJake Burkholder 	return (rval);
253096977faSMark Newton }
254096977faSMark Newton 
2555fe3ed62SJake Burkholder static Elf_Brandinfo *
25632c01de2SDmitry Chagin __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
257cefb93f2SKonstantin Belousov     int interp_name_len, int32_t *osrel, uint32_t *fctl0)
2585fe3ed62SJake Burkholder {
25932c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
260af582aaeSKonstantin Belousov 	Elf_Brandinfo *bi, *bi_m;
26132c01de2SDmitry Chagin 	boolean_t ret;
2625fe3ed62SJake Burkholder 	int i;
2635fe3ed62SJake Burkholder 
2645fe3ed62SJake Burkholder 	/*
26532c01de2SDmitry Chagin 	 * We support four types of branding -- (1) the ELF EI_OSABI field
2665fe3ed62SJake Burkholder 	 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
26732c01de2SDmitry Chagin 	 * branding w/in the ELF header, (3) path of the `interp_path'
26832c01de2SDmitry Chagin 	 * field, and (4) the ".note.ABI-tag" ELF section.
2695fe3ed62SJake Burkholder 	 */
2705fe3ed62SJake Burkholder 
27132c01de2SDmitry Chagin 	/* Look for an ".note.ABI-tag" ELF section */
272af582aaeSKonstantin Belousov 	bi_m = NULL;
27332c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
27432c01de2SDmitry Chagin 		bi = elf_brand_list[i];
275ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
276ecc2fda8SBjoern A. Zeeb 			continue;
2772274ab3dSKonstantin Belousov 		if (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0)
2781438fe3cSKonstantin Belousov 			continue;
279ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
280ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
281cefb93f2SKonstantin Belousov 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel,
282cefb93f2SKonstantin Belousov 			    fctl0);
283f19d421aSNathan Whitehorn 			/* Give brand a chance to veto check_note's guess */
284f19d421aSNathan Whitehorn 			if (ret && bi->header_supported)
285f19d421aSNathan Whitehorn 				ret = bi->header_supported(imgp);
286af582aaeSKonstantin Belousov 			/*
287af582aaeSKonstantin Belousov 			 * If note checker claimed the binary, but the
288af582aaeSKonstantin Belousov 			 * interpreter path in the image does not
289af582aaeSKonstantin Belousov 			 * match default one for the brand, try to
290af582aaeSKonstantin Belousov 			 * search for other brands with the same
291af582aaeSKonstantin Belousov 			 * interpreter.  Either there is better brand
292af582aaeSKonstantin Belousov 			 * with the right interpreter, or, failing
293af582aaeSKonstantin Belousov 			 * this, we return first brand which accepted
294af582aaeSKonstantin Belousov 			 * our note and, optionally, header.
295af582aaeSKonstantin Belousov 			 */
2963aeacc55SKonstantin Belousov 			if (ret && bi_m == NULL && interp != NULL &&
2973aeacc55SKonstantin Belousov 			    (bi->interp_path == NULL ||
2983aeacc55SKonstantin Belousov 			    (strlen(bi->interp_path) + 1 != interp_name_len ||
2993aeacc55SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
3003aeacc55SKonstantin Belousov 			    != 0))) {
301af582aaeSKonstantin Belousov 				bi_m = bi;
302af582aaeSKonstantin Belousov 				ret = 0;
303af582aaeSKonstantin Belousov 			}
30432c01de2SDmitry Chagin 			if (ret)
30532c01de2SDmitry Chagin 				return (bi);
30632c01de2SDmitry Chagin 		}
30732c01de2SDmitry Chagin 	}
308af582aaeSKonstantin Belousov 	if (bi_m != NULL)
309af582aaeSKonstantin Belousov 		return (bi_m);
31032c01de2SDmitry Chagin 
3115fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
3125fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3135fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
3141438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
3152274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
316ecc2fda8SBjoern A. Zeeb 			continue;
317ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
3185fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
3190fe98320SEd Schouten 		    (bi->compat_3_brand != NULL &&
3203d560b4bSKonstantin Belousov 		    strcmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
3210fe98320SEd Schouten 		    bi->compat_3_brand) == 0))) {
322686d2f31SNathan Whitehorn 			/* Looks good, but give brand a chance to veto */
323d722231bSJohn Baldwin 			if (bi->header_supported == NULL ||
32415a9aedfSKonstantin Belousov 			    bi->header_supported(imgp)) {
32515a9aedfSKonstantin Belousov 				/*
32615a9aedfSKonstantin Belousov 				 * Again, prefer strictly matching
32715a9aedfSKonstantin Belousov 				 * interpreter path.
32815a9aedfSKonstantin Belousov 				 */
3297aab7a80SKonstantin Belousov 				if (interp_name_len == 0 &&
3307aab7a80SKonstantin Belousov 				    bi->interp_path == NULL)
3317aab7a80SKonstantin Belousov 					return (bi);
3327aab7a80SKonstantin Belousov 				if (bi->interp_path != NULL &&
3337aab7a80SKonstantin Belousov 				    strlen(bi->interp_path) + 1 ==
33415a9aedfSKonstantin Belousov 				    interp_name_len && strncmp(interp,
33515a9aedfSKonstantin Belousov 				    bi->interp_path, interp_name_len) == 0)
3365fe3ed62SJake Burkholder 					return (bi);
33715a9aedfSKonstantin Belousov 				if (bi_m == NULL)
33815a9aedfSKonstantin Belousov 					bi_m = bi;
3395fe3ed62SJake Burkholder 			}
340686d2f31SNathan Whitehorn 		}
34115a9aedfSKonstantin Belousov 	}
34215a9aedfSKonstantin Belousov 	if (bi_m != NULL)
34315a9aedfSKonstantin Belousov 		return (bi_m);
3445fe3ed62SJake Burkholder 
345817dc004SWarner Losh 	/* No known brand, see if the header is recognized by any brand */
346817dc004SWarner Losh 	for (i = 0; i < MAX_BRANDS; i++) {
347817dc004SWarner Losh 		bi = elf_brand_list[i];
348817dc004SWarner Losh 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
349817dc004SWarner Losh 		    bi->header_supported == NULL)
350817dc004SWarner Losh 			continue;
351817dc004SWarner Losh 		if (hdr->e_machine == bi->machine) {
352817dc004SWarner Losh 			ret = bi->header_supported(imgp);
353817dc004SWarner Losh 			if (ret)
354817dc004SWarner Losh 				return (bi);
355817dc004SWarner Losh 		}
356817dc004SWarner Losh 	}
357817dc004SWarner Losh 
3585fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
3595fe3ed62SJake Burkholder 	if (interp != NULL) {
3605fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
3615fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
3622274ab3dSKonstantin Belousov 			if (bi == NULL || (bi->flags &
3632274ab3dSKonstantin Belousov 			    (BI_BRAND_NOTE_MANDATORY | BI_BRAND_ONLY_STATIC))
3642274ab3dSKonstantin Belousov 			    != 0)
365ecc2fda8SBjoern A. Zeeb 				continue;
366ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
3673aeacc55SKonstantin Belousov 			    bi->interp_path != NULL &&
368d1ae5c83SKonstantin Belousov 			    /* ELF image p_filesz includes terminating zero */
369d1ae5c83SKonstantin Belousov 			    strlen(bi->interp_path) + 1 == interp_name_len &&
370d1ae5c83SKonstantin Belousov 			    strncmp(interp, bi->interp_path, interp_name_len)
371d722231bSJohn Baldwin 			    == 0 && (bi->header_supported == NULL ||
372d722231bSJohn Baldwin 			    bi->header_supported(imgp)))
3735fe3ed62SJake Burkholder 				return (bi);
3745fe3ed62SJake Burkholder 		}
3755fe3ed62SJake Burkholder 	}
3765fe3ed62SJake Burkholder 
3775fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
3785fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3795fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
3801438fe3cSKonstantin Belousov 		if (bi == NULL || (bi->flags & BI_BRAND_NOTE_MANDATORY) != 0 ||
3812274ab3dSKonstantin Belousov 		    (interp != NULL && (bi->flags & BI_BRAND_ONLY_STATIC) != 0))
382ecc2fda8SBjoern A. Zeeb 			continue;
383ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
384d722231bSJohn Baldwin 		    __elfN(fallback_brand) == bi->brand &&
385d722231bSJohn Baldwin 		    (bi->header_supported == NULL ||
386d722231bSJohn Baldwin 		    bi->header_supported(imgp)))
3875fe3ed62SJake Burkholder 			return (bi);
3885fe3ed62SJake Burkholder 	}
3895fe3ed62SJake Burkholder 	return (NULL);
3905fe3ed62SJake Burkholder }
3915fe3ed62SJake Burkholder 
392e1743d02SSøren Schmidt static int
3933ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
394e1743d02SSøren Schmidt {
395d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
3963ebc1248SPeter Wemm 	int i;
3973ebc1248SPeter Wemm 
39852c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
39952c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
40052c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
4013dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
4023dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
4033dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
404a7cddfedSJake Burkholder 		return (ENOEXEC);
405e1743d02SSøren Schmidt 
4063ebc1248SPeter Wemm 	/*
4073ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
4083ebc1248SPeter Wemm 	 */
4093ebc1248SPeter Wemm 
4103ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
411d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
412d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
4133ebc1248SPeter Wemm 			break;
4143ebc1248SPeter Wemm 	}
4153ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
416a7cddfedSJake Burkholder 		return (ENOEXEC);
417e1743d02SSøren Schmidt 
418a7cddfedSJake Burkholder 	return (0);
419e1743d02SSøren Schmidt }
420e1743d02SSøren Schmidt 
421e1743d02SSøren Schmidt static int
4223ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
423ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
4243ebc1248SPeter Wemm {
425da61b9a6SAlan Cox 	struct sf_buf *sf;
426da61b9a6SAlan Cox 	int error;
4273ebc1248SPeter Wemm 	vm_offset_t off;
4283ebc1248SPeter Wemm 
4293ebc1248SPeter Wemm 	/*
4303ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
4313ebc1248SPeter Wemm 	 */
432aaadc41fSKonstantin Belousov 	vm_map_fixed(map, NULL, 0, trunc_page(start), round_page(end) -
433aaadc41fSKonstantin Belousov 	    trunc_page(start), VM_PROT_ALL, VM_PROT_ALL, MAP_CHECK_EXCL);
4343ebc1248SPeter Wemm 
4353ebc1248SPeter Wemm 	/*
4363ebc1248SPeter Wemm 	 * Find the page from the underlying object.
4373ebc1248SPeter Wemm 	 */
43828e8da65SAlan Cox 	if (object != NULL) {
439da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
440da61b9a6SAlan Cox 		if (sf == NULL)
441da61b9a6SAlan Cox 			return (KERN_FAILURE);
4423ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
443da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
444ca0387efSJake Burkholder 		    end - start);
445be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
446fe0a8a39SKonstantin Belousov 		if (error != 0)
447a7cddfedSJake Burkholder 			return (KERN_FAILURE);
4483ebc1248SPeter Wemm 	}
4493ebc1248SPeter Wemm 
450a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
4513ebc1248SPeter Wemm }
4523ebc1248SPeter Wemm 
4533ebc1248SPeter Wemm static int
454e3d8f8feSKonstantin Belousov __elfN(map_insert)(struct image_params *imgp, vm_map_t map, vm_object_t object,
455e3d8f8feSKonstantin Belousov     vm_ooffset_t offset, vm_offset_t start, vm_offset_t end, vm_prot_t prot,
456e3d8f8feSKonstantin Belousov     int cow)
4573ebc1248SPeter Wemm {
458da61b9a6SAlan Cox 	struct sf_buf *sf;
459da61b9a6SAlan Cox 	vm_offset_t off;
460a063facbSMarcel Moolenaar 	vm_size_t sz;
461e3d8f8feSKonstantin Belousov 	int error, locked, rv;
4623ebc1248SPeter Wemm 
4633ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
46481f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
465ff6f03c7SAlan Cox 		    round_page(start), prot);
46628e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
467a7cddfedSJake Burkholder 			return (rv);
4683ebc1248SPeter Wemm 		offset += round_page(start) - start;
4693ebc1248SPeter Wemm 		start = round_page(start);
4703ebc1248SPeter Wemm 	}
4713ebc1248SPeter Wemm 	if (end != round_page(end)) {
47281f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
473ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
47428e8da65SAlan Cox 		if (rv != KERN_SUCCESS)
475a7cddfedSJake Burkholder 			return (rv);
4763ebc1248SPeter Wemm 		end = trunc_page(end);
4773ebc1248SPeter Wemm 	}
478e383e820SAlan Cox 	if (start >= end)
479e383e820SAlan Cox 		return (KERN_SUCCESS);
480e383e820SAlan Cox 	if ((offset & PAGE_MASK) != 0) {
4813ebc1248SPeter Wemm 		/*
482e383e820SAlan Cox 		 * The mapping is not page aligned.  This means that we have
483e383e820SAlan Cox 		 * to copy the data.
4843ebc1248SPeter Wemm 		 */
485aaadc41fSKonstantin Belousov 		rv = vm_map_fixed(map, NULL, 0, start, end - start,
486aaadc41fSKonstantin Belousov 		    prot | VM_PROT_WRITE, VM_PROT_ALL, MAP_CHECK_EXCL);
4875420f76bSKonstantin Belousov 		if (rv != KERN_SUCCESS)
488a7cddfedSJake Burkholder 			return (rv);
489da61b9a6SAlan Cox 		if (object == NULL)
490da61b9a6SAlan Cox 			return (KERN_SUCCESS);
491da61b9a6SAlan Cox 		for (; start < end; start += sz) {
492da61b9a6SAlan Cox 			sf = vm_imgact_map_page(object, offset);
493da61b9a6SAlan Cox 			if (sf == NULL)
494da61b9a6SAlan Cox 				return (KERN_FAILURE);
4953ebc1248SPeter Wemm 			off = offset - trunc_page(offset);
4963ebc1248SPeter Wemm 			sz = end - start;
497da61b9a6SAlan Cox 			if (sz > PAGE_SIZE - off)
498da61b9a6SAlan Cox 				sz = PAGE_SIZE - off;
499da61b9a6SAlan Cox 			error = copyout((caddr_t)sf_buf_kva(sf) + off,
5003ebc1248SPeter Wemm 			    (caddr_t)start, sz);
501be996836SAttilio Rao 			vm_imgact_unmap_page(sf);
5025420f76bSKonstantin Belousov 			if (error != 0)
503a7cddfedSJake Burkholder 				return (KERN_FAILURE);
504da61b9a6SAlan Cox 			offset += sz;
5053ebc1248SPeter Wemm 		}
5063ebc1248SPeter Wemm 	} else {
507e5e6093bSAlan Cox 		vm_object_reference(object);
508e383e820SAlan Cox 		rv = vm_map_fixed(map, object, offset, start, end - start,
509e383e820SAlan Cox 		    prot, VM_PROT_ALL, cow | MAP_CHECK_EXCL);
510e3d8f8feSKonstantin Belousov 		if (rv != KERN_SUCCESS) {
511e3d8f8feSKonstantin Belousov 			locked = VOP_ISLOCKED(imgp->vp);
512e3d8f8feSKonstantin Belousov 			VOP_UNLOCK(imgp->vp, 0);
513e5e6093bSAlan Cox 			vm_object_deallocate(object);
514e3d8f8feSKonstantin Belousov 			vn_lock(imgp->vp, locked | LK_RETRY);
515a7cddfedSJake Burkholder 			return (rv);
5163ebc1248SPeter Wemm 		}
5173ebc1248SPeter Wemm 	}
518e383e820SAlan Cox 	return (KERN_SUCCESS);
519e383e820SAlan Cox }
5203ebc1248SPeter Wemm 
5213ebc1248SPeter Wemm static int
5220bbee4cdSKonstantin Belousov __elfN(load_section)(struct image_params *imgp, vm_ooffset_t offset,
5233ebc1248SPeter Wemm     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
5243ebc1248SPeter Wemm     size_t pagesize)
525e1743d02SSøren Schmidt {
526da61b9a6SAlan Cox 	struct sf_buf *sf;
527e1743d02SSøren Schmidt 	size_t map_len;
528292177e6SAlan Cox 	vm_map_t map;
529292177e6SAlan Cox 	vm_object_t object;
530973d67c4SKonstantin Belousov 	vm_offset_t off, map_addr;
531fa7dd9c5SMatthew Dillon 	int error, rv, cow;
532e1743d02SSøren Schmidt 	size_t copy_len;
5330bbee4cdSKonstantin Belousov 	vm_ooffset_t file_addr;
53452c24af7SPeter Wemm 
53525ead034SBrian Feldman 	/*
53625ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
53725ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
53825ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
53925ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
54025ead034SBrian Feldman 	 *
54125ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
54225ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
54325ead034SBrian Feldman 	 */
5449bcf2f2dSKonstantin Belousov 	if ((filsz != 0 && (off_t)filsz + offset > imgp->attr->va_size) ||
5459bcf2f2dSKonstantin Belousov 	    filsz > memsz) {
54625ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
54725ead034SBrian Feldman 		return (ENOEXEC);
54825ead034SBrian Feldman 	}
54925ead034SBrian Feldman 
550292177e6SAlan Cox 	object = imgp->object;
551292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
5523ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
5533ebc1248SPeter Wemm 	file_addr = trunc_page_ps(offset, pagesize);
554e1743d02SSøren Schmidt 
555e1743d02SSøren Schmidt 	/*
55652c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
55752c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
55852c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
55928e8da65SAlan Cox 	 * choose the second.
56052c24af7SPeter Wemm 	 */
5619bcf2f2dSKonstantin Belousov 	if (filsz == 0)
5629bcf2f2dSKonstantin Belousov 		map_len = 0;
5639bcf2f2dSKonstantin Belousov 	else if (memsz > filsz)
5643ebc1248SPeter Wemm 		map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
56552c24af7SPeter Wemm 	else
5663ebc1248SPeter Wemm 		map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
56752c24af7SPeter Wemm 
56852c24af7SPeter Wemm 	if (map_len != 0) {
569fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
570fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
571fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
572fa7dd9c5SMatthew Dillon 
573e3d8f8feSKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map,
57452c24af7SPeter Wemm 				      object,
57552c24af7SPeter Wemm 				      file_addr,	/* file offset */
57652c24af7SPeter Wemm 				      map_addr,		/* virtual start */
57752c24af7SPeter Wemm 				      map_addr + map_len,/* virtual end */
57852c24af7SPeter Wemm 				      prot,
579fa7dd9c5SMatthew Dillon 				      cow);
580e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
581a7cddfedSJake Burkholder 			return (EINVAL);
58252c24af7SPeter Wemm 
58352c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
584973d67c4SKonstantin Belousov 		if (memsz == filsz)
585a7cddfedSJake Burkholder 			return (0);
58652c24af7SPeter Wemm 	}
58752c24af7SPeter Wemm 
58852c24af7SPeter Wemm 
58952c24af7SPeter Wemm 	/*
59052c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
59152c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
59252c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
59352c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
594e1743d02SSøren Schmidt 	 */
5959bcf2f2dSKonstantin Belousov 	copy_len = filsz == 0 ? 0 : (offset + filsz) - trunc_page_ps(offset +
5969bcf2f2dSKonstantin Belousov 	    filsz, pagesize);
5973ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
598ca0387efSJake Burkholder 	map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
599ca0387efSJake Burkholder 	    map_addr;
600e1743d02SSøren Schmidt 
60152c24af7SPeter Wemm 	/* This had damn well better be true! */
6028191d577SPeter Wemm 	if (map_len != 0) {
603e3d8f8feSKonstantin Belousov 		rv = __elfN(map_insert)(imgp, map, NULL, 0, map_addr,
604c547cbb4SAlan Cox 		    map_addr + map_len, prot, 0);
605973d67c4SKonstantin Belousov 		if (rv != KERN_SUCCESS)
606a7cddfedSJake Burkholder 			return (EINVAL);
6078191d577SPeter Wemm 	}
608e1743d02SSøren Schmidt 
60952c24af7SPeter Wemm 	if (copy_len != 0) {
610da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
611da61b9a6SAlan Cox 		if (sf == NULL)
612da61b9a6SAlan Cox 			return (EIO);
613e1743d02SSøren Schmidt 
61452c24af7SPeter Wemm 		/* send the page fragment to user space */
61581f223caSJake Burkholder 		off = trunc_page_ps(offset + filsz, pagesize) -
61681f223caSJake Burkholder 		    trunc_page(offset + filsz);
617da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off,
618da61b9a6SAlan Cox 		    (caddr_t)map_addr, copy_len);
619be996836SAttilio Rao 		vm_imgact_unmap_page(sf);
620973d67c4SKonstantin Belousov 		if (error != 0)
62152c24af7SPeter Wemm 			return (error);
62252c24af7SPeter Wemm 	}
623e1743d02SSøren Schmidt 
624e1743d02SSøren Schmidt 	/*
625c547cbb4SAlan Cox 	 * Remove write access to the page if it was only granted by map_insert
626c547cbb4SAlan Cox 	 * to allow copyout.
627e1743d02SSøren Schmidt 	 */
628c547cbb4SAlan Cox 	if ((prot & VM_PROT_WRITE) == 0)
629292177e6SAlan Cox 		vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
630292177e6SAlan Cox 		    map_len), prot, FALSE);
6318191d577SPeter Wemm 
632ff6f03c7SAlan Cox 	return (0);
633e1743d02SSøren Schmidt }
634e1743d02SSøren Schmidt 
635c33fe779SJohn Polstra /*
636c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
637c33fe779SJohn Polstra  * or an executable.
638c33fe779SJohn Polstra  *
639c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
640c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
641c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
642c33fe779SJohn Polstra  * where the file was actually loaded.
643c33fe779SJohn Polstra  *
644c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
645c33fe779SJohn Polstra  * the entry point for the loaded file.
646c33fe779SJohn Polstra  */
647e1743d02SSøren Schmidt static int
6483ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
6493ebc1248SPeter Wemm 	u_long *entry, size_t pagesize)
650e1743d02SSøren Schmidt {
651911c2be0SMark Peek 	struct {
652911c2be0SMark Peek 		struct nameidata nd;
653911c2be0SMark Peek 		struct vattr attr;
654911c2be0SMark Peek 		struct image_params image_params;
655911c2be0SMark Peek 	} *tempdata;
656d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
657d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
658911c2be0SMark Peek 	struct nameidata *nd;
659911c2be0SMark Peek 	struct vattr *attr;
660911c2be0SMark Peek 	struct image_params *imgp;
66152c24af7SPeter Wemm 	vm_prot_t prot;
662c33fe779SJohn Polstra 	u_long rbase;
663c33fe779SJohn Polstra 	u_long base_addr = 0;
6645050aa86SKonstantin Belousov 	int error, i, numsegs;
665e1743d02SSøren Schmidt 
66612bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
66712bc222eSJonathan Anderson 	/*
66812bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
66912bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
67012bc222eSJonathan Anderson 	 */
67112bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
67212bc222eSJonathan Anderson 		return (ECAPMODE);
67312bc222eSJonathan Anderson #endif
67412bc222eSJonathan Anderson 
675a163d034SWarner Losh 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
676911c2be0SMark Peek 	nd = &tempdata->nd;
677911c2be0SMark Peek 	attr = &tempdata->attr;
678911c2be0SMark Peek 	imgp = &tempdata->image_params;
679911c2be0SMark Peek 
680c8a79999SPeter Wemm 	/*
681c8a79999SPeter Wemm 	 * Initialize part of the common data
682c8a79999SPeter Wemm 	 */
683c8a79999SPeter Wemm 	imgp->proc = p;
684911c2be0SMark Peek 	imgp->attr = attr;
685c8a79999SPeter Wemm 	imgp->firstpage = NULL;
68659c8bc40SAlan Cox 	imgp->image_header = NULL;
6870b2ed1aeSJeff Roberson 	imgp->object = NULL;
6886d7bdc8dSRobert Watson 	imgp->execlabel = NULL;
689c8a79999SPeter Wemm 
6905050aa86SKonstantin Belousov 	NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
691911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
692911c2be0SMark Peek 		nd->ni_vp = NULL;
693e1743d02SSøren Schmidt 		goto fail;
694e1743d02SSøren Schmidt 	}
695911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
696911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
697c8a79999SPeter Wemm 
698e1743d02SSøren Schmidt 	/*
699e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
700e1743d02SSøren Schmidt 	 */
701c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
702373d1a3fSAlan Cox 	if (error)
703c8a79999SPeter Wemm 		goto fail;
704e1743d02SSøren Schmidt 
705c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
706373d1a3fSAlan Cox 	if (error)
707373d1a3fSAlan Cox 		goto fail;
708373d1a3fSAlan Cox 
70925ead034SBrian Feldman 	/*
71025ead034SBrian Feldman 	 * Also make certain that the interpreter stays the same, so set
711e6e370a7SJeff Roberson 	 * its VV_TEXT flag, too.
71225ead034SBrian Feldman 	 */
713877d24acSKonstantin Belousov 	VOP_SET_TEXT(nd->ni_vp);
714e6e370a7SJeff Roberson 
7158516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
716e1743d02SSøren Schmidt 
717d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
7183ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
719e1743d02SSøren Schmidt 		goto fail;
720c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
721c33fe779SJohn Polstra 		rbase = *addr;
722c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
723c33fe779SJohn Polstra 		rbase = 0;
724c33fe779SJohn Polstra 	else {
725c33fe779SJohn Polstra 		error = ENOEXEC;
726c33fe779SJohn Polstra 		goto fail;
727c33fe779SJohn Polstra 	}
728e1743d02SSøren Schmidt 
729c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
73052c24af7SPeter Wemm 	if ((hdr->e_phoff > PAGE_SIZE) ||
731d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
732c8a79999SPeter Wemm 		error = ENOEXEC;
733e1743d02SSøren Schmidt 		goto fail;
734c8a79999SPeter Wemm 	}
735c8a79999SPeter Wemm 
736d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
73793d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
73893d1c728SKonstantin Belousov 		error = ENOEXEC;
73993d1c728SKonstantin Belousov 		goto fail;
74093d1c728SKonstantin Belousov 	}
741e1743d02SSøren Schmidt 
742c33fe779SJohn Polstra 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
7435b33842aSKonstantin Belousov 		if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
7445b33842aSKonstantin Belousov 			/* Loadable segment */
745ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
746292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
74781f223caSJake Burkholder 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
748292177e6SAlan Cox 			    phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
749292177e6SAlan Cox 			if (error != 0)
750e1743d02SSøren Schmidt 				goto fail;
751e1743d02SSøren Schmidt 			/*
752c33fe779SJohn Polstra 			 * Establish the base address if this is the
753c33fe779SJohn Polstra 			 * first segment.
754e1743d02SSøren Schmidt 			 */
755c33fe779SJohn Polstra 			if (numsegs == 0)
756ca0387efSJake Burkholder   				base_addr = trunc_page(phdr[i].p_vaddr +
757ca0387efSJake Burkholder 				    rbase);
758c33fe779SJohn Polstra 			numsegs++;
759e1743d02SSøren Schmidt 		}
760e1743d02SSøren Schmidt 	}
761c33fe779SJohn Polstra 	*addr = base_addr;
762c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
763e1743d02SSøren Schmidt 
764e1743d02SSøren Schmidt fail:
765c8a79999SPeter Wemm 	if (imgp->firstpage)
766c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
7670b2ed1aeSJeff Roberson 
768911c2be0SMark Peek 	if (nd->ni_vp)
769373d1a3fSAlan Cox 		vput(nd->ni_vp);
770911c2be0SMark Peek 
771911c2be0SMark Peek 	free(tempdata, M_TEMP);
772e1743d02SSøren Schmidt 
773a7cddfedSJake Burkholder 	return (error);
774e1743d02SSøren Schmidt }
775e1743d02SSøren Schmidt 
776303b270bSEivind Eklund static int
7773ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
778e1743d02SSøren Schmidt {
7796c775eb6SKonstantin Belousov 	struct thread *td;
7806c775eb6SKonstantin Belousov 	const Elf_Ehdr *hdr;
78132c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
782e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
7835856e12eSJohn Dyson 	struct vmspace *vmspace;
7846c775eb6SKonstantin Belousov 	const char *err_str, *newinterp;
7856c775eb6SKonstantin Belousov 	char *interp, *interp_buf, *path;
786d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
7875fe3ed62SJake Burkholder 	struct sysentvec *sv;
7886c775eb6SKonstantin Belousov 	vm_prot_t prot;
7896c775eb6SKonstantin Belousov 	u_long text_size, data_size, total_size, text_addr, data_addr;
7906c775eb6SKonstantin Belousov 	u_long seg_size, seg_addr, addr, baddr, et_dyn_addr, entry, proghdr;
791cefb93f2SKonstantin Belousov 	uint32_t fctl0;
7926c775eb6SKonstantin Belousov 	int32_t osrel;
7936c775eb6SKonstantin Belousov 	int error, i, n, interp_name_len, have_interp;
7946c775eb6SKonstantin Belousov 
7956c775eb6SKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
796e1743d02SSøren Schmidt 
797e1743d02SSøren Schmidt 	/*
798e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
799900b28f9SMaxim Sobolev 	 *
800900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
801900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
802e1743d02SSøren Schmidt 	 */
803900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
804900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
805a7cddfedSJake Burkholder 		return (-1);
806e1743d02SSøren Schmidt 
807e1743d02SSøren Schmidt 	/*
808e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
809e1743d02SSøren Schmidt 	 * detected an ELF file.
810e1743d02SSøren Schmidt 	 */
811e1743d02SSøren Schmidt 
812e1743d02SSøren Schmidt 	if ((hdr->e_phoff > PAGE_SIZE) ||
813d19d5bf4STijl Coosemans 	    (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
814c8a79999SPeter Wemm 		/* Only support headers in first page for now */
8156b16d664SEd Maste 		uprintf("Program headers not in the first page\n");
816a7cddfedSJake Burkholder 		return (ENOEXEC);
817e1743d02SSøren Schmidt 	}
81852c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
8196b16d664SEd Maste 	if (!aligned(phdr, Elf_Addr)) {
8206b16d664SEd Maste 		uprintf("Unaligned program headers\n");
82193d1c728SKonstantin Belousov 		return (ENOEXEC);
8226b16d664SEd Maste 	}
8236c775eb6SKonstantin Belousov 
8246c775eb6SKonstantin Belousov 	n = error = 0;
8257564c4adSKonstantin Belousov 	baddr = 0;
8266c775eb6SKonstantin Belousov 	osrel = 0;
827cefb93f2SKonstantin Belousov 	fctl0 = 0;
8286c775eb6SKonstantin Belousov 	text_size = data_size = total_size = text_addr = data_addr = 0;
8296c775eb6SKonstantin Belousov 	entry = proghdr = 0;
8306c775eb6SKonstantin Belousov 	interp_name_len = 0;
8316c775eb6SKonstantin Belousov 	err_str = newinterp = NULL;
8326c775eb6SKonstantin Belousov 	interp = interp_buf = NULL;
8336c775eb6SKonstantin Belousov 	td = curthread;
8346c775eb6SKonstantin Belousov 
8355fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
836291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
837291c06a1SKonstantin Belousov 		case PT_LOAD:
8387564c4adSKonstantin Belousov 			if (n == 0)
8397564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
8407564c4adSKonstantin Belousov 			n++;
841291c06a1SKonstantin Belousov 			break;
842291c06a1SKonstantin Belousov 		case PT_INTERP:
843e5e6093bSAlan Cox 			/* Path to interpreter */
844c9e562b1SGordon Tetlow 			if (phdr[i].p_filesz < 2 ||
845c9e562b1SGordon Tetlow 			    phdr[i].p_filesz > MAXPATHLEN) {
8466b16d664SEd Maste 				uprintf("Invalid PT_INTERP\n");
8476c775eb6SKonstantin Belousov 				error = ENOEXEC;
8486c775eb6SKonstantin Belousov 				goto ret;
8496b16d664SEd Maste 			}
850d3ee0a15SJonathan T. Looney 			if (interp != NULL) {
851d3ee0a15SJonathan T. Looney 				uprintf("Multiple PT_INTERP headers\n");
852d3ee0a15SJonathan T. Looney 				error = ENOEXEC;
853d3ee0a15SJonathan T. Looney 				goto ret;
854d3ee0a15SJonathan T. Looney 			}
855d1ae5c83SKonstantin Belousov 			interp_name_len = phdr[i].p_filesz;
8566c775eb6SKonstantin Belousov 			if (phdr[i].p_offset > PAGE_SIZE ||
8576c775eb6SKonstantin Belousov 			    interp_name_len > PAGE_SIZE - phdr[i].p_offset) {
8586c775eb6SKonstantin Belousov 				VOP_UNLOCK(imgp->vp, 0);
8596c775eb6SKonstantin Belousov 				interp_buf = malloc(interp_name_len + 1, M_TEMP,
8606c775eb6SKonstantin Belousov 				    M_WAITOK);
8616c775eb6SKonstantin Belousov 				vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
8626c775eb6SKonstantin Belousov 				error = vn_rdwr(UIO_READ, imgp->vp, interp_buf,
8636c775eb6SKonstantin Belousov 				    interp_name_len, phdr[i].p_offset,
8646c775eb6SKonstantin Belousov 				    UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
8656c775eb6SKonstantin Belousov 				    NOCRED, NULL, td);
8666c775eb6SKonstantin Belousov 				if (error != 0) {
86753e20b27SKonstantin Belousov 					uprintf("i/o error PT_INTERP %d\n",
86853e20b27SKonstantin Belousov 					    error);
8696c775eb6SKonstantin Belousov 					goto ret;
8706c775eb6SKonstantin Belousov 				}
8716c775eb6SKonstantin Belousov 				interp_buf[interp_name_len] = '\0';
8726c775eb6SKonstantin Belousov 				interp = interp_buf;
8736c775eb6SKonstantin Belousov 			} else {
8746c775eb6SKonstantin Belousov 				interp = __DECONST(char *, imgp->image_header) +
8756c775eb6SKonstantin Belousov 				    phdr[i].p_offset;
876c9e562b1SGordon Tetlow 				if (interp[interp_name_len - 1] != '\0') {
877c9e562b1SGordon Tetlow 					uprintf("Invalid PT_INTERP\n");
878c9e562b1SGordon Tetlow 					error = ENOEXEC;
879c9e562b1SGordon Tetlow 					goto ret;
880c9e562b1SGordon Tetlow 				}
8816c775eb6SKonstantin Belousov 			}
882291c06a1SKonstantin Belousov 			break;
883291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
884291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
885291c06a1SKonstantin Belousov 				imgp->stack_prot =
886291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
887316b3843SKonstantin Belousov 			imgp->stack_sz = phdr[i].p_memsz;
888291c06a1SKonstantin Belousov 			break;
8893ebc1248SPeter Wemm 		}
8903ebc1248SPeter Wemm 	}
8913ebc1248SPeter Wemm 
892d1ae5c83SKonstantin Belousov 	brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
893cefb93f2SKonstantin Belousov 	    &osrel, &fctl0);
8945fe3ed62SJake Burkholder 	if (brand_info == NULL) {
8955fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
8965fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
8976c775eb6SKonstantin Belousov 		error = ENOEXEC;
8986c775eb6SKonstantin Belousov 		goto ret;
8993ebc1248SPeter Wemm 	}
90077ebe276SEd Maste 	et_dyn_addr = 0;
901ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
9026b16d664SEd Maste 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
9036b16d664SEd Maste 			uprintf("Cannot execute shared object\n");
9046c775eb6SKonstantin Belousov 			error = ENOEXEC;
9056c775eb6SKonstantin Belousov 			goto ret;
9066b16d664SEd Maste 		}
9077564c4adSKonstantin Belousov 		/*
9087564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
9097564c4adSKonstantin Belousov 		 * non-zero for some reason.
9107564c4adSKonstantin Belousov 		 */
9117564c4adSKonstantin Belousov 		if (baddr == 0)
912ab02d85fSKonstantin Belousov 			et_dyn_addr = ET_DYN_LOAD_ADDR;
91377ebe276SEd Maste 	}
9145fe3ed62SJake Burkholder 	sv = brand_info->sysvec;
9159b68618dSPeter Wemm 	if (interp != NULL && brand_info->interp_newpath != NULL)
9164113f8d7SPeter Wemm 		newinterp = brand_info->interp_newpath;
9173ebc1248SPeter Wemm 
91860bb3943SAlan Cox 	/*
91960bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
92060bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
92160bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
92260bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
92360bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
92460bb3943SAlan Cox 	 * v_usecount may become zero.
9251dfab802SAlan Cox 	 *
9261dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
9271dfab802SAlan Cox 	 * the vnode is unlocked.
92860bb3943SAlan Cox 	 */
92922db15c0SAttilio Rao 	VOP_UNLOCK(imgp->vp, 0);
93060bb3943SAlan Cox 
93189b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
93219059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
933e1743d02SSøren Schmidt 
934cb05b60aSAttilio Rao 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
9356c775eb6SKonstantin Belousov 	if (error != 0)
9366c775eb6SKonstantin Belousov 		goto ret;
93760bb3943SAlan Cox 
938e1743d02SSøren Schmidt 	for (i = 0; i < hdr->e_phnum; i++) {
939e1743d02SSøren Schmidt 		switch (phdr[i].p_type) {
940e1743d02SSøren Schmidt 		case PT_LOAD:	/* Loadable segment */
9415b33842aSKonstantin Belousov 			if (phdr[i].p_memsz == 0)
9425b33842aSKonstantin Belousov 				break;
943ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
944292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
945ab02d85fSKonstantin Belousov 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
94681f223caSJake Burkholder 			    phdr[i].p_memsz, phdr[i].p_filesz, prot,
947292177e6SAlan Cox 			    sv->sv_pagesize);
948292177e6SAlan Cox 			if (error != 0)
9496c775eb6SKonstantin Belousov 				goto ret;
950e1743d02SSøren Schmidt 
951cfaf7e60SDoug Rabson 			/*
952cfaf7e60SDoug Rabson 			 * If this segment contains the program headers,
953cfaf7e60SDoug Rabson 			 * remember their virtual address for the AT_PHDR
954cfaf7e60SDoug Rabson 			 * aux entry. Static binaries don't usually include
955cfaf7e60SDoug Rabson 			 * a PT_PHDR entry.
956cfaf7e60SDoug Rabson 			 */
957cfaf7e60SDoug Rabson 			if (phdr[i].p_offset == 0 &&
958cfaf7e60SDoug Rabson 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
959cfaf7e60SDoug Rabson 				<= phdr[i].p_filesz)
960ab02d85fSKonstantin Belousov 				proghdr = phdr[i].p_vaddr + hdr->e_phoff +
961ab02d85fSKonstantin Belousov 				    et_dyn_addr;
962cfaf7e60SDoug Rabson 
963ab02d85fSKonstantin Belousov 			seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
964cac45152SMatthew Dillon 			seg_size = round_page(phdr[i].p_memsz +
965ab02d85fSKonstantin Belousov 			    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
966cac45152SMatthew Dillon 
967e1743d02SSøren Schmidt 			/*
968920acedbSNathan Whitehorn 			 * Make the largest executable segment the official
969920acedbSNathan Whitehorn 			 * text segment and all others data.
97021c2d047SMatthew Dillon 			 *
97121c2d047SMatthew Dillon 			 * Note that obreak() assumes that data_addr +
97221c2d047SMatthew Dillon 			 * data_size == end of data load area, and the ELF
97321c2d047SMatthew Dillon 			 * file format expects segments to be sorted by
97421c2d047SMatthew Dillon 			 * address.  If multiple data segments exist, the
97521c2d047SMatthew Dillon 			 * last one will be used.
976e1743d02SSøren Schmidt 			 */
977920acedbSNathan Whitehorn 
978920acedbSNathan Whitehorn 			if (phdr[i].p_flags & PF_X && text_size < seg_size) {
9799782ecbaSPeter Wemm 				text_size = seg_size;
9809782ecbaSPeter Wemm 				text_addr = seg_addr;
9819782ecbaSPeter Wemm 			} else {
98221c2d047SMatthew Dillon 				data_size = seg_size;
983cac45152SMatthew Dillon 				data_addr = seg_addr;
984cac45152SMatthew Dillon 			}
98521c2d047SMatthew Dillon 			total_size += seg_size;
98696725dd0SAlexander Kabaev 			break;
98796725dd0SAlexander Kabaev 		case PT_PHDR: 	/* Program header table info */
988ab02d85fSKonstantin Belousov 			proghdr = phdr[i].p_vaddr + et_dyn_addr;
98996725dd0SAlexander Kabaev 			break;
99096725dd0SAlexander Kabaev 		default:
99196725dd0SAlexander Kabaev 			break;
99296725dd0SAlexander Kabaev 		}
99396725dd0SAlexander Kabaev 	}
99496725dd0SAlexander Kabaev 
99596725dd0SAlexander Kabaev 	if (data_addr == 0 && data_size == 0) {
99696725dd0SAlexander Kabaev 		data_addr = text_addr;
99796725dd0SAlexander Kabaev 		data_size = text_size;
99896725dd0SAlexander Kabaev 	}
999cac45152SMatthew Dillon 
1000920acedbSNathan Whitehorn 	entry = (u_long)hdr->e_entry + et_dyn_addr;
1001920acedbSNathan Whitehorn 
1002cac45152SMatthew Dillon 	/*
1003cac45152SMatthew Dillon 	 * Check limits.  It should be safe to check the
100496725dd0SAlexander Kabaev 	 * limits after loading the segments since we do
100596725dd0SAlexander Kabaev 	 * not actually fault in all the segments pages.
1006cac45152SMatthew Dillon 	 */
100791d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
1008f6f6d240SMateusz Guzik 	if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
10096b16d664SEd Maste 		err_str = "Data segment size exceeds process limit";
10106b16d664SEd Maste 	else if (text_size > maxtsiz)
10116b16d664SEd Maste 		err_str = "Text segment size exceeds system limit";
1012f6f6d240SMateusz Guzik 	else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
10136b16d664SEd Maste 		err_str = "Total segment size exceeds process limit";
10146b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
10156b16d664SEd Maste 		err_str = "Data segment size exceeds resource limit";
10166b16d664SEd Maste 	else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
10176b16d664SEd Maste 		err_str = "Total segment size exceeds resource limit";
10186b16d664SEd Maste 	if (err_str != NULL) {
101991d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
10206b16d664SEd Maste 		uprintf("%s\n", err_str);
10216c775eb6SKonstantin Belousov 		error = ENOMEM;
10226c775eb6SKonstantin Belousov 		goto ret;
1023cac45152SMatthew Dillon 	}
1024e1743d02SSøren Schmidt 
1025292177e6SAlan Cox 	vmspace = imgp->proc->p_vmspace;
1026e1743d02SSøren Schmidt 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
10277cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
1028e1743d02SSøren Schmidt 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
10297cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
1030e1743d02SSøren Schmidt 
1031c460ac3aSPeter Wemm 	/*
1032c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
1033c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
1034c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
1035c460ac3aSPeter Wemm 	 * its maximum allowed size.
1036c460ac3aSPeter Wemm 	 */
10376c775eb6SKonstantin Belousov 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(td,
1038292177e6SAlan Cox 	    RLIMIT_DATA));
103991d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
1040e1743d02SSøren Schmidt 
1041ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
1042ea5a2b2eSSøren Schmidt 
104360bb3943SAlan Cox 	if (interp != NULL) {
10446c775eb6SKonstantin Belousov 		have_interp = FALSE;
104522db15c0SAttilio Rao 		VOP_UNLOCK(imgp->vp, 0);
104660bb3943SAlan Cox 		if (brand_info->emul_path != NULL &&
10479b68618dSPeter Wemm 		    brand_info->emul_path[0] != '\0') {
1048a163d034SWarner Losh 			path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
104960bb3943SAlan Cox 			snprintf(path, MAXPATHLEN, "%s%s",
105060bb3943SAlan Cox 			    brand_info->emul_path, interp);
10519b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, path, &addr,
10529b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
1053911c2be0SMark Peek 			free(path, M_TEMP);
10549b68618dSPeter Wemm 			if (error == 0)
10554113f8d7SPeter Wemm 				have_interp = TRUE;
10569b68618dSPeter Wemm 		}
105718995077SKonstantin Belousov 		if (!have_interp && newinterp != NULL &&
105818995077SKonstantin Belousov 		    (brand_info->interp_path == NULL ||
105918995077SKonstantin Belousov 		    strcmp(interp, brand_info->interp_path) == 0)) {
10604113f8d7SPeter Wemm 			error = __elfN(load_file)(imgp->proc, newinterp, &addr,
10614113f8d7SPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
1062387ad998SKonstantin Belousov 			if (error == 0)
10634113f8d7SPeter Wemm 				have_interp = TRUE;
10644113f8d7SPeter Wemm 		}
10654113f8d7SPeter Wemm 		if (!have_interp) {
10669b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, interp, &addr,
10679b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
106860bb3943SAlan Cox 		}
1069cb05b60aSAttilio Rao 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
10709b68618dSPeter Wemm 		if (error != 0) {
1071d943fa35SKonstantin Belousov 			uprintf("ELF interpreter %s not found, error %d\n",
1072d943fa35SKonstantin Belousov 			    interp, error);
10736c775eb6SKonstantin Belousov 			goto ret;
1074e1743d02SSøren Schmidt 		}
107595c807cfSRobert Watson 	} else
10767564c4adSKonstantin Belousov 		addr = et_dyn_addr;
1077ea5a2b2eSSøren Schmidt 
1078e1743d02SSøren Schmidt 	/*
1079e1743d02SSøren Schmidt 	 * Construct auxargs table (used by the fixup routine)
1080e1743d02SSøren Schmidt 	 */
1081a163d034SWarner Losh 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
1082e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
1083e1743d02SSøren Schmidt 	elf_auxargs->phdr = proghdr;
1084e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
1085e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
1086e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
1087e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
1088e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
1089e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
1090d36eec69SWarner Losh 	elf_auxargs->hdr_eflags = hdr->e_flags;
1091e1743d02SSøren Schmidt 
1092e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
1093e1743d02SSøren Schmidt 	imgp->interpreted = 0;
1094a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
109532c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
1096cefb93f2SKonstantin Belousov 	imgp->proc->p_fctl0 = fctl0;
1097885f13dcSJohn Baldwin 	imgp->proc->p_elf_machine = hdr->e_machine;
1098885f13dcSJohn Baldwin 	imgp->proc->p_elf_flags = hdr->e_flags;
1099f231de47SKonstantin Belousov 
11006c775eb6SKonstantin Belousov ret:
11016c775eb6SKonstantin Belousov 	free(interp_buf, M_TEMP);
1102a7cddfedSJake Burkholder 	return (error);
1103e1743d02SSøren Schmidt }
1104e1743d02SSøren Schmidt 
1105a360a43dSJake Burkholder #define	suword __CONCAT(suword, __ELF_WORD_SIZE)
11063ebc1248SPeter Wemm 
11073ebc1248SPeter Wemm int
11083ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
1109e1743d02SSøren Schmidt {
1110ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
11115f77b8a8SBrooks Davis 	Elf_Auxinfo *argarray, *pos;
11125f77b8a8SBrooks Davis 	Elf_Addr *base, *auxbase;
11135f77b8a8SBrooks Davis 	int error;
1114e1743d02SSøren Schmidt 
1115a360a43dSJake Burkholder 	base = (Elf_Addr *)*stack_base;
11165f77b8a8SBrooks Davis 	auxbase = base + imgp->args->argc + 1 + imgp->args->envc + 1;
11175f77b8a8SBrooks Davis 	argarray = pos = malloc(AT_COUNT * sizeof(*pos), M_TEMP,
11185f77b8a8SBrooks Davis 	    M_WAITOK | M_ZERO);
1119e1743d02SSøren Schmidt 
112035c2a5a8SWarner Losh 	if (args->execfd != -1)
1121e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1122e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1123e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1124e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1125e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1126e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1127e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1128e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
1129d36eec69SWarner Losh 	AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
11303ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
11313ff06357SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1132b96bd95bSIan Lepore 	AUXARGS_ENTRY(pos, AT_OSRELDATE,
1133b96bd95bSIan Lepore 	    imgp->proc->p_ucred->cr_prison->pr_osreldate);
1134ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1135ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1136ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1137ee235befSKonstantin Belousov 	}
1138ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1139ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1140ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1141ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1142ee235befSKonstantin Belousov 	}
1143aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1144aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1145aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1146aea81038SKonstantin Belousov 	}
114726d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
114826d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
114926d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1150c2f37b92SJohn Baldwin 	if (imgp->sysent->sv_hwcap != NULL)
1151c2f37b92SJohn Baldwin 		AUXARGS_ENTRY(pos, AT_HWCAP, *imgp->sysent->sv_hwcap);
1152904d8c49SMichal Meloun 	if (imgp->sysent->sv_hwcap2 != NULL)
1153904d8c49SMichal Meloun 		AUXARGS_ENTRY(pos, AT_HWCAP2, *imgp->sysent->sv_hwcap2);
1154e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1155e1743d02SSøren Schmidt 
1156e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1157e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1158d8b2f079SBrooks Davis 	KASSERT(pos - argarray <= AT_COUNT, ("Too many auxargs"));
11595f77b8a8SBrooks Davis 
11605f77b8a8SBrooks Davis 	error = copyout(argarray, auxbase, sizeof(*argarray) * AT_COUNT);
11615f77b8a8SBrooks Davis 	free(argarray, M_TEMP);
11625f77b8a8SBrooks Davis 	if (error != 0)
11635f77b8a8SBrooks Davis 		return (error);
1164e1743d02SSøren Schmidt 
11653ebc1248SPeter Wemm 	base--;
11665f77b8a8SBrooks Davis 	if (suword(base, imgp->args->argc) == -1)
11675f77b8a8SBrooks Davis 		return (EFAULT);
11683ebc1248SPeter Wemm 	*stack_base = (register_t *)base;
1169a7cddfedSJake Burkholder 	return (0);
1170e1743d02SSøren Schmidt }
1171e1743d02SSøren Schmidt 
1172e1743d02SSøren Schmidt /*
11738c64af4fSJohn Polstra  * Code for generating ELF core dumps.
11748c64af4fSJohn Polstra  */
11758c64af4fSJohn Polstra 
11764d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
11770ff27d31SJohn Polstra 
11780ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
11790ff27d31SJohn Polstra struct phdr_closure {
11800ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
11810ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
11820ff27d31SJohn Polstra };
11830ff27d31SJohn Polstra 
11840ff27d31SJohn Polstra /* Closure for cb_size_segment(). */
11850ff27d31SJohn Polstra struct sseg_closure {
11860ff27d31SJohn Polstra 	int count;		/* Count of writable segments. */
11870ff27d31SJohn Polstra 	size_t size;		/* Total size of all writable segments. */
11880ff27d31SJohn Polstra };
11890ff27d31SJohn Polstra 
1190bd390213SMikolaj Golub typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1191bd390213SMikolaj Golub 
1192bd390213SMikolaj Golub struct note_info {
1193bd390213SMikolaj Golub 	int		type;		/* Note type. */
1194bd390213SMikolaj Golub 	outfunc_t 	outfunc; 	/* Output function. */
1195bd390213SMikolaj Golub 	void		*outarg;	/* Argument for the output function. */
1196bd390213SMikolaj Golub 	size_t		outsize;	/* Output size. */
1197bd390213SMikolaj Golub 	TAILQ_ENTRY(note_info) link;	/* Link to the next note info. */
1198bd390213SMikolaj Golub };
1199bd390213SMikolaj Golub 
1200bd390213SMikolaj Golub TAILQ_HEAD(note_info_list, note_info);
1201bd390213SMikolaj Golub 
1202aa14e9b7SMark Johnston /* Coredump output parameters. */
1203aa14e9b7SMark Johnston struct coredump_params {
1204aa14e9b7SMark Johnston 	off_t		offset;
1205aa14e9b7SMark Johnston 	struct ucred	*active_cred;
1206aa14e9b7SMark Johnston 	struct ucred	*file_cred;
1207aa14e9b7SMark Johnston 	struct thread	*td;
1208aa14e9b7SMark Johnston 	struct vnode	*vp;
120978f57a9cSMark Johnston 	struct compressor *comp;
1210aa14e9b7SMark Johnston };
1211aa14e9b7SMark Johnston 
121278f57a9cSMark Johnston extern int compress_user_cores;
121378f57a9cSMark Johnston extern int compress_user_cores_level;
121478f57a9cSMark Johnston 
12154d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
12164d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
1217c468ff88SAndriy Gapon static int core_write(struct coredump_params *, const void *, size_t, off_t,
1218aa14e9b7SMark Johnston     enum uio_seg);
12191005d8afSConrad Meyer static void each_dumpable_segment(struct thread *, segment_callback, void *);
1220aa14e9b7SMark Johnston static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1221aa14e9b7SMark Johnston     struct note_info_list *, size_t);
1222bd390213SMikolaj Golub static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1223bd390213SMikolaj Golub     size_t *);
1224bd390213SMikolaj Golub static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1225bd390213SMikolaj Golub static void __elfN(putnote)(struct note_info *, struct sbuf *);
1226bd390213SMikolaj Golub static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1227bd390213SMikolaj Golub static int sbuf_drain_core_output(void *, const char *, int);
1228f1fca82eSMikolaj Golub static int sbuf_drain_count(void *arg, const char *data, int len);
1229bd390213SMikolaj Golub 
1230bd390213SMikolaj Golub static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1231bd390213SMikolaj Golub static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1232bd390213SMikolaj Golub static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1233bd390213SMikolaj Golub static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1234bd390213SMikolaj Golub static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
123586be94fcSTycho Nightingale static void __elfN(note_ptlwpinfo)(void *, struct sbuf *, size_t *);
1236f1fca82eSMikolaj Golub static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1237f1fca82eSMikolaj Golub static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1238f1fca82eSMikolaj Golub static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1239f1fca82eSMikolaj Golub static void note_procstat_files(void *, struct sbuf *, size_t *);
1240f1fca82eSMikolaj Golub static void note_procstat_groups(void *, struct sbuf *, size_t *);
1241f1fca82eSMikolaj Golub static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1242f1fca82eSMikolaj Golub static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1243f1fca82eSMikolaj Golub static void note_procstat_umask(void *, struct sbuf *, size_t *);
1244f1fca82eSMikolaj Golub static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
12458c64af4fSJohn Polstra 
1246aa14e9b7SMark Johnston /*
1247aa14e9b7SMark Johnston  * Write out a core segment to the compression stream.
1248aa14e9b7SMark Johnston  */
1249e7228204SAlfred Perlstein static int
1250aa14e9b7SMark Johnston compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
1251aa14e9b7SMark Johnston {
1252aa14e9b7SMark Johnston 	u_int chunk_len;
1253e7228204SAlfred Perlstein 	int error;
1254aa14e9b7SMark Johnston 
1255aa14e9b7SMark Johnston 	while (len > 0) {
1256aa14e9b7SMark Johnston 		chunk_len = MIN(len, CORE_BUF_SIZE);
1257c468ff88SAndriy Gapon 
1258c468ff88SAndriy Gapon 		/*
1259c468ff88SAndriy Gapon 		 * We can get EFAULT error here.
1260c468ff88SAndriy Gapon 		 * In that case zero out the current chunk of the segment.
1261c468ff88SAndriy Gapon 		 */
1262c468ff88SAndriy Gapon 		error = copyin(base, buf, chunk_len);
1263c468ff88SAndriy Gapon 		if (error != 0)
1264c468ff88SAndriy Gapon 			bzero(buf, chunk_len);
126578f57a9cSMark Johnston 		error = compressor_write(p->comp, buf, chunk_len);
1266aa14e9b7SMark Johnston 		if (error != 0)
1267aa14e9b7SMark Johnston 			break;
1268aa14e9b7SMark Johnston 		base += chunk_len;
1269aa14e9b7SMark Johnston 		len -= chunk_len;
1270e7228204SAlfred Perlstein 	}
1271e7228204SAlfred Perlstein 	return (error);
1272e7228204SAlfred Perlstein }
1273e7228204SAlfred Perlstein 
1274aa14e9b7SMark Johnston static int
127578f57a9cSMark Johnston core_compressed_write(void *base, size_t len, off_t offset, void *arg)
1276aa14e9b7SMark Johnston {
1277aa14e9b7SMark Johnston 
1278aa14e9b7SMark Johnston 	return (core_write((struct coredump_params *)arg, base, len, offset,
1279aa14e9b7SMark Johnston 	    UIO_SYSSPACE));
1280aa14e9b7SMark Johnston }
1281aa14e9b7SMark Johnston 
1282aa14e9b7SMark Johnston static int
1283c468ff88SAndriy Gapon core_write(struct coredump_params *p, const void *base, size_t len,
1284c468ff88SAndriy Gapon     off_t offset, enum uio_seg seg)
1285aa14e9b7SMark Johnston {
1286aa14e9b7SMark Johnston 
1287c468ff88SAndriy Gapon 	return (vn_rdwr_inchunks(UIO_WRITE, p->vp, __DECONST(void *, base),
1288c468ff88SAndriy Gapon 	    len, offset, seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
1289aa14e9b7SMark Johnston 	    p->active_cred, p->file_cred, NULL, p->td));
1290aa14e9b7SMark Johnston }
1291aa14e9b7SMark Johnston 
1292aa14e9b7SMark Johnston static int
1293aa14e9b7SMark Johnston core_output(void *base, size_t len, off_t offset, struct coredump_params *p,
1294aa14e9b7SMark Johnston     void *tmpbuf)
1295aa14e9b7SMark Johnston {
1296c468ff88SAndriy Gapon 	int error;
1297aa14e9b7SMark Johnston 
129878f57a9cSMark Johnston 	if (p->comp != NULL)
1299aa14e9b7SMark Johnston 		return (compress_chunk(p, base, tmpbuf, len));
130078f57a9cSMark Johnston 
1301c468ff88SAndriy Gapon 	/*
1302c468ff88SAndriy Gapon 	 * EFAULT is a non-fatal error that we can get, for example,
1303c468ff88SAndriy Gapon 	 * if the segment is backed by a file but extends beyond its
1304c468ff88SAndriy Gapon 	 * end.
1305c468ff88SAndriy Gapon 	 */
1306c468ff88SAndriy Gapon 	error = core_write(p, base, len, offset, UIO_USERSPACE);
1307c468ff88SAndriy Gapon 	if (error == EFAULT) {
1308c468ff88SAndriy Gapon 		log(LOG_WARNING, "Failed to fully fault in a core file segment "
1309c468ff88SAndriy Gapon 		    "at VA %p with size 0x%zx to be written at offset 0x%jx "
1310c468ff88SAndriy Gapon 		    "for process %s\n", base, len, offset, curproc->p_comm);
1311c468ff88SAndriy Gapon 
1312c468ff88SAndriy Gapon 		/*
1313c468ff88SAndriy Gapon 		 * Write a "real" zero byte at the end of the target region
1314c468ff88SAndriy Gapon 		 * in the case this is the last segment.
1315c468ff88SAndriy Gapon 		 * The intermediate space will be implicitly zero-filled.
1316c468ff88SAndriy Gapon 		 */
1317c468ff88SAndriy Gapon 		error = core_write(p, zero_region, 1, offset + len - 1,
1318c468ff88SAndriy Gapon 		    UIO_SYSSPACE);
1319c468ff88SAndriy Gapon 	}
1320c468ff88SAndriy Gapon 	return (error);
1321aa14e9b7SMark Johnston }
1322bd390213SMikolaj Golub 
1323bd390213SMikolaj Golub /*
1324bd390213SMikolaj Golub  * Drain into a core file.
1325bd390213SMikolaj Golub  */
1326bd390213SMikolaj Golub static int
1327bd390213SMikolaj Golub sbuf_drain_core_output(void *arg, const char *data, int len)
1328bd390213SMikolaj Golub {
1329aa14e9b7SMark Johnston 	struct coredump_params *p;
1330f1fca82eSMikolaj Golub 	int error, locked;
1331bd390213SMikolaj Golub 
1332aa14e9b7SMark Johnston 	p = (struct coredump_params *)arg;
1333f1fca82eSMikolaj Golub 
1334f1fca82eSMikolaj Golub 	/*
1335f1fca82eSMikolaj Golub 	 * Some kern_proc out routines that print to this sbuf may
1336f1fca82eSMikolaj Golub 	 * call us with the process lock held. Draining with the
1337f1fca82eSMikolaj Golub 	 * non-sleepable lock held is unsafe. The lock is needed for
1338f1fca82eSMikolaj Golub 	 * those routines when dumping a live process. In our case we
1339f1fca82eSMikolaj Golub 	 * can safely release the lock before draining and acquire
1340f1fca82eSMikolaj Golub 	 * again after.
1341f1fca82eSMikolaj Golub 	 */
1342f1fca82eSMikolaj Golub 	locked = PROC_LOCKED(p->td->td_proc);
1343f1fca82eSMikolaj Golub 	if (locked)
1344f1fca82eSMikolaj Golub 		PROC_UNLOCK(p->td->td_proc);
134578f57a9cSMark Johnston 	if (p->comp != NULL)
134678f57a9cSMark Johnston 		error = compressor_write(p->comp, __DECONST(char *, data), len);
1347bd390213SMikolaj Golub 	else
1348aa14e9b7SMark Johnston 		error = core_write(p, __DECONST(void *, data), len, p->offset,
1349aa14e9b7SMark Johnston 		    UIO_SYSSPACE);
1350f1fca82eSMikolaj Golub 	if (locked)
1351f1fca82eSMikolaj Golub 		PROC_LOCK(p->td->td_proc);
1352bd390213SMikolaj Golub 	if (error != 0)
1353bd390213SMikolaj Golub 		return (-error);
1354bd390213SMikolaj Golub 	p->offset += len;
1355bd390213SMikolaj Golub 	return (len);
1356bd390213SMikolaj Golub }
1357bd390213SMikolaj Golub 
1358f1fca82eSMikolaj Golub /*
1359f1fca82eSMikolaj Golub  * Drain into a counter.
1360f1fca82eSMikolaj Golub  */
1361f1fca82eSMikolaj Golub static int
1362f1fca82eSMikolaj Golub sbuf_drain_count(void *arg, const char *data __unused, int len)
1363f1fca82eSMikolaj Golub {
1364f1fca82eSMikolaj Golub 	size_t *sizep;
1365f1fca82eSMikolaj Golub 
1366f1fca82eSMikolaj Golub 	sizep = (size_t *)arg;
1367f1fca82eSMikolaj Golub 	*sizep += len;
1368f1fca82eSMikolaj Golub 	return (len);
1369f1fca82eSMikolaj Golub }
1370f1fca82eSMikolaj Golub 
13718c64af4fSJohn Polstra int
1372e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1373fca666a1SJulian Elischer {
1374247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
1375fca666a1SJulian Elischer 	int error = 0;
13760ff27d31SJohn Polstra 	struct sseg_closure seginfo;
1377bd390213SMikolaj Golub 	struct note_info_list notelst;
1378aa14e9b7SMark Johnston 	struct coredump_params params;
1379bd390213SMikolaj Golub 	struct note_info *ninfo;
1380aa14e9b7SMark Johnston 	void *hdr, *tmpbuf;
1381bd390213SMikolaj Golub 	size_t hdrsize, notesz, coresize;
13828c64af4fSJohn Polstra 
1383e7228204SAlfred Perlstein 	hdr = NULL;
138402d131adSMark Johnston 	tmpbuf = NULL;
1385bd390213SMikolaj Golub 	TAILQ_INIT(&notelst);
1386e7228204SAlfred Perlstein 
13870ff27d31SJohn Polstra 	/* Size the program segments. */
13880ff27d31SJohn Polstra 	seginfo.count = 0;
13890ff27d31SJohn Polstra 	seginfo.size = 0;
13901005d8afSConrad Meyer 	each_dumpable_segment(td, cb_size_segment, &seginfo);
13910ff27d31SJohn Polstra 
13920ff27d31SJohn Polstra 	/*
1393bd390213SMikolaj Golub 	 * Collect info about the core file header area.
13940ff27d31SJohn Polstra 	 */
1395bd390213SMikolaj Golub 	hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1396c17b0bd2SConrad Meyer 	if (seginfo.count + 1 >= PN_XNUM)
1397c17b0bd2SConrad Meyer 		hdrsize += sizeof(Elf_Shdr);
1398bd390213SMikolaj Golub 	__elfN(prepare_notes)(td, &notelst, &notesz);
1399bd390213SMikolaj Golub 	coresize = round_page(hdrsize + notesz) + seginfo.size;
14000ff27d31SJohn Polstra 
140102d131adSMark Johnston 	/* Set up core dump parameters. */
140202d131adSMark Johnston 	params.offset = 0;
140302d131adSMark Johnston 	params.active_cred = cred;
140402d131adSMark Johnston 	params.file_cred = NOCRED;
140502d131adSMark Johnston 	params.td = td;
140602d131adSMark Johnston 	params.vp = vp;
140778f57a9cSMark Johnston 	params.comp = NULL;
140802d131adSMark Johnston 
1409afcc55f3SEdward Tomasz Napierala #ifdef RACCT
14104b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
14111ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(td->td_proc);
1412bd390213SMikolaj Golub 		error = racct_add(td->td_proc, RACCT_CORE, coresize);
14131ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(td->td_proc);
14141ba5ad42SEdward Tomasz Napierala 		if (error != 0) {
14151ba5ad42SEdward Tomasz Napierala 			error = EFAULT;
14161ba5ad42SEdward Tomasz Napierala 			goto done;
14171ba5ad42SEdward Tomasz Napierala 		}
14184b5c9cf6SEdward Tomasz Napierala 	}
1419afcc55f3SEdward Tomasz Napierala #endif
1420bd390213SMikolaj Golub 	if (coresize >= limit) {
1421fba6b1afSAlfred Perlstein 		error = EFAULT;
1422fba6b1afSAlfred Perlstein 		goto done;
1423fba6b1afSAlfred Perlstein 	}
14240ff27d31SJohn Polstra 
1425aa14e9b7SMark Johnston 	/* Create a compression stream if necessary. */
142678f57a9cSMark Johnston 	if (compress_user_cores != 0) {
142778f57a9cSMark Johnston 		params.comp = compressor_init(core_compressed_write,
142878f57a9cSMark Johnston 		    compress_user_cores, CORE_BUF_SIZE,
142978f57a9cSMark Johnston 		    compress_user_cores_level, &params);
143078f57a9cSMark Johnston 		if (params.comp == NULL) {
1431aa14e9b7SMark Johnston 			error = EFAULT;
1432aa14e9b7SMark Johnston 			goto done;
1433aa14e9b7SMark Johnston 		}
1434aa14e9b7SMark Johnston 		tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1435aa14e9b7SMark Johnston         }
1436aa14e9b7SMark Johnston 
14370ff27d31SJohn Polstra 	/*
14380ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
1439bd390213SMikolaj Golub 	 * and write it out following the notes.
14400ff27d31SJohn Polstra 	 */
1441a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1442aa14e9b7SMark Johnston 	error = __elfN(corehdr)(&params, seginfo.count, hdr, hdrsize, &notelst,
1443aa14e9b7SMark Johnston 	    notesz);
14440ff27d31SJohn Polstra 
14450ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
14460ff27d31SJohn Polstra 	if (error == 0) {
14470ff27d31SJohn Polstra 		Elf_Phdr *php;
14482b471bc6STim J. Robbins 		off_t offset;
14490ff27d31SJohn Polstra 		int i;
14500ff27d31SJohn Polstra 
14510ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1452bd390213SMikolaj Golub 		offset = round_page(hdrsize + notesz);
14530ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1454aa14e9b7SMark Johnston 			error = core_output((caddr_t)(uintptr_t)php->p_vaddr,
1455aa14e9b7SMark Johnston 			    php->p_filesz, offset, &params, tmpbuf);
14560ff27d31SJohn Polstra 			if (error != 0)
14572b471bc6STim J. Robbins 				break;
14580ff27d31SJohn Polstra 			offset += php->p_filesz;
14590ff27d31SJohn Polstra 			php++;
14600ff27d31SJohn Polstra 		}
146178f57a9cSMark Johnston 		if (error == 0 && params.comp != NULL)
146278f57a9cSMark Johnston 			error = compressor_flush(params.comp);
14630ff27d31SJohn Polstra 	}
1464e7228204SAlfred Perlstein 	if (error) {
1465e7228204SAlfred Perlstein 		log(LOG_WARNING,
1466e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1467e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1468e7228204SAlfred Perlstein 	}
1469e7228204SAlfred Perlstein 
1470e7228204SAlfred Perlstein done:
1471aa14e9b7SMark Johnston 	free(tmpbuf, M_TEMP);
147278f57a9cSMark Johnston 	if (params.comp != NULL)
147378f57a9cSMark Johnston 		compressor_fini(params.comp);
1474bd390213SMikolaj Golub 	while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
1475bd390213SMikolaj Golub 		TAILQ_REMOVE(&notelst, ninfo, link);
1476bd390213SMikolaj Golub 		free(ninfo, M_TEMP);
1477bd390213SMikolaj Golub 	}
1478bd390213SMikolaj Golub 	if (hdr != NULL)
14790ff27d31SJohn Polstra 		free(hdr, M_TEMP);
14800ff27d31SJohn Polstra 
1481a7cddfedSJake Burkholder 	return (error);
14828c64af4fSJohn Polstra }
14838c64af4fSJohn Polstra 
14840ff27d31SJohn Polstra /*
14851005d8afSConrad Meyer  * A callback for each_dumpable_segment() to write out the segment's
14860ff27d31SJohn Polstra  * program header entry.
14870ff27d31SJohn Polstra  */
14880ff27d31SJohn Polstra static void
14895cc6d253SEd Maste cb_put_phdr(vm_map_entry_t entry, void *closure)
14900ff27d31SJohn Polstra {
14910ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
14920ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
14930ff27d31SJohn Polstra 
14940ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
14950ff27d31SJohn Polstra 
14960ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
14970ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
14980ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
14990ff27d31SJohn Polstra 	phdr->p_paddr = 0;
15000ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
15010ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1502ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
15030ff27d31SJohn Polstra 
15040ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
15050ff27d31SJohn Polstra 	phc->phdr++;
15060ff27d31SJohn Polstra }
15070ff27d31SJohn Polstra 
15080ff27d31SJohn Polstra /*
15091005d8afSConrad Meyer  * A callback for each_dumpable_segment() to gather information about
15100ff27d31SJohn Polstra  * the number of segments and their total size.
15110ff27d31SJohn Polstra  */
15120ff27d31SJohn Polstra static void
1513f3325003SConrad Meyer cb_size_segment(vm_map_entry_t entry, void *closure)
15140ff27d31SJohn Polstra {
15150ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
15160ff27d31SJohn Polstra 
15170ff27d31SJohn Polstra 	ssc->count++;
15180ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
15190ff27d31SJohn Polstra }
15200ff27d31SJohn Polstra 
15210ff27d31SJohn Polstra /*
15220ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
15230ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
15240ff27d31SJohn Polstra  * caller-supplied data.
15250ff27d31SJohn Polstra  */
15260ff27d31SJohn Polstra static void
15271005d8afSConrad Meyer each_dumpable_segment(struct thread *td, segment_callback func, void *closure)
15280ff27d31SJohn Polstra {
1529247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
15300ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
15310ff27d31SJohn Polstra 	vm_map_entry_t entry;
1532976a87a2SAlan Cox 	vm_object_t backing_object, object;
1533976a87a2SAlan Cox 	boolean_t ignore_entry;
15340ff27d31SJohn Polstra 
1535976a87a2SAlan Cox 	vm_map_lock_read(map);
15360ff27d31SJohn Polstra 	for (entry = map->header.next; entry != &map->header;
15370ff27d31SJohn Polstra 	    entry = entry->next) {
1538fa7dd9c5SMatthew Dillon 		/*
1539fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1540fa7dd9c5SMatthew Dillon 		 * coredump mode.
1541fa7dd9c5SMatthew Dillon 		 *
1542fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1543fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1544fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1545fa7dd9c5SMatthew Dillon 		 */
1546fa7dd9c5SMatthew Dillon 		if (elf_legacy_coredump) {
1547fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
15480ff27d31SJohn Polstra 				continue;
1549fa7dd9c5SMatthew Dillon 		} else {
1550fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_ALL) == 0)
1551fa7dd9c5SMatthew Dillon 				continue;
1552fa7dd9c5SMatthew Dillon 		}
15530ff27d31SJohn Polstra 
15549730a5daSPaul Saab 		/*
1555fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1556fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1557fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1558fa7dd9c5SMatthew Dillon 		 * kernel map).
15599730a5daSPaul Saab 		 */
1560fa7dd9c5SMatthew Dillon 		if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
15619730a5daSPaul Saab 			continue;
15629730a5daSPaul Saab 
1563976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
15640ff27d31SJohn Polstra 			continue;
15650ff27d31SJohn Polstra 
15660ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1567bc403f03SAttilio Rao 		VM_OBJECT_RLOCK(object);
1568976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1569bc403f03SAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1570bc403f03SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1571976a87a2SAlan Cox 			object = backing_object;
1572976a87a2SAlan Cox 		}
1573976a87a2SAlan Cox 		ignore_entry = object->type != OBJT_DEFAULT &&
1574bc411bc2SJohn Baldwin 		    object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
1575bc411bc2SJohn Baldwin 		    object->type != OBJT_PHYS;
1576bc403f03SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1577976a87a2SAlan Cox 		if (ignore_entry)
15780ff27d31SJohn Polstra 			continue;
15790ff27d31SJohn Polstra 
15800ff27d31SJohn Polstra 		(*func)(entry, closure);
15810ff27d31SJohn Polstra 	}
1582976a87a2SAlan Cox 	vm_map_unlock_read(map);
15830ff27d31SJohn Polstra }
15840ff27d31SJohn Polstra 
15850ff27d31SJohn Polstra /*
15860ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
15870ff27d31SJohn Polstra  * the page boundary.
15880ff27d31SJohn Polstra  */
15898c64af4fSJohn Polstra static int
1590aa14e9b7SMark Johnston __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1591aa14e9b7SMark Johnston     size_t hdrsize, struct note_info_list *notelst, size_t notesz)
15928c64af4fSJohn Polstra {
1593bd390213SMikolaj Golub 	struct note_info *ninfo;
1594bd390213SMikolaj Golub 	struct sbuf *sb;
1595bd390213SMikolaj Golub 	int error;
15968c64af4fSJohn Polstra 
15978c64af4fSJohn Polstra 	/* Fill in the header. */
15980ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
1599aa14e9b7SMark Johnston 	__elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz);
16008c64af4fSJohn Polstra 
1601bd390213SMikolaj Golub 	sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1602aa14e9b7SMark Johnston 	sbuf_set_drain(sb, sbuf_drain_core_output, p);
1603bd390213SMikolaj Golub 	sbuf_start_section(sb, NULL);
1604bd390213SMikolaj Golub 	sbuf_bcat(sb, hdr, hdrsize);
1605bd390213SMikolaj Golub 	TAILQ_FOREACH(ninfo, notelst, link)
1606bd390213SMikolaj Golub 	    __elfN(putnote)(ninfo, sb);
1607bd390213SMikolaj Golub 	/* Align up to a page boundary for the program segments. */
1608bd390213SMikolaj Golub 	sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1609bd390213SMikolaj Golub 	error = sbuf_finish(sb);
1610bd390213SMikolaj Golub 	sbuf_delete(sb);
1611bd390213SMikolaj Golub 
1612bd390213SMikolaj Golub 	return (error);
1613e7228204SAlfred Perlstein }
1614bd390213SMikolaj Golub 
1615bd390213SMikolaj Golub static void
1616bd390213SMikolaj Golub __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1617bd390213SMikolaj Golub     size_t *sizep)
1618bd390213SMikolaj Golub {
1619bd390213SMikolaj Golub 	struct proc *p;
1620bd390213SMikolaj Golub 	struct thread *thr;
1621bd390213SMikolaj Golub 	size_t size;
1622bd390213SMikolaj Golub 
1623bd390213SMikolaj Golub 	p = td->td_proc;
1624bd390213SMikolaj Golub 	size = 0;
1625bd390213SMikolaj Golub 
1626bd390213SMikolaj Golub 	size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1627bd390213SMikolaj Golub 
1628bd390213SMikolaj Golub 	/*
1629bd390213SMikolaj Golub 	 * To have the debugger select the right thread (LWP) as the initial
1630bd390213SMikolaj Golub 	 * thread, we dump the state of the thread passed to us in td first.
1631bd390213SMikolaj Golub 	 * This is the thread that causes the core dump and thus likely to
1632bd390213SMikolaj Golub 	 * be the right thread one wants to have selected in the debugger.
1633bd390213SMikolaj Golub 	 */
1634bd390213SMikolaj Golub 	thr = td;
1635bd390213SMikolaj Golub 	while (thr != NULL) {
1636bd390213SMikolaj Golub 		size += register_note(list, NT_PRSTATUS,
1637bd390213SMikolaj Golub 		    __elfN(note_prstatus), thr);
1638bd390213SMikolaj Golub 		size += register_note(list, NT_FPREGSET,
1639bd390213SMikolaj Golub 		    __elfN(note_fpregset), thr);
1640bd390213SMikolaj Golub 		size += register_note(list, NT_THRMISC,
1641bd390213SMikolaj Golub 		    __elfN(note_thrmisc), thr);
164286be94fcSTycho Nightingale 		size += register_note(list, NT_PTLWPINFO,
164386be94fcSTycho Nightingale 		    __elfN(note_ptlwpinfo), thr);
1644bd390213SMikolaj Golub 		size += register_note(list, -1,
1645bd390213SMikolaj Golub 		    __elfN(note_threadmd), thr);
1646bd390213SMikolaj Golub 
1647bd390213SMikolaj Golub 		thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1648bd390213SMikolaj Golub 		    TAILQ_NEXT(thr, td_plist);
1649bd390213SMikolaj Golub 		if (thr == td)
1650bd390213SMikolaj Golub 			thr = TAILQ_NEXT(thr, td_plist);
1651dada0278SJohn Polstra 	}
1652dada0278SJohn Polstra 
1653f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PROC,
1654f1fca82eSMikolaj Golub 	    __elfN(note_procstat_proc), p);
1655f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_FILES,
1656f1fca82eSMikolaj Golub 	    note_procstat_files, p);
1657f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_VMMAP,
1658f1fca82eSMikolaj Golub 	    note_procstat_vmmap, p);
1659f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_GROUPS,
1660f1fca82eSMikolaj Golub 	    note_procstat_groups, p);
1661f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_UMASK,
1662f1fca82eSMikolaj Golub 	    note_procstat_umask, p);
1663f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_RLIMIT,
1664f1fca82eSMikolaj Golub 	    note_procstat_rlimit, p);
1665f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_OSREL,
1666f1fca82eSMikolaj Golub 	    note_procstat_osrel, p);
1667f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1668f1fca82eSMikolaj Golub 	    __elfN(note_procstat_psstrings), p);
1669f1fca82eSMikolaj Golub 	size += register_note(list, NT_PROCSTAT_AUXV,
1670f1fca82eSMikolaj Golub 	    __elfN(note_procstat_auxv), p);
1671f1fca82eSMikolaj Golub 
1672bd390213SMikolaj Golub 	*sizep = size;
1673bd390213SMikolaj Golub }
1674bd390213SMikolaj Golub 
1675bd390213SMikolaj Golub static void
1676bd390213SMikolaj Golub __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1677bd390213SMikolaj Golub     size_t notesz)
1678bd390213SMikolaj Golub {
1679bd390213SMikolaj Golub 	Elf_Ehdr *ehdr;
1680bd390213SMikolaj Golub 	Elf_Phdr *phdr;
1681c17b0bd2SConrad Meyer 	Elf_Shdr *shdr;
1682bd390213SMikolaj Golub 	struct phdr_closure phc;
1683bd390213SMikolaj Golub 
1684bd390213SMikolaj Golub 	ehdr = (Elf_Ehdr *)hdr;
1685bd390213SMikolaj Golub 
1686bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG0] = ELFMAG0;
1687bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG1] = ELFMAG1;
1688bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG2] = ELFMAG2;
1689bd390213SMikolaj Golub 	ehdr->e_ident[EI_MAG3] = ELFMAG3;
1690bd390213SMikolaj Golub 	ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1691bd390213SMikolaj Golub 	ehdr->e_ident[EI_DATA] = ELF_DATA;
1692bd390213SMikolaj Golub 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1693bd390213SMikolaj Golub 	ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1694bd390213SMikolaj Golub 	ehdr->e_ident[EI_ABIVERSION] = 0;
1695bd390213SMikolaj Golub 	ehdr->e_ident[EI_PAD] = 0;
1696bd390213SMikolaj Golub 	ehdr->e_type = ET_CORE;
1697885f13dcSJohn Baldwin 	ehdr->e_machine = td->td_proc->p_elf_machine;
1698bd390213SMikolaj Golub 	ehdr->e_version = EV_CURRENT;
1699bd390213SMikolaj Golub 	ehdr->e_entry = 0;
1700bd390213SMikolaj Golub 	ehdr->e_phoff = sizeof(Elf_Ehdr);
1701885f13dcSJohn Baldwin 	ehdr->e_flags = td->td_proc->p_elf_flags;
1702bd390213SMikolaj Golub 	ehdr->e_ehsize = sizeof(Elf_Ehdr);
1703bd390213SMikolaj Golub 	ehdr->e_phentsize = sizeof(Elf_Phdr);
1704bd390213SMikolaj Golub 	ehdr->e_shentsize = sizeof(Elf_Shdr);
1705bd390213SMikolaj Golub 	ehdr->e_shstrndx = SHN_UNDEF;
1706c17b0bd2SConrad Meyer 	if (numsegs + 1 < PN_XNUM) {
1707c17b0bd2SConrad Meyer 		ehdr->e_phnum = numsegs + 1;
1708c17b0bd2SConrad Meyer 		ehdr->e_shnum = 0;
1709c17b0bd2SConrad Meyer 	} else {
1710c17b0bd2SConrad Meyer 		ehdr->e_phnum = PN_XNUM;
1711c17b0bd2SConrad Meyer 		ehdr->e_shnum = 1;
1712c17b0bd2SConrad Meyer 
1713c17b0bd2SConrad Meyer 		ehdr->e_shoff = ehdr->e_phoff +
1714c17b0bd2SConrad Meyer 		    (numsegs + 1) * ehdr->e_phentsize;
1715c17b0bd2SConrad Meyer 		KASSERT(ehdr->e_shoff == hdrsize - sizeof(Elf_Shdr),
1716c17b0bd2SConrad Meyer 		    ("e_shoff: %zu, hdrsize - shdr: %zu",
171707f825e8SConrad Meyer 		     (size_t)ehdr->e_shoff, hdrsize - sizeof(Elf_Shdr)));
1718c17b0bd2SConrad Meyer 
1719c17b0bd2SConrad Meyer 		shdr = (Elf_Shdr *)((char *)hdr + ehdr->e_shoff);
1720c17b0bd2SConrad Meyer 		memset(shdr, 0, sizeof(*shdr));
1721c17b0bd2SConrad Meyer 		/*
1722c17b0bd2SConrad Meyer 		 * A special first section is used to hold large segment and
1723c17b0bd2SConrad Meyer 		 * section counts.  This was proposed by Sun Microsystems in
1724c17b0bd2SConrad Meyer 		 * Solaris and has been adopted by Linux; the standard ELF
1725c17b0bd2SConrad Meyer 		 * tools are already familiar with the technique.
1726c17b0bd2SConrad Meyer 		 *
1727c17b0bd2SConrad Meyer 		 * See table 7-7 of the Solaris "Linker and Libraries Guide"
1728c17b0bd2SConrad Meyer 		 * (or 12-7 depending on the version of the document) for more
1729c17b0bd2SConrad Meyer 		 * details.
1730c17b0bd2SConrad Meyer 		 */
1731c17b0bd2SConrad Meyer 		shdr->sh_type = SHT_NULL;
1732c17b0bd2SConrad Meyer 		shdr->sh_size = ehdr->e_shnum;
1733c17b0bd2SConrad Meyer 		shdr->sh_link = ehdr->e_shstrndx;
1734c17b0bd2SConrad Meyer 		shdr->sh_info = numsegs + 1;
1735c17b0bd2SConrad Meyer 	}
1736bd390213SMikolaj Golub 
1737bd390213SMikolaj Golub 	/*
1738bd390213SMikolaj Golub 	 * Fill in the program header entries.
1739bd390213SMikolaj Golub 	 */
1740c17b0bd2SConrad Meyer 	phdr = (Elf_Phdr *)((char *)hdr + ehdr->e_phoff);
1741bd390213SMikolaj Golub 
1742bd390213SMikolaj Golub 	/* The note segement. */
1743bd390213SMikolaj Golub 	phdr->p_type = PT_NOTE;
1744bd390213SMikolaj Golub 	phdr->p_offset = hdrsize;
1745bd390213SMikolaj Golub 	phdr->p_vaddr = 0;
1746bd390213SMikolaj Golub 	phdr->p_paddr = 0;
1747bd390213SMikolaj Golub 	phdr->p_filesz = notesz;
1748bd390213SMikolaj Golub 	phdr->p_memsz = 0;
1749bd390213SMikolaj Golub 	phdr->p_flags = PF_R;
17501b8388cdSMikolaj Golub 	phdr->p_align = ELF_NOTE_ROUNDSIZE;
1751bd390213SMikolaj Golub 	phdr++;
1752bd390213SMikolaj Golub 
1753bd390213SMikolaj Golub 	/* All the writable segments from the program. */
1754bd390213SMikolaj Golub 	phc.phdr = phdr;
1755bd390213SMikolaj Golub 	phc.offset = round_page(hdrsize + notesz);
17561005d8afSConrad Meyer 	each_dumpable_segment(td, cb_put_phdr, &phc);
1757bd390213SMikolaj Golub }
1758bd390213SMikolaj Golub 
1759bd390213SMikolaj Golub static size_t
1760bd390213SMikolaj Golub register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1761bd390213SMikolaj Golub {
1762bd390213SMikolaj Golub 	struct note_info *ninfo;
1763bd390213SMikolaj Golub 	size_t size, notesize;
1764bd390213SMikolaj Golub 
1765bd390213SMikolaj Golub 	size = 0;
1766bd390213SMikolaj Golub 	out(arg, NULL, &size);
1767bd390213SMikolaj Golub 	ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1768bd390213SMikolaj Golub 	ninfo->type = type;
1769bd390213SMikolaj Golub 	ninfo->outfunc = out;
1770bd390213SMikolaj Golub 	ninfo->outarg = arg;
1771bd390213SMikolaj Golub 	ninfo->outsize = size;
1772bd390213SMikolaj Golub 	TAILQ_INSERT_TAIL(list, ninfo, link);
1773bd390213SMikolaj Golub 
1774bd390213SMikolaj Golub 	if (type == -1)
1775bd390213SMikolaj Golub 		return (size);
1776bd390213SMikolaj Golub 
1777bd390213SMikolaj Golub 	notesize = sizeof(Elf_Note) +		/* note header */
1778180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1779180e57e5SJohn Baldwin 						/* note name */
1780180e57e5SJohn Baldwin 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1781180e57e5SJohn Baldwin 
1782180e57e5SJohn Baldwin 	return (notesize);
1783180e57e5SJohn Baldwin }
1784180e57e5SJohn Baldwin 
1785180e57e5SJohn Baldwin static size_t
1786180e57e5SJohn Baldwin append_note_data(const void *src, void *dst, size_t len)
1787180e57e5SJohn Baldwin {
1788180e57e5SJohn Baldwin 	size_t padded_len;
1789180e57e5SJohn Baldwin 
1790180e57e5SJohn Baldwin 	padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
1791180e57e5SJohn Baldwin 	if (dst != NULL) {
1792180e57e5SJohn Baldwin 		bcopy(src, dst, len);
1793180e57e5SJohn Baldwin 		bzero((char *)dst + len, padded_len - len);
1794180e57e5SJohn Baldwin 	}
1795180e57e5SJohn Baldwin 	return (padded_len);
1796180e57e5SJohn Baldwin }
1797180e57e5SJohn Baldwin 
1798180e57e5SJohn Baldwin size_t
1799180e57e5SJohn Baldwin __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
1800180e57e5SJohn Baldwin {
1801180e57e5SJohn Baldwin 	Elf_Note *note;
1802180e57e5SJohn Baldwin 	char *buf;
1803180e57e5SJohn Baldwin 	size_t notesize;
1804180e57e5SJohn Baldwin 
1805180e57e5SJohn Baldwin 	buf = dst;
1806180e57e5SJohn Baldwin 	if (buf != NULL) {
1807180e57e5SJohn Baldwin 		note = (Elf_Note *)buf;
1808180e57e5SJohn Baldwin 		note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1809180e57e5SJohn Baldwin 		note->n_descsz = size;
1810180e57e5SJohn Baldwin 		note->n_type = type;
1811180e57e5SJohn Baldwin 		buf += sizeof(*note);
1812180e57e5SJohn Baldwin 		buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
1813180e57e5SJohn Baldwin 		    sizeof(FREEBSD_ABI_VENDOR));
1814180e57e5SJohn Baldwin 		append_note_data(src, buf, size);
1815180e57e5SJohn Baldwin 		if (descp != NULL)
1816180e57e5SJohn Baldwin 			*descp = buf;
1817180e57e5SJohn Baldwin 	}
1818180e57e5SJohn Baldwin 
1819180e57e5SJohn Baldwin 	notesize = sizeof(Elf_Note) +		/* note header */
1820180e57e5SJohn Baldwin 	    roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1821180e57e5SJohn Baldwin 						/* note name */
18221b8388cdSMikolaj Golub 	    roundup2(size, ELF_NOTE_ROUNDSIZE);	/* note description */
1823bd390213SMikolaj Golub 
1824bd390213SMikolaj Golub 	return (notesize);
1825bd390213SMikolaj Golub }
1826bd390213SMikolaj Golub 
1827bd390213SMikolaj Golub static void
1828bd390213SMikolaj Golub __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1829bd390213SMikolaj Golub {
1830bd390213SMikolaj Golub 	Elf_Note note;
183114bdbaf2SConrad Meyer 	ssize_t old_len, sect_len;
183214bdbaf2SConrad Meyer 	size_t new_len, descsz, i;
1833bd390213SMikolaj Golub 
1834bd390213SMikolaj Golub 	if (ninfo->type == -1) {
1835bd390213SMikolaj Golub 		ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1836bd390213SMikolaj Golub 		return;
1837bd390213SMikolaj Golub 	}
1838bd390213SMikolaj Golub 
1839180e57e5SJohn Baldwin 	note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1840bd390213SMikolaj Golub 	note.n_descsz = ninfo->outsize;
1841bd390213SMikolaj Golub 	note.n_type = ninfo->type;
1842bd390213SMikolaj Golub 
1843bd390213SMikolaj Golub 	sbuf_bcat(sb, &note, sizeof(note));
1844bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1845180e57e5SJohn Baldwin 	sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
18461b8388cdSMikolaj Golub 	sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1847bd390213SMikolaj Golub 	if (note.n_descsz == 0)
1848bd390213SMikolaj Golub 		return;
1849bd390213SMikolaj Golub 	sbuf_start_section(sb, &old_len);
1850bd390213SMikolaj Golub 	ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
185114bdbaf2SConrad Meyer 	sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
185214bdbaf2SConrad Meyer 	if (sect_len < 0)
185314bdbaf2SConrad Meyer 		return;
185414bdbaf2SConrad Meyer 
185514bdbaf2SConrad Meyer 	new_len = (size_t)sect_len;
185614bdbaf2SConrad Meyer 	descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
185714bdbaf2SConrad Meyer 	if (new_len < descsz) {
185814bdbaf2SConrad Meyer 		/*
185914bdbaf2SConrad Meyer 		 * It is expected that individual note emitters will correctly
186014bdbaf2SConrad Meyer 		 * predict their expected output size and fill up to that size
186114bdbaf2SConrad Meyer 		 * themselves, padding in a format-specific way if needed.
186214bdbaf2SConrad Meyer 		 * However, in case they don't, just do it here with zeros.
186314bdbaf2SConrad Meyer 		 */
186414bdbaf2SConrad Meyer 		for (i = 0; i < descsz - new_len; i++)
186514bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
186614bdbaf2SConrad Meyer 	} else if (new_len > descsz) {
186714bdbaf2SConrad Meyer 		/*
186814bdbaf2SConrad Meyer 		 * We can't always truncate sb -- we may have drained some
186914bdbaf2SConrad Meyer 		 * of it already.
187014bdbaf2SConrad Meyer 		 */
187114bdbaf2SConrad Meyer 		KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
187214bdbaf2SConrad Meyer 		    "read it (%zu > %zu).  Since it is longer than "
187314bdbaf2SConrad Meyer 		    "expected, this coredump's notes are corrupt.  THIS "
187414bdbaf2SConrad Meyer 		    "IS A BUG in the note_procstat routine for type %u.\n",
187514bdbaf2SConrad Meyer 		    __func__, (unsigned)note.n_type, new_len, descsz,
187614bdbaf2SConrad Meyer 		    (unsigned)note.n_type));
187714bdbaf2SConrad Meyer 	}
1878bd390213SMikolaj Golub }
1879bd390213SMikolaj Golub 
1880bd390213SMikolaj Golub /*
1881bd390213SMikolaj Golub  * Miscellaneous note out functions.
1882bd390213SMikolaj Golub  */
1883bd390213SMikolaj Golub 
1884841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1885841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
188651645e83SJohn Baldwin #include <compat/freebsd32/freebsd32_signal.h>
1887841c0c7eSNathan Whitehorn 
188862919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
188962919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
189062919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
189162919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
189262919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
18937f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
1894f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	KERN_PROC_MASK32
1895f1fca82eSMikolaj Golub typedef struct kinfo_proc32 elf_kinfo_proc_t;
1896f1fca82eSMikolaj Golub typedef uint32_t elf_ps_strings_t;
189762919d78SPeter Wemm #else
189862919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
189962919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
190062919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
190162919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
190262919d78SPeter Wemm typedef gregset_t elf_gregset_t;
19037f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
1904f1fca82eSMikolaj Golub #define ELF_KERN_PROC_MASK	0
1905f1fca82eSMikolaj Golub typedef struct kinfo_proc elf_kinfo_proc_t;
1906f1fca82eSMikolaj Golub typedef vm_offset_t elf_ps_strings_t;
190762919d78SPeter Wemm #endif
190862919d78SPeter Wemm 
19098c64af4fSJohn Polstra static void
1910bd390213SMikolaj Golub __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
19118c64af4fSJohn Polstra {
1912c77547d2SJohn Baldwin 	struct sbuf sbarg;
1913c77547d2SJohn Baldwin 	size_t len;
1914c77547d2SJohn Baldwin 	char *cp, *end;
1915247aba24SMarcel Moolenaar 	struct proc *p;
1916bd390213SMikolaj Golub 	elf_prpsinfo_t *psinfo;
1917c77547d2SJohn Baldwin 	int error;
19188c64af4fSJohn Polstra 
1919bd390213SMikolaj Golub 	p = (struct proc *)arg;
1920bd390213SMikolaj Golub 	if (sb != NULL) {
1921bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1922bd390213SMikolaj Golub 		psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
19238c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
192462919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1925ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1926c77547d2SJohn Baldwin 		PROC_LOCK(p);
1927c77547d2SJohn Baldwin 		if (p->p_args != NULL) {
1928c77547d2SJohn Baldwin 			len = sizeof(psinfo->pr_psargs) - 1;
1929c77547d2SJohn Baldwin 			if (len > p->p_args->ar_length)
1930c77547d2SJohn Baldwin 				len = p->p_args->ar_length;
1931c77547d2SJohn Baldwin 			memcpy(psinfo->pr_psargs, p->p_args->ar_args, len);
1932c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
1933c77547d2SJohn Baldwin 			error = 0;
1934c77547d2SJohn Baldwin 		} else {
1935c77547d2SJohn Baldwin 			_PHOLD(p);
1936c77547d2SJohn Baldwin 			PROC_UNLOCK(p);
1937c77547d2SJohn Baldwin 			sbuf_new(&sbarg, psinfo->pr_psargs,
1938c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs), SBUF_FIXEDLEN);
1939c77547d2SJohn Baldwin 			error = proc_getargv(curthread, p, &sbarg);
1940c77547d2SJohn Baldwin 			PRELE(p);
1941c77547d2SJohn Baldwin 			if (sbuf_finish(&sbarg) == 0)
1942c77547d2SJohn Baldwin 				len = sbuf_len(&sbarg) - 1;
1943c77547d2SJohn Baldwin 			else
1944c77547d2SJohn Baldwin 				len = sizeof(psinfo->pr_psargs) - 1;
1945c77547d2SJohn Baldwin 			sbuf_delete(&sbarg);
1946c77547d2SJohn Baldwin 		}
1947c77547d2SJohn Baldwin 		if (error || len == 0)
1948ccd3953eSJohn Baldwin 			strlcpy(psinfo->pr_psargs, p->p_comm,
19498c9b7b2cSMarcel Moolenaar 			    sizeof(psinfo->pr_psargs));
1950c77547d2SJohn Baldwin 		else {
1951c77547d2SJohn Baldwin 			KASSERT(len < sizeof(psinfo->pr_psargs),
1952c77547d2SJohn Baldwin 			    ("len is too long: %zu vs %zu", len,
1953c77547d2SJohn Baldwin 			    sizeof(psinfo->pr_psargs)));
1954c77547d2SJohn Baldwin 			cp = psinfo->pr_psargs;
1955c77547d2SJohn Baldwin 			end = cp + len - 1;
1956c77547d2SJohn Baldwin 			for (;;) {
1957c77547d2SJohn Baldwin 				cp = memchr(cp, '\0', end - cp);
1958c77547d2SJohn Baldwin 				if (cp == NULL)
1959c77547d2SJohn Baldwin 					break;
1960c77547d2SJohn Baldwin 				*cp = ' ';
1961c77547d2SJohn Baldwin 			}
1962c77547d2SJohn Baldwin 		}
1963ccb83afdSJohn Baldwin 		psinfo->pr_pid = p->p_pid;
1964bd390213SMikolaj Golub 		sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1965bd390213SMikolaj Golub 		free(psinfo, M_TEMP);
1966bd390213SMikolaj Golub 	}
1967bd390213SMikolaj Golub 	*sizep = sizeof(*psinfo);
1968bd390213SMikolaj Golub }
1969bd390213SMikolaj Golub 
1970bd390213SMikolaj Golub static void
1971bd390213SMikolaj Golub __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1972bd390213SMikolaj Golub {
1973bd390213SMikolaj Golub 	struct thread *td;
1974bd390213SMikolaj Golub 	elf_prstatus_t *status;
1975bd390213SMikolaj Golub 
1976bd390213SMikolaj Golub 	td = (struct thread *)arg;
1977bd390213SMikolaj Golub 	if (sb != NULL) {
1978bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*status), ("invalid size"));
1979bd390213SMikolaj Golub 		status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
19808c9b7b2cSMarcel Moolenaar 		status->pr_version = PRSTATUS_VERSION;
198162919d78SPeter Wemm 		status->pr_statussz = sizeof(elf_prstatus_t);
198262919d78SPeter Wemm 		status->pr_gregsetsz = sizeof(elf_gregset_t);
198362919d78SPeter Wemm 		status->pr_fpregsetsz = sizeof(elf_fpregset_t);
19848c9b7b2cSMarcel Moolenaar 		status->pr_osreldate = osreldate;
1985bd390213SMikolaj Golub 		status->pr_cursig = td->td_proc->p_sig;
1986bd390213SMikolaj Golub 		status->pr_pid = td->td_tid;
1987841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1988bd390213SMikolaj Golub 		fill_regs32(td, &status->pr_reg);
198962919d78SPeter Wemm #else
1990bd390213SMikolaj Golub 		fill_regs(td, &status->pr_reg);
199162919d78SPeter Wemm #endif
1992bd390213SMikolaj Golub 		sbuf_bcat(sb, status, sizeof(*status));
1993bd390213SMikolaj Golub 		free(status, M_TEMP);
19948c9b7b2cSMarcel Moolenaar 	}
1995bd390213SMikolaj Golub 	*sizep = sizeof(*status);
1996bd390213SMikolaj Golub }
1997bd390213SMikolaj Golub 
1998bd390213SMikolaj Golub static void
1999bd390213SMikolaj Golub __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
2000bd390213SMikolaj Golub {
2001bd390213SMikolaj Golub 	struct thread *td;
2002bd390213SMikolaj Golub 	elf_prfpregset_t *fpregset;
2003bd390213SMikolaj Golub 
2004bd390213SMikolaj Golub 	td = (struct thread *)arg;
2005bd390213SMikolaj Golub 	if (sb != NULL) {
2006bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
2007bd390213SMikolaj Golub 		fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
2008bd390213SMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2009bd390213SMikolaj Golub 		fill_fpregs32(td, fpregset);
2010bd390213SMikolaj Golub #else
2011bd390213SMikolaj Golub 		fill_fpregs(td, fpregset);
2012bd390213SMikolaj Golub #endif
2013bd390213SMikolaj Golub 		sbuf_bcat(sb, fpregset, sizeof(*fpregset));
2014bd390213SMikolaj Golub 		free(fpregset, M_TEMP);
2015bd390213SMikolaj Golub 	}
2016bd390213SMikolaj Golub 	*sizep = sizeof(*fpregset);
2017bd390213SMikolaj Golub }
2018bd390213SMikolaj Golub 
2019bd390213SMikolaj Golub static void
2020bd390213SMikolaj Golub __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
2021bd390213SMikolaj Golub {
2022bd390213SMikolaj Golub 	struct thread *td;
2023bd390213SMikolaj Golub 	elf_thrmisc_t thrmisc;
2024bd390213SMikolaj Golub 
2025bd390213SMikolaj Golub 	td = (struct thread *)arg;
2026bd390213SMikolaj Golub 	if (sb != NULL) {
2027bd390213SMikolaj Golub 		KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
2028bd390213SMikolaj Golub 		bzero(&thrmisc._pad, sizeof(thrmisc._pad));
2029bd390213SMikolaj Golub 		strcpy(thrmisc.pr_tname, td->td_name);
2030bd390213SMikolaj Golub 		sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
2031bd390213SMikolaj Golub 	}
2032bd390213SMikolaj Golub 	*sizep = sizeof(thrmisc);
2033bd390213SMikolaj Golub }
2034bd390213SMikolaj Golub 
203586be94fcSTycho Nightingale static void
203686be94fcSTycho Nightingale __elfN(note_ptlwpinfo)(void *arg, struct sbuf *sb, size_t *sizep)
203786be94fcSTycho Nightingale {
203886be94fcSTycho Nightingale 	struct thread *td;
203986be94fcSTycho Nightingale 	size_t size;
204086be94fcSTycho Nightingale 	int structsize;
204151645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
204251645e83SJohn Baldwin 	struct ptrace_lwpinfo32 pl;
204351645e83SJohn Baldwin #else
204486be94fcSTycho Nightingale 	struct ptrace_lwpinfo pl;
204551645e83SJohn Baldwin #endif
204686be94fcSTycho Nightingale 
204786be94fcSTycho Nightingale 	td = (struct thread *)arg;
204851645e83SJohn Baldwin 	size = sizeof(structsize) + sizeof(pl);
204986be94fcSTycho Nightingale 	if (sb != NULL) {
205086be94fcSTycho Nightingale 		KASSERT(*sizep == size, ("invalid size"));
205151645e83SJohn Baldwin 		structsize = sizeof(pl);
205286be94fcSTycho Nightingale 		sbuf_bcat(sb, &structsize, sizeof(structsize));
205386be94fcSTycho Nightingale 		bzero(&pl, sizeof(pl));
205486be94fcSTycho Nightingale 		pl.pl_lwpid = td->td_tid;
205586be94fcSTycho Nightingale 		pl.pl_event = PL_EVENT_NONE;
205686be94fcSTycho Nightingale 		pl.pl_sigmask = td->td_sigmask;
205786be94fcSTycho Nightingale 		pl.pl_siglist = td->td_siglist;
205886be94fcSTycho Nightingale 		if (td->td_si.si_signo != 0) {
205986be94fcSTycho Nightingale 			pl.pl_event = PL_EVENT_SIGNAL;
206086be94fcSTycho Nightingale 			pl.pl_flags |= PL_FLAG_SI;
206151645e83SJohn Baldwin #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
206251645e83SJohn Baldwin 			siginfo_to_siginfo32(&td->td_si, &pl.pl_siginfo);
206351645e83SJohn Baldwin #else
206486be94fcSTycho Nightingale 			pl.pl_siginfo = td->td_si;
206551645e83SJohn Baldwin #endif
206686be94fcSTycho Nightingale 		}
206786be94fcSTycho Nightingale 		strcpy(pl.pl_tdname, td->td_name);
206886be94fcSTycho Nightingale 		/* XXX TODO: supply more information in struct ptrace_lwpinfo*/
206951645e83SJohn Baldwin 		sbuf_bcat(sb, &pl, sizeof(pl));
207086be94fcSTycho Nightingale 	}
207186be94fcSTycho Nightingale 	*sizep = size;
207286be94fcSTycho Nightingale }
207386be94fcSTycho Nightingale 
20744da47b2fSMarcel Moolenaar /*
20754da47b2fSMarcel Moolenaar  * Allow for MD specific notes, as well as any MD
20764da47b2fSMarcel Moolenaar  * specific preparations for writing MI notes.
20774da47b2fSMarcel Moolenaar  */
20788c64af4fSJohn Polstra static void
2079bd390213SMikolaj Golub __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
20808c64af4fSJohn Polstra {
2081bd390213SMikolaj Golub 	struct thread *td;
2082bd390213SMikolaj Golub 	void *buf;
2083bd390213SMikolaj Golub 	size_t size;
20848c64af4fSJohn Polstra 
2085bd390213SMikolaj Golub 	td = (struct thread *)arg;
2086bd390213SMikolaj Golub 	size = *sizep;
2087bd390213SMikolaj Golub 	if (size != 0 && sb != NULL)
2088bd390213SMikolaj Golub 		buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
208983a396ceSChristian Brueffer 	else
209083a396ceSChristian Brueffer 		buf = NULL;
2091bd390213SMikolaj Golub 	size = 0;
2092bd390213SMikolaj Golub 	__elfN(dump_thread)(td, buf, &size);
209364779280SKonstantin Belousov 	KASSERT(sb == NULL || *sizep == size, ("invalid size"));
209483a396ceSChristian Brueffer 	if (size != 0 && sb != NULL)
2095bd390213SMikolaj Golub 		sbuf_bcat(sb, buf, size);
2096a1761d73SChristian Brueffer 	free(buf, M_TEMP);
2097bd390213SMikolaj Golub 	*sizep = size;
20988c64af4fSJohn Polstra }
20998c64af4fSJohn Polstra 
2100f1fca82eSMikolaj Golub #ifdef KINFO_PROC_SIZE
2101f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
2102f1fca82eSMikolaj Golub #endif
2103f1fca82eSMikolaj Golub 
2104f1fca82eSMikolaj Golub static void
2105f1fca82eSMikolaj Golub __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
2106f1fca82eSMikolaj Golub {
2107f1fca82eSMikolaj Golub 	struct proc *p;
2108f1fca82eSMikolaj Golub 	size_t size;
2109f1fca82eSMikolaj Golub 	int structsize;
2110f1fca82eSMikolaj Golub 
2111f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2112f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_numthreads *
2113f1fca82eSMikolaj Golub 	    sizeof(elf_kinfo_proc_t);
2114f1fca82eSMikolaj Golub 
2115f1fca82eSMikolaj Golub 	if (sb != NULL) {
2116f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2117f1fca82eSMikolaj Golub 		structsize = sizeof(elf_kinfo_proc_t);
2118f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2119f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2120f1fca82eSMikolaj Golub 		kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
2121f1fca82eSMikolaj Golub 	}
2122f1fca82eSMikolaj Golub 	*sizep = size;
2123f1fca82eSMikolaj Golub }
2124f1fca82eSMikolaj Golub 
2125f1fca82eSMikolaj Golub #ifdef KINFO_FILE_SIZE
2126f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
2127f1fca82eSMikolaj Golub #endif
2128f1fca82eSMikolaj Golub 
2129f1fca82eSMikolaj Golub static void
2130f1fca82eSMikolaj Golub note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
2131f1fca82eSMikolaj Golub {
2132f1fca82eSMikolaj Golub 	struct proc *p;
213314bdbaf2SConrad Meyer 	size_t size, sect_sz, i;
213414bdbaf2SConrad Meyer 	ssize_t start_len, sect_len;
213514bdbaf2SConrad Meyer 	int structsize, filedesc_flags;
213614bdbaf2SConrad Meyer 
2137bcb60d52SConrad Meyer 	if (coredump_pack_fileinfo)
213814bdbaf2SConrad Meyer 		filedesc_flags = KERN_FILEDESC_PACK_KINFO;
213914bdbaf2SConrad Meyer 	else
214014bdbaf2SConrad Meyer 		filedesc_flags = 0;
2141f1fca82eSMikolaj Golub 
2142f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
214314bdbaf2SConrad Meyer 	structsize = sizeof(struct kinfo_file);
2144f1fca82eSMikolaj Golub 	if (sb == NULL) {
2145f1fca82eSMikolaj Golub 		size = 0;
2146f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2147f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2148f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2149f1fca82eSMikolaj Golub 		PROC_LOCK(p);
215014bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
2151f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2152f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2153f1fca82eSMikolaj Golub 		*sizep = size;
2154f1fca82eSMikolaj Golub 	} else {
215514bdbaf2SConrad Meyer 		sbuf_start_section(sb, &start_len);
215614bdbaf2SConrad Meyer 
2157f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2158f1fca82eSMikolaj Golub 		PROC_LOCK(p);
215914bdbaf2SConrad Meyer 		kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
216014bdbaf2SConrad Meyer 		    filedesc_flags);
216114bdbaf2SConrad Meyer 
216214bdbaf2SConrad Meyer 		sect_len = sbuf_end_section(sb, start_len, 0, 0);
216314bdbaf2SConrad Meyer 		if (sect_len < 0)
216414bdbaf2SConrad Meyer 			return;
216514bdbaf2SConrad Meyer 		sect_sz = sect_len;
216614bdbaf2SConrad Meyer 
216714bdbaf2SConrad Meyer 		KASSERT(sect_sz <= *sizep,
216814bdbaf2SConrad Meyer 		    ("kern_proc_filedesc_out did not respect maxlen; "
216914bdbaf2SConrad Meyer 		     "requested %zu, got %zu", *sizep - sizeof(structsize),
217014bdbaf2SConrad Meyer 		     sect_sz - sizeof(structsize)));
217114bdbaf2SConrad Meyer 
217214bdbaf2SConrad Meyer 		for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
217314bdbaf2SConrad Meyer 			sbuf_putc(sb, 0);
2174f1fca82eSMikolaj Golub 	}
2175f1fca82eSMikolaj Golub }
2176f1fca82eSMikolaj Golub 
2177f1fca82eSMikolaj Golub #ifdef KINFO_VMENTRY_SIZE
2178f1fca82eSMikolaj Golub CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
2179f1fca82eSMikolaj Golub #endif
2180f1fca82eSMikolaj Golub 
2181f1fca82eSMikolaj Golub static void
2182f1fca82eSMikolaj Golub note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
2183f1fca82eSMikolaj Golub {
2184f1fca82eSMikolaj Golub 	struct proc *p;
2185f1fca82eSMikolaj Golub 	size_t size;
2186e6b95927SConrad Meyer 	int structsize, vmmap_flags;
2187e6b95927SConrad Meyer 
2188e6b95927SConrad Meyer 	if (coredump_pack_vmmapinfo)
2189e6b95927SConrad Meyer 		vmmap_flags = KERN_VMMAP_PACK_KINFO;
2190e6b95927SConrad Meyer 	else
2191e6b95927SConrad Meyer 		vmmap_flags = 0;
2192f1fca82eSMikolaj Golub 
2193f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2194e6b95927SConrad Meyer 	structsize = sizeof(struct kinfo_vmentry);
2195f1fca82eSMikolaj Golub 	if (sb == NULL) {
2196f1fca82eSMikolaj Golub 		size = 0;
2197f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2198f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2199f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2200f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2201e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
2202f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2203f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2204f1fca82eSMikolaj Golub 		*sizep = size;
2205f1fca82eSMikolaj Golub 	} else {
2206f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2207f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2208e6b95927SConrad Meyer 		kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
2209e6b95927SConrad Meyer 		    vmmap_flags);
2210f1fca82eSMikolaj Golub 	}
2211f1fca82eSMikolaj Golub }
2212f1fca82eSMikolaj Golub 
2213f1fca82eSMikolaj Golub static void
2214f1fca82eSMikolaj Golub note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
2215f1fca82eSMikolaj Golub {
2216f1fca82eSMikolaj Golub 	struct proc *p;
2217f1fca82eSMikolaj Golub 	size_t size;
2218f1fca82eSMikolaj Golub 	int structsize;
2219f1fca82eSMikolaj Golub 
2220f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2221f1fca82eSMikolaj Golub 	size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
2222f1fca82eSMikolaj Golub 	if (sb != NULL) {
2223f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2224f1fca82eSMikolaj Golub 		structsize = sizeof(gid_t);
2225f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2226f1fca82eSMikolaj Golub 		sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2227f1fca82eSMikolaj Golub 		    sizeof(gid_t));
2228f1fca82eSMikolaj Golub 	}
2229f1fca82eSMikolaj Golub 	*sizep = size;
2230f1fca82eSMikolaj Golub }
2231f1fca82eSMikolaj Golub 
2232f1fca82eSMikolaj Golub static void
2233f1fca82eSMikolaj Golub note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2234f1fca82eSMikolaj Golub {
2235f1fca82eSMikolaj Golub 	struct proc *p;
2236f1fca82eSMikolaj Golub 	size_t size;
2237f1fca82eSMikolaj Golub 	int structsize;
2238f1fca82eSMikolaj Golub 
2239f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2240f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
2241f1fca82eSMikolaj Golub 	if (sb != NULL) {
2242f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2243f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_fd->fd_cmask);
2244f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2245f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
2246f1fca82eSMikolaj Golub 	}
2247f1fca82eSMikolaj Golub 	*sizep = size;
2248f1fca82eSMikolaj Golub }
2249f1fca82eSMikolaj Golub 
2250f1fca82eSMikolaj Golub static void
2251f1fca82eSMikolaj Golub note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2252f1fca82eSMikolaj Golub {
2253f1fca82eSMikolaj Golub 	struct proc *p;
2254f1fca82eSMikolaj Golub 	struct rlimit rlim[RLIM_NLIMITS];
2255f1fca82eSMikolaj Golub 	size_t size;
2256f1fca82eSMikolaj Golub 	int structsize, i;
2257f1fca82eSMikolaj Golub 
2258f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2259f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(rlim);
2260f1fca82eSMikolaj Golub 	if (sb != NULL) {
2261f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2262f1fca82eSMikolaj Golub 		structsize = sizeof(rlim);
2263f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2264f1fca82eSMikolaj Golub 		PROC_LOCK(p);
2265f1fca82eSMikolaj Golub 		for (i = 0; i < RLIM_NLIMITS; i++)
2266f6f6d240SMateusz Guzik 			lim_rlimit_proc(p, i, &rlim[i]);
2267f1fca82eSMikolaj Golub 		PROC_UNLOCK(p);
2268f1fca82eSMikolaj Golub 		sbuf_bcat(sb, rlim, sizeof(rlim));
2269f1fca82eSMikolaj Golub 	}
2270f1fca82eSMikolaj Golub 	*sizep = size;
2271f1fca82eSMikolaj Golub }
2272f1fca82eSMikolaj Golub 
2273f1fca82eSMikolaj Golub static void
2274f1fca82eSMikolaj Golub note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2275f1fca82eSMikolaj Golub {
2276f1fca82eSMikolaj Golub 	struct proc *p;
2277f1fca82eSMikolaj Golub 	size_t size;
2278f1fca82eSMikolaj Golub 	int structsize;
2279f1fca82eSMikolaj Golub 
2280f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2281f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(p->p_osrel);
2282f1fca82eSMikolaj Golub 	if (sb != NULL) {
2283f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2284f1fca82eSMikolaj Golub 		structsize = sizeof(p->p_osrel);
2285f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2286f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2287f1fca82eSMikolaj Golub 	}
2288f1fca82eSMikolaj Golub 	*sizep = size;
2289f1fca82eSMikolaj Golub }
2290f1fca82eSMikolaj Golub 
2291f1fca82eSMikolaj Golub static void
2292f1fca82eSMikolaj Golub __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2293f1fca82eSMikolaj Golub {
2294f1fca82eSMikolaj Golub 	struct proc *p;
2295f1fca82eSMikolaj Golub 	elf_ps_strings_t ps_strings;
2296f1fca82eSMikolaj Golub 	size_t size;
2297f1fca82eSMikolaj Golub 	int structsize;
2298f1fca82eSMikolaj Golub 
2299f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2300f1fca82eSMikolaj Golub 	size = sizeof(structsize) + sizeof(ps_strings);
2301f1fca82eSMikolaj Golub 	if (sb != NULL) {
2302f1fca82eSMikolaj Golub 		KASSERT(*sizep == size, ("invalid size"));
2303f1fca82eSMikolaj Golub 		structsize = sizeof(ps_strings);
2304f1fca82eSMikolaj Golub #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2305f1fca82eSMikolaj Golub 		ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2306f1fca82eSMikolaj Golub #else
2307f1fca82eSMikolaj Golub 		ps_strings = p->p_sysent->sv_psstrings;
2308f1fca82eSMikolaj Golub #endif
2309f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2310f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2311f1fca82eSMikolaj Golub 	}
2312f1fca82eSMikolaj Golub 	*sizep = size;
2313f1fca82eSMikolaj Golub }
2314f1fca82eSMikolaj Golub 
2315f1fca82eSMikolaj Golub static void
2316f1fca82eSMikolaj Golub __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2317f1fca82eSMikolaj Golub {
2318f1fca82eSMikolaj Golub 	struct proc *p;
2319f1fca82eSMikolaj Golub 	size_t size;
2320f1fca82eSMikolaj Golub 	int structsize;
2321f1fca82eSMikolaj Golub 
2322f1fca82eSMikolaj Golub 	p = (struct proc *)arg;
2323f1fca82eSMikolaj Golub 	if (sb == NULL) {
2324f1fca82eSMikolaj Golub 		size = 0;
2325f1fca82eSMikolaj Golub 		sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2326f1fca82eSMikolaj Golub 		sbuf_set_drain(sb, sbuf_drain_count, &size);
2327f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2328f1fca82eSMikolaj Golub 		PHOLD(p);
2329f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2330f1fca82eSMikolaj Golub 		PRELE(p);
2331f1fca82eSMikolaj Golub 		sbuf_finish(sb);
2332f1fca82eSMikolaj Golub 		sbuf_delete(sb);
2333f1fca82eSMikolaj Golub 		*sizep = size;
2334f1fca82eSMikolaj Golub 	} else {
2335f1fca82eSMikolaj Golub 		structsize = sizeof(Elf_Auxinfo);
2336f1fca82eSMikolaj Golub 		sbuf_bcat(sb, &structsize, sizeof(structsize));
2337f1fca82eSMikolaj Golub 		PHOLD(p);
2338f1fca82eSMikolaj Golub 		proc_getauxv(curthread, p, sb);
2339f1fca82eSMikolaj Golub 		PRELE(p);
2340f1fca82eSMikolaj Golub 	}
2341f1fca82eSMikolaj Golub }
2342f1fca82eSMikolaj Golub 
234332c01de2SDmitry Chagin static boolean_t
234492328a32SKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Note *checknote,
234592328a32SKonstantin Belousov     const char *note_vendor, const Elf_Phdr *pnote,
234692328a32SKonstantin Belousov     boolean_t (*cb)(const Elf_Note *, void *, boolean_t *), void *cb_arg)
234732c01de2SDmitry Chagin {
2348267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
234932c01de2SDmitry Chagin 	const char *note_name;
23506c775eb6SKonstantin Belousov 	char *buf;
23516c775eb6SKonstantin Belousov 	int i, error;
23526c775eb6SKonstantin Belousov 	boolean_t res;
235332c01de2SDmitry Chagin 
23546c775eb6SKonstantin Belousov 	/* We need some limit, might as well use PAGE_SIZE. */
23556c775eb6SKonstantin Belousov 	if (pnote == NULL || pnote->p_filesz > PAGE_SIZE)
235632c01de2SDmitry Chagin 		return (FALSE);
23576c775eb6SKonstantin Belousov 	ASSERT_VOP_LOCKED(imgp->vp, "parse_notes");
23586c775eb6SKonstantin Belousov 	if (pnote->p_offset > PAGE_SIZE ||
23596c775eb6SKonstantin Belousov 	    pnote->p_filesz > PAGE_SIZE - pnote->p_offset) {
23606c775eb6SKonstantin Belousov 		VOP_UNLOCK(imgp->vp, 0);
23616c775eb6SKonstantin Belousov 		buf = malloc(pnote->p_filesz, M_TEMP, M_WAITOK);
23626c775eb6SKonstantin Belousov 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
23636c775eb6SKonstantin Belousov 		error = vn_rdwr(UIO_READ, imgp->vp, buf, pnote->p_filesz,
23646c775eb6SKonstantin Belousov 		    pnote->p_offset, UIO_SYSSPACE, IO_NODELOCKED,
23656c775eb6SKonstantin Belousov 		    curthread->td_ucred, NOCRED, NULL, curthread);
23666c775eb6SKonstantin Belousov 		if (error != 0) {
23676c775eb6SKonstantin Belousov 			uprintf("i/o error PT_NOTE\n");
2368eda8fe63SKonstantin Belousov 			goto retf;
23696c775eb6SKonstantin Belousov 		}
23706c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)buf;
23716c775eb6SKonstantin Belousov 		note_end = (const Elf_Note *)(buf + pnote->p_filesz);
23726c775eb6SKonstantin Belousov 	} else {
23736c775eb6SKonstantin Belousov 		note = note0 = (const Elf_Note *)(imgp->image_header +
23746c775eb6SKonstantin Belousov 		    pnote->p_offset);
237532c01de2SDmitry Chagin 		note_end = (const Elf_Note *)(imgp->image_header +
237632c01de2SDmitry Chagin 		    pnote->p_offset + pnote->p_filesz);
23776c775eb6SKonstantin Belousov 		buf = NULL;
23786c775eb6SKonstantin Belousov 	}
2379267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2380d1ae5c83SKonstantin Belousov 		if (!aligned(note, Elf32_Addr) || (const char *)note_end -
23816c775eb6SKonstantin Belousov 		    (const char *)note < sizeof(Elf_Note)) {
2382eda8fe63SKonstantin Belousov 			goto retf;
23836c775eb6SKonstantin Belousov 		}
238492328a32SKonstantin Belousov 		if (note->n_namesz != checknote->n_namesz ||
238592328a32SKonstantin Belousov 		    note->n_descsz != checknote->n_descsz ||
238692328a32SKonstantin Belousov 		    note->n_type != checknote->n_type)
238732c01de2SDmitry Chagin 			goto nextnote;
238832c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
238992328a32SKonstantin Belousov 		if (note_name + checknote->n_namesz >=
239092328a32SKonstantin Belousov 		    (const char *)note_end || strncmp(note_vendor,
239192328a32SKonstantin Belousov 		    note_name, checknote->n_namesz) != 0)
239232c01de2SDmitry Chagin 			goto nextnote;
239332c01de2SDmitry Chagin 
239492328a32SKonstantin Belousov 		if (cb(note, cb_arg, &res))
23956c775eb6SKonstantin Belousov 			goto ret;
239632c01de2SDmitry Chagin nextnote:
239732c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
23981b8388cdSMikolaj Golub 		    roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
23991b8388cdSMikolaj Golub 		    roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
240032c01de2SDmitry Chagin 	}
2401eda8fe63SKonstantin Belousov retf:
24026c775eb6SKonstantin Belousov 	res = FALSE;
24036c775eb6SKonstantin Belousov ret:
24046c775eb6SKonstantin Belousov 	free(buf, M_TEMP);
24056c775eb6SKonstantin Belousov 	return (res);
240632c01de2SDmitry Chagin }
240732c01de2SDmitry Chagin 
240892328a32SKonstantin Belousov struct brandnote_cb_arg {
240992328a32SKonstantin Belousov 	Elf_Brandnote *brandnote;
241092328a32SKonstantin Belousov 	int32_t *osrel;
241192328a32SKonstantin Belousov };
241292328a32SKonstantin Belousov 
241392328a32SKonstantin Belousov static boolean_t
241492328a32SKonstantin Belousov brandnote_cb(const Elf_Note *note, void *arg0, boolean_t *res)
241592328a32SKonstantin Belousov {
241692328a32SKonstantin Belousov 	struct brandnote_cb_arg *arg;
241792328a32SKonstantin Belousov 
241892328a32SKonstantin Belousov 	arg = arg0;
241992328a32SKonstantin Belousov 
242092328a32SKonstantin Belousov 	/*
242192328a32SKonstantin Belousov 	 * Fetch the osreldate for binary from the ELF OSABI-note if
242292328a32SKonstantin Belousov 	 * necessary.
242392328a32SKonstantin Belousov 	 */
242492328a32SKonstantin Belousov 	*res = (arg->brandnote->flags & BN_TRANSLATE_OSREL) != 0 &&
242592328a32SKonstantin Belousov 	    arg->brandnote->trans_osrel != NULL ?
242692328a32SKonstantin Belousov 	    arg->brandnote->trans_osrel(note, arg->osrel) : TRUE;
242792328a32SKonstantin Belousov 
242892328a32SKonstantin Belousov 	return (TRUE);
242992328a32SKonstantin Belousov }
243092328a32SKonstantin Belousov 
2431cefb93f2SKonstantin Belousov static Elf_Note fctl_note = {
2432cefb93f2SKonstantin Belousov 	.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
2433cefb93f2SKonstantin Belousov 	.n_descsz = sizeof(uint32_t),
2434cefb93f2SKonstantin Belousov 	.n_type = NT_FREEBSD_FEATURE_CTL,
2435cefb93f2SKonstantin Belousov };
2436cefb93f2SKonstantin Belousov 
2437cefb93f2SKonstantin Belousov struct fctl_cb_arg {
2438cefb93f2SKonstantin Belousov 	uint32_t *fctl0;
2439cefb93f2SKonstantin Belousov };
2440cefb93f2SKonstantin Belousov 
2441cefb93f2SKonstantin Belousov static boolean_t
2442cefb93f2SKonstantin Belousov note_fctl_cb(const Elf_Note *note, void *arg0, boolean_t *res)
2443cefb93f2SKonstantin Belousov {
2444cefb93f2SKonstantin Belousov 	struct fctl_cb_arg *arg;
2445cefb93f2SKonstantin Belousov 	const Elf32_Word *desc;
2446cefb93f2SKonstantin Belousov 	uintptr_t p;
2447cefb93f2SKonstantin Belousov 
2448cefb93f2SKonstantin Belousov 	arg = arg0;
2449cefb93f2SKonstantin Belousov 	p = (uintptr_t)(note + 1);
2450cefb93f2SKonstantin Belousov 	p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
2451cefb93f2SKonstantin Belousov 	desc = (const Elf32_Word *)p;
2452cefb93f2SKonstantin Belousov 	*arg->fctl0 = desc[0];
2453cefb93f2SKonstantin Belousov 	return (TRUE);
2454cefb93f2SKonstantin Belousov }
2455cefb93f2SKonstantin Belousov 
245632c01de2SDmitry Chagin /*
2457cefb93f2SKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote, fetch
2458cefb93f2SKonstantin Belousov  * the osreldate and feature control flags for binary from the ELF
2459cefb93f2SKonstantin Belousov  * OSABI-note.  Only the first page of the image is searched, the same
2460cefb93f2SKonstantin Belousov  * as for headers.
24611a9c7decSKonstantin Belousov  */
24621a9c7decSKonstantin Belousov static boolean_t
246392328a32SKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *brandnote,
2464cefb93f2SKonstantin Belousov     int32_t *osrel, uint32_t *fctl0)
24651a9c7decSKonstantin Belousov {
24661a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
24671a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
246892328a32SKonstantin Belousov 	struct brandnote_cb_arg b_arg;
2469cefb93f2SKonstantin Belousov 	struct fctl_cb_arg f_arg;
2470cefb93f2SKonstantin Belousov 	int i, j;
24711a9c7decSKonstantin Belousov 
24721a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
24731a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
247492328a32SKonstantin Belousov 	b_arg.brandnote = brandnote;
247592328a32SKonstantin Belousov 	b_arg.osrel = osrel;
2476cefb93f2SKonstantin Belousov 	f_arg.fctl0 = fctl0;
24771a9c7decSKonstantin Belousov 
24781a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
247992328a32SKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE && __elfN(parse_notes)(imgp,
248092328a32SKonstantin Belousov 		    &brandnote->hdr, brandnote->vendor, &phdr[i], brandnote_cb,
248192328a32SKonstantin Belousov 		    &b_arg)) {
2482cefb93f2SKonstantin Belousov 			for (j = 0; j < hdr->e_phnum; j++) {
2483cefb93f2SKonstantin Belousov 				if (phdr[j].p_type == PT_NOTE &&
2484cefb93f2SKonstantin Belousov 				    __elfN(parse_notes)(imgp, &fctl_note,
2485cefb93f2SKonstantin Belousov 				    FREEBSD_ABI_VENDOR, &phdr[j],
2486cefb93f2SKonstantin Belousov 				    note_fctl_cb, &f_arg))
2487cefb93f2SKonstantin Belousov 					break;
2488cefb93f2SKonstantin Belousov 			}
24891a9c7decSKonstantin Belousov 			return (TRUE);
24901a9c7decSKonstantin Belousov 		}
249192328a32SKonstantin Belousov 	}
24921a9c7decSKonstantin Belousov 	return (FALSE);
24931a9c7decSKonstantin Belousov 
24941a9c7decSKonstantin Belousov }
24951a9c7decSKonstantin Belousov 
24961a9c7decSKonstantin Belousov /*
2497e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
2498e1743d02SSøren Schmidt  */
2499a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
2500b7feabf9SEd Maste 	.ex_imgact = __CONCAT(exec_, __elfN(imgact)),
2501b7feabf9SEd Maste 	.ex_name = __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2502a360a43dSJake Burkholder };
2503a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2504e7228204SAlfred Perlstein 
2505ed167eaaSKonstantin Belousov static vm_prot_t
2506ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
2507ed167eaaSKonstantin Belousov {
2508ed167eaaSKonstantin Belousov 	vm_prot_t prot;
2509ed167eaaSKonstantin Belousov 
2510ed167eaaSKonstantin Belousov 	prot = 0;
2511ed167eaaSKonstantin Belousov 	if (flags & PF_X)
2512ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
2513ed167eaaSKonstantin Belousov 	if (flags & PF_W)
2514ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
2515ed167eaaSKonstantin Belousov 	if (flags & PF_R)
2516ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
2517*eb785fabSKonstantin Belousov #if __ELF_WORD_SIZE == 32 && (defined(__amd64__) || defined(__i386__))
2518126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
2519676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
2520676eda08SMarcel Moolenaar #endif
2521ed167eaaSKonstantin Belousov 	return (prot);
2522ed167eaaSKonstantin Belousov }
2523ed167eaaSKonstantin Belousov 
2524ed167eaaSKonstantin Belousov static Elf_Word
2525ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
2526ed167eaaSKonstantin Belousov {
2527ed167eaaSKonstantin Belousov 	Elf_Word flags;
2528ed167eaaSKonstantin Belousov 
2529ed167eaaSKonstantin Belousov 	flags = 0;
2530ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
2531ed167eaaSKonstantin Belousov 		flags |= PF_X;
2532ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
2533ed167eaaSKonstantin Belousov 		flags |= PF_R;
2534ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
2535ed167eaaSKonstantin Belousov 		flags |= PF_W;
2536ed167eaaSKonstantin Belousov 	return (flags);
2537ed167eaaSKonstantin Belousov }
2538