1/*	$NetBSD: aout.c,v 1.11 2009/01/06 13:35:30 tsutsui 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 <libkern.h>
48#include <sys/exec_aout.h>
49
50#define	MALLOC(x)	alloc(x)
51#endif
52
53#include "libtos.h"
54#include "kparamb.h"
55#include "tosdefs.h"
56#include "cread.h"
57
58
59#ifdef TOSTOOLS
60/*
61 * Assume compiling under TOS or MINT. The page-size will always
62 * be incorrect then (if it is defined anyway).
63 */
64#ifdef AOUT_LDPGSZ
65#undef AOUT_LDPGSZ
66#endif
67
68#define AOUT_LDPGSZ	(8*1024)	/* Page size for NetBSD		*/
69
70#endif /* TOSTOOLS */
71
72/*
73 * Load an a.out image.
74 * Exit codes:
75 *	-1      : Not an a.outfile
76 *	 0      : OK
77 *	 error# : Error during load (*errp might contain error string).
78 */
79int
80aout_load(int fd, osdsc_t *od, char **errp, int loadsyms)
81{
82	long		textsz, stringsz;
83	struct exec	ehdr;
84	int		err;
85
86	*errp = NULL;
87
88	lseek(fd, (off_t)0, SEEK_SET);
89	if (read(fd, (char *)&ehdr, sizeof(ehdr)) != sizeof(ehdr))
90		return -1;
91
92#ifdef TOSTOOLS
93	if ((ehdr.a_magic & 0xffff) != NMAGIC)
94		return -1;
95#else
96	if ((N_GETMAGIC(ehdr) != NMAGIC) && (N_GETMAGIC(ehdr) != OMAGIC))
97		return -1;
98#endif
99
100	/*
101	 * Extract various sizes from the kernel executable
102	 */
103	textsz     = (ehdr.a_text + AOUT_LDPGSZ - 1) & ~(AOUT_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		err = 1;
110		if (lseek(fd, ehdr.a_text+ehdr.a_data+ehdr.a_syms+sizeof(ehdr),
111			  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	memset(od->kstart + textsz + ehdr.a_data, 0, 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
154error:
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