exec_elf32.c revision 1.100
11.100Schristos/* $NetBSD: exec_elf32.c,v 1.100 2005/02/07 00:12:49 christos Exp $ */ 21.36Schristos 31.36Schristos/*- 41.60Smycroft * Copyright (c) 1994, 2000 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.100Schristos__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.100 2005/02/07 00:12:49 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.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.92Sfvdlint ELFNAME(load_file)(struct proc *, struct exec_package *, char *, 921.54Sthorpej struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *); 931.54Sthorpejvoid ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *, 941.54Sthorpej const Elf_Phdr *, Elf_Addr *, u_long *, int *, int); 951.54Sthorpej 961.92Sfvdlint ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *, 971.54Sthorpej Elf_Ehdr *); 981.92Sfvdlint ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *, 991.58Sjdolecek void *, char *, vaddr_t *); 1001.8Schristos 1011.18Scgd/* round up and down to page boundaries. */ 1021.18Scgd#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 1031.18Scgd#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 1041.8Schristos 1051.90Schristos#define MAXPHNUM 50 1061.90Schristos 1071.8Schristos/* 1081.1Sfvdl * Copy arguments onto the stack in the normal way, but add some 1091.1Sfvdl * extra information in case of dynamic binding. 1101.1Sfvdl */ 1111.66Schristosint 1121.92SfvdlELFNAME(copyargs)(struct proc *p, struct exec_package *pack, 1131.72Schristos struct ps_strings *arginfo, char **stackp, void *argp) 1141.1Sfvdl{ 1151.1Sfvdl size_t len; 1161.4Sfvdl AuxInfo ai[ELF_AUX_ENTRIES], *a; 1171.1Sfvdl struct elf_args *ap; 1181.66Schristos int error; 1191.1Sfvdl 1201.92Sfvdl if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0) 1211.66Schristos return error; 1221.1Sfvdl 1231.22Scgd a = ai; 1241.22Scgd 1251.1Sfvdl /* 1261.1Sfvdl * Push extra arguments on the stack needed by dynamically 1271.1Sfvdl * linked binaries 1281.1Sfvdl */ 1291.17Scgd if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 1301.77Sjdolecek struct vattr *vap = pack->ep_vap; 1311.1Sfvdl 1321.46Skleink a->a_type = AT_PHDR; 1331.46Skleink a->a_v = ap->arg_phaddr; 1341.1Sfvdl a++; 1351.1Sfvdl 1361.46Skleink a->a_type = AT_PHENT; 1371.46Skleink a->a_v = ap->arg_phentsize; 1381.1Sfvdl a++; 1391.1Sfvdl 1401.46Skleink a->a_type = AT_PHNUM; 1411.46Skleink a->a_v = ap->arg_phnum; 1421.1Sfvdl a++; 1431.1Sfvdl 1441.46Skleink a->a_type = AT_PAGESZ; 1451.57Sthorpej a->a_v = PAGE_SIZE; 1461.1Sfvdl a++; 1471.1Sfvdl 1481.46Skleink a->a_type = AT_BASE; 1491.46Skleink a->a_v = ap->arg_interp; 1501.1Sfvdl a++; 1511.1Sfvdl 1521.46Skleink a->a_type = AT_FLAGS; 1531.46Skleink a->a_v = 0; 1541.1Sfvdl a++; 1551.1Sfvdl 1561.46Skleink a->a_type = AT_ENTRY; 1571.46Skleink a->a_v = ap->arg_entry; 1581.72Schristos a++; 1591.72Schristos 1601.72Schristos a->a_type = AT_EUID; 1611.77Sjdolecek if (vap->va_mode & S_ISUID) 1621.77Sjdolecek a->a_v = vap->va_uid; 1631.77Sjdolecek else 1641.77Sjdolecek a->a_v = p->p_ucred->cr_uid; 1651.72Schristos a++; 1661.72Schristos 1671.72Schristos a->a_type = AT_RUID; 1681.72Schristos a->a_v = p->p_cred->p_ruid; 1691.72Schristos a++; 1701.72Schristos 1711.72Schristos a->a_type = AT_EGID; 1721.77Sjdolecek if (vap->va_mode & S_ISGID) 1731.77Sjdolecek a->a_v = vap->va_gid; 1741.77Sjdolecek else 1751.77Sjdolecek a->a_v = p->p_ucred->cr_gid; 1761.72Schristos a++; 1771.72Schristos 1781.72Schristos a->a_type = AT_RGID; 1791.72Schristos a->a_v = p->p_cred->p_rgid; 1801.1Sfvdl a++; 1811.1Sfvdl 1821.76Schs free(ap, M_TEMP); 1831.9Scgd pack->ep_emul_arg = NULL; 1841.1Sfvdl } 1851.22Scgd 1861.46Skleink a->a_type = AT_NULL; 1871.46Skleink a->a_v = 0; 1881.22Scgd a++; 1891.22Scgd 1901.34Sperry len = (a - ai) * sizeof(AuxInfo); 1911.66Schristos if ((error = copyout(ai, *stackp, len)) != 0) 1921.66Schristos return error; 1931.67Schristos *stackp += len; 1941.22Scgd 1951.66Schristos return 0; 1961.1Sfvdl} 1971.1Sfvdl 1981.1Sfvdl/* 1991.1Sfvdl * elf_check_header(): 2001.1Sfvdl * 2011.1Sfvdl * Check header for validity; return 0 of ok ENOEXEC if error 2021.1Sfvdl */ 2031.17Scgdint 2041.54SthorpejELFNAME(check_header)(Elf_Ehdr *eh, int type) 2051.1Sfvdl{ 2061.3Sthorpej 2071.46Skleink if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 2081.46Skleink eh->e_ident[EI_CLASS] != ELFCLASS) 2091.61Smycroft return (ENOEXEC); 2101.1Sfvdl 2111.1Sfvdl switch (eh->e_machine) { 2121.9Scgd 2131.10Scgd ELFDEFNNAME(MACHDEP_ID_CASES) 2141.1Sfvdl 2151.1Sfvdl default: 2161.61Smycroft return (ENOEXEC); 2171.1Sfvdl } 2181.70Sthorpej 2191.70Sthorpej if (ELF_EHDR_FLAGS_OK(eh) == 0) 2201.70Sthorpej return (ENOEXEC); 2211.1Sfvdl 2221.1Sfvdl if (eh->e_type != type) 2231.61Smycroft return (ENOEXEC); 2241.61Smycroft 2251.100Schristos if (eh->e_shnum > 32768 || eh->e_phnum > 128) 2261.61Smycroft return (ENOEXEC); 2271.1Sfvdl 2281.61Smycroft return (0); 2291.1Sfvdl} 2301.1Sfvdl 2311.1Sfvdl/* 2321.1Sfvdl * elf_load_psection(): 2331.17Scgd * 2341.1Sfvdl * Load a psection at the appropriate address 2351.1Sfvdl */ 2361.17Scgdvoid 2371.54SthorpejELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp, 2381.54Sthorpej const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags) 2391.1Sfvdl{ 2401.87Smatt u_long msize, psize, rm, rf; 2411.1Sfvdl long diff, offset; 2421.1Sfvdl 2431.1Sfvdl /* 2441.17Scgd * If the user specified an address, then we load there. 2451.17Scgd */ 2461.87Smatt if (*addr == ELFDEFNNAME(NO_ADDR)) 2471.87Smatt *addr = ph->p_vaddr; 2481.87Smatt 2491.87Smatt if (ph->p_align > 1) { 2501.87Smatt /* 2511.87Smatt * Make sure we are virtually aligned as we are supposed to be. 2521.87Smatt */ 2531.87Smatt diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align); 2541.87Smatt KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align)); 2551.87Smatt /* 2561.87Smatt * But make sure to not map any pages before the start of the 2571.87Smatt * psection by limiting the difference to within a page. 2581.87Smatt */ 2591.87Smatt diff &= PAGE_MASK; 2601.87Smatt } else 2611.87Smatt diff = 0; 2621.1Sfvdl 2631.46Skleink *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 2641.46Skleink *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 2651.46Skleink *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 2661.1Sfvdl 2671.87Smatt /* 2681.87Smatt * Adjust everything so it all starts on a page boundary. 2691.87Smatt */ 2701.87Smatt *addr -= diff; 2711.1Sfvdl offset = ph->p_offset - diff; 2721.1Sfvdl *size = ph->p_filesz + diff; 2731.1Sfvdl msize = ph->p_memsz + diff; 2741.1Sfvdl 2751.65Schristos if (ph->p_align >= PAGE_SIZE) { 2761.65Schristos if ((ph->p_flags & PF_W) != 0) { 2771.65Schristos /* 2781.65Schristos * Because the pagedvn pager can't handle zero fill 2791.65Schristos * of the last data page if it's not page aligned we 2801.65Schristos * map the last page readvn. 2811.65Schristos */ 2821.65Schristos psize = trunc_page(*size); 2831.65Schristos } else { 2841.65Schristos psize = round_page(*size); 2851.65Schristos } 2861.65Schristos } else { 2871.65Schristos psize = *size; 2881.53Smatt } 2891.65Schristos 2901.53Smatt if (psize > 0) { 2911.65Schristos NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ? 2921.65Schristos vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp, 2931.53Smatt offset, *prot, flags); 2941.87Smatt flags &= VMCMD_RELATIVE; 2951.53Smatt } 2961.53Smatt if (psize < *size) { 2971.53Smatt NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize, 2981.87Smatt *addr + psize, vp, offset + psize, *prot, flags); 2991.53Smatt } 3001.1Sfvdl 3011.1Sfvdl /* 3021.87Smatt * Check if we need to extend the size of the segment (does 3031.87Smatt * bss extend page the next page boundary)? 3041.17Scgd */ 3051.1Sfvdl rm = round_page(*addr + msize); 3061.1Sfvdl rf = round_page(*addr + *size); 3071.1Sfvdl 3081.1Sfvdl if (rm != rf) { 3091.53Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 3101.83Smatt 0, *prot, flags & VMCMD_RELATIVE); 3111.1Sfvdl *size = msize; 3121.1Sfvdl } 3131.1Sfvdl} 3141.1Sfvdl 3151.1Sfvdl/* 3161.1Sfvdl * elf_load_file(): 3171.1Sfvdl * 3181.1Sfvdl * Load a file (interpreter/library) pointed to by path 3191.1Sfvdl * [stolen from coff_load_shlib()]. Made slightly generic 3201.1Sfvdl * so it might be used externally. 3211.1Sfvdl */ 3221.17Scgdint 3231.92SfvdlELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path, 3241.79Satatat struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap, 3251.54Sthorpej Elf_Addr *last) 3261.1Sfvdl{ 3271.83Smatt int error, i; 3281.1Sfvdl struct nameidata nd; 3291.14Scgd struct vnode *vp; 3301.14Scgd struct vattr attr; 3311.9Scgd Elf_Ehdr eh; 3321.9Scgd Elf_Phdr *ph = NULL; 3331.83Smatt const Elf_Phdr *ph0; 3341.83Smatt const Elf_Phdr *base_ph; 3351.83Smatt const Elf_Phdr *last_ph; 3361.1Sfvdl u_long phsize; 3371.9Scgd Elf_Addr addr = *last; 3381.1Sfvdl 3391.1Sfvdl /* 3401.17Scgd * 1. open file 3411.17Scgd * 2. read filehdr 3421.17Scgd * 3. map text, data, and bss out of it using VM_* 3431.17Scgd */ 3441.92Sfvdl NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 3451.12Scgd if ((error = namei(&nd)) != 0) 3461.1Sfvdl return error; 3471.14Scgd vp = nd.ni_vp; 3481.14Scgd 3491.26Smycroft /* 3501.26Smycroft * Similarly, if it's not marked as executable, or it's not a regular 3511.26Smycroft * file, we don't allow it to be used. 3521.26Smycroft */ 3531.14Scgd if (vp->v_type != VREG) { 3541.14Scgd error = EACCES; 3551.14Scgd goto badunlock; 3561.14Scgd } 3571.92Sfvdl if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 3581.26Smycroft goto badunlock; 3591.14Scgd 3601.17Scgd /* get attributes */ 3611.92Sfvdl if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 3621.14Scgd goto badunlock; 3631.14Scgd 3641.14Scgd /* 3651.14Scgd * Check mount point. Though we're not trying to exec this binary, 3661.15Scgd * we will be executing code from it, so if the mount point 3671.15Scgd * disallows execution or set-id-ness, we punt or kill the set-id. 3681.14Scgd */ 3691.14Scgd if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 3701.14Scgd error = EACCES; 3711.14Scgd goto badunlock; 3721.14Scgd } 3731.14Scgd if (vp->v_mount->mnt_flag & MNT_NOSUID) 3741.24Smycroft epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 3751.14Scgd 3761.12Scgd#ifdef notyet /* XXX cgd 960926 */ 3771.12Scgd XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 3781.12Scgd#endif 3791.76Schs 3801.76Schs error = vn_marktext(vp); 3811.76Schs if (error) 3821.76Schs goto badunlock; 3831.76Schs 3841.28Sfvdl VOP_UNLOCK(vp, 0); 3851.12Scgd 3861.92Sfvdl if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0) 3871.1Sfvdl goto bad; 3881.1Sfvdl 3891.46Skleink if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0) 3901.1Sfvdl goto bad; 3911.1Sfvdl 3921.90Schristos if (eh.e_phnum > MAXPHNUM) 3931.90Schristos goto bad; 3941.90Schristos 3951.9Scgd phsize = eh.e_phnum * sizeof(Elf_Phdr); 3961.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 3971.1Sfvdl 3981.92Sfvdl if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0) 3991.1Sfvdl goto bad; 4001.1Sfvdl 4011.1Sfvdl /* 4021.83Smatt * If no position to load the interpreter was set by a probe 4031.83Smatt * function, pick the same address that a non-fixed mmap(0, ..) 4041.83Smatt * would (i.e. something safely out of the way). 4051.83Smatt */ 4061.83Smatt if (*last == ELFDEFNNAME(NO_ADDR)) { 4071.83Smatt u_long limit = 0; 4081.83Smatt /* 4091.83Smatt * Find the start and ending addresses of the psections to 4101.83Smatt * be loaded. This will give us the size. 4111.83Smatt */ 4121.83Smatt for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum; 4131.83Smatt i++, ph0++) { 4141.83Smatt if (ph0->p_type == PT_LOAD) { 4151.83Smatt u_long psize = ph0->p_vaddr + ph0->p_memsz; 4161.83Smatt if (base_ph == NULL) 4171.83Smatt base_ph = ph0; 4181.83Smatt if (psize > limit) 4191.83Smatt limit = psize; 4201.83Smatt } 4211.83Smatt } 4221.83Smatt 4231.83Smatt /* 4241.83Smatt * Now compute the size and load address. 4251.83Smatt */ 4261.86Smatt addr = VM_DEFAULT_ADDRESS(epp->ep_daddr, 4271.83Smatt round_page(limit) - trunc_page(base_ph->p_vaddr)); 4281.83Smatt } else 4291.95Sdrochner addr = *last; /* may be ELF_LINK_ADDR */ 4301.83Smatt 4311.83Smatt /* 4321.83Smatt * Load all the necessary sections 4331.83Smatt */ 4341.83Smatt for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL; 4351.83Smatt i < eh.e_phnum; i++, ph0++) { 4361.83Smatt switch (ph0->p_type) { 4371.83Smatt case PT_LOAD: { 4381.83Smatt u_long size; 4391.83Smatt int prot = 0; 4401.83Smatt int flags; 4411.1Sfvdl 4421.53Smatt if (base_ph == NULL) { 4431.82Smatt /* 4441.82Smatt * First encountered psection is always the 4451.88Smatt * base psection. Make sure it's aligned 4461.89Smatt * properly (align down for topdown and align 4471.89Smatt * upwards for not topdown). 4481.82Smatt */ 4491.83Smatt base_ph = ph0; 4501.53Smatt flags = VMCMD_BASE; 4511.95Sdrochner if (addr == ELF_LINK_ADDR) 4521.95Sdrochner addr = ph0->p_vaddr; 4531.89Smatt if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN) 4541.89Smatt addr = ELF_TRUNC(addr, ph0->p_align); 4551.89Smatt else 4561.89Smatt addr = ELF_ROUND(addr, ph0->p_align); 4571.53Smatt } else { 4581.83Smatt u_long limit = round_page(last_ph->p_vaddr 4591.83Smatt + last_ph->p_memsz); 4601.87Smatt u_long base = trunc_page(ph0->p_vaddr); 4611.83Smatt 4621.82Smatt /* 4631.83Smatt * If there is a gap in between the psections, 4641.83Smatt * map it as inaccessible so nothing else 4651.83Smatt * mmap'ed will be placed there. 4661.82Smatt */ 4671.85Smatt if (limit != base) { 4681.83Smatt NEW_VMCMD2(vcset, vmcmd_map_zero, 4691.85Smatt base - limit, 4701.87Smatt limit - base_ph->p_vaddr, NULLVP, 4711.87Smatt 0, VM_PROT_NONE, VMCMD_RELATIVE); 4721.83Smatt } 4731.83Smatt 4741.83Smatt addr = ph0->p_vaddr - base_ph->p_vaddr; 4751.53Smatt flags = VMCMD_RELATIVE; 4761.53Smatt } 4771.83Smatt last_ph = ph0; 4781.14Scgd ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 4791.83Smatt &size, &prot, flags); 4801.82Smatt /* 4811.82Smatt * If entry is within this psection then this 4821.82Smatt * must contain the .text section. *entryoff is 4831.83Smatt * relative to the base psection. 4841.82Smatt */ 4851.83Smatt if (eh.e_entry >= ph0->p_vaddr && 4861.83Smatt eh.e_entry < (ph0->p_vaddr + size)) { 4871.83Smatt *entryoff = eh.e_entry - base_ph->p_vaddr; 4881.1Sfvdl } 4891.84Smatt addr += size; 4901.1Sfvdl break; 4911.83Smatt } 4921.1Sfvdl 4931.46Skleink case PT_DYNAMIC: 4941.46Skleink case PT_PHDR: 4951.46Skleink case PT_NOTE: 4961.1Sfvdl break; 4971.1Sfvdl 4981.1Sfvdl default: 4991.1Sfvdl break; 5001.1Sfvdl } 5011.1Sfvdl } 5021.1Sfvdl 5031.76Schs free(ph, M_TEMP); 5041.82Smatt /* 5051.82Smatt * This value is ignored if TOPDOWN. 5061.82Smatt */ 5071.12Scgd *last = addr; 5081.14Scgd vrele(vp); 5091.12Scgd return 0; 5101.12Scgd 5111.14Scgdbadunlock: 5121.28Sfvdl VOP_UNLOCK(vp, 0); 5131.14Scgd 5141.1Sfvdlbad: 5151.1Sfvdl if (ph != NULL) 5161.76Schs free(ph, M_TEMP); 5171.12Scgd#ifdef notyet /* XXX cgd 960926 */ 5181.12Scgd (maybe) VOP_CLOSE it 5191.12Scgd#endif 5201.14Scgd vrele(vp); 5211.1Sfvdl return error; 5221.1Sfvdl} 5231.1Sfvdl 5241.1Sfvdl/* 5251.1Sfvdl * exec_elf_makecmds(): Prepare an Elf binary's exec package 5261.1Sfvdl * 5271.1Sfvdl * First, set of the various offsets/lengths in the exec package. 5281.1Sfvdl * 5291.1Sfvdl * Then, mark the text image busy (so it can be demand paged) or error 5301.1Sfvdl * out if this is not possible. Finally, set up vmcmds for the 5311.1Sfvdl * text, data, bss, and stack segments. 5321.1Sfvdl */ 5331.1Sfvdlint 5341.92SfvdlELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp) 5351.1Sfvdl{ 5361.9Scgd Elf_Ehdr *eh = epp->ep_hdr; 5371.9Scgd Elf_Phdr *ph, *pp; 5381.9Scgd Elf_Addr phdr = 0, pos = 0; 5391.97Sthorpej int error, i, nload; 5401.58Sjdolecek char *interp = NULL; 5411.9Scgd u_long phsize; 5421.1Sfvdl 5431.9Scgd if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 5441.1Sfvdl return ENOEXEC; 5451.1Sfvdl 5461.45Sfvdl /* 5471.45Sfvdl * XXX allow for executing shared objects. It seems silly 5481.45Sfvdl * but other ELF-based systems allow it as well. 5491.45Sfvdl */ 5501.46Skleink if (ELFNAME(check_header)(eh, ET_EXEC) != 0 && 5511.46Skleink ELFNAME(check_header)(eh, ET_DYN) != 0) 5521.1Sfvdl return ENOEXEC; 5531.1Sfvdl 5541.90Schristos if (eh->e_phnum > MAXPHNUM) 5551.90Schristos return ENOEXEC; 5561.90Schristos 5571.76Schs error = vn_marktext(epp->ep_vp); 5581.76Schs if (error) 5591.76Schs return (error); 5601.76Schs 5611.1Sfvdl /* 5621.17Scgd * Allocate space to hold all the program headers, and read them 5631.17Scgd * from the file 5641.17Scgd */ 5651.9Scgd phsize = eh->e_phnum * sizeof(Elf_Phdr); 5661.17Scgd ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 5671.1Sfvdl 5681.92Sfvdl if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) != 5691.64Schristos 0) 5701.1Sfvdl goto bad; 5711.1Sfvdl 5721.19Scgd epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 5731.19Scgd epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 5741.1Sfvdl 5751.1Sfvdl for (i = 0; i < eh->e_phnum; i++) { 5761.1Sfvdl pp = &ph[i]; 5771.46Skleink if (pp->p_type == PT_INTERP) { 5781.58Sjdolecek if (pp->p_filesz >= MAXPATHLEN) 5791.1Sfvdl goto bad; 5801.95Sdrochner MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK); 5811.95Sdrochner interp[0] = '\0'; 5821.92Sfvdl if ((error = exec_read_from(p, epp->ep_vp, 5831.64Schristos pp->p_offset, interp, pp->p_filesz)) != 0) 5841.1Sfvdl goto bad; 5851.1Sfvdl break; 5861.1Sfvdl } 5871.1Sfvdl } 5881.1Sfvdl 5891.9Scgd /* 5901.1Sfvdl * On the same architecture, we may be emulating different systems. 5911.99Sskrll * See which one will accept this executable. 5921.1Sfvdl * 5931.1Sfvdl * Probe functions would normally see if the interpreter (if any) 5941.1Sfvdl * exists. Emulation packages may possibly replace the interpreter in 5951.58Sjdolecek * interp[] with a changed path (/emul/xxx/<path>). 5961.1Sfvdl */ 5971.95Sdrochner pos = ELFDEFNNAME(NO_ADDR); 5981.95Sdrochner if (epp->ep_esch->u.elf_probe_func) { 5991.95Sdrochner vaddr_t startp = (vaddr_t)pos; 6001.62Seeh 6011.92Sfvdl error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp, 6021.62Seeh &startp); 6031.1Sfvdl if (error) 6041.1Sfvdl goto bad; 6051.95Sdrochner pos = (Elf_Addr)startp; 6061.1Sfvdl } 6071.1Sfvdl 6081.1Sfvdl /* 6091.17Scgd * Load all the necessary sections 6101.17Scgd */ 6111.97Sthorpej for (i = nload = 0; i < eh->e_phnum; i++) { 6121.9Scgd Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 6131.9Scgd u_long size = 0; 6141.1Sfvdl int prot = 0; 6151.1Sfvdl 6161.1Sfvdl pp = &ph[i]; 6171.1Sfvdl 6181.1Sfvdl switch (ph[i].p_type) { 6191.46Skleink case PT_LOAD: 6201.4Sfvdl /* 6211.97Sthorpej * XXX 6221.97Sthorpej * Can handle only 2 sections: text and data 6231.4Sfvdl */ 6241.97Sthorpej if (nload++ == 2) 6251.97Sthorpej goto bad; 6261.9Scgd ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 6271.79Satatat &ph[i], &addr, &size, &prot, VMCMD_FIXED); 6281.17Scgd 6291.4Sfvdl /* 6301.4Sfvdl * Decide whether it's text or data by looking 6311.97Sthorpej * at the entry point. 6321.4Sfvdl */ 6331.97Sthorpej if (eh->e_entry >= addr && 6341.97Sthorpej eh->e_entry < (addr + size)) { 6351.97Sthorpej epp->ep_taddr = addr; 6361.97Sthorpej epp->ep_tsize = size; 6371.97Sthorpej if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 6381.19Scgd epp->ep_daddr = addr; 6391.19Scgd epp->ep_dsize = size; 6401.19Scgd } 6411.97Sthorpej } else { 6421.97Sthorpej epp->ep_daddr = addr; 6431.97Sthorpej epp->ep_dsize = size; 6441.4Sfvdl } 6451.1Sfvdl break; 6461.1Sfvdl 6471.46Skleink case PT_SHLIB: 6481.60Smycroft /* SCO has these sections. */ 6491.46Skleink case PT_INTERP: 6501.60Smycroft /* Already did this one. */ 6511.46Skleink case PT_DYNAMIC: 6521.46Skleink case PT_NOTE: 6531.1Sfvdl break; 6541.1Sfvdl 6551.46Skleink case PT_PHDR: 6561.4Sfvdl /* Note address of program headers (in text segment) */ 6571.4Sfvdl phdr = pp->p_vaddr; 6581.7Schristos break; 6591.4Sfvdl 6601.1Sfvdl default: 6611.1Sfvdl /* 6621.9Scgd * Not fatal; we don't need to understand everything. 6631.1Sfvdl */ 6641.1Sfvdl break; 6651.1Sfvdl } 6661.1Sfvdl } 6671.1Sfvdl 6681.1Sfvdl /* 6691.17Scgd * Check if we found a dynamically linked binary and arrange to load 6701.79Satatat * its interpreter 6711.17Scgd */ 6721.95Sdrochner if (interp) { 6731.1Sfvdl struct elf_args *ap; 6741.79Satatat int i = epp->ep_vmcmds.evs_used; 6751.79Satatat u_long interp_offset; 6761.1Sfvdl 6771.58Sjdolecek MALLOC(ap, struct elf_args *, sizeof(struct elf_args), 6781.17Scgd M_TEMP, M_WAITOK); 6791.92Sfvdl if ((error = ELFNAME(load_file)(p, epp, interp, 6801.79Satatat &epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) { 6811.76Schs FREE(ap, M_TEMP); 6821.1Sfvdl goto bad; 6831.1Sfvdl } 6841.79Satatat ap->arg_interp = epp->ep_vmcmds.evs_cmds[i].ev_addr; 6851.79Satatat epp->ep_entry = ap->arg_interp + interp_offset; 6861.4Sfvdl ap->arg_phaddr = phdr; 6871.1Sfvdl 6881.1Sfvdl ap->arg_phentsize = eh->e_phentsize; 6891.1Sfvdl ap->arg_phnum = eh->e_phnum; 6901.1Sfvdl ap->arg_entry = eh->e_entry; 6911.1Sfvdl 6921.1Sfvdl epp->ep_emul_arg = ap; 6931.95Sdrochner 6941.95Sdrochner FREE(interp, M_TEMP); 6951.1Sfvdl } else 6961.1Sfvdl epp->ep_entry = eh->e_entry; 6971.1Sfvdl 6981.8Schristos#ifdef ELF_MAP_PAGE_ZERO 6991.8Schristos /* Dell SVR4 maps page zero, yeuch! */ 7001.57Sthorpej NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0, 7011.57Sthorpej epp->ep_vp, 0, VM_PROT_READ); 7021.8Schristos#endif 7031.76Schs free(ph, M_TEMP); 7041.94Schristos return (*epp->ep_esch->es_setup_stack)(p, epp); 7051.1Sfvdl 7061.1Sfvdlbad: 7071.58Sjdolecek if (interp) 7081.58Sjdolecek FREE(interp, M_TEMP); 7091.76Schs free(ph, M_TEMP); 7101.1Sfvdl kill_vmcmds(&epp->ep_vmcmds); 7111.1Sfvdl return ENOEXEC; 7121.40Schristos} 7131.40Schristos 7141.59Smrgint 7151.92SfvdlELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp, 7161.54Sthorpej Elf_Ehdr *eh) 7171.40Schristos{ 7181.61Smycroft size_t i; 7191.61Smycroft Elf_Phdr *ph; 7201.40Schristos size_t phsize; 7211.40Schristos int error; 7221.90Schristos 7231.90Schristos if (eh->e_phnum > MAXPHNUM) 7241.90Schristos return ENOEXEC; 7251.40Schristos 7261.40Schristos phsize = eh->e_phnum * sizeof(Elf_Phdr); 7271.61Smycroft ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 7281.92Sfvdl error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize); 7291.61Smycroft if (error) 7301.61Smycroft goto out; 7311.40Schristos 7321.61Smycroft for (i = 0; i < eh->e_phnum; i++) { 7331.61Smycroft Elf_Phdr *ephp = &ph[i]; 7341.61Smycroft Elf_Nhdr *np; 7351.40Schristos 7361.61Smycroft if (ephp->p_type != PT_NOTE || 7371.61Smycroft ephp->p_filesz > 1024 || 7381.61Smycroft ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 7391.40Schristos continue; 7401.40Schristos 7411.61Smycroft np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 7421.92Sfvdl error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np, 7431.64Schristos ephp->p_filesz); 7441.61Smycroft if (error) 7451.61Smycroft goto next; 7461.40Schristos 7471.61Smycroft if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG || 7481.61Smycroft np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 7491.61Smycroft np->n_descsz != ELF_NOTE_NETBSD_DESCSZ || 7501.61Smycroft memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME, 7511.40Schristos ELF_NOTE_NETBSD_NAMESZ)) 7521.61Smycroft goto next; 7531.40Schristos 7541.40Schristos error = 0; 7551.61Smycroft free(np, M_TEMP); 7561.61Smycroft goto out; 7571.61Smycroft 7581.61Smycroft next: 7591.61Smycroft free(np, M_TEMP); 7601.61Smycroft continue; 7611.40Schristos } 7621.40Schristos 7631.40Schristos error = ENOEXEC; 7641.61Smycroftout: 7651.61Smycroft free(ph, M_TEMP); 7661.61Smycroft return (error); 7671.40Schristos} 7681.40Schristos 7691.58Sjdolecekint 7701.92SfvdlELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp, 7711.58Sjdolecek void *eh, char *itp, vaddr_t *pos) 7721.40Schristos{ 7731.40Schristos int error; 7741.40Schristos 7751.92Sfvdl if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0) 7761.40Schristos return error; 7771.95Sdrochner#ifdef ELF_INTERP_NON_RELOCATABLE 7781.95Sdrochner *pos = ELF_LINK_ADDR; 7791.95Sdrochner#endif 7801.40Schristos return 0; 7811.1Sfvdl} 782