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.55 2008/08/17 17:21:36 nth Exp $
33 #include <sys/param.h>
35 #include <sys/fcntl.h>
37 #include <sys/imgact.h>
38 #include <sys/imgact_elf.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/systm.h>
44 #include <sys/nlookup.h>
45 #include <sys/pioctl.h>
46 #include <sys/procfs.h>
47 #include <sys/resourcevar.h>
48 #include <sys/signalvar.h>
50 #include <sys/syscall.h>
51 #include <sys/sysctl.h>
52 #include <sys/sysent.h>
53 #include <sys/vnode.h>
54 #include <sys/sfbuf.h>
57 #include <vm/vm_kern.h>
58 #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>
67 #include <sys/mount.h>
69 #define OLD_EI_BRAND 8
74 static int elf_check_header (const Elf_Ehdr
*hdr
);
75 static int elf_freebsd_fixup (register_t
**stack_base
,
76 struct image_params
*imgp
);
77 static int elf_load_file (struct proc
*p
, const char *file
, u_long
*addr
,
79 static int elf_load_section (struct proc
*p
,
80 struct vmspace
*vmspace
, struct vnode
*vp
,
81 vm_offset_t offset
, caddr_t vmaddr
, size_t memsz
, size_t filsz
,
83 static int exec_elf_imgact (struct image_params
*imgp
);
85 static int elf_trace
= 0;
86 SYSCTL_INT(_debug
, OID_AUTO
, elf_trace
, CTLFLAG_RW
, &elf_trace
, 0, "");
87 static int elf_legacy_coredump
= 0;
88 SYSCTL_INT(_debug
, OID_AUTO
, elf_legacy_coredump
, CTLFLAG_RW
,
89 &elf_legacy_coredump
, 0, "");
91 static int dragonfly_match_abi_note(const Elf_Note
*);
92 static int freebsd_match_abi_note(const Elf_Note
*);
94 static struct sysentvec elf_freebsd_sysvec
= {
114 static Elf_Brandinfo freebsd_brand_info
= {
117 freebsd_match_abi_note
,
119 "/usr/libexec/ld-elf.so.1",
123 static Elf_Brandinfo dragonfly_brand_info
= {
126 dragonfly_match_abi_note
,
128 "/usr/libexec/ld-elf.so.2",
132 static Elf_Brandinfo
*elf_brand_list
[MAX_BRANDS
] = {
133 &dragonfly_brand_info
,
140 freebsd_match_abi_note(const Elf_Note
*abi_note
)
142 const char *abi_name
= (const char *)
143 ((const uint8_t *)abi_note
+ sizeof(*abi_note
));
145 if (abi_note
->n_namesz
!= sizeof("FreeBSD"))
147 if (memcmp(abi_name
, "FreeBSD", sizeof("FreeBSD")))
153 dragonfly_match_abi_note(const Elf_Note
*abi_note
)
155 const char *abi_name
= (const char *)
156 ((const uint8_t *)abi_note
+ sizeof(*abi_note
));
158 if (abi_note
->n_namesz
!= sizeof("DragonFly"))
160 if (memcmp(abi_name
, "DragonFly", sizeof("DragonFly")))
166 elf_insert_brand_entry(Elf_Brandinfo
*entry
)
170 for (i
=1; i
<MAX_BRANDS
; i
++) {
171 if (elf_brand_list
[i
] == NULL
) {
172 elf_brand_list
[i
] = entry
;
182 elf_remove_brand_entry(Elf_Brandinfo
*entry
)
186 for (i
=1; i
<MAX_BRANDS
; i
++) {
187 if (elf_brand_list
[i
] == entry
) {
188 elf_brand_list
[i
] = NULL
;
198 * Check if an elf brand is being used anywhere in the system.
200 * Used by the linux emulation module unloader. This isn't safe from
203 struct elf_brand_inuse_info
{
205 Elf_Brandinfo
*entry
;
208 static int elf_brand_inuse_callback(struct proc
*p
, void *data
);
211 elf_brand_inuse(Elf_Brandinfo
*entry
)
213 struct elf_brand_inuse_info info
;
217 allproc_scan(elf_brand_inuse_callback
, entry
);
223 elf_brand_inuse_callback(struct proc
*p
, void *data
)
225 struct elf_brand_inuse_info
*info
= data
;
227 if (p
->p_sysent
== info
->entry
->sysvec
) {
235 elf_check_header(const Elf_Ehdr
*hdr
)
238 hdr
->e_ident
[EI_CLASS
] != ELF_TARG_CLASS
||
239 hdr
->e_ident
[EI_DATA
] != ELF_TARG_DATA
||
240 hdr
->e_ident
[EI_VERSION
] != EV_CURRENT
||
241 hdr
->e_phentsize
!= sizeof(Elf_Phdr
) ||
242 hdr
->e_ehsize
!= sizeof(Elf_Ehdr
) ||
243 hdr
->e_version
!= ELF_TARG_VER
)
246 if (!ELF_MACHINE_OK(hdr
->e_machine
))
253 elf_load_section(struct proc
*p
, struct vmspace
*vmspace
, struct vnode
*vp
,
254 vm_offset_t offset
, caddr_t vmaddr
, size_t memsz
,
255 size_t filsz
, vm_prot_t prot
)
258 vm_offset_t map_addr
;
263 vm_offset_t file_addr
;
265 object
= vp
->v_object
;
269 * It's necessary to fail if the filsz + offset taken from the
270 * header is greater than the actual file pager object's size.
271 * If we were to allow this, then the vm_map_find() below would
272 * walk right off the end of the file object and into the ether.
274 * While I'm here, might as well check for something else that
275 * is invalid: filsz cannot be greater than memsz.
277 if ((off_t
)filsz
+ offset
> vp
->v_filesize
|| filsz
> memsz
) {
278 uprintf("elf_load_section: truncated ELF file\n");
282 map_addr
= trunc_page((vm_offset_t
)vmaddr
);
283 file_addr
= trunc_page(offset
);
286 * We have two choices. We can either clear the data in the last page
287 * of an oversized mapping, or we can start the anon mapping a page
288 * early and copy the initialized data into that first page. We
289 * choose the second..
292 map_len
= trunc_page(offset
+filsz
) - file_addr
;
294 map_len
= round_page(offset
+filsz
) - file_addr
;
297 vm_object_reference(object
);
299 /* cow flags: don't dump readonly sections in core */
300 cow
= MAP_COPY_ON_WRITE
| MAP_PREFAULT
|
301 (prot
& VM_PROT_WRITE
? 0 : MAP_DISABLE_COREDUMP
);
303 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
304 vm_map_lock(&vmspace
->vm_map
);
305 rv
= vm_map_insert(&vmspace
->vm_map
, &count
,
307 file_addr
, /* file offset */
308 map_addr
, /* virtual start */
309 map_addr
+ map_len
,/* virtual end */
313 vm_map_unlock(&vmspace
->vm_map
);
314 vm_map_entry_release(count
);
315 if (rv
!= KERN_SUCCESS
) {
316 vm_object_deallocate(object
);
320 /* we can stop now if we've covered it all */
321 if (memsz
== filsz
) {
328 * We have to get the remaining bit of the file into the first part
329 * of the oversized map segment. This is normally because the .data
330 * segment in the file is extended to provide bss. It's a neat idea
331 * to try and save a page, but it's a pain in the behind to implement.
333 copy_len
= (offset
+ filsz
) - trunc_page(offset
+ filsz
);
334 map_addr
= trunc_page((vm_offset_t
)vmaddr
+ filsz
);
335 map_len
= round_page((vm_offset_t
)vmaddr
+ memsz
) - map_addr
;
337 /* This had damn well better be true! */
339 count
= vm_map_entry_reserve(MAP_RESERVE_COUNT
);
340 vm_map_lock(&vmspace
->vm_map
);
341 rv
= vm_map_insert(&vmspace
->vm_map
, &count
,
343 map_addr
, map_addr
+ map_len
,
345 VM_PROT_ALL
, VM_PROT_ALL
,
347 vm_map_unlock(&vmspace
->vm_map
);
348 vm_map_entry_release(count
);
349 if (rv
!= KERN_SUCCESS
) {
358 m
= vm_fault_object_page(object
, trunc_page(offset
+ filsz
),
359 VM_PROT_READ
, 0, &error
);
361 sf
= sf_buf_alloc(m
, SFB_CPUPRIVATE
);
362 error
= copyout((caddr_t
)sf_buf_kva(sf
),
363 (caddr_t
)map_addr
, copy_len
);
373 * set it to the specified protection
375 vm_map_protect(&vmspace
->vm_map
, map_addr
, map_addr
+ map_len
, prot
,
382 * Load the file "file" into memory. It may be either a shared object
385 * The "addr" reference parameter is in/out. On entry, it specifies
386 * the address where a shared object should be loaded. If the file is
387 * an executable, this value is ignored. On exit, "addr" specifies
388 * where the file was actually loaded.
390 * The "entry" reference parameter is out only. On exit, it specifies
391 * the entry point for the loaded file.
394 elf_load_file(struct proc
*p
, const char *file
, u_long
*addr
, u_long
*entry
)
397 struct nlookupdata nd
;
399 struct image_params image_params
;
401 const Elf_Ehdr
*hdr
= NULL
;
402 const Elf_Phdr
*phdr
= NULL
;
403 struct nlookupdata
*nd
;
404 struct vmspace
*vmspace
= p
->p_vmspace
;
406 struct image_params
*imgp
;
409 u_long base_addr
= 0;
410 int error
, i
, numsegs
;
412 tempdata
= kmalloc(sizeof(*tempdata
), M_TEMP
, M_WAITOK
);
414 attr
= &tempdata
->attr
;
415 imgp
= &tempdata
->image_params
;
418 * Initialize part of the common data
422 imgp
->firstpage
= NULL
;
423 imgp
->image_header
= NULL
;
426 error
= nlookup_init(nd
, file
, UIO_SYSSPACE
, NLC_FOLLOW
);
430 error
= cache_vget(&nd
->nl_nch
, nd
->nl_cred
, LK_EXCLUSIVE
, &imgp
->vp
);
436 * Check permissions, modes, uid, etc on the file, and "open" it.
438 error
= exec_check_permissions(imgp
);
444 error
= exec_map_first_page(imgp
);
446 * Also make certain that the interpreter stays the same, so set
447 * its VTEXT flag, too.
450 imgp
->vp
->v_flag
|= VTEXT
;
455 hdr
= (const Elf_Ehdr
*)imgp
->image_header
;
456 if ((error
= elf_check_header(hdr
)) != 0)
458 if (hdr
->e_type
== ET_DYN
)
460 else if (hdr
->e_type
== ET_EXEC
)
467 /* Only support headers that fit within first page for now
468 * (multiplication of two Elf_Half fields will not overflow) */
469 if ((hdr
->e_phoff
> PAGE_SIZE
) ||
470 (hdr
->e_phentsize
* hdr
->e_phnum
) > PAGE_SIZE
- hdr
->e_phoff
) {
475 phdr
= (const Elf_Phdr
*)(imgp
->image_header
+ hdr
->e_phoff
);
477 for (i
= 0, numsegs
= 0; i
< hdr
->e_phnum
; i
++) {
478 if (phdr
[i
].p_type
== PT_LOAD
) { /* Loadable segment */
480 if (phdr
[i
].p_flags
& PF_X
)
481 prot
|= VM_PROT_EXECUTE
;
482 if (phdr
[i
].p_flags
& PF_W
)
483 prot
|= VM_PROT_WRITE
;
484 if (phdr
[i
].p_flags
& PF_R
)
485 prot
|= VM_PROT_READ
;
487 error
= elf_load_section(
488 p
, vmspace
, imgp
->vp
,
490 (caddr_t
)phdr
[i
].p_vaddr
+
493 phdr
[i
].p_filesz
, prot
);
497 * Establish the base address if this is the
501 base_addr
= trunc_page(phdr
[i
].p_vaddr
+ rbase
);
506 *entry
=(unsigned long)hdr
->e_entry
+ rbase
;
510 exec_unmap_first_page(imgp
);
515 kfree(tempdata
, M_TEMP
);
521 * non static, as it can be overridden by start_init()
523 int fallback_elf_brand
= -1;
524 SYSCTL_INT(_kern
, OID_AUTO
, fallback_elf_brand
, CTLFLAG_RW
,
525 &fallback_elf_brand
, -1,
526 "ELF brand of last resort");
529 exec_elf_imgact(struct image_params
*imgp
)
531 const Elf_Ehdr
*hdr
= (const Elf_Ehdr
*) imgp
->image_header
;
532 const Elf_Phdr
*phdr
;
533 Elf_Auxargs
*elf_auxargs
= NULL
;
534 struct vmspace
*vmspace
;
536 u_long text_size
= 0, data_size
= 0, total_size
= 0;
537 u_long text_addr
= 0, data_addr
= 0;
538 u_long seg_size
, seg_addr
;
539 u_long addr
, entry
= 0, proghdr
= 0;
541 const char *interp
= NULL
;
542 const Elf_Note
*abi_note
= NULL
;
543 Elf_Brandinfo
*brand_info
;
549 * Do we have a valid ELF header ?
551 if (elf_check_header(hdr
) != 0 || hdr
->e_type
!= ET_EXEC
)
555 * From here on down, we return an errno, not -1, as we've
556 * detected an ELF file.
559 if ((hdr
->e_phoff
> PAGE_SIZE
) ||
560 (hdr
->e_phoff
+ hdr
->e_phentsize
* hdr
->e_phnum
) > PAGE_SIZE
) {
561 /* Only support headers in first page for now */
564 phdr
= (const Elf_Phdr
*)(imgp
->image_header
+ hdr
->e_phoff
);
567 * From this point on, we may have resources that need to be freed.
570 exec_new_vmspace(imgp
, NULL
);
573 * Yeah, I'm paranoid. There is every reason in the world to get
574 * VTEXT now since from here on out, there are places we can have
575 * a context switch. Better safe than sorry; I really don't want
576 * the file to change while it's being loaded.
578 vsetflags(imgp
->vp
, VTEXT
);
580 vmspace
= imgp
->proc
->p_vmspace
;
582 for (i
= 0; i
< hdr
->e_phnum
; i
++) {
583 switch(phdr
[i
].p_type
) {
585 case PT_LOAD
: /* Loadable segment */
587 if (phdr
[i
].p_flags
& PF_X
)
588 prot
|= VM_PROT_EXECUTE
;
589 if (phdr
[i
].p_flags
& PF_W
)
590 prot
|= VM_PROT_WRITE
;
591 if (phdr
[i
].p_flags
& PF_R
)
592 prot
|= VM_PROT_READ
;
594 if ((error
= elf_load_section(imgp
->proc
,
597 (caddr_t
)phdr
[i
].p_vaddr
,
599 phdr
[i
].p_filesz
, prot
)) != 0)
603 * If this segment contains the program headers,
604 * remember their virtual address for the AT_PHDR
605 * aux entry. Static binaries don't usually include
608 if (phdr
[i
].p_offset
== 0 &&
609 hdr
->e_phoff
+ hdr
->e_phnum
* hdr
->e_phentsize
611 proghdr
= phdr
[i
].p_vaddr
+ hdr
->e_phoff
;
613 seg_addr
= trunc_page(phdr
[i
].p_vaddr
);
614 seg_size
= round_page(phdr
[i
].p_memsz
+
615 phdr
[i
].p_vaddr
- seg_addr
);
618 * Is this .text or .data? We can't use
619 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
620 * alpha terribly and possibly does other bad
621 * things so we stick to the old way of figuring
622 * it out: If the segment contains the program
623 * entry point, it's a text segment, otherwise it
626 * Note that obreak() assumes that data_addr +
627 * data_size == end of data load area, and the ELF
628 * file format expects segments to be sorted by
629 * address. If multiple data segments exist, the
630 * last one will be used.
632 if (hdr
->e_entry
>= phdr
[i
].p_vaddr
&&
633 hdr
->e_entry
< (phdr
[i
].p_vaddr
+
635 text_size
= seg_size
;
636 text_addr
= seg_addr
;
637 entry
= (u_long
)hdr
->e_entry
;
639 data_size
= seg_size
;
640 data_addr
= seg_addr
;
642 total_size
+= seg_size
;
645 * Check limits. It should be safe to check the
646 * limits after loading the segment since we do
647 * not actually fault in all the segment's pages.
650 imgp
->proc
->p_rlimit
[RLIMIT_DATA
].rlim_cur
||
651 text_size
> maxtsiz
||
653 imgp
->proc
->p_rlimit
[RLIMIT_VMEM
].rlim_cur
) {
658 case PT_INTERP
: /* Path to interpreter */
659 if (phdr
[i
].p_filesz
> MAXPATHLEN
||
660 phdr
[i
].p_offset
+ phdr
[i
].p_filesz
> PAGE_SIZE
) {
664 interp
= imgp
->image_header
+ phdr
[i
].p_offset
;
666 case PT_NOTE
: /* Check for .note.ABI-tag */
668 const Elf_Note
*tmp_note
;
669 /* XXX handle anything outside the first page */
670 if (phdr
[i
].p_offset
+ phdr
[i
].p_filesz
> PAGE_SIZE
)
672 if (phdr
[i
].p_filesz
< sizeof(Elf_Note
))
673 continue; /* ENOEXEC? */
674 tmp_note
= (const Elf_Note
*)(imgp
->image_header
+ phdr
[i
].p_offset
);
675 if (tmp_note
->n_type
!= 1)
677 if (tmp_note
->n_namesz
+ sizeof(Elf_Note
) +
678 tmp_note
->n_descsz
> phdr
[i
].p_filesz
)
679 continue; /* ENOEXEC? */
683 case PT_PHDR
: /* Program header table info */
684 proghdr
= phdr
[i
].p_vaddr
;
691 vmspace
->vm_tsize
= text_size
>> PAGE_SHIFT
;
692 vmspace
->vm_taddr
= (caddr_t
)(uintptr_t)text_addr
;
693 vmspace
->vm_dsize
= data_size
>> PAGE_SHIFT
;
694 vmspace
->vm_daddr
= (caddr_t
)(uintptr_t)data_addr
;
696 addr
= ELF_RTLD_ADDR(vmspace
);
698 imgp
->entry_addr
= entry
;
702 /* We support three types of branding -- (1) the ELF EI_OSABI field
703 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
704 * branding w/in the ELF header, and (3) path of the `interp_path'
705 * field. We should also look for an ".note.ABI-tag" ELF section now
706 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
709 /* If the executable has a brand, search for it in the brand list. */
710 if (brand_info
== NULL
&& hdr
->e_ident
[EI_OSABI
] != ELFOSABI_NONE
) {
711 for (i
= 0; i
< MAX_BRANDS
; i
++) {
712 Elf_Brandinfo
*bi
= elf_brand_list
[i
];
715 (hdr
->e_ident
[EI_OSABI
] == bi
->brand
717 strncmp((const char *)&hdr
->e_ident
[OLD_EI_BRAND
],
718 bi
->compat_3_brand
, strlen(bi
->compat_3_brand
)))) {
725 /* Search for a recognized ABI. */
726 if (brand_info
== NULL
&& abi_note
!= NULL
) {
727 for (i
= 0; i
< MAX_BRANDS
; i
++) {
728 Elf_Brandinfo
*bi
= elf_brand_list
[i
];
730 if (bi
!= NULL
&& bi
->match_abi_note
!= NULL
&&
731 (*bi
->match_abi_note
)(abi_note
)) {
739 * ELFOSABI_NONE == ELFOSABI_SYSV, so a SYSV binary misses all
740 * checks so far, since it is neither branded nor does it have
741 * an ABI note. If the EI_OSABI field is ELFOSABI_NONE, assume
742 * it is svr4 and look for an entry in the elf_brand_list with
743 * match_abi_note == NULL.
745 if (brand_info
== NULL
&& hdr
->e_ident
[EI_OSABI
] == ELFOSABI_NONE
) {
746 for (i
= 0; i
< MAX_BRANDS
; i
++) {
747 Elf_Brandinfo
*bi
= elf_brand_list
[i
];
749 if (bi
!= NULL
&& bi
->match_abi_note
== NULL
&&
750 ELFOSABI_SYSV
== bi
->brand
) {
757 /* Lacking a recognized ABI, search for a recognized interpreter. */
758 if (brand_info
== NULL
&& interp
!= NULL
) {
759 for (i
= 0; i
< MAX_BRANDS
; i
++) {
760 Elf_Brandinfo
*bi
= elf_brand_list
[i
];
763 strcmp(interp
, bi
->interp_path
) == 0) {
770 /* Lacking a recognized interpreter, try the default brand */
771 if (brand_info
== NULL
) {
772 for (i
= 0; i
< MAX_BRANDS
; i
++) {
773 Elf_Brandinfo
*bi
= elf_brand_list
[i
];
775 if (bi
!= NULL
&& fallback_elf_brand
== bi
->brand
) {
782 if (brand_info
== NULL
) {
783 uprintf("ELF binary type \"%u\" not known.\n",
784 hdr
->e_ident
[EI_OSABI
]);
789 imgp
->proc
->p_sysent
= brand_info
->sysvec
;
790 if (interp
!= NULL
) {
791 path
= kmalloc(MAXPATHLEN
, M_TEMP
, M_WAITOK
);
792 ksnprintf(path
, MAXPATHLEN
, "%s%s",
793 brand_info
->emul_path
, interp
);
794 if ((error
= elf_load_file(imgp
->proc
, path
, &addr
,
795 &imgp
->entry_addr
)) != 0) {
796 if ((error
= elf_load_file(imgp
->proc
, interp
, &addr
,
797 &imgp
->entry_addr
)) != 0) {
798 uprintf("ELF interpreter %s not found\n", path
);
809 * Construct auxargs table (used by the fixup routine)
811 elf_auxargs
= kmalloc(sizeof(Elf_Auxargs
), M_TEMP
, M_WAITOK
);
812 elf_auxargs
->execfd
= -1;
813 elf_auxargs
->phdr
= proghdr
;
814 elf_auxargs
->phent
= hdr
->e_phentsize
;
815 elf_auxargs
->phnum
= hdr
->e_phnum
;
816 elf_auxargs
->pagesz
= PAGE_SIZE
;
817 elf_auxargs
->base
= addr
;
818 elf_auxargs
->flags
= 0;
819 elf_auxargs
->entry
= entry
;
820 elf_auxargs
->trace
= elf_trace
;
822 imgp
->auxargs
= elf_auxargs
;
823 imgp
->interpreted
= 0;
830 elf_freebsd_fixup(register_t
**stack_base
, struct image_params
*imgp
)
832 Elf_Auxargs
*args
= (Elf_Auxargs
*)imgp
->auxargs
;
835 pos
= *stack_base
+ (imgp
->args
->argc
+ imgp
->args
->envc
+ 2);
838 AUXARGS_ENTRY(pos
, AT_DEBUG
, 1);
840 if (args
->execfd
!= -1) {
841 AUXARGS_ENTRY(pos
, AT_EXECFD
, args
->execfd
);
843 AUXARGS_ENTRY(pos
, AT_PHDR
, args
->phdr
);
844 AUXARGS_ENTRY(pos
, AT_PHENT
, args
->phent
);
845 AUXARGS_ENTRY(pos
, AT_PHNUM
, args
->phnum
);
846 AUXARGS_ENTRY(pos
, AT_PAGESZ
, args
->pagesz
);
847 AUXARGS_ENTRY(pos
, AT_FLAGS
, args
->flags
);
848 AUXARGS_ENTRY(pos
, AT_ENTRY
, args
->entry
);
849 AUXARGS_ENTRY(pos
, AT_BASE
, args
->base
);
850 AUXARGS_ENTRY(pos
, AT_NULL
, 0);
852 kfree(imgp
->auxargs
, M_TEMP
);
853 imgp
->auxargs
= NULL
;
856 suword(*stack_base
, (long) imgp
->args
->argc
);
861 * Code for generating ELF core dumps.
864 typedef int (*segment_callback
) (vm_map_entry_t
, void *);
866 /* Closure for cb_put_phdr(). */
867 struct phdr_closure
{
868 Elf_Phdr
*phdr
; /* Program header to fill in (incremented) */
869 Elf_Phdr
*phdr_max
; /* Pointer bound for error check */
870 Elf_Off offset
; /* Offset of segment in core file */
873 /* Closure for cb_size_segment(). */
874 struct sseg_closure
{
875 int count
; /* Count of writable segments. */
876 size_t vsize
; /* Total size of all writable segments. */
879 /* Closure for cb_put_fp(). */
882 struct vn_hdr
*vnh_max
;
887 typedef struct elf_buf
{
893 static void *target_reserve(elf_buf_t target
, size_t bytes
, int *error
);
895 static int cb_put_phdr (vm_map_entry_t
, void *);
896 static int cb_size_segment (vm_map_entry_t
, void *);
897 static int cb_fpcount_segment(vm_map_entry_t
, void *);
898 static int cb_put_fp(vm_map_entry_t
, void *);
901 static int each_segment (struct proc
*, segment_callback
, void *, int);
902 static int elf_corehdr (struct lwp
*, int, struct file
*, struct ucred
*,
904 enum putmode
{ WRITE
, DRYRUN
};
905 static int elf_puthdr (struct lwp
*, elf_buf_t
, int sig
, enum putmode
,
907 static int elf_putallnotes(struct lwp
*, elf_buf_t
, int, enum putmode
);
908 static int elf_putnote (elf_buf_t
, const char *, int, const void *, size_t);
910 static int elf_putsigs(struct lwp
*, elf_buf_t
);
911 static int elf_puttextvp(struct proc
*, elf_buf_t
);
912 static int elf_putfiles(struct proc
*, elf_buf_t
, struct file
*);
914 extern int osreldate
;
917 elf_coredump(struct lwp
*lp
, int sig
, struct vnode
*vp
, off_t limit
)
922 if ((error
= falloc(NULL
, &fp
, NULL
)) != 0)
924 fsetcred(fp
, lp
->lwp_proc
->p_ucred
);
929 fp
->f_type
= DTYPE_VNODE
;
930 fp
->f_flag
= O_CREAT
|O_WRONLY
|O_NOFOLLOW
;
931 fp
->f_ops
= &vnode_fileops
;
935 error
= generic_elf_coredump(lp
, sig
, fp
, limit
);
939 fp
->f_ops
= &badfileops
;
946 generic_elf_coredump(struct lwp
*lp
, int sig
, struct file
*fp
, off_t limit
)
948 struct proc
*p
= lp
->lwp_proc
;
949 struct ucred
*cred
= p
->p_ucred
;
951 struct sseg_closure seginfo
;
952 struct elf_buf target
;
955 kprintf("can't dump core - null fp\n");
958 * Size the program segments
962 each_segment(p
, cb_size_segment
, &seginfo
, 1);
965 * Calculate the size of the core file header area by making
966 * a dry run of generating it. Nothing is written, but the
967 * size is calculated.
969 bzero(&target
, sizeof(target
));
970 elf_puthdr(lp
, &target
, sig
, DRYRUN
, seginfo
.count
, fp
);
972 if (target
.off
+ seginfo
.vsize
>= limit
)
976 * Allocate memory for building the header, fill it up,
979 target
.off_max
= target
.off
;
981 target
.buf
= kmalloc(target
.off_max
, M_TEMP
, M_WAITOK
|M_ZERO
);
983 error
= elf_corehdr(lp
, sig
, fp
, cred
, seginfo
.count
, &target
);
985 /* Write the contents of all of the writable segments. */
991 php
= (Elf_Phdr
*)(target
.buf
+ sizeof(Elf_Ehdr
)) + 1;
992 for (i
= 0; i
< seginfo
.count
; i
++) {
993 error
= fp_write(fp
, (caddr_t
)php
->p_vaddr
,
994 php
->p_filesz
, &nbytes
, UIO_USERSPACE
);
1000 kfree(target
.buf
, M_TEMP
);
1006 * A callback for each_segment() to write out the segment's
1007 * program header entry.
1010 cb_put_phdr(vm_map_entry_t entry
, void *closure
)
1012 struct phdr_closure
*phc
= closure
;
1013 Elf_Phdr
*phdr
= phc
->phdr
;
1015 if (phc
->phdr
== phc
->phdr_max
)
1018 phc
->offset
= round_page(phc
->offset
);
1020 phdr
->p_type
= PT_LOAD
;
1021 phdr
->p_offset
= phc
->offset
;
1022 phdr
->p_vaddr
= entry
->start
;
1024 phdr
->p_filesz
= phdr
->p_memsz
= entry
->end
- entry
->start
;
1025 phdr
->p_align
= PAGE_SIZE
;
1027 if (entry
->protection
& VM_PROT_READ
)
1028 phdr
->p_flags
|= PF_R
;
1029 if (entry
->protection
& VM_PROT_WRITE
)
1030 phdr
->p_flags
|= PF_W
;
1031 if (entry
->protection
& VM_PROT_EXECUTE
)
1032 phdr
->p_flags
|= PF_X
;
1034 phc
->offset
+= phdr
->p_filesz
;
1040 * A callback for each_writable_segment() to gather information about
1041 * the number of segments and their total size.
1044 cb_size_segment(vm_map_entry_t entry
, void *closure
)
1046 struct sseg_closure
*ssc
= closure
;
1049 ssc
->vsize
+= entry
->end
- entry
->start
;
1054 * A callback for each_segment() to gather information about
1055 * the number of text segments.
1058 cb_fpcount_segment(vm_map_entry_t entry
, void *closure
)
1060 int *count
= closure
;
1063 if (entry
->object
.vm_object
->type
== OBJT_VNODE
) {
1064 vp
= (struct vnode
*)entry
->object
.vm_object
->handle
;
1065 if ((vp
->v_flag
& VCKPT
) && curproc
->p_textvp
== vp
)
1073 cb_put_fp(vm_map_entry_t entry
, void *closure
)
1075 struct fp_closure
*fpc
= closure
;
1076 struct vn_hdr
*vnh
= fpc
->vnh
;
1077 Elf_Phdr
*phdr
= &vnh
->vnh_phdr
;
1082 * If an entry represents a vnode then write out a file handle.
1084 * If we are checkpointing a checkpoint-restored program we do
1085 * NOT record the filehandle for the old checkpoint vnode (which
1086 * is mapped all over the place). Instead we rely on the fact
1087 * that a checkpoint-restored program does not mmap() the checkpt
1088 * vnode NOCORE, so its contents will be written out to the
1089 * new checkpoint file. This is necessary because the 'old'
1090 * checkpoint file is typically destroyed when a new one is created
1091 * and thus cannot be used to restore the new checkpoint.
1093 * Theoretically we could create a chain of checkpoint files and
1094 * operate the checkpointing operation kinda like an incremental
1095 * checkpoint, but a checkpoint restore would then likely wind up
1096 * referencing many prior checkpoint files and that is a bit over
1097 * the top for the purpose of the checkpoint API.
1099 if (entry
->object
.vm_object
->type
== OBJT_VNODE
) {
1100 vp
= (struct vnode
*)entry
->object
.vm_object
->handle
;
1101 if ((vp
->v_flag
& VCKPT
) && curproc
->p_textvp
== vp
)
1103 if (vnh
== fpc
->vnh_max
)
1107 vnh
->vnh_fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
1108 error
= VFS_VPTOFH(vp
, &vnh
->vnh_fh
.fh_fid
);
1110 char *freepath
, *fullpath
;
1112 if (vn_fullpath(curproc
, vp
, &fullpath
, &freepath
)) {
1113 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error
, vp
);
1115 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error
, fullpath
);
1116 kfree(freepath
, M_TEMP
);
1121 phdr
->p_type
= PT_LOAD
;
1122 phdr
->p_offset
= 0; /* not written to core */
1123 phdr
->p_vaddr
= entry
->start
;
1125 phdr
->p_filesz
= phdr
->p_memsz
= entry
->end
- entry
->start
;
1126 phdr
->p_align
= PAGE_SIZE
;
1128 if (entry
->protection
& VM_PROT_READ
)
1129 phdr
->p_flags
|= PF_R
;
1130 if (entry
->protection
& VM_PROT_WRITE
)
1131 phdr
->p_flags
|= PF_W
;
1132 if (entry
->protection
& VM_PROT_EXECUTE
)
1133 phdr
->p_flags
|= PF_X
;
1141 * For each writable segment in the process's memory map, call the given
1142 * function with a pointer to the map entry and some arbitrary
1143 * caller-supplied data.
1146 each_segment(struct proc
*p
, segment_callback func
, void *closure
, int writable
)
1149 vm_map_t map
= &p
->p_vmspace
->vm_map
;
1150 vm_map_entry_t entry
;
1152 for (entry
= map
->header
.next
; error
== 0 && entry
!= &map
->header
;
1153 entry
= entry
->next
) {
1157 * Don't dump inaccessible mappings, deal with legacy
1160 * Note that read-only segments related to the elf binary
1161 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1162 * need to arbitrarily ignore such segments.
1164 if (elf_legacy_coredump
) {
1165 if (writable
&& (entry
->protection
& VM_PROT_RW
) != VM_PROT_RW
)
1168 if (writable
&& (entry
->protection
& VM_PROT_ALL
) == 0)
1173 * Dont include memory segment in the coredump if
1174 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1177 * Currently we only dump normal VM object maps. We do
1178 * not dump submaps or virtual page tables.
1180 if (writable
&& (entry
->eflags
& MAP_ENTRY_NOCOREDUMP
))
1182 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
1184 if ((obj
= entry
->object
.vm_object
) == NULL
)
1187 /* Find the deepest backing object. */
1188 while (obj
->backing_object
!= NULL
)
1189 obj
= obj
->backing_object
;
1191 /* Ignore memory-mapped devices and such things. */
1192 if (obj
->type
!= OBJT_DEFAULT
&&
1193 obj
->type
!= OBJT_SWAP
&&
1194 obj
->type
!= OBJT_VNODE
)
1197 error
= (*func
)(entry
, closure
);
1204 target_reserve(elf_buf_t target
, size_t bytes
, int *error
)
1209 if (target
->off
+ bytes
> target
->off_max
)
1212 res
= target
->buf
+ target
->off
;
1214 target
->off
+= bytes
;
1219 * Write the core file header to the file, including padding up to
1220 * the page boundary.
1223 elf_corehdr(struct lwp
*lp
, int sig
, struct file
*fp
, struct ucred
*cred
,
1224 int numsegs
, elf_buf_t target
)
1230 * Fill in the header. The fp is passed so we can detect and flag
1231 * a checkpoint file pointer within the core file itself, because
1232 * it may not be restored from the same file handle.
1234 error
= elf_puthdr(lp
, target
, sig
, WRITE
, numsegs
, fp
);
1236 /* Write it to the core file. */
1238 error
= fp_write(fp
, target
->buf
, target
->off
, &nbytes
,
1245 elf_puthdr(struct lwp
*lp
, elf_buf_t target
, int sig
, enum putmode mode
,
1246 int numsegs
, struct file
*fp
)
1248 struct proc
*p
= lp
->lwp_proc
;
1256 ehdr
= target_reserve(target
, sizeof(Elf_Ehdr
), &error
);
1258 phoff
= target
->off
;
1259 phdr
= target_reserve(target
, (numsegs
+ 1) * sizeof(Elf_Phdr
), &error
);
1261 noteoff
= target
->off
;
1263 elf_putallnotes(lp
, target
, sig
, mode
);
1264 notesz
= target
->off
- noteoff
;
1267 * put extra cruft for dumping process state here
1268 * - we really want it be before all the program
1270 * - we just need to update the offset accordingly
1271 * and GDB will be none the wiser.
1274 error
= elf_puttextvp(p
, target
);
1276 error
= elf_putsigs(lp
, target
);
1278 error
= elf_putfiles(p
, target
, fp
);
1281 * Align up to a page boundary for the program segments. The
1282 * actual data will be written to the outptu file, not to elf_buf_t,
1283 * so we do not have to do any further bounds checking.
1285 target
->off
= round_page(target
->off
);
1286 if (error
== 0 && ehdr
!= NULL
) {
1288 * Fill in the ELF header.
1290 ehdr
->e_ident
[EI_MAG0
] = ELFMAG0
;
1291 ehdr
->e_ident
[EI_MAG1
] = ELFMAG1
;
1292 ehdr
->e_ident
[EI_MAG2
] = ELFMAG2
;
1293 ehdr
->e_ident
[EI_MAG3
] = ELFMAG3
;
1294 ehdr
->e_ident
[EI_CLASS
] = ELF_CLASS
;
1295 ehdr
->e_ident
[EI_DATA
] = ELF_DATA
;
1296 ehdr
->e_ident
[EI_VERSION
] = EV_CURRENT
;
1297 ehdr
->e_ident
[EI_OSABI
] = ELFOSABI_FREEBSD
;
1298 ehdr
->e_ident
[EI_ABIVERSION
] = 0;
1299 ehdr
->e_ident
[EI_PAD
] = 0;
1300 ehdr
->e_type
= ET_CORE
;
1301 ehdr
->e_machine
= ELF_ARCH
;
1302 ehdr
->e_version
= EV_CURRENT
;
1304 ehdr
->e_phoff
= phoff
;
1306 ehdr
->e_ehsize
= sizeof(Elf_Ehdr
);
1307 ehdr
->e_phentsize
= sizeof(Elf_Phdr
);
1308 ehdr
->e_phnum
= numsegs
+ 1;
1309 ehdr
->e_shentsize
= sizeof(Elf_Shdr
);
1311 ehdr
->e_shstrndx
= SHN_UNDEF
;
1313 if (error
== 0 && phdr
!= NULL
) {
1315 * Fill in the program header entries.
1317 struct phdr_closure phc
;
1319 /* The note segement. */
1320 phdr
->p_type
= PT_NOTE
;
1321 phdr
->p_offset
= noteoff
;
1324 phdr
->p_filesz
= notesz
;
1330 /* All the writable segments from the program. */
1332 phc
.phdr_max
= phdr
+ numsegs
;
1333 phc
.offset
= target
->off
;
1334 each_segment(p
, cb_put_phdr
, &phc
, 1);
1340 * Append core dump notes to target ELF buffer or simply update target size
1341 * if dryrun selected.
1344 elf_putallnotes(struct lwp
*corelp
, elf_buf_t target
, int sig
,
1347 struct proc
*p
= corelp
->lwp_proc
;
1351 prfpregset_t fpregs
;
1355 prfpregset_t
*fpregs
;
1360 * Allocate temporary storage for notes on heap to avoid stack overflow.
1362 if (mode
!= DRYRUN
) {
1363 tmpdata
= kmalloc(sizeof(*tmpdata
), M_TEMP
, M_ZERO
| M_WAITOK
);
1364 status
= &tmpdata
->status
;
1365 fpregs
= &tmpdata
->fpregs
;
1366 psinfo
= &tmpdata
->psinfo
;
1375 * Append LWP-agnostic note.
1377 if (mode
!= DRYRUN
) {
1378 psinfo
->pr_version
= PRPSINFO_VERSION
;
1379 psinfo
->pr_psinfosz
= sizeof(prpsinfo_t
);
1380 strncpy(psinfo
->pr_fname
, p
->p_comm
,
1381 sizeof(psinfo
->pr_fname
) - 1);
1383 * XXX - We don't fill in the command line arguments
1386 strncpy(psinfo
->pr_psargs
, p
->p_comm
, PRARGSZ
);
1389 elf_putnote(target
, "FreeBSD", NT_PRPSINFO
, psinfo
, sizeof *psinfo
);
1394 * Append first note for LWP that triggered core so that it is
1395 * the selected one when the debugger starts.
1397 if (mode
!= DRYRUN
) {
1398 status
->pr_version
= PRSTATUS_VERSION
;
1399 status
->pr_statussz
= sizeof(prstatus_t
);
1400 status
->pr_gregsetsz
= sizeof(gregset_t
);
1401 status
->pr_fpregsetsz
= sizeof(fpregset_t
);
1402 status
->pr_osreldate
= osreldate
;
1403 status
->pr_cursig
= sig
;
1405 * XXX GDB needs unique pr_pid for each LWP and does not
1406 * not support pr_pid==0 but lwp_tid can be 0, so hack unique
1409 status
->pr_pid
= p
->p_pid
+ corelp
->lwp_tid
;
1410 fill_regs(corelp
, &status
->pr_reg
);
1411 fill_fpregs(corelp
, fpregs
);
1414 elf_putnote(target
, "FreeBSD", NT_PRSTATUS
, status
, sizeof *status
);
1418 elf_putnote(target
, "FreeBSD", NT_FPREGSET
, fpregs
, sizeof *fpregs
);
1423 * Then append notes for other LWPs.
1425 FOREACH_LWP_IN_PROC(lp
, p
) {
1428 /* skip lwps being created */
1429 if (lp
->lwp_thread
== NULL
)
1431 if (mode
!= DRYRUN
) {
1432 status
->pr_pid
= p
->p_pid
+ lp
->lwp_tid
;
1433 fill_regs(lp
, &status
->pr_reg
);
1434 fill_fpregs(lp
, fpregs
);
1436 error
= elf_putnote(target
, "FreeBSD", NT_PRSTATUS
,
1437 status
, sizeof *status
);
1440 error
= elf_putnote(target
, "FreeBSD", NT_FPREGSET
,
1441 fpregs
, sizeof *fpregs
);
1447 if (tmpdata
!= NULL
)
1448 kfree(tmpdata
, M_TEMP
);
1453 elf_putnote(elf_buf_t target
, const char *name
, int type
,
1454 const void *desc
, size_t descsz
)
1460 note
.n_namesz
= strlen(name
) + 1;
1461 note
.n_descsz
= descsz
;
1463 dst
= target_reserve(target
, sizeof(note
), &error
);
1465 bcopy(¬e
, dst
, sizeof note
);
1466 dst
= target_reserve(target
, note
.n_namesz
, &error
);
1468 bcopy(name
, dst
, note
.n_namesz
);
1469 target
->off
= roundup2(target
->off
, sizeof(Elf_Size
));
1470 dst
= target_reserve(target
, note
.n_descsz
, &error
);
1472 bcopy(desc
, dst
, note
.n_descsz
);
1473 target
->off
= roundup2(target
->off
, sizeof(Elf_Size
));
1479 elf_putsigs(struct lwp
*lp
, elf_buf_t target
)
1481 /* XXX lwp handle more than one lwp */
1482 struct proc
*p
= lp
->lwp_proc
;
1484 struct ckpt_siginfo
*csi
;
1486 csi
= target_reserve(target
, sizeof(struct ckpt_siginfo
), &error
);
1488 csi
->csi_ckptpisz
= sizeof(struct ckpt_siginfo
);
1489 bcopy(p
->p_sigacts
, &csi
->csi_sigacts
, sizeof(*p
->p_sigacts
));
1490 bcopy(&p
->p_realtimer
, &csi
->csi_itimerval
, sizeof(struct itimerval
));
1491 bcopy(&lp
->lwp_sigmask
, &csi
->csi_sigmask
,
1493 csi
->csi_sigparent
= p
->p_sigparent
;
1499 elf_putfiles(struct proc
*p
, elf_buf_t target
, struct file
*ckfp
)
1503 struct ckpt_filehdr
*cfh
= NULL
;
1504 struct ckpt_fileinfo
*cfi
;
1508 * the duplicated loop is gross, but it was the only way
1509 * to eliminate uninitialized variable warnings
1511 cfh
= target_reserve(target
, sizeof(struct ckpt_filehdr
), &error
);
1513 cfh
->cfh_nfiles
= 0;
1517 * ignore STDIN/STDERR/STDOUT.
1519 for (i
= 3; error
== 0 && i
< p
->p_fd
->fd_nfiles
; i
++) {
1520 fp
= holdfp(p
->p_fd
, i
, -1);
1524 * XXX Only checkpoint vnodes for now.
1526 if (fp
->f_type
!= DTYPE_VNODE
) {
1530 cfi
= target_reserve(target
, sizeof(struct ckpt_fileinfo
),
1536 cfi
->cfi_index
= -1;
1537 cfi
->cfi_type
= fp
->f_type
;
1538 cfi
->cfi_flags
= fp
->f_flag
;
1539 cfi
->cfi_offset
= fp
->f_offset
;
1540 cfi
->cfi_ckflags
= 0;
1543 cfi
->cfi_ckflags
|= CKFIF_ISCKPTFD
;
1544 /* f_count and f_msgcount should not be saved/restored */
1545 /* XXX save cred info */
1547 switch(fp
->f_type
) {
1549 vp
= (struct vnode
*)fp
->f_data
;
1551 * it looks like a bug in ptrace is marking
1552 * a non-vnode as a vnode - until we find the
1553 * root cause this will at least prevent
1554 * further panics from truss
1556 if (vp
== NULL
|| vp
->v_mount
== NULL
)
1560 cfi
->cfi_fh
.fh_fsid
= vp
->v_mount
->mnt_stat
.f_fsid
;
1561 error
= VFS_VPTOFH(vp
, &cfi
->cfi_fh
.fh_fid
);
1572 elf_puttextvp(struct proc
*p
, elf_buf_t target
)
1576 struct fp_closure fpc
;
1577 struct ckpt_vminfo
*vminfo
;
1579 vminfo
= target_reserve(target
, sizeof(struct ckpt_vminfo
), &error
);
1580 if (vminfo
!= NULL
) {
1581 vminfo
->cvm_dsize
= p
->p_vmspace
->vm_dsize
;
1582 vminfo
->cvm_tsize
= p
->p_vmspace
->vm_tsize
;
1583 vminfo
->cvm_daddr
= p
->p_vmspace
->vm_daddr
;
1584 vminfo
->cvm_taddr
= p
->p_vmspace
->vm_taddr
;
1588 vn_count
= target_reserve(target
, sizeof(int), &error
);
1589 if (target
->buf
!= NULL
) {
1590 fpc
.vnh
= (struct vn_hdr
*)(target
->buf
+ target
->off
);
1591 fpc
.vnh_max
= fpc
.vnh
+
1592 (target
->off_max
- target
->off
) / sizeof(struct vn_hdr
);
1593 error
= each_segment(p
, cb_put_fp
, &fpc
, 0);
1595 *vn_count
= fpc
.count
;
1597 error
= each_segment(p
, cb_fpcount_segment
, &fpc
.count
, 0);
1599 target
->off
+= fpc
.count
* sizeof(struct vn_hdr
);
1605 * Tell kern_execve.c about it, with a little help from the linker.
1607 static struct execsw elf_execsw
= {exec_elf_imgact
, "ELF"};
1608 EXEC_SET_ORDERED(elf
, elf_execsw
, SI_ORDER_FIRST
);