elf - Fill sv_name with the appropiate value for each arch.
[dragonfly.git] / sys / kern / imgact_elf.c
CommitLineData
984263bc 1/*-
08d72226 2 * Copyright (c) 1995-1996 Søren Schmidt
984263bc
MD
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
4d9022e3 16 * derived from this software without specific prior written permission
984263bc
MD
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 * $FreeBSD: src/sys/kern/imgact_elf.c,v 1.73.2.13 2002/12/28 19:49:41 dillon Exp $
d913b2fa 30 * $DragonFly: src/sys/kern/imgact_elf.c,v 1.55 2008/08/17 17:21:36 nth Exp $
984263bc
MD
31 */
32
33#include <sys/param.h>
34#include <sys/exec.h>
35#include <sys/fcntl.h>
731100e5 36#include <sys/file.h>
984263bc
MD
37#include <sys/imgact.h>
38#include <sys/imgact_elf.h>
39#include <sys/kernel.h>
40#include <sys/malloc.h>
41#include <sys/mman.h>
dadab5e9
MD
42#include <sys/systm.h>
43#include <sys/proc.h>
fad57d0e 44#include <sys/nlookup.h>
984263bc 45#include <sys/pioctl.h>
984263bc
MD
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>
984263bc 53#include <sys/vnode.h>
8ba5f7ef 54#include <sys/eventhandler.h>
984263bc 55
5c5185ae
SG
56#include <cpu/lwbuf.h>
57
984263bc
MD
58#include <vm/vm.h>
59#include <vm/vm_kern.h>
60#include <vm/vm_param.h>
61#include <vm/pmap.h>
62#include <sys/lock.h>
63#include <vm/vm_map.h>
64#include <vm/vm_object.h>
65#include <vm/vm_extern.h>
66
67#include <machine/elf.h>
68#include <machine/md_var.h>
731100e5
MD
69#include <sys/mount.h>
70#include <sys/ckpt.h>
984263bc
MD
71#define OLD_EI_BRAND 8
72
73__ElfType(Brandinfo);
74__ElfType(Auxargs);
75
402ed7e1
RG
76static int elf_check_header (const Elf_Ehdr *hdr);
77static int elf_freebsd_fixup (register_t **stack_base,
78 struct image_params *imgp);
79static int elf_load_file (struct proc *p, const char *file, u_long *addr,
80 u_long *entry);
81static int elf_load_section (struct proc *p,
984263bc
MD
82 struct vmspace *vmspace, struct vnode *vp,
83 vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz,
402ed7e1
RG
84 vm_prot_t prot);
85static int exec_elf_imgact (struct image_params *imgp);
984263bc
MD
86
87static int elf_trace = 0;
88SYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, "");
89static int elf_legacy_coredump = 0;
90SYSCTL_INT(_debug, OID_AUTO, elf_legacy_coredump, CTLFLAG_RW,
91 &elf_legacy_coredump, 0, "");
92
8d0415e1
JS
93static int dragonfly_match_abi_note(const Elf_Note *);
94static int freebsd_match_abi_note(const Elf_Note *);
95
984263bc
MD
96static struct sysentvec elf_freebsd_sysvec = {
97 SYS_MAXSYSCALL,
98 sysent,
8ec60c3f 99 -1,
984263bc
MD
100 0,
101 0,
102 0,
103 0,
104 0,
105 elf_freebsd_fixup,
106 sendsig,
107 sigcode,
108 &szsigcode,
109 0,
1799f7b1
AHJ
110#if defined(__x86_64__)
111 "FreeBSD ELF64",
112#else
113 "FreeBSD ELF32",
114#endif
984263bc
MD
115 elf_coredump,
116 NULL,
117 MINSIGSTKSZ
118};
119
120static Elf_Brandinfo freebsd_brand_info = {
121 ELFOSABI_FREEBSD,
122 "FreeBSD",
8d0415e1 123 freebsd_match_abi_note,
984263bc
MD
124 "",
125 "/usr/libexec/ld-elf.so.1",
126 &elf_freebsd_sysvec
127 };
8d0415e1
JS
128
129static Elf_Brandinfo dragonfly_brand_info = {
130 ELFOSABI_NONE,
131 "DragonFly",
132 dragonfly_match_abi_note,
133 "",
134 "/usr/libexec/ld-elf.so.2",
135 &elf_freebsd_sysvec
136 };
137
984263bc 138static Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = {
8d0415e1 139 &dragonfly_brand_info,
984263bc
MD
140 &freebsd_brand_info,
141 NULL, NULL, NULL,
8d0415e1 142 NULL, NULL, NULL
984263bc
MD
143 };
144
8d0415e1
JS
145static int
146freebsd_match_abi_note(const Elf_Note *abi_note)
147{
148 const char *abi_name = (const char *)
149 ((const uint8_t *)abi_note + sizeof(*abi_note));
150
151 if (abi_note->n_namesz != sizeof("FreeBSD"))
152 return(FALSE);
153 if (memcmp(abi_name, "FreeBSD", sizeof("FreeBSD")))
154 return(FALSE);
155 return(TRUE);
156}
157
158static int
159dragonfly_match_abi_note(const Elf_Note *abi_note)
160{
161 const char *abi_name = (const char *)
162 ((const uint8_t *)abi_note + sizeof(*abi_note));
163
164 if (abi_note->n_namesz != sizeof("DragonFly"))
165 return(FALSE);
166 if (memcmp(abi_name, "DragonFly", sizeof("DragonFly")))
167 return(FALSE);
168 return(TRUE);
169}
170
984263bc
MD
171int
172elf_insert_brand_entry(Elf_Brandinfo *entry)
173{
174 int i;
175
176 for (i=1; i<MAX_BRANDS; i++) {
177 if (elf_brand_list[i] == NULL) {
178 elf_brand_list[i] = entry;
179 break;
180 }
181 }
182 if (i == MAX_BRANDS)
183 return -1;
184 return 0;
185}
186
187int
188elf_remove_brand_entry(Elf_Brandinfo *entry)
189{
190 int i;
191
192 for (i=1; i<MAX_BRANDS; i++) {
193 if (elf_brand_list[i] == entry) {
194 elf_brand_list[i] = NULL;
195 break;
196 }
197 }
198 if (i == MAX_BRANDS)
199 return -1;
200 return 0;
201}
202
0e5797fe
MD
203/*
204 * Check if an elf brand is being used anywhere in the system.
205 *
7bd34050 206 * Used by the linux emulation module unloader. This isn't safe from
0e5797fe
MD
207 * races.
208 */
209struct elf_brand_inuse_info {
210 int rval;
211 Elf_Brandinfo *entry;
212};
213
214static int elf_brand_inuse_callback(struct proc *p, void *data);
215
984263bc
MD
216int
217elf_brand_inuse(Elf_Brandinfo *entry)
218{
0e5797fe 219 struct elf_brand_inuse_info info;
984263bc 220
0e5797fe
MD
221 info.rval = FALSE;
222 info.entry = entry;
223 allproc_scan(elf_brand_inuse_callback, entry);
224 return (info.rval);
225}
226
227static
228int
229elf_brand_inuse_callback(struct proc *p, void *data)
230{
231 struct elf_brand_inuse_info *info = data;
984263bc 232
0e5797fe
MD
233 if (p->p_sysent == info->entry->sysvec) {
234 info->rval = TRUE;
235 return(-1);
236 }
237 return(0);
984263bc
MD
238}
239
240static int
241elf_check_header(const Elf_Ehdr *hdr)
242{
243 if (!IS_ELF(*hdr) ||
244 hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
245 hdr->e_ident[EI_DATA] != ELF_TARG_DATA ||
2cc7c579
DR
246 hdr->e_ident[EI_VERSION] != EV_CURRENT ||
247 hdr->e_phentsize != sizeof(Elf_Phdr) ||
248 hdr->e_ehsize != sizeof(Elf_Ehdr) ||
249 hdr->e_version != ELF_TARG_VER)
984263bc
MD
250 return ENOEXEC;
251
252 if (!ELF_MACHINE_OK(hdr->e_machine))
253 return ENOEXEC;
254
984263bc
MD
255 return 0;
256}
257
7c34d798
SS
258static Elf_Brandinfo *
259elf_check_abi_note(struct image_params *imgp, const Elf_Phdr *ph)
260{
261 Elf_Brandinfo *match = NULL;
262 const Elf_Note *tmp_note;
5c5185ae 263 struct lwbuf *lwb;
7c34d798
SS
264 const char *page;
265 char *data = NULL;
266 Elf_Off off;
267 size_t firstoff;
268 size_t len;
269 size_t firstlen;
270
271 len = ph->p_filesz;
272 off = ph->p_offset;
273
274 firstoff = off & PAGE_MASK;
275 firstlen = PAGE_SIZE - firstoff;
276
277 if (len < sizeof(Elf_Note) || len > PAGE_SIZE)
278 return NULL; /* ENOEXEC? */
279
5c5185ae 280 if (exec_map_page(imgp, off >> PAGE_SHIFT, &lwb, &page))
7c34d798
SS
281 return NULL;
282
283 /*
284 * Crosses page boundary? Is that allowed?
285 */
286 if (firstlen < len) {
287 data = kmalloc(len, M_TEMP, M_WAITOK);
288
289 bcopy(page + firstoff, data, firstlen);
290
5c5185ae
SG
291 exec_unmap_page(lwb);
292 if (exec_map_page(imgp, (off >> PAGE_SHIFT) + 1, &lwb, &page)) {
7c34d798
SS
293 kfree(data, M_TEMP);
294 return NULL;
295 }
43297079 296 bcopy(page, data + firstlen, len - firstlen);
7c34d798
SS
297 tmp_note = (void *)data;
298 } else {
299 tmp_note = (const void *)(page + firstoff);
300 }
301
302 while (len >= sizeof(Elf_Note)) {
303 int i;
304 size_t nlen = roundup(tmp_note->n_namesz, sizeof(Elf_Word)) +
305 roundup(tmp_note->n_descsz, sizeof(Elf_Word)) +
306 sizeof(Elf_Note);
307
308 if (nlen > len)
309 break;
310
311 if (tmp_note->n_type != 1)
312 goto next;
313
314 for (i = 0; i < MAX_BRANDS; i++) {
315 Elf_Brandinfo *bi = elf_brand_list[i];
316
317 if (bi != NULL && bi->match_abi_note != NULL &&
318 bi->match_abi_note(tmp_note)) {
319 match = bi;
320 break;
321 }
322 }
323
324 if (match != NULL)
325 break;
326
327next:
328 len -= nlen;
329 tmp_note += nlen;
330 }
331
332 if (data != NULL)
333 kfree(data, M_TEMP);
5c5185ae 334 exec_unmap_page(lwb);
7c34d798
SS
335
336 return (match);
337}
338
984263bc 339static int
731100e5 340elf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp,
57f7b636
MD
341 vm_offset_t offset, caddr_t vmaddr, size_t memsz,
342 size_t filsz, vm_prot_t prot)
984263bc
MD
343{
344 size_t map_len;
345 vm_offset_t map_addr;
346 int error, rv, cow;
a108bf71 347 int count;
984263bc
MD
348 size_t copy_len;
349 vm_object_t object;
350 vm_offset_t file_addr;
984263bc 351
7540ab49 352 object = vp->v_object;
984263bc
MD
353 error = 0;
354
355 /*
356 * It's necessary to fail if the filsz + offset taken from the
357 * header is greater than the actual file pager object's size.
358 * If we were to allow this, then the vm_map_find() below would
359 * walk right off the end of the file object and into the ether.
360 *
361 * While I'm here, might as well check for something else that
362 * is invalid: filsz cannot be greater than memsz.
363 */
57f7b636 364 if ((off_t)filsz + offset > vp->v_filesize || filsz > memsz) {
984263bc
MD
365 uprintf("elf_load_section: truncated ELF file\n");
366 return (ENOEXEC);
367 }
368
369 map_addr = trunc_page((vm_offset_t)vmaddr);
370 file_addr = trunc_page(offset);
371
372 /*
373 * We have two choices. We can either clear the data in the last page
374 * of an oversized mapping, or we can start the anon mapping a page
375 * early and copy the initialized data into that first page. We
376 * choose the second..
377 */
378 if (memsz > filsz)
379 map_len = trunc_page(offset+filsz) - file_addr;
380 else
381 map_len = round_page(offset+filsz) - file_addr;
382
383 if (map_len != 0) {
384 vm_object_reference(object);
385
386 /* cow flags: don't dump readonly sections in core */
387 cow = MAP_COPY_ON_WRITE | MAP_PREFAULT |
388 (prot & VM_PROT_WRITE ? 0 : MAP_DISABLE_COREDUMP);
389
a108bf71 390 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc 391 vm_map_lock(&vmspace->vm_map);
a108bf71 392 rv = vm_map_insert(&vmspace->vm_map, &count,
984263bc
MD
393 object,
394 file_addr, /* file offset */
395 map_addr, /* virtual start */
396 map_addr + map_len,/* virtual end */
1b874851
MD
397 VM_MAPTYPE_NORMAL,
398 prot, VM_PROT_ALL,
984263bc
MD
399 cow);
400 vm_map_unlock(&vmspace->vm_map);
a108bf71 401 vm_map_entry_release(count);
984263bc
MD
402 if (rv != KERN_SUCCESS) {
403 vm_object_deallocate(object);
404 return EINVAL;
405 }
406
407 /* we can stop now if we've covered it all */
408 if (memsz == filsz) {
409 return 0;
410 }
411 }
412
413
414 /*
415 * We have to get the remaining bit of the file into the first part
416 * of the oversized map segment. This is normally because the .data
417 * segment in the file is extended to provide bss. It's a neat idea
418 * to try and save a page, but it's a pain in the behind to implement.
419 */
420 copy_len = (offset + filsz) - trunc_page(offset + filsz);
421 map_addr = trunc_page((vm_offset_t)vmaddr + filsz);
422 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr;
423
424 /* This had damn well better be true! */
425 if (map_len != 0) {
a108bf71 426 count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
984263bc 427 vm_map_lock(&vmspace->vm_map);
a108bf71
MD
428 rv = vm_map_insert(&vmspace->vm_map, &count,
429 NULL, 0,
984263bc 430 map_addr, map_addr + map_len,
1b874851
MD
431 VM_MAPTYPE_NORMAL,
432 VM_PROT_ALL, VM_PROT_ALL,
433 0);
984263bc 434 vm_map_unlock(&vmspace->vm_map);
a108bf71 435 vm_map_entry_release(count);
984263bc
MD
436 if (rv != KERN_SUCCESS) {
437 return EINVAL;
438 }
439 }
440
441 if (copy_len != 0) {
18b3f457 442 vm_page_t m;
5c5185ae 443 struct lwbuf *lwb;
18b3f457
MD
444
445 m = vm_fault_object_page(object, trunc_page(offset + filsz),
446 VM_PROT_READ, 0, &error);
447 if (m) {
5c5185ae
SG
448 lwb = lwbuf_alloc(m);
449 error = copyout((caddr_t)lwbuf_kva(lwb),
18b3f457 450 (caddr_t)map_addr, copy_len);
5c5185ae 451 lwbuf_free(lwb);
18b3f457 452 vm_page_unhold(m);
984263bc 453 }
984263bc
MD
454 if (error) {
455 return (error);
456 }
457 }
458
459 /*
460 * set it to the specified protection
461 */
462 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len, prot,
463 FALSE);
464
465 return error;
466}
467
468/*
469 * Load the file "file" into memory. It may be either a shared object
470 * or an executable.
471 *
472 * The "addr" reference parameter is in/out. On entry, it specifies
473 * the address where a shared object should be loaded. If the file is
474 * an executable, this value is ignored. On exit, "addr" specifies
475 * where the file was actually loaded.
476 *
477 * The "entry" reference parameter is out only. On exit, it specifies
478 * the entry point for the loaded file.
479 */
480static int
481elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry)
482{
483 struct {
fad57d0e 484 struct nlookupdata nd;
984263bc
MD
485 struct vattr attr;
486 struct image_params image_params;
487 } *tempdata;
488 const Elf_Ehdr *hdr = NULL;
489 const Elf_Phdr *phdr = NULL;
fad57d0e 490 struct nlookupdata *nd;
984263bc
MD
491 struct vmspace *vmspace = p->p_vmspace;
492 struct vattr *attr;
493 struct image_params *imgp;
494 vm_prot_t prot;
495 u_long rbase;
496 u_long base_addr = 0;
497 int error, i, numsegs;
498
efda3bd0 499 tempdata = kmalloc(sizeof(*tempdata), M_TEMP, M_WAITOK);
984263bc
MD
500 nd = &tempdata->nd;
501 attr = &tempdata->attr;
502 imgp = &tempdata->image_params;
503
504 /*
505 * Initialize part of the common data
506 */
507 imgp->proc = p;
984263bc
MD
508 imgp->attr = attr;
509 imgp->firstpage = NULL;
18f40545 510 imgp->image_header = NULL;
fad57d0e 511 imgp->vp = NULL;
984263bc 512
fad57d0e
MD
513 error = nlookup_init(nd, file, UIO_SYSSPACE, NLC_FOLLOW);
514 if (error == 0)
515 error = nlookup(nd);
516 if (error == 0)
28623bf9 517 error = cache_vget(&nd->nl_nch, nd->nl_cred, LK_EXCLUSIVE, &imgp->vp);
fad57d0e
MD
518 nlookup_done(nd);
519 if (error)
984263bc 520 goto fail;
984263bc
MD
521
522 /*
523 * Check permissions, modes, uid, etc on the file, and "open" it.
524 */
525 error = exec_check_permissions(imgp);
526 if (error) {
a11aaa81 527 vn_unlock(imgp->vp);
984263bc
MD
528 goto fail;
529 }
530
531 error = exec_map_first_page(imgp);
532 /*
533 * Also make certain that the interpreter stays the same, so set
534 * its VTEXT flag, too.
535 */
536 if (error == 0)
2247fe02 537 vsetflags(imgp->vp, VTEXT);
a11aaa81 538 vn_unlock(imgp->vp);
984263bc
MD
539 if (error)
540 goto fail;
541
542 hdr = (const Elf_Ehdr *)imgp->image_header;
543 if ((error = elf_check_header(hdr)) != 0)
544 goto fail;
545 if (hdr->e_type == ET_DYN)
546 rbase = *addr;
547 else if (hdr->e_type == ET_EXEC)
548 rbase = 0;
549 else {
550 error = ENOEXEC;
551 goto fail;
552 }
553
2cc7c579
DR
554 /* Only support headers that fit within first page for now
555 * (multiplication of two Elf_Half fields will not overflow) */
984263bc 556 if ((hdr->e_phoff > PAGE_SIZE) ||
2cc7c579 557 (hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE - hdr->e_phoff) {
984263bc
MD
558 error = ENOEXEC;
559 goto fail;
560 }
561
562 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff);
563
564 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) {
565 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */
566 prot = 0;
567 if (phdr[i].p_flags & PF_X)
568 prot |= VM_PROT_EXECUTE;
569 if (phdr[i].p_flags & PF_W)
570 prot |= VM_PROT_WRITE;
571 if (phdr[i].p_flags & PF_R)
572 prot |= VM_PROT_READ;
573
dadab5e9 574 error = elf_load_section(
fad57d0e 575 p, vmspace, imgp->vp,
dadab5e9
MD
576 phdr[i].p_offset,
577 (caddr_t)phdr[i].p_vaddr +
578 rbase,
579 phdr[i].p_memsz,
580 phdr[i].p_filesz, prot);
581 if (error != 0)
984263bc
MD
582 goto fail;
583 /*
584 * Establish the base address if this is the
585 * first segment.
586 */
587 if (numsegs == 0)
588 base_addr = trunc_page(phdr[i].p_vaddr + rbase);
589 numsegs++;
590 }
591 }
592 *addr = base_addr;
593 *entry=(unsigned long)hdr->e_entry + rbase;
594
595fail:
596 if (imgp->firstpage)
597 exec_unmap_first_page(imgp);
fad57d0e
MD
598 if (imgp->vp) {
599 vrele(imgp->vp);
600 imgp->vp = NULL;
601 }
efda3bd0 602 kfree(tempdata, M_TEMP);
984263bc
MD
603
604 return error;
605}
606
607/*
608 * non static, as it can be overridden by start_init()
609 */
610int fallback_elf_brand = -1;
611SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW,
612 &fallback_elf_brand, -1,
613 "ELF brand of last resort");
614
08d72226
AH
615static int can_exec_dyn = 1;
616SYSCTL_INT(_kern, OID_AUTO, elf_exec_dyn, CTLFLAG_RW,
617 &can_exec_dyn, 1,
618 "ELF: can exec shared libraries");
619
984263bc
MD
620static int
621exec_elf_imgact(struct image_params *imgp)
622{
623 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header;
624 const Elf_Phdr *phdr;
625 Elf_Auxargs *elf_auxargs = NULL;
626 struct vmspace *vmspace;
627 vm_prot_t prot;
628 u_long text_size = 0, data_size = 0, total_size = 0;
629 u_long text_addr = 0, data_addr = 0;
630 u_long seg_size, seg_addr;
631 u_long addr, entry = 0, proghdr = 0;
632 int error, i;
633 const char *interp = NULL;
8d0415e1 634 const Elf_Note *abi_note = NULL;
7c34d798 635 Elf_Brandinfo *brand_info = NULL;
984263bc
MD
636 char *path;
637
2bd9d75c
DRJ
638 error = 0;
639
984263bc
MD
640 /*
641 * Do we have a valid ELF header ?
08d72226 642 * We allow execution of ET_EXEC and, if kern.elf_exec_dyn is 1, ET_DYN.
984263bc 643 */
08d72226
AH
644 if (elf_check_header(hdr) != 0 ||
645 (hdr->e_type != ET_EXEC && (!can_exec_dyn || hdr->e_type != ET_DYN)))
984263bc
MD
646 return -1;
647
648 /*
649 * From here on down, we return an errno, not -1, as we've
650 * detected an ELF file.
651 */
652
653 if ((hdr->e_phoff > PAGE_SIZE) ||
654 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) {
655 /* Only support headers in first page for now */
656 return ENOEXEC;
657 }
658 phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff);
659
660 /*
661 * From this point on, we may have resources that need to be freed.
662 */
663
29802dbb 664 exec_new_vmspace(imgp, NULL);
984263bc
MD
665
666 /*
667 * Yeah, I'm paranoid. There is every reason in the world to get
668 * VTEXT now since from here on out, there are places we can have
669 * a context switch. Better safe than sorry; I really don't want
670 * the file to change while it's being loaded.
671 */
5fd012e0 672 vsetflags(imgp->vp, VTEXT);
984263bc
MD
673
674 vmspace = imgp->proc->p_vmspace;
675
676 for (i = 0; i < hdr->e_phnum; i++) {
677 switch(phdr[i].p_type) {
678
679 case PT_LOAD: /* Loadable segment */
680 prot = 0;
681 if (phdr[i].p_flags & PF_X)
682 prot |= VM_PROT_EXECUTE;
683 if (phdr[i].p_flags & PF_W)
684 prot |= VM_PROT_WRITE;
685 if (phdr[i].p_flags & PF_R)
686 prot |= VM_PROT_READ;
687
688 if ((error = elf_load_section(imgp->proc,
689 vmspace, imgp->vp,
690 phdr[i].p_offset,
691 (caddr_t)phdr[i].p_vaddr,
692 phdr[i].p_memsz,
693 phdr[i].p_filesz, prot)) != 0)
694 goto fail;
695
22cc9971
DX
696 /*
697 * If this segment contains the program headers,
698 * remember their virtual address for the AT_PHDR
699 * aux entry. Static binaries don't usually include
700 * a PT_PHDR entry.
701 */
702 if (phdr[i].p_offset == 0 &&
703 hdr->e_phoff + hdr->e_phnum * hdr->e_phentsize
704 <= phdr[i].p_filesz)
705 proghdr = phdr[i].p_vaddr + hdr->e_phoff;
706
984263bc
MD
707 seg_addr = trunc_page(phdr[i].p_vaddr);
708 seg_size = round_page(phdr[i].p_memsz +
709 phdr[i].p_vaddr - seg_addr);
710
711 /*
712 * Is this .text or .data? We can't use
713 * VM_PROT_WRITE or VM_PROT_EXEC, it breaks the
714 * alpha terribly and possibly does other bad
715 * things so we stick to the old way of figuring
716 * it out: If the segment contains the program
717 * entry point, it's a text segment, otherwise it
718 * is a data segment.
719 *
720 * Note that obreak() assumes that data_addr +
721 * data_size == end of data load area, and the ELF
722 * file format expects segments to be sorted by
723 * address. If multiple data segments exist, the
724 * last one will be used.
725 */
726 if (hdr->e_entry >= phdr[i].p_vaddr &&
727 hdr->e_entry < (phdr[i].p_vaddr +
728 phdr[i].p_memsz)) {
729 text_size = seg_size;
730 text_addr = seg_addr;
731 entry = (u_long)hdr->e_entry;
732 } else {
733 data_size = seg_size;
734 data_addr = seg_addr;
735 }
736 total_size += seg_size;
737
738 /*
739 * Check limits. It should be safe to check the
740 * limits after loading the segment since we do
741 * not actually fault in all the segment's pages.
742 */
743 if (data_size >
744 imgp->proc->p_rlimit[RLIMIT_DATA].rlim_cur ||
745 text_size > maxtsiz ||
746 total_size >
747 imgp->proc->p_rlimit[RLIMIT_VMEM].rlim_cur) {
748 error = ENOMEM;
749 goto fail;
750 }
751 break;
752 case PT_INTERP: /* Path to interpreter */
753 if (phdr[i].p_filesz > MAXPATHLEN ||
754 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) {
755 error = ENOEXEC;
756 goto fail;
757 }
758 interp = imgp->image_header + phdr[i].p_offset;
759 break;
8d0415e1 760 case PT_NOTE: /* Check for .note.ABI-tag */
7c34d798
SS
761 if (brand_info == NULL)
762 brand_info = elf_check_abi_note(imgp, &phdr[i]);
8d0415e1 763 break;
984263bc
MD
764 case PT_PHDR: /* Program header table info */
765 proghdr = phdr[i].p_vaddr;
766 break;
767 default:
768 break;
769 }
770 }
771
772 vmspace->vm_tsize = text_size >> PAGE_SHIFT;
773 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr;
774 vmspace->vm_dsize = data_size >> PAGE_SHIFT;
775 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr;
776
777 addr = ELF_RTLD_ADDR(vmspace);
778
779 imgp->entry_addr = entry;
780
984263bc
MD
781 /* We support three types of branding -- (1) the ELF EI_OSABI field
782 * that SCO added to the ELF spec, (2) FreeBSD 3.x's traditional string
783 * branding w/in the ELF header, and (3) path of the `interp_path'
784 * field. We should also look for an ".note.ABI-tag" ELF section now
785 * in all Linux ELF binaries, FreeBSD 4.1+, and some NetBSD ones.
786 */
787
788 /* If the executable has a brand, search for it in the brand list. */
8d0415e1 789 if (brand_info == NULL && hdr->e_ident[EI_OSABI] != ELFOSABI_NONE) {
984263bc
MD
790 for (i = 0; i < MAX_BRANDS; i++) {
791 Elf_Brandinfo *bi = elf_brand_list[i];
792
793 if (bi != NULL &&
794 (hdr->e_ident[EI_OSABI] == bi->brand
795 || 0 ==
796 strncmp((const char *)&hdr->e_ident[OLD_EI_BRAND],
797 bi->compat_3_brand, strlen(bi->compat_3_brand)))) {
798 brand_info = bi;
799 break;
800 }
801 }
802 }
803
8d0415e1
JS
804 /* Search for a recognized ABI. */
805 if (brand_info == NULL && abi_note != NULL) {
237770a0
JS
806 }
807
808 /*
809 * ELFOSABI_NONE == ELFOSABI_SYSV, so a SYSV binary misses all
810 * checks so far, since it is neither branded nor does it have
811 * an ABI note. If the EI_OSABI field is ELFOSABI_NONE, assume
812 * it is svr4 and look for an entry in the elf_brand_list with
813 * match_abi_note == NULL.
814 */
815 if (brand_info == NULL && hdr->e_ident[EI_OSABI] == ELFOSABI_NONE) {
816 for (i = 0; i < MAX_BRANDS; i++) {
817 Elf_Brandinfo *bi = elf_brand_list[i];
818
819 if (bi != NULL && bi->match_abi_note == NULL &&
820 ELFOSABI_SYSV == bi->brand) {
821 brand_info = bi;
8d0415e1
JS
822 break;
823 }
824 }
825 }
826
827 /* Lacking a recognized ABI, search for a recognized interpreter. */
984263bc
MD
828 if (brand_info == NULL && interp != NULL) {
829 for (i = 0; i < MAX_BRANDS; i++) {
830 Elf_Brandinfo *bi = elf_brand_list[i];
831
832 if (bi != NULL &&
833 strcmp(interp, bi->interp_path) == 0) {
834 brand_info = bi;
835 break;
836 }
837 }
838 }
839
840 /* Lacking a recognized interpreter, try the default brand */
841 if (brand_info == NULL) {
842 for (i = 0; i < MAX_BRANDS; i++) {
843 Elf_Brandinfo *bi = elf_brand_list[i];
844
845 if (bi != NULL && fallback_elf_brand == bi->brand) {
846 brand_info = bi;
847 break;
848 }
849 }
850 }
851
852 if (brand_info == NULL) {
853 uprintf("ELF binary type \"%u\" not known.\n",
854 hdr->e_ident[EI_OSABI]);
855 error = ENOEXEC;
856 goto fail;
7c03f43a 857 }
984263bc
MD
858
859 imgp->proc->p_sysent = brand_info->sysvec;
8ba5f7ef
AH
860 EVENTHANDLER_INVOKE(process_exec, imgp);
861
984263bc 862 if (interp != NULL) {
efda3bd0 863 path = kmalloc(MAXPATHLEN, M_TEMP, M_WAITOK);
f8c7a42d 864 ksnprintf(path, MAXPATHLEN, "%s%s",
984263bc
MD
865 brand_info->emul_path, interp);
866 if ((error = elf_load_file(imgp->proc, path, &addr,
867 &imgp->entry_addr)) != 0) {
868 if ((error = elf_load_file(imgp->proc, interp, &addr,
869 &imgp->entry_addr)) != 0) {
870 uprintf("ELF interpreter %s not found\n", path);
efda3bd0 871 kfree(path, M_TEMP);
984263bc
MD
872 goto fail;
873 }
874 }
efda3bd0 875 kfree(path, M_TEMP);
5347affc
MD
876 } else {
877 addr = 0;
984263bc
MD
878 }
879
880 /*
881 * Construct auxargs table (used by the fixup routine)
882 */
efda3bd0 883 elf_auxargs = kmalloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK);
984263bc
MD
884 elf_auxargs->execfd = -1;
885 elf_auxargs->phdr = proghdr;
886 elf_auxargs->phent = hdr->e_phentsize;
887 elf_auxargs->phnum = hdr->e_phnum;
888 elf_auxargs->pagesz = PAGE_SIZE;
889 elf_auxargs->base = addr;
890 elf_auxargs->flags = 0;
891 elf_auxargs->entry = entry;
892 elf_auxargs->trace = elf_trace;
893
894 imgp->auxargs = elf_auxargs;
895 imgp->interpreted = 0;
896
897fail:
898 return error;
899}
900
901static int
902elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp)
903{
904 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs;
905 register_t *pos;
906
2bd9d75c 907 pos = *stack_base + (imgp->args->argc + imgp->args->envc + 2);
984263bc
MD
908
909 if (args->trace) {
910 AUXARGS_ENTRY(pos, AT_DEBUG, 1);
911 }
912 if (args->execfd != -1) {
913 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd);
914 }
915 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr);
916 AUXARGS_ENTRY(pos, AT_PHENT, args->phent);
917 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum);
918 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz);
919 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags);
920 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry);
921 AUXARGS_ENTRY(pos, AT_BASE, args->base);
922 AUXARGS_ENTRY(pos, AT_NULL, 0);
923
efda3bd0 924 kfree(imgp->auxargs, M_TEMP);
984263bc
MD
925 imgp->auxargs = NULL;
926
927 (*stack_base)--;
2bd9d75c 928 suword(*stack_base, (long) imgp->args->argc);
984263bc
MD
929 return 0;
930}
931
932/*
933 * Code for generating ELF core dumps.
934 */
935
55e44363 936typedef int (*segment_callback) (vm_map_entry_t, void *);
984263bc
MD
937
938/* Closure for cb_put_phdr(). */
939struct phdr_closure {
55e44363
MD
940 Elf_Phdr *phdr; /* Program header to fill in (incremented) */
941 Elf_Phdr *phdr_max; /* Pointer bound for error check */
984263bc
MD
942 Elf_Off offset; /* Offset of segment in core file */
943};
944
945/* Closure for cb_size_segment(). */
946struct sseg_closure {
947 int count; /* Count of writable segments. */
55e44363 948 size_t vsize; /* Total size of all writable segments. */
984263bc
MD
949};
950
55e44363 951/* Closure for cb_put_fp(). */
731100e5
MD
952struct fp_closure {
953 struct vn_hdr *vnh;
55e44363 954 struct vn_hdr *vnh_max;
731100e5
MD
955 int count;
956 struct stat *sb;
957};
958
55e44363
MD
959typedef struct elf_buf {
960 char *buf;
961 size_t off;
962 size_t off_max;
963} *elf_buf_t;
731100e5 964
55e44363 965static void *target_reserve(elf_buf_t target, size_t bytes, int *error);
731100e5 966
55e44363
MD
967static int cb_put_phdr (vm_map_entry_t, void *);
968static int cb_size_segment (vm_map_entry_t, void *);
969static int cb_fpcount_segment(vm_map_entry_t, void *);
970static int cb_put_fp(vm_map_entry_t, void *);
984263bc 971
731100e5 972
55e44363 973static int each_segment (struct proc *, segment_callback, void *, int);
7d20a8ff 974static int elf_corehdr (struct lwp *, int, struct file *, struct ucred *,
55e44363 975 int, elf_buf_t);
d913b2fa
NT
976enum putmode { WRITE, DRYRUN };
977static int elf_puthdr (struct lwp *, elf_buf_t, int sig, enum putmode,
978 int, struct file *);
979static int elf_putallnotes(struct lwp *, elf_buf_t, int, enum putmode);
55e44363 980static int elf_putnote (elf_buf_t, const char *, int, const void *, size_t);
731100e5 981
7d20a8ff 982static int elf_putsigs(struct lwp *, elf_buf_t);
55e44363 983static int elf_puttextvp(struct proc *, elf_buf_t);
ff7a3478 984static int elf_putfiles(struct proc *, elf_buf_t, struct file *);
731100e5 985
984263bc
MD
986extern int osreldate;
987
988int
7d20a8ff 989elf_coredump(struct lwp *lp, int sig, struct vnode *vp, off_t limit)
984263bc 990{
731100e5
MD
991 struct file *fp;
992 int error;
993
994 if ((error = falloc(NULL, &fp, NULL)) != 0)
995 return (error);
7d20a8ff 996 fsetcred(fp, lp->lwp_proc->p_ucred);
731100e5 997
fad57d0e
MD
998 /*
999 * XXX fixme.
1000 */
fbb4eeab 1001 fp->f_type = DTYPE_VNODE;
731100e5 1002 fp->f_flag = O_CREAT|O_WRONLY|O_NOFOLLOW;
fad57d0e 1003 fp->f_ops = &vnode_fileops;
fbb4eeab 1004 fp->f_data = vp;
a11aaa81 1005 vn_unlock(vp);
731100e5 1006
7d20a8ff 1007 error = generic_elf_coredump(lp, sig, fp, limit);
731100e5 1008
fbb4eeab 1009 fp->f_type = 0;
731100e5
MD
1010 fp->f_flag = 0;
1011 fp->f_ops = &badfileops;
fbb4eeab 1012 fp->f_data = NULL;
9f87144f 1013 fdrop(fp);
731100e5
MD
1014 return (error);
1015}
1016
1017int
7d20a8ff 1018generic_elf_coredump(struct lwp *lp, int sig, struct file *fp, off_t limit)
731100e5 1019{
7d20a8ff 1020 struct proc *p = lp->lwp_proc;
dadab5e9 1021 struct ucred *cred = p->p_ucred;
984263bc
MD
1022 int error = 0;
1023 struct sseg_closure seginfo;
55e44363 1024 struct elf_buf target;
984263bc 1025
731100e5 1026 if (!fp)
6ea70f76 1027 kprintf("can't dump core - null fp\n");
55e44363
MD
1028
1029 /*
1030 * Size the program segments
1031 */
984263bc 1032 seginfo.count = 0;
55e44363 1033 seginfo.vsize = 0;
731100e5 1034 each_segment(p, cb_size_segment, &seginfo, 1);
984263bc
MD
1035
1036 /*
1037 * Calculate the size of the core file header area by making
1038 * a dry run of generating it. Nothing is written, but the
1039 * size is calculated.
1040 */
55e44363 1041 bzero(&target, sizeof(target));
d913b2fa 1042 elf_puthdr(lp, &target, sig, DRYRUN, seginfo.count, fp);
731100e5 1043
55e44363 1044 if (target.off + seginfo.vsize >= limit)
984263bc
MD
1045 return (EFAULT);
1046
1047 /*
1048 * Allocate memory for building the header, fill it up,
1049 * and write it out.
1050 */
55e44363
MD
1051 target.off_max = target.off;
1052 target.off = 0;
efda3bd0 1053 target.buf = kmalloc(target.off_max, M_TEMP, M_WAITOK|M_ZERO);
55e44363 1054
7d20a8ff 1055 error = elf_corehdr(lp, sig, fp, cred, seginfo.count, &target);
984263bc
MD
1056
1057 /* Write the contents of all of the writable segments. */
1058 if (error == 0) {
1059 Elf_Phdr *php;
984263bc 1060 int i;
a63bf9b6 1061 ssize_t nbytes;
984263bc 1062
55e44363
MD
1063 php = (Elf_Phdr *)(target.buf + sizeof(Elf_Ehdr)) + 1;
1064 for (i = 0; i < seginfo.count; i++) {
be7d8f4f 1065 error = fp_write(fp, (caddr_t)php->p_vaddr,
e7440b28 1066 php->p_filesz, &nbytes, UIO_USERSPACE);
984263bc
MD
1067 if (error != 0)
1068 break;
984263bc
MD
1069 php++;
1070 }
1071 }
efda3bd0 1072 kfree(target.buf, M_TEMP);
984263bc
MD
1073
1074 return error;
1075}
1076
1077/*
731100e5 1078 * A callback for each_segment() to write out the segment's
984263bc
MD
1079 * program header entry.
1080 */
55e44363 1081static int
731100e5 1082cb_put_phdr(vm_map_entry_t entry, void *closure)
984263bc 1083{
55e44363 1084 struct phdr_closure *phc = closure;
984263bc
MD
1085 Elf_Phdr *phdr = phc->phdr;
1086
55e44363
MD
1087 if (phc->phdr == phc->phdr_max)
1088 return EINVAL;
1089
984263bc
MD
1090 phc->offset = round_page(phc->offset);
1091
1092 phdr->p_type = PT_LOAD;
1093 phdr->p_offset = phc->offset;
1094 phdr->p_vaddr = entry->start;
1095 phdr->p_paddr = 0;
1096 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1097 phdr->p_align = PAGE_SIZE;
1098 phdr->p_flags = 0;
1099 if (entry->protection & VM_PROT_READ)
1100 phdr->p_flags |= PF_R;
1101 if (entry->protection & VM_PROT_WRITE)
1102 phdr->p_flags |= PF_W;
1103 if (entry->protection & VM_PROT_EXECUTE)
1104 phdr->p_flags |= PF_X;
1105
1106 phc->offset += phdr->p_filesz;
55e44363
MD
1107 ++phc->phdr;
1108 return 0;
984263bc
MD
1109}
1110
1111/*
1112 * A callback for each_writable_segment() to gather information about
1113 * the number of segments and their total size.
1114 */
55e44363 1115static int
731100e5 1116cb_size_segment(vm_map_entry_t entry, void *closure)
984263bc 1117{
55e44363 1118 struct sseg_closure *ssc = closure;
984263bc 1119
55e44363
MD
1120 ++ssc->count;
1121 ssc->vsize += entry->end - entry->start;
1122 return 0;
984263bc
MD
1123}
1124
1125/*
731100e5
MD
1126 * A callback for each_segment() to gather information about
1127 * the number of text segments.
1128 */
55e44363 1129static int
731100e5
MD
1130cb_fpcount_segment(vm_map_entry_t entry, void *closure)
1131{
55e44363 1132 int *count = closure;
12693083
MD
1133 struct vnode *vp;
1134
1135 if (entry->object.vm_object->type == OBJT_VNODE) {
1136 vp = (struct vnode *)entry->object.vm_object->handle;
1137 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1138 return 0;
731100e5 1139 ++*count;
12693083 1140 }
55e44363 1141 return 0;
731100e5
MD
1142}
1143
55e44363 1144static int
731100e5
MD
1145cb_put_fp(vm_map_entry_t entry, void *closure)
1146{
55e44363 1147 struct fp_closure *fpc = closure;
731100e5
MD
1148 struct vn_hdr *vnh = fpc->vnh;
1149 Elf_Phdr *phdr = &vnh->vnh_phdr;
1150 struct vnode *vp;
1151 int error;
1152
12693083
MD
1153 /*
1154 * If an entry represents a vnode then write out a file handle.
1155 *
1156 * If we are checkpointing a checkpoint-restored program we do
1157 * NOT record the filehandle for the old checkpoint vnode (which
1158 * is mapped all over the place). Instead we rely on the fact
1159 * that a checkpoint-restored program does not mmap() the checkpt
1160 * vnode NOCORE, so its contents will be written out to the
4f12bfd3
MD
1161 * new checkpoint file. This is necessary because the 'old'
1162 * checkpoint file is typically destroyed when a new one is created
1163 * and thus cannot be used to restore the new checkpoint.
1164 *
1165 * Theoretically we could create a chain of checkpoint files and
1166 * operate the checkpointing operation kinda like an incremental
1167 * checkpoint, but a checkpoint restore would then likely wind up
1168 * referencing many prior checkpoint files and that is a bit over
1169 * the top for the purpose of the checkpoint API.
12693083 1170 */
731100e5 1171 if (entry->object.vm_object->type == OBJT_VNODE) {
12693083
MD
1172 vp = (struct vnode *)entry->object.vm_object->handle;
1173 if ((vp->v_flag & VCKPT) && curproc->p_textvp == vp)
1174 return 0;
55e44363
MD
1175 if (vnh == fpc->vnh_max)
1176 return EINVAL;
55e44363
MD
1177
1178 if (vp->v_mount)
1179 vnh->vnh_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
731100e5 1180 error = VFS_VPTOFH(vp, &vnh->vnh_fh.fh_fid);
4fe2217f
MD
1181 if (error) {
1182 char *freepath, *fullpath;
1183
5b4cfb7e 1184 if (vn_fullpath(curproc, vp, &fullpath, &freepath, 0)) {
6ea70f76 1185 kprintf("Warning: coredump, error %d: cannot store file handle for vnode %p\n", error, vp);
4fe2217f 1186 } else {
6ea70f76 1187 kprintf("Warning: coredump, error %d: cannot store file handle for %s\n", error, fullpath);
efda3bd0 1188 kfree(freepath, M_TEMP);
4fe2217f
MD
1189 }
1190 error = 0;
1191 }
55e44363 1192
731100e5
MD
1193 phdr->p_type = PT_LOAD;
1194 phdr->p_offset = 0; /* not written to core */
1195 phdr->p_vaddr = entry->start;
1196 phdr->p_paddr = 0;
1197 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start;
1198 phdr->p_align = PAGE_SIZE;
1199 phdr->p_flags = 0;
1200 if (entry->protection & VM_PROT_READ)
1201 phdr->p_flags |= PF_R;
1202 if (entry->protection & VM_PROT_WRITE)
1203 phdr->p_flags |= PF_W;
1204 if (entry->protection & VM_PROT_EXECUTE)
1205 phdr->p_flags |= PF_X;
1206 ++fpc->vnh;
1207 ++fpc->count;
1208 }
55e44363 1209 return 0;
731100e5
MD
1210}
1211
731100e5 1212/*
984263bc
MD
1213 * For each writable segment in the process's memory map, call the given
1214 * function with a pointer to the map entry and some arbitrary
1215 * caller-supplied data.
1216 */
55e44363 1217static int
731100e5 1218each_segment(struct proc *p, segment_callback func, void *closure, int writable)
984263bc 1219{
55e44363 1220 int error = 0;
984263bc
MD
1221 vm_map_t map = &p->p_vmspace->vm_map;
1222 vm_map_entry_t entry;
1223
55e44363 1224 for (entry = map->header.next; error == 0 && entry != &map->header;
984263bc
MD
1225 entry = entry->next) {
1226 vm_object_t obj;
1227
1228 /*
1229 * Don't dump inaccessible mappings, deal with legacy
1230 * coredump mode.
1231 *
1232 * Note that read-only segments related to the elf binary
1233 * are marked MAP_ENTRY_NOCOREDUMP now so we no longer
1234 * need to arbitrarily ignore such segments.
1235 */
1236 if (elf_legacy_coredump) {
731100e5 1237 if (writable && (entry->protection & VM_PROT_RW) != VM_PROT_RW)
984263bc
MD
1238 continue;
1239 } else {
731100e5 1240 if (writable && (entry->protection & VM_PROT_ALL) == 0)
984263bc
MD
1241 continue;
1242 }
1243
1244 /*
1245 * Dont include memory segment in the coredump if
1246 * MAP_NOCORE is set in mmap(2) or MADV_NOCORE in
1b874851
MD
1247 * madvise(2).
1248 *
1249 * Currently we only dump normal VM object maps. We do
1250 * not dump submaps or virtual page tables.
984263bc 1251 */
1b874851
MD
1252 if (writable && (entry->eflags & MAP_ENTRY_NOCOREDUMP))
1253 continue;
1254 if (entry->maptype != VM_MAPTYPE_NORMAL)
984263bc 1255 continue;
984263bc
MD
1256 if ((obj = entry->object.vm_object) == NULL)
1257 continue;
1258
1259 /* Find the deepest backing object. */
1260 while (obj->backing_object != NULL)
1261 obj = obj->backing_object;
1262
1263 /* Ignore memory-mapped devices and such things. */
1264 if (obj->type != OBJT_DEFAULT &&
1265 obj->type != OBJT_SWAP &&
1266 obj->type != OBJT_VNODE)
1267 continue;
1268
55e44363 1269 error = (*func)(entry, closure);
984263bc 1270 }
55e44363
MD
1271 return error;
1272}
1273
1274static
1275void *
1276target_reserve(elf_buf_t target, size_t bytes, int *error)
1277{
1278 void *res = NULL;
1279
1280 if (target->buf) {
1281 if (target->off + bytes > target->off_max)
1282 *error = EINVAL;
1283 else
1284 res = target->buf + target->off;
1285 }
1286 target->off += bytes;
1287 return (res);
984263bc
MD
1288}
1289
1290/*
1291 * Write the core file header to the file, including padding up to
1292 * the page boundary.
1293 */
1294static int
d913b2fa
NT
1295elf_corehdr(struct lwp *lp, int sig, struct file *fp, struct ucred *cred,
1296 int numsegs, elf_buf_t target)
984263bc 1297{
55e44363 1298 int error;
a63bf9b6 1299 ssize_t nbytes;
7d20a8ff 1300
ff7a3478
MD
1301 /*
1302 * Fill in the header. The fp is passed so we can detect and flag
1303 * a checkpoint file pointer within the core file itself, because
1304 * it may not be restored from the same file handle.
1305 */
d913b2fa 1306 error = elf_puthdr(lp, target, sig, WRITE, numsegs, fp);
984263bc
MD
1307
1308 /* Write it to the core file. */
e7440b28
MD
1309 if (error == 0) {
1310 error = fp_write(fp, target->buf, target->off, &nbytes,
1311 UIO_SYSSPACE);
1312 }
55e44363 1313 return error;
984263bc
MD
1314}
1315
55e44363 1316static int
d913b2fa
NT
1317elf_puthdr(struct lwp *lp, elf_buf_t target, int sig, enum putmode mode,
1318 int numsegs, struct file *fp)
984263bc 1319{
7d20a8ff 1320 struct proc *p = lp->lwp_proc;
55e44363 1321 int error = 0;
984263bc
MD
1322 size_t phoff;
1323 size_t noteoff;
1324 size_t notesz;
55e44363
MD
1325 Elf_Ehdr *ehdr;
1326 Elf_Phdr *phdr;
984263bc 1327
55e44363
MD
1328 ehdr = target_reserve(target, sizeof(Elf_Ehdr), &error);
1329
1330 phoff = target->off;
1331 phdr = target_reserve(target, (numsegs + 1) * sizeof(Elf_Phdr), &error);
1332
1333 noteoff = target->off;
d913b2fa
NT
1334 if (error == 0)
1335 elf_putallnotes(lp, target, sig, mode);
55e44363 1336 notesz = target->off - noteoff;
984263bc 1337
55e44363
MD
1338 /*
1339 * put extra cruft for dumping process state here
1340 * - we really want it be before all the program
1341 * mappings
1342 * - we just need to update the offset accordingly
1343 * and GDB will be none the wiser.
1344 */
1345 if (error == 0)
1346 error = elf_puttextvp(p, target);
1347 if (error == 0)
7d20a8ff 1348 error = elf_putsigs(lp, target);
55e44363 1349 if (error == 0)
ff7a3478 1350 error = elf_putfiles(p, target, fp);
55e44363
MD
1351
1352 /*
1353 * Align up to a page boundary for the program segments. The
1354 * actual data will be written to the outptu file, not to elf_buf_t,
1355 * so we do not have to do any further bounds checking.
1356 */
1357 target->off = round_page(target->off);
1358 if (error == 0 && ehdr != NULL) {
984263bc
MD
1359 /*
1360 * Fill in the ELF header.
1361 */
984263bc
MD
1362 ehdr->e_ident[EI_MAG0] = ELFMAG0;
1363 ehdr->e_ident[EI_MAG1] = ELFMAG1;
1364 ehdr->e_ident[EI_MAG2] = ELFMAG2;
1365 ehdr->e_ident[EI_MAG3] = ELFMAG3;
1366 ehdr->e_ident[EI_CLASS] = ELF_CLASS;
1367 ehdr->e_ident[EI_DATA] = ELF_DATA;
1368 ehdr->e_ident[EI_VERSION] = EV_CURRENT;
64fce751 1369 ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
984263bc
MD
1370 ehdr->e_ident[EI_ABIVERSION] = 0;
1371 ehdr->e_ident[EI_PAD] = 0;
1372 ehdr->e_type = ET_CORE;
1373 ehdr->e_machine = ELF_ARCH;
1374 ehdr->e_version = EV_CURRENT;
1375 ehdr->e_entry = 0;
1376 ehdr->e_phoff = phoff;
1377 ehdr->e_flags = 0;
1378 ehdr->e_ehsize = sizeof(Elf_Ehdr);
1379 ehdr->e_phentsize = sizeof(Elf_Phdr);
1380 ehdr->e_phnum = numsegs + 1;
1381 ehdr->e_shentsize = sizeof(Elf_Shdr);
1382 ehdr->e_shnum = 0;
1383 ehdr->e_shstrndx = SHN_UNDEF;
55e44363
MD
1384 }
1385 if (error == 0 && phdr != NULL) {
984263bc
MD
1386 /*
1387 * Fill in the program header entries.
1388 */
55e44363 1389 struct phdr_closure phc;
984263bc
MD
1390
1391 /* The note segement. */
1392 phdr->p_type = PT_NOTE;
1393 phdr->p_offset = noteoff;
1394 phdr->p_vaddr = 0;
1395 phdr->p_paddr = 0;
1396 phdr->p_filesz = notesz;
1397 phdr->p_memsz = 0;
1398 phdr->p_flags = 0;
1399 phdr->p_align = 0;
55e44363 1400 ++phdr;
984263bc
MD
1401
1402 /* All the writable segments from the program. */
1403 phc.phdr = phdr;
55e44363
MD
1404 phc.phdr_max = phdr + numsegs;
1405 phc.offset = target->off;
731100e5 1406 each_segment(p, cb_put_phdr, &phc, 1);
984263bc 1407 }
55e44363 1408 return (error);
984263bc
MD
1409}
1410
d913b2fa
NT
1411/*
1412 * Append core dump notes to target ELF buffer or simply update target size
1413 * if dryrun selected.
1414 */
1415static int
1416elf_putallnotes(struct lwp *corelp, elf_buf_t target, int sig,
1417 enum putmode mode)
1418{
1419 struct proc *p = corelp->lwp_proc;
1420 int error;
1421 struct {
1422 prstatus_t status;
1423 prfpregset_t fpregs;
1424 prpsinfo_t psinfo;
1425 } *tmpdata;
1426 prstatus_t *status;
1427 prfpregset_t *fpregs;
1428 prpsinfo_t *psinfo;
1429 struct lwp *lp;
1430
1431 /*
1432 * Allocate temporary storage for notes on heap to avoid stack overflow.
1433 */
1434 if (mode != DRYRUN) {
1435 tmpdata = kmalloc(sizeof(*tmpdata), M_TEMP, M_ZERO | M_WAITOK);
1436 status = &tmpdata->status;
1437 fpregs = &tmpdata->fpregs;
1438 psinfo = &tmpdata->psinfo;
1439 } else {
1440 tmpdata = NULL;
1441 status = NULL;
1442 fpregs = NULL;
1443 psinfo = NULL;
1444 }
1445
1446 /*
1447 * Append LWP-agnostic note.
1448 */
1449 if (mode != DRYRUN) {
1450 psinfo->pr_version = PRPSINFO_VERSION;
1451 psinfo->pr_psinfosz = sizeof(prpsinfo_t);
1452 strncpy(psinfo->pr_fname, p->p_comm,
1453 sizeof(psinfo->pr_fname) - 1);
1454 /*
1455 * XXX - We don't fill in the command line arguments
1456 * properly yet.
1457 */
1458 strncpy(psinfo->pr_psargs, p->p_comm, PRARGSZ);
1459 }
1460 error =
64fce751 1461 elf_putnote(target, "CORE", NT_PRPSINFO, psinfo, sizeof *psinfo);
d913b2fa
NT
1462 if (error)
1463 goto exit;
1464
1465 /*
1466 * Append first note for LWP that triggered core so that it is
1467 * the selected one when the debugger starts.
1468 */
1469 if (mode != DRYRUN) {
1470 status->pr_version = PRSTATUS_VERSION;
1471 status->pr_statussz = sizeof(prstatus_t);
1472 status->pr_gregsetsz = sizeof(gregset_t);
1473 status->pr_fpregsetsz = sizeof(fpregset_t);
1474 status->pr_osreldate = osreldate;
1475 status->pr_cursig = sig;
1476 /*
1477 * XXX GDB needs unique pr_pid for each LWP and does not
1478 * not support pr_pid==0 but lwp_tid can be 0, so hack unique
1479 * value.
1480 */
64fce751 1481 status->pr_pid = corelp->lwp_tid;
d913b2fa
NT
1482 fill_regs(corelp, &status->pr_reg);
1483 fill_fpregs(corelp, fpregs);
1484 }
1485 error =
64fce751 1486 elf_putnote(target, "CORE", NT_PRSTATUS, status, sizeof *status);
d913b2fa
NT
1487 if (error)
1488 goto exit;
1489 error =
64fce751 1490 elf_putnote(target, "CORE", NT_FPREGSET, fpregs, sizeof *fpregs);
d913b2fa
NT
1491 if (error)
1492 goto exit;
1493
1494 /*
1495 * Then append notes for other LWPs.
1496 */
1497 FOREACH_LWP_IN_PROC(lp, p) {
1498 if (lp == corelp)
1499 continue;
1500 /* skip lwps being created */
1501 if (lp->lwp_thread == NULL)
1502 continue;
1503 if (mode != DRYRUN) {
64fce751 1504 status->pr_pid = lp->lwp_tid;
d913b2fa
NT
1505 fill_regs(lp, &status->pr_reg);
1506 fill_fpregs(lp, fpregs);
1507 }
64fce751 1508 error = elf_putnote(target, "CORE", NT_PRSTATUS,
d913b2fa
NT
1509 status, sizeof *status);
1510 if (error)
1511 goto exit;
64fce751 1512 error = elf_putnote(target, "CORE", NT_FPREGSET,
d913b2fa
NT
1513 fpregs, sizeof *fpregs);
1514 if (error)
1515 goto exit;
1516 }
1517
1518exit:
1519 if (tmpdata != NULL)
1520 kfree(tmpdata, M_TEMP);
1521 return (error);
1522}
1523
f606e7b4
MD
1524/*
1525 * Generate a note sub-structure.
1526 *
1527 * NOTE: 4-byte alignment.
1528 */
55e44363
MD
1529static int
1530elf_putnote(elf_buf_t target, const char *name, int type,
1531 const void *desc, size_t descsz)
984263bc 1532{
55e44363
MD
1533 int error = 0;
1534 char *dst;
984263bc
MD
1535 Elf_Note note;
1536
1537 note.n_namesz = strlen(name) + 1;
1538 note.n_descsz = descsz;
1539 note.n_type = type;
55e44363 1540 dst = target_reserve(target, sizeof(note), &error);
984263bc 1541 if (dst != NULL)
55e44363
MD
1542 bcopy(&note, dst, sizeof note);
1543 dst = target_reserve(target, note.n_namesz, &error);
984263bc 1544 if (dst != NULL)
55e44363 1545 bcopy(name, dst, note.n_namesz);
f606e7b4 1546 target->off = roundup2(target->off, sizeof(Elf_Word));
55e44363 1547 dst = target_reserve(target, note.n_descsz, &error);
984263bc 1548 if (dst != NULL)
55e44363 1549 bcopy(desc, dst, note.n_descsz);
f606e7b4 1550 target->off = roundup2(target->off, sizeof(Elf_Word));
55e44363 1551 return(error);
984263bc
MD
1552}
1553
731100e5 1554
55e44363 1555static int
7d20a8ff 1556elf_putsigs(struct lwp *lp, elf_buf_t target)
731100e5 1557{
7d20a8ff
SS
1558 /* XXX lwp handle more than one lwp */
1559 struct proc *p = lp->lwp_proc;
55e44363 1560 int error = 0;
731100e5 1561 struct ckpt_siginfo *csi;
55e44363
MD
1562
1563 csi = target_reserve(target, sizeof(struct ckpt_siginfo), &error);
1564 if (csi) {
1565 csi->csi_ckptpisz = sizeof(struct ckpt_siginfo);
b1b4e5a6 1566 bcopy(p->p_sigacts, &csi->csi_sigacts, sizeof(*p->p_sigacts));
55e44363 1567 bcopy(&p->p_realtimer, &csi->csi_itimerval, sizeof(struct itimerval));
7d20a8ff 1568 bcopy(&lp->lwp_sigmask, &csi->csi_sigmask,
08f2f1bb 1569 sizeof(sigset_t));
55e44363 1570 csi->csi_sigparent = p->p_sigparent;
731100e5 1571 }
55e44363 1572 return(error);
731100e5
MD
1573}
1574
55e44363 1575static int
ff7a3478 1576elf_putfiles(struct proc *p, elf_buf_t target, struct file *ckfp)
731100e5 1577{
55e44363
MD
1578 int error = 0;
1579 int i;
be7d8f4f 1580 struct ckpt_filehdr *cfh = NULL;
731100e5
MD
1581 struct ckpt_fileinfo *cfi;
1582 struct file *fp;
1583 struct vnode *vp;
1584 /*
1585 * the duplicated loop is gross, but it was the only way
1586 * to eliminate uninitialized variable warnings
1587 */
55e44363
MD
1588 cfh = target_reserve(target, sizeof(struct ckpt_filehdr), &error);
1589 if (cfh) {
731100e5 1590 cfh->cfh_nfiles = 0;
be7d8f4f 1591 }
be7d8f4f
MD
1592
1593 /*
4f12bfd3 1594 * ignore STDIN/STDERR/STDOUT.
be7d8f4f 1595 */
55e44363 1596 for (i = 3; error == 0 && i < p->p_fd->fd_nfiles; i++) {
228b401d
MD
1597 fp = holdfp(p->p_fd, i, -1);
1598 if (fp == NULL)
be7d8f4f 1599 continue;
4f12bfd3
MD
1600 /*
1601 * XXX Only checkpoint vnodes for now.
1602 */
228b401d
MD
1603 if (fp->f_type != DTYPE_VNODE) {
1604 fdrop(fp);
be7d8f4f 1605 continue;
228b401d 1606 }
4f12bfd3
MD
1607 cfi = target_reserve(target, sizeof(struct ckpt_fileinfo),
1608 &error);
228b401d
MD
1609 if (cfi == NULL) {
1610 fdrop(fp);
4f12bfd3 1611 continue;
228b401d 1612 }
4f12bfd3
MD
1613 cfi->cfi_index = -1;
1614 cfi->cfi_type = fp->f_type;
1615 cfi->cfi_flags = fp->f_flag;
1616 cfi->cfi_offset = fp->f_offset;
ff7a3478
MD
1617 cfi->cfi_ckflags = 0;
1618
1619 if (fp == ckfp)
1620 cfi->cfi_ckflags |= CKFIF_ISCKPTFD;
4f12bfd3
MD
1621 /* f_count and f_msgcount should not be saved/restored */
1622 /* XXX save cred info */
1623
1624 switch(fp->f_type) {
1625 case DTYPE_VNODE:
9ce25870 1626 vp = (struct vnode *)fp->f_data;
55e44363
MD
1627 /*
1628 * it looks like a bug in ptrace is marking
9ce25870
MD
1629 * a non-vnode as a vnode - until we find the
1630 * root cause this will at least prevent
1631 * further panics from truss
1632 */
55e44363 1633 if (vp == NULL || vp->v_mount == NULL)
4f12bfd3 1634 break;
be7d8f4f 1635 cfh->cfh_nfiles++;
be7d8f4f 1636 cfi->cfi_index = i;
be7d8f4f
MD
1637 cfi->cfi_fh.fh_fsid = vp->v_mount->mnt_stat.f_fsid;
1638 error = VFS_VPTOFH(vp, &cfi->cfi_fh.fh_fid);
4f12bfd3
MD
1639 break;
1640 default:
1641 break;
731100e5 1642 }
228b401d 1643 fdrop(fp);
731100e5 1644 }
55e44363 1645 return(error);
731100e5
MD
1646}
1647
55e44363
MD
1648static int
1649elf_puttextvp(struct proc *p, elf_buf_t target)
731100e5 1650{
55e44363 1651 int error = 0;
731100e5 1652 int *vn_count;
5a5c6af6
MD
1653 struct fp_closure fpc;
1654 struct ckpt_vminfo *vminfo;
1655
55e44363
MD
1656 vminfo = target_reserve(target, sizeof(struct ckpt_vminfo), &error);
1657 if (vminfo != NULL) {
5a5c6af6
MD
1658 vminfo->cvm_dsize = p->p_vmspace->vm_dsize;
1659 vminfo->cvm_tsize = p->p_vmspace->vm_tsize;
1660 vminfo->cvm_daddr = p->p_vmspace->vm_daddr;
1661 vminfo->cvm_taddr = p->p_vmspace->vm_taddr;
731100e5 1662 }
5a5c6af6 1663
731100e5 1664 fpc.count = 0;
55e44363
MD
1665 vn_count = target_reserve(target, sizeof(int), &error);
1666 if (target->buf != NULL) {
1667 fpc.vnh = (struct vn_hdr *)(target->buf + target->off);
1668 fpc.vnh_max = fpc.vnh +
1669 (target->off_max - target->off) / sizeof(struct vn_hdr);
1670 error = each_segment(p, cb_put_fp, &fpc, 0);
1671 if (vn_count)
1672 *vn_count = fpc.count;
5a5c6af6 1673 } else {
55e44363 1674 error = each_segment(p, cb_fpcount_segment, &fpc.count, 0);
5a5c6af6 1675 }
55e44363
MD
1676 target->off += fpc.count * sizeof(struct vn_hdr);
1677 return(error);
731100e5
MD
1678}
1679
1680
984263bc
MD
1681/*
1682 * Tell kern_execve.c about it, with a little help from the linker.
1683 */
1684static struct execsw elf_execsw = {exec_elf_imgact, "ELF"};
1843e3f2 1685EXEC_SET_ORDERED(elf, elf_execsw, SI_ORDER_FIRST);