Home | History | Annotate | Line # | Download | only in kern
exec_elf.c revision 1.10
      1  1.10        ad /*	$NetBSD: exec_elf.c,v 1.10 2008/11/19 18:36:06 ad Exp $	*/
      2  1.10        ad 
      3  1.10        ad /*-
      4  1.10        ad  * Copyright (c) 1994, 2000, 2005 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.10        ad  * by Christos Zoulas.
      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.10        ad __KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.10 2008/11/19 18:36:06 ad 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.1      fvdl #include <sys/malloc.h>
     69  1.10        ad #include <sys/kmem.h>
     70   1.1      fvdl #include <sys/namei.h>
     71   1.1      fvdl #include <sys/vnode.h>
     72   1.1      fvdl #include <sys/exec.h>
     73   1.1      fvdl #include <sys/exec_elf.h>
     74   1.8  christos #include <sys/syscall.h>
     75   1.8  christos #include <sys/signalvar.h>
     76  1.10        ad #include <sys/mount.h>
     77  1.10        ad #include <sys/stat.h>
     78  1.10        ad #include <sys/kauth.h>
     79  1.10        ad #include <sys/bitops.h>
     80  1.10        ad 
     81  1.10        ad #include <sys/cpu.h>
     82  1.10        ad #include <machine/reg.h>
     83   1.1      fvdl 
     84  1.10        ad #include <compat/common/compat_util.h>
     85   1.1      fvdl 
     86  1.10        ad #include <sys/pax.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.10        ad #define elf_load_file		ELFNAME(load_file)
     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.10        ad #define netbsd_elf_probe	ELFNAME2(netbsd,probe)
     97  1.10        ad #define	coredump		ELFNAMEEND(coredump)
     98   1.1      fvdl 
     99  1.10        ad int	elf_load_file(struct lwp *, struct exec_package *, char *,
    100  1.10        ad 	    struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
    101  1.10        ad void	elf_load_psection(struct exec_vmcmd_set *, struct vnode *,
    102  1.10        ad 	    const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
    103   1.6  christos 
    104  1.10        ad int	netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *);
    105  1.10        ad int	netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *,
    106  1.10        ad 	    vaddr_t *);
    107   1.1      fvdl 
    108  1.10        ad /* round up and down to page boundaries. */
    109  1.10        ad #define	ELF_ROUND(a, b)		(((a) + (b) - 1) & ~((b) - 1))
    110  1.10        ad #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
    111   1.1      fvdl 
    112  1.10        ad #define MAXPHNUM	50
    113   1.1      fvdl 
    114  1.10        ad #ifdef PAX_ASLR
    115   1.1      fvdl /*
    116  1.10        ad  * We don't move this code in kern_pax.c because it is compiled twice.
    117   1.8  christos  */
    118  1.10        ad static void
    119  1.10        ad pax_aslr_elf(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh,
    120  1.10        ad     Elf_Phdr *ph)
    121  1.10        ad {
    122  1.10        ad 	size_t pax_align = 0, pax_offset, i;
    123  1.10        ad 	uint32_t r;
    124  1.10        ad 
    125  1.10        ad 	if (!pax_aslr_active(l))
    126  1.10        ad 		return;
    127  1.10        ad 
    128  1.10        ad 	/*
    129  1.10        ad 	 * find align XXX: not all sections might have the same
    130  1.10        ad 	 * alignment
    131  1.10        ad 	 */
    132  1.10        ad 	for (i = 0; i < eh->e_phnum; i++)
    133  1.10        ad 		if (ph[i].p_type == PT_LOAD) {
    134  1.10        ad 			pax_align = ph[i].p_align;
    135  1.10        ad 			break;
    136  1.10        ad 		}
    137  1.10        ad 
    138  1.10        ad 	r = arc4random();
    139   1.8  christos 
    140  1.10        ad 	if (pax_align == 0)
    141  1.10        ad 		pax_align = PGSHIFT;
    142  1.10        ad #ifdef DEBUG_ASLR
    143  1.10        ad 	uprintf("r=0x%x a=0x%x p=0x%x Delta=0x%lx\n", r,
    144  1.10        ad 	    ilog2(pax_align), PGSHIFT, PAX_ASLR_DELTA(r,
    145  1.10        ad 		ilog2(pax_align), PAX_ASLR_DELTA_EXEC_LEN));
    146  1.10        ad #endif
    147  1.10        ad 	pax_offset = ELF_TRUNC(PAX_ASLR_DELTA(r,
    148  1.10        ad 	    ilog2(pax_align), PAX_ASLR_DELTA_EXEC_LEN), pax_align);
    149  1.10        ad 
    150  1.10        ad 	for (i = 0; i < eh->e_phnum; i++)
    151  1.10        ad 		ph[i].p_vaddr += pax_offset;
    152  1.10        ad 	eh->e_entry += pax_offset;
    153  1.10        ad #ifdef DEBUG_ASLR
    154  1.10        ad 	uprintf("pax offset=0x%zx entry=0x%llx\n",
    155  1.10        ad 	    pax_offset, (unsigned long long)eh->e_entry);
    156   1.8  christos #endif
    157  1.10        ad }
    158  1.10        ad #endif /* PAX_ASLR */
    159   1.8  christos 
    160   1.8  christos /*
    161   1.1      fvdl  * Copy arguments onto the stack in the normal way, but add some
    162   1.1      fvdl  * extra information in case of dynamic binding.
    163   1.1      fvdl  */
    164  1.10        ad int
    165  1.10        ad elf_copyargs(struct lwp *l, struct exec_package *pack,
    166  1.10        ad     struct ps_strings *arginfo, char **stackp, void *argp)
    167   1.1      fvdl {
    168  1.10        ad 	size_t len, vlen;
    169  1.10        ad 	AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname;
    170   1.1      fvdl 	struct elf_args *ap;
    171  1.10        ad 	int error;
    172   1.1      fvdl 
    173  1.10        ad 	if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0)
    174  1.10        ad 		return error;
    175  1.10        ad 
    176  1.10        ad 	a = ai;
    177  1.10        ad 	execname = NULL;
    178   1.1      fvdl 
    179   1.1      fvdl 	/*
    180   1.1      fvdl 	 * Push extra arguments on the stack needed by dynamically
    181   1.1      fvdl 	 * linked binaries
    182   1.1      fvdl 	 */
    183  1.10        ad 	if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
    184  1.10        ad 		struct vattr *vap = pack->ep_vap;
    185  1.10        ad 
    186  1.10        ad 		a->a_type = AT_PHDR;
    187  1.10        ad 		a->a_v = ap->arg_phaddr;
    188  1.10        ad 		a++;
    189  1.10        ad 
    190  1.10        ad 		a->a_type = AT_PHENT;
    191  1.10        ad 		a->a_v = ap->arg_phentsize;
    192  1.10        ad 		a++;
    193  1.10        ad 
    194  1.10        ad 		a->a_type = AT_PHNUM;
    195  1.10        ad 		a->a_v = ap->arg_phnum;
    196  1.10        ad 		a++;
    197   1.1      fvdl 
    198  1.10        ad 		a->a_type = AT_PAGESZ;
    199  1.10        ad 		a->a_v = PAGE_SIZE;
    200   1.1      fvdl 		a++;
    201   1.1      fvdl 
    202  1.10        ad 		a->a_type = AT_BASE;
    203  1.10        ad 		a->a_v = ap->arg_interp;
    204   1.1      fvdl 		a++;
    205   1.1      fvdl 
    206  1.10        ad 		a->a_type = AT_FLAGS;
    207  1.10        ad 		a->a_v = 0;
    208   1.1      fvdl 		a++;
    209   1.1      fvdl 
    210  1.10        ad 		a->a_type = AT_ENTRY;
    211  1.10        ad 		a->a_v = ap->arg_entry;
    212   1.1      fvdl 		a++;
    213   1.1      fvdl 
    214  1.10        ad 		a->a_type = AT_EUID;
    215  1.10        ad 		if (vap->va_mode & S_ISUID)
    216  1.10        ad 			a->a_v = vap->va_uid;
    217  1.10        ad 		else
    218  1.10        ad 			a->a_v = kauth_cred_geteuid(l->l_cred);
    219   1.1      fvdl 		a++;
    220   1.1      fvdl 
    221  1.10        ad 		a->a_type = AT_RUID;
    222  1.10        ad 		a->a_v = kauth_cred_getuid(l->l_cred);
    223   1.1      fvdl 		a++;
    224   1.1      fvdl 
    225  1.10        ad 		a->a_type = AT_EGID;
    226  1.10        ad 		if (vap->va_mode & S_ISGID)
    227  1.10        ad 			a->a_v = vap->va_gid;
    228  1.10        ad 		else
    229  1.10        ad 			a->a_v = kauth_cred_getegid(l->l_cred);
    230   1.1      fvdl 		a++;
    231   1.1      fvdl 
    232  1.10        ad 		a->a_type = AT_RGID;
    233  1.10        ad 		a->a_v = kauth_cred_getgid(l->l_cred);
    234   1.1      fvdl 		a++;
    235   1.1      fvdl 
    236  1.10        ad 		if (pack->ep_path) {
    237  1.10        ad 			execname = a;
    238  1.10        ad 			a->a_type = AT_SUN_EXECNAME;
    239  1.10        ad 			a++;
    240  1.10        ad 		}
    241  1.10        ad 
    242  1.10        ad 		free(ap, M_TEMP);
    243  1.10        ad 		pack->ep_emul_arg = NULL;
    244   1.1      fvdl 	}
    245  1.10        ad 
    246  1.10        ad 	a->a_type = AT_NULL;
    247  1.10        ad 	a->a_v = 0;
    248  1.10        ad 	a++;
    249  1.10        ad 
    250  1.10        ad 	vlen = (a - ai) * sizeof(AuxInfo);
    251  1.10        ad 
    252  1.10        ad 	if (execname) {
    253  1.10        ad 		char *path = pack->ep_path;
    254  1.10        ad 		execname->a_v = (uintptr_t)(*stackp + vlen);
    255  1.10        ad 		len = strlen(path) + 1;
    256  1.10        ad 		if ((error = copyout(path, (*stackp + vlen), len)) != 0)
    257  1.10        ad 			return error;
    258  1.10        ad 		len = ALIGN(len);
    259  1.10        ad 	} else
    260  1.10        ad 		len = 0;
    261  1.10        ad 
    262  1.10        ad 	if ((error = copyout(ai, *stackp, vlen)) != 0)
    263  1.10        ad 		return error;
    264  1.10        ad 	*stackp += vlen + len;
    265  1.10        ad 
    266  1.10        ad 	return 0;
    267   1.1      fvdl }
    268   1.1      fvdl 
    269   1.1      fvdl /*
    270   1.1      fvdl  * elf_check_header():
    271   1.1      fvdl  *
    272   1.1      fvdl  * Check header for validity; return 0 of ok ENOEXEC if error
    273   1.1      fvdl  */
    274   1.1      fvdl int
    275  1.10        ad elf_check_header(Elf_Ehdr *eh, int type)
    276   1.1      fvdl {
    277   1.3   thorpej 
    278  1.10        ad 	if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
    279  1.10        ad 	    eh->e_ident[EI_CLASS] != ELFCLASS)
    280   1.1      fvdl 		return ENOEXEC;
    281   1.1      fvdl 
    282   1.1      fvdl 	switch (eh->e_machine) {
    283  1.10        ad 
    284  1.10        ad 	ELFDEFNNAME(MACHDEP_ID_CASES)
    285   1.1      fvdl 
    286   1.1      fvdl 	default:
    287   1.1      fvdl 		return ENOEXEC;
    288   1.1      fvdl 	}
    289   1.1      fvdl 
    290  1.10        ad 	if (ELF_EHDR_FLAGS_OK(eh) == 0)
    291  1.10        ad 		return ENOEXEC;
    292  1.10        ad 
    293   1.1      fvdl 	if (eh->e_type != type)
    294   1.1      fvdl 		return ENOEXEC;
    295   1.1      fvdl 
    296  1.10        ad 	if (eh->e_shnum > 32768 || eh->e_phnum > 128)
    297  1.10        ad 		return ENOEXEC;
    298  1.10        ad 
    299   1.1      fvdl 	return 0;
    300   1.1      fvdl }
    301   1.1      fvdl 
    302   1.1      fvdl /*
    303   1.1      fvdl  * elf_load_psection():
    304  1.10        ad  *
    305   1.1      fvdl  * Load a psection at the appropriate address
    306   1.1      fvdl  */
    307  1.10        ad void
    308  1.10        ad elf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp,
    309  1.10        ad     const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
    310   1.1      fvdl {
    311  1.10        ad 	u_long msize, psize, rm, rf;
    312   1.1      fvdl 	long diff, offset;
    313   1.1      fvdl 
    314   1.1      fvdl 	/*
    315  1.10        ad 	 * If the user specified an address, then we load there.
    316  1.10        ad 	 */
    317  1.10        ad 	if (*addr == ELFDEFNNAME(NO_ADDR))
    318  1.10        ad 		*addr = ph->p_vaddr;
    319  1.10        ad 
    320  1.10        ad 	if (ph->p_align > 1) {
    321  1.10        ad 		/*
    322  1.10        ad 		 * Make sure we are virtually aligned as we are supposed to be.
    323  1.10        ad 		 */
    324  1.10        ad 		diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
    325  1.10        ad 		KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align));
    326  1.10        ad 		/*
    327  1.10        ad 		 * But make sure to not map any pages before the start of the
    328  1.10        ad 		 * psection by limiting the difference to within a page.
    329  1.10        ad 		 */
    330  1.10        ad 		diff &= PAGE_MASK;
    331  1.10        ad 	} else
    332  1.10        ad 		diff = 0;
    333   1.1      fvdl 
    334  1.10        ad 	*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
    335  1.10        ad 	*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
    336  1.10        ad 	*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
    337   1.1      fvdl 
    338  1.10        ad 	/*
    339  1.10        ad 	 * Adjust everything so it all starts on a page boundary.
    340  1.10        ad 	 */
    341  1.10        ad 	*addr -= diff;
    342   1.1      fvdl 	offset = ph->p_offset - diff;
    343   1.1      fvdl 	*size = ph->p_filesz + diff;
    344   1.1      fvdl 	msize = ph->p_memsz + diff;
    345   1.1      fvdl 
    346  1.10        ad 	if (ph->p_align >= PAGE_SIZE) {
    347  1.10        ad 		if ((ph->p_flags & PF_W) != 0) {
    348  1.10        ad 			/*
    349  1.10        ad 			 * Because the pagedvn pager can't handle zero fill
    350  1.10        ad 			 * of the last data page if it's not page aligned we
    351  1.10        ad 			 * map the last page readvn.
    352  1.10        ad 			 */
    353  1.10        ad 			psize = trunc_page(*size);
    354  1.10        ad 		} else {
    355  1.10        ad 			psize = round_page(*size);
    356  1.10        ad 		}
    357  1.10        ad 	} else {
    358  1.10        ad 		psize = *size;
    359  1.10        ad 	}
    360  1.10        ad 
    361  1.10        ad 	if (psize > 0) {
    362  1.10        ad 		NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
    363  1.10        ad 		    vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
    364  1.10        ad 		    offset, *prot, flags);
    365  1.10        ad 		flags &= VMCMD_RELATIVE;
    366  1.10        ad 	}
    367  1.10        ad 	if (psize < *size) {
    368  1.10        ad 		NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
    369  1.10        ad 		    *addr + psize, vp, offset + psize, *prot, flags);
    370  1.10        ad 	}
    371   1.1      fvdl 
    372   1.1      fvdl 	/*
    373  1.10        ad 	 * Check if we need to extend the size of the segment (does
    374  1.10        ad 	 * bss extend page the next page boundary)?
    375  1.10        ad 	 */
    376   1.1      fvdl 	rm = round_page(*addr + msize);
    377   1.1      fvdl 	rf = round_page(*addr + *size);
    378   1.1      fvdl 
    379   1.1      fvdl 	if (rm != rf) {
    380  1.10        ad 		NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
    381  1.10        ad 		    0, *prot, flags & VMCMD_RELATIVE);
    382   1.1      fvdl 		*size = msize;
    383   1.1      fvdl 	}
    384   1.1      fvdl }
    385   1.1      fvdl 
    386   1.1      fvdl /*
    387   1.1      fvdl  * elf_load_file():
    388   1.1      fvdl  *
    389   1.1      fvdl  * Load a file (interpreter/library) pointed to by path
    390   1.1      fvdl  * [stolen from coff_load_shlib()]. Made slightly generic
    391   1.1      fvdl  * so it might be used externally.
    392   1.1      fvdl  */
    393   1.1      fvdl int
    394  1.10        ad elf_load_file(struct lwp *l, struct exec_package *epp, char *path,
    395  1.10        ad     struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap,
    396  1.10        ad     Elf_Addr *last)
    397   1.1      fvdl {
    398   1.1      fvdl 	int error, i;
    399  1.10        ad 	struct vnode *vp;
    400  1.10        ad 	struct vattr attr;
    401  1.10        ad 	Elf_Ehdr eh;
    402  1.10        ad 	Elf_Phdr *ph = NULL;
    403  1.10        ad 	const Elf_Phdr *ph0;
    404  1.10        ad 	const Elf_Phdr *base_ph;
    405  1.10        ad 	const Elf_Phdr *last_ph;
    406   1.1      fvdl 	u_long phsize;
    407  1.10        ad 	Elf_Addr addr = *last;
    408  1.10        ad 	struct proc *p;
    409  1.10        ad 
    410  1.10        ad 	p = l->l_proc;
    411  1.10        ad 
    412  1.10        ad 	/*
    413  1.10        ad 	 * 1. open file
    414  1.10        ad 	 * 2. read filehdr
    415  1.10        ad 	 * 3. map text, data, and bss out of it using VM_*
    416  1.10        ad 	 */
    417  1.10        ad 	vp = epp->ep_interp;
    418  1.10        ad 	if (vp == NULL) {
    419  1.10        ad 		error = emul_find_interp(l, epp, path);
    420  1.10        ad 		if (error != 0)
    421  1.10        ad 			return error;
    422  1.10        ad 		vp = epp->ep_interp;
    423  1.10        ad 	}
    424  1.10        ad 	/* We'll tidy this ourselves - otherwise we have locking issues */
    425  1.10        ad 	epp->ep_interp = NULL;
    426  1.10        ad 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    427   1.1      fvdl 
    428   1.1      fvdl 	/*
    429  1.10        ad 	 * Similarly, if it's not marked as executable, or it's not a regular
    430  1.10        ad 	 * file, we don't allow it to be used.
    431  1.10        ad 	 */
    432  1.10        ad 	if (vp->v_type != VREG) {
    433  1.10        ad 		error = EACCES;
    434  1.10        ad 		goto badunlock;
    435  1.10        ad 	}
    436  1.10        ad 	if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0)
    437  1.10        ad 		goto badunlock;
    438  1.10        ad 
    439  1.10        ad 	/* get attributes */
    440  1.10        ad 	if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0)
    441  1.10        ad 		goto badunlock;
    442  1.10        ad 
    443  1.10        ad 	/*
    444  1.10        ad 	 * Check mount point.  Though we're not trying to exec this binary,
    445  1.10        ad 	 * we will be executing code from it, so if the mount point
    446  1.10        ad 	 * disallows execution or set-id-ness, we punt or kill the set-id.
    447  1.10        ad 	 */
    448  1.10        ad 	if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
    449  1.10        ad 		error = EACCES;
    450  1.10        ad 		goto badunlock;
    451   1.1      fvdl 	}
    452  1.10        ad 	if (vp->v_mount->mnt_flag & MNT_NOSUID)
    453  1.10        ad 		epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
    454  1.10        ad 
    455  1.10        ad #ifdef notyet /* XXX cgd 960926 */
    456  1.10        ad 	XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
    457  1.10        ad #endif
    458  1.10        ad 
    459  1.10        ad 	error = vn_marktext(vp);
    460  1.10        ad 	if (error)
    461  1.10        ad 		goto badunlock;
    462  1.10        ad 
    463  1.10        ad 	VOP_UNLOCK(vp, 0);
    464  1.10        ad 
    465  1.10        ad 	if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0)
    466  1.10        ad 		goto bad;
    467  1.10        ad 
    468  1.10        ad 	if ((error = elf_check_header(&eh, ET_DYN)) != 0)
    469   1.1      fvdl 		goto bad;
    470   1.1      fvdl 
    471  1.10        ad 	if (eh.e_phnum > MAXPHNUM || eh.e_phnum == 0) {
    472  1.10        ad 		error = ENOEXEC;
    473   1.1      fvdl 		goto bad;
    474  1.10        ad 	}
    475   1.1      fvdl 
    476  1.10        ad 	phsize = eh.e_phnum * sizeof(Elf_Phdr);
    477  1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    478   1.1      fvdl 
    479  1.10        ad 	if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0)
    480   1.1      fvdl 		goto bad;
    481   1.1      fvdl 
    482  1.10        ad #ifdef ELF_INTERP_NON_RELOCATABLE
    483  1.10        ad 	/*
    484  1.10        ad 	 * Evil hack:  Only MIPS should be non-relocatable, and the
    485  1.10        ad 	 * psections should have a high address (typically 0x5ffe0000).
    486  1.10        ad 	 * If it's now relocatable, it should be linked at 0 and the
    487  1.10        ad 	 * psections should have zeros in the upper part of the address.
    488  1.10        ad 	 * Otherwise, force the load at the linked address.
    489  1.10        ad 	 */
    490  1.10        ad 	if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0)
    491  1.10        ad 		*last = ELFDEFNNAME(NO_ADDR);
    492  1.10        ad #endif
    493  1.10        ad 
    494  1.10        ad 	/*
    495  1.10        ad 	 * If no position to load the interpreter was set by a probe
    496  1.10        ad 	 * function, pick the same address that a non-fixed mmap(0, ..)
    497  1.10        ad 	 * would (i.e. something safely out of the way).
    498  1.10        ad 	 */
    499  1.10        ad 	if (*last == ELFDEFNNAME(NO_ADDR)) {
    500  1.10        ad 		u_long limit = 0;
    501  1.10        ad 		/*
    502  1.10        ad 		 * Find the start and ending addresses of the psections to
    503  1.10        ad 		 * be loaded.  This will give us the size.
    504  1.10        ad 		 */
    505  1.10        ad 		for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum;
    506  1.10        ad 		     i++, ph0++) {
    507  1.10        ad 			if (ph0->p_type == PT_LOAD) {
    508  1.10        ad 				u_long psize = ph0->p_vaddr + ph0->p_memsz;
    509  1.10        ad 				if (base_ph == NULL)
    510  1.10        ad 					base_ph = ph0;
    511  1.10        ad 				if (psize > limit)
    512  1.10        ad 					limit = psize;
    513  1.10        ad 			}
    514  1.10        ad 		}
    515  1.10        ad 
    516  1.10        ad 		if (base_ph == NULL) {
    517  1.10        ad 			error = ENOEXEC;
    518  1.10        ad 			goto bad;
    519  1.10        ad 		}
    520  1.10        ad 
    521  1.10        ad 		/*
    522  1.10        ad 		 * Now compute the size and load address.
    523  1.10        ad 		 */
    524  1.10        ad 		addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p,
    525  1.10        ad 		    epp->ep_daddr,
    526  1.10        ad 		    round_page(limit) - trunc_page(base_ph->p_vaddr));
    527  1.10        ad 	} else
    528  1.10        ad 		addr = *last; /* may be ELF_LINK_ADDR */
    529  1.10        ad 
    530   1.1      fvdl 	/*
    531  1.10        ad 	 * Load all the necessary sections
    532  1.10        ad 	 */
    533  1.10        ad 	for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
    534  1.10        ad 	     i < eh.e_phnum; i++, ph0++) {
    535  1.10        ad 		switch (ph0->p_type) {
    536  1.10        ad 		case PT_LOAD: {
    537  1.10        ad 			u_long size;
    538  1.10        ad 			int prot = 0;
    539  1.10        ad 			int flags;
    540  1.10        ad 
    541  1.10        ad 			if (base_ph == NULL) {
    542  1.10        ad 				/*
    543  1.10        ad 				 * First encountered psection is always the
    544  1.10        ad 				 * base psection.  Make sure it's aligned
    545  1.10        ad 				 * properly (align down for topdown and align
    546  1.10        ad 				 * upwards for not topdown).
    547  1.10        ad 				 */
    548  1.10        ad 				base_ph = ph0;
    549  1.10        ad 				flags = VMCMD_BASE;
    550  1.10        ad 				if (addr == ELF_LINK_ADDR)
    551  1.10        ad 					addr = ph0->p_vaddr;
    552  1.10        ad 				if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
    553  1.10        ad 					addr = ELF_TRUNC(addr, ph0->p_align);
    554  1.10        ad 				else
    555  1.10        ad 					addr = ELF_ROUND(addr, ph0->p_align);
    556  1.10        ad 			} else {
    557  1.10        ad 				u_long limit = round_page(last_ph->p_vaddr
    558  1.10        ad 				    + last_ph->p_memsz);
    559  1.10        ad 				u_long base = trunc_page(ph0->p_vaddr);
    560  1.10        ad 
    561  1.10        ad 				/*
    562  1.10        ad 				 * If there is a gap in between the psections,
    563  1.10        ad 				 * map it as inaccessible so nothing else
    564  1.10        ad 				 * mmap'ed will be placed there.
    565  1.10        ad 				 */
    566  1.10        ad 				if (limit != base) {
    567  1.10        ad 					NEW_VMCMD2(vcset, vmcmd_map_zero,
    568  1.10        ad 					    base - limit,
    569  1.10        ad 					    limit - base_ph->p_vaddr, NULLVP,
    570  1.10        ad 					    0, VM_PROT_NONE, VMCMD_RELATIVE);
    571  1.10        ad 				}
    572   1.1      fvdl 
    573  1.10        ad 				addr = ph0->p_vaddr - base_ph->p_vaddr;
    574  1.10        ad 				flags = VMCMD_RELATIVE;
    575  1.10        ad 			}
    576  1.10        ad 			last_ph = ph0;
    577  1.10        ad 			elf_load_psection(vcset, vp, &ph[i], &addr,
    578  1.10        ad 			    &size, &prot, flags);
    579  1.10        ad 			/*
    580  1.10        ad 			 * If entry is within this psection then this
    581  1.10        ad 			 * must contain the .text section.  *entryoff is
    582  1.10        ad 			 * relative to the base psection.
    583  1.10        ad 			 */
    584  1.10        ad 			if (eh.e_entry >= ph0->p_vaddr &&
    585  1.10        ad 			    eh.e_entry < (ph0->p_vaddr + size)) {
    586  1.10        ad 				*entryoff = eh.e_entry - base_ph->p_vaddr;
    587   1.1      fvdl 			}
    588   1.1      fvdl 			addr += size;
    589   1.1      fvdl 			break;
    590  1.10        ad 		}
    591   1.1      fvdl 
    592  1.10        ad 		case PT_DYNAMIC:
    593  1.10        ad 		case PT_PHDR:
    594  1.10        ad 			break;
    595  1.10        ad 
    596  1.10        ad 		case PT_NOTE:
    597   1.1      fvdl 			break;
    598   1.1      fvdl 
    599   1.1      fvdl 		default:
    600   1.1      fvdl 			break;
    601   1.1      fvdl 		}
    602   1.1      fvdl 	}
    603   1.1      fvdl 
    604  1.10        ad 	kmem_free(ph, phsize);
    605  1.10        ad 	/*
    606  1.10        ad 	 * This value is ignored if TOPDOWN.
    607  1.10        ad 	 */
    608  1.10        ad 	*last = addr;
    609  1.10        ad 	vrele(vp);
    610  1.10        ad 	return 0;
    611  1.10        ad 
    612  1.10        ad badunlock:
    613  1.10        ad 	VOP_UNLOCK(vp, 0);
    614  1.10        ad 
    615   1.1      fvdl bad:
    616   1.1      fvdl 	if (ph != NULL)
    617  1.10        ad 		kmem_free(ph, phsize);
    618  1.10        ad #ifdef notyet /* XXX cgd 960926 */
    619  1.10        ad 	(maybe) VOP_CLOSE it
    620  1.10        ad #endif
    621  1.10        ad 	vrele(vp);
    622   1.1      fvdl 	return error;
    623   1.1      fvdl }
    624   1.1      fvdl 
    625   1.1      fvdl /*
    626   1.1      fvdl  * exec_elf_makecmds(): Prepare an Elf binary's exec package
    627   1.1      fvdl  *
    628   1.1      fvdl  * First, set of the various offsets/lengths in the exec package.
    629   1.1      fvdl  *
    630   1.1      fvdl  * Then, mark the text image busy (so it can be demand paged) or error
    631   1.1      fvdl  * out if this is not possible.  Finally, set up vmcmds for the
    632   1.1      fvdl  * text, data, bss, and stack segments.
    633   1.1      fvdl  */
    634   1.1      fvdl int
    635  1.10        ad exec_elf_makecmds(struct lwp *l, struct exec_package *epp)
    636  1.10        ad {
    637  1.10        ad 	Elf_Ehdr *eh = epp->ep_hdr;
    638  1.10        ad 	Elf_Phdr *ph, *pp;
    639  1.10        ad 	Elf_Addr phdr = 0, pos = 0;
    640  1.10        ad 	int error, i, nload;
    641  1.10        ad 	char *interp = NULL;
    642  1.10        ad 	u_long phsize;
    643   1.1      fvdl 	struct proc *p;
    644  1.10        ad 	bool is_dyn;
    645   1.1      fvdl 
    646  1.10        ad 	if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
    647   1.1      fvdl 		return ENOEXEC;
    648   1.1      fvdl 
    649  1.10        ad 	is_dyn = elf_check_header(eh, ET_DYN) == 0;
    650  1.10        ad 	/*
    651  1.10        ad 	 * XXX allow for executing shared objects. It seems silly
    652  1.10        ad 	 * but other ELF-based systems allow it as well.
    653  1.10        ad 	 */
    654  1.10        ad 	if (elf_check_header(eh, ET_EXEC) != 0 && !is_dyn)
    655   1.1      fvdl 		return ENOEXEC;
    656   1.1      fvdl 
    657  1.10        ad 	if (eh->e_phnum > MAXPHNUM || eh->e_phnum == 0)
    658  1.10        ad 		return ENOEXEC;
    659  1.10        ad 
    660  1.10        ad 	error = vn_marktext(epp->ep_vp);
    661  1.10        ad 	if (error)
    662  1.10        ad 		return error;
    663  1.10        ad 
    664   1.1      fvdl 	/*
    665  1.10        ad 	 * Allocate space to hold all the program headers, and read them
    666  1.10        ad 	 * from the file
    667  1.10        ad 	 */
    668  1.10        ad 	p = l->l_proc;
    669  1.10        ad 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    670  1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    671   1.1      fvdl 
    672  1.10        ad 	if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
    673  1.10        ad 	    0)
    674   1.1      fvdl 		goto bad;
    675   1.1      fvdl 
    676  1.10        ad 	epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
    677  1.10        ad 	epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
    678   1.1      fvdl 
    679   1.1      fvdl 	for (i = 0; i < eh->e_phnum; i++) {
    680   1.1      fvdl 		pp = &ph[i];
    681  1.10        ad 		if (pp->p_type == PT_INTERP) {
    682  1.10        ad 			if (pp->p_filesz >= MAXPATHLEN) {
    683  1.10        ad 				error = ENOEXEC;
    684   1.1      fvdl 				goto bad;
    685  1.10        ad 			}
    686  1.10        ad 			interp = PNBUF_GET();
    687  1.10        ad 			interp[0] = '\0';
    688  1.10        ad 			if ((error = exec_read_from(l, epp->ep_vp,
    689  1.10        ad 			    pp->p_offset, interp, pp->p_filesz)) != 0)
    690   1.1      fvdl 				goto bad;
    691   1.1      fvdl 			break;
    692   1.1      fvdl 		}
    693   1.1      fvdl 	}
    694   1.1      fvdl 
    695   1.1      fvdl 	/*
    696   1.1      fvdl 	 * On the same architecture, we may be emulating different systems.
    697  1.10        ad 	 * See which one will accept this executable.
    698   1.1      fvdl 	 *
    699   1.1      fvdl 	 * Probe functions would normally see if the interpreter (if any)
    700   1.1      fvdl 	 * exists. Emulation packages may possibly replace the interpreter in
    701  1.10        ad 	 * interp[] with a changed path (/emul/xxx/<path>).
    702   1.1      fvdl 	 */
    703  1.10        ad 	pos = ELFDEFNNAME(NO_ADDR);
    704  1.10        ad 	if (epp->ep_esch->u.elf_probe_func) {
    705  1.10        ad 		vaddr_t startp = (vaddr_t)pos;
    706   1.1      fvdl 
    707  1.10        ad 		error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp,
    708  1.10        ad 							  &startp);
    709   1.1      fvdl 		if (error)
    710   1.1      fvdl 			goto bad;
    711  1.10        ad 		pos = (Elf_Addr)startp;
    712   1.1      fvdl 	}
    713   1.1      fvdl 
    714  1.10        ad #if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
    715  1.10        ad 	p->p_pax = epp->ep_pax_flags;
    716  1.10        ad #endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
    717  1.10        ad 
    718  1.10        ad #ifdef PAX_ASLR
    719  1.10        ad 	if (is_dyn)
    720  1.10        ad 		pax_aslr_elf(l, epp, eh, ph);
    721  1.10        ad #endif /* PAX_ASLR */
    722  1.10        ad 
    723   1.1      fvdl 	/*
    724  1.10        ad 	 * Load all the necessary sections
    725  1.10        ad 	 */
    726   1.4      fvdl 	for (i = nload = 0; i < eh->e_phnum; i++) {
    727  1.10        ad 		Elf_Addr  addr = ELFDEFNNAME(NO_ADDR);
    728  1.10        ad 		u_long size = 0;
    729   1.1      fvdl 		int prot = 0;
    730   1.1      fvdl 
    731   1.1      fvdl 		pp = &ph[i];
    732   1.1      fvdl 
    733   1.1      fvdl 		switch (ph[i].p_type) {
    734  1.10        ad 		case PT_LOAD:
    735   1.4      fvdl 			/*
    736   1.4      fvdl 			 * XXX
    737   1.4      fvdl 			 * Can handle only 2 sections: text and data
    738   1.4      fvdl 			 */
    739  1.10        ad 			if (nload++ == 2) {
    740  1.10        ad 				error = ENOEXEC;
    741   1.4      fvdl 				goto bad;
    742  1.10        ad 			}
    743   1.1      fvdl 			elf_load_psection(&epp->ep_vmcmds, epp->ep_vp,
    744  1.10        ad 			    &ph[i], &addr, &size, &prot, VMCMD_FIXED);
    745  1.10        ad 
    746   1.4      fvdl 			/*
    747   1.4      fvdl 			 * Decide whether it's text or data by looking
    748   1.4      fvdl 			 * at the entry point.
    749   1.4      fvdl 			 */
    750  1.10        ad 			if (eh->e_entry >= addr &&
    751  1.10        ad 			    eh->e_entry < (addr + size)) {
    752   1.4      fvdl 				epp->ep_taddr = addr;
    753   1.4      fvdl 				epp->ep_tsize = size;
    754  1.10        ad 				if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
    755  1.10        ad 					epp->ep_daddr = addr;
    756  1.10        ad 					epp->ep_dsize = size;
    757  1.10        ad 				}
    758   1.4      fvdl 			} else {
    759   1.4      fvdl 				epp->ep_daddr = addr;
    760   1.4      fvdl 				epp->ep_dsize = size;
    761   1.4      fvdl 			}
    762   1.1      fvdl 			break;
    763   1.1      fvdl 
    764  1.10        ad 		case PT_SHLIB:
    765  1.10        ad 			/* SCO has these sections. */
    766  1.10        ad 		case PT_INTERP:
    767  1.10        ad 			/* Already did this one. */
    768  1.10        ad 		case PT_DYNAMIC:
    769  1.10        ad 			break;
    770  1.10        ad 		case PT_NOTE:
    771   1.1      fvdl 			break;
    772  1.10        ad 		case PT_PHDR:
    773   1.4      fvdl 			/* Note address of program headers (in text segment) */
    774   1.4      fvdl 			phdr = pp->p_vaddr;
    775   1.7  christos 			break;
    776   1.4      fvdl 
    777   1.1      fvdl 		default:
    778   1.1      fvdl 			/*
    779  1.10        ad 			 * Not fatal; we don't need to understand everything.
    780   1.1      fvdl 			 */
    781   1.1      fvdl 			break;
    782   1.1      fvdl 		}
    783   1.1      fvdl 	}
    784   1.5      fvdl 
    785   1.5      fvdl 	/*
    786  1.10        ad 	 * Check if we found a dynamically linked binary and arrange to load
    787  1.10        ad 	 * its interpreter
    788   1.5      fvdl 	 */
    789  1.10        ad 	if (interp) {
    790   1.1      fvdl 		struct elf_args *ap;
    791  1.10        ad 		int j = epp->ep_vmcmds.evs_used;
    792  1.10        ad 		u_long interp_offset;
    793   1.1      fvdl 
    794  1.10        ad 		MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
    795  1.10        ad 		    M_TEMP, M_WAITOK);
    796  1.10        ad 		if ((error = elf_load_file(l, epp, interp,
    797  1.10        ad 		    &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
    798  1.10        ad 			FREE(ap, M_TEMP);
    799   1.1      fvdl 			goto bad;
    800   1.1      fvdl 		}
    801  1.10        ad 		ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr;
    802  1.10        ad 		epp->ep_entry = ap->arg_interp + interp_offset;
    803   1.4      fvdl 		ap->arg_phaddr = phdr;
    804   1.1      fvdl 
    805   1.1      fvdl 		ap->arg_phentsize = eh->e_phentsize;
    806   1.1      fvdl 		ap->arg_phnum = eh->e_phnum;
    807   1.1      fvdl 		ap->arg_entry = eh->e_entry;
    808   1.1      fvdl 
    809   1.1      fvdl 		epp->ep_emul_arg = ap;
    810  1.10        ad 
    811  1.10        ad 		PNBUF_PUT(interp);
    812   1.1      fvdl 	} else
    813   1.1      fvdl 		epp->ep_entry = eh->e_entry;
    814   1.1      fvdl 
    815   1.8  christos #ifdef ELF_MAP_PAGE_ZERO
    816   1.8  christos 	/* Dell SVR4 maps page zero, yeuch! */
    817  1.10        ad 	NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
    818  1.10        ad 	    epp->ep_vp, 0, VM_PROT_READ);
    819   1.8  christos #endif
    820  1.10        ad 	kmem_free(ph, phsize);
    821  1.10        ad 	return (*epp->ep_esch->es_setup_stack)(l, epp);
    822   1.1      fvdl 
    823   1.1      fvdl bad:
    824  1.10        ad 	if (interp)
    825  1.10        ad 		PNBUF_PUT(interp);
    826  1.10        ad 	kmem_free(ph, phsize);
    827   1.1      fvdl 	kill_vmcmds(&epp->ep_vmcmds);
    828  1.10        ad 	return error;
    829  1.10        ad }
    830  1.10        ad 
    831  1.10        ad int
    832  1.10        ad netbsd_elf_signature(struct lwp *l, struct exec_package *epp,
    833  1.10        ad     Elf_Ehdr *eh)
    834  1.10        ad {
    835  1.10        ad 	size_t i;
    836  1.10        ad 	Elf_Phdr *ph;
    837  1.10        ad 	size_t phsize;
    838  1.10        ad 	int error;
    839  1.10        ad 	int isnetbsd = 0;
    840  1.10        ad 	char *ndata;
    841  1.10        ad 
    842  1.10        ad 	epp->ep_pax_flags = 0;
    843  1.10        ad 	if (eh->e_phnum > MAXPHNUM || eh->e_phnum == 0)
    844  1.10        ad 		return ENOEXEC;
    845  1.10        ad 
    846  1.10        ad 	phsize = eh->e_phnum * sizeof(Elf_Phdr);
    847  1.10        ad 	ph = kmem_alloc(phsize, KM_SLEEP);
    848  1.10        ad 	error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize);
    849  1.10        ad 	if (error)
    850  1.10        ad 		goto out;
    851  1.10        ad 
    852  1.10        ad 	for (i = 0; i < eh->e_phnum; i++) {
    853  1.10        ad 		Elf_Phdr *ephp = &ph[i];
    854  1.10        ad 		Elf_Nhdr *np;
    855  1.10        ad 
    856  1.10        ad 		if (ephp->p_type != PT_NOTE ||
    857  1.10        ad 		    ephp->p_filesz > 1024 ||
    858  1.10        ad 		    ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
    859  1.10        ad 			continue;
    860  1.10        ad 
    861  1.10        ad 		np = kmem_alloc(ephp->p_filesz, KM_SLEEP);
    862  1.10        ad 		error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np,
    863  1.10        ad 		    ephp->p_filesz);
    864  1.10        ad 		if (error)
    865  1.10        ad 			goto next;
    866  1.10        ad 
    867  1.10        ad 		ndata = (char *)(np + 1);
    868  1.10        ad 		switch (np->n_type) {
    869  1.10        ad 		case ELF_NOTE_TYPE_NETBSD_TAG:
    870  1.10        ad 			if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
    871  1.10        ad 			    np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
    872  1.10        ad 			    memcmp(ndata, ELF_NOTE_NETBSD_NAME,
    873  1.10        ad 			    ELF_NOTE_NETBSD_NAMESZ))
    874  1.10        ad 				goto next;
    875  1.10        ad 			isnetbsd = 1;
    876  1.10        ad 			break;
    877  1.10        ad 
    878  1.10        ad 		case ELF_NOTE_TYPE_PAX_TAG:
    879  1.10        ad 			if (np->n_namesz != ELF_NOTE_PAX_NAMESZ ||
    880  1.10        ad 			    np->n_descsz != ELF_NOTE_PAX_DESCSZ ||
    881  1.10        ad 			    memcmp(ndata, ELF_NOTE_PAX_NAME,
    882  1.10        ad 			    ELF_NOTE_PAX_NAMESZ))
    883  1.10        ad 				goto next;
    884  1.10        ad 			(void)memcpy(&epp->ep_pax_flags,
    885  1.10        ad 			    ndata + ELF_NOTE_PAX_NAMESZ,
    886  1.10        ad 			    sizeof(epp->ep_pax_flags));
    887  1.10        ad 			break;
    888  1.10        ad 
    889  1.10        ad 		default:
    890  1.10        ad 			break;
    891  1.10        ad 		}
    892  1.10        ad 
    893  1.10        ad next:
    894  1.10        ad 		kmem_free(np, ephp->p_filesz);
    895  1.10        ad 		continue;
    896  1.10        ad 	}
    897  1.10        ad 
    898  1.10        ad 	error = isnetbsd ? 0 : ENOEXEC;
    899  1.10        ad out:
    900  1.10        ad 	kmem_free(ph, phsize);
    901  1.10        ad 	return error;
    902  1.10        ad }
    903  1.10        ad 
    904  1.10        ad int
    905  1.10        ad netbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp,
    906  1.10        ad     vaddr_t *pos)
    907  1.10        ad {
    908  1.10        ad 	int error;
    909  1.10        ad 
    910  1.10        ad 	if ((error = netbsd_elf_signature(l, epp, eh)) != 0)
    911  1.10        ad 		return error;
    912  1.10        ad #ifdef ELF_INTERP_NON_RELOCATABLE
    913  1.10        ad 	*pos = ELF_LINK_ADDR;
    914  1.10        ad #endif
    915  1.10        ad 	return 0;
    916   1.1      fvdl }
    917