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 withough 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.
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>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
43 #include <sys/namei.h>
44 #include <sys/pioctl.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/systm.h>
49 #include <sys/signalvar.h>
52 #include <sys/syscall.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/vnode.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_param.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
65 #include <machine/elf.h>
66 #include <machine/md_var.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, "");
87 struct sysentvec elf_freebsd_sysvec = {
107 static Elf_Brandinfo freebsd_brand_info = {
111 "/usr/libexec/ld-elf.so.1",
114 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
117 NULL, NULL, NULL, NULL
121 elf_insert_brand_entry(Elf_Brandinfo *entry)
125 for (i=1; i<MAX_BRANDS; i++) {
126 if (elf_brand_list[i] == NULL) {
127 elf_brand_list[i] = entry;
137 elf_remove_brand_entry(Elf_Brandinfo *entry)
141 for (i=1; i<MAX_BRANDS; i++) {
142 if (elf_brand_list[i] == entry) {
143 elf_brand_list[i] = NULL;
153 elf_brand_inuse(Elf_Brandinfo *entry)
158 sx_slock(&allproc_lock);
159 LIST_FOREACH(p, &allproc, p_list) {
160 if (p->p_sysent == entry->sysvec) {
165 sx_sunlock(&allproc_lock);
171 elf_check_header(const Elf_Ehdr *hdr)
174 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
175 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
176 hdr->e_ident[EI_VERSION] != EV_CURRENT)
179 if (!ELF_MACHINE_OK(hdr->e_machine))
182 if (hdr->e_version != ELF_TARG_VER)
189 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)
192 vm_offset_t map_addr;
196 vm_offset_t file_addr;
197 vm_offset_t data_buf = 0;
201 VOP_GETVOBJECT(vp, &object);
205 * It's necessary to fail if the filsz + offset taken from the
206 * header is greater than the actual file pager object's size.
207 * If we were to allow this, then the vm_map_find() below would
208 * walk right off the end of the file object and into the ether.
210 * While I'm here, might as well check for something else that
211 * is invalid: filsz cannot be greater than memsz.
213 if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
215 uprintf("elf_load_section: truncated ELF file\n");
219 map_addr = trunc_page((vm_offset_t)vmaddr);
220 file_addr = trunc_page(offset);
223 * We have two choices. We can either clear the data in the last page
224 * of an oversized mapping, or we can start the anon mapping a page
225 * early and copy the initialized data into that first page. We
226 * choose the second..
229 map_len = trunc_page(offset+filsz) - file_addr;
231 map_len = round_page(offset+filsz) - file_addr;
234 vm_object_reference(object);
235 vm_map_lock(&vmspace->vm_map);
236 rv = vm_map_insert(&vmspace->vm_map,
238 file_addr, /* file offset */
239 map_addr, /* virtual start */
240 map_addr + map_len,/* virtual end */
243 MAP_COPY_ON_WRITE | MAP_PREFAULT);
244 vm_map_unlock(&vmspace->vm_map);
245 if (rv != KERN_SUCCESS) {
246 vm_object_deallocate(object);
250 /* we can stop now if we've covered it all */
251 if (memsz == filsz) {
258 * We have to get the remaining bit of the file into the first part
259 * of the oversized map segment. This is normally because the .data
260 * segment in the file is extended to provide bss. It's a neat idea
261 * to try and save a page, but it's a pain in the behind to implement.
263 copy_len = (offset + filsz) - trunc_page(offset + filsz);
264 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
265 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
267 /* This had damn well better be true! */
269 vm_map_lock(&vmspace->vm_map);
270 rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
271 map_addr, map_addr + map_len,
272 VM_PROT_ALL, VM_PROT_ALL, 0);
273 vm_map_unlock(&vmspace->vm_map);
274 if (rv != KERN_SUCCESS) {
280 vm_object_reference(object);
281 rv = vm_map_find(exec_map,
283 trunc_page(offset + filsz),
289 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
290 if (rv != KERN_SUCCESS) {
291 vm_object_deallocate(object);
295 /* send the page fragment to user space */
296 error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
297 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
304 * set it to the specified protection
306 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len, prot,
313 * Load the file "file" into memory. It may be either a shared object
316 * The "addr" reference parameter is in/out. On entry, it specifies
317 * the address where a shared object should be loaded. If the file is
318 * an executable, this value is ignored. On exit, "addr" specifies
319 * where the file was actually loaded.
321 * The "entry" reference parameter is out only. On exit, it specifies
322 * the entry point for the loaded file.
325 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
330 struct image_params image_params;
332 const Elf_Ehdr *hdr = NULL;
333 const Elf_Phdr *phdr = NULL;
334 struct nameidata *nd;
335 struct vmspace *vmspace = p->p_vmspace;
337 struct image_params *imgp;
340 u_long base_addr = 0;
341 int error, i, numsegs;
343 if (curthread->td_proc != p)
344 panic("elf_load_file - thread"); /* XXXKSE DIAGNOSTIC */
346 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
348 attr = &tempdata->attr;
349 imgp = &tempdata->image_params;
352 * Initialize part of the common data
357 imgp->firstpage = NULL;
358 imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
360 if (imgp->image_header == NULL) {
367 NDINIT(nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, curthread);
369 if ((error = namei(nd)) != 0) {
373 NDFREE(nd, NDF_ONLY_PNBUF);
374 imgp->vp = nd->ni_vp;
377 * Check permissions, modes, uid, etc on the file, and "open" it.
379 error = exec_check_permissions(imgp);
381 VOP_UNLOCK(nd->ni_vp, 0, curthread); /* XXXKSE */
385 error = exec_map_first_page(imgp);
387 * Also make certain that the interpreter stays the same, so set
388 * its VTEXT flag, too.
391 nd->ni_vp->v_flag |= VTEXT;
392 VOP_UNLOCK(nd->ni_vp, 0, curthread); /* XXXKSE */
396 hdr = (const Elf_Ehdr *)imgp->image_header;
397 if ((error = elf_check_header(hdr)) != 0)
399 if (hdr->e_type == ET_DYN)
401 else if (hdr->e_type == ET_EXEC)
408 /* Only support headers that fit within first page for now */
409 if ((hdr->e_phoff > PAGE_SIZE) ||
410 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
415 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
417 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
418 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */
420 if (phdr[i].p_flags & PF_X)
421 prot |= VM_PROT_EXECUTE;
422 if (phdr[i].p_flags & PF_W)
423 prot |= VM_PROT_WRITE;
424 if (phdr[i].p_flags & PF_R)
425 prot |= VM_PROT_READ;
427 if ((error = elf_load_section(p, vmspace, nd->ni_vp,
429 (caddr_t)phdr[i].p_vaddr +
432 phdr[i].p_filesz, prot)) != 0)
435 * Establish the base address if this is the
439 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
444 *entry=(unsigned long)hdr->e_entry + rbase;
448 exec_unmap_first_page(imgp);
449 if (imgp->image_header)
450 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
455 free(tempdata, M_TEMP);
461 * non static, as it can be overridden by start_init()
464 int fallback_elf_brand = ELFOSABI_FREEBSD;
466 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;
481 u_long text_addr = 0, data_addr = 0;
482 u_long addr, entry = 0, proghdr = 0;
484 const char *interp = NULL;
485 Elf_Brandinfo *brand_info;
491 * Do we have a valid ELF header ?
493 if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
497 * From here on down, we return an errno, not -1, as we've
498 * detected an ELF file.
501 if ((hdr->e_phoff > PAGE_SIZE) ||
502 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
503 /* Only support headers in first page for now */
506 phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
509 * From this point on, we may have resources that need to be freed.
513 * Yeah, I'm paranoid. There is every reason in the world to get
514 * VTEXT now since from here on out, there are places we can have
515 * a context switch. Better safe than sorry; I really don't want
516 * the file to change while it's being loaded.
518 mtx_lock(&imgp->vp->v_interlock);
519 imgp->vp->v_flag |= VTEXT;
520 mtx_unlock(&imgp->vp->v_interlock);
522 if ((error = exec_extract_strings(imgp)) != 0)
525 exec_new_vmspace(imgp);
527 vmspace = imgp->proc->p_vmspace;
529 for (i = 0; i < hdr->e_phnum; i++) {
530 switch(phdr[i].p_type) {
532 case PT_LOAD: /* Loadable segment */
534 if (phdr[i].p_flags & PF_X)
535 prot |= VM_PROT_EXECUTE;
536 if (phdr[i].p_flags & PF_W)
537 prot |= VM_PROT_WRITE;
538 if (phdr[i].p_flags & PF_R)
539 prot |= VM_PROT_READ;
541 if ((error = elf_load_section(imgp->proc,
544 (caddr_t)phdr[i].p_vaddr,
546 phdr[i].p_filesz, prot)) != 0)
550 * Is this .text or .data ??
552 * We only handle one each of those yet XXX
554 if (hdr->e_entry >= phdr[i].p_vaddr &&
555 hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) {
556 text_addr = trunc_page(phdr[i].p_vaddr);
557 text_size = round_page(phdr[i].p_memsz +
560 entry = (u_long)hdr->e_entry;
562 data_addr = trunc_page(phdr[i].p_vaddr);
563 data_size = round_page(phdr[i].p_memsz +
568 case PT_INTERP: /* Path to interpreter */
569 if (phdr[i].p_filesz > MAXPATHLEN ||
570 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
574 interp = imgp->image_header + phdr[i].p_offset;
576 case PT_PHDR: /* Program header table info */
577 proghdr = phdr[i].p_vaddr;
584 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
585 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
586 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
587 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
589 addr = ELF_RTLD_ADDR(vmspace);
591 imgp->entry_addr = entry;
595 /* We support three types of branding -- (1) the ELF EI_OSABI field
596 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
597 * branding w/in the ELF header, and (3) path of the `interp_path'
598 * field. We should also look for an ".note.ABI-tag" ELF section now
599 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
602 /* If the executable has a brand, search for it in the brand list. */
603 if (brand_info == NULL) {
604 for (i = 0; i < MAX_BRANDS; i++) {
605 Elf_Brandinfo *bi = elf_brand_list[i];
608 (hdr->e_ident[EI_OSABI] == bi->brand
610 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
611 bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
618 /* Lacking a known brand, search for a recognized interpreter. */
619 if (brand_info == NULL && interp != NULL) {
620 for (i = 0; i < MAX_BRANDS; i++) {
621 Elf_Brandinfo *bi = elf_brand_list[i];
624 strcmp(interp, bi->interp_path) == 0) {
631 /* Lacking a recognized interpreter, try the default brand */
632 if (brand_info == NULL) {
633 for (i = 0; i < MAX_BRANDS; i++) {
634 Elf_Brandinfo *bi = elf_brand_list[i];
636 if (bi != NULL && fallback_elf_brand == bi->brand) {
643 if (brand_info == NULL) {
644 uprintf("ELF binary type \"%u\" not known.\n",
645 hdr->e_ident[EI_OSABI]);
650 imgp->proc->p_sysent = brand_info->sysvec;
651 if (interp != NULL) {
652 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
653 snprintf(path, MAXPATHLEN, "%s%s",
654 brand_info->emul_path, interp);
655 if ((error = elf_load_file(imgp->proc, path, &addr,
656 &imgp->entry_addr)) != 0) {
657 if ((error = elf_load_file(imgp->proc, interp, &addr,
658 &imgp->entry_addr)) != 0) {
659 uprintf("ELF interpreter %s not found\n", path);
668 * Construct auxargs table (used by the fixup routine)
670 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
671 elf_auxargs->execfd = -1;
672 elf_auxargs->phdr = proghdr;
673 elf_auxargs->phent = hdr->e_phentsize;
674 elf_auxargs->phnum = hdr->e_phnum;
675 elf_auxargs->pagesz = PAGE_SIZE;
676 elf_auxargs->base = addr;
677 elf_auxargs->flags = 0;
678 elf_auxargs->entry = entry;
679 elf_auxargs->trace = elf_trace;
681 imgp->auxargs = elf_auxargs;
682 imgp->interpreted = 0;
689 elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
691 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
694 pos = *stack_base + (imgp->argc + imgp->envc + 2);
697 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
699 if (args->execfd != -1) {
700 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
702 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
703 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
704 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
705 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
706 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
707 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
708 AUXARGS_ENTRY(pos, AT_BASE, args->base);
709 AUXARGS_ENTRY(pos, AT_NULL, 0);
711 free(imgp->auxargs, M_TEMP);
712 imgp->auxargs = NULL;
715 suword(*stack_base, (long) imgp->argc);
720 * Code for generating ELF core dumps.
723 typedef void (*segment_callback)(vm_map_entry_t, void *);
725 /* Closure for cb_put_phdr(). */
726 struct phdr_closure {
727 Elf_Phdr *phdr; /* Program header to fill in */
728 Elf_Off offset; /* Offset of segment in core file */
731 /* Closure for cb_size_segment(). */
732 struct sseg_closure {
733 int count; /* Count of writable segments. */
734 size_t size; /* Total size of all writable segments. */
737 static void cb_put_phdr(vm_map_entry_t, void *);
738 static void cb_size_segment(vm_map_entry_t, void *);
739 static void each_writable_segment(struct proc *, segment_callback, void *);
740 static int elf_corehdr(struct thread *, struct vnode *, struct ucred *,
741 int, void *, size_t);
742 static void elf_puthdr(struct proc *, void *, size_t *,
743 const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int);
744 static void elf_putnote(void *, size_t *, const char *, int,
745 const void *, size_t);
747 extern int osreldate;
750 elf_coredump(td, vp, limit)
752 register struct vnode *vp;
755 register struct proc *p = td->td_proc;
756 register struct ucred *cred = td->td_ucred;
758 struct sseg_closure seginfo;
762 /* Size the program segments. */
765 each_writable_segment(p, cb_size_segment, &seginfo);
768 * Calculate the size of the core file header area by making
769 * a dry run of generating it. Nothing is written, but the
770 * size is calculated.
773 elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize,
774 (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
775 (const prpsinfo_t *)NULL, seginfo.count);
777 if (hdrsize + seginfo.size >= limit)
781 * Allocate memory for building the header, fill it up,
784 hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
788 error = elf_corehdr(td, vp, cred, seginfo.count, hdr, hdrsize);
790 /* Write the contents of all of the writable segments. */
796 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
798 for (i = 0; i < seginfo.count; i++) {
799 error = vn_rdwr_inchunks(UIO_WRITE, vp,
800 (caddr_t)php->p_vaddr,
801 php->p_filesz, offset, UIO_USERSPACE,
802 IO_UNIT | IO_DIRECT, cred, (int *)NULL, curthread); /* XXXKSE */
805 offset += php->p_filesz;
815 * A callback for each_writable_segment() to write out the segment's
816 * program header entry.
819 cb_put_phdr(entry, closure)
820 vm_map_entry_t entry;
823 struct phdr_closure *phc = (struct phdr_closure *)closure;
824 Elf_Phdr *phdr = phc->phdr;
826 phc->offset = round_page(phc->offset);
828 phdr->p_type = PT_LOAD;
829 phdr->p_offset = phc->offset;
830 phdr->p_vaddr = entry->start;
832 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
833 phdr->p_align = PAGE_SIZE;
835 if (entry->protection & VM_PROT_READ)
836 phdr->p_flags |= PF_R;
837 if (entry->protection & VM_PROT_WRITE)
838 phdr->p_flags |= PF_W;
839 if (entry->protection & VM_PROT_EXECUTE)
840 phdr->p_flags |= PF_X;
842 phc->offset += phdr->p_filesz;
847 * A callback for each_writable_segment() to gather information about
848 * the number of segments and their total size.
851 cb_size_segment(entry, closure)
852 vm_map_entry_t entry;
855 struct sseg_closure *ssc = (struct sseg_closure *)closure;
858 ssc->size += entry->end - entry->start;
862 * For each writable segment in the process's memory map, call the given
863 * function with a pointer to the map entry and some arbitrary
864 * caller-supplied data.
867 each_writable_segment(p, func, closure)
869 segment_callback func;
872 vm_map_t map = &p->p_vmspace->vm_map;
873 vm_map_entry_t entry;
875 for (entry = map->header.next; entry != &map->header;
876 entry = entry->next) {
879 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
880 (entry->protection & (VM_PROT_READ|VM_PROT_WRITE)) !=
881 (VM_PROT_READ|VM_PROT_WRITE))
885 ** Dont include memory segment in the coredump if
886 ** MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
889 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
892 if ((obj = entry->object.vm_object) == NULL)
895 /* Find the deepest backing object. */
896 while (obj->backing_object != NULL)
897 obj = obj->backing_object;
899 /* Ignore memory-mapped devices and such things. */
900 if (obj->type != OBJT_DEFAULT &&
901 obj->type != OBJT_SWAP &&
902 obj->type != OBJT_VNODE)
905 (*func)(entry, closure);
910 * Write the core file header to the file, including padding up to
914 elf_corehdr(td, vp, cred, numsegs, hdr, hdrsize)
924 prfpregset_t fpregset;
927 struct proc *p = td->td_proc;
930 prfpregset_t *fpregset;
933 tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO | M_WAITOK);
934 status = &tempdata->status;
935 fpregset = &tempdata->fpregset;
936 psinfo = &tempdata->psinfo;
938 /* Gather the information for the header. */
939 status->pr_version = PRSTATUS_VERSION;
940 status->pr_statussz = sizeof(prstatus_t);
941 status->pr_gregsetsz = sizeof(gregset_t);
942 status->pr_fpregsetsz = sizeof(fpregset_t);
943 status->pr_osreldate = osreldate;
944 status->pr_cursig = p->p_sig;
945 status->pr_pid = p->p_pid;
946 fill_regs(td, &status->pr_reg);
948 fill_fpregs(td, fpregset);
950 psinfo->pr_version = PRPSINFO_VERSION;
951 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
952 strncpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname) - 1);
954 /* XXX - We don't fill in the command line arguments properly yet. */
955 strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
957 /* Fill in the header. */
960 elf_puthdr(p, hdr, &off, status, fpregset, psinfo, numsegs);
962 free(tempdata, M_TEMP);
964 /* Write it to the core file. */
965 return vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
966 UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NULL, td); /* XXXKSE */
970 elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status,
971 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
979 *off += sizeof(Elf_Ehdr);
982 *off += (numsegs + 1) * sizeof(Elf_Phdr);
985 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
987 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
989 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
991 notesz = *off - noteoff;
993 /* Align up to a page boundary for the program segments. */
994 *off = round_page(*off);
999 struct phdr_closure phc;
1002 * Fill in the ELF header.
1004 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
1005 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1006 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1007 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1008 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1009 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1010 ehdr->e_ident[EI_DATA] = ELF_DATA;
1011 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1012 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1013 ehdr->e_ident[EI_ABIVERSION] = 0;
1014 ehdr->e_ident[EI_PAD] = 0;
1015 ehdr->e_type = ET_CORE;
1016 ehdr->e_machine = ELF_ARCH;
1017 ehdr->e_version = EV_CURRENT;
1019 ehdr->e_phoff = phoff;
1021 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1022 ehdr->e_phentsize = sizeof(Elf_Phdr);
1023 ehdr->e_phnum = numsegs + 1;
1024 ehdr->e_shentsize = sizeof(Elf_Shdr);
1026 ehdr->e_shstrndx = SHN_UNDEF;
1029 * Fill in the program header entries.
1031 phdr = (Elf_Phdr *)((char *)dst + phoff);
1033 /* The note segement. */
1034 phdr->p_type = PT_NOTE;
1035 phdr->p_offset = noteoff;
1038 phdr->p_filesz = notesz;
1044 /* All the writable segments from the program. */
1047 each_writable_segment(p, cb_put_phdr, &phc);
1052 elf_putnote(void *dst, size_t *off, const char *name, int type,
1053 const void *desc, size_t descsz)
1057 note.n_namesz = strlen(name) + 1;
1058 note.n_descsz = descsz;
1061 bcopy(¬e, (char *)dst + *off, sizeof note);
1062 *off += sizeof note;
1064 bcopy(name, (char *)dst + *off, note.n_namesz);
1065 *off += roundup2(note.n_namesz, sizeof(Elf_Size));
1067 bcopy(desc, (char *)dst + *off, note.n_descsz);
1068 *off += roundup2(note.n_descsz, sizeof(Elf_Size));
1072 * Tell kern_execve.c about it, with a little help from the linker.
1074 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1075 EXEC_SET(elf, elf_execsw);