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