1999-10-25 17:55:06 +04:00
|
|
|
/* $NetBSD: linux_exec_elf32.c,v 1.43 1999/10/25 13:55:07 kleink Exp $ */
|
1998-09-05 18:50:25 +04:00
|
|
|
|
|
|
|
/*-
|
1998-10-04 04:02:23 +04:00
|
|
|
* Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
|
1998-09-05 18:50:25 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
1998-10-04 04:02:23 +04:00
|
|
|
* by Christos Zoulas, Frank van der Linden and Eric Haszlakiewicz.
|
1998-09-05 18:50:25 +04:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1998-10-01 07:11:33 +04:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
1998-09-05 18:50:25 +04:00
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
1995-03-01 02:24:35 +03:00
|
|
|
|
|
|
|
/*
|
1995-06-11 19:15:09 +04:00
|
|
|
* based on exec_aout.c, sunos_exec.c and svr4_exec.c
|
1995-03-01 02:24:35 +03:00
|
|
|
*/
|
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
#ifndef ELFSIZE
|
1996-09-27 22:14:23 +04:00
|
|
|
#define ELFSIZE 32 /* XXX should die */
|
1998-10-01 07:11:33 +04:00
|
|
|
#endif
|
1996-09-27 00:52:42 +04:00
|
|
|
|
1995-03-01 02:24:35 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/vnode.h>
|
1996-04-05 04:01:10 +04:00
|
|
|
#include <sys/mount.h>
|
1996-10-13 22:30:05 +04:00
|
|
|
#include <sys/exec.h>
|
1995-06-23 01:34:27 +04:00
|
|
|
#include <sys/exec_elf.h>
|
1995-03-01 02:24:35 +03:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
1996-04-05 04:01:10 +04:00
|
|
|
#include <sys/syscallargs.h>
|
|
|
|
|
1995-03-01 02:24:35 +03:00
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/vm_map.h>
|
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/reg.h>
|
|
|
|
|
1998-10-04 00:17:36 +04:00
|
|
|
#include <compat/linux/common/linux_types.h>
|
|
|
|
#include <compat/linux/common/linux_signal.h>
|
|
|
|
#include <compat/linux/common/linux_util.h>
|
|
|
|
#include <compat/linux/common/linux_exec.h>
|
|
|
|
#include <compat/linux/common/linux_machdep.h>
|
1998-10-23 14:54:58 +04:00
|
|
|
#include <compat/linux/common/linux_errno.h>
|
1995-03-01 02:24:35 +03:00
|
|
|
|
1998-10-04 00:17:36 +04:00
|
|
|
#include <compat/linux/linux_syscallargs.h>
|
|
|
|
#include <compat/linux/linux_syscall.h>
|
1998-10-01 07:11:33 +04:00
|
|
|
|
|
|
|
static int ELFNAME2(linux,signature) __P((struct proc *, struct exec_package *,
|
|
|
|
Elf_Ehdr *));
|
|
|
|
#ifdef LINUX_GCC_SIGNATURE
|
|
|
|
static int ELFNAME2(linux,gcc_signature) __P((struct proc *p,
|
|
|
|
struct exec_package *, Elf_Ehdr *));
|
|
|
|
#endif
|
1995-04-22 23:48:19 +04:00
|
|
|
|
1999-05-01 03:07:01 +04:00
|
|
|
#define LINUX_ELF_AUX_ARGSIZ howmany(sizeof(AuxInfo) * 8, sizeof(char *))
|
1995-06-23 01:34:27 +04:00
|
|
|
|
1995-04-22 23:48:19 +04:00
|
|
|
|
1995-10-07 09:25:19 +03:00
|
|
|
extern char linux_sigcode[], linux_esigcode[];
|
1995-04-22 23:48:19 +04:00
|
|
|
extern struct sysent linux_sysent[];
|
|
|
|
extern char *linux_syscallnames[];
|
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
struct emul ELFNAMEEND(emul_linux) = {
|
1995-06-11 18:56:47 +04:00
|
|
|
"linux",
|
1998-10-23 07:53:18 +04:00
|
|
|
native_to_linux_errno,
|
1995-06-11 18:56:47 +04:00
|
|
|
linux_sendsig,
|
|
|
|
LINUX_SYS_syscall,
|
|
|
|
LINUX_SYS_MAXSYSCALL,
|
|
|
|
linux_sysent,
|
|
|
|
linux_syscallnames,
|
|
|
|
LINUX_ELF_AUX_ARGSIZ,
|
1998-10-01 07:11:33 +04:00
|
|
|
ELFNAME(copyargs),
|
1998-01-24 15:32:15 +03:00
|
|
|
linux_setregs,
|
1995-04-22 23:48:19 +04:00
|
|
|
linux_sigcode,
|
|
|
|
linux_esigcode,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
#ifdef LINUX_GCC_SIGNATURE
|
1996-06-13 22:42:01 +04:00
|
|
|
/*
|
|
|
|
* Take advantage of the fact that all the linux binaries are compiled
|
|
|
|
* with gcc, and gcc sticks in the comment field a signature. Note that
|
|
|
|
* on SVR4 binaries, the gcc signature will follow the OS name signature,
|
|
|
|
* that will not be a problem. We don't bother to read in the string table,
|
|
|
|
* but we check all the progbits headers.
|
1998-10-01 07:11:33 +04:00
|
|
|
*
|
|
|
|
* XXX This only works in the i386. On the alpha (at least)
|
|
|
|
* XXX we have the same gcc signature which incorrectly identifies
|
|
|
|
* XXX NetBSD binaries as Linux.
|
1996-06-13 22:42:01 +04:00
|
|
|
*/
|
|
|
|
static int
|
1998-10-01 07:11:33 +04:00
|
|
|
ELFNAME2(linux,gcc_signature)(p, epp, eh)
|
1996-06-13 22:42:01 +04:00
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
1998-10-01 07:11:33 +04:00
|
|
|
Elf_Ehdr *eh;
|
1996-06-13 22:42:01 +04:00
|
|
|
{
|
1998-10-01 07:11:33 +04:00
|
|
|
size_t shsize = sizeof(Elf_Shdr) * eh->e_shnum;
|
1996-06-13 22:42:01 +04:00
|
|
|
size_t i;
|
|
|
|
static const char signature[] = "\0GCC: (GNU) ";
|
|
|
|
char buf[sizeof(signature) - 1];
|
1998-10-01 07:11:33 +04:00
|
|
|
Elf_Shdr *sh;
|
1996-06-13 22:42:01 +04:00
|
|
|
int error;
|
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
error = ENOEXEC;
|
|
|
|
sh = (Elf_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
|
1996-06-13 22:42:01 +04:00
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_shoff,
|
1996-06-13 22:42:01 +04:00
|
|
|
(caddr_t) sh, shsize)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (i = 0; i < eh->e_shnum; i++) {
|
1998-10-01 07:11:33 +04:00
|
|
|
Elf_Shdr *s = &sh[i];
|
1996-06-13 22:42:01 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify candidates for the comment header;
|
1996-06-13 23:27:01 +04:00
|
|
|
* Header cannot have a load address, or flags and
|
1996-06-13 22:42:01 +04:00
|
|
|
* it must be large enough.
|
|
|
|
*/
|
1999-10-25 17:55:06 +04:00
|
|
|
if (s->sh_type != SHT_PROGBITS ||
|
1996-06-13 22:42:01 +04:00
|
|
|
s->sh_addr != 0 ||
|
|
|
|
s->sh_flags != 0 ||
|
|
|
|
s->sh_size < sizeof(signature) - 1)
|
|
|
|
continue;
|
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, s->sh_offset,
|
1996-06-13 23:27:01 +04:00
|
|
|
(caddr_t) buf, sizeof(signature) - 1)) != 0)
|
1996-06-13 22:42:01 +04:00
|
|
|
goto out;
|
|
|
|
|
1996-06-13 23:27:01 +04:00
|
|
|
/*
|
|
|
|
* error is 0, if the signatures match we are done.
|
|
|
|
*/
|
1998-10-23 07:53:18 +04:00
|
|
|
#ifdef DEBUG_LINUX
|
|
|
|
printf("linux_gcc_sig: sig=%s\n", buf);
|
|
|
|
#endif
|
1998-08-10 00:36:58 +04:00
|
|
|
if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
|
1996-06-13 22:42:01 +04:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(sh, M_TEMP);
|
1998-10-23 07:53:18 +04:00
|
|
|
#ifdef DEBUG_LINUX
|
|
|
|
printf("linux_gcc_sig: returning %d\n", error);
|
|
|
|
#endif
|
1996-06-13 22:42:01 +04:00
|
|
|
return error;
|
|
|
|
}
|
1998-10-01 07:11:33 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static int
|
|
|
|
ELFNAME2(linux,signature)(p, epp, eh)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
Elf_Ehdr *eh;
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
Elf_Phdr *ph;
|
1999-10-25 17:55:06 +04:00
|
|
|
Elf_Nhdr *notep;
|
1998-10-01 07:11:33 +04:00
|
|
|
size_t phsize;
|
|
|
|
int error = ENOEXEC;
|
|
|
|
|
|
|
|
phsize = eh->e_phnum * sizeof(Elf_Phdr);
|
|
|
|
ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
|
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
|
|
|
|
(caddr_t) ph, phsize)) != 0)
|
|
|
|
goto out1;
|
|
|
|
|
|
|
|
for (i = 0; i < eh->e_phnum; i++) {
|
|
|
|
Elf_Phdr *ephp = &ph[i];
|
|
|
|
u_int32_t ostype;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
if (ephp->p_type != PT_INTERP /* XAX PT_NOTE */
|
1998-10-04 00:28:03 +04:00
|
|
|
#if 0
|
|
|
|
|| ephp->p_flags != 0
|
1999-10-25 17:55:06 +04:00
|
|
|
|| ephp->p_filesz < sizeof(Elf_Nhdr))
|
1998-10-04 00:28:03 +04:00
|
|
|
#endif
|
|
|
|
)
|
1998-10-01 07:11:33 +04:00
|
|
|
continue;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
notep = (Elf_Nhdr *)malloc(ephp->p_filesz+1, M_TEMP, M_WAITOK);
|
1998-10-01 07:11:33 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, ephp->p_offset,
|
|
|
|
(caddr_t)notep, ephp->p_filesz)) != 0)
|
|
|
|
goto out3;
|
|
|
|
|
1998-11-06 01:19:25 +03:00
|
|
|
/* Check for "linux" in the intepreter name. */
|
|
|
|
if (ephp->p_filesz < 8 + 5) {
|
|
|
|
error = ENOEXEC;
|
|
|
|
goto out3;
|
|
|
|
}
|
1998-10-23 07:53:18 +04:00
|
|
|
#ifdef DEBUG_LINUX
|
1998-11-06 01:19:25 +03:00
|
|
|
printf("linux_signature: interp=%s\n", notep);
|
1998-10-23 07:53:18 +04:00
|
|
|
#endif
|
1998-11-06 01:19:25 +03:00
|
|
|
if (strncmp(&((char *)notep)[8], "linux", 5) == 0 ||
|
|
|
|
strncmp((char *)notep, "/lib/ld.so.", 11) == 0) {
|
1998-10-01 07:11:33 +04:00
|
|
|
error = 0;
|
|
|
|
goto out3;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto out2;
|
|
|
|
|
|
|
|
/* XXX XAX Should handle NETBSD_TYPE_EMULNAME */
|
1999-10-25 17:55:06 +04:00
|
|
|
if (notep->n_type != ELF_NOTE_TYPE_OSVERSION) {
|
1998-10-01 07:11:33 +04:00
|
|
|
free(notep, M_TEMP);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the name and description sizes. */
|
1999-10-25 17:55:06 +04:00
|
|
|
if (notep->n_namesz != ELF_NOTE_GNU_NAMESZ ||
|
|
|
|
notep->n_descsz != ELF_NOTE_GNU_DESCSZ)
|
1998-10-01 07:11:33 +04:00
|
|
|
goto out2;
|
|
|
|
|
|
|
|
/* Is the name "GNU\0"? */
|
1999-10-25 17:55:06 +04:00
|
|
|
if (memcmp((notep + sizeof(Elf_Nhdr)),
|
1998-10-01 07:11:33 +04:00
|
|
|
ELF_NOTE_GNU_NAME, ELF_NOTE_GNU_NAMESZ))
|
|
|
|
goto out2;
|
|
|
|
|
|
|
|
/* Make sure the OS is Linux */
|
1999-10-25 17:55:06 +04:00
|
|
|
ostype = (u_int32_t)(*((u_int32_t *)notep + sizeof(Elf_Nhdr) +
|
|
|
|
notep->n_namesz)) & ELF_NOTE_GNU_OSMASK;
|
1998-10-01 07:11:33 +04:00
|
|
|
if (ostype != ELF_NOTE_GNU_OSLINUX)
|
|
|
|
goto out2;
|
|
|
|
|
|
|
|
/* All checks succeeded. */
|
|
|
|
error = 0;
|
|
|
|
goto out3;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = ENOEXEC;
|
|
|
|
|
|
|
|
out1:
|
|
|
|
free(ph, M_TEMP);
|
1998-10-23 07:53:18 +04:00
|
|
|
#ifdef DEBUG_LINUX
|
|
|
|
printf("linux_signature: out1=%d\n", error);
|
|
|
|
#endif
|
1998-10-01 07:11:33 +04:00
|
|
|
return error;
|
|
|
|
|
|
|
|
out2:
|
|
|
|
error = ENOEXEC;
|
|
|
|
out3:
|
|
|
|
free(notep, M_TEMP);
|
|
|
|
free(ph, M_TEMP);
|
1998-10-23 07:53:18 +04:00
|
|
|
#ifdef DEBUG_LINUX
|
|
|
|
printf("linux_signature: out2,3=%d\n", error);
|
|
|
|
#endif
|
1998-10-01 07:11:33 +04:00
|
|
|
return error;
|
|
|
|
}
|
1996-06-13 22:42:01 +04:00
|
|
|
|
1995-06-11 18:56:47 +04:00
|
|
|
int
|
1998-10-01 07:11:33 +04:00
|
|
|
ELFNAME2(linux,probe)(p, epp, eh, itp, pos)
|
1995-06-23 01:34:27 +04:00
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
1998-10-01 07:11:33 +04:00
|
|
|
Elf_Ehdr *eh;
|
1995-06-23 01:34:27 +04:00
|
|
|
char *itp;
|
1998-10-01 07:11:33 +04:00
|
|
|
Elf_Addr *pos;
|
1995-06-11 18:56:47 +04:00
|
|
|
{
|
1999-02-09 23:37:19 +03:00
|
|
|
const char *bp;
|
1995-06-23 01:34:27 +04:00
|
|
|
int error;
|
|
|
|
size_t len;
|
1995-06-11 18:56:47 +04:00
|
|
|
|
1998-10-01 07:11:33 +04:00
|
|
|
if ((error = ELFNAME2(linux,signature)(p, epp, eh)) != 0)
|
|
|
|
#ifdef LINUX_GCC_SIGNATURE
|
|
|
|
if ((error = ELFNAME2(linux,gcc_signature)(p, epp, eh)) != 0)
|
|
|
|
return error;
|
|
|
|
#else
|
1996-06-13 22:42:01 +04:00
|
|
|
return error;
|
1998-10-01 07:11:33 +04:00
|
|
|
#endif
|
1996-06-13 22:42:01 +04:00
|
|
|
|
1995-06-23 01:34:27 +04:00
|
|
|
if (itp[0]) {
|
1995-06-25 00:20:04 +04:00
|
|
|
if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
|
1995-06-23 01:34:27 +04:00
|
|
|
return error;
|
|
|
|
if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
|
|
|
|
return error;
|
1999-02-09 23:37:19 +03:00
|
|
|
free((void *)bp, M_TEMP);
|
1995-06-11 18:56:47 +04:00
|
|
|
}
|
1998-10-01 07:11:33 +04:00
|
|
|
epp->ep_emul = &ELFNAMEEND(emul_linux);
|
|
|
|
*pos = ELF_NO_ADDR;
|
1998-10-23 07:53:18 +04:00
|
|
|
#ifdef DEBUG_LINUX
|
|
|
|
printf("linux_probe: returning 0\n");
|
|
|
|
#endif
|
1995-06-23 01:34:27 +04:00
|
|
|
return 0;
|
1995-06-11 18:56:47 +04:00
|
|
|
}
|
1995-06-23 01:34:27 +04:00
|
|
|
|