annotate lwlink/readfiles.c @ 209:d6cba9d66979

Fixed bug with library search path
author lost
date Tue, 21 Apr 2009 03:45:12 +0000
parents 42df94f30d82
children bae1e3ecdce1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
1 /*
817ab0e124fa reading files implemented
lost
parents:
diff changeset
2 readfiles.c
118
d450943305a7 Fixed copyright year
lost
parents: 116
diff changeset
3 Copyright © 2009 William Astle
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
4
817ab0e124fa reading files implemented
lost
parents:
diff changeset
5 This file is part of LWLINK.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
6
817ab0e124fa reading files implemented
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
817ab0e124fa reading files implemented
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
817ab0e124fa reading files implemented
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
817ab0e124fa reading files implemented
lost
parents:
diff changeset
10 version.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
11
817ab0e124fa reading files implemented
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
817ab0e124fa reading files implemented
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
817ab0e124fa reading files implemented
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
817ab0e124fa reading files implemented
lost
parents:
diff changeset
15 more details.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
16
817ab0e124fa reading files implemented
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
817ab0e124fa reading files implemented
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
19
817ab0e124fa reading files implemented
lost
parents:
diff changeset
20
817ab0e124fa reading files implemented
lost
parents:
diff changeset
21 Reads input files
817ab0e124fa reading files implemented
lost
parents:
diff changeset
22
817ab0e124fa reading files implemented
lost
parents:
diff changeset
23 */
817ab0e124fa reading files implemented
lost
parents:
diff changeset
24
817ab0e124fa reading files implemented
lost
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
817ab0e124fa reading files implemented
lost
parents:
diff changeset
26 #include "config.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
27 #endif
817ab0e124fa reading files implemented
lost
parents:
diff changeset
28
817ab0e124fa reading files implemented
lost
parents:
diff changeset
29 #include <argp.h>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
30 #include <errno.h>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
31 #include <stdio.h>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
32 #include <stdlib.h>
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
33 #include <string.h>
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
34
817ab0e124fa reading files implemented
lost
parents:
diff changeset
35 #include "lwlink.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
36 #include "util.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
37
817ab0e124fa reading files implemented
lost
parents:
diff changeset
38 void read_lwobj16v0(fileinfo_t *fn);
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
39 void read_lwar1v(fileinfo_t *fn);
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
40
817ab0e124fa reading files implemented
lost
parents:
diff changeset
41 /*
817ab0e124fa reading files implemented
lost
parents:
diff changeset
42 The logic of reading the entire file into memory is simple. All the symbol
817ab0e124fa reading files implemented
lost
parents:
diff changeset
43 names in the file are NUL terminated strings and can be used directly without
817ab0e124fa reading files implemented
lost
parents:
diff changeset
44 making additional copies.
817ab0e124fa reading files implemented
lost
parents:
diff changeset
45 */
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
46 void read_file(fileinfo_t *fn)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
47 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
48 if (!memcmp(fn -> filedata, "LWOBJ16", 8))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
49 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
50 // read v0 LWOBJ16 file
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
51 read_lwobj16v0(fn);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
52 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
53 else if (!memcmp(fn -> filedata, "LWAR1V", 6))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
54 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
55 // archive file
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
56 read_lwar1v(fn);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
57 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
58 else
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
59 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
60 fprintf(stderr, "%s: unknown file format\n", fn -> filename);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
61 exit(1);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
62 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
63 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
64
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
65 void read_files(void)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
66 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
67 int i;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
68 long size;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
69 FILE *f;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
70 long bread;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
71 for (i = 0; i < ninputfiles; i++)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
72 {
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
73 if (inputfiles[i] -> islib)
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
74 {
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
75 char *tf;
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
76 int s;
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
77 int j;
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
78
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
79 f = NULL;
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
80
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
81 for (j = 0; j < nlibdirs; j++)
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
82 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
83 s = strlen(libdirs[j]) + 7 + strlen(inputfiles[i] -> filename);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
84 tf = lw_malloc(s + 1);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
85 sprintf(tf, "%s/lib%s.a", libdirs[j], inputfiles[i] -> filename);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
86 f = fopen(tf, "rb");
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
87 if (!f)
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
88 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
89 free(tf);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
90 continue;
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
91 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
92 free(tf);
209
d6cba9d66979 Fixed bug with library search path
lost
parents: 205
diff changeset
93 break;
180
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
94 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
95 if (!f)
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
96 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
97 fprintf(stderr, "Can't open library: -l%s\n", inputfiles[i] -> filename);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
98 exit(1);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
99 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
100 }
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
101 else
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
102 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
103 f = fopen(inputfiles[i] -> filename, "rb");
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
104 if (!f)
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
105 {
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
106 fprintf(stderr, "Can't open file %s:", inputfiles[i] -> filename);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
107 perror("");
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
108 exit(1);
6ebb93b409ba Added library paths and --section-base
lost
parents: 173
diff changeset
109 }
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
110 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
111 fseek(f, 0, SEEK_END);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
112 size = ftell(f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
113 rewind(f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
114
817ab0e124fa reading files implemented
lost
parents:
diff changeset
115 inputfiles[i] -> filedata = lw_malloc(size);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
116 inputfiles[i] -> filesize = size;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
117
817ab0e124fa reading files implemented
lost
parents:
diff changeset
118 bread = fread(inputfiles[i] -> filedata, 1, size, f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
119 if (bread < size)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
120 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
121 fprintf(stderr, "Short read on file %s (%ld/%ld):", inputfiles[i] -> filename, bread, size);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
122 perror("");
817ab0e124fa reading files implemented
lost
parents:
diff changeset
123 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
124 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
125
817ab0e124fa reading files implemented
lost
parents:
diff changeset
126 fclose(f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
127
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
128 read_file(inputfiles[i]);
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
129 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
130 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
131
817ab0e124fa reading files implemented
lost
parents:
diff changeset
132 // this macro is used to bail out if we run off the end of the file data
817ab0e124fa reading files implemented
lost
parents:
diff changeset
133 // while parsing - it keeps the code below cleaner
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
134 #define NEXTBYTE() do { cc++; if (cc > fn -> filesize) { fprintf(stderr, "%s: invalid file format\n", fn -> filename); exit(1); } } while (0)
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
135 // this macro is used to refer to the current byte in the stream
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
136 #define CURBYTE() (fn -> filedata[cc < fn -> filesize ? cc : fn -> filesize - 1])
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
137 // this one will leave the input pointer past the trailing NUL
817ab0e124fa reading files implemented
lost
parents:
diff changeset
138 #define CURSTR() read_lwobj16v0_str(&cc, fn)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
139 unsigned char *read_lwobj16v0_str(long *cc1, fileinfo_t *fn)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
140 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
141 int cc = *cc1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
142 unsigned char *fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
143 fp = &CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
144 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
145 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
146 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
147 *cc1 = cc;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
148 return fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
149 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
150 // the function below can be switched to dealing with data coming from a
817ab0e124fa reading files implemented
lost
parents:
diff changeset
151 // source other than an in-memory byte pool by adjusting the input data
817ab0e124fa reading files implemented
lost
parents:
diff changeset
152 // in "fn" and the above two macros
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
153
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
154 void read_lwobj16v0(fileinfo_t *fn)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
155 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
156 unsigned char *fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
157 long cc;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
158 section_t *s;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
159 int val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
160 symtab_t *se;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
161
817ab0e124fa reading files implemented
lost
parents:
diff changeset
162 // start reading *after* the magic number
126
269ef87192ad Fixed stupid logic problem reading input files
lost
parents: 124
diff changeset
163 cc = 8;
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
164
817ab0e124fa reading files implemented
lost
parents:
diff changeset
165 // init data
817ab0e124fa reading files implemented
lost
parents:
diff changeset
166 fn -> sections = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
167 fn -> nsections = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
168
817ab0e124fa reading files implemented
lost
parents:
diff changeset
169 while (1)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
170 {
126
269ef87192ad Fixed stupid logic problem reading input files
lost
parents: 124
diff changeset
171 // NEXTBYTE();
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
172 // bail out if no more sections
817ab0e124fa reading files implemented
lost
parents:
diff changeset
173 if (!(CURBYTE()))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
174 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
175
817ab0e124fa reading files implemented
lost
parents:
diff changeset
176 fp = CURSTR();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
177
817ab0e124fa reading files implemented
lost
parents:
diff changeset
178 // we now have a section name in fp
817ab0e124fa reading files implemented
lost
parents:
diff changeset
179 // create new section entry
817ab0e124fa reading files implemented
lost
parents:
diff changeset
180 fn -> sections = lw_realloc(fn -> sections, sizeof(section_t) * (fn -> nsections + 1));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
181 s = &(fn -> sections[fn -> nsections]);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
182 fn -> nsections += 1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
183
817ab0e124fa reading files implemented
lost
parents:
diff changeset
184 s -> localsyms = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
185 s -> flags = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
186 s -> codesize = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
187 s -> name = fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
188 s -> loadaddress = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
189 s -> localsyms = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
190 s -> exportedsyms = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
191 s -> incompletes = NULL;
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
192 s -> processed = 0;
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
193 s -> file = fn;
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
194
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
195 // read flags
817ab0e124fa reading files implemented
lost
parents:
diff changeset
196 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
197 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
198 switch (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
199 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
200 case 0x01:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
201 s -> flags |= SECTION_BSS;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
202 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
203
817ab0e124fa reading files implemented
lost
parents:
diff changeset
204 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
205 fprintf(stderr, "%s (%s): unrecognized section flag %02X\n", fn -> filename, s -> name, (int)(CURBYTE()));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
206 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
207 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
208 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
209 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
210 // skip NUL terminating flags
817ab0e124fa reading files implemented
lost
parents:
diff changeset
211 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
212
817ab0e124fa reading files implemented
lost
parents:
diff changeset
213 // now parse the local symbol table
817ab0e124fa reading files implemented
lost
parents:
diff changeset
214 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
215 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
216 fp = CURSTR();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
217
817ab0e124fa reading files implemented
lost
parents:
diff changeset
218 // fp is the symbol name
817ab0e124fa reading files implemented
lost
parents:
diff changeset
219 val = (CURBYTE()) << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
220 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
221 val |= (CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
222 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
223 // val is now the symbol value
817ab0e124fa reading files implemented
lost
parents:
diff changeset
224
817ab0e124fa reading files implemented
lost
parents:
diff changeset
225 // create symbol table entry
817ab0e124fa reading files implemented
lost
parents:
diff changeset
226 se = lw_malloc(sizeof(symtab_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
227 se -> next = s -> localsyms;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
228 s -> localsyms = se;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
229 se -> sym = fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
230 se -> offset = val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
231 }
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
232 // skip terminating NUL
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
233 NEXTBYTE();
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
234
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
235 // now parse the exported symbol table
817ab0e124fa reading files implemented
lost
parents:
diff changeset
236 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
237 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
238 fp = CURSTR();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
239
817ab0e124fa reading files implemented
lost
parents:
diff changeset
240 // fp is the symbol name
817ab0e124fa reading files implemented
lost
parents:
diff changeset
241 val = (CURBYTE()) << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
242 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
243 val |= (CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
244 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
245 // val is now the symbol value
817ab0e124fa reading files implemented
lost
parents:
diff changeset
246
817ab0e124fa reading files implemented
lost
parents:
diff changeset
247 // create symbol table entry
817ab0e124fa reading files implemented
lost
parents:
diff changeset
248 se = lw_malloc(sizeof(symtab_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
249 se -> next = s -> exportedsyms;
128
d25dd04c2af9 Fixed error reading external references
lost
parents: 127
diff changeset
250 s -> exportedsyms = se;
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
251 se -> sym = fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
252 se -> offset = val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
253 }
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
254 // skip terminating NUL
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
255 NEXTBYTE();
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
256
817ab0e124fa reading files implemented
lost
parents:
diff changeset
257 // now parse the incomplete references and make a list of
817ab0e124fa reading files implemented
lost
parents:
diff changeset
258 // external references that need resolution
817ab0e124fa reading files implemented
lost
parents:
diff changeset
259 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
260 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
261 reloc_t *rp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
262 lw_expr_term_t *term;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
263
817ab0e124fa reading files implemented
lost
parents:
diff changeset
264 // we have a reference
817ab0e124fa reading files implemented
lost
parents:
diff changeset
265 rp = lw_malloc(sizeof(reloc_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
266 rp -> next = s -> incompletes;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
267 s -> incompletes = rp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
268 rp -> offset = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
269 rp -> expr = lw_expr_stack_create();
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
270 rp -> flags = RELOC_NORM;
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
271
817ab0e124fa reading files implemented
lost
parents:
diff changeset
272 // parse the expression
817ab0e124fa reading files implemented
lost
parents:
diff changeset
273 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
274 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
275 int tt = CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
276 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
277 switch (tt)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
278 {
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
279 case 0xFF:
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
280 // a flag specifier
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
281 tt = CURBYTE();
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
282 rp -> flags = tt;
205
42df94f30d82 checkpoint
lost
parents: 204
diff changeset
283 NEXTBYTE();
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
284 term = NULL;
205
42df94f30d82 checkpoint
lost
parents: 204
diff changeset
285 break;
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
286
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
287 case 0x01:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
288 // 16 bit integer
817ab0e124fa reading files implemented
lost
parents:
diff changeset
289 tt = CURBYTE() << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
290 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
291 tt |= CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
292 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
293 // normalize for negatives...
817ab0e124fa reading files implemented
lost
parents:
diff changeset
294 if (tt > 0x7fff)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
295 tt -= 0x10000;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
296 term = lw_expr_term_create_int(tt);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
297 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
298
817ab0e124fa reading files implemented
lost
parents:
diff changeset
299 case 0x02:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
300 // external symbol reference
817ab0e124fa reading files implemented
lost
parents:
diff changeset
301 term = lw_expr_term_create_sym(CURSTR(), 0);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
302 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
303
817ab0e124fa reading files implemented
lost
parents:
diff changeset
304 case 0x03:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
305 // internal symbol reference
817ab0e124fa reading files implemented
lost
parents:
diff changeset
306 term = lw_expr_term_create_sym(CURSTR(), 1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
307 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
308
817ab0e124fa reading files implemented
lost
parents:
diff changeset
309 case 0x04:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
310 // operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
311 term = lw_expr_term_create_oper(CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
312 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
313 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
314
817ab0e124fa reading files implemented
lost
parents:
diff changeset
315 case 0x05:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
316 // section base reference (NULL internal reference is
817ab0e124fa reading files implemented
lost
parents:
diff changeset
317 // the section base address
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
318 term = lw_expr_term_create_sym(NULL, 1);
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
319 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
320
817ab0e124fa reading files implemented
lost
parents:
diff changeset
321 default:
205
42df94f30d82 checkpoint
lost
parents: 204
diff changeset
322 fprintf(stderr, "%s (%s): bad relocation expression (%02X)\n", fn -> filename, s -> name, tt);
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
323 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
324 }
204
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
325 if (term)
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
326 {
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
327 lw_expr_stack_push(rp -> expr, term);
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
328 lw_expr_term_free(term);
048ebb85f6ef Added 8 bit external references for base page addressing mode
lost
parents: 180
diff changeset
329 }
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
330 }
127
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
331 // skip the NUL
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
332 NEXTBYTE();
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
333
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
334 // fetch the offset
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
335 rp -> offset = CURBYTE() << 8;
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
336 NEXTBYTE();
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
337 rp -> offset |= CURBYTE() & 0xff;
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
338 NEXTBYTE();
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
339 }
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
340 // skip the NUL terminating the relocations
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
341 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
342
817ab0e124fa reading files implemented
lost
parents:
diff changeset
343 // now set code location and size and verify that the file
817ab0e124fa reading files implemented
lost
parents:
diff changeset
344 // contains data going to the end of the code (if !SECTION_BSS)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
345 s -> codesize = CURBYTE() << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
346 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
347 s -> codesize |= CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
348 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
349
817ab0e124fa reading files implemented
lost
parents:
diff changeset
350 s -> code = &(CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
351
817ab0e124fa reading files implemented
lost
parents:
diff changeset
352 // skip the code if we're not in a BSS section
817ab0e124fa reading files implemented
lost
parents:
diff changeset
353 if (!(s -> flags & SECTION_BSS))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
354 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
355 int i;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
356 for (i = 0; i < s -> codesize; i++)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
357 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
358 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
359 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
360 }
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
361
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
362 /*
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
363 Read an archive file - this will create a "sub" record and farm out the
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
364 parsing of the sub files to the regular file parsers
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
365
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
366 The archive file format consists of the 6 byte magic number followed by a
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
367 series of records as follows:
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
368
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
369 - NUL terminated file name
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
370 - 32 bit file length in big endian order
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
371 - the file data
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
372
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
373 An empty file name indicates the end of the file.
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
374
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
375 */
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
376 void read_lwar1v(fileinfo_t *fn)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
377 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
378 unsigned long cc = 6;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
379 unsigned long flen;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
380 unsigned long l;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
381 for (;;)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
382 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
383 if (cc >= fn -> filesize || !(fn -> filedata[cc]))
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
384 return;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
385
173
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
386 for (l = cc; cc < fn -> filesize && fn -> filedata[cc]; cc++)
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
387 /* do nothing */ ;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
388
173
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
389 cc++;
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
390
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
391 if (cc >= fn -> filesize)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
392 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
393 fprintf(stderr, "Malformed archive file %s.\n", fn -> filename);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
394 exit(1);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
395 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
396
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
397 if (cc + 4 > fn -> filesize)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
398 return;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
399
173
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
400 flen = (fn -> filedata[cc++] << 24);
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
401 flen |= (fn -> filedata[cc++] << 16);
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
402 flen |= (fn -> filedata[cc++] << 8);
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
403 flen |= (fn -> filedata[cc++]);
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
404
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
405 if (flen == 0)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
406 return;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
407
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
408 if (cc + flen > fn -> filesize)
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
409 {
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
410 fprintf(stderr, "Malformed archive file %s.\n", fn -> filename);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
411 exit(1);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
412 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
413
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
414 // add the "sub" input file
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
415 fn -> subs = lw_realloc(fn -> subs, sizeof(fileinfo_t *) * (fn -> nsubs + 1));
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
416 fn -> subs[fn -> nsubs] = lw_malloc(sizeof(fileinfo_t));
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
417 memset(fn -> subs[fn -> nsubs], 0, sizeof(fileinfo_t));
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
418 fn -> subs[fn -> nsubs] -> filedata = fn -> filedata + cc;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
419 fn -> subs[fn -> nsubs] -> filesize = flen;
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
420 fn -> subs[fn -> nsubs] -> filename = lw_strdup(fn -> filedata + l);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
421 fn -> subs[fn -> nsubs] -> parent = fn;
173
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
422
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
423 read_file(fn -> subs[fn -> nsubs]);
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
424 fn -> nsubs++;
173
0395e6fd67e9 Fixed stupid errors with changes for archive handling
lost
parents: 171
diff changeset
425 cc += flen;
171
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
426 }
d610b8aef91b Added LWAR skeleton
lost
parents: 139
diff changeset
427 }