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