aout.c revision 1.9.24.1 1 /* $NetBSD: aout.c,v 1.9.24.1 2009/01/15 23:20:02 bouyer 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 <lib/libsa/stand.h>
53 #include <atari_stand.h>
54 #include <libkern.h>
55 #include <sys/exec_aout.h>
56
57 #define MALLOC(x) alloc(x)
58 #endif
59
60 #include "libtos.h"
61 #include "kparamb.h"
62 #include "tosdefs.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 AOUT_LDPGSZ
72 #undef AOUT_LDPGSZ
73 #endif
74
75 #define AOUT_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(int fd, osdsc_t *od, char **errp, int loadsyms)
88 {
89 long textsz, stringsz;
90 struct exec ehdr;
91 int err;
92
93 *errp = NULL;
94
95 lseek(fd, (off_t)0, SEEK_SET);
96 if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
97 return -1;
98
99 #ifdef TOSTOOLS
100 if ((ehdr.a_magic & 0xffff) != NMAGIC)
101 return -1;
102 #else
103 if ((N_GETMAGIC(ehdr) != NMAGIC) && (N_GETMAGIC(ehdr) != OMAGIC))
104 return -1;
105 #endif
106
107 /*
108 * Extract various sizes from the kernel executable
109 */
110 textsz = (ehdr.a_text + AOUT_LDPGSZ - 1) & ~(AOUT_LDPGSZ - 1);
111 od->k_esym = 0;
112 od->ksize = textsz + ehdr.a_data + ehdr.a_bss;
113 od->kentry = ehdr.a_entry;
114
115 if (loadsyms && ehdr.a_syms) {
116 err = 1;
117 if (lseek(fd, ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),
118 0) <= 0)
119 goto error;
120 err = 2;
121 if (read(fd, (char *)&stringsz, sizeof(long)) != sizeof(long))
122 goto error;
123 err = 3;
124 if (lseek(fd, sizeof(ehdr), 0) <= 0)
125 goto error;
126 od->ksize += ehdr.a_syms + sizeof(long) + stringsz;
127 }
128
129 err = 4;
130 if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL)
131 goto error;
132
133 /*
134 * Read text & data, clear bss
135 */
136 err = 5;
137 if ((read(fd, (char *)(od->kstart), ehdr.a_text) != ehdr.a_text)
138 ||(read(fd,(char *)(od->kstart+textsz),ehdr.a_data) != ehdr.a_data))
139 goto error;
140 bzero(od->kstart + textsz + ehdr.a_data, ehdr.a_bss);
141
142 /*
143 * Read symbol and string table
144 */
145 if (loadsyms && ehdr.a_syms) {
146 long *p;
147
148 p = (long *)((od->kstart) + textsz + ehdr.a_data + ehdr.a_bss);
149 *p++ = ehdr.a_syms;
150 err = 6;
151 if (read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
152 goto error;
153 p = (long *)((char *)p + ehdr.a_syms);
154 err = 7;
155 if (read(fd, (char *)p, stringsz) != stringsz)
156 goto error;
157 od->k_esym = (long)((char *)p-(char *)od->kstart +stringsz);
158 }
159 return 0;
160
161 error:
162 #ifdef TOSTOOLS
163 {
164 static char *errs[] = {
165 /* 1 */ "Cannot seek to string table",
166 /* 2 */ "Cannot read string-table size",
167 /* 3 */ "Cannot seek back to text start",
168 /* 4 */ "Cannot malloc kernel image space",
169 /* 5 */ "Unable to read kernel image",
170 /* 6 */ "Cannot read symbol table",
171 /* 7 */ "Cannot read string table"
172 };
173 *errp = errs[err];
174 }
175 #endif /* TOSTOOLS */
176
177 return err;
178 }
179