1 /* $NetBSD: linux_exec_elf32.c,v 1.54 2001/11/13 02:08:51 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1998, 2000, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas, Frank van der Linden, Eric Haszlakiewicz and 9 * Emmanuel Dreyfus. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * based on exec_aout.c, sunos_exec.c and svr4_exec.c 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: linux_exec_elf32.c,v 1.54 2001/11/13 02:08:51 lukem Exp $"); 46 47 #ifndef ELFSIZE 48 #define ELFSIZE 32 /* XXX should die */ 49 #endif 50 51 #include <sys/param.h> 52 #include <sys/systm.h> 53 #include <sys/kernel.h> 54 #include <sys/proc.h> 55 #include <sys/malloc.h> 56 #include <sys/namei.h> 57 #include <sys/vnode.h> 58 #include <sys/mount.h> 59 #include <sys/exec.h> 60 #include <sys/exec_elf.h> 61 62 #include <sys/mman.h> 63 #include <sys/syscallargs.h> 64 65 #include <machine/cpu.h> 66 #include <machine/reg.h> 67 68 #include <compat/linux/common/linux_types.h> 69 #include <compat/linux/common/linux_signal.h> 70 #include <compat/linux/common/linux_util.h> 71 #include <compat/linux/common/linux_exec.h> 72 #include <compat/linux/common/linux_machdep.h> 73 74 #include <compat/linux/linux_syscallargs.h> 75 #include <compat/linux/linux_syscall.h> 76 77 static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *, 78 Elf_Ehdr *, char *)); 79 #ifdef LINUX_GCC_SIGNATURE 80 static int ELFNAME2(linux,gcc_signature) __P((struct proc *p, 81 struct exec_package *, Elf_Ehdr *)); 82 #endif 83 #ifdef LINUX_ATEXIT_SIGNATURE 84 static int ELFNAME2(linux,atexit_signature) __P((struct proc *p, 85 struct exec_package *, Elf_Ehdr *)); 86 #endif 87 88 #ifdef LINUX_ATEXIT_SIGNATURE 89 /* 90 * On the PowerPC, statically linked Linux binaries are not recognized 91 * by linux_signature nor by linux_gcc_signature. Fortunately, thoses 92 * binaries features a __libc_atexit ELF section. We therefore assume we 93 * have a Linux binary if we find this section. 94 */ 95 static int 96 ELFNAME2(linux,atexit_signature)(p, epp, eh) 97 struct proc *p; 98 struct exec_package *epp; 99 Elf_Ehdr *eh; 100 { 101 size_t shsize; 102 int strndx; 103 size_t i; 104 static const char signature[] = "__libc_atexit"; 105 char* strtable; 106 Elf_Shdr *sh; 107 108 int error; 109 110 /* 111 * load the section header table 112 */ 113 shsize = eh->e_shnum * sizeof(Elf_Shdr); 114 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK); 115 error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize); 116 if (error) 117 goto out; 118 119 /* 120 * Now let's find the string table. If it does not exists, give up. 121 */ 122 strndx = (int)(eh->e_shstrndx); 123 if (strndx == SHN_UNDEF) { 124 error = ENOEXEC; 125 goto out; 126 } 127 128 /* 129 * strndx is the index in section header table of the string table 130 * section get the whole string table in strtable, and then we get access to the names 131 * s->sh_name is the offset of the section name in strtable. 132 */ 133 strtable = malloc(sh[strndx].sh_size, M_TEMP, M_WAITOK); 134 error = exec_read_from(p, epp->ep_vp, sh[strndx].sh_offset, strtable, 135 sh[strndx].sh_size); 136 if (error) 137 goto out; 138 139 for (i = 0; i < eh->e_shnum; i++) { 140 Elf_Shdr *s = &sh[i]; 141 if (!memcmp((void*)(&(strtable[s->sh_name])), signature, 142 sizeof(signature))) { 143 #ifdef DEBUG_LINUX 144 printf("linux_atexit_sig=%s\n",&(strtable[s->sh_name])); 145 #endif 146 error = 0; 147 goto out; 148 } 149 } 150 error = ENOEXEC; 151 152 out: 153 free(sh, M_TEMP); 154 free(strtable, M_TEMP); 155 return (error); 156 } 157 #endif 158 159 #ifdef LINUX_GCC_SIGNATURE 160 /* 161 * Take advantage of the fact that all the linux binaries are compiled 162 * with gcc, and gcc sticks in the comment field a signature. Note that 163 * on SVR4 binaries, the gcc signature will follow the OS name signature, 164 * that will not be a problem. We don't bother to read in the string table, 165 * but we check all the progbits headers. 166 * 167 * XXX This only works in the i386. On the alpha (at least) 168 * XXX we have the same gcc signature which incorrectly identifies 169 * XXX NetBSD binaries as Linux. 170 */ 171 static int 172 ELFNAME2(linux,gcc_signature)(p, epp, eh) 173 struct proc *p; 174 struct exec_package *epp; 175 Elf_Ehdr *eh; 176 { 177 size_t shsize; 178 size_t i; 179 static const char signature[] = "\0GCC: (GNU) "; 180 char buf[sizeof(signature) - 1]; 181 Elf_Shdr *sh; 182 int error; 183 184 shsize = eh->e_shnum * sizeof(Elf_Shdr); 185 sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK); 186 error = exec_read_from(p, epp->ep_vp, eh->e_shoff, sh, shsize); 187 if (error) 188 goto out; 189 190 for (i = 0; i < eh->e_shnum; i++) { 191 Elf_Shdr *s = &sh[i]; 192 193 /* 194 * Identify candidates for the comment header; 195 * Header cannot have a load address, or flags and 196 * it must be large enough. 197 */ 198 if (s->sh_type != SHT_PROGBITS || 199 s->sh_addr != 0 || 200 s->sh_flags != 0 || 201 s->sh_size < sizeof(signature) - 1) 202 continue; 203 204 error = exec_read_from(p, epp->ep_vp, s->sh_offset, buf, 205 sizeof(signature) - 1); 206 if (error) 207 continue; 208 209 /* 210 * error is 0, if the signatures match we are done. 211 */ 212 #ifdef DEBUG_LINUX 213 printf("linux_gcc_sig: sig=%s\n", buf); 214 #endif 215 if (!memcmp(buf, signature, sizeof(signature) - 1)) { 216 error = 0; 217 goto out; 218 } 219 } 220 error = ENOEXEC; 221 222 out: 223 free(sh, M_TEMP); 224 return (error); 225 } 226 #endif 227 228 static int 229 ELFNAME2(linux,signature)(p, epp, eh, itp) 230 struct proc *p; 231 struct exec_package *epp; 232 Elf_Ehdr *eh; 233 char *itp; 234 { 235 size_t i; 236 Elf_Phdr *ph; 237 size_t phsize; 238 int error; 239 240 phsize = eh->e_phnum * sizeof(Elf_Phdr); 241 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 242 error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize); 243 if (error) 244 goto out; 245 246 for (i = 0; i < eh->e_phnum; i++) { 247 Elf_Phdr *ephp = &ph[i]; 248 Elf_Nhdr *np; 249 u_int32_t *abi; 250 251 if (ephp->p_type != PT_NOTE || 252 ephp->p_filesz > 1024 || 253 ephp->p_filesz < sizeof(Elf_Nhdr) + 20) 254 continue; 255 256 np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK); 257 error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np, 258 ephp->p_filesz); 259 if (error) 260 goto next; 261 262 if (np->n_type != ELF_NOTE_TYPE_ABI_TAG || 263 np->n_namesz != ELF_NOTE_ABI_NAMESZ || 264 np->n_descsz != ELF_NOTE_ABI_DESCSZ || 265 memcmp((caddr_t)(np + 1), ELF_NOTE_ABI_NAME, 266 ELF_NOTE_ABI_NAMESZ)) 267 goto next; 268 269 /* Make sure the OS is Linux. */ 270 abi = (u_int32_t *)((caddr_t)np + sizeof(Elf_Nhdr) + 271 np->n_namesz); 272 if (abi[0] == ELF_NOTE_ABI_OS_LINUX) 273 error = 0; 274 else 275 error = ENOEXEC; 276 free(np, M_TEMP); 277 goto out; 278 279 next: 280 free(np, M_TEMP); 281 continue; 282 } 283 284 /* Check for certain intepreter names. */ 285 if (itp[0]) { 286 if (!strncmp(itp, "/lib/ld-linux", 13) || 287 !strncmp(itp, "/lib/ld.so.", 11)) 288 error = 0; 289 else 290 error = ENOEXEC; 291 goto out; 292 } 293 294 error = ENOEXEC; 295 out: 296 free(ph, M_TEMP); 297 return (error); 298 } 299 300 int 301 ELFNAME2(linux,probe)(p, epp, eh, itp, pos) 302 struct proc *p; 303 struct exec_package *epp; 304 void *eh; 305 char *itp; 306 vaddr_t *pos; 307 { 308 const char *bp; 309 int error; 310 size_t len; 311 312 if (((error = ELFNAME2(linux,signature)(p, epp, eh, itp)) != 0) && 313 #ifdef LINUX_GCC_SIGNATURE 314 ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0) && 315 #endif 316 #ifdef LINUX_ATEXIT_SIGNATURE 317 ((error = ELFNAME2(linux,atexit_signature)(p, epp, eh)) != 0) && 318 #endif 319 1) 320 return error; 321 322 if (itp[0]) { 323 if ((error = emul_find(p, NULL, epp->ep_esch->es_emul->e_path, 324 itp, &bp, 0))) 325 return error; 326 if ((error = copystr(bp, itp, MAXPATHLEN, &len))) 327 return error; 328 free((void *)bp, M_TEMP); 329 } 330 *pos = ELF_NO_ADDR; 331 #ifdef DEBUG_LINUX 332 printf("linux_probe: returning 0\n"); 333 #endif 334 return 0; 335 } 336 337