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/imgact_elf.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) 2000 David O'Brien
    3  * Copyright (c) 1995-1996 Søren Schmidt
    4  * Copyright (c) 1996 Peter Wemm
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer
   12  *    in this position and unchanged.
   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. The name of the author may not be used to endorse or promote products
   17  *    derived from this software without specific prior written permission
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   29  */
   30 
   31 #include <sys/cdefs.h>
   32 __FBSDID("$FreeBSD: releng/10.2/sys/kern/imgact_elf.c 284665 2015-06-21 06:28:26Z trasz $");
   33 
   34 #include "opt_capsicum.h"
   35 #include "opt_compat.h"
   36 #include "opt_core.h"
   37 
   38 #include <sys/param.h>
   39 #include <sys/capsicum.h>
   40 #include <sys/exec.h>
   41 #include <sys/fcntl.h>
   42 #include <sys/imgact.h>
   43 #include <sys/imgact_elf.h>
   44 #include <sys/jail.h>
   45 #include <sys/kernel.h>
   46 #include <sys/lock.h>
   47 #include <sys/malloc.h>
   48 #include <sys/mount.h>
   49 #include <sys/mman.h>
   50 #include <sys/namei.h>
   51 #include <sys/pioctl.h>
   52 #include <sys/proc.h>
   53 #include <sys/procfs.h>
   54 #include <sys/racct.h>
   55 #include <sys/resourcevar.h>
   56 #include <sys/rwlock.h>
   57 #include <sys/sbuf.h>
   58 #include <sys/sf_buf.h>
   59 #include <sys/smp.h>
   60 #include <sys/systm.h>
   61 #include <sys/signalvar.h>
   62 #include <sys/stat.h>
   63 #include <sys/sx.h>
   64 #include <sys/syscall.h>
   65 #include <sys/sysctl.h>
   66 #include <sys/sysent.h>
   67 #include <sys/vnode.h>
   68 #include <sys/syslog.h>
   69 #include <sys/eventhandler.h>
   70 #include <sys/user.h>
   71 
   72 #include <net/zlib.h>
   73 
   74 #include <vm/vm.h>
   75 #include <vm/vm_kern.h>
   76 #include <vm/vm_param.h>
   77 #include <vm/pmap.h>
   78 #include <vm/vm_map.h>
   79 #include <vm/vm_object.h>
   80 #include <vm/vm_extern.h>
   81 
   82 #include <machine/elf.h>
   83 #include <machine/md_var.h>
   84 
   85 #define ELF_NOTE_ROUNDSIZE      4
   86 #define OLD_EI_BRAND    8
   87 
   88 static int __elfN(check_header)(const Elf_Ehdr *hdr);
   89 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
   90     const char *interp, int interp_name_len, int32_t *osrel);
   91 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
   92     u_long *entry, size_t pagesize);
   93 static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
   94     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
   95     size_t pagesize);
   96 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
   97 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
   98     int32_t *osrel);
   99 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
  100 static boolean_t __elfN(check_note)(struct image_params *imgp,
  101     Elf_Brandnote *checknote, int32_t *osrel);
  102 static vm_prot_t __elfN(trans_prot)(Elf_Word);
  103 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
  104 
  105 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
  106     "");
  107 
  108 #ifdef COMPRESS_USER_CORES
  109 static int compress_core(gzFile, char *, char *, unsigned int,
  110     struct thread * td);
  111 #endif
  112 #define CORE_BUF_SIZE   (16 * 1024)
  113 
  114 int __elfN(fallback_brand) = -1;
  115 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
  116     fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
  117     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
  118 TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand",
  119     &__elfN(fallback_brand));
  120 
  121 static int elf_legacy_coredump = 0;
  122 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW, 
  123     &elf_legacy_coredump, 0, "");
  124 
  125 int __elfN(nxstack) =
  126 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
  127         1;
  128 #else
  129         0;
  130 #endif
  131 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
  132     nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
  133     __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
  134 
  135 #if __ELF_WORD_SIZE == 32
  136 #if defined(__amd64__) || defined(__ia64__)
  137 int i386_read_exec = 0;
  138 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
  139     "enable execution from readable segments");
  140 #endif
  141 #endif
  142 
  143 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
  144 
  145 #define trunc_page_ps(va, ps)   ((va) & ~(ps - 1))
  146 #define round_page_ps(va, ps)   (((va) + (ps - 1)) & ~(ps - 1))
  147 #define aligned(a, t)   (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
  148 
  149 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
  150 
  151 Elf_Brandnote __elfN(freebsd_brandnote) = {
  152         .hdr.n_namesz   = sizeof(FREEBSD_ABI_VENDOR),
  153         .hdr.n_descsz   = sizeof(int32_t),
  154         .hdr.n_type     = 1,
  155         .vendor         = FREEBSD_ABI_VENDOR,
  156         .flags          = BN_TRANSLATE_OSREL,
  157         .trans_osrel    = __elfN(freebsd_trans_osrel)
  158 };
  159 
  160 static boolean_t
  161 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
  162 {
  163         uintptr_t p;
  164 
  165         p = (uintptr_t)(note + 1);
  166         p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
  167         *osrel = *(const int32_t *)(p);
  168 
  169         return (TRUE);
  170 }
  171 
  172 static const char GNU_ABI_VENDOR[] = "GNU";
  173 static int GNU_KFREEBSD_ABI_DESC = 3;
  174 
  175 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
  176         .hdr.n_namesz   = sizeof(GNU_ABI_VENDOR),
  177         .hdr.n_descsz   = 16,   /* XXX at least 16 */
  178         .hdr.n_type     = 1,
  179         .vendor         = GNU_ABI_VENDOR,
  180         .flags          = BN_TRANSLATE_OSREL,
  181         .trans_osrel    = kfreebsd_trans_osrel
  182 };
  183 
  184 static boolean_t
  185 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
  186 {
  187         const Elf32_Word *desc;
  188         uintptr_t p;
  189 
  190         p = (uintptr_t)(note + 1);
  191         p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
  192 
  193         desc = (const Elf32_Word *)p;
  194         if (desc[0] != GNU_KFREEBSD_ABI_DESC)
  195                 return (FALSE);
  196 
  197         /*
  198          * Debian GNU/kFreeBSD embed the earliest compatible kernel version
  199          * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
  200          */
  201         *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
  202 
  203         return (TRUE);
  204 }
  205 
  206 int
  207 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
  208 {
  209         int i;
  210 
  211         for (i = 0; i < MAX_BRANDS; i++) {
  212                 if (elf_brand_list[i] == NULL) {
  213                         elf_brand_list[i] = entry;
  214                         break;
  215                 }
  216         }
  217         if (i == MAX_BRANDS) {
  218                 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
  219                         __func__, entry);
  220                 return (-1);
  221         }
  222         return (0);
  223 }
  224 
  225 int
  226 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
  227 {
  228         int i;
  229 
  230         for (i = 0; i < MAX_BRANDS; i++) {
  231                 if (elf_brand_list[i] == entry) {
  232                         elf_brand_list[i] = NULL;
  233                         break;
  234                 }
  235         }
  236         if (i == MAX_BRANDS)
  237                 return (-1);
  238         return (0);
  239 }
  240 
  241 int
  242 __elfN(brand_inuse)(Elf_Brandinfo *entry)
  243 {
  244         struct proc *p;
  245         int rval = FALSE;
  246 
  247         sx_slock(&allproc_lock);
  248         FOREACH_PROC_IN_SYSTEM(p) {
  249                 if (p->p_sysent == entry->sysvec) {
  250                         rval = TRUE;
  251                         break;
  252                 }
  253         }
  254         sx_sunlock(&allproc_lock);
  255 
  256         return (rval);
  257 }
  258 
  259 static Elf_Brandinfo *
  260 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
  261     int interp_name_len, int32_t *osrel)
  262 {
  263         const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
  264         Elf_Brandinfo *bi;
  265         boolean_t ret;
  266         int i;
  267 
  268         /*
  269          * We support four types of branding -- (1) the ELF EI_OSABI field
  270          * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
  271          * branding w/in the ELF header, (3) path of the `interp_path'
  272          * field, and (4) the ".note.ABI-tag" ELF section.
  273          */
  274 
  275         /* Look for an ".note.ABI-tag" ELF section */
  276         for (i = 0; i < MAX_BRANDS; i++) {
  277                 bi = elf_brand_list[i];
  278                 if (bi == NULL)
  279                         continue;
  280                 if (hdr->e_machine == bi->machine && (bi->flags &
  281                     (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
  282                         ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
  283                         if (ret)
  284                                 return (bi);
  285                 }
  286         }
  287 
  288         /* If the executable has a brand, search for it in the brand list. */
  289         for (i = 0; i < MAX_BRANDS; i++) {
  290                 bi = elf_brand_list[i];
  291                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
  292                         continue;
  293                 if (hdr->e_machine == bi->machine &&
  294                     (hdr->e_ident[EI_OSABI] == bi->brand ||
  295                     strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
  296                     bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
  297                         return (bi);
  298         }
  299 
  300         /* Lacking a known brand, search for a recognized interpreter. */
  301         if (interp != NULL) {
  302                 for (i = 0; i < MAX_BRANDS; i++) {
  303                         bi = elf_brand_list[i];
  304                         if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
  305                                 continue;
  306                         if (hdr->e_machine == bi->machine &&
  307                             /* ELF image p_filesz includes terminating zero */
  308                             strlen(bi->interp_path) + 1 == interp_name_len &&
  309                             strncmp(interp, bi->interp_path, interp_name_len)
  310                             == 0)
  311                                 return (bi);
  312                 }
  313         }
  314 
  315         /* Lacking a recognized interpreter, try the default brand */
  316         for (i = 0; i < MAX_BRANDS; i++) {
  317                 bi = elf_brand_list[i];
  318                 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
  319                         continue;
  320                 if (hdr->e_machine == bi->machine &&
  321                     __elfN(fallback_brand) == bi->brand)
  322                         return (bi);
  323         }
  324         return (NULL);
  325 }
  326 
  327 static int
  328 __elfN(check_header)(const Elf_Ehdr *hdr)
  329 {
  330         Elf_Brandinfo *bi;
  331         int i;
  332 
  333         if (!IS_ELF(*hdr) ||
  334             hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
  335             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
  336             hdr->e_ident[EI_VERSION] != EV_CURRENT ||
  337             hdr->e_phentsize != sizeof(Elf_Phdr) ||
  338             hdr->e_version != ELF_TARG_VER)
  339                 return (ENOEXEC);
  340 
  341         /*
  342          * Make sure we have at least one brand for this machine.
  343          */
  344 
  345         for (i = 0; i < MAX_BRANDS; i++) {
  346                 bi = elf_brand_list[i];
  347                 if (bi != NULL && bi->machine == hdr->e_machine)
  348                         break;
  349         }
  350         if (i == MAX_BRANDS)
  351                 return (ENOEXEC);
  352 
  353         return (0);
  354 }
  355 
  356 static int
  357 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
  358     vm_offset_t start, vm_offset_t end, vm_prot_t prot)
  359 {
  360         struct sf_buf *sf;
  361         int error;
  362         vm_offset_t off;
  363 
  364         /*
  365          * Create the page if it doesn't exist yet. Ignore errors.
  366          */
  367         vm_map_lock(map);
  368         vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
  369             VM_PROT_ALL, VM_PROT_ALL, 0);
  370         vm_map_unlock(map);
  371 
  372         /*
  373          * Find the page from the underlying object.
  374          */
  375         if (object) {
  376                 sf = vm_imgact_map_page(object, offset);
  377                 if (sf == NULL)
  378                         return (KERN_FAILURE);
  379                 off = offset - trunc_page(offset);
  380                 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
  381                     end - start);
  382                 vm_imgact_unmap_page(sf);
  383                 if (error) {
  384                         return (KERN_FAILURE);
  385                 }
  386         }
  387 
  388         return (KERN_SUCCESS);
  389 }
  390 
  391 static int
  392 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
  393     vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
  394 {
  395         struct sf_buf *sf;
  396         vm_offset_t off;
  397         vm_size_t sz;
  398         int error, rv;
  399 
  400         if (start != trunc_page(start)) {
  401                 rv = __elfN(map_partial)(map, object, offset, start,
  402                     round_page(start), prot);
  403                 if (rv)
  404                         return (rv);
  405                 offset += round_page(start) - start;
  406                 start = round_page(start);
  407         }
  408         if (end != round_page(end)) {
  409                 rv = __elfN(map_partial)(map, object, offset +
  410                     trunc_page(end) - start, trunc_page(end), end, prot);
  411                 if (rv)
  412                         return (rv);
  413                 end = trunc_page(end);
  414         }
  415         if (end > start) {
  416                 if (offset & PAGE_MASK) {
  417                         /*
  418                          * The mapping is not page aligned. This means we have
  419                          * to copy the data. Sigh.
  420                          */
  421                         rv = vm_map_find(map, NULL, 0, &start, end - start, 0,
  422                             VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL,
  423                             0);
  424                         if (rv)
  425                                 return (rv);
  426                         if (object == NULL)
  427                                 return (KERN_SUCCESS);
  428                         for (; start < end; start += sz) {
  429                                 sf = vm_imgact_map_page(object, offset);
  430                                 if (sf == NULL)
  431                                         return (KERN_FAILURE);
  432                                 off = offset - trunc_page(offset);
  433                                 sz = end - start;
  434                                 if (sz > PAGE_SIZE - off)
  435                                         sz = PAGE_SIZE - off;
  436                                 error = copyout((caddr_t)sf_buf_kva(sf) + off,
  437                                     (caddr_t)start, sz);
  438                                 vm_imgact_unmap_page(sf);
  439                                 if (error) {
  440                                         return (KERN_FAILURE);
  441                                 }
  442                                 offset += sz;
  443                         }
  444                         rv = KERN_SUCCESS;
  445                 } else {
  446                         vm_object_reference(object);
  447                         vm_map_lock(map);
  448                         rv = vm_map_insert(map, object, offset, start, end,
  449                             prot, VM_PROT_ALL, cow);
  450                         vm_map_unlock(map);
  451                         if (rv != KERN_SUCCESS)
  452                                 vm_object_deallocate(object);
  453                 }
  454                 return (rv);
  455         } else {
  456                 return (KERN_SUCCESS);
  457         }
  458 }
  459 
  460 static int
  461 __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
  462     caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
  463     size_t pagesize)
  464 {
  465         struct sf_buf *sf;
  466         size_t map_len;
  467         vm_map_t map;
  468         vm_object_t object;
  469         vm_offset_t map_addr;
  470         int error, rv, cow;
  471         size_t copy_len;
  472         vm_offset_t file_addr;
  473 
  474         /*
  475          * It's necessary to fail if the filsz + offset taken from the
  476          * header is greater than the actual file pager object's size.
  477          * If we were to allow this, then the vm_map_find() below would
  478          * walk right off the end of the file object and into the ether.
  479          *
  480          * While I'm here, might as well check for something else that
  481          * is invalid: filsz cannot be greater than memsz.
  482          */
  483         if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
  484                 uprintf("elf_load_section: truncated ELF file\n");
  485                 return (ENOEXEC);
  486         }
  487 
  488         object = imgp->object;
  489         map = &imgp->proc->p_vmspace->vm_map;
  490         map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
  491         file_addr = trunc_page_ps(offset, pagesize);
  492 
  493         /*
  494          * We have two choices.  We can either clear the data in the last page
  495          * of an oversized mapping, or we can start the anon mapping a page
  496          * early and copy the initialized data into that first page.  We
  497          * choose the second..
  498          */
  499         if (memsz > filsz)
  500                 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
  501         else
  502                 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
  503 
  504         if (map_len != 0) {
  505                 /* cow flags: don't dump readonly sections in core */
  506                 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
  507                     (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
  508 
  509                 rv = __elfN(map_insert)(map,
  510                                       object,
  511                                       file_addr,        /* file offset */
  512                                       map_addr,         /* virtual start */
  513                                       map_addr + map_len,/* virtual end */
  514                                       prot,
  515                                       cow);
  516                 if (rv != KERN_SUCCESS)
  517                         return (EINVAL);
  518 
  519                 /* we can stop now if we've covered it all */
  520                 if (memsz == filsz) {
  521                         return (0);
  522                 }
  523         }
  524 
  525 
  526         /*
  527          * We have to get the remaining bit of the file into the first part
  528          * of the oversized map segment.  This is normally because the .data
  529          * segment in the file is extended to provide bss.  It's a neat idea
  530          * to try and save a page, but it's a pain in the behind to implement.
  531          */
  532         copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
  533         map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
  534         map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
  535             map_addr;
  536 
  537         /* This had damn well better be true! */
  538         if (map_len != 0) {
  539                 rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
  540                     map_len, VM_PROT_ALL, 0);
  541                 if (rv != KERN_SUCCESS) {
  542                         return (EINVAL);
  543                 }
  544         }
  545 
  546         if (copy_len != 0) {
  547                 vm_offset_t off;
  548 
  549                 sf = vm_imgact_map_page(object, offset + filsz);
  550                 if (sf == NULL)
  551                         return (EIO);
  552 
  553                 /* send the page fragment to user space */
  554                 off = trunc_page_ps(offset + filsz, pagesize) -
  555                     trunc_page(offset + filsz);
  556                 error = copyout((caddr_t)sf_buf_kva(sf) + off,
  557                     (caddr_t)map_addr, copy_len);
  558                 vm_imgact_unmap_page(sf);
  559                 if (error) {
  560                         return (error);
  561                 }
  562         }
  563 
  564         /*
  565          * set it to the specified protection.
  566          * XXX had better undo the damage from pasting over the cracks here!
  567          */
  568         vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
  569             map_len), prot, FALSE);
  570 
  571         return (0);
  572 }
  573 
  574 /*
  575  * Load the file "file" into memory.  It may be either a shared object
  576  * or an executable.
  577  *
  578  * The "addr" reference parameter is in/out.  On entry, it specifies
  579  * the address where a shared object should be loaded.  If the file is
  580  * an executable, this value is ignored.  On exit, "addr" specifies
  581  * where the file was actually loaded.
  582  *
  583  * The "entry" reference parameter is out only.  On exit, it specifies
  584  * the entry point for the loaded file.
  585  */
  586 static int
  587 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
  588         u_long *entry, size_t pagesize)
  589 {
  590         struct {
  591                 struct nameidata nd;
  592                 struct vattr attr;
  593                 struct image_params image_params;
  594         } *tempdata;
  595         const Elf_Ehdr *hdr = NULL;
  596         const Elf_Phdr *phdr = NULL;
  597         struct nameidata *nd;
  598         struct vattr *attr;
  599         struct image_params *imgp;
  600         vm_prot_t prot;
  601         u_long rbase;
  602         u_long base_addr = 0;
  603         int error, i, numsegs;
  604 
  605 #ifdef CAPABILITY_MODE
  606         /*
  607          * XXXJA: This check can go away once we are sufficiently confident
  608          * that the checks in namei() are correct.
  609          */
  610         if (IN_CAPABILITY_MODE(curthread))
  611                 return (ECAPMODE);
  612 #endif
  613 
  614         tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
  615         nd = &tempdata->nd;
  616         attr = &tempdata->attr;
  617         imgp = &tempdata->image_params;
  618 
  619         /*
  620          * Initialize part of the common data
  621          */
  622         imgp->proc = p;
  623         imgp->attr = attr;
  624         imgp->firstpage = NULL;
  625         imgp->image_header = NULL;
  626         imgp->object = NULL;
  627         imgp->execlabel = NULL;
  628 
  629         NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
  630         if ((error = namei(nd)) != 0) {
  631                 nd->ni_vp = NULL;
  632                 goto fail;
  633         }
  634         NDFREE(nd, NDF_ONLY_PNBUF);
  635         imgp->vp = nd->ni_vp;
  636 
  637         /*
  638          * Check permissions, modes, uid, etc on the file, and "open" it.
  639          */
  640         error = exec_check_permissions(imgp);
  641         if (error)
  642                 goto fail;
  643 
  644         error = exec_map_first_page(imgp);
  645         if (error)
  646                 goto fail;
  647 
  648         /*
  649          * Also make certain that the interpreter stays the same, so set
  650          * its VV_TEXT flag, too.
  651          */
  652         VOP_SET_TEXT(nd->ni_vp);
  653 
  654         imgp->object = nd->ni_vp->v_object;
  655 
  656         hdr = (const Elf_Ehdr *)imgp->image_header;
  657         if ((error = __elfN(check_header)(hdr)) != 0)
  658                 goto fail;
  659         if (hdr->e_type == ET_DYN)
  660                 rbase = *addr;
  661         else if (hdr->e_type == ET_EXEC)
  662                 rbase = 0;
  663         else {
  664                 error = ENOEXEC;
  665                 goto fail;
  666         }
  667 
  668         /* Only support headers that fit within first page for now      */
  669         if ((hdr->e_phoff > PAGE_SIZE) ||
  670             (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
  671                 error = ENOEXEC;
  672                 goto fail;
  673         }
  674 
  675         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
  676         if (!aligned(phdr, Elf_Addr)) {
  677                 error = ENOEXEC;
  678                 goto fail;
  679         }
  680 
  681         for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
  682                 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
  683                         /* Loadable segment */
  684                         prot = __elfN(trans_prot)(phdr[i].p_flags);
  685                         error = __elfN(load_section)(imgp, phdr[i].p_offset,
  686                             (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
  687                             phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
  688                         if (error != 0)
  689                                 goto fail;
  690                         /*
  691                          * Establish the base address if this is the
  692                          * first segment.
  693                          */
  694                         if (numsegs == 0)
  695                                 base_addr = trunc_page(phdr[i].p_vaddr +
  696                                     rbase);
  697                         numsegs++;
  698                 }
  699         }
  700         *addr = base_addr;
  701         *entry = (unsigned long)hdr->e_entry + rbase;
  702 
  703 fail:
  704         if (imgp->firstpage)
  705                 exec_unmap_first_page(imgp);
  706 
  707         if (nd->ni_vp)
  708                 vput(nd->ni_vp);
  709 
  710         free(tempdata, M_TEMP);
  711 
  712         return (error);
  713 }
  714 
  715 static int
  716 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
  717 {
  718         const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
  719         const Elf_Phdr *phdr;
  720         Elf_Auxargs *elf_auxargs;
  721         struct vmspace *vmspace;
  722         vm_prot_t prot;
  723         u_long text_size = 0, data_size = 0, total_size = 0;
  724         u_long text_addr = 0, data_addr = 0;
  725         u_long seg_size, seg_addr;
  726         u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
  727         int32_t osrel = 0;
  728         int error = 0, i, n, interp_name_len = 0;
  729         const char *interp = NULL, *newinterp = NULL;
  730         Elf_Brandinfo *brand_info;
  731         char *path;
  732         struct sysentvec *sv;
  733 
  734         /*
  735          * Do we have a valid ELF header ?
  736          *
  737          * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
  738          * if particular brand doesn't support it.
  739          */
  740         if (__elfN(check_header)(hdr) != 0 ||
  741             (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
  742                 return (-1);
  743 
  744         /*
  745          * From here on down, we return an errno, not -1, as we've
  746          * detected an ELF file.
  747          */
  748 
  749         if ((hdr->e_phoff > PAGE_SIZE) ||
  750             (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
  751                 /* Only support headers in first page for now */
  752                 return (ENOEXEC);
  753         }
  754         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
  755         if (!aligned(phdr, Elf_Addr))
  756                 return (ENOEXEC);
  757         n = 0;
  758         baddr = 0;
  759         for (i = 0; i < hdr->e_phnum; i++) {
  760                 switch (phdr[i].p_type) {
  761                 case PT_LOAD:
  762                         if (n == 0)
  763                                 baddr = phdr[i].p_vaddr;
  764                         n++;
  765                         break;
  766                 case PT_INTERP:
  767                         /* Path to interpreter */
  768                         if (phdr[i].p_filesz > MAXPATHLEN ||
  769                             phdr[i].p_offset > PAGE_SIZE ||
  770                             phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset)
  771                                 return (ENOEXEC);
  772                         interp = imgp->image_header + phdr[i].p_offset;
  773                         interp_name_len = phdr[i].p_filesz;
  774                         break;
  775                 case PT_GNU_STACK:
  776                         if (__elfN(nxstack))
  777                                 imgp->stack_prot =
  778                                     __elfN(trans_prot)(phdr[i].p_flags);
  779                         imgp->stack_sz = phdr[i].p_memsz;
  780                         break;
  781                 }
  782         }
  783 
  784         brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
  785             &osrel);
  786         if (brand_info == NULL) {
  787                 uprintf("ELF binary type \"%u\" not known.\n",
  788                     hdr->e_ident[EI_OSABI]);
  789                 return (ENOEXEC);
  790         }
  791         if (hdr->e_type == ET_DYN) {
  792                 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
  793                         return (ENOEXEC);
  794                 /*
  795                  * Honour the base load address from the dso if it is
  796                  * non-zero for some reason.
  797                  */
  798                 if (baddr == 0)
  799                         et_dyn_addr = ET_DYN_LOAD_ADDR;
  800                 else
  801                         et_dyn_addr = 0;
  802         } else
  803                 et_dyn_addr = 0;
  804         sv = brand_info->sysvec;
  805         if (interp != NULL && brand_info->interp_newpath != NULL)
  806                 newinterp = brand_info->interp_newpath;
  807 
  808         /*
  809          * Avoid a possible deadlock if the current address space is destroyed
  810          * and that address space maps the locked vnode.  In the common case,
  811          * the locked vnode's v_usecount is decremented but remains greater
  812          * than zero.  Consequently, the vnode lock is not needed by vrele().
  813          * However, in cases where the vnode lock is external, such as nullfs,
  814          * v_usecount may become zero.
  815          *
  816          * The VV_TEXT flag prevents modifications to the executable while
  817          * the vnode is unlocked.
  818          */
  819         VOP_UNLOCK(imgp->vp, 0);
  820 
  821         error = exec_new_vmspace(imgp, sv);
  822         imgp->proc->p_sysent = sv;
  823 
  824         vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
  825         if (error)
  826                 return (error);
  827 
  828         for (i = 0; i < hdr->e_phnum; i++) {
  829                 switch (phdr[i].p_type) {
  830                 case PT_LOAD:   /* Loadable segment */
  831                         if (phdr[i].p_memsz == 0)
  832                                 break;
  833                         prot = __elfN(trans_prot)(phdr[i].p_flags);
  834                         error = __elfN(load_section)(imgp, phdr[i].p_offset,
  835                             (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
  836                             phdr[i].p_memsz, phdr[i].p_filesz, prot,
  837                             sv->sv_pagesize);
  838                         if (error != 0)
  839                                 return (error);
  840 
  841                         /*
  842                          * If this segment contains the program headers,
  843                          * remember their virtual address for the AT_PHDR
  844                          * aux entry. Static binaries don't usually include
  845                          * a PT_PHDR entry.
  846                          */
  847                         if (phdr[i].p_offset == 0 &&
  848                             hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
  849                                 <= phdr[i].p_filesz)
  850                                 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
  851                                     et_dyn_addr;
  852 
  853                         seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
  854                         seg_size = round_page(phdr[i].p_memsz +
  855                             phdr[i].p_vaddr + et_dyn_addr - seg_addr);
  856 
  857                         /*
  858                          * Make the largest executable segment the official
  859                          * text segment and all others data.
  860                          *
  861                          * Note that obreak() assumes that data_addr + 
  862                          * data_size == end of data load area, and the ELF
  863                          * file format expects segments to be sorted by
  864                          * address.  If multiple data segments exist, the
  865                          * last one will be used.
  866                          */
  867 
  868                         if (phdr[i].p_flags & PF_X && text_size < seg_size) {
  869                                 text_size = seg_size;
  870                                 text_addr = seg_addr;
  871                         } else {
  872                                 data_size = seg_size;
  873                                 data_addr = seg_addr;
  874                         }
  875                         total_size += seg_size;
  876                         break;
  877                 case PT_PHDR:   /* Program header table info */
  878                         proghdr = phdr[i].p_vaddr + et_dyn_addr;
  879                         break;
  880                 default:
  881                         break;
  882                 }
  883         }
  884         
  885         if (data_addr == 0 && data_size == 0) {
  886                 data_addr = text_addr;
  887                 data_size = text_size;
  888         }
  889 
  890         entry = (u_long)hdr->e_entry + et_dyn_addr;
  891 
  892         /*
  893          * Check limits.  It should be safe to check the
  894          * limits after loading the segments since we do
  895          * not actually fault in all the segments pages.
  896          */
  897         PROC_LOCK(imgp->proc);
  898         if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) ||
  899             text_size > maxtsiz ||
  900             total_size > lim_cur(imgp->proc, RLIMIT_VMEM) ||
  901             racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
  902             racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
  903                 PROC_UNLOCK(imgp->proc);
  904                 return (ENOMEM);
  905         }
  906 
  907         vmspace = imgp->proc->p_vmspace;
  908         vmspace->vm_tsize = text_size >> PAGE_SHIFT;
  909         vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
  910         vmspace->vm_dsize = data_size >> PAGE_SHIFT;
  911         vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
  912 
  913         /*
  914          * We load the dynamic linker where a userland call
  915          * to mmap(0, ...) would put it.  The rationale behind this
  916          * calculation is that it leaves room for the heap to grow to
  917          * its maximum allowed size.
  918          */
  919         addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(imgp->proc,
  920             RLIMIT_DATA));
  921         PROC_UNLOCK(imgp->proc);
  922 
  923         imgp->entry_addr = entry;
  924 
  925         if (interp != NULL) {
  926                 int have_interp = FALSE;
  927                 VOP_UNLOCK(imgp->vp, 0);
  928                 if (brand_info->emul_path != NULL &&
  929                     brand_info->emul_path[0] != '\0') {
  930                         path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
  931                         snprintf(path, MAXPATHLEN, "%s%s",
  932                             brand_info->emul_path, interp);
  933                         error = __elfN(load_file)(imgp->proc, path, &addr,
  934                             &imgp->entry_addr, sv->sv_pagesize);
  935                         free(path, M_TEMP);
  936                         if (error == 0)
  937                                 have_interp = TRUE;
  938                 }
  939                 if (!have_interp && newinterp != NULL) {
  940                         error = __elfN(load_file)(imgp->proc, newinterp, &addr,
  941                             &imgp->entry_addr, sv->sv_pagesize);
  942                         if (error == 0)
  943                                 have_interp = TRUE;
  944                 }
  945                 if (!have_interp) {
  946                         error = __elfN(load_file)(imgp->proc, interp, &addr,
  947                             &imgp->entry_addr, sv->sv_pagesize);
  948                 }
  949                 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
  950                 if (error != 0) {
  951                         uprintf("ELF interpreter %s not found\n", interp);
  952                         return (error);
  953                 }
  954         } else
  955                 addr = et_dyn_addr;
  956 
  957         /*
  958          * Construct auxargs table (used by the fixup routine)
  959          */
  960         elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
  961         elf_auxargs->execfd = -1;
  962         elf_auxargs->phdr = proghdr;
  963         elf_auxargs->phent = hdr->e_phentsize;
  964         elf_auxargs->phnum = hdr->e_phnum;
  965         elf_auxargs->pagesz = PAGE_SIZE;
  966         elf_auxargs->base = addr;
  967         elf_auxargs->flags = 0;
  968         elf_auxargs->entry = entry;
  969 
  970         imgp->auxargs = elf_auxargs;
  971         imgp->interpreted = 0;
  972         imgp->reloc_base = addr;
  973         imgp->proc->p_osrel = osrel;
  974 
  975         return (error);
  976 }
  977 
  978 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
  979 
  980 int
  981 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
  982 {
  983         Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
  984         Elf_Addr *base;
  985         Elf_Addr *pos;
  986 
  987         base = (Elf_Addr *)*stack_base;
  988         pos = base + (imgp->args->argc + imgp->args->envc + 2);
  989 
  990         if (args->execfd != -1)
  991                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
  992         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
  993         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
  994         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
  995         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
  996         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
  997         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
  998         AUXARGS_ENTRY(pos, AT_BASE, args->base);
  999         if (imgp->execpathp != 0)
 1000                 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
 1001         AUXARGS_ENTRY(pos, AT_OSRELDATE,
 1002             imgp->proc->p_ucred->cr_prison->pr_osreldate);
 1003         if (imgp->canary != 0) {
 1004                 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
 1005                 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
 1006         }
 1007         AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
 1008         if (imgp->pagesizes != 0) {
 1009                 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
 1010                 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
 1011         }
 1012         if (imgp->sysent->sv_timekeep_base != 0) {
 1013                 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
 1014                     imgp->sysent->sv_timekeep_base);
 1015         }
 1016         AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
 1017             != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
 1018             imgp->sysent->sv_stackprot);
 1019         AUXARGS_ENTRY(pos, AT_NULL, 0);
 1020 
 1021         free(imgp->auxargs, M_TEMP);
 1022         imgp->auxargs = NULL;
 1023 
 1024         base--;
 1025         suword(base, (long)imgp->args->argc);
 1026         *stack_base = (register_t *)base;
 1027         return (0);
 1028 }
 1029 
 1030 /*
 1031  * Code for generating ELF core dumps.
 1032  */
 1033 
 1034 typedef void (*segment_callback)(vm_map_entry_t, void *);
 1035 
 1036 /* Closure for cb_put_phdr(). */
 1037 struct phdr_closure {
 1038         Elf_Phdr *phdr;         /* Program header to fill in */
 1039         Elf_Off offset;         /* Offset of segment in core file */
 1040 };
 1041 
 1042 /* Closure for cb_size_segment(). */
 1043 struct sseg_closure {
 1044         int count;              /* Count of writable segments. */
 1045         size_t size;            /* Total size of all writable segments. */
 1046 };
 1047 
 1048 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
 1049 
 1050 struct note_info {
 1051         int             type;           /* Note type. */
 1052         outfunc_t       outfunc;        /* Output function. */
 1053         void            *outarg;        /* Argument for the output function. */
 1054         size_t          outsize;        /* Output size. */
 1055         TAILQ_ENTRY(note_info) link;    /* Link to the next note info. */
 1056 };
 1057 
 1058 TAILQ_HEAD(note_info_list, note_info);
 1059 
 1060 static void cb_put_phdr(vm_map_entry_t, void *);
 1061 static void cb_size_segment(vm_map_entry_t, void *);
 1062 static void each_writable_segment(struct thread *, segment_callback, void *);
 1063 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
 1064     int, void *, size_t, struct note_info_list *, size_t, gzFile);
 1065 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
 1066     size_t *);
 1067 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
 1068 static void __elfN(putnote)(struct note_info *, struct sbuf *);
 1069 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
 1070 static int sbuf_drain_core_output(void *, const char *, int);
 1071 static int sbuf_drain_count(void *arg, const char *data, int len);
 1072 
 1073 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
 1074 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
 1075 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
 1076 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
 1077 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
 1078 static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
 1079 static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
 1080 static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
 1081 static void note_procstat_files(void *, struct sbuf *, size_t *);
 1082 static void note_procstat_groups(void *, struct sbuf *, size_t *);
 1083 static void note_procstat_osrel(void *, struct sbuf *, size_t *);
 1084 static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
 1085 static void note_procstat_umask(void *, struct sbuf *, size_t *);
 1086 static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
 1087 
 1088 #ifdef COMPRESS_USER_CORES
 1089 extern int compress_user_cores;
 1090 extern int compress_user_cores_gzlevel;
 1091 #endif
 1092 
 1093 static int
 1094 core_output(struct vnode *vp, void *base, size_t len, off_t offset,
 1095     struct ucred *active_cred, struct ucred *file_cred,
 1096     struct thread *td, char *core_buf, gzFile gzfile) {
 1097 
 1098         int error;
 1099         if (gzfile) {
 1100 #ifdef COMPRESS_USER_CORES
 1101                 error = compress_core(gzfile, base, core_buf, len, td);
 1102 #else
 1103                 panic("shouldn't be here");
 1104 #endif
 1105         } else {
 1106                 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
 1107                     UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
 1108                     NULL, td);
 1109         }
 1110         return (error);
 1111 }
 1112 
 1113 /* Coredump output parameters for sbuf drain routine. */
 1114 struct sbuf_drain_core_params {
 1115         off_t           offset;
 1116         struct ucred    *active_cred;
 1117         struct ucred    *file_cred;
 1118         struct thread   *td;
 1119         struct vnode    *vp;
 1120 #ifdef COMPRESS_USER_CORES
 1121         gzFile          gzfile;
 1122 #endif
 1123 };
 1124 
 1125 /*
 1126  * Drain into a core file.
 1127  */
 1128 static int
 1129 sbuf_drain_core_output(void *arg, const char *data, int len)
 1130 {
 1131         struct sbuf_drain_core_params *p;
 1132         int error, locked;
 1133 
 1134         p = (struct sbuf_drain_core_params *)arg;
 1135 
 1136         /*
 1137          * Some kern_proc out routines that print to this sbuf may
 1138          * call us with the process lock held. Draining with the
 1139          * non-sleepable lock held is unsafe. The lock is needed for
 1140          * those routines when dumping a live process. In our case we
 1141          * can safely release the lock before draining and acquire
 1142          * again after.
 1143          */
 1144         locked = PROC_LOCKED(p->td->td_proc);
 1145         if (locked)
 1146                 PROC_UNLOCK(p->td->td_proc);
 1147 #ifdef COMPRESS_USER_CORES
 1148         if (p->gzfile != Z_NULL)
 1149                 error = compress_core(p->gzfile, NULL, __DECONST(char *, data),
 1150                     len, p->td);
 1151         else
 1152 #endif
 1153                 error = vn_rdwr_inchunks(UIO_WRITE, p->vp,
 1154                     __DECONST(void *, data), len, p->offset, UIO_SYSSPACE,
 1155                     IO_UNIT | IO_DIRECT, p->active_cred, p->file_cred, NULL,
 1156                     p->td);
 1157         if (locked)
 1158                 PROC_LOCK(p->td->td_proc);
 1159         if (error != 0)
 1160                 return (-error);
 1161         p->offset += len;
 1162         return (len);
 1163 }
 1164 
 1165 /*
 1166  * Drain into a counter.
 1167  */
 1168 static int
 1169 sbuf_drain_count(void *arg, const char *data __unused, int len)
 1170 {
 1171         size_t *sizep;
 1172 
 1173         sizep = (size_t *)arg;
 1174         *sizep += len;
 1175         return (len);
 1176 }
 1177 
 1178 int
 1179 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
 1180 {
 1181         struct ucred *cred = td->td_ucred;
 1182         int error = 0;
 1183         struct sseg_closure seginfo;
 1184         struct note_info_list notelst;
 1185         struct note_info *ninfo;
 1186         void *hdr;
 1187         size_t hdrsize, notesz, coresize;
 1188 
 1189         gzFile gzfile = Z_NULL;
 1190         char *core_buf = NULL;
 1191 #ifdef COMPRESS_USER_CORES
 1192         char gzopen_flags[8];
 1193         char *p;
 1194         int doing_compress = flags & IMGACT_CORE_COMPRESS;
 1195 #endif
 1196 
 1197         hdr = NULL;
 1198         TAILQ_INIT(&notelst);
 1199 
 1200 #ifdef COMPRESS_USER_CORES
 1201         if (doing_compress) {
 1202                 p = gzopen_flags;
 1203                 *p++ = 'w';
 1204                 if (compress_user_cores_gzlevel >= 0 &&
 1205                     compress_user_cores_gzlevel <= 9)
 1206                         *p++ = '' + compress_user_cores_gzlevel;
 1207                 *p = 0;
 1208                 gzfile = gz_open("", gzopen_flags, vp);
 1209                 if (gzfile == Z_NULL) {
 1210                         error = EFAULT;
 1211                         goto done;
 1212                 }
 1213                 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
 1214                 if (!core_buf) {
 1215                         error = ENOMEM;
 1216                         goto done;
 1217                 }
 1218         }
 1219 #endif
 1220 
 1221         /* Size the program segments. */
 1222         seginfo.count = 0;
 1223         seginfo.size = 0;
 1224         each_writable_segment(td, cb_size_segment, &seginfo);
 1225 
 1226         /*
 1227          * Collect info about the core file header area.
 1228          */
 1229         hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
 1230         __elfN(prepare_notes)(td, &notelst, &notesz);
 1231         coresize = round_page(hdrsize + notesz) + seginfo.size;
 1232 
 1233 #ifdef RACCT
 1234         if (racct_enable) {
 1235                 PROC_LOCK(td->td_proc);
 1236                 error = racct_add(td->td_proc, RACCT_CORE, coresize);
 1237                 PROC_UNLOCK(td->td_proc);
 1238                 if (error != 0) {
 1239                         error = EFAULT;
 1240                         goto done;
 1241                 }
 1242         }
 1243 #endif
 1244         if (coresize >= limit) {
 1245                 error = EFAULT;
 1246                 goto done;
 1247         }
 1248 
 1249         /*
 1250          * Allocate memory for building the header, fill it up,
 1251          * and write it out following the notes.
 1252          */
 1253         hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
 1254         if (hdr == NULL) {
 1255                 error = EINVAL;
 1256                 goto done;
 1257         }
 1258         error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
 1259             &notelst, notesz, gzfile);
 1260 
 1261         /* Write the contents of all of the writable segments. */
 1262         if (error == 0) {
 1263                 Elf_Phdr *php;
 1264                 off_t offset;
 1265                 int i;
 1266 
 1267                 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
 1268                 offset = round_page(hdrsize + notesz);
 1269                 for (i = 0; i < seginfo.count; i++) {
 1270                         error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
 1271                             php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
 1272                         if (error != 0)
 1273                                 break;
 1274                         offset += php->p_filesz;
 1275                         php++;
 1276                 }
 1277         }
 1278         if (error) {
 1279                 log(LOG_WARNING,
 1280                     "Failed to write core file for process %s (error %d)\n",
 1281                     curproc->p_comm, error);
 1282         }
 1283 
 1284 done:
 1285 #ifdef COMPRESS_USER_CORES
 1286         if (core_buf)
 1287                 free(core_buf, M_TEMP);
 1288         if (gzfile)
 1289                 gzclose(gzfile);
 1290 #endif
 1291         while ((ninfo = TAILQ_FIRST(&notelst)) != NULL) {
 1292                 TAILQ_REMOVE(&notelst, ninfo, link);
 1293                 free(ninfo, M_TEMP);
 1294         }
 1295         if (hdr != NULL)
 1296                 free(hdr, M_TEMP);
 1297 
 1298         return (error);
 1299 }
 1300 
 1301 /*
 1302  * A callback for each_writable_segment() to write out the segment's
 1303  * program header entry.
 1304  */
 1305 static void
 1306 cb_put_phdr(entry, closure)
 1307         vm_map_entry_t entry;
 1308         void *closure;
 1309 {
 1310         struct phdr_closure *phc = (struct phdr_closure *)closure;
 1311         Elf_Phdr *phdr = phc->phdr;
 1312 
 1313         phc->offset = round_page(phc->offset);
 1314 
 1315         phdr->p_type = PT_LOAD;
 1316         phdr->p_offset = phc->offset;
 1317         phdr->p_vaddr = entry->start;
 1318         phdr->p_paddr = 0;
 1319         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
 1320         phdr->p_align = PAGE_SIZE;
 1321         phdr->p_flags = __elfN(untrans_prot)(entry->protection);
 1322 
 1323         phc->offset += phdr->p_filesz;
 1324         phc->phdr++;
 1325 }
 1326 
 1327 /*
 1328  * A callback for each_writable_segment() to gather information about
 1329  * the number of segments and their total size.
 1330  */
 1331 static void
 1332 cb_size_segment(entry, closure)
 1333         vm_map_entry_t entry;
 1334         void *closure;
 1335 {
 1336         struct sseg_closure *ssc = (struct sseg_closure *)closure;
 1337 
 1338         ssc->count++;
 1339         ssc->size += entry->end - entry->start;
 1340 }
 1341 
 1342 /*
 1343  * For each writable segment in the process's memory map, call the given
 1344  * function with a pointer to the map entry and some arbitrary
 1345  * caller-supplied data.
 1346  */
 1347 static void
 1348 each_writable_segment(td, func, closure)
 1349         struct thread *td;
 1350         segment_callback func;
 1351         void *closure;
 1352 {
 1353         struct proc *p = td->td_proc;
 1354         vm_map_t map = &p->p_vmspace->vm_map;
 1355         vm_map_entry_t entry;
 1356         vm_object_t backing_object, object;
 1357         boolean_t ignore_entry;
 1358 
 1359         vm_map_lock_read(map);
 1360         for (entry = map->header.next; entry != &map->header;
 1361             entry = entry->next) {
 1362                 /*
 1363                  * Don't dump inaccessible mappings, deal with legacy
 1364                  * coredump mode.
 1365                  *
 1366                  * Note that read-only segments related to the elf binary
 1367                  * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
 1368                  * need to arbitrarily ignore such segments.
 1369                  */
 1370                 if (elf_legacy_coredump) {
 1371                         if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
 1372                                 continue;
 1373                 } else {
 1374                         if ((entry->protection & VM_PROT_ALL) == 0)
 1375                                 continue;
 1376                 }
 1377 
 1378                 /*
 1379                  * Dont include memory segment in the coredump if
 1380                  * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
 1381                  * madvise(2).  Do not dump submaps (i.e. parts of the
 1382                  * kernel map).
 1383                  */
 1384                 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
 1385                         continue;
 1386 
 1387                 if ((object = entry->object.vm_object) == NULL)
 1388                         continue;
 1389 
 1390                 /* Ignore memory-mapped devices and such things. */
 1391                 VM_OBJECT_RLOCK(object);
 1392                 while ((backing_object = object->backing_object) != NULL) {
 1393                         VM_OBJECT_RLOCK(backing_object);
 1394                         VM_OBJECT_RUNLOCK(object);
 1395                         object = backing_object;
 1396                 }
 1397                 ignore_entry = object->type != OBJT_DEFAULT &&
 1398                     object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
 1399                     object->type != OBJT_PHYS;
 1400                 VM_OBJECT_RUNLOCK(object);
 1401                 if (ignore_entry)
 1402                         continue;
 1403 
 1404                 (*func)(entry, closure);
 1405         }
 1406         vm_map_unlock_read(map);
 1407 }
 1408 
 1409 /*
 1410  * Write the core file header to the file, including padding up to
 1411  * the page boundary.
 1412  */
 1413 static int
 1414 __elfN(corehdr)(struct thread *td, struct vnode *vp, struct ucred *cred,
 1415     int numsegs, void *hdr, size_t hdrsize, struct note_info_list *notelst,
 1416     size_t notesz, gzFile gzfile)
 1417 {
 1418         struct sbuf_drain_core_params params;
 1419         struct note_info *ninfo;
 1420         struct sbuf *sb;
 1421         int error;
 1422 
 1423         /* Fill in the header. */
 1424         bzero(hdr, hdrsize);
 1425         __elfN(puthdr)(td, hdr, hdrsize, numsegs, notesz);
 1426 
 1427         params.offset = 0;
 1428         params.active_cred = cred;
 1429         params.file_cred = NOCRED;
 1430         params.td = td;
 1431         params.vp = vp;
 1432 #ifdef COMPRESS_USER_CORES
 1433         params.gzfile = gzfile;
 1434 #endif
 1435         sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
 1436         sbuf_set_drain(sb, sbuf_drain_core_output, &params);
 1437         sbuf_start_section(sb, NULL);
 1438         sbuf_bcat(sb, hdr, hdrsize);
 1439         TAILQ_FOREACH(ninfo, notelst, link)
 1440             __elfN(putnote)(ninfo, sb);
 1441         /* Align up to a page boundary for the program segments. */
 1442         sbuf_end_section(sb, -1, PAGE_SIZE, 0);
 1443         error = sbuf_finish(sb);
 1444         sbuf_delete(sb);
 1445 
 1446         return (error);
 1447 }
 1448 
 1449 static void
 1450 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
 1451     size_t *sizep)
 1452 {
 1453         struct proc *p;
 1454         struct thread *thr;
 1455         size_t size;
 1456 
 1457         p = td->td_proc;
 1458         size = 0;
 1459 
 1460         size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
 1461 
 1462         /*
 1463          * To have the debugger select the right thread (LWP) as the initial
 1464          * thread, we dump the state of the thread passed to us in td first.
 1465          * This is the thread that causes the core dump and thus likely to
 1466          * be the right thread one wants to have selected in the debugger.
 1467          */
 1468         thr = td;
 1469         while (thr != NULL) {
 1470                 size += register_note(list, NT_PRSTATUS,
 1471                     __elfN(note_prstatus), thr);
 1472                 size += register_note(list, NT_FPREGSET,
 1473                     __elfN(note_fpregset), thr);
 1474                 size += register_note(list, NT_THRMISC,
 1475                     __elfN(note_thrmisc), thr);
 1476                 size += register_note(list, -1,
 1477                     __elfN(note_threadmd), thr);
 1478 
 1479                 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
 1480                     TAILQ_NEXT(thr, td_plist);
 1481                 if (thr == td)
 1482                         thr = TAILQ_NEXT(thr, td_plist);
 1483         }
 1484 
 1485         size += register_note(list, NT_PROCSTAT_PROC,
 1486             __elfN(note_procstat_proc), p);
 1487         size += register_note(list, NT_PROCSTAT_FILES,
 1488             note_procstat_files, p);
 1489         size += register_note(list, NT_PROCSTAT_VMMAP,
 1490             note_procstat_vmmap, p);
 1491         size += register_note(list, NT_PROCSTAT_GROUPS,
 1492             note_procstat_groups, p);
 1493         size += register_note(list, NT_PROCSTAT_UMASK,
 1494             note_procstat_umask, p);
 1495         size += register_note(list, NT_PROCSTAT_RLIMIT,
 1496             note_procstat_rlimit, p);
 1497         size += register_note(list, NT_PROCSTAT_OSREL,
 1498             note_procstat_osrel, p);
 1499         size += register_note(list, NT_PROCSTAT_PSSTRINGS,
 1500             __elfN(note_procstat_psstrings), p);
 1501         size += register_note(list, NT_PROCSTAT_AUXV,
 1502             __elfN(note_procstat_auxv), p);
 1503 
 1504         *sizep = size;
 1505 }
 1506 
 1507 static void
 1508 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
 1509     size_t notesz)
 1510 {
 1511         Elf_Ehdr *ehdr;
 1512         Elf_Phdr *phdr;
 1513         struct phdr_closure phc;
 1514 
 1515         ehdr = (Elf_Ehdr *)hdr;
 1516         phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
 1517 
 1518         ehdr->e_ident[EI_MAG0] = ELFMAG0;
 1519         ehdr->e_ident[EI_MAG1] = ELFMAG1;
 1520         ehdr->e_ident[EI_MAG2] = ELFMAG2;
 1521         ehdr->e_ident[EI_MAG3] = ELFMAG3;
 1522         ehdr->e_ident[EI_CLASS] = ELF_CLASS;
 1523         ehdr->e_ident[EI_DATA] = ELF_DATA;
 1524         ehdr->e_ident[EI_VERSION] = EV_CURRENT;
 1525         ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
 1526         ehdr->e_ident[EI_ABIVERSION] = 0;
 1527         ehdr->e_ident[EI_PAD] = 0;
 1528         ehdr->e_type = ET_CORE;
 1529 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
 1530         ehdr->e_machine = ELF_ARCH32;
 1531 #else
 1532         ehdr->e_machine = ELF_ARCH;
 1533 #endif
 1534         ehdr->e_version = EV_CURRENT;
 1535         ehdr->e_entry = 0;
 1536         ehdr->e_phoff = sizeof(Elf_Ehdr);
 1537         ehdr->e_flags = 0;
 1538         ehdr->e_ehsize = sizeof(Elf_Ehdr);
 1539         ehdr->e_phentsize = sizeof(Elf_Phdr);
 1540         ehdr->e_phnum = numsegs + 1;
 1541         ehdr->e_shentsize = sizeof(Elf_Shdr);
 1542         ehdr->e_shnum = 0;
 1543         ehdr->e_shstrndx = SHN_UNDEF;
 1544 
 1545         /*
 1546          * Fill in the program header entries.
 1547          */
 1548 
 1549         /* The note segement. */
 1550         phdr->p_type = PT_NOTE;
 1551         phdr->p_offset = hdrsize;
 1552         phdr->p_vaddr = 0;
 1553         phdr->p_paddr = 0;
 1554         phdr->p_filesz = notesz;
 1555         phdr->p_memsz = 0;
 1556         phdr->p_flags = PF_R;
 1557         phdr->p_align = ELF_NOTE_ROUNDSIZE;
 1558         phdr++;
 1559 
 1560         /* All the writable segments from the program. */
 1561         phc.phdr = phdr;
 1562         phc.offset = round_page(hdrsize + notesz);
 1563         each_writable_segment(td, cb_put_phdr, &phc);
 1564 }
 1565 
 1566 static size_t
 1567 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
 1568 {
 1569         struct note_info *ninfo;
 1570         size_t size, notesize;
 1571 
 1572         size = 0;
 1573         out(arg, NULL, &size);
 1574         ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
 1575         ninfo->type = type;
 1576         ninfo->outfunc = out;
 1577         ninfo->outarg = arg;
 1578         ninfo->outsize = size;
 1579         TAILQ_INSERT_TAIL(list, ninfo, link);
 1580 
 1581         if (type == -1)
 1582                 return (size);
 1583 
 1584         notesize = sizeof(Elf_Note) +           /* note header */
 1585             roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
 1586                                                 /* note name */
 1587             roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
 1588 
 1589         return (notesize);
 1590 }
 1591 
 1592 static size_t
 1593 append_note_data(const void *src, void *dst, size_t len)
 1594 {
 1595         size_t padded_len;
 1596 
 1597         padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
 1598         if (dst != NULL) {
 1599                 bcopy(src, dst, len);
 1600                 bzero((char *)dst + len, padded_len - len);
 1601         }
 1602         return (padded_len);
 1603 }
 1604 
 1605 size_t
 1606 __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
 1607 {
 1608         Elf_Note *note;
 1609         char *buf;
 1610         size_t notesize;
 1611 
 1612         buf = dst;
 1613         if (buf != NULL) {
 1614                 note = (Elf_Note *)buf;
 1615                 note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
 1616                 note->n_descsz = size;
 1617                 note->n_type = type;
 1618                 buf += sizeof(*note);
 1619                 buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
 1620                     sizeof(FREEBSD_ABI_VENDOR));
 1621                 append_note_data(src, buf, size);
 1622                 if (descp != NULL)
 1623                         *descp = buf;
 1624         }
 1625 
 1626         notesize = sizeof(Elf_Note) +           /* note header */
 1627             roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
 1628                                                 /* note name */
 1629             roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
 1630 
 1631         return (notesize);
 1632 }
 1633 
 1634 static void
 1635 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
 1636 {
 1637         Elf_Note note;
 1638         ssize_t old_len;
 1639 
 1640         if (ninfo->type == -1) {
 1641                 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
 1642                 return;
 1643         }
 1644 
 1645         note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
 1646         note.n_descsz = ninfo->outsize;
 1647         note.n_type = ninfo->type;
 1648 
 1649         sbuf_bcat(sb, &note, sizeof(note));
 1650         sbuf_start_section(sb, &old_len);
 1651         sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
 1652         sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
 1653         if (note.n_descsz == 0)
 1654                 return;
 1655         sbuf_start_section(sb, &old_len);
 1656         ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
 1657         sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
 1658 }
 1659 
 1660 /*
 1661  * Miscellaneous note out functions.
 1662  */
 1663 
 1664 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
 1665 #include <compat/freebsd32/freebsd32.h>
 1666 
 1667 typedef struct prstatus32 elf_prstatus_t;
 1668 typedef struct prpsinfo32 elf_prpsinfo_t;
 1669 typedef struct fpreg32 elf_prfpregset_t;
 1670 typedef struct fpreg32 elf_fpregset_t;
 1671 typedef struct reg32 elf_gregset_t;
 1672 typedef struct thrmisc32 elf_thrmisc_t;
 1673 #define ELF_KERN_PROC_MASK      KERN_PROC_MASK32
 1674 typedef struct kinfo_proc32 elf_kinfo_proc_t;
 1675 typedef uint32_t elf_ps_strings_t;
 1676 #else
 1677 typedef prstatus_t elf_prstatus_t;
 1678 typedef prpsinfo_t elf_prpsinfo_t;
 1679 typedef prfpregset_t elf_prfpregset_t;
 1680 typedef prfpregset_t elf_fpregset_t;
 1681 typedef gregset_t elf_gregset_t;
 1682 typedef thrmisc_t elf_thrmisc_t;
 1683 #define ELF_KERN_PROC_MASK      0
 1684 typedef struct kinfo_proc elf_kinfo_proc_t;
 1685 typedef vm_offset_t elf_ps_strings_t;
 1686 #endif
 1687 
 1688 static void
 1689 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
 1690 {
 1691         struct proc *p;
 1692         elf_prpsinfo_t *psinfo;
 1693 
 1694         p = (struct proc *)arg;
 1695         if (sb != NULL) {
 1696                 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
 1697                 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
 1698                 psinfo->pr_version = PRPSINFO_VERSION;
 1699                 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
 1700                 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
 1701                 /*
 1702                  * XXX - We don't fill in the command line arguments properly
 1703                  * yet.
 1704                  */
 1705                 strlcpy(psinfo->pr_psargs, p->p_comm,
 1706                     sizeof(psinfo->pr_psargs));
 1707 
 1708                 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
 1709                 free(psinfo, M_TEMP);
 1710         }
 1711         *sizep = sizeof(*psinfo);
 1712 }
 1713 
 1714 static void
 1715 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
 1716 {
 1717         struct thread *td;
 1718         elf_prstatus_t *status;
 1719 
 1720         td = (struct thread *)arg;
 1721         if (sb != NULL) {
 1722                 KASSERT(*sizep == sizeof(*status), ("invalid size"));
 1723                 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
 1724                 status->pr_version = PRSTATUS_VERSION;
 1725                 status->pr_statussz = sizeof(elf_prstatus_t);
 1726                 status->pr_gregsetsz = sizeof(elf_gregset_t);
 1727                 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
 1728                 status->pr_osreldate = osreldate;
 1729                 status->pr_cursig = td->td_proc->p_sig;
 1730                 status->pr_pid = td->td_tid;
 1731 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
 1732                 fill_regs32(td, &status->pr_reg);
 1733 #else
 1734                 fill_regs(td, &status->pr_reg);
 1735 #endif
 1736                 sbuf_bcat(sb, status, sizeof(*status));
 1737                 free(status, M_TEMP);
 1738         }
 1739         *sizep = sizeof(*status);
 1740 }
 1741 
 1742 static void
 1743 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
 1744 {
 1745         struct thread *td;
 1746         elf_prfpregset_t *fpregset;
 1747 
 1748         td = (struct thread *)arg;
 1749         if (sb != NULL) {
 1750                 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
 1751                 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
 1752 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
 1753                 fill_fpregs32(td, fpregset);
 1754 #else
 1755                 fill_fpregs(td, fpregset);
 1756 #endif
 1757                 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
 1758                 free(fpregset, M_TEMP);
 1759         }
 1760         *sizep = sizeof(*fpregset);
 1761 }
 1762 
 1763 static void
 1764 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
 1765 {
 1766         struct thread *td;
 1767         elf_thrmisc_t thrmisc;
 1768 
 1769         td = (struct thread *)arg;
 1770         if (sb != NULL) {
 1771                 KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
 1772                 bzero(&thrmisc._pad, sizeof(thrmisc._pad));
 1773                 strcpy(thrmisc.pr_tname, td->td_name);
 1774                 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
 1775         }
 1776         *sizep = sizeof(thrmisc);
 1777 }
 1778 
 1779 /*
 1780  * Allow for MD specific notes, as well as any MD
 1781  * specific preparations for writing MI notes.
 1782  */
 1783 static void
 1784 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
 1785 {
 1786         struct thread *td;
 1787         void *buf;
 1788         size_t size;
 1789 
 1790         td = (struct thread *)arg;
 1791         size = *sizep;
 1792         if (size != 0 && sb != NULL)
 1793                 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
 1794         else
 1795                 buf = NULL;
 1796         size = 0;
 1797         __elfN(dump_thread)(td, buf, &size);
 1798         KASSERT(sb == NULL || *sizep == size, ("invalid size"));
 1799         if (size != 0 && sb != NULL)
 1800                 sbuf_bcat(sb, buf, size);
 1801         free(buf, M_TEMP);
 1802         *sizep = size;
 1803 }
 1804 
 1805 #ifdef KINFO_PROC_SIZE
 1806 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
 1807 #endif
 1808 
 1809 static void
 1810 __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
 1811 {
 1812         struct proc *p;
 1813         size_t size;
 1814         int structsize;
 1815 
 1816         p = (struct proc *)arg;
 1817         size = sizeof(structsize) + p->p_numthreads *
 1818             sizeof(elf_kinfo_proc_t);
 1819 
 1820         if (sb != NULL) {
 1821                 KASSERT(*sizep == size, ("invalid size"));
 1822                 structsize = sizeof(elf_kinfo_proc_t);
 1823                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1824                 PROC_LOCK(p);
 1825                 kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
 1826         }
 1827         *sizep = size;
 1828 }
 1829 
 1830 #ifdef KINFO_FILE_SIZE
 1831 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
 1832 #endif
 1833 
 1834 static void
 1835 note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
 1836 {
 1837         struct proc *p;
 1838         size_t size;
 1839         int structsize;
 1840 
 1841         p = (struct proc *)arg;
 1842         if (sb == NULL) {
 1843                 size = 0;
 1844                 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
 1845                 sbuf_set_drain(sb, sbuf_drain_count, &size);
 1846                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1847                 PROC_LOCK(p);
 1848                 kern_proc_filedesc_out(p, sb, -1);
 1849                 sbuf_finish(sb);
 1850                 sbuf_delete(sb);
 1851                 *sizep = size;
 1852         } else {
 1853                 structsize = sizeof(struct kinfo_file);
 1854                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1855                 PROC_LOCK(p);
 1856                 kern_proc_filedesc_out(p, sb, -1);
 1857         }
 1858 }
 1859 
 1860 #ifdef KINFO_VMENTRY_SIZE
 1861 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
 1862 #endif
 1863 
 1864 static void
 1865 note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
 1866 {
 1867         struct proc *p;
 1868         size_t size;
 1869         int structsize;
 1870 
 1871         p = (struct proc *)arg;
 1872         if (sb == NULL) {
 1873                 size = 0;
 1874                 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
 1875                 sbuf_set_drain(sb, sbuf_drain_count, &size);
 1876                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1877                 PROC_LOCK(p);
 1878                 kern_proc_vmmap_out(p, sb);
 1879                 sbuf_finish(sb);
 1880                 sbuf_delete(sb);
 1881                 *sizep = size;
 1882         } else {
 1883                 structsize = sizeof(struct kinfo_vmentry);
 1884                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1885                 PROC_LOCK(p);
 1886                 kern_proc_vmmap_out(p, sb);
 1887         }
 1888 }
 1889 
 1890 static void
 1891 note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
 1892 {
 1893         struct proc *p;
 1894         size_t size;
 1895         int structsize;
 1896 
 1897         p = (struct proc *)arg;
 1898         size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
 1899         if (sb != NULL) {
 1900                 KASSERT(*sizep == size, ("invalid size"));
 1901                 structsize = sizeof(gid_t);
 1902                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1903                 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
 1904                     sizeof(gid_t));
 1905         }
 1906         *sizep = size;
 1907 }
 1908 
 1909 static void
 1910 note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
 1911 {
 1912         struct proc *p;
 1913         size_t size;
 1914         int structsize;
 1915 
 1916         p = (struct proc *)arg;
 1917         size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
 1918         if (sb != NULL) {
 1919                 KASSERT(*sizep == size, ("invalid size"));
 1920                 structsize = sizeof(p->p_fd->fd_cmask);
 1921                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1922                 sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
 1923         }
 1924         *sizep = size;
 1925 }
 1926 
 1927 static void
 1928 note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
 1929 {
 1930         struct proc *p;
 1931         struct rlimit rlim[RLIM_NLIMITS];
 1932         size_t size;
 1933         int structsize, i;
 1934 
 1935         p = (struct proc *)arg;
 1936         size = sizeof(structsize) + sizeof(rlim);
 1937         if (sb != NULL) {
 1938                 KASSERT(*sizep == size, ("invalid size"));
 1939                 structsize = sizeof(rlim);
 1940                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1941                 PROC_LOCK(p);
 1942                 for (i = 0; i < RLIM_NLIMITS; i++)
 1943                         lim_rlimit(p, i, &rlim[i]);
 1944                 PROC_UNLOCK(p);
 1945                 sbuf_bcat(sb, rlim, sizeof(rlim));
 1946         }
 1947         *sizep = size;
 1948 }
 1949 
 1950 static void
 1951 note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
 1952 {
 1953         struct proc *p;
 1954         size_t size;
 1955         int structsize;
 1956 
 1957         p = (struct proc *)arg;
 1958         size = sizeof(structsize) + sizeof(p->p_osrel);
 1959         if (sb != NULL) {
 1960                 KASSERT(*sizep == size, ("invalid size"));
 1961                 structsize = sizeof(p->p_osrel);
 1962                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1963                 sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
 1964         }
 1965         *sizep = size;
 1966 }
 1967 
 1968 static void
 1969 __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
 1970 {
 1971         struct proc *p;
 1972         elf_ps_strings_t ps_strings;
 1973         size_t size;
 1974         int structsize;
 1975 
 1976         p = (struct proc *)arg;
 1977         size = sizeof(structsize) + sizeof(ps_strings);
 1978         if (sb != NULL) {
 1979                 KASSERT(*sizep == size, ("invalid size"));
 1980                 structsize = sizeof(ps_strings);
 1981 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
 1982                 ps_strings = PTROUT(p->p_sysent->sv_psstrings);
 1983 #else
 1984                 ps_strings = p->p_sysent->sv_psstrings;
 1985 #endif
 1986                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 1987                 sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
 1988         }
 1989         *sizep = size;
 1990 }
 1991 
 1992 static void
 1993 __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
 1994 {
 1995         struct proc *p;
 1996         size_t size;
 1997         int structsize;
 1998 
 1999         p = (struct proc *)arg;
 2000         if (sb == NULL) {
 2001                 size = 0;
 2002                 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
 2003                 sbuf_set_drain(sb, sbuf_drain_count, &size);
 2004                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 2005                 PHOLD(p);
 2006                 proc_getauxv(curthread, p, sb);
 2007                 PRELE(p);
 2008                 sbuf_finish(sb);
 2009                 sbuf_delete(sb);
 2010                 *sizep = size;
 2011         } else {
 2012                 structsize = sizeof(Elf_Auxinfo);
 2013                 sbuf_bcat(sb, &structsize, sizeof(structsize));
 2014                 PHOLD(p);
 2015                 proc_getauxv(curthread, p, sb);
 2016                 PRELE(p);
 2017         }
 2018 }
 2019 
 2020 static boolean_t
 2021 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
 2022     int32_t *osrel, const Elf_Phdr *pnote)
 2023 {
 2024         const Elf_Note *note, *note0, *note_end;
 2025         const char *note_name;
 2026         int i;
 2027 
 2028         if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
 2029             pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
 2030                 return (FALSE);
 2031 
 2032         note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
 2033         note_end = (const Elf_Note *)(imgp->image_header +
 2034             pnote->p_offset + pnote->p_filesz);
 2035         for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
 2036                 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
 2037                     (const char *)note < sizeof(Elf_Note))
 2038                         return (FALSE);
 2039                 if (note->n_namesz != checknote->hdr.n_namesz ||
 2040                     note->n_descsz != checknote->hdr.n_descsz ||
 2041                     note->n_type != checknote->hdr.n_type)
 2042                         goto nextnote;
 2043                 note_name = (const char *)(note + 1);
 2044                 if (note_name + checknote->hdr.n_namesz >=
 2045                     (const char *)note_end || strncmp(checknote->vendor,
 2046                     note_name, checknote->hdr.n_namesz) != 0)
 2047                         goto nextnote;
 2048 
 2049                 /*
 2050                  * Fetch the osreldate for binary
 2051                  * from the ELF OSABI-note if necessary.
 2052                  */
 2053                 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
 2054                     checknote->trans_osrel != NULL)
 2055                         return (checknote->trans_osrel(note, osrel));
 2056                 return (TRUE);
 2057 
 2058 nextnote:
 2059                 note = (const Elf_Note *)((const char *)(note + 1) +
 2060                     roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
 2061                     roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
 2062         }
 2063 
 2064         return (FALSE);
 2065 }
 2066 
 2067 /*
 2068  * Try to find the appropriate ABI-note section for checknote,
 2069  * fetch the osreldate for binary from the ELF OSABI-note. Only the
 2070  * first page of the image is searched, the same as for headers.
 2071  */
 2072 static boolean_t
 2073 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
 2074     int32_t *osrel)
 2075 {
 2076         const Elf_Phdr *phdr;
 2077         const Elf_Ehdr *hdr;
 2078         int i;
 2079 
 2080         hdr = (const Elf_Ehdr *)imgp->image_header;
 2081         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
 2082 
 2083         for (i = 0; i < hdr->e_phnum; i++) {
 2084                 if (phdr[i].p_type == PT_NOTE &&
 2085                     __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
 2086                         return (TRUE);
 2087         }
 2088         return (FALSE);
 2089 
 2090 }
 2091 
 2092 /*
 2093  * Tell kern_execve.c about it, with a little help from the linker.
 2094  */
 2095 static struct execsw __elfN(execsw) = {
 2096         __CONCAT(exec_, __elfN(imgact)),
 2097         __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
 2098 };
 2099 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
 2100 
 2101 #ifdef COMPRESS_USER_CORES
 2102 /*
 2103  * Compress and write out a core segment for a user process.
 2104  *
 2105  * 'inbuf' is the starting address of a VM segment in the process' address
 2106  * space that is to be compressed and written out to the core file.  'dest_buf'
 2107  * is a buffer in the kernel's address space.  The segment is copied from 
 2108  * 'inbuf' to 'dest_buf' first before being processed by the compression
 2109  * routine gzwrite().  This copying is necessary because the content of the VM
 2110  * segment may change between the compression pass and the crc-computation pass
 2111  * in gzwrite().  This is because realtime threads may preempt the UNIX kernel.
 2112  *
 2113  * If inbuf is NULL it is assumed that data is already copied to 'dest_buf'.
 2114  */
 2115 static int
 2116 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
 2117     struct thread *td)
 2118 {
 2119         int len_compressed;
 2120         int error = 0;
 2121         unsigned int chunk_len;
 2122 
 2123         while (len) {
 2124                 if (inbuf != NULL) {
 2125                         chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
 2126                         copyin(inbuf, dest_buf, chunk_len);
 2127                         inbuf += chunk_len;
 2128                 } else {
 2129                         chunk_len = len;
 2130                 }
 2131                 len_compressed = gzwrite(file, dest_buf, chunk_len);
 2132 
 2133                 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
 2134 
 2135                 if ((unsigned int)len_compressed != chunk_len) {
 2136                         log(LOG_WARNING,
 2137                             "compress_core: length mismatch (0x%x returned, "
 2138                             "0x%x expected)\n", len_compressed, chunk_len);
 2139                         EVENTHANDLER_INVOKE(app_coredump_error, td,
 2140                             "compress_core: length mismatch %x -> %x",
 2141                             chunk_len, len_compressed);
 2142                         error = EFAULT;
 2143                         break;
 2144                 }
 2145                 len -= chunk_len;
 2146                 maybe_yield();
 2147         }
 2148 
 2149         return (error);
 2150 }
 2151 #endif /* COMPRESS_USER_CORES */
 2152 
 2153 static vm_prot_t
 2154 __elfN(trans_prot)(Elf_Word flags)
 2155 {
 2156         vm_prot_t prot;
 2157 
 2158         prot = 0;
 2159         if (flags & PF_X)
 2160                 prot |= VM_PROT_EXECUTE;
 2161         if (flags & PF_W)
 2162                 prot |= VM_PROT_WRITE;
 2163         if (flags & PF_R)
 2164                 prot |= VM_PROT_READ;
 2165 #if __ELF_WORD_SIZE == 32
 2166 #if defined(__amd64__) || defined(__ia64__)
 2167         if (i386_read_exec && (flags & PF_R))
 2168                 prot |= VM_PROT_EXECUTE;
 2169 #endif
 2170 #endif
 2171         return (prot);
 2172 }
 2173 
 2174 static Elf_Word
 2175 __elfN(untrans_prot)(vm_prot_t prot)
 2176 {
 2177         Elf_Word flags;
 2178 
 2179         flags = 0;
 2180         if (prot & VM_PROT_EXECUTE)
 2181                 flags |= PF_X;
 2182         if (prot & VM_PROT_READ)
 2183                 flags |= PF_R;
 2184         if (prot & VM_PROT_WRITE)
 2185                 flags |= PF_W;
 2186         return (flags);
 2187 }

Cache object: 62c43575722f0aa202a8fe3dca652e92


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