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.24 2004/11/18 13:09:30 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>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_param.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_extern.h>
64 #include <machine/elf.h>
65 #include <machine/md_var.h>
66 #include <sys/mount.h>
68 #define OLD_EI_BRAND 8
73 static int elf_check_header (const Elf_Ehdr *hdr);
74 static int elf_freebsd_fixup (register_t **stack_base,
75 struct image_params *imgp);
76 static int elf_load_file (struct proc *p, const char *file, u_long *addr,
78 static int elf_load_section (struct proc *p,
79 struct vmspace *vmspace, struct vnode *vp,
80 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
82 static int exec_elf_imgact (struct image_params *imgp);
84 static int elf_trace = 0;
85 SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
86 static int elf_legacy_coredump = 0;
87 SYSCTL_INT(_debug, OID_AUTO, elf_legacy_coredump, CTLFLAG_RW,
88 &elf_legacy_coredump, 0, "");
90 static struct sysentvec elf_freebsd_sysvec = {
110 static Elf_Brandinfo freebsd_brand_info = {
114 "/usr/libexec/ld-elf.so.1",
117 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
120 NULL, NULL, NULL, NULL
124 elf_insert_brand_entry(Elf_Brandinfo *entry)
128 for (i=1; i<MAX_BRANDS; i++) {
129 if (elf_brand_list[i] == NULL) {
130 elf_brand_list[i] = entry;
140 elf_remove_brand_entry(Elf_Brandinfo *entry)
144 for (i=1; i<MAX_BRANDS; i++) {
145 if (elf_brand_list[i] == entry) {
146 elf_brand_list[i] = NULL;
156 elf_brand_inuse(Elf_Brandinfo *entry)
161 FOREACH_PROC_IN_SYSTEM(p) {
162 if (p->p_sysent == entry->sysvec) {
172 elf_check_header(const Elf_Ehdr *hdr)
175 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
176 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
177 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
178 hdr->e_phentsize != sizeof(Elf_Phdr) ||
179 hdr->e_ehsize != sizeof(Elf_Ehdr) ||
180 hdr->e_version != ELF_TARG_VER)
183 if (!ELF_MACHINE_OK(hdr->e_machine))
190 elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp,
191 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
195 vm_offset_t map_addr;
200 vm_offset_t file_addr;
201 vm_offset_t data_buf = 0;
203 VOP_GETVOBJECT(vp, &object);
207 * It's necessary to fail if the filsz + offset taken from the
208 * header is greater than the actual file pager object's size.
209 * If we were to allow this, then the vm_map_find() below would
210 * walk right off the end of the file object and into the ether.
212 * While I'm here, might as well check for something else that
213 * is invalid: filsz cannot be greater than memsz.
215 if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
217 uprintf("elf_load_section: truncated ELF file\n");
221 map_addr = trunc_page((vm_offset_t)vmaddr);
222 file_addr = trunc_page(offset);
225 * We have two choices. We can either clear the data in the last page
226 * of an oversized mapping, or we can start the anon mapping a page
227 * early and copy the initialized data into that first page. We
228 * choose the second..
231 map_len = trunc_page(offset+filsz) - file_addr;
233 map_len = round_page(offset+filsz) - file_addr;
236 vm_object_reference(object);
238 /* cow flags: don't dump readonly sections in core */
239 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
240 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
242 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
243 vm_map_lock(&vmspace->vm_map);
244 rv = vm_map_insert(&vmspace->vm_map, &count,
246 file_addr, /* file offset */
247 map_addr, /* virtual start */
248 map_addr + map_len,/* virtual end */
252 vm_map_unlock(&vmspace->vm_map);
253 vm_map_entry_release(count);
254 if (rv != KERN_SUCCESS) {
255 vm_object_deallocate(object);
259 /* we can stop now if we've covered it all */
260 if (memsz == filsz) {
267 * We have to get the remaining bit of the file into the first part
268 * of the oversized map segment. This is normally because the .data
269 * segment in the file is extended to provide bss. It's a neat idea
270 * to try and save a page, but it's a pain in the behind to implement.
272 copy_len = (offset + filsz) - trunc_page(offset + filsz);
273 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
274 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
276 /* This had damn well better be true! */
278 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
279 vm_map_lock(&vmspace->vm_map);
280 rv = vm_map_insert(&vmspace->vm_map, &count,
282 map_addr, map_addr + map_len,
283 VM_PROT_ALL, VM_PROT_ALL, 0);
284 vm_map_unlock(&vmspace->vm_map);
285 vm_map_entry_release(count);
286 if (rv != KERN_SUCCESS) {
292 vm_object_reference(object);
293 rv = vm_map_find(exec_map,
295 trunc_page(offset + filsz),
301 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
302 if (rv != KERN_SUCCESS) {
303 vm_object_deallocate(object);
307 /* send the page fragment to user space */
308 error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
309 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
316 * set it to the specified protection
318 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len, prot,
325 * Load the file "file" into memory. It may be either a shared object
328 * The "addr" reference parameter is in/out. On entry, it specifies
329 * the address where a shared object should be loaded. If the file is
330 * an executable, this value is ignored. On exit, "addr" specifies
331 * where the file was actually loaded.
333 * The "entry" reference parameter is out only. On exit, it specifies
334 * the entry point for the loaded file.
337 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
340 struct nlookupdata nd;
342 struct image_params image_params;
344 const Elf_Ehdr *hdr = NULL;
345 const Elf_Phdr *phdr = NULL;
346 struct nlookupdata *nd;
347 struct vmspace *vmspace = p->p_vmspace;
349 struct image_params *imgp;
352 u_long base_addr = 0;
353 int error, i, numsegs;
354 struct thread *td = p->p_thread;
356 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
358 attr = &tempdata->attr;
359 imgp = &tempdata->image_params;
362 * Initialize part of the common data
366 imgp->firstpage = NULL;
367 imgp->image_header = NULL;
370 error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW);
374 error = cache_vget(nd->nl_ncp, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
380 * Check permissions, modes, uid, etc on the file, and "open" it.
382 error = exec_check_permissions(imgp);
384 VOP_UNLOCK(imgp->vp, 0, td);
388 error = exec_map_first_page(imgp);
390 * Also make certain that the interpreter stays the same, so set
391 * its VTEXT flag, too.
394 imgp->vp->v_flag |= VTEXT;
395 VOP_UNLOCK(imgp->vp, 0, td);
399 hdr = (const Elf_Ehdr *)imgp->image_header;
400 if ((error = elf_check_header(hdr)) != 0)
402 if (hdr->e_type == ET_DYN)
404 else if (hdr->e_type == ET_EXEC)
411 /* Only support headers that fit within first page for now
412 * (multiplication of two Elf_Half fields will not overflow) */
413 if ((hdr->e_phoff > PAGE_SIZE) ||
414 (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
419 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
421 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
422 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */
424 if (phdr[i].p_flags & PF_X)
425 prot |= VM_PROT_EXECUTE;
426 if (phdr[i].p_flags & PF_W)
427 prot |= VM_PROT_WRITE;
428 if (phdr[i].p_flags & PF_R)
429 prot |= VM_PROT_READ;
431 error = elf_load_section(
432 p, vmspace, imgp->vp,
434 (caddr_t)phdr[i].p_vaddr +
437 phdr[i].p_filesz, prot);
441 * Establish the base address if this is the
445 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
450 *entry=(unsigned long)hdr->e_entry + rbase;
454 exec_unmap_first_page(imgp);
459 free(tempdata, M_TEMP);
465 * non static, as it can be overridden by start_init()
467 int fallback_elf_brand = -1;
468 SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
469 &fallback_elf_brand, -1,
470 "ELF brand of last resort");
473 exec_elf_imgact(struct image_params *imgp)
475 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
476 const Elf_Phdr *phdr;
477 Elf_Auxargs *elf_auxargs = NULL;
478 struct vmspace *vmspace;
480 u_long text_size = 0, data_size = 0, total_size = 0;
481 u_long text_addr = 0, data_addr = 0;
482 u_long seg_size, seg_addr;
483 u_long addr, entry = 0, proghdr = 0;
485 const char *interp = NULL;
486 Elf_Brandinfo *brand_info;
492 * Do we have a valid ELF header ?
494 if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
498 * From here on down, we return an errno, not -1, as we've
499 * detected an ELF file.
502 if ((hdr->e_phoff > PAGE_SIZE) ||
503 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
504 /* Only support headers in first page for now */
507 phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
510 * From this point on, we may have resources that need to be freed.
513 exec_new_vmspace(imgp, NULL);
516 * Yeah, I'm paranoid. There is every reason in the world to get
517 * VTEXT now since from here on out, there are places we can have
518 * a context switch. Better safe than sorry; I really don't want
519 * the file to change while it's being loaded.
521 vsetflags(imgp->vp, VTEXT);
523 vmspace = imgp->proc->p_vmspace;
525 for (i = 0; i < hdr->e_phnum; i++) {
526 switch(phdr[i].p_type) {
528 case PT_LOAD: /* Loadable segment */
530 if (phdr[i].p_flags & PF_X)
531 prot |= VM_PROT_EXECUTE;
532 if (phdr[i].p_flags & PF_W)
533 prot |= VM_PROT_WRITE;
534 if (phdr[i].p_flags & PF_R)
535 prot |= VM_PROT_READ;
537 if ((error = elf_load_section(imgp->proc,
540 (caddr_t)phdr[i].p_vaddr,
542 phdr[i].p_filesz, prot)) != 0)
545 seg_addr = trunc_page(phdr[i].p_vaddr);
546 seg_size = round_page(phdr[i].p_memsz +
547 phdr[i].p_vaddr - seg_addr);
550 * Is this .text or .data? We can't use
551 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
552 * alpha terribly and possibly does other bad
553 * things so we stick to the old way of figuring
554 * it out: If the segment contains the program
555 * entry point, it's a text segment, otherwise it
558 * Note that obreak() assumes that data_addr +
559 * data_size == end of data load area, and the ELF
560 * file format expects segments to be sorted by
561 * address. If multiple data segments exist, the
562 * last one will be used.
564 if (hdr->e_entry >= phdr[i].p_vaddr &&
565 hdr->e_entry < (phdr[i].p_vaddr +
567 text_size = seg_size;
568 text_addr = seg_addr;
569 entry = (u_long)hdr->e_entry;
571 data_size = seg_size;
572 data_addr = seg_addr;
574 total_size += seg_size;
577 * Check limits. It should be safe to check the
578 * limits after loading the segment since we do
579 * not actually fault in all the segment's pages.
582 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
583 text_size > maxtsiz ||
585 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
590 case PT_INTERP: /* Path to interpreter */
591 if (phdr[i].p_filesz > MAXPATHLEN ||
592 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
596 interp = imgp->image_header + phdr[i].p_offset;
598 case PT_PHDR: /* Program header table info */
599 proghdr = phdr[i].p_vaddr;
606 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
607 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
608 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
609 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
611 addr = ELF_RTLD_ADDR(vmspace);
613 imgp->entry_addr = entry;
617 /* We support three types of branding -- (1) the ELF EI_OSABI field
618 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
619 * branding w/in the ELF header, and (3) path of the `interp_path'
620 * field. We should also look for an ".note.ABI-tag" ELF section now
621 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
624 /* If the executable has a brand, search for it in the brand list. */
625 if (brand_info == NULL) {
626 for (i = 0; i < MAX_BRANDS; i++) {
627 Elf_Brandinfo *bi = elf_brand_list[i];
630 (hdr->e_ident[EI_OSABI] == bi->brand
632 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
633 bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
640 /* Lacking a known brand, search for a recognized interpreter. */
641 if (brand_info == NULL && interp != NULL) {
642 for (i = 0; i < MAX_BRANDS; i++) {
643 Elf_Brandinfo *bi = elf_brand_list[i];
646 strcmp(interp, bi->interp_path) == 0) {
653 /* Lacking a recognized interpreter, try the default brand */
654 if (brand_info == NULL) {
655 for (i = 0; i < MAX_BRANDS; i++) {
656 Elf_Brandinfo *bi = elf_brand_list[i];
658 if (bi != NULL && fallback_elf_brand == bi->brand) {
665 if (brand_info == NULL) {
666 uprintf("ELF binary type \"%u\" not known.\n",
667 hdr->e_ident[EI_OSABI]);
672 imgp->proc->p_sysent = brand_info->sysvec;
673 if (interp != NULL) {
674 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
675 snprintf(path, MAXPATHLEN, "%s%s",
676 brand_info->emul_path, interp);
677 if ((error = elf_load_file(imgp->proc, path, &addr,
678 &imgp->entry_addr)) != 0) {
679 if ((error = elf_load_file(imgp->proc, interp, &addr,
680 &imgp->entry_addr)) != 0) {
681 uprintf("ELF interpreter %s not found\n", path);
690 * Construct auxargs table (used by the fixup routine)
692 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
693 elf_auxargs->execfd = -1;
694 elf_auxargs->phdr = proghdr;
695 elf_auxargs->phent = hdr->e_phentsize;
696 elf_auxargs->phnum = hdr->e_phnum;
697 elf_auxargs->pagesz = PAGE_SIZE;
698 elf_auxargs->base = addr;
699 elf_auxargs->flags = 0;
700 elf_auxargs->entry = entry;
701 elf_auxargs->trace = elf_trace;
703 imgp->auxargs = elf_auxargs;
704 imgp->interpreted = 0;
711 elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
713 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
716 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
719 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
721 if (args->execfd != -1) {
722 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
724 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
725 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
726 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
727 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
728 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
729 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
730 AUXARGS_ENTRY(pos, AT_BASE, args->base);
731 AUXARGS_ENTRY(pos, AT_NULL, 0);
733 free(imgp->auxargs, M_TEMP);
734 imgp->auxargs = NULL;
737 suword(*stack_base, (long) imgp->args->argc);
742 * Code for generating ELF core dumps.
745 typedef int (*segment_callback) (vm_map_entry_t, void *);
747 /* Closure for cb_put_phdr(). */
748 struct phdr_closure {
749 Elf_Phdr *phdr; /* Program header to fill in (incremented) */
750 Elf_Phdr *phdr_max; /* Pointer bound for error check */
751 Elf_Off offset; /* Offset of segment in core file */
754 /* Closure for cb_size_segment(). */
755 struct sseg_closure {
756 int count; /* Count of writable segments. */
757 size_t vsize; /* Total size of all writable segments. */
760 /* Closure for cb_put_fp(). */
763 struct vn_hdr *vnh_max;
768 typedef struct elf_buf {
774 static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
776 static int cb_put_phdr (vm_map_entry_t, void *);
777 static int cb_size_segment (vm_map_entry_t, void *);
778 static int cb_fpcount_segment(vm_map_entry_t, void *);
779 static int cb_put_fp(vm_map_entry_t, void *);
782 static int each_segment (struct proc *, segment_callback, void *, int);
783 static int elf_corehdr (struct proc *, struct file *, struct ucred *,
785 static int elf_puthdr (struct proc *, elf_buf_t, const prstatus_t *,
786 const prfpregset_t *, const prpsinfo_t *, int);
787 static int elf_putnote (elf_buf_t, const char *, int, const void *, size_t);
789 static int elf_putsigs(struct proc *, elf_buf_t);
790 static int elf_puttextvp(struct proc *, elf_buf_t);
791 static int elf_putfiles(struct proc *, elf_buf_t);
793 extern int osreldate;
796 elf_coredump(struct proc *p, struct vnode *vp, off_t limit)
801 if ((error = falloc(NULL, &fp, NULL)) != 0)
803 fsetcred(fp, p->p_ucred);
808 fp->f_data = (caddr_t)vp;
809 fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
810 fp->f_ops = &vnode_fileops;
811 fp->f_type = DTYPE_VNODE;
812 VOP_UNLOCK(vp, 0, p->p_thread);
814 error = generic_elf_coredump(p, fp, limit);
818 fp->f_ops = &badfileops;
820 fdrop(fp, p->p_thread);
825 generic_elf_coredump(struct proc *p, struct file *fp, off_t limit)
827 struct ucred *cred = p->p_ucred;
829 struct sseg_closure seginfo;
830 struct elf_buf target;
833 printf("can't dump core - null fp\n");
836 * Size the program segments
840 each_segment(p, cb_size_segment, &seginfo, 1);
843 * Calculate the size of the core file header area by making
844 * a dry run of generating it. Nothing is written, but the
845 * size is calculated.
847 bzero(&target, sizeof(target));
848 elf_puthdr(p, &target, NULL, NULL, NULL, seginfo.count);
850 if (target.off + seginfo.vsize >= limit)
854 * Allocate memory for building the header, fill it up,
857 target.off_max = target.off;
859 target.buf = malloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
861 if (target.buf == NULL)
863 error = elf_corehdr(p, fp, cred, seginfo.count, &target);
865 /* Write the contents of all of the writable segments. */
871 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
872 for (i = 0; i < seginfo.count; i++) {
873 error = fp_write(fp, (caddr_t)php->p_vaddr,
874 php->p_filesz, &nbytes);
880 free(target.buf, M_TEMP);
886 * A callback for each_segment() to write out the segment's
887 * program header entry.
890 cb_put_phdr(vm_map_entry_t entry, void *closure)
892 struct phdr_closure *phc = closure;
893 Elf_Phdr *phdr = phc->phdr;
895 if (phc->phdr == phc->phdr_max)
898 phc->offset = round_page(phc->offset);
900 phdr->p_type = PT_LOAD;
901 phdr->p_offset = phc->offset;
902 phdr->p_vaddr = entry->start;
904 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
905 phdr->p_align = PAGE_SIZE;
907 if (entry->protection & VM_PROT_READ)
908 phdr->p_flags |= PF_R;
909 if (entry->protection & VM_PROT_WRITE)
910 phdr->p_flags |= PF_W;
911 if (entry->protection & VM_PROT_EXECUTE)
912 phdr->p_flags |= PF_X;
914 phc->offset += phdr->p_filesz;
920 * A callback for each_writable_segment() to gather information about
921 * the number of segments and their total size.
924 cb_size_segment(vm_map_entry_t entry, void *closure)
926 struct sseg_closure *ssc = closure;
929 ssc->vsize += entry->end - entry->start;
934 * A callback for each_segment() to gather information about
935 * the number of text segments.
938 cb_fpcount_segment(vm_map_entry_t entry, void *closure)
940 int *count = closure;
943 if (entry->object.vm_object->type == OBJT_VNODE) {
944 vp = (struct vnode *)entry->object.vm_object->handle;
945 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
953 cb_put_fp(vm_map_entry_t entry, void *closure)
955 struct fp_closure *fpc = closure;
956 struct vn_hdr *vnh = fpc->vnh;
957 Elf_Phdr *phdr = &vnh->vnh_phdr;
962 * If an entry represents a vnode then write out a file handle.
964 * If we are checkpointing a checkpoint-restored program we do
965 * NOT record the filehandle for the old checkpoint vnode (which
966 * is mapped all over the place). Instead we rely on the fact
967 * that a checkpoint-restored program does not mmap() the checkpt
968 * vnode NOCORE, so its contents will be written out to the
969 * checkpoint file itself. This is necessary because the 'old'
970 * checkpoint file is typically destroyed when a new one is created.
972 if (entry->object.vm_object->type == OBJT_VNODE) {
973 vp = (struct vnode *)entry->object.vm_object->handle;
974 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
976 if (vnh == fpc->vnh_max)
980 vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
981 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
985 phdr->p_type = PT_LOAD;
986 phdr->p_offset = 0; /* not written to core */
987 phdr->p_vaddr = entry->start;
989 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
990 phdr->p_align = PAGE_SIZE;
992 if (entry->protection & VM_PROT_READ)
993 phdr->p_flags |= PF_R;
994 if (entry->protection & VM_PROT_WRITE)
995 phdr->p_flags |= PF_W;
996 if (entry->protection & VM_PROT_EXECUTE)
997 phdr->p_flags |= PF_X;
1005 * For each writable segment in the process's memory map, call the given
1006 * function with a pointer to the map entry and some arbitrary
1007 * caller-supplied data.
1010 each_segment(struct proc *p, segment_callback func, void *closure, int writable)
1013 vm_map_t map = &p->p_vmspace->vm_map;
1014 vm_map_entry_t entry;
1016 for (entry = map->header.next; error == 0 && entry != &map->header;
1017 entry = entry->next) {
1021 * Don't dump inaccessible mappings, deal with legacy
1024 * Note that read-only segments related to the elf binary
1025 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1026 * need to arbitrarily ignore such segments.
1028 if (elf_legacy_coredump) {
1029 if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
1032 if (writable && (entry->protection & VM_PROT_ALL) == 0)
1037 * Dont include memory segment in the coredump if
1038 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1039 * madvise(2). Do not dump submaps (i.e. parts of the
1042 if (writable && entry->eflags & (MAP_ENTRY_NOCOREDUMP|MAP_ENTRY_IS_SUB_MAP))
1045 if ((obj = entry->object.vm_object) == NULL)
1048 /* Find the deepest backing object. */
1049 while (obj->backing_object != NULL)
1050 obj = obj->backing_object;
1052 /* Ignore memory-mapped devices and such things. */
1053 if (obj->type != OBJT_DEFAULT &&
1054 obj->type != OBJT_SWAP &&
1055 obj->type != OBJT_VNODE)
1058 error = (*func)(entry, closure);
1065 target_reserve(elf_buf_t target, size_t bytes, int *error)
1070 if (target->off + bytes > target->off_max)
1073 res = target->buf + target->off;
1075 target->off += bytes;
1080 * Write the core file header to the file, including padding up to
1081 * the page boundary.
1084 elf_corehdr(struct proc *p, struct file *fp, struct ucred *cred, int numsegs,
1089 prfpregset_t fpregset;
1094 prfpregset_t *fpregset;
1097 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO | M_WAITOK);
1098 status = &tempdata->status;
1099 fpregset = &tempdata->fpregset;
1100 psinfo = &tempdata->psinfo;
1102 /* Gather the information for the header. */
1103 status->pr_version = PRSTATUS_VERSION;
1104 status->pr_statussz = sizeof(prstatus_t);
1105 status->pr_gregsetsz = sizeof(gregset_t);
1106 status->pr_fpregsetsz = sizeof(fpregset_t);
1107 status->pr_osreldate = osreldate;
1108 status->pr_cursig = p->p_sig;
1109 status->pr_pid = p->p_pid;
1110 fill_regs(p, &status->pr_reg);
1112 fill_fpregs(p, fpregset);
1114 psinfo->pr_version = PRPSINFO_VERSION;
1115 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1116 strncpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname) - 1);
1118 /* XXX - We don't fill in the command line arguments properly yet. */
1119 strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
1121 /* Fill in the header. */
1122 error = elf_puthdr(p, target, status, fpregset, psinfo, numsegs);
1124 free(tempdata, M_TEMP);
1126 /* Write it to the core file. */
1128 error = fp_write(fp, target->buf, target->off, &nbytes);
1133 elf_puthdr(struct proc *p, elf_buf_t target, const prstatus_t *status,
1134 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
1143 ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1145 phoff = target->off;
1146 phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1148 noteoff = target->off;
1150 error = elf_putnote(target, "FreeBSD", NT_PRSTATUS,
1151 status, sizeof *status);
1154 error = elf_putnote(target, "FreeBSD", NT_FPREGSET,
1155 fpregset, sizeof *fpregset);
1158 error = elf_putnote(target, "FreeBSD", NT_PRPSINFO,
1159 psinfo, sizeof *psinfo);
1161 notesz = target->off - noteoff;
1164 * put extra cruft for dumping process state here
1165 * - we really want it be before all the program
1167 * - we just need to update the offset accordingly
1168 * and GDB will be none the wiser.
1171 error = elf_puttextvp(p, target);
1173 error = elf_putsigs(p, target);
1175 error = elf_putfiles(p, target);
1178 * Align up to a page boundary for the program segments. The
1179 * actual data will be written to the outptu file, not to elf_buf_t,
1180 * so we do not have to do any further bounds checking.
1182 target->off = round_page(target->off);
1183 if (error == 0 && ehdr != NULL) {
1185 * Fill in the ELF header.
1187 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1188 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1189 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1190 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1191 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1192 ehdr->e_ident[EI_DATA] = ELF_DATA;
1193 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1194 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1195 ehdr->e_ident[EI_ABIVERSION] = 0;
1196 ehdr->e_ident[EI_PAD] = 0;
1197 ehdr->e_type = ET_CORE;
1198 ehdr->e_machine = ELF_ARCH;
1199 ehdr->e_version = EV_CURRENT;
1201 ehdr->e_phoff = phoff;
1203 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1204 ehdr->e_phentsize = sizeof(Elf_Phdr);
1205 ehdr->e_phnum = numsegs + 1;
1206 ehdr->e_shentsize = sizeof(Elf_Shdr);
1208 ehdr->e_shstrndx = SHN_UNDEF;
1210 if (error == 0 && phdr != NULL) {
1212 * Fill in the program header entries.
1214 struct phdr_closure phc;
1216 /* The note segement. */
1217 phdr->p_type = PT_NOTE;
1218 phdr->p_offset = noteoff;
1221 phdr->p_filesz = notesz;
1227 /* All the writable segments from the program. */
1229 phc.phdr_max = phdr + numsegs;
1230 phc.offset = target->off;
1231 each_segment(p, cb_put_phdr, &phc, 1);
1237 elf_putnote(elf_buf_t target, const char *name, int type,
1238 const void *desc, size_t descsz)
1244 note.n_namesz = strlen(name) + 1;
1245 note.n_descsz = descsz;
1247 dst = target_reserve(target, sizeof(note), &error);
1249 bcopy(¬e, dst, sizeof note);
1250 dst = target_reserve(target, note.n_namesz, &error);
1252 bcopy(name, dst, note.n_namesz);
1253 target->off = roundup2(target->off, sizeof(Elf_Size));
1254 dst = target_reserve(target, note.n_descsz, &error);
1256 bcopy(desc, dst, note.n_descsz);
1257 target->off = roundup2(target->off, sizeof(Elf_Size));
1263 elf_putsigs(struct proc *p, elf_buf_t target)
1266 struct ckpt_siginfo *csi;
1268 csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1270 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
1271 bcopy(p->p_procsig, &csi->csi_procsig, sizeof(struct procsig));
1272 bcopy(p->p_procsig->ps_sigacts, &csi->csi_sigacts, sizeof(struct sigacts));
1273 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
1274 csi->csi_sigparent = p->p_sigparent;
1280 elf_putfiles(struct proc *p, elf_buf_t target)
1284 struct ckpt_filehdr *cfh = NULL;
1285 struct ckpt_fileinfo *cfi;
1289 * the duplicated loop is gross, but it was the only way
1290 * to eliminate uninitialized variable warnings
1292 cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1294 cfh->cfh_nfiles = 0;
1298 * ignore STDIN/STDERR/STDOUT
1300 for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
1301 if ((fp = p->p_fd->fd_ofiles[i]) == NULL)
1303 if (fp->f_type != DTYPE_VNODE)
1305 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo), &error);
1307 cfi->cfi_index = -1;
1308 vp = (struct vnode *)fp->f_data;
1310 * it looks like a bug in ptrace is marking
1311 * a non-vnode as a vnode - until we find the
1312 * root cause this will at least prevent
1313 * further panics from truss
1315 if (vp == NULL || vp->v_mount == NULL)
1319 cfi->cfi_flags = fp->f_flag;
1320 cfi->cfi_offset = fp->f_offset;
1321 cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1322 error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
1329 elf_puttextvp(struct proc *p, elf_buf_t target)
1333 struct fp_closure fpc;
1334 struct ckpt_vminfo *vminfo;
1336 vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1337 if (vminfo != NULL) {
1338 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1339 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1340 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1341 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
1345 vn_count = target_reserve(target, sizeof(int), &error);
1346 if (target->buf != NULL) {
1347 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1348 fpc.vnh_max = fpc.vnh +
1349 (target->off_max - target->off) / sizeof(struct vn_hdr);
1350 error = each_segment(p, cb_put_fp, &fpc, 0);
1352 *vn_count = fpc.count;
1354 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
1356 target->off += fpc.count * sizeof(struct vn_hdr);
1362 * Tell kern_execve.c about it, with a little help from the linker.
1364 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1365 EXEC_SET_ORDERED(elf, elf_execsw, SI_ORDER_FIRST);