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