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