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