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/filedesc.h>
43 #include <sys/imgact.h>
44 #include <sys/imgact_elf.h>
45 #include <sys/kernel.h>
47 #include <sys/malloc.h>
48 #include <sys/mount.h>
49 #include <sys/mutex.h>
51 #include <sys/namei.h>
52 #include <sys/pioctl.h>
54 #include <sys/procfs.h>
55 #include <sys/racct.h>
56 #include <sys/resourcevar.h>
58 #include <sys/sf_buf.h>
60 #include <sys/systm.h>
61 #include <sys/signalvar.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>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_param.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_extern.h>
82 #include <machine/elf.h>
83 #include <machine/md_var.h>
85 #define ELF_NOTE_ROUNDSIZE 4
86 #define OLD_EI_BRAND 8
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 vmspace *vmspace, vm_object_t object,
94 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
95 vm_prot_t prot, 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,
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);
105 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
108 #ifdef COMPRESS_USER_CORES
109 static int compress_core(gzFile, char *, char *, unsigned int,
112 #define CORE_BUF_SIZE (16 * 1024)
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));
121 static int elf_legacy_coredump = 0;
122 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
123 &elf_legacy_coredump, 0, "");
125 int __elfN(nxstack) =
126 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
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");
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");
143 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
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))
149 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
151 Elf_Brandnote __elfN(freebsd_brandnote) = {
152 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
153 .hdr.n_descsz = sizeof(int32_t),
155 .vendor = FREEBSD_ABI_VENDOR,
156 .flags = BN_TRANSLATE_OSREL,
157 .trans_osrel = __elfN(freebsd_trans_osrel)
161 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
165 p = (uintptr_t)(note + 1);
166 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
167 *osrel = *(const int32_t *)(p);
172 static const char GNU_ABI_VENDOR[] = "GNU";
173 static int GNU_KFREEBSD_ABI_DESC = 3;
175 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
176 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
177 .hdr.n_descsz = 16, /* XXX at least 16 */
179 .vendor = GNU_ABI_VENDOR,
180 .flags = BN_TRANSLATE_OSREL,
181 .trans_osrel = kfreebsd_trans_osrel
185 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
187 const Elf32_Word *desc;
190 p = (uintptr_t)(note + 1);
191 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
193 desc = (const Elf32_Word *)p;
194 if (desc[0] != GNU_KFREEBSD_ABI_DESC)
198 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
199 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
201 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
207 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
211 for (i = 0; i < MAX_BRANDS; i++) {
212 if (elf_brand_list[i] == NULL) {
213 elf_brand_list[i] = entry;
217 if (i == MAX_BRANDS) {
218 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
226 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
230 for (i = 0; i < MAX_BRANDS; i++) {
231 if (elf_brand_list[i] == entry) {
232 elf_brand_list[i] = NULL;
242 __elfN(brand_inuse)(Elf_Brandinfo *entry)
247 sx_slock(&allproc_lock);
248 FOREACH_PROC_IN_SYSTEM(p) {
249 if (p->p_sysent == entry->sysvec) {
254 sx_sunlock(&allproc_lock);
259 static Elf_Brandinfo *
260 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
261 int interp_name_len, int32_t *osrel)
263 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
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.
275 /* Look for an ".note.ABI-tag" ELF section */
276 for (i = 0; i < MAX_BRANDS; i++) {
277 bi = elf_brand_list[i];
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);
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)
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))
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)
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)
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)
320 if (hdr->e_machine == bi->machine &&
321 __elfN(fallback_brand) == bi->brand)
328 __elfN(check_header)(const Elf_Ehdr *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)
342 * Make sure we have at least one brand for this machine.
345 for (i = 0; i < MAX_BRANDS; i++) {
346 bi = elf_brand_list[i];
347 if (bi != NULL && bi->machine == hdr->e_machine)
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)
365 * Create the page if it doesn't exist yet. Ignore errors.
368 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
369 VM_PROT_ALL, VM_PROT_ALL, 0);
373 * Find the page from the underlying object.
376 sf = vm_imgact_map_page(object, offset);
378 return (KERN_FAILURE);
379 off = offset - trunc_page(offset);
380 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
382 vm_imgact_unmap_page(sf);
384 return (KERN_FAILURE);
388 return (KERN_SUCCESS);
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)
400 if (start != trunc_page(start)) {
401 rv = __elfN(map_partial)(map, object, offset, start,
402 round_page(start), prot);
405 offset += round_page(start) - start;
406 start = round_page(start);
408 if (end != round_page(end)) {
409 rv = __elfN(map_partial)(map, object, offset +
410 trunc_page(end) - start, trunc_page(end), end, prot);
413 end = trunc_page(end);
416 if (offset & PAGE_MASK) {
418 * The mapping is not page aligned. This means we have
419 * to copy the data. Sigh.
421 rv = vm_map_find(map, NULL, 0, &start, end - start,
422 FALSE, prot | VM_PROT_WRITE, VM_PROT_ALL, 0);
426 return (KERN_SUCCESS);
427 for (; start < end; start += sz) {
428 sf = vm_imgact_map_page(object, offset);
430 return (KERN_FAILURE);
431 off = offset - trunc_page(offset);
433 if (sz > PAGE_SIZE - off)
434 sz = PAGE_SIZE - off;
435 error = copyout((caddr_t)sf_buf_kva(sf) + off,
437 vm_imgact_unmap_page(sf);
439 return (KERN_FAILURE);
445 vm_object_reference(object);
447 rv = vm_map_insert(map, object, offset, start, end,
448 prot, VM_PROT_ALL, cow);
450 if (rv != KERN_SUCCESS)
451 vm_object_deallocate(object);
455 return (KERN_SUCCESS);
460 __elfN(load_section)(struct vmspace *vmspace,
461 vm_object_t object, vm_offset_t offset,
462 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
467 vm_offset_t map_addr;
470 vm_offset_t file_addr;
473 * It's necessary to fail if the filsz + offset taken from the
474 * header is greater than the actual file pager object's size.
475 * If we were to allow this, then the vm_map_find() below would
476 * walk right off the end of the file object and into the ether.
478 * While I'm here, might as well check for something else that
479 * is invalid: filsz cannot be greater than memsz.
481 if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
483 uprintf("elf_load_section: truncated ELF file\n");
487 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
488 file_addr = trunc_page_ps(offset, pagesize);
491 * We have two choices. We can either clear the data in the last page
492 * of an oversized mapping, or we can start the anon mapping a page
493 * early and copy the initialized data into that first page. We
494 * choose the second..
497 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
499 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
502 /* cow flags: don't dump readonly sections in core */
503 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
504 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
506 rv = __elfN(map_insert)(&vmspace->vm_map,
508 file_addr, /* file offset */
509 map_addr, /* virtual start */
510 map_addr + map_len,/* virtual end */
513 if (rv != KERN_SUCCESS)
516 /* we can stop now if we've covered it all */
517 if (memsz == filsz) {
524 * We have to get the remaining bit of the file into the first part
525 * of the oversized map segment. This is normally because the .data
526 * segment in the file is extended to provide bss. It's a neat idea
527 * to try and save a page, but it's a pain in the behind to implement.
529 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
530 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
531 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
534 /* This had damn well better be true! */
536 rv = __elfN(map_insert)(&vmspace->vm_map, NULL, 0, map_addr,
537 map_addr + map_len, VM_PROT_ALL, 0);
538 if (rv != KERN_SUCCESS) {
546 sf = vm_imgact_map_page(object, offset + filsz);
550 /* send the page fragment to user space */
551 off = trunc_page_ps(offset + filsz, pagesize) -
552 trunc_page(offset + filsz);
553 error = copyout((caddr_t)sf_buf_kva(sf) + off,
554 (caddr_t)map_addr, copy_len);
555 vm_imgact_unmap_page(sf);
562 * set it to the specified protection.
563 * XXX had better undo the damage from pasting over the cracks here!
565 vm_map_protect(&vmspace->vm_map, trunc_page(map_addr),
566 round_page(map_addr + map_len), prot, FALSE);
572 * Load the file "file" into memory. It may be either a shared object
575 * The "addr" reference parameter is in/out. On entry, it specifies
576 * the address where a shared object should be loaded. If the file is
577 * an executable, this value is ignored. On exit, "addr" specifies
578 * where the file was actually loaded.
580 * The "entry" reference parameter is out only. On exit, it specifies
581 * the entry point for the loaded file.
584 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
585 u_long *entry, size_t pagesize)
590 struct image_params image_params;
592 const Elf_Ehdr *hdr = NULL;
593 const Elf_Phdr *phdr = NULL;
594 struct nameidata *nd;
595 struct vmspace *vmspace = p->p_vmspace;
597 struct image_params *imgp;
600 u_long base_addr = 0;
601 int vfslocked, error, i, numsegs;
603 #ifdef CAPABILITY_MODE
605 * XXXJA: This check can go away once we are sufficiently confident
606 * that the checks in namei() are correct.
608 if (IN_CAPABILITY_MODE(curthread))
612 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
614 attr = &tempdata->attr;
615 imgp = &tempdata->image_params;
618 * Initialize part of the common data
622 imgp->firstpage = NULL;
623 imgp->image_header = NULL;
625 imgp->execlabel = NULL;
627 NDINIT(nd, LOOKUP, MPSAFE|LOCKLEAF|FOLLOW, UIO_SYSSPACE, file,
630 if ((error = namei(nd)) != 0) {
634 vfslocked = NDHASGIANT(nd);
635 NDFREE(nd, NDF_ONLY_PNBUF);
636 imgp->vp = nd->ni_vp;
639 * Check permissions, modes, uid, etc on the file, and "open" it.
641 error = exec_check_permissions(imgp);
645 error = exec_map_first_page(imgp);
650 * Also make certain that the interpreter stays the same, so set
651 * its VV_TEXT flag, too.
653 VOP_SET_TEXT(nd->ni_vp);
655 imgp->object = nd->ni_vp->v_object;
657 hdr = (const Elf_Ehdr *)imgp->image_header;
658 if ((error = __elfN(check_header)(hdr)) != 0)
660 if (hdr->e_type == ET_DYN)
662 else if (hdr->e_type == ET_EXEC)
669 /* Only support headers that fit within first page for now */
670 if ((hdr->e_phoff > PAGE_SIZE) ||
671 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
676 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
677 if (!aligned(phdr, Elf_Addr)) {
682 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
683 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
684 /* Loadable segment */
685 prot = __elfN(trans_prot)(phdr[i].p_flags);
686 if ((error = __elfN(load_section)(vmspace,
687 imgp->object, phdr[i].p_offset,
688 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
689 phdr[i].p_memsz, phdr[i].p_filesz, prot,
693 * Establish the base address if this is the
697 base_addr = trunc_page(phdr[i].p_vaddr +
703 *entry = (unsigned long)hdr->e_entry + rbase;
707 exec_unmap_first_page(imgp);
712 VFS_UNLOCK_GIANT(vfslocked);
713 free(tempdata, M_TEMP);
719 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
721 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
722 const Elf_Phdr *phdr;
723 Elf_Auxargs *elf_auxargs;
724 struct vmspace *vmspace;
726 u_long text_size = 0, data_size = 0, total_size = 0;
727 u_long text_addr = 0, data_addr = 0;
728 u_long seg_size, seg_addr;
729 u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
731 int error = 0, i, n, interp_name_len = 0;
732 const char *interp = NULL, *newinterp = NULL;
733 Elf_Brandinfo *brand_info;
735 struct sysentvec *sv;
738 * Do we have a valid ELF header ?
740 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
741 * if particular brand doesn't support it.
743 if (__elfN(check_header)(hdr) != 0 ||
744 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
748 * From here on down, we return an errno, not -1, as we've
749 * detected an ELF file.
752 if ((hdr->e_phoff > PAGE_SIZE) ||
753 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
754 /* Only support headers in first page for now */
757 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
758 if (!aligned(phdr, Elf_Addr))
762 for (i = 0; i < hdr->e_phnum; i++) {
763 switch (phdr[i].p_type) {
766 baddr = phdr[i].p_vaddr;
770 /* Path to interpreter */
771 if (phdr[i].p_filesz > MAXPATHLEN ||
772 phdr[i].p_offset > PAGE_SIZE ||
773 phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset)
775 interp = imgp->image_header + phdr[i].p_offset;
776 interp_name_len = phdr[i].p_filesz;
781 __elfN(trans_prot)(phdr[i].p_flags);
786 brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
788 if (brand_info == NULL) {
789 uprintf("ELF binary type \"%u\" not known.\n",
790 hdr->e_ident[EI_OSABI]);
793 if (hdr->e_type == ET_DYN) {
794 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0)
797 * Honour the base load address from the dso if it is
798 * non-zero for some reason.
801 et_dyn_addr = ET_DYN_LOAD_ADDR;
806 sv = brand_info->sysvec;
807 if (interp != NULL && brand_info->interp_newpath != NULL)
808 newinterp = brand_info->interp_newpath;
811 * Avoid a possible deadlock if the current address space is destroyed
812 * and that address space maps the locked vnode. In the common case,
813 * the locked vnode's v_usecount is decremented but remains greater
814 * than zero. Consequently, the vnode lock is not needed by vrele().
815 * However, in cases where the vnode lock is external, such as nullfs,
816 * v_usecount may become zero.
818 VOP_UNLOCK(imgp->vp, 0);
820 error = exec_new_vmspace(imgp, sv);
821 imgp->proc->p_sysent = sv;
823 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
827 vmspace = imgp->proc->p_vmspace;
829 for (i = 0; i < hdr->e_phnum; i++) {
830 switch (phdr[i].p_type) {
831 case PT_LOAD: /* Loadable segment */
832 if (phdr[i].p_memsz == 0)
834 prot = __elfN(trans_prot)(phdr[i].p_flags);
835 if ((error = __elfN(load_section)(vmspace,
836 imgp->object, phdr[i].p_offset,
837 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
838 phdr[i].p_memsz, phdr[i].p_filesz, prot,
839 sv->sv_pagesize)) != 0)
843 * If this segment contains the program headers,
844 * remember their virtual address for the AT_PHDR
845 * aux entry. Static binaries don't usually include
848 if (phdr[i].p_offset == 0 &&
849 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
851 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
854 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
855 seg_size = round_page(phdr[i].p_memsz +
856 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
859 * Make the largest executable segment the official
860 * text segment and all others data.
862 * Note that obreak() assumes that data_addr +
863 * data_size == end of data load area, and the ELF
864 * file format expects segments to be sorted by
865 * address. If multiple data segments exist, the
866 * last one will be used.
869 if (phdr[i].p_flags & PF_X && text_size < seg_size) {
870 text_size = seg_size;
871 text_addr = seg_addr;
873 data_size = seg_size;
874 data_addr = seg_addr;
876 total_size += seg_size;
878 case PT_PHDR: /* Program header table info */
879 proghdr = phdr[i].p_vaddr + et_dyn_addr;
886 if (data_addr == 0 && data_size == 0) {
887 data_addr = text_addr;
888 data_size = text_size;
891 entry = (u_long)hdr->e_entry + et_dyn_addr;
894 * Check limits. It should be safe to check the
895 * limits after loading the segments since we do
896 * not actually fault in all the segments pages.
898 PROC_LOCK(imgp->proc);
899 if (data_size > lim_cur(imgp->proc, RLIMIT_DATA) ||
900 text_size > maxtsiz ||
901 total_size > lim_cur(imgp->proc, RLIMIT_VMEM) ||
902 racct_set(imgp->proc, RACCT_DATA, data_size) != 0 ||
903 racct_set(imgp->proc, RACCT_VMEM, total_size) != 0) {
904 PROC_UNLOCK(imgp->proc);
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;
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.
919 addr = round_page((vm_offset_t)imgp->proc->p_vmspace->vm_daddr +
920 lim_max(imgp->proc, RLIMIT_DATA));
921 PROC_UNLOCK(imgp->proc);
923 imgp->entry_addr = entry;
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);
939 if (!have_interp && newinterp != NULL) {
940 error = __elfN(load_file)(imgp->proc, newinterp, &addr,
941 &imgp->entry_addr, sv->sv_pagesize);
946 error = __elfN(load_file)(imgp->proc, interp, &addr,
947 &imgp->entry_addr, sv->sv_pagesize);
949 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
951 uprintf("ELF interpreter %s not found\n", interp);
958 * Construct auxargs table (used by the fixup routine)
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;
970 imgp->auxargs = elf_auxargs;
971 imgp->interpreted = 0;
972 imgp->reloc_base = addr;
973 imgp->proc->p_osrel = osrel;
978 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
981 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
983 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
987 base = (Elf_Addr *)*stack_base;
988 pos = base + (imgp->args->argc + imgp->args->envc + 2);
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, osreldate);
1002 if (imgp->canary != 0) {
1003 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1004 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1006 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1007 if (imgp->pagesizes != 0) {
1008 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1009 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1011 if (imgp->sysent->sv_timekeep_base != 0) {
1012 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1013 imgp->sysent->sv_timekeep_base);
1015 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1016 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1017 imgp->sysent->sv_stackprot);
1018 AUXARGS_ENTRY(pos, AT_NULL, 0);
1020 free(imgp->auxargs, M_TEMP);
1021 imgp->auxargs = NULL;
1024 suword(base, (long)imgp->args->argc);
1025 *stack_base = (register_t *)base;
1030 * Code for generating ELF core dumps.
1033 typedef void (*segment_callback)(vm_map_entry_t, void *);
1035 /* Closure for cb_put_phdr(). */
1036 struct phdr_closure {
1037 Elf_Phdr *phdr; /* Program header to fill in */
1038 Elf_Off offset; /* Offset of segment in core file */
1041 /* Closure for cb_size_segment(). */
1042 struct sseg_closure {
1043 int count; /* Count of writable segments. */
1044 size_t size; /* Total size of all writable segments. */
1047 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1050 int type; /* Note type. */
1051 outfunc_t outfunc; /* Output function. */
1052 void *outarg; /* Argument for the output function. */
1053 size_t outsize; /* Output size. */
1054 TAILQ_ENTRY(note_info) link; /* Link to the next note info. */
1057 TAILQ_HEAD(note_info_list, note_info);
1059 static void cb_put_phdr(vm_map_entry_t, void *);
1060 static void cb_size_segment(vm_map_entry_t, void *);
1061 static void each_writable_segment(struct thread *, segment_callback, void *);
1062 static int __elfN(corehdr)(struct thread *, struct vnode *, struct ucred *,
1063 int, void *, size_t, struct note_info_list *, size_t, gzFile);
1064 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1066 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1067 static void __elfN(putnote)(struct note_info *, struct sbuf *);
1068 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1069 static int sbuf_drain_core_output(void *, const char *, int);
1070 static int sbuf_drain_count(void *arg, const char *data, int len);
1072 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1073 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1074 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1075 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1076 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1077 static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1078 static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1079 static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1080 static void note_procstat_files(void *, struct sbuf *, size_t *);
1081 static void note_procstat_groups(void *, struct sbuf *, size_t *);
1082 static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1083 static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1084 static void note_procstat_umask(void *, struct sbuf *, size_t *);
1085 static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
1087 #ifdef COMPRESS_USER_CORES
1088 extern int compress_user_cores;
1089 extern int compress_user_cores_gzlevel;
1093 core_output(struct vnode *vp, void *base, size_t len, off_t offset,
1094 struct ucred *active_cred, struct ucred *file_cred,
1095 struct thread *td, char *core_buf, gzFile gzfile) {
1099 #ifdef COMPRESS_USER_CORES
1100 error = compress_core(gzfile, base, core_buf, len, td);
1102 panic("shouldn't be here");
1105 error = vn_rdwr_inchunks(UIO_WRITE, vp, base, len, offset,
1106 UIO_USERSPACE, IO_UNIT | IO_DIRECT, active_cred, file_cred,
1112 /* Coredump output parameters for sbuf drain routine. */
1113 struct sbuf_drain_core_params {
1115 struct ucred *active_cred;
1116 struct ucred *file_cred;
1119 #ifdef COMPRESS_USER_CORES
1125 * Drain into a core file.
1128 sbuf_drain_core_output(void *arg, const char *data, int len)
1130 struct sbuf_drain_core_params *p;
1133 p = (struct sbuf_drain_core_params *)arg;
1136 * Some kern_proc out routines that print to this sbuf may
1137 * call us with the process lock held. Draining with the
1138 * non-sleepable lock held is unsafe. The lock is needed for
1139 * those routines when dumping a live process. In our case we
1140 * can safely release the lock before draining and acquire
1143 locked = PROC_LOCKED(p->td->td_proc);
1145 PROC_UNLOCK(p->td->td_proc);
1146 #ifdef COMPRESS_USER_CORES
1147 if (p->gzfile != Z_NULL)
1148 error = compress_core(p->gzfile, NULL, __DECONST(char *, data),
1152 error = vn_rdwr_inchunks(UIO_WRITE, p->vp,
1153 __DECONST(void *, data), len, p->offset, UIO_SYSSPACE,
1154 IO_UNIT | IO_DIRECT, p->active_cred, p->file_cred, NULL,
1157 PROC_LOCK(p->td->td_proc);
1165 * Drain into a counter.
1168 sbuf_drain_count(void *arg, const char *data __unused, int len)
1172 sizep = (size_t *)arg;
1178 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1180 struct ucred *cred = td->td_ucred;
1182 struct sseg_closure seginfo;
1183 struct note_info_list notelst;
1184 struct note_info *ninfo;
1186 size_t hdrsize, notesz, coresize;
1188 gzFile gzfile = Z_NULL;
1189 char *core_buf = NULL;
1190 #ifdef COMPRESS_USER_CORES
1191 char gzopen_flags[8];
1193 int doing_compress = flags & IMGACT_CORE_COMPRESS;
1197 TAILQ_INIT(¬elst);
1199 #ifdef COMPRESS_USER_CORES
1200 if (doing_compress) {
1203 if (compress_user_cores_gzlevel >= 0 &&
1204 compress_user_cores_gzlevel <= 9)
1205 *p++ = '0' + compress_user_cores_gzlevel;
1207 gzfile = gz_open("", gzopen_flags, vp);
1208 if (gzfile == Z_NULL) {
1212 core_buf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1220 /* Size the program segments. */
1223 each_writable_segment(td, cb_size_segment, &seginfo);
1226 * Collect info about the core file header area.
1228 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1229 __elfN(prepare_notes)(td, ¬elst, ¬esz);
1230 coresize = round_page(hdrsize + notesz) + seginfo.size;
1233 PROC_LOCK(td->td_proc);
1234 error = racct_add(td->td_proc, RACCT_CORE, coresize);
1235 PROC_UNLOCK(td->td_proc);
1241 if (coresize >= limit) {
1247 * Allocate memory for building the header, fill it up,
1248 * and write it out following the notes.
1250 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1255 error = __elfN(corehdr)(td, vp, cred, seginfo.count, hdr, hdrsize,
1256 ¬elst, notesz, gzfile);
1258 /* Write the contents of all of the writable segments. */
1264 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1265 offset = round_page(hdrsize + notesz);
1266 for (i = 0; i < seginfo.count; i++) {
1267 error = core_output(vp, (caddr_t)(uintptr_t)php->p_vaddr,
1268 php->p_filesz, offset, cred, NOCRED, curthread, core_buf, gzfile);
1271 offset += php->p_filesz;
1277 "Failed to write core file for process %s (error %d)\n",
1278 curproc->p_comm, error);
1282 #ifdef COMPRESS_USER_CORES
1284 free(core_buf, M_TEMP);
1288 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) {
1289 TAILQ_REMOVE(¬elst, ninfo, link);
1290 free(ninfo, M_TEMP);
1299 * A callback for each_writable_segment() to write out the segment's
1300 * program header entry.
1303 cb_put_phdr(entry, closure)
1304 vm_map_entry_t entry;
1307 struct phdr_closure *phc = (struct phdr_closure *)closure;
1308 Elf_Phdr *phdr = phc->phdr;
1310 phc->offset = round_page(phc->offset);
1312 phdr->p_type = PT_LOAD;
1313 phdr->p_offset = phc->offset;
1314 phdr->p_vaddr = entry->start;
1316 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1317 phdr->p_align = PAGE_SIZE;
1318 phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1320 phc->offset += phdr->p_filesz;
1325 * A callback for each_writable_segment() to gather information about
1326 * the number of segments and their total size.
1329 cb_size_segment(entry, closure)
1330 vm_map_entry_t entry;
1333 struct sseg_closure *ssc = (struct sseg_closure *)closure;
1336 ssc->size += entry->end - entry->start;
1340 * For each writable segment in the process's memory map, call the given
1341 * function with a pointer to the map entry and some arbitrary
1342 * caller-supplied data.
1345 each_writable_segment(td, func, closure)
1347 segment_callback func;
1350 struct proc *p = td->td_proc;
1351 vm_map_t map = &p->p_vmspace->vm_map;
1352 vm_map_entry_t entry;
1353 vm_object_t backing_object, object;
1354 boolean_t ignore_entry;
1356 vm_map_lock_read(map);
1357 for (entry = map->header.next; entry != &map->header;
1358 entry = entry->next) {
1360 * Don't dump inaccessible mappings, deal with legacy
1363 * Note that read-only segments related to the elf binary
1364 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1365 * need to arbitrarily ignore such segments.
1367 if (elf_legacy_coredump) {
1368 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1371 if ((entry->protection & VM_PROT_ALL) == 0)
1376 * Dont include memory segment in the coredump if
1377 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1378 * madvise(2). Do not dump submaps (i.e. parts of the
1381 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1384 if ((object = entry->object.vm_object) == NULL)
1387 /* Ignore memory-mapped devices and such things. */
1388 VM_OBJECT_LOCK(object);
1389 while ((backing_object = object->backing_object) != NULL) {
1390 VM_OBJECT_LOCK(backing_object);
1391 VM_OBJECT_UNLOCK(object);
1392 object = backing_object;
1394 ignore_entry = object->type != OBJT_DEFAULT &&
1395 object->type != OBJT_SWAP && object->type != OBJT_VNODE;
1396 VM_OBJECT_UNLOCK(object);
1400 (*func)(entry, closure);
1402 vm_map_unlock_read(map);
1406 * Write the core file header to the file, including padding up to
1407 * the page boundary.
1410 __elfN(corehdr)(struct thread *td, struct vnode *vp, struct ucred *cred,
1411 int numsegs, void *hdr, size_t hdrsize, struct note_info_list *notelst,
1412 size_t notesz, gzFile gzfile)
1414 struct sbuf_drain_core_params params;
1415 struct note_info *ninfo;
1419 /* Fill in the header. */
1420 bzero(hdr, hdrsize);
1421 __elfN(puthdr)(td, hdr, hdrsize, numsegs, notesz);
1424 params.active_cred = cred;
1425 params.file_cred = NOCRED;
1428 #ifdef COMPRESS_USER_CORES
1429 params.gzfile = gzfile;
1431 sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1432 sbuf_set_drain(sb, sbuf_drain_core_output, ¶ms);
1433 sbuf_start_section(sb, NULL);
1434 sbuf_bcat(sb, hdr, hdrsize);
1435 TAILQ_FOREACH(ninfo, notelst, link)
1436 __elfN(putnote)(ninfo, sb);
1437 /* Align up to a page boundary for the program segments. */
1438 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1439 error = sbuf_finish(sb);
1446 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1456 size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1459 * To have the debugger select the right thread (LWP) as the initial
1460 * thread, we dump the state of the thread passed to us in td first.
1461 * This is the thread that causes the core dump and thus likely to
1462 * be the right thread one wants to have selected in the debugger.
1465 while (thr != NULL) {
1466 size += register_note(list, NT_PRSTATUS,
1467 __elfN(note_prstatus), thr);
1468 size += register_note(list, NT_FPREGSET,
1469 __elfN(note_fpregset), thr);
1470 size += register_note(list, NT_THRMISC,
1471 __elfN(note_thrmisc), thr);
1472 size += register_note(list, -1,
1473 __elfN(note_threadmd), thr);
1475 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1476 TAILQ_NEXT(thr, td_plist);
1478 thr = TAILQ_NEXT(thr, td_plist);
1481 size += register_note(list, NT_PROCSTAT_PROC,
1482 __elfN(note_procstat_proc), p);
1483 size += register_note(list, NT_PROCSTAT_FILES,
1484 note_procstat_files, p);
1485 size += register_note(list, NT_PROCSTAT_VMMAP,
1486 note_procstat_vmmap, p);
1487 size += register_note(list, NT_PROCSTAT_GROUPS,
1488 note_procstat_groups, p);
1489 size += register_note(list, NT_PROCSTAT_UMASK,
1490 note_procstat_umask, p);
1491 size += register_note(list, NT_PROCSTAT_RLIMIT,
1492 note_procstat_rlimit, p);
1493 size += register_note(list, NT_PROCSTAT_OSREL,
1494 note_procstat_osrel, p);
1495 size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1496 __elfN(note_procstat_psstrings), p);
1497 size += register_note(list, NT_PROCSTAT_AUXV,
1498 __elfN(note_procstat_auxv), p);
1504 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1509 struct phdr_closure phc;
1511 ehdr = (Elf_Ehdr *)hdr;
1512 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
1514 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1515 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1516 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1517 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1518 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1519 ehdr->e_ident[EI_DATA] = ELF_DATA;
1520 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1521 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1522 ehdr->e_ident[EI_ABIVERSION] = 0;
1523 ehdr->e_ident[EI_PAD] = 0;
1524 ehdr->e_type = ET_CORE;
1525 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1526 ehdr->e_machine = ELF_ARCH32;
1528 ehdr->e_machine = ELF_ARCH;
1530 ehdr->e_version = EV_CURRENT;
1532 ehdr->e_phoff = sizeof(Elf_Ehdr);
1534 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1535 ehdr->e_phentsize = sizeof(Elf_Phdr);
1536 ehdr->e_phnum = numsegs + 1;
1537 ehdr->e_shentsize = sizeof(Elf_Shdr);
1539 ehdr->e_shstrndx = SHN_UNDEF;
1542 * Fill in the program header entries.
1545 /* The note segement. */
1546 phdr->p_type = PT_NOTE;
1547 phdr->p_offset = hdrsize;
1550 phdr->p_filesz = notesz;
1552 phdr->p_flags = PF_R;
1553 phdr->p_align = ELF_NOTE_ROUNDSIZE;
1556 /* All the writable segments from the program. */
1558 phc.offset = round_page(hdrsize + notesz);
1559 each_writable_segment(td, cb_put_phdr, &phc);
1563 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1565 struct note_info *ninfo;
1566 size_t size, notesize;
1569 out(arg, NULL, &size);
1570 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1572 ninfo->outfunc = out;
1573 ninfo->outarg = arg;
1574 ninfo->outsize = size;
1575 TAILQ_INSERT_TAIL(list, ninfo, link);
1580 notesize = sizeof(Elf_Note) + /* note header */
1581 roundup2(8, ELF_NOTE_ROUNDSIZE) + /* note name ("FreeBSD") */
1582 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
1588 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1593 if (ninfo->type == -1) {
1594 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1598 note.n_namesz = 8; /* strlen("FreeBSD") + 1 */
1599 note.n_descsz = ninfo->outsize;
1600 note.n_type = ninfo->type;
1602 sbuf_bcat(sb, ¬e, sizeof(note));
1603 sbuf_start_section(sb, &old_len);
1604 sbuf_bcat(sb, "FreeBSD", note.n_namesz);
1605 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1606 if (note.n_descsz == 0)
1608 sbuf_start_section(sb, &old_len);
1609 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1610 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1614 * Miscellaneous note out functions.
1617 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1618 #include <compat/freebsd32/freebsd32.h>
1620 typedef struct prstatus32 elf_prstatus_t;
1621 typedef struct prpsinfo32 elf_prpsinfo_t;
1622 typedef struct fpreg32 elf_prfpregset_t;
1623 typedef struct fpreg32 elf_fpregset_t;
1624 typedef struct reg32 elf_gregset_t;
1625 typedef struct thrmisc32 elf_thrmisc_t;
1626 #define ELF_KERN_PROC_MASK KERN_PROC_MASK32
1627 typedef struct kinfo_proc32 elf_kinfo_proc_t;
1628 typedef uint32_t elf_ps_strings_t;
1630 typedef prstatus_t elf_prstatus_t;
1631 typedef prpsinfo_t elf_prpsinfo_t;
1632 typedef prfpregset_t elf_prfpregset_t;
1633 typedef prfpregset_t elf_fpregset_t;
1634 typedef gregset_t elf_gregset_t;
1635 typedef thrmisc_t elf_thrmisc_t;
1636 #define ELF_KERN_PROC_MASK 0
1637 typedef struct kinfo_proc elf_kinfo_proc_t;
1638 typedef vm_offset_t elf_ps_strings_t;
1642 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
1645 elf_prpsinfo_t *psinfo;
1647 p = (struct proc *)arg;
1649 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1650 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
1651 psinfo->pr_version = PRPSINFO_VERSION;
1652 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1653 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1655 * XXX - We don't fill in the command line arguments properly
1658 strlcpy(psinfo->pr_psargs, p->p_comm,
1659 sizeof(psinfo->pr_psargs));
1661 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1662 free(psinfo, M_TEMP);
1664 *sizep = sizeof(*psinfo);
1668 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1671 elf_prstatus_t *status;
1673 td = (struct thread *)arg;
1675 KASSERT(*sizep == sizeof(*status), ("invalid size"));
1676 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
1677 status->pr_version = PRSTATUS_VERSION;
1678 status->pr_statussz = sizeof(elf_prstatus_t);
1679 status->pr_gregsetsz = sizeof(elf_gregset_t);
1680 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
1681 status->pr_osreldate = osreldate;
1682 status->pr_cursig = td->td_proc->p_sig;
1683 status->pr_pid = td->td_tid;
1684 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1685 fill_regs32(td, &status->pr_reg);
1687 fill_regs(td, &status->pr_reg);
1689 sbuf_bcat(sb, status, sizeof(*status));
1690 free(status, M_TEMP);
1692 *sizep = sizeof(*status);
1696 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1699 elf_prfpregset_t *fpregset;
1701 td = (struct thread *)arg;
1703 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1704 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1705 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1706 fill_fpregs32(td, fpregset);
1708 fill_fpregs(td, fpregset);
1710 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1711 free(fpregset, M_TEMP);
1713 *sizep = sizeof(*fpregset);
1717 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1720 elf_thrmisc_t thrmisc;
1722 td = (struct thread *)arg;
1724 KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1725 bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1726 strcpy(thrmisc.pr_tname, td->td_name);
1727 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1729 *sizep = sizeof(thrmisc);
1733 * Allow for MD specific notes, as well as any MD
1734 * specific preparations for writing MI notes.
1737 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
1743 td = (struct thread *)arg;
1745 if (size != 0 && sb != NULL)
1746 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
1750 __elfN(dump_thread)(td, buf, &size);
1751 KASSERT(*sizep == size, ("invalid size"));
1752 if (size != 0 && sb != NULL)
1753 sbuf_bcat(sb, buf, size);
1758 #ifdef KINFO_PROC_SIZE
1759 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
1763 __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
1769 p = (struct proc *)arg;
1770 size = sizeof(structsize) + p->p_numthreads *
1771 sizeof(elf_kinfo_proc_t);
1774 KASSERT(*sizep == size, ("invalid size"));
1775 structsize = sizeof(elf_kinfo_proc_t);
1776 sbuf_bcat(sb, &structsize, sizeof(structsize));
1778 kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
1783 #ifdef KINFO_FILE_SIZE
1784 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
1788 note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
1794 p = (struct proc *)arg;
1797 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1798 sbuf_set_drain(sb, sbuf_drain_count, &size);
1799 sbuf_bcat(sb, &structsize, sizeof(structsize));
1801 kern_proc_filedesc_out(p, sb, -1);
1806 structsize = sizeof(struct kinfo_file);
1807 sbuf_bcat(sb, &structsize, sizeof(structsize));
1809 kern_proc_filedesc_out(p, sb, -1);
1813 #ifdef KINFO_VMENTRY_SIZE
1814 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1818 note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
1824 p = (struct proc *)arg;
1827 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1828 sbuf_set_drain(sb, sbuf_drain_count, &size);
1829 sbuf_bcat(sb, &structsize, sizeof(structsize));
1831 kern_proc_vmmap_out(p, sb);
1836 structsize = sizeof(struct kinfo_vmentry);
1837 sbuf_bcat(sb, &structsize, sizeof(structsize));
1839 kern_proc_vmmap_out(p, sb);
1844 note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
1850 p = (struct proc *)arg;
1851 size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
1853 KASSERT(*sizep == size, ("invalid size"));
1854 structsize = sizeof(gid_t);
1855 sbuf_bcat(sb, &structsize, sizeof(structsize));
1856 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
1863 note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
1869 p = (struct proc *)arg;
1870 size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
1872 KASSERT(*sizep == size, ("invalid size"));
1873 structsize = sizeof(p->p_fd->fd_cmask);
1874 sbuf_bcat(sb, &structsize, sizeof(structsize));
1875 sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
1881 note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
1884 struct rlimit rlim[RLIM_NLIMITS];
1888 p = (struct proc *)arg;
1889 size = sizeof(structsize) + sizeof(rlim);
1891 KASSERT(*sizep == size, ("invalid size"));
1892 structsize = sizeof(rlim);
1893 sbuf_bcat(sb, &structsize, sizeof(structsize));
1895 for (i = 0; i < RLIM_NLIMITS; i++)
1896 lim_rlimit(p, i, &rlim[i]);
1898 sbuf_bcat(sb, rlim, sizeof(rlim));
1904 note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
1910 p = (struct proc *)arg;
1911 size = sizeof(structsize) + sizeof(p->p_osrel);
1913 KASSERT(*sizep == size, ("invalid size"));
1914 structsize = sizeof(p->p_osrel);
1915 sbuf_bcat(sb, &structsize, sizeof(structsize));
1916 sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
1922 __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
1925 elf_ps_strings_t ps_strings;
1929 p = (struct proc *)arg;
1930 size = sizeof(structsize) + sizeof(ps_strings);
1932 KASSERT(*sizep == size, ("invalid size"));
1933 structsize = sizeof(ps_strings);
1934 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1935 ps_strings = PTROUT(p->p_sysent->sv_psstrings);
1937 ps_strings = p->p_sysent->sv_psstrings;
1939 sbuf_bcat(sb, &structsize, sizeof(structsize));
1940 sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
1946 __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
1952 p = (struct proc *)arg;
1955 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1956 sbuf_set_drain(sb, sbuf_drain_count, &size);
1957 sbuf_bcat(sb, &structsize, sizeof(structsize));
1959 proc_getauxv(curthread, p, sb);
1965 structsize = sizeof(Elf_Auxinfo);
1966 sbuf_bcat(sb, &structsize, sizeof(structsize));
1968 proc_getauxv(curthread, p, sb);
1974 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
1975 int32_t *osrel, const Elf_Phdr *pnote)
1977 const Elf_Note *note, *note0, *note_end;
1978 const char *note_name;
1981 if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
1982 pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
1985 note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
1986 note_end = (const Elf_Note *)(imgp->image_header +
1987 pnote->p_offset + pnote->p_filesz);
1988 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
1989 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
1990 (const char *)note < sizeof(Elf_Note))
1992 if (note->n_namesz != checknote->hdr.n_namesz ||
1993 note->n_descsz != checknote->hdr.n_descsz ||
1994 note->n_type != checknote->hdr.n_type)
1996 note_name = (const char *)(note + 1);
1997 if (note_name + checknote->hdr.n_namesz >=
1998 (const char *)note_end || strncmp(checknote->vendor,
1999 note_name, checknote->hdr.n_namesz) != 0)
2003 * Fetch the osreldate for binary
2004 * from the ELF OSABI-note if necessary.
2006 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
2007 checknote->trans_osrel != NULL)
2008 return (checknote->trans_osrel(note, osrel));
2012 note = (const Elf_Note *)((const char *)(note + 1) +
2013 roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
2014 roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
2021 * Try to find the appropriate ABI-note section for checknote,
2022 * fetch the osreldate for binary from the ELF OSABI-note. Only the
2023 * first page of the image is searched, the same as for headers.
2026 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
2029 const Elf_Phdr *phdr;
2030 const Elf_Ehdr *hdr;
2033 hdr = (const Elf_Ehdr *)imgp->image_header;
2034 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
2036 for (i = 0; i < hdr->e_phnum; i++) {
2037 if (phdr[i].p_type == PT_NOTE &&
2038 __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
2046 * Tell kern_execve.c about it, with a little help from the linker.
2048 static struct execsw __elfN(execsw) = {
2049 __CONCAT(exec_, __elfN(imgact)),
2050 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2052 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2054 #ifdef COMPRESS_USER_CORES
2056 * Compress and write out a core segment for a user process.
2058 * 'inbuf' is the starting address of a VM segment in the process' address
2059 * space that is to be compressed and written out to the core file. 'dest_buf'
2060 * is a buffer in the kernel's address space. The segment is copied from
2061 * 'inbuf' to 'dest_buf' first before being processed by the compression
2062 * routine gzwrite(). This copying is necessary because the content of the VM
2063 * segment may change between the compression pass and the crc-computation pass
2064 * in gzwrite(). This is because realtime threads may preempt the UNIX kernel.
2066 * If inbuf is NULL it is assumed that data is already copied to 'dest_buf'.
2069 compress_core (gzFile file, char *inbuf, char *dest_buf, unsigned int len,
2074 unsigned int chunk_len;
2077 if (inbuf != NULL) {
2078 chunk_len = (len > CORE_BUF_SIZE) ? CORE_BUF_SIZE : len;
2079 copyin(inbuf, dest_buf, chunk_len);
2084 len_compressed = gzwrite(file, dest_buf, chunk_len);
2086 EVENTHANDLER_INVOKE(app_coredump_progress, td, len_compressed);
2088 if ((unsigned int)len_compressed != chunk_len) {
2090 "compress_core: length mismatch (0x%x returned, "
2091 "0x%x expected)\n", len_compressed, chunk_len);
2092 EVENTHANDLER_INVOKE(app_coredump_error, td,
2093 "compress_core: length mismatch %x -> %x",
2094 chunk_len, len_compressed);
2104 #endif /* COMPRESS_USER_CORES */
2107 __elfN(trans_prot)(Elf_Word flags)
2113 prot |= VM_PROT_EXECUTE;
2115 prot |= VM_PROT_WRITE;
2117 prot |= VM_PROT_READ;
2118 #if __ELF_WORD_SIZE == 32
2119 #if defined(__amd64__) || defined(__ia64__)
2120 if (i386_read_exec && (flags & PF_R))
2121 prot |= VM_PROT_EXECUTE;
2128 __elfN(untrans_prot)(vm_prot_t prot)
2133 if (prot & VM_PROT_EXECUTE)
2135 if (prot & VM_PROT_READ)
2137 if (prot & VM_PROT_WRITE)