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