imgact_elf.c revision 43748
1/*-
2 * Copyright (c) 1995-1996 S�ren Schmidt
3 * Copyright (c) 1996 Peter Wemm
4 * All rights reserved.
5 *
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
17 *
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.
28 *
29 *	$Id: imgact_elf.c,v 1.51 1999/02/05 22:24:26 jdp Exp $
30 */
31
32#include "opt_rlimit.h"
33
34#include <sys/param.h>
35#include <sys/acct.h>
36#include <sys/exec.h>
37#include <sys/fcntl.h>
38#include <sys/imgact.h>
39#include <sys/imgact_elf.h>
40#include <sys/kernel.h>
41#include <sys/malloc.h>
42#include <sys/mman.h>
43#include <sys/namei.h>
44#include <sys/pioctl.h>
45#include <sys/proc.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/systm.h>
54#include <sys/vnode.h>
55
56#include <vm/vm.h>
57#include <vm/vm_kern.h>
58#include <vm/vm_param.h>
59#include <vm/pmap.h>
60#include <sys/lock.h>
61#include <vm/vm_map.h>
62#include <vm/vm_object.h>
63#include <vm/vm_prot.h>
64#include <vm/vm_extern.h>
65
66#include <machine/elf.h>
67#include <machine/md_var.h>
68
69__ElfType(Brandinfo);
70__ElfType(Auxargs);
71
72static int elf_check_header __P((const Elf_Ehdr *hdr, int type));
73static int elf_freebsd_fixup __P((long **stack_base,
74    struct image_params *imgp));
75static int elf_load_file __P((struct proc *p, char *file, u_long *addr,
76    u_long *entry));
77static int elf_load_section __P((struct proc *p,
78    struct vmspace *vmspace, struct vnode *vp,
79    vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
80    vm_prot_t prot));
81static int exec_elf_imgact __P((struct image_params *imgp));
82
83static int elf_trace = 0;
84SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
85
86/*
87 * XXX Maximum length of an ELF brand (sysctl wants a statically-allocated
88 * buffer).
89 */
90#define	MAXBRANDLEN	16
91
92static struct sysentvec elf_freebsd_sysvec = {
93        SYS_MAXSYSCALL,
94        sysent,
95        0,
96        0,
97        0,
98        0,
99        0,
100        0,
101        elf_freebsd_fixup,
102        sendsig,
103        sigcode,
104        &szsigcode,
105        0,
106	"FreeBSD ELF",
107	elf_coredump
108};
109
110static Elf_Brandinfo freebsd_brand_info = {
111						"FreeBSD",
112						"",
113						"/usr/libexec/ld-elf.so.1",
114						&elf_freebsd_sysvec
115					  };
116static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
117							&freebsd_brand_info,
118							NULL, NULL, NULL,
119							NULL, NULL, NULL, NULL
120						    };
121
122int
123elf_insert_brand_entry(Elf_Brandinfo *entry)
124{
125	int i;
126
127	for (i=1; i<MAX_BRANDS; i++) {
128		if (elf_brand_list[i] == NULL) {
129			elf_brand_list[i] = entry;
130			break;
131		}
132	}
133	if (i == MAX_BRANDS)
134		return -1;
135	return 0;
136}
137
138int
139elf_remove_brand_entry(Elf_Brandinfo *entry)
140{
141	int i;
142
143	for (i=1; i<MAX_BRANDS; i++) {
144		if (elf_brand_list[i] == entry) {
145			elf_brand_list[i] = NULL;
146			break;
147		}
148	}
149	if (i == MAX_BRANDS)
150		return -1;
151	return 0;
152}
153
154int
155elf_brand_inuse(Elf_Brandinfo *entry)
156{
157	struct proc *p;
158
159	for (p = allproc.lh_first; p != 0; p = p->p_list.le_next) {
160		if (p->p_sysent == entry->sysvec)
161			return TRUE;
162	}
163
164	return FALSE;
165}
166
167static int
168elf_check_header(const Elf_Ehdr *hdr, int type)
169{
170	if (!IS_ELF(*hdr) ||
171	    hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
172	    hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
173	    hdr->e_ident[EI_VERSION] != EV_CURRENT)
174		return ENOEXEC;
175
176	if (!ELF_MACHINE_OK(hdr->e_machine))
177		return ENOEXEC;
178
179	if (hdr->e_type != type || hdr->e_version != ELF_TARG_VER)
180		return ENOEXEC;
181
182	return 0;
183}
184
185static int
186elf_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)
187{
188	size_t map_len;
189	vm_offset_t map_addr;
190	int error, rv;
191	size_t copy_len;
192	vm_object_t object;
193	vm_offset_t file_addr;
194	vm_offset_t data_buf = 0;
195
196	object = vp->v_object;
197	error = 0;
198
199	map_addr = trunc_page((vm_offset_t)vmaddr);
200	file_addr = trunc_page(offset);
201
202	/*
203	 * We have two choices.  We can either clear the data in the last page
204	 * of an oversized mapping, or we can start the anon mapping a page
205	 * early and copy the initialized data into that first page.  We
206	 * choose the second..
207	 */
208	if (memsz > filsz)
209		map_len = trunc_page(offset+filsz) - file_addr;
210	else
211		map_len = round_page(offset+filsz) - file_addr;
212
213	if (map_len != 0) {
214		vm_object_reference(object);
215		vm_map_lock(&vmspace->vm_map);
216		rv = vm_map_insert(&vmspace->vm_map,
217				      object,
218				      file_addr,	/* file offset */
219				      map_addr,		/* virtual start */
220				      map_addr + map_len,/* virtual end */
221				      prot,
222				      VM_PROT_ALL,
223				      MAP_COPY_NEEDED | MAP_COPY_ON_WRITE);
224		vm_map_unlock(&vmspace->vm_map);
225		if (rv != KERN_SUCCESS)
226			return EINVAL;
227
228		/* prefault the page tables */
229		pmap_object_init_pt(&vmspace->vm_pmap,
230				    map_addr,
231				    object,
232				    (vm_pindex_t) OFF_TO_IDX(file_addr),
233				    map_len,
234				    0);
235
236		/* we can stop now if we've covered it all */
237		if (memsz == filsz)
238			return 0;
239	}
240
241
242	/*
243	 * We have to get the remaining bit of the file into the first part
244	 * of the oversized map segment.  This is normally because the .data
245	 * segment in the file is extended to provide bss.  It's a neat idea
246	 * to try and save a page, but it's a pain in the behind to implement.
247	 */
248	copy_len = (offset + filsz) - trunc_page(offset + filsz);
249	map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
250	map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
251
252	/* This had damn well better be true! */
253        if (map_len != 0) {
254		vm_map_lock(&vmspace->vm_map);
255		rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
256					map_addr, map_addr + map_len,
257					VM_PROT_ALL, VM_PROT_ALL, 0);
258		vm_map_unlock(&vmspace->vm_map);
259		if (rv != KERN_SUCCESS)
260			return EINVAL;
261	}
262
263	if (copy_len != 0) {
264		vm_object_reference(object);
265		rv = vm_map_find(exec_map,
266				 object,
267				 trunc_page(offset + filsz),
268				 &data_buf,
269				 PAGE_SIZE,
270				 TRUE,
271				 VM_PROT_READ,
272				 VM_PROT_ALL,
273				 MAP_COPY_ON_WRITE | MAP_COPY_NEEDED);
274		if (rv != KERN_SUCCESS) {
275			vm_object_deallocate(object);
276			return EINVAL;
277		}
278		pmap_object_init_pt(exec_map->pmap, data_buf, object,
279			(vm_pindex_t) OFF_TO_IDX(trunc_page(offset + filsz)),
280			PAGE_SIZE, 1);
281
282		/* send the page fragment to user space */
283		error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
284		vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
285		if (error)
286			return (error);
287	}
288
289	/*
290	 * set it to the specified protection
291	 */
292	vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,  prot,
293		       FALSE);
294
295	return error;
296}
297
298static int
299elf_load_file(struct proc *p, char *file, u_long *addr, u_long *entry)
300{
301	const Elf_Ehdr *hdr = NULL;
302	const Elf_Phdr *phdr = NULL;
303	struct nameidata nd;
304	struct vmspace *vmspace = p->p_vmspace;
305	struct vattr attr;
306	struct image_params image_params, *imgp;
307	vm_prot_t prot;
308	unsigned long text_size = 0, data_size = 0;
309	unsigned long text_addr = 0, data_addr = 0;
310        int error, i;
311
312	imgp = &image_params;
313	/*
314	 * Initialize part of the common data
315	 */
316	imgp->proc = p;
317	imgp->uap = NULL;
318	imgp->attr = &attr;
319	imgp->firstpage = NULL;
320	imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
321
322	if (imgp->image_header == NULL) {
323		nd.ni_vp = NULL;
324		error = ENOMEM;
325		goto fail;
326	}
327
328        NDINIT(&nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, p);
329
330	if ((error = namei(&nd)) != 0) {
331		nd.ni_vp = NULL;
332		goto fail;
333	}
334
335	imgp->vp = nd.ni_vp;
336
337	/*
338	 * Check permissions, modes, uid, etc on the file, and "open" it.
339	 */
340	error = exec_check_permissions(imgp);
341	if (error) {
342		VOP_UNLOCK(nd.ni_vp, 0, p);
343		goto fail;
344	}
345
346	error = exec_map_first_page(imgp);
347	VOP_UNLOCK(nd.ni_vp, 0, p);
348	if (error)
349                goto fail;
350
351	hdr = (const Elf_Ehdr *)imgp->image_header;
352	if ((error = elf_check_header(hdr, ET_DYN)) != 0)
353		goto fail;
354
355	/* Only support headers that fit within first page for now */
356	if ((hdr->e_phoff > PAGE_SIZE) ||
357	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
358		error = ENOEXEC;
359		goto fail;
360	}
361
362	phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
363
364	for (i = 0; i < hdr->e_phnum; i++) {
365		if (phdr[i].p_type == PT_LOAD) {	/* Loadable segment */
366			prot = 0;
367			if (phdr[i].p_flags & PF_X)
368  				prot |= VM_PROT_EXECUTE;
369			if (phdr[i].p_flags & PF_W)
370  				prot |= VM_PROT_WRITE;
371			if (phdr[i].p_flags & PF_R)
372  				prot |= VM_PROT_READ;
373
374			if ((error = elf_load_section(p, vmspace, nd.ni_vp,
375  						     phdr[i].p_offset,
376  						     (caddr_t)phdr[i].p_vaddr +
377							(*addr),
378  						     phdr[i].p_memsz,
379  						     phdr[i].p_filesz, prot)) != 0)
380				goto fail;
381
382			/*
383			 * Is this .text or .data ??
384			 *
385			 * We only handle one each of those yet XXX
386			 */
387			if (hdr->e_entry >= phdr[i].p_vaddr &&
388			hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) {
389  				text_addr = trunc_page(phdr[i].p_vaddr+(*addr));
390  				text_size = round_page(phdr[i].p_memsz +
391						       phdr[i].p_vaddr -
392						       trunc_page(phdr[i].p_vaddr));
393				*entry=(unsigned long)hdr->e_entry+(*addr);
394			} else {
395  				data_addr = trunc_page(phdr[i].p_vaddr+(*addr));
396  				data_size = round_page(phdr[i].p_memsz +
397						       phdr[i].p_vaddr -
398						       trunc_page(phdr[i].p_vaddr));
399			}
400		}
401	}
402
403fail:
404	if (imgp->firstpage)
405		exec_unmap_first_page(imgp);
406	if (imgp->image_header)
407		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
408			PAGE_SIZE);
409	if (nd.ni_vp)
410		vrele(nd.ni_vp);
411
412	return error;
413}
414
415static char fallback_elf_brand[MAXBRANDLEN+1] = { "none" };
416SYSCTL_STRING(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
417		fallback_elf_brand, sizeof(fallback_elf_brand),
418		"ELF brand of last resort");
419
420static int
421exec_elf_imgact(struct image_params *imgp)
422{
423	const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
424	const Elf_Phdr *phdr;
425	Elf_Auxargs *elf_auxargs = NULL;
426	struct vmspace *vmspace;
427	vm_prot_t prot;
428	u_long text_size = 0, data_size = 0;
429	u_long text_addr = 0, data_addr = 0;
430	u_long addr, entry = 0, proghdr = 0;
431	int error, i;
432	const char *interp = NULL;
433	Elf_Brandinfo *brand_info;
434	const char *brand;
435	char path[MAXPATHLEN];
436
437	/*
438	 * Do we have a valid ELF header ?
439	 */
440	if (elf_check_header(hdr, ET_EXEC))
441		return -1;
442
443	/*
444	 * From here on down, we return an errno, not -1, as we've
445	 * detected an ELF file.
446	 */
447
448	if ((hdr->e_phoff > PAGE_SIZE) ||
449	    (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
450		/* Only support headers in first page for now */
451		return ENOEXEC;
452	}
453	phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
454
455	/*
456	 * From this point on, we may have resources that need to be freed.
457	 */
458	if ((error = exec_extract_strings(imgp)) != 0)
459		goto fail;
460
461	exec_new_vmspace(imgp);
462
463	vmspace = imgp->proc->p_vmspace;
464
465	for (i = 0; i < hdr->e_phnum; i++) {
466		switch(phdr[i].p_type) {
467
468		case PT_LOAD:	/* Loadable segment */
469			prot = 0;
470			if (phdr[i].p_flags & PF_X)
471  				prot |= VM_PROT_EXECUTE;
472			if (phdr[i].p_flags & PF_W)
473  				prot |= VM_PROT_WRITE;
474			if (phdr[i].p_flags & PF_R)
475  				prot |= VM_PROT_READ;
476
477			if ((error = elf_load_section(imgp->proc,
478						     vmspace, imgp->vp,
479  						     phdr[i].p_offset,
480  						     (caddr_t)phdr[i].p_vaddr,
481  						     phdr[i].p_memsz,
482  						     phdr[i].p_filesz, prot)) != 0)
483  				goto fail;
484
485			/*
486			 * Is this .text or .data ??
487			 *
488			 * We only handle one each of those yet XXX
489			 */
490			if (hdr->e_entry >= phdr[i].p_vaddr &&
491			hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) {
492  				text_addr = trunc_page(phdr[i].p_vaddr);
493  				text_size = round_page(phdr[i].p_memsz +
494						       phdr[i].p_vaddr -
495						       text_addr);
496				entry = (u_long)hdr->e_entry;
497			} else {
498  				data_addr = trunc_page(phdr[i].p_vaddr);
499  				data_size = round_page(phdr[i].p_memsz +
500						       phdr[i].p_vaddr -
501						       data_addr);
502			}
503			break;
504	  	case PT_INTERP:	/* Path to interpreter */
505			if (phdr[i].p_filesz > MAXPATHLEN ||
506			    phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
507				error = ENOEXEC;
508				goto fail;
509			}
510			interp = imgp->image_header + phdr[i].p_offset;
511			break;
512		case PT_PHDR: 	/* Program header table info */
513			proghdr = phdr[i].p_vaddr;
514			break;
515		default:
516			break;
517		}
518	}
519
520	vmspace->vm_tsize = text_size >> PAGE_SHIFT;
521	vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
522	vmspace->vm_dsize = data_size >> PAGE_SHIFT;
523	vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
524
525	addr = 2L*MAXDSIZ; /* May depend on OS type XXX */
526
527	imgp->entry_addr = entry;
528
529	/* If the executable has a brand, search for it in the brand list. */
530	brand_info = NULL;
531	brand = (const char *)&hdr->e_ident[EI_BRAND];
532	if (brand[0] != '\0') {
533		for (i = 0;  i < MAX_BRANDS;  i++) {
534			Elf_Brandinfo *bi = elf_brand_list[i];
535
536			if (bi != NULL && strcmp(brand, bi->brand) == 0) {
537				brand_info = bi;
538				break;
539			}
540		}
541	}
542
543	/* Lacking a known brand, search for a recognized interpreter. */
544	if (brand_info == NULL && interp != NULL) {
545		for (i = 0;  i < MAX_BRANDS;  i++) {
546			Elf_Brandinfo *bi = elf_brand_list[i];
547
548			if (bi != NULL &&
549			    strcmp(interp, bi->interp_path) == 0) {
550				brand_info = bi;
551				break;
552			}
553		}
554	}
555
556	/* Lacking a recognized interpreter, try the default brand */
557	if (brand_info == NULL && fallback_elf_brand[0] != '\0') {
558		for (i = 0; i < MAX_BRANDS; i++) {
559			Elf_Brandinfo *bi = elf_brand_list[i];
560
561			if (bi != NULL
562			    && strcmp(fallback_elf_brand, bi->brand) == 0) {
563				brand_info = bi;
564				break;
565			}
566		}
567	}
568
569#ifdef __alpha__
570	/* XXX - Assume FreeBSD on the alpha. */
571	if (brand_info == NULL)
572		brand_info = &freebsd_brand_info;
573#endif
574
575	if (brand_info == NULL) {
576		if (brand[0] == 0)
577			uprintf("ELF binary type not known."
578			    "  Use \"brandelf\" to brand it.\n");
579		else
580			uprintf("ELF binary type \"%.*s\" not known.\n",
581			    EI_NIDENT - EI_BRAND, brand);
582		error = ENOEXEC;
583		goto fail;
584	}
585
586	imgp->proc->p_sysent = brand_info->sysvec;
587	if (interp != NULL) {
588		snprintf(path, sizeof(path), "%s%s",
589		    brand_info->emul_path, interp);
590                if ((error = elf_load_file(imgp->proc, path, &addr,
591		    &imgp->entry_addr)) != 0) {
592                        uprintf("ELF interpreter %s not found\n", path);
593                        goto fail;
594                }
595	}
596
597	/*
598	 * Construct auxargs table (used by the fixup routine)
599	 */
600	elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
601	elf_auxargs->execfd = -1;
602	elf_auxargs->phdr = proghdr;
603	elf_auxargs->phent = hdr->e_phentsize;
604	elf_auxargs->phnum = hdr->e_phnum;
605	elf_auxargs->pagesz = PAGE_SIZE;
606	elf_auxargs->base = addr;
607	elf_auxargs->flags = 0;
608	elf_auxargs->entry = entry;
609	elf_auxargs->trace = elf_trace;
610
611	imgp->auxargs = elf_auxargs;
612	imgp->interpreted = 0;
613
614	/* don't allow modifying the file while we run it */
615	imgp->vp->v_flag |= VTEXT;
616
617fail:
618	return error;
619}
620
621static int
622elf_freebsd_fixup(long **stack_base, struct image_params *imgp)
623{
624	Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
625	long *pos;
626
627	pos = *stack_base + (imgp->argc + imgp->envc + 2);
628
629	if (args->trace) {
630		AUXARGS_ENTRY(pos, AT_DEBUG, 1);
631	}
632	if (args->execfd != -1) {
633		AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
634	}
635	AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
636	AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
637	AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
638	AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
639	AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
640	AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
641	AUXARGS_ENTRY(pos, AT_BASE, args->base);
642	AUXARGS_ENTRY(pos, AT_NULL, 0);
643
644	free(imgp->auxargs, M_TEMP);
645	imgp->auxargs = NULL;
646
647	(*stack_base)--;
648	suword(*stack_base, (long) imgp->argc);
649	return 0;
650}
651
652/*
653 * Code for generating ELF core dumps.
654 */
655
656typedef void (*segment_callback) __P((vm_map_entry_t, void *));
657
658/* Closure for cb_put_phdr(). */
659struct phdr_closure {
660	Elf_Phdr *phdr;		/* Program header to fill in */
661	Elf_Off offset;		/* Offset of segment in core file */
662};
663
664/* Closure for cb_size_segment(). */
665struct sseg_closure {
666	int count;		/* Count of writable segments. */
667	size_t size;		/* Total size of all writable segments. */
668};
669
670static void cb_put_phdr __P((vm_map_entry_t, void *));
671static void cb_size_segment __P((vm_map_entry_t, void *));
672static void each_writable_segment __P((struct proc *, segment_callback,
673    void *));
674static int elf_corehdr __P((struct proc *, struct vnode *, struct ucred *,
675    int, void *, size_t));
676static void elf_puthdr __P((struct proc *, void *, size_t *,
677    const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int));
678static void elf_putnote __P((void *, size_t *, const char *, int,
679    const void *, size_t));
680
681extern int osreldate;
682
683int
684elf_coredump(p)
685	register struct proc *p;
686{
687	register struct vnode *vp;
688	register struct ucred *cred = p->p_cred->pc_ucred;
689	struct nameidata nd;
690	struct vattr vattr;
691	int error, error1;
692	char *name;			/* name of corefile */
693	struct sseg_closure seginfo;
694	void *hdr;
695	size_t hdrsize;
696
697	STOPEVENT(p, S_CORE, 0);
698
699	if (sugid_coredump == 0 && p->p_flag & P_SUGID)
700		return (EFAULT);
701
702	/* Size the program segments. */
703	seginfo.count = 0;
704	seginfo.size = 0;
705	each_writable_segment(p, cb_size_segment, &seginfo);
706
707	/*
708	 * Calculate the size of the core file header area by making
709	 * a dry run of generating it.  Nothing is written, but the
710	 * size is calculated.
711	 */
712	hdrsize = 0;
713	elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize,
714	    (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
715	    (const prpsinfo_t *)NULL, seginfo.count);
716
717	if (hdrsize + seginfo.size >= p->p_rlimit[RLIMIT_CORE].rlim_cur)
718		return (EFAULT);
719	name = expand_name(p->p_comm, p->p_ucred->cr_uid, p->p_pid);
720	if (name == NULL)
721		return (EFAULT);	/* XXX -- not the best error */
722
723	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, name, p);
724	error = vn_open(&nd, O_CREAT | FWRITE, S_IRUSR | S_IWUSR);
725	free(name, M_TEMP);
726	if (error)
727		return (error);
728	vp = nd.ni_vp;
729
730	/* Don't dump to non-regular files or files with links. */
731	if (vp->v_type != VREG ||
732	    VOP_GETATTR(vp, &vattr, cred, p) || vattr.va_nlink != 1) {
733		error = EFAULT;
734		goto out;
735	}
736	VATTR_NULL(&vattr);
737	vattr.va_size = 0;
738	VOP_LEASE(vp, p, cred, LEASE_WRITE);
739	VOP_SETATTR(vp, &vattr, cred, p);
740	p->p_acflag |= ACORE;
741
742
743	/*
744	 * Allocate memory for building the header, fill it up,
745	 * and write it out.
746	 */
747	hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
748	if (hdr == NULL) {
749		error = EINVAL;
750		goto out;
751	}
752	error = elf_corehdr(p, vp, cred, seginfo.count, hdr, hdrsize);
753
754	/* Write the contents of all of the writable segments. */
755	if (error == 0) {
756		Elf_Phdr *php;
757		off_t offset;
758		int i;
759
760		php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
761		offset = hdrsize;
762		for (i = 0;  i < seginfo.count;  i++) {
763			error = vn_rdwr(UIO_WRITE, vp, (caddr_t)php->p_vaddr,
764			    php->p_filesz, offset, UIO_USERSPACE,
765			    IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, p);
766			if (error != 0)
767				break;
768			offset += php->p_filesz;
769			php++;
770		}
771	}
772	free(hdr, M_TEMP);
773
774out:
775	VOP_UNLOCK(vp, 0, p);
776	error1 = vn_close(vp, FWRITE, cred, p);
777	if (error == 0)
778		error = error1;
779	return (error);
780}
781
782/*
783 * A callback for each_writable_segment() to write out the segment's
784 * program header entry.
785 */
786static void
787cb_put_phdr(entry, closure)
788	vm_map_entry_t entry;
789	void *closure;
790{
791	struct phdr_closure *phc = (struct phdr_closure *)closure;
792	Elf_Phdr *phdr = phc->phdr;
793
794	phc->offset = round_page(phc->offset);
795
796	phdr->p_type = PT_LOAD;
797	phdr->p_offset = phc->offset;
798	phdr->p_vaddr = entry->start;
799	phdr->p_paddr = 0;
800	phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
801	phdr->p_align = PAGE_SIZE;
802	phdr->p_flags = 0;
803	if (entry->protection & VM_PROT_READ)
804		phdr->p_flags |= PF_R;
805	if (entry->protection & VM_PROT_WRITE)
806		phdr->p_flags |= PF_W;
807	if (entry->protection & VM_PROT_EXECUTE)
808		phdr->p_flags |= PF_X;
809
810	phc->offset += phdr->p_filesz;
811	phc->phdr++;
812}
813
814/*
815 * A callback for each_writable_segment() to gather information about
816 * the number of segments and their total size.
817 */
818static void
819cb_size_segment(entry, closure)
820	vm_map_entry_t entry;
821	void *closure;
822{
823	struct sseg_closure *ssc = (struct sseg_closure *)closure;
824
825	ssc->count++;
826	ssc->size += entry->end - entry->start;
827}
828
829/*
830 * For each writable segment in the process's memory map, call the given
831 * function with a pointer to the map entry and some arbitrary
832 * caller-supplied data.
833 */
834static void
835each_writable_segment(p, func, closure)
836	struct proc *p;
837	segment_callback func;
838	void *closure;
839{
840	vm_map_t map = &p->p_vmspace->vm_map;
841	vm_map_entry_t entry;
842
843	for (entry = map->header.next;  entry != &map->header;
844	    entry = entry->next) {
845		vm_object_t obj;
846
847		if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
848		    (entry->protection & (VM_PROT_READ|VM_PROT_WRITE)) !=
849		    (VM_PROT_READ|VM_PROT_WRITE))
850			continue;
851
852		if ((obj = entry->object.vm_object) == NULL)
853			continue;
854
855		/* Find the deepest backing object. */
856		while (obj->backing_object != NULL)
857			obj = obj->backing_object;
858
859		/* Ignore memory-mapped devices and such things. */
860		if (obj->type != OBJT_DEFAULT &&
861		    obj->type != OBJT_SWAP &&
862		    obj->type != OBJT_VNODE)
863			continue;
864
865		(*func)(entry, closure);
866	}
867}
868
869/*
870 * Write the core file header to the file, including padding up to
871 * the page boundary.
872 */
873static int
874elf_corehdr(p, vp, cred, numsegs, hdr, hdrsize)
875	struct proc *p;
876	struct vnode *vp;
877	struct ucred *cred;
878	int numsegs;
879	size_t hdrsize;
880	void *hdr;
881{
882	size_t off;
883	prstatus_t status;
884	prfpregset_t fpregset;
885	prpsinfo_t psinfo;
886
887	/* Gather the information for the header. */
888	bzero(&status, sizeof status);
889	status.pr_version = PRSTATUS_VERSION;
890	status.pr_statussz = sizeof(prstatus_t);
891	status.pr_gregsetsz = sizeof(gregset_t);
892	status.pr_fpregsetsz = sizeof(fpregset_t);
893	status.pr_osreldate = osreldate;
894	status.pr_cursig = p->p_sig;
895	status.pr_pid = p->p_pid;
896	fill_regs(p, &status.pr_reg);
897
898	fill_fpregs(p, &fpregset);
899
900	bzero(&psinfo, sizeof psinfo);
901	psinfo.pr_version = PRPSINFO_VERSION;
902	psinfo.pr_psinfosz = sizeof(prpsinfo_t);
903	strncpy(psinfo.pr_fname, p->p_comm, MAXCOMLEN);
904	/* XXX - We don't fill in the command line arguments properly yet. */
905	strncpy(psinfo.pr_psargs, p->p_comm, PRARGSZ);
906
907	/* Fill in the header. */
908	bzero(hdr, hdrsize);
909	off = 0;
910	elf_puthdr(p, hdr, &off, &status, &fpregset, &psinfo, numsegs);
911
912	/* Write it to the core file. */
913	return vn_rdwr(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
914	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p);
915}
916
917static void
918elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status,
919    const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
920{
921	size_t ehoff;
922	size_t phoff;
923	size_t noteoff;
924	size_t notesz;
925
926	ehoff = *off;
927	*off += sizeof(Elf_Ehdr);
928
929	phoff = *off;
930	*off += (numsegs + 1) * sizeof(Elf_Phdr);
931
932	noteoff = *off;
933	elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
934	    sizeof *status);
935	elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
936	    sizeof *fpregset);
937	elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
938	    sizeof *psinfo);
939	notesz = *off - noteoff;
940
941	/* Align up to a page boundary for the program segments. */
942	*off = round_page(*off);
943
944	if (dst != NULL) {
945		Elf_Ehdr *ehdr;
946		Elf_Phdr *phdr;
947		struct phdr_closure phc;
948
949		/*
950		 * Fill in the ELF header.
951		 */
952		ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
953		ehdr->e_ident[EI_MAG0] = ELFMAG0;
954		ehdr->e_ident[EI_MAG1] = ELFMAG1;
955		ehdr->e_ident[EI_MAG2] = ELFMAG2;
956		ehdr->e_ident[EI_MAG3] = ELFMAG3;
957		ehdr->e_ident[EI_CLASS] = ELF_CLASS;
958		ehdr->e_ident[EI_DATA] = ELF_DATA;
959		ehdr->e_ident[EI_VERSION] = EV_CURRENT;
960		ehdr->e_ident[EI_PAD] = 0;
961		strncpy(ehdr->e_ident + EI_BRAND, "FreeBSD",
962		    EI_NIDENT - EI_BRAND);
963		ehdr->e_type = ET_CORE;
964		ehdr->e_machine = ELF_ARCH;
965		ehdr->e_version = EV_CURRENT;
966		ehdr->e_entry = 0;
967		ehdr->e_phoff = phoff;
968		ehdr->e_flags = 0;
969		ehdr->e_ehsize = sizeof(Elf_Ehdr);
970		ehdr->e_phentsize = sizeof(Elf_Phdr);
971		ehdr->e_phnum = numsegs + 1;
972		ehdr->e_shentsize = sizeof(Elf_Shdr);
973		ehdr->e_shnum = 0;
974		ehdr->e_shstrndx = SHN_UNDEF;
975
976		/*
977		 * Fill in the program header entries.
978		 */
979		phdr = (Elf_Phdr *)((char *)dst + phoff);
980
981		/* The note segement. */
982		phdr->p_type = PT_NOTE;
983		phdr->p_offset = noteoff;
984		phdr->p_vaddr = 0;
985		phdr->p_paddr = 0;
986		phdr->p_filesz = notesz;
987		phdr->p_memsz = 0;
988		phdr->p_flags = 0;
989		phdr->p_align = 0;
990		phdr++;
991
992		/* All the writable segments from the program. */
993		phc.phdr = phdr;
994		phc.offset = *off;
995		each_writable_segment(p, cb_put_phdr, &phc);
996	}
997}
998
999static void
1000elf_putnote(void *dst, size_t *off, const char *name, int type,
1001    const void *desc, size_t descsz)
1002{
1003	Elf_Note note;
1004
1005	note.n_namesz = strlen(name) + 1;
1006	note.n_descsz = descsz;
1007	note.n_type = type;
1008	if (dst != NULL)
1009		bcopy(&note, (char *)dst + *off, sizeof note);
1010	*off += sizeof note;
1011	if (dst != NULL)
1012		bcopy(name, (char *)dst + *off, note.n_namesz);
1013	*off += roundup2(note.n_namesz, sizeof(Elf_Size));
1014	if (dst != NULL)
1015		bcopy(desc, (char *)dst + *off, note.n_descsz);
1016	*off += roundup2(note.n_descsz, sizeof(Elf_Size));
1017}
1018
1019/*
1020 * Tell kern_execve.c about it, with a little help from the linker.
1021 * Since `const' objects end up in the text segment, TEXT_SET is the
1022 * correct directive to use.
1023 */
1024static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1025EXEC_SET(elf, elf_execsw);
1026