Home | History | Annotate | Line # | Download | only in kern
exec_elf.c revision 1.43.2.3
      1  1.43.2.1       tls /*	$NetBSD: exec_elf.c,v 1.43.2.3 2017/12/03 11:38:44 jdolecek Exp $	*/
      2      1.10        ad 
      3      1.10        ad /*-
      4  1.43.2.3  jdolecek  * 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.43.2.3  jdolecek  * 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.43.2.1       tls __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.43.2.3 2017/12/03 11:38:44 jdolecek 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.43.2.2       tls #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.43.2.3  jdolecek #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.43.2.2       tls static int
    102  1.43.2.2       tls elf_load_interp(struct lwp *, struct exec_package *, char *,
    103  1.43.2.2       tls     struct exec_vmcmd_set *, u_long *, Elf_Addr *);
    104  1.43.2.2       tls static void
    105  1.43.2.2       tls elf_load_psection(struct exec_vmcmd_set *, struct vnode *, const Elf_Phdr *,
    106  1.43.2.2       tls     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.43.2.3  jdolecek int	netbsd_elf_note(struct exec_package *, const Elf_Nhdr *, const char *,
    110  1.43.2.3  jdolecek 	    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.43.2.3  jdolecek #ifdef DEBUG_ELF
    117  1.43.2.3  jdolecek #define DPRINTF(a, ...)	printf("%s: " a "\n", __func__, ##__VA_ARGS__)
    118  1.43.2.3  jdolecek #else
    119  1.43.2.3  jdolecek #define DPRINTF(a, ...)
    120  1.43.2.3  jdolecek #endif
    121  1.43.2.3  jdolecek 
    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.43.2.3  jdolecek 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.43.2.3  jdolecek 	offset = (Elf_Addr)pax_aslr_exec_offset(epp, align);
    137  1.43.2.3  jdolecek 	if (offset < epp->ep_vm_minaddr)
    138  1.43.2.3  jdolecek 		offset = roundup(epp->ep_vm_minaddr, align);
    139  1.43.2.3  jdolecek 	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.43.2.2       tls 	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.43.2.3  jdolecek 	memset(ai, 0, sizeof(ai));
    167  1.43.2.3  jdolecek 
    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.43.2.3  jdolecek 		execname = a;
    230  1.43.2.3  jdolecek 		a->a_type = AT_SUN_EXECNAME;
    231  1.43.2.3  jdolecek 		a++;
    232      1.10        ad 
    233      1.35      matt 		exec_free_emul_arg(pack);
    234       1.1      fvdl 	}
    235      1.10        ad 
    236      1.10        ad 	a->a_type = AT_NULL;
    237      1.10        ad 	a->a_v = 0;
    238      1.10        ad 	a++;
    239      1.10        ad 
    240      1.36     joerg 	vlen = (a - ai) * sizeof(ai[0]);
    241      1.36     joerg 
    242      1.36     joerg 	KASSERT(vlen <= sizeof(ai));
    243      1.10        ad 
    244  1.43.2.3  jdolecek 	char *path = l->l_proc->p_path;
    245  1.43.2.3  jdolecek 	execname->a_v = (uintptr_t)(*stackp + vlen);
    246  1.43.2.3  jdolecek 	len = strlen(path) + 1;
    247  1.43.2.3  jdolecek 	if ((error = copyout(path, (*stackp + vlen), len)) != 0)
    248  1.43.2.3  jdolecek 		return error;
    249  1.43.2.3  jdolecek 	len = ALIGN(len);
    250      1.10        ad 
    251      1.10        ad 	if ((error = copyout(ai, *stackp, vlen)) != 0)
    252      1.10        ad 		return error;
    253      1.10        ad 	*stackp += vlen + len;
    254      1.10        ad 
    255      1.10        ad 	return 0;
    256       1.1      fvdl }
    257       1.1      fvdl 
    258       1.1      fvdl /*
    259       1.1      fvdl  * elf_check_header():
    260       1.1      fvdl  *
    261  1.43.2.2       tls  * Check header for validity; return 0 if ok, ENOEXEC if error
    262       1.1      fvdl  */
    263       1.1      fvdl int
    264  1.43.2.2       tls elf_check_header(Elf_Ehdr *eh)
    265       1.1      fvdl {
    266       1.3   thorpej 
    267      1.10        ad 	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
    268  1.43.2.3  jdolecek 	    eh->e_ident[EI_CLASS] != ELFCLASS) {
    269  1.43.2.3  jdolecek 		DPRINTF("bad magic e_ident[EI_MAG0,EI_MAG3] %#x%x%x%x, "
    270  1.43.2.3  jdolecek 		    "e_ident[EI_CLASS] %#x", eh->e_ident[EI_MAG0],
    271  1.43.2.3  jdolecek 		    eh->e_ident[EI_MAG1], eh->e_ident[EI_MAG2],
    272  1.43.2.3  jdolecek 		    eh->e_ident[EI_MAG3], eh->e_ident[EI_CLASS]);
    273       1.1      fvdl 		return ENOEXEC;
    274  1.43.2.3  jdolecek 	}
    275       1.1      fvdl 
    276       1.1      fvdl 	switch (eh->e_machine) {
    277      1.10        ad 
    278      1.10        ad 	ELFDEFNNAME(MACHDEP_ID_CASES)
    279       1.1      fvdl 
    280       1.1      fvdl 	default:
    281  1.43.2.3  jdolecek 		DPRINTF("bad machine %#x", eh->e_machine);
    282       1.1      fvdl 		return ENOEXEC;
    283       1.1      fvdl 	}
    284       1.1      fvdl 
    285  1.43.2.3  jdolecek 	if (ELF_EHDR_FLAGS_OK(eh) == 0) {
    286  1.43.2.3  jdolecek 		DPRINTF("bad flags %#x", eh->e_flags);
    287      1.10        ad 		return ENOEXEC;
    288  1.43.2.3  jdolecek 	}
    289      1.10        ad 
    290  1.43.2.3  jdolecek 	if (eh->e_shnum > ELF_MAXSHNUM || eh->e_phnum > ELF_MAXPHNUM) {
    291  1.43.2.3  jdolecek 		DPRINTF("bad shnum/phnum %#x/%#x", eh->e_shnum, eh->e_phnum);
    292      1.10        ad 		return ENOEXEC;
    293  1.43.2.3  jdolecek 	}
    294      1.10        ad 
    295       1.1      fvdl 	return 0;
    296       1.1      fvdl }
    297       1.1      fvdl 
    298       1.1      fvdl /*
    299       1.1      fvdl  * elf_load_psection():
    300      1.10        ad  *
    301       1.1      fvdl  * Load a psection at the appropriate address
    302       1.1      fvdl  */
    303  1.43.2.2       tls static void
    304      1.10        ad elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
    305  1.43.2.2       tls     const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int flags)
    306       1.1      fvdl {
    307      1.10        ad 	u_long msize, psize, rm, rf;
    308       1.1      fvdl 	long diff, offset;
    309  1.43.2.2       tls 	int vmprot = 0;
    310       1.1      fvdl 
    311       1.1      fvdl 	/*
    312      1.10        ad 	 * If the user specified an address, then we load there.
    313      1.10        ad 	 */
    314      1.10        ad 	if (*addr == ELFDEFNNAME(NO_ADDR))
    315      1.10        ad 		*addr = ph->p_vaddr;
    316      1.10        ad 
    317      1.10        ad 	if (ph->p_align > 1) {
    318      1.10        ad 		/*
    319      1.10        ad 		 * Make sure we are virtually aligned as we are supposed to be.
    320      1.10        ad 		 */
    321      1.10        ad 		diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
    322      1.10        ad 		KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align));
    323      1.10        ad 		/*
    324      1.10        ad 		 * But make sure to not map any pages before the start of the
    325      1.10        ad 		 * psection by limiting the difference to within a page.
    326      1.10        ad 		 */
    327      1.10        ad 		diff &= PAGE_MASK;
    328      1.10        ad 	} else
    329      1.10        ad 		diff = 0;
    330       1.1      fvdl 
    331  1.43.2.2       tls 	vmprot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
    332  1.43.2.2       tls 	vmprot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
    333  1.43.2.2       tls 	vmprot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
    334       1.1      fvdl 
    335      1.10        ad 	/*
    336      1.10        ad 	 * Adjust everything so it all starts on a page boundary.
    337      1.10        ad 	 */
    338      1.10        ad 	*addr -= diff;
    339       1.1      fvdl 	offset = ph->p_offset - diff;
    340       1.1      fvdl 	*size = ph->p_filesz + diff;
    341       1.1      fvdl 	msize = ph->p_memsz + diff;
    342       1.1      fvdl 
    343      1.10        ad 	if (ph->p_align >= PAGE_SIZE) {
    344      1.10        ad 		if ((ph->p_flags & PF_W) != 0) {
    345      1.10        ad 			/*
    346      1.10        ad 			 * Because the pagedvn pager can't handle zero fill
    347      1.10        ad 			 * of the last data page if it's not page aligned we
    348      1.10        ad 			 * map the last page readvn.
    349      1.10        ad 			 */
    350      1.10        ad 			psize = trunc_page(*size);
    351      1.10        ad 		} else {
    352      1.10        ad 			psize = round_page(*size);
    353      1.10        ad 		}
    354      1.10        ad 	} else {
    355      1.10        ad 		psize = *size;
    356      1.10        ad 	}
    357      1.10        ad 
    358      1.10        ad 	if (psize > 0) {
    359      1.10        ad 		NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
    360      1.10        ad 		    vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
    361  1.43.2.2       tls 		    offset, vmprot, flags);
    362      1.10        ad 		flags &= VMCMD_RELATIVE;
    363      1.10        ad 	}
    364      1.10        ad 	if (psize < *size) {
    365      1.10        ad 		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
    366  1.43.2.2       tls 		    *addr + psize, vp, offset + psize, vmprot, flags);
    367      1.10        ad 	}
    368       1.1      fvdl 
    369       1.1      fvdl 	/*
    370      1.10        ad 	 * Check if we need to extend the size of the segment (does
    371      1.10        ad 	 * bss extend page the next page boundary)?
    372      1.10        ad 	 */
    373       1.1      fvdl 	rm = round_page(*addr + msize);
    374       1.1      fvdl 	rf = round_page(*addr + *size);
    375       1.1      fvdl 
    376       1.1      fvdl 	if (rm != rf) {
    377      1.10        ad 		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
    378  1.43.2.2       tls 		    0, vmprot, flags & VMCMD_RELATIVE);
    379       1.1      fvdl 		*size = msize;
    380       1.1      fvdl 	}
    381       1.1      fvdl }
    382       1.1      fvdl 
    383       1.1      fvdl /*
    384  1.43.2.2       tls  * elf_load_interp():
    385       1.1      fvdl  *
    386  1.43.2.2       tls  * Load an interpreter pointed to by path.
    387       1.1      fvdl  */
    388  1.43.2.2       tls static int
    389  1.43.2.2       tls elf_load_interp(struct lwp *l, struct exec_package *epp, char *path,
    390  1.43.2.2       tls     struct exec_vmcmd_set *vcset, u_long *entryoff, Elf_Addr *last)
    391       1.1      fvdl {
    392       1.1      fvdl 	int error, i;
    393      1.10        ad 	struct vnode *vp;
    394      1.10        ad 	struct vattr attr;
    395      1.10        ad 	Elf_Ehdr eh;
    396      1.10        ad 	Elf_Phdr *ph = NULL;
    397      1.10        ad 	const Elf_Phdr *base_ph;
    398      1.10        ad 	const Elf_Phdr *last_ph;
    399       1.1      fvdl 	u_long phsize;
    400      1.10        ad 	Elf_Addr addr = *last;
    401      1.10        ad 	struct proc *p;
    402      1.37    martin 	bool use_topdown;
    403      1.10        ad 
    404      1.10        ad 	p = l->l_proc;
    405      1.10        ad 
    406      1.38    martin 	KASSERT(p->p_vmspace);
    407  1.43.2.3  jdolecek 	KASSERT(p->p_vmspace != proc0.p_vmspace);
    408  1.43.2.3  jdolecek 
    409  1.43.2.2       tls #ifdef __USE_TOPDOWN_VM
    410  1.43.2.3  jdolecek 	use_topdown = epp->ep_flags & EXEC_TOPDOWN_VM;
    411      1.37    martin #else
    412  1.43.2.3  jdolecek 	use_topdown = false;
    413      1.37    martin #endif
    414      1.37    martin 
    415      1.10        ad 	/*
    416      1.10        ad 	 * 1. open file
    417      1.10        ad 	 * 2. read filehdr
    418      1.10        ad 	 * 3. map text, data, and bss out of it using VM_*
    419      1.10        ad 	 */
    420      1.10        ad 	vp = epp->ep_interp;
    421      1.10        ad 	if (vp == NULL) {
    422      1.10        ad 		error = emul_find_interp(l, epp, path);
    423      1.10        ad 		if (error != 0)
    424      1.10        ad 			return error;
    425      1.10        ad 		vp = epp->ep_interp;
    426      1.10        ad 	}
    427      1.10        ad 	/* We'll tidy this ourselves - otherwise we have locking issues */
    428      1.10        ad 	epp->ep_interp = NULL;
    429      1.10        ad 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    430       1.1      fvdl 
    431       1.1      fvdl 	/*
    432      1.10        ad 	 * Similarly, if it's not marked as executable, or it's not a regular
    433      1.10        ad 	 * file, we don't allow it to be used.
    434      1.10        ad 	 */
    435      1.10        ad 	if (vp->v_type != VREG) {
    436      1.10        ad 		error = EACCES;
    437      1.10        ad 		goto badunlock;
    438      1.10        ad 	}
    439      1.10        ad 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
    440      1.10        ad 		goto badunlock;
    441      1.10        ad 
    442      1.10        ad 	/* get attributes */
    443      1.10        ad 	if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
    444      1.10        ad 		goto badunlock;
    445      1.10        ad 
    446      1.10        ad 	/*
    447      1.10        ad 	 * Check mount point.  Though we're not trying to exec this binary,
    448      1.10        ad 	 * we will be executing code from it, so if the mount point
    449      1.10        ad 	 * disallows execution or set-id-ness, we punt or kill the set-id.
    450      1.10        ad 	 */
    451      1.10        ad 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    452      1.10        ad 		error = EACCES;
    453      1.10        ad 		goto badunlock;
    454       1.1      fvdl 	}
    455      1.10        ad 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
    456      1.10        ad 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    457      1.10        ad 
    458      1.10        ad 	error = vn_marktext(vp);
    459      1.10        ad 	if (error)
    460      1.10        ad 		goto badunlock;
    461      1.10        ad 
    462      1.23   hannken 	VOP_UNLOCK(vp);
    463      1.10        ad 
    464      1.10        ad 	if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
    465      1.10        ad 		goto bad;
    466      1.10        ad 
    467  1.43.2.2       tls 	if ((error = elf_check_header(&eh)) != 0)
    468       1.1      fvdl 		goto bad;
    469  1.43.2.2       tls 	if (eh.e_type != ET_DYN || eh.e_phnum == 0) {
    470  1.43.2.3  jdolecek 		DPRINTF("bad interpreter type %#x", eh.e_type);
    471      1.10        ad 		error = ENOEXEC;
    472       1.1      fvdl 		goto bad;
    473      1.10        ad 	}
    474       1.1      fvdl 
    475      1.10        ad 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
    476      1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    477       1.1      fvdl 
    478      1.10        ad 	if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
    479       1.1      fvdl 		goto bad;
    480       1.1      fvdl 
    481      1.10        ad #ifdef ELF_INTERP_NON_RELOCATABLE
    482      1.10        ad 	/*
    483      1.10        ad 	 * Evil hack:  Only MIPS should be non-relocatable, and the
    484      1.10        ad 	 * psections should have a high address (typically 0x5ffe0000).
    485      1.10        ad 	 * If it's now relocatable, it should be linked at 0 and the
    486      1.10        ad 	 * psections should have zeros in the upper part of the address.
    487      1.10        ad 	 * Otherwise, force the load at the linked address.
    488      1.10        ad 	 */
    489      1.10        ad 	if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
    490      1.10        ad 		*last = ELFDEFNNAME(NO_ADDR);
    491      1.10        ad #endif
    492      1.10        ad 
    493      1.10        ad 	/*
    494      1.10        ad 	 * If no position to load the interpreter was set by a probe
    495      1.10        ad 	 * function, pick the same address that a non-fixed mmap(0, ..)
    496      1.10        ad 	 * would (i.e. something safely out of the way).
    497      1.10        ad 	 */
    498      1.10        ad 	if (*last == ELFDEFNNAME(NO_ADDR)) {
    499      1.10        ad 		u_long limit = 0;
    500      1.10        ad 		/*
    501      1.10        ad 		 * Find the start and ending addresses of the psections to
    502      1.10        ad 		 * be loaded.  This will give us the size.
    503      1.10        ad 		 */
    504  1.43.2.2       tls 		for (i = 0, base_ph = NULL; i < eh.e_phnum; i++) {
    505  1.43.2.2       tls 			if (ph[i].p_type == PT_LOAD) {
    506  1.43.2.2       tls 				u_long psize = ph[i].p_vaddr + ph[i].p_memsz;
    507      1.10        ad 				if (base_ph == NULL)
    508  1.43.2.2       tls 					base_ph = &ph[i];
    509      1.10        ad 				if (psize > limit)
    510      1.10        ad 					limit = psize;
    511      1.10        ad 			}
    512      1.10        ad 		}
    513      1.10        ad 
    514      1.10        ad 		if (base_ph == NULL) {
    515  1.43.2.3  jdolecek 			DPRINTF("no interpreter loadable sections");
    516      1.10        ad 			error = ENOEXEC;
    517      1.10        ad 			goto bad;
    518      1.10        ad 		}
    519      1.10        ad 
    520      1.10        ad 		/*
    521      1.10        ad 		 * Now compute the size and load address.
    522      1.10        ad 		 */
    523      1.10        ad 		addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
    524      1.10        ad 		    epp->ep_daddr,
    525  1.43.2.3  jdolecek 		    round_page(limit) - trunc_page(base_ph->p_vaddr),
    526  1.43.2.3  jdolecek 		    use_topdown);
    527  1.43.2.3  jdolecek 		addr += (Elf_Addr)pax_aslr_rtld_offset(epp, base_ph->p_align,
    528  1.43.2.3  jdolecek 		    use_topdown);
    529  1.43.2.3  jdolecek 	} else {
    530      1.10        ad 		addr = *last; /* may be ELF_LINK_ADDR */
    531  1.43.2.3  jdolecek 	}
    532      1.10        ad 
    533       1.1      fvdl 	/*
    534      1.10        ad 	 * Load all the necessary sections
    535      1.10        ad 	 */
    536  1.43.2.2       tls 	for (i = 0, base_ph = NULL, last_ph = NULL; i < eh.e_phnum; i++) {
    537  1.43.2.2       tls 		switch (ph[i].p_type) {
    538      1.10        ad 		case PT_LOAD: {
    539      1.10        ad 			u_long size;
    540      1.10        ad 			int flags;
    541      1.10        ad 
    542      1.10        ad 			if (base_ph == NULL) {
    543      1.10        ad 				/*
    544      1.10        ad 				 * First encountered psection is always the
    545      1.10        ad 				 * base psection.  Make sure it's aligned
    546      1.10        ad 				 * properly (align down for topdown and align
    547      1.10        ad 				 * upwards for not topdown).
    548      1.10        ad 				 */
    549  1.43.2.2       tls 				base_ph = &ph[i];
    550      1.10        ad 				flags = VMCMD_BASE;
    551      1.10        ad 				if (addr == ELF_LINK_ADDR)
    552  1.43.2.2       tls 					addr = ph[i].p_vaddr;
    553      1.37    martin 				if (use_topdown)
    554  1.43.2.2       tls 					addr = ELF_TRUNC(addr, ph[i].p_align);
    555      1.10        ad 				else
    556  1.43.2.2       tls 					addr = ELF_ROUND(addr, ph[i].p_align);
    557      1.10        ad 			} else {
    558      1.10        ad 				u_long limit = round_page(last_ph->p_vaddr
    559      1.10        ad 				    + last_ph->p_memsz);
    560  1.43.2.2       tls 				u_long base = trunc_page(ph[i].p_vaddr);
    561      1.10        ad 
    562      1.10        ad 				/*
    563      1.10        ad 				 * If there is a gap in between the psections,
    564      1.10        ad 				 * map it as inaccessible so nothing else
    565      1.10        ad 				 * mmap'ed will be placed there.
    566      1.10        ad 				 */
    567      1.10        ad 				if (limit != base) {
    568      1.10        ad 					NEW_VMCMD2(vcset, vmcmd_map_zero,
    569      1.10        ad 					    base - limit,
    570      1.10        ad 					    limit - base_ph->p_vaddr, NULLVP,
    571      1.10        ad 					    0, VM_PROT_NONE, VMCMD_RELATIVE);
    572      1.10        ad 				}
    573       1.1      fvdl 
    574  1.43.2.2       tls 				addr = ph[i].p_vaddr - base_ph->p_vaddr;
    575      1.10        ad 				flags = VMCMD_RELATIVE;
    576      1.10        ad 			}
    577  1.43.2.2       tls 			last_ph = &ph[i];
    578      1.10        ad 			elf_load_psection(vcset, vp, &ph[i], &addr,
    579  1.43.2.2       tls 			    &size, flags);
    580      1.10        ad 			/*
    581      1.10        ad 			 * If entry is within this psection then this
    582      1.10        ad 			 * must contain the .text section.  *entryoff is
    583      1.10        ad 			 * relative to the base psection.
    584      1.10        ad 			 */
    585  1.43.2.2       tls 			if (eh.e_entry >= ph[i].p_vaddr &&
    586  1.43.2.2       tls 			    eh.e_entry < (ph[i].p_vaddr + size)) {
    587      1.10        ad 				*entryoff = eh.e_entry - base_ph->p_vaddr;
    588       1.1      fvdl 			}
    589       1.1      fvdl 			addr += size;
    590       1.1      fvdl 			break;
    591      1.10        ad 		}
    592       1.1      fvdl 
    593       1.1      fvdl 		default:
    594       1.1      fvdl 			break;
    595       1.1      fvdl 		}
    596       1.1      fvdl 	}
    597       1.1      fvdl 
    598      1.10        ad 	kmem_free(ph, phsize);
    599      1.10        ad 	/*
    600      1.10        ad 	 * This value is ignored if TOPDOWN.
    601      1.10        ad 	 */
    602      1.10        ad 	*last = addr;
    603      1.10        ad 	vrele(vp);
    604      1.10        ad 	return 0;
    605      1.10        ad 
    606      1.10        ad badunlock:
    607      1.23   hannken 	VOP_UNLOCK(vp);
    608      1.10        ad 
    609       1.1      fvdl bad:
    610       1.1      fvdl 	if (ph != NULL)
    611      1.10        ad 		kmem_free(ph, phsize);
    612      1.10        ad 	vrele(vp);
    613       1.1      fvdl 	return error;
    614       1.1      fvdl }
    615       1.1      fvdl 
    616       1.1      fvdl /*
    617       1.1      fvdl  * exec_elf_makecmds(): Prepare an Elf binary's exec package
    618       1.1      fvdl  *
    619       1.1      fvdl  * First, set of the various offsets/lengths in the exec package.
    620       1.1      fvdl  *
    621       1.1      fvdl  * Then, mark the text image busy (so it can be demand paged) or error
    622       1.1      fvdl  * out if this is not possible.  Finally, set up vmcmds for the
    623       1.1      fvdl  * text, data, bss, and stack segments.
    624       1.1      fvdl  */
    625       1.1      fvdl int
    626      1.10        ad exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
    627      1.10        ad {
    628      1.10        ad 	Elf_Ehdr *eh = epp->ep_hdr;
    629      1.10        ad 	Elf_Phdr *ph, *pp;
    630      1.26       chs 	Elf_Addr phdr = 0, computed_phdr = 0, pos = 0, end_text = 0;
    631  1.43.2.2       tls 	int error, i;
    632      1.10        ad 	char *interp = NULL;
    633      1.10        ad 	u_long phsize;
    634  1.43.2.2       tls 	struct elf_args *ap;
    635  1.43.2.2       tls 	bool is_dyn = false;
    636       1.1      fvdl 
    637  1.43.2.3  jdolecek 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) {
    638  1.43.2.3  jdolecek 		DPRINTF("small header %#x", epp->ep_hdrvalid);
    639       1.1      fvdl 		return ENOEXEC;
    640  1.43.2.3  jdolecek 	}
    641  1.43.2.2       tls 	if ((error = elf_check_header(eh)) != 0)
    642  1.43.2.2       tls 		return error;
    643       1.1      fvdl 
    644  1.43.2.2       tls 	if (eh->e_type == ET_DYN)
    645  1.43.2.3  jdolecek 		/* PIE, and some libs have an entry point */
    646  1.43.2.2       tls 		is_dyn = true;
    647  1.43.2.3  jdolecek 	else if (eh->e_type != ET_EXEC) {
    648  1.43.2.3  jdolecek 		DPRINTF("bad type %#x", eh->e_type);
    649       1.1      fvdl 		return ENOEXEC;
    650  1.43.2.3  jdolecek 	}
    651       1.1      fvdl 
    652  1.43.2.3  jdolecek 	if (eh->e_phnum == 0) {
    653  1.43.2.3  jdolecek 		DPRINTF("no program headers");
    654      1.10        ad 		return ENOEXEC;
    655  1.43.2.3  jdolecek 	}
    656      1.10        ad 
    657      1.10        ad 	error = vn_marktext(epp->ep_vp);
    658      1.10        ad 	if (error)
    659      1.10        ad 		return error;
    660      1.10        ad 
    661       1.1      fvdl 	/*
    662      1.10        ad 	 * Allocate space to hold all the program headers, and read them
    663      1.10        ad 	 * from the file
    664      1.10        ad 	 */
    665      1.10        ad 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    666      1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    667       1.1      fvdl 
    668      1.10        ad 	if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
    669      1.10        ad 	    0)
    670       1.1      fvdl 		goto bad;
    671       1.1      fvdl 
    672      1.10        ad 	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
    673      1.10        ad 	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
    674       1.1      fvdl 
    675       1.1      fvdl 	for (i = 0; i < eh->e_phnum; i++) {
    676       1.1      fvdl 		pp = &ph[i];
    677      1.10        ad 		if (pp->p_type == PT_INTERP) {
    678  1.43.2.2       tls 			if (pp->p_filesz < 2 || pp->p_filesz > MAXPATHLEN) {
    679  1.43.2.3  jdolecek 				DPRINTF("bad interpreter namelen %#jx",
    680  1.43.2.3  jdolecek 				    (uintmax_t)pp->p_filesz);
    681      1.10        ad 				error = ENOEXEC;
    682       1.1      fvdl 				goto bad;
    683      1.10        ad 			}
    684      1.10        ad 			interp = PNBUF_GET();
    685      1.10        ad 			if ((error = exec_read_from(l, epp->ep_vp,
    686      1.10        ad 			    pp->p_offset, interp, pp->p_filesz)) != 0)
    687       1.1      fvdl 				goto bad;
    688  1.43.2.2       tls 			/* Ensure interp is NUL-terminated and of the expected length */
    689  1.43.2.2       tls 			if (strnlen(interp, pp->p_filesz) != pp->p_filesz - 1) {
    690  1.43.2.3  jdolecek 				DPRINTF("bad interpreter name");
    691  1.43.2.2       tls 				error = ENOEXEC;
    692  1.43.2.2       tls 				goto bad;
    693  1.43.2.2       tls 			}
    694       1.1      fvdl 			break;
    695       1.1      fvdl 		}
    696       1.1      fvdl 	}
    697       1.1      fvdl 
    698       1.1      fvdl 	/*
    699       1.1      fvdl 	 * On the same architecture, we may be emulating different systems.
    700      1.10        ad 	 * See which one will accept this executable.
    701       1.1      fvdl 	 *
    702       1.1      fvdl 	 * Probe functions would normally see if the interpreter (if any)
    703       1.1      fvdl 	 * exists. Emulation packages may possibly replace the interpreter in
    704  1.43.2.3  jdolecek 	 * interp with a changed path (/emul/xxx/<path>).
    705       1.1      fvdl 	 */
    706      1.10        ad 	pos = ELFDEFNNAME(NO_ADDR);
    707      1.10        ad 	if (epp->ep_esch->u.elf_probe_func) {
    708      1.10        ad 		vaddr_t startp = (vaddr_t)pos;
    709       1.1      fvdl 
    710      1.10        ad 		error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
    711      1.10        ad 							  &startp);
    712       1.1      fvdl 		if (error)
    713       1.1      fvdl 			goto bad;
    714      1.10        ad 		pos = (Elf_Addr)startp;
    715       1.1      fvdl 	}
    716       1.1      fvdl 
    717      1.13  drochner 	if (is_dyn)
    718  1.43.2.3  jdolecek 		elf_placedynexec(epp, eh, ph);
    719      1.10        ad 
    720       1.1      fvdl 	/*
    721      1.10        ad 	 * Load all the necessary sections
    722      1.10        ad 	 */
    723  1.43.2.2       tls 	for (i = 0; i < eh->e_phnum; i++) {
    724  1.43.2.2       tls 		Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
    725      1.10        ad 		u_long size = 0;
    726       1.1      fvdl 
    727       1.1      fvdl 		switch (ph[i].p_type) {
    728      1.10        ad 		case PT_LOAD:
    729       1.1      fvdl 			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
    730  1.43.2.2       tls 			    &ph[i], &addr, &size, VMCMD_FIXED);
    731      1.10        ad 
    732       1.4      fvdl 			/*
    733      1.24     joerg 			 * Consider this as text segment, if it is executable.
    734      1.24     joerg 			 * If there is more than one text segment, pick the
    735      1.24     joerg 			 * largest.
    736       1.4      fvdl 			 */
    737      1.24     joerg 			if (ph[i].p_flags & PF_X) {
    738      1.24     joerg 				if (epp->ep_taddr == ELFDEFNNAME(NO_ADDR) ||
    739      1.24     joerg 				    size > epp->ep_tsize) {
    740      1.24     joerg 					epp->ep_taddr = addr;
    741      1.24     joerg 					epp->ep_tsize = size;
    742      1.10        ad 				}
    743      1.25     joerg 				end_text = addr + size;
    744       1.4      fvdl 			} else {
    745       1.4      fvdl 				epp->ep_daddr = addr;
    746       1.4      fvdl 				epp->ep_dsize = size;
    747       1.4      fvdl 			}
    748      1.26       chs 			if (ph[i].p_offset == 0) {
    749      1.26       chs 				computed_phdr = ph[i].p_vaddr + eh->e_phoff;
    750      1.26       chs 			}
    751       1.1      fvdl 			break;
    752       1.1      fvdl 
    753      1.10        ad 		case PT_SHLIB:
    754      1.10        ad 			/* SCO has these sections. */
    755      1.10        ad 		case PT_INTERP:
    756      1.10        ad 			/* Already did this one. */
    757      1.10        ad 		case PT_DYNAMIC:
    758      1.10        ad 		case PT_NOTE:
    759       1.1      fvdl 			break;
    760      1.10        ad 		case PT_PHDR:
    761       1.4      fvdl 			/* Note address of program headers (in text segment) */
    762  1.43.2.2       tls 			phdr = ph[i].p_vaddr;
    763       1.7  christos 			break;
    764       1.4      fvdl 
    765       1.1      fvdl 		default:
    766       1.1      fvdl 			/*
    767      1.10        ad 			 * Not fatal; we don't need to understand everything.
    768       1.1      fvdl 			 */
    769       1.1      fvdl 			break;
    770       1.1      fvdl 		}
    771       1.1      fvdl 	}
    772  1.43.2.2       tls 
    773  1.43.2.2       tls 	if (epp->ep_vmcmds.evs_used == 0) {
    774  1.43.2.2       tls 		/* No VMCMD; there was no PT_LOAD section, or those
    775  1.43.2.2       tls 		 * sections were empty */
    776  1.43.2.3  jdolecek 		DPRINTF("no vmcommands");
    777  1.43.2.2       tls 		error = ENOEXEC;
    778  1.43.2.2       tls 		goto bad;
    779      1.26       chs 	}
    780       1.5      fvdl 
    781      1.24     joerg 	if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
    782      1.27       chs 		epp->ep_daddr = round_page(end_text);
    783      1.25     joerg 		epp->ep_dsize = 0;
    784      1.24     joerg 	}
    785      1.24     joerg 
    786       1.5      fvdl 	/*
    787      1.10        ad 	 * Check if we found a dynamically linked binary and arrange to load
    788      1.10        ad 	 * its interpreter
    789       1.5      fvdl 	 */
    790      1.10        ad 	if (interp) {
    791  1.43.2.2       tls 		u_int nused = epp->ep_vmcmds.evs_used;
    792  1.43.2.2       tls 		u_long interp_offset = 0;
    793       1.1      fvdl 
    794  1.43.2.2       tls 		if ((error = elf_load_interp(l, epp, interp,
    795  1.43.2.2       tls 		    &epp->ep_vmcmds, &interp_offset, &pos)) != 0) {
    796       1.1      fvdl 			goto bad;
    797       1.1      fvdl 		}
    798  1.43.2.2       tls 		if (epp->ep_vmcmds.evs_used == nused) {
    799  1.43.2.2       tls 			/* elf_load_interp() has not set up any new VMCMD */
    800  1.43.2.3  jdolecek 			DPRINTF("no vmcommands for interpreter");
    801  1.43.2.2       tls 			error = ENOEXEC;
    802  1.43.2.2       tls 			goto bad;
    803  1.43.2.2       tls 		}
    804  1.43.2.2       tls 
    805  1.43.2.2       tls 		ap = kmem_alloc(sizeof(*ap), KM_SLEEP);
    806  1.43.2.2       tls 		ap->arg_interp = epp->ep_vmcmds.evs_cmds[nused].ev_addr;
    807  1.43.2.2       tls 		epp->ep_entryoffset = interp_offset;
    808      1.10        ad 		epp->ep_entry = ap->arg_interp + interp_offset;
    809      1.26       chs 		PNBUF_PUT(interp);
    810  1.43.2.3  jdolecek 		interp = NULL;
    811  1.43.2.2       tls 	} else {
    812      1.26       chs 		epp->ep_entry = eh->e_entry;
    813  1.43.2.2       tls 		if (epp->ep_flags & EXEC_FORCEAUX) {
    814  1.43.2.3  jdolecek 			ap = kmem_zalloc(sizeof(*ap), KM_SLEEP);
    815  1.43.2.2       tls 			ap->arg_interp = (vaddr_t)NULL;
    816  1.43.2.3  jdolecek 		} else {
    817  1.43.2.2       tls 			ap = NULL;
    818  1.43.2.3  jdolecek 		}
    819  1.43.2.2       tls 	}
    820       1.1      fvdl 
    821      1.26       chs 	if (ap) {
    822      1.26       chs 		ap->arg_phaddr = phdr ? phdr : computed_phdr;
    823       1.1      fvdl 		ap->arg_phentsize = eh->e_phentsize;
    824       1.1      fvdl 		ap->arg_phnum = eh->e_phnum;
    825       1.1      fvdl 		ap->arg_entry = eh->e_entry;
    826       1.1      fvdl 		epp->ep_emul_arg = ap;
    827      1.35      matt 		epp->ep_emul_arg_free = elf_free_emul_arg;
    828      1.26       chs 	}
    829       1.1      fvdl 
    830       1.8  christos #ifdef ELF_MAP_PAGE_ZERO
    831       1.8  christos 	/* Dell SVR4 maps page zero, yeuch! */
    832      1.10        ad 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
    833      1.10        ad 	    epp->ep_vp, 0, VM_PROT_READ);
    834       1.8  christos #endif
    835  1.43.2.3  jdolecek 
    836  1.43.2.3  jdolecek 	error = (*epp->ep_esch->es_setup_stack)(l, epp);
    837  1.43.2.3  jdolecek 	if (error)
    838  1.43.2.3  jdolecek 		goto bad;
    839  1.43.2.3  jdolecek 
    840      1.10        ad 	kmem_free(ph, phsize);
    841  1.43.2.3  jdolecek 	return 0;
    842       1.1      fvdl 
    843       1.1      fvdl bad:
    844      1.10        ad 	if (interp)
    845      1.10        ad 		PNBUF_PUT(interp);
    846      1.35      matt 	exec_free_emul_arg(epp);
    847      1.10        ad 	kmem_free(ph, phsize);
    848       1.1      fvdl 	kill_vmcmds(&epp->ep_vmcmds);
    849      1.10        ad 	return error;
    850      1.10        ad }
    851      1.10        ad 
    852      1.10        ad int
    853      1.10        ad netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
    854      1.10        ad     Elf_Ehdr *eh)
    855      1.10        ad {
    856      1.10        ad 	size_t i;
    857  1.43.2.3  jdolecek 	Elf_Phdr *ph;
    858  1.43.2.3  jdolecek 	size_t phsize;
    859  1.43.2.3  jdolecek 	char *nbuf;
    860      1.10        ad 	int error;
    861      1.10        ad 	int isnetbsd = 0;
    862      1.10        ad 
    863      1.10        ad 	epp->ep_pax_flags = 0;
    864  1.43.2.3  jdolecek 
    865  1.43.2.3  jdolecek 	if (eh->e_phnum > ELF_MAXPHNUM || eh->e_phnum == 0) {
    866  1.43.2.3  jdolecek 		DPRINTF("no signature %#x", eh->e_phnum);
    867      1.10        ad 		return ENOEXEC;
    868  1.43.2.3  jdolecek 	}
    869      1.10        ad 
    870  1.43.2.3  jdolecek 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    871  1.43.2.3  jdolecek 	ph = kmem_alloc(phsize, KM_SLEEP);
    872  1.43.2.3  jdolecek 	error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
    873      1.10        ad 	if (error)
    874      1.10        ad 		goto out;
    875      1.10        ad 
    876  1.43.2.3  jdolecek 	nbuf = kmem_alloc(ELF_MAXNOTESIZE, KM_SLEEP);
    877  1.43.2.3  jdolecek 	for (i = 0; i < eh->e_phnum; i++) {
    878  1.43.2.3  jdolecek 		const char *nptr;
    879  1.43.2.3  jdolecek 		size_t nlen;
    880  1.43.2.3  jdolecek 
    881  1.43.2.3  jdolecek 		if (ph[i].p_type != PT_NOTE ||
    882  1.43.2.3  jdolecek 		    ph[i].p_filesz > ELF_MAXNOTESIZE)
    883      1.10        ad 			continue;
    884      1.10        ad 
    885  1.43.2.3  jdolecek 		nlen = ph[i].p_filesz;
    886  1.43.2.3  jdolecek 		error = exec_read_from(l, epp->ep_vp, ph[i].p_offset,
    887  1.43.2.3  jdolecek 				       nbuf, nlen);
    888      1.10        ad 		if (error)
    889      1.15  christos 			continue;
    890      1.10        ad 
    891  1.43.2.3  jdolecek 		nptr = nbuf;
    892  1.43.2.3  jdolecek 		while (nlen > 0) {
    893  1.43.2.3  jdolecek 			const Elf_Nhdr *np;
    894  1.43.2.3  jdolecek 			const char *ndata, *ndesc;
    895  1.43.2.3  jdolecek 
    896  1.43.2.3  jdolecek 			/* note header */
    897  1.43.2.3  jdolecek 			np = (const Elf_Nhdr *)nptr;
    898  1.43.2.3  jdolecek 			if (nlen < sizeof(*np)) {
    899      1.39  christos 				break;
    900      1.39  christos 			}
    901  1.43.2.3  jdolecek 			nptr += sizeof(*np);
    902  1.43.2.3  jdolecek 			nlen -= sizeof(*np);
    903  1.43.2.2       tls 
    904  1.43.2.3  jdolecek 			/* note name */
    905  1.43.2.3  jdolecek 			ndata = nptr;
    906  1.43.2.3  jdolecek 			if (nlen < roundup(np->n_namesz, 4)) {
    907  1.43.2.2       tls 				break;
    908  1.43.2.2       tls 			}
    909  1.43.2.3  jdolecek 			nptr += roundup(np->n_namesz, 4);
    910  1.43.2.3  jdolecek 			nlen -= roundup(np->n_namesz, 4);
    911  1.43.2.2       tls 
    912  1.43.2.3  jdolecek 			/* note description */
    913  1.43.2.3  jdolecek 			ndesc = nptr;
    914  1.43.2.3  jdolecek 			if (nlen < roundup(np->n_descsz, 4)) {
    915  1.43.2.2       tls 				break;
    916  1.43.2.2       tls 			}
    917  1.43.2.3  jdolecek 			nptr += roundup(np->n_descsz, 4);
    918  1.43.2.3  jdolecek 			nlen -= roundup(np->n_descsz, 4);
    919  1.43.2.2       tls 
    920  1.43.2.3  jdolecek 			isnetbsd |= netbsd_elf_note(epp, np, ndata, ndesc);
    921  1.43.2.3  jdolecek 		}
    922  1.43.2.3  jdolecek 	}
    923  1.43.2.3  jdolecek 	kmem_free(nbuf, ELF_MAXNOTESIZE);
    924  1.43.2.3  jdolecek 
    925  1.43.2.3  jdolecek 	error = isnetbsd ? 0 : ENOEXEC;
    926  1.43.2.3  jdolecek #ifdef DEBUG_ELF
    927  1.43.2.3  jdolecek 	if (error)
    928  1.43.2.3  jdolecek 		DPRINTF("not netbsd");
    929  1.43.2.3  jdolecek #endif
    930  1.43.2.3  jdolecek out:
    931  1.43.2.3  jdolecek 	kmem_free(ph, phsize);
    932  1.43.2.3  jdolecek 	return error;
    933  1.43.2.3  jdolecek }
    934  1.43.2.3  jdolecek 
    935  1.43.2.3  jdolecek int
    936  1.43.2.3  jdolecek netbsd_elf_note(struct exec_package *epp,
    937  1.43.2.3  jdolecek 		const Elf_Nhdr *np, const char *ndata, const char *ndesc)
    938  1.43.2.3  jdolecek {
    939  1.43.2.3  jdolecek 	int isnetbsd = 0;
    940  1.43.2.3  jdolecek 
    941  1.43.2.3  jdolecek #ifdef DIAGNOSTIC
    942  1.43.2.3  jdolecek 	const char *badnote;
    943  1.43.2.3  jdolecek #define BADNOTE(n) badnote = (n)
    944  1.43.2.3  jdolecek #else
    945  1.43.2.3  jdolecek #define BADNOTE(n)
    946  1.43.2.2       tls #endif
    947  1.43.2.3  jdolecek 
    948  1.43.2.3  jdolecek 	switch (np->n_type) {
    949  1.43.2.3  jdolecek 	case ELF_NOTE_TYPE_NETBSD_TAG:
    950  1.43.2.3  jdolecek 		/* It is us */
    951  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_NETBSD_NAMESZ &&
    952  1.43.2.3  jdolecek 		    np->n_descsz == ELF_NOTE_NETBSD_DESCSZ &&
    953  1.43.2.3  jdolecek 		    memcmp(ndata, ELF_NOTE_NETBSD_NAME,
    954  1.43.2.3  jdolecek 		    ELF_NOTE_NETBSD_NAMESZ) == 0) {
    955  1.43.2.3  jdolecek 			memcpy(&epp->ep_osversion, ndesc,
    956  1.43.2.3  jdolecek 			    ELF_NOTE_NETBSD_DESCSZ);
    957  1.43.2.3  jdolecek 			isnetbsd = 1;
    958      1.10        ad 			break;
    959  1.43.2.3  jdolecek 		}
    960      1.10        ad 
    961  1.43.2.3  jdolecek 		/*
    962  1.43.2.3  jdolecek 		 * Ignore SuSE tags; SuSE's n_type is the same the
    963  1.43.2.3  jdolecek 		 * NetBSD one.
    964  1.43.2.3  jdolecek 		 */
    965  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_SUSE_NAMESZ &&
    966  1.43.2.3  jdolecek 		    memcmp(ndata, ELF_NOTE_SUSE_NAME,
    967  1.43.2.3  jdolecek 		    ELF_NOTE_SUSE_NAMESZ) == 0)
    968      1.31  christos 			break;
    969  1.43.2.3  jdolecek 		/*
    970  1.43.2.3  jdolecek 		 * Ignore old GCC
    971  1.43.2.3  jdolecek 		 */
    972  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_OGCC_NAMESZ &&
    973  1.43.2.3  jdolecek 		    memcmp(ndata, ELF_NOTE_OGCC_NAME,
    974  1.43.2.3  jdolecek 		    ELF_NOTE_OGCC_NAMESZ) == 0)
    975  1.43.2.3  jdolecek 			break;
    976  1.43.2.3  jdolecek 		BADNOTE("NetBSD tag");
    977  1.43.2.3  jdolecek 		goto bad;
    978      1.31  christos 
    979  1.43.2.3  jdolecek 	case ELF_NOTE_TYPE_PAX_TAG:
    980  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_PAX_NAMESZ &&
    981  1.43.2.3  jdolecek 		    np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
    982  1.43.2.3  jdolecek 		    memcmp(ndata, ELF_NOTE_PAX_NAME,
    983  1.43.2.3  jdolecek 		    ELF_NOTE_PAX_NAMESZ) == 0) {
    984  1.43.2.3  jdolecek 			uint32_t flags;
    985  1.43.2.3  jdolecek 			memcpy(&flags, ndesc, sizeof(flags));
    986  1.43.2.3  jdolecek 			/* Convert the flags and insert them into
    987  1.43.2.3  jdolecek 			 * the exec package. */
    988  1.43.2.3  jdolecek 			pax_setup_elf_flags(epp, flags);
    989  1.43.2.3  jdolecek 			break;
    990  1.43.2.3  jdolecek 		}
    991  1.43.2.3  jdolecek 		BADNOTE("PaX tag");
    992  1.43.2.3  jdolecek 		goto bad;
    993  1.43.2.3  jdolecek 
    994  1.43.2.3  jdolecek 	case ELF_NOTE_TYPE_MARCH_TAG:
    995  1.43.2.3  jdolecek 		/* Copy the machine arch into the package. */
    996  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_MARCH_NAMESZ
    997  1.43.2.3  jdolecek 		    && memcmp(ndata, ELF_NOTE_MARCH_NAME,
    998  1.43.2.3  jdolecek 			    ELF_NOTE_MARCH_NAMESZ) == 0) {
    999  1.43.2.3  jdolecek 			/* Do not truncate the buffer */
   1000  1.43.2.3  jdolecek 			if (np->n_descsz > sizeof(epp->ep_machine_arch)) {
   1001  1.43.2.3  jdolecek 				BADNOTE("description size limit");
   1002  1.43.2.3  jdolecek 				goto bad;
   1003  1.43.2.3  jdolecek 			}
   1004  1.43.2.3  jdolecek 			/*
   1005  1.43.2.3  jdolecek 			 * Ensure ndesc is NUL-terminated and of the
   1006  1.43.2.3  jdolecek 			 * expected length.
   1007  1.43.2.3  jdolecek 			 */
   1008  1.43.2.3  jdolecek 			if (strnlen(ndesc, np->n_descsz) + 1 !=
   1009  1.43.2.3  jdolecek 			    np->n_descsz) {
   1010  1.43.2.3  jdolecek 				BADNOTE("description size");
   1011  1.43.2.3  jdolecek 				goto bad;
   1012  1.43.2.3  jdolecek 			}
   1013  1.43.2.3  jdolecek 			strlcpy(epp->ep_machine_arch, ndesc,
   1014  1.43.2.3  jdolecek 			    sizeof(epp->ep_machine_arch));
   1015  1.43.2.3  jdolecek 			break;
   1016  1.43.2.3  jdolecek 		}
   1017  1.43.2.3  jdolecek 		BADNOTE("march tag");
   1018  1.43.2.3  jdolecek 		goto bad;
   1019  1.43.2.3  jdolecek 
   1020  1.43.2.3  jdolecek 	case ELF_NOTE_TYPE_MCMODEL_TAG:
   1021  1.43.2.3  jdolecek 		/* arch specific check for code model */
   1022  1.43.2.3  jdolecek #ifdef ELF_MD_MCMODEL_CHECK
   1023  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_MCMODEL_NAMESZ
   1024  1.43.2.3  jdolecek 		    && memcmp(ndata, ELF_NOTE_MCMODEL_NAME,
   1025  1.43.2.3  jdolecek 			    ELF_NOTE_MCMODEL_NAMESZ) == 0) {
   1026  1.43.2.3  jdolecek 			ELF_MD_MCMODEL_CHECK(epp, ndesc, np->n_descsz);
   1027  1.43.2.3  jdolecek 			break;
   1028  1.43.2.3  jdolecek 		}
   1029  1.43.2.3  jdolecek 		BADNOTE("mcmodel tag");
   1030  1.43.2.3  jdolecek 		goto bad;
   1031  1.43.2.3  jdolecek #endif
   1032  1.43.2.3  jdolecek 		break;
   1033  1.43.2.3  jdolecek 
   1034  1.43.2.3  jdolecek 	case ELF_NOTE_TYPE_SUSE_VERSION_TAG:
   1035  1.43.2.3  jdolecek 		break;
   1036  1.43.2.3  jdolecek 
   1037  1.43.2.3  jdolecek 	case ELF_NOTE_TYPE_GO_BUILDID_TAG:
   1038  1.43.2.3  jdolecek 		break;
   1039  1.43.2.3  jdolecek 
   1040  1.43.2.3  jdolecek 	default:
   1041  1.43.2.3  jdolecek 		BADNOTE("unknown tag");
   1042  1.43.2.2       tls bad:
   1043      1.15  christos #ifdef DIAGNOSTIC
   1044  1.43.2.3  jdolecek 		/* Ignore GNU tags */
   1045  1.43.2.3  jdolecek 		if (np->n_namesz == ELF_NOTE_GNU_NAMESZ &&
   1046  1.43.2.3  jdolecek 		    memcmp(ndata, ELF_NOTE_GNU_NAME,
   1047  1.43.2.3  jdolecek 		    ELF_NOTE_GNU_NAMESZ) == 0)
   1048  1.43.2.3  jdolecek 		    break;
   1049  1.43.2.3  jdolecek 
   1050  1.43.2.3  jdolecek 		int ns = (int)np->n_namesz;
   1051  1.43.2.3  jdolecek 		printf("%s: Unknown elf note type %d (%s): "
   1052  1.43.2.3  jdolecek 		    "[namesz=%d, descsz=%d name=%-*.*s]\n",
   1053  1.43.2.3  jdolecek 		    epp->ep_kname, np->n_type, badnote, np->n_namesz,
   1054  1.43.2.3  jdolecek 		    np->n_descsz, ns, ns, ndata);
   1055      1.15  christos #endif
   1056  1.43.2.3  jdolecek 		break;
   1057      1.10        ad 	}
   1058      1.10        ad 
   1059  1.43.2.3  jdolecek 	return isnetbsd;
   1060      1.10        ad }
   1061      1.10        ad 
   1062      1.10        ad int
   1063      1.10        ad netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
   1064      1.10        ad     vaddr_t *pos)
   1065      1.10        ad {
   1066      1.10        ad 	int error;
   1067      1.10        ad 
   1068      1.10        ad 	if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
   1069      1.10        ad 		return error;
   1070      1.12      matt #ifdef ELF_MD_PROBE_FUNC
   1071      1.12      matt 	if ((error = ELF_MD_PROBE_FUNC(l, epp, eh, itp, pos)) != 0)
   1072      1.12      matt 		return error;
   1073      1.12      matt #elif defined(ELF_INTERP_NON_RELOCATABLE)
   1074      1.10        ad 	*pos = ELF_LINK_ADDR;
   1075      1.10        ad #endif
   1076      1.29     joerg 	epp->ep_flags |= EXEC_FORCEAUX;
   1077      1.10        ad 	return 0;
   1078       1.1      fvdl }
   1079      1.35      matt 
   1080      1.35      matt void
   1081      1.35      matt elf_free_emul_arg(void *arg)
   1082      1.35      matt {
   1083      1.35      matt 	struct elf_args *ap = arg;
   1084      1.35      matt 	KASSERT(ap != NULL);
   1085      1.35      matt 	kmem_free(ap, sizeof(*ap));
   1086      1.35      matt }
   1087