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/capsicum.h>
41 #include <sys/fcntl.h>
43 #include <sys/imgact.h>
44 #include <sys/imgact_elf.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mount.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>
57 #include <sys/rwlock.h>
59 #include <sys/sf_buf.h>
61 #include <sys/systm.h>
62 #include <sys/signalvar.h>
65 #include <sys/syscall.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysent.h>
68 #include <sys/vnode.h>
69 #include <sys/syslog.h>
70 #include <sys/eventhandler.h>
74 #include <vm/vm_kern.h>
75 #include <vm/vm_param.h>
77 #include <vm/vm_map.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_extern.h>
81 #include <machine/elf.h>
82 #include <machine/md_var.h>
84 #define ELF_NOTE_ROUNDSIZE 4
85 #define OLD_EI_BRAND 8
87 static int __elfN(check_header)(const Elf_Ehdr *hdr);
88 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
89 const char *interp, int interp_name_len, int32_t *osrel);
90 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
91 u_long *entry, size_t pagesize);
92 static int __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
93 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
95 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
96 static boolean_t __elfN(freebsd_trans_osrel)(const Elf_Note *note,
98 static boolean_t kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel);
99 static boolean_t __elfN(check_note)(struct image_params *imgp,
100 Elf_Brandnote *checknote, int32_t *osrel);
101 static vm_prot_t __elfN(trans_prot)(Elf_Word);
102 static Elf_Word __elfN(untrans_prot)(vm_prot_t);
104 SYSCTL_NODE(_kern, OID_AUTO, __CONCAT(elf, __ELF_WORD_SIZE), CTLFLAG_RW, 0,
107 #define CORE_BUF_SIZE (16 * 1024)
109 int __elfN(fallback_brand) = -1;
110 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
111 fallback_brand, CTLFLAG_RWTUN, &__elfN(fallback_brand), 0,
112 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) " brand of last resort");
114 static int elf_legacy_coredump = 0;
115 SYSCTL_INT(_debug, OID_AUTO, __elfN(legacy_coredump), CTLFLAG_RW,
116 &elf_legacy_coredump, 0, "");
118 int __elfN(nxstack) =
119 #if defined(__amd64__) || defined(__powerpc64__) /* both 64 and 32 bit */
124 SYSCTL_INT(__CONCAT(_kern_elf, __ELF_WORD_SIZE), OID_AUTO,
125 nxstack, CTLFLAG_RW, &__elfN(nxstack), 0,
126 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE)) ": enable non-executable stack");
128 #if __ELF_WORD_SIZE == 32
129 #if defined(__amd64__)
130 int i386_read_exec = 0;
131 SYSCTL_INT(_kern_elf32, OID_AUTO, read_exec, CTLFLAG_RW, &i386_read_exec, 0,
132 "enable execution from readable segments");
136 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
138 #define trunc_page_ps(va, ps) ((va) & ~(ps - 1))
139 #define round_page_ps(va, ps) (((va) + (ps - 1)) & ~(ps - 1))
140 #define aligned(a, t) (trunc_page_ps((u_long)(a), sizeof(t)) == (u_long)(a))
142 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
144 Elf_Brandnote __elfN(freebsd_brandnote) = {
145 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
146 .hdr.n_descsz = sizeof(int32_t),
148 .vendor = FREEBSD_ABI_VENDOR,
149 .flags = BN_TRANSLATE_OSREL,
150 .trans_osrel = __elfN(freebsd_trans_osrel)
154 __elfN(freebsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
158 p = (uintptr_t)(note + 1);
159 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
160 *osrel = *(const int32_t *)(p);
165 static const char GNU_ABI_VENDOR[] = "GNU";
166 static int GNU_KFREEBSD_ABI_DESC = 3;
168 Elf_Brandnote __elfN(kfreebsd_brandnote) = {
169 .hdr.n_namesz = sizeof(GNU_ABI_VENDOR),
170 .hdr.n_descsz = 16, /* XXX at least 16 */
172 .vendor = GNU_ABI_VENDOR,
173 .flags = BN_TRANSLATE_OSREL,
174 .trans_osrel = kfreebsd_trans_osrel
178 kfreebsd_trans_osrel(const Elf_Note *note, int32_t *osrel)
180 const Elf32_Word *desc;
183 p = (uintptr_t)(note + 1);
184 p += roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE);
186 desc = (const Elf32_Word *)p;
187 if (desc[0] != GNU_KFREEBSD_ABI_DESC)
191 * Debian GNU/kFreeBSD embed the earliest compatible kernel version
192 * (__FreeBSD_version: <major><two digit minor>Rxx) in the LSB way.
194 *osrel = desc[1] * 100000 + desc[2] * 1000 + desc[3];
200 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
204 for (i = 0; i < MAX_BRANDS; i++) {
205 if (elf_brand_list[i] == NULL) {
206 elf_brand_list[i] = entry;
210 if (i == MAX_BRANDS) {
211 printf("WARNING: %s: could not insert brandinfo entry: %p\n",
219 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
223 for (i = 0; i < MAX_BRANDS; i++) {
224 if (elf_brand_list[i] == entry) {
225 elf_brand_list[i] = NULL;
235 __elfN(brand_inuse)(Elf_Brandinfo *entry)
240 sx_slock(&allproc_lock);
241 FOREACH_PROC_IN_SYSTEM(p) {
242 if (p->p_sysent == entry->sysvec) {
247 sx_sunlock(&allproc_lock);
252 static Elf_Brandinfo *
253 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
254 int interp_name_len, int32_t *osrel)
256 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
262 * We support four types of branding -- (1) the ELF EI_OSABI field
263 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
264 * branding w/in the ELF header, (3) path of the `interp_path'
265 * field, and (4) the ".note.ABI-tag" ELF section.
268 /* Look for an ".note.ABI-tag" ELF section */
269 for (i = 0; i < MAX_BRANDS; i++) {
270 bi = elf_brand_list[i];
273 if (hdr->e_machine == bi->machine && (bi->flags &
274 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
275 ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
281 /* If the executable has a brand, search for it in the brand list. */
282 for (i = 0; i < MAX_BRANDS; i++) {
283 bi = elf_brand_list[i];
284 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
286 if (hdr->e_machine == bi->machine &&
287 (hdr->e_ident[EI_OSABI] == bi->brand ||
288 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
289 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
293 /* No known brand, see if the header is recognized by any brand */
294 for (i = 0; i < MAX_BRANDS; i++) {
295 bi = elf_brand_list[i];
296 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY ||
297 bi->header_supported == NULL)
299 if (hdr->e_machine == bi->machine) {
300 ret = bi->header_supported(imgp);
306 /* Lacking a known brand, search for a recognized interpreter. */
307 if (interp != NULL) {
308 for (i = 0; i < MAX_BRANDS; i++) {
309 bi = elf_brand_list[i];
310 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
312 if (hdr->e_machine == bi->machine &&
313 /* ELF image p_filesz includes terminating zero */
314 strlen(bi->interp_path) + 1 == interp_name_len &&
315 strncmp(interp, bi->interp_path, interp_name_len)
321 /* Lacking a recognized interpreter, try the default brand */
322 for (i = 0; i < MAX_BRANDS; i++) {
323 bi = elf_brand_list[i];
324 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
326 if (hdr->e_machine == bi->machine &&
327 __elfN(fallback_brand) == bi->brand)
334 __elfN(check_header)(const Elf_Ehdr *hdr)
340 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
341 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
342 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
343 hdr->e_phentsize != sizeof(Elf_Phdr) ||
344 hdr->e_version != ELF_TARG_VER)
348 * Make sure we have at least one brand for this machine.
351 for (i = 0; i < MAX_BRANDS; i++) {
352 bi = elf_brand_list[i];
353 if (bi != NULL && bi->machine == hdr->e_machine)
363 __elfN(map_partial)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
364 vm_offset_t start, vm_offset_t end, vm_prot_t prot)
371 * Create the page if it doesn't exist yet. Ignore errors.
374 vm_map_insert(map, NULL, 0, trunc_page(start), round_page(end),
375 VM_PROT_ALL, VM_PROT_ALL, 0);
379 * Find the page from the underlying object.
382 sf = vm_imgact_map_page(object, offset);
384 return (KERN_FAILURE);
385 off = offset - trunc_page(offset);
386 error = copyout((caddr_t)sf_buf_kva(sf) + off, (caddr_t)start,
388 vm_imgact_unmap_page(sf);
390 return (KERN_FAILURE);
394 return (KERN_SUCCESS);
398 __elfN(map_insert)(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
399 vm_offset_t start, vm_offset_t end, vm_prot_t prot, int cow)
406 if (start != trunc_page(start)) {
407 rv = __elfN(map_partial)(map, object, offset, start,
408 round_page(start), prot);
411 offset += round_page(start) - start;
412 start = round_page(start);
414 if (end != round_page(end)) {
415 rv = __elfN(map_partial)(map, object, offset +
416 trunc_page(end) - start, trunc_page(end), end, prot);
419 end = trunc_page(end);
422 if (offset & PAGE_MASK) {
424 * The mapping is not page aligned. This means we have
425 * to copy the data. Sigh.
427 rv = vm_map_find(map, NULL, 0, &start, end - start, 0,
428 VMFS_NO_SPACE, prot | VM_PROT_WRITE, VM_PROT_ALL,
433 return (KERN_SUCCESS);
434 for (; start < end; start += sz) {
435 sf = vm_imgact_map_page(object, offset);
437 return (KERN_FAILURE);
438 off = offset - trunc_page(offset);
440 if (sz > PAGE_SIZE - off)
441 sz = PAGE_SIZE - off;
442 error = copyout((caddr_t)sf_buf_kva(sf) + off,
444 vm_imgact_unmap_page(sf);
446 return (KERN_FAILURE);
452 vm_object_reference(object);
454 rv = vm_map_insert(map, object, offset, start, end,
455 prot, VM_PROT_ALL, cow);
457 if (rv != KERN_SUCCESS)
458 vm_object_deallocate(object);
462 return (KERN_SUCCESS);
467 __elfN(load_section)(struct image_params *imgp, vm_offset_t offset,
468 caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot,
475 vm_offset_t map_addr;
478 vm_offset_t file_addr;
481 * It's necessary to fail if the filsz + offset taken from the
482 * header is greater than the actual file pager object's size.
483 * If we were to allow this, then the vm_map_find() below would
484 * walk right off the end of the file object and into the ether.
486 * While I'm here, might as well check for something else that
487 * is invalid: filsz cannot be greater than memsz.
489 if ((off_t)filsz + offset > imgp->attr->va_size || filsz > memsz) {
490 uprintf("elf_load_section: truncated ELF file\n");
494 object = imgp->object;
495 map = &imgp->proc->p_vmspace->vm_map;
496 map_addr = trunc_page_ps((vm_offset_t)vmaddr, pagesize);
497 file_addr = trunc_page_ps(offset, pagesize);
500 * We have two choices. We can either clear the data in the last page
501 * of an oversized mapping, or we can start the anon mapping a page
502 * early and copy the initialized data into that first page. We
503 * choose the second..
506 map_len = trunc_page_ps(offset + filsz, pagesize) - file_addr;
508 map_len = round_page_ps(offset + filsz, pagesize) - file_addr;
511 /* cow flags: don't dump readonly sections in core */
512 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
513 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
515 rv = __elfN(map_insert)(map,
517 file_addr, /* file offset */
518 map_addr, /* virtual start */
519 map_addr + map_len,/* virtual end */
522 if (rv != KERN_SUCCESS)
525 /* we can stop now if we've covered it all */
526 if (memsz == filsz) {
533 * We have to get the remaining bit of the file into the first part
534 * of the oversized map segment. This is normally because the .data
535 * segment in the file is extended to provide bss. It's a neat idea
536 * to try and save a page, but it's a pain in the behind to implement.
538 copy_len = (offset + filsz) - trunc_page_ps(offset + filsz, pagesize);
539 map_addr = trunc_page_ps((vm_offset_t)vmaddr + filsz, pagesize);
540 map_len = round_page_ps((vm_offset_t)vmaddr + memsz, pagesize) -
543 /* This had damn well better be true! */
545 rv = __elfN(map_insert)(map, NULL, 0, map_addr, map_addr +
546 map_len, VM_PROT_ALL, 0);
547 if (rv != KERN_SUCCESS) {
555 sf = vm_imgact_map_page(object, offset + filsz);
559 /* send the page fragment to user space */
560 off = trunc_page_ps(offset + filsz, pagesize) -
561 trunc_page(offset + filsz);
562 error = copyout((caddr_t)sf_buf_kva(sf) + off,
563 (caddr_t)map_addr, copy_len);
564 vm_imgact_unmap_page(sf);
571 * set it to the specified protection.
572 * XXX had better undo the damage from pasting over the cracks here!
574 vm_map_protect(map, trunc_page(map_addr), round_page(map_addr +
575 map_len), prot, FALSE);
581 * Load the file "file" into memory. It may be either a shared object
584 * The "addr" reference parameter is in/out. On entry, it specifies
585 * the address where a shared object should be loaded. If the file is
586 * an executable, this value is ignored. On exit, "addr" specifies
587 * where the file was actually loaded.
589 * The "entry" reference parameter is out only. On exit, it specifies
590 * the entry point for the loaded file.
593 __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
594 u_long *entry, size_t pagesize)
599 struct image_params image_params;
601 const Elf_Ehdr *hdr = NULL;
602 const Elf_Phdr *phdr = NULL;
603 struct nameidata *nd;
605 struct image_params *imgp;
608 u_long base_addr = 0;
609 int error, i, numsegs;
611 #ifdef CAPABILITY_MODE
613 * XXXJA: This check can go away once we are sufficiently confident
614 * that the checks in namei() are correct.
616 if (IN_CAPABILITY_MODE(curthread))
620 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
622 attr = &tempdata->attr;
623 imgp = &tempdata->image_params;
626 * Initialize part of the common data
630 imgp->firstpage = NULL;
631 imgp->image_header = NULL;
633 imgp->execlabel = NULL;
635 NDINIT(nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_SYSSPACE, file, curthread);
636 if ((error = namei(nd)) != 0) {
640 NDFREE(nd, NDF_ONLY_PNBUF);
641 imgp->vp = nd->ni_vp;
644 * Check permissions, modes, uid, etc on the file, and "open" it.
646 error = exec_check_permissions(imgp);
650 error = exec_map_first_page(imgp);
655 * Also make certain that the interpreter stays the same, so set
656 * its VV_TEXT flag, too.
658 VOP_SET_TEXT(nd->ni_vp);
660 imgp->object = nd->ni_vp->v_object;
662 hdr = (const Elf_Ehdr *)imgp->image_header;
663 if ((error = __elfN(check_header)(hdr)) != 0)
665 if (hdr->e_type == ET_DYN)
667 else if (hdr->e_type == ET_EXEC)
674 /* Only support headers that fit within first page for now */
675 if ((hdr->e_phoff > PAGE_SIZE) ||
676 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
681 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
682 if (!aligned(phdr, Elf_Addr)) {
687 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
688 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
689 /* Loadable segment */
690 prot = __elfN(trans_prot)(phdr[i].p_flags);
691 error = __elfN(load_section)(imgp, phdr[i].p_offset,
692 (caddr_t)(uintptr_t)phdr[i].p_vaddr + rbase,
693 phdr[i].p_memsz, phdr[i].p_filesz, prot, pagesize);
697 * Establish the base address if this is the
701 base_addr = trunc_page(phdr[i].p_vaddr +
707 *entry = (unsigned long)hdr->e_entry + rbase;
711 exec_unmap_first_page(imgp);
716 free(tempdata, M_TEMP);
722 __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp)
724 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
725 const Elf_Phdr *phdr;
726 Elf_Auxargs *elf_auxargs;
727 struct vmspace *vmspace;
729 u_long text_size = 0, data_size = 0, total_size = 0;
730 u_long text_addr = 0, data_addr = 0;
731 u_long seg_size, seg_addr;
732 u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
734 int error = 0, i, n, interp_name_len = 0;
735 const char *err_str = NULL, *interp = NULL, *newinterp = NULL;
736 Elf_Brandinfo *brand_info;
738 struct sysentvec *sv;
741 * Do we have a valid ELF header ?
743 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later
744 * if particular brand doesn't support it.
746 if (__elfN(check_header)(hdr) != 0 ||
747 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
751 * From here on down, we return an errno, not -1, as we've
752 * detected an ELF file.
755 if ((hdr->e_phoff > PAGE_SIZE) ||
756 (u_int)hdr->e_phentsize * hdr->e_phnum > PAGE_SIZE - hdr->e_phoff) {
757 /* Only support headers in first page for now */
758 uprintf("Program headers not in the first page\n");
761 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
762 if (!aligned(phdr, Elf_Addr)) {
763 uprintf("Unaligned program headers\n");
768 for (i = 0; i < hdr->e_phnum; i++) {
769 switch (phdr[i].p_type) {
772 baddr = phdr[i].p_vaddr;
776 /* Path to interpreter */
777 if (phdr[i].p_filesz > MAXPATHLEN ||
778 phdr[i].p_offset > PAGE_SIZE ||
779 phdr[i].p_filesz > PAGE_SIZE - phdr[i].p_offset) {
780 uprintf("Invalid PT_INTERP\n");
783 interp = imgp->image_header + phdr[i].p_offset;
784 interp_name_len = phdr[i].p_filesz;
789 __elfN(trans_prot)(phdr[i].p_flags);
790 imgp->stack_sz = phdr[i].p_memsz;
795 brand_info = __elfN(get_brandinfo)(imgp, interp, interp_name_len,
797 if (brand_info == NULL) {
798 uprintf("ELF binary type \"%u\" not known.\n",
799 hdr->e_ident[EI_OSABI]);
802 if (hdr->e_type == ET_DYN) {
803 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
804 uprintf("Cannot execute shared object\n");
808 * Honour the base load address from the dso if it is
809 * non-zero for some reason.
812 et_dyn_addr = ET_DYN_LOAD_ADDR;
817 sv = brand_info->sysvec;
818 if (interp != NULL && brand_info->interp_newpath != NULL)
819 newinterp = brand_info->interp_newpath;
822 * Avoid a possible deadlock if the current address space is destroyed
823 * and that address space maps the locked vnode. In the common case,
824 * the locked vnode's v_usecount is decremented but remains greater
825 * than zero. Consequently, the vnode lock is not needed by vrele().
826 * However, in cases where the vnode lock is external, such as nullfs,
827 * v_usecount may become zero.
829 * The VV_TEXT flag prevents modifications to the executable while
830 * the vnode is unlocked.
832 VOP_UNLOCK(imgp->vp, 0);
834 error = exec_new_vmspace(imgp, sv);
835 imgp->proc->p_sysent = sv;
837 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
841 for (i = 0; i < hdr->e_phnum; i++) {
842 switch (phdr[i].p_type) {
843 case PT_LOAD: /* Loadable segment */
844 if (phdr[i].p_memsz == 0)
846 prot = __elfN(trans_prot)(phdr[i].p_flags);
847 error = __elfN(load_section)(imgp, phdr[i].p_offset,
848 (caddr_t)(uintptr_t)phdr[i].p_vaddr + et_dyn_addr,
849 phdr[i].p_memsz, phdr[i].p_filesz, prot,
855 * If this segment contains the program headers,
856 * remember their virtual address for the AT_PHDR
857 * aux entry. Static binaries don't usually include
860 if (phdr[i].p_offset == 0 &&
861 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
863 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
866 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
867 seg_size = round_page(phdr[i].p_memsz +
868 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
871 * Make the largest executable segment the official
872 * text segment and all others data.
874 * Note that obreak() assumes that data_addr +
875 * data_size == end of data load area, and the ELF
876 * file format expects segments to be sorted by
877 * address. If multiple data segments exist, the
878 * last one will be used.
881 if (phdr[i].p_flags & PF_X && text_size < seg_size) {
882 text_size = seg_size;
883 text_addr = seg_addr;
885 data_size = seg_size;
886 data_addr = seg_addr;
888 total_size += seg_size;
890 case PT_PHDR: /* Program header table info */
891 proghdr = phdr[i].p_vaddr + et_dyn_addr;
898 if (data_addr == 0 && data_size == 0) {
899 data_addr = text_addr;
900 data_size = text_size;
903 entry = (u_long)hdr->e_entry + et_dyn_addr;
906 * Check limits. It should be safe to check the
907 * limits after loading the segments since we do
908 * not actually fault in all the segments pages.
910 PROC_LOCK(imgp->proc);
911 if (data_size > lim_cur_proc(imgp->proc, RLIMIT_DATA))
912 err_str = "Data segment size exceeds process limit";
913 else if (text_size > maxtsiz)
914 err_str = "Text segment size exceeds system limit";
915 else if (total_size > lim_cur_proc(imgp->proc, RLIMIT_VMEM))
916 err_str = "Total segment size exceeds process limit";
917 else if (racct_set(imgp->proc, RACCT_DATA, data_size) != 0)
918 err_str = "Data segment size exceeds resource limit";
919 else if (racct_set(imgp->proc, RACCT_VMEM, total_size) != 0)
920 err_str = "Total segment size exceeds resource limit";
921 if (err_str != NULL) {
922 PROC_UNLOCK(imgp->proc);
923 uprintf("%s\n", err_str);
927 vmspace = imgp->proc->p_vmspace;
928 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
929 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
930 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
931 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
934 * We load the dynamic linker where a userland call
935 * to mmap(0, ...) would put it. The rationale behind this
936 * calculation is that it leaves room for the heap to grow to
937 * its maximum allowed size.
939 addr = round_page((vm_offset_t)vmspace->vm_daddr + lim_max(curthread,
941 PROC_UNLOCK(imgp->proc);
943 imgp->entry_addr = entry;
945 if (interp != NULL) {
946 int have_interp = FALSE;
947 VOP_UNLOCK(imgp->vp, 0);
948 if (brand_info->emul_path != NULL &&
949 brand_info->emul_path[0] != '\0') {
950 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
951 snprintf(path, MAXPATHLEN, "%s%s",
952 brand_info->emul_path, interp);
953 error = __elfN(load_file)(imgp->proc, path, &addr,
954 &imgp->entry_addr, sv->sv_pagesize);
959 if (!have_interp && newinterp != NULL) {
960 error = __elfN(load_file)(imgp->proc, newinterp, &addr,
961 &imgp->entry_addr, sv->sv_pagesize);
966 error = __elfN(load_file)(imgp->proc, interp, &addr,
967 &imgp->entry_addr, sv->sv_pagesize);
969 vn_lock(imgp->vp, LK_EXCLUSIVE | LK_RETRY);
971 uprintf("ELF interpreter %s not found\n", interp);
978 * Construct auxargs table (used by the fixup routine)
980 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
981 elf_auxargs->execfd = -1;
982 elf_auxargs->phdr = proghdr;
983 elf_auxargs->phent = hdr->e_phentsize;
984 elf_auxargs->phnum = hdr->e_phnum;
985 elf_auxargs->pagesz = PAGE_SIZE;
986 elf_auxargs->base = addr;
987 elf_auxargs->flags = 0;
988 elf_auxargs->entry = entry;
989 elf_auxargs->hdr_eflags = hdr->e_flags;
991 imgp->auxargs = elf_auxargs;
992 imgp->interpreted = 0;
993 imgp->reloc_base = addr;
994 imgp->proc->p_osrel = osrel;
999 #define suword __CONCAT(suword, __ELF_WORD_SIZE)
1002 __elfN(freebsd_fixup)(register_t **stack_base, struct image_params *imgp)
1004 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
1008 base = (Elf_Addr *)*stack_base;
1009 pos = base + (imgp->args->argc + imgp->args->envc + 2);
1011 if (args->execfd != -1)
1012 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
1013 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
1014 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
1015 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
1016 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
1017 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
1018 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
1019 AUXARGS_ENTRY(pos, AT_BASE, args->base);
1021 AUXARGS_ENTRY(pos, AT_EHDRFLAGS, args->hdr_eflags);
1023 if (imgp->execpathp != 0)
1024 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
1025 AUXARGS_ENTRY(pos, AT_OSRELDATE,
1026 imgp->proc->p_ucred->cr_prison->pr_osreldate);
1027 if (imgp->canary != 0) {
1028 AUXARGS_ENTRY(pos, AT_CANARY, imgp->canary);
1029 AUXARGS_ENTRY(pos, AT_CANARYLEN, imgp->canarylen);
1031 AUXARGS_ENTRY(pos, AT_NCPUS, mp_ncpus);
1032 if (imgp->pagesizes != 0) {
1033 AUXARGS_ENTRY(pos, AT_PAGESIZES, imgp->pagesizes);
1034 AUXARGS_ENTRY(pos, AT_PAGESIZESLEN, imgp->pagesizeslen);
1036 if (imgp->sysent->sv_timekeep_base != 0) {
1037 AUXARGS_ENTRY(pos, AT_TIMEKEEP,
1038 imgp->sysent->sv_timekeep_base);
1040 AUXARGS_ENTRY(pos, AT_STACKPROT, imgp->sysent->sv_shared_page_obj
1041 != NULL && imgp->stack_prot != 0 ? imgp->stack_prot :
1042 imgp->sysent->sv_stackprot);
1043 AUXARGS_ENTRY(pos, AT_NULL, 0);
1045 free(imgp->auxargs, M_TEMP);
1046 imgp->auxargs = NULL;
1049 suword(base, (long)imgp->args->argc);
1050 *stack_base = (register_t *)base;
1055 * Code for generating ELF core dumps.
1058 typedef void (*segment_callback)(vm_map_entry_t, void *);
1060 /* Closure for cb_put_phdr(). */
1061 struct phdr_closure {
1062 Elf_Phdr *phdr; /* Program header to fill in */
1063 Elf_Off offset; /* Offset of segment in core file */
1066 /* Closure for cb_size_segment(). */
1067 struct sseg_closure {
1068 int count; /* Count of writable segments. */
1069 size_t size; /* Total size of all writable segments. */
1072 typedef void (*outfunc_t)(void *, struct sbuf *, size_t *);
1075 int type; /* Note type. */
1076 outfunc_t outfunc; /* Output function. */
1077 void *outarg; /* Argument for the output function. */
1078 size_t outsize; /* Output size. */
1079 TAILQ_ENTRY(note_info) link; /* Link to the next note info. */
1082 TAILQ_HEAD(note_info_list, note_info);
1084 /* Coredump output parameters. */
1085 struct coredump_params {
1087 struct ucred *active_cred;
1088 struct ucred *file_cred;
1091 struct gzio_stream *gzs;
1094 static void cb_put_phdr(vm_map_entry_t, void *);
1095 static void cb_size_segment(vm_map_entry_t, void *);
1096 static int core_write(struct coredump_params *, void *, size_t, off_t,
1098 static void each_writable_segment(struct thread *, segment_callback, void *);
1099 static int __elfN(corehdr)(struct coredump_params *, int, void *, size_t,
1100 struct note_info_list *, size_t);
1101 static void __elfN(prepare_notes)(struct thread *, struct note_info_list *,
1103 static void __elfN(puthdr)(struct thread *, void *, size_t, int, size_t);
1104 static void __elfN(putnote)(struct note_info *, struct sbuf *);
1105 static size_t register_note(struct note_info_list *, int, outfunc_t, void *);
1106 static int sbuf_drain_core_output(void *, const char *, int);
1107 static int sbuf_drain_count(void *arg, const char *data, int len);
1109 static void __elfN(note_fpregset)(void *, struct sbuf *, size_t *);
1110 static void __elfN(note_prpsinfo)(void *, struct sbuf *, size_t *);
1111 static void __elfN(note_prstatus)(void *, struct sbuf *, size_t *);
1112 static void __elfN(note_threadmd)(void *, struct sbuf *, size_t *);
1113 static void __elfN(note_thrmisc)(void *, struct sbuf *, size_t *);
1114 static void __elfN(note_procstat_auxv)(void *, struct sbuf *, size_t *);
1115 static void __elfN(note_procstat_proc)(void *, struct sbuf *, size_t *);
1116 static void __elfN(note_procstat_psstrings)(void *, struct sbuf *, size_t *);
1117 static void note_procstat_files(void *, struct sbuf *, size_t *);
1118 static void note_procstat_groups(void *, struct sbuf *, size_t *);
1119 static void note_procstat_osrel(void *, struct sbuf *, size_t *);
1120 static void note_procstat_rlimit(void *, struct sbuf *, size_t *);
1121 static void note_procstat_umask(void *, struct sbuf *, size_t *);
1122 static void note_procstat_vmmap(void *, struct sbuf *, size_t *);
1125 extern int compress_user_cores_gzlevel;
1128 * Write out a core segment to the compression stream.
1131 compress_chunk(struct coredump_params *p, char *base, char *buf, u_int len)
1137 chunk_len = MIN(len, CORE_BUF_SIZE);
1138 copyin(base, buf, chunk_len);
1139 error = gzio_write(p->gzs, buf, chunk_len);
1149 core_gz_write(void *base, size_t len, off_t offset, void *arg)
1152 return (core_write((struct coredump_params *)arg, base, len, offset,
1158 core_write(struct coredump_params *p, void *base, size_t len, off_t offset,
1162 return (vn_rdwr_inchunks(UIO_WRITE, p->vp, base, len, offset,
1163 seg, IO_UNIT | IO_DIRECT | IO_RANGELOCKED,
1164 p->active_cred, p->file_cred, NULL, p->td));
1168 core_output(void *base, size_t len, off_t offset, struct coredump_params *p,
1174 return (compress_chunk(p, base, tmpbuf, len));
1176 return (core_write(p, base, len, offset, UIO_USERSPACE));
1180 * Drain into a core file.
1183 sbuf_drain_core_output(void *arg, const char *data, int len)
1185 struct coredump_params *p;
1188 p = (struct coredump_params *)arg;
1191 * Some kern_proc out routines that print to this sbuf may
1192 * call us with the process lock held. Draining with the
1193 * non-sleepable lock held is unsafe. The lock is needed for
1194 * those routines when dumping a live process. In our case we
1195 * can safely release the lock before draining and acquire
1198 locked = PROC_LOCKED(p->td->td_proc);
1200 PROC_UNLOCK(p->td->td_proc);
1203 error = gzio_write(p->gzs, __DECONST(char *, data), len);
1206 error = core_write(p, __DECONST(void *, data), len, p->offset,
1209 PROC_LOCK(p->td->td_proc);
1217 * Drain into a counter.
1220 sbuf_drain_count(void *arg, const char *data __unused, int len)
1224 sizep = (size_t *)arg;
1230 __elfN(coredump)(struct thread *td, struct vnode *vp, off_t limit, int flags)
1232 struct ucred *cred = td->td_ucred;
1234 struct sseg_closure seginfo;
1235 struct note_info_list notelst;
1236 struct coredump_params params;
1237 struct note_info *ninfo;
1239 size_t hdrsize, notesz, coresize;
1242 compress = (flags & IMGACT_CORE_COMPRESS) != 0;
1245 TAILQ_INIT(¬elst);
1247 /* Size the program segments. */
1250 each_writable_segment(td, cb_size_segment, &seginfo);
1253 * Collect info about the core file header area.
1255 hdrsize = sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * (1 + seginfo.count);
1256 __elfN(prepare_notes)(td, ¬elst, ¬esz);
1257 coresize = round_page(hdrsize + notesz) + seginfo.size;
1259 /* Set up core dump parameters. */
1261 params.active_cred = cred;
1262 params.file_cred = NOCRED;
1269 PROC_LOCK(td->td_proc);
1270 error = racct_add(td->td_proc, RACCT_CORE, coresize);
1271 PROC_UNLOCK(td->td_proc);
1278 if (coresize >= limit) {
1284 /* Create a compression stream if necessary. */
1286 params.gzs = gzio_init(core_gz_write, GZIO_DEFLATE,
1287 CORE_BUF_SIZE, compress_user_cores_gzlevel, ¶ms);
1288 if (params.gzs == NULL) {
1292 tmpbuf = malloc(CORE_BUF_SIZE, M_TEMP, M_WAITOK | M_ZERO);
1297 * Allocate memory for building the header, fill it up,
1298 * and write it out following the notes.
1300 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
1305 error = __elfN(corehdr)(¶ms, seginfo.count, hdr, hdrsize, ¬elst,
1308 /* Write the contents of all of the writable segments. */
1314 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
1315 offset = round_page(hdrsize + notesz);
1316 for (i = 0; i < seginfo.count; i++) {
1317 error = core_output((caddr_t)(uintptr_t)php->p_vaddr,
1318 php->p_filesz, offset, ¶ms, tmpbuf);
1321 offset += php->p_filesz;
1325 if (error == 0 && compress)
1326 error = gzio_flush(params.gzs);
1331 "Failed to write core file for process %s (error %d)\n",
1332 curproc->p_comm, error);
1338 free(tmpbuf, M_TEMP);
1339 if (params.gzs != NULL)
1340 gzio_fini(params.gzs);
1343 while ((ninfo = TAILQ_FIRST(¬elst)) != NULL) {
1344 TAILQ_REMOVE(¬elst, ninfo, link);
1345 free(ninfo, M_TEMP);
1354 * A callback for each_writable_segment() to write out the segment's
1355 * program header entry.
1358 cb_put_phdr(entry, closure)
1359 vm_map_entry_t entry;
1362 struct phdr_closure *phc = (struct phdr_closure *)closure;
1363 Elf_Phdr *phdr = phc->phdr;
1365 phc->offset = round_page(phc->offset);
1367 phdr->p_type = PT_LOAD;
1368 phdr->p_offset = phc->offset;
1369 phdr->p_vaddr = entry->start;
1371 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1372 phdr->p_align = PAGE_SIZE;
1373 phdr->p_flags = __elfN(untrans_prot)(entry->protection);
1375 phc->offset += phdr->p_filesz;
1380 * A callback for each_writable_segment() to gather information about
1381 * the number of segments and their total size.
1384 cb_size_segment(entry, closure)
1385 vm_map_entry_t entry;
1388 struct sseg_closure *ssc = (struct sseg_closure *)closure;
1391 ssc->size += entry->end - entry->start;
1395 * For each writable segment in the process's memory map, call the given
1396 * function with a pointer to the map entry and some arbitrary
1397 * caller-supplied data.
1400 each_writable_segment(td, func, closure)
1402 segment_callback func;
1405 struct proc *p = td->td_proc;
1406 vm_map_t map = &p->p_vmspace->vm_map;
1407 vm_map_entry_t entry;
1408 vm_object_t backing_object, object;
1409 boolean_t ignore_entry;
1411 vm_map_lock_read(map);
1412 for (entry = map->header.next; entry != &map->header;
1413 entry = entry->next) {
1415 * Don't dump inaccessible mappings, deal with legacy
1418 * Note that read-only segments related to the elf binary
1419 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1420 * need to arbitrarily ignore such segments.
1422 if (elf_legacy_coredump) {
1423 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
1426 if ((entry->protection & VM_PROT_ALL) == 0)
1431 * Dont include memory segment in the coredump if
1432 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1433 * madvise(2). Do not dump submaps (i.e. parts of the
1436 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1439 if ((object = entry->object.vm_object) == NULL)
1442 /* Ignore memory-mapped devices and such things. */
1443 VM_OBJECT_RLOCK(object);
1444 while ((backing_object = object->backing_object) != NULL) {
1445 VM_OBJECT_RLOCK(backing_object);
1446 VM_OBJECT_RUNLOCK(object);
1447 object = backing_object;
1449 ignore_entry = object->type != OBJT_DEFAULT &&
1450 object->type != OBJT_SWAP && object->type != OBJT_VNODE &&
1451 object->type != OBJT_PHYS;
1452 VM_OBJECT_RUNLOCK(object);
1456 (*func)(entry, closure);
1458 vm_map_unlock_read(map);
1462 * Write the core file header to the file, including padding up to
1463 * the page boundary.
1466 __elfN(corehdr)(struct coredump_params *p, int numsegs, void *hdr,
1467 size_t hdrsize, struct note_info_list *notelst, size_t notesz)
1469 struct note_info *ninfo;
1473 /* Fill in the header. */
1474 bzero(hdr, hdrsize);
1475 __elfN(puthdr)(p->td, hdr, hdrsize, numsegs, notesz);
1477 sb = sbuf_new(NULL, NULL, CORE_BUF_SIZE, SBUF_FIXEDLEN);
1478 sbuf_set_drain(sb, sbuf_drain_core_output, p);
1479 sbuf_start_section(sb, NULL);
1480 sbuf_bcat(sb, hdr, hdrsize);
1481 TAILQ_FOREACH(ninfo, notelst, link)
1482 __elfN(putnote)(ninfo, sb);
1483 /* Align up to a page boundary for the program segments. */
1484 sbuf_end_section(sb, -1, PAGE_SIZE, 0);
1485 error = sbuf_finish(sb);
1492 __elfN(prepare_notes)(struct thread *td, struct note_info_list *list,
1502 size += register_note(list, NT_PRPSINFO, __elfN(note_prpsinfo), p);
1505 * To have the debugger select the right thread (LWP) as the initial
1506 * thread, we dump the state of the thread passed to us in td first.
1507 * This is the thread that causes the core dump and thus likely to
1508 * be the right thread one wants to have selected in the debugger.
1511 while (thr != NULL) {
1512 size += register_note(list, NT_PRSTATUS,
1513 __elfN(note_prstatus), thr);
1514 size += register_note(list, NT_FPREGSET,
1515 __elfN(note_fpregset), thr);
1516 size += register_note(list, NT_THRMISC,
1517 __elfN(note_thrmisc), thr);
1518 size += register_note(list, -1,
1519 __elfN(note_threadmd), thr);
1521 thr = (thr == td) ? TAILQ_FIRST(&p->p_threads) :
1522 TAILQ_NEXT(thr, td_plist);
1524 thr = TAILQ_NEXT(thr, td_plist);
1527 size += register_note(list, NT_PROCSTAT_PROC,
1528 __elfN(note_procstat_proc), p);
1529 size += register_note(list, NT_PROCSTAT_FILES,
1530 note_procstat_files, p);
1531 size += register_note(list, NT_PROCSTAT_VMMAP,
1532 note_procstat_vmmap, p);
1533 size += register_note(list, NT_PROCSTAT_GROUPS,
1534 note_procstat_groups, p);
1535 size += register_note(list, NT_PROCSTAT_UMASK,
1536 note_procstat_umask, p);
1537 size += register_note(list, NT_PROCSTAT_RLIMIT,
1538 note_procstat_rlimit, p);
1539 size += register_note(list, NT_PROCSTAT_OSREL,
1540 note_procstat_osrel, p);
1541 size += register_note(list, NT_PROCSTAT_PSSTRINGS,
1542 __elfN(note_procstat_psstrings), p);
1543 size += register_note(list, NT_PROCSTAT_AUXV,
1544 __elfN(note_procstat_auxv), p);
1550 __elfN(puthdr)(struct thread *td, void *hdr, size_t hdrsize, int numsegs,
1555 struct phdr_closure phc;
1557 ehdr = (Elf_Ehdr *)hdr;
1558 phdr = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr));
1560 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1561 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1562 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1563 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1564 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1565 ehdr->e_ident[EI_DATA] = ELF_DATA;
1566 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1567 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1568 ehdr->e_ident[EI_ABIVERSION] = 0;
1569 ehdr->e_ident[EI_PAD] = 0;
1570 ehdr->e_type = ET_CORE;
1571 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1572 ehdr->e_machine = ELF_ARCH32;
1574 ehdr->e_machine = ELF_ARCH;
1576 ehdr->e_version = EV_CURRENT;
1578 ehdr->e_phoff = sizeof(Elf_Ehdr);
1580 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1581 ehdr->e_phentsize = sizeof(Elf_Phdr);
1582 ehdr->e_phnum = numsegs + 1;
1583 ehdr->e_shentsize = sizeof(Elf_Shdr);
1585 ehdr->e_shstrndx = SHN_UNDEF;
1588 * Fill in the program header entries.
1591 /* The note segement. */
1592 phdr->p_type = PT_NOTE;
1593 phdr->p_offset = hdrsize;
1596 phdr->p_filesz = notesz;
1598 phdr->p_flags = PF_R;
1599 phdr->p_align = ELF_NOTE_ROUNDSIZE;
1602 /* All the writable segments from the program. */
1604 phc.offset = round_page(hdrsize + notesz);
1605 each_writable_segment(td, cb_put_phdr, &phc);
1609 register_note(struct note_info_list *list, int type, outfunc_t out, void *arg)
1611 struct note_info *ninfo;
1612 size_t size, notesize;
1615 out(arg, NULL, &size);
1616 ninfo = malloc(sizeof(*ninfo), M_TEMP, M_ZERO | M_WAITOK);
1618 ninfo->outfunc = out;
1619 ninfo->outarg = arg;
1620 ninfo->outsize = size;
1621 TAILQ_INSERT_TAIL(list, ninfo, link);
1626 notesize = sizeof(Elf_Note) + /* note header */
1627 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1629 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
1635 append_note_data(const void *src, void *dst, size_t len)
1639 padded_len = roundup2(len, ELF_NOTE_ROUNDSIZE);
1641 bcopy(src, dst, len);
1642 bzero((char *)dst + len, padded_len - len);
1644 return (padded_len);
1648 __elfN(populate_note)(int type, void *src, void *dst, size_t size, void **descp)
1656 note = (Elf_Note *)buf;
1657 note->n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1658 note->n_descsz = size;
1659 note->n_type = type;
1660 buf += sizeof(*note);
1661 buf += append_note_data(FREEBSD_ABI_VENDOR, buf,
1662 sizeof(FREEBSD_ABI_VENDOR));
1663 append_note_data(src, buf, size);
1668 notesize = sizeof(Elf_Note) + /* note header */
1669 roundup2(sizeof(FREEBSD_ABI_VENDOR), ELF_NOTE_ROUNDSIZE) +
1671 roundup2(size, ELF_NOTE_ROUNDSIZE); /* note description */
1677 __elfN(putnote)(struct note_info *ninfo, struct sbuf *sb)
1680 ssize_t old_len, sect_len;
1681 size_t new_len, descsz, i;
1683 if (ninfo->type == -1) {
1684 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1688 note.n_namesz = sizeof(FREEBSD_ABI_VENDOR);
1689 note.n_descsz = ninfo->outsize;
1690 note.n_type = ninfo->type;
1692 sbuf_bcat(sb, ¬e, sizeof(note));
1693 sbuf_start_section(sb, &old_len);
1694 sbuf_bcat(sb, FREEBSD_ABI_VENDOR, sizeof(FREEBSD_ABI_VENDOR));
1695 sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1696 if (note.n_descsz == 0)
1698 sbuf_start_section(sb, &old_len);
1699 ninfo->outfunc(ninfo->outarg, sb, &ninfo->outsize);
1700 sect_len = sbuf_end_section(sb, old_len, ELF_NOTE_ROUNDSIZE, 0);
1704 new_len = (size_t)sect_len;
1705 descsz = roundup(note.n_descsz, ELF_NOTE_ROUNDSIZE);
1706 if (new_len < descsz) {
1708 * It is expected that individual note emitters will correctly
1709 * predict their expected output size and fill up to that size
1710 * themselves, padding in a format-specific way if needed.
1711 * However, in case they don't, just do it here with zeros.
1713 for (i = 0; i < descsz - new_len; i++)
1715 } else if (new_len > descsz) {
1717 * We can't always truncate sb -- we may have drained some
1720 KASSERT(new_len == descsz, ("%s: Note type %u changed as we "
1721 "read it (%zu > %zu). Since it is longer than "
1722 "expected, this coredump's notes are corrupt. THIS "
1723 "IS A BUG in the note_procstat routine for type %u.\n",
1724 __func__, (unsigned)note.n_type, new_len, descsz,
1725 (unsigned)note.n_type));
1730 * Miscellaneous note out functions.
1733 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1734 #include <compat/freebsd32/freebsd32.h>
1736 typedef struct prstatus32 elf_prstatus_t;
1737 typedef struct prpsinfo32 elf_prpsinfo_t;
1738 typedef struct fpreg32 elf_prfpregset_t;
1739 typedef struct fpreg32 elf_fpregset_t;
1740 typedef struct reg32 elf_gregset_t;
1741 typedef struct thrmisc32 elf_thrmisc_t;
1742 #define ELF_KERN_PROC_MASK KERN_PROC_MASK32
1743 typedef struct kinfo_proc32 elf_kinfo_proc_t;
1744 typedef uint32_t elf_ps_strings_t;
1746 typedef prstatus_t elf_prstatus_t;
1747 typedef prpsinfo_t elf_prpsinfo_t;
1748 typedef prfpregset_t elf_prfpregset_t;
1749 typedef prfpregset_t elf_fpregset_t;
1750 typedef gregset_t elf_gregset_t;
1751 typedef thrmisc_t elf_thrmisc_t;
1752 #define ELF_KERN_PROC_MASK 0
1753 typedef struct kinfo_proc elf_kinfo_proc_t;
1754 typedef vm_offset_t elf_ps_strings_t;
1758 __elfN(note_prpsinfo)(void *arg, struct sbuf *sb, size_t *sizep)
1761 elf_prpsinfo_t *psinfo;
1763 p = (struct proc *)arg;
1765 KASSERT(*sizep == sizeof(*psinfo), ("invalid size"));
1766 psinfo = malloc(sizeof(*psinfo), M_TEMP, M_ZERO | M_WAITOK);
1767 psinfo->pr_version = PRPSINFO_VERSION;
1768 psinfo->pr_psinfosz = sizeof(elf_prpsinfo_t);
1769 strlcpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname));
1771 * XXX - We don't fill in the command line arguments properly
1774 strlcpy(psinfo->pr_psargs, p->p_comm,
1775 sizeof(psinfo->pr_psargs));
1777 sbuf_bcat(sb, psinfo, sizeof(*psinfo));
1778 free(psinfo, M_TEMP);
1780 *sizep = sizeof(*psinfo);
1784 __elfN(note_prstatus)(void *arg, struct sbuf *sb, size_t *sizep)
1787 elf_prstatus_t *status;
1789 td = (struct thread *)arg;
1791 KASSERT(*sizep == sizeof(*status), ("invalid size"));
1792 status = malloc(sizeof(*status), M_TEMP, M_ZERO | M_WAITOK);
1793 status->pr_version = PRSTATUS_VERSION;
1794 status->pr_statussz = sizeof(elf_prstatus_t);
1795 status->pr_gregsetsz = sizeof(elf_gregset_t);
1796 status->pr_fpregsetsz = sizeof(elf_fpregset_t);
1797 status->pr_osreldate = osreldate;
1798 status->pr_cursig = td->td_proc->p_sig;
1799 status->pr_pid = td->td_tid;
1800 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1801 fill_regs32(td, &status->pr_reg);
1803 fill_regs(td, &status->pr_reg);
1805 sbuf_bcat(sb, status, sizeof(*status));
1806 free(status, M_TEMP);
1808 *sizep = sizeof(*status);
1812 __elfN(note_fpregset)(void *arg, struct sbuf *sb, size_t *sizep)
1815 elf_prfpregset_t *fpregset;
1817 td = (struct thread *)arg;
1819 KASSERT(*sizep == sizeof(*fpregset), ("invalid size"));
1820 fpregset = malloc(sizeof(*fpregset), M_TEMP, M_ZERO | M_WAITOK);
1821 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
1822 fill_fpregs32(td, fpregset);
1824 fill_fpregs(td, fpregset);
1826 sbuf_bcat(sb, fpregset, sizeof(*fpregset));
1827 free(fpregset, M_TEMP);
1829 *sizep = sizeof(*fpregset);
1833 __elfN(note_thrmisc)(void *arg, struct sbuf *sb, size_t *sizep)
1836 elf_thrmisc_t thrmisc;
1838 td = (struct thread *)arg;
1840 KASSERT(*sizep == sizeof(thrmisc), ("invalid size"));
1841 bzero(&thrmisc._pad, sizeof(thrmisc._pad));
1842 strcpy(thrmisc.pr_tname, td->td_name);
1843 sbuf_bcat(sb, &thrmisc, sizeof(thrmisc));
1845 *sizep = sizeof(thrmisc);
1849 * Allow for MD specific notes, as well as any MD
1850 * specific preparations for writing MI notes.
1853 __elfN(note_threadmd)(void *arg, struct sbuf *sb, size_t *sizep)
1859 td = (struct thread *)arg;
1861 if (size != 0 && sb != NULL)
1862 buf = malloc(size, M_TEMP, M_ZERO | M_WAITOK);
1866 __elfN(dump_thread)(td, buf, &size);
1867 KASSERT(sb == NULL || *sizep == size, ("invalid size"));
1868 if (size != 0 && sb != NULL)
1869 sbuf_bcat(sb, buf, size);
1874 #ifdef KINFO_PROC_SIZE
1875 CTASSERT(sizeof(struct kinfo_proc) == KINFO_PROC_SIZE);
1879 __elfN(note_procstat_proc)(void *arg, struct sbuf *sb, size_t *sizep)
1885 p = (struct proc *)arg;
1886 size = sizeof(structsize) + p->p_numthreads *
1887 sizeof(elf_kinfo_proc_t);
1890 KASSERT(*sizep == size, ("invalid size"));
1891 structsize = sizeof(elf_kinfo_proc_t);
1892 sbuf_bcat(sb, &structsize, sizeof(structsize));
1893 sx_slock(&proctree_lock);
1895 kern_proc_out(p, sb, ELF_KERN_PROC_MASK);
1896 sx_sunlock(&proctree_lock);
1901 #ifdef KINFO_FILE_SIZE
1902 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
1906 note_procstat_files(void *arg, struct sbuf *sb, size_t *sizep)
1909 size_t size, sect_sz, i;
1910 ssize_t start_len, sect_len;
1911 int structsize, filedesc_flags;
1913 if (coredump_pack_fileinfo)
1914 filedesc_flags = KERN_FILEDESC_PACK_KINFO;
1918 p = (struct proc *)arg;
1919 structsize = sizeof(struct kinfo_file);
1922 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1923 sbuf_set_drain(sb, sbuf_drain_count, &size);
1924 sbuf_bcat(sb, &structsize, sizeof(structsize));
1926 kern_proc_filedesc_out(p, sb, -1, filedesc_flags);
1931 sbuf_start_section(sb, &start_len);
1933 sbuf_bcat(sb, &structsize, sizeof(structsize));
1935 kern_proc_filedesc_out(p, sb, *sizep - sizeof(structsize),
1938 sect_len = sbuf_end_section(sb, start_len, 0, 0);
1943 KASSERT(sect_sz <= *sizep,
1944 ("kern_proc_filedesc_out did not respect maxlen; "
1945 "requested %zu, got %zu", *sizep - sizeof(structsize),
1946 sect_sz - sizeof(structsize)));
1948 for (i = 0; i < *sizep - sect_sz && sb->s_error == 0; i++)
1953 #ifdef KINFO_VMENTRY_SIZE
1954 CTASSERT(sizeof(struct kinfo_vmentry) == KINFO_VMENTRY_SIZE);
1958 note_procstat_vmmap(void *arg, struct sbuf *sb, size_t *sizep)
1962 int structsize, vmmap_flags;
1964 if (coredump_pack_vmmapinfo)
1965 vmmap_flags = KERN_VMMAP_PACK_KINFO;
1969 p = (struct proc *)arg;
1970 structsize = sizeof(struct kinfo_vmentry);
1973 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
1974 sbuf_set_drain(sb, sbuf_drain_count, &size);
1975 sbuf_bcat(sb, &structsize, sizeof(structsize));
1977 kern_proc_vmmap_out(p, sb, -1, vmmap_flags);
1982 sbuf_bcat(sb, &structsize, sizeof(structsize));
1984 kern_proc_vmmap_out(p, sb, *sizep - sizeof(structsize),
1990 note_procstat_groups(void *arg, struct sbuf *sb, size_t *sizep)
1996 p = (struct proc *)arg;
1997 size = sizeof(structsize) + p->p_ucred->cr_ngroups * sizeof(gid_t);
1999 KASSERT(*sizep == size, ("invalid size"));
2000 structsize = sizeof(gid_t);
2001 sbuf_bcat(sb, &structsize, sizeof(structsize));
2002 sbuf_bcat(sb, p->p_ucred->cr_groups, p->p_ucred->cr_ngroups *
2009 note_procstat_umask(void *arg, struct sbuf *sb, size_t *sizep)
2015 p = (struct proc *)arg;
2016 size = sizeof(structsize) + sizeof(p->p_fd->fd_cmask);
2018 KASSERT(*sizep == size, ("invalid size"));
2019 structsize = sizeof(p->p_fd->fd_cmask);
2020 sbuf_bcat(sb, &structsize, sizeof(structsize));
2021 sbuf_bcat(sb, &p->p_fd->fd_cmask, sizeof(p->p_fd->fd_cmask));
2027 note_procstat_rlimit(void *arg, struct sbuf *sb, size_t *sizep)
2030 struct rlimit rlim[RLIM_NLIMITS];
2034 p = (struct proc *)arg;
2035 size = sizeof(structsize) + sizeof(rlim);
2037 KASSERT(*sizep == size, ("invalid size"));
2038 structsize = sizeof(rlim);
2039 sbuf_bcat(sb, &structsize, sizeof(structsize));
2041 for (i = 0; i < RLIM_NLIMITS; i++)
2042 lim_rlimit_proc(p, i, &rlim[i]);
2044 sbuf_bcat(sb, rlim, sizeof(rlim));
2050 note_procstat_osrel(void *arg, struct sbuf *sb, size_t *sizep)
2056 p = (struct proc *)arg;
2057 size = sizeof(structsize) + sizeof(p->p_osrel);
2059 KASSERT(*sizep == size, ("invalid size"));
2060 structsize = sizeof(p->p_osrel);
2061 sbuf_bcat(sb, &structsize, sizeof(structsize));
2062 sbuf_bcat(sb, &p->p_osrel, sizeof(p->p_osrel));
2068 __elfN(note_procstat_psstrings)(void *arg, struct sbuf *sb, size_t *sizep)
2071 elf_ps_strings_t ps_strings;
2075 p = (struct proc *)arg;
2076 size = sizeof(structsize) + sizeof(ps_strings);
2078 KASSERT(*sizep == size, ("invalid size"));
2079 structsize = sizeof(ps_strings);
2080 #if defined(COMPAT_FREEBSD32) && __ELF_WORD_SIZE == 32
2081 ps_strings = PTROUT(p->p_sysent->sv_psstrings);
2083 ps_strings = p->p_sysent->sv_psstrings;
2085 sbuf_bcat(sb, &structsize, sizeof(structsize));
2086 sbuf_bcat(sb, &ps_strings, sizeof(ps_strings));
2092 __elfN(note_procstat_auxv)(void *arg, struct sbuf *sb, size_t *sizep)
2098 p = (struct proc *)arg;
2101 sb = sbuf_new(NULL, NULL, 128, SBUF_FIXEDLEN);
2102 sbuf_set_drain(sb, sbuf_drain_count, &size);
2103 sbuf_bcat(sb, &structsize, sizeof(structsize));
2105 proc_getauxv(curthread, p, sb);
2111 structsize = sizeof(Elf_Auxinfo);
2112 sbuf_bcat(sb, &structsize, sizeof(structsize));
2114 proc_getauxv(curthread, p, sb);
2120 __elfN(parse_notes)(struct image_params *imgp, Elf_Brandnote *checknote,
2121 int32_t *osrel, const Elf_Phdr *pnote)
2123 const Elf_Note *note, *note0, *note_end;
2124 const char *note_name;
2127 if (pnote == NULL || pnote->p_offset > PAGE_SIZE ||
2128 pnote->p_filesz > PAGE_SIZE - pnote->p_offset)
2131 note = note0 = (const Elf_Note *)(imgp->image_header + pnote->p_offset);
2132 note_end = (const Elf_Note *)(imgp->image_header +
2133 pnote->p_offset + pnote->p_filesz);
2134 for (i = 0; i < 100 && note >= note0 && note < note_end; i++) {
2135 if (!aligned(note, Elf32_Addr) || (const char *)note_end -
2136 (const char *)note < sizeof(Elf_Note))
2138 if (note->n_namesz != checknote->hdr.n_namesz ||
2139 note->n_descsz != checknote->hdr.n_descsz ||
2140 note->n_type != checknote->hdr.n_type)
2142 note_name = (const char *)(note + 1);
2143 if (note_name + checknote->hdr.n_namesz >=
2144 (const char *)note_end || strncmp(checknote->vendor,
2145 note_name, checknote->hdr.n_namesz) != 0)
2149 * Fetch the osreldate for binary
2150 * from the ELF OSABI-note if necessary.
2152 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
2153 checknote->trans_osrel != NULL)
2154 return (checknote->trans_osrel(note, osrel));
2158 note = (const Elf_Note *)((const char *)(note + 1) +
2159 roundup2(note->n_namesz, ELF_NOTE_ROUNDSIZE) +
2160 roundup2(note->n_descsz, ELF_NOTE_ROUNDSIZE));
2167 * Try to find the appropriate ABI-note section for checknote,
2168 * fetch the osreldate for binary from the ELF OSABI-note. Only the
2169 * first page of the image is searched, the same as for headers.
2172 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
2175 const Elf_Phdr *phdr;
2176 const Elf_Ehdr *hdr;
2179 hdr = (const Elf_Ehdr *)imgp->image_header;
2180 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
2182 for (i = 0; i < hdr->e_phnum; i++) {
2183 if (phdr[i].p_type == PT_NOTE &&
2184 __elfN(parse_notes)(imgp, checknote, osrel, &phdr[i]))
2192 * Tell kern_execve.c about it, with a little help from the linker.
2194 static struct execsw __elfN(execsw) = {
2195 __CONCAT(exec_, __elfN(imgact)),
2196 __XSTRING(__CONCAT(ELF, __ELF_WORD_SIZE))
2198 EXEC_SET(__CONCAT(elf, __ELF_WORD_SIZE), __elfN(execsw));
2201 __elfN(trans_prot)(Elf_Word flags)
2207 prot |= VM_PROT_EXECUTE;
2209 prot |= VM_PROT_WRITE;
2211 prot |= VM_PROT_READ;
2212 #if __ELF_WORD_SIZE == 32
2213 #if defined(__amd64__)
2214 if (i386_read_exec && (flags & PF_R))
2215 prot |= VM_PROT_EXECUTE;
2222 __elfN(untrans_prot)(vm_prot_t prot)
2227 if (prot & VM_PROT_EXECUTE)
2229 if (prot & VM_PROT_READ)
2231 if (prot & VM_PROT_WRITE)