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