nlookup - introduce nlookup_init_root
[dragonfly.git] / sys / kern / imgact_elf.c
blob77a3bf40219c6dece29b3e1f7f91ab5c10ba7e35
1 /*-
2 * Copyright (c) 1995-1996 Søren Schmidt
3 * Copyright (c) 1996 Peter Wemm
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.55 2008/08/17 17:21:36 nth Exp $
33 #include <sys/param.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/file.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/mman.h>
42 #include <sys/systm.h>
43 #include <sys/proc.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>
49 #include <sys/stat.h>
50 #include <sys/syscall.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/vnode.h>
54 #include <sys/sfbuf.h>
55 #include <sys/eventhandler.h>
57 #include <vm/vm.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_param.h>
60 #include <vm/pmap.h>
61 #include <sys/lock.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_object.h>
64 #include <vm/vm_extern.h>
66 #include <machine/elf.h>
67 #include <machine/md_var.h>
68 #include <sys/mount.h>
69 #include <sys/ckpt.h>
70 #define OLD_EI_BRAND 8
72 __ElfType(Brandinfo);
73 __ElfType(Auxargs);
75 static int elf_check_header (const Elf_Ehdr *hdr);
76 static int elf_freebsd_fixup (register_t **stack_base,
77 struct image_params *imgp);
78 static int elf_load_file (struct proc *p, const char *file, u_long *addr,
79 u_long *entry);
80 static int elf_load_section (struct proc *p,
81 struct vmspace *vmspace, struct vnode *vp,
82 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
83 vm_prot_t prot);
84 static int exec_elf_imgact (struct image_params *imgp);
86 static int elf_trace = 0;
87 SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
88 static int elf_legacy_coredump = 0;
89 SYSCTL_INT(_debug, OID_AUTO, elf_legacy_coredump, CTLFLAG_RW,
90 &elf_legacy_coredump, 0, "");
92 static int dragonfly_match_abi_note(const Elf_Note *);
93 static int freebsd_match_abi_note(const Elf_Note *);
95 static struct sysentvec elf_freebsd_sysvec = {
96 SYS_MAXSYSCALL,
97 sysent,
98 -1,
104 elf_freebsd_fixup,
105 sendsig,
106 sigcode,
107 &szsigcode,
109 "FreeBSD ELF",
110 elf_coredump,
111 NULL,
112 MINSIGSTKSZ
115 static Elf_Brandinfo freebsd_brand_info = {
116 ELFOSABI_FREEBSD,
117 "FreeBSD",
118 freebsd_match_abi_note,
120 "/usr/libexec/ld-elf.so.1",
121 &elf_freebsd_sysvec
124 static Elf_Brandinfo dragonfly_brand_info = {
125 ELFOSABI_NONE,
126 "DragonFly",
127 dragonfly_match_abi_note,
129 "/usr/libexec/ld-elf.so.2",
130 &elf_freebsd_sysvec
133 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
134 &dragonfly_brand_info,
135 &freebsd_brand_info,
136 NULL, NULL, NULL,
137 NULL, NULL, NULL
140 static int
141 freebsd_match_abi_note(const Elf_Note *abi_note)
143 const char *abi_name = (const char *)
144 ((const uint8_t *)abi_note + sizeof(*abi_note));
146 if (abi_note->n_namesz != sizeof("FreeBSD"))
147 return(FALSE);
148 if (memcmp(abi_name, "FreeBSD", sizeof("FreeBSD")))
149 return(FALSE);
150 return(TRUE);
153 static int
154 dragonfly_match_abi_note(const Elf_Note *abi_note)
156 const char *abi_name = (const char *)
157 ((const uint8_t *)abi_note + sizeof(*abi_note));
159 if (abi_note->n_namesz != sizeof("DragonFly"))
160 return(FALSE);
161 if (memcmp(abi_name, "DragonFly", sizeof("DragonFly")))
162 return(FALSE);
163 return(TRUE);
167 elf_insert_brand_entry(Elf_Brandinfo *entry)
169 int i;
171 for (i=1; i<MAX_BRANDS; i++) {
172 if (elf_brand_list[i] == NULL) {
173 elf_brand_list[i] = entry;
174 break;
177 if (i == MAX_BRANDS)
178 return -1;
179 return 0;
183 elf_remove_brand_entry(Elf_Brandinfo *entry)
185 int i;
187 for (i=1; i<MAX_BRANDS; i++) {
188 if (elf_brand_list[i] == entry) {
189 elf_brand_list[i] = NULL;
190 break;
193 if (i == MAX_BRANDS)
194 return -1;
195 return 0;
199 * Check if an elf brand is being used anywhere in the system.
201 * Used by the linux emulation module unloader. This isn't safe from
202 * races.
204 struct elf_brand_inuse_info {
205 int rval;
206 Elf_Brandinfo *entry;
209 static int elf_brand_inuse_callback(struct proc *p, void *data);
212 elf_brand_inuse(Elf_Brandinfo *entry)
214 struct elf_brand_inuse_info info;
216 info.rval = FALSE;
217 info.entry = entry;
218 allproc_scan(elf_brand_inuse_callback, entry);
219 return (info.rval);
222 static
224 elf_brand_inuse_callback(struct proc *p, void *data)
226 struct elf_brand_inuse_info *info = data;
228 if (p->p_sysent == info->entry->sysvec) {
229 info->rval = TRUE;
230 return(-1);
232 return(0);
235 static int
236 elf_check_header(const Elf_Ehdr *hdr)
238 if (!IS_ELF(*hdr) ||
239 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
240 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
241 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
242 hdr->e_phentsize != sizeof(Elf_Phdr) ||
243 hdr->e_ehsize != sizeof(Elf_Ehdr) ||
244 hdr->e_version != ELF_TARG_VER)
245 return ENOEXEC;
247 if (!ELF_MACHINE_OK(hdr->e_machine))
248 return ENOEXEC;
250 return 0;
253 static Elf_Brandinfo *
254 elf_check_abi_note(struct image_params *imgp, const Elf_Phdr *ph)
256 Elf_Brandinfo *match = NULL;
257 const Elf_Note *tmp_note;
258 struct sf_buf *sfb;
259 const char *page;
260 char *data = NULL;
261 Elf_Off off;
262 size_t firstoff;
263 size_t len;
264 size_t firstlen;
266 len = ph->p_filesz;
267 off = ph->p_offset;
269 firstoff = off & PAGE_MASK;
270 firstlen = PAGE_SIZE - firstoff;
272 if (len < sizeof(Elf_Note) || len > PAGE_SIZE)
273 return NULL; /* ENOEXEC? */
275 if (exec_map_page(imgp, off >> PAGE_SHIFT, &sfb, &page))
276 return NULL;
279 * Crosses page boundary? Is that allowed?
281 if (firstlen < len) {
282 data = kmalloc(len, M_TEMP, M_WAITOK);
284 bcopy(page + firstoff, data, firstlen);
286 exec_unmap_page(sfb);
287 if (exec_map_page(imgp, (off >> PAGE_SHIFT) + 1, &sfb, &page)) {
288 kfree(data, M_TEMP);
289 return NULL;
291 bcopy(page, data + firstlen, len - firstlen);
292 tmp_note = (void *)data;
293 } else {
294 tmp_note = (const void *)(page + firstoff);
297 while (len >= sizeof(Elf_Note)) {
298 int i;
299 size_t nlen = roundup(tmp_note->n_namesz, sizeof(Elf_Word)) +
300 roundup(tmp_note->n_descsz, sizeof(Elf_Word)) +
301 sizeof(Elf_Note);
303 if (nlen > len)
304 break;
306 if (tmp_note->n_type != 1)
307 goto next;
309 for (i = 0; i < MAX_BRANDS; i++) {
310 Elf_Brandinfo *bi = elf_brand_list[i];
312 if (bi != NULL && bi->match_abi_note != NULL &&
313 bi->match_abi_note(tmp_note)) {
314 match = bi;
315 break;
319 if (match != NULL)
320 break;
322 next:
323 len -= nlen;
324 tmp_note += nlen;
327 if (data != NULL)
328 kfree(data, M_TEMP);
329 exec_unmap_page(sfb);
331 return (match);
334 static int
335 elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp,
336 vm_offset_t offset, caddr_t vmaddr, size_t memsz,
337 size_t filsz, vm_prot_t prot)
339 size_t map_len;
340 vm_offset_t map_addr;
341 int error, rv, cow;
342 int count;
343 size_t copy_len;
344 vm_object_t object;
345 vm_offset_t file_addr;
347 object = vp->v_object;
348 error = 0;
351 * It's necessary to fail if the filsz + offset taken from the
352 * header is greater than the actual file pager object's size.
353 * If we were to allow this, then the vm_map_find() below would
354 * walk right off the end of the file object and into the ether.
356 * While I'm here, might as well check for something else that
357 * is invalid: filsz cannot be greater than memsz.
359 if ((off_t)filsz + offset > vp->v_filesize || filsz > memsz) {
360 uprintf("elf_load_section: truncated ELF file\n");
361 return (ENOEXEC);
364 map_addr = trunc_page((vm_offset_t)vmaddr);
365 file_addr = trunc_page(offset);
368 * We have two choices. We can either clear the data in the last page
369 * of an oversized mapping, or we can start the anon mapping a page
370 * early and copy the initialized data into that first page. We
371 * choose the second..
373 if (memsz > filsz)
374 map_len = trunc_page(offset+filsz) - file_addr;
375 else
376 map_len = round_page(offset+filsz) - file_addr;
378 if (map_len != 0) {
379 vm_object_reference(object);
381 /* cow flags: don't dump readonly sections in core */
382 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
383 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
385 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
386 vm_map_lock(&vmspace->vm_map);
387 rv = vm_map_insert(&vmspace->vm_map, &count,
388 object,
389 file_addr, /* file offset */
390 map_addr, /* virtual start */
391 map_addr + map_len,/* virtual end */
392 VM_MAPTYPE_NORMAL,
393 prot, VM_PROT_ALL,
394 cow);
395 vm_map_unlock(&vmspace->vm_map);
396 vm_map_entry_release(count);
397 if (rv != KERN_SUCCESS) {
398 vm_object_deallocate(object);
399 return EINVAL;
402 /* we can stop now if we've covered it all */
403 if (memsz == filsz) {
404 return 0;
410 * We have to get the remaining bit of the file into the first part
411 * of the oversized map segment. This is normally because the .data
412 * segment in the file is extended to provide bss. It's a neat idea
413 * to try and save a page, but it's a pain in the behind to implement.
415 copy_len = (offset + filsz) - trunc_page(offset + filsz);
416 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
417 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
419 /* This had damn well better be true! */
420 if (map_len != 0) {
421 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
422 vm_map_lock(&vmspace->vm_map);
423 rv = vm_map_insert(&vmspace->vm_map, &count,
424 NULL, 0,
425 map_addr, map_addr + map_len,
426 VM_MAPTYPE_NORMAL,
427 VM_PROT_ALL, VM_PROT_ALL,
429 vm_map_unlock(&vmspace->vm_map);
430 vm_map_entry_release(count);
431 if (rv != KERN_SUCCESS) {
432 return EINVAL;
436 if (copy_len != 0) {
437 vm_page_t m;
438 struct sf_buf *sf;
440 m = vm_fault_object_page(object, trunc_page(offset + filsz),
441 VM_PROT_READ, 0, &error);
442 if (m) {
443 sf = sf_buf_alloc(m, SFB_CPUPRIVATE);
444 error = copyout((caddr_t)sf_buf_kva(sf),
445 (caddr_t)map_addr, copy_len);
446 sf_buf_free(sf);
447 vm_page_unhold(m);
449 if (error) {
450 return (error);
455 * set it to the specified protection
457 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len, prot,
458 FALSE);
460 return error;
464 * Load the file "file" into memory. It may be either a shared object
465 * or an executable.
467 * The "addr" reference parameter is in/out. On entry, it specifies
468 * the address where a shared object should be loaded. If the file is
469 * an executable, this value is ignored. On exit, "addr" specifies
470 * where the file was actually loaded.
472 * The "entry" reference parameter is out only. On exit, it specifies
473 * the entry point for the loaded file.
475 static int
476 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
478 struct {
479 struct nlookupdata nd;
480 struct vattr attr;
481 struct image_params image_params;
482 } *tempdata;
483 const Elf_Ehdr *hdr = NULL;
484 const Elf_Phdr *phdr = NULL;
485 struct nlookupdata *nd;
486 struct vmspace *vmspace = p->p_vmspace;
487 struct vattr *attr;
488 struct image_params *imgp;
489 vm_prot_t prot;
490 u_long rbase;
491 u_long base_addr = 0;
492 int error, i, numsegs;
494 tempdata = kmalloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
495 nd = &tempdata->nd;
496 attr = &tempdata->attr;
497 imgp = &tempdata->image_params;
500 * Initialize part of the common data
502 imgp->proc = p;
503 imgp->attr = attr;
504 imgp->firstpage = NULL;
505 imgp->image_header = NULL;
506 imgp->vp = NULL;
508 error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW);
509 if (error == 0)
510 error = nlookup(nd);
511 if (error == 0)
512 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
513 nlookup_done(nd);
514 if (error)
515 goto fail;
518 * Check permissions, modes, uid, etc on the file, and "open" it.
520 error = exec_check_permissions(imgp);
521 if (error) {
522 vn_unlock(imgp->vp);
523 goto fail;
526 error = exec_map_first_page(imgp);
528 * Also make certain that the interpreter stays the same, so set
529 * its VTEXT flag, too.
531 if (error == 0)
532 vsetflags(imgp->vp, VTEXT);
533 vn_unlock(imgp->vp);
534 if (error)
535 goto fail;
537 hdr = (const Elf_Ehdr *)imgp->image_header;
538 if ((error = elf_check_header(hdr)) != 0)
539 goto fail;
540 if (hdr->e_type == ET_DYN)
541 rbase = *addr;
542 else if (hdr->e_type == ET_EXEC)
543 rbase = 0;
544 else {
545 error = ENOEXEC;
546 goto fail;
549 /* Only support headers that fit within first page for now
550 * (multiplication of two Elf_Half fields will not overflow) */
551 if ((hdr->e_phoff > PAGE_SIZE) ||
552 (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
553 error = ENOEXEC;
554 goto fail;
557 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
559 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
560 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */
561 prot = 0;
562 if (phdr[i].p_flags & PF_X)
563 prot |= VM_PROT_EXECUTE;
564 if (phdr[i].p_flags & PF_W)
565 prot |= VM_PROT_WRITE;
566 if (phdr[i].p_flags & PF_R)
567 prot |= VM_PROT_READ;
569 error = elf_load_section(
570 p, vmspace, imgp->vp,
571 phdr[i].p_offset,
572 (caddr_t)phdr[i].p_vaddr +
573 rbase,
574 phdr[i].p_memsz,
575 phdr[i].p_filesz, prot);
576 if (error != 0)
577 goto fail;
579 * Establish the base address if this is the
580 * first segment.
582 if (numsegs == 0)
583 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
584 numsegs++;
587 *addr = base_addr;
588 *entry=(unsigned long)hdr->e_entry + rbase;
590 fail:
591 if (imgp->firstpage)
592 exec_unmap_first_page(imgp);
593 if (imgp->vp) {
594 vrele(imgp->vp);
595 imgp->vp = NULL;
597 kfree(tempdata, M_TEMP);
599 return error;
603 * non static, as it can be overridden by start_init()
605 int fallback_elf_brand = -1;
606 SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
607 &fallback_elf_brand, -1,
608 "ELF brand of last resort");
610 static int can_exec_dyn = 1;
611 SYSCTL_INT(_kern, OID_AUTO, elf_exec_dyn, CTLFLAG_RW,
612 &can_exec_dyn, 1,
613 "ELF: can exec shared libraries");
615 static int
616 exec_elf_imgact(struct image_params *imgp)
618 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
619 const Elf_Phdr *phdr;
620 Elf_Auxargs *elf_auxargs = NULL;
621 struct vmspace *vmspace;
622 vm_prot_t prot;
623 u_long text_size = 0, data_size = 0, total_size = 0;
624 u_long text_addr = 0, data_addr = 0;
625 u_long seg_size, seg_addr;
626 u_long addr, entry = 0, proghdr = 0;
627 int error, i;
628 const char *interp = NULL;
629 const Elf_Note *abi_note = NULL;
630 Elf_Brandinfo *brand_info = NULL;
631 char *path;
633 error = 0;
636 * Do we have a valid ELF header ?
637 * We allow execution of ET_EXEC and, if kern.elf_exec_dyn is 1, ET_DYN.
639 if (elf_check_header(hdr) != 0 ||
640 (hdr->e_type != ET_EXEC && (!can_exec_dyn || hdr->e_type != ET_DYN)))
641 return -1;
644 * From here on down, we return an errno, not -1, as we've
645 * detected an ELF file.
648 if ((hdr->e_phoff > PAGE_SIZE) ||
649 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
650 /* Only support headers in first page for now */
651 return ENOEXEC;
653 phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
656 * From this point on, we may have resources that need to be freed.
659 exec_new_vmspace(imgp, NULL);
662 * Yeah, I'm paranoid. There is every reason in the world to get
663 * VTEXT now since from here on out, there are places we can have
664 * a context switch. Better safe than sorry; I really don't want
665 * the file to change while it's being loaded.
667 vsetflags(imgp->vp, VTEXT);
669 vmspace = imgp->proc->p_vmspace;
671 for (i = 0; i < hdr->e_phnum; i++) {
672 switch(phdr[i].p_type) {
674 case PT_LOAD: /* Loadable segment */
675 prot = 0;
676 if (phdr[i].p_flags & PF_X)
677 prot |= VM_PROT_EXECUTE;
678 if (phdr[i].p_flags & PF_W)
679 prot |= VM_PROT_WRITE;
680 if (phdr[i].p_flags & PF_R)
681 prot |= VM_PROT_READ;
683 if ((error = elf_load_section(imgp->proc,
684 vmspace, imgp->vp,
685 phdr[i].p_offset,
686 (caddr_t)phdr[i].p_vaddr,
687 phdr[i].p_memsz,
688 phdr[i].p_filesz, prot)) != 0)
689 goto fail;
692 * If this segment contains the program headers,
693 * remember their virtual address for the AT_PHDR
694 * aux entry. Static binaries don't usually include
695 * a PT_PHDR entry.
697 if (phdr[i].p_offset == 0 &&
698 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
699 <= phdr[i].p_filesz)
700 proghdr = phdr[i].p_vaddr + hdr->e_phoff;
702 seg_addr = trunc_page(phdr[i].p_vaddr);
703 seg_size = round_page(phdr[i].p_memsz +
704 phdr[i].p_vaddr - seg_addr);
707 * Is this .text or .data? We can't use
708 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
709 * alpha terribly and possibly does other bad
710 * things so we stick to the old way of figuring
711 * it out: If the segment contains the program
712 * entry point, it's a text segment, otherwise it
713 * is a data segment.
715 * Note that obreak() assumes that data_addr +
716 * data_size == end of data load area, and the ELF
717 * file format expects segments to be sorted by
718 * address. If multiple data segments exist, the
719 * last one will be used.
721 if (hdr->e_entry >= phdr[i].p_vaddr &&
722 hdr->e_entry < (phdr[i].p_vaddr +
723 phdr[i].p_memsz)) {
724 text_size = seg_size;
725 text_addr = seg_addr;
726 entry = (u_long)hdr->e_entry;
727 } else {
728 data_size = seg_size;
729 data_addr = seg_addr;
731 total_size += seg_size;
734 * Check limits. It should be safe to check the
735 * limits after loading the segment since we do
736 * not actually fault in all the segment's pages.
738 if (data_size >
739 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
740 text_size > maxtsiz ||
741 total_size >
742 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
743 error = ENOMEM;
744 goto fail;
746 break;
747 case PT_INTERP: /* Path to interpreter */
748 if (phdr[i].p_filesz > MAXPATHLEN ||
749 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
750 error = ENOEXEC;
751 goto fail;
753 interp = imgp->image_header + phdr[i].p_offset;
754 break;
755 case PT_NOTE: /* Check for .note.ABI-tag */
756 if (brand_info == NULL)
757 brand_info = elf_check_abi_note(imgp, &phdr[i]);
758 break;
759 case PT_PHDR: /* Program header table info */
760 proghdr = phdr[i].p_vaddr;
761 break;
762 default:
763 break;
767 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
768 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
769 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
770 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
772 addr = ELF_RTLD_ADDR(vmspace);
774 imgp->entry_addr = entry;
776 /* We support three types of branding -- (1) the ELF EI_OSABI field
777 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
778 * branding w/in the ELF header, and (3) path of the `interp_path'
779 * field. We should also look for an ".note.ABI-tag" ELF section now
780 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
783 /* If the executable has a brand, search for it in the brand list. */
784 if (brand_info == NULL && hdr->e_ident[EI_OSABI] != ELFOSABI_NONE) {
785 for (i = 0; i < MAX_BRANDS; i++) {
786 Elf_Brandinfo *bi = elf_brand_list[i];
788 if (bi != NULL &&
789 (hdr->e_ident[EI_OSABI] == bi->brand
790 || 0 ==
791 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
792 bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
793 brand_info = bi;
794 break;
799 /* Search for a recognized ABI. */
800 if (brand_info == NULL && abi_note != NULL) {
804 * ELFOSABI_NONE == ELFOSABI_SYSV, so a SYSV binary misses all
805 * checks so far, since it is neither branded nor does it have
806 * an ABI note. If the EI_OSABI field is ELFOSABI_NONE, assume
807 * it is svr4 and look for an entry in the elf_brand_list with
808 * match_abi_note == NULL.
810 if (brand_info == NULL && hdr->e_ident[EI_OSABI] == ELFOSABI_NONE) {
811 for (i = 0; i < MAX_BRANDS; i++) {
812 Elf_Brandinfo *bi = elf_brand_list[i];
814 if (bi != NULL && bi->match_abi_note == NULL &&
815 ELFOSABI_SYSV == bi->brand) {
816 brand_info = bi;
817 break;
822 /* Lacking a recognized ABI, search for a recognized interpreter. */
823 if (brand_info == NULL && interp != NULL) {
824 for (i = 0; i < MAX_BRANDS; i++) {
825 Elf_Brandinfo *bi = elf_brand_list[i];
827 if (bi != NULL &&
828 strcmp(interp, bi->interp_path) == 0) {
829 brand_info = bi;
830 break;
835 /* Lacking a recognized interpreter, try the default brand */
836 if (brand_info == NULL) {
837 for (i = 0; i < MAX_BRANDS; i++) {
838 Elf_Brandinfo *bi = elf_brand_list[i];
840 if (bi != NULL && fallback_elf_brand == bi->brand) {
841 brand_info = bi;
842 break;
847 if (brand_info == NULL) {
848 uprintf("ELF binary type \"%u\" not known.\n",
849 hdr->e_ident[EI_OSABI]);
850 error = ENOEXEC;
851 goto fail;
854 imgp->proc->p_sysent = brand_info->sysvec;
855 EVENTHANDLER_INVOKE(process_exec, imgp);
857 if (interp != NULL) {
858 path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
859 ksnprintf(path, MAXPATHLEN, "%s%s",
860 brand_info->emul_path, interp);
861 if ((error = elf_load_file(imgp->proc, path, &addr,
862 &imgp->entry_addr)) != 0) {
863 if ((error = elf_load_file(imgp->proc, interp, &addr,
864 &imgp->entry_addr)) != 0) {
865 uprintf("ELF interpreter %s not found\n", path);
866 kfree(path, M_TEMP);
867 goto fail;
870 kfree(path, M_TEMP);
871 } else {
872 addr = 0;
876 * Construct auxargs table (used by the fixup routine)
878 elf_auxargs = kmalloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
879 elf_auxargs->execfd = -1;
880 elf_auxargs->phdr = proghdr;
881 elf_auxargs->phent = hdr->e_phentsize;
882 elf_auxargs->phnum = hdr->e_phnum;
883 elf_auxargs->pagesz = PAGE_SIZE;
884 elf_auxargs->base = addr;
885 elf_auxargs->flags = 0;
886 elf_auxargs->entry = entry;
887 elf_auxargs->trace = elf_trace;
889 imgp->auxargs = elf_auxargs;
890 imgp->interpreted = 0;
892 fail:
893 return error;
896 static int
897 elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
899 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
900 register_t *pos;
902 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
904 if (args->trace) {
905 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
907 if (args->execfd != -1) {
908 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
910 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
911 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
912 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
913 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
914 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
915 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
916 AUXARGS_ENTRY(pos, AT_BASE, args->base);
917 AUXARGS_ENTRY(pos, AT_NULL, 0);
919 kfree(imgp->auxargs, M_TEMP);
920 imgp->auxargs = NULL;
922 (*stack_base)--;
923 suword(*stack_base, (long) imgp->args->argc);
924 return 0;
928 * Code for generating ELF core dumps.
931 typedef int (*segment_callback) (vm_map_entry_t, void *);
933 /* Closure for cb_put_phdr(). */
934 struct phdr_closure {
935 Elf_Phdr *phdr; /* Program header to fill in (incremented) */
936 Elf_Phdr *phdr_max; /* Pointer bound for error check */
937 Elf_Off offset; /* Offset of segment in core file */
940 /* Closure for cb_size_segment(). */
941 struct sseg_closure {
942 int count; /* Count of writable segments. */
943 size_t vsize; /* Total size of all writable segments. */
946 /* Closure for cb_put_fp(). */
947 struct fp_closure {
948 struct vn_hdr *vnh;
949 struct vn_hdr *vnh_max;
950 int count;
951 struct stat *sb;
954 typedef struct elf_buf {
955 char *buf;
956 size_t off;
957 size_t off_max;
958 } *elf_buf_t;
960 static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
962 static int cb_put_phdr (vm_map_entry_t, void *);
963 static int cb_size_segment (vm_map_entry_t, void *);
964 static int cb_fpcount_segment(vm_map_entry_t, void *);
965 static int cb_put_fp(vm_map_entry_t, void *);
968 static int each_segment (struct proc *, segment_callback, void *, int);
969 static int elf_corehdr (struct lwp *, int, struct file *, struct ucred *,
970 int, elf_buf_t);
971 enum putmode { WRITE, DRYRUN };
972 static int elf_puthdr (struct lwp *, elf_buf_t, int sig, enum putmode,
973 int, struct file *);
974 static int elf_putallnotes(struct lwp *, elf_buf_t, int, enum putmode);
975 static int elf_putnote (elf_buf_t, const char *, int, const void *, size_t);
977 static int elf_putsigs(struct lwp *, elf_buf_t);
978 static int elf_puttextvp(struct proc *, elf_buf_t);
979 static int elf_putfiles(struct proc *, elf_buf_t, struct file *);
981 extern int osreldate;
984 elf_coredump(struct lwp *lp, int sig, struct vnode *vp, off_t limit)
986 struct file *fp;
987 int error;
989 if ((error = falloc(NULL, &fp, NULL)) != 0)
990 return (error);
991 fsetcred(fp, lp->lwp_proc->p_ucred);
994 * XXX fixme.
996 fp->f_type = DTYPE_VNODE;
997 fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
998 fp->f_ops = &vnode_fileops;
999 fp->f_data = vp;
1000 vn_unlock(vp);
1002 error = generic_elf_coredump(lp, sig, fp, limit);
1004 fp->f_type = 0;
1005 fp->f_flag = 0;
1006 fp->f_ops = &badfileops;
1007 fp->f_data = NULL;
1008 fdrop(fp);
1009 return (error);
1013 generic_elf_coredump(struct lwp *lp, int sig, struct file *fp, off_t limit)
1015 struct proc *p = lp->lwp_proc;
1016 struct ucred *cred = p->p_ucred;
1017 int error = 0;
1018 struct sseg_closure seginfo;
1019 struct elf_buf target;
1021 if (!fp)
1022 kprintf("can't dump core - null fp\n");
1025 * Size the program segments
1027 seginfo.count = 0;
1028 seginfo.vsize = 0;
1029 each_segment(p, cb_size_segment, &seginfo, 1);
1032 * Calculate the size of the core file header area by making
1033 * a dry run of generating it. Nothing is written, but the
1034 * size is calculated.
1036 bzero(&target, sizeof(target));
1037 elf_puthdr(lp, &target, sig, DRYRUN, seginfo.count, fp);
1039 if (target.off + seginfo.vsize >= limit)
1040 return (EFAULT);
1043 * Allocate memory for building the header, fill it up,
1044 * and write it out.
1046 target.off_max = target.off;
1047 target.off = 0;
1048 target.buf = kmalloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
1050 error = elf_corehdr(lp, sig, fp, cred, seginfo.count, &target);
1052 /* Write the contents of all of the writable segments. */
1053 if (error == 0) {
1054 Elf_Phdr *php;
1055 int i;
1056 ssize_t nbytes;
1058 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
1059 for (i = 0; i < seginfo.count; i++) {
1060 error = fp_write(fp, (caddr_t)php->p_vaddr,
1061 php->p_filesz, &nbytes, UIO_USERSPACE);
1062 if (error != 0)
1063 break;
1064 php++;
1067 kfree(target.buf, M_TEMP);
1069 return error;
1073 * A callback for each_segment() to write out the segment's
1074 * program header entry.
1076 static int
1077 cb_put_phdr(vm_map_entry_t entry, void *closure)
1079 struct phdr_closure *phc = closure;
1080 Elf_Phdr *phdr = phc->phdr;
1082 if (phc->phdr == phc->phdr_max)
1083 return EINVAL;
1085 phc->offset = round_page(phc->offset);
1087 phdr->p_type = PT_LOAD;
1088 phdr->p_offset = phc->offset;
1089 phdr->p_vaddr = entry->start;
1090 phdr->p_paddr = 0;
1091 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1092 phdr->p_align = PAGE_SIZE;
1093 phdr->p_flags = 0;
1094 if (entry->protection & VM_PROT_READ)
1095 phdr->p_flags |= PF_R;
1096 if (entry->protection & VM_PROT_WRITE)
1097 phdr->p_flags |= PF_W;
1098 if (entry->protection & VM_PROT_EXECUTE)
1099 phdr->p_flags |= PF_X;
1101 phc->offset += phdr->p_filesz;
1102 ++phc->phdr;
1103 return 0;
1107 * A callback for each_writable_segment() to gather information about
1108 * the number of segments and their total size.
1110 static int
1111 cb_size_segment(vm_map_entry_t entry, void *closure)
1113 struct sseg_closure *ssc = closure;
1115 ++ssc->count;
1116 ssc->vsize += entry->end - entry->start;
1117 return 0;
1121 * A callback for each_segment() to gather information about
1122 * the number of text segments.
1124 static int
1125 cb_fpcount_segment(vm_map_entry_t entry, void *closure)
1127 int *count = closure;
1128 struct vnode *vp;
1130 if (entry->object.vm_object->type == OBJT_VNODE) {
1131 vp = (struct vnode *)entry->object.vm_object->handle;
1132 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1133 return 0;
1134 ++*count;
1136 return 0;
1139 static int
1140 cb_put_fp(vm_map_entry_t entry, void *closure)
1142 struct fp_closure *fpc = closure;
1143 struct vn_hdr *vnh = fpc->vnh;
1144 Elf_Phdr *phdr = &vnh->vnh_phdr;
1145 struct vnode *vp;
1146 int error;
1149 * If an entry represents a vnode then write out a file handle.
1151 * If we are checkpointing a checkpoint-restored program we do
1152 * NOT record the filehandle for the old checkpoint vnode (which
1153 * is mapped all over the place). Instead we rely on the fact
1154 * that a checkpoint-restored program does not mmap() the checkpt
1155 * vnode NOCORE, so its contents will be written out to the
1156 * new checkpoint file. This is necessary because the 'old'
1157 * checkpoint file is typically destroyed when a new one is created
1158 * and thus cannot be used to restore the new checkpoint.
1160 * Theoretically we could create a chain of checkpoint files and
1161 * operate the checkpointing operation kinda like an incremental
1162 * checkpoint, but a checkpoint restore would then likely wind up
1163 * referencing many prior checkpoint files and that is a bit over
1164 * the top for the purpose of the checkpoint API.
1166 if (entry->object.vm_object->type == OBJT_VNODE) {
1167 vp = (struct vnode *)entry->object.vm_object->handle;
1168 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1169 return 0;
1170 if (vnh == fpc->vnh_max)
1171 return EINVAL;
1173 if (vp->v_mount)
1174 vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1175 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
1176 if (error) {
1177 char *freepath, *fullpath;
1179 if (vn_fullpath(curproc, vp, &fullpath, &freepath)) {
1180 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error, vp);
1181 } else {
1182 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error, fullpath);
1183 kfree(freepath, M_TEMP);
1185 error = 0;
1188 phdr->p_type = PT_LOAD;
1189 phdr->p_offset = 0; /* not written to core */
1190 phdr->p_vaddr = entry->start;
1191 phdr->p_paddr = 0;
1192 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1193 phdr->p_align = PAGE_SIZE;
1194 phdr->p_flags = 0;
1195 if (entry->protection & VM_PROT_READ)
1196 phdr->p_flags |= PF_R;
1197 if (entry->protection & VM_PROT_WRITE)
1198 phdr->p_flags |= PF_W;
1199 if (entry->protection & VM_PROT_EXECUTE)
1200 phdr->p_flags |= PF_X;
1201 ++fpc->vnh;
1202 ++fpc->count;
1204 return 0;
1208 * For each writable segment in the process's memory map, call the given
1209 * function with a pointer to the map entry and some arbitrary
1210 * caller-supplied data.
1212 static int
1213 each_segment(struct proc *p, segment_callback func, void *closure, int writable)
1215 int error = 0;
1216 vm_map_t map = &p->p_vmspace->vm_map;
1217 vm_map_entry_t entry;
1219 for (entry = map->header.next; error == 0 && entry != &map->header;
1220 entry = entry->next) {
1221 vm_object_t obj;
1224 * Don't dump inaccessible mappings, deal with legacy
1225 * coredump mode.
1227 * Note that read-only segments related to the elf binary
1228 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1229 * need to arbitrarily ignore such segments.
1231 if (elf_legacy_coredump) {
1232 if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
1233 continue;
1234 } else {
1235 if (writable && (entry->protection & VM_PROT_ALL) == 0)
1236 continue;
1240 * Dont include memory segment in the coredump if
1241 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1242 * madvise(2).
1244 * Currently we only dump normal VM object maps. We do
1245 * not dump submaps or virtual page tables.
1247 if (writable && (entry->eflags & MAP_ENTRY_NOCOREDUMP))
1248 continue;
1249 if (entry->maptype != VM_MAPTYPE_NORMAL)
1250 continue;
1251 if ((obj = entry->object.vm_object) == NULL)
1252 continue;
1254 /* Find the deepest backing object. */
1255 while (obj->backing_object != NULL)
1256 obj = obj->backing_object;
1258 /* Ignore memory-mapped devices and such things. */
1259 if (obj->type != OBJT_DEFAULT &&
1260 obj->type != OBJT_SWAP &&
1261 obj->type != OBJT_VNODE)
1262 continue;
1264 error = (*func)(entry, closure);
1266 return error;
1269 static
1270 void *
1271 target_reserve(elf_buf_t target, size_t bytes, int *error)
1273 void *res = NULL;
1275 if (target->buf) {
1276 if (target->off + bytes > target->off_max)
1277 *error = EINVAL;
1278 else
1279 res = target->buf + target->off;
1281 target->off += bytes;
1282 return (res);
1286 * Write the core file header to the file, including padding up to
1287 * the page boundary.
1289 static int
1290 elf_corehdr(struct lwp *lp, int sig, struct file *fp, struct ucred *cred,
1291 int numsegs, elf_buf_t target)
1293 int error;
1294 ssize_t nbytes;
1297 * Fill in the header. The fp is passed so we can detect and flag
1298 * a checkpoint file pointer within the core file itself, because
1299 * it may not be restored from the same file handle.
1301 error = elf_puthdr(lp, target, sig, WRITE, numsegs, fp);
1303 /* Write it to the core file. */
1304 if (error == 0) {
1305 error = fp_write(fp, target->buf, target->off, &nbytes,
1306 UIO_SYSSPACE);
1308 return error;
1311 static int
1312 elf_puthdr(struct lwp *lp, elf_buf_t target, int sig, enum putmode mode,
1313 int numsegs, struct file *fp)
1315 struct proc *p = lp->lwp_proc;
1316 int error = 0;
1317 size_t phoff;
1318 size_t noteoff;
1319 size_t notesz;
1320 Elf_Ehdr *ehdr;
1321 Elf_Phdr *phdr;
1323 ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1325 phoff = target->off;
1326 phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1328 noteoff = target->off;
1329 if (error == 0)
1330 elf_putallnotes(lp, target, sig, mode);
1331 notesz = target->off - noteoff;
1334 * put extra cruft for dumping process state here
1335 * - we really want it be before all the program
1336 * mappings
1337 * - we just need to update the offset accordingly
1338 * and GDB will be none the wiser.
1340 if (error == 0)
1341 error = elf_puttextvp(p, target);
1342 if (error == 0)
1343 error = elf_putsigs(lp, target);
1344 if (error == 0)
1345 error = elf_putfiles(p, target, fp);
1348 * Align up to a page boundary for the program segments. The
1349 * actual data will be written to the outptu file, not to elf_buf_t,
1350 * so we do not have to do any further bounds checking.
1352 target->off = round_page(target->off);
1353 if (error == 0 && ehdr != NULL) {
1355 * Fill in the ELF header.
1357 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1358 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1359 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1360 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1361 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1362 ehdr->e_ident[EI_DATA] = ELF_DATA;
1363 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1364 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1365 ehdr->e_ident[EI_ABIVERSION] = 0;
1366 ehdr->e_ident[EI_PAD] = 0;
1367 ehdr->e_type = ET_CORE;
1368 ehdr->e_machine = ELF_ARCH;
1369 ehdr->e_version = EV_CURRENT;
1370 ehdr->e_entry = 0;
1371 ehdr->e_phoff = phoff;
1372 ehdr->e_flags = 0;
1373 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1374 ehdr->e_phentsize = sizeof(Elf_Phdr);
1375 ehdr->e_phnum = numsegs + 1;
1376 ehdr->e_shentsize = sizeof(Elf_Shdr);
1377 ehdr->e_shnum = 0;
1378 ehdr->e_shstrndx = SHN_UNDEF;
1380 if (error == 0 && phdr != NULL) {
1382 * Fill in the program header entries.
1384 struct phdr_closure phc;
1386 /* The note segement. */
1387 phdr->p_type = PT_NOTE;
1388 phdr->p_offset = noteoff;
1389 phdr->p_vaddr = 0;
1390 phdr->p_paddr = 0;
1391 phdr->p_filesz = notesz;
1392 phdr->p_memsz = 0;
1393 phdr->p_flags = 0;
1394 phdr->p_align = 0;
1395 ++phdr;
1397 /* All the writable segments from the program. */
1398 phc.phdr = phdr;
1399 phc.phdr_max = phdr + numsegs;
1400 phc.offset = target->off;
1401 each_segment(p, cb_put_phdr, &phc, 1);
1403 return (error);
1407 * Append core dump notes to target ELF buffer or simply update target size
1408 * if dryrun selected.
1410 static int
1411 elf_putallnotes(struct lwp *corelp, elf_buf_t target, int sig,
1412 enum putmode mode)
1414 struct proc *p = corelp->lwp_proc;
1415 int error;
1416 struct {
1417 prstatus_t status;
1418 prfpregset_t fpregs;
1419 prpsinfo_t psinfo;
1420 } *tmpdata;
1421 prstatus_t *status;
1422 prfpregset_t *fpregs;
1423 prpsinfo_t *psinfo;
1424 struct lwp *lp;
1427 * Allocate temporary storage for notes on heap to avoid stack overflow.
1429 if (mode != DRYRUN) {
1430 tmpdata = kmalloc(sizeof(*tmpdata), M_TEMP, M_ZERO | M_WAITOK);
1431 status = &tmpdata->status;
1432 fpregs = &tmpdata->fpregs;
1433 psinfo = &tmpdata->psinfo;
1434 } else {
1435 tmpdata = NULL;
1436 status = NULL;
1437 fpregs = NULL;
1438 psinfo = NULL;
1442 * Append LWP-agnostic note.
1444 if (mode != DRYRUN) {
1445 psinfo->pr_version = PRPSINFO_VERSION;
1446 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1447 strncpy(psinfo->pr_fname, p->p_comm,
1448 sizeof(psinfo->pr_fname) - 1);
1450 * XXX - We don't fill in the command line arguments
1451 * properly yet.
1453 strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
1455 error =
1456 elf_putnote(target, "CORE", NT_PRPSINFO, psinfo, sizeof *psinfo);
1457 if (error)
1458 goto exit;
1461 * Append first note for LWP that triggered core so that it is
1462 * the selected one when the debugger starts.
1464 if (mode != DRYRUN) {
1465 status->pr_version = PRSTATUS_VERSION;
1466 status->pr_statussz = sizeof(prstatus_t);
1467 status->pr_gregsetsz = sizeof(gregset_t);
1468 status->pr_fpregsetsz = sizeof(fpregset_t);
1469 status->pr_osreldate = osreldate;
1470 status->pr_cursig = sig;
1472 * XXX GDB needs unique pr_pid for each LWP and does not
1473 * not support pr_pid==0 but lwp_tid can be 0, so hack unique
1474 * value.
1476 status->pr_pid = corelp->lwp_tid;
1477 fill_regs(corelp, &status->pr_reg);
1478 fill_fpregs(corelp, fpregs);
1480 error =
1481 elf_putnote(target, "CORE", NT_PRSTATUS, status, sizeof *status);
1482 if (error)
1483 goto exit;
1484 error =
1485 elf_putnote(target, "CORE", NT_FPREGSET, fpregs, sizeof *fpregs);
1486 if (error)
1487 goto exit;
1490 * Then append notes for other LWPs.
1492 FOREACH_LWP_IN_PROC(lp, p) {
1493 if (lp == corelp)
1494 continue;
1495 /* skip lwps being created */
1496 if (lp->lwp_thread == NULL)
1497 continue;
1498 if (mode != DRYRUN) {
1499 status->pr_pid = lp->lwp_tid;
1500 fill_regs(lp, &status->pr_reg);
1501 fill_fpregs(lp, fpregs);
1503 error = elf_putnote(target, "CORE", NT_PRSTATUS,
1504 status, sizeof *status);
1505 if (error)
1506 goto exit;
1507 error = elf_putnote(target, "CORE", NT_FPREGSET,
1508 fpregs, sizeof *fpregs);
1509 if (error)
1510 goto exit;
1513 exit:
1514 if (tmpdata != NULL)
1515 kfree(tmpdata, M_TEMP);
1516 return (error);
1520 * Generate a note sub-structure.
1522 * NOTE: 4-byte alignment.
1524 static int
1525 elf_putnote(elf_buf_t target, const char *name, int type,
1526 const void *desc, size_t descsz)
1528 int error = 0;
1529 char *dst;
1530 Elf_Note note;
1532 note.n_namesz = strlen(name) + 1;
1533 note.n_descsz = descsz;
1534 note.n_type = type;
1535 dst = target_reserve(target, sizeof(note), &error);
1536 if (dst != NULL)
1537 bcopy(&note, dst, sizeof note);
1538 dst = target_reserve(target, note.n_namesz, &error);
1539 if (dst != NULL)
1540 bcopy(name, dst, note.n_namesz);
1541 target->off = roundup2(target->off, sizeof(Elf_Word));
1542 dst = target_reserve(target, note.n_descsz, &error);
1543 if (dst != NULL)
1544 bcopy(desc, dst, note.n_descsz);
1545 target->off = roundup2(target->off, sizeof(Elf_Word));
1546 return(error);
1550 static int
1551 elf_putsigs(struct lwp *lp, elf_buf_t target)
1553 /* XXX lwp handle more than one lwp */
1554 struct proc *p = lp->lwp_proc;
1555 int error = 0;
1556 struct ckpt_siginfo *csi;
1558 csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1559 if (csi) {
1560 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
1561 bcopy(p->p_sigacts, &csi->csi_sigacts, sizeof(*p->p_sigacts));
1562 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
1563 bcopy(&lp->lwp_sigmask, &csi->csi_sigmask,
1564 sizeof(sigset_t));
1565 csi->csi_sigparent = p->p_sigparent;
1567 return(error);
1570 static int
1571 elf_putfiles(struct proc *p, elf_buf_t target, struct file *ckfp)
1573 int error = 0;
1574 int i;
1575 struct ckpt_filehdr *cfh = NULL;
1576 struct ckpt_fileinfo *cfi;
1577 struct file *fp;
1578 struct vnode *vp;
1580 * the duplicated loop is gross, but it was the only way
1581 * to eliminate uninitialized variable warnings
1583 cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1584 if (cfh) {
1585 cfh->cfh_nfiles = 0;
1589 * ignore STDIN/STDERR/STDOUT.
1591 for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
1592 fp = holdfp(p->p_fd, i, -1);
1593 if (fp == NULL)
1594 continue;
1596 * XXX Only checkpoint vnodes for now.
1598 if (fp->f_type != DTYPE_VNODE) {
1599 fdrop(fp);
1600 continue;
1602 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo),
1603 &error);
1604 if (cfi == NULL) {
1605 fdrop(fp);
1606 continue;
1608 cfi->cfi_index = -1;
1609 cfi->cfi_type = fp->f_type;
1610 cfi->cfi_flags = fp->f_flag;
1611 cfi->cfi_offset = fp->f_offset;
1612 cfi->cfi_ckflags = 0;
1614 if (fp == ckfp)
1615 cfi->cfi_ckflags |= CKFIF_ISCKPTFD;
1616 /* f_count and f_msgcount should not be saved/restored */
1617 /* XXX save cred info */
1619 switch(fp->f_type) {
1620 case DTYPE_VNODE:
1621 vp = (struct vnode *)fp->f_data;
1623 * it looks like a bug in ptrace is marking
1624 * a non-vnode as a vnode - until we find the
1625 * root cause this will at least prevent
1626 * further panics from truss
1628 if (vp == NULL || vp->v_mount == NULL)
1629 break;
1630 cfh->cfh_nfiles++;
1631 cfi->cfi_index = i;
1632 cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1633 error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
1634 break;
1635 default:
1636 break;
1638 fdrop(fp);
1640 return(error);
1643 static int
1644 elf_puttextvp(struct proc *p, elf_buf_t target)
1646 int error = 0;
1647 int *vn_count;
1648 struct fp_closure fpc;
1649 struct ckpt_vminfo *vminfo;
1651 vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1652 if (vminfo != NULL) {
1653 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1654 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1655 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1656 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
1659 fpc.count = 0;
1660 vn_count = target_reserve(target, sizeof(int), &error);
1661 if (target->buf != NULL) {
1662 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1663 fpc.vnh_max = fpc.vnh +
1664 (target->off_max - target->off) / sizeof(struct vn_hdr);
1665 error = each_segment(p, cb_put_fp, &fpc, 0);
1666 if (vn_count)
1667 *vn_count = fpc.count;
1668 } else {
1669 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
1671 target->off += fpc.count * sizeof(struct vn_hdr);
1672 return(error);
1677 * Tell kern_execve.c about it, with a little help from the linker.
1679 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1680 EXEC_SET_ORDERED(elf, elf_execsw, SI_ORDER_FIRST);