1 /* $NetBSD: netbsd32_exec_elf32.c,v 1.29 2008/10/26 07:07:35 mrg Exp $ */ 2 /* from: NetBSD: exec_aout.c,v 1.15 1996/09/26 23:34:46 cgd Exp */ 3 4 /* 5 * Copyright (c) 1998, 2001 Matthew R. Green. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /* 30 * Copyright (c) 1993, 1994 Christopher G. Demetriou 31 * All rights reserved. 32 * 33 * Redistribution and use in source and binary forms, with or without 34 * modification, are permitted provided that the following conditions 35 * are met: 36 * 1. Redistributions of source code must retain the above copyright 37 * notice, this list of conditions and the following disclaimer. 38 * 2. Redistributions in binary form must reproduce the above copyright 39 * notice, this list of conditions and the following disclaimer in the 40 * documentation and/or other materials provided with the distribution. 41 * 3. All advertising materials mentioning features or use of this software 42 * must display the following acknowledgement: 43 * This product includes software developed by Christopher G. Demetriou. 44 * 4. The name of the author may not be used to endorse or promote products 45 * derived from this software without specific prior written permission 46 * 47 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 48 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 49 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 50 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 51 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 52 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 53 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 54 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 55 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 56 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 57 */ 58 59 #include <sys/cdefs.h> 60 __KERNEL_RCSID(0, "$NetBSD: netbsd32_exec_elf32.c,v 1.29 2008/10/26 07:07:35 mrg Exp $"); 61 62 #define ELFSIZE 32 63 64 #include <sys/param.h> 65 #include <sys/systm.h> 66 #include <sys/proc.h> 67 #include <sys/malloc.h> 68 #include <sys/vnode.h> 69 #include <sys/exec.h> 70 #include <sys/exec_elf.h> 71 #include <sys/resourcevar.h> 72 #include <sys/signal.h> 73 #include <sys/signalvar.h> 74 #include <sys/kauth.h> 75 #include <sys/namei.h> 76 77 #include <compat/netbsd32/netbsd32.h> 78 #include <compat/netbsd32/netbsd32_exec.h> 79 80 #include <machine/frame.h> 81 #include <machine/netbsd32_machdep.h> 82 83 int netbsd32_copyinargs(struct exec_package *, struct ps_strings *, 84 void *, size_t, const void *, const void *); 85 int ELFNAME2(netbsd32,probe_noteless)(struct lwp *, struct exec_package *epp, 86 void *eh, char *itp, vaddr_t *pos); 87 extern int ELFNAME2(netbsd,signature)(struct lwp *, struct exec_package *, 88 Elf_Ehdr *); 89 90 int 91 ELFNAME2(netbsd32,probe)(struct lwp *l, struct exec_package *epp, 92 void *eh, char *itp, vaddr_t *pos) 93 { 94 int error; 95 96 if ((error = ELFNAME2(netbsd,signature)(l, epp, eh)) != 0) 97 return error; 98 99 return ELFNAME2(netbsd32,probe_noteless)(l, epp, eh, itp, pos); 100 } 101 102 int 103 ELFNAME2(netbsd32,probe_noteless)(struct lwp *l, struct exec_package *epp, 104 void *eh, char *itp, vaddr_t *pos) 105 { 106 int error; 107 108 if (itp) { 109 /* 110 * If the path is exactly "/usr/libexec/ld.elf_so", first 111 * try to see if "/usr/libexec/ld.elf_so-<arch>" exists 112 * and if so, use that instead. 113 * XXX maybe move this into compat/common 114 */ 115 error = 0; 116 if (strcmp(itp, "/usr/libexec/ld.elf_so") == 0 || 117 strcmp(itp, "/libexec/ld.elf_so") == 0) { 118 extern const char machine32[]; 119 struct nameidata nd; 120 char *path; 121 122 if (epp->ep_interp != NULL) 123 vrele(epp->ep_interp); 124 125 path = PNBUF_GET(); 126 snprintf(path, MAXPATHLEN, "%s-%s", itp, machine32); 127 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path); 128 error = namei(&nd); 129 /* 130 * If that worked, save interpreter in case we 131 * actually need to load it 132 */ 133 if (error != 0) 134 epp->ep_interp = NULL; 135 else 136 epp->ep_interp = nd.ni_vp; 137 PNBUF_PUT(path); 138 } 139 140 /* Translate interpreter name if needed */ 141 if (error && (error = emul_find_interp(l, epp, itp)) != 0) 142 return error; 143 } 144 epp->ep_flags |= EXEC_32; 145 epp->ep_vm_minaddr = VM_MIN_ADDRESS; 146 epp->ep_vm_maxaddr = USRSTACK32; 147 #ifdef ELF_INTERP_NON_RELOCATABLE 148 *pos = ELF_LINK_ADDR; 149 #endif 150 return 0; 151 } 152 153 /* round up and down to page boundaries. */ 154 #define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 155 #define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 156 157 /* 158 * Copy arguments onto the stack in the normal way, but add some 159 * extra information in case of dynamic binding. 160 */ 161 int 162 netbsd32_elf32_copyargs(struct lwp *l, struct exec_package *pack, 163 struct ps_strings *arginfo, char **stackp, void *argp) 164 { 165 size_t len; 166 AuxInfo ai[ELF_AUX_ENTRIES], *a; 167 struct elf_args *ap; 168 int error; 169 170 if ((error = netbsd32_copyargs(l, pack, arginfo, stackp, argp)) != 0) 171 return error; 172 173 a = ai; 174 175 /* 176 * Push extra arguments on the stack needed by dynamically 177 * linked binaries 178 */ 179 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 180 181 a->a_type = AT_PHDR; 182 a->a_v = ap->arg_phaddr; 183 a++; 184 185 a->a_type = AT_PHENT; 186 a->a_v = ap->arg_phentsize; 187 a++; 188 189 a->a_type = AT_PHNUM; 190 a->a_v = ap->arg_phnum; 191 a++; 192 193 a->a_type = AT_PAGESZ; 194 a->a_v = PAGE_SIZE; 195 a++; 196 197 a->a_type = AT_BASE; 198 a->a_v = ap->arg_interp; 199 a++; 200 201 a->a_type = AT_FLAGS; 202 a->a_v = 0; 203 a++; 204 205 a->a_type = AT_ENTRY; 206 a->a_v = ap->arg_entry; 207 a++; 208 209 a->a_type = AT_EUID; 210 a->a_v = kauth_cred_geteuid(l->l_cred); 211 a++; 212 213 a->a_type = AT_RUID; 214 a->a_v = kauth_cred_getuid(l->l_cred); 215 a++; 216 217 a->a_type = AT_EGID; 218 a->a_v = kauth_cred_getegid(l->l_cred); 219 a++; 220 221 a->a_type = AT_RGID; 222 a->a_v = kauth_cred_getgid(l->l_cred); 223 a++; 224 225 free((char *)ap, M_TEMP); 226 pack->ep_emul_arg = NULL; 227 } 228 229 a->a_type = AT_NULL; 230 a->a_v = 0; 231 a++; 232 233 len = (a - ai) * sizeof(AuxInfo); 234 if ((error = copyout(ai, *stackp, len)) != 0) 235 return error; 236 *stackp += len; 237 238 return 0; 239 } 240