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