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