exec_elf32.c revision 1.9
1/* $NetBSD: exec_elf32.c,v 1.9 1996/09/26 20:51:05 cgd Exp $ */ 2 3/* 4 * Copyright (c) 1996 Christopher G. Demetriou 5 * Copyright (c) 1994 Christos Zoulas 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31/* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 32#ifndef ELFSIZE 33#define ELFSIZE 32 34#endif 35 36#if defined(COMPAT_SVR4) || defined(COMPAT_LINUX) /* XXX should die */ 37#undef EXEC_ELF32 /* XXX should die */ 38#define EXEC_ELF32 /* XXX should die */ 39#endif /* XXX should die */ 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/kernel.h> 44#include <sys/proc.h> 45#include <sys/malloc.h> 46#include <sys/namei.h> 47#include <sys/vnode.h> 48#include <sys/exec.h> 49#include <sys/exec_elf.h> 50#include <sys/fcntl.h> 51#include <sys/syscall.h> 52#include <sys/signalvar.h> 53 54#include <sys/mman.h> 55#include <vm/vm.h> 56#include <vm/vm_param.h> 57#include <vm/vm_map.h> 58 59#include <machine/cpu.h> 60#include <machine/reg.h> 61#include <machine/exec.h> 62 63#ifdef COMPAT_LINUX 64#include <compat/linux/linux_exec.h> 65#endif 66 67#ifdef COMPAT_SVR4 68#include <compat/svr4/svr4_exec.h> 69#endif 70 71#define CONCAT(x,y) __CONCAT(x,y) 72#define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x))) 73#define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y)))) 74#define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE)) 75#define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x))) 76 77int ELFNAME(check_header) __P((Elf_Ehdr *, int)); 78int ELFNAME(load_file) __P((struct proc *, char *, struct exec_vmcmd_set *, 79 u_long *, struct elf_args *, Elf_Addr *)); 80static void ELFNAME(load_psection) __P((struct exec_vmcmd_set *, 81 struct vnode *, Elf_Phdr *, Elf_Addr *, u_long *, int *)); 82 83extern char sigcode[], esigcode[]; 84#ifdef SYSCALL_DEBUG 85extern char *syscallnames[]; 86#endif 87 88struct emul ELFNAMEEND(emul_netbsd) = { 89 "netbsd", 90 NULL, 91 sendsig, 92 SYS_syscall, 93 SYS_MAXSYSCALL, 94 sysent, 95#ifdef SYSCALL_DEBUG 96 syscallnames, 97#else 98 NULL, 99#endif 100 ELF_AUX_ENTRIES * sizeof(AuxInfo), 101 ELFNAME(copyargs), 102 setregs, 103 sigcode, 104 esigcode, 105}; 106 107int (*ELFNAME(probe_funcs)[]) __P((struct proc *, struct exec_package *, 108 Elf_Ehdr *, char *, Elf_Addr *)) = { 109#if defined(COMPAT_SVR4) && (ELFSIZE == 32) 110 ELFNAME2(svr4,probe), /* XXX not 64-bit safe */ 111#endif 112#if defined(COMPAT_LINUX) && (ELFSIZE == 32) 113 ELFNAME2(linux,probe), /* XXX not 64-bit safe */ 114#endif 115}; 116 117#define ELF_ALIGN(a, b) ((a) & ~((b) - 1)) 118 119/* 120 * Copy arguments onto the stack in the normal way, but add some 121 * extra information in case of dynamic binding. 122 */ 123void * 124ELFNAME(copyargs)(pack, arginfo, stack, argp) 125 struct exec_package *pack; 126 struct ps_strings *arginfo; 127 void *stack; 128 void *argp; 129{ 130 size_t len; 131 AuxInfo ai[ELF_AUX_ENTRIES], *a; 132 struct elf_args *ap; 133 134 stack = copyargs(pack, arginfo, stack, argp); 135 if (!stack) 136 return NULL; 137 138 /* 139 * Push extra arguments on the stack needed by dynamically 140 * linked binaries 141 */ 142 if ((ap = (struct elf_args *) pack->ep_emul_arg)) { 143 a = ai; 144 145 a->au_id = AUX_phdr; 146 a->au_v = ap->arg_phaddr; 147 a++; 148 149 a->au_id = AUX_phent; 150 a->au_v = ap->arg_phentsize; 151 a++; 152 153 a->au_id = AUX_phnum; 154 a->au_v = ap->arg_phnum; 155 a++; 156 157 a->au_id = AUX_pagesz; 158 a->au_v = NBPG; 159 a++; 160 161 a->au_id = AUX_base; 162 a->au_v = ap->arg_interp; 163 a++; 164 165 a->au_id = AUX_flags; 166 a->au_v = 0; 167 a++; 168 169 a->au_id = AUX_entry; 170 a->au_v = ap->arg_entry; 171 a++; 172 173 a->au_id = AUX_null; 174 a->au_v = 0; 175 a++; 176 177 free((char *) ap, M_TEMP); 178 pack->ep_emul_arg = NULL; 179 len = ELF_AUX_ENTRIES * sizeof (AuxInfo); 180 if (copyout(ai, stack, len)) 181 return NULL; 182 stack += len; 183 } 184 return stack; 185} 186 187/* 188 * elf_check_header(): 189 * 190 * Check header for validity; return 0 of ok ENOEXEC if error 191 * 192 * XXX machine type needs to be moved to <machine/param.h> so 193 * just one comparison can be done. Unfortunately, there is both 194 * em_486 and em_386, so this would not work on the i386. 195 */ 196int 197ELFNAME(check_header)(eh, type) 198 Elf_Ehdr *eh; 199 int type; 200{ 201 202 if (bcmp(eh->e_ident, Elf_e_ident, Elf_e_siz) != 0) 203 return ENOEXEC; 204 205 switch (eh->e_machine) { 206 207#if 0 /* XXX notyet */ 208 ELFDEFNNAME(MACHDEP_ID_CASES); 209#else /* XXX */ 210#ifdef i386 211 case Elf_em_386: 212 case Elf_em_486: 213 break; 214#endif 215#ifdef sparc 216 case Elf_em_sparc: 217 break; 218#endif 219#ifdef mips 220 case Elf_em_mips: 221 break; 222#endif 223#ifdef alpha 224 case Elf_em_alpha: 225 break; 226#endif 227#endif /* XXX */ 228 229 default: 230 return ENOEXEC; 231 } 232 233 if (eh->e_type != type) 234 return ENOEXEC; 235 236 return 0; 237} 238 239/* 240 * elf_load_psection(): 241 * 242 * Load a psection at the appropriate address 243 */ 244static void 245ELFNAME(load_psection)(vcset, vp, ph, addr, size, prot) 246 struct exec_vmcmd_set *vcset; 247 struct vnode *vp; 248 Elf_Phdr *ph; 249 Elf_Addr *addr; 250 u_long *size; 251 int *prot; 252{ 253 u_long uaddr, msize, psize, rm, rf; 254 long diff, offset; 255 256 /* 257 * If the user specified an address, then we load there. 258 */ 259 if (*addr != ELFDEFNNAME(NO_ADDR)) { 260 if (ph->p_align > 1) { 261 *addr = ELF_ALIGN(*addr + ph->p_align, ph->p_align); 262 uaddr = ELF_ALIGN(ph->p_vaddr, ph->p_align); 263 } else 264 uaddr = ph->p_vaddr; 265 diff = ph->p_vaddr - uaddr; 266 } else { 267 *addr = uaddr = ph->p_vaddr; 268 if (ph->p_align > 1) 269 *addr = ELF_ALIGN(uaddr, ph->p_align); 270 diff = uaddr - *addr; 271 } 272 273 *prot |= (ph->p_flags & Elf_pf_r) ? VM_PROT_READ : 0; 274 *prot |= (ph->p_flags & Elf_pf_w) ? VM_PROT_WRITE : 0; 275 *prot |= (ph->p_flags & Elf_pf_x) ? VM_PROT_EXECUTE : 0; 276 277 offset = ph->p_offset - diff; 278 *size = ph->p_filesz + diff; 279 msize = ph->p_memsz + diff; 280 psize = round_page(*size); 281 282 if ((ph->p_flags & Elf_pf_w) != 0) { 283 /* 284 * Because the pagedvn pager can't handle zero fill of the last 285 * data page if it's not page aligned we map the last page 286 * readvn. 287 */ 288 psize = trunc_page(*size); 289 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 290 offset, *prot); 291 if(psize != *size) 292 NEW_VMCMD(vcset, vmcmd_map_readvn, *size - psize, 293 *addr + psize, vp, offset + psize, *prot); 294 } else 295 NEW_VMCMD(vcset, vmcmd_map_pagedvn, psize, *addr, vp, 296 offset, *prot); 297 298 /* 299 * Check if we need to extend the size of the segment 300 */ 301 rm = round_page(*addr + msize); 302 rf = round_page(*addr + *size); 303 304 if (rm != rf) { 305 NEW_VMCMD(vcset, vmcmd_map_zero, rm - rf, rf, NULLVP, 0, *prot); 306 *size = msize; 307 } 308} 309 310/* 311 * elf_read_from(): 312 * 313 * Read from vnode into buffer at offset. 314 */ 315int 316ELFNAME(read_from)(p, vp, off, buf, size) 317 struct vnode *vp; 318 u_long off; 319 struct proc *p; 320 caddr_t buf; 321 int size; 322{ 323 int error; 324 int resid; 325 326 if ((error = vn_rdwr(UIO_READ, vp, buf, size, 327 off, UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, 328 &resid, p)) != 0) 329 return error; 330 /* 331 * See if we got all of it 332 */ 333 if (resid != 0) 334 return ENOEXEC; 335 return 0; 336} 337 338/* 339 * elf_load_file(): 340 * 341 * Load a file (interpreter/library) pointed to by path 342 * [stolen from coff_load_shlib()]. Made slightly generic 343 * so it might be used externally. 344 */ 345int 346ELFNAME(load_file)(p, path, vcset, entry, ap, last) 347 struct proc *p; 348 char *path; 349 struct exec_vmcmd_set *vcset; 350 u_long *entry; 351 struct elf_args *ap; 352 Elf_Addr *last; 353{ 354 int error, i; 355 struct nameidata nd; 356 Elf_Ehdr eh; 357 Elf_Phdr *ph = NULL; 358 u_long phsize; 359 char *bp = NULL; 360 Elf_Addr addr = *last; 361 362 bp = path; 363 /* 364 * 1. open file 365 * 2. read filehdr 366 * 3. map text, data, and bss out of it using VM_* 367 */ 368 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path, p); 369 if ((error = namei(&nd)) != 0) { 370 return error; 371 } 372 if ((error = ELFNAME(read_from)(p, nd.ni_vp, 0, (caddr_t) &eh, 373 sizeof(eh))) != 0) 374 goto bad; 375 376 if ((error = ELFNAME(check_header)(&eh, Elf_et_dyn)) != 0) 377 goto bad; 378 379 phsize = eh.e_phnum * sizeof(Elf_Phdr); 380 ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK); 381 382 if ((error = ELFNAME(read_from)(p, nd.ni_vp, eh.e_phoff, 383 (caddr_t) ph, phsize)) != 0) 384 goto bad; 385 386 /* 387 * Load all the necessary sections 388 */ 389 for (i = 0; i < eh.e_phnum; i++) { 390 u_long size = 0; 391 int prot = 0; 392 393 switch (ph[i].p_type) { 394 case Elf_pt_load: 395 ELFNAME(load_psection)(vcset, nd.ni_vp, &ph[i], &addr, 396 &size, &prot); 397 /* If entry is within this section it must be text */ 398 if (eh.e_entry >= ph[i].p_vaddr && 399 eh.e_entry < (ph[i].p_vaddr + size)) { 400 *entry = addr + eh.e_entry; 401 ap->arg_interp = addr; 402 } 403 addr += size; 404 break; 405 406 case Elf_pt_dynamic: 407 case Elf_pt_phdr: 408 case Elf_pt_note: 409 break; 410 411 default: 412 break; 413 } 414 } 415 416bad: 417 if (ph != NULL) 418 free((char *) ph, M_TEMP); 419 420 *last = addr; 421 vrele(nd.ni_vp); 422 return error; 423} 424 425/* 426 * exec_elf_makecmds(): Prepare an Elf binary's exec package 427 * 428 * First, set of the various offsets/lengths in the exec package. 429 * 430 * Then, mark the text image busy (so it can be demand paged) or error 431 * out if this is not possible. Finally, set up vmcmds for the 432 * text, data, bss, and stack segments. 433 */ 434int 435ELFNAME2(exec,makecmds)(p, epp) 436 struct proc *p; 437 struct exec_package *epp; 438{ 439 Elf_Ehdr *eh = epp->ep_hdr; 440 Elf_Phdr *ph, *pp; 441 Elf_Addr phdr = 0, pos = 0; 442 int error, i, n, nload; 443 char interp[MAXPATHLEN]; 444 u_long phsize; 445 446 if (epp->ep_hdrvalid < sizeof(Elf_Ehdr)) 447 return ENOEXEC; 448 449 if (ELFNAME(check_header)(eh, Elf_et_exec)) 450 return ENOEXEC; 451 452 /* 453 * check if vnode is in open for writing, because we want to 454 * demand-page out of it. if it is, don't do it, for various 455 * reasons 456 */ 457 if (epp->ep_vp->v_writecount != 0) { 458#ifdef DIAGNOSTIC 459 if (epp->ep_vp->v_flag & VTEXT) 460 panic("exec: a VTEXT vnode has writecount != 0\n"); 461#endif 462 return ETXTBSY; 463 } 464 /* 465 * Allocate space to hold all the program headers, and read them 466 * from the file 467 */ 468 phsize = eh->e_phnum * sizeof(Elf_Phdr); 469 ph = (Elf_Phdr *) malloc(phsize, M_TEMP, M_WAITOK); 470 471 if ((error = ELFNAME(read_from)(p, epp->ep_vp, eh->e_phoff, 472 (caddr_t) ph, phsize)) != 0) 473 goto bad; 474 475 epp->ep_tsize = ELFDEFNNAME(NO_ADDR); 476 epp->ep_dsize = ELFDEFNNAME(NO_ADDR); 477 478 interp[0] = '\0'; 479 480 for (i = 0; i < eh->e_phnum; i++) { 481 pp = &ph[i]; 482 if (pp->p_type == Elf_pt_interp) { 483 if (pp->p_filesz >= sizeof(interp)) 484 goto bad; 485 if ((error = ELFNAME(read_from)(p, epp->ep_vp, pp->p_offset, 486 (caddr_t) interp, pp->p_filesz)) != 0) 487 goto bad; 488 break; 489 } 490 } 491 492 /* 493 * Setup things for native emulation. 494 */ 495 epp->ep_emul = &ELFNAMEEND(emul_netbsd); 496 pos = ELFDEFNNAME(NO_ADDR); 497 498 /* 499 * On the same architecture, we may be emulating different systems. 500 * See which one will accept this executable. This currently only 501 * applies to Linux and SVR4 on the i386. 502 * 503 * Probe functions would normally see if the interpreter (if any) 504 * exists. Emulation packages may possibly replace the interpreter in 505 * interp[] with a changed path (/emul/xxx/<path>), and also 506 * set the ep_emul field in the exec package structure. 507 */ 508 if ((n = sizeof ELFNAME(probe_funcs) / sizeof ELFNAME(probe_funcs)[0])) { 509 error = ENOEXEC; 510 for (i = 0; i < n && error; i++) 511 error = ELFNAME(probe_funcs)[i](p, epp, eh, interp, &pos); 512 513#ifdef notyet 514 /* 515 * We should really use a signature in our native binaries 516 * and have our own probe function for matching binaries, 517 * before trying the emulations. For now, if the emulation 518 * probes failed we default to native. 519 */ 520 if (error) 521 goto bad; 522#endif 523 } 524 525 /* 526 * Load all the necessary sections 527 */ 528 for (i = nload = 0; i < eh->e_phnum; i++) { 529 Elf_Addr addr = ELFDEFNNAME(NO_ADDR); 530 u_long size = 0; 531 int prot = 0; 532 533 pp = &ph[i]; 534 535 switch (ph[i].p_type) { 536 case Elf_pt_load: 537 /* 538 * XXX 539 * Can handle only 2 sections: text and data 540 */ 541 if (nload++ == 2) 542 goto bad; 543 ELFNAME(load_psection)(&epp->ep_vmcmds, epp->ep_vp, 544 &ph[i], &addr, &size, &prot); 545 /* 546 * Decide whether it's text or data by looking 547 * at the entry point. 548 */ 549 if (eh->e_entry >= addr && eh->e_entry < (addr + size)){ 550 epp->ep_taddr = addr; 551 epp->ep_tsize = size; 552 } else { 553 epp->ep_daddr = addr; 554 epp->ep_dsize = size; 555 } 556 break; 557 558 case Elf_pt_shlib: 559 error = ENOEXEC; 560 goto bad; 561 562 case Elf_pt_interp: 563 /* Already did this one */ 564 case Elf_pt_dynamic: 565 case Elf_pt_note: 566 break; 567 568 case Elf_pt_phdr: 569 /* Note address of program headers (in text segment) */ 570 phdr = pp->p_vaddr; 571 break; 572 573 default: 574 /* 575 * Not fatal; we don't need to understand everything. 576 */ 577 break; 578 } 579 } 580 581 /* 582 * If no position to load the interpreter was set by a probe 583 * function, pick the same address that a non-fixed mmap(0, ..) 584 * would (i.e. something safely out of the way). 585 */ 586 if (pos == ELFDEFNNAME(NO_ADDR) && 587 epp->ep_emul == &ELFNAMEEND(emul_netbsd)) 588 pos = round_page(epp->ep_daddr + MAXDSIZ); 589 590 /* 591 * Check if we found a dynamically linked binary and arrange to load 592 * it's interpreter 593 */ 594 if (interp[0]) { 595 struct elf_args *ap; 596 597 ap = (struct elf_args *) malloc(sizeof(struct elf_args), 598 M_TEMP, M_WAITOK); 599 if ((error = ELFNAME(load_file)(p, interp, &epp->ep_vmcmds, 600 &epp->ep_entry, ap, &pos)) != 0) { 601 free((char *) ap, M_TEMP); 602 goto bad; 603 } 604 pos += phsize; 605 ap->arg_phaddr = phdr; 606 607 ap->arg_phentsize = eh->e_phentsize; 608 ap->arg_phnum = eh->e_phnum; 609 ap->arg_entry = eh->e_entry; 610 611 epp->ep_emul_arg = ap; 612 } else 613 epp->ep_entry = eh->e_entry; 614 615#ifdef ELF_MAP_PAGE_ZERO 616 /* Dell SVR4 maps page zero, yeuch! */ 617 NEW_VMCMD(&epp->ep_vmcmds, vmcmd_map_readvn, NBPG, 0, epp->ep_vp, 0, 618 VM_PROT_READ); 619#endif 620 free((char *) ph, M_TEMP); 621 epp->ep_vp->v_flag |= VTEXT; 622 return exec_elf_setup_stack(p, epp); 623 624bad: 625 free((char *) ph, M_TEMP); 626 kill_vmcmds(&epp->ep_vmcmds); 627 return ENOEXEC; 628} 629