summaryrefslogtreecommitdiff
path: root/sys/compat/linux/common/linux_exec.c
diff options
context:
space:
mode:
authorerh <erh@NetBSD.org>1998-10-01 03:11:33 +0000
committererh <erh@NetBSD.org>1998-10-01 03:11:33 +0000
commitf7ac1bd375b093e74eb4cd9a341b1a3894b526c8 (patch)
treed370992ffc5ea845de51259cff6e844324e84058 /sys/compat/linux/common/linux_exec.c
parentd15a64c38bb8b999f9b3b1d7082b3df72a089023 (diff)
Split compat/linux/linux_exec.c into common, elf and a.out parts.
Diffstat (limited to 'sys/compat/linux/common/linux_exec.c')
-rw-r--r--sys/compat/linux/common/linux_exec.c500
1 files changed, 7 insertions, 493 deletions
diff --git a/sys/compat/linux/common/linux_exec.c b/sys/compat/linux/common/linux_exec.c
index 234328e019b..a57dd170729 100644
--- a/sys/compat/linux/common/linux_exec.c
+++ b/sys/compat/linux/common/linux_exec.c
@@ -1,10 +1,13 @@
-/* $NetBSD: linux_exec.c,v 1.30 1998/09/11 12:50:08 mycroft Exp $ */
+/* $NetBSD: linux_exec.c,v 1.31 1998/10/01 03:11:33 erh Exp $ */
/*-
* Copyright (c) 1994 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
+ * by Eric Haszlakiewicz and Thor Lancelot Simon.
+ *
+ * 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
@@ -65,8 +68,6 @@
* based on exec_aout.c, sunos_exec.c and svr4_exec.c
*/
-#define ELFSIZE 32 /* XXX should die */
-
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
@@ -89,503 +90,16 @@
#include <machine/reg.h>
#include <compat/linux/linux_types.h>
-#include <compat/linux/linux_syscall.h>
#include <compat/linux/linux_signal.h>
+#include <compat/linux/linux_siginfo.h>
+#include <compat/linux/linux_syscall.h>
#include <compat/linux/linux_syscallargs.h>
#include <compat/linux/linux_util.h>
#include <compat/linux/linux_exec.h>
-#include <machine/linux_machdep.h>
-
-static void *linux_aout_copyargs __P((struct exec_package *,
- struct ps_strings *, void *, void *));
-static int linux_elf32_signature __P((struct proc *p, struct exec_package *,
- Elf32_Ehdr *));
-
-#define LINUX_AOUT_AUX_ARGSIZ 2
-#define LINUX_ELF_AUX_ARGSIZ (sizeof(AuxInfo) * 8 / sizeof(char *))
+#include <compat/linux/linux_machdep.h>
const char linux_emul_path[] = "/emul/linux";
-extern int linux_error[];
-extern char linux_sigcode[], linux_esigcode[];
-extern struct sysent linux_sysent[];
-extern char *linux_syscallnames[];
-
-int exec_linux_aout_prep_zmagic __P((struct proc *, struct exec_package *));
-int exec_linux_aout_prep_nmagic __P((struct proc *, struct exec_package *));
-int exec_linux_aout_prep_omagic __P((struct proc *, struct exec_package *));
-int exec_linux_aout_prep_qmagic __P((struct proc *, struct exec_package *));
-
-struct emul emul_linux_aout = {
- "linux",
- linux_error,
- linux_sendsig,
- LINUX_SYS_syscall,
- LINUX_SYS_MAXSYSCALL,
- linux_sysent,
- linux_syscallnames,
- LINUX_AOUT_AUX_ARGSIZ,
- linux_aout_copyargs,
- linux_setregs,
- linux_sigcode,
- linux_esigcode,
-};
-
-struct emul emul_linux_elf = {
- "linux",
- linux_error,
- linux_sendsig,
- LINUX_SYS_syscall,
- LINUX_SYS_MAXSYSCALL,
- linux_sysent,
- linux_syscallnames,
- LINUX_ELF_AUX_ARGSIZ,
- elf32_copyargs,
- linux_setregs,
- linux_sigcode,
- linux_esigcode,
-};
-
-
-static void *
-linux_aout_copyargs(pack, arginfo, stack, argp)
- struct exec_package *pack;
- struct ps_strings *arginfo;
- void *stack;
- void *argp;
-{
- char **cpp = stack;
- char **stk = stack;
- char *dp, *sp;
- size_t len;
- void *nullp = NULL;
- int argc = arginfo->ps_nargvstr;
- int envc = arginfo->ps_nenvstr;
-
- if (copyout(&argc, cpp++, sizeof(argc)))
- return NULL;
-
- /* leave room for envp and argv */
- cpp += 2;
- if (copyout(&cpp, &stk[1], sizeof (cpp)))
- return NULL;
-
- dp = (char *) (cpp + argc + envc + 2);
- sp = argp;
-
- /* XXX don't copy them out, remap them! */
- arginfo->ps_argvstr = cpp; /* remember location of argv for later */
-
- for (; --argc >= 0; sp += len, dp += len)
- if (copyout(&dp, cpp++, sizeof(dp)) ||
- copyoutstr(sp, dp, ARG_MAX, &len))
- return NULL;
-
- if (copyout(&nullp, cpp++, sizeof(nullp)))
- return NULL;
-
- if (copyout(&cpp, &stk[2], sizeof (cpp)))
- return NULL;
-
- arginfo->ps_envstr = cpp; /* remember location of envp for later */
-
- for (; --envc >= 0; sp += len, dp += len)
- if (copyout(&dp, cpp++, sizeof(dp)) ||
- copyoutstr(sp, dp, ARG_MAX, &len))
- return NULL;
-
- if (copyout(&nullp, cpp++, sizeof(nullp)))
- return NULL;
-
- return cpp;
-}
-
-int
-exec_linux_aout_makecmds(p, epp)
- struct proc *p;
- struct exec_package *epp;
-{
- struct exec *linux_ep = epp->ep_hdr;
- int machtype, magic;
- int error = ENOEXEC;
-
- magic = LINUX_N_MAGIC(linux_ep);
- machtype = LINUX_N_MACHTYPE(linux_ep);
-
-
- if (machtype != LINUX_MID_MACHINE)
- return (ENOEXEC);
-
- switch (magic) {
- case QMAGIC:
- error = exec_linux_aout_prep_qmagic(p, epp);
- break;
- case ZMAGIC:
- error = exec_linux_aout_prep_zmagic(p, epp);
- break;
- case NMAGIC:
- error = exec_linux_aout_prep_nmagic(p, epp);
- break;
- case OMAGIC:
- error = exec_linux_aout_prep_omagic(p, epp);
- break;
- }
- if (error == 0)
- epp->ep_emul = &emul_linux_aout;
- return error;
-}
-
-/*
- * Since text starts at 0x400 in Linux ZMAGIC executables, and 0x400
- * is very likely not page aligned on most architectures, it is treated
- * as an NMAGIC here. XXX
- */
-
-int
-exec_linux_aout_prep_zmagic(p, epp)
- struct proc *p;
- struct exec_package *epp;
-{
- struct exec *execp = epp->ep_hdr;
-
- epp->ep_taddr = LINUX_N_TXTADDR(*execp, ZMAGIC);
- epp->ep_tsize = execp->a_text;
- epp->ep_daddr = LINUX_N_DATADDR(*execp, ZMAGIC);
- epp->ep_dsize = execp->a_data + execp->a_bss;
- epp->ep_entry = execp->a_entry;
-
- /* set up command for text segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
- epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, ZMAGIC),
- VM_PROT_READ|VM_PROT_EXECUTE);
-
- /* set up command for data segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
- epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, ZMAGIC),
- VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- /* set up command for bss segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
- epp->ep_daddr + execp->a_data, NULLVP, 0,
- VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- return exec_aout_setup_stack(p, epp);
-}
-
-/*
- * exec_aout_prep_nmagic(): Prepare Linux NMAGIC package.
- * Not different from the normal stuff.
- */
-
-int
-exec_linux_aout_prep_nmagic(p, epp)
- struct proc *p;
- struct exec_package *epp;
-{
- struct exec *execp = epp->ep_hdr;
- long bsize, baddr;
-
- epp->ep_taddr = LINUX_N_TXTADDR(*execp, NMAGIC);
- epp->ep_tsize = execp->a_text;
- epp->ep_daddr = LINUX_N_DATADDR(*execp, NMAGIC);
- epp->ep_dsize = execp->a_data + execp->a_bss;
- epp->ep_entry = execp->a_entry;
-
- /* set up command for text segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
- epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, NMAGIC),
- VM_PROT_READ|VM_PROT_EXECUTE);
-
- /* set up command for data segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
- epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, NMAGIC),
- VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- /* set up command for bss segment */
- baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
- bsize = epp->ep_daddr + epp->ep_dsize - baddr;
- if (bsize > 0)
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
- NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- return exec_aout_setup_stack(p, epp);
-}
-
-/*
- * exec_aout_prep_omagic(): Prepare Linux OMAGIC package.
- * Business as usual.
- */
-
-int
-exec_linux_aout_prep_omagic(p, epp)
- struct proc *p;
- struct exec_package *epp;
-{
- struct exec *execp = epp->ep_hdr;
- long dsize, bsize, baddr;
-
- epp->ep_taddr = LINUX_N_TXTADDR(*execp, OMAGIC);
- epp->ep_tsize = execp->a_text;
- epp->ep_daddr = LINUX_N_DATADDR(*execp, OMAGIC);
- epp->ep_dsize = execp->a_data + execp->a_bss;
- epp->ep_entry = execp->a_entry;
-
- /* set up command for text and data segments */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
- execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
- LINUX_N_TXTOFF(*execp, OMAGIC), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- /* set up command for bss segment */
- baddr = roundup(epp->ep_daddr + execp->a_data, NBPG);
- bsize = epp->ep_daddr + epp->ep_dsize - baddr;
- if (bsize > 0)
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
- NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- /*
- * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
- * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
- * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
- * respectively to page boundaries.
- * Compensate `ep_dsize' for the amount of data covered by the last
- * text page.
- */
- dsize = epp->ep_dsize + execp->a_text - roundup(execp->a_text, NBPG);
- epp->ep_dsize = (dsize > 0) ? dsize : 0;
- return exec_aout_setup_stack(p, epp);
-}
-
-int
-exec_linux_aout_prep_qmagic(p, epp)
- struct proc *p;
- struct exec_package *epp;
-{
- struct exec *execp = epp->ep_hdr;
-
- epp->ep_taddr = LINUX_N_TXTADDR(*execp, QMAGIC);
- epp->ep_tsize = execp->a_text;
- epp->ep_daddr = LINUX_N_DATADDR(*execp, QMAGIC);
- epp->ep_dsize = execp->a_data + execp->a_bss;
- epp->ep_entry = execp->a_entry;
-
- /*
- * 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
- */
- if ((execp->a_text != 0 || execp->a_data != 0) &&
- 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;
- }
- epp->ep_vp->v_flag |= VTEXT;
-
- /* set up command for text segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
- epp->ep_taddr, epp->ep_vp, LINUX_N_TXTOFF(*execp, QMAGIC),
- VM_PROT_READ|VM_PROT_EXECUTE);
-
- /* set up command for data segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
- epp->ep_daddr, epp->ep_vp, LINUX_N_DATOFF(*execp, QMAGIC),
- VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- /* set up command for bss segment */
- NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
- epp->ep_daddr + execp->a_data, NULLVP, 0,
- VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
-
- return exec_aout_setup_stack(p, epp);
-}
-
-/*
- * 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.
- */
-static int
-linux_elf32_signature(p, epp, eh)
- struct proc *p;
- struct exec_package *epp;
- Elf32_Ehdr *eh;
-{
- size_t shsize = sizeof(Elf32_Shdr) * eh->e_shnum;
- size_t i;
- static const char signature[] = "\0GCC: (GNU) ";
- char buf[sizeof(signature) - 1];
- Elf32_Shdr *sh;
- int error;
-
- sh = (Elf32_Shdr *) malloc(shsize, M_TEMP, M_WAITOK);
-
- if ((error = elf32_read_from(p, epp->ep_vp, eh->e_shoff,
- (caddr_t) sh, shsize)) != 0)
- goto out;
-
- for (i = 0; i < eh->e_shnum; i++) {
- Elf32_Shdr *s = &sh[i];
-
- /*
- * Identify candidates for the comment header;
- * Header cannot have a load address, or flags and
- * it must be large enough.
- */
- if (s->sh_type != Elf_sht_progbits ||
- s->sh_addr != 0 ||
- s->sh_flags != 0 ||
- s->sh_size < sizeof(signature) - 1)
- continue;
-
- if ((error = elf32_read_from(p, epp->ep_vp, s->sh_offset,
- (caddr_t) buf, sizeof(signature) - 1)) != 0)
- goto out;
-
- /*
- * error is 0, if the signatures match we are done.
- */
- if (memcmp(buf, signature, sizeof(signature) - 1) == 0)
- goto out;
- }
- error = EFTYPE;
-
-out:
- free(sh, M_TEMP);
- return error;
-}
-
-int
-linux_elf32_probe(p, epp, eh, itp, pos)
- struct proc *p;
- struct exec_package *epp;
- Elf32_Ehdr *eh;
- char *itp;
- Elf32_Addr *pos;
-{
- char *bp;
- int error;
- size_t len;
-
- if ((error = linux_elf32_signature(p, epp, eh)) != 0)
- return error;
-
- if (itp[0]) {
- if ((error = emul_find(p, NULL, linux_emul_path, itp, &bp, 0)))
- return error;
- if ((error = copystr(bp, itp, MAXPATHLEN, &len)))
- return error;
- free(bp, M_TEMP);
- }
- epp->ep_emul = &emul_linux_elf;
- *pos = ELF32_NO_ADDR;
- return 0;
-}
-
-/*
- * The Linux system call to load shared libraries, a.out version. The
- * a.out shared libs are just files that are mapped onto a fixed
- * address in the process' address space. The address is given in
- * a_entry. Read in the header, set up some VM commands and run them.
- *
- * Yes, both text and data are mapped at once, so we're left with
- * writeable text for the shared libs. The Linux crt0 seemed to break
- * sometimes when data was mapped seperately. It munmapped a uselib()
- * of ld.so by hand, which failed with shared text and data for ld.so
- * Yuck.
- *
- * Because of the problem with ZMAGIC executables (text starts
- * at 0x400 in the file, but needs to be mapped at 0), ZMAGIC
- * shared libs are not handled very efficiently :-(
- */
-
-int
-linux_sys_uselib(p, v, retval)
- struct proc *p;
- void *v;
- register_t *retval;
-{
- struct linux_sys_uselib_args /* {
- syscallarg(char *) path;
- } */ *uap = v;
- caddr_t sg;
- long bsize, dsize, tsize, taddr, baddr, daddr;
- struct nameidata ni;
- struct vnode *vp;
- struct exec hdr;
- struct exec_vmcmd_set vcset;
- int i, magic, error;
- size_t rem;
-
- sg = stackgap_init(p->p_emul);
- LINUX_CHECK_ALT_EXIST(p, &sg, SCARG(uap, path));
-
- NDINIT(&ni, LOOKUP, FOLLOW, UIO_USERSPACE, SCARG(uap, path), p);
-
- if ((error = namei(&ni)))
- return error;
-
- vp = ni.ni_vp;
-
- if ((error = vn_rdwr(UIO_READ, vp, (caddr_t) &hdr, LINUX_AOUT_HDR_SIZE,
- 0, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred,
- &rem, p))) {
- vrele(vp);
- return error;
- }
-
- if (rem != 0) {
- vrele(vp);
- return ENOEXEC;
- }
-
- if (LINUX_N_MACHTYPE(&hdr) != LINUX_MID_MACHINE)
- return ENOEXEC;
-
- magic = LINUX_N_MAGIC(&hdr);
- taddr = hdr.a_entry & (~(NBPG - 1));
- tsize = hdr.a_text;
- daddr = taddr + tsize;
- dsize = hdr.a_data + hdr.a_bss;
-
- if ((hdr.a_text != 0 || hdr.a_data != 0) && vp->v_writecount != 0) {
- vrele(vp);
- return ETXTBSY;
- }
- vp->v_flag |= VTEXT;
-
- vcset.evs_cnt = 0;
- vcset.evs_used = 0;
-
- NEW_VMCMD(&vcset,
- magic == ZMAGIC ? vmcmd_map_readvn : vmcmd_map_pagedvn,
- hdr.a_text + hdr.a_data, taddr,
- vp, LINUX_N_TXTOFF(hdr, magic),
- VM_PROT_READ|VM_PROT_EXECUTE|VM_PROT_WRITE);
-
- baddr = roundup(daddr + hdr.a_data, NBPG);
- bsize = daddr + dsize - baddr;
- if (bsize > 0) {
- NEW_VMCMD(&vcset, vmcmd_map_zero, bsize, baddr,
- NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
- }
-
- for (i = 0; i < vcset.evs_used && !error; i++) {
- struct exec_vmcmd *vcp;
-
- vcp = &vcset.evs_cmds[i];
- error = (*vcp->ev_proc)(p, vcp);
- }
-
- kill_vmcmds(&vcset);
-
- vrele(vp);
-
- return error;
-}
/*
* Execve(2). Just check the alternate emulation path, and pass it on