imgact_elf.c revision 63768
1195379Ssam/*- 2195379Ssam * Copyright (c) 1995-1996 S�ren Schmidt 3195379Ssam * Copyright (c) 1996 Peter Wemm 4195379Ssam * All rights reserved. 5195379Ssam * 6195379Ssam * Redistribution and use in source and binary forms, with or without 7195379Ssam * modification, are permitted provided that the following conditions 8195379Ssam * are met: 9195379Ssam * 1. Redistributions of source code must retain the above copyright 10195379Ssam * notice, this list of conditions and the following disclaimer 11195379Ssam * in this position and unchanged. 12195379Ssam * 2. Redistributions in binary form must reproduce the above copyright 13195379Ssam * notice, this list of conditions and the following disclaimer in the 14195379Ssam * documentation and/or other materials provided with the distribution. 15195379Ssam * 3. The name of the author may not be used to endorse or promote products 16195379Ssam * derived from this software withough specific prior written permission 17195379Ssam * 18195379Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19195379Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20195379Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21195379Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22195379Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23195379Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24195379Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25195379Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26195379Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27195379Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28195379Ssam * 29195379Ssam * $FreeBSD: head/sys/kern/imgact_elf.c 63768 2000-07-23 06:49:46Z green $ 30195379Ssam */ 31195379Ssam 32195379Ssam#include "opt_rlimit.h" 33195379Ssam 34195379Ssam#include <sys/param.h> 35195379Ssam#include <sys/exec.h> 36195379Ssam#include <sys/fcntl.h> 37295126Sglebius#include <sys/imgact.h> 38195379Ssam#include <sys/imgact_elf.h> 39195379Ssam#include <sys/kernel.h> 40195379Ssam#include <sys/malloc.h> 41195379Ssam#include <sys/mman.h> 42257176Sglebius#include <sys/namei.h> 43195379Ssam#include <sys/pioctl.h> 44195379Ssam#include <sys/proc.h> 45195379Ssam#include <sys/procfs.h> 46195379Ssam#include <sys/resourcevar.h> 47195379Ssam#include <sys/signalvar.h> 48195379Ssam#include <sys/stat.h> 49195379Ssam#include <sys/syscall.h> 50195379Ssam#include <sys/sysctl.h> 51195379Ssam#include <sys/sysent.h> 52195379Ssam#include <sys/systm.h> 53195379Ssam#include <sys/vnode.h> 54223842Skevlo 55195379Ssam#include <vm/vm.h> 56195379Ssam#include <vm/vm_kern.h> 57195379Ssam#include <vm/vm_param.h> 58195379Ssam#include <vm/pmap.h> 59195379Ssam#include <sys/lock.h> 60195379Ssam#include <vm/vm_map.h> 61195379Ssam#include <vm/vm_object.h> 62195379Ssam#include <vm/vm_extern.h> 63195379Ssam 64195379Ssam#include <machine/atomic.h> 65195379Ssam#include <machine/elf.h> 66195379Ssam#include <machine/md_var.h> 67195379Ssam 68195379Ssam#define OLD_EI_BRAND 8 69195379Ssam 70195379Ssam__ElfType(Brandinfo); 71195379Ssam__ElfType(Auxargs); 72195379Ssam 73195379Ssamstatic int elf_check_header __P((const Elf_Ehdr *hdr)); 74195379Ssamstatic int elf_freebsd_fixup __P((register_t **stack_base, 75195379Ssam struct image_params *imgp)); 76195379Ssamstatic int elf_load_file __P((struct proc *p, const char *file, u_long *addr, 77195379Ssam u_long *entry)); 78195379Ssamstatic int elf_load_section __P((struct proc *p, 79195379Ssam struct vmspace *vmspace, struct vnode *vp, 80195379Ssam vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, 81195379Ssam vm_prot_t prot)); 82195379Ssamstatic int exec_elf_imgact __P((struct image_params *imgp)); 83195379Ssam 84195379Ssamstatic int elf_trace = 0; 85195379SsamSYSCTL_INT(_debug, OID_AUTO, elf_trace, CTLFLAG_RW, &elf_trace, 0, ""); 86195379Ssam 87195379Ssamstatic struct sysentvec elf_freebsd_sysvec = { 88195379Ssam SYS_MAXSYSCALL, 89195379Ssam sysent, 90195379Ssam 0, 91195379Ssam 0, 92195379Ssam 0, 93195379Ssam 0, 94195379Ssam 0, 95195379Ssam 0, 96195379Ssam elf_freebsd_fixup, 97195379Ssam sendsig, 98195379Ssam sigcode, 99195379Ssam &szsigcode, 100195379Ssam 0, 101195379Ssam "FreeBSD ELF", 102195379Ssam elf_coredump 103195379Ssam}; 104195379Ssam 105195379Ssamstatic Elf_Brandinfo freebsd_brand_info = { 106195379Ssam ELFOSABI_FREEBSD, 107195379Ssam "", 108195379Ssam "/usr/libexec/ld-elf.so.1", 109195379Ssam &elf_freebsd_sysvec 110195379Ssam }; 111195379Ssamstatic Elf_Brandinfo *elf_brand_list[MAX_BRANDS] = { 112195379Ssam &freebsd_brand_info, 113195379Ssam NULL, NULL, NULL, 114195379Ssam NULL, NULL, NULL, NULL 115195379Ssam }; 116195379Ssam 117195379Ssamint 118195379Ssamelf_insert_brand_entry(Elf_Brandinfo *entry) 119195379Ssam{ 120195379Ssam int i; 121195379Ssam 122195379Ssam for (i=1; i<MAX_BRANDS; i++) { 123195379Ssam if (elf_brand_list[i] == NULL) { 124195379Ssam elf_brand_list[i] = entry; 125195379Ssam break; 126195379Ssam } 127195379Ssam } 128195379Ssam if (i == MAX_BRANDS) 129195379Ssam return -1; 130195379Ssam return 0; 131195379Ssam} 132195379Ssam 133195379Ssamint 134195379Ssamelf_remove_brand_entry(Elf_Brandinfo *entry) 135195379Ssam{ 136195379Ssam int i; 137195379Ssam 138195379Ssam for (i=1; i<MAX_BRANDS; i++) { 139195379Ssam if (elf_brand_list[i] == entry) { 140195379Ssam elf_brand_list[i] = NULL; 141195379Ssam break; 142195379Ssam } 143195379Ssam } 144195379Ssam if (i == MAX_BRANDS) 145195379Ssam return -1; 146195379Ssam return 0; 147195379Ssam} 148195379Ssam 149195379Ssamint 150195379Ssamelf_brand_inuse(Elf_Brandinfo *entry) 151195379Ssam{ 152195379Ssam struct proc *p; 153195379Ssam 154195379Ssam LIST_FOREACH(p, &allproc, p_list) { 155195379Ssam if (p->p_sysent == entry->sysvec) 156195379Ssam return TRUE; 157195379Ssam } 158195379Ssam 159218965Sbrucec return FALSE; 160195379Ssam} 161195379Ssam 162195379Ssamstatic int 163195379Ssamelf_check_header(const Elf_Ehdr *hdr) 164195379Ssam{ 165195379Ssam if (!IS_ELF(*hdr) || 166195379Ssam hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 167195379Ssam hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 168195379Ssam hdr->e_ident[EI_VERSION] != EV_CURRENT) 169195379Ssam return ENOEXEC; 170195379Ssam 171195379Ssam if (!ELF_MACHINE_OK(hdr->e_machine)) 172195379Ssam return ENOEXEC; 173195379Ssam 174195379Ssam if (hdr->e_version != ELF_TARG_VER) 175195379Ssam return ENOEXEC; 176195379Ssam 177195379Ssam return 0; 178195379Ssam} 179195379Ssam 180195379Ssamstatic int 181195379Ssamelf_load_section(struct proc *p, struct vmspace *vmspace, struct vnode *vp, vm_offset_t offset, caddr_t vmaddr, size_t memsz, size_t filsz, vm_prot_t prot) 182195379Ssam{ 183195379Ssam size_t map_len; 184195379Ssam vm_offset_t map_addr; 185195379Ssam int error, rv; 186195379Ssam size_t copy_len; 187195379Ssam vm_object_t object; 188195379Ssam vm_offset_t file_addr; 189195379Ssam vm_offset_t data_buf = 0; 190195379Ssam 191195379Ssam object = vp->v_object; 192195379Ssam error = 0; 193195379Ssam 194195379Ssam /* 195195379Ssam * It's necessary to fail if the filsz + offset taken from the 196195379Ssam * header is greater than the actual file pager object's size. 197195379Ssam * If we were to allow this, then the vm_map_find() below would 198195379Ssam * walk right off the end of the file object and into the ether. 199195379Ssam * 200195379Ssam * While I'm here, might as well check for something else that 201195379Ssam * is invalid: filsz cannot be greater than memsz. 202195379Ssam */ 203195379Ssam if ((off_t)filsz + offset > object->un_pager.vnp.vnp_size || 204195379Ssam filsz > memsz) { 205195379Ssam uprintf("elf_load_section: truncated ELF file\n"); 206195379Ssam return (ENOEXEC); 207195379Ssam } 208195379Ssam 209195379Ssam map_addr = trunc_page((vm_offset_t)vmaddr); 210195379Ssam file_addr = trunc_page(offset); 211195379Ssam 212195379Ssam /* 213195379Ssam * We have two choices. We can either clear the data in the last page 214195379Ssam * of an oversized mapping, or we can start the anon mapping a page 215195379Ssam * early and copy the initialized data into that first page. We 216195379Ssam * choose the second.. 217195379Ssam */ 218195379Ssam if (memsz > filsz) 219195379Ssam map_len = trunc_page(offset+filsz) - file_addr; 220195379Ssam else 221195379Ssam map_len = round_page(offset+filsz) - file_addr; 222195379Ssam 223195379Ssam if (map_len != 0) { 224195379Ssam vm_object_reference(object); 225195379Ssam vm_map_lock(&vmspace->vm_map); 226195379Ssam rv = vm_map_insert(&vmspace->vm_map, 227195527Ssam object, 228195527Ssam file_addr, /* file offset */ 229195379Ssam map_addr, /* virtual start */ 230195379Ssam map_addr + map_len,/* virtual end */ 231195379Ssam prot, 232195379Ssam VM_PROT_ALL, 233195379Ssam MAP_COPY_ON_WRITE | MAP_PREFAULT); 234195379Ssam vm_map_unlock(&vmspace->vm_map); 235195379Ssam if (rv != KERN_SUCCESS) { 236195379Ssam vm_object_deallocate(object); 237195379Ssam return EINVAL; 238195379Ssam } 239195379Ssam 240 /* we can stop now if we've covered it all */ 241 if (memsz == filsz) 242 return 0; 243 } 244 245 246 /* 247 * We have to get the remaining bit of the file into the first part 248 * of the oversized map segment. This is normally because the .data 249 * segment in the file is extended to provide bss. It's a neat idea 250 * to try and save a page, but it's a pain in the behind to implement. 251 */ 252 copy_len = (offset + filsz) - trunc_page(offset + filsz); 253 map_addr = trunc_page((vm_offset_t)vmaddr + filsz); 254 map_len = round_page((vm_offset_t)vmaddr + memsz) - map_addr; 255 256 /* This had damn well better be true! */ 257 if (map_len != 0) { 258 vm_map_lock(&vmspace->vm_map); 259 rv = vm_map_insert(&vmspace->vm_map, NULL, 0, 260 map_addr, map_addr + map_len, 261 VM_PROT_ALL, VM_PROT_ALL, 0); 262 vm_map_unlock(&vmspace->vm_map); 263 if (rv != KERN_SUCCESS) 264 return EINVAL; 265 } 266 267 if (copy_len != 0) { 268 vm_object_reference(object); 269 rv = vm_map_find(exec_map, 270 object, 271 trunc_page(offset + filsz), 272 &data_buf, 273 PAGE_SIZE, 274 TRUE, 275 VM_PROT_READ, 276 VM_PROT_ALL, 277 MAP_COPY_ON_WRITE | MAP_PREFAULT_PARTIAL); 278 if (rv != KERN_SUCCESS) { 279 vm_object_deallocate(object); 280 return EINVAL; 281 } 282 283 /* send the page fragment to user space */ 284 error = copyout((caddr_t)data_buf, (caddr_t)map_addr, copy_len); 285 vm_map_remove(exec_map, data_buf, data_buf + PAGE_SIZE); 286 if (error) 287 return (error); 288 } 289 290 /* 291 * set it to the specified protection 292 */ 293 vm_map_protect(&vmspace->vm_map, map_addr, map_addr + map_len, prot, 294 FALSE); 295 296 return error; 297} 298 299/* 300 * Load the file "file" into memory. It may be either a shared object 301 * or an executable. 302 * 303 * The "addr" reference parameter is in/out. On entry, it specifies 304 * the address where a shared object should be loaded. If the file is 305 * an executable, this value is ignored. On exit, "addr" specifies 306 * where the file was actually loaded. 307 * 308 * The "entry" reference parameter is out only. On exit, it specifies 309 * the entry point for the loaded file. 310 */ 311static int 312elf_load_file(struct proc *p, const char *file, u_long *addr, u_long *entry) 313{ 314 const Elf_Ehdr *hdr = NULL; 315 const Elf_Phdr *phdr = NULL; 316 struct nameidata nd; 317 struct vmspace *vmspace = p->p_vmspace; 318 struct vattr attr; 319 struct image_params image_params, *imgp; 320 vm_prot_t prot; 321 u_long rbase; 322 u_long base_addr = 0; 323 int error, i, numsegs; 324 325 imgp = &image_params; 326 /* 327 * Initialize part of the common data 328 */ 329 imgp->proc = p; 330 imgp->uap = NULL; 331 imgp->attr = &attr; 332 imgp->firstpage = NULL; 333 imgp->image_header = (char *)kmem_alloc_wait(exec_map, PAGE_SIZE); 334 335 if (imgp->image_header == NULL) { 336 nd.ni_vp = NULL; 337 error = ENOMEM; 338 goto fail; 339 } 340 341 NDINIT(&nd, LOOKUP, LOCKLEAF|FOLLOW, UIO_SYSSPACE, file, p); 342 343 if ((error = namei(&nd)) != 0) { 344 nd.ni_vp = NULL; 345 goto fail; 346 } 347 NDFREE(&nd, NDF_ONLY_PNBUF); 348 imgp->vp = nd.ni_vp; 349 350 /* 351 * Check permissions, modes, uid, etc on the file, and "open" it. 352 */ 353 error = exec_check_permissions(imgp); 354 if (error) { 355 VOP_UNLOCK(nd.ni_vp, 0, p); 356 goto fail; 357 } 358 359 error = exec_map_first_page(imgp); 360 /* 361 * Also make certain that the interpreter stays the same, so set 362 * its VTEXT flag, too. 363 */ 364 if (error == 0) 365 nd.ni_vp->v_flag |= VTEXT; 366 VOP_UNLOCK(nd.ni_vp, 0, p); 367 if (error) 368 goto fail; 369 370 hdr = (const Elf_Ehdr *)imgp->image_header; 371 if ((error = elf_check_header(hdr)) != 0) 372 goto fail; 373 if (hdr->e_type == ET_DYN) 374 rbase = *addr; 375 else if (hdr->e_type == ET_EXEC) 376 rbase = 0; 377 else { 378 error = ENOEXEC; 379 goto fail; 380 } 381 382 /* Only support headers that fit within first page for now */ 383 if ((hdr->e_phoff > PAGE_SIZE) || 384 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 385 error = ENOEXEC; 386 goto fail; 387 } 388 389 phdr = (const Elf_Phdr *)(imgp->image_header + hdr->e_phoff); 390 391 for (i = 0, numsegs = 0; i < hdr->e_phnum; i++) { 392 if (phdr[i].p_type == PT_LOAD) { /* Loadable segment */ 393 prot = 0; 394 if (phdr[i].p_flags & PF_X) 395 prot |= VM_PROT_EXECUTE; 396 if (phdr[i].p_flags & PF_W) 397 prot |= VM_PROT_WRITE; 398 if (phdr[i].p_flags & PF_R) 399 prot |= VM_PROT_READ; 400 401 if ((error = elf_load_section(p, vmspace, nd.ni_vp, 402 phdr[i].p_offset, 403 (caddr_t)phdr[i].p_vaddr + 404 rbase, 405 phdr[i].p_memsz, 406 phdr[i].p_filesz, prot)) != 0) 407 goto fail; 408 /* 409 * Establish the base address if this is the 410 * first segment. 411 */ 412 if (numsegs == 0) 413 base_addr = trunc_page(phdr[i].p_vaddr + rbase); 414 numsegs++; 415 } 416 } 417 *addr = base_addr; 418 *entry=(unsigned long)hdr->e_entry + rbase; 419 420fail: 421 if (imgp->firstpage) 422 exec_unmap_first_page(imgp); 423 if (imgp->image_header) 424 kmem_free_wakeup(exec_map, (vm_offset_t)imgp->image_header, 425 PAGE_SIZE); 426 if (nd.ni_vp) 427 vrele(nd.ni_vp); 428 429 return error; 430} 431 432static int fallback_elf_brand = ELFOSABI_FREEBSD; 433SYSCTL_INT(_kern, OID_AUTO, fallback_elf_brand, CTLFLAG_RW, 434 &fallback_elf_brand, ELFOSABI_FREEBSD, 435 "ELF brand of last resort"); 436 437static int 438exec_elf_imgact(struct image_params *imgp) 439{ 440 const Elf_Ehdr *hdr = (const Elf_Ehdr *) imgp->image_header; 441 const Elf_Phdr *phdr; 442 Elf_Auxargs *elf_auxargs = NULL; 443 struct vmspace *vmspace; 444 vm_prot_t prot; 445 u_long text_size = 0, data_size = 0; 446 u_long text_addr = 0, data_addr = 0; 447 u_long addr, entry = 0, proghdr = 0; 448 int error, i; 449 const char *interp = NULL; 450 Elf_Brandinfo *brand_info; 451 char path[MAXPATHLEN]; 452 453 /* 454 * Do we have a valid ELF header ? 455 */ 456 if (elf_check_header(hdr) != 0 || hdr->e_type != ET_EXEC) 457 return -1; 458 459 /* 460 * From here on down, we return an errno, not -1, as we've 461 * detected an ELF file. 462 */ 463 464 if ((hdr->e_phoff > PAGE_SIZE) || 465 (hdr->e_phoff + hdr->e_phentsize * hdr->e_phnum) > PAGE_SIZE) { 466 /* Only support headers in first page for now */ 467 return ENOEXEC; 468 } 469 phdr = (const Elf_Phdr*)(imgp->image_header + hdr->e_phoff); 470 471 /* 472 * From this point on, we may have resources that need to be freed. 473 */ 474 475 /* 476 * Yeah, I'm paranoid. There is every reason in the world to get 477 * VTEXT now since from here on out, there are places we can have 478 * a context switch. Better safe than sorry; I really don't want 479 * the file to change while it's being loaded. 480 */ 481 atomic_set_long(&imgp->vp->v_flag, VTEXT); 482 483 if ((error = exec_extract_strings(imgp)) != 0) 484 goto fail; 485 486 exec_new_vmspace(imgp); 487 488 vmspace = imgp->proc->p_vmspace; 489 490 for (i = 0; i < hdr->e_phnum; i++) { 491 switch(phdr[i].p_type) { 492 493 case PT_LOAD: /* Loadable segment */ 494 prot = 0; 495 if (phdr[i].p_flags & PF_X) 496 prot |= VM_PROT_EXECUTE; 497 if (phdr[i].p_flags & PF_W) 498 prot |= VM_PROT_WRITE; 499 if (phdr[i].p_flags & PF_R) 500 prot |= VM_PROT_READ; 501 502 if ((error = elf_load_section(imgp->proc, 503 vmspace, imgp->vp, 504 phdr[i].p_offset, 505 (caddr_t)phdr[i].p_vaddr, 506 phdr[i].p_memsz, 507 phdr[i].p_filesz, prot)) != 0) 508 goto fail; 509 510 /* 511 * Is this .text or .data ?? 512 * 513 * We only handle one each of those yet XXX 514 */ 515 if (hdr->e_entry >= phdr[i].p_vaddr && 516 hdr->e_entry <(phdr[i].p_vaddr+phdr[i].p_memsz)) { 517 text_addr = trunc_page(phdr[i].p_vaddr); 518 text_size = round_page(phdr[i].p_memsz + 519 phdr[i].p_vaddr - 520 text_addr); 521 entry = (u_long)hdr->e_entry; 522 } else { 523 data_addr = trunc_page(phdr[i].p_vaddr); 524 data_size = round_page(phdr[i].p_memsz + 525 phdr[i].p_vaddr - 526 data_addr); 527 } 528 break; 529 case PT_INTERP: /* Path to interpreter */ 530 if (phdr[i].p_filesz > MAXPATHLEN || 531 phdr[i].p_offset + phdr[i].p_filesz > PAGE_SIZE) { 532 error = ENOEXEC; 533 goto fail; 534 } 535 interp = imgp->image_header + phdr[i].p_offset; 536 break; 537 case PT_PHDR: /* Program header table info */ 538 proghdr = phdr[i].p_vaddr; 539 break; 540 default: 541 break; 542 } 543 } 544 545 vmspace->vm_tsize = text_size >> PAGE_SHIFT; 546 vmspace->vm_taddr = (caddr_t)(uintptr_t)text_addr; 547 vmspace->vm_dsize = data_size >> PAGE_SHIFT; 548 vmspace->vm_daddr = (caddr_t)(uintptr_t)data_addr; 549 550 addr = ELF_RTLD_ADDR(vmspace); 551 552 imgp->entry_addr = entry; 553 554 brand_info = NULL; 555 556 /* XXX For now we look for the magic "FreeBSD" that we used to put 557 * into the ELF header at the EI_ABIVERSION location. If found use 558 * that information rather than figuring out the ABI from proper 559 * branding. This should be removed for 5.0-RELEASE. The Linux caes 560 * can be figured out from the `interp_path' field. 561 */ 562 if (strcmp("FreeBSD", (const char *)&hdr->e_ident[OLD_EI_BRAND]) == 0) 563 brand_info = &freebsd_brand_info; 564 565 /* If the executable has a brand, search for it in the brand list. */ 566 if (brand_info == NULL) { 567 for (i = 0; i < MAX_BRANDS; i++) { 568 Elf_Brandinfo *bi = elf_brand_list[i]; 569 570 if (bi != NULL && hdr->e_ident[EI_OSABI] == bi->brand) { 571 brand_info = bi; 572 break; 573 } 574 } 575 } 576 577 /* Lacking a known brand, search for a recognized interpreter. */ 578 if (brand_info == NULL && interp != NULL) { 579 for (i = 0; i < MAX_BRANDS; i++) { 580 Elf_Brandinfo *bi = elf_brand_list[i]; 581 582 if (bi != NULL && 583 strcmp(interp, bi->interp_path) == 0) { 584 brand_info = bi; 585 break; 586 } 587 } 588 } 589 590 /* Lacking a recognized interpreter, try the default brand */ 591 if (brand_info == NULL) { 592 for (i = 0; i < MAX_BRANDS; i++) { 593 Elf_Brandinfo *bi = elf_brand_list[i]; 594 595 if (bi != NULL && fallback_elf_brand == bi->brand) { 596 brand_info = bi; 597 break; 598 } 599 } 600 } 601 602 /* XXX - Assume FreeBSD after the branding method change. */ 603 if (brand_info == NULL) 604 brand_info = &freebsd_brand_info; 605 606 if (brand_info == NULL) { 607 uprintf("ELF binary type \"%u\" not known.\n", 608 hdr->e_ident[EI_OSABI]); 609 error = ENOEXEC; 610 goto fail; 611 } 612 613 imgp->proc->p_sysent = brand_info->sysvec; 614 if (interp != NULL) { 615 snprintf(path, sizeof(path), "%s%s", 616 brand_info->emul_path, interp); 617 if ((error = elf_load_file(imgp->proc, path, &addr, 618 &imgp->entry_addr)) != 0) { 619 if ((error = elf_load_file(imgp->proc, interp, &addr, 620 &imgp->entry_addr)) != 0) { 621 uprintf("ELF interpreter %s not found\n", path); 622 goto fail; 623 } 624 } 625 } 626 627 /* 628 * Construct auxargs table (used by the fixup routine) 629 */ 630 elf_auxargs = malloc(sizeof(Elf_Auxargs), M_TEMP, M_WAITOK); 631 elf_auxargs->execfd = -1; 632 elf_auxargs->phdr = proghdr; 633 elf_auxargs->phent = hdr->e_phentsize; 634 elf_auxargs->phnum = hdr->e_phnum; 635 elf_auxargs->pagesz = PAGE_SIZE; 636 elf_auxargs->base = addr; 637 elf_auxargs->flags = 0; 638 elf_auxargs->entry = entry; 639 elf_auxargs->trace = elf_trace; 640 641 imgp->auxargs = elf_auxargs; 642 imgp->interpreted = 0; 643 644fail: 645 return error; 646} 647 648static int 649elf_freebsd_fixup(register_t **stack_base, struct image_params *imgp) 650{ 651 Elf_Auxargs *args = (Elf_Auxargs *)imgp->auxargs; 652 register_t *pos; 653 654 pos = *stack_base + (imgp->argc + imgp->envc + 2); 655 656 if (args->trace) { 657 AUXARGS_ENTRY(pos, AT_DEBUG, 1); 658 } 659 if (args->execfd != -1) { 660 AUXARGS_ENTRY(pos, AT_EXECFD, args->execfd); 661 } 662 AUXARGS_ENTRY(pos, AT_PHDR, args->phdr); 663 AUXARGS_ENTRY(pos, AT_PHENT, args->phent); 664 AUXARGS_ENTRY(pos, AT_PHNUM, args->phnum); 665 AUXARGS_ENTRY(pos, AT_PAGESZ, args->pagesz); 666 AUXARGS_ENTRY(pos, AT_FLAGS, args->flags); 667 AUXARGS_ENTRY(pos, AT_ENTRY, args->entry); 668 AUXARGS_ENTRY(pos, AT_BASE, args->base); 669 AUXARGS_ENTRY(pos, AT_NULL, 0); 670 671 free(imgp->auxargs, M_TEMP); 672 imgp->auxargs = NULL; 673 674 (*stack_base)--; 675 suword(*stack_base, (long) imgp->argc); 676 return 0; 677} 678 679/* 680 * Code for generating ELF core dumps. 681 */ 682 683typedef void (*segment_callback) __P((vm_map_entry_t, void *)); 684 685/* Closure for cb_put_phdr(). */ 686struct phdr_closure { 687 Elf_Phdr *phdr; /* Program header to fill in */ 688 Elf_Off offset; /* Offset of segment in core file */ 689}; 690 691/* Closure for cb_size_segment(). */ 692struct sseg_closure { 693 int count; /* Count of writable segments. */ 694 size_t size; /* Total size of all writable segments. */ 695}; 696 697static void cb_put_phdr __P((vm_map_entry_t, void *)); 698static void cb_size_segment __P((vm_map_entry_t, void *)); 699static void each_writable_segment __P((struct proc *, segment_callback, 700 void *)); 701static int elf_corehdr __P((struct proc *, struct vnode *, struct ucred *, 702 int, void *, size_t)); 703static void elf_puthdr __P((struct proc *, void *, size_t *, 704 const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int)); 705static void elf_putnote __P((void *, size_t *, const char *, int, 706 const void *, size_t)); 707 708extern int osreldate; 709 710int 711elf_coredump(p, vp, limit) 712 register struct proc *p; 713 register struct vnode *vp; 714 off_t limit; 715{ 716 register struct ucred *cred = p->p_ucred; 717 int error = 0; 718 struct sseg_closure seginfo; 719 void *hdr; 720 size_t hdrsize; 721 722 /* Size the program segments. */ 723 seginfo.count = 0; 724 seginfo.size = 0; 725 each_writable_segment(p, cb_size_segment, &seginfo); 726 727 /* 728 * Calculate the size of the core file header area by making 729 * a dry run of generating it. Nothing is written, but the 730 * size is calculated. 731 */ 732 hdrsize = 0; 733 elf_puthdr((struct proc *)NULL, (void *)NULL, &hdrsize, 734 (const prstatus_t *)NULL, (const prfpregset_t *)NULL, 735 (const prpsinfo_t *)NULL, seginfo.count); 736 737 if (hdrsize + seginfo.size >= limit) 738 return (EFAULT); 739 740 /* 741 * Allocate memory for building the header, fill it up, 742 * and write it out. 743 */ 744 hdr = malloc(hdrsize, M_TEMP, M_WAITOK); 745 if (hdr == NULL) { 746 return EINVAL; 747 } 748 error = elf_corehdr(p, vp, cred, seginfo.count, hdr, hdrsize); 749 750 /* Write the contents of all of the writable segments. */ 751 if (error == 0) { 752 Elf_Phdr *php; 753 off_t offset; 754 int i; 755 756 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 757 offset = hdrsize; 758 for (i = 0; i < seginfo.count; i++) { 759 error = vn_rdwr(UIO_WRITE, vp, (caddr_t)php->p_vaddr, 760 php->p_filesz, offset, UIO_USERSPACE, 761 IO_NODELOCKED|IO_UNIT, cred, (int *)NULL, p); 762 if (error != 0) 763 break; 764 offset += php->p_filesz; 765 php++; 766 } 767 } 768 free(hdr, M_TEMP); 769 770 return error; 771} 772 773/* 774 * A callback for each_writable_segment() to write out the segment's 775 * program header entry. 776 */ 777static void 778cb_put_phdr(entry, closure) 779 vm_map_entry_t entry; 780 void *closure; 781{ 782 struct phdr_closure *phc = (struct phdr_closure *)closure; 783 Elf_Phdr *phdr = phc->phdr; 784 785 phc->offset = round_page(phc->offset); 786 787 phdr->p_type = PT_LOAD; 788 phdr->p_offset = phc->offset; 789 phdr->p_vaddr = entry->start; 790 phdr->p_paddr = 0; 791 phdr->p_filesz = phdr->p_memsz = entry->end - entry->start; 792 phdr->p_align = PAGE_SIZE; 793 phdr->p_flags = 0; 794 if (entry->protection & VM_PROT_READ) 795 phdr->p_flags |= PF_R; 796 if (entry->protection & VM_PROT_WRITE) 797 phdr->p_flags |= PF_W; 798 if (entry->protection & VM_PROT_EXECUTE) 799 phdr->p_flags |= PF_X; 800 801 phc->offset += phdr->p_filesz; 802 phc->phdr++; 803} 804 805/* 806 * A callback for each_writable_segment() to gather information about 807 * the number of segments and their total size. 808 */ 809static void 810cb_size_segment(entry, closure) 811 vm_map_entry_t entry; 812 void *closure; 813{ 814 struct sseg_closure *ssc = (struct sseg_closure *)closure; 815 816 ssc->count++; 817 ssc->size += entry->end - entry->start; 818} 819 820/* 821 * For each writable segment in the process's memory map, call the given 822 * function with a pointer to the map entry and some arbitrary 823 * caller-supplied data. 824 */ 825static void 826each_writable_segment(p, func, closure) 827 struct proc *p; 828 segment_callback func; 829 void *closure; 830{ 831 vm_map_t map = &p->p_vmspace->vm_map; 832 vm_map_entry_t entry; 833 834 for (entry = map->header.next; entry != &map->header; 835 entry = entry->next) { 836 vm_object_t obj; 837 838 if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) || 839 (entry->protection & (VM_PROT_READ|VM_PROT_WRITE)) != 840 (VM_PROT_READ|VM_PROT_WRITE)) 841 continue; 842 843 /* 844 ** Dont include memory segment in the coredump if 845 ** MAP_NOCORE is set in mmap(2) or MADV_NOCORE in 846 ** madvise(2). 847 */ 848 if (entry->eflags & MAP_ENTRY_NOCOREDUMP) 849 continue; 850 851 if ((obj = entry->object.vm_object) == NULL) 852 continue; 853 854 /* Find the deepest backing object. */ 855 while (obj->backing_object != NULL) 856 obj = obj->backing_object; 857 858 /* Ignore memory-mapped devices and such things. */ 859 if (obj->type != OBJT_DEFAULT && 860 obj->type != OBJT_SWAP && 861 obj->type != OBJT_VNODE) 862 continue; 863 864 (*func)(entry, closure); 865 } 866} 867 868/* 869 * Write the core file header to the file, including padding up to 870 * the page boundary. 871 */ 872static int 873elf_corehdr(p, vp, cred, numsegs, hdr, hdrsize) 874 struct proc *p; 875 struct vnode *vp; 876 struct ucred *cred; 877 int numsegs; 878 size_t hdrsize; 879 void *hdr; 880{ 881 size_t off; 882 prstatus_t status; 883 prfpregset_t fpregset; 884 prpsinfo_t psinfo; 885 886 /* Gather the information for the header. */ 887 bzero(&status, sizeof status); 888 status.pr_version = PRSTATUS_VERSION; 889 status.pr_statussz = sizeof(prstatus_t); 890 status.pr_gregsetsz = sizeof(gregset_t); 891 status.pr_fpregsetsz = sizeof(fpregset_t); 892 status.pr_osreldate = osreldate; 893 status.pr_cursig = p->p_sig; 894 status.pr_pid = p->p_pid; 895 fill_regs(p, &status.pr_reg); 896 897 fill_fpregs(p, &fpregset); 898 899 bzero(&psinfo, sizeof psinfo); 900 psinfo.pr_version = PRPSINFO_VERSION; 901 psinfo.pr_psinfosz = sizeof(prpsinfo_t); 902 strncpy(psinfo.pr_fname, p->p_comm, MAXCOMLEN); 903 /* XXX - We don't fill in the command line arguments properly yet. */ 904 strncpy(psinfo.pr_psargs, p->p_comm, PRARGSZ); 905 906 /* Fill in the header. */ 907 bzero(hdr, hdrsize); 908 off = 0; 909 elf_puthdr(p, hdr, &off, &status, &fpregset, &psinfo, numsegs); 910 911 /* Write it to the core file. */ 912 return vn_rdwr(UIO_WRITE, vp, hdr, hdrsize, (off_t)0, 913 UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, cred, NULL, p); 914} 915 916static void 917elf_puthdr(struct proc *p, void *dst, size_t *off, const prstatus_t *status, 918 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs) 919{ 920 size_t ehoff; 921 size_t phoff; 922 size_t noteoff; 923 size_t notesz; 924 925 ehoff = *off; 926 *off += sizeof(Elf_Ehdr); 927 928 phoff = *off; 929 *off += (numsegs + 1) * sizeof(Elf_Phdr); 930 931 noteoff = *off; 932 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status, 933 sizeof *status); 934 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset, 935 sizeof *fpregset); 936 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo, 937 sizeof *psinfo); 938 notesz = *off - noteoff; 939 940 /* Align up to a page boundary for the program segments. */ 941 *off = round_page(*off); 942 943 if (dst != NULL) { 944 Elf_Ehdr *ehdr; 945 Elf_Phdr *phdr; 946 struct phdr_closure phc; 947 948 /* 949 * Fill in the ELF header. 950 */ 951 ehdr = (Elf_Ehdr *)((char *)dst + ehoff); 952 ehdr->e_ident[EI_MAG0] = ELFMAG0; 953 ehdr->e_ident[EI_MAG1] = ELFMAG1; 954 ehdr->e_ident[EI_MAG2] = ELFMAG2; 955 ehdr->e_ident[EI_MAG3] = ELFMAG3; 956 ehdr->e_ident[EI_CLASS] = ELF_CLASS; 957 ehdr->e_ident[EI_DATA] = ELF_DATA; 958 ehdr->e_ident[EI_VERSION] = EV_CURRENT; 959 ehdr->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 960 ehdr->e_ident[EI_ABIVERSION] = 0; 961 ehdr->e_ident[EI_PAD] = 0; 962 ehdr->e_type = ET_CORE; 963 ehdr->e_machine = ELF_ARCH; 964 ehdr->e_version = EV_CURRENT; 965 ehdr->e_entry = 0; 966 ehdr->e_phoff = phoff; 967 ehdr->e_flags = 0; 968 ehdr->e_ehsize = sizeof(Elf_Ehdr); 969 ehdr->e_phentsize = sizeof(Elf_Phdr); 970 ehdr->e_phnum = numsegs + 1; 971 ehdr->e_shentsize = sizeof(Elf_Shdr); 972 ehdr->e_shnum = 0; 973 ehdr->e_shstrndx = SHN_UNDEF; 974 975 /* 976 * Fill in the program header entries. 977 */ 978 phdr = (Elf_Phdr *)((char *)dst + phoff); 979 980 /* The note segement. */ 981 phdr->p_type = PT_NOTE; 982 phdr->p_offset = noteoff; 983 phdr->p_vaddr = 0; 984 phdr->p_paddr = 0; 985 phdr->p_filesz = notesz; 986 phdr->p_memsz = 0; 987 phdr->p_flags = 0; 988 phdr->p_align = 0; 989 phdr++; 990 991 /* All the writable segments from the program. */ 992 phc.phdr = phdr; 993 phc.offset = *off; 994 each_writable_segment(p, cb_put_phdr, &phc); 995 } 996} 997 998static void 999elf_putnote(void *dst, size_t *off, const char *name, int type, 1000 const void *desc, size_t descsz) 1001{ 1002 Elf_Note note; 1003 1004 note.n_namesz = strlen(name) + 1; 1005 note.n_descsz = descsz; 1006 note.n_type = type; 1007 if (dst != NULL) 1008 bcopy(¬e, (char *)dst + *off, sizeof note); 1009 *off += sizeof note; 1010 if (dst != NULL) 1011 bcopy(name, (char *)dst + *off, note.n_namesz); 1012 *off += roundup2(note.n_namesz, sizeof(Elf_Size)); 1013 if (dst != NULL) 1014 bcopy(desc, (char *)dst + *off, note.n_descsz); 1015 *off += roundup2(note.n_descsz, sizeof(Elf_Size)); 1016} 1017 1018/* 1019 * Tell kern_execve.c about it, with a little help from the linker. 1020 */ 1021static struct execsw elf_execsw = {exec_elf_imgact, "ELF"}; 1022EXEC_SET(elf, elf_execsw); 1023