exec_elf32.c revision 1.49
1/* $NetBSD: exec_elf32.c,v 1.49 2000/06/06 18:26:34 soren Exp $ */ 2 3/*- 4 * Copyright (c) 1994 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Christos Zoulas. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39/* 40 * Copyright (c) 1996 Christopher G. Demetriou 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 3. The name of the author may not be used to endorse or promote products 52 * derived from this software without specific prior written permission 53 * 54 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 57 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 67#ifndef ELFSIZE 68#define ELFSIZE 32 69#endif 70 71#include "opt_compat_linux.h" 72#include "opt_compat_ibcs2.h" 73#include "opt_compat_svr4.h" 74#include "opt_compat_freebsd.h" 75#include "opt_compat_netbsd32.h" 76#include "opt_syscall_debug.h" 77 78#include <sys/param.h> 79#include <sys/systm.h> 80#include <sys/kernel.h> 81#include <sys/proc.h> 82#include <sys/malloc.h> 83#include <sys/namei.h> 84#include <sys/vnode.h> 85#include <sys/exec.h> 86#include <sys/exec_elf.h> 87#include <sys/fcntl.h> 88#include <sys/syscall.h> 89#include <sys/signalvar.h> 90#include <sys/mount.h> 91#include <sys/stat.h> 92 93#include <sys/mman.h> 94#include <vm/vm.h> 95#include <vm/vm_param.h> 96#include <vm/vm_map.h> 97 98#include <machine/cpu.h> 99#include <machine/reg.h> 100 101#ifdef COMPAT_NETBSD32 102#include <compat/netbsd32/netbsd32_exec.h> 103#endif 104 105#ifdef COMPAT_LINUX 106#include <compat/linux/common/linux_exec.h> 107#endif 108 109#ifdef COMPAT_SVR4 110#include <compat/svr4/svr4_exec.h> 111#endif 112 113#ifdef COMPAT_IBCS2 114#include <compat/ibcs2/ibcs2_exec.h> 115#endif 116 117#ifdef COMPAT_FREEBSD 118#include <compat/freebsd/freebsd_exec.h> 119#endif 120 121int ELFNAME(check_header) __P((Elf_Ehdr *, int)); 122int ELFNAME(load_file) __P((struct proc *, struct exec_package *, char *, 123 struct exec_vmcmd_set *, u_long *, struct elf_args *, Elf_Addr *)); 124void ELFNAME(load_psection) __P((struct exec_vmcmd_set *, struct vnode *, 125 Elf_Phdr *, Elf_Addr *, u_long *, int *)); 126 127int ELFNAME2(netbsd,signature) __P((struct proc *, struct exec_package *, 128 Elf_Ehdr *)); 129static int ELFNAME2(netbsd,probe) __P((struct proc *, struct exec_package *, 130 Elf_Ehdr *, char *, Elf_Addr *)); 131 132extern char sigcode[], esigcode[]; 133#ifdef SYSCALL_DEBUG 134extern char *syscallnames[]; 135#endif 136 137struct emul ELFNAMEEND(emul_netbsd) = { 138 "netbsd", 139 NULL, 140 sendsig, 141 SYS_syscall, 142 SYS_MAXSYSCALL, 143 sysent, 144#ifdef SYSCALL_DEBUG 145 syscallnames, 146#else 147 NULL, 148#endif 149 howmany(ELF_AUX_ENTRIES * sizeof(AuxInfo), sizeof (Elf_Addr)), 150 ELFNAME(copyargs), 151 setregs, 152 sigcode, 153 esigcode, 154}; 155 156int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *, 157 Elf_Ehdr *, char *, Elf_Addr *)) = { 158#if defined(COMPAT_NETBSD32) && (ELFSIZE == 32) 159 /* This one should go first so it matches instead of netbsd */ 160 ELFNAME2(netbsd32,probe), 161#endif 162 ELFNAME2(netbsd,probe), 163#if defined(COMPAT_FREEBSD) && (ELFSIZE == 32) 164 ELFNAME2(freebsd,probe), /* XXX not 64-bit safe */ 165#endif 166#if defined(COMPAT_LINUX) 167 ELFNAME2(linux,probe), 168#endif 169#if defined(COMPAT_SVR4) && (ELFSIZE == 32) 170 ELFNAME2(svr4,probe), /* XXX not 64-bit safe */ 171#endif 172#if defined(COMPAT_IBCS2) && (ELFSIZE == 32) 173 ELFNAME2(ibcs2,probe), /* XXX not 64-bit safe */ 174#endif 175}; 176 177/* round up and down to page boundaries. */ 178#define ELF_ROUND(a, b) (((a) + (b) - 1) & ~((b) - 1)) 179#define ELF_TRUNC(a, b) ((a) & ~((b) - 1)) 180 181/* 182 * Copy arguments onto the stack in the normal way, but add some 183 * extra information in case of dynamic binding. 184 */ 185void * 186ELFNAME(copyargs)(pack, arginfo, stack, argp) 187 struct exec_package *pack; 188 struct ps_strings *arginfo; 189 void *stack; 190 void *argp; 191{ 192 size_t len; 193 AuxInfo ai[ELF_AUX_ENTRIES], *a; 194 struct elf_args *ap; 195 196 stack = copyargs(pack, arginfo, stack, argp); 197 if (!stack) 198 return NULL; 199 200 a = ai; 201 202 /* 203 * Push extra arguments on the stack needed by dynamically 204 * linked binaries 205 */ 206 if ((ap = (struct elf_args *)pack->ep_emul_arg)) { 207 208 a->a_type = AT_PHDR; 209 a->a_v = ap->arg_phaddr; 210 a++; 211 212 a->a_type = AT_PHENT; 213 a->a_v = ap->arg_phentsize; 214 a++; 215 216 a->a_type = AT_PHNUM; 217 a->a_v = ap->arg_phnum; 218 a++; 219 220 a->a_type = AT_PAGESZ; 221 a->a_v = NBPG; 222 a++; 223 224 a->a_type = AT_BASE; 225 a->a_v = ap->arg_interp; 226 a++; 227 228 a->a_type = AT_FLAGS; 229 a->a_v = 0; 230 a++; 231 232 a->a_type = AT_ENTRY; 233 a->a_v = ap->arg_entry; 234 a++; 235 236 free((char *)ap, M_TEMP); 237 pack->ep_emul_arg = NULL; 238 } 239 240 a->a_type = AT_NULL; 241 a->a_v = 0; 242 a++; 243 244 len = (a - ai) * sizeof(AuxInfo); 245 if (copyout(ai, stack, len)) 246 return NULL; 247 stack = (caddr_t)stack + len; 248 249 return stack; 250} 251 252/* 253 * elf_check_header(): 254 * 255 * Check header for validity; return 0 of ok ENOEXEC if error 256 */ 257int 258ELFNAME(check_header)(eh, type) 259 Elf_Ehdr *eh; 260 int type; 261{ 262 263 if (memcmp(eh->e_ident, ELFMAG, SELFMAG) != 0 || 264 eh->e_ident[EI_CLASS] != ELFCLASS) 265 266 switch (eh->e_machine) { 267 268 ELFDEFNNAME(MACHDEP_ID_CASES) 269 270 default: 271 return ENOEXEC; 272 } 273 274 if (eh->e_type != type) 275 return ENOEXEC; 276 277 return 0; 278} 279 280/* 281 * elf_load_psection(): 282 * 283 * Load a psection at the appropriate address 284 */ 285void 286ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot) 287 struct exec_vmcmd_set *vcset; 288 struct vnode *vp; 289 Elf_Phdr *ph; 290 Elf_Addr *addr; 291 u_long *size; 292 int *prot; 293{ 294 u_long uaddr, msize, psize, rm, rf; 295 long diff, offset; 296 297 /* 298 * If the user specified an address, then we load there. 299 */ 300 if (*addr != ELFDEFNNAME(NO_ADDR)) { 301 if (ph->p_align > 1) { 302 *addr = ELF_ROUND(*addr, ph->p_align); 303 uaddr = ELF_TRUNC(ph->p_vaddr, ph->p_align); 304 } else 305 uaddr = ph->p_vaddr; 306 diff = ph->p_vaddr - uaddr; 307 } else { 308 *addr = uaddr = ph->p_vaddr; 309 if (ph->p_align > 1) 310 *addr = ELF_TRUNC(uaddr, ph->p_align); 311 diff = uaddr - *addr; 312 } 313 314 *prot |= (ph->p_flags & PF_R) ? VM_PROT_READ : 0; 315 *prot |= (ph->p_flags & PF_W) ? VM_PROT_WRITE : 0; 316 *prot |= (ph->p_flags & PF_X) ? VM_PROT_EXECUTE : 0; 317 318 offset = ph->p_offset - diff; 319 *size = ph->p_filesz + diff; 320 msize = ph->p_memsz + diff; 321 psize = round_page(*size); 322 323 if ((ph->p_flags & PF_W) != 0) { 324 /* 325 * Because the pagedvn pager can't handle zero fill of the last 326 * data page if it's not page aligned we map the last page 327 * readvn. 328 */ 329 psize = trunc_page(*size); 330 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 331 offset, *prot); 332 if(psize != *size) 333 NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize, 334 *addr + psize, vp, offset + psize, *prot); 335 } else 336 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 337 offset, *prot); 338 339 /* 340 * Check if we need to extend the size of the segment 341 */ 342 rm = round_page(*addr + msize); 343 rf = round_page(*addr + *size); 344 345 if (rm != rf) { 346 NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 347 0, *prot); 348 *size = msize; 349 } 350} 351 352/* 353 * elf_read_from(): 354 * 355 * Read from vnode into buffer at offset. 356 */ 357int 358ELFNAME(read_from)(p, vp, off, buf, size) 359 struct vnode *vp; 360 u_long off; 361 struct proc *p; 362 caddr_t buf; 363 int size; 364{ 365 int error; 366 size_t resid; 367 368 if ((error = vn_rdwr(UIO_READ, vp, buf, size, off, UIO_SYSSPACE, 369 0, p->p_ucred, &resid, p)) != 0) 370 return error; 371 /* 372 * See if we got all of it 373 */ 374 if (resid != 0) 375 return ENOEXEC; 376 return 0; 377} 378 379/* 380 * elf_load_file(): 381 * 382 * Load a file (interpreter/library) pointed to by path 383 * [stolen from coff_load_shlib()]. Made slightly generic 384 * so it might be used externally. 385 */ 386int 387ELFNAME(load_file)(p, epp, path, vcset, entry, ap, last) 388 struct proc *p; 389 struct exec_package *epp; 390 char *path; 391 struct exec_vmcmd_set *vcset; 392 u_long *entry; 393 struct elf_args *ap; 394 Elf_Addr *last; 395{ 396 int error, i; 397 struct nameidata nd; 398 struct vnode *vp; 399 struct vattr attr; 400 Elf_Ehdr eh; 401 Elf_Phdr *ph = NULL; 402 u_long phsize; 403 char *bp = NULL; 404 Elf_Addr addr = *last; 405 406 bp = path; 407 /* 408 * 1. open file 409 * 2. read filehdr 410 * 3. map text, data, and bss out of it using VM_* 411 */ 412 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path, p); 413 if ((error = namei(&nd)) != 0) 414 return error; 415 vp = nd.ni_vp; 416 417 /* 418 * Similarly, if it's not marked as executable, or it's not a regular 419 * file, we don't allow it to be used. 420 */ 421 if (vp->v_type != VREG) { 422 error = EACCES; 423 goto badunlock; 424 } 425 if ((error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p)) != 0) 426 goto badunlock; 427 428 /* get attributes */ 429 if ((error = VOP_GETATTR(vp, &attr, p->p_ucred, p)) != 0) 430 goto badunlock; 431 432 /* 433 * Check mount point. Though we're not trying to exec this binary, 434 * we will be executing code from it, so if the mount point 435 * disallows execution or set-id-ness, we punt or kill the set-id. 436 */ 437 if (vp->v_mount->mnt_flag & MNT_NOEXEC) { 438 error = EACCES; 439 goto badunlock; 440 } 441 if (vp->v_mount->mnt_flag & MNT_NOSUID) 442 epp->ep_vap->va_mode &= ~(S_ISUID | S_ISGID); 443 444#ifdef notyet /* XXX cgd 960926 */ 445 XXX cgd 960926: (maybe) VOP_OPEN it (and VOP_CLOSE in copyargs?) 446#endif 447 VOP_UNLOCK(vp, 0); 448 449 if ((error = ELFNAME(read_from)(p, vp, 0, (caddr_t) &eh, 450 sizeof(eh))) != 0) 451 goto bad; 452 453 if ((error = ELFNAME(check_header)(&eh, ET_DYN)) != 0) 454 goto bad; 455 456 phsize = eh.e_phnum * sizeof(Elf_Phdr); 457 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 458 459 if ((error = ELFNAME(read_from)(p, vp, eh.e_phoff, 460 (caddr_t) ph, phsize)) != 0) 461 goto bad; 462 463 /* 464 * Load all the necessary sections 465 */ 466 for (i = 0; i < eh.e_phnum; i++) { 467 u_long size = 0; 468 int prot = 0; 469 470 switch (ph[i].p_type) { 471 case PT_LOAD: 472 ELFNAME(load_psection)(vcset, vp, &ph[i], &addr, 473 &size, &prot); 474 /* If entry is within this section it must be text */ 475 if (eh.e_entry >= ph[i].p_vaddr && 476 eh.e_entry < (ph[i].p_vaddr + size)) { 477 /* XXX */ 478 *entry = addr + eh.e_entry; 479#ifdef mips 480 *entry -= ph[i].p_vaddr; 481#endif 482 ap->arg_interp = addr; 483 } 484 addr += size; 485 break; 486 487 case PT_DYNAMIC: 488 case PT_PHDR: 489 case PT_NOTE: 490 break; 491 492 default: 493 break; 494 } 495 } 496 497 free((char *)ph, M_TEMP); 498 *last = addr; 499 vrele(vp); 500 return 0; 501 502badunlock: 503 VOP_UNLOCK(vp, 0); 504 505bad: 506 if (ph != NULL) 507 free((char *)ph, M_TEMP); 508#ifdef notyet /* XXX cgd 960926 */ 509 (maybe) VOP_CLOSE it 510#endif 511 vrele(vp); 512 return error; 513} 514 515/* 516 * exec_elf_makecmds(): Prepare an Elf binary's exec package 517 * 518 * First, set of the various offsets/lengths in the exec package. 519 * 520 * Then, mark the text image busy (so it can be demand paged) or error 521 * out if this is not possible. Finally, set up vmcmds for the 522 * text, data, bss, and stack segments. 523 */ 524int 525ELFNAME2(exec,makecmds)(p, epp) 526 struct proc *p; 527 struct exec_package *epp; 528{ 529 Elf_Ehdr *eh = epp->ep_hdr; 530 Elf_Phdr *ph, *pp; 531 Elf_Addr phdr = 0, pos = 0; 532 int error, i, n, nload; 533 char interp[MAXPATHLEN]; 534 u_long phsize; 535 536 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 537 return ENOEXEC; 538 539 /* 540 * XXX allow for executing shared objects. It seems silly 541 * but other ELF-based systems allow it as well. 542 */ 543 if (ELFNAME(check_header)(eh, ET_EXEC) != 0 && 544 ELFNAME(check_header)(eh, ET_DYN) != 0) 545 return ENOEXEC; 546 547 /* 548 * check if vnode is in open for writing, because we want to 549 * demand-page out of it. if it is, don't do it, for various 550 * reasons 551 */ 552 if (epp->ep_vp->v_writecount != 0) { 553#ifdef DIAGNOSTIC 554 if (epp->ep_vp->v_flag & VTEXT) 555 panic("exec: a VTEXT vnode has writecount != 0\n"); 556#endif 557 return ETXTBSY; 558 } 559 /* 560 * Allocate space to hold all the program headers, and read them 561 * from the file 562 */ 563 phsize = eh->e_phnum * sizeof(Elf_Phdr); 564 ph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 565 566 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 567 (caddr_t) ph, phsize)) != 0) 568 goto bad; 569 570 epp->ep_taddr = epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 571 epp->ep_daddr = epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 572 573 interp[0] = '\0'; 574 575 for (i = 0; i < eh->e_phnum; i++) { 576 pp = &ph[i]; 577 if (pp->p_type == PT_INTERP) { 578 if (pp->p_filesz >= sizeof(interp)) 579 goto bad; 580 if ((error = ELFNAME(read_from)(p, epp->ep_vp, 581 pp->p_offset, (caddr_t) interp, 582 pp->p_filesz)) != 0) 583 goto bad; 584 break; 585 } 586 } 587 588 /* 589 * Setup things for native emulation. 590 */ 591 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 592 pos = ELFDEFNNAME(NO_ADDR); 593 594 /* 595 * On the same architecture, we may be emulating different systems. 596 * See which one will accept this executable. This currently only 597 * applies to SVR4, and IBCS2 on the i386 and Linux on the i386 598 * and the Alpha. 599 * 600 * Probe functions would normally see if the interpreter (if any) 601 * exists. Emulation packages may possibly replace the interpreter in 602 * interp[] with a changed path (/emul/xxx/<path>), and also 603 * set the ep_emul field in the exec package structure. 604 */ 605 n = sizeof(ELFNAME(probe_funcs)) / sizeof(ELFNAME(probe_funcs)[0]); 606 if (n != 0) { 607 error = ENOEXEC; 608 for (i = 0; i < n && error; i++) 609 error = ELFNAME(probe_funcs)[i](p, epp, eh, 610 interp, &pos); 611#ifdef notyet 612 /* 613 * We should really use a signature in our native binaries 614 * and have our own probe function for matching binaries, 615 * before trying the emulations. For now, if the emulation 616 * probes failed we default to native. 617 */ 618 if (error) 619 goto bad; 620#endif 621 } 622 623 /* 624 * Load all the necessary sections 625 */ 626 for (i = nload = 0; i < eh->e_phnum; i++) { 627 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 628 u_long size = 0; 629 int prot = 0; 630 631 pp = &ph[i]; 632 633 switch (ph[i].p_type) { 634 case PT_LOAD: 635 /* 636 * XXX 637 * Can handle only 2 sections: text and data 638 */ 639 if (nload++ == 2) 640 goto bad; 641 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 642 &ph[i], &addr, &size, &prot); 643 644 /* 645 * Decide whether it's text or data by looking 646 * at the entry point. 647 */ 648 if (eh->e_entry >= addr && 649 eh->e_entry < (addr + size)) { 650 epp->ep_taddr = addr; 651 epp->ep_tsize = size; 652 if (epp->ep_daddr == ELFDEFNNAME(NO_ADDR)) { 653 epp->ep_daddr = addr; 654 epp->ep_dsize = size; 655 } 656 } else { 657 epp->ep_daddr = addr; 658 epp->ep_dsize = size; 659 } 660 break; 661 662 case PT_SHLIB: 663#ifndef COMPAT_IBCS2 /* SCO has these sections */ 664 error = ENOEXEC; 665 goto bad; 666#endif 667 668 case PT_INTERP: 669 /* Already did this one */ 670 case PT_DYNAMIC: 671 case PT_NOTE: 672 break; 673 674 case PT_PHDR: 675 /* Note address of program headers (in text segment) */ 676 phdr = pp->p_vaddr; 677 break; 678 679 default: 680 /* 681 * Not fatal; we don't need to understand everything. 682 */ 683 break; 684 } 685 } 686 687 /* this breaks on, e.g., OpenBSD-compatible mips shared binaries. */ 688#ifndef ELF_INTERP_NON_RELOCATABLE 689 /* 690 * If no position to load the interpreter was set by a probe 691 * function, pick the same address that a non-fixed mmap(0, ..) 692 * would (i.e. something safely out of the way). 693 */ 694 if (pos == ELFDEFNNAME(NO_ADDR)) 695 pos = round_page(epp->ep_daddr + MAXDSIZ); 696#endif /* !ELF_INTERP_NON_RELOCATABLE */ 697 698 /* 699 * Check if we found a dynamically linked binary and arrange to load 700 * it's interpreter 701 */ 702 if (interp[0]) { 703 struct elf_args *ap; 704 705 ap = (struct elf_args *)malloc(sizeof(struct elf_args), 706 M_TEMP, M_WAITOK); 707 if ((error = ELFNAME(load_file)(p, epp, interp, 708 &epp->ep_vmcmds, &epp->ep_entry, ap, &pos)) != 0) { 709 free((char *)ap, M_TEMP); 710 goto bad; 711 } 712 pos += phsize; 713 ap->arg_phaddr = phdr; 714 715 ap->arg_phentsize = eh->e_phentsize; 716 ap->arg_phnum = eh->e_phnum; 717 ap->arg_entry = eh->e_entry; 718 719 epp->ep_emul_arg = ap; 720 } else 721 epp->ep_entry = eh->e_entry; 722 723#ifdef ELF_MAP_PAGE_ZERO 724 /* Dell SVR4 maps page zero, yeuch! */ 725 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0, 726 VM_PROT_READ); 727#endif 728 free((char *)ph, M_TEMP); 729 vn_marktext(epp->ep_vp); 730 return exec_elf_setup_stack(p, epp); 731 732bad: 733 free((char *)ph, M_TEMP); 734 kill_vmcmds(&epp->ep_vmcmds); 735 return ENOEXEC; 736} 737 738int 739ELFNAME2(netbsd,signature)(p, epp, eh) 740 struct proc *p; 741 struct exec_package *epp; 742 Elf_Ehdr *eh; 743{ 744 Elf_Phdr *hph, *ph; 745 Elf_Nhdr *np = NULL; 746 size_t phsize; 747 int error; 748 749 phsize = eh->e_phnum * sizeof(Elf_Phdr); 750 hph = (Elf_Phdr *)malloc(phsize, M_TEMP, M_WAITOK); 751 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 752 (caddr_t)hph, phsize)) != 0) 753 goto out1; 754 755 for (ph = hph; ph < &hph[eh->e_phnum]; ph++) { 756 if (ph->p_type != PT_NOTE || 757 ph->p_filesz < sizeof(Elf_Nhdr) + ELF_NOTE_NETBSD_NAMESZ) 758 continue; 759 760 np = (Elf_Nhdr *)malloc(ph->p_filesz, M_TEMP, M_WAITOK); 761 if ((error = ELFNAME(read_from)(p, epp->ep_vp, ph->p_offset, 762 (caddr_t)np, ph->p_filesz)) != 0) 763 goto out2; 764 765 if (np->n_type != ELF_NOTE_TYPE_OSVERSION) { 766 free(np, M_TEMP); 767 np = NULL; 768 continue; 769 } 770 771 /* Check the name and description sizes. */ 772 if (np->n_namesz != ELF_NOTE_NETBSD_NAMESZ || 773 np->n_descsz != ELF_NOTE_NETBSD_DESCSZ) 774 goto out3; 775 776 /* Is the name "NetBSD\0\0"? */ 777 if (memcmp((np + 1), ELF_NOTE_NETBSD_NAME, 778 ELF_NOTE_NETBSD_NAMESZ)) 779 goto out3; 780 781 /* XXX: We could check for the specific emulation here */ 782 /* All checks succeeded. */ 783 error = 0; 784 goto out2; 785 } 786 787out3: 788 error = ENOEXEC; 789out2: 790 if (np) 791 free(np, M_TEMP); 792out1: 793 free(hph, M_TEMP); 794 return error; 795} 796 797static int 798ELFNAME2(netbsd,probe)(p, epp, eh, itp, pos) 799 struct proc *p; 800 struct exec_package *epp; 801 Elf_Ehdr *eh; 802 char *itp; 803 Elf_Addr *pos; 804{ 805 int error; 806 807 if ((error = ELFNAME2(netbsd,signature)(p, epp, eh)) != 0) 808 return error; 809 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 810 *pos = ELFDEFNNAME(NO_ADDR); 811 return 0; 812} 813