1 /* $NetBSD: elf.c,v 1.5 2002/02/24 20:51:08 leo Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Leo Weppelman. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #ifdef TOSTOOLS 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <unistd.h> 43 #include <string.h> 44 #include <sys/types.h> 45 #include "exec_elf.h" 46 47 #define MALLOC(x) malloc(x) 48 49 #else 50 51 #include <stand.h> 52 #include <atari_stand.h> 53 #include <string.h> 54 #include <libkern.h> 55 #include <sys/exec_elf.h> 56 57 #define MALLOC(x) alloc(x) 58 #endif 59 60 #include "libtos.h" 61 #include "tosdefs.h" 62 #include "kparamb.h" 63 #include "cread.h" 64 65 /* 66 * Load an elf image. 67 * Exit codes: 68 * -1 : Not an ELF file. 69 * 0 : OK 70 * error# : Error during load (*errp might contain error string). 71 */ 72 #define ELFMAGIC ((ELFMAG0 << 24) | (ELFMAG1 << 16) | \ 73 (ELFMAG2 << 8) | ELFMAG3) 74 75 int 76 elf_load(fd, od, errp, loadsyms) 77 int fd; 78 osdsc_t *od; 79 char **errp; 80 int loadsyms; 81 { 82 int i,j; 83 int err; 84 Elf32_Ehdr ehdr; 85 Elf32_Phdr *phdrs; 86 Elf32_Word symsize, symstart; 87 long kernsize; 88 89 *errp = NULL; 90 lseek(fd, (off_t)0, SEEK_SET); 91 if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr)) 92 return -1; 93 94 if (*((u_int *)ehdr.e_ident) != ELFMAGIC) 95 return -1; 96 97 /* 98 * calculate highest used address 99 */ 100 i = ehdr.e_phnum * sizeof(Elf32_Phdr); 101 err = 1; 102 if ((phdrs = (Elf32_Phdr *)MALLOC(i)) == NULL) 103 goto error; 104 err = 2; 105 if (read(fd, phdrs, i) != i) 106 goto error; 107 108 kernsize = 0; 109 for (i = 0; i < ehdr.e_phnum; i++) { 110 Elf32_Word sum; 111 112 sum = phdrs[i].p_vaddr + phdrs[i].p_memsz; 113 if ((phdrs[i].p_flags & (PF_W|PF_X)) && (sum > kernsize)) 114 kernsize = sum; 115 } 116 117 /* 118 * look for symbols and calculate the size 119 * XXX: This increases the load time by a factor 2 for gzipped 120 * images! 121 */ 122 symsize = 0; 123 symstart = 0; 124 if (loadsyms) { 125 i = ehdr.e_shnum + 1; 126 err = 3; 127 if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff) 128 goto error; 129 while (--i) { 130 Elf32_Shdr shdr; 131 132 err = 4; 133 if (read(fd, &shdr, sizeof(shdr)) != sizeof(shdr)) 134 goto error; 135 if ((shdr.sh_type == SHT_SYMTAB) || (shdr.sh_type == SHT_STRTAB)) 136 symsize += shdr.sh_size; 137 } 138 } 139 140 if (symsize) { 141 symstart = kernsize; 142 kernsize += symsize + sizeof(ehdr) + ehdr.e_shnum*sizeof(Elf32_Shdr); 143 } 144 145 /* 146 * Extract various sizes from the kernel executable 147 */ 148 od->k_esym = symsize ? kernsize : 0; 149 od->ksize = kernsize; 150 od->kentry = ehdr.e_entry; 151 152 err = 5; 153 if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL) 154 goto error; 155 156 /* 157 * Read text & data, clear bss 158 */ 159 for (i = 0; i < ehdr.e_phnum; i++) { 160 u_char *p; 161 Elf32_Phdr *php = &phdrs[i]; 162 163 if (php->p_flags & (PF_W|PF_X)) { 164 err = 6; 165 if (lseek(fd, (off_t)php->p_offset, SEEK_SET) != php->p_offset) 166 goto error; 167 p = (u_char *)(od->kstart) + php->p_vaddr; 168 err = 7; 169 if (read(fd, p, php->p_filesz) != php->p_filesz) 170 goto error; 171 if (php->p_memsz > php->p_filesz) 172 bzero(p + php->p_filesz, php->p_memsz - php->p_filesz); 173 } 174 } 175 176 /* 177 * Read symbols and strings 178 */ 179 if (symsize) { 180 u_char *p, *symtab; 181 int nhdrs; 182 Elf32_Shdr *shp; 183 184 symtab = od->kstart + symstart; 185 186 p = symtab + sizeof(ehdr); 187 nhdrs = ehdr.e_shnum; 188 err = 8; 189 if (lseek(fd, (off_t)ehdr.e_shoff, SEEK_SET) != ehdr.e_shoff) 190 goto error; 191 err = 9; 192 if (read(fd, p, nhdrs * sizeof(*shp)) != nhdrs * sizeof(*shp)) 193 goto error; 194 shp = (Elf32_Shdr*)p; 195 p += nhdrs * sizeof(*shp); 196 for (i = 0; i < nhdrs; i++) { 197 if (shp[i].sh_type == SHT_SYMTAB) { 198 if (shp[i].sh_offset == 0) 199 continue; 200 /* Got the symbol table. */ 201 err = 10; 202 if (lseek(fd, (off_t)shp[i].sh_offset, SEEK_SET) != 203 shp[i].sh_offset) 204 goto error; 205 err = 11; 206 if (read(fd, p, shp[i].sh_size) != shp[i].sh_size) 207 goto error; 208 shp[i].sh_offset = p - symtab; 209 /* Find the string table to go with it. */ 210 j = shp[i].sh_link; 211 if (shp[j].sh_offset == 0) 212 continue; 213 p += shp[i].sh_size; 214 err = 12; 215 if (lseek(fd, (off_t)shp[j].sh_offset, SEEK_SET) != 216 shp[j].sh_offset) 217 goto error; 218 err = 13; 219 if (read(fd, p, shp[j].sh_size) != shp[j].sh_size) 220 goto error; 221 shp[j].sh_offset = p - symtab; 222 /* There should only be one symbol table. */ 223 break; 224 } 225 } 226 ehdr.e_shoff = sizeof(ehdr); 227 bcopy(&ehdr, symtab, sizeof(ehdr)); 228 } 229 return 0; 230 231 error: 232 #ifdef TOSTOOLS 233 { 234 static char *errs[] = { 235 /* 1 */ "Cannot malloc Elf phdr storage space", 236 /* 2 */ "Cannot read Elf32_Phdrs", 237 /* 3 */ "Cannot seek to e_shoff location", 238 /* 4 */ "Cannot read Elf32_shdr", 239 /* 5 */ "Cannot malloc kernel image space", 240 /* 6 */ "Seek error while reading text segment\n", 241 /* 7 */ "Read error in text segment\n", 242 /* 8 */ "Error seeking to section headers", 243 /* 9 */ "Error reading section headers", 244 /* 10 */ "Error seeking to symbols", 245 /* 11 */ "Error reading symbols", 246 /* 12 */ "Error seeking to string table", 247 /* 13 */ "Error reading strings" 248 }; 249 *errp = errs[err]; 250 } 251 #endif /* TOSTOOLS */ 252 253 return err; 254 } 255