annotate lwlink/readfiles.c @ 405:7bcc50e828ff

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