Home | History | Annotate | Line # | Download | only in kern
exec_elf.c revision 1.70.2.8
      1  1.70.2.8     skrll /*	$NetBSD: exec_elf.c,v 1.70.2.8 2017/08/28 17:53:07 skrll Exp $	*/
      2      1.10        ad 
      3      1.10        ad /*-
      4  1.70.2.4     skrll  * Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc.
      5      1.10        ad  * All rights reserved.
      6      1.10        ad  *
      7      1.10        ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.70.2.4     skrll  * by Christos Zoulas and Maxime Villard.
      9      1.10        ad  *
     10      1.10        ad  * Redistribution and use in source and binary forms, with or without
     11      1.10        ad  * modification, are permitted provided that the following conditions
     12      1.10        ad  * are met:
     13      1.10        ad  * 1. Redistributions of source code must retain the above copyright
     14      1.10        ad  *    notice, this list of conditions and the following disclaimer.
     15      1.10        ad  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.10        ad  *    notice, this list of conditions and the following disclaimer in the
     17      1.10        ad  *    documentation and/or other materials provided with the distribution.
     18      1.10        ad  *
     19      1.10        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20      1.10        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21      1.10        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22      1.10        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23      1.10        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24      1.10        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25      1.10        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26      1.10        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27      1.10        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28      1.10        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29      1.10        ad  * POSSIBILITY OF SUCH DAMAGE.
     30      1.10        ad  */
     31       1.1      fvdl 
     32       1.1      fvdl /*
     33      1.10        ad  * Copyright (c) 1996 Christopher G. Demetriou
     34       1.1      fvdl  * All rights reserved.
     35       1.1      fvdl  *
     36       1.1      fvdl  * Redistribution and use in source and binary forms, with or without
     37       1.1      fvdl  * modification, are permitted provided that the following conditions
     38       1.1      fvdl  * are met:
     39       1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     40       1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     41       1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     42       1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     43       1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     44       1.1      fvdl  * 3. The name of the author may not be used to endorse or promote products
     45       1.1      fvdl  *    derived from this software without specific prior written permission
     46       1.1      fvdl  *
     47       1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     48       1.1      fvdl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     49       1.1      fvdl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     50       1.1      fvdl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     51       1.1      fvdl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     52       1.1      fvdl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     53       1.1      fvdl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     54       1.1      fvdl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     55       1.1      fvdl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     56       1.1      fvdl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     57       1.1      fvdl  */
     58       1.1      fvdl 
     59      1.10        ad #include <sys/cdefs.h>
     60  1.70.2.8     skrll __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.70.2.8 2017/08/28 17:53:07 skrll Exp $");
     61      1.10        ad 
     62      1.10        ad #ifdef _KERNEL_OPT
     63      1.10        ad #include "opt_pax.h"
     64      1.10        ad #endif /* _KERNEL_OPT */
     65      1.10        ad 
     66       1.1      fvdl #include <sys/param.h>
     67       1.1      fvdl #include <sys/proc.h>
     68      1.10        ad #include <sys/kmem.h>
     69       1.1      fvdl #include <sys/namei.h>
     70       1.1      fvdl #include <sys/vnode.h>
     71       1.1      fvdl #include <sys/exec.h>
     72       1.1      fvdl #include <sys/exec_elf.h>
     73       1.8  christos #include <sys/syscall.h>
     74       1.8  christos #include <sys/signalvar.h>
     75      1.10        ad #include <sys/mount.h>
     76      1.10        ad #include <sys/stat.h>
     77      1.10        ad #include <sys/kauth.h>
     78      1.10        ad #include <sys/bitops.h>
     79      1.10        ad 
     80      1.10        ad #include <sys/cpu.h>
     81      1.10        ad #include <machine/reg.h>
     82       1.1      fvdl 
     83      1.10        ad #include <compat/common/compat_util.h>
     84       1.1      fvdl 
     85      1.10        ad #include <sys/pax.h>
     86      1.37    martin #include <uvm/uvm_param.h>
     87       1.1      fvdl 
     88      1.10        ad extern struct emul emul_netbsd;
     89       1.1      fvdl 
     90      1.10        ad #define elf_check_header	ELFNAME(check_header)
     91      1.10        ad #define elf_copyargs		ELFNAME(copyargs)
     92      1.65      maxv #define elf_load_interp		ELFNAME(load_interp)
     93      1.10        ad #define elf_load_psection	ELFNAME(load_psection)
     94      1.10        ad #define exec_elf_makecmds	ELFNAME2(exec,makecmds)
     95      1.10        ad #define netbsd_elf_signature	ELFNAME2(netbsd,signature)
     96  1.70.2.8     skrll #define netbsd_elf_note       	ELFNAME2(netbsd,note)
     97      1.10        ad #define netbsd_elf_probe	ELFNAME2(netbsd,probe)
     98      1.10        ad #define	coredump		ELFNAMEEND(coredump)
     99      1.35      matt #define	elf_free_emul_arg	ELFNAME(free_emul_arg)
    100       1.1      fvdl 
    101      1.58      maxv static int
    102      1.65      maxv elf_load_interp(struct lwp *, struct exec_package *, char *,
    103      1.58      maxv     struct exec_vmcmd_set *, u_long *, Elf_Addr *);
    104      1.58      maxv static void
    105      1.58      maxv elf_load_psection(struct exec_vmcmd_set *, struct vnode *, const Elf_Phdr *,
    106      1.64      maxv     Elf_Addr *, u_long *, int);
    107       1.6  christos 
    108      1.10        ad int	netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
    109  1.70.2.8     skrll int	netbsd_elf_note(struct exec_package *, const Elf_Nhdr *, const char *,
    110  1.70.2.8     skrll 	    const char *);
    111      1.10        ad int	netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
    112      1.10        ad 	    vaddr_t *);
    113       1.1      fvdl 
    114      1.35      matt static void	elf_free_emul_arg(void *);
    115      1.35      matt 
    116  1.70.2.7     skrll #ifdef DEBUG_ELF
    117  1.70.2.7     skrll #define DPRINTF(a, ...)	printf("%s: " a "\n", __func__, ##__VA_ARGS__)
    118  1.70.2.7     skrll #else
    119  1.70.2.7     skrll #define DPRINTF(a, ...)
    120  1.70.2.7     skrll #endif
    121  1.70.2.7     skrll 
    122      1.10        ad /* round up and down to page boundaries. */
    123      1.10        ad #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
    124      1.10        ad #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
    125       1.1      fvdl 
    126      1.10        ad static void
    127  1.70.2.4     skrll elf_placedynexec(struct exec_package *epp, Elf_Ehdr *eh, Elf_Phdr *ph)
    128      1.10        ad {
    129      1.19  drochner 	Elf_Addr align, offset;
    130      1.19  drochner 	int i;
    131      1.19  drochner 
    132      1.19  drochner 	for (align = i = 0; i < eh->e_phnum; i++)
    133      1.19  drochner 		if (ph[i].p_type == PT_LOAD && ph[i].p_align > align)
    134      1.19  drochner 			align = ph[i].p_align;
    135      1.10        ad 
    136  1.70.2.6     skrll 	offset = (Elf_Addr)pax_aslr_exec_offset(epp, align);
    137  1.70.2.8     skrll 	if (offset < epp->ep_vm_minaddr)
    138  1.70.2.8     skrll 		offset = roundup(epp->ep_vm_minaddr, align);
    139  1.70.2.8     skrll 	KASSERT((offset & (align - 1)) == 0);
    140      1.32   reinoud 
    141      1.10        ad 	for (i = 0; i < eh->e_phnum; i++)
    142      1.18  christos 		ph[i].p_vaddr += offset;
    143      1.63      matt 	epp->ep_entryoffset = offset;
    144      1.16  christos 	eh->e_entry += offset;
    145      1.10        ad }
    146       1.8  christos 
    147       1.8  christos /*
    148       1.1      fvdl  * Copy arguments onto the stack in the normal way, but add some
    149       1.1      fvdl  * extra information in case of dynamic binding.
    150       1.1      fvdl  */
    151      1.10        ad int
    152      1.10        ad elf_copyargs(struct lwp *l, struct exec_package *pack,
    153      1.10        ad     struct ps_strings *arginfo, char **stackp, void *argp)
    154       1.1      fvdl {
    155      1.10        ad 	size_t len, vlen;
    156      1.10        ad 	AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname;
    157       1.1      fvdl 	struct elf_args *ap;
    158      1.10        ad 	int error;
    159       1.1      fvdl 
    160      1.10        ad 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
    161      1.10        ad 		return error;
    162      1.10        ad 
    163      1.10        ad 	a = ai;
    164      1.10        ad 	execname = NULL;
    165       1.1      fvdl 
    166  1.70.2.1     skrll 	memset(ai, 0, sizeof(ai));
    167  1.70.2.1     skrll 
    168       1.1      fvdl 	/*
    169       1.1      fvdl 	 * Push extra arguments on the stack needed by dynamically
    170       1.1      fvdl 	 * linked binaries
    171       1.1      fvdl 	 */
    172      1.10        ad 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
    173      1.10        ad 		struct vattr *vap = pack->ep_vap;
    174      1.10        ad 
    175      1.10        ad 		a->a_type = AT_PHDR;
    176      1.10        ad 		a->a_v = ap->arg_phaddr;
    177      1.10        ad 		a++;
    178      1.10        ad 
    179      1.10        ad 		a->a_type = AT_PHENT;
    180      1.10        ad 		a->a_v = ap->arg_phentsize;
    181      1.10        ad 		a++;
    182      1.10        ad 
    183      1.10        ad 		a->a_type = AT_PHNUM;
    184      1.10        ad 		a->a_v = ap->arg_phnum;
    185      1.10        ad 		a++;
    186       1.1      fvdl 
    187      1.10        ad 		a->a_type = AT_PAGESZ;
    188      1.10        ad 		a->a_v = PAGE_SIZE;
    189       1.1      fvdl 		a++;
    190       1.1      fvdl 
    191      1.10        ad 		a->a_type = AT_BASE;
    192      1.10        ad 		a->a_v = ap->arg_interp;
    193       1.1      fvdl 		a++;
    194       1.1      fvdl 
    195      1.10        ad 		a->a_type = AT_FLAGS;
    196      1.10        ad 		a->a_v = 0;
    197       1.1      fvdl 		a++;
    198       1.1      fvdl 
    199      1.10        ad 		a->a_type = AT_ENTRY;
    200      1.10        ad 		a->a_v = ap->arg_entry;
    201       1.1      fvdl 		a++;
    202       1.1      fvdl 
    203      1.10        ad 		a->a_type = AT_EUID;
    204      1.10        ad 		if (vap->va_mode & S_ISUID)
    205      1.10        ad 			a->a_v = vap->va_uid;
    206      1.10        ad 		else
    207      1.10        ad 			a->a_v = kauth_cred_geteuid(l->l_cred);
    208       1.1      fvdl 		a++;
    209       1.1      fvdl 
    210      1.10        ad 		a->a_type = AT_RUID;
    211      1.10        ad 		a->a_v = kauth_cred_getuid(l->l_cred);
    212       1.1      fvdl 		a++;
    213       1.1      fvdl 
    214      1.10        ad 		a->a_type = AT_EGID;
    215      1.10        ad 		if (vap->va_mode & S_ISGID)
    216      1.10        ad 			a->a_v = vap->va_gid;
    217      1.10        ad 		else
    218      1.10        ad 			a->a_v = kauth_cred_getegid(l->l_cred);
    219       1.1      fvdl 		a++;
    220       1.1      fvdl 
    221      1.10        ad 		a->a_type = AT_RGID;
    222      1.10        ad 		a->a_v = kauth_cred_getgid(l->l_cred);
    223       1.1      fvdl 		a++;
    224       1.1      fvdl 
    225      1.36     joerg 		a->a_type = AT_STACKBASE;
    226      1.36     joerg 		a->a_v = l->l_proc->p_stackbase;
    227      1.36     joerg 		a++;
    228      1.36     joerg 
    229      1.10        ad 		if (pack->ep_path) {
    230      1.10        ad 			execname = a;
    231      1.10        ad 			a->a_type = AT_SUN_EXECNAME;
    232      1.10        ad 			a++;
    233      1.10        ad 		}
    234      1.10        ad 
    235      1.35      matt 		exec_free_emul_arg(pack);
    236       1.1      fvdl 	}
    237      1.10        ad 
    238      1.10        ad 	a->a_type = AT_NULL;
    239      1.10        ad 	a->a_v = 0;
    240      1.10        ad 	a++;
    241      1.10        ad 
    242      1.36     joerg 	vlen = (a - ai) * sizeof(ai[0]);
    243      1.36     joerg 
    244      1.36     joerg 	KASSERT(vlen <= sizeof(ai));
    245      1.10        ad 
    246      1.10        ad 	if (execname) {
    247      1.10        ad 		char *path = pack->ep_path;
    248      1.10        ad 		execname->a_v = (uintptr_t)(*stackp + vlen);
    249      1.10        ad 		len = strlen(path) + 1;
    250      1.10        ad 		if ((error = copyout(path, (*stackp + vlen), len)) != 0)
    251      1.10        ad 			return error;
    252      1.10        ad 		len = ALIGN(len);
    253      1.10        ad 	} else
    254      1.10        ad 		len = 0;
    255      1.10        ad 
    256      1.10        ad 	if ((error = copyout(ai, *stackp, vlen)) != 0)
    257      1.10        ad 		return error;
    258      1.10        ad 	*stackp += vlen + len;
    259      1.10        ad 
    260      1.10        ad 	return 0;
    261       1.1      fvdl }
    262       1.1      fvdl 
    263       1.1      fvdl /*
    264       1.1      fvdl  * elf_check_header():
    265       1.1      fvdl  *
    266      1.46    martin  * Check header for validity; return 0 if ok, ENOEXEC if error
    267       1.1      fvdl  */
    268       1.1      fvdl int
    269      1.56      maxv elf_check_header(Elf_Ehdr *eh)
    270       1.1      fvdl {
    271       1.3   thorpej 
    272      1.10        ad 	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
    273  1.70.2.7     skrll 	    eh->e_ident[EI_CLASS] != ELFCLASS) {
    274  1.70.2.8     skrll 		DPRINTF("bad magic e_ident[EI_MAG0,EI_MAG3] %#x%x%x%x, "
    275  1.70.2.8     skrll 		    "e_ident[EI_CLASS] %#x", eh->e_ident[EI_MAG0],
    276  1.70.2.8     skrll 		    eh->e_ident[EI_MAG1], eh->e_ident[EI_MAG2],
    277  1.70.2.8     skrll 		    eh->e_ident[EI_MAG3], eh->e_ident[EI_CLASS]);
    278       1.1      fvdl 		return ENOEXEC;
    279  1.70.2.7     skrll 	}
    280       1.1      fvdl 
    281       1.1      fvdl 	switch (eh->e_machine) {
    282      1.10        ad 
    283      1.10        ad 	ELFDEFNNAME(MACHDEP_ID_CASES)
    284       1.1      fvdl 
    285       1.1      fvdl 	default:
    286  1.70.2.7     skrll 		DPRINTF("bad machine %#x", eh->e_machine);
    287       1.1      fvdl 		return ENOEXEC;
    288       1.1      fvdl 	}
    289       1.1      fvdl 
    290  1.70.2.7     skrll 	if (ELF_EHDR_FLAGS_OK(eh) == 0) {
    291  1.70.2.7     skrll 		DPRINTF("bad flags %#x", eh->e_flags);
    292      1.10        ad 		return ENOEXEC;
    293  1.70.2.7     skrll 	}
    294      1.10        ad 
    295  1.70.2.7     skrll 	if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM) {
    296  1.70.2.7     skrll 		DPRINTF("bad shnum/phnum %#x/%#x", eh->e_shnum, eh->e_phnum);
    297      1.10        ad 		return ENOEXEC;
    298  1.70.2.7     skrll 	}
    299      1.10        ad 
    300       1.1      fvdl 	return 0;
    301       1.1      fvdl }
    302       1.1      fvdl 
    303       1.1      fvdl /*
    304       1.1      fvdl  * elf_load_psection():
    305      1.10        ad  *
    306       1.1      fvdl  * Load a psection at the appropriate address
    307       1.1      fvdl  */
    308      1.58      maxv static void
    309      1.10        ad elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
    310      1.64      maxv     const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags)
    311       1.1      fvdl {
    312      1.10        ad 	u_long msize, psize, rm, rf;
    313       1.1      fvdl 	long diff, offset;
    314      1.64      maxv 	int vmprot = 0;
    315       1.1      fvdl 
    316       1.1      fvdl 	/*
    317      1.10        ad 	 * If the user specified an address, then we load there.
    318      1.10        ad 	 */
    319      1.10        ad 	if (*addr == ELFDEFNNAME(NO_ADDR))
    320      1.10        ad 		*addr = ph->p_vaddr;
    321      1.10        ad 
    322      1.10        ad 	if (ph->p_align > 1) {
    323      1.10        ad 		/*
    324      1.10        ad 		 * Make sure we are virtually aligned as we are supposed to be.
    325      1.10        ad 		 */
    326      1.10        ad 		diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
    327      1.10        ad 		KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align));
    328      1.10        ad 		/*
    329      1.10        ad 		 * But make sure to not map any pages before the start of the
    330      1.10        ad 		 * psection by limiting the difference to within a page.
    331      1.10        ad 		 */
    332      1.10        ad 		diff &= PAGE_MASK;
    333      1.10        ad 	} else
    334      1.10        ad 		diff = 0;
    335       1.1      fvdl 
    336      1.64      maxv 	vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
    337      1.64      maxv 	vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
    338      1.64      maxv 	vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
    339       1.1      fvdl 
    340      1.10        ad 	/*
    341      1.10        ad 	 * Adjust everything so it all starts on a page boundary.
    342      1.10        ad 	 */
    343      1.10        ad 	*addr -= diff;
    344       1.1      fvdl 	offset = ph->p_offset - diff;
    345       1.1      fvdl 	*size = ph->p_filesz + diff;
    346       1.1      fvdl 	msize = ph->p_memsz + diff;
    347       1.1      fvdl 
    348      1.10        ad 	if (ph->p_align >= PAGE_SIZE) {
    349      1.10        ad 		if ((ph->p_flags & PF_W) != 0) {
    350      1.10        ad 			/*
    351      1.10        ad 			 * Because the pagedvn pager can't handle zero fill
    352      1.10        ad 			 * of the last data page if it's not page aligned we
    353      1.10        ad 			 * map the last page readvn.
    354      1.10        ad 			 */
    355      1.10        ad 			psize = trunc_page(*size);
    356      1.10        ad 		} else {
    357      1.10        ad 			psize = round_page(*size);
    358      1.10        ad 		}
    359      1.10        ad 	} else {
    360      1.10        ad 		psize = *size;
    361      1.10        ad 	}
    362      1.10        ad 
    363      1.10        ad 	if (psize > 0) {
    364      1.10        ad 		NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
    365      1.10        ad 		    vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
    366      1.64      maxv 		    offset, vmprot, flags);
    367      1.10        ad 		flags &= VMCMD_RELATIVE;
    368      1.10        ad 	}
    369      1.10        ad 	if (psize < *size) {
    370      1.10        ad 		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
    371      1.64      maxv 		    *addr + psize, vp, offset + psize, vmprot, flags);
    372      1.10        ad 	}
    373       1.1      fvdl 
    374       1.1      fvdl 	/*
    375      1.10        ad 	 * Check if we need to extend the size of the segment (does
    376      1.10        ad 	 * bss extend page the next page boundary)?
    377      1.10        ad 	 */
    378       1.1      fvdl 	rm = round_page(*addr + msize);
    379       1.1      fvdl 	rf = round_page(*addr + *size);
    380       1.1      fvdl 
    381       1.1      fvdl 	if (rm != rf) {
    382      1.10        ad 		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
    383      1.64      maxv 		    0, vmprot, flags & VMCMD_RELATIVE);
    384       1.1      fvdl 		*size = msize;
    385       1.1      fvdl 	}
    386       1.1      fvdl }
    387       1.1      fvdl 
    388       1.1      fvdl /*
    389      1.65      maxv  * elf_load_interp():
    390       1.1      fvdl  *
    391      1.65      maxv  * Load an interpreter pointed to by path.
    392       1.1      fvdl  */
    393      1.58      maxv static int
    394      1.65      maxv elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
    395      1.58      maxv     struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last)
    396       1.1      fvdl {
    397       1.1      fvdl 	int error, i;
    398      1.10        ad 	struct vnode *vp;
    399      1.10        ad 	struct vattr attr;
    400      1.10        ad 	Elf_Ehdr eh;
    401      1.10        ad 	Elf_Phdr *ph = NULL;
    402      1.10        ad 	const Elf_Phdr *base_ph;
    403      1.10        ad 	const Elf_Phdr *last_ph;
    404       1.1      fvdl 	u_long phsize;
    405      1.10        ad 	Elf_Addr addr = *last;
    406      1.10        ad 	struct proc *p;
    407      1.37    martin 	bool use_topdown;
    408      1.10        ad 
    409      1.10        ad 	p = l->l_proc;
    410      1.10        ad 
    411      1.38    martin 	KASSERT(p->p_vmspace);
    412  1.70.2.4     skrll 	KASSERT(p->p_vmspace != proc0.p_vmspace);
    413  1.70.2.4     skrll 
    414      1.54  christos #ifdef __USE_TOPDOWN_VM
    415  1.70.2.4     skrll 	use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
    416      1.37    martin #else
    417  1.70.2.4     skrll 	use_topdown = false;
    418      1.37    martin #endif
    419      1.37    martin 
    420      1.10        ad 	/*
    421      1.10        ad 	 * 1. open file
    422      1.10        ad 	 * 2. read filehdr
    423      1.10        ad 	 * 3. map text, data, and bss out of it using VM_*
    424      1.10        ad 	 */
    425      1.10        ad 	vp = epp->ep_interp;
    426      1.10        ad 	if (vp == NULL) {
    427      1.10        ad 		error = emul_find_interp(l, epp, path);
    428      1.10        ad 		if (error != 0)
    429      1.10        ad 			return error;
    430      1.10        ad 		vp = epp->ep_interp;
    431      1.10        ad 	}
    432      1.10        ad 	/* We'll tidy this ourselves - otherwise we have locking issues */
    433      1.10        ad 	epp->ep_interp = NULL;
    434      1.10        ad 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    435       1.1      fvdl 
    436       1.1      fvdl 	/*
    437      1.10        ad 	 * Similarly, if it's not marked as executable, or it's not a regular
    438      1.10        ad 	 * file, we don't allow it to be used.
    439      1.10        ad 	 */
    440      1.10        ad 	if (vp->v_type != VREG) {
    441      1.10        ad 		error = EACCES;
    442      1.10        ad 		goto badunlock;
    443      1.10        ad 	}
    444      1.10        ad 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
    445      1.10        ad 		goto badunlock;
    446      1.10        ad 
    447      1.10        ad 	/* get attributes */
    448      1.10        ad 	if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
    449      1.10        ad 		goto badunlock;
    450      1.10        ad 
    451      1.10        ad 	/*
    452      1.10        ad 	 * Check mount point.  Though we're not trying to exec this binary,
    453      1.10        ad 	 * we will be executing code from it, so if the mount point
    454      1.10        ad 	 * disallows execution or set-id-ness, we punt or kill the set-id.
    455      1.10        ad 	 */
    456      1.10        ad 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    457      1.10        ad 		error = EACCES;
    458      1.10        ad 		goto badunlock;
    459       1.1      fvdl 	}
    460      1.10        ad 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
    461      1.10        ad 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    462      1.10        ad 
    463      1.10        ad 	error = vn_marktext(vp);
    464      1.10        ad 	if (error)
    465      1.10        ad 		goto badunlock;
    466      1.10        ad 
    467      1.23   hannken 	VOP_UNLOCK(vp);
    468      1.10        ad 
    469      1.10        ad 	if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
    470      1.10        ad 		goto bad;
    471      1.10        ad 
    472      1.56      maxv 	if ((error = elf_check_header(&eh)) != 0)
    473       1.1      fvdl 		goto bad;
    474      1.56      maxv 	if (eh.e_type != ET_DYN || eh.e_phnum == 0) {
    475  1.70.2.7     skrll 		DPRINTF("bad interpreter type %#x", eh.e_type);
    476      1.10        ad 		error = ENOEXEC;
    477       1.1      fvdl 		goto bad;
    478      1.10        ad 	}
    479       1.1      fvdl 
    480      1.10        ad 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
    481      1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    482       1.1      fvdl 
    483      1.10        ad 	if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
    484       1.1      fvdl 		goto bad;
    485       1.1      fvdl 
    486      1.10        ad #ifdef ELF_INTERP_NON_RELOCATABLE
    487      1.10        ad 	/*
    488      1.10        ad 	 * Evil hack:  Only MIPS should be non-relocatable, and the
    489      1.10        ad 	 * psections should have a high address (typically 0x5ffe0000).
    490      1.10        ad 	 * If it's now relocatable, it should be linked at 0 and the
    491      1.10        ad 	 * psections should have zeros in the upper part of the address.
    492      1.10        ad 	 * Otherwise, force the load at the linked address.
    493      1.10        ad 	 */
    494      1.10        ad 	if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
    495      1.10        ad 		*last = ELFDEFNNAME(NO_ADDR);
    496      1.10        ad #endif
    497      1.10        ad 
    498      1.10        ad 	/*
    499      1.10        ad 	 * If no position to load the interpreter was set by a probe
    500      1.10        ad 	 * function, pick the same address that a non-fixed mmap(0, ..)
    501      1.10        ad 	 * would (i.e. something safely out of the way).
    502      1.10        ad 	 */
    503      1.10        ad 	if (*last == ELFDEFNNAME(NO_ADDR)) {
    504      1.10        ad 		u_long limit = 0;
    505      1.10        ad 		/*
    506      1.10        ad 		 * Find the start and ending addresses of the psections to
    507      1.10        ad 		 * be loaded.  This will give us the size.
    508      1.10        ad 		 */
    509      1.58      maxv 		for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) {
    510      1.58      maxv 			if (ph[i].p_type == PT_LOAD) {
    511      1.58      maxv 				u_long psize = ph[i].p_vaddr + ph[i].p_memsz;
    512      1.10        ad 				if (base_ph == NULL)
    513      1.58      maxv 					base_ph = &ph[i];
    514      1.10        ad 				if (psize > limit)
    515      1.10        ad 					limit = psize;
    516      1.10        ad 			}
    517      1.10        ad 		}
    518      1.10        ad 
    519      1.10        ad 		if (base_ph == NULL) {
    520  1.70.2.7     skrll 			DPRINTF("no interpreter loadable sections");
    521      1.10        ad 			error = ENOEXEC;
    522      1.10        ad 			goto bad;
    523      1.10        ad 		}
    524      1.10        ad 
    525      1.10        ad 		/*
    526      1.10        ad 		 * Now compute the size and load address.
    527      1.10        ad 		 */
    528      1.10        ad 		addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
    529      1.10        ad 		    epp->ep_daddr,
    530  1.70.2.4     skrll 		    round_page(limit) - trunc_page(base_ph->p_vaddr),
    531  1.70.2.4     skrll 		    use_topdown);
    532  1.70.2.6     skrll 		addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align,
    533  1.70.2.6     skrll 		    use_topdown);
    534  1.70.2.4     skrll 	} else {
    535      1.10        ad 		addr = *last; /* may be ELF_LINK_ADDR */
    536  1.70.2.4     skrll 	}
    537      1.10        ad 
    538       1.1      fvdl 	/*
    539      1.10        ad 	 * Load all the necessary sections
    540      1.10        ad 	 */
    541      1.58      maxv 	for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) {
    542      1.58      maxv 		switch (ph[i].p_type) {
    543      1.10        ad 		case PT_LOAD: {
    544      1.10        ad 			u_long size;
    545      1.10        ad 			int flags;
    546      1.10        ad 
    547      1.10        ad 			if (base_ph == NULL) {
    548      1.10        ad 				/*
    549      1.10        ad 				 * First encountered psection is always the
    550      1.10        ad 				 * base psection.  Make sure it's aligned
    551      1.10        ad 				 * properly (align down for topdown and align
    552      1.10        ad 				 * upwards for not topdown).
    553      1.10        ad 				 */
    554      1.58      maxv 				base_ph = &ph[i];
    555      1.10        ad 				flags = VMCMD_BASE;
    556      1.10        ad 				if (addr == ELF_LINK_ADDR)
    557      1.58      maxv 					addr = ph[i].p_vaddr;
    558      1.37    martin 				if (use_topdown)
    559      1.58      maxv 					addr = ELF_TRUNC(addr, ph[i].p_align);
    560      1.10        ad 				else
    561      1.58      maxv 					addr = ELF_ROUND(addr, ph[i].p_align);
    562      1.10        ad 			} else {
    563      1.10        ad 				u_long limit = round_page(last_ph->p_vaddr
    564      1.10        ad 				    + last_ph->p_memsz);
    565      1.58      maxv 				u_long base = trunc_page(ph[i].p_vaddr);
    566      1.10        ad 
    567      1.10        ad 				/*
    568      1.10        ad 				 * If there is a gap in between the psections,
    569      1.10        ad 				 * map it as inaccessible so nothing else
    570      1.10        ad 				 * mmap'ed will be placed there.
    571      1.10        ad 				 */
    572      1.10        ad 				if (limit != base) {
    573      1.10        ad 					NEW_VMCMD2(vcset, vmcmd_map_zero,
    574      1.10        ad 					    base - limit,
    575      1.10        ad 					    limit - base_ph->p_vaddr, NULLVP,
    576      1.10        ad 					    0, VM_PROT_NONE, VMCMD_RELATIVE);
    577      1.10        ad 				}
    578       1.1      fvdl 
    579      1.58      maxv 				addr = ph[i].p_vaddr - base_ph->p_vaddr;
    580      1.10        ad 				flags = VMCMD_RELATIVE;
    581      1.10        ad 			}
    582      1.58      maxv 			last_ph = &ph[i];
    583      1.10        ad 			elf_load_psection(vcset, vp, &ph[i], &addr,
    584      1.64      maxv 			    &size, flags);
    585      1.10        ad 			/*
    586      1.10        ad 			 * If entry is within this psection then this
    587      1.10        ad 			 * must contain the .text section.  *entryoff is
    588      1.10        ad 			 * relative to the base psection.
    589      1.10        ad 			 */
    590      1.58      maxv 			if (eh.e_entry >= ph[i].p_vaddr &&
    591      1.58      maxv 			    eh.e_entry < (ph[i].p_vaddr + size)) {
    592      1.10        ad 				*entryoff = eh.e_entry - base_ph->p_vaddr;
    593       1.1      fvdl 			}
    594       1.1      fvdl 			addr += size;
    595       1.1      fvdl 			break;
    596      1.10        ad 		}
    597       1.1      fvdl 
    598       1.1      fvdl 		default:
    599       1.1      fvdl 			break;
    600       1.1      fvdl 		}
    601       1.1      fvdl 	}
    602       1.1      fvdl 
    603      1.10        ad 	kmem_free(ph, phsize);
    604      1.10        ad 	/*
    605      1.10        ad 	 * This value is ignored if TOPDOWN.
    606      1.10        ad 	 */
    607      1.10        ad 	*last = addr;
    608      1.10        ad 	vrele(vp);
    609      1.10        ad 	return 0;
    610      1.10        ad 
    611      1.10        ad badunlock:
    612      1.23   hannken 	VOP_UNLOCK(vp);
    613      1.10        ad 
    614       1.1      fvdl bad:
    615       1.1      fvdl 	if (ph != NULL)
    616      1.10        ad 		kmem_free(ph, phsize);
    617      1.10        ad 	vrele(vp);
    618       1.1      fvdl 	return error;
    619       1.1      fvdl }
    620       1.1      fvdl 
    621       1.1      fvdl /*
    622       1.1      fvdl  * exec_elf_makecmds(): Prepare an Elf binary's exec package
    623       1.1      fvdl  *
    624       1.1      fvdl  * First, set of the various offsets/lengths in the exec package.
    625       1.1      fvdl  *
    626       1.1      fvdl  * Then, mark the text image busy (so it can be demand paged) or error
    627       1.1      fvdl  * out if this is not possible.  Finally, set up vmcmds for the
    628       1.1      fvdl  * text, data, bss, and stack segments.
    629       1.1      fvdl  */
    630       1.1      fvdl int
    631      1.10        ad exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
    632      1.10        ad {
    633      1.10        ad 	Elf_Ehdr *eh = epp->ep_hdr;
    634      1.10        ad 	Elf_Phdr *ph, *pp;
    635      1.26       chs 	Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
    636      1.58      maxv 	int error, i;
    637      1.10        ad 	char *interp = NULL;
    638      1.10        ad 	u_long phsize;
    639      1.65      maxv 	struct elf_args *ap;
    640      1.56      maxv 	bool is_dyn = false;
    641       1.1      fvdl 
    642  1.70.2.7     skrll 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) {
    643  1.70.2.7     skrll 		DPRINTF("small header %#x", epp->ep_hdrvalid);
    644       1.1      fvdl 		return ENOEXEC;
    645  1.70.2.7     skrll 	}
    646      1.56      maxv 	if ((error = elf_check_header(eh)) != 0)
    647      1.56      maxv 		return error;
    648       1.1      fvdl 
    649      1.56      maxv 	if (eh->e_type == ET_DYN)
    650  1.70.2.3     skrll 		/* PIE, and some libs have an entry point */
    651      1.56      maxv 		is_dyn = true;
    652  1.70.2.7     skrll 	else if (eh->e_type != ET_EXEC) {
    653  1.70.2.7     skrll 		DPRINTF("bad type %#x", eh->e_type);
    654       1.1      fvdl 		return ENOEXEC;
    655  1.70.2.7     skrll 	}
    656       1.1      fvdl 
    657  1.70.2.7     skrll 	if (eh->e_phnum == 0) {
    658  1.70.2.7     skrll 		DPRINTF("no program headers");
    659      1.10        ad 		return ENOEXEC;
    660  1.70.2.7     skrll 	}
    661      1.10        ad 
    662      1.10        ad 	error = vn_marktext(epp->ep_vp);
    663      1.10        ad 	if (error)
    664      1.10        ad 		return error;
    665      1.10        ad 
    666       1.1      fvdl 	/*
    667      1.10        ad 	 * Allocate space to hold all the program headers, and read them
    668      1.10        ad 	 * from the file
    669      1.10        ad 	 */
    670      1.10        ad 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    671      1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    672       1.1      fvdl 
    673      1.10        ad 	if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
    674      1.10        ad 	    0)
    675       1.1      fvdl 		goto bad;
    676       1.1      fvdl 
    677      1.10        ad 	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
    678      1.10        ad 	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
    679       1.1      fvdl 
    680       1.1      fvdl 	for (i = 0; i < eh->e_phnum; i++) {
    681       1.1      fvdl 		pp = &ph[i];
    682      1.10        ad 		if (pp->p_type == PT_INTERP) {
    683      1.52  christos 			if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) {
    684  1.70.2.7     skrll 				DPRINTF("bad interpreter namelen %#jx",
    685  1.70.2.7     skrll 				    (uintmax_t)pp->p_filesz);
    686      1.10        ad 				error = ENOEXEC;
    687       1.1      fvdl 				goto bad;
    688      1.10        ad 			}
    689      1.10        ad 			interp = PNBUF_GET();
    690      1.53     skrll 			if ((error = exec_read_from(l, epp->ep_vp,
    691      1.53     skrll 			    pp->p_offset, interp, pp->p_filesz)) != 0)
    692      1.53     skrll 				goto bad;
    693      1.52  christos 			/* Ensure interp is NUL-terminated and of the expected length */
    694      1.52  christos 			if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) {
    695  1.70.2.7     skrll 				DPRINTF("bad interpreter name");
    696      1.52  christos 				error = ENOEXEC;
    697      1.52  christos 				goto bad;
    698      1.52  christos 			}
    699       1.1      fvdl 			break;
    700       1.1      fvdl 		}
    701       1.1      fvdl 	}
    702       1.1      fvdl 
    703       1.1      fvdl 	/*
    704       1.1      fvdl 	 * On the same architecture, we may be emulating different systems.
    705      1.10        ad 	 * See which one will accept this executable.
    706       1.1      fvdl 	 *
    707       1.1      fvdl 	 * Probe functions would normally see if the interpreter (if any)
    708       1.1      fvdl 	 * exists. Emulation packages may possibly replace the interpreter in
    709  1.70.2.3     skrll 	 * interp with a changed path (/emul/xxx/<path>).
    710       1.1      fvdl 	 */
    711      1.10        ad 	pos = ELFDEFNNAME(NO_ADDR);
    712      1.10        ad 	if (epp->ep_esch->u.elf_probe_func) {
    713      1.10        ad 		vaddr_t startp = (vaddr_t)pos;
    714       1.1      fvdl 
    715      1.10        ad 		error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
    716      1.10        ad 							  &startp);
    717       1.1      fvdl 		if (error)
    718       1.1      fvdl 			goto bad;
    719      1.10        ad 		pos = (Elf_Addr)startp;
    720       1.1      fvdl 	}
    721       1.1      fvdl 
    722      1.13  drochner 	if (is_dyn)
    723  1.70.2.4     skrll 		elf_placedynexec(epp, eh, ph);
    724      1.10        ad 
    725       1.1      fvdl 	/*
    726      1.10        ad 	 * Load all the necessary sections
    727      1.10        ad 	 */
    728      1.58      maxv 	for (i = 0; i < eh->e_phnum; i++) {
    729      1.58      maxv 		Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
    730      1.10        ad 		u_long size = 0;
    731       1.1      fvdl 
    732       1.1      fvdl 		switch (ph[i].p_type) {
    733      1.10        ad 		case PT_LOAD:
    734       1.1      fvdl 			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
    735      1.64      maxv 			    &ph[i], &addr, &size, VMCMD_FIXED);
    736      1.10        ad 
    737       1.4      fvdl 			/*
    738      1.24     joerg 			 * Consider this as text segment, if it is executable.
    739      1.24     joerg 			 * If there is more than one text segment, pick the
    740      1.24     joerg 			 * largest.
    741       1.4      fvdl 			 */
    742      1.24     joerg 			if (ph[i].p_flags & PF_X) {
    743      1.24     joerg 				if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
    744      1.24     joerg 				    size > epp->ep_tsize) {
    745      1.24     joerg 					epp->ep_taddr = addr;
    746      1.24     joerg 					epp->ep_tsize = size;
    747      1.10        ad 				}
    748      1.25     joerg 				end_text = addr + size;
    749       1.4      fvdl 			} else {
    750       1.4      fvdl 				epp->ep_daddr = addr;
    751       1.4      fvdl 				epp->ep_dsize = size;
    752       1.4      fvdl 			}
    753      1.26       chs 			if (ph[i].p_offset == 0) {
    754      1.26       chs 				computed_phdr = ph[i].p_vaddr + eh->e_phoff;
    755      1.26       chs 			}
    756       1.1      fvdl 			break;
    757       1.1      fvdl 
    758      1.10        ad 		case PT_SHLIB:
    759      1.10        ad 			/* SCO has these sections. */
    760      1.10        ad 		case PT_INTERP:
    761      1.10        ad 			/* Already did this one. */
    762      1.10        ad 		case PT_DYNAMIC:
    763      1.10        ad 		case PT_NOTE:
    764       1.1      fvdl 			break;
    765      1.10        ad 		case PT_PHDR:
    766       1.4      fvdl 			/* Note address of program headers (in text segment) */
    767      1.58      maxv 			phdr = ph[i].p_vaddr;
    768       1.7  christos 			break;
    769       1.4      fvdl 
    770       1.1      fvdl 		default:
    771       1.1      fvdl 			/*
    772      1.10        ad 			 * Not fatal; we don't need to understand everything.
    773       1.1      fvdl 			 */
    774       1.1      fvdl 			break;
    775       1.1      fvdl 		}
    776       1.1      fvdl 	}
    777      1.59      maxv 
    778      1.68      maxv 	if (epp->ep_vmcmds.evs_used == 0) {
    779      1.59      maxv 		/* No VMCMD; there was no PT_LOAD section, or those
    780      1.59      maxv 		 * sections were empty */
    781  1.70.2.7     skrll 		DPRINTF("no vmcommands");
    782      1.59      maxv 		error = ENOEXEC;
    783      1.59      maxv 		goto bad;
    784      1.59      maxv 	}
    785      1.59      maxv 
    786      1.24     joerg 	if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
    787      1.27       chs 		epp->ep_daddr = round_page(end_text);
    788      1.25     joerg 		epp->ep_dsize = 0;
    789      1.24     joerg 	}
    790      1.24     joerg 
    791       1.5      fvdl 	/*
    792      1.10        ad 	 * Check if we found a dynamically linked binary and arrange to load
    793      1.10        ad 	 * its interpreter
    794       1.5      fvdl 	 */
    795      1.10        ad 	if (interp) {
    796      1.65      maxv 		u_int nused = epp->ep_vmcmds.evs_used;
    797      1.57  christos 		u_long interp_offset = 0;
    798       1.1      fvdl 
    799      1.65      maxv 		if ((error = elf_load_interp(l, epp, interp,
    800      1.58      maxv 		    &epp->ep_vmcmds, &interp_offset, &pos)) != 0) {
    801       1.1      fvdl 			goto bad;
    802       1.1      fvdl 		}
    803      1.59      maxv 		if (epp->ep_vmcmds.evs_used == nused) {
    804      1.65      maxv 			/* elf_load_interp() has not set up any new VMCMD */
    805  1.70.2.7     skrll 			DPRINTF("no vmcommands for interpreter");
    806      1.59      maxv 			error = ENOEXEC;
    807      1.59      maxv 			goto bad;
    808      1.59      maxv 		}
    809      1.59      maxv 
    810      1.65      maxv 		ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
    811      1.59      maxv 		ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr;
    812      1.63      matt 		epp->ep_entryoffset = interp_offset;
    813      1.10        ad 		epp->ep_entry = ap->arg_interp + interp_offset;
    814      1.26       chs 		PNBUF_PUT(interp);
    815  1.70.2.4     skrll 		interp = NULL;
    816      1.65      maxv 	} else {
    817      1.26       chs 		epp->ep_entry = eh->e_entry;
    818      1.65      maxv 		if (epp->ep_flags & EXEC_FORCEAUX) {
    819      1.65      maxv 			ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
    820      1.65      maxv 			ap->arg_interp = (vaddr_t)NULL;
    821      1.65      maxv 		} else
    822      1.65      maxv 			ap = NULL;
    823      1.65      maxv 	}
    824       1.1      fvdl 
    825      1.26       chs 	if (ap) {
    826      1.26       chs 		ap->arg_phaddr = phdr ? phdr : computed_phdr;
    827       1.1      fvdl 		ap->arg_phentsize = eh->e_phentsize;
    828       1.1      fvdl 		ap->arg_phnum = eh->e_phnum;
    829       1.1      fvdl 		ap->arg_entry = eh->e_entry;
    830       1.1      fvdl 		epp->ep_emul_arg = ap;
    831      1.35      matt 		epp->ep_emul_arg_free = elf_free_emul_arg;
    832      1.26       chs 	}
    833       1.1      fvdl 
    834       1.8  christos #ifdef ELF_MAP_PAGE_ZERO
    835       1.8  christos 	/* Dell SVR4 maps page zero, yeuch! */
    836      1.10        ad 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
    837      1.10        ad 	    epp->ep_vp, 0, VM_PROT_READ);
    838       1.8  christos #endif
    839  1.70.2.4     skrll 
    840  1.70.2.4     skrll 	error = (*epp->ep_esch->es_setup_stack)(l, epp);
    841  1.70.2.4     skrll 	if (error)
    842  1.70.2.4     skrll 		goto bad;
    843  1.70.2.4     skrll 
    844      1.10        ad 	kmem_free(ph, phsize);
    845  1.70.2.4     skrll 	return 0;
    846       1.1      fvdl 
    847       1.1      fvdl bad:
    848      1.10        ad 	if (interp)
    849      1.10        ad 		PNBUF_PUT(interp);
    850      1.35      matt 	exec_free_emul_arg(epp);
    851      1.10        ad 	kmem_free(ph, phsize);
    852       1.1      fvdl 	kill_vmcmds(&epp->ep_vmcmds);
    853      1.10        ad 	return error;
    854      1.10        ad }
    855      1.10        ad 
    856      1.10        ad int
    857      1.10        ad netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
    858      1.10        ad     Elf_Ehdr *eh)
    859      1.10        ad {
    860      1.10        ad 	size_t i;
    861  1.70.2.8     skrll 	Elf_Phdr *ph;
    862  1.70.2.8     skrll 	size_t phsize;
    863  1.70.2.8     skrll 	char *nbuf;
    864      1.10        ad 	int error;
    865      1.10        ad 	int isnetbsd = 0;
    866      1.10        ad 
    867      1.10        ad 	epp->ep_pax_flags = 0;
    868  1.70.2.8     skrll 
    869  1.70.2.8     skrll 	if (eh->e_phnum > ELF_MAXPHNUM || eh->e_phnum == 0) {
    870  1.70.2.8     skrll 		DPRINTF("no signature %#x", eh->e_phnum);
    871      1.10        ad 		return ENOEXEC;
    872  1.70.2.7     skrll 	}
    873      1.10        ad 
    874  1.70.2.8     skrll 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    875  1.70.2.8     skrll 	ph = kmem_alloc(phsize, KM_SLEEP);
    876  1.70.2.8     skrll 	error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
    877      1.10        ad 	if (error)
    878      1.10        ad 		goto out;
    879      1.10        ad 
    880  1.70.2.8     skrll 	nbuf = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP);
    881  1.70.2.8     skrll 	for (i = 0; i < eh->e_phnum; i++) {
    882  1.70.2.8     skrll 		const char *nptr;
    883  1.70.2.8     skrll 		size_t nlen;
    884  1.70.2.8     skrll 
    885  1.70.2.8     skrll 		if (ph[i].p_type != PT_NOTE ||
    886  1.70.2.8     skrll 		    ph[i].p_filesz > ELF_MAXNOTESIZE)
    887      1.10        ad 			continue;
    888      1.10        ad 
    889  1.70.2.8     skrll 		nlen = ph[i].p_filesz;
    890  1.70.2.8     skrll 		error = exec_read_from(l, epp->ep_vp, ph[i].p_offset,
    891  1.70.2.8     skrll 				       nbuf, nlen);
    892      1.10        ad 		if (error)
    893      1.15  christos 			continue;
    894      1.10        ad 
    895  1.70.2.8     skrll 		nptr = nbuf;
    896  1.70.2.8     skrll 		while (nlen > 0) {
    897  1.70.2.8     skrll 			const Elf_Nhdr *np;
    898  1.70.2.8     skrll 			const char *ndata, *ndesc;
    899  1.70.2.8     skrll 
    900  1.70.2.8     skrll 			/* note header */
    901  1.70.2.8     skrll 			np = (const Elf_Nhdr *)nptr;
    902  1.70.2.8     skrll 			if (nlen < sizeof(*np)) {
    903      1.39  christos 				break;
    904      1.39  christos 			}
    905  1.70.2.8     skrll 			nptr += sizeof(*np);
    906  1.70.2.8     skrll 			nlen -= sizeof(*np);
    907      1.61      maxv 
    908  1.70.2.8     skrll 			/* note name */
    909  1.70.2.8     skrll 			ndata = nptr;
    910  1.70.2.8     skrll 			if (nlen < roundup(np->n_namesz, 4)) {
    911      1.61      maxv 				break;
    912      1.39  christos 			}
    913  1.70.2.8     skrll 			nptr += roundup(np->n_namesz, 4);
    914  1.70.2.8     skrll 			nlen -= roundup(np->n_namesz, 4);
    915      1.10        ad 
    916  1.70.2.8     skrll 			/* note description */
    917  1.70.2.8     skrll 			ndesc = nptr;
    918  1.70.2.8     skrll 			if (nlen < roundup(np->n_descsz, 4)) {
    919      1.47      matt 				break;
    920      1.47      matt 			}
    921  1.70.2.8     skrll 			nptr += roundup(np->n_descsz, 4);
    922  1.70.2.8     skrll 			nlen -= roundup(np->n_descsz, 4);
    923      1.61      maxv 
    924  1.70.2.8     skrll 			isnetbsd |= netbsd_elf_note(epp, np, ndata, ndesc);
    925  1.70.2.8     skrll 		}
    926  1.70.2.8     skrll 	}
    927  1.70.2.8     skrll 	kmem_free(nbuf, ELF_MAXNOTESIZE);
    928  1.70.2.8     skrll 
    929  1.70.2.8     skrll 	error = isnetbsd ? 0 : ENOEXEC;
    930  1.70.2.8     skrll #ifdef DEBUG_ELF
    931  1.70.2.8     skrll 	if (error)
    932  1.70.2.8     skrll 		DPRINTF("not netbsd");
    933      1.49    martin #endif
    934  1.70.2.8     skrll out:
    935  1.70.2.8     skrll 	kmem_free(ph, phsize);
    936  1.70.2.8     skrll 	return error;
    937  1.70.2.8     skrll }
    938  1.70.2.8     skrll 
    939  1.70.2.8     skrll int
    940  1.70.2.8     skrll netbsd_elf_note(struct exec_package *epp,
    941  1.70.2.8     skrll 		const Elf_Nhdr *np, const char *ndata, const char *ndesc)
    942  1.70.2.8     skrll {
    943  1.70.2.8     skrll 	int isnetbsd = 0;
    944  1.70.2.8     skrll 
    945  1.70.2.8     skrll #ifdef DIAGNOSTIC
    946  1.70.2.8     skrll 	const char *badnote;
    947  1.70.2.8     skrll #define BADNOTE(n) badnote = (n)
    948  1.70.2.8     skrll #else
    949  1.70.2.8     skrll #define BADNOTE(n)
    950  1.70.2.8     skrll #endif
    951  1.70.2.8     skrll 
    952  1.70.2.8     skrll 	switch (np->n_type) {
    953  1.70.2.8     skrll 	case ELF_NOTE_TYPE_NETBSD_TAG:
    954  1.70.2.8     skrll 		/* It is us */
    955  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ &&
    956  1.70.2.8     skrll 		    np->n_descsz == ELF_NOTE_NETBSD_DESCSZ &&
    957  1.70.2.8     skrll 		    memcmp(ndata, ELF_NOTE_NETBSD_NAME,
    958  1.70.2.8     skrll 		    ELF_NOTE_NETBSD_NAMESZ) == 0) {
    959  1.70.2.8     skrll 			memcpy(&epp->ep_osversion, ndesc,
    960  1.70.2.8     skrll 			    ELF_NOTE_NETBSD_DESCSZ);
    961  1.70.2.8     skrll 			isnetbsd = 1;
    962      1.49    martin 			break;
    963  1.70.2.8     skrll 		}
    964      1.47      matt 
    965  1.70.2.8     skrll 		/*
    966  1.70.2.8     skrll 		 * Ignore SuSE tags; SuSE's n_type is the same the
    967  1.70.2.8     skrll 		 * NetBSD one.
    968  1.70.2.8     skrll 		 */
    969  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ &&
    970  1.70.2.8     skrll 		    memcmp(ndata, ELF_NOTE_SUSE_NAME,
    971  1.70.2.8     skrll 		    ELF_NOTE_SUSE_NAMESZ) == 0)
    972  1.70.2.8     skrll 			break;
    973  1.70.2.8     skrll 		/*
    974  1.70.2.8     skrll 		 * Ignore old GCC
    975  1.70.2.8     skrll 		 */
    976  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ &&
    977  1.70.2.8     skrll 		    memcmp(ndata, ELF_NOTE_OGCC_NAME,
    978  1.70.2.8     skrll 		    ELF_NOTE_OGCC_NAMESZ) == 0)
    979      1.31  christos 			break;
    980  1.70.2.8     skrll 		BADNOTE("NetBSD tag");
    981  1.70.2.8     skrll 		goto bad;
    982      1.31  christos 
    983  1.70.2.8     skrll 	case ELF_NOTE_TYPE_PAX_TAG:
    984  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_PAX_NAMESZ &&
    985  1.70.2.8     skrll 		    np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
    986  1.70.2.8     skrll 		    memcmp(ndata, ELF_NOTE_PAX_NAME,
    987  1.70.2.8     skrll 		    ELF_NOTE_PAX_NAMESZ) == 0) {
    988  1.70.2.8     skrll 			uint32_t flags;
    989  1.70.2.8     skrll 			memcpy(&flags, ndesc, sizeof(flags));
    990  1.70.2.8     skrll 			/* Convert the flags and insert them into
    991  1.70.2.8     skrll 			 * the exec package. */
    992  1.70.2.8     skrll 			pax_setup_elf_flags(epp, flags);
    993  1.70.2.4     skrll 			break;
    994  1.70.2.8     skrll 		}
    995  1.70.2.8     skrll 		BADNOTE("PaX tag");
    996  1.70.2.8     skrll 		goto bad;
    997  1.70.2.4     skrll 
    998  1.70.2.8     skrll 	case ELF_NOTE_TYPE_MARCH_TAG:
    999  1.70.2.8     skrll 		/* Copy the machine arch into the package. */
   1000  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ
   1001  1.70.2.8     skrll 		    && memcmp(ndata, ELF_NOTE_MARCH_NAME,
   1002  1.70.2.8     skrll 			    ELF_NOTE_MARCH_NAMESZ) == 0) {
   1003  1.70.2.8     skrll 			/* Do not truncate the buffer */
   1004  1.70.2.8     skrll 			if (np->n_descsz > sizeof(epp->ep_machine_arch)) {
   1005  1.70.2.8     skrll 				BADNOTE("description size limit");
   1006  1.70.2.8     skrll 				goto bad;
   1007  1.70.2.8     skrll 			}
   1008  1.70.2.8     skrll 			/*
   1009  1.70.2.8     skrll 			 * Ensure ndesc is NUL-terminated and of the
   1010  1.70.2.8     skrll 			 * expected length.
   1011  1.70.2.8     skrll 			 */
   1012  1.70.2.8     skrll 			if (strnlen(ndesc, np->n_descsz) + 1 !=
   1013  1.70.2.8     skrll 			    np->n_descsz) {
   1014  1.70.2.8     skrll 				BADNOTE("description size");
   1015  1.70.2.8     skrll 				goto bad;
   1016  1.70.2.8     skrll 			}
   1017  1.70.2.8     skrll 			strlcpy(epp->ep_machine_arch, ndesc,
   1018  1.70.2.8     skrll 			    sizeof(epp->ep_machine_arch));
   1019  1.70.2.8     skrll 			break;
   1020  1.70.2.8     skrll 		}
   1021  1.70.2.8     skrll 		BADNOTE("march tag");
   1022  1.70.2.8     skrll 		goto bad;
   1023  1.70.2.8     skrll 
   1024  1.70.2.8     skrll 	case ELF_NOTE_TYPE_MCMODEL_TAG:
   1025  1.70.2.8     skrll 		/* arch specific check for code model */
   1026  1.70.2.8     skrll #ifdef ELF_MD_MCMODEL_CHECK
   1027  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ
   1028  1.70.2.8     skrll 		    && memcmp(ndata, ELF_NOTE_MCMODEL_NAME,
   1029  1.70.2.8     skrll 			    ELF_NOTE_MCMODEL_NAMESZ) == 0) {
   1030  1.70.2.8     skrll 			ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz);
   1031  1.70.2.8     skrll 			break;
   1032  1.70.2.8     skrll 		}
   1033  1.70.2.8     skrll 		BADNOTE("mcmodel tag");
   1034  1.70.2.8     skrll 		goto bad;
   1035  1.70.2.8     skrll #endif
   1036  1.70.2.8     skrll 		break;
   1037  1.70.2.8     skrll 
   1038  1.70.2.8     skrll 	case ELF_NOTE_TYPE_SUSE_VERSION_TAG:
   1039  1.70.2.8     skrll 		break;
   1040  1.70.2.8     skrll 
   1041  1.70.2.8     skrll 	case ELF_NOTE_TYPE_GO_BUILDID_TAG:
   1042  1.70.2.8     skrll 		break;
   1043  1.70.2.8     skrll 
   1044  1.70.2.8     skrll 	default:
   1045  1.70.2.8     skrll 		BADNOTE("unknown tag");
   1046      1.61      maxv bad:
   1047      1.15  christos #ifdef DIAGNOSTIC
   1048  1.70.2.8     skrll 		/* Ignore GNU tags */
   1049  1.70.2.8     skrll 		if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
   1050  1.70.2.8     skrll 		    memcmp(ndata, ELF_NOTE_GNU_NAME,
   1051  1.70.2.8     skrll 		    ELF_NOTE_GNU_NAMESZ) == 0)
   1052  1.70.2.8     skrll 		    break;
   1053  1.70.2.8     skrll 
   1054  1.70.2.8     skrll 		int ns = (int)np->n_namesz;
   1055  1.70.2.8     skrll 		printf("%s: Unknown elf note type %d (%s): "
   1056  1.70.2.8     skrll 		    "[namesz=%d, descsz=%d name=%-*.*s]\n",
   1057  1.70.2.8     skrll 		    epp->ep_kname, np->n_type, badnote, np->n_namesz,
   1058  1.70.2.8     skrll 		    np->n_descsz, ns, ns, ndata);
   1059      1.15  christos #endif
   1060  1.70.2.8     skrll 		break;
   1061      1.10        ad 	}
   1062      1.10        ad 
   1063  1.70.2.8     skrll 	return isnetbsd;
   1064      1.10        ad }
   1065      1.10        ad 
   1066      1.10        ad int
   1067      1.10        ad netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
   1068      1.10        ad     vaddr_t *pos)
   1069      1.10        ad {
   1070      1.10        ad 	int error;
   1071      1.10        ad 
   1072      1.10        ad 	if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
   1073      1.10        ad 		return error;
   1074      1.12      matt #ifdef ELF_MD_PROBE_FUNC
   1075      1.12      matt 	if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
   1076      1.12      matt 		return error;
   1077      1.12      matt #elif defined(ELF_INTERP_NON_RELOCATABLE)
   1078      1.10        ad 	*pos = ELF_LINK_ADDR;
   1079      1.10        ad #endif
   1080      1.29     joerg 	epp->ep_flags |= EXEC_FORCEAUX;
   1081      1.10        ad 	return 0;
   1082       1.1      fvdl }
   1083      1.35      matt 
   1084      1.35      matt void
   1085      1.35      matt elf_free_emul_arg(void *arg)
   1086      1.35      matt {
   1087      1.35      matt 	struct elf_args *ap = arg;
   1088      1.35      matt 	KASSERT(ap != NULL);
   1089      1.35      matt 	kmem_free(ap, sizeof(*ap));
   1090      1.35      matt }
   1091