1999-05-01 03:02:06 +04:00
|
|
|
/* $NetBSD: exec_elf32.c,v 1.44 1999/04/30 23:02:06 cgd Exp $ */
|
1998-09-05 18:30:31 +04:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 1994 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Christos Zoulas.
|
|
|
|
*
|
|
|
|
* 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-09-12 21:20:02 +04:00
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
1998-09-05 18:30:31 +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-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
/*
|
1996-09-27 00:51:03 +04:00
|
|
|
* Copyright (c) 1996 Christopher G. Demetriou
|
1995-06-23 01:29:47 +04:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*/
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
/* If not included by exec_elf64.c, ELFSIZE won't be defined. */
|
|
|
|
#ifndef ELFSIZE
|
|
|
|
#define ELFSIZE 32
|
|
|
|
#endif
|
|
|
|
|
1998-06-26 03:18:23 +04:00
|
|
|
#include "opt_compat_linux.h"
|
1998-06-26 03:22:37 +04:00
|
|
|
#include "opt_compat_ibcs2.h"
|
1998-06-26 04:07:06 +04:00
|
|
|
#include "opt_compat_svr4.h"
|
1999-02-09 23:35:04 +03:00
|
|
|
#include "opt_compat_freebsd.h"
|
1998-06-26 03:18:23 +04:00
|
|
|
|
1995-06-23 01:29:47 +04: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>
|
|
|
|
#include <sys/exec.h>
|
|
|
|
#include <sys/exec_elf.h>
|
1996-09-27 00:51:03 +04:00
|
|
|
#include <sys/fcntl.h>
|
1996-06-14 22:15:55 +04:00
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/signalvar.h>
|
1996-10-07 22:24:48 +04:00
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/vm_param.h>
|
|
|
|
#include <vm/vm_map.h>
|
|
|
|
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/reg.h>
|
|
|
|
|
|
|
|
#ifdef COMPAT_LINUX
|
1998-10-04 00:39:32 +04:00
|
|
|
#include <compat/linux/common/linux_exec.h>
|
1995-06-23 01:29:47 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef COMPAT_SVR4
|
|
|
|
#include <compat/svr4/svr4_exec.h>
|
|
|
|
#endif
|
|
|
|
|
1998-02-09 04:29:10 +03:00
|
|
|
#ifdef COMPAT_IBCS2
|
|
|
|
#include <compat/ibcs2/ibcs2_exec.h>
|
|
|
|
#endif
|
|
|
|
|
1999-02-09 23:35:04 +03:00
|
|
|
#ifdef COMPAT_FREEBSD
|
|
|
|
#include <compat/freebsd/freebsd_exec.h>
|
|
|
|
#endif
|
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
int ELFNAME(check_header) __P((Elf_Ehdr *, int));
|
|
|
|
int ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *,
|
|
|
|
struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *));
|
|
|
|
void ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *,
|
|
|
|
Elf_Phdr *, Elf_Addr *, u_long *, int *));
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1998-12-23 18:08:37 +03:00
|
|
|
static int ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *,
|
|
|
|
Elf_Ehdr *));
|
|
|
|
static int ELFNAME2(netbsd,probe) __P((struct proc *, struct exec_package *,
|
|
|
|
Elf_Ehdr *, char *, Elf_Addr *));
|
|
|
|
|
1996-06-14 22:15:55 +04:00
|
|
|
extern char sigcode[], esigcode[];
|
|
|
|
#ifdef SYSCALL_DEBUG
|
|
|
|
extern char *syscallnames[];
|
1996-10-08 07:40:40 +04:00
|
|
|
#endif
|
1996-06-14 22:15:55 +04:00
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
struct emul ELFNAMEEND(emul_netbsd) = {
|
1996-06-14 22:15:55 +04:00
|
|
|
"netbsd",
|
|
|
|
NULL,
|
|
|
|
sendsig,
|
|
|
|
SYS_syscall,
|
|
|
|
SYS_MAXSYSCALL,
|
|
|
|
sysent,
|
|
|
|
#ifdef SYSCALL_DEBUG
|
|
|
|
syscallnames,
|
1996-10-08 07:40:40 +04:00
|
|
|
#else
|
1996-06-14 22:15:55 +04:00
|
|
|
NULL,
|
1996-10-08 07:40:40 +04:00
|
|
|
#endif
|
1999-05-01 03:02:06 +04:00
|
|
|
howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (char *)),
|
1996-10-08 07:40:40 +04:00
|
|
|
ELFNAME(copyargs),
|
1996-06-14 22:15:55 +04:00
|
|
|
setregs,
|
|
|
|
sigcode,
|
|
|
|
esigcode,
|
1996-10-08 07:40:40 +04:00
|
|
|
};
|
1996-09-27 00:51:03 +04:00
|
|
|
|
|
|
|
int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *,
|
|
|
|
Elf_Ehdr *, char *, Elf_Addr *)) = {
|
1998-12-23 18:08:37 +03:00
|
|
|
ELFNAME2(netbsd,probe),
|
1999-02-09 23:35:04 +03:00
|
|
|
#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32)
|
|
|
|
ELFNAME2(freebsd,probe), /* XXX not 64-bit safe */
|
|
|
|
#endif
|
1998-10-01 20:06:24 +04:00
|
|
|
#if defined(COMPAT_LINUX)
|
|
|
|
ELFNAME2(linux,probe),
|
1996-09-27 00:51:03 +04:00
|
|
|
#endif
|
1997-03-13 05:55:25 +03:00
|
|
|
#if defined(COMPAT_SVR4) && (ELFSIZE == 32)
|
|
|
|
ELFNAME2(svr4,probe), /* XXX not 64-bit safe */
|
|
|
|
#endif
|
1998-02-09 04:29:10 +03:00
|
|
|
#if defined(COMPAT_IBCS2) && (ELFSIZE == 32)
|
|
|
|
ELFNAME2(ibcs2,probe), /* XXX not 64-bit safe */
|
|
|
|
#endif
|
1996-06-14 22:15:55 +04:00
|
|
|
};
|
|
|
|
|
1996-10-11 23:44:02 +04:00
|
|
|
/* round up and down to page boundaries. */
|
|
|
|
#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1))
|
|
|
|
#define ELF_TRUNC(a, b) ((a) & ~((b) - 1))
|
1996-06-14 22:15:55 +04:00
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
/*
|
|
|
|
* Copy arguments onto the stack in the normal way, but add some
|
|
|
|
* extra information in case of dynamic binding.
|
|
|
|
*/
|
|
|
|
void *
|
1996-09-27 00:51:03 +04:00
|
|
|
ELFNAME(copyargs)(pack, arginfo, stack, argp)
|
1995-06-23 01:29:47 +04:00
|
|
|
struct exec_package *pack;
|
|
|
|
struct ps_strings *arginfo;
|
|
|
|
void *stack;
|
|
|
|
void *argp;
|
|
|
|
{
|
|
|
|
size_t len;
|
1996-01-17 02:07:18 +03:00
|
|
|
AuxInfo ai[ELF_AUX_ENTRIES], *a;
|
1995-06-23 01:29:47 +04:00
|
|
|
struct elf_args *ap;
|
|
|
|
|
1996-01-17 02:07:18 +03:00
|
|
|
stack = copyargs(pack, arginfo, stack, argp);
|
|
|
|
if (!stack)
|
1995-06-23 01:29:47 +04:00
|
|
|
return NULL;
|
|
|
|
|
1996-12-18 01:04:20 +03:00
|
|
|
a = ai;
|
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
/*
|
|
|
|
* Push extra arguments on the stack needed by dynamically
|
|
|
|
* linked binaries
|
|
|
|
*/
|
1996-10-08 07:40:40 +04:00
|
|
|
if ((ap = (struct elf_args *)pack->ep_emul_arg)) {
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
a->au_id = AUX_phdr;
|
|
|
|
a->au_v = ap->arg_phaddr;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->au_id = AUX_phent;
|
|
|
|
a->au_v = ap->arg_phentsize;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->au_id = AUX_phnum;
|
|
|
|
a->au_v = ap->arg_phnum;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->au_id = AUX_pagesz;
|
|
|
|
a->au_v = NBPG;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->au_id = AUX_base;
|
|
|
|
a->au_v = ap->arg_interp;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->au_id = AUX_flags;
|
|
|
|
a->au_v = 0;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->au_id = AUX_entry;
|
|
|
|
a->au_v = ap->arg_entry;
|
|
|
|
a++;
|
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
free((char *)ap, M_TEMP);
|
1996-09-27 00:51:03 +04:00
|
|
|
pack->ep_emul_arg = NULL;
|
1995-06-23 01:29:47 +04:00
|
|
|
}
|
1996-12-18 01:04:20 +03:00
|
|
|
|
|
|
|
a->au_id = AUX_null;
|
|
|
|
a->au_v = 0;
|
|
|
|
a++;
|
|
|
|
|
1998-08-01 02:50:48 +04:00
|
|
|
len = (a - ai) * sizeof(AuxInfo);
|
1996-12-18 01:04:20 +03:00
|
|
|
if (copyout(ai, stack, len))
|
|
|
|
return NULL;
|
1999-02-10 20:03:26 +03:00
|
|
|
stack = (caddr_t)stack + len;
|
1996-12-18 01:04:20 +03:00
|
|
|
|
1996-01-17 02:07:18 +03:00
|
|
|
return stack;
|
1995-06-23 01:29:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* elf_check_header():
|
|
|
|
*
|
|
|
|
* Check header for validity; return 0 of ok ENOEXEC if error
|
|
|
|
*/
|
1996-10-08 07:40:40 +04:00
|
|
|
int
|
1996-09-27 00:51:03 +04:00
|
|
|
ELFNAME(check_header)(eh, type)
|
|
|
|
Elf_Ehdr *eh;
|
1995-06-23 01:29:47 +04:00
|
|
|
int type;
|
|
|
|
{
|
1995-09-16 04:28:08 +04:00
|
|
|
|
Abolition of bcopy, ovbcopy, bcmp, and bzero, phase one.
bcopy(x, y, z) -> memcpy(y, x, z)
ovbcopy(x, y, z) -> memmove(y, x, z)
bcmp(x, y, z) -> memcmp(x, y, z)
bzero(x, y) -> memset(x, 0, y)
1998-08-04 08:03:10 +04:00
|
|
|
if (memcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
return ENOEXEC;
|
|
|
|
|
|
|
|
switch (eh->e_machine) {
|
1996-09-27 00:51:03 +04:00
|
|
|
|
1996-09-27 01:50:55 +04:00
|
|
|
ELFDEFNNAME(MACHDEP_ID_CASES)
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (eh->e_type != type)
|
|
|
|
return ENOEXEC;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* elf_load_psection():
|
1996-10-08 07:40:40 +04:00
|
|
|
*
|
1995-06-23 01:29:47 +04:00
|
|
|
* Load a psection at the appropriate address
|
|
|
|
*/
|
1996-10-08 07:40:40 +04:00
|
|
|
void
|
1996-09-27 00:51:03 +04:00
|
|
|
ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot)
|
1995-06-23 01:29:47 +04:00
|
|
|
struct exec_vmcmd_set *vcset;
|
|
|
|
struct vnode *vp;
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Phdr *ph;
|
|
|
|
Elf_Addr *addr;
|
1995-06-23 01:29:47 +04:00
|
|
|
u_long *size;
|
|
|
|
int *prot;
|
|
|
|
{
|
1996-06-14 22:15:55 +04:00
|
|
|
u_long uaddr, msize, psize, rm, rf;
|
1995-06-23 01:29:47 +04:00
|
|
|
long diff, offset;
|
|
|
|
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* If the user specified an address, then we load there.
|
|
|
|
*/
|
1996-09-27 00:51:03 +04:00
|
|
|
if (*addr != ELFDEFNNAME(NO_ADDR)) {
|
1995-06-23 01:29:47 +04:00
|
|
|
if (ph->p_align > 1) {
|
1996-10-11 23:44:02 +04:00
|
|
|
*addr = ELF_ROUND(*addr, ph->p_align);
|
|
|
|
uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align);
|
1995-06-23 01:29:47 +04:00
|
|
|
} else
|
|
|
|
uaddr = ph->p_vaddr;
|
|
|
|
diff = ph->p_vaddr - uaddr;
|
|
|
|
} else {
|
|
|
|
*addr = uaddr = ph->p_vaddr;
|
|
|
|
if (ph->p_align > 1)
|
1996-10-11 23:44:02 +04:00
|
|
|
*addr = ELF_TRUNC(uaddr, ph->p_align);
|
1995-06-23 01:29:47 +04:00
|
|
|
diff = uaddr - *addr;
|
|
|
|
}
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
*prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0;
|
|
|
|
*prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0;
|
|
|
|
*prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
offset = ph->p_offset - diff;
|
|
|
|
*size = ph->p_filesz + diff;
|
|
|
|
msize = ph->p_memsz + diff;
|
1996-06-14 22:15:55 +04:00
|
|
|
psize = round_page(*size);
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
if ((ph->p_flags & Elf_pf_w) != 0) {
|
1996-06-14 22:15:55 +04:00
|
|
|
/*
|
|
|
|
* Because the pagedvn pager can't handle zero fill of the last
|
|
|
|
* data page if it's not page aligned we map the last page
|
|
|
|
* readvn.
|
|
|
|
*/
|
|
|
|
psize = trunc_page(*size);
|
|
|
|
NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
|
|
|
|
offset, *prot);
|
|
|
|
if(psize != *size)
|
|
|
|
NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize,
|
|
|
|
*addr + psize, vp, offset + psize, *prot);
|
1996-09-27 00:51:03 +04:00
|
|
|
} else
|
1996-06-14 22:15:55 +04:00
|
|
|
NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp,
|
|
|
|
offset, *prot);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* Check if we need to extend the size of the segment
|
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
rm = round_page(*addr + msize);
|
|
|
|
rf = round_page(*addr + *size);
|
|
|
|
|
|
|
|
if (rm != rf) {
|
1996-10-08 07:40:40 +04:00
|
|
|
NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
|
|
|
|
0, *prot);
|
1995-06-23 01:29:47 +04:00
|
|
|
*size = msize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* elf_read_from():
|
|
|
|
*
|
|
|
|
* Read from vnode into buffer at offset.
|
|
|
|
*/
|
1996-06-13 22:35:25 +04:00
|
|
|
int
|
1996-09-27 00:51:03 +04:00
|
|
|
ELFNAME(read_from)(p, vp, off, buf, size)
|
1995-06-23 01:29:47 +04:00
|
|
|
struct vnode *vp;
|
|
|
|
u_long off;
|
|
|
|
struct proc *p;
|
|
|
|
caddr_t buf;
|
|
|
|
int size;
|
|
|
|
{
|
|
|
|
int error;
|
1998-07-28 22:37:47 +04:00
|
|
|
size_t resid;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE,
|
|
|
|
0, p->p_ucred, &resid, p)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
return error;
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* See if we got all of it
|
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
if (resid != 0)
|
1996-01-17 02:07:18 +03:00
|
|
|
return ENOEXEC;
|
1995-06-23 01:29:47 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* elf_load_file():
|
|
|
|
*
|
|
|
|
* Load a file (interpreter/library) pointed to by path
|
|
|
|
* [stolen from coff_load_shlib()]. Made slightly generic
|
|
|
|
* so it might be used externally.
|
|
|
|
*/
|
1996-10-08 07:40:40 +04:00
|
|
|
int
|
1996-10-07 22:24:48 +04:00
|
|
|
ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last)
|
1995-06-23 01:29:47 +04:00
|
|
|
struct proc *p;
|
1996-10-07 22:24:48 +04:00
|
|
|
struct exec_package *epp;
|
1995-06-23 01:29:47 +04:00
|
|
|
char *path;
|
|
|
|
struct exec_vmcmd_set *vcset;
|
|
|
|
u_long *entry;
|
|
|
|
struct elf_args *ap;
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Addr *last;
|
1995-06-23 01:29:47 +04:00
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
struct nameidata nd;
|
1996-10-07 22:24:48 +04:00
|
|
|
struct vnode *vp;
|
|
|
|
struct vattr attr;
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Ehdr eh;
|
|
|
|
Elf_Phdr *ph = NULL;
|
1995-06-23 01:29:47 +04:00
|
|
|
u_long phsize;
|
|
|
|
char *bp = NULL;
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Addr addr = *last;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
bp = path;
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* 1. open file
|
|
|
|
* 2. read filehdr
|
|
|
|
* 3. map text, data, and bss out of it using VM_*
|
|
|
|
*/
|
1996-10-01 03:18:43 +04:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
|
|
|
|
if ((error = namei(&nd)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
return error;
|
1996-10-07 22:24:48 +04:00
|
|
|
vp = nd.ni_vp;
|
|
|
|
|
1997-05-08 20:19:43 +04:00
|
|
|
/*
|
|
|
|
* Similarly, if it's not marked as executable, or it's not a regular
|
|
|
|
* file, we don't allow it to be used.
|
|
|
|
*/
|
1996-10-07 22:24:48 +04:00
|
|
|
if (vp->v_type != VREG) {
|
|
|
|
error = EACCES;
|
|
|
|
goto badunlock;
|
|
|
|
}
|
1997-05-08 20:19:43 +04:00
|
|
|
if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
|
|
|
|
goto badunlock;
|
1996-10-07 22:24:48 +04:00
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
/* get attributes */
|
1996-10-07 22:24:48 +04:00
|
|
|
if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
|
|
|
|
goto badunlock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check mount point. Though we're not trying to exec this binary,
|
1996-10-07 22:29:24 +04:00
|
|
|
* we will be executing code from it, so if the mount point
|
|
|
|
* disallows execution or set-id-ness, we punt or kill the set-id.
|
1996-10-07 22:24:48 +04:00
|
|
|
*/
|
|
|
|
if (vp->v_mount->mnt_flag & MNT_NOEXEC) {
|
|
|
|
error = EACCES;
|
|
|
|
goto badunlock;
|
|
|
|
}
|
|
|
|
if (vp->v_mount->mnt_flag & MNT_NOSUID)
|
1997-05-08 14:19:10 +04:00
|
|
|
epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID);
|
1996-10-07 22:24:48 +04:00
|
|
|
|
1996-10-01 03:18:43 +04:00
|
|
|
#ifdef notyet /* XXX cgd 960926 */
|
|
|
|
XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?)
|
|
|
|
#endif
|
1998-03-01 05:20:01 +03:00
|
|
|
VOP_UNLOCK(vp, 0);
|
1996-10-01 03:18:43 +04:00
|
|
|
|
1996-10-07 22:24:48 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh,
|
1996-10-08 07:40:40 +04:00
|
|
|
sizeof(eh))) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
phsize = eh.e_phnum * sizeof(Elf_Phdr);
|
1996-10-08 07:40:40 +04:00
|
|
|
ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1996-10-07 22:24:48 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff,
|
1996-09-27 00:51:03 +04:00
|
|
|
(caddr_t) ph, phsize)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* Load all the necessary sections
|
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
for (i = 0; i < eh.e_phnum; i++) {
|
|
|
|
u_long size = 0;
|
|
|
|
int prot = 0;
|
|
|
|
|
|
|
|
switch (ph[i].p_type) {
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_load:
|
1996-10-07 22:24:48 +04:00
|
|
|
ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
|
1996-10-08 07:40:40 +04:00
|
|
|
&size, &prot);
|
1996-01-17 02:07:18 +03:00
|
|
|
/* If entry is within this section it must be text */
|
|
|
|
if (eh.e_entry >= ph[i].p_vaddr &&
|
|
|
|
eh.e_entry < (ph[i].p_vaddr + size)) {
|
1996-11-23 14:46:34 +03:00
|
|
|
/* XXX */
|
|
|
|
*entry = addr + eh.e_entry;
|
|
|
|
#ifdef mips
|
|
|
|
*entry -= ph[i].p_vaddr;
|
|
|
|
#endif
|
1995-06-23 01:29:47 +04:00
|
|
|
ap->arg_interp = addr;
|
|
|
|
}
|
|
|
|
addr += size;
|
|
|
|
break;
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_dynamic:
|
|
|
|
case Elf_pt_phdr:
|
|
|
|
case Elf_pt_note:
|
1995-06-23 01:29:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
free((char *)ph, M_TEMP);
|
1996-10-01 03:18:43 +04:00
|
|
|
*last = addr;
|
1996-10-07 22:24:48 +04:00
|
|
|
vrele(vp);
|
1996-10-01 03:18:43 +04:00
|
|
|
return 0;
|
|
|
|
|
1996-10-07 22:24:48 +04:00
|
|
|
badunlock:
|
1998-03-01 05:20:01 +03:00
|
|
|
VOP_UNLOCK(vp, 0);
|
1996-10-07 22:24:48 +04:00
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
bad:
|
|
|
|
if (ph != NULL)
|
1996-10-08 07:40:40 +04:00
|
|
|
free((char *)ph, M_TEMP);
|
1996-10-01 03:18:43 +04:00
|
|
|
#ifdef notyet /* XXX cgd 960926 */
|
|
|
|
(maybe) VOP_CLOSE it
|
|
|
|
#endif
|
1996-10-07 22:24:48 +04:00
|
|
|
vrele(vp);
|
1995-06-23 01:29:47 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* exec_elf_makecmds(): Prepare an Elf binary's exec package
|
|
|
|
*
|
|
|
|
* First, set of the various offsets/lengths in the exec package.
|
|
|
|
*
|
|
|
|
* Then, mark the text image busy (so it can be demand paged) or error
|
|
|
|
* out if this is not possible. Finally, set up vmcmds for the
|
|
|
|
* text, data, bss, and stack segments.
|
|
|
|
*/
|
|
|
|
int
|
1996-09-27 00:51:03 +04:00
|
|
|
ELFNAME2(exec,makecmds)(p, epp)
|
1995-06-23 01:29:47 +04:00
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
{
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Ehdr *eh = epp->ep_hdr;
|
|
|
|
Elf_Phdr *ph, *pp;
|
|
|
|
Elf_Addr phdr = 0, pos = 0;
|
1996-01-17 02:07:18 +03:00
|
|
|
int error, i, n, nload;
|
1995-06-23 01:29:47 +04:00
|
|
|
char interp[MAXPATHLEN];
|
1996-09-27 00:51:03 +04:00
|
|
|
u_long phsize;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
if (epp->ep_hdrvalid < sizeof(Elf_Ehdr))
|
1995-06-23 01:29:47 +04:00
|
|
|
return ENOEXEC;
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
if (ELFNAME(check_header)(eh, Elf_et_exec))
|
1995-06-23 01:29:47 +04:00
|
|
|
return ENOEXEC;
|
|
|
|
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* check if vnode is in open for writing, because we want to
|
|
|
|
* demand-page out of it. if it is, don't do it, for various
|
|
|
|
* reasons
|
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
if (epp->ep_vp->v_writecount != 0) {
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (epp->ep_vp->v_flag & VTEXT)
|
|
|
|
panic("exec: a VTEXT vnode has writecount != 0\n");
|
|
|
|
#endif
|
|
|
|
return ETXTBSY;
|
|
|
|
}
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* Allocate space to hold all the program headers, and read them
|
|
|
|
* from the file
|
|
|
|
*/
|
1996-09-27 00:51:03 +04:00
|
|
|
phsize = eh->e_phnum * sizeof(Elf_Phdr);
|
1996-10-08 07:40:40 +04:00
|
|
|
ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
|
1996-06-14 22:15:55 +04:00
|
|
|
(caddr_t) ph, phsize)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
1996-10-14 02:54:52 +04:00
|
|
|
epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR);
|
|
|
|
epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
interp[0] = '\0';
|
|
|
|
|
|
|
|
for (i = 0; i < eh->e_phnum; i++) {
|
|
|
|
pp = &ph[i];
|
1996-09-27 00:51:03 +04:00
|
|
|
if (pp->p_type == Elf_pt_interp) {
|
1995-06-23 01:29:47 +04:00
|
|
|
if (pp->p_filesz >= sizeof(interp))
|
|
|
|
goto bad;
|
1996-10-08 07:40:40 +04:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp,
|
|
|
|
pp->p_offset, (caddr_t) interp,
|
|
|
|
pp->p_filesz)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
/*
|
1996-06-14 22:15:55 +04:00
|
|
|
* Setup things for native emulation.
|
|
|
|
*/
|
1996-09-27 00:51:03 +04:00
|
|
|
epp->ep_emul = &ELFNAMEEND(emul_netbsd);
|
|
|
|
pos = ELFDEFNNAME(NO_ADDR);
|
1996-06-14 22:15:55 +04:00
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
/*
|
|
|
|
* On the same architecture, we may be emulating different systems.
|
|
|
|
* See which one will accept this executable. This currently only
|
1998-10-01 20:06:24 +04:00
|
|
|
* applies to SVR4, and IBCS2 on the i386 and Linux on the i386
|
|
|
|
* and the Alpha.
|
1995-06-23 01:29:47 +04:00
|
|
|
*
|
|
|
|
* Probe functions would normally see if the interpreter (if any)
|
|
|
|
* exists. Emulation packages may possibly replace the interpreter in
|
|
|
|
* interp[] with a changed path (/emul/xxx/<path>), and also
|
|
|
|
* set the ep_emul field in the exec package structure.
|
|
|
|
*/
|
1998-08-01 02:50:48 +04:00
|
|
|
n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]);
|
1996-10-08 07:40:40 +04:00
|
|
|
if (n != 0) {
|
1995-06-23 01:29:47 +04:00
|
|
|
error = ENOEXEC;
|
|
|
|
for (i = 0; i < n && error; i++)
|
1996-10-08 07:40:40 +04:00
|
|
|
error = ELFNAME(probe_funcs)[i](p, epp, eh,
|
|
|
|
interp, &pos);
|
1996-06-14 22:15:55 +04:00
|
|
|
#ifdef notyet
|
|
|
|
/*
|
|
|
|
* We should really use a signature in our native binaries
|
|
|
|
* and have our own probe function for matching binaries,
|
|
|
|
* before trying the emulations. For now, if the emulation
|
|
|
|
* probes failed we default to native.
|
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
1996-06-14 22:15:55 +04:00
|
|
|
#endif
|
1995-06-23 01:29:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* Load all the necessary sections
|
|
|
|
*/
|
1996-01-17 02:07:18 +03:00
|
|
|
for (i = nload = 0; i < eh->e_phnum; i++) {
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Addr addr = ELFDEFNNAME(NO_ADDR);
|
|
|
|
u_long size = 0;
|
1995-06-23 01:29:47 +04:00
|
|
|
int prot = 0;
|
|
|
|
|
|
|
|
pp = &ph[i];
|
|
|
|
|
|
|
|
switch (ph[i].p_type) {
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_load:
|
1996-01-17 02:07:18 +03:00
|
|
|
/*
|
|
|
|
* XXX
|
|
|
|
* Can handle only 2 sections: text and data
|
|
|
|
*/
|
|
|
|
if (nload++ == 2)
|
|
|
|
goto bad;
|
1996-09-27 00:51:03 +04:00
|
|
|
ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp,
|
1996-10-08 07:40:40 +04:00
|
|
|
&ph[i], &addr, &size, &prot);
|
|
|
|
|
1996-01-17 02:07:18 +03:00
|
|
|
/*
|
|
|
|
* Decide whether it's text or data by looking
|
|
|
|
* at the entry point.
|
|
|
|
*/
|
1996-10-14 02:54:52 +04:00
|
|
|
if (eh->e_entry >= addr &&
|
|
|
|
eh->e_entry < (addr + size)) {
|
1996-01-17 02:07:18 +03:00
|
|
|
epp->ep_taddr = addr;
|
|
|
|
epp->ep_tsize = size;
|
1996-10-14 02:54:52 +04:00
|
|
|
if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) {
|
|
|
|
epp->ep_daddr = addr;
|
|
|
|
epp->ep_dsize = size;
|
|
|
|
}
|
1996-01-17 02:07:18 +03:00
|
|
|
} else {
|
|
|
|
epp->ep_daddr = addr;
|
|
|
|
epp->ep_dsize = size;
|
|
|
|
}
|
1995-06-23 01:29:47 +04:00
|
|
|
break;
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_shlib:
|
1998-02-09 04:29:10 +03:00
|
|
|
#ifndef COMPAT_IBCS2 /* SCO has these sections */
|
1995-06-23 01:29:47 +04:00
|
|
|
error = ENOEXEC;
|
|
|
|
goto bad;
|
1998-02-09 04:29:10 +03:00
|
|
|
#endif
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_interp:
|
1995-06-23 01:29:47 +04:00
|
|
|
/* Already did this one */
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_dynamic:
|
|
|
|
case Elf_pt_note:
|
1995-06-23 01:29:47 +04:00
|
|
|
break;
|
|
|
|
|
1996-09-27 00:51:03 +04:00
|
|
|
case Elf_pt_phdr:
|
1996-01-17 02:07:18 +03:00
|
|
|
/* Note address of program headers (in text segment) */
|
|
|
|
phdr = pp->p_vaddr;
|
1996-06-13 22:35:25 +04:00
|
|
|
break;
|
1996-01-17 02:07:18 +03:00
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
default:
|
|
|
|
/*
|
1996-09-27 00:51:03 +04:00
|
|
|
* Not fatal; we don't need to understand everything.
|
1995-06-23 01:29:47 +04:00
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-11-11 23:33:10 +03:00
|
|
|
/* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */
|
|
|
|
#ifndef ELF_INTERP_NON_RELOCATABLE
|
1996-02-09 16:25:54 +03:00
|
|
|
/*
|
|
|
|
* If no position to load the interpreter was set by a probe
|
|
|
|
* function, pick the same address that a non-fixed mmap(0, ..)
|
|
|
|
* would (i.e. something safely out of the way).
|
|
|
|
*/
|
1996-11-23 14:46:34 +03:00
|
|
|
if (pos == ELFDEFNNAME(NO_ADDR))
|
1996-02-09 16:25:54 +03:00
|
|
|
pos = round_page(epp->ep_daddr + MAXDSIZ);
|
1996-11-11 23:33:10 +03:00
|
|
|
#endif /* !ELF_INTERP_NON_RELOCATABLE */
|
1996-02-09 16:25:54 +03:00
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
/*
|
1996-10-08 07:40:40 +04:00
|
|
|
* Check if we found a dynamically linked binary and arrange to load
|
|
|
|
* it's interpreter
|
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
if (interp[0]) {
|
|
|
|
struct elf_args *ap;
|
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
ap = (struct elf_args *)malloc(sizeof(struct elf_args),
|
|
|
|
M_TEMP, M_WAITOK);
|
1996-10-07 22:24:48 +04:00
|
|
|
if ((error = ELFNAME(load_file)(p, epp, interp,
|
|
|
|
&epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) {
|
1996-10-08 07:40:40 +04:00
|
|
|
free((char *)ap, M_TEMP);
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
}
|
|
|
|
pos += phsize;
|
1996-01-17 02:07:18 +03:00
|
|
|
ap->arg_phaddr = phdr;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
ap->arg_phentsize = eh->e_phentsize;
|
|
|
|
ap->arg_phnum = eh->e_phnum;
|
|
|
|
ap->arg_entry = eh->e_entry;
|
|
|
|
|
|
|
|
epp->ep_emul_arg = ap;
|
|
|
|
} else
|
|
|
|
epp->ep_entry = eh->e_entry;
|
|
|
|
|
1996-06-14 22:15:55 +04:00
|
|
|
#ifdef ELF_MAP_PAGE_ZERO
|
|
|
|
/* Dell SVR4 maps page zero, yeuch! */
|
|
|
|
NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0,
|
|
|
|
VM_PROT_READ);
|
|
|
|
#endif
|
1996-10-08 07:40:40 +04:00
|
|
|
free((char *)ph, M_TEMP);
|
1995-06-23 01:29:47 +04:00
|
|
|
epp->ep_vp->v_flag |= VTEXT;
|
1996-09-27 00:51:03 +04:00
|
|
|
return exec_elf_setup_stack(p, epp);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
bad:
|
1996-10-08 07:40:40 +04:00
|
|
|
free((char *)ph, M_TEMP);
|
1995-06-23 01:29:47 +04:00
|
|
|
kill_vmcmds(&epp->ep_vmcmds);
|
|
|
|
return ENOEXEC;
|
|
|
|
}
|
1998-12-23 18:08:37 +03:00
|
|
|
|
|
|
|
static int
|
|
|
|
ELFNAME2(netbsd,signature)(p, epp, eh)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
Elf_Ehdr *eh;
|
|
|
|
{
|
|
|
|
Elf_Phdr *hph, *ph;
|
|
|
|
Elf_Note *np = NULL;
|
|
|
|
size_t phsize;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
phsize = eh->e_phnum * sizeof(Elf_Phdr);
|
|
|
|
hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
|
1999-01-06 14:52:53 +03:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff,
|
|
|
|
(caddr_t)hph, phsize)) != 0)
|
1998-12-23 18:08:37 +03:00
|
|
|
goto out1;
|
|
|
|
|
|
|
|
for (ph = hph; ph < &hph[eh->e_phnum]; ph++) {
|
|
|
|
if (ph->p_type != Elf_pt_note ||
|
1999-01-06 14:52:53 +03:00
|
|
|
ph->p_filesz < sizeof(Elf_Note) + ELF_NOTE_NETBSD_NAMESZ)
|
1998-12-23 18:08:37 +03:00
|
|
|
continue;
|
|
|
|
|
1999-01-06 14:52:53 +03:00
|
|
|
np = (Elf_Note *)malloc(ph->p_filesz, M_TEMP, M_WAITOK);
|
1998-12-23 18:08:37 +03:00
|
|
|
if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset,
|
|
|
|
(caddr_t)np, ph->p_filesz)) != 0)
|
|
|
|
goto out2;
|
|
|
|
|
|
|
|
if (np->type != ELF_NOTE_TYPE_OSVERSION) {
|
|
|
|
free(np, M_TEMP);
|
|
|
|
np = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the name and description sizes. */
|
|
|
|
if (np->namesz != ELF_NOTE_NETBSD_NAMESZ ||
|
|
|
|
np->descsz != ELF_NOTE_NETBSD_DESCSZ)
|
|
|
|
goto out3;
|
|
|
|
|
|
|
|
/* Is the name "NetBSD\0\0"? */
|
|
|
|
if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME,
|
|
|
|
ELF_NOTE_NETBSD_NAMESZ))
|
|
|
|
goto out3;
|
|
|
|
|
|
|
|
/* XXX: We could check for the specific emulation here */
|
|
|
|
/* All checks succeeded. */
|
|
|
|
error = 0;
|
|
|
|
goto out2;
|
|
|
|
}
|
|
|
|
|
|
|
|
out3:
|
|
|
|
error = ENOEXEC;
|
|
|
|
out2:
|
|
|
|
if (np)
|
|
|
|
free(np, M_TEMP);
|
|
|
|
out1:
|
|
|
|
free(hph, M_TEMP);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ELFNAME2(netbsd,probe)(p, epp, eh, itp, pos)
|
|
|
|
struct proc *p;
|
|
|
|
struct exec_package *epp;
|
|
|
|
Elf_Ehdr *eh;
|
|
|
|
char *itp;
|
|
|
|
Elf_Addr *pos;
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
epp->ep_emul = &ELFNAMEEND(emul_netbsd);
|
1999-01-06 14:52:53 +03:00
|
|
|
*pos = ELFDEFNNAME(NO_ADDR);
|
1998-12-23 18:08:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|