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

Cache object: f1a4ce46fca712dde16321912bf35348


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