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.
30 * $FreeBSD: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 dillon Exp $
33 #include <sys/param.h>
35 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/systm.h>
44 #include <sys/nlookup.h>
45 #include <sys/pioctl.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
50 #include <sys/syscall.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/vnode.h>
54 #include <sys/eventhandler.h>
56 #include <cpu/lwbuf.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_param.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_extern.h>
67 #include <machine/elf.h>
68 #include <machine/md_var.h>
69 #include <sys/mount.h>
72 #define OLD_EI_BRAND 8
73 #define truncps(va,ps) ((va) & ~(ps - 1))
74 #define aligned(a,t) (truncps((u_long)(a), sizeof(t)) == (u_long)(a))
76 static int __elfN(check_header)(const Elf_Ehdr *hdr);
77 static Elf_Brandinfo *__elfN(get_brandinfo)(struct image_params *imgp,
78 const char *interp, int32_t *osrel);
79 static int __elfN(load_file)(struct proc *p, const char *file, u_long *addr,
81 static int __elfN(load_section)(struct proc *p,
82 struct vmspace *vmspace, struct vnode *vp,
83 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
85 static int __CONCAT(exec_, __elfN(imgact))(struct image_params *imgp);
86 static boolean_t __elfN(bsd_trans_osrel)(const Elf_Note *note,
88 static boolean_t __elfN(check_note)(struct image_params *imgp,
89 Elf_Brandnote *checknote, int32_t *osrel);
90 static boolean_t check_PT_NOTE(struct image_params *imgp,
91 Elf_Brandnote *checknote, int32_t *osrel, const Elf_Phdr * pnote);
92 static boolean_t extract_interpreter(struct image_params *imgp,
93 const Elf_Phdr *pinterpreter, char *data);
95 static int elf_legacy_coredump = 0;
96 static int __elfN(fallback_brand) = -1;
97 #if defined(__x86_64__)
98 SYSCTL_NODE(_kern, OID_AUTO, elf64, CTLFLAG_RW, 0, "");
99 SYSCTL_INT(_debug, OID_AUTO, elf64_legacy_coredump, CTLFLAG_RW,
100 &elf_legacy_coredump, 0, "legacy coredump mode");
101 SYSCTL_INT(_kern_elf64, OID_AUTO, fallback_brand, CTLFLAG_RW,
102 &elf64_fallback_brand, 0, "ELF64 brand of last resort");
103 TUNABLE_INT("kern.elf64.fallback_brand", &elf64_fallback_brand);
104 #else /* i386 assumed */
105 SYSCTL_NODE(_kern, OID_AUTO, elf32, CTLFLAG_RW, 0, "");
106 SYSCTL_INT(_debug, OID_AUTO, elf32_legacy_coredump, CTLFLAG_RW,
107 &elf_legacy_coredump, 0, "legacy coredump mode");
108 SYSCTL_INT(_kern_elf32, OID_AUTO, fallback_brand, CTLFLAG_RW,
109 &elf32_fallback_brand, 0, "ELF32 brand of last resort");
110 TUNABLE_INT("kern.elf32.fallback_brand", &elf32_fallback_brand);
113 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS];
115 static const char DRAGONFLY_ABI_VENDOR[] = "DragonFly";
116 static const char FREEBSD_ABI_VENDOR[] = "FreeBSD";
118 Elf_Brandnote __elfN(dragonfly_brandnote) = {
119 .hdr.n_namesz = sizeof(DRAGONFLY_ABI_VENDOR),
120 .hdr.n_descsz = sizeof(int32_t),
122 .vendor = DRAGONFLY_ABI_VENDOR,
123 .flags = BN_TRANSLATE_OSREL,
124 .trans_osrel = __elfN(bsd_trans_osrel),
127 Elf_Brandnote __elfN(freebsd_brandnote) = {
128 .hdr.n_namesz = sizeof(FREEBSD_ABI_VENDOR),
129 .hdr.n_descsz = sizeof(int32_t),
131 .vendor = FREEBSD_ABI_VENDOR,
132 .flags = BN_TRANSLATE_OSREL,
133 .trans_osrel = __elfN(bsd_trans_osrel),
137 __elfN(insert_brand_entry)(Elf_Brandinfo *entry)
141 for (i = 0; i < MAX_BRANDS; i++) {
142 if (elf_brand_list[i] == NULL) {
143 elf_brand_list[i] = entry;
147 if (i == MAX_BRANDS) {
148 uprintf("WARNING: %s: could not insert brandinfo entry: %p\n",
156 __elfN(remove_brand_entry)(Elf_Brandinfo *entry)
160 for (i = 0; i < MAX_BRANDS; i++) {
161 if (elf_brand_list[i] == entry) {
162 elf_brand_list[i] = NULL;
172 * Check if an elf brand is being used anywhere in the system.
174 * Used by the linux emulation module unloader. This isn't safe from
177 struct elf_brand_inuse_info {
179 Elf_Brandinfo *entry;
182 static int elf_brand_inuse_callback(struct proc *p, void *data);
185 __elfN(brand_inuse)(Elf_Brandinfo *entry)
187 struct elf_brand_inuse_info info;
191 allproc_scan(elf_brand_inuse_callback, entry);
197 elf_brand_inuse_callback(struct proc *p, void *data)
199 struct elf_brand_inuse_info *info = data;
201 if (p->p_sysent == info->entry->sysvec) {
209 __elfN(check_header)(const Elf_Ehdr *hdr)
215 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
216 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
217 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
218 hdr->e_phentsize != sizeof(Elf_Phdr) ||
219 hdr->e_ehsize != sizeof(Elf_Ehdr) ||
220 hdr->e_version != ELF_TARG_VER)
224 * Make sure we have at least one brand for this machine.
227 for (i = 0; i < MAX_BRANDS; i++) {
228 bi = elf_brand_list[i];
229 if (bi != NULL && bi->machine == hdr->e_machine)
239 __elfN(load_section)(struct proc *p, struct vmspace *vmspace, struct vnode *vp,
240 vm_offset_t offset, caddr_t vmaddr, size_t memsz,
241 size_t filsz, vm_prot_t prot)
244 vm_offset_t map_addr;
249 vm_offset_t file_addr;
251 object = vp->v_object;
254 vm_object_hold(object);
257 * It's necessary to fail if the filsz + offset taken from the
258 * header is greater than the actual file pager object's size.
259 * If we were to allow this, then the vm_map_find() below would
260 * walk right off the end of the file object and into the ether.
262 * While I'm here, might as well check for something else that
263 * is invalid: filsz cannot be greater than memsz.
265 if ((off_t)filsz + offset > vp->v_filesize || filsz > memsz) {
266 uprintf("elf_load_section: truncated ELF file\n");
267 vm_object_drop(object);
271 map_addr = trunc_page((vm_offset_t)vmaddr);
272 file_addr = trunc_page(offset);
275 * We have two choices. We can either clear the data in the last page
276 * of an oversized mapping, or we can start the anon mapping a page
277 * early and copy the initialized data into that first page. We
278 * choose the second..
281 map_len = trunc_page(offset+filsz) - file_addr;
283 map_len = round_page(offset+filsz) - file_addr;
286 vm_object_reference_locked(object);
288 /* cow flags: don't dump readonly sections in core */
289 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
290 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
292 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
293 vm_map_lock(&vmspace->vm_map);
294 rv = vm_map_insert(&vmspace->vm_map, &count,
296 file_addr, /* file offset */
297 map_addr, /* virtual start */
298 map_addr + map_len,/* virtual end */
302 vm_map_unlock(&vmspace->vm_map);
303 vm_map_entry_release(count);
304 if (rv != KERN_SUCCESS) {
305 vm_object_deallocate(object);
306 vm_object_drop(object);
310 /* we can stop now if we've covered it all */
311 if (memsz == filsz) {
312 vm_object_drop(object);
319 * We have to get the remaining bit of the file into the first part
320 * of the oversized map segment. This is normally because the .data
321 * segment in the file is extended to provide bss. It's a neat idea
322 * to try and save a page, but it's a pain in the behind to implement.
324 copy_len = (offset + filsz) - trunc_page(offset + filsz);
325 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
326 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
328 /* This had damn well better be true! */
330 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
331 vm_map_lock(&vmspace->vm_map);
332 rv = vm_map_insert(&vmspace->vm_map, &count,
334 map_addr, map_addr + map_len,
336 VM_PROT_ALL, VM_PROT_ALL,
338 vm_map_unlock(&vmspace->vm_map);
339 vm_map_entry_release(count);
340 if (rv != KERN_SUCCESS) {
341 vm_object_drop(object);
349 struct lwbuf lwb_cache;
351 m = vm_fault_object_page(object, trunc_page(offset + filsz),
352 VM_PROT_READ, 0, &error);
354 lwb = lwbuf_alloc(m, &lwb_cache);
355 error = copyout((caddr_t)lwbuf_kva(lwb),
356 (caddr_t)map_addr, copy_len);
361 vm_object_drop(object);
366 vm_object_drop(object);
368 * set it to the specified protection
370 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,
377 * Load the file "file" into memory. It may be either a shared object
380 * The "addr" reference parameter is in/out. On entry, it specifies
381 * the address where a shared object should be loaded. If the file is
382 * an executable, this value is ignored. On exit, "addr" specifies
383 * where the file was actually loaded.
385 * The "entry" reference parameter is out only. On exit, it specifies
386 * the entry point for the loaded file.
389 __elfN(load_file)(struct proc *p, const char *file, u_long *addr, u_long *entry)
392 struct nlookupdata nd;
394 struct image_params image_params;
396 const Elf_Ehdr *hdr = NULL;
397 const Elf_Phdr *phdr = NULL;
398 struct nlookupdata *nd;
399 struct vmspace *vmspace = p->p_vmspace;
401 struct image_params *imgp;
402 struct mount *topmnt;
405 u_long base_addr = 0;
406 int error, i, numsegs;
408 tempdata = kmalloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
410 attr = &tempdata->attr;
411 imgp = &tempdata->image_params;
414 * Initialize part of the common data
418 imgp->firstpage = NULL;
419 imgp->image_header = NULL;
422 error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW);
426 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
427 topmnt = nd->nl_nch.mount;
433 * Check permissions, modes, uid, etc on the file, and "open" it.
435 error = exec_check_permissions(imgp, topmnt);
441 error = exec_map_first_page(imgp);
443 * Also make certain that the interpreter stays the same, so set
444 * its VTEXT flag, too.
447 vsetflags(imgp->vp, VTEXT);
452 hdr = (const Elf_Ehdr *)imgp->image_header;
453 if ((error = __elfN(check_header)(hdr)) != 0)
455 if (hdr->e_type == ET_DYN)
457 else if (hdr->e_type == ET_EXEC)
464 /* Only support headers that fit within first page for now */
465 /* (multiplication of two Elf_Half fields will not overflow) */
466 if ((hdr->e_phoff > PAGE_SIZE) ||
467 (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
472 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
473 if (!aligned(phdr, Elf_Addr)) {
478 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
479 if (phdr[i].p_type == PT_LOAD && phdr[i].p_memsz != 0) {
480 /* Loadable segment */
482 if (phdr[i].p_flags & PF_X)
483 prot |= VM_PROT_EXECUTE;
484 if (phdr[i].p_flags & PF_W)
485 prot |= VM_PROT_WRITE;
486 if (phdr[i].p_flags & PF_R)
487 prot |= VM_PROT_READ;
489 error = __elfN(load_section)(
490 p, vmspace, imgp->vp,
492 (caddr_t)phdr[i].p_vaddr +
495 phdr[i].p_filesz, prot);
499 * Establish the base address if this is the
503 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
508 *entry = (unsigned long)hdr->e_entry + rbase;
512 exec_unmap_first_page(imgp);
517 kfree(tempdata, M_TEMP);
522 static Elf_Brandinfo *
523 __elfN(get_brandinfo)(struct image_params *imgp, const char *interp,
526 const Elf_Ehdr *hdr = (const Elf_Ehdr *)imgp->image_header;
531 /* We support four types of branding -- (1) the ELF EI_OSABI field
532 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
533 * branding within the ELF header, (3) path of the `interp_path' field,
534 * and (4) the ".note.ABI-tag" ELF section.
537 /* Look for an ".note.ABI-tag" ELF section */
538 for (i = 0; i < MAX_BRANDS; i++) {
539 bi = elf_brand_list[i];
543 if (hdr->e_machine == bi->machine && (bi->flags &
544 (BI_BRAND_NOTE|BI_BRAND_NOTE_MANDATORY)) != 0) {
545 ret = __elfN(check_note)(imgp, bi->brand_note, osrel);
551 /* If the executable has a brand, search for it in the brand list. */
552 for (i = 0; i < MAX_BRANDS; i++) {
553 bi = elf_brand_list[i];
555 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
557 if (hdr->e_machine == bi->machine &&
558 (hdr->e_ident[EI_OSABI] == bi->brand ||
559 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
560 bi->compat_3_brand, strlen(bi->compat_3_brand)) == 0))
564 /* Lacking a known brand, search for a recognized interpreter. */
565 if (interp != NULL) {
566 for (i = 0; i < MAX_BRANDS; i++) {
567 bi = elf_brand_list[i];
569 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
571 if (hdr->e_machine == bi->machine &&
572 strcmp(interp, bi->interp_path) == 0)
577 /* Lacking a recognized interpreter, try the default brand */
578 for (i = 0; i < MAX_BRANDS; i++) {
579 bi = elf_brand_list[i];
581 if (bi == NULL || bi->flags & BI_BRAND_NOTE_MANDATORY)
583 if (hdr->e_machine == bi->machine &&
584 __elfN(fallback_brand) == bi->brand)
591 __CONCAT(exec_,__elfN(imgact))(struct image_params *imgp)
593 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
594 const Elf_Phdr *phdr;
595 Elf_Auxargs *elf_auxargs;
596 struct vmspace *vmspace;
598 u_long text_size = 0, data_size = 0, total_size = 0;
599 u_long text_addr = 0, data_addr = 0;
600 u_long seg_size, seg_addr;
601 u_long addr, baddr, et_dyn_addr, entry = 0, proghdr = 0;
606 const char *newinterp = NULL;
607 Elf_Brandinfo *brand_info;
611 * Do we have a valid ELF header ?
613 * Only allow ET_EXEC & ET_DYN here, reject ET_DYN later if a particular
614 * brand doesn't support it. Both DragonFly platforms do by default.
616 if (__elfN(check_header)(hdr) != 0 ||
617 (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN))
621 * From here on down, we return an errno, not -1, as we've
622 * detected an ELF file.
625 if ((hdr->e_phoff > PAGE_SIZE) ||
626 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
627 /* Only support headers in first page for now */
630 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
631 if (!aligned(phdr, Elf_Addr))
635 for (i = 0; i < hdr->e_phnum; i++) {
636 if (phdr[i].p_type == PT_LOAD) {
638 baddr = phdr[i].p_vaddr;
642 if (phdr[i].p_type == PT_INTERP) {
644 * If interp is already defined there are more than
645 * one PT_INTERP program headers present. Take only
646 * the first one and ignore the rest.
651 if (phdr[i].p_filesz == 0 ||
652 phdr[i].p_filesz > PAGE_SIZE ||
653 phdr[i].p_filesz > MAXPATHLEN)
656 interp = kmalloc(phdr[i].p_filesz, M_TEMP, M_WAITOK);
657 failure = extract_interpreter(imgp, &phdr[i], interp);
659 kfree(interp, M_TEMP);
666 brand_info = __elfN(get_brandinfo)(imgp, interp, &osrel);
667 if (brand_info == NULL) {
668 uprintf("ELF binary type \"%u\" not known.\n",
669 hdr->e_ident[EI_OSABI]);
671 kfree(interp, M_TEMP);
674 if (hdr->e_type == ET_DYN) {
675 if ((brand_info->flags & BI_CAN_EXEC_DYN) == 0) {
677 kfree(interp, M_TEMP);
681 * Honour the base load address from the dso if it is
682 * non-zero for some reason.
685 et_dyn_addr = ET_DYN_LOAD_ADDR;
691 if (interp != NULL && brand_info->interp_newpath != NULL)
692 newinterp = brand_info->interp_newpath;
694 exec_new_vmspace(imgp, NULL);
697 * Yeah, I'm paranoid. There is every reason in the world to get
698 * VTEXT now since from here on out, there are places we can have
699 * a context switch. Better safe than sorry; I really don't want
700 * the file to change while it's being loaded.
702 vsetflags(imgp->vp, VTEXT);
704 vmspace = imgp->proc->p_vmspace;
706 for (i = 0; i < hdr->e_phnum; i++) {
707 switch (phdr[i].p_type) {
709 case PT_LOAD: /* Loadable segment */
710 if (phdr[i].p_memsz == 0)
713 if (phdr[i].p_flags & PF_X)
714 prot |= VM_PROT_EXECUTE;
715 if (phdr[i].p_flags & PF_W)
716 prot |= VM_PROT_WRITE;
717 if (phdr[i].p_flags & PF_R)
718 prot |= VM_PROT_READ;
720 if ((error = __elfN(load_section)(
725 (caddr_t)phdr[i].p_vaddr + et_dyn_addr,
730 kfree (interp, M_TEMP);
735 * If this segment contains the program headers,
736 * remember their virtual address for the AT_PHDR
737 * aux entry. Static binaries don't usually include
740 if (phdr[i].p_offset == 0 &&
741 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
743 proghdr = phdr[i].p_vaddr + hdr->e_phoff +
746 seg_addr = trunc_page(phdr[i].p_vaddr + et_dyn_addr);
747 seg_size = round_page(phdr[i].p_memsz +
748 phdr[i].p_vaddr + et_dyn_addr - seg_addr);
751 * Is this .text or .data? We can't use
752 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
753 * alpha terribly and possibly does other bad
754 * things so we stick to the old way of figuring
755 * it out: If the segment contains the program
756 * entry point, it's a text segment, otherwise it
759 * Note that obreak() assumes that data_addr +
760 * data_size == end of data load area, and the ELF
761 * file format expects segments to be sorted by
762 * address. If multiple data segments exist, the
763 * last one will be used.
765 if (hdr->e_entry >= phdr[i].p_vaddr &&
766 hdr->e_entry < (phdr[i].p_vaddr +
768 text_size = seg_size;
769 text_addr = seg_addr;
770 entry = (u_long)hdr->e_entry + et_dyn_addr;
772 data_size = seg_size;
773 data_addr = seg_addr;
775 total_size += seg_size;
778 * Check limits. It should be safe to check the
779 * limits after loading the segment since we do
780 * not actually fault in all the segment's pages.
783 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
784 text_size > maxtsiz ||
786 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
788 kfree(interp, M_TEMP);
793 case PT_PHDR: /* Program header table info */
794 proghdr = phdr[i].p_vaddr + et_dyn_addr;
801 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
802 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
803 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
804 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
806 addr = ELF_RTLD_ADDR(vmspace);
808 imgp->entry_addr = entry;
810 imgp->proc->p_sysent = brand_info->sysvec;
811 EVENTHANDLER_INVOKE(process_exec, imgp);
813 if (interp != NULL) {
814 int have_interp = FALSE;
815 if (brand_info->emul_path != NULL &&
816 brand_info->emul_path[0] != '\0') {
817 path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
818 ksnprintf(path, MAXPATHLEN, "%s%s",
819 brand_info->emul_path, interp);
820 error = __elfN(load_file)(imgp->proc, path, &addr,
826 if (!have_interp && newinterp != NULL) {
827 error = __elfN(load_file)(imgp->proc, newinterp,
828 &addr, &imgp->entry_addr);
833 error = __elfN(load_file)(imgp->proc, interp, &addr,
837 uprintf("ELF interpreter %s not found\n", interp);
838 kfree(interp, M_TEMP);
841 kfree(interp, M_TEMP);
846 * Construct auxargs table (used by the fixup routine)
848 elf_auxargs = kmalloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
849 elf_auxargs->execfd = -1;
850 elf_auxargs->phdr = proghdr;
851 elf_auxargs->phent = hdr->e_phentsize;
852 elf_auxargs->phnum = hdr->e_phnum;
853 elf_auxargs->pagesz = PAGE_SIZE;
854 elf_auxargs->base = addr;
855 elf_auxargs->flags = 0;
856 elf_auxargs->entry = entry;
858 imgp->auxargs = elf_auxargs;
859 imgp->interpreted = 0;
860 imgp->proc->p_osrel = osrel;
866 __elfN(dragonfly_fixup)(register_t **stack_base, struct image_params *imgp)
868 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
872 base = (Elf_Addr *)*stack_base;
873 pos = base + (imgp->args->argc + imgp->args->envc + 2);
875 if (args->execfd != -1)
876 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
877 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
878 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
879 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
880 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
881 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
882 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
883 AUXARGS_ENTRY(pos, AT_BASE, args->base);
884 if (imgp->execpathp != 0)
885 AUXARGS_ENTRY(pos, AT_EXECPATH, imgp->execpathp);
886 AUXARGS_ENTRY(pos, AT_OSRELDATE, osreldate);
887 AUXARGS_ENTRY(pos, AT_NULL, 0);
889 kfree(imgp->auxargs, M_TEMP);
890 imgp->auxargs = NULL;
893 suword(base, (long)imgp->args->argc);
894 *stack_base = (register_t *)base;
899 * Code for generating ELF core dumps.
902 typedef int (*segment_callback)(vm_map_entry_t, void *);
904 /* Closure for cb_put_phdr(). */
905 struct phdr_closure {
906 Elf_Phdr *phdr; /* Program header to fill in (incremented) */
907 Elf_Phdr *phdr_max; /* Pointer bound for error check */
908 Elf_Off offset; /* Offset of segment in core file */
911 /* Closure for cb_size_segment(). */
912 struct sseg_closure {
913 int count; /* Count of writable segments. */
914 size_t vsize; /* Total size of all writable segments. */
917 /* Closure for cb_put_fp(). */
920 struct vn_hdr *vnh_max;
925 typedef struct elf_buf {
931 static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
933 static int cb_put_phdr (vm_map_entry_t, void *);
934 static int cb_size_segment (vm_map_entry_t, void *);
935 static int cb_fpcount_segment(vm_map_entry_t, void *);
936 static int cb_put_fp(vm_map_entry_t, void *);
939 static int each_segment (struct proc *, segment_callback, void *, int);
940 static int __elfN(corehdr)(struct lwp *, int, struct file *, struct ucred *,
942 enum putmode { WRITE, DRYRUN };
943 static int __elfN(puthdr)(struct lwp *, elf_buf_t, int sig, enum putmode,
945 static int elf_putallnotes(struct lwp *, elf_buf_t, int, enum putmode);
946 static int __elfN(putnote)(elf_buf_t, const char *, int, const void *, size_t);
948 static int elf_putsigs(struct lwp *, elf_buf_t);
949 static int elf_puttextvp(struct proc *, elf_buf_t);
950 static int elf_putfiles(struct proc *, elf_buf_t, struct file *);
953 __elfN(coredump)(struct lwp *lp, int sig, struct vnode *vp, off_t limit)
958 if ((error = falloc(NULL, &fp, NULL)) != 0)
960 fsetcred(fp, lp->lwp_proc->p_ucred);
965 fp->f_type = DTYPE_VNODE;
966 fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
967 fp->f_ops = &vnode_fileops;
971 error = generic_elf_coredump(lp, sig, fp, limit);
975 fp->f_ops = &badfileops;
982 generic_elf_coredump(struct lwp *lp, int sig, struct file *fp, off_t limit)
984 struct proc *p = lp->lwp_proc;
985 struct ucred *cred = p->p_ucred;
987 struct sseg_closure seginfo;
988 struct elf_buf target;
991 kprintf("can't dump core - null fp\n");
994 * Size the program segments
998 each_segment(p, cb_size_segment, &seginfo, 1);
1001 * Calculate the size of the core file header area by making
1002 * a dry run of generating it. Nothing is written, but the
1003 * size is calculated.
1005 bzero(&target, sizeof(target));
1006 __elfN(puthdr)(lp, &target, sig, DRYRUN, seginfo.count, fp);
1008 if (target.off + seginfo.vsize >= limit)
1012 * Allocate memory for building the header, fill it up,
1015 target.off_max = target.off;
1017 target.buf = kmalloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
1019 error = __elfN(corehdr)(lp, sig, fp, cred, seginfo.count, &target);
1021 /* Write the contents of all of the writable segments. */
1027 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
1028 for (i = 0; i < seginfo.count; i++) {
1029 error = fp_write(fp, (caddr_t)php->p_vaddr,
1030 php->p_filesz, &nbytes, UIO_USERSPACE);
1036 kfree(target.buf, M_TEMP);
1042 * A callback for each_segment() to write out the segment's
1043 * program header entry.
1046 cb_put_phdr(vm_map_entry_t entry, void *closure)
1048 struct phdr_closure *phc = closure;
1049 Elf_Phdr *phdr = phc->phdr;
1051 if (phc->phdr == phc->phdr_max)
1054 phc->offset = round_page(phc->offset);
1056 phdr->p_type = PT_LOAD;
1057 phdr->p_offset = phc->offset;
1058 phdr->p_vaddr = entry->start;
1060 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1061 phdr->p_align = PAGE_SIZE;
1063 if (entry->protection & VM_PROT_READ)
1064 phdr->p_flags |= PF_R;
1065 if (entry->protection & VM_PROT_WRITE)
1066 phdr->p_flags |= PF_W;
1067 if (entry->protection & VM_PROT_EXECUTE)
1068 phdr->p_flags |= PF_X;
1070 phc->offset += phdr->p_filesz;
1076 * A callback for each_writable_segment() to gather information about
1077 * the number of segments and their total size.
1080 cb_size_segment(vm_map_entry_t entry, void *closure)
1082 struct sseg_closure *ssc = closure;
1085 ssc->vsize += entry->end - entry->start;
1090 * A callback for each_segment() to gather information about
1091 * the number of text segments.
1094 cb_fpcount_segment(vm_map_entry_t entry, void *closure)
1096 int *count = closure;
1099 if (entry->object.vm_object->type == OBJT_VNODE) {
1100 vp = (struct vnode *)entry->object.vm_object->handle;
1101 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1109 cb_put_fp(vm_map_entry_t entry, void *closure)
1111 struct fp_closure *fpc = closure;
1112 struct vn_hdr *vnh = fpc->vnh;
1113 Elf_Phdr *phdr = &vnh->vnh_phdr;
1118 * If an entry represents a vnode then write out a file handle.
1120 * If we are checkpointing a checkpoint-restored program we do
1121 * NOT record the filehandle for the old checkpoint vnode (which
1122 * is mapped all over the place). Instead we rely on the fact
1123 * that a checkpoint-restored program does not mmap() the checkpt
1124 * vnode NOCORE, so its contents will be written out to the
1125 * new checkpoint file. This is necessary because the 'old'
1126 * checkpoint file is typically destroyed when a new one is created
1127 * and thus cannot be used to restore the new checkpoint.
1129 * Theoretically we could create a chain of checkpoint files and
1130 * operate the checkpointing operation kinda like an incremental
1131 * checkpoint, but a checkpoint restore would then likely wind up
1132 * referencing many prior checkpoint files and that is a bit over
1133 * the top for the purpose of the checkpoint API.
1135 if (entry->object.vm_object->type == OBJT_VNODE) {
1136 vp = (struct vnode *)entry->object.vm_object->handle;
1137 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1139 if (vnh == fpc->vnh_max)
1143 vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1144 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
1146 char *freepath, *fullpath;
1148 if (vn_fullpath(curproc, vp, &fullpath, &freepath, 0)) {
1149 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error, vp);
1151 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error, fullpath);
1152 kfree(freepath, M_TEMP);
1157 phdr->p_type = PT_LOAD;
1158 phdr->p_offset = 0; /* not written to core */
1159 phdr->p_vaddr = entry->start;
1161 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1162 phdr->p_align = PAGE_SIZE;
1164 if (entry->protection & VM_PROT_READ)
1165 phdr->p_flags |= PF_R;
1166 if (entry->protection & VM_PROT_WRITE)
1167 phdr->p_flags |= PF_W;
1168 if (entry->protection & VM_PROT_EXECUTE)
1169 phdr->p_flags |= PF_X;
1177 * For each writable segment in the process's memory map, call the given
1178 * function with a pointer to the map entry and some arbitrary
1179 * caller-supplied data.
1182 each_segment(struct proc *p, segment_callback func, void *closure, int writable)
1185 vm_map_t map = &p->p_vmspace->vm_map;
1186 vm_map_entry_t entry;
1188 for (entry = map->header.next; error == 0 && entry != &map->header;
1189 entry = entry->next) {
1195 * Don't dump inaccessible mappings, deal with legacy
1198 * Note that read-only segments related to the elf binary
1199 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1200 * need to arbitrarily ignore such segments.
1202 if (elf_legacy_coredump) {
1203 if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
1206 if (writable && (entry->protection & VM_PROT_ALL) == 0)
1211 * Dont include memory segment in the coredump if
1212 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1215 * Currently we only dump normal VM object maps. We do
1216 * not dump submaps or virtual page tables.
1218 if (writable && (entry->eflags & MAP_ENTRY_NOCOREDUMP))
1220 if (entry->maptype != VM_MAPTYPE_NORMAL)
1222 if ((obj = entry->object.vm_object) == NULL)
1226 * Find the bottom-most object, leaving the base object
1227 * and the bottom-most object held (but only one hold
1228 * if they happen to be the same).
1230 vm_object_hold(obj);
1233 while (lobj && (tobj = lobj->backing_object) != NULL) {
1234 KKASSERT(tobj != obj);
1235 vm_object_hold(tobj);
1236 if (tobj == lobj->backing_object) {
1238 vm_object_lock_swap();
1239 vm_object_drop(lobj);
1243 vm_object_drop(tobj);
1248 * The callback only applies to default, swap, or vnode
1249 * objects. Other types of objects such as memory-mapped
1250 * devices are ignored.
1252 if (lobj->type == OBJT_DEFAULT || lobj->type == OBJT_SWAP ||
1253 lobj->type == OBJT_VNODE) {
1254 error = (*func)(entry, closure);
1257 vm_object_drop(lobj);
1258 vm_object_drop(obj);
1265 target_reserve(elf_buf_t target, size_t bytes, int *error)
1270 if (target->off + bytes > target->off_max)
1273 res = target->buf + target->off;
1275 target->off += bytes;
1280 * Write the core file header to the file, including padding up to
1281 * the page boundary.
1284 __elfN(corehdr)(struct lwp *lp, int sig, struct file *fp, struct ucred *cred,
1285 int numsegs, elf_buf_t target)
1291 * Fill in the header. The fp is passed so we can detect and flag
1292 * a checkpoint file pointer within the core file itself, because
1293 * it may not be restored from the same file handle.
1295 error = __elfN(puthdr)(lp, target, sig, WRITE, numsegs, fp);
1297 /* Write it to the core file. */
1299 error = fp_write(fp, target->buf, target->off, &nbytes,
1306 __elfN(puthdr)(struct lwp *lp, elf_buf_t target, int sig, enum putmode mode,
1307 int numsegs, struct file *fp)
1309 struct proc *p = lp->lwp_proc;
1317 ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1319 phoff = target->off;
1320 phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1322 noteoff = target->off;
1324 elf_putallnotes(lp, target, sig, mode);
1325 notesz = target->off - noteoff;
1328 * put extra cruft for dumping process state here
1329 * - we really want it be before all the program
1331 * - we just need to update the offset accordingly
1332 * and GDB will be none the wiser.
1335 error = elf_puttextvp(p, target);
1337 error = elf_putsigs(lp, target);
1339 error = elf_putfiles(p, target, fp);
1342 * Align up to a page boundary for the program segments. The
1343 * actual data will be written to the outptu file, not to elf_buf_t,
1344 * so we do not have to do any further bounds checking.
1346 target->off = round_page(target->off);
1347 if (error == 0 && ehdr != NULL) {
1349 * Fill in the ELF header.
1351 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1352 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1353 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1354 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1355 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1356 ehdr->e_ident[EI_DATA] = ELF_DATA;
1357 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1358 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1359 ehdr->e_ident[EI_ABIVERSION] = 0;
1360 ehdr->e_ident[EI_PAD] = 0;
1361 ehdr->e_type = ET_CORE;
1362 ehdr->e_machine = ELF_ARCH;
1363 ehdr->e_version = EV_CURRENT;
1365 ehdr->e_phoff = phoff;
1367 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1368 ehdr->e_phentsize = sizeof(Elf_Phdr);
1369 ehdr->e_phnum = numsegs + 1;
1370 ehdr->e_shentsize = sizeof(Elf_Shdr);
1372 ehdr->e_shstrndx = SHN_UNDEF;
1374 if (error == 0 && phdr != NULL) {
1376 * Fill in the program header entries.
1378 struct phdr_closure phc;
1380 /* The note segement. */
1381 phdr->p_type = PT_NOTE;
1382 phdr->p_offset = noteoff;
1385 phdr->p_filesz = notesz;
1391 /* All the writable segments from the program. */
1393 phc.phdr_max = phdr + numsegs;
1394 phc.offset = target->off;
1395 each_segment(p, cb_put_phdr, &phc, 1);
1401 * Append core dump notes to target ELF buffer or simply update target size
1402 * if dryrun selected.
1405 elf_putallnotes(struct lwp *corelp, elf_buf_t target, int sig,
1408 struct proc *p = corelp->lwp_proc;
1412 prfpregset_t fpregs;
1416 prfpregset_t *fpregs;
1421 * Allocate temporary storage for notes on heap to avoid stack overflow.
1423 if (mode != DRYRUN) {
1424 tmpdata = kmalloc(sizeof(*tmpdata), M_TEMP, M_ZERO | M_WAITOK);
1425 status = &tmpdata->status;
1426 fpregs = &tmpdata->fpregs;
1427 psinfo = &tmpdata->psinfo;
1436 * Append LWP-agnostic note.
1438 if (mode != DRYRUN) {
1439 psinfo->pr_version = PRPSINFO_VERSION;
1440 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1441 strlcpy(psinfo->pr_fname, p->p_comm,
1442 sizeof(psinfo->pr_fname));
1444 * XXX - We don't fill in the command line arguments
1447 strlcpy(psinfo->pr_psargs, p->p_comm,
1448 sizeof(psinfo->pr_psargs));
1451 __elfN(putnote)(target, "CORE", NT_PRPSINFO, psinfo, sizeof *psinfo);
1456 * Append first note for LWP that triggered core so that it is
1457 * the selected one when the debugger starts.
1459 if (mode != DRYRUN) {
1460 status->pr_version = PRSTATUS_VERSION;
1461 status->pr_statussz = sizeof(prstatus_t);
1462 status->pr_gregsetsz = sizeof(gregset_t);
1463 status->pr_fpregsetsz = sizeof(fpregset_t);
1464 status->pr_osreldate = osreldate;
1465 status->pr_cursig = sig;
1467 * XXX GDB needs unique pr_pid for each LWP and does not
1468 * not support pr_pid==0 but lwp_tid can be 0, so hack unique
1471 status->pr_pid = corelp->lwp_tid;
1472 fill_regs(corelp, &status->pr_reg);
1473 fill_fpregs(corelp, fpregs);
1476 __elfN(putnote)(target, "CORE", NT_PRSTATUS, status, sizeof *status);
1480 __elfN(putnote)(target, "CORE", NT_FPREGSET, fpregs, sizeof *fpregs);
1485 * Then append notes for other LWPs.
1487 FOREACH_LWP_IN_PROC(lp, p) {
1490 /* skip lwps being created */
1491 if (lp->lwp_thread == NULL)
1493 if (mode != DRYRUN) {
1494 status->pr_pid = lp->lwp_tid;
1495 fill_regs(lp, &status->pr_reg);
1496 fill_fpregs(lp, fpregs);
1498 error = __elfN(putnote)(target, "CORE", NT_PRSTATUS,
1499 status, sizeof *status);
1502 error = __elfN(putnote)(target, "CORE", NT_FPREGSET,
1503 fpregs, sizeof *fpregs);
1509 if (tmpdata != NULL)
1510 kfree(tmpdata, M_TEMP);
1515 * Generate a note sub-structure.
1517 * NOTE: 4-byte alignment.
1520 __elfN(putnote)(elf_buf_t target, const char *name, int type,
1521 const void *desc, size_t descsz)
1527 note.n_namesz = strlen(name) + 1;
1528 note.n_descsz = descsz;
1530 dst = target_reserve(target, sizeof(note), &error);
1532 bcopy(¬e, dst, sizeof note);
1533 dst = target_reserve(target, note.n_namesz, &error);
1535 bcopy(name, dst, note.n_namesz);
1536 target->off = roundup2(target->off, sizeof(Elf_Word));
1537 dst = target_reserve(target, note.n_descsz, &error);
1539 bcopy(desc, dst, note.n_descsz);
1540 target->off = roundup2(target->off, sizeof(Elf_Word));
1546 elf_putsigs(struct lwp *lp, elf_buf_t target)
1548 /* XXX lwp handle more than one lwp */
1549 struct proc *p = lp->lwp_proc;
1551 struct ckpt_siginfo *csi;
1553 csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1555 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
1556 bcopy(p->p_sigacts, &csi->csi_sigacts, sizeof(*p->p_sigacts));
1557 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
1558 bcopy(&lp->lwp_sigmask, &csi->csi_sigmask,
1560 csi->csi_sigparent = p->p_sigparent;
1566 elf_putfiles(struct proc *p, elf_buf_t target, struct file *ckfp)
1570 struct ckpt_filehdr *cfh = NULL;
1571 struct ckpt_fileinfo *cfi;
1575 * the duplicated loop is gross, but it was the only way
1576 * to eliminate uninitialized variable warnings
1578 cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1580 cfh->cfh_nfiles = 0;
1584 * ignore STDIN/STDERR/STDOUT.
1586 for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
1587 fp = holdfp(p->p_fd, i, -1);
1591 * XXX Only checkpoint vnodes for now.
1593 if (fp->f_type != DTYPE_VNODE) {
1597 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo),
1603 cfi->cfi_index = -1;
1604 cfi->cfi_type = fp->f_type;
1605 cfi->cfi_flags = fp->f_flag;
1606 cfi->cfi_offset = fp->f_offset;
1607 cfi->cfi_ckflags = 0;
1610 cfi->cfi_ckflags |= CKFIF_ISCKPTFD;
1611 /* f_count and f_msgcount should not be saved/restored */
1612 /* XXX save cred info */
1614 switch(fp->f_type) {
1616 vp = (struct vnode *)fp->f_data;
1618 * it looks like a bug in ptrace is marking
1619 * a non-vnode as a vnode - until we find the
1620 * root cause this will at least prevent
1621 * further panics from truss
1623 if (vp == NULL || vp->v_mount == NULL)
1627 cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1628 error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
1639 elf_puttextvp(struct proc *p, elf_buf_t target)
1643 struct fp_closure fpc;
1644 struct ckpt_vminfo *vminfo;
1646 vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1647 if (vminfo != NULL) {
1648 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1649 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1650 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1651 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
1655 vn_count = target_reserve(target, sizeof(int), &error);
1656 if (target->buf != NULL) {
1657 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1658 fpc.vnh_max = fpc.vnh +
1659 (target->off_max - target->off) / sizeof(struct vn_hdr);
1660 error = each_segment(p, cb_put_fp, &fpc, 0);
1662 *vn_count = fpc.count;
1664 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
1666 target->off += fpc.count * sizeof(struct vn_hdr);
1671 * Try to find the appropriate ABI-note section for checknote,
1672 * The entire image is searched if necessary, not only the first page.
1675 __elfN(check_note)(struct image_params *imgp, Elf_Brandnote *checknote,
1678 boolean_t valid_note_found;
1679 const Elf_Phdr *phdr, *pnote;
1680 const Elf_Ehdr *hdr;
1683 valid_note_found = FALSE;
1684 hdr = (const Elf_Ehdr *)imgp->image_header;
1685 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
1687 for (i = 0; i < hdr->e_phnum; i++) {
1688 if (phdr[i].p_type == PT_NOTE) {
1690 valid_note_found = check_PT_NOTE (imgp, checknote,
1692 if (valid_note_found)
1696 return valid_note_found;
1700 check_PT_NOTE(struct image_params *imgp, Elf_Brandnote *checknote,
1701 int32_t *osrel, const Elf_Phdr * pnote)
1703 boolean_t limited_to_first_page;
1704 boolean_t found = FALSE;
1705 const Elf_Note *note, *note0, *note_end;
1706 const char *note_name;
1707 __ElfN(Off) noteloc, firstloc;
1708 __ElfN(Size) notesz, firstlen, endbyte;
1710 struct lwbuf lwb_cache;
1715 notesz = pnote->p_filesz;
1716 noteloc = pnote->p_offset;
1717 endbyte = noteloc + notesz;
1718 limited_to_first_page = noteloc < PAGE_SIZE && endbyte < PAGE_SIZE;
1720 if (limited_to_first_page) {
1721 note = (const Elf_Note *)(imgp->image_header + noteloc);
1722 note_end = (const Elf_Note *)(imgp->image_header + endbyte);
1725 firstloc = noteloc & PAGE_MASK;
1726 firstlen = PAGE_SIZE - firstloc;
1727 if (notesz < sizeof(Elf_Note) || notesz > PAGE_SIZE)
1731 if (exec_map_page(imgp, noteloc >> PAGE_SHIFT, &lwb, &page))
1733 if (firstlen < notesz) { /* crosses page boundary */
1734 data = kmalloc(notesz, M_TEMP, M_WAITOK);
1735 bcopy(page + firstloc, data, firstlen);
1737 exec_unmap_page(lwb);
1739 if (exec_map_page(imgp, (noteloc >> PAGE_SHIFT) + 1,
1741 kfree(data, M_TEMP);
1744 bcopy(page, data + firstlen, notesz - firstlen);
1745 note = note0 = (const Elf_Note *)(data);
1746 note_end = (const Elf_Note *)(data + notesz);
1748 note = note0 = (const Elf_Note *)(page + firstloc);
1749 note_end = (const Elf_Note *)(page + firstloc +
1754 for (n = 0; n < 100 && note >= note0 && note < note_end; n++) {
1755 if (!aligned(note, Elf32_Addr))
1757 note_name = (const char *)(note + 1);
1759 if (note->n_namesz == checknote->hdr.n_namesz
1760 && note->n_descsz == checknote->hdr.n_descsz
1761 && note->n_type == checknote->hdr.n_type
1762 && (strncmp(checknote->vendor, note_name,
1763 checknote->hdr.n_namesz) == 0)) {
1764 /* Fetch osreldata from ABI.note-tag */
1765 if ((checknote->flags & BN_TRANSLATE_OSREL) != 0 &&
1766 checknote->trans_osrel != NULL)
1767 checknote->trans_osrel(note, osrel);
1771 note = (const Elf_Note *)((const char *)(note + 1) +
1772 roundup2(note->n_namesz, sizeof(Elf32_Addr)) +
1773 roundup2(note->n_descsz, sizeof(Elf32_Addr)));
1776 if (!limited_to_first_page) {
1778 kfree(data, M_TEMP);
1779 exec_unmap_page(lwb);
1785 * The interpreter program header may be located beyond the first page, so
1786 * regardless of its location, a copy of the interpreter path is created so
1787 * that it may be safely referenced by the calling function in all case. The
1788 * memory is allocated by calling function, and the copying is done here.
1791 extract_interpreter(struct image_params *imgp, const Elf_Phdr *pinterpreter,
1794 boolean_t limited_to_first_page;
1795 const boolean_t result_success = FALSE;
1796 const boolean_t result_failure = TRUE;
1797 __ElfN(Off) pathloc, firstloc;
1798 __ElfN(Size) pathsz, firstlen, endbyte;
1800 struct lwbuf lwb_cache;
1803 pathsz = pinterpreter->p_filesz;
1804 pathloc = pinterpreter->p_offset;
1805 endbyte = pathloc + pathsz;
1807 limited_to_first_page = pathloc < PAGE_SIZE && endbyte < PAGE_SIZE;
1808 if (limited_to_first_page) {
1809 bcopy(imgp->image_header + pathloc, data, pathsz);
1810 return (result_success);
1813 firstloc = pathloc & PAGE_MASK;
1814 firstlen = PAGE_SIZE - firstloc;
1817 if (exec_map_page(imgp, pathloc >> PAGE_SHIFT, &lwb, &page))
1818 return (result_failure);
1820 if (firstlen < pathsz) { /* crosses page boundary */
1821 bcopy(page + firstloc, data, firstlen);
1823 exec_unmap_page(lwb);
1825 if (exec_map_page(imgp, (pathloc >> PAGE_SHIFT) + 1, &lwb,
1827 return (result_failure);
1828 bcopy(page, data + firstlen, pathsz - firstlen);
1830 bcopy(page + firstloc, data, pathsz);
1832 exec_unmap_page(lwb);
1833 return (result_success);
1837 __elfN(bsd_trans_osrel)(const Elf_Note *note, int32_t *osrel)
1841 p = (uintptr_t)(note + 1);
1842 p += roundup2(note->n_namesz, sizeof(Elf32_Addr));
1843 *osrel = *(const int32_t *)(p);
1849 * Tell kern_execve.c about it, with a little help from the linker.
1851 #if defined(__x86_64__)
1852 static struct execsw elf_execsw = {exec_elf64_imgact, "ELF64"};
1853 EXEC_SET_ORDERED(elf64, elf_execsw, SI_ORDER_FIRST);
1854 #else /* i386 assumed */
1855 static struct execsw elf_execsw = {exec_elf32_imgact, "ELF32"};
1856 EXEC_SET_ORDERED(elf32, elf_execsw, SI_ORDER_FIRST);