aout.c revision 1.4.6.2 1 1.4.6.2 thorpej /* $NetBSD: aout.c,v 1.4.6.2 2002/01/10 19:40:22 thorpej Exp $ */
2 1.4.6.2 thorpej
3 1.4.6.2 thorpej /*-
4 1.4.6.2 thorpej * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.4.6.2 thorpej * All rights reserved.
6 1.4.6.2 thorpej *
7 1.4.6.2 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.4.6.2 thorpej * by Leo Weppelman.
9 1.4.6.2 thorpej *
10 1.4.6.2 thorpej * Redistribution and use in source and binary forms, with or without
11 1.4.6.2 thorpej * modification, are permitted provided that the following conditions
12 1.4.6.2 thorpej * are met:
13 1.4.6.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.4.6.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.4.6.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.4.6.2 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.4.6.2 thorpej * documentation and/or other materials provided with the distribution.
18 1.4.6.2 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.4.6.2 thorpej * must display the following acknowledgement:
20 1.4.6.2 thorpej * This product includes software developed by the NetBSD
21 1.4.6.2 thorpej * Foundation, Inc. and its contributors.
22 1.4.6.2 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4.6.2 thorpej * contributors may be used to endorse or promote products derived
24 1.4.6.2 thorpej * from this software without specific prior written permission.
25 1.4.6.2 thorpej *
26 1.4.6.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4.6.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4.6.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4.6.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4.6.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4.6.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4.6.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4.6.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4.6.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4.6.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.4.6.2 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.4.6.2 thorpej */
38 1.4.6.2 thorpej
39 1.4.6.2 thorpej
40 1.4.6.2 thorpej #ifdef TOSTOOLS
41 1.4.6.2 thorpej #include <stdio.h>
42 1.4.6.2 thorpej #include <stdlib.h>
43 1.4.6.2 thorpej #include <unistd.h>
44 1.4.6.2 thorpej #include <string.h>
45 1.4.6.2 thorpej #include <sys/types.h>
46 1.4.6.2 thorpej #include <a_out.h>
47 1.4.6.2 thorpej
48 1.4.6.2 thorpej #define MALLOC(x) malloc(x)
49 1.4.6.2 thorpej
50 1.4.6.2 thorpej #else
51 1.4.6.2 thorpej
52 1.4.6.2 thorpej #include <stand.h>
53 1.4.6.2 thorpej #include <atari_stand.h>
54 1.4.6.2 thorpej #include <string.h>
55 1.4.6.2 thorpej #include <libkern.h>
56 1.4.6.2 thorpej #include <sys/exec_aout.h>
57 1.4.6.2 thorpej
58 1.4.6.2 thorpej #define MALLOC(x) alloc(x)
59 1.4.6.2 thorpej #endif
60 1.4.6.2 thorpej
61 1.4.6.2 thorpej #include "tosdefs.h"
62 1.4.6.2 thorpej #include "kparamb.h"
63 1.4.6.2 thorpej #include "libtos.h"
64 1.4.6.2 thorpej #include "cread.h"
65 1.4.6.2 thorpej
66 1.4.6.2 thorpej
67 1.4.6.2 thorpej #ifdef TOSTOOLS
68 1.4.6.2 thorpej /*
69 1.4.6.2 thorpej * Assume compiling under TOS or MINT. The page-size will always
70 1.4.6.2 thorpej * be incorrect then (if it is defined anyway).
71 1.4.6.2 thorpej */
72 1.4.6.2 thorpej #ifdef __LDPGSZ
73 1.4.6.2 thorpej #undef __LDPGSZ
74 1.4.6.2 thorpej #endif
75 1.4.6.2 thorpej
76 1.4.6.2 thorpej #define __LDPGSZ (8*1024) /* Page size for NetBSD */
77 1.4.6.2 thorpej
78 1.4.6.2 thorpej #endif /* TOSTOOLS */
79 1.4.6.2 thorpej
80 1.4.6.2 thorpej /*
81 1.4.6.2 thorpej * Load an a.out image.
82 1.4.6.2 thorpej * Exit codes:
83 1.4.6.2 thorpej * -1 : Not an a.outfile
84 1.4.6.2 thorpej * 0 : OK
85 1.4.6.2 thorpej * error# : Error during load (*errp might contain error string).
86 1.4.6.2 thorpej */
87 1.4.6.2 thorpej int
88 1.4.6.2 thorpej aout_load(fd, od, errp, loadsyms)
89 1.4.6.2 thorpej int fd;
90 1.4.6.2 thorpej osdsc_t *od;
91 1.4.6.2 thorpej char **errp;
92 1.4.6.2 thorpej int loadsyms;
93 1.4.6.2 thorpej {
94 1.4.6.2 thorpej long textsz, stringsz;
95 1.4.6.2 thorpej struct exec ehdr;
96 1.4.6.2 thorpej int err;
97 1.4.6.2 thorpej
98 1.4.6.2 thorpej *errp = NULL;
99 1.4.6.2 thorpej
100 1.4.6.2 thorpej lseek(fd, (off_t)0, SEEK_SET);
101 1.4.6.2 thorpej if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
102 1.4.6.2 thorpej return -1;
103 1.4.6.2 thorpej
104 1.4.6.2 thorpej #ifdef TOSTOOLS
105 1.4.6.2 thorpej if ((ehdr.a_magic & 0xffff) != NMAGIC)
106 1.4.6.2 thorpej return -1;
107 1.4.6.2 thorpej #else
108 1.4.6.2 thorpej if ((N_GETMAGIC(ehdr) != NMAGIC) && (N_GETMAGIC(ehdr) != OMAGIC))
109 1.4.6.2 thorpej return -1;
110 1.4.6.2 thorpej #endif
111 1.4.6.2 thorpej
112 1.4.6.2 thorpej /*
113 1.4.6.2 thorpej * Extract various sizes from the kernel executable
114 1.4.6.2 thorpej */
115 1.4.6.2 thorpej textsz = (ehdr.a_text + __LDPGSZ - 1) & ~(__LDPGSZ - 1);
116 1.4.6.2 thorpej od->k_esym = 0;
117 1.4.6.2 thorpej od->ksize = textsz + ehdr.a_data + ehdr.a_bss;
118 1.4.6.2 thorpej od->kentry = ehdr.a_entry;
119 1.4.6.2 thorpej
120 1.4.6.2 thorpej if (loadsyms && ehdr.a_syms) {
121 1.4.6.2 thorpej
122 1.4.6.2 thorpej err = 1;
123 1.4.6.2 thorpej if (lseek(fd,ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),0) <= 0)
124 1.4.6.2 thorpej goto error;
125 1.4.6.2 thorpej err = 2;
126 1.4.6.2 thorpej if (read(fd, (char *)&stringsz, sizeof(long)) != sizeof(long))
127 1.4.6.2 thorpej goto error;
128 1.4.6.2 thorpej err = 3;
129 1.4.6.2 thorpej if (lseek(fd, sizeof(ehdr), 0) <= 0)
130 1.4.6.2 thorpej goto error;
131 1.4.6.2 thorpej od->ksize += ehdr.a_syms + sizeof(long) + stringsz;
132 1.4.6.2 thorpej }
133 1.4.6.2 thorpej
134 1.4.6.2 thorpej err = 4;
135 1.4.6.2 thorpej if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL)
136 1.4.6.2 thorpej goto error;
137 1.4.6.2 thorpej
138 1.4.6.2 thorpej /*
139 1.4.6.2 thorpej * Read text & data, clear bss
140 1.4.6.2 thorpej */
141 1.4.6.2 thorpej err = 5;
142 1.4.6.2 thorpej if ((read(fd, (char *)(od->kstart), ehdr.a_text) != ehdr.a_text)
143 1.4.6.2 thorpej ||(read(fd,(char *)(od->kstart+textsz),ehdr.a_data) != ehdr.a_data))
144 1.4.6.2 thorpej goto error;
145 1.4.6.2 thorpej bzero(od->kstart + textsz + ehdr.a_data, ehdr.a_bss);
146 1.4.6.2 thorpej
147 1.4.6.2 thorpej /*
148 1.4.6.2 thorpej * Read symbol and string table
149 1.4.6.2 thorpej */
150 1.4.6.2 thorpej if (loadsyms && ehdr.a_syms) {
151 1.4.6.2 thorpej long *p;
152 1.4.6.2 thorpej
153 1.4.6.2 thorpej p = (long *)((od->kstart) + textsz + ehdr.a_data + ehdr.a_bss);
154 1.4.6.2 thorpej *p++ = ehdr.a_syms;
155 1.4.6.2 thorpej err = 6;
156 1.4.6.2 thorpej if (read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
157 1.4.6.2 thorpej goto error;
158 1.4.6.2 thorpej p = (long *)((char *)p + ehdr.a_syms);
159 1.4.6.2 thorpej err = 7;
160 1.4.6.2 thorpej if (read(fd, (char *)p, stringsz) != stringsz)
161 1.4.6.2 thorpej goto error;
162 1.4.6.2 thorpej od->k_esym = (long)((char *)p-(char *)od->kstart +stringsz);
163 1.4.6.2 thorpej }
164 1.4.6.2 thorpej return 0;
165 1.4.6.2 thorpej
166 1.4.6.2 thorpej error:
167 1.4.6.2 thorpej #ifdef TOSTOOLS
168 1.4.6.2 thorpej {
169 1.4.6.2 thorpej static char *errs[] = {
170 1.4.6.2 thorpej /* 1 */ "Cannot seek to string table",
171 1.4.6.2 thorpej /* 2 */ "Cannot read string-table size",
172 1.4.6.2 thorpej /* 3 */ "Cannot seek back to text start",
173 1.4.6.2 thorpej /* 4 */ "Cannot malloc kernel image space",
174 1.4.6.2 thorpej /* 5 */ "Unable to read kernel image",
175 1.4.6.2 thorpej /* 6 */ "Cannot read symbol table",
176 1.4.6.2 thorpej /* 7 */ "Cannot read string table"
177 1.4.6.2 thorpej };
178 1.4.6.2 thorpej *errp = errs[err];
179 1.4.6.2 thorpej }
180 1.4.6.2 thorpej #endif /* TOSTOOLS */
181 1.4.6.2 thorpej
182 1.4.6.2 thorpej return err;
183 1.4.6.2 thorpej }
184