1 /* $OpenBSD: exec_elf.c,v 1.11 2008/08/12 09:44:26 otto 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 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef LINT 28 static char rcsid[] = "$OpenBSD: exec_elf.c,v 1.11 2008/08/12 09:44:26 otto Exp $"; 29 #endif 30 31 #include <err.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <nlist.h> 35 #include <stdlib.h> 36 #include <stdio.h> 37 #include <unistd.h> 38 #include <sys/exec.h> 39 #include <sys/exec_elf.h> 40 #include <sys/types.h> 41 42 #include "ukc.h" 43 #include "config.h" 44 45 caddr_t ptr, rest, pre; 46 Elf_Ehdr elf_ex; 47 Elf_Phdr *elf_phdr; 48 Elf_Shdr *elf_shdr; 49 char *elf_total; 50 char *elf_shstrtab; 51 off_t elf_size; 52 53 caddr_t elf_adjust(caddr_t); 54 caddr_t elf_readjust(caddr_t); 55 int elf_check(char *); 56 void elf_loadkernel(char *); 57 void elf_savekernel(char *); 58 59 caddr_t 60 elf_adjust(caddr_t x) 61 { 62 int i; 63 Elf_Shdr *s; 64 unsigned long y = 0; 65 66 s = elf_shdr; 67 68 for (i = 0; i < elf_ex.e_shnum; i++) { 69 if (s[i].sh_addr == 0) 70 continue; 71 if (((unsigned long)x >= s[i].sh_addr) && 72 ((unsigned long)x < (s[i].sh_addr+s[i].sh_size))) { 73 y = (unsigned long)&elf_total[(unsigned long)x - 74 s[i].sh_addr + s[i].sh_offset]; 75 break; 76 } 77 } 78 79 return((caddr_t)y); 80 } 81 82 caddr_t 83 elf_readjust(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 = (unsigned long)x - (unsigned long)elf_total + 97 (unsigned long)s[i].sh_addr - s[i].sh_offset; 98 } 99 100 return((caddr_t)y); 101 } 102 103 int 104 elf_check(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(char *file) 125 { 126 int fd; 127 128 if ((fd = open(file, O_RDONLY | O_EXLOCK, 0)) < 0) 129 err(1, "%s", file); 130 131 if (read(fd, (char *)&elf_ex, sizeof(elf_ex)) != sizeof(elf_ex)) 132 errx(1, "can't read elf header"); 133 134 if (!IS_ELF(elf_ex)) 135 errx(1, "bad elf magic"); 136 137 elf_size = lseek(fd, (off_t)0, SEEK_END); 138 (void)lseek(fd, (off_t)0, SEEK_SET); 139 elf_total = emalloc((size_t)elf_size); 140 141 if (read(fd, elf_total, (size_t)elf_size) != elf_size) 142 errx(1, "can't read elf kernel"); 143 144 if (elf_ex.e_phoff > (size_t)elf_size) 145 errx(1, "incorrect ELF header or truncated file"); 146 if (elf_ex.e_shoff > (size_t)elf_size) 147 errx(1, "incorrect ELF header or truncated file"); 148 149 elf_phdr = (Elf_Phdr *)&elf_total[elf_ex.e_phoff]; 150 elf_shdr = (Elf_Shdr *)&elf_total[elf_ex.e_shoff]; 151 152 if ((char *)&elf_shdr[elf_ex.e_shstrndx] + 153 sizeof(elf_shdr[elf_ex.e_shstrndx]) >= elf_total + (size_t)elf_size) 154 errx(1, "incorrect ELF header or truncated file"); 155 156 if ((char *)&elf_shdr[elf_ex.e_shstrndx].sh_offset + 157 sizeof(elf_shdr[elf_ex.e_shstrndx].sh_offset) >= 158 elf_total + (size_t)elf_size) 159 errx(1, "incorrect ELF header or truncated file"); 160 161 elf_shstrtab = &elf_total[elf_shdr[elf_ex.e_shstrndx].sh_offset]; 162 163 close(fd); 164 } 165 166 void 167 elf_savekernel(char *outfile) 168 { 169 int fd; 170 171 if ((fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, 0755)) < 0) 172 err(1, "%s", outfile); 173 174 if (write(fd, elf_total, (size_t)elf_size) != elf_size) 175 errx(1, "can't write file %s", outfile); 176 177 close(fd); 178 } 179