1/* $NetBSD: ibcs2_exec_elf32.c,v 1.16 2007/12/04 18:40:10 dsl Exp $ */ 2 3/* 4 * Copyright (c) 1994, 1995, 1998 Scott Bartram 5 * Copyright (c) 1994 Adam Glass 6 * Copyright (c) 1993, 1994 Christopher G. Demetriou 7 * All rights reserved. 8 * 9 * originally from kern/exec_ecoff.c 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 Scott Bartram. 22 * 4. The name of the author may not be used to endorse or promote products 23 * derived from this software without specific prior written permission 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37#include <sys/cdefs.h> 38__KERNEL_RCSID(0, "$NetBSD: ibcs2_exec_elf32.c,v 1.16 2007/12/04 18:40:10 dsl Exp $"); 39 40#define ELFSIZE 32 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/kernel.h> 45#include <sys/proc.h> 46#include <sys/malloc.h> 47#include <sys/namei.h> 48#include <sys/vnode.h> 49#include <sys/mount.h> 50#include <sys/exec.h> 51#include <sys/exec_elf.h> 52 53#include <sys/mman.h> 54 55#include <sys/cpu.h> 56#include <machine/reg.h> 57#include <machine/ibcs2_machdep.h> 58 59#include <compat/ibcs2/ibcs2_types.h> 60#include <compat/ibcs2/ibcs2_exec.h> 61#include <compat/ibcs2/ibcs2_errno.h> 62#include <compat/ibcs2/ibcs2_util.h> 63 64static int ibcs2_elf32_signature(struct lwp *l, struct exec_package *, 65 Elf32_Ehdr *); 66 67/* 68 * The SCO compiler adds the string "SCO" to the .notes section of all 69 * binaries I've seen so far. 70 * 71 * XXX - probably should only compare the id in the actual ELF notes struct 72 */ 73 74#define SCO_SIGNATURE "\004\0\0\0\014\0\0\0\001\0\0\0SCO\0" 75 76static int 77ibcs2_elf32_signature(struct lwp *l, struct exec_package *epp, Elf32_Ehdr *eh) 78{ 79 size_t shsize = sizeof(Elf32_Shdr) * eh->e_shnum; 80 size_t i; 81 static const char signature[] = SCO_SIGNATURE; 82 char tbuf[sizeof(signature) - 1]; 83 Elf32_Shdr *sh; 84 int error; 85 86 if (shsize > 64 * 1024) 87 return ENOEXEC; 88 89 sh = (Elf32_Shdr *)malloc(shsize, M_TEMP, M_WAITOK); 90 91 if ((error = exec_read_from(l, epp->ep_vp, eh->e_shoff, sh, 92 shsize)) != 0) 93 goto out; 94 95 for (i = 0; i < eh->e_shnum; i++) { 96 Elf32_Shdr *s = &sh[i]; 97 if (s->sh_type != SHT_NOTE || 98 s->sh_flags != 0 || 99 s->sh_size < sizeof(signature) - 1) 100 continue; 101 102 if ((error = exec_read_from(l, epp->ep_vp, s->sh_offset, tbuf, 103 sizeof(signature) - 1)) != 0) 104 goto out; 105 106 if (memcmp(tbuf, signature, sizeof(signature) - 1) == 0) 107 goto out; 108 else 109 break; /* only one .note section so quit */ 110 } 111 error = EFTYPE; 112 113out: 114 free(sh, M_TEMP); 115 return error; 116} 117 118/* 119 * ibcs2_elf32_probe - search the executable for signs of SCO 120 */ 121 122int 123ibcs2_elf32_probe(struct lwp *l, struct exec_package *epp, void *eh, char *itp, 124 vaddr_t *pos) 125{ 126 int error; 127 128 if ((error = ibcs2_elf32_signature(l, epp, eh)) != 0) 129 return error; 130 131 if (itp) { 132 if ((error = emul_find_interp(l, epp, itp))) 133 return error; 134 } 135 return 0; 136} 137