annotate src/readfiles.c @ 318:be4d471f3de5 lwlink-1.0

Moved old tags and branches for lwlink to global tags/branches dirs
author lost
date Wed, 28 Jan 2009 05:54:43 +0000
parents 3476629ee0ce
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
1 /*
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
2 readfiles.c
300
48945dac8178 Fixed copyright year
lost
parents: 298
diff changeset
3 Copyright © 2009 William Astle
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
4
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
5 This file is part of LWLINK.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
6
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
7 LWLINK is free software: you can redistribute it and/or modify it under the
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
8 terms of the GNU General Public License as published by the Free Software
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
9 Foundation, either version 3 of the License, or (at your option) any later
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
10 version.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
11
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
12 This program is distributed in the hope that it will be useful, but WITHOUT
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
15 more details.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
16
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
17 You should have received a copy of the GNU General Public License along with
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
18 this program. If not, see <http://www.gnu.org/licenses/>.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
19
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
20
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
21 Reads input files
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
22
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
23 */
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
24
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
25 #ifdef HAVE_CONFIG_H
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
26 #include "config.h"
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
27 #endif
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
28
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
29 #include <argp.h>
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
30 #include <errno.h>
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
31 #include <stdio.h>
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
32 #include <stdlib.h>
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
33
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
34 #include "lwlink.h"
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
35 #include "util.h"
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
36
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
37 void read_lwobj16v0(fileinfo_t *fn);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
38
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
39 /*
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
40 The logic of reading the entire file into memory is simple. All the symbol
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
41 names in the file are NUL terminated strings and can be used directly without
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
42 making additional copies.
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
43 */
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
44 void read_files(void)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
45 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
46 int i;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
47 long size;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
48 FILE *f;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
49 long bread;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
50 for (i = 0; i < ninputfiles; i++)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
51 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
52 f = fopen(inputfiles[i] -> filename, "rb");
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
53 if (!f)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
54 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
55 fprintf(stderr, "Can't open file %s:", inputfiles[i] -> filename);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
56 perror("");
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
57 exit(1);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
58 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
59 fseek(f, 0, SEEK_END);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
60 size = ftell(f);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
61 rewind(f);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
62
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
63 inputfiles[i] -> filedata = lw_malloc(size);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
64 inputfiles[i] -> filesize = size;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
65
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
66 bread = fread(inputfiles[i] -> filedata, 1, size, f);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
67 if (bread < size)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
68 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
69 fprintf(stderr, "Short read on file %s (%ld/%ld):", inputfiles[i] -> filename, bread, size);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
70 perror("");
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
71 exit(1);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
72 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
73
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
74 fclose(f);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
75
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
76 if (!memcmp(inputfiles[i] -> filedata, "LWOBJ16", 8))
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
77 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
78 // read v0 LWOBJ16 file
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
79 read_lwobj16v0(inputfiles[i]);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
80 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
81 else
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
82 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
83 fprintf(stderr, "%s: unknown file format\n", inputfiles[i] -> filename);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
84 exit(1);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
85 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
86 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
87 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
88
96a35a4245f3 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
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
90 // while parsing - it keeps the code below cleaner
306
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
91 #define NEXTBYTE() do { cc++; if (cc > fn -> filesize) { fprintf(stderr, "%s: invalid file format\n", fn -> filename); exit(1); } } while (0)
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
92 // this macro is used to refer to the current byte in the stream
306
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
93 #define CURBYTE() (fn -> filedata[cc < fn -> filesize ? cc : fn -> filesize - 1])
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
94 // this one will leave the input pointer past the trailing NUL
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
95 #define CURSTR() read_lwobj16v0_str(&cc, fn)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
96 unsigned char *read_lwobj16v0_str(long *cc1, fileinfo_t *fn)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
97 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
98 int cc = *cc1;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
99 unsigned char *fp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
100 fp = &CURBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
101 while (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
102 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
103 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
104 *cc1 = cc;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
105 return fp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
106 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
107 // the function below can be switched to dealing with data coming from a
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
108 // source other than an in-memory byte pool by adjusting the input data
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
109 // in "fn" and the above two macros
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
110 void read_lwobj16v0(fileinfo_t *fn)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
111 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
112 unsigned char *fp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
113 long cc;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
114 section_t *s;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
115 int val;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
116 symtab_t *se;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
117
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
118 // start reading *after* the magic number
308
ecc092adc0be Fixed stupid logic problem reading input files
lost
parents: 306
diff changeset
119 cc = 8;
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
120
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
121 // init data
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
122 fn -> sections = NULL;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
123 fn -> nsections = 0;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
124
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
125 while (1)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
126 {
308
ecc092adc0be Fixed stupid logic problem reading input files
lost
parents: 306
diff changeset
127 // NEXTBYTE();
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
128 // bail out if no more sections
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
129 if (!(CURBYTE()))
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
130 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
131
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
132 fp = CURSTR();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
133
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
134 // we now have a section name in fp
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
135 // create new section entry
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
136 fn -> sections = lw_realloc(fn -> sections, sizeof(section_t) * (fn -> nsections + 1));
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
137 s = &(fn -> sections[fn -> nsections]);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
138 fn -> nsections += 1;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
139
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
140 s -> localsyms = NULL;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
141 s -> flags = 0;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
142 s -> codesize = 0;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
143 s -> name = fp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
144 s -> loadaddress = 0;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
145 s -> localsyms = NULL;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
146 s -> exportedsyms = NULL;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
147 s -> incompletes = NULL;
301
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
148 s -> processed = 0;
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
149 s -> file = fn;
376cc55361c7 section order and address resolution done
lost
parents: 300
diff changeset
150
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
151 // read flags
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
152 while (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
153 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
154 switch (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
155 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
156 case 0x01:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
157 s -> flags |= SECTION_BSS;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
158 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
159
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
160 default:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
161 fprintf(stderr, "%s (%s): unrecognized section flag %02X\n", fn -> filename, s -> name, (int)(CURBYTE()));
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
162 exit(1);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
163 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
164 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
165 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
166 // skip NUL terminating flags
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
167 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
168
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
169 // now parse the local symbol table
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
170 while (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
171 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
172 fp = CURSTR();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
173
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
174 // fp is the symbol name
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
175 val = (CURBYTE()) << 8;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
176 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
177 val |= (CURBYTE());
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
178 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
179 // val is now the symbol value
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
180
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
181 // create symbol table entry
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
182 se = lw_malloc(sizeof(symtab_t));
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
183 se -> next = s -> localsyms;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
184 s -> localsyms = se;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
185 se -> sym = fp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
186 se -> offset = val;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
187 }
306
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
188 // skip terminating NUL
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
189 NEXTBYTE();
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
190
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
191 // now parse the exported symbol table
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
192 while (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
193 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
194 fp = CURSTR();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
195
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
196 // fp is the symbol name
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
197 val = (CURBYTE()) << 8;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
198 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
199 val |= (CURBYTE());
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
200 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
201 // val is now the symbol value
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
202
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
203 // create symbol table entry
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
204 se = lw_malloc(sizeof(symtab_t));
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
205 se -> next = s -> exportedsyms;
310
3476629ee0ce Fixed error reading external references
lost
parents: 309
diff changeset
206 s -> exportedsyms = se;
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
207 se -> sym = fp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
208 se -> offset = val;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
209 }
306
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
210 // skip terminating NUL
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
211 NEXTBYTE();
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
212
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
213 // now parse the incomplete references and make a list of
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
214 // external references that need resolution
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
215 while (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
216 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
217 reloc_t *rp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
218 lw_expr_term_t *term;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
219
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
220 // we have a reference
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
221 rp = lw_malloc(sizeof(reloc_t));
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
222 rp -> next = s -> incompletes;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
223 s -> incompletes = rp;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
224 rp -> offset = 0;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
225 rp -> expr = lw_expr_stack_create();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
226
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
227 // parse the expression
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
228 while (CURBYTE())
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
229 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
230 int tt = CURBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
231 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
232 switch (tt)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
233 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
234 case 0x01:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
235 // 16 bit integer
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
236 tt = CURBYTE() << 8;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
237 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
238 tt |= CURBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
239 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
240 // normalize for negatives...
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
241 if (tt > 0x7fff)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
242 tt -= 0x10000;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
243 term = lw_expr_term_create_int(tt);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
244 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
245
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
246 case 0x02:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
247 // external symbol reference
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
248 term = lw_expr_term_create_sym(CURSTR(), 0);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
249 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
250
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
251 case 0x03:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
252 // internal symbol reference
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
253 term = lw_expr_term_create_sym(CURSTR(), 1);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
254 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
255
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
256 case 0x04:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
257 // operator
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
258 term = lw_expr_term_create_oper(CURBYTE());
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
259 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
260 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
261
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
262 case 0x05:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
263 // section base reference (NULL internal reference is
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
264 // the section base address
302
eff969272fb9 resolve incomplete references done
lost
parents: 301
diff changeset
265 term = lw_expr_term_create_sym(NULL, 1);
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
266 break;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
267
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
268 default:
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
269 fprintf(stderr, "%s (%s): bad relocation expression\n", fn -> filename, s -> name);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
270 exit(1);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
271 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
272 lw_expr_stack_push(rp -> expr, term);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
273 lw_expr_term_free(term);
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
274 }
309
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
275 // skip the NUL
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
276 NEXTBYTE();
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
277
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
278 // fetch the offset
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
279 rp -> offset = CURBYTE() << 8;
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
280 NEXTBYTE();
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
281 rp -> offset |= CURBYTE() & 0xff;
34203598be6d Fixed reading object file incomplete references table
lost
parents: 308
diff changeset
282 NEXTBYTE();
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
283 }
306
be1abc4ac114 Corrected errors with parsing script and object files
lost
parents: 302
diff changeset
284 // skip the NUL terminating the relocations
298
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
285 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
286
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
287 // now set code location and size and verify that the file
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
288 // contains data going to the end of the code (if !SECTION_BSS)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
289 s -> codesize = CURBYTE() << 8;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
290 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
291 s -> codesize |= CURBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
292 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
293
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
294 s -> code = &(CURBYTE());
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
295
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
296 // skip the code if we're not in a BSS section
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
297 if (!(s -> flags & SECTION_BSS))
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
298 {
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
299 int i;
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
300 for (i = 0; i < s -> codesize; i++)
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
301 NEXTBYTE();
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
302 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
303 }
96a35a4245f3 reading files implemented
lost
parents:
diff changeset
304 }