1 /* $OpenBSD: exec_elf.c,v 1.3 2001/01/25 05:42:12 art 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.3 2001/01/25 05:42:12 art 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 + 74 s[i].sh_offset]; 75 break; 76 } 77 } 78 79 return((caddr_t)y); 80 } 81 82 caddr_t 83 elf_readjust(x) 84 caddr_t x; 85 { 86 int i; 87 Elf_Shdr *s; 88 unsigned long y = 0; 89 90 s = elf_shdr; 91 92 for (i = 0; i < elf_ex.e_shnum; i++) { 93 if (s[i].sh_addr == 0) 94 continue; 95 if (((x - elf_total) >= s[i].sh_offset) && 96 ((x - elf_total) <= (s[i].sh_offset + s[i].sh_size))) 97 y = x - elf_total + s[i].sh_addr; 98 } 99 100 return((caddr_t)y); 101 } 102 103 int 104 elf_check(file) 105 char *file; 106 { 107 int fd,ret = 1; 108 109 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 110 return (0); 111 112 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 113 ret = 0; 114 115 if (ret) { 116 if (!IS_ELF(elf_ex)) 117 ret = 0; 118 } 119 120 close(fd); 121 return (ret); 122 } 123 124 void 125 elf_loadkernel(file) 126 char *file; 127 { 128 int fd; 129 Elf_Phdr *p; 130 Elf_Shdr *s; 131 132 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 133 err(1, "%s", file); 134 135 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 136 errx(1, "can't read elf header"); 137 138 if (!IS_ELF(elf_ex)) 139 errx(1, "bad elf magic\n"); 140 141 elf_size = lseek(fd, 0L, SEEK_END); 142 (void)lseek(fd, 0L, SEEK_SET); 143 elf_total = malloc(elf_size); 144 145 if (read(fd, elf_total, elf_size) != elf_size) 146 errx(1, "can't read elf kernel"); 147 148 elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff]; 149 elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff]; 150 151 p = elf_phdr; 152 s = elf_shdr; 153 154 elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset]; 155 156 close(fd); 157 } 158 159 void 160 elf_savekernel(outfile) 161 162 char *outfile; 163 { 164 int fd; 165 166 if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0) 167 err(1, "%s", outfile); 168 169 if (write(fd, elf_total, elf_size) != elf_size) 170 errx(1, "can't write file %s", outfile); 171 172 close(fd); 173 } 174