2 * Copyright (c) 1995-1996 Søren Schmidt
3 * Copyright (c) 1996 Peter Wemm
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software withough specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * $FreeBSD: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 dillon Exp $
30 * $DragonFly: src/sys/kern/imgact_elf.c,v 1.9 2003/09/23 05:03:51 dillon Exp $
33 #include <sys/param.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_elf.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
41 #include <sys/systm.h>
43 #include <sys/namei.h>
44 #include <sys/pioctl.h>
45 #include <sys/procfs.h>
46 #include <sys/resourcevar.h>
47 #include <sys/signalvar.h>
49 #include <sys/syscall.h>
50 #include <sys/sysctl.h>
51 #include <sys/sysent.h>
52 #include <sys/vnode.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_param.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_extern.h>
63 #include <machine/elf.h>
64 #include <machine/md_var.h>
66 #define OLD_EI_BRAND 8
71 static int elf_check_header (const Elf_Ehdr *hdr);
72 static int elf_freebsd_fixup (register_t **stack_base,
73 struct image_params *imgp);
74 static int elf_load_file (struct proc *p, const char *file, u_long *addr,
76 static int elf_load_section (struct proc *p,
77 struct vmspace *vmspace, struct vnode *vp,
78 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
80 static int exec_elf_imgact (struct image_params *imgp);
82 static int elf_trace = 0;
83 SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
84 static int elf_legacy_coredump = 0;
85 SYSCTL_INT(_debug, OID_AUTO, elf_legacy_coredump, CTLFLAG_RW,
86 &elf_legacy_coredump, 0, "");
88 static struct sysentvec elf_freebsd_sysvec = {
108 static Elf_Brandinfo freebsd_brand_info = {
112 "/usr/libexec/ld-elf.so.1",
115 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
118 NULL, NULL, NULL, NULL
122 elf_insert_brand_entry(Elf_Brandinfo *entry)
126 for (i=1; i<MAX_BRANDS; i++) {
127 if (elf_brand_list[i] == NULL) {
128 elf_brand_list[i] = entry;
138 elf_remove_brand_entry(Elf_Brandinfo *entry)
142 for (i=1; i<MAX_BRANDS; i++) {
143 if (elf_brand_list[i] == entry) {
144 elf_brand_list[i] = NULL;
154 elf_brand_inuse(Elf_Brandinfo *entry)
159 FOREACH_PROC_IN_SYSTEM(p) {
160 if (p->p_sysent == entry->sysvec) {
170 elf_check_header(const Elf_Ehdr *hdr)
173 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
174 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
175 hdr->e_ident[EI_VERSION] != EV_CURRENT)
178 if (!ELF_MACHINE_OK(hdr->e_machine))
181 if (hdr->e_version != ELF_TARG_VER)
188 elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
191 vm_offset_t map_addr;
196 vm_offset_t file_addr;
197 vm_offset_t data_buf = 0;
199 VOP_GETVOBJECT(vp, &object);
203 * It's necessary to fail if the filsz + offset taken from the
204 * header is greater than the actual file pager object's size.
205 * If we were to allow this, then the vm_map_find() below would
206 * walk right off the end of the file object and into the ether.
208 * While I'm here, might as well check for something else that
209 * is invalid: filsz cannot be greater than memsz.
211 if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
213 uprintf("elf_load_section: truncated ELF file\n");
217 map_addr = trunc_page((vm_offset_t)vmaddr);
218 file_addr = trunc_page(offset);
221 * We have two choices. We can either clear the data in the last page
222 * of an oversized mapping, or we can start the anon mapping a page
223 * early and copy the initialized data into that first page. We
224 * choose the second..
227 map_len = trunc_page(offset+filsz) - file_addr;
229 map_len = round_page(offset+filsz) - file_addr;
232 vm_object_reference(object);
234 /* cow flags: don't dump readonly sections in core */
235 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
236 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
238 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
239 vm_map_lock(&vmspace->vm_map);
240 rv = vm_map_insert(&vmspace->vm_map, &count,
242 file_addr, /* file offset */
243 map_addr, /* virtual start */
244 map_addr + map_len,/* virtual end */
248 vm_map_unlock(&vmspace->vm_map);
249 vm_map_entry_release(count);
250 if (rv != KERN_SUCCESS) {
251 vm_object_deallocate(object);
255 /* we can stop now if we've covered it all */
256 if (memsz == filsz) {
263 * We have to get the remaining bit of the file into the first part
264 * of the oversized map segment. This is normally because the .data
265 * segment in the file is extended to provide bss. It's a neat idea
266 * to try and save a page, but it's a pain in the behind to implement.
268 copy_len = (offset + filsz) - trunc_page(offset + filsz);
269 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
270 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
272 /* This had damn well better be true! */
274 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
275 vm_map_lock(&vmspace->vm_map);
276 rv = vm_map_insert(&vmspace->vm_map, &count,
278 map_addr, map_addr + map_len,
279 VM_PROT_ALL, VM_PROT_ALL, 0);
280 vm_map_unlock(&vmspace->vm_map);
281 vm_map_entry_release(count);
282 if (rv != KERN_SUCCESS) {
288 vm_object_reference(object);
289 rv = vm_map_find(exec_map,
291 trunc_page(offset + filsz),
297 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
298 if (rv != KERN_SUCCESS) {
299 vm_object_deallocate(object);
303 /* send the page fragment to user space */
304 error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
305 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
312 * set it to the specified protection
314 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len, prot,
321 * Load the file "file" into memory. It may be either a shared object
324 * The "addr" reference parameter is in/out. On entry, it specifies
325 * the address where a shared object should be loaded. If the file is
326 * an executable, this value is ignored. On exit, "addr" specifies
327 * where the file was actually loaded.
329 * The "entry" reference parameter is out only. On exit, it specifies
330 * the entry point for the loaded file.
333 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
338 struct image_params image_params;
340 const Elf_Ehdr *hdr = NULL;
341 const Elf_Phdr *phdr = NULL;
342 struct nameidata *nd;
343 struct vmspace *vmspace = p->p_vmspace;
345 struct image_params *imgp;
348 u_long base_addr = 0;
349 int error, i, numsegs;
350 struct thread *td = p->p_thread;
352 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
354 attr = &tempdata->attr;
355 imgp = &tempdata->image_params;
358 * Initialize part of the common data
363 imgp->firstpage = NULL;
364 imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
366 if (imgp->image_header == NULL) {
372 NDINIT(nd, NAMEI_LOOKUP, CNP_LOCKLEAF | CNP_FOLLOW,
373 UIO_SYSSPACE, file, td);
375 if ((error = namei(nd)) != 0) {
379 NDFREE(nd, NDF_ONLY_PNBUF);
380 imgp->vp = nd->ni_vp;
383 * Check permissions, modes, uid, etc on the file, and "open" it.
385 error = exec_check_permissions(imgp);
387 VOP_UNLOCK(nd->ni_vp, 0, td);
391 error = exec_map_first_page(imgp);
393 * Also make certain that the interpreter stays the same, so set
394 * its VTEXT flag, too.
397 nd->ni_vp->v_flag |= VTEXT;
398 VOP_UNLOCK(nd->ni_vp, 0, td);
402 hdr = (const Elf_Ehdr *)imgp->image_header;
403 if ((error = elf_check_header(hdr)) != 0)
405 if (hdr->e_type == ET_DYN)
407 else if (hdr->e_type == ET_EXEC)
414 /* Only support headers that fit within first page for now */
415 if ((hdr->e_phoff > PAGE_SIZE) ||
416 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
421 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
423 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
424 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */
426 if (phdr[i].p_flags & PF_X)
427 prot |= VM_PROT_EXECUTE;
428 if (phdr[i].p_flags & PF_W)
429 prot |= VM_PROT_WRITE;
430 if (phdr[i].p_flags & PF_R)
431 prot |= VM_PROT_READ;
433 error = elf_load_section(
434 p, vmspace, nd->ni_vp,
436 (caddr_t)phdr[i].p_vaddr +
439 phdr[i].p_filesz, prot);
443 * Establish the base address if this is the
447 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
452 *entry=(unsigned long)hdr->e_entry + rbase;
456 exec_unmap_first_page(imgp);
457 if (imgp->image_header)
458 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
463 free(tempdata, M_TEMP);
469 * non static, as it can be overridden by start_init()
471 int fallback_elf_brand = -1;
472 SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
473 &fallback_elf_brand, -1,
474 "ELF brand of last resort");
477 exec_elf_imgact(struct image_params *imgp)
479 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
480 const Elf_Phdr *phdr;
481 Elf_Auxargs *elf_auxargs = NULL;
482 struct vmspace *vmspace;
484 u_long text_size = 0, data_size = 0, total_size = 0;
485 u_long text_addr = 0, data_addr = 0;
486 u_long seg_size, seg_addr;
487 u_long addr, entry = 0, proghdr = 0;
489 const char *interp = NULL;
490 Elf_Brandinfo *brand_info;
494 * Do we have a valid ELF header ?
496 if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
500 * From here on down, we return an errno, not -1, as we've
501 * detected an ELF file.
504 if ((hdr->e_phoff > PAGE_SIZE) ||
505 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
506 /* Only support headers in first page for now */
509 phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
512 * From this point on, we may have resources that need to be freed.
515 if ((error = exec_extract_strings(imgp)) != 0)
518 exec_new_vmspace(imgp);
521 * Yeah, I'm paranoid. There is every reason in the world to get
522 * VTEXT now since from here on out, there are places we can have
523 * a context switch. Better safe than sorry; I really don't want
524 * the file to change while it's being loaded.
526 lwkt_gettoken(&imgp->vp->v_interlock);
527 imgp->vp->v_flag |= VTEXT;
528 lwkt_reltoken(&imgp->vp->v_interlock);
530 vmspace = imgp->proc->p_vmspace;
532 for (i = 0; i < hdr->e_phnum; i++) {
533 switch(phdr[i].p_type) {
535 case PT_LOAD: /* Loadable segment */
537 if (phdr[i].p_flags & PF_X)
538 prot |= VM_PROT_EXECUTE;
539 if (phdr[i].p_flags & PF_W)
540 prot |= VM_PROT_WRITE;
541 if (phdr[i].p_flags & PF_R)
542 prot |= VM_PROT_READ;
544 if ((error = elf_load_section(imgp->proc,
547 (caddr_t)phdr[i].p_vaddr,
549 phdr[i].p_filesz, prot)) != 0)
552 seg_addr = trunc_page(phdr[i].p_vaddr);
553 seg_size = round_page(phdr[i].p_memsz +
554 phdr[i].p_vaddr - seg_addr);
557 * Is this .text or .data? We can't use
558 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
559 * alpha terribly and possibly does other bad
560 * things so we stick to the old way of figuring
561 * it out: If the segment contains the program
562 * entry point, it's a text segment, otherwise it
565 * Note that obreak() assumes that data_addr +
566 * data_size == end of data load area, and the ELF
567 * file format expects segments to be sorted by
568 * address. If multiple data segments exist, the
569 * last one will be used.
571 if (hdr->e_entry >= phdr[i].p_vaddr &&
572 hdr->e_entry < (phdr[i].p_vaddr +
574 text_size = seg_size;
575 text_addr = seg_addr;
576 entry = (u_long)hdr->e_entry;
578 data_size = seg_size;
579 data_addr = seg_addr;
581 total_size += seg_size;
584 * Check limits. It should be safe to check the
585 * limits after loading the segment since we do
586 * not actually fault in all the segment's pages.
589 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
590 text_size > maxtsiz ||
592 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
597 case PT_INTERP: /* Path to interpreter */
598 if (phdr[i].p_filesz > MAXPATHLEN ||
599 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
603 interp = imgp->image_header + phdr[i].p_offset;
605 case PT_PHDR: /* Program header table info */
606 proghdr = phdr[i].p_vaddr;
613 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
614 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
615 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
616 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
618 addr = ELF_RTLD_ADDR(vmspace);
620 imgp->entry_addr = entry;
624 /* We support three types of branding -- (1) the ELF EI_OSABI field
625 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
626 * branding w/in the ELF header, and (3) path of the `interp_path'
627 * field. We should also look for an ".note.ABI-tag" ELF section now
628 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
631 /* If the executable has a brand, search for it in the brand list. */
632 if (brand_info == NULL) {
633 for (i = 0; i < MAX_BRANDS; i++) {
634 Elf_Brandinfo *bi = elf_brand_list[i];
637 (hdr->e_ident[EI_OSABI] == bi->brand
639 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
640 bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
647 /* Lacking a known brand, search for a recognized interpreter. */
648 if (brand_info == NULL && interp != NULL) {
649 for (i = 0; i < MAX_BRANDS; i++) {
650 Elf_Brandinfo *bi = elf_brand_list[i];
653 strcmp(interp, bi->interp_path) == 0) {
660 /* Lacking a recognized interpreter, try the default brand */
661 if (brand_info == NULL) {
662 for (i = 0; i < MAX_BRANDS; i++) {
663 Elf_Brandinfo *bi = elf_brand_list[i];
665 if (bi != NULL && fallback_elf_brand == bi->brand) {
672 if (brand_info == NULL) {
673 uprintf("ELF binary type \"%u\" not known.\n",
674 hdr->e_ident[EI_OSABI]);
679 imgp->proc->p_sysent = brand_info->sysvec;
680 if (interp != NULL) {
681 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
682 snprintf(path, MAXPATHLEN, "%s%s",
683 brand_info->emul_path, interp);
684 if ((error = elf_load_file(imgp->proc, path, &addr,
685 &imgp->entry_addr)) != 0) {
686 if ((error = elf_load_file(imgp->proc, interp, &addr,
687 &imgp->entry_addr)) != 0) {
688 uprintf("ELF interpreter %s not found\n", path);
697 * Construct auxargs table (used by the fixup routine)
699 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
700 elf_auxargs->execfd = -1;
701 elf_auxargs->phdr = proghdr;
702 elf_auxargs->phent = hdr->e_phentsize;
703 elf_auxargs->phnum = hdr->e_phnum;
704 elf_auxargs->pagesz = PAGE_SIZE;
705 elf_auxargs->base = addr;
706 elf_auxargs->flags = 0;
707 elf_auxargs->entry = entry;
708 elf_auxargs->trace = elf_trace;
710 imgp->auxargs = elf_auxargs;
711 imgp->interpreted = 0;
718 elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
720 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
723 pos = *stack_base + (imgp->argc + imgp->envc + 2);
726 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
728 if (args->execfd != -1) {
729 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
731 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
732 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
733 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
734 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
735 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
736 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
737 AUXARGS_ENTRY(pos, AT_BASE, args->base);
738 AUXARGS_ENTRY(pos, AT_NULL, 0);
740 free(imgp->auxargs, M_TEMP);
741 imgp->auxargs = NULL;
744 suword(*stack_base, (long) imgp->argc);
749 * Code for generating ELF core dumps.
752 typedef void (*segment_callback) (vm_map_entry_t, void *);
754 /* Closure for cb_put_phdr(). */
755 struct phdr_closure {
756 Elf_Phdr *phdr; /* Program header to fill in */
757 Elf_Off offset; /* Offset of segment in core file */
760 /* Closure for cb_size_segment(). */
761 struct sseg_closure {
762 int count; /* Count of writable segments. */
763 size_t size; /* Total size of all writable segments. */
766 static void cb_put_phdr (vm_map_entry_t, void *);
767 static void cb_size_segment (vm_map_entry_t, void *);
768 static void each_writable_segment (struct proc *, segment_callback,
770 static int elf_corehdr (struct proc *, struct vnode *, struct ucred *,
771 int, void *, size_t);
772 static void elf_puthdr (struct proc *, void *, size_t *,
773 const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int);
774 static void elf_putnote (void *, size_t *, const char *, int,
775 const void *, size_t);
777 extern int osreldate;
780 elf_coredump(p, vp, limit)
785 struct ucred *cred = p->p_ucred;
786 struct thread *td = p->p_thread;
788 struct sseg_closure seginfo;
792 /* Size the program segments. */
795 each_writable_segment(p, cb_size_segment, &seginfo);
798 * Calculate the size of the core file header area by making
799 * a dry run of generating it. Nothing is written, but the
800 * size is calculated.
803 elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize,
804 (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
805 (const prpsinfo_t *)NULL, seginfo.count);
807 if (hdrsize + seginfo.size >= limit)
811 * Allocate memory for building the header, fill it up,
814 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
818 error = elf_corehdr(p, vp, cred, seginfo.count, hdr, hdrsize);
820 /* Write the contents of all of the writable segments. */
826 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
828 for (i = 0; i < seginfo.count; i++) {
829 error = vn_rdwr_inchunks(UIO_WRITE, vp,
830 (caddr_t)php->p_vaddr,
831 php->p_filesz, offset, UIO_USERSPACE,
832 IO_UNIT | IO_DIRECT | IO_CORE, cred,
836 offset += php->p_filesz;
846 * A callback for each_writable_segment() to write out the segment's
847 * program header entry.
850 cb_put_phdr(entry, closure)
851 vm_map_entry_t entry;
854 struct phdr_closure *phc = (struct phdr_closure *)closure;
855 Elf_Phdr *phdr = phc->phdr;
857 phc->offset = round_page(phc->offset);
859 phdr->p_type = PT_LOAD;
860 phdr->p_offset = phc->offset;
861 phdr->p_vaddr = entry->start;
863 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
864 phdr->p_align = PAGE_SIZE;
866 if (entry->protection & VM_PROT_READ)
867 phdr->p_flags |= PF_R;
868 if (entry->protection & VM_PROT_WRITE)
869 phdr->p_flags |= PF_W;
870 if (entry->protection & VM_PROT_EXECUTE)
871 phdr->p_flags |= PF_X;
873 phc->offset += phdr->p_filesz;
878 * A callback for each_writable_segment() to gather information about
879 * the number of segments and their total size.
882 cb_size_segment(entry, closure)
883 vm_map_entry_t entry;
886 struct sseg_closure *ssc = (struct sseg_closure *)closure;
889 ssc->size += entry->end - entry->start;
893 * For each writable segment in the process's memory map, call the given
894 * function with a pointer to the map entry and some arbitrary
895 * caller-supplied data.
898 each_writable_segment(p, func, closure)
900 segment_callback func;
903 vm_map_t map = &p->p_vmspace->vm_map;
904 vm_map_entry_t entry;
906 for (entry = map->header.next; entry != &map->header;
907 entry = entry->next) {
911 * Don't dump inaccessible mappings, deal with legacy
914 * Note that read-only segments related to the elf binary
915 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
916 * need to arbitrarily ignore such segments.
918 if (elf_legacy_coredump) {
919 if ((entry->protection & VM_PROT_RW) != VM_PROT_RW)
922 if ((entry->protection & VM_PROT_ALL) == 0)
927 * Dont include memory segment in the coredump if
928 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
929 * madvise(2). Do not dump submaps (i.e. parts of the
932 if (entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
935 if ((obj = entry->object.vm_object) == NULL)
938 /* Find the deepest backing object. */
939 while (obj->backing_object != NULL)
940 obj = obj->backing_object;
942 /* Ignore memory-mapped devices and such things. */
943 if (obj->type != OBJT_DEFAULT &&
944 obj->type != OBJT_SWAP &&
945 obj->type != OBJT_VNODE)
948 (*func)(entry, closure);
953 * Write the core file header to the file, including padding up to
957 elf_corehdr(p, vp, cred, numsegs, hdr, hdrsize)
967 prfpregset_t fpregset;
972 prfpregset_t *fpregset;
974 struct thread *td = p->p_thread;
976 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO | M_WAITOK);
977 status = &tempdata->status;
978 fpregset = &tempdata->fpregset;
979 psinfo = &tempdata->psinfo;
981 /* Gather the information for the header. */
982 status->pr_version = PRSTATUS_VERSION;
983 status->pr_statussz = sizeof(prstatus_t);
984 status->pr_gregsetsz = sizeof(gregset_t);
985 status->pr_fpregsetsz = sizeof(fpregset_t);
986 status->pr_osreldate = osreldate;
987 status->pr_cursig = p->p_sig;
988 status->pr_pid = p->p_pid;
989 fill_regs(p, &status->pr_reg);
991 fill_fpregs(p, fpregset);
993 psinfo->pr_version = PRPSINFO_VERSION;
994 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
995 strncpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname) - 1);
997 /* XXX - We don't fill in the command line arguments properly yet. */
998 strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
1000 /* Fill in the header. */
1001 bzero(hdr, hdrsize);
1003 elf_puthdr(p, hdr, &off, status, fpregset, psinfo, numsegs);
1005 free(tempdata, M_TEMP);
1007 /* Write it to the core file. */
1008 return vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
1009 UIO_SYSSPACE, IO_UNIT | IO_DIRECT | IO_CORE, cred, NULL, td);
1013 elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status,
1014 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
1022 *off += sizeof(Elf_Ehdr);
1025 *off += (numsegs + 1) * sizeof(Elf_Phdr);
1028 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
1030 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
1032 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
1034 notesz = *off - noteoff;
1036 /* Align up to a page boundary for the program segments. */
1037 *off = round_page(*off);
1042 struct phdr_closure phc;
1045 * Fill in the ELF header.
1047 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
1048 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1049 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1050 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1051 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1052 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1053 ehdr->e_ident[EI_DATA] = ELF_DATA;
1054 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1055 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1056 ehdr->e_ident[EI_ABIVERSION] = 0;
1057 ehdr->e_ident[EI_PAD] = 0;
1058 ehdr->e_type = ET_CORE;
1059 ehdr->e_machine = ELF_ARCH;
1060 ehdr->e_version = EV_CURRENT;
1062 ehdr->e_phoff = phoff;
1064 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1065 ehdr->e_phentsize = sizeof(Elf_Phdr);
1066 ehdr->e_phnum = numsegs + 1;
1067 ehdr->e_shentsize = sizeof(Elf_Shdr);
1069 ehdr->e_shstrndx = SHN_UNDEF;
1072 * Fill in the program header entries.
1074 phdr = (Elf_Phdr *)((char *)dst + phoff);
1076 /* The note segement. */
1077 phdr->p_type = PT_NOTE;
1078 phdr->p_offset = noteoff;
1081 phdr->p_filesz = notesz;
1087 /* All the writable segments from the program. */
1090 each_writable_segment(p, cb_put_phdr, &phc);
1095 elf_putnote(void *dst, size_t *off, const char *name, int type,
1096 const void *desc, size_t descsz)
1100 note.n_namesz = strlen(name) + 1;
1101 note.n_descsz = descsz;
1104 bcopy(¬e, (char *)dst + *off, sizeof note);
1105 *off += sizeof note;
1107 bcopy(name, (char *)dst + *off, note.n_namesz);
1108 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
1110 bcopy(desc, (char *)dst + *off, note.n_descsz);
1111 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
1115 * Tell kern_execve.c about it, with a little help from the linker.
1117 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1118 EXEC_SET(elf, elf_execsw);