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>
57 #include <sys/sf_buf.h>
59 #include <sys/systm.h>
60 #include <sys/signalvar.h>
63 #include <sys/syscall.h>
64 #include <sys/sysctl.h>
65 #include <sys/sysent.h>
66 #include <sys/vnode.h>
67 #include <sys/syslog.h>
68 #include <sys/eventhandler.h>
73 #include <vm/vm_kern.h>
74 #include <vm/vm_param.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_object.h>
78 #include <vm/vm_extern.h>
80 #include <machine/elf.h>
81 #include <machine/md_var.h>
83 #define OLD_EI_BRAND 8
85 static int __elfN(check_header)(const Elf_Ehdr *hdr);
86 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
87 const char *interp, int interp_name_len, int32_t *osrel);
88 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
89 u_long *entry, size_t pagesize);
90 static int __elfN(load_section)(struct vmspace *vmspace, vm_object_t object,
91 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
92 vm_prot_t prot, size_t pagesize);
93 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
94 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
96 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
97 static boolean_t __elfN(check_note)(struct image_params *imgp,
98 Elf_Brandnote *checknote, int32_t *osrel);
99 static vm_prot_t __elfN(trans_prot)(Elf_Word);
100 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
102 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
105 #ifdef COMPRESS_USER_CORES
106 static int compress_core(gzFile, char *, char *, unsigned int,
109 #define CORE_BUF_SIZE (16 * 1024)
111 int __elfN(fallback_brand) = -1;
112 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
113 fallback_brand, CTLFLAG_RW, &__elfN(fallback_brand), 0,
114 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
115 TUNABLE_INT("kern.elf" __XSTRING(__ELF_WORD_SIZE) ".fallback_brand",
116 &__elfN(fallback_brand));
118 static int elf_legacy_coredump = 0;
119 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
120 &elf_legacy_coredump, 0, "");
122 int __elfN(nxstack) =
123 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
128 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
129 nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
130 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
132 #if __ELF_WORD_SIZE == 32
133 #if defined(__amd64__) || defined(__ia64__)
134 int i386_read_exec = 0;
135 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
136 "enable execution from readable segments");
140 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
142 #define trunc_page_ps(va, ps) ((va) & ~(ps - 1))
143 #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1))
144 #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
146 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
148 Elf_Brandnote __elfN(freebsd_brandnote) = {
149 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
150 .hdr.n_descsz = sizeof(int32_t),
152 .vendor = FREEBSD_ABI_VENDOR,
153 .flags = BN_TRANSLATE_OSREL,
154 .trans_osrel = __elfN(freebsd_trans_osrel)
158 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
162 p = (uintptr_t)(note + 1);
163 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
164 *osrel = *(const int32_t *)(p);
169 static const char GNU_ABI_VENDOR[] = "GNU";
170 static int GNU_KFREEBSD_ABI_DESC = 3;
172 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
173 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
174 .hdr.n_descsz = 16, /* XXX at least 16 */
176 .vendor = GNU_ABI_VENDOR,
177 .flags = BN_TRANSLATE_OSREL,
178 .trans_osrel = kfreebsd_trans_osrel
182 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
184 const Elf32_Word *desc;
187 p = (uintptr_t)(note + 1);
188 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
190 desc = (const Elf32_Word *)p;
191 if (desc[0] != GNU_KFREEBSD_ABI_DESC)
195 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
196 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
198 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
204 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
208 for (i = 0; i < MAX_BRANDS; i++) {
209 if (elf_brand_list[i] == NULL) {
210 elf_brand_list[i] = entry;
214 if (i == MAX_BRANDS) {
215 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
223 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
227 for (i = 0; i < MAX_BRANDS; i++) {
228 if (elf_brand_list[i] == entry) {
229 elf_brand_list[i] = NULL;
239 __elfN(brand_inuse)(Elf_Brandinfo *entry)
244 sx_slock(&allproc_lock);
245 FOREACH_PROC_IN_SYSTEM(p) {
246 if (p->p_sysent == entry->sysvec) {
251 sx_sunlock(&allproc_lock);
256 static Elf_Brandinfo *
257 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
258 int interp_name_len, int32_t *osrel)
260 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
266 * We support four types of branding -- (1) the ELF EI_OSABI field
267 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
268 * branding w/in the ELF header, (3) path of the `interp_path'
269 * field, and (4) the ".note.ABI-tag" ELF section.
272 /* Look for an ".note.ABI-tag" ELF section */
273 for (i = 0; i < MAX_BRANDS; i++) {
274 bi = elf_brand_list[i];
277 if (hdr->e_machine == bi->machine && (bi->flags &
278 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
279 ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
285 /* If the executable has a brand, search for it in the brand list. */
286 for (i = 0; i < MAX_BRANDS; i++) {
287 bi = elf_brand_list[i];
288 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
290 if (hdr->e_machine == bi->machine &&
291 (hdr->e_ident[EI_OSABI] == bi->brand ||
292 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
293 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
297 /* Lacking a known brand, search for a recognized interpreter. */
298 if (interp != NULL) {
299 for (i = 0; i < MAX_BRANDS; i++) {
300 bi = elf_brand_list[i];
301 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
303 if (hdr->e_machine == bi->machine &&
304 /* ELF image p_filesz includes terminating zero */
305 strlen(bi->interp_path) + 1 == interp_name_len &&
306 strncmp(interp, bi->interp_path, interp_name_len)
312 /* Lacking a recognized interpreter, try the default brand */
313 for (i = 0; i < MAX_BRANDS; i++) {
314 bi = elf_brand_list[i];
315 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
317 if (hdr->e_machine == bi->machine &&
318 __elfN(fallback_brand) == bi->brand)
325 __elfN(check_header)(const Elf_Ehdr *hdr)
331 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
332 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
333 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
334 hdr->e_phentsize != sizeof(Elf_Phdr) ||
335 hdr->e_version != ELF_TARG_VER)
339 * Make sure we have at least one brand for this machine.
342 for (i = 0; i < MAX_BRANDS; i++) {
343 bi = elf_brand_list[i];
344 if (bi != NULL && bi->machine == hdr->e_machine)
354 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
355 vm_offset_t start, vm_offset_t end, vm_prot_t prot)
362 * Create the page if it doesn't exist yet. Ignore errors.
365 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
366 VM_PROT_ALL, VM_PROT_ALL, 0);
370 * Find the page from the underlying object.
373 sf = vm_imgact_map_page(object, offset);
375 return (KERN_FAILURE);
376 off = offset - trunc_page(offset);
377 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
379 vm_imgact_unmap_page(sf);
381 return (KERN_FAILURE);
385 return (KERN_SUCCESS);
389 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
390 vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
397 if (start != trunc_page(start)) {
398 rv = __elfN(map_partial)(map, object, offset, start,
399 round_page(start), prot);
402 offset += round_page(start) - start;
403 start = round_page(start);
405 if (end != round_page(end)) {
406 rv = __elfN(map_partial)(map, object, offset +
407 trunc_page(end) - start, trunc_page(end), end, prot);
410 end = trunc_page(end);
413 if (offset & PAGE_MASK) {
415 * The mapping is not page aligned. This means we have
416 * to copy the data. Sigh.
418 rv = vm_map_find(map, NULL, 0, &start, end - start,
419 FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0);
423 return (KERN_SUCCESS);
424 for (; start < end; start += sz) {
425 sf = vm_imgact_map_page(object, offset);
427 return (KERN_FAILURE);
428 off = offset - trunc_page(offset);
430 if (sz > PAGE_SIZE - off)
431 sz = PAGE_SIZE - off;
432 error = copyout((caddr_t)sf_buf_kva(sf) + off,
434 vm_imgact_unmap_page(sf);
436 return (KERN_FAILURE);
442 vm_object_reference(object);
444 rv = vm_map_insert(map, object, offset, start, end,
445 prot, VM_PROT_ALL, cow);
447 if (rv != KERN_SUCCESS)
448 vm_object_deallocate(object);
452 return (KERN_SUCCESS);
457 __elfN(load_section)(struct vmspace *vmspace,
458 vm_object_t object, vm_offset_t offset,
459 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 > object->un_pager.vnp.vnp_size ||
480 uprintf("elf_load_section: truncated ELF file\n");
484 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
485 file_addr = trunc_page_ps(offset, pagesize);
488 * We have two choices. We can either clear the data in the last page
489 * of an oversized mapping, or we can start the anon mapping a page
490 * early and copy the initialized data into that first page. We
491 * choose the second..
494 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
496 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
499 /* cow flags: don't dump readonly sections in core */
500 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
501 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
503 rv = __elfN(map_insert)(&vmspace->vm_map,
505 file_addr, /* file offset */
506 map_addr, /* virtual start */
507 map_addr + map_len,/* virtual end */
510 if (rv != KERN_SUCCESS)
513 /* we can stop now if we've covered it all */
514 if (memsz == filsz) {
521 * We have to get the remaining bit of the file into the first part
522 * of the oversized map segment. This is normally because the .data
523 * segment in the file is extended to provide bss. It's a neat idea
524 * to try and save a page, but it's a pain in the behind to implement.
526 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
527 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
528 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
531 /* This had damn well better be true! */
533 rv = __elfN(map_insert)(&vmspace->vm_map, NULL, 0, map_addr,
534 map_addr + map_len, VM_PROT_ALL, 0);
535 if (rv != KERN_SUCCESS) {
543 sf = vm_imgact_map_page(object, offset + filsz);
547 /* send the page fragment to user space */
548 off = trunc_page_ps(offset + filsz, pagesize) -
549 trunc_page(offset + filsz);
550 error = copyout((caddr_t)sf_buf_kva(sf) + off,
551 (caddr_t)map_addr, copy_len);
552 vm_imgact_unmap_page(sf);
559 * set it to the specified protection.
560 * XXX had better undo the damage from pasting over the cracks here!
562 vm_map_protect(&vmspace->vm_map, trunc_page(map_addr),
563 round_page(map_addr + map_len), prot, FALSE);
569 * Load the file "file" into memory. It may be either a shared object
572 * The "addr" reference parameter is in/out. On entry, it specifies
573 * the address where a shared object should be loaded. If the file is
574 * an executable, this value is ignored. On exit, "addr" specifies
575 * where the file was actually loaded.
577 * The "entry" reference parameter is out only. On exit, it specifies
578 * the entry point for the loaded file.
581 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
582 u_long *entry, size_t pagesize)
587 struct image_params image_params;
589 const Elf_Ehdr *hdr = NULL;
590 const Elf_Phdr *phdr = NULL;
591 struct nameidata *nd;
592 struct vmspace *vmspace = p->p_vmspace;
594 struct image_params *imgp;
597 u_long base_addr = 0;
598 int vfslocked, 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, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file,
627 if ((error = namei(nd)) != 0) {
631 vfslocked = NDHASGIANT(nd);
632 NDFREE(nd, NDF_ONLY_PNBUF);
633 imgp->vp = nd->ni_vp;
636 * Check permissions, modes, uid, etc on the file, and "open" it.
638 error = exec_check_permissions(imgp);
642 error = exec_map_first_page(imgp);
647 * Also make certain that the interpreter stays the same, so set
648 * its VV_TEXT flag, too.
650 VOP_SET_TEXT(nd->ni_vp);
652 imgp->object = nd->ni_vp->v_object;
654 hdr = (const Elf_Ehdr *)imgp->image_header;
655 if ((error = __elfN(check_header)(hdr)) != 0)
657 if (hdr->e_type == ET_DYN)
659 else if (hdr->e_type == ET_EXEC)
666 /* Only support headers that fit within first page for now */
667 if ((hdr->e_phoff > PAGE_SIZE) ||
668 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
673 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
674 if (!aligned(phdr, Elf_Addr)) {
679 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
680 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
681 /* Loadable segment */
682 prot = __elfN(trans_prot)(phdr[i].p_flags);
683 if ((error = __elfN(load_section)(vmspace,
684 imgp->object, phdr[i].p_offset,
685 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
686 phdr[i].p_memsz, phdr[i].p_filesz, prot,
690 * Establish the base address if this is the
694 base_addr = trunc_page(phdr[i].p_vaddr +
700 *entry = (unsigned long)hdr->e_entry + rbase;
704 exec_unmap_first_page(imgp);
709 VFS_UNLOCK_GIANT(vfslocked);
710 free(tempdata, M_TEMP);
716 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
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;
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;
728 int error = 0, i, n, interp_name_len = 0;
729 const char *interp = NULL, *newinterp = NULL;
730 Elf_Brandinfo *brand_info;
732 struct sysentvec *sv;
735 * Do we have a valid ELF header ?
737 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
738 * if particular brand doesn't support it.
740 if (__elfN(check_header)(hdr) != 0 ||
741 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
745 * From here on down, we return an errno, not -1, as we've
746 * detected an ELF file.
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 */
754 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
755 if (!aligned(phdr, Elf_Addr))
759 for (i = 0; i < hdr->e_phnum; i++) {
760 switch (phdr[i].p_type) {
763 baddr = phdr[i].p_vaddr;
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)
772 interp = imgp->image_header + phdr[i].p_offset;
773 interp_name_len = phdr[i].p_filesz;
778 __elfN(trans_prot)(phdr[i].p_flags);
783 brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
785 if (brand_info == NULL) {
786 uprintf("ELF binary type \"%u\" not known.\n",
787 hdr->e_ident[EI_OSABI]);
790 if (hdr->e_type == ET_DYN) {
791 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
794 * Honour the base load address from the dso if it is
795 * non-zero for some reason.
798 et_dyn_addr = ET_DYN_LOAD_ADDR;
803 sv = brand_info->sysvec;
804 if (interp != NULL && brand_info->interp_newpath != NULL)
805 newinterp = brand_info->interp_newpath;
808 * Avoid a possible deadlock if the current address space is destroyed
809 * and that address space maps the locked vnode. In the common case,
810 * the locked vnode's v_usecount is decremented but remains greater
811 * than zero. Consequently, the vnode lock is not needed by vrele().
812 * However, in cases where the vnode lock is external, such as nullfs,
813 * v_usecount may become zero.
815 VOP_UNLOCK(imgp->vp, 0);
817 error = exec_new_vmspace(imgp, sv);
818 imgp->proc->p_sysent = sv;
820 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
824 vmspace = imgp->proc->p_vmspace;
826 for (i = 0; i < hdr->e_phnum; i++) {
827 switch (phdr[i].p_type) {
828 case PT_LOAD: /* Loadable segment */
829 if (phdr[i].p_memsz == 0)
831 prot = __elfN(trans_prot)(phdr[i].p_flags);
832 if ((error = __elfN(load_section)(vmspace,
833 imgp->object, phdr[i].p_offset,
834 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
835 phdr[i].p_memsz, phdr[i].p_filesz, prot,
836 sv->sv_pagesize)) != 0)
840 * If this segment contains the program headers,
841 * remember their virtual address for the AT_PHDR
842 * aux entry. Static binaries don't usually include
845 if (phdr[i].p_offset == 0 &&
846 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
848 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
851 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
852 seg_size = round_page(phdr[i].p_memsz +
853 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
856 * Make the largest executable segment the official
857 * text segment and all others data.
859 * Note that obreak() assumes that data_addr +
860 * data_size == end of data load area, and the ELF
861 * file format expects segments to be sorted by
862 * address. If multiple data segments exist, the
863 * last one will be used.
866 if (phdr[i].p_flags & PF_X && text_size < seg_size) {
867 text_size = seg_size;
868 text_addr = seg_addr;
870 data_size = seg_size;
871 data_addr = seg_addr;
873 total_size += seg_size;
875 case PT_PHDR: /* Program header table info */
876 proghdr = phdr[i].p_vaddr + et_dyn_addr;
883 if (data_addr == 0 && data_size == 0) {
884 data_addr = text_addr;
885 data_size = text_size;
888 entry = (u_long)hdr->e_entry + et_dyn_addr;
891 * Check limits. It should be safe to check the
892 * limits after loading the segments since we do
893 * not actually fault in all the segments pages.
895 PROC_LOCK(imgp->proc);
896 if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) ||
897 text_size > maxtsiz ||
898 total_size > lim_cur(imgp->proc, RLIMIT_VMEM) ||
899 racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
900 racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
901 PROC_UNLOCK(imgp->proc);
905 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
906 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
907 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
908 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
911 * We load the dynamic linker where a userland call
912 * to mmap(0, ...) would put it. The rationale behind this
913 * calculation is that it leaves room for the heap to grow to
914 * its maximum allowed size.
916 addr = round_page((vm_offset_t)imgp->proc->p_vmspace->vm_daddr +
917 lim_max(imgp->proc, RLIMIT_DATA));
918 PROC_UNLOCK(imgp->proc);
920 imgp->entry_addr = entry;
922 if (interp != NULL) {
923 int have_interp = FALSE;
924 VOP_UNLOCK(imgp->vp, 0);
925 if (brand_info->emul_path != NULL &&
926 brand_info->emul_path[0] != '\0') {
927 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
928 snprintf(path, MAXPATHLEN, "%s%s",
929 brand_info->emul_path, interp);
930 error = __elfN(load_file)(imgp->proc, path, &addr,
931 &imgp->entry_addr, sv->sv_pagesize);
936 if (!have_interp && newinterp != NULL) {
937 error = __elfN(load_file)(imgp->proc, newinterp, &addr,
938 &imgp->entry_addr, sv->sv_pagesize);
943 error = __elfN(load_file)(imgp->proc, interp, &addr,
944 &imgp->entry_addr, sv->sv_pagesize);
946 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
948 uprintf("ELF interpreter %s not found\n", interp);
955 * Construct auxargs table (used by the fixup routine)
957 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
958 elf_auxargs->execfd = -1;
959 elf_auxargs->phdr = proghdr;
960 elf_auxargs->phent = hdr->e_phentsize;
961 elf_auxargs->phnum = hdr->e_phnum;
962 elf_auxargs->pagesz = PAGE_SIZE;
963 elf_auxargs->base = addr;
964 elf_auxargs->flags = 0;
965 elf_auxargs->entry = entry;
967 imgp->auxargs = elf_auxargs;
968 imgp->interpreted = 0;
969 imgp->reloc_base = addr;
970 imgp->proc->p_osrel = osrel;
975 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
978 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
980 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
984 base = (Elf_Addr *)*stack_base;
985 pos = base + (imgp->args->argc + imgp->args->envc + 2);
987 if (args->execfd != -1)
988 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
989 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
990 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
991 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
992 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
993 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
994 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
995 AUXARGS_ENTRY(pos, AT_BASE, args->base);
996 if (imgp->execpathp != 0)
997 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
998 AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
999 if (imgp->canary != 0) {
1000 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1001 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1003 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1004 if (imgp->pagesizes != 0) {
1005 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1006 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1008 if (imgp->sysent->sv_timekeep_base != 0) {
1009 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1010 imgp->sysent->sv_timekeep_base);
1012 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1013 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1014 imgp->sysent->sv_stackprot);
1015 AUXARGS_ENTRY(pos, AT_NULL, 0);
1017 free(imgp->auxargs, M_TEMP);
1018 imgp->auxargs = NULL;
1021 suword(base, (long)imgp->args->argc);
1022 *stack_base = (register_t *)base;
1027 * Code for generating ELF core dumps.
1030 typedef void (*segment_callback)(vm_map_entry_t, void *);
1032 /* Closure for cb_put_phdr(). */
1033 struct phdr_closure {
1034 Elf_Phdr *phdr; /* Program header to fill in */
1035 Elf_Off offset; /* Offset of segment in core file */
1038 /* Closure for cb_size_segment(). */
1039 struct sseg_closure {
1040 int count; /* Count of writable segments. */
1041 size_t size; /* Total size of all writable segments. */
1044 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1047 int type; /* Note type. */
1048 outfunc_t outfunc; /* Output function. */
1049 void *outarg; /* Argument for the output function. */
1050 size_t outsize; /* Output size. */
1051 TAILQ_ENTRY(note_info) link; /* Link to the next note info. */
1054 TAILQ_HEAD(note_info_list, note_info);
1056 static void cb_put_phdr(vm_map_entry_t, void *);
1057 static void cb_size_segment(vm_map_entry_t, void *);
1058 static void each_writable_segment(struct thread *, segment_callback, void *);
1059 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
1060 int, void *, size_t, struct note_info_list *, size_t, gzFile);
1061 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1063 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1064 static void __elfN(putnote)(struct note_info *, struct sbuf *);
1065 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1066 static int sbuf_drain_core_output(void *, const char *, int);
1068 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1069 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1070 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1071 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1072 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1074 #ifdef COMPRESS_USER_CORES
1075 extern int compress_user_cores;
1076 extern int compress_user_cores_gzlevel;
1080 core_output(struct vnode *vp, void *base, size_t len, off_t offset,
1081 struct ucred *active_cred, struct ucred *file_cred,
1082 struct thread *td, char *core_buf, gzFile gzfile) {
1086 #ifdef COMPRESS_USER_CORES
1087 error = compress_core(gzfile, base, core_buf, len, td);
1089 panic("shouldn't be here");
1092 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
1093 UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1099 /* Coredump output parameters for sbuf drain routine. */
1100 struct sbuf_drain_core_params {
1102 struct ucred *active_cred;
1103 struct ucred *file_cred;
1106 #ifdef COMPRESS_USER_CORES
1112 * Drain into a core file.
1115 sbuf_drain_core_output(void *arg, const char *data, int len)
1117 struct sbuf_drain_core_params *p;
1120 p = (struct sbuf_drain_core_params *)arg;
1121 #ifdef COMPRESS_USER_CORES
1122 if (p->gzfile != Z_NULL)
1123 error = compress_core(p->gzfile, NULL, __DECONST(char *, data),
1127 error = vn_rdwr_inchunks(UIO_WRITE, p->vp,
1128 __DECONST(void *, data), len, p->offset, UIO_SYSSPACE,
1129 IO_UNIT | IO_DIRECT, p->active_cred, p->file_cred, NULL,
1138 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1140 struct ucred *cred = td->td_ucred;
1142 struct sseg_closure seginfo;
1143 struct note_info_list notelst;
1144 struct note_info *ninfo;
1146 size_t hdrsize, notesz, coresize;
1148 gzFile gzfile = Z_NULL;
1149 char *core_buf = NULL;
1150 #ifdef COMPRESS_USER_CORES
1151 char gzopen_flags[8];
1153 int doing_compress = flags & IMGACT_CORE_COMPRESS;
1157 TAILQ_INIT(¬elst);
1159 #ifdef COMPRESS_USER_CORES
1160 if (doing_compress) {
1163 if (compress_user_cores_gzlevel >= 0 &&
1164 compress_user_cores_gzlevel <= 9)
1165 *p++ = '0' + compress_user_cores_gzlevel;
1167 gzfile = gz_open("", gzopen_flags, vp);
1168 if (gzfile == Z_NULL) {
1172 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1180 /* Size the program segments. */
1183 each_writable_segment(td, cb_size_segment, &seginfo);
1186 * Collect info about the core file header area.
1188 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1189 __elfN(prepare_notes)(td, ¬elst, ¬esz);
1190 coresize = round_page(hdrsize + notesz) + seginfo.size;
1193 PROC_LOCK(td->td_proc);
1194 error = racct_add(td->td_proc, RACCT_CORE, coresize);
1195 PROC_UNLOCK(td->td_proc);
1201 if (coresize >= limit) {
1207 * Allocate memory for building the header, fill it up,
1208 * and write it out following the notes.
1210 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1215 error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
1216 ¬elst, notesz, gzfile);
1218 /* Write the contents of all of the writable segments. */
1224 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1225 offset = round_page(hdrsize + notesz);
1226 for (i = 0; i < seginfo.count; i++) {
1227 error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1228 php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
1231 offset += php->p_filesz;
1237 "Failed to write core file for process %s (error %d)\n",
1238 curproc->p_comm, error);
1242 #ifdef COMPRESS_USER_CORES
1244 free(core_buf, M_TEMP);
1248 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) {
1249 TAILQ_REMOVE(¬elst, ninfo, link);
1250 free(ninfo, M_TEMP);
1259 * A callback for each_writable_segment() to write out the segment's
1260 * program header entry.
1263 cb_put_phdr(entry, closure)
1264 vm_map_entry_t entry;
1267 struct phdr_closure *phc = (struct phdr_closure *)closure;
1268 Elf_Phdr *phdr = phc->phdr;
1270 phc->offset = round_page(phc->offset);
1272 phdr->p_type = PT_LOAD;
1273 phdr->p_offset = phc->offset;
1274 phdr->p_vaddr = entry->start;
1276 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1277 phdr->p_align = PAGE_SIZE;
1278 phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1280 phc->offset += phdr->p_filesz;
1285 * A callback for each_writable_segment() to gather information about
1286 * the number of segments and their total size.
1289 cb_size_segment(entry, closure)
1290 vm_map_entry_t entry;
1293 struct sseg_closure *ssc = (struct sseg_closure *)closure;
1296 ssc->size += entry->end - entry->start;
1300 * For each writable segment in the process's memory map, call the given
1301 * function with a pointer to the map entry and some arbitrary
1302 * caller-supplied data.
1305 each_writable_segment(td, func, closure)
1307 segment_callback func;
1310 struct proc *p = td->td_proc;
1311 vm_map_t map = &p->p_vmspace->vm_map;
1312 vm_map_entry_t entry;
1313 vm_object_t backing_object, object;
1314 boolean_t ignore_entry;
1316 vm_map_lock_read(map);
1317 for (entry = map->header.next; entry != &map->header;
1318 entry = entry->next) {
1320 * Don't dump inaccessible mappings, deal with legacy
1323 * Note that read-only segments related to the elf binary
1324 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1325 * need to arbitrarily ignore such segments.
1327 if (elf_legacy_coredump) {
1328 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1331 if ((entry->protection & VM_PROT_ALL) == 0)
1336 * Dont include memory segment in the coredump if
1337 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1338 * madvise(2). Do not dump submaps (i.e. parts of the
1341 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1344 if ((object = entry->object.vm_object) == NULL)
1347 /* Ignore memory-mapped devices and such things. */
1348 VM_OBJECT_LOCK(object);
1349 while ((backing_object = object->backing_object) != NULL) {
1350 VM_OBJECT_LOCK(backing_object);
1351 VM_OBJECT_UNLOCK(object);
1352 object = backing_object;
1354 ignore_entry = object->type != OBJT_DEFAULT &&
1355 object->type != OBJT_SWAP && object->type != OBJT_VNODE;
1356 VM_OBJECT_UNLOCK(object);
1360 (*func)(entry, closure);
1362 vm_map_unlock_read(map);
1366 * Write the core file header to the file, including padding up to
1367 * the page boundary.
1370 __elfN(corehdr)(struct thread *td, struct vnode *vp, struct ucred *cred,
1371 int numsegs, void *hdr, size_t hdrsize, struct note_info_list *notelst,
1372 size_t notesz, gzFile gzfile)
1374 struct sbuf_drain_core_params params;
1375 struct note_info *ninfo;
1379 /* Fill in the header. */
1380 bzero(hdr, hdrsize);
1381 __elfN(puthdr)(td, hdr, hdrsize, numsegs, notesz);
1384 params.active_cred = cred;
1385 params.file_cred = NOCRED;
1388 #ifdef COMPRESS_USER_CORES
1389 params.gzfile = gzfile;
1391 sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1392 sbuf_set_drain(sb, sbuf_drain_core_output, ¶ms);
1393 sbuf_start_section(sb, NULL);
1394 sbuf_bcat(sb, hdr, hdrsize);
1395 TAILQ_FOREACH(ninfo, notelst, link)
1396 __elfN(putnote)(ninfo, sb);
1397 /* Align up to a page boundary for the program segments. */
1398 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1399 error = sbuf_finish(sb);
1406 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1416 size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1419 * To have the debugger select the right thread (LWP) as the initial
1420 * thread, we dump the state of the thread passed to us in td first.
1421 * This is the thread that causes the core dump and thus likely to
1422 * be the right thread one wants to have selected in the debugger.
1425 while (thr != NULL) {
1426 size += register_note(list, NT_PRSTATUS,
1427 __elfN(note_prstatus), thr);
1428 size += register_note(list, NT_FPREGSET,
1429 __elfN(note_fpregset), thr);
1430 size += register_note(list, NT_THRMISC,
1431 __elfN(note_thrmisc), thr);
1432 size += register_note(list, -1,
1433 __elfN(note_threadmd), thr);
1435 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1436 TAILQ_NEXT(thr, td_plist);
1438 thr = TAILQ_NEXT(thr, td_plist);
1445 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1450 struct phdr_closure phc;
1452 ehdr = (Elf_Ehdr *)hdr;
1453 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
1455 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1456 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1457 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1458 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1459 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1460 ehdr->e_ident[EI_DATA] = ELF_DATA;
1461 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1462 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1463 ehdr->e_ident[EI_ABIVERSION] = 0;
1464 ehdr->e_ident[EI_PAD] = 0;
1465 ehdr->e_type = ET_CORE;
1466 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1467 ehdr->e_machine = ELF_ARCH32;
1469 ehdr->e_machine = ELF_ARCH;
1471 ehdr->e_version = EV_CURRENT;
1473 ehdr->e_phoff = sizeof(Elf_Ehdr);
1475 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1476 ehdr->e_phentsize = sizeof(Elf_Phdr);
1477 ehdr->e_phnum = numsegs + 1;
1478 ehdr->e_shentsize = sizeof(Elf_Shdr);
1480 ehdr->e_shstrndx = SHN_UNDEF;
1483 * Fill in the program header entries.
1486 /* The note segement. */
1487 phdr->p_type = PT_NOTE;
1488 phdr->p_offset = hdrsize;
1491 phdr->p_filesz = notesz;
1493 phdr->p_flags = PF_R;
1494 phdr->p_align = sizeof(Elf32_Size);
1497 /* All the writable segments from the program. */
1499 phc.offset = round_page(hdrsize + notesz);
1500 each_writable_segment(td, cb_put_phdr, &phc);
1504 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1506 struct note_info *ninfo;
1507 size_t size, notesize;
1510 out(arg, NULL, &size);
1511 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1513 ninfo->outfunc = out;
1514 ninfo->outarg = arg;
1515 ninfo->outsize = size;
1516 TAILQ_INSERT_TAIL(list, ninfo, link);
1521 notesize = sizeof(Elf_Note) + /* note header */
1522 roundup2(8, sizeof(Elf32_Size)) + /* note name ("FreeBSD") */
1523 roundup2(size, sizeof(Elf32_Size)); /* note description */
1529 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1534 if (ninfo->type == -1) {
1535 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1539 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
1540 note.n_descsz = ninfo->outsize;
1541 note.n_type = ninfo->type;
1543 sbuf_bcat(sb, ¬e, sizeof(note));
1544 sbuf_start_section(sb, &old_len);
1545 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
1546 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
1547 if (note.n_descsz == 0)
1549 sbuf_start_section(sb, &old_len);
1550 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1551 sbuf_end_section(sb, old_len, sizeof(Elf32_Size), 0);
1555 * Miscellaneous note out functions.
1558 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1559 #include <compat/freebsd32/freebsd32.h>
1561 typedef struct prstatus32 elf_prstatus_t;
1562 typedef struct prpsinfo32 elf_prpsinfo_t;
1563 typedef struct fpreg32 elf_prfpregset_t;
1564 typedef struct fpreg32 elf_fpregset_t;
1565 typedef struct reg32 elf_gregset_t;
1566 typedef struct thrmisc32 elf_thrmisc_t;
1568 typedef prstatus_t elf_prstatus_t;
1569 typedef prpsinfo_t elf_prpsinfo_t;
1570 typedef prfpregset_t elf_prfpregset_t;
1571 typedef prfpregset_t elf_fpregset_t;
1572 typedef gregset_t elf_gregset_t;
1573 typedef thrmisc_t elf_thrmisc_t;
1577 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
1580 elf_prpsinfo_t *psinfo;
1582 p = (struct proc *)arg;
1584 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1585 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
1586 psinfo->pr_version = PRPSINFO_VERSION;
1587 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1588 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1590 * XXX - We don't fill in the command line arguments properly
1593 strlcpy(psinfo->pr_psargs, p->p_comm,
1594 sizeof(psinfo->pr_psargs));
1596 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1597 free(psinfo, M_TEMP);
1599 *sizep = sizeof(*psinfo);
1603 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1606 elf_prstatus_t *status;
1608 td = (struct thread *)arg;
1610 KASSERT(*sizep == sizeof(*status), ("invalid size"));
1611 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
1612 status->pr_version = PRSTATUS_VERSION;
1613 status->pr_statussz = sizeof(elf_prstatus_t);
1614 status->pr_gregsetsz = sizeof(elf_gregset_t);
1615 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
1616 status->pr_osreldate = osreldate;
1617 status->pr_cursig = td->td_proc->p_sig;
1618 status->pr_pid = td->td_tid;
1619 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1620 fill_regs32(td, &status->pr_reg);
1622 fill_regs(td, &status->pr_reg);
1624 sbuf_bcat(sb, status, sizeof(*status));
1625 free(status, M_TEMP);
1627 *sizep = sizeof(*status);
1631 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1634 elf_prfpregset_t *fpregset;
1636 td = (struct thread *)arg;
1638 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1639 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1640 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1641 fill_fpregs32(td, fpregset);
1643 fill_fpregs(td, fpregset);
1645 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1646 free(fpregset, M_TEMP);
1648 *sizep = sizeof(*fpregset);
1652 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1655 elf_thrmisc_t thrmisc;
1657 td = (struct thread *)arg;
1659 KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1660 bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1661 strcpy(thrmisc.pr_tname, td->td_name);
1662 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1664 *sizep = sizeof(thrmisc);
1668 * Allow for MD specific notes, as well as any MD
1669 * specific preparations for writing MI notes.
1672 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
1678 td = (struct thread *)arg;
1681 if (size != 0 && sb != NULL)
1682 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
1684 __elfN(dump_thread)(td, buf, &size);
1685 KASSERT(*sizep == size, ("invalid size"));
1686 if (size != 0 && sb != NULL)
1687 sbuf_bcat(sb, buf, size);
1692 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
1693 int32_t *osrel, const Elf_Phdr *pnote)
1695 const Elf_Note *note, *note0, *note_end;
1696 const char *note_name;
1699 if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
1700 pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
1703 note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
1704 note_end = (const Elf_Note *)(imgp->image_header +
1705 pnote->p_offset + pnote->p_filesz);
1706 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
1707 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
1708 (const char *)note < sizeof(Elf_Note))
1710 if (note->n_namesz != checknote->hdr.n_namesz ||
1711 note->n_descsz != checknote->hdr.n_descsz ||
1712 note->n_type != checknote->hdr.n_type)
1714 note_name = (const char *)(note + 1);
1715 if (note_name + checknote->hdr.n_namesz >=
1716 (const char *)note_end || strncmp(checknote->vendor,
1717 note_name, checknote->hdr.n_namesz) != 0)
1721 * Fetch the osreldate for binary
1722 * from the ELF OSABI-note if necessary.
1724 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
1725 checknote->trans_osrel != NULL)
1726 return (checknote->trans_osrel(note, osrel));
1730 note = (const Elf_Note *)((const char *)(note + 1) +
1731 roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
1732 roundup2(note->n_descsz, sizeof(Elf32_Addr)));
1739 * Try to find the appropriate ABI-note section for checknote,
1740 * fetch the osreldate for binary from the ELF OSABI-note. Only the
1741 * first page of the image is searched, the same as for headers.
1744 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
1747 const Elf_Phdr *phdr;
1748 const Elf_Ehdr *hdr;
1751 hdr = (const Elf_Ehdr *)imgp->image_header;
1752 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1754 for (i = 0; i < hdr->e_phnum; i++) {
1755 if (phdr[i].p_type == PT_NOTE &&
1756 __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
1764 * Tell kern_execve.c about it, with a little help from the linker.
1766 static struct execsw __elfN(execsw) = {
1767 __CONCAT(exec_, __elfN(imgact)),
1768 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
1770 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
1772 #ifdef COMPRESS_USER_CORES
1774 * Compress and write out a core segment for a user process.
1776 * 'inbuf' is the starting address of a VM segment in the process' address
1777 * space that is to be compressed and written out to the core file. 'dest_buf'
1778 * is a buffer in the kernel's address space. The segment is copied from
1779 * 'inbuf' to 'dest_buf' first before being processed by the compression
1780 * routine gzwrite(). This copying is necessary because the content of the VM
1781 * segment may change between the compression pass and the crc-computation pass
1782 * in gzwrite(). This is because realtime threads may preempt the UNIX kernel.
1784 * If inbuf is NULL it is assumed that data is already copied to 'dest_buf'.
1787 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
1792 unsigned int chunk_len;
1795 if (inbuf != NULL) {
1796 chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
1797 copyin(inbuf, dest_buf, chunk_len);
1802 len_compressed = gzwrite(file, dest_buf, chunk_len);
1804 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
1806 if ((unsigned int)len_compressed != chunk_len) {
1808 "compress_core: length mismatch (0x%x returned, "
1809 "0x%x expected)\n", len_compressed, chunk_len);
1810 EVENTHANDLER_INVOKE(app_coredump_error, td,
1811 "compress_core: length mismatch %x -> %x",
1812 chunk_len, len_compressed);
1822 #endif /* COMPRESS_USER_CORES */
1825 __elfN(trans_prot)(Elf_Word flags)
1831 prot |= VM_PROT_EXECUTE;
1833 prot |= VM_PROT_WRITE;
1835 prot |= VM_PROT_READ;
1836 #if __ELF_WORD_SIZE == 32
1837 #if defined(__amd64__) || defined(__ia64__)
1838 if (i386_read_exec && (flags & PF_R))
1839 prot |= VM_PROT_EXECUTE;
1846 __elfN(untrans_prot)(vm_prot_t prot)
1851 if (prot & VM_PROT_EXECUTE)
1853 if (prot & VM_PROT_READ)
1855 if (prot & VM_PROT_WRITE)