]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/imgact_elf.c
This commit was generated by cvs2svn to compensate for changes in r93968,
[FreeBSD/FreeBSD.git] / sys / kern / imgact_elf.c
1 /*-
2  * Copyright (c) 2000 David O'Brien
3  * Copyright (c) 1995-1996 Søren Schmidt
4  * Copyright (c) 1996 Peter Wemm
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software withough specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #include <sys/param.h>
34 #include <sys/exec.h>
35 #include <sys/fcntl.h>
36 #include <sys/imgact.h>
37 #include <sys/imgact_elf.h>
38 #include <sys/kernel.h>
39 #include <sys/lock.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.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/systm.h>
49 #include <sys/signalvar.h>
50 #include <sys/stat.h>
51 #include <sys/sx.h>
52 #include <sys/syscall.h>
53 #include <sys/sysctl.h>
54 #include <sys/sysent.h>
55 #include <sys/vnode.h>
56
57 #include <vm/vm.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_param.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_map.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_extern.h>
64
65 #include <machine/elf.h>
66 #include <machine/md_var.h>
67
68 #define OLD_EI_BRAND    8
69
70 __ElfType(Brandinfo);
71 __ElfType(Auxargs);
72
73 static int elf_check_header(const Elf_Ehdr *hdr);
74 static int elf_freebsd_fixup(register_t **stack_base,
75     struct image_params *imgp);
76 static int elf_load_file(struct proc *p, const char *file, u_long *addr,
77     u_long *entry);
78 static int elf_load_section(struct proc *p,
79     struct vmspace *vmspace, struct vnode *vp,
80     vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
81     vm_prot_t prot);
82 static int exec_elf_imgact(struct image_params *imgp);
83
84 static int elf_trace = 0;
85 SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
86
87 struct sysentvec elf_freebsd_sysvec = {
88         SYS_MAXSYSCALL,
89         sysent,
90         0,
91         0,
92         0,
93         0,
94         0,
95         0,
96         elf_freebsd_fixup,
97         sendsig,
98         sigcode,
99         &szsigcode,
100         0,
101         "FreeBSD ELF",
102         elf_coredump,
103         NULL,
104         MINSIGSTKSZ
105 };
106
107 static Elf_Brandinfo freebsd_brand_info = {
108                                                 ELFOSABI_FREEBSD,
109                                                 "FreeBSD",
110                                                 "",
111                                                 "/usr/libexec/ld-elf.so.1",
112                                                 &elf_freebsd_sysvec
113                                           };
114 static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
115                                                         &freebsd_brand_info,
116                                                         NULL, NULL, NULL,
117                                                         NULL, NULL, NULL, NULL
118                                                     };
119
120 int
121 elf_insert_brand_entry(Elf_Brandinfo *entry)
122 {
123         int i;
124
125         for (i=1; i<MAX_BRANDS; i++) {
126                 if (elf_brand_list[i] == NULL) {
127                         elf_brand_list[i] = entry;
128                         break;
129                 }
130         }
131         if (i == MAX_BRANDS)
132                 return -1;
133         return 0;
134 }
135
136 int
137 elf_remove_brand_entry(Elf_Brandinfo *entry)
138 {
139         int i;
140
141         for (i=1; i<MAX_BRANDS; i++) {
142                 if (elf_brand_list[i] == entry) {
143                         elf_brand_list[i] = NULL;
144                         break;
145                 }
146         }
147         if (i == MAX_BRANDS)
148                 return -1;
149         return 0;
150 }
151
152 int
153 elf_brand_inuse(Elf_Brandinfo *entry)
154 {
155         struct proc *p;
156         int rval = FALSE;
157
158         sx_slock(&allproc_lock);
159         LIST_FOREACH(p, &allproc, p_list) {
160                 if (p->p_sysent == entry->sysvec) {
161                         rval = TRUE;
162                         break;
163                 }
164         }
165         sx_sunlock(&allproc_lock);
166
167         return (rval);
168 }
169
170 static int
171 elf_check_header(const Elf_Ehdr *hdr)
172 {
173         if (!IS_ELF(*hdr) ||
174             hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
175             hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
176             hdr->e_ident[EI_VERSION] != EV_CURRENT)
177                 return ENOEXEC;
178
179         if (!ELF_MACHINE_OK(hdr->e_machine))
180                 return ENOEXEC;
181
182         if (hdr->e_version != ELF_TARG_VER)
183                 return ENOEXEC;
184         
185         return 0;
186 }
187
188 static int
189 elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot)
190 {
191         size_t map_len;
192         vm_offset_t map_addr;
193         int error, rv;
194         size_t copy_len;
195         vm_object_t object;
196         vm_offset_t file_addr;
197         vm_offset_t data_buf = 0;
198
199         GIANT_REQUIRED;
200
201         VOP_GETVOBJECT(vp, &object);
202         error = 0;
203
204         /*
205          * It's necessary to fail if the filsz + offset taken from the
206          * header is greater than the actual file pager object's size.
207          * If we were to allow this, then the vm_map_find() below would
208          * walk right off the end of the file object and into the ether.
209          *
210          * While I'm here, might as well check for something else that
211          * is invalid: filsz cannot be greater than memsz.
212          */
213         if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size ||
214             filsz > memsz) {
215                 uprintf("elf_load_section: truncated ELF file\n");
216                 return (ENOEXEC);
217         }
218
219         map_addr = trunc_page((vm_offset_t)vmaddr);
220         file_addr = trunc_page(offset);
221
222         /*
223          * We have two choices.  We can either clear the data in the last page
224          * of an oversized mapping, or we can start the anon mapping a page
225          * early and copy the initialized data into that first page.  We
226          * choose the second..
227          */
228         if (memsz > filsz)
229                 map_len = trunc_page(offset+filsz) - file_addr;
230         else
231                 map_len = round_page(offset+filsz) - file_addr;
232
233         if (map_len != 0) {
234                 vm_object_reference(object);
235                 vm_map_lock(&vmspace->vm_map);
236                 rv = vm_map_insert(&vmspace->vm_map,
237                                       object,
238                                       file_addr,        /* file offset */
239                                       map_addr,         /* virtual start */
240                                       map_addr + map_len,/* virtual end */
241                                       prot,
242                                       VM_PROT_ALL,
243                                       MAP_COPY_ON_WRITE | MAP_PREFAULT);
244                 vm_map_unlock(&vmspace->vm_map);
245                 if (rv != KERN_SUCCESS) {
246                         vm_object_deallocate(object);
247                         return EINVAL;
248                 }
249
250                 /* we can stop now if we've covered it all */
251                 if (memsz == filsz) {
252                         return 0;
253                 }
254         }
255
256
257         /*
258          * We have to get the remaining bit of the file into the first part
259          * of the oversized map segment.  This is normally because the .data
260          * segment in the file is extended to provide bss.  It's a neat idea
261          * to try and save a page, but it's a pain in the behind to implement.
262          */
263         copy_len = (offset + filsz) - trunc_page(offset + filsz);
264         map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
265         map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
266
267         /* This had damn well better be true! */
268         if (map_len != 0) {
269                 vm_map_lock(&vmspace->vm_map);
270                 rv = vm_map_insert(&vmspace->vm_map, NULL, 0,
271                                         map_addr, map_addr + map_len,
272                                         VM_PROT_ALL, VM_PROT_ALL, 0);
273                 vm_map_unlock(&vmspace->vm_map);
274                 if (rv != KERN_SUCCESS) {
275                         return EINVAL; 
276                 }
277         }
278
279         if (copy_len != 0) {
280                 vm_object_reference(object);
281                 rv = vm_map_find(exec_map,
282                                  object, 
283                                  trunc_page(offset + filsz),
284                                  &data_buf,
285                                  PAGE_SIZE,
286                                  TRUE,
287                                  VM_PROT_READ,
288                                  VM_PROT_ALL,
289                                  MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL);
290                 if (rv != KERN_SUCCESS) {
291                         vm_object_deallocate(object);
292                         return EINVAL;
293                 }
294
295                 /* send the page fragment to user space */
296                 error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len);
297                 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE);
298                 if (error) {
299                         return (error);
300                 }
301         }
302
303         /*
304          * set it to the specified protection
305          */
306         vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len,  prot,
307                        FALSE);
308
309         return error;
310 }
311
312 /*
313  * Load the file "file" into memory.  It may be either a shared object
314  * or an executable.
315  *
316  * The "addr" reference parameter is in/out.  On entry, it specifies
317  * the address where a shared object should be loaded.  If the file is
318  * an executable, this value is ignored.  On exit, "addr" specifies
319  * where the file was actually loaded.
320  *
321  * The "entry" reference parameter is out only.  On exit, it specifies
322  * the entry point for the loaded file.
323  */
324 static int
325 elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
326 {
327         struct {
328                 struct nameidata nd;
329                 struct vattr attr;
330                 struct image_params image_params;
331         } *tempdata;
332         const Elf_Ehdr *hdr = NULL;
333         const Elf_Phdr *phdr = NULL;
334         struct nameidata *nd;
335         struct vmspace *vmspace = p->p_vmspace;
336         struct vattr *attr;
337         struct image_params *imgp;
338         vm_prot_t prot;
339         u_long rbase;
340         u_long base_addr = 0;
341         int error, i, numsegs;
342
343         if (curthread->td_proc != p)
344                 panic("elf_load_file - thread");        /* XXXKSE DIAGNOSTIC */
345
346         tempdata = malloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
347         nd = &tempdata->nd;
348         attr = &tempdata->attr;
349         imgp = &tempdata->image_params;
350
351         /*
352          * Initialize part of the common data
353          */
354         imgp->proc = p;
355         imgp->uap = NULL;
356         imgp->attr = attr;
357         imgp->firstpage = NULL;
358         imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE);
359
360         if (imgp->image_header == NULL) {
361                 nd->ni_vp = NULL;
362                 error = ENOMEM;
363                 goto fail;
364         }
365
366         /* XXXKSE */
367         NDINIT(nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, curthread);   
368                          
369         if ((error = namei(nd)) != 0) {
370                 nd->ni_vp = NULL;
371                 goto fail;
372         }
373         NDFREE(nd, NDF_ONLY_PNBUF);
374         imgp->vp = nd->ni_vp;
375
376         /*
377          * Check permissions, modes, uid, etc on the file, and "open" it.
378          */
379         error = exec_check_permissions(imgp);
380         if (error) {
381                 VOP_UNLOCK(nd->ni_vp, 0, curthread); /* XXXKSE */
382                 goto fail;
383         }
384
385         error = exec_map_first_page(imgp);
386         /*
387          * Also make certain that the interpreter stays the same, so set
388          * its VTEXT flag, too.
389          */
390         if (error == 0)
391                 nd->ni_vp->v_flag |= VTEXT;
392         VOP_UNLOCK(nd->ni_vp, 0, curthread); /* XXXKSE */
393         if (error)
394                 goto fail;
395
396         hdr = (const Elf_Ehdr *)imgp->image_header;
397         if ((error = elf_check_header(hdr)) != 0)
398                 goto fail;
399         if (hdr->e_type == ET_DYN)
400                 rbase = *addr;
401         else if (hdr->e_type == ET_EXEC)
402                 rbase = 0;
403         else {
404                 error = ENOEXEC;
405                 goto fail;
406         }
407
408         /* Only support headers that fit within first page for now */
409         if ((hdr->e_phoff > PAGE_SIZE) ||
410             (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
411                 error = ENOEXEC;
412                 goto fail;
413         }
414
415         phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
416
417         for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
418                 if (phdr[i].p_type == PT_LOAD) {        /* Loadable segment */
419                         prot = 0;
420                         if (phdr[i].p_flags & PF_X)
421                                 prot |= VM_PROT_EXECUTE;
422                         if (phdr[i].p_flags & PF_W)
423                                 prot |= VM_PROT_WRITE;
424                         if (phdr[i].p_flags & PF_R)
425                                 prot |= VM_PROT_READ;
426
427                         if ((error = elf_load_section(p, vmspace, nd->ni_vp,
428                                                      phdr[i].p_offset,
429                                                      (caddr_t)phdr[i].p_vaddr +
430                                                         rbase,
431                                                      phdr[i].p_memsz,
432                                                      phdr[i].p_filesz, prot)) != 0)
433                                 goto fail;
434                         /*
435                          * Establish the base address if this is the
436                          * first segment.
437                          */
438                         if (numsegs == 0)
439                                 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
440                         numsegs++;
441                 }
442         }
443         *addr = base_addr;
444         *entry=(unsigned long)hdr->e_entry + rbase;
445
446 fail:
447         if (imgp->firstpage)
448                 exec_unmap_first_page(imgp);
449         if (imgp->image_header)
450                 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header,
451                         PAGE_SIZE);
452         if (nd->ni_vp)
453                 vrele(nd->ni_vp);
454
455         free(tempdata, M_TEMP);
456
457         return error;
458 }
459
460 /*
461  * non static, as it can be overridden by start_init()
462  */
463 #ifdef __ia64__
464 int fallback_elf_brand = ELFOSABI_FREEBSD;
465 #else
466 int fallback_elf_brand = -1;
467 #endif
468 SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
469                 &fallback_elf_brand, -1,
470                 "ELF brand of last resort");
471
472 static int
473 exec_elf_imgact(struct image_params *imgp)
474 {
475         const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
476         const Elf_Phdr *phdr;
477         Elf_Auxargs *elf_auxargs = NULL;
478         struct vmspace *vmspace;
479         vm_prot_t prot;
480         u_long text_size = 0, data_size = 0;
481         u_long text_addr = 0, data_addr = 0;
482         u_long addr, entry = 0, proghdr = 0;
483         int error, i;
484         const char *interp = NULL;
485         Elf_Brandinfo *brand_info;
486         char *path;
487
488         GIANT_REQUIRED;
489
490         /*
491          * Do we have a valid ELF header ?
492          */
493         if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC)
494                 return -1;
495
496         /*
497          * From here on down, we return an errno, not -1, as we've
498          * detected an ELF file.
499          */
500
501         if ((hdr->e_phoff > PAGE_SIZE) ||
502             (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
503                 /* Only support headers in first page for now */
504                 return ENOEXEC;
505         }
506         phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
507         
508         /*
509          * From this point on, we may have resources that need to be freed.
510          */
511
512         /*
513          * Yeah, I'm paranoid.  There is every reason in the world to get
514          * VTEXT now since from here on out, there are places we can have
515          * a context switch.  Better safe than sorry; I really don't want
516          * the file to change while it's being loaded.
517          */
518         mtx_lock(&imgp->vp->v_interlock);
519         imgp->vp->v_flag |= VTEXT;
520         mtx_unlock(&imgp->vp->v_interlock);
521
522         if ((error = exec_extract_strings(imgp)) != 0)
523                 goto fail;
524
525         exec_new_vmspace(imgp);
526
527         vmspace = imgp->proc->p_vmspace;
528
529         for (i = 0; i < hdr->e_phnum; i++) {
530                 switch(phdr[i].p_type) {
531
532                 case PT_LOAD:   /* Loadable segment */
533                         prot = 0;
534                         if (phdr[i].p_flags & PF_X)
535                                 prot |= VM_PROT_EXECUTE;
536                         if (phdr[i].p_flags & PF_W)
537                                 prot |= VM_PROT_WRITE;
538                         if (phdr[i].p_flags & PF_R)
539                                 prot |= VM_PROT_READ;
540
541                         if ((error = elf_load_section(imgp->proc,
542                                                      vmspace, imgp->vp,
543                                                      phdr[i].p_offset,
544                                                      (caddr_t)phdr[i].p_vaddr,
545                                                      phdr[i].p_memsz,
546                                                      phdr[i].p_filesz, prot)) != 0)
547                                 goto fail;
548
549                         /*
550                          * Is this .text or .data ??
551                          *
552                          * We only handle one each of those yet XXX
553                          */
554                         if (hdr->e_entry >= phdr[i].p_vaddr &&
555                         hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) {
556                                 text_addr = trunc_page(phdr[i].p_vaddr);
557                                 text_size = round_page(phdr[i].p_memsz +
558                                                        phdr[i].p_vaddr -
559                                                        text_addr);
560                                 entry = (u_long)hdr->e_entry;
561                         } else {
562                                 data_addr = trunc_page(phdr[i].p_vaddr);
563                                 data_size = round_page(phdr[i].p_memsz +
564                                                        phdr[i].p_vaddr -
565                                                        data_addr);
566                         }
567                         break;
568                 case PT_INTERP: /* Path to interpreter */
569                         if (phdr[i].p_filesz > MAXPATHLEN ||
570                             phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
571                                 error = ENOEXEC;
572                                 goto fail;
573                         }
574                         interp = imgp->image_header + phdr[i].p_offset;
575                         break;
576                 case PT_PHDR:   /* Program header table info */
577                         proghdr = phdr[i].p_vaddr;
578                         break;
579                 default:
580                         break;
581                 }
582         }
583
584         vmspace->vm_tsize = text_size >> PAGE_SHIFT;
585         vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
586         vmspace->vm_dsize = data_size >> PAGE_SHIFT;
587         vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
588
589         addr = ELF_RTLD_ADDR(vmspace);
590
591         imgp->entry_addr = entry;
592
593         brand_info = NULL;
594
595         /* We support three types of branding -- (1) the ELF EI_OSABI field
596          * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
597          * branding w/in the ELF header, and (3) path of the `interp_path'
598          * field.  We should also look for an ".note.ABI-tag" ELF section now
599          * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
600          */
601
602         /* If the executable has a brand, search for it in the brand list. */
603         if (brand_info == NULL) {
604                 for (i = 0;  i < MAX_BRANDS;  i++) {
605                         Elf_Brandinfo *bi = elf_brand_list[i];
606
607                         if (bi != NULL && 
608                             (hdr->e_ident[EI_OSABI] == bi->brand
609                             || 0 == 
610                             strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND], 
611                             bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
612                                 brand_info = bi;
613                                 break;
614                         }
615                 }
616         }
617
618         /* Lacking a known brand, search for a recognized interpreter. */
619         if (brand_info == NULL && interp != NULL) {
620                 for (i = 0;  i < MAX_BRANDS;  i++) {
621                         Elf_Brandinfo *bi = elf_brand_list[i];
622
623                         if (bi != NULL &&
624                             strcmp(interp, bi->interp_path) == 0) {
625                                 brand_info = bi;
626                                 break;
627                         }
628                 }
629         }
630
631         /* Lacking a recognized interpreter, try the default brand */
632         if (brand_info == NULL) {
633                 for (i = 0; i < MAX_BRANDS; i++) {
634                         Elf_Brandinfo *bi = elf_brand_list[i];
635
636                         if (bi != NULL && fallback_elf_brand == bi->brand) {
637                                 brand_info = bi;
638                                 break;
639                         }
640                 }
641         }
642
643         if (brand_info == NULL) {
644                 uprintf("ELF binary type \"%u\" not known.\n",
645                     hdr->e_ident[EI_OSABI]);
646                 error = ENOEXEC;
647                 goto fail;
648         }
649
650         imgp->proc->p_sysent = brand_info->sysvec;
651         if (interp != NULL) {
652                 path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
653                 snprintf(path, MAXPATHLEN, "%s%s",
654                          brand_info->emul_path, interp);
655                 if ((error = elf_load_file(imgp->proc, path, &addr,
656                                            &imgp->entry_addr)) != 0) {
657                         if ((error = elf_load_file(imgp->proc, interp, &addr,
658                                                    &imgp->entry_addr)) != 0) {
659                                 uprintf("ELF interpreter %s not found\n", path);
660                                 free(path, M_TEMP);
661                                 goto fail;
662                         }
663                 }
664                 free(path, M_TEMP);
665         }
666
667         /*
668          * Construct auxargs table (used by the fixup routine)
669          */
670         elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
671         elf_auxargs->execfd = -1;
672         elf_auxargs->phdr = proghdr;
673         elf_auxargs->phent = hdr->e_phentsize;
674         elf_auxargs->phnum = hdr->e_phnum;
675         elf_auxargs->pagesz = PAGE_SIZE;
676         elf_auxargs->base = addr;
677         elf_auxargs->flags = 0;
678         elf_auxargs->entry = entry;
679         elf_auxargs->trace = elf_trace;
680
681         imgp->auxargs = elf_auxargs;
682         imgp->interpreted = 0;
683
684 fail:
685         return error;
686 }
687
688 static int
689 elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
690 {
691         Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
692         register_t *pos;
693
694         pos = *stack_base + (imgp->argc + imgp->envc + 2);
695
696         if (args->trace) {
697                 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
698         }
699         if (args->execfd != -1) {
700                 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
701         }
702         AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
703         AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
704         AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
705         AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
706         AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
707         AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
708         AUXARGS_ENTRY(pos, AT_BASE, args->base);
709         AUXARGS_ENTRY(pos, AT_NULL, 0);
710
711         free(imgp->auxargs, M_TEMP);
712         imgp->auxargs = NULL;
713
714         (*stack_base)--;
715         suword(*stack_base, (long) imgp->argc);
716         return 0;
717
718
719 /*
720  * Code for generating ELF core dumps.
721  */
722
723 typedef void (*segment_callback)(vm_map_entry_t, void *);
724
725 /* Closure for cb_put_phdr(). */
726 struct phdr_closure {
727         Elf_Phdr *phdr;         /* Program header to fill in */
728         Elf_Off offset;         /* Offset of segment in core file */
729 };
730
731 /* Closure for cb_size_segment(). */
732 struct sseg_closure {
733         int count;              /* Count of writable segments. */
734         size_t size;            /* Total size of all writable segments. */
735 };
736
737 static void cb_put_phdr(vm_map_entry_t, void *);
738 static void cb_size_segment(vm_map_entry_t, void *);
739 static void each_writable_segment(struct proc *, segment_callback, void *);
740 static int elf_corehdr(struct thread *, struct vnode *, struct ucred *,
741     int, void *, size_t);
742 static void elf_puthdr(struct proc *, void *, size_t *,
743     const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int);
744 static void elf_putnote(void *, size_t *, const char *, int,
745     const void *, size_t);
746
747 extern int osreldate;
748
749 int
750 elf_coredump(td, vp, limit)
751         struct thread *td;
752         register struct vnode *vp;
753         off_t limit;
754 {
755         register struct proc *p = td->td_proc;
756         register struct ucred *cred = td->td_ucred;
757         int error = 0;
758         struct sseg_closure seginfo;
759         void *hdr;
760         size_t hdrsize;
761
762         /* Size the program segments. */
763         seginfo.count = 0;
764         seginfo.size = 0;
765         each_writable_segment(p, cb_size_segment, &seginfo);
766
767         /*
768          * Calculate the size of the core file header area by making
769          * a dry run of generating it.  Nothing is written, but the
770          * size is calculated.
771          */
772         hdrsize = 0;
773         elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize,
774             (const prstatus_t *)NULL, (const prfpregset_t *)NULL,
775             (const prpsinfo_t *)NULL, seginfo.count);
776
777         if (hdrsize + seginfo.size >= limit)
778                 return (EFAULT);
779
780         /*
781          * Allocate memory for building the header, fill it up,
782          * and write it out.
783          */
784         hdr = malloc(hdrsize, M_TEMP, M_WAITOK);
785         if (hdr == NULL) {
786                 return EINVAL;
787         }
788         error = elf_corehdr(td, vp, cred, seginfo.count, hdr, hdrsize);
789
790         /* Write the contents of all of the writable segments. */
791         if (error == 0) {
792                 Elf_Phdr *php;
793                 off_t offset;
794                 int i;
795
796                 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1;
797                 offset = hdrsize;
798                 for (i = 0;  i < seginfo.count;  i++) {
799                         error = vn_rdwr_inchunks(UIO_WRITE, vp, 
800                             (caddr_t)php->p_vaddr,
801                             php->p_filesz, offset, UIO_USERSPACE,
802                             IO_UNIT | IO_DIRECT, cred, (int *)NULL, curthread); /* XXXKSE */
803                         if (error != 0)
804                                 break;
805                         offset += php->p_filesz;
806                         php++;
807                 }
808         }
809         free(hdr, M_TEMP);
810         
811         return error;
812 }
813
814 /*
815  * A callback for each_writable_segment() to write out the segment's
816  * program header entry.
817  */
818 static void
819 cb_put_phdr(entry, closure)
820         vm_map_entry_t entry;
821         void *closure;
822 {
823         struct phdr_closure *phc = (struct phdr_closure *)closure;
824         Elf_Phdr *phdr = phc->phdr;
825
826         phc->offset = round_page(phc->offset);
827
828         phdr->p_type = PT_LOAD;
829         phdr->p_offset = phc->offset;
830         phdr->p_vaddr = entry->start;
831         phdr->p_paddr = 0;
832         phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
833         phdr->p_align = PAGE_SIZE;
834         phdr->p_flags = 0;
835         if (entry->protection & VM_PROT_READ)
836                 phdr->p_flags |= PF_R;
837         if (entry->protection & VM_PROT_WRITE)
838                 phdr->p_flags |= PF_W;
839         if (entry->protection & VM_PROT_EXECUTE)
840                 phdr->p_flags |= PF_X;
841
842         phc->offset += phdr->p_filesz;
843         phc->phdr++;
844 }
845
846 /*
847  * A callback for each_writable_segment() to gather information about
848  * the number of segments and their total size.
849  */
850 static void
851 cb_size_segment(entry, closure)
852         vm_map_entry_t entry;
853         void *closure;
854 {
855         struct sseg_closure *ssc = (struct sseg_closure *)closure;
856
857         ssc->count++;
858         ssc->size += entry->end - entry->start;
859 }
860
861 /*
862  * For each writable segment in the process's memory map, call the given
863  * function with a pointer to the map entry and some arbitrary
864  * caller-supplied data.
865  */
866 static void
867 each_writable_segment(p, func, closure)
868         struct proc *p;
869         segment_callback func;
870         void *closure;
871 {
872         vm_map_t map = &p->p_vmspace->vm_map;
873         vm_map_entry_t entry;
874
875         for (entry = map->header.next;  entry != &map->header;
876             entry = entry->next) {
877                 vm_object_t obj;
878
879                 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) ||
880                     (entry->protection & (VM_PROT_READ|VM_PROT_WRITE)) !=
881                     (VM_PROT_READ|VM_PROT_WRITE))
882                         continue;
883
884                 /*
885                 ** Dont include memory segment in the coredump if
886                 ** MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
887                 ** madvise(2).
888                 */
889                 if (entry->eflags & MAP_ENTRY_NOCOREDUMP)
890                         continue;
891
892                 if ((obj = entry->object.vm_object) == NULL)
893                         continue;
894
895                 /* Find the deepest backing object. */
896                 while (obj->backing_object != NULL)
897                         obj = obj->backing_object;
898
899                 /* Ignore memory-mapped devices and such things. */
900                 if (obj->type != OBJT_DEFAULT &&
901                     obj->type != OBJT_SWAP &&
902                     obj->type != OBJT_VNODE)
903                         continue;
904
905                 (*func)(entry, closure);
906         }
907 }
908
909 /*
910  * Write the core file header to the file, including padding up to
911  * the page boundary.
912  */
913 static int
914 elf_corehdr(td, vp, cred, numsegs, hdr, hdrsize)
915         struct thread *td;
916         struct vnode *vp;
917         struct ucred *cred;
918         int numsegs;
919         size_t hdrsize;
920         void *hdr;
921 {
922         struct {
923                 prstatus_t status;
924                 prfpregset_t fpregset;
925                 prpsinfo_t psinfo;
926         } *tempdata;
927         struct proc *p = td->td_proc;
928         size_t off;
929         prstatus_t *status;
930         prfpregset_t *fpregset;
931         prpsinfo_t *psinfo;
932
933         tempdata = malloc(sizeof(*tempdata), M_TEMP, M_ZERO | M_WAITOK);
934         status = &tempdata->status;
935         fpregset = &tempdata->fpregset;
936         psinfo = &tempdata->psinfo;
937
938         /* Gather the information for the header. */
939         status->pr_version = PRSTATUS_VERSION;
940         status->pr_statussz = sizeof(prstatus_t);
941         status->pr_gregsetsz = sizeof(gregset_t);
942         status->pr_fpregsetsz = sizeof(fpregset_t);
943         status->pr_osreldate = osreldate;
944         status->pr_cursig = p->p_sig;
945         status->pr_pid = p->p_pid;
946         fill_regs(td, &status->pr_reg);
947
948         fill_fpregs(td, fpregset);
949
950         psinfo->pr_version = PRPSINFO_VERSION;
951         psinfo->pr_psinfosz = sizeof(prpsinfo_t);
952         strncpy(psinfo->pr_fname, p->p_comm, sizeof(psinfo->pr_fname) - 1);
953
954         /* XXX - We don't fill in the command line arguments properly yet. */
955         strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
956
957         /* Fill in the header. */
958         bzero(hdr, hdrsize);
959         off = 0;
960         elf_puthdr(p, hdr, &off, status, fpregset, psinfo, numsegs);
961
962         free(tempdata, M_TEMP);
963
964         /* Write it to the core file. */
965         return vn_rdwr_inchunks(UIO_WRITE, vp, hdr, hdrsize, (off_t)0,
966             UIO_SYSSPACE, IO_UNIT | IO_DIRECT, cred, NULL, td); /* XXXKSE */
967 }
968
969 static void
970 elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status,
971     const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs)
972 {
973         size_t ehoff;
974         size_t phoff;
975         size_t noteoff;
976         size_t notesz;
977
978         ehoff = *off;
979         *off += sizeof(Elf_Ehdr);
980
981         phoff = *off;
982         *off += (numsegs + 1) * sizeof(Elf_Phdr);
983
984         noteoff = *off;
985         elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status,
986             sizeof *status);
987         elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset,
988             sizeof *fpregset);
989         elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo,
990             sizeof *psinfo);
991         notesz = *off - noteoff;
992
993         /* Align up to a page boundary for the program segments. */
994         *off = round_page(*off);
995
996         if (dst != NULL) {
997                 Elf_Ehdr *ehdr;
998                 Elf_Phdr *phdr;
999                 struct phdr_closure phc;
1000
1001                 /*
1002                  * Fill in the ELF header.
1003                  */
1004                 ehdr = (Elf_Ehdr *)((char *)dst + ehoff);
1005                 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1006                 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1007                 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1008                 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1009                 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1010                 ehdr->e_ident[EI_DATA] = ELF_DATA;
1011                 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1012                 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD;
1013                 ehdr->e_ident[EI_ABIVERSION] = 0;
1014                 ehdr->e_ident[EI_PAD] = 0;
1015                 ehdr->e_type = ET_CORE;
1016                 ehdr->e_machine = ELF_ARCH;
1017                 ehdr->e_version = EV_CURRENT;
1018                 ehdr->e_entry = 0;
1019                 ehdr->e_phoff = phoff;
1020                 ehdr->e_flags = 0;
1021                 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1022                 ehdr->e_phentsize = sizeof(Elf_Phdr);
1023                 ehdr->e_phnum = numsegs + 1;
1024                 ehdr->e_shentsize = sizeof(Elf_Shdr);
1025                 ehdr->e_shnum = 0;
1026                 ehdr->e_shstrndx = SHN_UNDEF;
1027
1028                 /*
1029                  * Fill in the program header entries.
1030                  */
1031                 phdr = (Elf_Phdr *)((char *)dst + phoff);
1032
1033                 /* The note segement. */
1034                 phdr->p_type = PT_NOTE;
1035                 phdr->p_offset = noteoff;
1036                 phdr->p_vaddr = 0;
1037                 phdr->p_paddr = 0;
1038                 phdr->p_filesz = notesz;
1039                 phdr->p_memsz = 0;
1040                 phdr->p_flags = 0;
1041                 phdr->p_align = 0;
1042                 phdr++;
1043
1044                 /* All the writable segments from the program. */
1045                 phc.phdr = phdr;
1046                 phc.offset = *off;
1047                 each_writable_segment(p, cb_put_phdr, &phc);
1048         }
1049 }
1050
1051 static void
1052 elf_putnote(void *dst, size_t *off, const char *name, int type,
1053     const void *desc, size_t descsz)
1054 {
1055         Elf_Note note;
1056
1057         note.n_namesz = strlen(name) + 1;
1058         note.n_descsz = descsz;
1059         note.n_type = type;
1060         if (dst != NULL)
1061                 bcopy(&note, (char *)dst + *off, sizeof note);
1062         *off += sizeof note;
1063         if (dst != NULL)
1064                 bcopy(name, (char *)dst + *off, note.n_namesz);
1065         *off += roundup2(note.n_namesz, sizeof(Elf_Size));
1066         if (dst != NULL)
1067                 bcopy(desc, (char *)dst + *off, note.n_descsz);
1068         *off += roundup2(note.n_descsz, sizeof(Elf_Size));
1069 }
1070
1071 /*
1072  * Tell kern_execve.c about it, with a little help from the linker.
1073  */
1074 static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1075 EXEC_SET(elf, elf_execsw);