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