|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 1995, 1994, 1993, 1992, 1991, 1990 ! 3: * Open Software Foundation, Inc. ! 4: * ! 5: * Permission to use, copy, modify, and distribute this software and ! 6: * its documentation for any purpose and without fee is hereby granted, ! 7: * provided that the above copyright notice appears in all copies and ! 8: * that both the copyright notice and this permission notice appear in ! 9: * supporting documentation, and that the name of ("OSF") or Open Software ! 10: * Foundation not be used in advertising or publicity pertaining to ! 11: * distribution of the software without specific, written prior permission. ! 12: * ! 13: * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE ! 14: * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ! 15: * FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL OSF BE LIABLE FOR ANY ! 16: * SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES ! 17: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN ! 18: * ACTION OF CONTRACT, NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING ! 19: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! 20: */ ! 21: /* ! 22: * OSF Research Institute MK6.1 (unencumbered) 1/31/1995 ! 23: */ ! 24: ! 25: #include <alloca.h> ! 26: #include <mach/machine/vm_types.h> ! 27: #include <mach/exec/elf.h> ! 28: #include <mach/exec/exec.h> ! 29: ! 30: int exec_load(exec_read_func_t *read, exec_read_exec_func_t *read_exec, ! 31: void *handle, exec_info_t *out_info) ! 32: { ! 33: vm_size_t actual; ! 34: Elf32_Ehdr x; ! 35: Elf32_Phdr *phdr, *ph; ! 36: vm_size_t phsize; ! 37: int i; ! 38: int result; ! 39: ! 40: /* Read the ELF header. */ ! 41: if ((result = (*read)(handle, 0, &x, sizeof(x), &actual)) != 0) ! 42: return result; ! 43: if (actual < sizeof(x)) ! 44: return EX_NOT_EXECUTABLE; ! 45: ! 46: if ((x.e_ident[EI_MAG0] != ELFMAG0) || ! 47: (x.e_ident[EI_MAG1] != ELFMAG1) || ! 48: (x.e_ident[EI_MAG2] != ELFMAG2) || ! 49: (x.e_ident[EI_MAG3] != ELFMAG3)) ! 50: return EX_NOT_EXECUTABLE; ! 51: ! 52: /* Make sure the file is of the right architecture. */ ! 53: if ((x.e_ident[EI_CLASS] != ELFCLASS32) || ! 54: (x.e_ident[EI_DATA] != MY_EI_DATA) || ! 55: (x.e_machine != MY_E_MACHINE)) ! 56: return EX_WRONG_ARCH; ! 57: ! 58: /* XXX others */ ! 59: out_info->entry = (vm_offset_t) x.e_entry; ! 60: ! 61: phsize = x.e_phnum * x.e_phentsize; ! 62: phdr = (Elf32_Phdr *)alloca(phsize); ! 63: ! 64: result = (*read)(handle, x.e_phoff, phdr, phsize, &actual); ! 65: if (result) ! 66: return result; ! 67: if (actual < phsize) ! 68: return EX_CORRUPT; ! 69: ! 70: for (i = 0; i < x.e_phnum; i++) ! 71: { ! 72: ph = (Elf32_Phdr *)((vm_offset_t)phdr + i * x.e_phentsize); ! 73: if (ph->p_type == PT_LOAD) ! 74: { ! 75: exec_sectype_t type = EXEC_SECTYPE_ALLOC | ! 76: EXEC_SECTYPE_LOAD; ! 77: if (ph->p_flags & PF_R) type |= EXEC_SECTYPE_READ; ! 78: if (ph->p_flags & PF_W) type |= EXEC_SECTYPE_WRITE; ! 79: if (ph->p_flags & PF_X) type |= EXEC_SECTYPE_EXECUTE; ! 80: result = (*read_exec)(handle, ! 81: ph->p_offset, ph->p_filesz, ! 82: ph->p_vaddr, ph->p_memsz, type); ! 83: } ! 84: } ! 85: ! 86: return 0; ! 87: } ! 88:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.