exec_elf32.c revision 1.127
11.127Schristos/* $NetBSD: exec_elf32.c,v 1.127 2007/12/03 02:06:58 christos Exp $ */ 21.36Schristos 31.36Schristos/*- 41.102Smycroft * Copyright (c) 1994, 2000, 2005 The NetBSD Foundation, Inc. 51.36Schristos * All rights reserved. 61.36Schristos * 71.36Schristos * This code is derived from software contributed to The NetBSD Foundation 81.36Schristos * by Christos Zoulas. 91.36Schristos * 101.36Schristos * Redistribution and use in source and binary forms, with or without 111.36Schristos * modification, are permitted provided that the following conditions 121.36Schristos * are met: 131.36Schristos * 1. Redistributions of source code must retain the above copyright 141.36Schristos * notice, this list of conditions and the following disclaimer. 151.36Schristos * 2. Redistributions in binary form must reproduce the above copyright 161.36Schristos * notice, this list of conditions and the following disclaimer in the 171.36Schristos * documentation and/or other materials provided with the distribution. 181.36Schristos * 3. All advertising materials mentioning features or use of this software 191.36Schristos * must display the following acknowledgement: 201.37Schristos * This product includes software developed by the NetBSD 211.37Schristos * Foundation, Inc. and its contributors. 221.36Schristos * 4. Neither the name of The NetBSD Foundation nor the names of its 231.36Schristos * contributors may be used to endorse or promote products derived 241.36Schristos * from this software without specific prior written permission. 251.36Schristos * 261.36Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 271.36Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 281.36Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 291.36Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 301.36Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 311.36Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 321.36Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 331.36Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 341.36Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 351.36Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 361.36Schristos * POSSIBILITY OF SUCH DAMAGE. 371.36Schristos */ 381.1Sfvdl 391.1Sfvdl/* 401.9Scgd * Copyright (c) 1996 Christopher G. Demetriou 411.1Sfvdl * All rights reserved. 421.1Sfvdl * 431.1Sfvdl * Redistribution and use in source and binary forms, with or without 441.1Sfvdl * modification, are permitted provided that the following conditions 451.1Sfvdl * are met: 461.1Sfvdl * 1. Redistributions of source code must retain the above copyright 471.1Sfvdl * notice, this list of conditions and the following disclaimer. 481.1Sfvdl * 2. Redistributions in binary form must reproduce the above copyright 491.1Sfvdl * notice, this list of conditions and the following disclaimer in the 501.1Sfvdl * documentation and/or other materials provided with the distribution. 511.1Sfvdl * 3. The name of the author may not be used to endorse or promote products 521.1Sfvdl * derived from this software without specific prior written permission 531.1Sfvdl * 541.1Sfvdl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 551.1Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 561.1Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 571.1Sfvdl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 581.1Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 591.1Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 601.1Sfvdl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 611.1Sfvdl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 621.1Sfvdl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 631.1Sfvdl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 641.1Sfvdl */ 651.69Slukem 661.69Slukem#include <sys/cdefs.h> 671.127Schristos__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.127 2007/12/03 02:06:58 christos Exp $"); 681.1Sfvdl 691.9Scgd/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 701.9Scgd#ifndef ELFSIZE 711.9Scgd#define ELFSIZE 32 721.9Scgd#endif 731.30Sthorpej 741.114Selad#ifdef _KERNEL_OPT 751.113Selad#include "opt_pax.h" 761.114Selad#endif /* _KERNEL_OPT */ 771.113Selad 781.1Sfvdl#include <sys/param.h> 791.1Sfvdl#include <sys/proc.h> 801.1Sfvdl#include <sys/malloc.h> 811.1Sfvdl#include <sys/namei.h> 821.1Sfvdl#include <sys/vnode.h> 831.1Sfvdl#include <sys/exec.h> 841.1Sfvdl#include <sys/exec_elf.h> 851.8Schristos#include <sys/syscall.h> 861.8Schristos#include <sys/signalvar.h> 871.14Scgd#include <sys/mount.h> 881.14Scgd#include <sys/stat.h> 891.112Selad#include <sys/kauth.h> 901.1Sfvdl 911.125Sad#include <sys/cpu.h> 921.58Sjdolecek#include <machine/reg.h> 931.57Sthorpej 941.123Sdsl#include <compat/common/compat_util.h> 951.123Sdsl 961.119Selad#if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) 971.113Selad#include <sys/pax.h> 981.119Selad#endif /* PAX_MPROTECT || PAX_SEGVGUARD */ 991.113Selad 1001.58Sjdolecekextern const struct emul emul_netbsd; 1011.42Schristos 1021.105Sjunyoung#define elf_check_header ELFNAME(check_header) 1031.105Sjunyoung#define elf_copyargs ELFNAME(copyargs) 1041.105Sjunyoung#define elf_load_file ELFNAME(load_file) 1051.105Sjunyoung#define elf_load_psection ELFNAME(load_psection) 1061.105Sjunyoung#define exec_elf_makecmds ELFNAME2(exec,makecmds) 1071.105Sjunyoung#define netbsd_elf_signature ELFNAME2(netbsd,signature) 1081.105Sjunyoung#define netbsd_elf_probe ELFNAME2(netbsd,probe) 1091.105Sjunyoung 1101.108Schristosint elf_load_file(struct lwp *, struct exec_package *, char *, 1111.54Sthorpej struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 1121.105Sjunyoungvoid elf_load_psection(struct exec_vmcmd_set *, struct vnode *, 1131.54Sthorpej const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 1141.54Sthorpej 1151.108Schristosint netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *); 1161.108Schristosint netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *, 1171.105Sjunyoung vaddr_t *); 1181.8Schristos 1191.18Scgd/* round up and down to page boundaries. */ 1201.18Scgd#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 1211.18Scgd#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 1221.8Schristos 1231.90Schristos#define MAXPHNUM 50 1241.90Schristos 1251.8Schristos/* 1261.1Sfvdl * Copy arguments onto the stack in the normal way, but add some 1271.1Sfvdl * extra information in case of dynamic binding. 1281.1Sfvdl */ 1291.66Schristosint 1301.108Schristoself_copyargs(struct lwp *l, struct exec_package *pack, 1311.72Schristos struct ps_strings *arginfo, char **stackp, void *argp) 1321.1Sfvdl{ 1331.127Schristos size_t len, vlen; 1341.127Schristos AuxInfo ai[ELF_AUX_ENTRIES], *a, *execname; 1351.1Sfvdl struct elf_args *ap; 1361.66Schristos int error; 1371.1Sfvdl 1381.108Schristos if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 1391.66Schristos return error; 1401.1Sfvdl 1411.22Scgd a = ai; 1421.127Schristos execname = NULL; 1431.22Scgd 1441.1Sfvdl /* 1451.1Sfvdl * Push extra arguments on the stack needed by dynamically 1461.1Sfvdl * linked binaries 1471.1Sfvdl */ 1481.17Scgd if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 1491.77Sjdolecek struct vattr *vap = pack->ep_vap; 1501.1Sfvdl 1511.46Skleink a->a_type = AT_PHDR; 1521.46Skleink a->a_v = ap->arg_phaddr; 1531.1Sfvdl a++; 1541.1Sfvdl 1551.46Skleink a->a_type = AT_PHENT; 1561.46Skleink a->a_v = ap->arg_phentsize; 1571.1Sfvdl a++; 1581.1Sfvdl 1591.46Skleink a->a_type = AT_PHNUM; 1601.46Skleink a->a_v = ap->arg_phnum; 1611.1Sfvdl a++; 1621.1Sfvdl 1631.46Skleink a->a_type = AT_PAGESZ; 1641.57Sthorpej a->a_v = PAGE_SIZE; 1651.1Sfvdl a++; 1661.1Sfvdl 1671.46Skleink a->a_type = AT_BASE; 1681.46Skleink a->a_v = ap->arg_interp; 1691.1Sfvdl a++; 1701.1Sfvdl 1711.46Skleink a->a_type = AT_FLAGS; 1721.46Skleink a->a_v = 0; 1731.1Sfvdl a++; 1741.1Sfvdl 1751.46Skleink a->a_type = AT_ENTRY; 1761.46Skleink a->a_v = ap->arg_entry; 1771.72Schristos a++; 1781.72Schristos 1791.72Schristos a->a_type = AT_EUID; 1801.77Sjdolecek if (vap->va_mode & S_ISUID) 1811.77Sjdolecek a->a_v = vap->va_uid; 1821.77Sjdolecek else 1831.115Sad a->a_v = kauth_cred_geteuid(l->l_cred); 1841.72Schristos a++; 1851.72Schristos 1861.72Schristos a->a_type = AT_RUID; 1871.115Sad a->a_v = kauth_cred_getuid(l->l_cred); 1881.72Schristos a++; 1891.72Schristos 1901.72Schristos a->a_type = AT_EGID; 1911.77Sjdolecek if (vap->va_mode & S_ISGID) 1921.77Sjdolecek a->a_v = vap->va_gid; 1931.77Sjdolecek else 1941.115Sad a->a_v = kauth_cred_getegid(l->l_cred); 1951.72Schristos a++; 1961.72Schristos 1971.72Schristos a->a_type = AT_RGID; 1981.115Sad a->a_v = kauth_cred_getgid(l->l_cred); 1991.1Sfvdl a++; 2001.1Sfvdl 2011.127Schristos if (pack->ep_path) { 2021.127Schristos execname = a; 2031.127Schristos a->a_type = AT_SUN_EXECNAME; 2041.127Schristos a++; 2051.127Schristos } 2061.127Schristos 2071.76Schs free(ap, M_TEMP); 2081.9Scgd pack->ep_emul_arg = NULL; 2091.1Sfvdl } 2101.22Scgd 2111.46Skleink a->a_type = AT_NULL; 2121.46Skleink a->a_v = 0; 2131.22Scgd a++; 2141.22Scgd 2151.127Schristos vlen = (a - ai) * sizeof(AuxInfo); 2161.127Schristos 2171.127Schristos if (execname) { 2181.127Schristos char *path = pack->ep_path; 2191.127Schristos execname->a_v = (intptr_t)(*stackp + vlen); 2201.127Schristos len = strlen(path) + 1; 2211.127Schristos if ((error = copyout(path, (*stackp + vlen), len)) != 0) 2221.127Schristos return error; 2231.127Schristos len = ALIGN(len); 2241.127Schristos } else 2251.127Schristos len = 0; 2261.127Schristos 2271.127Schristos if ((error = copyout(ai, *stackp, vlen)) != 0) 2281.66Schristos return error; 2291.127Schristos *stackp += vlen + len; 2301.22Scgd 2311.66Schristos return 0; 2321.1Sfvdl} 2331.1Sfvdl 2341.1Sfvdl/* 2351.1Sfvdl * elf_check_header(): 2361.1Sfvdl * 2371.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error 2381.1Sfvdl */ 2391.17Scgdint 2401.105Sjunyoungelf_check_header(Elf_Ehdr *eh, int type) 2411.1Sfvdl{ 2421.3Sthorpej 2431.46Skleink if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 2441.46Skleink eh->e_ident[EI_CLASS] != ELFCLASS) 2451.106Sjunyoung return ENOEXEC; 2461.1Sfvdl 2471.1Sfvdl switch (eh->e_machine) { 2481.9Scgd 2491.10Scgd ELFDEFNNAME(MACHDEP_ID_CASES) 2501.1Sfvdl 2511.1Sfvdl default: 2521.106Sjunyoung return ENOEXEC; 2531.1Sfvdl } 2541.70Sthorpej 2551.70Sthorpej if (ELF_EHDR_FLAGS_OK(eh) == 0) 2561.106Sjunyoung return ENOEXEC; 2571.1Sfvdl 2581.1Sfvdl if (eh->e_type != type) 2591.106Sjunyoung return ENOEXEC; 2601.61Smycroft 2611.100Schristos if (eh->e_shnum > 32768 || eh->e_phnum > 128) 2621.106Sjunyoung return ENOEXEC; 2631.1Sfvdl 2641.106Sjunyoung return 0; 2651.1Sfvdl} 2661.1Sfvdl 2671.1Sfvdl/* 2681.1Sfvdl * elf_load_psection(): 2691.17Scgd * 2701.1Sfvdl * Load a psection at the appropriate address 2711.1Sfvdl */ 2721.17Scgdvoid 2731.105Sjunyoungelf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp, 2741.54Sthorpej const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags) 2751.1Sfvdl{ 2761.87Smatt u_long msize, psize, rm, rf; 2771.1Sfvdl long diff, offset; 2781.1Sfvdl 2791.1Sfvdl /* 2801.17Scgd * If the user specified an address, then we load there. 2811.17Scgd */ 2821.87Smatt if (*addr == ELFDEFNNAME(NO_ADDR)) 2831.87Smatt *addr = ph->p_vaddr; 2841.87Smatt 2851.87Smatt if (ph->p_align > 1) { 2861.87Smatt /* 2871.87Smatt * Make sure we are virtually aligned as we are supposed to be. 2881.87Smatt */ 2891.87Smatt diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 2901.87Smatt KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 2911.87Smatt /* 2921.87Smatt * But make sure to not map any pages before the start of the 2931.87Smatt * psection by limiting the difference to within a page. 2941.87Smatt */ 2951.101Sperry diff &= PAGE_MASK; 2961.87Smatt } else 2971.87Smatt diff = 0; 2981.1Sfvdl 2991.46Skleink *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 3001.46Skleink *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 3011.46Skleink *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 3021.1Sfvdl 3031.87Smatt /* 3041.87Smatt * Adjust everything so it all starts on a page boundary. 3051.87Smatt */ 3061.87Smatt *addr -= diff; 3071.1Sfvdl offset = ph->p_offset - diff; 3081.1Sfvdl *size = ph->p_filesz + diff; 3091.1Sfvdl msize = ph->p_memsz + diff; 3101.1Sfvdl 3111.65Schristos if (ph->p_align >= PAGE_SIZE) { 3121.65Schristos if ((ph->p_flags & PF_W) != 0) { 3131.65Schristos /* 3141.65Schristos * Because the pagedvn pager can't handle zero fill 3151.65Schristos * of the last data page if it's not page aligned we 3161.65Schristos * map the last page readvn. 3171.65Schristos */ 3181.65Schristos psize = trunc_page(*size); 3191.65Schristos } else { 3201.65Schristos psize = round_page(*size); 3211.65Schristos } 3221.65Schristos } else { 3231.65Schristos psize = *size; 3241.53Smatt } 3251.65Schristos 3261.53Smatt if (psize > 0) { 3271.65Schristos NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 3281.65Schristos vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 3291.53Smatt offset, *prot, flags); 3301.87Smatt flags &= VMCMD_RELATIVE; 3311.53Smatt } 3321.53Smatt if (psize < *size) { 3331.53Smatt NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 3341.87Smatt *addr + psize, vp, offset + psize, *prot, flags); 3351.53Smatt } 3361.1Sfvdl 3371.1Sfvdl /* 3381.87Smatt * Check if we need to extend the size of the segment (does 3391.87Smatt * bss extend page the next page boundary)? 3401.17Scgd */ 3411.1Sfvdl rm = round_page(*addr + msize); 3421.1Sfvdl rf = round_page(*addr + *size); 3431.1Sfvdl 3441.1Sfvdl if (rm != rf) { 3451.53Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 3461.83Smatt 0, *prot, flags & VMCMD_RELATIVE); 3471.1Sfvdl *size = msize; 3481.1Sfvdl } 3491.1Sfvdl} 3501.1Sfvdl 3511.1Sfvdl/* 3521.1Sfvdl * elf_load_file(): 3531.1Sfvdl * 3541.1Sfvdl * Load a file (interpreter/library) pointed to by path 3551.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic 3561.1Sfvdl * so it might be used externally. 3571.1Sfvdl */ 3581.17Scgdint 3591.108Schristoself_load_file(struct lwp *l, struct exec_package *epp, char *path, 3601.117Syamt struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap, 3611.117Syamt Elf_Addr *last) 3621.1Sfvdl{ 3631.83Smatt int error, i; 3641.14Scgd struct vnode *vp; 3651.14Scgd struct vattr attr; 3661.9Scgd Elf_Ehdr eh; 3671.9Scgd Elf_Phdr *ph = NULL; 3681.83Smatt const Elf_Phdr *ph0; 3691.83Smatt const Elf_Phdr *base_ph; 3701.83Smatt const Elf_Phdr *last_ph; 3711.1Sfvdl u_long phsize; 3721.9Scgd Elf_Addr addr = *last; 3731.108Schristos struct proc *p; 3741.108Schristos 3751.108Schristos p = l->l_proc; 3761.1Sfvdl 3771.1Sfvdl /* 3781.17Scgd * 1. open file 3791.17Scgd * 2. read filehdr 3801.17Scgd * 3. map text, data, and bss out of it using VM_* 3811.17Scgd */ 3821.123Sdsl vp = epp->ep_interp; 3831.123Sdsl if (vp == NULL) { 3841.123Sdsl error = emul_find_interp(l, epp, path); 3851.123Sdsl if (error != 0) 3861.123Sdsl return error; 3871.123Sdsl vp = epp->ep_interp; 3881.123Sdsl } 3891.123Sdsl /* We'll tidy this ourselves - otherwise we have locking issues */ 3901.123Sdsl epp->ep_interp = NULL; 3911.123Sdsl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 3921.14Scgd 3931.26Smycroft /* 3941.26Smycroft * Similarly, if it's not marked as executable, or it's not a regular 3951.26Smycroft * file, we don't allow it to be used. 3961.26Smycroft */ 3971.14Scgd if (vp->v_type != VREG) { 3981.14Scgd error = EACCES; 3991.14Scgd goto badunlock; 4001.14Scgd } 4011.126Spooka if ((error = VOP_ACCESS(vp, VEXEC, l->l_cred)) != 0) 4021.26Smycroft goto badunlock; 4031.14Scgd 4041.17Scgd /* get attributes */ 4051.126Spooka if ((error = VOP_GETATTR(vp, &attr, l->l_cred)) != 0) 4061.14Scgd goto badunlock; 4071.14Scgd 4081.14Scgd /* 4091.14Scgd * Check mount point. Though we're not trying to exec this binary, 4101.15Scgd * we will be executing code from it, so if the mount point 4111.15Scgd * disallows execution or set-id-ness, we punt or kill the set-id. 4121.14Scgd */ 4131.14Scgd if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 4141.14Scgd error = EACCES; 4151.14Scgd goto badunlock; 4161.14Scgd } 4171.14Scgd if (vp->v_mount->mnt_flag & MNT_NOSUID) 4181.24Smycroft epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 4191.14Scgd 4201.12Scgd#ifdef notyet /* XXX cgd 960926 */ 4211.12Scgd XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 4221.12Scgd#endif 4231.76Schs 4241.76Schs error = vn_marktext(vp); 4251.76Schs if (error) 4261.76Schs goto badunlock; 4271.76Schs 4281.28Sfvdl VOP_UNLOCK(vp, 0); 4291.12Scgd 4301.108Schristos if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0) 4311.1Sfvdl goto bad; 4321.1Sfvdl 4331.105Sjunyoung if ((error = elf_check_header(&eh, ET_DYN)) != 0) 4341.1Sfvdl goto bad; 4351.1Sfvdl 4361.90Schristos if (eh.e_phnum > MAXPHNUM) 4371.90Schristos goto bad; 4381.90Schristos 4391.9Scgd phsize = eh.e_phnum * sizeof(Elf_Phdr); 4401.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 4411.1Sfvdl 4421.108Schristos if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0) 4431.1Sfvdl goto bad; 4441.1Sfvdl 4451.107Ssimonb#ifdef ELF_INTERP_NON_RELOCATABLE 4461.107Ssimonb /* 4471.107Ssimonb * Evil hack: Only MIPS should be non-relocatable, and the 4481.107Ssimonb * psections should have a high address (typically 0x5ffe0000). 4491.107Ssimonb * If it's now relocatable, it should be linked at 0 and the 4501.107Ssimonb * psections should have zeros in the upper part of the address. 4511.107Ssimonb * Otherwise, force the load at the linked address. 4521.107Ssimonb */ 4531.107Ssimonb if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0) 4541.107Ssimonb *last = ELFDEFNNAME(NO_ADDR); 4551.107Ssimonb#endif 4561.107Ssimonb 4571.1Sfvdl /* 4581.83Smatt * If no position to load the interpreter was set by a probe 4591.83Smatt * function, pick the same address that a non-fixed mmap(0, ..) 4601.83Smatt * would (i.e. something safely out of the way). 4611.83Smatt */ 4621.83Smatt if (*last == ELFDEFNNAME(NO_ADDR)) { 4631.83Smatt u_long limit = 0; 4641.83Smatt /* 4651.83Smatt * Find the start and ending addresses of the psections to 4661.83Smatt * be loaded. This will give us the size. 4671.83Smatt */ 4681.83Smatt for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum; 4691.83Smatt i++, ph0++) { 4701.83Smatt if (ph0->p_type == PT_LOAD) { 4711.83Smatt u_long psize = ph0->p_vaddr + ph0->p_memsz; 4721.83Smatt if (base_ph == NULL) 4731.83Smatt base_ph = ph0; 4741.83Smatt if (psize > limit) 4751.83Smatt limit = psize; 4761.83Smatt } 4771.83Smatt } 4781.83Smatt 4791.111Sskrll if (base_ph == NULL) { 4801.110Serh error = ENOEXEC; 4811.110Serh goto bad; 4821.110Serh } 4831.110Serh 4841.83Smatt /* 4851.83Smatt * Now compute the size and load address. 4861.83Smatt */ 4871.105Sjunyoung addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p, 4881.103Sfvdl epp->ep_daddr, 4891.83Smatt round_page(limit) - trunc_page(base_ph->p_vaddr)); 4901.83Smatt } else 4911.95Sdrochner addr = *last; /* may be ELF_LINK_ADDR */ 4921.83Smatt 4931.83Smatt /* 4941.83Smatt * Load all the necessary sections 4951.83Smatt */ 4961.83Smatt for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL; 4971.83Smatt i < eh.e_phnum; i++, ph0++) { 4981.83Smatt switch (ph0->p_type) { 4991.83Smatt case PT_LOAD: { 5001.83Smatt u_long size; 5011.83Smatt int prot = 0; 5021.83Smatt int flags; 5031.1Sfvdl 5041.53Smatt if (base_ph == NULL) { 5051.82Smatt /* 5061.82Smatt * First encountered psection is always the 5071.88Smatt * base psection. Make sure it's aligned 5081.89Smatt * properly (align down for topdown and align 5091.89Smatt * upwards for not topdown). 5101.82Smatt */ 5111.83Smatt base_ph = ph0; 5121.53Smatt flags = VMCMD_BASE; 5131.95Sdrochner if (addr == ELF_LINK_ADDR) 5141.95Sdrochner addr = ph0->p_vaddr; 5151.89Smatt if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN) 5161.89Smatt addr = ELF_TRUNC(addr, ph0->p_align); 5171.89Smatt else 5181.89Smatt addr = ELF_ROUND(addr, ph0->p_align); 5191.53Smatt } else { 5201.83Smatt u_long limit = round_page(last_ph->p_vaddr 5211.83Smatt + last_ph->p_memsz); 5221.87Smatt u_long base = trunc_page(ph0->p_vaddr); 5231.83Smatt 5241.82Smatt /* 5251.83Smatt * If there is a gap in between the psections, 5261.83Smatt * map it as inaccessible so nothing else 5271.83Smatt * mmap'ed will be placed there. 5281.82Smatt */ 5291.85Smatt if (limit != base) { 5301.83Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, 5311.85Smatt base - limit, 5321.87Smatt limit - base_ph->p_vaddr, NULLVP, 5331.87Smatt 0, VM_PROT_NONE, VMCMD_RELATIVE); 5341.83Smatt } 5351.83Smatt 5361.83Smatt addr = ph0->p_vaddr - base_ph->p_vaddr; 5371.53Smatt flags = VMCMD_RELATIVE; 5381.53Smatt } 5391.83Smatt last_ph = ph0; 5401.105Sjunyoung elf_load_psection(vcset, vp, &ph[i], &addr, 5411.83Smatt &size, &prot, flags); 5421.82Smatt /* 5431.82Smatt * If entry is within this psection then this 5441.82Smatt * must contain the .text section. *entryoff is 5451.83Smatt * relative to the base psection. 5461.82Smatt */ 5471.83Smatt if (eh.e_entry >= ph0->p_vaddr && 5481.83Smatt eh.e_entry < (ph0->p_vaddr + size)) { 5491.83Smatt *entryoff = eh.e_entry - base_ph->p_vaddr; 5501.1Sfvdl } 5511.84Smatt addr += size; 5521.1Sfvdl break; 5531.83Smatt } 5541.1Sfvdl 5551.46Skleink case PT_DYNAMIC: 5561.46Skleink case PT_PHDR: 5571.113Selad break; 5581.113Selad 5591.46Skleink case PT_NOTE: 5601.1Sfvdl break; 5611.1Sfvdl 5621.1Sfvdl default: 5631.1Sfvdl break; 5641.1Sfvdl } 5651.1Sfvdl } 5661.1Sfvdl 5671.76Schs free(ph, M_TEMP); 5681.82Smatt /* 5691.82Smatt * This value is ignored if TOPDOWN. 5701.82Smatt */ 5711.12Scgd *last = addr; 5721.14Scgd vrele(vp); 5731.12Scgd return 0; 5741.12Scgd 5751.14Scgdbadunlock: 5761.28Sfvdl VOP_UNLOCK(vp, 0); 5771.14Scgd 5781.1Sfvdlbad: 5791.1Sfvdl if (ph != NULL) 5801.76Schs free(ph, M_TEMP); 5811.12Scgd#ifdef notyet /* XXX cgd 960926 */ 5821.12Scgd (maybe) VOP_CLOSE it 5831.12Scgd#endif 5841.14Scgd vrele(vp); 5851.1Sfvdl return error; 5861.1Sfvdl} 5871.1Sfvdl 5881.1Sfvdl/* 5891.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package 5901.1Sfvdl * 5911.1Sfvdl * First, set of the various offsets/lengths in the exec package. 5921.1Sfvdl * 5931.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error 5941.1Sfvdl * out if this is not possible. Finally, set up vmcmds for the 5951.1Sfvdl * text, data, bss, and stack segments. 5961.1Sfvdl */ 5971.1Sfvdlint 5981.108Schristosexec_elf_makecmds(struct lwp *l, struct exec_package *epp) 5991.1Sfvdl{ 6001.9Scgd Elf_Ehdr *eh = epp->ep_hdr; 6011.9Scgd Elf_Phdr *ph, *pp; 6021.9Scgd Elf_Addr phdr = 0, pos = 0; 6031.97Sthorpej int error, i, nload; 6041.58Sjdolecek char *interp = NULL; 6051.9Scgd u_long phsize; 6061.108Schristos struct proc *p; 6071.1Sfvdl 6081.9Scgd if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 6091.1Sfvdl return ENOEXEC; 6101.1Sfvdl 6111.45Sfvdl /* 6121.45Sfvdl * XXX allow for executing shared objects. It seems silly 6131.45Sfvdl * but other ELF-based systems allow it as well. 6141.45Sfvdl */ 6151.105Sjunyoung if (elf_check_header(eh, ET_EXEC) != 0 && 6161.105Sjunyoung elf_check_header(eh, ET_DYN) != 0) 6171.1Sfvdl return ENOEXEC; 6181.1Sfvdl 6191.90Schristos if (eh->e_phnum > MAXPHNUM) 6201.90Schristos return ENOEXEC; 6211.90Schristos 6221.76Schs error = vn_marktext(epp->ep_vp); 6231.76Schs if (error) 6241.106Sjunyoung return error; 6251.76Schs 6261.1Sfvdl /* 6271.17Scgd * Allocate space to hold all the program headers, and read them 6281.17Scgd * from the file 6291.17Scgd */ 6301.108Schristos p = l->l_proc; 6311.9Scgd phsize = eh->e_phnum * sizeof(Elf_Phdr); 6321.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 6331.1Sfvdl 6341.108Schristos if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 6351.64Schristos 0) 6361.1Sfvdl goto bad; 6371.1Sfvdl 6381.19Scgd epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 6391.19Scgd epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 6401.1Sfvdl 6411.1Sfvdl for (i = 0; i < eh->e_phnum; i++) { 6421.1Sfvdl pp = &ph[i]; 6431.46Skleink if (pp->p_type == PT_INTERP) { 6441.58Sjdolecek if (pp->p_filesz >= MAXPATHLEN) 6451.1Sfvdl goto bad; 6461.109Syamt interp = PNBUF_GET(); 6471.95Sdrochner interp[0] = '\0'; 6481.108Schristos if ((error = exec_read_from(l, epp->ep_vp, 6491.64Schristos pp->p_offset, interp, pp->p_filesz)) != 0) 6501.1Sfvdl goto bad; 6511.1Sfvdl break; 6521.1Sfvdl } 6531.1Sfvdl } 6541.1Sfvdl 6551.9Scgd /* 6561.1Sfvdl * On the same architecture, we may be emulating different systems. 6571.99Sskrll * See which one will accept this executable. 6581.1Sfvdl * 6591.1Sfvdl * Probe functions would normally see if the interpreter (if any) 6601.1Sfvdl * exists. Emulation packages may possibly replace the interpreter in 6611.58Sjdolecek * interp[] with a changed path (/emul/xxx/<path>). 6621.1Sfvdl */ 6631.95Sdrochner pos = ELFDEFNNAME(NO_ADDR); 6641.95Sdrochner if (epp->ep_esch->u.elf_probe_func) { 6651.95Sdrochner vaddr_t startp = (vaddr_t)pos; 6661.62Seeh 6671.108Schristos error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp, 6681.62Seeh &startp); 6691.1Sfvdl if (error) 6701.1Sfvdl goto bad; 6711.95Sdrochner pos = (Elf_Addr)startp; 6721.1Sfvdl } 6731.1Sfvdl 6741.1Sfvdl /* 6751.17Scgd * Load all the necessary sections 6761.17Scgd */ 6771.97Sthorpej for (i = nload = 0; i < eh->e_phnum; i++) { 6781.9Scgd Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 6791.9Scgd u_long size = 0; 6801.1Sfvdl int prot = 0; 6811.1Sfvdl 6821.1Sfvdl pp = &ph[i]; 6831.1Sfvdl 6841.1Sfvdl switch (ph[i].p_type) { 6851.46Skleink case PT_LOAD: 6861.4Sfvdl /* 6871.97Sthorpej * XXX 6881.97Sthorpej * Can handle only 2 sections: text and data 6891.4Sfvdl */ 6901.97Sthorpej if (nload++ == 2) 6911.97Sthorpej goto bad; 6921.105Sjunyoung elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 6931.79Satatat &ph[i], &addr, &size, &prot, VMCMD_FIXED); 6941.17Scgd 6951.4Sfvdl /* 6961.4Sfvdl * Decide whether it's text or data by looking 6971.97Sthorpej * at the entry point. 6981.4Sfvdl */ 6991.97Sthorpej if (eh->e_entry >= addr && 7001.97Sthorpej eh->e_entry < (addr + size)) { 7011.97Sthorpej epp->ep_taddr = addr; 7021.97Sthorpej epp->ep_tsize = size; 7031.97Sthorpej if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 7041.19Scgd epp->ep_daddr = addr; 7051.19Scgd epp->ep_dsize = size; 7061.19Scgd } 7071.97Sthorpej } else { 7081.97Sthorpej epp->ep_daddr = addr; 7091.97Sthorpej epp->ep_dsize = size; 7101.4Sfvdl } 7111.1Sfvdl break; 7121.1Sfvdl 7131.46Skleink case PT_SHLIB: 7141.60Smycroft /* SCO has these sections. */ 7151.46Skleink case PT_INTERP: 7161.60Smycroft /* Already did this one. */ 7171.46Skleink case PT_DYNAMIC: 7181.120Selad break; 7191.46Skleink case PT_NOTE: 7201.1Sfvdl break; 7211.46Skleink case PT_PHDR: 7221.4Sfvdl /* Note address of program headers (in text segment) */ 7231.4Sfvdl phdr = pp->p_vaddr; 7241.7Schristos break; 7251.4Sfvdl 7261.1Sfvdl default: 7271.1Sfvdl /* 7281.9Scgd * Not fatal; we don't need to understand everything. 7291.1Sfvdl */ 7301.1Sfvdl break; 7311.1Sfvdl } 7321.1Sfvdl } 7331.1Sfvdl 7341.124Schristos#if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) 7351.124Schristos if (epp->ep_pax_flags) 7361.124Schristos pax_adjust(l, epp->ep_pax_flags); 7371.124Schristos#endif /* PAX_MPROTECT || PAX_SEGVGUARD */ 7381.124Schristos 7391.1Sfvdl /* 7401.17Scgd * Check if we found a dynamically linked binary and arrange to load 7411.79Satatat * its interpreter 7421.17Scgd */ 7431.95Sdrochner if (interp) { 7441.1Sfvdl struct elf_args *ap; 7451.104Schristos int j = epp->ep_vmcmds.evs_used; 7461.79Satatat u_long interp_offset; 7471.1Sfvdl 7481.58Sjdolecek MALLOC(ap, struct elf_args *, sizeof(struct elf_args), 7491.17Scgd M_TEMP, M_WAITOK); 7501.108Schristos if ((error = elf_load_file(l, epp, interp, 7511.79Satatat &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) { 7521.76Schs FREE(ap, M_TEMP); 7531.1Sfvdl goto bad; 7541.1Sfvdl } 7551.104Schristos ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr; 7561.79Satatat epp->ep_entry = ap->arg_interp + interp_offset; 7571.4Sfvdl ap->arg_phaddr = phdr; 7581.1Sfvdl 7591.1Sfvdl ap->arg_phentsize = eh->e_phentsize; 7601.1Sfvdl ap->arg_phnum = eh->e_phnum; 7611.1Sfvdl ap->arg_entry = eh->e_entry; 7621.1Sfvdl 7631.1Sfvdl epp->ep_emul_arg = ap; 7641.95Sdrochner 7651.109Syamt PNBUF_PUT(interp); 7661.1Sfvdl } else 7671.1Sfvdl epp->ep_entry = eh->e_entry; 7681.1Sfvdl 7691.8Schristos#ifdef ELF_MAP_PAGE_ZERO 7701.8Schristos /* Dell SVR4 maps page zero, yeuch! */ 7711.57Sthorpej NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 7721.57Sthorpej epp->ep_vp, 0, VM_PROT_READ); 7731.8Schristos#endif 7741.76Schs free(ph, M_TEMP); 7751.108Schristos return (*epp->ep_esch->es_setup_stack)(l, epp); 7761.1Sfvdl 7771.1Sfvdlbad: 7781.58Sjdolecek if (interp) 7791.109Syamt PNBUF_PUT(interp); 7801.76Schs free(ph, M_TEMP); 7811.1Sfvdl kill_vmcmds(&epp->ep_vmcmds); 7821.1Sfvdl return ENOEXEC; 7831.40Schristos} 7841.40Schristos 7851.59Smrgint 7861.108Schristosnetbsd_elf_signature(struct lwp *l, struct exec_package *epp, 7871.54Sthorpej Elf_Ehdr *eh) 7881.40Schristos{ 7891.61Smycroft size_t i; 7901.61Smycroft Elf_Phdr *ph; 7911.40Schristos size_t phsize; 7921.40Schristos int error; 7931.124Schristos int isnetbsd = 0; 7941.124Schristos char *ndata; 7951.90Schristos 7961.124Schristos epp->ep_pax_flags = 0; 7971.90Schristos if (eh->e_phnum > MAXPHNUM) 7981.90Schristos return ENOEXEC; 7991.40Schristos 8001.40Schristos phsize = eh->e_phnum * sizeof(Elf_Phdr); 8011.61Smycroft ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 8021.108Schristos error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize); 8031.61Smycroft if (error) 8041.61Smycroft goto out; 8051.40Schristos 8061.61Smycroft for (i = 0; i < eh->e_phnum; i++) { 8071.61Smycroft Elf_Phdr *ephp = &ph[i]; 8081.61Smycroft Elf_Nhdr *np; 8091.40Schristos 8101.61Smycroft if (ephp->p_type != PT_NOTE || 8111.61Smycroft ephp->p_filesz > 1024 || 8121.61Smycroft ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 8131.40Schristos continue; 8141.40Schristos 8151.61Smycroft np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 8161.108Schristos error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np, 8171.64Schristos ephp->p_filesz); 8181.61Smycroft if (error) 8191.61Smycroft goto next; 8201.40Schristos 8211.124Schristos ndata = (char *)(np + 1); 8221.124Schristos switch (np->n_type) { 8231.124Schristos case ELF_NOTE_TYPE_NETBSD_TAG: 8241.124Schristos if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 8251.124Schristos np->n_descsz != ELF_NOTE_NETBSD_DESCSZ || 8261.124Schristos memcmp(ndata, ELF_NOTE_NETBSD_NAME, 8271.124Schristos ELF_NOTE_NETBSD_NAMESZ)) 8281.124Schristos goto next; 8291.124Schristos isnetbsd = 1; 8301.124Schristos break; 8311.124Schristos 8321.124Schristos case ELF_NOTE_TYPE_PAX_TAG: 8331.124Schristos if (np->n_namesz != ELF_NOTE_PAX_NAMESZ || 8341.124Schristos np->n_descsz != ELF_NOTE_PAX_DESCSZ || 8351.124Schristos memcmp(ndata, ELF_NOTE_PAX_NAME, 8361.124Schristos ELF_NOTE_PAX_NAMESZ)) 8371.124Schristos goto next; 8381.124Schristos (void)memcpy(&epp->ep_pax_flags, 8391.124Schristos ndata + ELF_NOTE_PAX_NAMESZ, 8401.124Schristos sizeof(epp->ep_pax_flags)); 8411.124Schristos break; 8421.40Schristos 8431.124Schristos default: 8441.124Schristos break; 8451.124Schristos } 8461.61Smycroft 8471.124Schristosnext: 8481.61Smycroft free(np, M_TEMP); 8491.61Smycroft continue; 8501.40Schristos } 8511.40Schristos 8521.124Schristos error = isnetbsd ? 0 : ENOEXEC; 8531.61Smycroftout: 8541.61Smycroft free(ph, M_TEMP); 8551.106Sjunyoung return error; 8561.40Schristos} 8571.40Schristos 8581.58Sjdolecekint 8591.117Syamtnetbsd_elf_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp, 8601.117Syamt vaddr_t *pos) 8611.40Schristos{ 8621.40Schristos int error; 8631.40Schristos 8641.108Schristos if ((error = netbsd_elf_signature(l, epp, eh)) != 0) 8651.40Schristos return error; 8661.95Sdrochner#ifdef ELF_INTERP_NON_RELOCATABLE 8671.95Sdrochner *pos = ELF_LINK_ADDR; 8681.95Sdrochner#endif 8691.40Schristos return 0; 8701.1Sfvdl} 871