annotate lwlink/readfiles.c @ 212:bae1e3ecdce1

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