exec_elf32.c revision 1.110
11.110Serh/* $NetBSD: exec_elf32.c,v 1.110 2006/03/17 08:51:35 erh 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.110Serh__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.110 2006/03/17 08:51:35 erh 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.1Sfvdl#include <sys/param.h> 751.1Sfvdl#include <sys/proc.h> 761.1Sfvdl#include <sys/malloc.h> 771.1Sfvdl#include <sys/namei.h> 781.1Sfvdl#include <sys/vnode.h> 791.1Sfvdl#include <sys/exec.h> 801.1Sfvdl#include <sys/exec_elf.h> 811.8Schristos#include <sys/syscall.h> 821.8Schristos#include <sys/signalvar.h> 831.14Scgd#include <sys/mount.h> 841.14Scgd#include <sys/stat.h> 851.1Sfvdl 861.58Sjdolecek#include <machine/cpu.h> 871.58Sjdolecek#include <machine/reg.h> 881.57Sthorpej 891.58Sjdolecekextern const struct emul emul_netbsd; 901.42Schristos 911.105Sjunyoung#define elf_check_header ELFNAME(check_header) 921.105Sjunyoung#define elf_copyargs ELFNAME(copyargs) 931.105Sjunyoung#define elf_load_file ELFNAME(load_file) 941.105Sjunyoung#define elf_load_psection ELFNAME(load_psection) 951.105Sjunyoung#define exec_elf_makecmds ELFNAME2(exec,makecmds) 961.105Sjunyoung#define netbsd_elf_signature ELFNAME2(netbsd,signature) 971.105Sjunyoung#define netbsd_elf_probe ELFNAME2(netbsd,probe) 981.105Sjunyoung 991.108Schristosint elf_load_file(struct lwp *, struct exec_package *, char *, 1001.54Sthorpej struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 1011.105Sjunyoungvoid elf_load_psection(struct exec_vmcmd_set *, struct vnode *, 1021.54Sthorpej const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 1031.54Sthorpej 1041.108Schristosint netbsd_elf_signature(struct lwp *, struct exec_package *, Elf_Ehdr *); 1051.108Schristosint netbsd_elf_probe(struct lwp *, struct exec_package *, void *, char *, 1061.105Sjunyoung vaddr_t *); 1071.8Schristos 1081.18Scgd/* round up and down to page boundaries. */ 1091.18Scgd#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 1101.18Scgd#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 1111.8Schristos 1121.90Schristos#define MAXPHNUM 50 1131.90Schristos 1141.8Schristos/* 1151.1Sfvdl * Copy arguments onto the stack in the normal way, but add some 1161.1Sfvdl * extra information in case of dynamic binding. 1171.1Sfvdl */ 1181.66Schristosint 1191.108Schristoself_copyargs(struct lwp *l, struct exec_package *pack, 1201.72Schristos struct ps_strings *arginfo, char **stackp, void *argp) 1211.1Sfvdl{ 1221.1Sfvdl size_t len; 1231.4Sfvdl AuxInfo ai[ELF_AUX_ENTRIES], *a; 1241.1Sfvdl struct elf_args *ap; 1251.108Schristos struct proc *p; 1261.66Schristos int error; 1271.1Sfvdl 1281.108Schristos if ((error = copyargs(l, pack, arginfo, stackp, argp)) != 0) 1291.66Schristos return error; 1301.1Sfvdl 1311.22Scgd a = ai; 1321.108Schristos p = l->l_proc; 1331.22Scgd 1341.1Sfvdl /* 1351.1Sfvdl * Push extra arguments on the stack needed by dynamically 1361.1Sfvdl * linked binaries 1371.1Sfvdl */ 1381.17Scgd if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 1391.77Sjdolecek struct vattr *vap = pack->ep_vap; 1401.1Sfvdl 1411.46Skleink a->a_type = AT_PHDR; 1421.46Skleink a->a_v = ap->arg_phaddr; 1431.1Sfvdl a++; 1441.1Sfvdl 1451.46Skleink a->a_type = AT_PHENT; 1461.46Skleink a->a_v = ap->arg_phentsize; 1471.1Sfvdl a++; 1481.1Sfvdl 1491.46Skleink a->a_type = AT_PHNUM; 1501.46Skleink a->a_v = ap->arg_phnum; 1511.1Sfvdl a++; 1521.1Sfvdl 1531.46Skleink a->a_type = AT_PAGESZ; 1541.57Sthorpej a->a_v = PAGE_SIZE; 1551.1Sfvdl a++; 1561.1Sfvdl 1571.46Skleink a->a_type = AT_BASE; 1581.46Skleink a->a_v = ap->arg_interp; 1591.1Sfvdl a++; 1601.1Sfvdl 1611.46Skleink a->a_type = AT_FLAGS; 1621.46Skleink a->a_v = 0; 1631.1Sfvdl a++; 1641.1Sfvdl 1651.46Skleink a->a_type = AT_ENTRY; 1661.46Skleink a->a_v = ap->arg_entry; 1671.72Schristos a++; 1681.72Schristos 1691.72Schristos a->a_type = AT_EUID; 1701.77Sjdolecek if (vap->va_mode & S_ISUID) 1711.77Sjdolecek a->a_v = vap->va_uid; 1721.77Sjdolecek else 1731.77Sjdolecek a->a_v = p->p_ucred->cr_uid; 1741.72Schristos a++; 1751.72Schristos 1761.72Schristos a->a_type = AT_RUID; 1771.72Schristos a->a_v = p->p_cred->p_ruid; 1781.72Schristos a++; 1791.72Schristos 1801.72Schristos a->a_type = AT_EGID; 1811.77Sjdolecek if (vap->va_mode & S_ISGID) 1821.77Sjdolecek a->a_v = vap->va_gid; 1831.77Sjdolecek else 1841.77Sjdolecek a->a_v = p->p_ucred->cr_gid; 1851.72Schristos a++; 1861.72Schristos 1871.72Schristos a->a_type = AT_RGID; 1881.72Schristos a->a_v = p->p_cred->p_rgid; 1891.1Sfvdl a++; 1901.1Sfvdl 1911.76Schs free(ap, M_TEMP); 1921.9Scgd pack->ep_emul_arg = NULL; 1931.1Sfvdl } 1941.22Scgd 1951.46Skleink a->a_type = AT_NULL; 1961.46Skleink a->a_v = 0; 1971.22Scgd a++; 1981.22Scgd 1991.34Sperry len = (a - ai) * sizeof(AuxInfo); 2001.66Schristos if ((error = copyout(ai, *stackp, len)) != 0) 2011.66Schristos return error; 2021.67Schristos *stackp += len; 2031.22Scgd 2041.66Schristos return 0; 2051.1Sfvdl} 2061.1Sfvdl 2071.1Sfvdl/* 2081.1Sfvdl * elf_check_header(): 2091.1Sfvdl * 2101.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error 2111.1Sfvdl */ 2121.17Scgdint 2131.105Sjunyoungelf_check_header(Elf_Ehdr *eh, int type) 2141.1Sfvdl{ 2151.3Sthorpej 2161.46Skleink if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 2171.46Skleink eh->e_ident[EI_CLASS] != ELFCLASS) 2181.106Sjunyoung return ENOEXEC; 2191.1Sfvdl 2201.1Sfvdl switch (eh->e_machine) { 2211.9Scgd 2221.10Scgd ELFDEFNNAME(MACHDEP_ID_CASES) 2231.1Sfvdl 2241.1Sfvdl default: 2251.106Sjunyoung return ENOEXEC; 2261.1Sfvdl } 2271.70Sthorpej 2281.70Sthorpej if (ELF_EHDR_FLAGS_OK(eh) == 0) 2291.106Sjunyoung return ENOEXEC; 2301.1Sfvdl 2311.1Sfvdl if (eh->e_type != type) 2321.106Sjunyoung return ENOEXEC; 2331.61Smycroft 2341.100Schristos if (eh->e_shnum > 32768 || eh->e_phnum > 128) 2351.106Sjunyoung return ENOEXEC; 2361.1Sfvdl 2371.106Sjunyoung return 0; 2381.1Sfvdl} 2391.1Sfvdl 2401.1Sfvdl/* 2411.1Sfvdl * elf_load_psection(): 2421.17Scgd * 2431.1Sfvdl * Load a psection at the appropriate address 2441.1Sfvdl */ 2451.17Scgdvoid 2461.105Sjunyoungelf_load_psection(struct exec_vmcmd_set *vcset, struct vnode *vp, 2471.54Sthorpej const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags) 2481.1Sfvdl{ 2491.87Smatt u_long msize, psize, rm, rf; 2501.1Sfvdl long diff, offset; 2511.1Sfvdl 2521.1Sfvdl /* 2531.17Scgd * If the user specified an address, then we load there. 2541.17Scgd */ 2551.87Smatt if (*addr == ELFDEFNNAME(NO_ADDR)) 2561.87Smatt *addr = ph->p_vaddr; 2571.87Smatt 2581.87Smatt if (ph->p_align > 1) { 2591.87Smatt /* 2601.87Smatt * Make sure we are virtually aligned as we are supposed to be. 2611.87Smatt */ 2621.87Smatt diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 2631.87Smatt KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 2641.87Smatt /* 2651.87Smatt * But make sure to not map any pages before the start of the 2661.87Smatt * psection by limiting the difference to within a page. 2671.87Smatt */ 2681.101Sperry diff &= PAGE_MASK; 2691.87Smatt } else 2701.87Smatt diff = 0; 2711.1Sfvdl 2721.46Skleink *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 2731.46Skleink *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 2741.46Skleink *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 2751.1Sfvdl 2761.87Smatt /* 2771.87Smatt * Adjust everything so it all starts on a page boundary. 2781.87Smatt */ 2791.87Smatt *addr -= diff; 2801.1Sfvdl offset = ph->p_offset - diff; 2811.1Sfvdl *size = ph->p_filesz + diff; 2821.1Sfvdl msize = ph->p_memsz + diff; 2831.1Sfvdl 2841.65Schristos if (ph->p_align >= PAGE_SIZE) { 2851.65Schristos if ((ph->p_flags & PF_W) != 0) { 2861.65Schristos /* 2871.65Schristos * Because the pagedvn pager can't handle zero fill 2881.65Schristos * of the last data page if it's not page aligned we 2891.65Schristos * map the last page readvn. 2901.65Schristos */ 2911.65Schristos psize = trunc_page(*size); 2921.65Schristos } else { 2931.65Schristos psize = round_page(*size); 2941.65Schristos } 2951.65Schristos } else { 2961.65Schristos psize = *size; 2971.53Smatt } 2981.65Schristos 2991.53Smatt if (psize > 0) { 3001.65Schristos NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 3011.65Schristos vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 3021.53Smatt offset, *prot, flags); 3031.87Smatt flags &= VMCMD_RELATIVE; 3041.53Smatt } 3051.53Smatt if (psize < *size) { 3061.53Smatt NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 3071.87Smatt *addr + psize, vp, offset + psize, *prot, flags); 3081.53Smatt } 3091.1Sfvdl 3101.1Sfvdl /* 3111.87Smatt * Check if we need to extend the size of the segment (does 3121.87Smatt * bss extend page the next page boundary)? 3131.17Scgd */ 3141.1Sfvdl rm = round_page(*addr + msize); 3151.1Sfvdl rf = round_page(*addr + *size); 3161.1Sfvdl 3171.1Sfvdl if (rm != rf) { 3181.53Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 3191.83Smatt 0, *prot, flags & VMCMD_RELATIVE); 3201.1Sfvdl *size = msize; 3211.1Sfvdl } 3221.1Sfvdl} 3231.1Sfvdl 3241.1Sfvdl/* 3251.1Sfvdl * elf_load_file(): 3261.1Sfvdl * 3271.1Sfvdl * Load a file (interpreter/library) pointed to by path 3281.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic 3291.1Sfvdl * so it might be used externally. 3301.1Sfvdl */ 3311.17Scgdint 3321.108Schristoself_load_file(struct lwp *l, struct exec_package *epp, char *path, 3331.79Satatat struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap, 3341.54Sthorpej Elf_Addr *last) 3351.1Sfvdl{ 3361.83Smatt int error, i; 3371.1Sfvdl struct nameidata nd; 3381.14Scgd struct vnode *vp; 3391.14Scgd struct vattr attr; 3401.9Scgd Elf_Ehdr eh; 3411.9Scgd Elf_Phdr *ph = NULL; 3421.83Smatt const Elf_Phdr *ph0; 3431.83Smatt const Elf_Phdr *base_ph; 3441.83Smatt const Elf_Phdr *last_ph; 3451.1Sfvdl u_long phsize; 3461.9Scgd Elf_Addr addr = *last; 3471.108Schristos struct proc *p; 3481.108Schristos 3491.108Schristos p = l->l_proc; 3501.1Sfvdl 3511.1Sfvdl /* 3521.17Scgd * 1. open file 3531.17Scgd * 2. read filehdr 3541.17Scgd * 3. map text, data, and bss out of it using VM_* 3551.17Scgd */ 3561.108Schristos NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, l); 3571.12Scgd if ((error = namei(&nd)) != 0) 3581.1Sfvdl return error; 3591.14Scgd vp = nd.ni_vp; 3601.14Scgd 3611.26Smycroft /* 3621.26Smycroft * Similarly, if it's not marked as executable, or it's not a regular 3631.26Smycroft * file, we don't allow it to be used. 3641.26Smycroft */ 3651.14Scgd if (vp->v_type != VREG) { 3661.14Scgd error = EACCES; 3671.14Scgd goto badunlock; 3681.14Scgd } 3691.108Schristos if ((error = VOP_ACCESS(vp, VEXEC, l->l_proc->p_ucred, l)) != 0) 3701.26Smycroft goto badunlock; 3711.14Scgd 3721.17Scgd /* get attributes */ 3731.108Schristos if ((error = VOP_GETATTR(vp, &attr, l->l_proc->p_ucred, l)) != 0) 3741.14Scgd goto badunlock; 3751.14Scgd 3761.14Scgd /* 3771.14Scgd * Check mount point. Though we're not trying to exec this binary, 3781.15Scgd * we will be executing code from it, so if the mount point 3791.15Scgd * disallows execution or set-id-ness, we punt or kill the set-id. 3801.14Scgd */ 3811.14Scgd if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 3821.14Scgd error = EACCES; 3831.14Scgd goto badunlock; 3841.14Scgd } 3851.14Scgd if (vp->v_mount->mnt_flag & MNT_NOSUID) 3861.24Smycroft epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 3871.14Scgd 3881.12Scgd#ifdef notyet /* XXX cgd 960926 */ 3891.12Scgd XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 3901.12Scgd#endif 3911.76Schs 3921.76Schs error = vn_marktext(vp); 3931.76Schs if (error) 3941.76Schs goto badunlock; 3951.76Schs 3961.28Sfvdl VOP_UNLOCK(vp, 0); 3971.12Scgd 3981.108Schristos if ((error = exec_read_from(l, vp, 0, &eh, sizeof(eh))) != 0) 3991.1Sfvdl goto bad; 4001.1Sfvdl 4011.105Sjunyoung if ((error = elf_check_header(&eh, ET_DYN)) != 0) 4021.1Sfvdl goto bad; 4031.1Sfvdl 4041.90Schristos if (eh.e_phnum > MAXPHNUM) 4051.90Schristos goto bad; 4061.90Schristos 4071.9Scgd phsize = eh.e_phnum * sizeof(Elf_Phdr); 4081.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 4091.1Sfvdl 4101.108Schristos if ((error = exec_read_from(l, vp, eh.e_phoff, ph, phsize)) != 0) 4111.1Sfvdl goto bad; 4121.1Sfvdl 4131.107Ssimonb#ifdef ELF_INTERP_NON_RELOCATABLE 4141.107Ssimonb /* 4151.107Ssimonb * Evil hack: Only MIPS should be non-relocatable, and the 4161.107Ssimonb * psections should have a high address (typically 0x5ffe0000). 4171.107Ssimonb * If it's now relocatable, it should be linked at 0 and the 4181.107Ssimonb * psections should have zeros in the upper part of the address. 4191.107Ssimonb * Otherwise, force the load at the linked address. 4201.107Ssimonb */ 4211.107Ssimonb if (*last == ELF_LINK_ADDR && (ph->p_vaddr & 0xffff0000) == 0) 4221.107Ssimonb *last = ELFDEFNNAME(NO_ADDR); 4231.107Ssimonb#endif 4241.107Ssimonb 4251.1Sfvdl /* 4261.83Smatt * If no position to load the interpreter was set by a probe 4271.83Smatt * function, pick the same address that a non-fixed mmap(0, ..) 4281.83Smatt * would (i.e. something safely out of the way). 4291.83Smatt */ 4301.83Smatt if (*last == ELFDEFNNAME(NO_ADDR)) { 4311.83Smatt u_long limit = 0; 4321.83Smatt /* 4331.83Smatt * Find the start and ending addresses of the psections to 4341.83Smatt * be loaded. This will give us the size. 4351.83Smatt */ 4361.83Smatt for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum; 4371.83Smatt i++, ph0++) { 4381.83Smatt if (ph0->p_type == PT_LOAD) { 4391.83Smatt u_long psize = ph0->p_vaddr + ph0->p_memsz; 4401.83Smatt if (base_ph == NULL) 4411.83Smatt base_ph = ph0; 4421.83Smatt if (psize > limit) 4431.83Smatt limit = psize; 4441.83Smatt } 4451.83Smatt } 4461.83Smatt 4471.110Serh if (base_ph == NULL) 4481.110Serh { 4491.110Serh error = ENOEXEC; 4501.110Serh goto bad; 4511.110Serh } 4521.110Serh 4531.83Smatt /* 4541.83Smatt * Now compute the size and load address. 4551.83Smatt */ 4561.105Sjunyoung addr = (*epp->ep_esch->es_emul->e_vm_default_addr)(p, 4571.103Sfvdl epp->ep_daddr, 4581.83Smatt round_page(limit) - trunc_page(base_ph->p_vaddr)); 4591.83Smatt } else 4601.95Sdrochner addr = *last; /* may be ELF_LINK_ADDR */ 4611.83Smatt 4621.83Smatt /* 4631.83Smatt * Load all the necessary sections 4641.83Smatt */ 4651.83Smatt for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL; 4661.83Smatt i < eh.e_phnum; i++, ph0++) { 4671.83Smatt switch (ph0->p_type) { 4681.83Smatt case PT_LOAD: { 4691.83Smatt u_long size; 4701.83Smatt int prot = 0; 4711.83Smatt int flags; 4721.1Sfvdl 4731.53Smatt if (base_ph == NULL) { 4741.82Smatt /* 4751.82Smatt * First encountered psection is always the 4761.88Smatt * base psection. Make sure it's aligned 4771.89Smatt * properly (align down for topdown and align 4781.89Smatt * upwards for not topdown). 4791.82Smatt */ 4801.83Smatt base_ph = ph0; 4811.53Smatt flags = VMCMD_BASE; 4821.95Sdrochner if (addr == ELF_LINK_ADDR) 4831.95Sdrochner addr = ph0->p_vaddr; 4841.89Smatt if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN) 4851.89Smatt addr = ELF_TRUNC(addr, ph0->p_align); 4861.89Smatt else 4871.89Smatt addr = ELF_ROUND(addr, ph0->p_align); 4881.53Smatt } else { 4891.83Smatt u_long limit = round_page(last_ph->p_vaddr 4901.83Smatt + last_ph->p_memsz); 4911.87Smatt u_long base = trunc_page(ph0->p_vaddr); 4921.83Smatt 4931.82Smatt /* 4941.83Smatt * If there is a gap in between the psections, 4951.83Smatt * map it as inaccessible so nothing else 4961.83Smatt * mmap'ed will be placed there. 4971.82Smatt */ 4981.85Smatt if (limit != base) { 4991.83Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, 5001.85Smatt base - limit, 5011.87Smatt limit - base_ph->p_vaddr, NULLVP, 5021.87Smatt 0, VM_PROT_NONE, VMCMD_RELATIVE); 5031.83Smatt } 5041.83Smatt 5051.83Smatt addr = ph0->p_vaddr - base_ph->p_vaddr; 5061.53Smatt flags = VMCMD_RELATIVE; 5071.53Smatt } 5081.83Smatt last_ph = ph0; 5091.105Sjunyoung elf_load_psection(vcset, vp, &ph[i], &addr, 5101.83Smatt &size, &prot, flags); 5111.82Smatt /* 5121.82Smatt * If entry is within this psection then this 5131.82Smatt * must contain the .text section. *entryoff is 5141.83Smatt * relative to the base psection. 5151.82Smatt */ 5161.83Smatt if (eh.e_entry >= ph0->p_vaddr && 5171.83Smatt eh.e_entry < (ph0->p_vaddr + size)) { 5181.83Smatt *entryoff = eh.e_entry - base_ph->p_vaddr; 5191.1Sfvdl } 5201.84Smatt addr += size; 5211.1Sfvdl break; 5221.83Smatt } 5231.1Sfvdl 5241.46Skleink case PT_DYNAMIC: 5251.46Skleink case PT_PHDR: 5261.46Skleink case PT_NOTE: 5271.1Sfvdl break; 5281.1Sfvdl 5291.1Sfvdl default: 5301.1Sfvdl break; 5311.1Sfvdl } 5321.1Sfvdl } 5331.1Sfvdl 5341.76Schs free(ph, M_TEMP); 5351.82Smatt /* 5361.82Smatt * This value is ignored if TOPDOWN. 5371.82Smatt */ 5381.12Scgd *last = addr; 5391.14Scgd vrele(vp); 5401.12Scgd return 0; 5411.12Scgd 5421.14Scgdbadunlock: 5431.28Sfvdl VOP_UNLOCK(vp, 0); 5441.14Scgd 5451.1Sfvdlbad: 5461.1Sfvdl if (ph != NULL) 5471.76Schs free(ph, M_TEMP); 5481.12Scgd#ifdef notyet /* XXX cgd 960926 */ 5491.12Scgd (maybe) VOP_CLOSE it 5501.12Scgd#endif 5511.14Scgd vrele(vp); 5521.1Sfvdl return error; 5531.1Sfvdl} 5541.1Sfvdl 5551.1Sfvdl/* 5561.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package 5571.1Sfvdl * 5581.1Sfvdl * First, set of the various offsets/lengths in the exec package. 5591.1Sfvdl * 5601.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error 5611.1Sfvdl * out if this is not possible. Finally, set up vmcmds for the 5621.1Sfvdl * text, data, bss, and stack segments. 5631.1Sfvdl */ 5641.1Sfvdlint 5651.108Schristosexec_elf_makecmds(struct lwp *l, struct exec_package *epp) 5661.1Sfvdl{ 5671.9Scgd Elf_Ehdr *eh = epp->ep_hdr; 5681.9Scgd Elf_Phdr *ph, *pp; 5691.9Scgd Elf_Addr phdr = 0, pos = 0; 5701.97Sthorpej int error, i, nload; 5711.58Sjdolecek char *interp = NULL; 5721.9Scgd u_long phsize; 5731.108Schristos struct proc *p; 5741.1Sfvdl 5751.9Scgd if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 5761.1Sfvdl return ENOEXEC; 5771.1Sfvdl 5781.45Sfvdl /* 5791.45Sfvdl * XXX allow for executing shared objects. It seems silly 5801.45Sfvdl * but other ELF-based systems allow it as well. 5811.45Sfvdl */ 5821.105Sjunyoung if (elf_check_header(eh, ET_EXEC) != 0 && 5831.105Sjunyoung elf_check_header(eh, ET_DYN) != 0) 5841.1Sfvdl return ENOEXEC; 5851.1Sfvdl 5861.90Schristos if (eh->e_phnum > MAXPHNUM) 5871.90Schristos return ENOEXEC; 5881.90Schristos 5891.76Schs error = vn_marktext(epp->ep_vp); 5901.76Schs if (error) 5911.106Sjunyoung return error; 5921.76Schs 5931.1Sfvdl /* 5941.17Scgd * Allocate space to hold all the program headers, and read them 5951.17Scgd * from the file 5961.17Scgd */ 5971.108Schristos p = l->l_proc; 5981.9Scgd phsize = eh->e_phnum * sizeof(Elf_Phdr); 5991.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 6001.1Sfvdl 6011.108Schristos if ((error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize)) != 6021.64Schristos 0) 6031.1Sfvdl goto bad; 6041.1Sfvdl 6051.19Scgd epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 6061.19Scgd epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 6071.1Sfvdl 6081.1Sfvdl for (i = 0; i < eh->e_phnum; i++) { 6091.1Sfvdl pp = &ph[i]; 6101.46Skleink if (pp->p_type == PT_INTERP) { 6111.58Sjdolecek if (pp->p_filesz >= MAXPATHLEN) 6121.1Sfvdl goto bad; 6131.109Syamt interp = PNBUF_GET(); 6141.95Sdrochner interp[0] = '\0'; 6151.108Schristos if ((error = exec_read_from(l, epp->ep_vp, 6161.64Schristos pp->p_offset, interp, pp->p_filesz)) != 0) 6171.1Sfvdl goto bad; 6181.1Sfvdl break; 6191.1Sfvdl } 6201.1Sfvdl } 6211.1Sfvdl 6221.9Scgd /* 6231.1Sfvdl * On the same architecture, we may be emulating different systems. 6241.99Sskrll * See which one will accept this executable. 6251.1Sfvdl * 6261.1Sfvdl * Probe functions would normally see if the interpreter (if any) 6271.1Sfvdl * exists. Emulation packages may possibly replace the interpreter in 6281.58Sjdolecek * interp[] with a changed path (/emul/xxx/<path>). 6291.1Sfvdl */ 6301.95Sdrochner pos = ELFDEFNNAME(NO_ADDR); 6311.95Sdrochner if (epp->ep_esch->u.elf_probe_func) { 6321.95Sdrochner vaddr_t startp = (vaddr_t)pos; 6331.62Seeh 6341.108Schristos error = (*epp->ep_esch->u.elf_probe_func)(l, epp, eh, interp, 6351.62Seeh &startp); 6361.1Sfvdl if (error) 6371.1Sfvdl goto bad; 6381.95Sdrochner pos = (Elf_Addr)startp; 6391.1Sfvdl } 6401.1Sfvdl 6411.1Sfvdl /* 6421.17Scgd * Load all the necessary sections 6431.17Scgd */ 6441.97Sthorpej for (i = nload = 0; i < eh->e_phnum; i++) { 6451.9Scgd Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 6461.9Scgd u_long size = 0; 6471.1Sfvdl int prot = 0; 6481.1Sfvdl 6491.1Sfvdl pp = &ph[i]; 6501.1Sfvdl 6511.1Sfvdl switch (ph[i].p_type) { 6521.46Skleink case PT_LOAD: 6531.4Sfvdl /* 6541.97Sthorpej * XXX 6551.97Sthorpej * Can handle only 2 sections: text and data 6561.4Sfvdl */ 6571.97Sthorpej if (nload++ == 2) 6581.97Sthorpej goto bad; 6591.105Sjunyoung elf_load_psection(&epp->ep_vmcmds, epp->ep_vp, 6601.79Satatat &ph[i], &addr, &size, &prot, VMCMD_FIXED); 6611.17Scgd 6621.4Sfvdl /* 6631.4Sfvdl * Decide whether it's text or data by looking 6641.97Sthorpej * at the entry point. 6651.4Sfvdl */ 6661.97Sthorpej if (eh->e_entry >= addr && 6671.97Sthorpej eh->e_entry < (addr + size)) { 6681.97Sthorpej epp->ep_taddr = addr; 6691.97Sthorpej epp->ep_tsize = size; 6701.97Sthorpej if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 6711.19Scgd epp->ep_daddr = addr; 6721.19Scgd epp->ep_dsize = size; 6731.19Scgd } 6741.97Sthorpej } else { 6751.97Sthorpej epp->ep_daddr = addr; 6761.97Sthorpej epp->ep_dsize = size; 6771.4Sfvdl } 6781.1Sfvdl break; 6791.1Sfvdl 6801.46Skleink case PT_SHLIB: 6811.60Smycroft /* SCO has these sections. */ 6821.46Skleink case PT_INTERP: 6831.60Smycroft /* Already did this one. */ 6841.46Skleink case PT_DYNAMIC: 6851.46Skleink case PT_NOTE: 6861.1Sfvdl break; 6871.1Sfvdl 6881.46Skleink case PT_PHDR: 6891.4Sfvdl /* Note address of program headers (in text segment) */ 6901.4Sfvdl phdr = pp->p_vaddr; 6911.7Schristos break; 6921.4Sfvdl 6931.1Sfvdl default: 6941.1Sfvdl /* 6951.9Scgd * Not fatal; we don't need to understand everything. 6961.1Sfvdl */ 6971.1Sfvdl break; 6981.1Sfvdl } 6991.1Sfvdl } 7001.1Sfvdl 7011.1Sfvdl /* 7021.17Scgd * Check if we found a dynamically linked binary and arrange to load 7031.79Satatat * its interpreter 7041.17Scgd */ 7051.95Sdrochner if (interp) { 7061.1Sfvdl struct elf_args *ap; 7071.104Schristos int j = epp->ep_vmcmds.evs_used; 7081.79Satatat u_long interp_offset; 7091.1Sfvdl 7101.58Sjdolecek MALLOC(ap, struct elf_args *, sizeof(struct elf_args), 7111.17Scgd M_TEMP, M_WAITOK); 7121.108Schristos if ((error = elf_load_file(l, epp, interp, 7131.79Satatat &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) { 7141.76Schs FREE(ap, M_TEMP); 7151.1Sfvdl goto bad; 7161.1Sfvdl } 7171.104Schristos ap->arg_interp = epp->ep_vmcmds.evs_cmds[j].ev_addr; 7181.79Satatat epp->ep_entry = ap->arg_interp + interp_offset; 7191.4Sfvdl ap->arg_phaddr = phdr; 7201.1Sfvdl 7211.1Sfvdl ap->arg_phentsize = eh->e_phentsize; 7221.1Sfvdl ap->arg_phnum = eh->e_phnum; 7231.1Sfvdl ap->arg_entry = eh->e_entry; 7241.1Sfvdl 7251.1Sfvdl epp->ep_emul_arg = ap; 7261.95Sdrochner 7271.109Syamt PNBUF_PUT(interp); 7281.1Sfvdl } else 7291.1Sfvdl epp->ep_entry = eh->e_entry; 7301.1Sfvdl 7311.8Schristos#ifdef ELF_MAP_PAGE_ZERO 7321.8Schristos /* Dell SVR4 maps page zero, yeuch! */ 7331.57Sthorpej NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 7341.57Sthorpej epp->ep_vp, 0, VM_PROT_READ); 7351.8Schristos#endif 7361.76Schs free(ph, M_TEMP); 7371.108Schristos return (*epp->ep_esch->es_setup_stack)(l, epp); 7381.1Sfvdl 7391.1Sfvdlbad: 7401.58Sjdolecek if (interp) 7411.109Syamt PNBUF_PUT(interp); 7421.76Schs free(ph, M_TEMP); 7431.1Sfvdl kill_vmcmds(&epp->ep_vmcmds); 7441.1Sfvdl return ENOEXEC; 7451.40Schristos} 7461.40Schristos 7471.59Smrgint 7481.108Schristosnetbsd_elf_signature(struct lwp *l, struct exec_package *epp, 7491.54Sthorpej Elf_Ehdr *eh) 7501.40Schristos{ 7511.61Smycroft size_t i; 7521.61Smycroft Elf_Phdr *ph; 7531.40Schristos size_t phsize; 7541.40Schristos int error; 7551.90Schristos 7561.90Schristos if (eh->e_phnum > MAXPHNUM) 7571.90Schristos return ENOEXEC; 7581.40Schristos 7591.40Schristos phsize = eh->e_phnum * sizeof(Elf_Phdr); 7601.61Smycroft ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 7611.108Schristos error = exec_read_from(l, epp->ep_vp, eh->e_phoff, ph, phsize); 7621.61Smycroft if (error) 7631.61Smycroft goto out; 7641.40Schristos 7651.61Smycroft for (i = 0; i < eh->e_phnum; i++) { 7661.61Smycroft Elf_Phdr *ephp = &ph[i]; 7671.61Smycroft Elf_Nhdr *np; 7681.40Schristos 7691.61Smycroft if (ephp->p_type != PT_NOTE || 7701.61Smycroft ephp->p_filesz > 1024 || 7711.61Smycroft ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 7721.40Schristos continue; 7731.40Schristos 7741.61Smycroft np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 7751.108Schristos error = exec_read_from(l, epp->ep_vp, ephp->p_offset, np, 7761.64Schristos ephp->p_filesz); 7771.61Smycroft if (error) 7781.61Smycroft goto next; 7791.40Schristos 7801.61Smycroft if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG || 7811.61Smycroft np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 7821.61Smycroft np->n_descsz != ELF_NOTE_NETBSD_DESCSZ || 7831.61Smycroft memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME, 7841.40Schristos ELF_NOTE_NETBSD_NAMESZ)) 7851.61Smycroft goto next; 7861.40Schristos 7871.40Schristos error = 0; 7881.61Smycroft free(np, M_TEMP); 7891.61Smycroft goto out; 7901.61Smycroft 7911.61Smycroft next: 7921.61Smycroft free(np, M_TEMP); 7931.61Smycroft continue; 7941.40Schristos } 7951.40Schristos 7961.40Schristos error = ENOEXEC; 7971.61Smycroftout: 7981.61Smycroft free(ph, M_TEMP); 7991.106Sjunyoung return error; 8001.40Schristos} 8011.40Schristos 8021.58Sjdolecekint 8031.108Schristosnetbsd_elf_probe(struct lwp *l, struct exec_package *epp, 8041.58Sjdolecek void *eh, char *itp, vaddr_t *pos) 8051.40Schristos{ 8061.40Schristos int error; 8071.40Schristos 8081.108Schristos if ((error = netbsd_elf_signature(l, epp, eh)) != 0) 8091.40Schristos return error; 8101.95Sdrochner#ifdef ELF_INTERP_NON_RELOCATABLE 8111.95Sdrochner *pos = ELF_LINK_ADDR; 8121.95Sdrochner#endif 8131.40Schristos return 0; 8141.1Sfvdl} 815