1 /* $OpenBSD: exec_elf.c,v 1.4 2001/12/05 10:11:23 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1999 Mats O Jansson. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Mats O Jansson. 17 * 4. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef LINT 33 static char rcsid[] = "$OpenBSD: exec_elf.c,v 1.4 2001/12/05 10:11:23 deraadt Exp $"; 34 #endif 35 36 #include <err.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <nlist.h> 40 #include <stdlib.h> 41 #include <stdio.h> 42 #include <unistd.h> 43 #include <sys/exec.h> 44 #include <sys/exec_elf.h> 45 #include <sys/types.h> 46 47 #include "ukc.h" 48 49 caddr_t ptr, rest, pre; 50 Elf_Ehdr elf_ex; 51 Elf_Phdr *elf_phdr; 52 Elf_Shdr *elf_shdr; 53 char *elf_total; 54 char *elf_shstrtab; 55 off_t elf_size; 56 57 caddr_t 58 elf_adjust(x) 59 caddr_t x; 60 { 61 int i; 62 Elf_Shdr *s; 63 unsigned long y = 0; 64 65 s = elf_shdr; 66 67 for (i = 0; i < elf_ex.e_shnum; i++) { 68 if (s[i].sh_addr == 0) 69 continue; 70 if (((unsigned long)x >= s[i].sh_addr) && 71 ((unsigned long)x < (s[i].sh_addr+s[i].sh_size))) { 72 y = (unsigned long)&elf_total[(unsigned long)x - 73 s[i].sh_addr + s[i].sh_offset]; 74 break; 75 } 76 } 77 78 return((caddr_t)y); 79 } 80 81 caddr_t 82 elf_readjust(x) 83 caddr_t x; 84 { 85 int i; 86 Elf_Shdr *s; 87 unsigned long y = 0; 88 89 s = elf_shdr; 90 91 for (i = 0; i < elf_ex.e_shnum; i++) { 92 if (s[i].sh_addr == 0) 93 continue; 94 if (((x - elf_total) >= s[i].sh_offset) && 95 ((x - elf_total) <= (s[i].sh_offset + s[i].sh_size))) 96 y = x - elf_total + s[i].sh_addr; 97 } 98 99 return((caddr_t)y); 100 } 101 102 int 103 elf_check(file) 104 char *file; 105 { 106 int fd, ret = 1; 107 108 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 109 return (0); 110 111 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 112 ret = 0; 113 114 if (ret) { 115 if (!IS_ELF(elf_ex)) 116 ret = 0; 117 } 118 119 close(fd); 120 return (ret); 121 } 122 123 void 124 elf_loadkernel(file) 125 char *file; 126 { 127 int fd; 128 Elf_Phdr *p; 129 Elf_Shdr *s; 130 131 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 132 err(1, "%s", file); 133 134 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 135 errx(1, "can't read elf header"); 136 137 if (!IS_ELF(elf_ex)) 138 errx(1, "bad elf magic\n"); 139 140 elf_size = lseek(fd, 0L, SEEK_END); 141 (void)lseek(fd, 0L, SEEK_SET); 142 elf_total = malloc(elf_size); 143 144 if (read(fd, elf_total, elf_size) != elf_size) 145 errx(1, "can't read elf kernel"); 146 147 elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff]; 148 elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff]; 149 150 p = elf_phdr; 151 s = elf_shdr; 152 153 elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset]; 154 155 close(fd); 156 } 157 158 void 159 elf_savekernel(outfile) 160 161 char *outfile; 162 { 163 int fd; 164 165 if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0) 166 err(1, "%s", outfile); 167 168 if (write(fd, elf_total, elf_size) != elf_size) 169 errx(1, "can't write file %s", outfile); 170 171 close(fd); 172 } 173