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