xref: /netbsd-src/sys/lib/libsa/loadfile_ecoff.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /* $NetBSD: loadfile_ecoff.c,v 1.3 2001/11/09 19:27:25 christos Exp $ */
2 
3 /*-
4  * Copyright (c) 1997 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center and by Christos Zoulas.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #ifdef _STANDALONE
41 #include <lib/libsa/stand.h>
42 #include <lib/libkern/libkern.h>
43 #else
44 #include <stdio.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <err.h>
51 #endif
52 
53 #include <sys/param.h>
54 #include <sys/exec.h>
55 
56 #include "loadfile.h"
57 
58 #ifdef BOOT_ECOFF
59 
60 int
61 loadfile_coff(fd, coff, marks, flags)
62 	int fd;
63 	struct ecoff_exechdr *coff;
64 	u_long *marks;
65 	int flags;
66 {
67 	paddr_t offset = marks[MARK_START];
68 	paddr_t minp = ~0, maxp = 0, pos;
69 
70 	/* Read in text. */
71 	if (lseek(fd, ECOFF_TXTOFF(coff), SEEK_SET) == -1)  {
72 		WARN(("lseek text"));
73 		return 1;
74 	}
75 
76 	if (coff->a.tsize != 0) {
77 		if (flags & LOAD_TEXT) {
78 			PROGRESS(("%lu", coff->a.tsize));
79 			if (READ(fd, coff->a.text_start, coff->a.tsize) !=
80 			    coff->a.tsize) {
81 				return 1;
82 			}
83 		}
84 		else {
85 			if (lseek(fd, coff->a.tsize, SEEK_CUR) == -1) {
86 				WARN(("read text"));
87 				return 1;
88 			}
89 		}
90 		if (flags & (COUNT_TEXT|LOAD_TEXT)) {
91 			pos = coff->a.text_start;
92 			if (minp > pos)
93 				minp = pos;
94 			pos += coff->a.tsize;
95 			if (maxp < pos)
96 				maxp = pos;
97 		}
98 	}
99 
100 	/* Read in data. */
101 	if (coff->a.dsize != 0) {
102 		if (flags & LOAD_DATA) {
103 			PROGRESS(("+%lu", coff->a.dsize));
104 			if (READ(fd, coff->a.data_start, coff->a.dsize) !=
105 			    coff->a.dsize) {
106 				WARN(("read data"));
107 				return 1;
108 			}
109 		}
110 		if (flags & (COUNT_DATA|LOAD_DATA)) {
111 			pos = coff->a.data_start;
112 			if (minp > pos)
113 				minp = pos;
114 			pos += coff->a.dsize;
115 			if (maxp < pos)
116 				maxp = pos;
117 		}
118 	}
119 
120 	/* Zero out bss. */
121 	if (coff->a.bsize != 0) {
122 		if (flags & LOAD_BSS) {
123 			PROGRESS(("+%lu", coff->a.bsize));
124 			BZERO(coff->a.bss_start, coff->a.bsize);
125 		}
126 		if (flags & (COUNT_BSS|LOAD_BSS)) {
127 			pos = coff->a.bss_start;
128 			if (minp > pos)
129 				minp = pos;
130 			pos = coff->a.bsize;
131 			if (maxp < pos)
132 				maxp = pos;
133 		}
134 	}
135 
136 	marks[MARK_START] = LOADADDR(minp);
137 	marks[MARK_ENTRY] = LOADADDR(coff->a.entry);
138 	marks[MARK_NSYM] = 1;	/* XXX: Kernel needs >= 0 */
139 	marks[MARK_SYM] = LOADADDR(maxp);
140 	marks[MARK_END] = LOADADDR(maxp);
141 	return 0;
142 }
143 
144 #endif /* BOOT_ECOFF */
145