2003-10-31 17:00:52 +03:00
|
|
|
/* $NetBSD: exec_elf32.c,v 1.95 2003/10/31 14:00:52 drochner Exp $ */
|
1998-09-05 18:30:31 +04:00
|
|
|
|
|
|
|
/*-
|
2000-12-11 08:28:59 +03:00
|
|
|
* Copyright (c) 1994, 2000 The NetBSD Foundation, Inc.
|
1998-09-05 18:30:31 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2001-11-12 18:25:01 +03:00
|
|
|
#include <sys/cdefs.h>
|
2003-10-31 17:00:52 +03:00
|
|
|
__KERNEL_RCSID(1, "$NetBSD: exec_elf32.c,v 1.95 2003/10/31 14:00:52 drochner Exp $");
|
2001-11-12 18:25:01 +03:00
|
|
|
|
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
|
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
#include <sys/param.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-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
|
|
|
|
2000-11-21 03:37:49 +03:00
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/reg.h>
|
2000-11-15 01:14:53 +03:00
|
|
|
|
2000-11-21 03:37:49 +03:00
|
|
|
extern const struct emul emul_netbsd;
|
1999-02-09 23:35:04 +03:00
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
int ELFNAME(load_file)(struct proc *, struct exec_package *, char *,
|
2000-08-01 08:57:28 +04:00
|
|
|
struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *);
|
|
|
|
void ELFNAME(load_psection)(struct exec_vmcmd_set *, struct vnode *,
|
|
|
|
const Elf_Phdr *, Elf_Addr *, u_long *, int *, int);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
int ELFNAME2(netbsd,signature)(struct proc *, struct exec_package *,
|
2000-08-01 08:57:28 +04:00
|
|
|
Elf_Ehdr *);
|
2003-06-30 02:28:00 +04:00
|
|
|
int ELFNAME2(netbsd,probe)(struct proc *, struct exec_package *,
|
2000-11-21 03:37:49 +03:00
|
|
|
void *, char *, vaddr_t *);
|
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
|
|
|
|
2003-06-25 17:48:06 +04:00
|
|
|
#define MAXPHNUM 50
|
|
|
|
|
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.
|
|
|
|
*/
|
2001-07-30 01:22:58 +04:00
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
ELFNAME(copyargs)(struct proc *p, struct exec_package *pack,
|
2002-08-27 01:07:38 +04:00
|
|
|
struct ps_strings *arginfo, char **stackp, void *argp)
|
1995-06-23 01:29:47 +04:00
|
|
|
{
|
|
|
|
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;
|
2001-07-30 01:22:58 +04:00
|
|
|
int error;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = copyargs(p, pack, arginfo, stackp, argp)) != 0)
|
2001-07-30 01:22:58 +04:00
|
|
|
return error;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
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)) {
|
2002-11-13 18:49:44 +03:00
|
|
|
struct vattr *vap = pack->ep_vap;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_PHDR;
|
|
|
|
a->a_v = ap->arg_phaddr;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_PHENT;
|
|
|
|
a->a_v = ap->arg_phentsize;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_PHNUM;
|
|
|
|
a->a_v = ap->arg_phnum;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_PAGESZ;
|
2000-11-15 01:14:53 +03:00
|
|
|
a->a_v = PAGE_SIZE;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_BASE;
|
|
|
|
a->a_v = ap->arg_interp;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_FLAGS;
|
|
|
|
a->a_v = 0;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_ENTRY;
|
|
|
|
a->a_v = ap->arg_entry;
|
1995-06-23 01:29:47 +04:00
|
|
|
a++;
|
|
|
|
|
2002-08-27 01:07:38 +04:00
|
|
|
a->a_type = AT_EUID;
|
2002-11-13 18:49:44 +03:00
|
|
|
if (vap->va_mode & S_ISUID)
|
|
|
|
a->a_v = vap->va_uid;
|
|
|
|
else
|
|
|
|
a->a_v = p->p_ucred->cr_uid;
|
2002-08-27 01:07:38 +04:00
|
|
|
a++;
|
|
|
|
|
|
|
|
a->a_type = AT_RUID;
|
|
|
|
a->a_v = p->p_cred->p_ruid;
|
|
|
|
a++;
|
|
|
|
|
|
|
|
a->a_type = AT_EGID;
|
2002-11-13 18:49:44 +03:00
|
|
|
if (vap->va_mode & S_ISGID)
|
|
|
|
a->a_v = vap->va_gid;
|
|
|
|
else
|
|
|
|
a->a_v = p->p_ucred->cr_gid;
|
2002-08-27 01:07:38 +04:00
|
|
|
a++;
|
|
|
|
|
|
|
|
a->a_type = AT_RGID;
|
|
|
|
a->a_v = p->p_cred->p_rgid;
|
|
|
|
a++;
|
|
|
|
|
2002-10-06 02:34:02 +04:00
|
|
|
free(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
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
a->a_type = AT_NULL;
|
|
|
|
a->a_v = 0;
|
1996-12-18 01:04:20 +03:00
|
|
|
a++;
|
|
|
|
|
1998-08-01 02:50:48 +04:00
|
|
|
len = (a - ai) * sizeof(AuxInfo);
|
2001-07-30 01:22:58 +04:00
|
|
|
if ((error = copyout(ai, *stackp, len)) != 0)
|
|
|
|
return error;
|
2001-07-30 01:24:23 +04:00
|
|
|
*stackp += len;
|
1996-12-18 01:04:20 +03:00
|
|
|
|
2001-07-30 01:22:58 +04:00
|
|
|
return 0;
|
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
|
2000-08-01 08:57:28 +04:00
|
|
|
ELFNAME(check_header)(Elf_Ehdr *eh, int type)
|
1995-06-23 01:29:47 +04:00
|
|
|
{
|
1995-09-16 04:28:08 +04:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 ||
|
|
|
|
eh->e_ident[EI_CLASS] != ELFCLASS)
|
2000-12-15 09:14:21 +03:00
|
|
|
return (ENOEXEC);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
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:
|
2000-12-15 09:14:21 +03:00
|
|
|
return (ENOEXEC);
|
1995-06-23 01:29:47 +04:00
|
|
|
}
|
|
|
|
|
2002-01-29 01:15:54 +03:00
|
|
|
if (ELF_EHDR_FLAGS_OK(eh) == 0)
|
|
|
|
return (ENOEXEC);
|
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
if (eh->e_type != type)
|
2000-12-15 09:14:21 +03:00
|
|
|
return (ENOEXEC);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
2001-05-07 21:09:13 +04:00
|
|
|
if (eh->e_shnum > 512 ||
|
2000-12-15 09:14:21 +03:00
|
|
|
eh->e_phnum > 128)
|
|
|
|
return (ENOEXEC);
|
|
|
|
|
|
|
|
return (0);
|
1995-06-23 01:29:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-08-01 08:57:28 +04:00
|
|
|
ELFNAME(load_psection)(struct exec_vmcmd_set *vcset, struct vnode *vp,
|
|
|
|
const Elf_Phdr *ph, Elf_Addr *addr, u_long *size, int *prot, int flags)
|
1995-06-23 01:29:47 +04:00
|
|
|
{
|
2003-02-28 22:09:08 +03:00
|
|
|
u_long 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.
|
|
|
|
*/
|
2003-02-28 22:09:08 +03:00
|
|
|
if (*addr == ELFDEFNNAME(NO_ADDR))
|
|
|
|
*addr = ph->p_vaddr;
|
|
|
|
|
|
|
|
if (ph->p_align > 1) {
|
|
|
|
/*
|
|
|
|
* Make sure we are virtually aligned as we are supposed to be.
|
|
|
|
*/
|
|
|
|
diff = ph->p_vaddr - ELF_TRUNC(ph->p_vaddr, ph->p_align);
|
|
|
|
KASSERT(*addr - diff == ELF_TRUNC(*addr, ph->p_align));
|
|
|
|
/*
|
|
|
|
* But make sure to not map any pages before the start of the
|
|
|
|
* psection by limiting the difference to within a page.
|
|
|
|
*/
|
|
|
|
diff &= PAGE_MASK;
|
|
|
|
} else
|
|
|
|
diff = 0;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
*prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0;
|
|
|
|
*prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0;
|
|
|
|
*prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
2003-02-28 22:09:08 +03:00
|
|
|
/*
|
|
|
|
* Adjust everything so it all starts on a page boundary.
|
|
|
|
*/
|
|
|
|
*addr -= diff;
|
1995-06-23 01:29:47 +04:00
|
|
|
offset = ph->p_offset - diff;
|
|
|
|
*size = ph->p_filesz + diff;
|
|
|
|
msize = ph->p_memsz + diff;
|
2001-07-16 00:52:35 +04:00
|
|
|
|
|
|
|
if (ph->p_align >= PAGE_SIZE) {
|
|
|
|
if ((ph->p_flags & PF_W) != 0) {
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
} else {
|
|
|
|
psize = round_page(*size);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
psize = *size;
|
2000-07-13 06:35:25 +04:00
|
|
|
}
|
2001-07-16 00:52:35 +04:00
|
|
|
|
2000-07-13 06:35:25 +04:00
|
|
|
if (psize > 0) {
|
2001-07-16 00:52:35 +04:00
|
|
|
NEW_VMCMD2(vcset, ph->p_align < PAGE_SIZE ?
|
|
|
|
vmcmd_map_readvn : vmcmd_map_pagedvn, psize, *addr, vp,
|
2000-07-13 06:35:25 +04:00
|
|
|
offset, *prot, flags);
|
2003-02-28 22:09:08 +03:00
|
|
|
flags &= VMCMD_RELATIVE;
|
2000-07-13 06:35:25 +04:00
|
|
|
}
|
|
|
|
if (psize < *size) {
|
|
|
|
NEW_VMCMD2(vcset, vmcmd_map_readvn, *size - psize,
|
2003-02-28 22:09:08 +03:00
|
|
|
*addr + psize, vp, offset + psize, *prot, flags);
|
2000-07-13 06:35:25 +04:00
|
|
|
}
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
/*
|
2003-02-28 22:09:08 +03:00
|
|
|
* Check if we need to extend the size of the segment (does
|
|
|
|
* bss extend page the next page boundary)?
|
1996-10-08 07:40:40 +04:00
|
|
|
*/
|
1995-06-23 01:29:47 +04:00
|
|
|
rm = round_page(*addr + msize);
|
|
|
|
rf = round_page(*addr + *size);
|
|
|
|
|
|
|
|
if (rm != rf) {
|
2000-07-13 06:35:25 +04:00
|
|
|
NEW_VMCMD2(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP,
|
2003-02-27 04:58:56 +03:00
|
|
|
0, *prot, flags & VMCMD_RELATIVE);
|
1995-06-23 01:29:47 +04:00
|
|
|
*size = msize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2003-06-30 02:28:00 +04:00
|
|
|
ELFNAME(load_file)(struct proc *p, struct exec_package *epp, char *path,
|
2003-01-30 23:03:46 +03:00
|
|
|
struct exec_vmcmd_set *vcset, u_long *entryoff, struct elf_args *ap,
|
2000-08-01 08:57:28 +04:00
|
|
|
Elf_Addr *last)
|
1995-06-23 01:29:47 +04:00
|
|
|
{
|
2003-02-27 04:58:56 +03:00
|
|
|
int error, i;
|
1995-06-23 01:29:47 +04:00
|
|
|
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;
|
2003-02-27 04:58:56 +03:00
|
|
|
const Elf_Phdr *ph0;
|
|
|
|
const Elf_Phdr *base_ph;
|
|
|
|
const Elf_Phdr *last_ph;
|
1995-06-23 01:29:47 +04:00
|
|
|
u_long phsize;
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Addr addr = *last;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
/*
|
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_*
|
|
|
|
*/
|
2003-06-30 02:28:00 +04:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p);
|
1996-10-01 03:18:43 +04:00
|
|
|
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;
|
|
|
|
}
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0)
|
1997-05-08 20:19:43 +04:00
|
|
|
goto badunlock;
|
1996-10-07 22:24:48 +04:00
|
|
|
|
1996-10-08 07:40:40 +04:00
|
|
|
/* get attributes */
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0)
|
1996-10-07 22:24:48 +04:00
|
|
|
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
|
2002-10-06 02:34:02 +04:00
|
|
|
|
|
|
|
error = vn_marktext(vp);
|
|
|
|
if (error)
|
|
|
|
goto badunlock;
|
|
|
|
|
1998-03-01 05:20:01 +03:00
|
|
|
VOP_UNLOCK(vp, 0);
|
1996-10-01 03:18:43 +04:00
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = exec_read_from(p, vp, 0, &eh, sizeof(eh))) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
2003-06-25 17:48:06 +04:00
|
|
|
if (eh.e_phnum > MAXPHNUM)
|
|
|
|
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
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = exec_read_from(p, vp, eh.e_phoff, ph, phsize)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/*
|
2003-02-27 04:58:56 +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-10-08 07:40:40 +04:00
|
|
|
*/
|
2003-02-27 04:58:56 +03:00
|
|
|
if (*last == ELFDEFNNAME(NO_ADDR)) {
|
|
|
|
u_long limit = 0;
|
|
|
|
/*
|
|
|
|
* Find the start and ending addresses of the psections to
|
|
|
|
* be loaded. This will give us the size.
|
|
|
|
*/
|
|
|
|
for (i = 0, ph0 = ph, base_ph = NULL; i < eh.e_phnum;
|
|
|
|
i++, ph0++) {
|
|
|
|
if (ph0->p_type == PT_LOAD) {
|
|
|
|
u_long psize = ph0->p_vaddr + ph0->p_memsz;
|
|
|
|
if (base_ph == NULL)
|
|
|
|
base_ph = ph0;
|
|
|
|
if (psize > limit)
|
|
|
|
limit = psize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now compute the size and load address.
|
|
|
|
*/
|
2003-02-28 03:11:14 +03:00
|
|
|
addr = VM_DEFAULT_ADDRESS(epp->ep_daddr,
|
2003-02-27 04:58:56 +03:00
|
|
|
round_page(limit) - trunc_page(base_ph->p_vaddr));
|
|
|
|
} else
|
2003-10-31 17:00:52 +03:00
|
|
|
addr = *last; /* may be ELF_LINK_ADDR */
|
2003-02-27 04:58:56 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Load all the necessary sections
|
|
|
|
*/
|
|
|
|
for (i = 0, ph0 = ph, base_ph = NULL, last_ph = NULL;
|
|
|
|
i < eh.e_phnum; i++, ph0++) {
|
|
|
|
switch (ph0->p_type) {
|
|
|
|
case PT_LOAD: {
|
|
|
|
u_long size;
|
|
|
|
int prot = 0;
|
|
|
|
int flags;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
2000-07-13 06:35:25 +04:00
|
|
|
if (base_ph == NULL) {
|
2003-02-27 00:18:22 +03:00
|
|
|
/*
|
|
|
|
* First encountered psection is always the
|
2003-02-28 22:44:42 +03:00
|
|
|
* base psection. Make sure it's aligned
|
2003-03-01 08:55:51 +03:00
|
|
|
* properly (align down for topdown and align
|
|
|
|
* upwards for not topdown).
|
2003-02-27 00:18:22 +03:00
|
|
|
*/
|
2003-02-27 04:58:56 +03:00
|
|
|
base_ph = ph0;
|
2000-07-13 06:35:25 +04:00
|
|
|
flags = VMCMD_BASE;
|
2003-10-31 17:00:52 +03:00
|
|
|
if (addr == ELF_LINK_ADDR)
|
|
|
|
addr = ph0->p_vaddr;
|
2003-03-01 08:55:51 +03:00
|
|
|
if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
|
|
|
|
addr = ELF_TRUNC(addr, ph0->p_align);
|
|
|
|
else
|
|
|
|
addr = ELF_ROUND(addr, ph0->p_align);
|
2000-07-13 06:35:25 +04:00
|
|
|
} else {
|
2003-02-27 04:58:56 +03:00
|
|
|
u_long limit = round_page(last_ph->p_vaddr
|
|
|
|
+ last_ph->p_memsz);
|
2003-02-28 22:09:08 +03:00
|
|
|
u_long base = trunc_page(ph0->p_vaddr);
|
2003-02-27 04:58:56 +03:00
|
|
|
|
2003-02-27 00:18:22 +03:00
|
|
|
/*
|
2003-02-27 04:58:56 +03:00
|
|
|
* If there is a gap in between the psections,
|
|
|
|
* map it as inaccessible so nothing else
|
|
|
|
* mmap'ed will be placed there.
|
2003-02-27 00:18:22 +03:00
|
|
|
*/
|
2003-02-28 02:48:29 +03:00
|
|
|
if (limit != base) {
|
2003-02-27 04:58:56 +03:00
|
|
|
NEW_VMCMD2(vcset, vmcmd_map_zero,
|
2003-02-28 02:48:29 +03:00
|
|
|
base - limit,
|
2003-02-28 22:09:08 +03:00
|
|
|
limit - base_ph->p_vaddr, NULLVP,
|
|
|
|
0, VM_PROT_NONE, VMCMD_RELATIVE);
|
2003-02-27 04:58:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addr = ph0->p_vaddr - base_ph->p_vaddr;
|
2000-07-13 06:35:25 +04:00
|
|
|
flags = VMCMD_RELATIVE;
|
|
|
|
}
|
2003-02-27 04:58:56 +03:00
|
|
|
last_ph = ph0;
|
1996-10-07 22:24:48 +04:00
|
|
|
ELFNAME(load_psection)(vcset, vp, &ph[i], &addr,
|
2003-02-27 04:58:56 +03:00
|
|
|
&size, &prot, flags);
|
2003-02-27 00:18:22 +03:00
|
|
|
/*
|
|
|
|
* If entry is within this psection then this
|
|
|
|
* must contain the .text section. *entryoff is
|
2003-02-27 04:58:56 +03:00
|
|
|
* relative to the base psection.
|
2003-02-27 00:18:22 +03:00
|
|
|
*/
|
2003-02-27 04:58:56 +03:00
|
|
|
if (eh.e_entry >= ph0->p_vaddr &&
|
|
|
|
eh.e_entry < (ph0->p_vaddr + size)) {
|
|
|
|
*entryoff = eh.e_entry - base_ph->p_vaddr;
|
1995-06-23 01:29:47 +04:00
|
|
|
}
|
2003-02-28 02:10:52 +03:00
|
|
|
addr += size;
|
1995-06-23 01:29:47 +04:00
|
|
|
break;
|
2003-02-27 04:58:56 +03:00
|
|
|
}
|
1995-06-23 01:29:47 +04:00
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_DYNAMIC:
|
|
|
|
case PT_PHDR:
|
|
|
|
case PT_NOTE:
|
1995-06-23 01:29:47 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-06 02:34:02 +04:00
|
|
|
free(ph, M_TEMP);
|
2003-02-27 00:18:22 +03:00
|
|
|
/*
|
|
|
|
* This value is ignored if TOPDOWN.
|
|
|
|
*/
|
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)
|
2002-10-06 02:34:02 +04:00
|
|
|
free(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
|
2003-06-30 02:28:00 +04:00
|
|
|
ELFNAME2(exec,makecmds)(struct proc *p, struct exec_package *epp)
|
1995-06-23 01:29:47 +04:00
|
|
|
{
|
1996-09-27 00:51:03 +04:00
|
|
|
Elf_Ehdr *eh = epp->ep_hdr;
|
|
|
|
Elf_Phdr *ph, *pp;
|
|
|
|
Elf_Addr phdr = 0, pos = 0;
|
2000-11-21 03:37:49 +03:00
|
|
|
int error, i, nload;
|
|
|
|
char *interp = NULL;
|
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;
|
|
|
|
|
1999-06-30 03:39:06 +04:00
|
|
|
/*
|
|
|
|
* XXX allow for executing shared objects. It seems silly
|
|
|
|
* but other ELF-based systems allow it as well.
|
|
|
|
*/
|
1999-10-25 17:55:06 +04:00
|
|
|
if (ELFNAME(check_header)(eh, ET_EXEC) != 0 &&
|
|
|
|
ELFNAME(check_header)(eh, ET_DYN) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
return ENOEXEC;
|
|
|
|
|
2003-06-25 17:48:06 +04:00
|
|
|
if (eh->e_phnum > MAXPHNUM)
|
|
|
|
return ENOEXEC;
|
|
|
|
|
2002-10-06 02:34:02 +04:00
|
|
|
error = vn_marktext(epp->ep_vp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
1995-06-23 01:29:47 +04:00
|
|
|
/*
|
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
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize)) !=
|
2001-07-14 06:08:29 +04:00
|
|
|
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
|
|
|
|
|
|
|
for (i = 0; i < eh->e_phnum; i++) {
|
|
|
|
pp = &ph[i];
|
1999-10-25 17:55:06 +04:00
|
|
|
if (pp->p_type == PT_INTERP) {
|
2000-11-21 03:37:49 +03:00
|
|
|
if (pp->p_filesz >= MAXPATHLEN)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
2003-10-31 17:00:52 +03:00
|
|
|
MALLOC(interp, char *, MAXPATHLEN, M_TEMP, M_WAITOK);
|
|
|
|
interp[0] = '\0';
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = exec_read_from(p, epp->ep_vp,
|
2001-07-14 06:08:29 +04:00
|
|
|
pp->p_offset, interp, pp->p_filesz)) != 0)
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-11-21 03:37:49 +03:00
|
|
|
* interp[] with a changed path (/emul/xxx/<path>).
|
1995-06-23 01:29:47 +04:00
|
|
|
*/
|
2003-10-31 17:00:52 +03:00
|
|
|
pos = ELFDEFNNAME(NO_ADDR);
|
|
|
|
if (epp->ep_esch->u.elf_probe_func) {
|
|
|
|
vaddr_t startp = (vaddr_t)pos;
|
2001-02-04 22:12:09 +03:00
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
error = (*epp->ep_esch->u.elf_probe_func)(p, epp, eh, interp,
|
2001-02-04 22:12:09 +03:00
|
|
|
&startp);
|
1995-06-23 01:29:47 +04:00
|
|
|
if (error)
|
|
|
|
goto bad;
|
2003-10-31 17:00:52 +03:00
|
|
|
pos = (Elf_Addr)startp;
|
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) {
|
1999-10-25 17:55:06 +04:00
|
|
|
case 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,
|
2003-01-30 23:03:46 +03:00
|
|
|
&ph[i], &addr, &size, &prot, VMCMD_FIXED);
|
1996-10-08 07:40:40 +04:00
|
|
|
|
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;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_SHLIB:
|
2000-12-11 08:28:59 +03:00
|
|
|
/* SCO has these sections. */
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_INTERP:
|
2000-12-11 08:28:59 +03:00
|
|
|
/* Already did this one. */
|
1999-10-25 17:55:06 +04:00
|
|
|
case PT_DYNAMIC:
|
|
|
|
case PT_NOTE:
|
1995-06-23 01:29:47 +04:00
|
|
|
break;
|
|
|
|
|
1999-10-25 17:55:06 +04:00
|
|
|
case 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-10-08 07:40:40 +04:00
|
|
|
* Check if we found a dynamically linked binary and arrange to load
|
2003-01-30 23:03:46 +03:00
|
|
|
* its interpreter
|
1996-10-08 07:40:40 +04:00
|
|
|
*/
|
2003-10-31 17:00:52 +03:00
|
|
|
if (interp) {
|
1995-06-23 01:29:47 +04:00
|
|
|
struct elf_args *ap;
|
2003-01-30 23:03:46 +03:00
|
|
|
int i = epp->ep_vmcmds.evs_used;
|
|
|
|
u_long interp_offset;
|
1995-06-23 01:29:47 +04:00
|
|
|
|
2000-11-21 03:37:49 +03:00
|
|
|
MALLOC(ap, struct elf_args *, sizeof(struct elf_args),
|
1996-10-08 07:40:40 +04:00
|
|
|
M_TEMP, M_WAITOK);
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = ELFNAME(load_file)(p, epp, interp,
|
2003-01-30 23:03:46 +03:00
|
|
|
&epp->ep_vmcmds, &interp_offset, ap, &pos)) != 0) {
|
2002-10-06 02:34:02 +04:00
|
|
|
FREE(ap, M_TEMP);
|
1995-06-23 01:29:47 +04:00
|
|
|
goto bad;
|
|
|
|
}
|
2003-01-30 23:03:46 +03:00
|
|
|
ap->arg_interp = epp->ep_vmcmds.evs_cmds[i].ev_addr;
|
|
|
|
epp->ep_entry = ap->arg_interp + interp_offset;
|
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;
|
2003-10-31 17:00:52 +03:00
|
|
|
|
|
|
|
FREE(interp, M_TEMP);
|
1995-06-23 01:29:47 +04:00
|
|
|
} 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! */
|
2000-11-15 01:14:53 +03:00
|
|
|
NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
|
|
|
|
epp->ep_vp, 0, VM_PROT_READ);
|
1996-06-14 22:15:55 +04:00
|
|
|
#endif
|
2002-10-06 02:34:02 +04:00
|
|
|
free(ph, M_TEMP);
|
2003-08-08 22:53:13 +04:00
|
|
|
return (*epp->ep_esch->es_setup_stack)(p, epp);
|
1995-06-23 01:29:47 +04:00
|
|
|
|
|
|
|
bad:
|
2000-11-21 03:37:49 +03:00
|
|
|
if (interp)
|
|
|
|
FREE(interp, M_TEMP);
|
2002-10-06 02:34:02 +04:00
|
|
|
free(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
|
|
|
|
2000-11-28 16:10:41 +03:00
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
ELFNAME2(netbsd,signature)(struct proc *p, struct exec_package *epp,
|
2000-08-01 08:57:28 +04:00
|
|
|
Elf_Ehdr *eh)
|
1998-12-23 18:08:37 +03:00
|
|
|
{
|
2000-12-15 09:14:21 +03:00
|
|
|
size_t i;
|
|
|
|
Elf_Phdr *ph;
|
1998-12-23 18:08:37 +03:00
|
|
|
size_t phsize;
|
|
|
|
int error;
|
|
|
|
|
2003-06-25 17:48:06 +04:00
|
|
|
if (eh->e_phnum > MAXPHNUM)
|
|
|
|
return ENOEXEC;
|
|
|
|
|
1998-12-23 18:08:37 +03:00
|
|
|
phsize = eh->e_phnum * sizeof(Elf_Phdr);
|
2000-12-15 09:14:21 +03:00
|
|
|
ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK);
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, epp->ep_vp, eh->e_phoff, ph, phsize);
|
2000-12-15 09:14:21 +03:00
|
|
|
if (error)
|
|
|
|
goto out;
|
1998-12-23 18:08:37 +03:00
|
|
|
|
2000-12-15 09:14:21 +03:00
|
|
|
for (i = 0; i < eh->e_phnum; i++) {
|
|
|
|
Elf_Phdr *ephp = &ph[i];
|
|
|
|
Elf_Nhdr *np;
|
1998-12-23 18:08:37 +03:00
|
|
|
|
2000-12-15 09:14:21 +03:00
|
|
|
if (ephp->p_type != PT_NOTE ||
|
|
|
|
ephp->p_filesz > 1024 ||
|
|
|
|
ephp->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ)
|
1998-12-23 18:08:37 +03:00
|
|
|
continue;
|
|
|
|
|
2000-12-15 09:14:21 +03:00
|
|
|
np = (Elf_Nhdr *)malloc(ephp->p_filesz, M_TEMP, M_WAITOK);
|
2003-06-30 02:28:00 +04:00
|
|
|
error = exec_read_from(p, epp->ep_vp, ephp->p_offset, np,
|
2001-07-14 06:08:29 +04:00
|
|
|
ephp->p_filesz);
|
2000-12-15 09:14:21 +03:00
|
|
|
if (error)
|
|
|
|
goto next;
|
1998-12-23 18:08:37 +03:00
|
|
|
|
2000-12-15 09:14:21 +03:00
|
|
|
if (np->n_type != ELF_NOTE_TYPE_NETBSD_TAG ||
|
|
|
|
np->n_namesz != ELF_NOTE_NETBSD_NAMESZ ||
|
|
|
|
np->n_descsz != ELF_NOTE_NETBSD_DESCSZ ||
|
|
|
|
memcmp((caddr_t)(np + 1), ELF_NOTE_NETBSD_NAME,
|
1998-12-23 18:08:37 +03:00
|
|
|
ELF_NOTE_NETBSD_NAMESZ))
|
2000-12-15 09:14:21 +03:00
|
|
|
goto next;
|
1998-12-23 18:08:37 +03:00
|
|
|
|
|
|
|
error = 0;
|
2000-12-15 09:14:21 +03:00
|
|
|
free(np, M_TEMP);
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
next:
|
|
|
|
free(np, M_TEMP);
|
|
|
|
continue;
|
1998-12-23 18:08:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
error = ENOEXEC;
|
2000-12-15 09:14:21 +03:00
|
|
|
out:
|
|
|
|
free(ph, M_TEMP);
|
|
|
|
return (error);
|
1998-12-23 18:08:37 +03:00
|
|
|
}
|
|
|
|
|
2000-11-21 03:37:49 +03:00
|
|
|
int
|
2003-06-30 02:28:00 +04:00
|
|
|
ELFNAME2(netbsd,probe)(struct proc *p, struct exec_package *epp,
|
2000-11-21 03:37:49 +03:00
|
|
|
void *eh, char *itp, vaddr_t *pos)
|
1998-12-23 18:08:37 +03:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2003-06-30 02:28:00 +04:00
|
|
|
if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0)
|
1998-12-23 18:08:37 +03:00
|
|
|
return error;
|
2003-10-31 17:00:52 +03:00
|
|
|
#ifdef ELF_INTERP_NON_RELOCATABLE
|
|
|
|
*pos = ELF_LINK_ADDR;
|
|
|
|
#endif
|
1998-12-23 18:08:37 +03:00
|
|
|
return 0;
|
|
|
|
}
|