annotate lwlink/trunk/src/readfiles.c @ 127:44dd2f2e42a7

Fixed reading object file incomplete references table
author lost
date Fri, 23 Jan 2009 05:49:16 +0000
parents 269ef87192ad
children d25dd04c2af9
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>
817ab0e124fa reading files implemented
lost
parents:
diff changeset
33
817ab0e124fa reading files implemented
lost
parents:
diff changeset
34 #include "lwlink.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
35 #include "util.h"
817ab0e124fa reading files implemented
lost
parents:
diff changeset
36
817ab0e124fa reading files implemented
lost
parents:
diff changeset
37 void read_lwobj16v0(fileinfo_t *fn);
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 */
817ab0e124fa reading files implemented
lost
parents:
diff changeset
44 void read_files(void)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
45 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
46 int i;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
47 long size;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
48 FILE *f;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
49 long bread;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
50 for (i = 0; i < ninputfiles; i++)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
51 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
52 f = fopen(inputfiles[i] -> filename, "rb");
817ab0e124fa reading files implemented
lost
parents:
diff changeset
53 if (!f)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
54 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
55 fprintf(stderr, "Can't open file %s:", inputfiles[i] -> filename);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
56 perror("");
817ab0e124fa reading files implemented
lost
parents:
diff changeset
57 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
58 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
59 fseek(f, 0, SEEK_END);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
60 size = ftell(f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
61 rewind(f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
62
817ab0e124fa reading files implemented
lost
parents:
diff changeset
63 inputfiles[i] -> filedata = lw_malloc(size);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
64 inputfiles[i] -> filesize = size;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
65
817ab0e124fa reading files implemented
lost
parents:
diff changeset
66 bread = fread(inputfiles[i] -> filedata, 1, size, f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
67 if (bread < size)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
68 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
69 fprintf(stderr, "Short read on file %s (%ld/%ld):", inputfiles[i] -> filename, bread, size);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
70 perror("");
817ab0e124fa reading files implemented
lost
parents:
diff changeset
71 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
72 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
73
817ab0e124fa reading files implemented
lost
parents:
diff changeset
74 fclose(f);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
75
817ab0e124fa reading files implemented
lost
parents:
diff changeset
76 if (!memcmp(inputfiles[i] -> filedata, "LWOBJ16", 8))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
77 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
78 // read v0 LWOBJ16 file
817ab0e124fa reading files implemented
lost
parents:
diff changeset
79 read_lwobj16v0(inputfiles[i]);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
80 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
81 else
817ab0e124fa reading files implemented
lost
parents:
diff changeset
82 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
83 fprintf(stderr, "%s: unknown file format\n", inputfiles[i] -> filename);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
84 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
85 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
86 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
87 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
88
817ab0e124fa reading files implemented
lost
parents:
diff changeset
89 // 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
90 // while parsing - it keeps the code below cleaner
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
91 #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
92 // 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
93 #define CURBYTE() (fn -> filedata[cc < fn -> filesize ? cc : fn -> filesize - 1])
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
94 // this one will leave the input pointer past the trailing NUL
817ab0e124fa reading files implemented
lost
parents:
diff changeset
95 #define CURSTR() read_lwobj16v0_str(&cc, fn)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
96 unsigned char *read_lwobj16v0_str(long *cc1, fileinfo_t *fn)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
97 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
98 int cc = *cc1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
99 unsigned char *fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
100 fp = &CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
101 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
102 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
103 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
104 *cc1 = cc;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
105 return fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
106 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
107 // the function below can be switched to dealing with data coming from a
817ab0e124fa reading files implemented
lost
parents:
diff changeset
108 // source other than an in-memory byte pool by adjusting the input data
817ab0e124fa reading files implemented
lost
parents:
diff changeset
109 // in "fn" and the above two macros
817ab0e124fa reading files implemented
lost
parents:
diff changeset
110 void read_lwobj16v0(fileinfo_t *fn)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
111 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
112 unsigned char *fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
113 long cc;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
114 section_t *s;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
115 int val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
116 symtab_t *se;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
117
817ab0e124fa reading files implemented
lost
parents:
diff changeset
118 // start reading *after* the magic number
126
269ef87192ad Fixed stupid logic problem reading input files
lost
parents: 124
diff changeset
119 cc = 8;
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
120
817ab0e124fa reading files implemented
lost
parents:
diff changeset
121 // init data
817ab0e124fa reading files implemented
lost
parents:
diff changeset
122 fn -> sections = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
123 fn -> nsections = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
124
817ab0e124fa reading files implemented
lost
parents:
diff changeset
125 while (1)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
126 {
126
269ef87192ad Fixed stupid logic problem reading input files
lost
parents: 124
diff changeset
127 // NEXTBYTE();
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
128 // bail out if no more sections
817ab0e124fa reading files implemented
lost
parents:
diff changeset
129 if (!(CURBYTE()))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
130 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
131
817ab0e124fa reading files implemented
lost
parents:
diff changeset
132 fp = CURSTR();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
133
817ab0e124fa reading files implemented
lost
parents:
diff changeset
134 // we now have a section name in fp
817ab0e124fa reading files implemented
lost
parents:
diff changeset
135 // create new section entry
817ab0e124fa reading files implemented
lost
parents:
diff changeset
136 fn -> sections = lw_realloc(fn -> sections, sizeof(section_t) * (fn -> nsections + 1));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
137 s = &(fn -> sections[fn -> nsections]);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
138 fn -> nsections += 1;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
139
817ab0e124fa reading files implemented
lost
parents:
diff changeset
140 s -> localsyms = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
141 s -> flags = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
142 s -> codesize = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
143 s -> name = fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
144 s -> loadaddress = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
145 s -> localsyms = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
146 s -> exportedsyms = NULL;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
147 s -> incompletes = NULL;
119
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
148 s -> processed = 0;
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
149 s -> file = fn;
8bc00221ae89 section order and address resolution done
lost
parents: 118
diff changeset
150
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
151 // read flags
817ab0e124fa reading files implemented
lost
parents:
diff changeset
152 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
153 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
154 switch (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
155 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
156 case 0x01:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
157 s -> flags |= SECTION_BSS;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
158 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
159
817ab0e124fa reading files implemented
lost
parents:
diff changeset
160 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
161 fprintf(stderr, "%s (%s): unrecognized section flag %02X\n", fn -> filename, s -> name, (int)(CURBYTE()));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
162 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
163 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
164 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
165 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
166 // skip NUL terminating flags
817ab0e124fa reading files implemented
lost
parents:
diff changeset
167 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
168
817ab0e124fa reading files implemented
lost
parents:
diff changeset
169 // now parse the local symbol table
817ab0e124fa reading files implemented
lost
parents:
diff changeset
170 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
171 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
172 fp = CURSTR();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
173
817ab0e124fa reading files implemented
lost
parents:
diff changeset
174 // fp is the symbol name
817ab0e124fa reading files implemented
lost
parents:
diff changeset
175 val = (CURBYTE()) << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
176 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
177 val |= (CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
178 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
179 // val is now the symbol value
817ab0e124fa reading files implemented
lost
parents:
diff changeset
180
817ab0e124fa reading files implemented
lost
parents:
diff changeset
181 // create symbol table entry
817ab0e124fa reading files implemented
lost
parents:
diff changeset
182 se = lw_malloc(sizeof(symtab_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
183 se -> next = s -> localsyms;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
184 s -> localsyms = se;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
185 se -> sym = fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
186 se -> offset = val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
187 }
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
188 // skip terminating NUL
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
189 NEXTBYTE();
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
190
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
191 // now parse the exported symbol table
817ab0e124fa reading files implemented
lost
parents:
diff changeset
192 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
193 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
194 fp = CURSTR();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
195
817ab0e124fa reading files implemented
lost
parents:
diff changeset
196 // fp is the symbol name
817ab0e124fa reading files implemented
lost
parents:
diff changeset
197 val = (CURBYTE()) << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
198 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
199 val |= (CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
200 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
201 // val is now the symbol value
817ab0e124fa reading files implemented
lost
parents:
diff changeset
202
817ab0e124fa reading files implemented
lost
parents:
diff changeset
203 // create symbol table entry
817ab0e124fa reading files implemented
lost
parents:
diff changeset
204 se = lw_malloc(sizeof(symtab_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
205 se -> next = s -> exportedsyms;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
206 s -> localsyms = se;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
207 se -> sym = fp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
208 se -> offset = val;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
209 }
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
210 // skip terminating NUL
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
211 NEXTBYTE();
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
212
817ab0e124fa reading files implemented
lost
parents:
diff changeset
213 // now parse the incomplete references and make a list of
817ab0e124fa reading files implemented
lost
parents:
diff changeset
214 // external references that need resolution
817ab0e124fa reading files implemented
lost
parents:
diff changeset
215 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
216 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
217 reloc_t *rp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
218 lw_expr_term_t *term;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
219
817ab0e124fa reading files implemented
lost
parents:
diff changeset
220 // we have a reference
817ab0e124fa reading files implemented
lost
parents:
diff changeset
221 rp = lw_malloc(sizeof(reloc_t));
817ab0e124fa reading files implemented
lost
parents:
diff changeset
222 rp -> next = s -> incompletes;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
223 s -> incompletes = rp;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
224 rp -> offset = 0;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
225 rp -> expr = lw_expr_stack_create();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
226
817ab0e124fa reading files implemented
lost
parents:
diff changeset
227 // parse the expression
817ab0e124fa reading files implemented
lost
parents:
diff changeset
228 while (CURBYTE())
817ab0e124fa reading files implemented
lost
parents:
diff changeset
229 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
230 int tt = CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
231 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
232 switch (tt)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
233 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
234 case 0x01:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
235 // 16 bit integer
817ab0e124fa reading files implemented
lost
parents:
diff changeset
236 tt = CURBYTE() << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
237 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
238 tt |= CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
239 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
240 // normalize for negatives...
817ab0e124fa reading files implemented
lost
parents:
diff changeset
241 if (tt > 0x7fff)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
242 tt -= 0x10000;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
243 term = lw_expr_term_create_int(tt);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
244 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
245
817ab0e124fa reading files implemented
lost
parents:
diff changeset
246 case 0x02:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
247 // external symbol reference
817ab0e124fa reading files implemented
lost
parents:
diff changeset
248 term = lw_expr_term_create_sym(CURSTR(), 0);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
249 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
250
817ab0e124fa reading files implemented
lost
parents:
diff changeset
251 case 0x03:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
252 // internal symbol reference
817ab0e124fa reading files implemented
lost
parents:
diff changeset
253 term = lw_expr_term_create_sym(CURSTR(), 1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
254 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
255
817ab0e124fa reading files implemented
lost
parents:
diff changeset
256 case 0x04:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
257 // operator
817ab0e124fa reading files implemented
lost
parents:
diff changeset
258 term = lw_expr_term_create_oper(CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
259 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
260 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
261
817ab0e124fa reading files implemented
lost
parents:
diff changeset
262 case 0x05:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
263 // section base reference (NULL internal reference is
817ab0e124fa reading files implemented
lost
parents:
diff changeset
264 // the section base address
120
36ce328df3c3 resolve incomplete references done
lost
parents: 119
diff changeset
265 term = lw_expr_term_create_sym(NULL, 1);
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
266 break;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
267
817ab0e124fa reading files implemented
lost
parents:
diff changeset
268 default:
817ab0e124fa reading files implemented
lost
parents:
diff changeset
269 fprintf(stderr, "%s (%s): bad relocation expression\n", fn -> filename, s -> name);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
270 exit(1);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
271 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
272 lw_expr_stack_push(rp -> expr, term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
273 lw_expr_term_free(term);
817ab0e124fa reading files implemented
lost
parents:
diff changeset
274 }
127
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
275 // skip the NUL
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
276 NEXTBYTE();
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
277
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
278 // fetch the offset
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
279 rp -> offset = CURBYTE() << 8;
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
280 NEXTBYTE();
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
281 rp -> offset |= CURBYTE() & 0xff;
44dd2f2e42a7 Fixed reading object file incomplete references table
lost
parents: 126
diff changeset
282 NEXTBYTE();
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
283 }
124
4ac96c697ef0 Corrected errors with parsing script and object files
lost
parents: 120
diff changeset
284 // skip the NUL terminating the relocations
116
817ab0e124fa reading files implemented
lost
parents:
diff changeset
285 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
286
817ab0e124fa reading files implemented
lost
parents:
diff changeset
287 // now set code location and size and verify that the file
817ab0e124fa reading files implemented
lost
parents:
diff changeset
288 // contains data going to the end of the code (if !SECTION_BSS)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
289 s -> codesize = CURBYTE() << 8;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
290 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
291 s -> codesize |= CURBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
292 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
293
817ab0e124fa reading files implemented
lost
parents:
diff changeset
294 s -> code = &(CURBYTE());
817ab0e124fa reading files implemented
lost
parents:
diff changeset
295
817ab0e124fa reading files implemented
lost
parents:
diff changeset
296 // skip the code if we're not in a BSS section
817ab0e124fa reading files implemented
lost
parents:
diff changeset
297 if (!(s -> flags & SECTION_BSS))
817ab0e124fa reading files implemented
lost
parents:
diff changeset
298 {
817ab0e124fa reading files implemented
lost
parents:
diff changeset
299 int i;
817ab0e124fa reading files implemented
lost
parents:
diff changeset
300 for (i = 0; i < s -> codesize; i++)
817ab0e124fa reading files implemented
lost
parents:
diff changeset
301 NEXTBYTE();
817ab0e124fa reading files implemented
lost
parents:
diff changeset
302 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
303 }
817ab0e124fa reading files implemented
lost
parents:
diff changeset
304 }