2 * Copyright (c) 2000 David O'Brien
3 * Copyright (c) 1995-1996 Søren Schmidt
4 * Copyright (c) 1996 Peter Wemm
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_capsicum.h"
35 #include "opt_compat.h"
38 #include <sys/param.h>
39 #include <sys/capability.h>
41 #include <sys/fcntl.h>
42 #include <sys/imgact.h>
43 #include <sys/imgact_elf.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
47 #include <sys/mount.h>
48 #include <sys/mutex.h>
50 #include <sys/namei.h>
51 #include <sys/pioctl.h>
53 #include <sys/procfs.h>
54 #include <sys/racct.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sf_buf.h>
58 #include <sys/systm.h>
59 #include <sys/signalvar.h>
62 #include <sys/syscall.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/vnode.h>
66 #include <sys/syslog.h>
67 #include <sys/eventhandler.h>
72 #include <vm/vm_kern.h>
73 #include <vm/vm_param.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_extern.h>
79 #include <machine/elf.h>
80 #include <machine/md_var.h>
82 #define OLD_EI_BRAND 8
84 static int __elfN(check_header)(const Elf_Ehdr *hdr);
85 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
86 const char *interp, int interp_name_len, int32_t *osrel);
87 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
88 u_long *entry, size_t pagesize);
89 static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
90 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
92 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
93 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
95 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
96 static boolean_t __elfN(check_note)(struct image_params *imgp,
97 Elf_Brandnote *checknote, int32_t *osrel);
98 static vm_prot_t __elfN(trans_prot)(Elf_Word);
99 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
101 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
104 #ifdef COMPRESS_USER_CORES
105 static int compress_core(gzFile, char *, char *, unsigned int,
107 #define CORE_BUF_SIZE (16 * 1024)
110 int __elfN(fallback_brand) = -1;
111 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
112 fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
113 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
114 TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand",
115 &__elfN(fallback_brand));
117 static int elf_legacy_coredump = 0;
118 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
119 &elf_legacy_coredump, 0, "");
121 int __elfN(nxstack) =
122 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
127 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
128 nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
129 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
131 #if __ELF_WORD_SIZE == 32
132 #if defined(__amd64__) || defined(__ia64__)
133 int i386_read_exec = 0;
134 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
135 "enable execution from readable segments");
139 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
141 #define trunc_page_ps(va, ps) ((va) & ~(ps - 1))
142 #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1))
143 #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
145 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
147 Elf_Brandnote __elfN(freebsd_brandnote) = {
148 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
149 .hdr.n_descsz = sizeof(int32_t),
151 .vendor = FREEBSD_ABI_VENDOR,
152 .flags = BN_TRANSLATE_OSREL,
153 .trans_osrel = __elfN(freebsd_trans_osrel)
157 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
161 p = (uintptr_t)(note + 1);
162 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
163 *osrel = *(const int32_t *)(p);
168 static const char GNU_ABI_VENDOR[] = "GNU";
169 static int GNU_KFREEBSD_ABI_DESC = 3;
171 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
172 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
173 .hdr.n_descsz = 16, /* XXX at least 16 */
175 .vendor = GNU_ABI_VENDOR,
176 .flags = BN_TRANSLATE_OSREL,
177 .trans_osrel = kfreebsd_trans_osrel
181 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
183 const Elf32_Word *desc;
186 p = (uintptr_t)(note + 1);
187 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
189 desc = (const Elf32_Word *)p;
190 if (desc[0] != GNU_KFREEBSD_ABI_DESC)
194 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
195 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
197 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
203 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
207 for (i = 0; i < MAX_BRANDS; i++) {
208 if (elf_brand_list[i] == NULL) {
209 elf_brand_list[i] = entry;
213 if (i == MAX_BRANDS) {
214 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
222 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
226 for (i = 0; i < MAX_BRANDS; i++) {
227 if (elf_brand_list[i] == entry) {
228 elf_brand_list[i] = NULL;
238 __elfN(brand_inuse)(Elf_Brandinfo *entry)
243 sx_slock(&allproc_lock);
244 FOREACH_PROC_IN_SYSTEM(p) {
245 if (p->p_sysent == entry->sysvec) {
250 sx_sunlock(&allproc_lock);
255 static Elf_Brandinfo *
256 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
257 int interp_name_len, int32_t *osrel)
259 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
265 * We support four types of branding -- (1) the ELF EI_OSABI field
266 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
267 * branding w/in the ELF header, (3) path of the `interp_path'
268 * field, and (4) the ".note.ABI-tag" ELF section.
271 /* Look for an ".note.ABI-tag" ELF section */
272 for (i = 0; i < MAX_BRANDS; i++) {
273 bi = elf_brand_list[i];
276 if (hdr->e_machine == bi->machine && (bi->flags &
277 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
278 ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
284 /* If the executable has a brand, search for it in the brand list. */
285 for (i = 0; i < MAX_BRANDS; i++) {
286 bi = elf_brand_list[i];
287 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
289 if (hdr->e_machine == bi->machine &&
290 (hdr->e_ident[EI_OSABI] == bi->brand ||
291 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
292 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
296 /* Lacking a known brand, search for a recognized interpreter. */
297 if (interp != NULL) {
298 for (i = 0; i < MAX_BRANDS; i++) {
299 bi = elf_brand_list[i];
300 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
302 if (hdr->e_machine == bi->machine &&
303 /* ELF image p_filesz includes terminating zero */
304 strlen(bi->interp_path) + 1 == interp_name_len &&
305 strncmp(interp, bi->interp_path, interp_name_len)
311 /* Lacking a recognized interpreter, try the default brand */
312 for (i = 0; i < MAX_BRANDS; i++) {
313 bi = elf_brand_list[i];
314 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
316 if (hdr->e_machine == bi->machine &&
317 __elfN(fallback_brand) == bi->brand)
324 __elfN(check_header)(const Elf_Ehdr *hdr)
330 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
331 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
332 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
333 hdr->e_phentsize != sizeof(Elf_Phdr) ||
334 hdr->e_version != ELF_TARG_VER)
338 * Make sure we have at least one brand for this machine.
341 for (i = 0; i < MAX_BRANDS; i++) {
342 bi = elf_brand_list[i];
343 if (bi != NULL && bi->machine == hdr->e_machine)
353 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
354 vm_offset_t start, vm_offset_t end, vm_prot_t prot)
361 * Create the page if it doesn't exist yet. Ignore errors.
364 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
365 VM_PROT_ALL, VM_PROT_ALL, 0);
369 * Find the page from the underlying object.
372 sf = vm_imgact_map_page(object, offset);
374 return (KERN_FAILURE);
375 off = offset - trunc_page(offset);
376 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
378 vm_imgact_unmap_page(sf);
380 return (KERN_FAILURE);
384 return (KERN_SUCCESS);
388 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
389 vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
396 if (start != trunc_page(start)) {
397 rv = __elfN(map_partial)(map, object, offset, start,
398 round_page(start), prot);
401 offset += round_page(start) - start;
402 start = round_page(start);
404 if (end != round_page(end)) {
405 rv = __elfN(map_partial)(map, object, offset +
406 trunc_page(end) - start, trunc_page(end), end, prot);
409 end = trunc_page(end);
412 if (offset & PAGE_MASK) {
414 * The mapping is not page aligned. This means we have
415 * to copy the data. Sigh.
417 rv = vm_map_find(map, NULL, 0, &start, end - start,
418 FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0);
422 return (KERN_SUCCESS);
423 for (; start < end; start += sz) {
424 sf = vm_imgact_map_page(object, offset);
426 return (KERN_FAILURE);
427 off = offset - trunc_page(offset);
429 if (sz > PAGE_SIZE - off)
430 sz = PAGE_SIZE - off;
431 error = copyout((caddr_t)sf_buf_kva(sf) + off,
433 vm_imgact_unmap_page(sf);
435 return (KERN_FAILURE);
441 vm_object_reference(object);
443 rv = vm_map_insert(map, object, offset, start, end,
444 prot, VM_PROT_ALL, cow);
446 if (rv != KERN_SUCCESS)
447 vm_object_deallocate(object);
451 return (KERN_SUCCESS);
456 __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
457 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
464 vm_offset_t map_addr;
467 vm_offset_t file_addr;
470 * It's necessary to fail if the filsz + offset taken from the
471 * header is greater than the actual file pager object's size.
472 * If we were to allow this, then the vm_map_find() below would
473 * walk right off the end of the file object and into the ether.
475 * While I'm here, might as well check for something else that
476 * is invalid: filsz cannot be greater than memsz.
478 if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
479 uprintf("elf_load_section: truncated ELF file\n");
483 object = imgp->object;
484 map = &imgp->proc->p_vmspace->vm_map;
485 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
486 file_addr = trunc_page_ps(offset, pagesize);
489 * We have two choices. We can either clear the data in the last page
490 * of an oversized mapping, or we can start the anon mapping a page
491 * early and copy the initialized data into that first page. We
492 * choose the second..
495 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
497 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
500 /* cow flags: don't dump readonly sections in core */
501 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
502 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
504 rv = __elfN(map_insert)(map,
506 file_addr, /* file offset */
507 map_addr, /* virtual start */
508 map_addr + map_len,/* virtual end */
511 if (rv != KERN_SUCCESS)
514 /* we can stop now if we've covered it all */
515 if (memsz == filsz) {
522 * We have to get the remaining bit of the file into the first part
523 * of the oversized map segment. This is normally because the .data
524 * segment in the file is extended to provide bss. It's a neat idea
525 * to try and save a page, but it's a pain in the behind to implement.
527 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
528 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
529 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
532 /* This had damn well better be true! */
534 rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
535 map_len, VM_PROT_ALL, 0);
536 if (rv != KERN_SUCCESS) {
544 sf = vm_imgact_map_page(object, offset + filsz);
548 /* send the page fragment to user space */
549 off = trunc_page_ps(offset + filsz, pagesize) -
550 trunc_page(offset + filsz);
551 error = copyout((caddr_t)sf_buf_kva(sf) + off,
552 (caddr_t)map_addr, copy_len);
553 vm_imgact_unmap_page(sf);
560 * set it to the specified protection.
561 * XXX had better undo the damage from pasting over the cracks here!
563 vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
564 map_len), prot, FALSE);
570 * Load the file "file" into memory. It may be either a shared object
573 * The "addr" reference parameter is in/out. On entry, it specifies
574 * the address where a shared object should be loaded. If the file is
575 * an executable, this value is ignored. On exit, "addr" specifies
576 * where the file was actually loaded.
578 * The "entry" reference parameter is out only. On exit, it specifies
579 * the entry point for the loaded file.
582 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
583 u_long *entry, size_t pagesize)
588 struct image_params image_params;
590 const Elf_Ehdr *hdr = NULL;
591 const Elf_Phdr *phdr = NULL;
592 struct nameidata *nd;
594 struct image_params *imgp;
597 u_long base_addr = 0;
598 int error, i, numsegs;
600 #ifdef CAPABILITY_MODE
602 * XXXJA: This check can go away once we are sufficiently confident
603 * that the checks in namei() are correct.
605 if (IN_CAPABILITY_MODE(curthread))
609 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
611 attr = &tempdata->attr;
612 imgp = &tempdata->image_params;
615 * Initialize part of the common data
619 imgp->firstpage = NULL;
620 imgp->image_header = NULL;
622 imgp->execlabel = NULL;
624 NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
625 if ((error = namei(nd)) != 0) {
629 NDFREE(nd, NDF_ONLY_PNBUF);
630 imgp->vp = nd->ni_vp;
633 * Check permissions, modes, uid, etc on the file, and "open" it.
635 error = exec_check_permissions(imgp);
639 error = exec_map_first_page(imgp);
644 * Also make certain that the interpreter stays the same, so set
645 * its VV_TEXT flag, too.
647 VOP_SET_TEXT(nd->ni_vp);
649 imgp->object = nd->ni_vp->v_object;
651 hdr = (const Elf_Ehdr *)imgp->image_header;
652 if ((error = __elfN(check_header)(hdr)) != 0)
654 if (hdr->e_type == ET_DYN)
656 else if (hdr->e_type == ET_EXEC)
663 /* Only support headers that fit within first page for now */
664 /* (multiplication of two Elf_Half fields will not overflow) */
665 if ((hdr->e_phoff > PAGE_SIZE) ||
666 (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
671 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
672 if (!aligned(phdr, Elf_Addr)) {
677 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
678 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
679 /* Loadable segment */
680 prot = __elfN(trans_prot)(phdr[i].p_flags);
681 error = __elfN(load_section)(imgp, phdr[i].p_offset,
682 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
683 phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
687 * Establish the base address if this is the
691 base_addr = trunc_page(phdr[i].p_vaddr +
697 *entry = (unsigned long)hdr->e_entry + rbase;
701 exec_unmap_first_page(imgp);
706 free(tempdata, M_TEMP);
712 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
714 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
715 const Elf_Phdr *phdr;
716 Elf_Auxargs *elf_auxargs;
717 struct vmspace *vmspace;
719 u_long text_size = 0, data_size = 0, total_size = 0;
720 u_long text_addr = 0, data_addr = 0;
721 u_long seg_size, seg_addr;
722 u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
724 int error = 0, i, n, interp_name_len = 0;
725 const char *interp = NULL, *newinterp = NULL;
726 Elf_Brandinfo *brand_info;
728 struct sysentvec *sv;
731 * Do we have a valid ELF header ?
733 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
734 * if particular brand doesn't support it.
736 if (__elfN(check_header)(hdr) != 0 ||
737 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
741 * From here on down, we return an errno, not -1, as we've
742 * detected an ELF file.
745 if ((hdr->e_phoff > PAGE_SIZE) ||
746 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
747 /* Only support headers in first page for now */
750 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
751 if (!aligned(phdr, Elf_Addr))
755 for (i = 0; i < hdr->e_phnum; i++) {
756 switch (phdr[i].p_type) {
759 baddr = phdr[i].p_vaddr;
763 /* Path to interpreter */
764 if (phdr[i].p_filesz > MAXPATHLEN ||
765 phdr[i].p_offset >= PAGE_SIZE ||
766 phdr[i].p_offset + phdr[i].p_filesz >= PAGE_SIZE)
768 interp = imgp->image_header + phdr[i].p_offset;
769 interp_name_len = phdr[i].p_filesz;
774 __elfN(trans_prot)(phdr[i].p_flags);
779 brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
781 if (brand_info == NULL) {
782 uprintf("ELF binary type \"%u\" not known.\n",
783 hdr->e_ident[EI_OSABI]);
786 if (hdr->e_type == ET_DYN) {
787 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
790 * Honour the base load address from the dso if it is
791 * non-zero for some reason.
794 et_dyn_addr = ET_DYN_LOAD_ADDR;
799 sv = brand_info->sysvec;
800 if (interp != NULL && brand_info->interp_newpath != NULL)
801 newinterp = brand_info->interp_newpath;
804 * Avoid a possible deadlock if the current address space is destroyed
805 * and that address space maps the locked vnode. In the common case,
806 * the locked vnode's v_usecount is decremented but remains greater
807 * than zero. Consequently, the vnode lock is not needed by vrele().
808 * However, in cases where the vnode lock is external, such as nullfs,
809 * v_usecount may become zero.
811 * The VV_TEXT flag prevents modifications to the executable while
812 * the vnode is unlocked.
814 VOP_UNLOCK(imgp->vp, 0);
816 error = exec_new_vmspace(imgp, sv);
817 imgp->proc->p_sysent = sv;
819 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
823 for (i = 0; i < hdr->e_phnum; i++) {
824 switch (phdr[i].p_type) {
825 case PT_LOAD: /* Loadable segment */
826 if (phdr[i].p_memsz == 0)
828 prot = __elfN(trans_prot)(phdr[i].p_flags);
830 #if defined(__ia64__) && __ELF_WORD_SIZE == 32 && defined(IA32_ME_HARDER)
832 * Some x86 binaries assume read == executable,
833 * notably the M3 runtime and therefore cvsup
835 if (prot & VM_PROT_READ)
836 prot |= VM_PROT_EXECUTE;
839 error = __elfN(load_section)(imgp, phdr[i].p_offset,
840 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
841 phdr[i].p_memsz, phdr[i].p_filesz, prot,
847 * If this segment contains the program headers,
848 * remember their virtual address for the AT_PHDR
849 * aux entry. Static binaries don't usually include
852 if (phdr[i].p_offset == 0 &&
853 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
855 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
858 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
859 seg_size = round_page(phdr[i].p_memsz +
860 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
863 * Make the largest executable segment the official
864 * text segment and all others data.
866 * Note that obreak() assumes that data_addr +
867 * data_size == end of data load area, and the ELF
868 * file format expects segments to be sorted by
869 * address. If multiple data segments exist, the
870 * last one will be used.
873 if (phdr[i].p_flags & PF_X && text_size < seg_size) {
874 text_size = seg_size;
875 text_addr = seg_addr;
877 data_size = seg_size;
878 data_addr = seg_addr;
880 total_size += seg_size;
882 case PT_PHDR: /* Program header table info */
883 proghdr = phdr[i].p_vaddr + et_dyn_addr;
890 if (data_addr == 0 && data_size == 0) {
891 data_addr = text_addr;
892 data_size = text_size;
895 entry = (u_long)hdr->e_entry + et_dyn_addr;
898 * Check limits. It should be safe to check the
899 * limits after loading the segments since we do
900 * not actually fault in all the segments pages.
902 PROC_LOCK(imgp->proc);
903 if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) ||
904 text_size > maxtsiz ||
905 total_size > lim_cur(imgp->proc, RLIMIT_VMEM) ||
906 racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
907 racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
908 PROC_UNLOCK(imgp->proc);
912 vmspace = imgp->proc->p_vmspace;
913 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
914 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
915 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
916 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
919 * We load the dynamic linker where a userland call
920 * to mmap(0, ...) would put it. The rationale behind this
921 * calculation is that it leaves room for the heap to grow to
922 * its maximum allowed size.
924 addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(imgp->proc,
926 PROC_UNLOCK(imgp->proc);
928 imgp->entry_addr = entry;
930 if (interp != NULL) {
931 int have_interp = FALSE;
932 VOP_UNLOCK(imgp->vp, 0);
933 if (brand_info->emul_path != NULL &&
934 brand_info->emul_path[0] != '\0') {
935 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
936 snprintf(path, MAXPATHLEN, "%s%s",
937 brand_info->emul_path, interp);
938 error = __elfN(load_file)(imgp->proc, path, &addr,
939 &imgp->entry_addr, sv->sv_pagesize);
944 if (!have_interp && newinterp != NULL) {
945 error = __elfN(load_file)(imgp->proc, newinterp, &addr,
946 &imgp->entry_addr, sv->sv_pagesize);
951 error = __elfN(load_file)(imgp->proc, interp, &addr,
952 &imgp->entry_addr, sv->sv_pagesize);
954 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
956 uprintf("ELF interpreter %s not found\n", interp);
963 * Construct auxargs table (used by the fixup routine)
965 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
966 elf_auxargs->execfd = -1;
967 elf_auxargs->phdr = proghdr;
968 elf_auxargs->phent = hdr->e_phentsize;
969 elf_auxargs->phnum = hdr->e_phnum;
970 elf_auxargs->pagesz = PAGE_SIZE;
971 elf_auxargs->base = addr;
972 elf_auxargs->flags = 0;
973 elf_auxargs->entry = entry;
975 imgp->auxargs = elf_auxargs;
976 imgp->interpreted = 0;
977 imgp->reloc_base = addr;
978 imgp->proc->p_osrel = osrel;
983 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
986 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
988 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
992 base = (Elf_Addr *)*stack_base;
993 pos = base + (imgp->args->argc + imgp->args->envc + 2);
995 if (args->execfd != -1)
996 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
997 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
998 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
999 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1000 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1001 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1002 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1003 AUXARGS_ENTRY(pos, AT_BASE, args->base);
1004 if (imgp->execpathp != 0)
1005 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1006 AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
1007 if (imgp->canary != 0) {
1008 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1009 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1011 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1012 if (imgp->pagesizes != 0) {
1013 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1014 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1016 if (imgp->sysent->sv_timekeep_base != 0) {
1017 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1018 imgp->sysent->sv_timekeep_base);
1020 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1021 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1022 imgp->sysent->sv_stackprot);
1023 AUXARGS_ENTRY(pos, AT_NULL, 0);
1025 free(imgp->auxargs, M_TEMP);
1026 imgp->auxargs = NULL;
1029 suword(base, (long)imgp->args->argc);
1030 *stack_base = (register_t *)base;
1035 * Code for generating ELF core dumps.
1038 typedef void (*segment_callback)(vm_map_entry_t, void *);
1040 /* Closure for cb_put_phdr(). */
1041 struct phdr_closure {
1042 Elf_Phdr *phdr; /* Program header to fill in */
1043 Elf_Off offset; /* Offset of segment in core file */
1046 /* Closure for cb_size_segment(). */
1047 struct sseg_closure {
1048 int count; /* Count of writable segments. */
1049 size_t size; /* Total size of all writable segments. */
1052 static void cb_put_phdr(vm_map_entry_t, void *);
1053 static void cb_size_segment(vm_map_entry_t, void *);
1054 static void each_writable_segment(struct thread *, segment_callback, void *);
1055 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
1056 int, void *, size_t, gzFile);
1057 static void __elfN(puthdr)(struct thread *, void *, size_t *, int);
1058 static void __elfN(putnote)(void *, size_t *, const char *, int,
1059 const void *, size_t);
1061 #ifdef COMPRESS_USER_CORES
1062 extern int compress_user_cores;
1063 extern int compress_user_cores_gzlevel;
1067 core_output(struct vnode *vp, void *base, size_t len, off_t offset,
1068 struct ucred *active_cred, struct ucred *file_cred,
1069 struct thread *td, char *core_buf, gzFile gzfile) {
1073 #ifdef COMPRESS_USER_CORES
1074 error = compress_core(gzfile, base, core_buf, len, td);
1076 panic("shouldn't be here");
1079 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
1080 UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1087 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1089 struct ucred *cred = td->td_ucred;
1091 struct sseg_closure seginfo;
1095 gzFile gzfile = Z_NULL;
1096 char *core_buf = NULL;
1097 #ifdef COMPRESS_USER_CORES
1098 char gzopen_flags[8];
1100 int doing_compress = flags & IMGACT_CORE_COMPRESS;
1105 #ifdef COMPRESS_USER_CORES
1106 if (doing_compress) {
1109 if (compress_user_cores_gzlevel >= 0 &&
1110 compress_user_cores_gzlevel <= 9)
1111 *p++ = '0' + compress_user_cores_gzlevel;
1113 gzfile = gz_open("", gzopen_flags, vp);
1114 if (gzfile == Z_NULL) {
1118 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1126 /* Size the program segments. */
1129 each_writable_segment(td, cb_size_segment, &seginfo);
1132 * Calculate the size of the core file header area by making
1133 * a dry run of generating it. Nothing is written, but the
1134 * size is calculated.
1137 __elfN(puthdr)(td, (void *)NULL, &hdrsize, seginfo.count);
1140 PROC_LOCK(td->td_proc);
1141 error = racct_add(td->td_proc, RACCT_CORE, hdrsize + seginfo.size);
1142 PROC_UNLOCK(td->td_proc);
1148 if (hdrsize + seginfo.size >= limit) {
1154 * Allocate memory for building the header, fill it up,
1157 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1162 error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
1165 /* Write the contents of all of the writable segments. */
1171 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1173 for (i = 0; i < seginfo.count; i++) {
1174 error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1175 php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
1178 offset += php->p_filesz;
1184 "Failed to write core file for process %s (error %d)\n",
1185 curproc->p_comm, error);
1189 #ifdef COMPRESS_USER_CORES
1191 free(core_buf, M_TEMP);
1202 * A callback for each_writable_segment() to write out the segment's
1203 * program header entry.
1206 cb_put_phdr(entry, closure)
1207 vm_map_entry_t entry;
1210 struct phdr_closure *phc = (struct phdr_closure *)closure;
1211 Elf_Phdr *phdr = phc->phdr;
1213 phc->offset = round_page(phc->offset);
1215 phdr->p_type = PT_LOAD;
1216 phdr->p_offset = phc->offset;
1217 phdr->p_vaddr = entry->start;
1219 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1220 phdr->p_align = PAGE_SIZE;
1221 phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1223 phc->offset += phdr->p_filesz;
1228 * A callback for each_writable_segment() to gather information about
1229 * the number of segments and their total size.
1232 cb_size_segment(entry, closure)
1233 vm_map_entry_t entry;
1236 struct sseg_closure *ssc = (struct sseg_closure *)closure;
1239 ssc->size += entry->end - entry->start;
1243 * For each writable segment in the process's memory map, call the given
1244 * function with a pointer to the map entry and some arbitrary
1245 * caller-supplied data.
1248 each_writable_segment(td, func, closure)
1250 segment_callback func;
1253 struct proc *p = td->td_proc;
1254 vm_map_t map = &p->p_vmspace->vm_map;
1255 vm_map_entry_t entry;
1256 vm_object_t backing_object, object;
1257 boolean_t ignore_entry;
1259 vm_map_lock_read(map);
1260 for (entry = map->header.next; entry != &map->header;
1261 entry = entry->next) {
1263 * Don't dump inaccessible mappings, deal with legacy
1266 * Note that read-only segments related to the elf binary
1267 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1268 * need to arbitrarily ignore such segments.
1270 if (elf_legacy_coredump) {
1271 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1274 if ((entry->protection & VM_PROT_ALL) == 0)
1279 * Dont include memory segment in the coredump if
1280 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1281 * madvise(2). Do not dump submaps (i.e. parts of the
1284 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1287 if ((object = entry->object.vm_object) == NULL)
1290 /* Ignore memory-mapped devices and such things. */
1291 VM_OBJECT_LOCK(object);
1292 while ((backing_object = object->backing_object) != NULL) {
1293 VM_OBJECT_LOCK(backing_object);
1294 VM_OBJECT_UNLOCK(object);
1295 object = backing_object;
1297 ignore_entry = object->type != OBJT_DEFAULT &&
1298 object->type != OBJT_SWAP && object->type != OBJT_VNODE;
1299 VM_OBJECT_UNLOCK(object);
1303 (*func)(entry, closure);
1305 vm_map_unlock_read(map);
1309 * Write the core file header to the file, including padding up to
1310 * the page boundary.
1313 __elfN(corehdr)(td, vp, cred, numsegs, hdr, hdrsize, gzfile)
1324 /* Fill in the header. */
1325 bzero(hdr, hdrsize);
1327 __elfN(puthdr)(td, hdr, &off, numsegs);
1330 /* Write it to the core file. */
1331 return (vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
1332 UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NOCRED, NULL,
1335 #ifdef COMPRESS_USER_CORES
1336 if (gzwrite(gzfile, hdr, hdrsize) != hdrsize) {
1338 "Failed to compress core file header for process"
1339 " %s.\n", curproc->p_comm);
1346 panic("shouldn't be here");
1351 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1352 #include <compat/freebsd32/freebsd32.h>
1354 typedef struct prstatus32 elf_prstatus_t;
1355 typedef struct prpsinfo32 elf_prpsinfo_t;
1356 typedef struct fpreg32 elf_prfpregset_t;
1357 typedef struct fpreg32 elf_fpregset_t;
1358 typedef struct reg32 elf_gregset_t;
1359 typedef struct thrmisc32 elf_thrmisc_t;
1361 typedef prstatus_t elf_prstatus_t;
1362 typedef prpsinfo_t elf_prpsinfo_t;
1363 typedef prfpregset_t elf_prfpregset_t;
1364 typedef prfpregset_t elf_fpregset_t;
1365 typedef gregset_t elf_gregset_t;
1366 typedef thrmisc_t elf_thrmisc_t;
1370 __elfN(puthdr)(struct thread *td, void *dst, size_t *off, int numsegs)
1373 elf_prstatus_t status;
1374 elf_prfpregset_t fpregset;
1375 elf_prpsinfo_t psinfo;
1376 elf_thrmisc_t thrmisc;
1378 elf_prstatus_t *status;
1379 elf_prfpregset_t *fpregset;
1380 elf_prpsinfo_t *psinfo;
1381 elf_thrmisc_t *thrmisc;
1384 size_t ehoff, noteoff, notesz, phoff;
1389 *off += sizeof(Elf_Ehdr);
1392 *off += (numsegs + 1) * sizeof(Elf_Phdr);
1396 * Don't allocate space for the notes if we're just calculating
1397 * the size of the header. We also don't collect the data.
1400 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO|M_WAITOK);
1401 status = &tempdata->status;
1402 fpregset = &tempdata->fpregset;
1403 psinfo = &tempdata->psinfo;
1404 thrmisc = &tempdata->thrmisc;
1414 psinfo->pr_version = PRPSINFO_VERSION;
1415 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1416 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1418 * XXX - We don't fill in the command line arguments properly
1421 strlcpy(psinfo->pr_psargs, p->p_comm,
1422 sizeof(psinfo->pr_psargs));
1424 __elfN(putnote)(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
1428 * To have the debugger select the right thread (LWP) as the initial
1429 * thread, we dump the state of the thread passed to us in td first.
1430 * This is the thread that causes the core dump and thus likely to
1431 * be the right thread one wants to have selected in the debugger.
1434 while (thr != NULL) {
1436 status->pr_version = PRSTATUS_VERSION;
1437 status->pr_statussz = sizeof(elf_prstatus_t);
1438 status->pr_gregsetsz = sizeof(elf_gregset_t);
1439 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
1440 status->pr_osreldate = osreldate;
1441 status->pr_cursig = p->p_sig;
1442 status->pr_pid = thr->td_tid;
1443 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1444 fill_regs32(thr, &status->pr_reg);
1445 fill_fpregs32(thr, fpregset);
1447 fill_regs(thr, &status->pr_reg);
1448 fill_fpregs(thr, fpregset);
1450 memset(&thrmisc->_pad, 0, sizeof (thrmisc->_pad));
1451 strcpy(thrmisc->pr_tname, thr->td_name);
1453 __elfN(putnote)(dst, off, "FreeBSD", NT_PRSTATUS, status,
1455 __elfN(putnote)(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
1457 __elfN(putnote)(dst, off, "FreeBSD", NT_THRMISC, thrmisc,
1460 * Allow for MD specific notes, as well as any MD
1461 * specific preparations for writing MI notes.
1463 __elfN(dump_thread)(thr, dst, off);
1465 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1466 TAILQ_NEXT(thr, td_plist);
1468 thr = TAILQ_NEXT(thr, td_plist);
1471 notesz = *off - noteoff;
1474 free(tempdata, M_TEMP);
1476 /* Align up to a page boundary for the program segments. */
1477 *off = round_page(*off);
1482 struct phdr_closure phc;
1485 * Fill in the ELF header.
1487 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
1488 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1489 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1490 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1491 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1492 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1493 ehdr->e_ident[EI_DATA] = ELF_DATA;
1494 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1495 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1496 ehdr->e_ident[EI_ABIVERSION] = 0;
1497 ehdr->e_ident[EI_PAD] = 0;
1498 ehdr->e_type = ET_CORE;
1499 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1500 ehdr->e_machine = ELF_ARCH32;
1502 ehdr->e_machine = ELF_ARCH;
1504 ehdr->e_version = EV_CURRENT;
1506 ehdr->e_phoff = phoff;
1508 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1509 ehdr->e_phentsize = sizeof(Elf_Phdr);
1510 ehdr->e_phnum = numsegs + 1;
1511 ehdr->e_shentsize = sizeof(Elf_Shdr);
1513 ehdr->e_shstrndx = SHN_UNDEF;
1516 * Fill in the program header entries.
1518 phdr = (Elf_Phdr *)((char *)dst + phoff);
1520 /* The note segement. */
1521 phdr->p_type = PT_NOTE;
1522 phdr->p_offset = noteoff;
1525 phdr->p_filesz = notesz;
1531 /* All the writable segments from the program. */
1534 each_writable_segment(td, cb_put_phdr, &phc);
1539 __elfN(putnote)(void *dst, size_t *off, const char *name, int type,
1540 const void *desc, size_t descsz)
1544 note.n_namesz = strlen(name) + 1;
1545 note.n_descsz = descsz;
1548 bcopy(¬e, (char *)dst + *off, sizeof note);
1549 *off += sizeof note;
1551 bcopy(name, (char *)dst + *off, note.n_namesz);
1552 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
1554 bcopy(desc, (char *)dst + *off, note.n_descsz);
1555 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
1559 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
1560 int32_t *osrel, const Elf_Phdr *pnote)
1562 const Elf_Note *note, *note0, *note_end;
1563 const char *note_name;
1566 if (pnote == NULL || pnote->p_offset >= PAGE_SIZE ||
1567 pnote->p_filesz > PAGE_SIZE ||
1568 pnote->p_offset + pnote->p_filesz >= PAGE_SIZE)
1571 note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
1572 note_end = (const Elf_Note *)(imgp->image_header +
1573 pnote->p_offset + pnote->p_filesz);
1574 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
1575 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
1576 (const char *)note < sizeof(Elf_Note))
1578 if (note->n_namesz != checknote->hdr.n_namesz ||
1579 note->n_descsz != checknote->hdr.n_descsz ||
1580 note->n_type != checknote->hdr.n_type)
1582 note_name = (const char *)(note + 1);
1583 if (note_name + checknote->hdr.n_namesz >=
1584 (const char *)note_end || strncmp(checknote->vendor,
1585 note_name, checknote->hdr.n_namesz) != 0)
1589 * Fetch the osreldate for binary
1590 * from the ELF OSABI-note if necessary.
1592 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
1593 checknote->trans_osrel != NULL)
1594 return (checknote->trans_osrel(note, osrel));
1598 note = (const Elf_Note *)((const char *)(note + 1) +
1599 roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
1600 roundup2(note->n_descsz, sizeof(Elf32_Addr)));
1607 * Try to find the appropriate ABI-note section for checknote,
1608 * fetch the osreldate for binary from the ELF OSABI-note. Only the
1609 * first page of the image is searched, the same as for headers.
1612 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
1615 const Elf_Phdr *phdr;
1616 const Elf_Ehdr *hdr;
1619 hdr = (const Elf_Ehdr *)imgp->image_header;
1620 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1622 for (i = 0; i < hdr->e_phnum; i++) {
1623 if (phdr[i].p_type == PT_NOTE &&
1624 __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
1632 * Tell kern_execve.c about it, with a little help from the linker.
1634 static struct execsw __elfN(execsw) = {
1635 __CONCAT(exec_, __elfN(imgact)),
1636 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
1638 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
1640 #ifdef COMPRESS_USER_CORES
1642 * Compress and write out a core segment for a user process.
1644 * 'inbuf' is the starting address of a VM segment in the process' address
1645 * space that is to be compressed and written out to the core file. 'dest_buf'
1646 * is a buffer in the kernel's address space. The segment is copied from
1647 * 'inbuf' to 'dest_buf' first before being processed by the compression
1648 * routine gzwrite(). This copying is necessary because the content of the VM
1649 * segment may change between the compression pass and the crc-computation pass
1650 * in gzwrite(). This is because realtime threads may preempt the UNIX kernel.
1653 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
1658 unsigned int chunk_len;
1661 chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
1662 copyin(inbuf, dest_buf, chunk_len);
1663 len_compressed = gzwrite(file, dest_buf, chunk_len);
1665 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
1667 if ((unsigned int)len_compressed != chunk_len) {
1669 "compress_core: length mismatch (0x%x returned, "
1670 "0x%x expected)\n", len_compressed, chunk_len);
1671 EVENTHANDLER_INVOKE(app_coredump_error, td,
1672 "compress_core: length mismatch %x -> %x",
1673 chunk_len, len_compressed);
1684 #endif /* COMPRESS_USER_CORES */
1687 __elfN(trans_prot)(Elf_Word flags)
1693 prot |= VM_PROT_EXECUTE;
1695 prot |= VM_PROT_WRITE;
1697 prot |= VM_PROT_READ;
1698 #if __ELF_WORD_SIZE == 32
1699 #if defined(__amd64__) || defined(__ia64__)
1700 if (i386_read_exec && (flags & PF_R))
1701 prot |= VM_PROT_EXECUTE;
1708 __elfN(untrans_prot)(vm_prot_t prot)
1713 if (prot & VM_PROT_EXECUTE)
1715 if (prot & VM_PROT_READ)
1717 if (prot & VM_PROT_WRITE)