xref: /netbsd-src/sys/arch/atari/stand/tostools/libtos/aout.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: aout.c,v 1.10 2008/04/28 20:23:15 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 
33 #ifdef TOSTOOLS
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <a_out.h>
40 
41 #define	MALLOC(x)	malloc(x)
42 
43 #else
44 
45 #include <lib/libsa/stand.h>
46 #include <atari_stand.h>
47 #include <string.h>
48 #include <libkern.h>
49 #include <sys/exec_aout.h>
50 
51 #define	MALLOC(x)	alloc(x)
52 #endif
53 
54 #include "libtos.h"
55 #include "kparamb.h"
56 #include "tosdefs.h"
57 #include "cread.h"
58 
59 
60 #ifdef TOSTOOLS
61 /*
62  * Assume compiling under TOS or MINT. The page-size will always
63  * be incorrect then (if it is defined anyway).
64  */
65 #ifdef AOUT_LDPGSZ
66 #undef AOUT_LDPGSZ
67 #endif
68 
69 #define AOUT_LDPGSZ	(8*1024)	/* Page size for NetBSD		*/
70 
71 #endif /* TOSTOOLS */
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(int fd, osdsc_t *od, char **errp, int loadsyms)
82 {
83 	long		textsz, stringsz;
84 	struct exec	ehdr;
85 	int		err;
86 
87 	*errp = NULL;
88 
89 	lseek(fd, (off_t)0, SEEK_SET);
90 	if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
91 		return -1;
92 
93 #ifdef TOSTOOLS
94 	if ((ehdr.a_magic & 0xffff) != NMAGIC)
95 		return -1;
96 #else
97 	if ((N_GETMAGIC(ehdr) != NMAGIC) && (N_GETMAGIC(ehdr) != OMAGIC))
98 		return -1;
99 #endif
100 
101 	/*
102 	 * Extract various sizes from the kernel executable
103 	 */
104 	textsz     = (ehdr.a_text + AOUT_LDPGSZ - 1) & ~(AOUT_LDPGSZ - 1);
105 	od->k_esym = 0;
106 	od->ksize  = textsz + ehdr.a_data + ehdr.a_bss;
107 	od->kentry = ehdr.a_entry;
108 
109 	if (loadsyms && ehdr.a_syms) {
110 		err = 1;
111 		if (lseek(fd, ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),
112 			  0) <= 0)
113 			goto error;
114 		err = 2;
115 		if (read(fd, (char *)&stringsz, sizeof(long)) != sizeof(long))
116 			goto error;
117 		err = 3;
118 		if (lseek(fd, sizeof(ehdr), 0) <= 0)
119 			goto error;
120 		od->ksize += ehdr.a_syms + sizeof(long) + stringsz;
121 	}
122 
123 	err = 4;
124 	if ((od->kstart = (u_char *)MALLOC(od->ksize)) == NULL)
125 		goto error;
126 
127 	/*
128 	 * Read text & data, clear bss
129 	 */
130 	err = 5;
131 	if ((read(fd, (char *)(od->kstart), ehdr.a_text) != ehdr.a_text)
132 	    ||(read(fd,(char *)(od->kstart+textsz),ehdr.a_data) != ehdr.a_data))
133 		goto error;
134 	bzero(od->kstart + textsz + ehdr.a_data, ehdr.a_bss);
135 
136 	/*
137 	 * Read symbol and string table
138 	 */
139 	if (loadsyms && ehdr.a_syms) {
140 		long	*p;
141 
142 		p = (long *)((od->kstart) + textsz + ehdr.a_data + ehdr.a_bss);
143 		*p++ = ehdr.a_syms;
144 		err = 6;
145 		if (read(fd, (char *)p, ehdr.a_syms) != ehdr.a_syms)
146 			goto error;
147 		p = (long *)((char *)p + ehdr.a_syms);
148 		err = 7;
149 		if (read(fd, (char *)p, stringsz) != stringsz)
150 			goto error;
151 		od->k_esym = (long)((char *)p-(char *)od->kstart +stringsz);
152 	}
153 	return 0;
154 
155 error:
156 #ifdef TOSTOOLS
157 	{
158 		static char *errs[] = {
159 			/* 1 */ "Cannot seek to string table",
160 			/* 2 */ "Cannot read string-table size",
161 			/* 3 */ "Cannot seek back to text start",
162 			/* 4 */ "Cannot malloc kernel image space",
163 			/* 5 */ "Unable to read kernel image",
164 			/* 6 */ "Cannot read symbol table",
165 			/* 7 */ "Cannot read string table"
166 		};
167 		*errp = errs[err];
168 	}
169 #endif /* TOSTOOLS */
170 
171 	return err;
172 }
173