1 /* $NetBSD: aout.c,v 1.5 2002/02/24 20:51:07 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 40 #ifdef TOSTOOLS 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <unistd.h> 44 #include <string.h> 45 #include <sys/types.h> 46 #include <a_out.h> 47 48 #define MALLOC(x) malloc(x) 49 50 #else 51 52 #include <stand.h> 53 #include <atari_stand.h> 54 #include <string.h> 55 #include <libkern.h> 56 #include <sys/exec_aout.h> 57 58 #define MALLOC(x) alloc(x) 59 #endif 60 61 #include "libtos.h" 62 #include "kparamb.h" 63 #include "tosdefs.h" 64 #include "cread.h" 65 66 67 #ifdef TOSTOOLS 68 /* 69 * Assume compiling under TOS or MINT. The page-size will always 70 * be incorrect then (if it is defined anyway). 71 */ 72 #ifdef __LDPGSZ 73 #undef __LDPGSZ 74 #endif 75 76 #define __LDPGSZ (8*1024) /* Page size for NetBSD */ 77 78 #endif /* TOSTOOLS */ 79 80 /* 81 * Load an a.out image. 82 * Exit codes: 83 * -1 : Not an a.outfile 84 * 0 : OK 85 * error# : Error during load (*errp might contain error string). 86 */ 87 int 88 aout_load(fd, od, errp, loadsyms) 89 int fd; 90 osdsc_t *od; 91 char **errp; 92 int loadsyms; 93 { 94 long textsz, stringsz; 95 struct exec ehdr; 96 int err; 97 98 *errp = NULL; 99 100 lseek(fd, (off_t)0, SEEK_SET); 101 if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr)) 102 return -1; 103 104 #ifdef TOSTOOLS 105 if ((ehdr.a_magic & 0xffff) != NMAGIC) 106 return -1; 107 #else 108 if ((N_GETMAGIC(ehdr) != NMAGIC) && (N_GETMAGIC(ehdr) != OMAGIC)) 109 return -1; 110 #endif 111 112 /* 113 * Extract various sizes from the kernel executable 114 */ 115 textsz = (ehdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1); 116 od->k_esym = 0; 117 od->ksize = textsz + ehdr.a_data + ehdr.a_bss; 118 od->kentry = ehdr.a_entry; 119 120 if (loadsyms && ehdr.a_syms) { 121 122 err = 1; 123 if (lseek(fd,ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),0) <= 0) 124 goto error; 125 err = 2; 126 if (read(fd, (char *)&stringsz, sizeof(long)) != sizeof(long)) 127 goto error; 128 err = 3; 129 if (lseek(fd, sizeof(ehdr), 0) <= 0) 130 goto error; 131 od->ksize += ehdr.a_syms + sizeof(long) + stringsz; 132 } 133 134 err = 4; 135 if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL) 136 goto error; 137 138 /* 139 * Read text & data, clear bss 140 */ 141 err = 5; 142 if ((read(fd, (char *)(od->kstart), ehdr.a_text) != ehdr.a_text) 143 ||(read(fd,(char *)(od->kstart+textsz),ehdr.a_data) != ehdr.a_data)) 144 goto error; 145 bzero(od->kstart + textsz + ehdr.a_data, ehdr.a_bss); 146 147 /* 148 * Read symbol and string table 149 */ 150 if (loadsyms && ehdr.a_syms) { 151 long *p; 152 153 p = (long *)((od->kstart) + textsz + ehdr.a_data + ehdr.a_bss); 154 *p++ = ehdr.a_syms; 155 err = 6; 156 if (read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms) 157 goto error; 158 p = (long *)((char *)p + ehdr.a_syms); 159 err = 7; 160 if (read(fd, (char *)p, stringsz) != stringsz) 161 goto error; 162 od->k_esym = (long)((char *)p-(char *)od->kstart +stringsz); 163 } 164 return 0; 165 166 error: 167 #ifdef TOSTOOLS 168 { 169 static char *errs[] = { 170 /* 1 */ "Cannot seek to string table", 171 /* 2 */ "Cannot read string-table size", 172 /* 3 */ "Cannot seek back to text start", 173 /* 4 */ "Cannot malloc kernel image space", 174 /* 5 */ "Unable to read kernel image", 175 /* 6 */ "Cannot read symbol table", 176 /* 7 */ "Cannot read string table" 177 }; 178 *errp = errs[err]; 179 } 180 #endif /* TOSTOOLS */ 181 182 return err; 183 } 184