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/i386/ibcs2/imgact_coff.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1994 Sean Eric Fagan
    3  * Copyright (c) 1994 Søren Schmidt
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer
   11  *    in this position and unchanged.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 3. The name of the author may not be used to endorse or promote products
   16  *    derived from this software without specific prior written permission
   17  *
   18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   28  */
   29 
   30 #include <sys/cdefs.h>
   31 __FBSDID("$FreeBSD: releng/8.2/sys/i386/ibcs2/imgact_coff.c 199583 2009-11-20 15:27:52Z jhb $");
   32 
   33 #include <sys/param.h>
   34 #include <sys/systm.h>
   35 #include <sys/exec.h>
   36 #include <sys/fcntl.h>
   37 #include <sys/imgact.h>
   38 #include <sys/kernel.h>
   39 #include <sys/lock.h>
   40 #include <sys/malloc.h>
   41 #include <sys/mman.h>
   42 #include <sys/mount.h>
   43 #include <sys/namei.h>
   44 #include <sys/vnode.h>
   45 
   46 #include <vm/vm.h>
   47 #include <vm/pmap.h>
   48 #include <vm/vm_map.h>
   49 #include <vm/vm_kern.h>
   50 #include <vm/vm_extern.h>
   51 
   52 #include <i386/ibcs2/coff.h>
   53 #include <i386/ibcs2/ibcs2_util.h>
   54 
   55 MODULE_DEPEND(coff, ibcs2, 1, 1, 1);
   56 
   57 extern struct sysentvec ibcs2_svr3_sysvec;
   58 
   59 static int coff_load_file(struct thread *td, char *name);
   60 static int exec_coff_imgact(struct image_params *imgp);
   61 
   62 static int load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot);
   63 
   64 static int
   65 load_coff_section(struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset,
   66                   caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
   67 {
   68         size_t map_len;
   69         vm_offset_t map_offset;
   70         vm_offset_t map_addr;
   71         int error;
   72         unsigned char *data_buf = 0;
   73         size_t copy_len;
   74 
   75         map_offset = trunc_page(offset);
   76         map_addr = trunc_page((vm_offset_t)vmaddr);
   77 
   78         if (memsz > filsz) {
   79                 /*
   80                  * We have the stupid situation that
   81                  * the section is longer than it is on file,
   82                  * which means it has zero-filled areas, and
   83                  * we have to work for it.  Stupid iBCS!
   84                  */
   85                 map_len = trunc_page(offset + filsz) - trunc_page(map_offset);
   86         } else {
   87                 /*
   88                  * The only stuff we care about is on disk, and we
   89                  * don't care if we map in more than is really there.
   90                  */
   91                 map_len = round_page(offset + filsz) - trunc_page(map_offset);
   92         }
   93 
   94         DPRINTF(("%s(%d):  vm_mmap(&vmspace->vm_map, &0x%08lx, 0x%x, 0x%x, "
   95                 "VM_PROT_ALL, MAP_PRIVATE | MAP_FIXED, OBJT_VNODE, vp, 0x%x)\n",
   96                 __FILE__, __LINE__, map_addr, map_len, prot, map_offset));
   97 
   98         if ((error = vm_mmap(&vmspace->vm_map,
   99                              &map_addr,
  100                              map_len,
  101                              prot,
  102                              VM_PROT_ALL,
  103                              MAP_PRIVATE | MAP_FIXED,
  104                              OBJT_VNODE,
  105                              vp,
  106                              map_offset)) != 0)
  107                 return error;
  108 
  109         if (memsz == filsz) {
  110                 /* We're done! */
  111                 return 0;
  112         }
  113 
  114         /*
  115          * Now we have screwball stuff, to accomodate stupid COFF.
  116          * We have to map the remaining bit of the file into the kernel's
  117          * memory map, allocate some anonymous memory, copy that last
  118          * bit into it, and then we're done. *sigh*
  119          * For clean-up reasons, we actally map in the file last.
  120          */
  121 
  122         copy_len = (offset + filsz) - trunc_page(offset + filsz);
  123         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
  124         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
  125 
  126         DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx,0x%x, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n", __FILE__, __LINE__, map_addr, map_len));
  127 
  128         if (map_len != 0) {
  129                 error = vm_map_find(&vmspace->vm_map, NULL, 0, &map_addr,
  130                                     map_len, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0);
  131                 if (error)
  132                         return error;
  133         }
  134 
  135         if ((error = vm_mmap(kernel_map,
  136                             (vm_offset_t *) &data_buf,
  137                             PAGE_SIZE,
  138                             VM_PROT_READ,
  139                             VM_PROT_READ,
  140                             0,
  141                             OBJT_VNODE,
  142                             vp,
  143                             trunc_page(offset + filsz))) != 0)
  144                 return error;
  145 
  146         error = copyout(data_buf, (caddr_t) map_addr, copy_len);
  147 
  148         if (vm_map_remove(kernel_map,
  149                           (vm_offset_t) data_buf,
  150                           (vm_offset_t) data_buf + PAGE_SIZE))
  151                 panic("load_coff_section vm_map_remove failed");
  152 
  153         return error;
  154 }
  155 
  156 static int
  157 coff_load_file(struct thread *td, char *name)
  158 {
  159         struct proc *p = td->td_proc;
  160         struct vmspace *vmspace = p->p_vmspace;
  161         int error;
  162         struct nameidata nd;
  163         struct vnode *vp;
  164         struct vattr attr;
  165         struct filehdr *fhdr;
  166         struct aouthdr *ahdr;
  167         struct scnhdr *scns;
  168         char *ptr = 0;
  169         int nscns;
  170         unsigned long text_offset = 0, text_address = 0, text_size = 0;
  171         unsigned long data_offset = 0, data_address = 0, data_size = 0;
  172         unsigned long bss_size = 0;
  173         int i;
  174 
  175         NDINIT(&nd, LOOKUP, ISOPEN | LOCKLEAF | FOLLOW | SAVENAME,
  176             UIO_SYSSPACE, name, td);
  177 
  178         error = namei(&nd);
  179         if (error)
  180                 return error;
  181 
  182         vp = nd.ni_vp;
  183         if (vp == NULL)
  184                 return ENOEXEC;
  185 
  186         if (vp->v_writecount) {
  187                 error = ETXTBSY;
  188                 goto fail;
  189         }
  190 
  191         if ((error = VOP_GETATTR(vp, &attr, td->td_ucred)) != 0)
  192                 goto fail;
  193 
  194         if ((vp->v_mount->mnt_flag & MNT_NOEXEC)
  195             || ((attr.va_mode & 0111) == 0)
  196             || (attr.va_type != VREG))
  197                 goto fail;
  198 
  199         if (attr.va_size == 0) {
  200                 error = ENOEXEC;
  201                 goto fail;
  202         }
  203 
  204         if ((error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td)) != 0)
  205                 goto fail;
  206 
  207         if ((error = VOP_OPEN(vp, FREAD, td->td_ucred, td, NULL)) != 0)
  208                 goto fail;
  209 
  210         /*
  211          * Lose the lock on the vnode. It's no longer needed, and must not
  212          * exist for the pagefault paging to work below.
  213          */
  214         VOP_UNLOCK(vp, 0);
  215 
  216         if ((error = vm_mmap(kernel_map,
  217                             (vm_offset_t *) &ptr,
  218                             PAGE_SIZE,
  219                             VM_PROT_READ,
  220                             VM_PROT_READ,
  221                             0,
  222                             OBJT_VNODE,
  223                             vp,
  224                             0)) != 0)
  225                 goto unlocked_fail;
  226 
  227         fhdr = (struct filehdr *)ptr;
  228 
  229         if (fhdr->f_magic != I386_COFF) {
  230                 error = ENOEXEC;
  231                 goto dealloc_and_fail;
  232         }
  233 
  234         nscns = fhdr->f_nscns;
  235 
  236         if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
  237                 /*
  238                  * XXX -- just fail.  I'm so lazy.
  239                  */
  240                 error = ENOEXEC;
  241                 goto dealloc_and_fail;
  242         }
  243 
  244         ahdr = (struct aouthdr*)(ptr + sizeof(struct filehdr));
  245 
  246         scns = (struct scnhdr*)(ptr + sizeof(struct filehdr)
  247                           + sizeof(struct aouthdr));
  248 
  249         for (i = 0; i < nscns; i++) {
  250                 if (scns[i].s_flags & STYP_NOLOAD)
  251                         continue;
  252                 else if (scns[i].s_flags & STYP_TEXT) {
  253                         text_address = scns[i].s_vaddr;
  254                         text_size = scns[i].s_size;
  255                         text_offset = scns[i].s_scnptr;
  256                 }
  257                 else if (scns[i].s_flags & STYP_DATA) {
  258                         data_address = scns[i].s_vaddr;
  259                         data_size = scns[i].s_size;
  260                         data_offset = scns[i].s_scnptr;
  261                 } else if (scns[i].s_flags & STYP_BSS) {
  262                         bss_size = scns[i].s_size;
  263                 }
  264         }
  265 
  266         if ((error = load_coff_section(vmspace, vp, text_offset,
  267                                       (caddr_t)(void *)(uintptr_t)text_address,
  268                                       text_size, text_size,
  269                                       VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
  270                 goto dealloc_and_fail;
  271         }
  272         if ((error = load_coff_section(vmspace, vp, data_offset,
  273                                       (caddr_t)(void *)(uintptr_t)data_address,
  274                                       data_size + bss_size, data_size,
  275                                       VM_PROT_ALL)) != 0) {
  276                 goto dealloc_and_fail;
  277         }
  278 
  279         error = 0;
  280 
  281  dealloc_and_fail:
  282         if (vm_map_remove(kernel_map,
  283                           (vm_offset_t) ptr,
  284                           (vm_offset_t) ptr + PAGE_SIZE))
  285                 panic("%s vm_map_remove failed", __func__);
  286 
  287  fail:
  288         VOP_UNLOCK(vp, 0);
  289  unlocked_fail:
  290         NDFREE(&nd, NDF_ONLY_PNBUF);
  291         vrele(nd.ni_vp);
  292         return error;
  293 }
  294 
  295 static int
  296 exec_coff_imgact(imgp)
  297         struct image_params *imgp;
  298 {
  299         const struct filehdr *fhdr = (const struct filehdr*)imgp->image_header;
  300         const struct aouthdr *ahdr;
  301         const struct scnhdr *scns;
  302         int i;
  303         struct vmspace *vmspace;
  304         int nscns;
  305         int error;
  306         unsigned long text_offset = 0, text_address = 0, text_size = 0;
  307         unsigned long data_offset = 0, data_address = 0, data_size = 0;
  308         unsigned long bss_size = 0;
  309         caddr_t hole;
  310 
  311         if (fhdr->f_magic != I386_COFF ||
  312             !(fhdr->f_flags & F_EXEC)) {
  313 
  314                  DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
  315                  return -1;
  316         }
  317 
  318         nscns = fhdr->f_nscns;
  319         if ((nscns * sizeof(struct scnhdr)) > PAGE_SIZE) {
  320                 /*
  321                  * For now, return an error -- need to be able to
  322                  * read in all of the section structures.
  323                  */
  324 
  325                 DPRINTF(("%s(%d): return -1\n", __FILE__, __LINE__));
  326                 return -1;
  327         }
  328 
  329         ahdr = (const struct aouthdr*)
  330                ((const char*)(imgp->image_header) + sizeof(struct filehdr));
  331         imgp->entry_addr = ahdr->entry;
  332 
  333         scns = (const struct scnhdr*)
  334                ((const char*)(imgp->image_header) + sizeof(struct filehdr) +
  335                 sizeof(struct aouthdr));
  336 
  337         VOP_UNLOCK(imgp->vp, 0);
  338 
  339         error = exec_new_vmspace(imgp, &ibcs2_svr3_sysvec);
  340         if (error)
  341                 goto fail;
  342         vmspace = imgp->proc->p_vmspace;
  343 
  344         for (i = 0; i < nscns; i++) {
  345 
  346           DPRINTF(("i = %d, scns[i].s_name = %s, scns[i].s_vaddr = %08lx, "
  347                    "scns[i].s_scnptr = %d\n", i, scns[i].s_name,
  348                    scns[i].s_vaddr, scns[i].s_scnptr));
  349           if (scns[i].s_flags & STYP_NOLOAD) {
  350                 /*
  351                  * A section that is not loaded, for whatever
  352                  * reason.  It takes precedance over other flag
  353                  * bits...
  354                  */
  355                 continue;
  356           } else if (scns[i].s_flags & STYP_TEXT) {
  357                 text_address = scns[i].s_vaddr;
  358                 text_size = scns[i].s_size;
  359                 text_offset = scns[i].s_scnptr;
  360           } else if (scns[i].s_flags & STYP_DATA) {
  361                 /* .data section */
  362                 data_address = scns[i].s_vaddr;
  363                 data_size = scns[i].s_size;
  364                 data_offset = scns[i].s_scnptr;
  365           } else if (scns[i].s_flags & STYP_BSS) {
  366                 /* .bss section */
  367                 bss_size = scns[i].s_size;
  368           } else if (scns[i].s_flags & STYP_LIB) {
  369                 char *buf = 0;
  370                 int foff = trunc_page(scns[i].s_scnptr);
  371                 int off = scns[i].s_scnptr - foff;
  372                 int len = round_page(scns[i].s_size + PAGE_SIZE);
  373                 int j;
  374 
  375                 if ((error = vm_mmap(kernel_map,
  376                                     (vm_offset_t *) &buf,
  377                                     len,
  378                                     VM_PROT_READ,
  379                                     VM_PROT_READ,
  380                                     0,
  381                                     OBJT_VNODE,
  382                                     imgp->vp,
  383                                     foff)) != 0) {
  384                         error = ENOEXEC;
  385                         goto fail;
  386                 }
  387                 if(scns[i].s_size) {
  388                         char *libbuf;
  389                         int emul_path_len = strlen(ibcs2_emul_path);
  390 
  391                         libbuf = malloc(MAXPATHLEN + emul_path_len,
  392                                         M_TEMP, M_WAITOK);
  393                         strcpy(libbuf, ibcs2_emul_path);
  394 
  395                         for (j = off; j < scns[i].s_size + off;) {
  396                                 long stroff, nextoff;
  397                                 char *libname;
  398 
  399                                 nextoff = 4 * *(long *)(buf + j);
  400                                 stroff = 4 * *(long *)(buf + j + sizeof(long));
  401 
  402                                 libname = buf + j + stroff;
  403                                 j += nextoff;
  404 
  405                                 DPRINTF(("%s(%d):  shared library %s\n",
  406                                          __FILE__, __LINE__, libname));
  407                                 strlcpy(&libbuf[emul_path_len], libname, MAXPATHLEN);
  408                                 error = coff_load_file(
  409                                     FIRST_THREAD_IN_PROC(imgp->proc), libbuf);
  410                                 if (error)
  411                                         error = coff_load_file(
  412                                             FIRST_THREAD_IN_PROC(imgp->proc),
  413                                             libname);
  414                                 if (error)
  415                                         break;
  416                         }
  417                         free(libbuf, M_TEMP);
  418                 }
  419                 if (vm_map_remove(kernel_map,
  420                                   (vm_offset_t) buf,
  421                                   (vm_offset_t) buf + len))
  422                         panic("exec_coff_imgact vm_map_remove failed");
  423                 if (error)
  424                         goto fail;
  425                 }
  426         }
  427         /*
  428          * Map in .text now
  429          */
  430 
  431         DPRINTF(("%s(%d):  load_coff_section(vmspace, "
  432                 "imgp->vp, %08lx, %08lx, 0x%x, 0x%x, 0x%x)\n",
  433                 __FILE__, __LINE__, text_offset, text_address,
  434                 text_size, text_size, VM_PROT_READ | VM_PROT_EXECUTE));
  435         if ((error = load_coff_section(vmspace, imgp->vp,
  436                                       text_offset,
  437                                       (caddr_t)(void *)(uintptr_t)text_address,
  438                                       text_size, text_size,
  439                                       VM_PROT_READ | VM_PROT_EXECUTE)) != 0) {
  440                 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
  441                 goto fail;
  442         }
  443         /*
  444          * Map in .data and .bss now
  445          */
  446 
  447 
  448         DPRINTF(("%s(%d): load_coff_section(vmspace, "
  449                 "imgp->vp, 0x%08lx, 0x%08lx, 0x%x, 0x%x, 0x%x)\n",
  450                 __FILE__, __LINE__, data_offset, data_address,
  451                 data_size + bss_size, data_size, VM_PROT_ALL));
  452         if ((error = load_coff_section(vmspace, imgp->vp,
  453                                       data_offset,
  454                                       (caddr_t)(void *)(uintptr_t)data_address,
  455                                       data_size + bss_size, data_size,
  456                                       VM_PROT_ALL)) != 0) {
  457 
  458                 DPRINTF(("%s(%d): error = %d\n", __FILE__, __LINE__, error));
  459                 goto fail;
  460         }
  461 
  462         imgp->interpreted = 0;
  463         imgp->proc->p_sysent = &ibcs2_svr3_sysvec;
  464 
  465         vmspace->vm_tsize = round_page(text_size) >> PAGE_SHIFT;
  466         vmspace->vm_dsize = round_page(data_size + bss_size) >> PAGE_SHIFT;
  467         vmspace->vm_taddr = (caddr_t)(void *)(uintptr_t)text_address;
  468         vmspace->vm_daddr = (caddr_t)(void *)(uintptr_t)data_address;
  469 
  470         hole = (caddr_t)trunc_page((vm_offset_t)vmspace->vm_daddr) + ctob(vmspace->vm_dsize);
  471 
  472 
  473         DPRINTF(("%s(%d): vm_map_find(&vmspace->vm_map, NULL, 0, &0x%08lx, PAGE_SIZE, FALSE, VM_PROT_ALL, VM_PROT_ALL, 0)\n",
  474                 __FILE__, __LINE__, hole));
  475         DPRINTF(("imgact: error = %d\n", error));
  476 
  477         error = vm_map_find(&vmspace->vm_map, NULL, 0,
  478                             (vm_offset_t *) &hole, PAGE_SIZE, FALSE,
  479                                 VM_PROT_ALL, VM_PROT_ALL, 0);
  480 
  481         DPRINTF(("IBCS2: start vm_dsize = 0x%x, vm_daddr = 0x%x end = 0x%x\n",
  482                 ctob(vmspace->vm_dsize), vmspace->vm_daddr,
  483                 ctob(vmspace->vm_dsize) + vmspace->vm_daddr ));
  484         DPRINTF(("%s(%d):  returning successfully!\n", __FILE__, __LINE__));
  485 
  486 fail:
  487         vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
  488 
  489         return error;
  490 }
  491 
  492 /*
  493  * Tell kern_execve.c about it, with a little help from the linker.
  494  */
  495 static struct execsw coff_execsw = { exec_coff_imgact, "coff" };
  496 EXEC_SET(coff, coff_execsw);

Cache object: 9d5e2cba66ad48a80649b38628c5dd2a


[ 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.