xref: /freebsd/sys/kern/imgact_elf.c (revision aea810386d8eb0602013a36a5d41822758972af6)
1e1743d02SSøren Schmidt /*-
221a3ee0eSDavid E. O'Brien  * Copyright (c) 2000 David O'Brien
39a14aa01SUlrich Spörlein  * Copyright (c) 1995-1996 Søren Schmidt
4e1743d02SSøren Schmidt  * Copyright (c) 1996 Peter Wemm
5e1743d02SSøren Schmidt  * All rights reserved.
6e1743d02SSøren Schmidt  *
7e1743d02SSøren Schmidt  * Redistribution and use in source and binary forms, with or without
8e1743d02SSøren Schmidt  * modification, are permitted provided that the following conditions
9e1743d02SSøren Schmidt  * are met:
10e1743d02SSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
11e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer
12e1743d02SSøren Schmidt  *    in this position and unchanged.
13e1743d02SSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
14e1743d02SSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
15e1743d02SSøren Schmidt  *    documentation and/or other materials provided with the distribution.
16e1743d02SSøren Schmidt  * 3. The name of the author may not be used to endorse or promote products
1721dc7d4fSJens Schweikhardt  *    derived from this software without specific prior written permission
18e1743d02SSøren Schmidt  *
19e1743d02SSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20e1743d02SSøren Schmidt  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21e1743d02SSøren Schmidt  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22e1743d02SSøren Schmidt  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23e1743d02SSøren Schmidt  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24e1743d02SSøren Schmidt  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e1743d02SSøren Schmidt  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e1743d02SSøren Schmidt  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e1743d02SSøren Schmidt  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28e1743d02SSøren Schmidt  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e1743d02SSøren Schmidt  */
30e1743d02SSøren Schmidt 
31677b542eSDavid E. O'Brien #include <sys/cdefs.h>
32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
33677b542eSDavid E. O'Brien 
3412bc222eSJonathan Anderson #include "opt_capsicum.h"
3562919d78SPeter Wemm #include "opt_compat.h"
36e7228204SAlfred Perlstein #include "opt_core.h"
3762919d78SPeter Wemm 
38e1743d02SSøren Schmidt #include <sys/param.h>
3912bc222eSJonathan Anderson #include <sys/capability.h>
40e1743d02SSøren Schmidt #include <sys/exec.h>
418c64af4fSJohn Polstra #include <sys/fcntl.h>
42e1743d02SSøren Schmidt #include <sys/imgact.h>
43e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
44e1743d02SSøren Schmidt #include <sys/kernel.h>
45f34fa851SJohn Baldwin #include <sys/lock.h>
46e1743d02SSøren Schmidt #include <sys/malloc.h>
4768ff2a43SChristian S.J. Peron #include <sys/mount.h>
4835e0e5b3SJohn Baldwin #include <sys/mutex.h>
498c64af4fSJohn Polstra #include <sys/mman.h>
50a794e791SBruce Evans #include <sys/namei.h>
518c64af4fSJohn Polstra #include <sys/pioctl.h>
52a794e791SBruce Evans #include <sys/proc.h>
538c64af4fSJohn Polstra #include <sys/procfs.h>
541ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
558c64af4fSJohn Polstra #include <sys/resourcevar.h>
56da61b9a6SAlan Cox #include <sys/sf_buf.h>
57ee235befSKonstantin Belousov #include <sys/smp.h>
5836240ea5SDoug Rabson #include <sys/systm.h>
59e1743d02SSøren Schmidt #include <sys/signalvar.h>
608c64af4fSJohn Polstra #include <sys/stat.h>
611005a129SJohn Baldwin #include <sys/sx.h>
628c64af4fSJohn Polstra #include <sys/syscall.h>
63e1743d02SSøren Schmidt #include <sys/sysctl.h>
648c64af4fSJohn Polstra #include <sys/sysent.h>
65a794e791SBruce Evans #include <sys/vnode.h>
66e7228204SAlfred Perlstein #include <sys/syslog.h>
67e7228204SAlfred Perlstein #include <sys/eventhandler.h>
68e7228204SAlfred Perlstein 
69e7228204SAlfred Perlstein #include <net/zlib.h>
70e1743d02SSøren Schmidt 
71e1743d02SSøren Schmidt #include <vm/vm.h>
72e1743d02SSøren Schmidt #include <vm/vm_kern.h>
73e1743d02SSøren Schmidt #include <vm/vm_param.h>
74e1743d02SSøren Schmidt #include <vm/pmap.h>
75e1743d02SSøren Schmidt #include <vm/vm_map.h>
760ff27d31SJohn Polstra #include <vm/vm_object.h>
77e1743d02SSøren Schmidt #include <vm/vm_extern.h>
78e1743d02SSøren Schmidt 
7952c24af7SPeter Wemm #include <machine/elf.h>
80e1743d02SSøren Schmidt #include <machine/md_var.h>
81e1743d02SSøren Schmidt 
82c815a20cSDavid E. O'Brien #define OLD_EI_BRAND	8
83c815a20cSDavid E. O'Brien 
843ebc1248SPeter Wemm static int __elfN(check_header)(const Elf_Ehdr *hdr);
8532c01de2SDmitry Chagin static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
8632c01de2SDmitry Chagin     const char *interp, int32_t *osrel);
873ebc1248SPeter Wemm static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
883ebc1248SPeter Wemm     u_long *entry, size_t pagesize);
89292177e6SAlan Cox static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
90292177e6SAlan Cox     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
91292177e6SAlan Cox     size_t pagesize);
923ebc1248SPeter Wemm static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
9389ffc202SBjoern A. Zeeb static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
9489ffc202SBjoern A. Zeeb     int32_t *osrel);
9589ffc202SBjoern A. Zeeb static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
9632c01de2SDmitry Chagin static boolean_t __elfN(check_note)(struct image_params *imgp,
9732c01de2SDmitry Chagin     Elf_Brandnote *checknote, int32_t *osrel);
98ed167eaaSKonstantin Belousov static vm_prot_t __elfN(trans_prot)(Elf_Word);
99ed167eaaSKonstantin Belousov static Elf_Word __elfN(untrans_prot)(vm_prot_t);
100e1743d02SSøren Schmidt 
101a360a43dSJake Burkholder SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
102a360a43dSJake Burkholder     "");
103a360a43dSJake Burkholder 
104e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
105e7228204SAlfred Perlstein static int compress_core(gzFile, char *, char *, unsigned int,
106e7228204SAlfred Perlstein     struct thread * td);
107e7228204SAlfred Perlstein #define CORE_BUF_SIZE	(16 * 1024)
108e7228204SAlfred Perlstein #endif
109e7228204SAlfred Perlstein 
110e548a1d4SJake Burkholder int __elfN(fallback_brand) = -1;
111e548a1d4SJake Burkholder SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
112e548a1d4SJake Burkholder     fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
113a360a43dSJake Burkholder     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
114a360a43dSJake Burkholder TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand",
115e548a1d4SJake Burkholder     &__elfN(fallback_brand));
116a360a43dSJake Burkholder 
117551d79e1SMarcel Moolenaar static int elf_legacy_coredump = 0;
118a360a43dSJake Burkholder SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
119551d79e1SMarcel Moolenaar     &elf_legacy_coredump, 0, "");
120e1743d02SSøren Schmidt 
12162c625fdSKonstantin Belousov int __elfN(nxstack) =
12262c625fdSKonstantin Belousov #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
12362c625fdSKonstantin Belousov 	1;
12462c625fdSKonstantin Belousov #else
12562c625fdSKonstantin Belousov 	0;
12662c625fdSKonstantin Belousov #endif
127291c06a1SKonstantin Belousov SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
128291c06a1SKonstantin Belousov     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
129291c06a1SKonstantin Belousov     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
130291c06a1SKonstantin Belousov 
131126b36a2SKonstantin Belousov #if __ELF_WORD_SIZE == 32
132126b36a2SKonstantin Belousov #if defined(__amd64__) || defined(__ia64__)
133126b36a2SKonstantin Belousov int i386_read_exec = 0;
134126b36a2SKonstantin Belousov SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
135126b36a2SKonstantin Belousov     "enable execution from readable segments");
136126b36a2SKonstantin Belousov #endif
137126b36a2SKonstantin Belousov #endif
138126b36a2SKonstantin Belousov 
1393ebc1248SPeter Wemm static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
140e1743d02SSøren Schmidt 
14193d1c728SKonstantin Belousov #define	trunc_page_ps(va, ps)	((va) & ~(ps - 1))
14293d1c728SKonstantin Belousov #define	round_page_ps(va, ps)	(((va) + (ps - 1)) & ~(ps - 1))
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),
15032c01de2SDmitry Chagin 	.hdr.n_type	= 1,
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 
15689ffc202SBjoern A. Zeeb static boolean_t
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);
16289ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
16389ffc202SBjoern A. Zeeb 	*osrel = *(const int32_t *)(p);
16489ffc202SBjoern A. Zeeb 
16589ffc202SBjoern A. Zeeb 	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 
18089ffc202SBjoern A. Zeeb static boolean_t
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);
18789ffc202SBjoern A. Zeeb 	p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
18889ffc202SBjoern A. Zeeb 
18989ffc202SBjoern A. Zeeb 	desc = (const Elf32_Word *)p;
19089ffc202SBjoern A. Zeeb 	if (desc[0] != GNU_KFREEBSD_ABI_DESC)
19189ffc202SBjoern A. Zeeb 		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 
19989ffc202SBjoern A. Zeeb 	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,
25732c01de2SDmitry Chagin     int32_t *osrel)
2585fe3ed62SJake Burkholder {
25932c01de2SDmitry Chagin 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
2605fe3ed62SJake Burkholder 	Elf_Brandinfo *bi;
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 */
27232c01de2SDmitry Chagin 	for (i = 0; i < MAX_BRANDS; i++) {
27332c01de2SDmitry Chagin 		bi = elf_brand_list[i];
274ecc2fda8SBjoern A. Zeeb 		if (bi == NULL)
275ecc2fda8SBjoern A. Zeeb 			continue;
276ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine && (bi->flags &
277ecc2fda8SBjoern A. Zeeb 		    (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
27832c01de2SDmitry Chagin 			ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
27932c01de2SDmitry Chagin 			if (ret)
28032c01de2SDmitry Chagin 				return (bi);
28132c01de2SDmitry Chagin 		}
28232c01de2SDmitry Chagin 	}
28332c01de2SDmitry Chagin 
2845fe3ed62SJake Burkholder 	/* If the executable has a brand, search for it in the brand list. */
2855fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
2865fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
287ecc2fda8SBjoern A. Zeeb 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
288ecc2fda8SBjoern A. Zeeb 			continue;
289ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
2905fe3ed62SJake Burkholder 		    (hdr->e_ident[EI_OSABI] == bi->brand ||
2915fe3ed62SJake Burkholder 		    strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
2925fe3ed62SJake Burkholder 		    bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
2935fe3ed62SJake Burkholder 			return (bi);
2945fe3ed62SJake Burkholder 	}
2955fe3ed62SJake Burkholder 
2965fe3ed62SJake Burkholder 	/* Lacking a known brand, search for a recognized interpreter. */
2975fe3ed62SJake Burkholder 	if (interp != NULL) {
2985fe3ed62SJake Burkholder 		for (i = 0; i < MAX_BRANDS; i++) {
2995fe3ed62SJake Burkholder 			bi = elf_brand_list[i];
300ecc2fda8SBjoern A. Zeeb 			if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
301ecc2fda8SBjoern A. Zeeb 				continue;
302ecc2fda8SBjoern A. Zeeb 			if (hdr->e_machine == bi->machine &&
3035fe3ed62SJake Burkholder 			    strcmp(interp, bi->interp_path) == 0)
3045fe3ed62SJake Burkholder 				return (bi);
3055fe3ed62SJake Burkholder 		}
3065fe3ed62SJake Burkholder 	}
3075fe3ed62SJake Burkholder 
3085fe3ed62SJake Burkholder 	/* Lacking a recognized interpreter, try the default brand */
3095fe3ed62SJake Burkholder 	for (i = 0; i < MAX_BRANDS; i++) {
3105fe3ed62SJake Burkholder 		bi = elf_brand_list[i];
311ecc2fda8SBjoern A. Zeeb 		if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
312ecc2fda8SBjoern A. Zeeb 			continue;
313ecc2fda8SBjoern A. Zeeb 		if (hdr->e_machine == bi->machine &&
314e548a1d4SJake Burkholder 		    __elfN(fallback_brand) == bi->brand)
3155fe3ed62SJake Burkholder 			return (bi);
3165fe3ed62SJake Burkholder 	}
3175fe3ed62SJake Burkholder 	return (NULL);
3185fe3ed62SJake Burkholder }
3195fe3ed62SJake Burkholder 
320e1743d02SSøren Schmidt static int
3213ebc1248SPeter Wemm __elfN(check_header)(const Elf_Ehdr *hdr)
322e1743d02SSøren Schmidt {
323d0ca7c29SPeter Wemm 	Elf_Brandinfo *bi;
3243ebc1248SPeter Wemm 	int i;
3253ebc1248SPeter Wemm 
32652c24af7SPeter Wemm 	if (!IS_ELF(*hdr) ||
32752c24af7SPeter Wemm 	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
32852c24af7SPeter Wemm 	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
3293dc19c46SJacques Vidrine 	    hdr->e_ident[EI_VERSION] != EV_CURRENT ||
3303dc19c46SJacques Vidrine 	    hdr->e_phentsize != sizeof(Elf_Phdr) ||
3313dc19c46SJacques Vidrine 	    hdr->e_version != ELF_TARG_VER)
332a7cddfedSJake Burkholder 		return (ENOEXEC);
333e1743d02SSøren Schmidt 
3343ebc1248SPeter Wemm 	/*
3353ebc1248SPeter Wemm 	 * Make sure we have at least one brand for this machine.
3363ebc1248SPeter Wemm 	 */
3373ebc1248SPeter Wemm 
3383ebc1248SPeter Wemm 	for (i = 0; i < MAX_BRANDS; i++) {
339d0ca7c29SPeter Wemm 		bi = elf_brand_list[i];
340d0ca7c29SPeter Wemm 		if (bi != NULL && bi->machine == hdr->e_machine)
3413ebc1248SPeter Wemm 			break;
3423ebc1248SPeter Wemm 	}
3433ebc1248SPeter Wemm 	if (i == MAX_BRANDS)
344a7cddfedSJake Burkholder 		return (ENOEXEC);
345e1743d02SSøren Schmidt 
346a7cddfedSJake Burkholder 	return (0);
347e1743d02SSøren Schmidt }
348e1743d02SSøren Schmidt 
349e1743d02SSøren Schmidt static int
3503ebc1248SPeter Wemm __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
351ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
3523ebc1248SPeter Wemm {
353da61b9a6SAlan Cox 	struct sf_buf *sf;
354da61b9a6SAlan Cox 	int error;
3553ebc1248SPeter Wemm 	vm_offset_t off;
3563ebc1248SPeter Wemm 
3573ebc1248SPeter Wemm 	/*
3583ebc1248SPeter Wemm 	 * Create the page if it doesn't exist yet. Ignore errors.
3593ebc1248SPeter Wemm 	 */
3603ebc1248SPeter Wemm 	vm_map_lock(map);
361ff6f03c7SAlan Cox 	vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
362ff6f03c7SAlan Cox 	    VM_PROT_ALL, VM_PROT_ALL, 0);
3633ebc1248SPeter Wemm 	vm_map_unlock(map);
3643ebc1248SPeter Wemm 
3653ebc1248SPeter Wemm 	/*
3663ebc1248SPeter Wemm 	 * Find the page from the underlying object.
3673ebc1248SPeter Wemm 	 */
3683ebc1248SPeter Wemm 	if (object) {
369da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset);
370da61b9a6SAlan Cox 		if (sf == NULL)
371da61b9a6SAlan Cox 			return (KERN_FAILURE);
3723ebc1248SPeter Wemm 		off = offset - trunc_page(offset);
373da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
374ca0387efSJake Burkholder 		    end - start);
375da61b9a6SAlan Cox 		vm_imgact_unmap_page(sf);
3763ebc1248SPeter Wemm 		if (error) {
377a7cddfedSJake Burkholder 			return (KERN_FAILURE);
3783ebc1248SPeter Wemm 		}
3793ebc1248SPeter Wemm 	}
3803ebc1248SPeter Wemm 
381a7cddfedSJake Burkholder 	return (KERN_SUCCESS);
3823ebc1248SPeter Wemm }
3833ebc1248SPeter Wemm 
3843ebc1248SPeter Wemm static int
3853ebc1248SPeter Wemm __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
386ff6f03c7SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
3873ebc1248SPeter Wemm {
388da61b9a6SAlan Cox 	struct sf_buf *sf;
389da61b9a6SAlan Cox 	vm_offset_t off;
390a063facbSMarcel Moolenaar 	vm_size_t sz;
391a063facbSMarcel Moolenaar 	int error, rv;
3923ebc1248SPeter Wemm 
3933ebc1248SPeter Wemm 	if (start != trunc_page(start)) {
39481f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset, start,
395ff6f03c7SAlan Cox 		    round_page(start), prot);
3963ebc1248SPeter Wemm 		if (rv)
397a7cddfedSJake Burkholder 			return (rv);
3983ebc1248SPeter Wemm 		offset += round_page(start) - start;
3993ebc1248SPeter Wemm 		start = round_page(start);
4003ebc1248SPeter Wemm 	}
4013ebc1248SPeter Wemm 	if (end != round_page(end)) {
40281f223caSJake Burkholder 		rv = __elfN(map_partial)(map, object, offset +
403ff6f03c7SAlan Cox 		    trunc_page(end) - start, trunc_page(end), end, prot);
4043ebc1248SPeter Wemm 		if (rv)
405a7cddfedSJake Burkholder 			return (rv);
4063ebc1248SPeter Wemm 		end = trunc_page(end);
4073ebc1248SPeter Wemm 	}
4083ebc1248SPeter Wemm 	if (end > start) {
4093ebc1248SPeter Wemm 		if (offset & PAGE_MASK) {
4103ebc1248SPeter Wemm 			/*
4113ebc1248SPeter Wemm 			 * The mapping is not page aligned. This means we have
4123ebc1248SPeter Wemm 			 * to copy the data. Sigh.
4133ebc1248SPeter Wemm 			 */
414584716b0SAlan Cox 			rv = vm_map_find(map, NULL, 0, &start, end - start,
415ff6f03c7SAlan Cox 			    FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0);
4163ebc1248SPeter Wemm 			if (rv)
417a7cddfedSJake Burkholder 				return (rv);
418da61b9a6SAlan Cox 			if (object == NULL)
419da61b9a6SAlan Cox 				return (KERN_SUCCESS);
420da61b9a6SAlan Cox 			for (; start < end; start += sz) {
421da61b9a6SAlan Cox 				sf = vm_imgact_map_page(object, offset);
422da61b9a6SAlan Cox 				if (sf == NULL)
423da61b9a6SAlan Cox 					return (KERN_FAILURE);
4243ebc1248SPeter Wemm 				off = offset - trunc_page(offset);
4253ebc1248SPeter Wemm 				sz = end - start;
426da61b9a6SAlan Cox 				if (sz > PAGE_SIZE - off)
427da61b9a6SAlan Cox 					sz = PAGE_SIZE - off;
428da61b9a6SAlan Cox 				error = copyout((caddr_t)sf_buf_kva(sf) + off,
4293ebc1248SPeter Wemm 				    (caddr_t)start, sz);
430da61b9a6SAlan Cox 				vm_imgact_unmap_page(sf);
4313ebc1248SPeter Wemm 				if (error) {
432a7cddfedSJake Burkholder 					return (KERN_FAILURE);
4333ebc1248SPeter Wemm 				}
434da61b9a6SAlan Cox 				offset += sz;
4353ebc1248SPeter Wemm 			}
4363ebc1248SPeter Wemm 			rv = KERN_SUCCESS;
4373ebc1248SPeter Wemm 		} else {
438e5e6093bSAlan Cox 			vm_object_reference(object);
4393ebc1248SPeter Wemm 			vm_map_lock(map);
4403ebc1248SPeter Wemm 			rv = vm_map_insert(map, object, offset, start, end,
441ff6f03c7SAlan Cox 			    prot, VM_PROT_ALL, cow);
4423ebc1248SPeter Wemm 			vm_map_unlock(map);
443e5e6093bSAlan Cox 			if (rv != KERN_SUCCESS)
444e5e6093bSAlan Cox 				vm_object_deallocate(object);
4453ebc1248SPeter Wemm 		}
446a7cddfedSJake Burkholder 		return (rv);
4473ebc1248SPeter Wemm 	} else {
448a7cddfedSJake Burkholder 		return (KERN_SUCCESS);
4493ebc1248SPeter Wemm 	}
4503ebc1248SPeter Wemm }
4513ebc1248SPeter Wemm 
4523ebc1248SPeter Wemm static int
453292177e6SAlan Cox __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
4543ebc1248SPeter Wemm     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
4553ebc1248SPeter Wemm     size_t pagesize)
456e1743d02SSøren Schmidt {
457da61b9a6SAlan Cox 	struct sf_buf *sf;
458e1743d02SSøren Schmidt 	size_t map_len;
459292177e6SAlan Cox 	vm_map_t map;
460292177e6SAlan Cox 	vm_object_t object;
461e1743d02SSøren Schmidt 	vm_offset_t map_addr;
462fa7dd9c5SMatthew Dillon 	int error, rv, cow;
463e1743d02SSøren Schmidt 	size_t copy_len;
46452c24af7SPeter Wemm 	vm_offset_t file_addr;
46552c24af7SPeter Wemm 
46625ead034SBrian Feldman 	/*
46725ead034SBrian Feldman 	 * It's necessary to fail if the filsz + offset taken from the
46825ead034SBrian Feldman 	 * header is greater than the actual file pager object's size.
46925ead034SBrian Feldman 	 * If we were to allow this, then the vm_map_find() below would
47025ead034SBrian Feldman 	 * walk right off the end of the file object and into the ether.
47125ead034SBrian Feldman 	 *
47225ead034SBrian Feldman 	 * While I'm here, might as well check for something else that
47325ead034SBrian Feldman 	 * is invalid: filsz cannot be greater than memsz.
47425ead034SBrian Feldman 	 */
475292177e6SAlan Cox 	if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
47625ead034SBrian Feldman 		uprintf("elf_load_section: truncated ELF file\n");
47725ead034SBrian Feldman 		return (ENOEXEC);
47825ead034SBrian Feldman 	}
47925ead034SBrian Feldman 
480292177e6SAlan Cox 	object = imgp->object;
481292177e6SAlan Cox 	map = &imgp->proc->p_vmspace->vm_map;
4823ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
4833ebc1248SPeter Wemm 	file_addr = trunc_page_ps(offset, pagesize);
484e1743d02SSøren Schmidt 
485e1743d02SSøren Schmidt 	/*
48652c24af7SPeter Wemm 	 * We have two choices.  We can either clear the data in the last page
48752c24af7SPeter Wemm 	 * of an oversized mapping, or we can start the anon mapping a page
48852c24af7SPeter Wemm 	 * early and copy the initialized data into that first page.  We
48952c24af7SPeter Wemm 	 * choose the second..
49052c24af7SPeter Wemm 	 */
49152c24af7SPeter Wemm 	if (memsz > filsz)
4923ebc1248SPeter Wemm 		map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
49352c24af7SPeter Wemm 	else
4943ebc1248SPeter Wemm 		map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
49552c24af7SPeter Wemm 
49652c24af7SPeter Wemm 	if (map_len != 0) {
497fa7dd9c5SMatthew Dillon 		/* cow flags: don't dump readonly sections in core */
498fa7dd9c5SMatthew Dillon 		cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
499fa7dd9c5SMatthew Dillon 		    (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
500fa7dd9c5SMatthew Dillon 
501292177e6SAlan Cox 		rv = __elfN(map_insert)(map,
50252c24af7SPeter Wemm 				      object,
50352c24af7SPeter Wemm 				      file_addr,	/* file offset */
50452c24af7SPeter Wemm 				      map_addr,		/* virtual start */
50552c24af7SPeter Wemm 				      map_addr + map_len,/* virtual end */
50652c24af7SPeter Wemm 				      prot,
507fa7dd9c5SMatthew Dillon 				      cow);
508e5e6093bSAlan Cox 		if (rv != KERN_SUCCESS)
509a7cddfedSJake Burkholder 			return (EINVAL);
51052c24af7SPeter Wemm 
51152c24af7SPeter Wemm 		/* we can stop now if we've covered it all */
51223955314SAlfred Perlstein 		if (memsz == filsz) {
513a7cddfedSJake Burkholder 			return (0);
51452c24af7SPeter Wemm 		}
51523955314SAlfred Perlstein 	}
51652c24af7SPeter Wemm 
51752c24af7SPeter Wemm 
51852c24af7SPeter Wemm 	/*
51952c24af7SPeter Wemm 	 * We have to get the remaining bit of the file into the first part
52052c24af7SPeter Wemm 	 * of the oversized map segment.  This is normally because the .data
52152c24af7SPeter Wemm 	 * segment in the file is extended to provide bss.  It's a neat idea
52252c24af7SPeter Wemm 	 * to try and save a page, but it's a pain in the behind to implement.
523e1743d02SSøren Schmidt 	 */
5243ebc1248SPeter Wemm 	copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
5253ebc1248SPeter Wemm 	map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
526ca0387efSJake Burkholder 	map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
527ca0387efSJake Burkholder 	    map_addr;
528e1743d02SSøren Schmidt 
52952c24af7SPeter Wemm 	/* This had damn well better be true! */
5308191d577SPeter Wemm 	if (map_len != 0) {
531292177e6SAlan Cox 		rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
532292177e6SAlan Cox 		    map_len, VM_PROT_ALL, 0);
53323955314SAlfred Perlstein 		if (rv != KERN_SUCCESS) {
534a7cddfedSJake Burkholder 			return (EINVAL);
5358191d577SPeter Wemm 		}
53623955314SAlfred Perlstein 	}
537e1743d02SSøren Schmidt 
53852c24af7SPeter Wemm 	if (copy_len != 0) {
5393ebc1248SPeter Wemm 		vm_offset_t off;
540da61b9a6SAlan Cox 
541da61b9a6SAlan Cox 		sf = vm_imgact_map_page(object, offset + filsz);
542da61b9a6SAlan Cox 		if (sf == NULL)
543da61b9a6SAlan Cox 			return (EIO);
544e1743d02SSøren Schmidt 
54552c24af7SPeter Wemm 		/* send the page fragment to user space */
54681f223caSJake Burkholder 		off = trunc_page_ps(offset + filsz, pagesize) -
54781f223caSJake Burkholder 		    trunc_page(offset + filsz);
548da61b9a6SAlan Cox 		error = copyout((caddr_t)sf_buf_kva(sf) + off,
549da61b9a6SAlan Cox 		    (caddr_t)map_addr, copy_len);
550da61b9a6SAlan Cox 		vm_imgact_unmap_page(sf);
55123955314SAlfred Perlstein 		if (error) {
55252c24af7SPeter Wemm 			return (error);
55352c24af7SPeter Wemm 		}
55423955314SAlfred Perlstein 	}
555e1743d02SSøren Schmidt 
556e1743d02SSøren Schmidt 	/*
5573ebc1248SPeter Wemm 	 * set it to the specified protection.
5583ebc1248SPeter Wemm 	 * XXX had better undo the damage from pasting over the cracks here!
559e1743d02SSøren Schmidt 	 */
560292177e6SAlan Cox 	vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
561292177e6SAlan Cox 	    map_len), prot, FALSE);
5628191d577SPeter Wemm 
563ff6f03c7SAlan Cox 	return (0);
564e1743d02SSøren Schmidt }
565e1743d02SSøren Schmidt 
566c33fe779SJohn Polstra /*
567c33fe779SJohn Polstra  * Load the file "file" into memory.  It may be either a shared object
568c33fe779SJohn Polstra  * or an executable.
569c33fe779SJohn Polstra  *
570c33fe779SJohn Polstra  * The "addr" reference parameter is in/out.  On entry, it specifies
571c33fe779SJohn Polstra  * the address where a shared object should be loaded.  If the file is
572c33fe779SJohn Polstra  * an executable, this value is ignored.  On exit, "addr" specifies
573c33fe779SJohn Polstra  * where the file was actually loaded.
574c33fe779SJohn Polstra  *
575c33fe779SJohn Polstra  * The "entry" reference parameter is out only.  On exit, it specifies
576c33fe779SJohn Polstra  * the entry point for the loaded file.
577c33fe779SJohn Polstra  */
578e1743d02SSøren Schmidt static int
5793ebc1248SPeter Wemm __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
5803ebc1248SPeter Wemm 	u_long *entry, size_t pagesize)
581e1743d02SSøren Schmidt {
582911c2be0SMark Peek 	struct {
583911c2be0SMark Peek 		struct nameidata nd;
584911c2be0SMark Peek 		struct vattr attr;
585911c2be0SMark Peek 		struct image_params image_params;
586911c2be0SMark Peek 	} *tempdata;
587d254af07SMatthew Dillon 	const Elf_Ehdr *hdr = NULL;
588d254af07SMatthew Dillon 	const Elf_Phdr *phdr = NULL;
589911c2be0SMark Peek 	struct nameidata *nd;
590911c2be0SMark Peek 	struct vattr *attr;
591911c2be0SMark Peek 	struct image_params *imgp;
59252c24af7SPeter Wemm 	vm_prot_t prot;
593c33fe779SJohn Polstra 	u_long rbase;
594c33fe779SJohn Polstra 	u_long base_addr = 0;
59568ff2a43SChristian S.J. Peron 	int vfslocked, error, i, numsegs;
596e1743d02SSøren Schmidt 
59712bc222eSJonathan Anderson #ifdef CAPABILITY_MODE
59812bc222eSJonathan Anderson 	/*
59912bc222eSJonathan Anderson 	 * XXXJA: This check can go away once we are sufficiently confident
60012bc222eSJonathan Anderson 	 * that the checks in namei() are correct.
60112bc222eSJonathan Anderson 	 */
60212bc222eSJonathan Anderson 	if (IN_CAPABILITY_MODE(curthread))
60312bc222eSJonathan Anderson 		return (ECAPMODE);
60412bc222eSJonathan Anderson #endif
60512bc222eSJonathan Anderson 
606a163d034SWarner Losh 	tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
607911c2be0SMark Peek 	nd = &tempdata->nd;
608911c2be0SMark Peek 	attr = &tempdata->attr;
609911c2be0SMark Peek 	imgp = &tempdata->image_params;
610911c2be0SMark Peek 
611c8a79999SPeter Wemm 	/*
612c8a79999SPeter Wemm 	 * Initialize part of the common data
613c8a79999SPeter Wemm 	 */
614c8a79999SPeter Wemm 	imgp->proc = p;
615911c2be0SMark Peek 	imgp->attr = attr;
616c8a79999SPeter Wemm 	imgp->firstpage = NULL;
61759c8bc40SAlan Cox 	imgp->image_header = NULL;
6180b2ed1aeSJeff Roberson 	imgp->object = NULL;
6196d7bdc8dSRobert Watson 	imgp->execlabel = NULL;
620c8a79999SPeter Wemm 
62168ff2a43SChristian S.J. Peron 	NDINIT(nd, LOOKUP, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file,
62268ff2a43SChristian S.J. Peron 	    curthread);
62368ff2a43SChristian S.J. Peron 	vfslocked = 0;
624911c2be0SMark Peek 	if ((error = namei(nd)) != 0) {
625911c2be0SMark Peek 		nd->ni_vp = NULL;
626e1743d02SSøren Schmidt 		goto fail;
627e1743d02SSøren Schmidt 	}
62868ff2a43SChristian S.J. Peron 	vfslocked = NDHASGIANT(nd);
629911c2be0SMark Peek 	NDFREE(nd, NDF_ONLY_PNBUF);
630911c2be0SMark Peek 	imgp->vp = nd->ni_vp;
631c8a79999SPeter Wemm 
632e1743d02SSøren Schmidt 	/*
633e1743d02SSøren Schmidt 	 * Check permissions, modes, uid, etc on the file, and "open" it.
634e1743d02SSøren Schmidt 	 */
635c8a79999SPeter Wemm 	error = exec_check_permissions(imgp);
636373d1a3fSAlan Cox 	if (error)
637c8a79999SPeter Wemm 		goto fail;
638e1743d02SSøren Schmidt 
639c8a79999SPeter Wemm 	error = exec_map_first_page(imgp);
640373d1a3fSAlan Cox 	if (error)
641373d1a3fSAlan Cox 		goto fail;
642373d1a3fSAlan Cox 
64325ead034SBrian Feldman 	/*
64425ead034SBrian Feldman 	 * Also make certain that the interpreter stays the same, so set
645e6e370a7SJeff Roberson 	 * its VV_TEXT flag, too.
64625ead034SBrian Feldman 	 */
647e6e370a7SJeff Roberson 	nd->ni_vp->v_vflag |= VV_TEXT;
648e6e370a7SJeff Roberson 
6498516dd18SPoul-Henning Kamp 	imgp->object = nd->ni_vp->v_object;
650e1743d02SSøren Schmidt 
651d254af07SMatthew Dillon 	hdr = (const Elf_Ehdr *)imgp->image_header;
6523ebc1248SPeter Wemm 	if ((error = __elfN(check_header)(hdr)) != 0)
653e1743d02SSøren Schmidt 		goto fail;
654c33fe779SJohn Polstra 	if (hdr->e_type == ET_DYN)
655c33fe779SJohn Polstra 		rbase = *addr;
656c33fe779SJohn Polstra 	else if (hdr->e_type == ET_EXEC)
657c33fe779SJohn Polstra 		rbase = 0;
658c33fe779SJohn Polstra 	else {
659c33fe779SJohn Polstra 		error = ENOEXEC;
660c33fe779SJohn Polstra 		goto fail;
661c33fe779SJohn Polstra 	}
662e1743d02SSøren Schmidt 
663c8a79999SPeter Wemm 	/* Only support headers that fit within first page for now      */
6643dc19c46SJacques Vidrine 	/*    (multiplication of two Elf_Half fields will not overflow) */
66552c24af7SPeter Wemm 	if ((hdr->e_phoff > PAGE_SIZE) ||
6663dc19c46SJacques Vidrine 	    (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
667c8a79999SPeter Wemm 		error = ENOEXEC;
668e1743d02SSøren Schmidt 		goto fail;
669c8a79999SPeter Wemm 	}
670c8a79999SPeter Wemm 
671d254af07SMatthew Dillon 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
67293d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr)) {
67393d1c728SKonstantin Belousov 		error = ENOEXEC;
67493d1c728SKonstantin Belousov 		goto fail;
67593d1c728SKonstantin Belousov 	}
676e1743d02SSøren Schmidt 
677c33fe779SJohn Polstra 	for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
6785b33842aSKonstantin Belousov 		if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
6795b33842aSKonstantin Belousov 			/* Loadable segment */
680ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
681292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
68281f223caSJake Burkholder 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
683292177e6SAlan Cox 			    phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
684292177e6SAlan Cox 			if (error != 0)
685e1743d02SSøren Schmidt 				goto fail;
686e1743d02SSøren Schmidt 			/*
687c33fe779SJohn Polstra 			 * Establish the base address if this is the
688c33fe779SJohn Polstra 			 * first segment.
689e1743d02SSøren Schmidt 			 */
690c33fe779SJohn Polstra 			if (numsegs == 0)
691ca0387efSJake Burkholder   				base_addr = trunc_page(phdr[i].p_vaddr +
692ca0387efSJake Burkholder 				    rbase);
693c33fe779SJohn Polstra 			numsegs++;
694e1743d02SSøren Schmidt 		}
695e1743d02SSøren Schmidt 	}
696c33fe779SJohn Polstra 	*addr = base_addr;
697c33fe779SJohn Polstra 	*entry = (unsigned long)hdr->e_entry + rbase;
698e1743d02SSøren Schmidt 
699e1743d02SSøren Schmidt fail:
700c8a79999SPeter Wemm 	if (imgp->firstpage)
701c8a79999SPeter Wemm 		exec_unmap_first_page(imgp);
7020b2ed1aeSJeff Roberson 
703911c2be0SMark Peek 	if (nd->ni_vp)
704373d1a3fSAlan Cox 		vput(nd->ni_vp);
705911c2be0SMark Peek 
70668ff2a43SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
707911c2be0SMark Peek 	free(tempdata, M_TEMP);
708e1743d02SSøren Schmidt 
709a7cddfedSJake Burkholder 	return (error);
710e1743d02SSøren Schmidt }
711e1743d02SSøren Schmidt 
712303b270bSEivind Eklund static int
7133ebc1248SPeter Wemm __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
714e1743d02SSøren Schmidt {
715ecbb00a2SDoug Rabson 	const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
71632c01de2SDmitry Chagin 	const Elf_Phdr *phdr;
717e5e6093bSAlan Cox 	Elf_Auxargs *elf_auxargs;
7185856e12eSJohn Dyson 	struct vmspace *vmspace;
71952c24af7SPeter Wemm 	vm_prot_t prot;
72021c2d047SMatthew Dillon 	u_long text_size = 0, data_size = 0, total_size = 0;
721e1743d02SSøren Schmidt 	u_long text_addr = 0, data_addr = 0;
722cac45152SMatthew Dillon 	u_long seg_size, seg_addr;
7237564c4adSKonstantin Belousov 	u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
72432c01de2SDmitry Chagin 	int32_t osrel = 0;
7257564c4adSKonstantin Belousov 	int error = 0, i, n;
7264113f8d7SPeter Wemm 	const char *interp = NULL, *newinterp = NULL;
727d1dbc694SJohn Polstra 	Elf_Brandinfo *brand_info;
728911c2be0SMark Peek 	char *path;
7295fe3ed62SJake Burkholder 	struct sysentvec *sv;
730e1743d02SSøren Schmidt 
731e1743d02SSøren Schmidt 	/*
732e1743d02SSøren Schmidt 	 * Do we have a valid ELF header ?
733900b28f9SMaxim Sobolev 	 *
734900b28f9SMaxim Sobolev 	 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
735900b28f9SMaxim Sobolev 	 * if particular brand doesn't support it.
736e1743d02SSøren Schmidt 	 */
737900b28f9SMaxim Sobolev 	if (__elfN(check_header)(hdr) != 0 ||
738900b28f9SMaxim Sobolev 	    (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
739a7cddfedSJake Burkholder 		return (-1);
740e1743d02SSøren Schmidt 
741e1743d02SSøren Schmidt 	/*
742e1743d02SSøren Schmidt 	 * From here on down, we return an errno, not -1, as we've
743e1743d02SSøren Schmidt 	 * detected an ELF file.
744e1743d02SSøren Schmidt 	 */
745e1743d02SSøren Schmidt 
746e1743d02SSøren Schmidt 	if ((hdr->e_phoff > PAGE_SIZE) ||
74752c24af7SPeter Wemm 	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
748c8a79999SPeter Wemm 		/* Only support headers in first page for now */
749a7cddfedSJake Burkholder 		return (ENOEXEC);
750e1743d02SSøren Schmidt 	}
75152c24af7SPeter Wemm 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
75293d1c728SKonstantin Belousov 	if (!aligned(phdr, Elf_Addr))
75393d1c728SKonstantin Belousov 		return (ENOEXEC);
7547564c4adSKonstantin Belousov 	n = 0;
7557564c4adSKonstantin Belousov 	baddr = 0;
7565fe3ed62SJake Burkholder 	for (i = 0; i < hdr->e_phnum; i++) {
757291c06a1SKonstantin Belousov 		switch (phdr[i].p_type) {
758291c06a1SKonstantin Belousov 		case PT_LOAD:
7597564c4adSKonstantin Belousov 			if (n == 0)
7607564c4adSKonstantin Belousov 				baddr = phdr[i].p_vaddr;
7617564c4adSKonstantin Belousov 			n++;
762291c06a1SKonstantin Belousov 			break;
763291c06a1SKonstantin Belousov 		case PT_INTERP:
764e5e6093bSAlan Cox 			/* Path to interpreter */
7655fe3ed62SJake Burkholder 			if (phdr[i].p_filesz > MAXPATHLEN ||
76660bb3943SAlan Cox 			    phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE)
76760bb3943SAlan Cox 				return (ENOEXEC);
7685fe3ed62SJake Burkholder 			interp = imgp->image_header + phdr[i].p_offset;
769291c06a1SKonstantin Belousov 			break;
770291c06a1SKonstantin Belousov 		case PT_GNU_STACK:
771291c06a1SKonstantin Belousov 			if (__elfN(nxstack))
772291c06a1SKonstantin Belousov 				imgp->stack_prot =
773291c06a1SKonstantin Belousov 				    __elfN(trans_prot)(phdr[i].p_flags);
774291c06a1SKonstantin Belousov 			break;
7753ebc1248SPeter Wemm 		}
7763ebc1248SPeter Wemm 	}
7773ebc1248SPeter Wemm 
77832c01de2SDmitry Chagin 	brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel);
7795fe3ed62SJake Burkholder 	if (brand_info == NULL) {
7805fe3ed62SJake Burkholder 		uprintf("ELF binary type \"%u\" not known.\n",
7815fe3ed62SJake Burkholder 		    hdr->e_ident[EI_OSABI]);
78260bb3943SAlan Cox 		return (ENOEXEC);
7833ebc1248SPeter Wemm 	}
784ab02d85fSKonstantin Belousov 	if (hdr->e_type == ET_DYN) {
785ab02d85fSKonstantin Belousov 		if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
786d49b2109SMaxim Sobolev 			return (ENOEXEC);
7877564c4adSKonstantin Belousov 		/*
7887564c4adSKonstantin Belousov 		 * Honour the base load address from the dso if it is
7897564c4adSKonstantin Belousov 		 * non-zero for some reason.
7907564c4adSKonstantin Belousov 		 */
7917564c4adSKonstantin Belousov 		if (baddr == 0)
792ab02d85fSKonstantin Belousov 			et_dyn_addr = ET_DYN_LOAD_ADDR;
7937564c4adSKonstantin Belousov 		else
7947564c4adSKonstantin Belousov 			et_dyn_addr = 0;
795ab02d85fSKonstantin Belousov 	} else
796ab02d85fSKonstantin Belousov 		et_dyn_addr = 0;
7975fe3ed62SJake Burkholder 	sv = brand_info->sysvec;
7989b68618dSPeter Wemm 	if (interp != NULL && brand_info->interp_newpath != NULL)
7994113f8d7SPeter Wemm 		newinterp = brand_info->interp_newpath;
8003ebc1248SPeter Wemm 
80160bb3943SAlan Cox 	/*
80260bb3943SAlan Cox 	 * Avoid a possible deadlock if the current address space is destroyed
80360bb3943SAlan Cox 	 * and that address space maps the locked vnode.  In the common case,
80460bb3943SAlan Cox 	 * the locked vnode's v_usecount is decremented but remains greater
80560bb3943SAlan Cox 	 * than zero.  Consequently, the vnode lock is not needed by vrele().
80660bb3943SAlan Cox 	 * However, in cases where the vnode lock is external, such as nullfs,
80760bb3943SAlan Cox 	 * v_usecount may become zero.
8081dfab802SAlan Cox 	 *
8091dfab802SAlan Cox 	 * The VV_TEXT flag prevents modifications to the executable while
8101dfab802SAlan Cox 	 * the vnode is unlocked.
81160bb3943SAlan Cox 	 */
81222db15c0SAttilio Rao 	VOP_UNLOCK(imgp->vp, 0);
81360bb3943SAlan Cox 
81489b57fcfSKonstantin Belousov 	error = exec_new_vmspace(imgp, sv);
81519059a13SJohn Baldwin 	imgp->proc->p_sysent = sv;
816e1743d02SSøren Schmidt 
817cb05b60aSAttilio Rao 	vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
81889b57fcfSKonstantin Belousov 	if (error)
81989b57fcfSKonstantin Belousov 		return (error);
82060bb3943SAlan Cox 
821e1743d02SSøren Schmidt 	for (i = 0; i < hdr->e_phnum; i++) {
822e1743d02SSøren Schmidt 		switch (phdr[i].p_type) {
823e1743d02SSøren Schmidt 		case PT_LOAD:	/* Loadable segment */
8245b33842aSKonstantin Belousov 			if (phdr[i].p_memsz == 0)
8255b33842aSKonstantin Belousov 				break;
826ed167eaaSKonstantin Belousov 			prot = __elfN(trans_prot)(phdr[i].p_flags);
827e1743d02SSøren Schmidt 
8283ebc1248SPeter Wemm #if defined(__ia64__) && __ELF_WORD_SIZE == 32 && defined(IA32_ME_HARDER)
8293ebc1248SPeter Wemm 			/*
8303ebc1248SPeter Wemm 			 * Some x86 binaries assume read == executable,
8313ebc1248SPeter Wemm 			 * notably the M3 runtime and therefore cvsup
8323ebc1248SPeter Wemm 			 */
8333ebc1248SPeter Wemm 			if (prot & VM_PROT_READ)
8343ebc1248SPeter Wemm 				prot |= VM_PROT_EXECUTE;
8353ebc1248SPeter Wemm #endif
8363ebc1248SPeter Wemm 
837292177e6SAlan Cox 			error = __elfN(load_section)(imgp, phdr[i].p_offset,
838ab02d85fSKonstantin Belousov 			    (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
83981f223caSJake Burkholder 			    phdr[i].p_memsz, phdr[i].p_filesz, prot,
840292177e6SAlan Cox 			    sv->sv_pagesize);
841292177e6SAlan Cox 			if (error != 0)
84260bb3943SAlan Cox 				return (error);
843e1743d02SSøren Schmidt 
844cfaf7e60SDoug Rabson 			/*
845cfaf7e60SDoug Rabson 			 * If this segment contains the program headers,
846cfaf7e60SDoug Rabson 			 * remember their virtual address for the AT_PHDR
847cfaf7e60SDoug Rabson 			 * aux entry. Static binaries don't usually include
848cfaf7e60SDoug Rabson 			 * a PT_PHDR entry.
849cfaf7e60SDoug Rabson 			 */
850cfaf7e60SDoug Rabson 			if (phdr[i].p_offset == 0 &&
851cfaf7e60SDoug Rabson 			    hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
852cfaf7e60SDoug Rabson 				<= phdr[i].p_filesz)
853ab02d85fSKonstantin Belousov 				proghdr = phdr[i].p_vaddr + hdr->e_phoff +
854ab02d85fSKonstantin Belousov 				    et_dyn_addr;
855cfaf7e60SDoug Rabson 
856ab02d85fSKonstantin Belousov 			seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
857cac45152SMatthew Dillon 			seg_size = round_page(phdr[i].p_memsz +
858ab02d85fSKonstantin Belousov 			    phdr[i].p_vaddr + et_dyn_addr - seg_addr);
859cac45152SMatthew Dillon 
860e1743d02SSøren Schmidt 			/*
861920acedbSNathan Whitehorn 			 * Make the largest executable segment the official
862920acedbSNathan Whitehorn 			 * text segment and all others data.
86321c2d047SMatthew Dillon 			 *
86421c2d047SMatthew Dillon 			 * Note that obreak() assumes that data_addr +
86521c2d047SMatthew Dillon 			 * data_size == end of data load area, and the ELF
86621c2d047SMatthew Dillon 			 * file format expects segments to be sorted by
86721c2d047SMatthew Dillon 			 * address.  If multiple data segments exist, the
86821c2d047SMatthew Dillon 			 * last one will be used.
869e1743d02SSøren Schmidt 			 */
870920acedbSNathan Whitehorn 
871920acedbSNathan Whitehorn 			if (phdr[i].p_flags & PF_X && text_size < seg_size) {
8729782ecbaSPeter Wemm 				text_size = seg_size;
8739782ecbaSPeter Wemm 				text_addr = seg_addr;
8749782ecbaSPeter Wemm 			} else {
87521c2d047SMatthew Dillon 				data_size = seg_size;
876cac45152SMatthew Dillon 				data_addr = seg_addr;
877cac45152SMatthew Dillon 			}
87821c2d047SMatthew Dillon 			total_size += seg_size;
87996725dd0SAlexander Kabaev 			break;
88096725dd0SAlexander Kabaev 		case PT_PHDR: 	/* Program header table info */
881ab02d85fSKonstantin Belousov 			proghdr = phdr[i].p_vaddr + et_dyn_addr;
88296725dd0SAlexander Kabaev 			break;
88396725dd0SAlexander Kabaev 		default:
88496725dd0SAlexander Kabaev 			break;
88596725dd0SAlexander Kabaev 		}
88696725dd0SAlexander Kabaev 	}
88796725dd0SAlexander Kabaev 
88896725dd0SAlexander Kabaev 	if (data_addr == 0 && data_size == 0) {
88996725dd0SAlexander Kabaev 		data_addr = text_addr;
89096725dd0SAlexander Kabaev 		data_size = text_size;
89196725dd0SAlexander Kabaev 	}
892cac45152SMatthew Dillon 
893920acedbSNathan Whitehorn 	entry = (u_long)hdr->e_entry + et_dyn_addr;
894920acedbSNathan Whitehorn 
895cac45152SMatthew Dillon 	/*
896cac45152SMatthew Dillon 	 * Check limits.  It should be safe to check the
89796725dd0SAlexander Kabaev 	 * limits after loading the segments since we do
89896725dd0SAlexander Kabaev 	 * not actually fault in all the segments pages.
899cac45152SMatthew Dillon 	 */
90091d5354aSJohn Baldwin 	PROC_LOCK(imgp->proc);
90191d5354aSJohn Baldwin 	if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) ||
902cac45152SMatthew Dillon 	    text_size > maxtsiz ||
9031ba5ad42SEdward Tomasz Napierala 	    total_size > lim_cur(imgp->proc, RLIMIT_VMEM) ||
9041ba5ad42SEdward Tomasz Napierala 	    racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
9051ba5ad42SEdward Tomasz Napierala 	    racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
90691d5354aSJohn Baldwin 		PROC_UNLOCK(imgp->proc);
90760bb3943SAlan Cox 		return (ENOMEM);
908cac45152SMatthew Dillon 	}
909e1743d02SSøren Schmidt 
910292177e6SAlan Cox 	vmspace = imgp->proc->p_vmspace;
911e1743d02SSøren Schmidt 	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
9127cd99438SBruce Evans 	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
913e1743d02SSøren Schmidt 	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
9147cd99438SBruce Evans 	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
915e1743d02SSøren Schmidt 
916c460ac3aSPeter Wemm 	/*
917c460ac3aSPeter Wemm 	 * We load the dynamic linker where a userland call
918c460ac3aSPeter Wemm 	 * to mmap(0, ...) would put it.  The rationale behind this
919c460ac3aSPeter Wemm 	 * calculation is that it leaves room for the heap to grow to
920c460ac3aSPeter Wemm 	 * its maximum allowed size.
921c460ac3aSPeter Wemm 	 */
922292177e6SAlan Cox 	addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(imgp->proc,
923292177e6SAlan Cox 	    RLIMIT_DATA));
92491d5354aSJohn Baldwin 	PROC_UNLOCK(imgp->proc);
925e1743d02SSøren Schmidt 
926ea5a2b2eSSøren Schmidt 	imgp->entry_addr = entry;
927ea5a2b2eSSøren Schmidt 
92860bb3943SAlan Cox 	if (interp != NULL) {
9294113f8d7SPeter Wemm 		int have_interp = FALSE;
93022db15c0SAttilio Rao 		VOP_UNLOCK(imgp->vp, 0);
93160bb3943SAlan Cox 		if (brand_info->emul_path != NULL &&
9329b68618dSPeter Wemm 		    brand_info->emul_path[0] != '\0') {
933a163d034SWarner Losh 			path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
93460bb3943SAlan Cox 			snprintf(path, MAXPATHLEN, "%s%s",
93560bb3943SAlan Cox 			    brand_info->emul_path, interp);
9369b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, path, &addr,
9379b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
938911c2be0SMark Peek 			free(path, M_TEMP);
9399b68618dSPeter Wemm 			if (error == 0)
9404113f8d7SPeter Wemm 				have_interp = TRUE;
9419b68618dSPeter Wemm 		}
9424113f8d7SPeter Wemm 		if (!have_interp && newinterp != NULL) {
9434113f8d7SPeter Wemm 			error = __elfN(load_file)(imgp->proc, newinterp, &addr,
9444113f8d7SPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
945387ad998SKonstantin Belousov 			if (error == 0)
9464113f8d7SPeter Wemm 				have_interp = TRUE;
9474113f8d7SPeter Wemm 		}
9484113f8d7SPeter Wemm 		if (!have_interp) {
9499b68618dSPeter Wemm 			error = __elfN(load_file)(imgp->proc, interp, &addr,
9509b68618dSPeter Wemm 			    &imgp->entry_addr, sv->sv_pagesize);
95160bb3943SAlan Cox 		}
952cb05b60aSAttilio Rao 		vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
9539b68618dSPeter Wemm 		if (error != 0) {
9549b68618dSPeter Wemm 			uprintf("ELF interpreter %s not found\n", interp);
95560bb3943SAlan Cox 			return (error);
956e1743d02SSøren Schmidt 		}
95795c807cfSRobert Watson 	} else
9587564c4adSKonstantin Belousov 		addr = et_dyn_addr;
959ea5a2b2eSSøren Schmidt 
960e1743d02SSøren Schmidt 	/*
961e1743d02SSøren Schmidt 	 * Construct auxargs table (used by the fixup routine)
962e1743d02SSøren Schmidt 	 */
963a163d034SWarner Losh 	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
964e1743d02SSøren Schmidt 	elf_auxargs->execfd = -1;
965e1743d02SSøren Schmidt 	elf_auxargs->phdr = proghdr;
966e1743d02SSøren Schmidt 	elf_auxargs->phent = hdr->e_phentsize;
967e1743d02SSøren Schmidt 	elf_auxargs->phnum = hdr->e_phnum;
968e1743d02SSøren Schmidt 	elf_auxargs->pagesz = PAGE_SIZE;
969e1743d02SSøren Schmidt 	elf_auxargs->base = addr;
970e1743d02SSøren Schmidt 	elf_auxargs->flags = 0;
971e1743d02SSøren Schmidt 	elf_auxargs->entry = entry;
972e1743d02SSøren Schmidt 
973e1743d02SSøren Schmidt 	imgp->auxargs = elf_auxargs;
974e1743d02SSøren Schmidt 	imgp->interpreted = 0;
975a0ea661fSNathan Whitehorn 	imgp->reloc_base = addr;
97632c01de2SDmitry Chagin 	imgp->proc->p_osrel = osrel;
977f231de47SKonstantin Belousov 
978a7cddfedSJake Burkholder 	return (error);
979e1743d02SSøren Schmidt }
980e1743d02SSøren Schmidt 
981a360a43dSJake Burkholder #define	suword __CONCAT(suword, __ELF_WORD_SIZE)
9823ebc1248SPeter Wemm 
9833ebc1248SPeter Wemm int
9843ebc1248SPeter Wemm __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
985e1743d02SSøren Schmidt {
986ecbb00a2SDoug Rabson 	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
987a360a43dSJake Burkholder 	Elf_Addr *base;
988a360a43dSJake Burkholder 	Elf_Addr *pos;
989e1743d02SSøren Schmidt 
990a360a43dSJake Burkholder 	base = (Elf_Addr *)*stack_base;
991610ecfe0SMaxim Sobolev 	pos = base + (imgp->args->argc + imgp->args->envc + 2);
992e1743d02SSøren Schmidt 
99335c2a5a8SWarner Losh 	if (args->execfd != -1)
994e1743d02SSøren Schmidt 		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
995e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
996e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
997e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
998e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
999e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1000e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1001e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_BASE, args->base);
10023ff06357SKonstantin Belousov 	if (imgp->execpathp != 0)
10033ff06357SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1004ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
1005ee235befSKonstantin Belousov 	if (imgp->canary != 0) {
1006ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1007ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1008ee235befSKonstantin Belousov 	}
1009ee235befSKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1010ee235befSKonstantin Belousov 	if (imgp->pagesizes != 0) {
1011ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1012ee235befSKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1013ee235befSKonstantin Belousov 	}
1014*aea81038SKonstantin Belousov 	if (imgp->sysent->sv_timekeep_base != 0) {
1015*aea81038SKonstantin Belousov 		AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1016*aea81038SKonstantin Belousov 		    imgp->sysent->sv_timekeep_base);
1017*aea81038SKonstantin Belousov 	}
101826d8f3e1SKonstantin Belousov 	AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
101926d8f3e1SKonstantin Belousov 	    != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
102026d8f3e1SKonstantin Belousov 	    imgp->sysent->sv_stackprot);
1021e1743d02SSøren Schmidt 	AUXARGS_ENTRY(pos, AT_NULL, 0);
1022e1743d02SSøren Schmidt 
1023e1743d02SSøren Schmidt 	free(imgp->auxargs, M_TEMP);
1024e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1025e1743d02SSøren Schmidt 
10263ebc1248SPeter Wemm 	base--;
1027610ecfe0SMaxim Sobolev 	suword(base, (long)imgp->args->argc);
10283ebc1248SPeter Wemm 	*stack_base = (register_t *)base;
1029a7cddfedSJake Burkholder 	return (0);
1030e1743d02SSøren Schmidt }
1031e1743d02SSøren Schmidt 
1032e1743d02SSøren Schmidt /*
10338c64af4fSJohn Polstra  * Code for generating ELF core dumps.
10348c64af4fSJohn Polstra  */
10358c64af4fSJohn Polstra 
10364d77a549SAlfred Perlstein typedef void (*segment_callback)(vm_map_entry_t, void *);
10370ff27d31SJohn Polstra 
10380ff27d31SJohn Polstra /* Closure for cb_put_phdr(). */
10390ff27d31SJohn Polstra struct phdr_closure {
10400ff27d31SJohn Polstra 	Elf_Phdr *phdr;		/* Program header to fill in */
10410ff27d31SJohn Polstra 	Elf_Off offset;		/* Offset of segment in core file */
10420ff27d31SJohn Polstra };
10430ff27d31SJohn Polstra 
10440ff27d31SJohn Polstra /* Closure for cb_size_segment(). */
10450ff27d31SJohn Polstra struct sseg_closure {
10460ff27d31SJohn Polstra 	int count;		/* Count of writable segments. */
10470ff27d31SJohn Polstra 	size_t size;		/* Total size of all writable segments. */
10480ff27d31SJohn Polstra };
10490ff27d31SJohn Polstra 
10504d77a549SAlfred Perlstein static void cb_put_phdr(vm_map_entry_t, void *);
10514d77a549SAlfred Perlstein static void cb_size_segment(vm_map_entry_t, void *);
1052247aba24SMarcel Moolenaar static void each_writable_segment(struct thread *, segment_callback, void *);
10533ebc1248SPeter Wemm static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
1054e7228204SAlfred Perlstein     int, void *, size_t, gzFile);
1055247aba24SMarcel Moolenaar static void __elfN(puthdr)(struct thread *, void *, size_t *, int);
10563ebc1248SPeter Wemm static void __elfN(putnote)(void *, size_t *, const char *, int,
10574d77a549SAlfred Perlstein     const void *, size_t);
10588c64af4fSJohn Polstra 
1059e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
1060e7228204SAlfred Perlstein extern int compress_user_cores;
1061e7228204SAlfred Perlstein extern int compress_user_cores_gzlevel;
1062e7228204SAlfred Perlstein #endif
1063e7228204SAlfred Perlstein 
1064e7228204SAlfred Perlstein static int
1065e7228204SAlfred Perlstein core_output(struct vnode *vp, void *base, size_t len, off_t offset,
1066e7228204SAlfred Perlstein     struct ucred *active_cred, struct ucred *file_cred,
1067e7228204SAlfred Perlstein     struct thread *td, char *core_buf, gzFile gzfile) {
1068e7228204SAlfred Perlstein 
1069e7228204SAlfred Perlstein 	int error;
1070e7228204SAlfred Perlstein 	if (gzfile) {
1071e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
1072e7228204SAlfred Perlstein 		error = compress_core(gzfile, base, core_buf, len, td);
1073e7228204SAlfred Perlstein #else
1074e7228204SAlfred Perlstein 		panic("shouldn't be here");
1075e7228204SAlfred Perlstein #endif
1076e7228204SAlfred Perlstein 	} else {
1077e7228204SAlfred Perlstein 		error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
1078e7228204SAlfred Perlstein 		    UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1079e7228204SAlfred Perlstein 		    NULL, td);
1080e7228204SAlfred Perlstein 	}
1081e7228204SAlfred Perlstein 	return (error);
1082e7228204SAlfred Perlstein }
1083e7228204SAlfred Perlstein 
10848c64af4fSJohn Polstra int
1085e7228204SAlfred Perlstein __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1086fca666a1SJulian Elischer {
1087247aba24SMarcel Moolenaar 	struct ucred *cred = td->td_ucred;
1088fca666a1SJulian Elischer 	int error = 0;
10890ff27d31SJohn Polstra 	struct sseg_closure seginfo;
10900ff27d31SJohn Polstra 	void *hdr;
10918c64af4fSJohn Polstra 	size_t hdrsize;
10928c64af4fSJohn Polstra 
1093e7228204SAlfred Perlstein 	gzFile gzfile = Z_NULL;
1094e7228204SAlfred Perlstein 	char *core_buf = NULL;
1095e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
1096e7228204SAlfred Perlstein 	char gzopen_flags[8];
1097e7228204SAlfred Perlstein 	char *p;
1098e7228204SAlfred Perlstein 	int doing_compress = flags & IMGACT_CORE_COMPRESS;
1099e7228204SAlfred Perlstein #endif
1100e7228204SAlfred Perlstein 
1101e7228204SAlfred Perlstein 	hdr = NULL;
1102e7228204SAlfred Perlstein 
1103e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
1104e7228204SAlfred Perlstein         if (doing_compress) {
1105e7228204SAlfred Perlstein                 p = gzopen_flags;
1106e7228204SAlfred Perlstein                 *p++ = 'w';
1107e7228204SAlfred Perlstein                 if (compress_user_cores_gzlevel >= 0 &&
1108e7228204SAlfred Perlstein                     compress_user_cores_gzlevel <= 9)
1109e7228204SAlfred Perlstein                         *p++ = '0' + compress_user_cores_gzlevel;
1110e7228204SAlfred Perlstein                 *p = 0;
1111e7228204SAlfred Perlstein                 gzfile = gz_open("", gzopen_flags, vp);
1112e7228204SAlfred Perlstein                 if (gzfile == Z_NULL) {
1113e7228204SAlfred Perlstein                         error = EFAULT;
1114e7228204SAlfred Perlstein                         goto done;
1115e7228204SAlfred Perlstein                 }
1116e7228204SAlfred Perlstein                 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1117e7228204SAlfred Perlstein                 if (!core_buf) {
1118e7228204SAlfred Perlstein                         error = ENOMEM;
1119e7228204SAlfred Perlstein                         goto done;
1120e7228204SAlfred Perlstein                 }
1121e7228204SAlfred Perlstein         }
1122e7228204SAlfred Perlstein #endif
1123e7228204SAlfred Perlstein 
11240ff27d31SJohn Polstra 	/* Size the program segments. */
11250ff27d31SJohn Polstra 	seginfo.count = 0;
11260ff27d31SJohn Polstra 	seginfo.size = 0;
1127247aba24SMarcel Moolenaar 	each_writable_segment(td, cb_size_segment, &seginfo);
11280ff27d31SJohn Polstra 
11290ff27d31SJohn Polstra 	/*
11300ff27d31SJohn Polstra 	 * Calculate the size of the core file header area by making
11310ff27d31SJohn Polstra 	 * a dry run of generating it.  Nothing is written, but the
11320ff27d31SJohn Polstra 	 * size is calculated.
11330ff27d31SJohn Polstra 	 */
11340ff27d31SJohn Polstra 	hdrsize = 0;
1135247aba24SMarcel Moolenaar 	__elfN(puthdr)(td, (void *)NULL, &hdrsize, seginfo.count);
11360ff27d31SJohn Polstra 
1137afcc55f3SEdward Tomasz Napierala #ifdef RACCT
11381ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(td->td_proc);
11391ba5ad42SEdward Tomasz Napierala 	error = racct_add(td->td_proc, RACCT_CORE, hdrsize + seginfo.size);
11401ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(td->td_proc);
11411ba5ad42SEdward Tomasz Napierala 	if (error != 0) {
11421ba5ad42SEdward Tomasz Napierala 		error = EFAULT;
11431ba5ad42SEdward Tomasz Napierala 		goto done;
11441ba5ad42SEdward Tomasz Napierala 	}
1145afcc55f3SEdward Tomasz Napierala #endif
1146fba6b1afSAlfred Perlstein 	if (hdrsize + seginfo.size >= limit) {
1147fba6b1afSAlfred Perlstein 		error = EFAULT;
1148fba6b1afSAlfred Perlstein 		goto done;
1149fba6b1afSAlfred Perlstein 	}
11500ff27d31SJohn Polstra 
11510ff27d31SJohn Polstra 	/*
11520ff27d31SJohn Polstra 	 * Allocate memory for building the header, fill it up,
11530ff27d31SJohn Polstra 	 * and write it out.
11540ff27d31SJohn Polstra 	 */
1155a163d034SWarner Losh 	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
11560ff27d31SJohn Polstra 	if (hdr == NULL) {
1157fba6b1afSAlfred Perlstein 		error = EINVAL;
1158fba6b1afSAlfred Perlstein 		goto done;
11590ff27d31SJohn Polstra 	}
1160e7228204SAlfred Perlstein 	error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
1161e7228204SAlfred Perlstein 	    gzfile);
11620ff27d31SJohn Polstra 
11630ff27d31SJohn Polstra 	/* Write the contents of all of the writable segments. */
11640ff27d31SJohn Polstra 	if (error == 0) {
11650ff27d31SJohn Polstra 		Elf_Phdr *php;
11662b471bc6STim J. Robbins 		off_t offset;
11670ff27d31SJohn Polstra 		int i;
11680ff27d31SJohn Polstra 
11690ff27d31SJohn Polstra 		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
11700ff27d31SJohn Polstra 		offset = hdrsize;
11710ff27d31SJohn Polstra 		for (i = 0; i < seginfo.count; i++) {
1172e7228204SAlfred Perlstein 			error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1173e7228204SAlfred Perlstein 			    php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
11740ff27d31SJohn Polstra 			if (error != 0)
11752b471bc6STim J. Robbins 				break;
11760ff27d31SJohn Polstra 			offset += php->p_filesz;
11770ff27d31SJohn Polstra 			php++;
11780ff27d31SJohn Polstra 		}
11790ff27d31SJohn Polstra 	}
1180e7228204SAlfred Perlstein 	if (error) {
1181e7228204SAlfred Perlstein 		log(LOG_WARNING,
1182e7228204SAlfred Perlstein 		    "Failed to write core file for process %s (error %d)\n",
1183e7228204SAlfred Perlstein 		    curproc->p_comm, error);
1184e7228204SAlfred Perlstein 	}
1185e7228204SAlfred Perlstein 
1186e7228204SAlfred Perlstein done:
1187fba6b1afSAlfred Perlstein #ifdef COMPRESS_USER_CORES
1188e7228204SAlfred Perlstein 	if (core_buf)
1189e7228204SAlfred Perlstein 		free(core_buf, M_TEMP);
1190e7228204SAlfred Perlstein 	if (gzfile)
1191e7228204SAlfred Perlstein 		gzclose(gzfile);
11928b325009SAlfred Perlstein #endif
1193e7228204SAlfred Perlstein 
11940ff27d31SJohn Polstra 	free(hdr, M_TEMP);
11950ff27d31SJohn Polstra 
1196a7cddfedSJake Burkholder 	return (error);
11978c64af4fSJohn Polstra }
11988c64af4fSJohn Polstra 
11990ff27d31SJohn Polstra /*
12000ff27d31SJohn Polstra  * A callback for each_writable_segment() to write out the segment's
12010ff27d31SJohn Polstra  * program header entry.
12020ff27d31SJohn Polstra  */
12030ff27d31SJohn Polstra static void
12040ff27d31SJohn Polstra cb_put_phdr(entry, closure)
12050ff27d31SJohn Polstra 	vm_map_entry_t entry;
12060ff27d31SJohn Polstra 	void *closure;
12070ff27d31SJohn Polstra {
12080ff27d31SJohn Polstra 	struct phdr_closure *phc = (struct phdr_closure *)closure;
12090ff27d31SJohn Polstra 	Elf_Phdr *phdr = phc->phdr;
12100ff27d31SJohn Polstra 
12110ff27d31SJohn Polstra 	phc->offset = round_page(phc->offset);
12120ff27d31SJohn Polstra 
12130ff27d31SJohn Polstra 	phdr->p_type = PT_LOAD;
12140ff27d31SJohn Polstra 	phdr->p_offset = phc->offset;
12150ff27d31SJohn Polstra 	phdr->p_vaddr = entry->start;
12160ff27d31SJohn Polstra 	phdr->p_paddr = 0;
12170ff27d31SJohn Polstra 	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
12180ff27d31SJohn Polstra 	phdr->p_align = PAGE_SIZE;
1219ed167eaaSKonstantin Belousov 	phdr->p_flags = __elfN(untrans_prot)(entry->protection);
12200ff27d31SJohn Polstra 
12210ff27d31SJohn Polstra 	phc->offset += phdr->p_filesz;
12220ff27d31SJohn Polstra 	phc->phdr++;
12230ff27d31SJohn Polstra }
12240ff27d31SJohn Polstra 
12250ff27d31SJohn Polstra /*
12260ff27d31SJohn Polstra  * A callback for each_writable_segment() to gather information about
12270ff27d31SJohn Polstra  * the number of segments and their total size.
12280ff27d31SJohn Polstra  */
12290ff27d31SJohn Polstra static void
12300ff27d31SJohn Polstra cb_size_segment(entry, closure)
12310ff27d31SJohn Polstra 	vm_map_entry_t entry;
12320ff27d31SJohn Polstra 	void *closure;
12330ff27d31SJohn Polstra {
12340ff27d31SJohn Polstra 	struct sseg_closure *ssc = (struct sseg_closure *)closure;
12350ff27d31SJohn Polstra 
12360ff27d31SJohn Polstra 	ssc->count++;
12370ff27d31SJohn Polstra 	ssc->size += entry->end - entry->start;
12380ff27d31SJohn Polstra }
12390ff27d31SJohn Polstra 
12400ff27d31SJohn Polstra /*
12410ff27d31SJohn Polstra  * For each writable segment in the process's memory map, call the given
12420ff27d31SJohn Polstra  * function with a pointer to the map entry and some arbitrary
12430ff27d31SJohn Polstra  * caller-supplied data.
12440ff27d31SJohn Polstra  */
12450ff27d31SJohn Polstra static void
1246247aba24SMarcel Moolenaar each_writable_segment(td, func, closure)
1247247aba24SMarcel Moolenaar 	struct thread *td;
12480ff27d31SJohn Polstra 	segment_callback func;
12490ff27d31SJohn Polstra 	void *closure;
12500ff27d31SJohn Polstra {
1251247aba24SMarcel Moolenaar 	struct proc *p = td->td_proc;
12520ff27d31SJohn Polstra 	vm_map_t map = &p->p_vmspace->vm_map;
12530ff27d31SJohn Polstra 	vm_map_entry_t entry;
1254976a87a2SAlan Cox 	vm_object_t backing_object, object;
1255976a87a2SAlan Cox 	boolean_t ignore_entry;
12560ff27d31SJohn Polstra 
1257976a87a2SAlan Cox 	vm_map_lock_read(map);
12580ff27d31SJohn Polstra 	for (entry = map->header.next; entry != &map->header;
12590ff27d31SJohn Polstra 	    entry = entry->next) {
1260fa7dd9c5SMatthew Dillon 		/*
1261fa7dd9c5SMatthew Dillon 		 * Don't dump inaccessible mappings, deal with legacy
1262fa7dd9c5SMatthew Dillon 		 * coredump mode.
1263fa7dd9c5SMatthew Dillon 		 *
1264fa7dd9c5SMatthew Dillon 		 * Note that read-only segments related to the elf binary
1265fa7dd9c5SMatthew Dillon 		 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1266fa7dd9c5SMatthew Dillon 		 * need to arbitrarily ignore such segments.
1267fa7dd9c5SMatthew Dillon 		 */
1268fa7dd9c5SMatthew Dillon 		if (elf_legacy_coredump) {
1269fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
12700ff27d31SJohn Polstra 				continue;
1271fa7dd9c5SMatthew Dillon 		} else {
1272fa7dd9c5SMatthew Dillon 			if ((entry->protection & VM_PROT_ALL) == 0)
1273fa7dd9c5SMatthew Dillon 				continue;
1274fa7dd9c5SMatthew Dillon 		}
12750ff27d31SJohn Polstra 
12769730a5daSPaul Saab 		/*
1277fa7dd9c5SMatthew Dillon 		 * Dont include memory segment in the coredump if
1278fa7dd9c5SMatthew Dillon 		 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1279fa7dd9c5SMatthew Dillon 		 * madvise(2).  Do not dump submaps (i.e. parts of the
1280fa7dd9c5SMatthew Dillon 		 * kernel map).
12819730a5daSPaul Saab 		 */
1282fa7dd9c5SMatthew Dillon 		if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
12839730a5daSPaul Saab 			continue;
12849730a5daSPaul Saab 
1285976a87a2SAlan Cox 		if ((object = entry->object.vm_object) == NULL)
12860ff27d31SJohn Polstra 			continue;
12870ff27d31SJohn Polstra 
12880ff27d31SJohn Polstra 		/* Ignore memory-mapped devices and such things. */
1289976a87a2SAlan Cox 		VM_OBJECT_LOCK(object);
1290976a87a2SAlan Cox 		while ((backing_object = object->backing_object) != NULL) {
1291976a87a2SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1292976a87a2SAlan Cox 			VM_OBJECT_UNLOCK(object);
1293976a87a2SAlan Cox 			object = backing_object;
1294976a87a2SAlan Cox 		}
1295976a87a2SAlan Cox 		ignore_entry = object->type != OBJT_DEFAULT &&
1296976a87a2SAlan Cox 		    object->type != OBJT_SWAP && object->type != OBJT_VNODE;
1297976a87a2SAlan Cox 		VM_OBJECT_UNLOCK(object);
1298976a87a2SAlan Cox 		if (ignore_entry)
12990ff27d31SJohn Polstra 			continue;
13000ff27d31SJohn Polstra 
13010ff27d31SJohn Polstra 		(*func)(entry, closure);
13020ff27d31SJohn Polstra 	}
1303976a87a2SAlan Cox 	vm_map_unlock_read(map);
13040ff27d31SJohn Polstra }
13050ff27d31SJohn Polstra 
13060ff27d31SJohn Polstra /*
13070ff27d31SJohn Polstra  * Write the core file header to the file, including padding up to
13080ff27d31SJohn Polstra  * the page boundary.
13090ff27d31SJohn Polstra  */
13108c64af4fSJohn Polstra static int
1311e7228204SAlfred Perlstein __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize, gzfile)
1312b40ce416SJulian Elischer 	struct thread *td;
13138c64af4fSJohn Polstra 	struct vnode *vp;
13148c64af4fSJohn Polstra 	struct ucred *cred;
13150ff27d31SJohn Polstra 	int numsegs;
13160ff27d31SJohn Polstra 	size_t hdrsize;
13170ff27d31SJohn Polstra 	void *hdr;
1318e7228204SAlfred Perlstein 	gzFile gzfile;
13198c64af4fSJohn Polstra {
1320911c2be0SMark Peek 	size_t off;
13218c64af4fSJohn Polstra 
13228c64af4fSJohn Polstra 	/* Fill in the header. */
13230ff27d31SJohn Polstra 	bzero(hdr, hdrsize);
13248c64af4fSJohn Polstra 	off = 0;
1325247aba24SMarcel Moolenaar 	__elfN(puthdr)(td, hdr, &off, numsegs);
13268c64af4fSJohn Polstra 
1327e7228204SAlfred Perlstein 	if (!gzfile) {
13288c64af4fSJohn Polstra 		/* Write it to the core file. */
1329a7cddfedSJake Burkholder 		return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
13309ca43589SRobert Watson 			UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL,
13316617724cSJeff Roberson 			td));
1332e7228204SAlfred Perlstein 	} else {
1333e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
1334e7228204SAlfred Perlstein 		if (gzwrite(gzfile, hdr, hdrsize) != hdrsize) {
1335e7228204SAlfred Perlstein 			log(LOG_WARNING,
1336e7228204SAlfred Perlstein 			    "Failed to compress core file header for process"
1337e7228204SAlfred Perlstein 			    " %s.\n", curproc->p_comm);
1338e7228204SAlfred Perlstein 			return (EFAULT);
1339e7228204SAlfred Perlstein 		}
1340e7228204SAlfred Perlstein 		else {
1341e7228204SAlfred Perlstein 			return (0);
1342e7228204SAlfred Perlstein 		}
1343e7228204SAlfred Perlstein #else
1344e7228204SAlfred Perlstein 		panic("shouldn't be here");
1345e7228204SAlfred Perlstein #endif
1346e7228204SAlfred Perlstein 	}
1347dada0278SJohn Polstra }
1348dada0278SJohn Polstra 
1349841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1350841c0c7eSNathan Whitehorn #include <compat/freebsd32/freebsd32.h>
1351841c0c7eSNathan Whitehorn 
135262919d78SPeter Wemm typedef struct prstatus32 elf_prstatus_t;
135362919d78SPeter Wemm typedef struct prpsinfo32 elf_prpsinfo_t;
135462919d78SPeter Wemm typedef struct fpreg32 elf_prfpregset_t;
135562919d78SPeter Wemm typedef struct fpreg32 elf_fpregset_t;
135662919d78SPeter Wemm typedef struct reg32 elf_gregset_t;
13577f08176eSAttilio Rao typedef struct thrmisc32 elf_thrmisc_t;
135862919d78SPeter Wemm #else
135962919d78SPeter Wemm typedef prstatus_t elf_prstatus_t;
136062919d78SPeter Wemm typedef prpsinfo_t elf_prpsinfo_t;
136162919d78SPeter Wemm typedef prfpregset_t elf_prfpregset_t;
136262919d78SPeter Wemm typedef prfpregset_t elf_fpregset_t;
136362919d78SPeter Wemm typedef gregset_t elf_gregset_t;
13647f08176eSAttilio Rao typedef thrmisc_t elf_thrmisc_t;
136562919d78SPeter Wemm #endif
136662919d78SPeter Wemm 
13678c64af4fSJohn Polstra static void
1368247aba24SMarcel Moolenaar __elfN(puthdr)(struct thread *td, void *dst, size_t *off, int numsegs)
13698c64af4fSJohn Polstra {
13708c9b7b2cSMarcel Moolenaar 	struct {
137162919d78SPeter Wemm 		elf_prstatus_t status;
137262919d78SPeter Wemm 		elf_prfpregset_t fpregset;
137362919d78SPeter Wemm 		elf_prpsinfo_t psinfo;
13747f08176eSAttilio Rao 		elf_thrmisc_t thrmisc;
13758c9b7b2cSMarcel Moolenaar 	} *tempdata;
137662919d78SPeter Wemm 	elf_prstatus_t *status;
137762919d78SPeter Wemm 	elf_prfpregset_t *fpregset;
137862919d78SPeter Wemm 	elf_prpsinfo_t *psinfo;
13797f08176eSAttilio Rao 	elf_thrmisc_t *thrmisc;
1380247aba24SMarcel Moolenaar 	struct proc *p;
1381247aba24SMarcel Moolenaar 	struct thread *thr;
13828c9b7b2cSMarcel Moolenaar 	size_t ehoff, noteoff, notesz, phoff;
13838c64af4fSJohn Polstra 
1384247aba24SMarcel Moolenaar 	p = td->td_proc;
1385247aba24SMarcel Moolenaar 
13868c64af4fSJohn Polstra 	ehoff = *off;
13878c64af4fSJohn Polstra 	*off += sizeof(Elf_Ehdr);
13888c64af4fSJohn Polstra 
13898c64af4fSJohn Polstra 	phoff = *off;
13900ff27d31SJohn Polstra 	*off += (numsegs + 1) * sizeof(Elf_Phdr);
13918c64af4fSJohn Polstra 
13928c64af4fSJohn Polstra 	noteoff = *off;
13938c9b7b2cSMarcel Moolenaar 	/*
13948c9b7b2cSMarcel Moolenaar 	 * Don't allocate space for the notes if we're just calculating
13958c9b7b2cSMarcel Moolenaar 	 * the size of the header. We also don't collect the data.
13968c9b7b2cSMarcel Moolenaar 	 */
13978c9b7b2cSMarcel Moolenaar 	if (dst != NULL) {
13988c9b7b2cSMarcel Moolenaar 		tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO|M_WAITOK);
13998c9b7b2cSMarcel Moolenaar 		status = &tempdata->status;
14008c9b7b2cSMarcel Moolenaar 		fpregset = &tempdata->fpregset;
14018c9b7b2cSMarcel Moolenaar 		psinfo = &tempdata->psinfo;
14027f08176eSAttilio Rao 		thrmisc = &tempdata->thrmisc;
14038c9b7b2cSMarcel Moolenaar 	} else {
14048c9b7b2cSMarcel Moolenaar 		tempdata = NULL;
14058c9b7b2cSMarcel Moolenaar 		status = NULL;
14068c9b7b2cSMarcel Moolenaar 		fpregset = NULL;
14078c9b7b2cSMarcel Moolenaar 		psinfo = NULL;
14087f08176eSAttilio Rao 		thrmisc = NULL;
14098c9b7b2cSMarcel Moolenaar 	}
14108c9b7b2cSMarcel Moolenaar 
14118c9b7b2cSMarcel Moolenaar 	if (dst != NULL) {
14128c9b7b2cSMarcel Moolenaar 		psinfo->pr_version = PRPSINFO_VERSION;
141362919d78SPeter Wemm 		psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1414ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
14158c9b7b2cSMarcel Moolenaar 		/*
14168c9b7b2cSMarcel Moolenaar 		 * XXX - We don't fill in the command line arguments properly
14178c9b7b2cSMarcel Moolenaar 		 * yet.
14188c9b7b2cSMarcel Moolenaar 		 */
1419ccd3953eSJohn Baldwin 		strlcpy(psinfo->pr_psargs, p->p_comm,
14208c9b7b2cSMarcel Moolenaar 		    sizeof(psinfo->pr_psargs));
14218c9b7b2cSMarcel Moolenaar 	}
14228c9b7b2cSMarcel Moolenaar 	__elfN(putnote)(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
14238c9b7b2cSMarcel Moolenaar 	    sizeof *psinfo);
14248c9b7b2cSMarcel Moolenaar 
14258c9b7b2cSMarcel Moolenaar 	/*
14261f7a1baaSMarcel Moolenaar 	 * To have the debugger select the right thread (LWP) as the initial
14271f7a1baaSMarcel Moolenaar 	 * thread, we dump the state of the thread passed to us in td first.
14281f7a1baaSMarcel Moolenaar 	 * This is the thread that causes the core dump and thus likely to
14291f7a1baaSMarcel Moolenaar 	 * be the right thread one wants to have selected in the debugger.
14308c9b7b2cSMarcel Moolenaar 	 */
1431247aba24SMarcel Moolenaar 	thr = td;
1432247aba24SMarcel Moolenaar 	while (thr != NULL) {
14338c9b7b2cSMarcel Moolenaar 		if (dst != NULL) {
14348c9b7b2cSMarcel Moolenaar 			status->pr_version = PRSTATUS_VERSION;
143562919d78SPeter Wemm 			status->pr_statussz = sizeof(elf_prstatus_t);
143662919d78SPeter Wemm 			status->pr_gregsetsz = sizeof(elf_gregset_t);
143762919d78SPeter Wemm 			status->pr_fpregsetsz = sizeof(elf_fpregset_t);
14388c9b7b2cSMarcel Moolenaar 			status->pr_osreldate = osreldate;
14398c9b7b2cSMarcel Moolenaar 			status->pr_cursig = p->p_sig;
14401f7a1baaSMarcel Moolenaar 			status->pr_pid = thr->td_tid;
1441841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
144262919d78SPeter Wemm 			fill_regs32(thr, &status->pr_reg);
144362919d78SPeter Wemm 			fill_fpregs32(thr, fpregset);
144462919d78SPeter Wemm #else
14458c9b7b2cSMarcel Moolenaar 			fill_regs(thr, &status->pr_reg);
14468c9b7b2cSMarcel Moolenaar 			fill_fpregs(thr, fpregset);
144762919d78SPeter Wemm #endif
14487f08176eSAttilio Rao 			memset(&thrmisc->_pad, 0, sizeof (thrmisc->_pad));
14497f08176eSAttilio Rao 			strcpy(thrmisc->pr_tname, thr->td_name);
14508c9b7b2cSMarcel Moolenaar 		}
14513ebc1248SPeter Wemm 		__elfN(putnote)(dst, off, "FreeBSD", NT_PRSTATUS, status,
14528c64af4fSJohn Polstra 		    sizeof *status);
14533ebc1248SPeter Wemm 		__elfN(putnote)(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
14548c64af4fSJohn Polstra 		    sizeof *fpregset);
14557f08176eSAttilio Rao 		__elfN(putnote)(dst, off, "FreeBSD", NT_THRMISC, thrmisc,
14567f08176eSAttilio Rao 		    sizeof *thrmisc);
14574da47b2fSMarcel Moolenaar 		/*
14584da47b2fSMarcel Moolenaar 		 * Allow for MD specific notes, as well as any MD
14594da47b2fSMarcel Moolenaar 		 * specific preparations for writing MI notes.
14604da47b2fSMarcel Moolenaar 		 */
14614da47b2fSMarcel Moolenaar 		__elfN(dump_thread)(thr, dst, off);
1462247aba24SMarcel Moolenaar 
1463247aba24SMarcel Moolenaar 		thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
14648c9b7b2cSMarcel Moolenaar 		    TAILQ_NEXT(thr, td_plist);
1465247aba24SMarcel Moolenaar 		if (thr == td)
14668c9b7b2cSMarcel Moolenaar 			thr = TAILQ_NEXT(thr, td_plist);
1467247aba24SMarcel Moolenaar 	}
14688c9b7b2cSMarcel Moolenaar 
14698c64af4fSJohn Polstra 	notesz = *off - noteoff;
14708c64af4fSJohn Polstra 
14718c9b7b2cSMarcel Moolenaar 	if (dst != NULL)
14728c9b7b2cSMarcel Moolenaar 		free(tempdata, M_TEMP);
14738c9b7b2cSMarcel Moolenaar 
14740ff27d31SJohn Polstra 	/* Align up to a page boundary for the program segments. */
14758c64af4fSJohn Polstra 	*off = round_page(*off);
14768c64af4fSJohn Polstra 
14778c64af4fSJohn Polstra 	if (dst != NULL) {
14788c64af4fSJohn Polstra 		Elf_Ehdr *ehdr;
14798c64af4fSJohn Polstra 		Elf_Phdr *phdr;
14800ff27d31SJohn Polstra 		struct phdr_closure phc;
14818c64af4fSJohn Polstra 
14828c64af4fSJohn Polstra 		/*
14838c64af4fSJohn Polstra 		 * Fill in the ELF header.
14848c64af4fSJohn Polstra 		 */
14858c64af4fSJohn Polstra 		ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
14868c64af4fSJohn Polstra 		ehdr->e_ident[EI_MAG0] = ELFMAG0;
14878c64af4fSJohn Polstra 		ehdr->e_ident[EI_MAG1] = ELFMAG1;
14888c64af4fSJohn Polstra 		ehdr->e_ident[EI_MAG2] = ELFMAG2;
14898c64af4fSJohn Polstra 		ehdr->e_ident[EI_MAG3] = ELFMAG3;
14908c64af4fSJohn Polstra 		ehdr->e_ident[EI_CLASS] = ELF_CLASS;
14918c64af4fSJohn Polstra 		ehdr->e_ident[EI_DATA] = ELF_DATA;
14928c64af4fSJohn Polstra 		ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1493c815a20cSDavid E. O'Brien 		ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1494c815a20cSDavid E. O'Brien 		ehdr->e_ident[EI_ABIVERSION] = 0;
14958c64af4fSJohn Polstra 		ehdr->e_ident[EI_PAD] = 0;
14968c64af4fSJohn Polstra 		ehdr->e_type = ET_CORE;
1497841c0c7eSNathan Whitehorn #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1498841c0c7eSNathan Whitehorn 		ehdr->e_machine = ELF_ARCH32;
149962919d78SPeter Wemm #else
15008c64af4fSJohn Polstra 		ehdr->e_machine = ELF_ARCH;
150162919d78SPeter Wemm #endif
15028c64af4fSJohn Polstra 		ehdr->e_version = EV_CURRENT;
15038c64af4fSJohn Polstra 		ehdr->e_entry = 0;
15048c64af4fSJohn Polstra 		ehdr->e_phoff = phoff;
15058c64af4fSJohn Polstra 		ehdr->e_flags = 0;
15068c64af4fSJohn Polstra 		ehdr->e_ehsize = sizeof(Elf_Ehdr);
15078c64af4fSJohn Polstra 		ehdr->e_phentsize = sizeof(Elf_Phdr);
15080ff27d31SJohn Polstra 		ehdr->e_phnum = numsegs + 1;
15098c64af4fSJohn Polstra 		ehdr->e_shentsize = sizeof(Elf_Shdr);
15108c64af4fSJohn Polstra 		ehdr->e_shnum = 0;
15118c64af4fSJohn Polstra 		ehdr->e_shstrndx = SHN_UNDEF;
15128c64af4fSJohn Polstra 
15138c64af4fSJohn Polstra 		/*
15148c64af4fSJohn Polstra 		 * Fill in the program header entries.
15158c64af4fSJohn Polstra 		 */
15168c64af4fSJohn Polstra 		phdr = (Elf_Phdr *)((char *)dst + phoff);
15178c64af4fSJohn Polstra 
15188c64af4fSJohn Polstra 		/* The note segement. */
15198c64af4fSJohn Polstra 		phdr->p_type = PT_NOTE;
15208c64af4fSJohn Polstra 		phdr->p_offset = noteoff;
15218c64af4fSJohn Polstra 		phdr->p_vaddr = 0;
15228c64af4fSJohn Polstra 		phdr->p_paddr = 0;
15238c64af4fSJohn Polstra 		phdr->p_filesz = notesz;
15248c64af4fSJohn Polstra 		phdr->p_memsz = 0;
15258c64af4fSJohn Polstra 		phdr->p_flags = 0;
15268c64af4fSJohn Polstra 		phdr->p_align = 0;
15278c64af4fSJohn Polstra 		phdr++;
15288c64af4fSJohn Polstra 
15290ff27d31SJohn Polstra 		/* All the writable segments from the program. */
15300ff27d31SJohn Polstra 		phc.phdr = phdr;
15310ff27d31SJohn Polstra 		phc.offset = *off;
1532247aba24SMarcel Moolenaar 		each_writable_segment(td, cb_put_phdr, &phc);
15338c64af4fSJohn Polstra 	}
15348c64af4fSJohn Polstra }
15358c64af4fSJohn Polstra 
15368c64af4fSJohn Polstra static void
15373ebc1248SPeter Wemm __elfN(putnote)(void *dst, size_t *off, const char *name, int type,
15388c64af4fSJohn Polstra     const void *desc, size_t descsz)
15398c64af4fSJohn Polstra {
15408c64af4fSJohn Polstra 	Elf_Note note;
15418c64af4fSJohn Polstra 
15428c64af4fSJohn Polstra 	note.n_namesz = strlen(name) + 1;
15438c64af4fSJohn Polstra 	note.n_descsz = descsz;
15448c64af4fSJohn Polstra 	note.n_type = type;
15458c64af4fSJohn Polstra 	if (dst != NULL)
15468c64af4fSJohn Polstra 		bcopy(&note, (char *)dst + *off, sizeof note);
15478c64af4fSJohn Polstra 	*off += sizeof note;
15488c64af4fSJohn Polstra 	if (dst != NULL)
15498c64af4fSJohn Polstra 		bcopy(name, (char *)dst + *off, note.n_namesz);
15508c64af4fSJohn Polstra 	*off += roundup2(note.n_namesz, sizeof(Elf_Size));
15518c64af4fSJohn Polstra 	if (dst != NULL)
15528c64af4fSJohn Polstra 		bcopy(desc, (char *)dst + *off, note.n_descsz);
15538c64af4fSJohn Polstra 	*off += roundup2(note.n_descsz, sizeof(Elf_Size));
15548c64af4fSJohn Polstra }
15558c64af4fSJohn Polstra 
155632c01de2SDmitry Chagin static boolean_t
15571a9c7decSKonstantin Belousov __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
15581a9c7decSKonstantin Belousov     int32_t *osrel, const Elf_Phdr *pnote)
155932c01de2SDmitry Chagin {
1560267c52fcSKonstantin Belousov 	const Elf_Note *note, *note0, *note_end;
156132c01de2SDmitry Chagin 	const char *note_name;
156232c01de2SDmitry Chagin 	int i;
156332c01de2SDmitry Chagin 
156432c01de2SDmitry Chagin 	if (pnote == NULL || pnote->p_offset >= PAGE_SIZE ||
156532c01de2SDmitry Chagin 	    pnote->p_offset + pnote->p_filesz >= PAGE_SIZE)
156632c01de2SDmitry Chagin 		return (FALSE);
156732c01de2SDmitry Chagin 
1568267c52fcSKonstantin Belousov 	note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
156932c01de2SDmitry Chagin 	note_end = (const Elf_Note *)(imgp->image_header +
157032c01de2SDmitry Chagin 	    pnote->p_offset + pnote->p_filesz);
1571267c52fcSKonstantin Belousov 	for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
1572267c52fcSKonstantin Belousov 		if (!aligned(note, Elf32_Addr))
1573267c52fcSKonstantin Belousov 			return (FALSE);
157432c01de2SDmitry Chagin 		if (note->n_namesz != checknote->hdr.n_namesz ||
157532c01de2SDmitry Chagin 		    note->n_descsz != checknote->hdr.n_descsz ||
157632c01de2SDmitry Chagin 		    note->n_type != checknote->hdr.n_type)
157732c01de2SDmitry Chagin 			goto nextnote;
157832c01de2SDmitry Chagin 		note_name = (const char *)(note + 1);
157932c01de2SDmitry Chagin 		if (strncmp(checknote->vendor, note_name,
158032c01de2SDmitry Chagin 		    checknote->hdr.n_namesz) != 0)
158132c01de2SDmitry Chagin 			goto nextnote;
158232c01de2SDmitry Chagin 
158332c01de2SDmitry Chagin 		/*
158432c01de2SDmitry Chagin 		 * Fetch the osreldate for binary
158532c01de2SDmitry Chagin 		 * from the ELF OSABI-note if necessary.
158632c01de2SDmitry Chagin 		 */
158789ffc202SBjoern A. Zeeb 		if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
158889ffc202SBjoern A. Zeeb 		    checknote->trans_osrel != NULL)
158989ffc202SBjoern A. Zeeb 			return (checknote->trans_osrel(note, osrel));
159032c01de2SDmitry Chagin 		return (TRUE);
159132c01de2SDmitry Chagin 
159232c01de2SDmitry Chagin nextnote:
159332c01de2SDmitry Chagin 		note = (const Elf_Note *)((const char *)(note + 1) +
159432c01de2SDmitry Chagin 		    roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
159532c01de2SDmitry Chagin 		    roundup2(note->n_descsz, sizeof(Elf32_Addr)));
159632c01de2SDmitry Chagin 	}
159732c01de2SDmitry Chagin 
159832c01de2SDmitry Chagin 	return (FALSE);
159932c01de2SDmitry Chagin }
160032c01de2SDmitry Chagin 
160132c01de2SDmitry Chagin /*
16021a9c7decSKonstantin Belousov  * Try to find the appropriate ABI-note section for checknote,
16031a9c7decSKonstantin Belousov  * fetch the osreldate for binary from the ELF OSABI-note. Only the
16041a9c7decSKonstantin Belousov  * first page of the image is searched, the same as for headers.
16051a9c7decSKonstantin Belousov  */
16061a9c7decSKonstantin Belousov static boolean_t
16071a9c7decSKonstantin Belousov __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
16081a9c7decSKonstantin Belousov     int32_t *osrel)
16091a9c7decSKonstantin Belousov {
16101a9c7decSKonstantin Belousov 	const Elf_Phdr *phdr;
16111a9c7decSKonstantin Belousov 	const Elf_Ehdr *hdr;
16121a9c7decSKonstantin Belousov 	int i;
16131a9c7decSKonstantin Belousov 
16141a9c7decSKonstantin Belousov 	hdr = (const Elf_Ehdr *)imgp->image_header;
16151a9c7decSKonstantin Belousov 	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
16161a9c7decSKonstantin Belousov 
16171a9c7decSKonstantin Belousov 	for (i = 0; i < hdr->e_phnum; i++) {
16181a9c7decSKonstantin Belousov 		if (phdr[i].p_type == PT_NOTE &&
16191a9c7decSKonstantin Belousov 		    __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
16201a9c7decSKonstantin Belousov 			return (TRUE);
16211a9c7decSKonstantin Belousov 	}
16221a9c7decSKonstantin Belousov 	return (FALSE);
16231a9c7decSKonstantin Belousov 
16241a9c7decSKonstantin Belousov }
16251a9c7decSKonstantin Belousov 
16261a9c7decSKonstantin Belousov /*
1627e1743d02SSøren Schmidt  * Tell kern_execve.c about it, with a little help from the linker.
1628e1743d02SSøren Schmidt  */
1629a360a43dSJake Burkholder static struct execsw __elfN(execsw) = {
1630a360a43dSJake Burkholder 	__CONCAT(exec_, __elfN(imgact)),
1631a360a43dSJake Burkholder 	__XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
1632a360a43dSJake Burkholder };
1633a360a43dSJake Burkholder EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
1634e7228204SAlfred Perlstein 
1635e7228204SAlfred Perlstein #ifdef COMPRESS_USER_CORES
1636e7228204SAlfred Perlstein /*
1637e7228204SAlfred Perlstein  * Compress and write out a core segment for a user process.
1638e7228204SAlfred Perlstein  *
1639e7228204SAlfred Perlstein  * 'inbuf' is the starting address of a VM segment in the process' address
1640e7228204SAlfred Perlstein  * space that is to be compressed and written out to the core file.  'dest_buf'
1641e7228204SAlfred Perlstein  * is a buffer in the kernel's address space.  The segment is copied from
1642e7228204SAlfred Perlstein  * 'inbuf' to 'dest_buf' first before being processed by the compression
1643e7228204SAlfred Perlstein  * routine gzwrite().  This copying is necessary because the content of the VM
1644e7228204SAlfred Perlstein  * segment may change between the compression pass and the crc-computation pass
1645e7228204SAlfred Perlstein  * in gzwrite().  This is because realtime threads may preempt the UNIX kernel.
1646e7228204SAlfred Perlstein  */
1647e7228204SAlfred Perlstein static int
1648e7228204SAlfred Perlstein compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
1649e7228204SAlfred Perlstein     struct thread *td)
1650e7228204SAlfred Perlstein {
1651e7228204SAlfred Perlstein 	int len_compressed;
1652e7228204SAlfred Perlstein 	int error = 0;
1653e7228204SAlfred Perlstein 	unsigned int chunk_len;
1654e7228204SAlfred Perlstein 
1655e7228204SAlfred Perlstein 	while (len) {
1656e7228204SAlfred Perlstein 		chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
1657e7228204SAlfred Perlstein 		copyin(inbuf, dest_buf, chunk_len);
1658e7228204SAlfred Perlstein 		len_compressed = gzwrite(file, dest_buf, chunk_len);
1659e7228204SAlfred Perlstein 
1660e7228204SAlfred Perlstein 		EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
1661e7228204SAlfred Perlstein 
1662e7228204SAlfred Perlstein 		if ((unsigned int)len_compressed != chunk_len) {
1663e7228204SAlfred Perlstein 			log(LOG_WARNING,
1664e7228204SAlfred Perlstein 			    "compress_core: length mismatch (0x%x returned, "
1665e7228204SAlfred Perlstein 			    "0x%x expected)\n", len_compressed, chunk_len);
1666e7228204SAlfred Perlstein 			EVENTHANDLER_INVOKE(app_coredump_error, td,
1667e7228204SAlfred Perlstein 			    "compress_core: length mismatch %x -> %x",
1668e7228204SAlfred Perlstein 			    chunk_len, len_compressed);
1669e7228204SAlfred Perlstein 			error = EFAULT;
1670e7228204SAlfred Perlstein 			break;
1671e7228204SAlfred Perlstein 		}
1672e7228204SAlfred Perlstein 		inbuf += chunk_len;
1673e7228204SAlfred Perlstein 		len -= chunk_len;
167408b163faSMatthew D Fleming 		maybe_yield();
1675e7228204SAlfred Perlstein 	}
1676e7228204SAlfred Perlstein 
1677e7228204SAlfred Perlstein 	return (error);
1678e7228204SAlfred Perlstein }
1679e7228204SAlfred Perlstein #endif /* COMPRESS_USER_CORES */
1680ed167eaaSKonstantin Belousov 
1681ed167eaaSKonstantin Belousov static vm_prot_t
1682ed167eaaSKonstantin Belousov __elfN(trans_prot)(Elf_Word flags)
1683ed167eaaSKonstantin Belousov {
1684ed167eaaSKonstantin Belousov 	vm_prot_t prot;
1685ed167eaaSKonstantin Belousov 
1686ed167eaaSKonstantin Belousov 	prot = 0;
1687ed167eaaSKonstantin Belousov 	if (flags & PF_X)
1688ed167eaaSKonstantin Belousov 		prot |= VM_PROT_EXECUTE;
1689ed167eaaSKonstantin Belousov 	if (flags & PF_W)
1690ed167eaaSKonstantin Belousov 		prot |= VM_PROT_WRITE;
1691ed167eaaSKonstantin Belousov 	if (flags & PF_R)
1692ed167eaaSKonstantin Belousov 		prot |= VM_PROT_READ;
1693676eda08SMarcel Moolenaar #if __ELF_WORD_SIZE == 32
1694676eda08SMarcel Moolenaar #if defined(__amd64__) || defined(__ia64__)
1695126b36a2SKonstantin Belousov 	if (i386_read_exec && (flags & PF_R))
1696676eda08SMarcel Moolenaar 		prot |= VM_PROT_EXECUTE;
1697676eda08SMarcel Moolenaar #endif
1698676eda08SMarcel Moolenaar #endif
1699ed167eaaSKonstantin Belousov 	return (prot);
1700ed167eaaSKonstantin Belousov }
1701ed167eaaSKonstantin Belousov 
1702ed167eaaSKonstantin Belousov static Elf_Word
1703ed167eaaSKonstantin Belousov __elfN(untrans_prot)(vm_prot_t prot)
1704ed167eaaSKonstantin Belousov {
1705ed167eaaSKonstantin Belousov 	Elf_Word flags;
1706ed167eaaSKonstantin Belousov 
1707ed167eaaSKonstantin Belousov 	flags = 0;
1708ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_EXECUTE)
1709ed167eaaSKonstantin Belousov 		flags |= PF_X;
1710ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_READ)
1711ed167eaaSKonstantin Belousov 		flags |= PF_R;
1712ed167eaaSKonstantin Belousov 	if (prot & VM_PROT_WRITE)
1713ed167eaaSKonstantin Belousov 		flags |= PF_W;
1714ed167eaaSKonstantin Belousov 	return (flags);
1715ed167eaaSKonstantin Belousov }
1716