The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/exec_aout.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $OpenBSD: exec_aout.c,v 1.10 2005/11/12 04:31:24 jsg Exp $      */
    2 /*      $NetBSD: exec_aout.c,v 1.14 1996/02/04 02:15:01 christos Exp $  */
    3 
    4 /*
    5  * Copyright (c) 1993, 1994 Christopher G. Demetriou
    6  * All rights reserved.
    7  *
    8  * Redistribution and use in source and binary forms, with or without
    9  * modification, are permitted provided that the following conditions
   10  * are met:
   11  * 1. Redistributions of source code must retain the above copyright
   12  *    notice, this list of conditions and the following disclaimer.
   13  * 2. Redistributions in binary form must reproduce the above copyright
   14  *    notice, this list of conditions and the following disclaimer in the
   15  *    documentation and/or other materials provided with the distribution.
   16  * 3. All advertising materials mentioning features or use of this software
   17  *    must display the following acknowledgement:
   18  *      This product includes software developed by Christopher G. Demetriou.
   19  * 4. The name of the author may not be used to endorse or promote products
   20  *    derived from this software without specific prior written permission
   21  *
   22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   32  */
   33 
   34 #include <sys/param.h>
   35 #include <sys/systm.h>
   36 #include <sys/proc.h>
   37 #include <sys/malloc.h>
   38 #include <sys/vnode.h>
   39 #include <sys/exec.h>
   40 #include <sys/resourcevar.h>
   41 #include <uvm/uvm_extern.h>
   42 
   43 #if defined(_KERN_DO_AOUT)
   44 #if defined(COMPAT_AOUT)
   45 void aout_compat_setup(struct exec_package *epp);
   46 #endif
   47 
   48 /*
   49  * exec_aout_makecmds(): Check if it's an a.out-format executable.
   50  *
   51  * Given a proc pointer and an exec package pointer, see if the referent
   52  * of the epp is in a.out format.  First check 'standard' magic numbers for
   53  * this architecture.  If that fails, try a cpu-dependent hook.
   54  *
   55  * This function, in the former case, or the hook, in the latter, is
   56  * responsible for creating a set of vmcmds which can be used to build
   57  * the process's vm space and inserting them into the exec package.
   58  */
   59 
   60 int
   61 exec_aout_makecmds(struct proc *p, struct exec_package *epp)
   62 {
   63         u_long midmag, magic;
   64         u_short mid;
   65         int error;
   66         struct exec *execp = epp->ep_hdr;
   67 
   68         if (epp->ep_hdrvalid < sizeof(struct exec))
   69                 return ENOEXEC;
   70 
   71         midmag = ntohl(execp->a_midmag);
   72         mid = (midmag >> 16) & 0x3ff;
   73         magic = midmag & 0xffff;
   74 
   75         midmag = mid << 16 | magic;
   76 
   77         switch (midmag) {
   78         case (MID_MACHINE << 16) | ZMAGIC:
   79                 error = exec_aout_prep_zmagic(p, epp);
   80                 break;
   81         case (MID_MACHINE << 16) | NMAGIC:
   82                 error = exec_aout_prep_nmagic(p, epp);
   83                 break;
   84         case (MID_MACHINE << 16) | OMAGIC:
   85                 error = exec_aout_prep_omagic(p, epp);
   86                 break;
   87         default:
   88                 error = cpu_exec_aout_makecmds(p, epp);
   89         }
   90 
   91         if (error)
   92                 kill_vmcmds(&epp->ep_vmcmds);
   93 #ifdef COMPAT_AOUT
   94         aout_compat_setup(epp);
   95 #endif
   96 
   97         return error;
   98 }
   99 
  100 /*
  101  * exec_aout_prep_zmagic(): Prepare a 'native' ZMAGIC binary's exec package
  102  *
  103  * First, set of the various offsets/lengths in the exec package.
  104  *
  105  * Then, mark the text image busy (so it can be demand paged) or error
  106  * out if this is not possible.  Finally, set up vmcmds for the
  107  * text, data, bss, and stack segments.
  108  */
  109 
  110 int
  111 exec_aout_prep_zmagic(struct proc *p, struct exec_package *epp)
  112 {
  113         struct exec *execp = epp->ep_hdr;
  114 
  115         epp->ep_taddr = USRTEXT;
  116         epp->ep_tsize = execp->a_text;
  117         epp->ep_daddr = epp->ep_taddr + execp->a_text;
  118         epp->ep_dsize = execp->a_data + execp->a_bss;
  119         epp->ep_entry = execp->a_entry;
  120 
  121         /*
  122          * check if vnode is in open for writing, because we want to
  123          * demand-page out of it.  if it is, don't do it, for various
  124          * reasons
  125          */
  126         if ((execp->a_text != 0 || execp->a_data != 0) &&
  127             epp->ep_vp->v_writecount != 0) {
  128 #ifdef DIAGNOSTIC
  129                 if (epp->ep_vp->v_flag & VTEXT)
  130                         panic("exec: a VTEXT vnode has writecount != 0");
  131 #endif
  132                 return ETXTBSY;
  133         }
  134         vn_marktext(epp->ep_vp);
  135 
  136         /* set up command for text segment */
  137         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_text,
  138             epp->ep_taddr, epp->ep_vp, 0, VM_PROT_READ|VM_PROT_EXECUTE);
  139 
  140         /* set up command for data segment */
  141         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_pagedvn, execp->a_data,
  142             epp->ep_daddr, epp->ep_vp, execp->a_text,
  143             VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
  144 
  145         /* set up command for bss segment */
  146         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, execp->a_bss,
  147             epp->ep_daddr + execp->a_data, NULLVP, 0,
  148             VM_PROT_READ|VM_PROT_WRITE);
  149 
  150         return exec_setup_stack(p, epp);
  151 }
  152 
  153 /*
  154  * exec_aout_prep_nmagic(): Prepare a 'native' NMAGIC binary's exec package
  155  */
  156 
  157 int
  158 exec_aout_prep_nmagic(struct proc *p, struct exec_package *epp)
  159 {
  160         struct exec *execp = epp->ep_hdr;
  161         long bsize, baddr;
  162 
  163         epp->ep_taddr = USRTEXT;
  164         epp->ep_tsize = execp->a_text;
  165         epp->ep_daddr = roundup(epp->ep_taddr + execp->a_text, __LDPGSZ);
  166         epp->ep_dsize = execp->a_data + execp->a_bss;
  167         epp->ep_entry = execp->a_entry;
  168 
  169         /* set up command for text segment */
  170         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_text,
  171             epp->ep_taddr, epp->ep_vp, sizeof(struct exec),
  172             VM_PROT_READ|VM_PROT_EXECUTE);
  173 
  174         /* set up command for data segment */
  175         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, execp->a_data,
  176             epp->ep_daddr, epp->ep_vp, execp->a_text + sizeof(struct exec),
  177             VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
  178 
  179         /* set up command for bss segment */
  180         baddr = round_page(epp->ep_daddr + execp->a_data);
  181         bsize = epp->ep_daddr + epp->ep_dsize - baddr;
  182         if (bsize > 0)
  183                 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
  184                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE);
  185 
  186         return exec_setup_stack(p, epp);
  187 }
  188 
  189 /*
  190  * exec_aout_prep_omagic(): Prepare a 'native' OMAGIC binary's exec package
  191  */
  192 
  193 int
  194 exec_aout_prep_omagic(struct proc *p, struct exec_package *epp)
  195 {
  196         struct exec *execp = epp->ep_hdr;
  197         long dsize, bsize, baddr;
  198 
  199         epp->ep_taddr = USRTEXT;
  200         epp->ep_tsize = execp->a_text;
  201         epp->ep_daddr = epp->ep_taddr + execp->a_text;
  202         epp->ep_dsize = execp->a_data + execp->a_bss;
  203         epp->ep_entry = execp->a_entry;
  204 
  205         /* set up command for text and data segments */
  206         NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn,
  207             execp->a_text + execp->a_data, epp->ep_taddr, epp->ep_vp,
  208             sizeof(struct exec), VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
  209 
  210         /* set up command for bss segment */
  211         baddr = round_page(epp->ep_daddr + execp->a_data);
  212         bsize = epp->ep_daddr + epp->ep_dsize - baddr;
  213         if (bsize > 0)
  214                 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_zero, bsize, baddr,
  215                     NULLVP, 0, VM_PROT_READ|VM_PROT_WRITE);
  216 
  217         /*
  218          * Make sure (# of pages) mapped above equals (vm_tsize + vm_dsize);
  219          * obreak(2) relies on this fact. Both `vm_tsize' and `vm_dsize' are
  220          * computed (in execve(2)) by rounding *up* `ep_tsize' and `ep_dsize'
  221          * respectively to page boundaries.
  222          * Compensate `ep_dsize' for the amount of data covered by the last
  223          * text page. 
  224          */
  225         dsize = epp->ep_dsize + execp->a_text - round_page(execp->a_text);
  226         epp->ep_dsize = (dsize > 0) ? dsize : 0;
  227         return exec_setup_stack(p, epp);
  228 }
  229 
  230 #endif /* _KERN_DO_AOUT */

Cache object: af42b61590c657c182637b7ce1ffc05b


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.