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