Mercurial > hg-old > index.cgi
annotate lwlink/link.c @ 243:f9f01a499525 2.x
Added zero-width external references
author | lost |
---|---|
date | Tue, 15 Sep 2009 03:16:17 +0000 |
parents | bae1e3ecdce1 |
children | eb499c146c0d |
rev | line source |
---|---|
119 | 1 /* |
2 link.c | |
3 Copyright © 2009 William Astle | |
4 | |
5 This file is part of LWLINK. | |
6 | |
7 LWLINK is free software: you can redistribute it and/or modify it under the | |
8 terms of the GNU General Public License as published by the Free Software | |
9 Foundation, either version 3 of the License, or (at your option) any later | |
10 version. | |
11 | |
12 This program is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
15 more details. | |
16 | |
17 You should have received a copy of the GNU General Public License along with | |
18 this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 Resolve section and symbol addresses; handle incomplete references | |
22 */ | |
23 | |
212 | 24 #include <config.h> |
119 | 25 |
120 | 26 #include <stdio.h> |
119 | 27 #include <stdlib.h> |
28 | |
120 | 29 #include "expr.h" |
119 | 30 #include "lwlink.h" |
31 #include "util.h" | |
32 | |
121 | 33 struct section_list *sectlist = NULL; |
34 int nsects = 0; | |
205 | 35 static int nforced = 0; |
119 | 36 |
171 | 37 void check_section_name(char *name, int *base, fileinfo_t *fn) |
38 { | |
39 int sn; | |
205 | 40 |
41 if (fn -> forced == 0) | |
42 return; | |
43 | |
171 | 44 for (sn = 0; sn < fn -> nsections; sn++) |
45 { | |
46 if (!strcmp(name, fn -> sections[sn].name)) | |
47 { | |
48 // we have a match | |
49 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1)); | |
50 sectlist[nsects].ptr = &(fn -> sections[sn]); | |
51 | |
52 fn -> sections[sn].processed = 1; | |
53 fn -> sections[sn].loadaddress = *base; | |
54 *base += fn -> sections[sn].codesize; | |
55 nsects++; | |
56 } | |
57 } | |
173 | 58 for (sn = 0; sn < fn -> nsubs; sn++) |
171 | 59 { |
60 check_section_name(name, base, fn -> subs[sn]); | |
61 } | |
62 } | |
63 | |
64 void add_matching_sections(char *name, int yesflags, int noflags, int *base); | |
65 void check_section_flags(int yesflags, int noflags, int *base, fileinfo_t *fn) | |
66 { | |
67 int sn; | |
205 | 68 |
69 if (fn -> forced == 0) | |
70 return; | |
71 | |
171 | 72 for (sn = 0; sn < fn -> nsections; sn++) |
73 { | |
74 // ignore if the noflags tell us to | |
75 if (noflags && (fn -> sections[sn].flags & noflags)) | |
76 continue; | |
77 // ignore unless the yesflags tell us not to | |
78 if (yesflags && (fn -> sections[sn].flags & yesflags == 0)) | |
79 continue; | |
80 // ignore it if already processed | |
81 if (fn -> sections[sn].processed) | |
82 continue; | |
83 | |
84 // we have a match - now collect *all* sections of the same name! | |
85 add_matching_sections(fn -> sections[sn].name, 0, 0, base); | |
86 | |
87 // and then continue looking for sections | |
88 } | |
173 | 89 for (sn = 0; sn < fn -> nsubs; sn++) |
171 | 90 { |
91 check_section_flags(yesflags, noflags, base, fn -> subs[sn]); | |
92 } | |
93 } | |
94 | |
95 | |
96 | |
97 void add_matching_sections(char *name, int yesflags, int noflags, int *base) | |
98 { | |
99 int fn; | |
100 if (name) | |
101 { | |
102 // named section | |
103 // look for all instances of a section by the specified name | |
104 // and resolve base addresses and add to the list | |
105 for (fn = 0; fn < ninputfiles; fn++) | |
106 { | |
107 check_section_name(name, base, inputfiles[fn]); | |
108 } | |
109 } | |
110 else | |
111 { | |
112 // wildcard section | |
113 // named section | |
114 // look for all instances of a section by the specified name | |
115 // and resolve base addresses and add to the list | |
116 for (fn = 0; fn < ninputfiles; fn++) | |
117 { | |
118 check_section_flags(yesflags, noflags, base, inputfiles[fn]); | |
119 } | |
120 } | |
121 } | |
122 | |
119 | 123 // work out section load order and resolve base addresses for each section |
124 // make a list of sections to load in order | |
125 void resolve_sections(void) | |
126 { | |
127 int laddr = 0; | |
128 int ln, sn, fn; | |
129 | |
130 for (ln = 0; ln < linkscript.nlines; ln++) | |
131 { | |
171 | 132 if (linkscript.lines[ln].loadat >= 0) |
133 laddr = linkscript.lines[ln].loadat; | |
134 add_matching_sections(linkscript.lines[ln].sectname, linkscript.lines[ln].yesflags, linkscript.lines[ln].noflags, &laddr); | |
135 | |
119 | 136 if (linkscript.lines[ln].sectname) |
137 { | |
138 } | |
139 else | |
140 { | |
141 // wildcard section | |
142 // look for all sections not yet processed that match flags | |
143 | |
144 int f = 0; | |
145 int fn0, sn0; | |
146 char *sname; | |
147 | |
148 // named section | |
149 // look for all instances of a section by the specified name | |
150 // and resolve base addresses and add to the list | |
151 for (fn0 = 0; fn0 < ninputfiles; fn0++) | |
152 { | |
153 for (sn0 = 0; sn0 < inputfiles[fn0] -> nsections; sn0++) | |
154 { | |
155 // ignore if the "no flags" bit says to | |
156 if (linkscript.lines[ln].noflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].noflags)) | |
157 continue; | |
158 // ignore unless the yes flags tell us not to | |
159 if (linkscript.lines[ln].yesflags && (inputfiles[fn0] -> sections[sn0].flags & linkscript.lines[ln].yesflags == 0)) | |
160 continue; | |
161 if (inputfiles[fn0] -> sections[sn0].processed == 0) | |
162 { | |
163 sname = inputfiles[fn0] -> sections[sn0].name; | |
164 for (fn = 0; fn < ninputfiles; fn++) | |
165 { | |
166 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++) | |
167 { | |
168 if (!strcmp(sname, inputfiles[fn] -> sections[sn].name)) | |
169 { | |
170 // we have a match | |
171 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1)); | |
172 sectlist[nsects].ptr = &(inputfiles[fn] -> sections[sn]); | |
173 | |
174 inputfiles[fn] -> sections[sn].processed = 1; | |
175 if (!f && linkscript.lines[ln].loadat >= 0) | |
176 { | |
177 f = 1; | |
178 sectlist[nsects].forceaddr = 1; | |
179 laddr = linkscript.lines[ln].loadat; | |
180 } | |
181 else | |
182 { | |
183 sectlist[nsects].forceaddr = 0; | |
184 } | |
185 inputfiles[fn] -> sections[sn].loadaddress = laddr; | |
131
5276565799bd
Fixed load addresses of chained sections and subsections to actually increment
lost
parents:
130
diff
changeset
|
186 laddr += inputfiles[fn] -> sections[sn].codesize; |
119 | 187 nsects++; |
188 } | |
189 } | |
190 } | |
191 } | |
192 } | |
193 } | |
194 } | |
195 } | |
196 | |
197 // theoretically, all the base addresses are set now | |
198 } | |
120 | 199 |
171 | 200 lw_expr_stack_t *find_external_sym_recurse(char *sym, fileinfo_t *fn) |
201 { | |
202 int sn; | |
203 lw_expr_stack_t *r; | |
204 lw_expr_term_t *term; | |
205 symtab_t *se; | |
206 int val; | |
207 | |
208 for (sn = 0; sn < fn -> nsections; sn++) | |
209 { | |
210 for (se = fn -> sections[sn].exportedsyms; se; se = se -> next) | |
211 { | |
212 if (!strcmp(sym, se -> sym)) | |
213 { | |
205 | 214 if (!(fn -> forced)) |
215 { | |
216 fn -> forced = 1; | |
217 nforced = 1; | |
218 } | |
171 | 219 val = se -> offset + fn -> sections[sn].loadaddress; |
220 r = lw_expr_stack_create(); | |
221 term = lw_expr_term_create_int(val & 0xffff); | |
222 lw_expr_stack_push(r, term); | |
223 lw_expr_term_free(term); | |
224 return r; | |
225 } | |
226 } | |
227 } | |
228 | |
229 for (sn = 0; sn < fn -> nsubs; sn++) | |
230 { | |
231 r = find_external_sym_recurse(sym, fn -> subs[sn]); | |
232 if (r) | |
205 | 233 { |
234 if (!(fn -> forced)) | |
235 { | |
236 nforced = 1; | |
237 fn -> forced = 1; | |
238 } | |
171 | 239 return r; |
205 | 240 } |
171 | 241 } |
242 return NULL; | |
243 } | |
244 | |
120 | 245 // resolve all incomplete references now |
246 // anything that is unresolvable at this stage will throw an error | |
247 // because we know the load address of every section now | |
248 lw_expr_stack_t *resolve_sym(char *sym, int symtype, void *state) | |
249 { | |
250 section_t *sect = state; | |
251 lw_expr_term_t *term; | |
252 int val = 0, i, fn; | |
253 lw_expr_stack_t *s; | |
254 symtab_t *se; | |
171 | 255 fileinfo_t *fp; |
256 | |
120 | 257 if (symtype == 1) |
258 { | |
259 // local symbol | |
260 if (!sym) | |
261 { | |
262 val = sect -> loadaddress; | |
263 goto out; | |
264 } | |
265 | |
266 // start with this section | |
267 for (se = sect -> localsyms; se; se = se -> next) | |
268 { | |
269 if (!strcmp(se -> sym, sym)) | |
270 { | |
271 val = se -> offset + sect -> loadaddress; | |
272 goto out; | |
273 } | |
274 } | |
275 // not in this section - check all sections in this file | |
276 for (i = 0; i < sect -> file -> nsections; i++) | |
277 { | |
278 for (se = sect -> file -> sections[i].localsyms; se; se = se -> next) | |
279 { | |
280 if (!strcmp(se -> sym, sym)) | |
281 { | |
282 val = se -> offset + sect -> file -> sections[i].loadaddress; | |
283 goto out; | |
284 } | |
285 } | |
286 } | |
287 // not found | |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
288 symerr = 1; |
120 | 289 fprintf(stderr, "Local symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name); |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
290 goto outerr; |
120 | 291 } |
292 else | |
293 { | |
294 // external symbol | |
295 // read all files in order until found (or not found) | |
173 | 296 if (sect) |
171 | 297 { |
173 | 298 for (fp = sect -> file; fp; fp = fp -> parent) |
299 { | |
300 s = find_external_sym_recurse(sym, fp); | |
301 if (s) | |
302 return s; | |
303 } | |
171 | 304 } |
305 | |
120 | 306 for (fn = 0; fn < ninputfiles; fn++) |
307 { | |
171 | 308 s = find_external_sym_recurse(sym, inputfiles[fn]); |
309 if (s) | |
310 return s; | |
120 | 311 } |
130 | 312 if (sect) |
313 { | |
314 fprintf(stderr, "External symbol %s not found in %s:%s\n", sym, sect -> file -> filename, sect -> name); | |
315 } | |
316 else | |
317 { | |
318 fprintf(stderr, "External symbol %s not found\n", sym); | |
319 } | |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
320 symerr = 1; |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
321 goto outerr; |
120 | 322 } |
323 fprintf(stderr, "Shouldn't ever get here!!!\n"); | |
324 exit(88); | |
325 out: | |
326 s = lw_expr_stack_create(); | |
327 term = lw_expr_term_create_int(val & 0xffff); | |
328 lw_expr_stack_push(s, term); | |
329 lw_expr_term_free(term); | |
330 return s; | |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
331 outerr: |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
332 return NULL; |
120 | 333 } |
334 | |
335 void resolve_references(void) | |
336 { | |
337 int sn; | |
338 reloc_t *rl; | |
339 int rval; | |
121 | 340 |
341 // resolve entry point if required | |
342 // this must resolve to an *exported* symbol and will resolve to the | |
343 // first instance of that symbol | |
344 if (linkscript.execsym) | |
345 { | |
346 lw_expr_stack_t *s; | |
347 | |
348 s = resolve_sym(linkscript.execsym, 0, NULL); | |
187 | 349 if (!s) |
350 { | |
351 fprintf(stderr, "Cannot resolve exec address '%s'\n", linkscript.execsym); | |
352 symerr = 1; | |
353 } | |
354 else | |
355 { | |
356 linkscript.execaddr = lw_expr_get_value(s); | |
357 lw_expr_stack_free(s); | |
358 } | |
121 | 359 } |
120 | 360 |
124 | 361 for (sn = 0; sn < nsects; sn++) |
120 | 362 { |
363 for (rl = sectlist[sn].ptr -> incompletes; rl; rl = rl -> next) | |
364 { | |
365 // do a "simplify" on the expression | |
366 rval = lw_expr_reval(rl -> expr, resolve_sym, sectlist[sn].ptr); | |
367 | |
368 // is it constant? error out if not | |
369 if (rval != 0 || !lw_expr_is_constant(rl -> expr)) | |
370 { | |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
371 fprintf(stderr, "Incomplete reference at %s:%s+%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset); |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
372 symerr = 1; |
120 | 373 } |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
374 else |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
375 { |
243 | 376 // is it a zero-width link? |
377 if (rl -> flags & RELOC_0BIT) | |
378 continue; | |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
379 // put the value into the relocation address |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
380 rval = lw_expr_get_value(rl -> expr); |
204
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
381 if (rl -> flags & RELOC_8BIT) |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
382 { |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
383 sectlist[sn].ptr -> code[rl -> offset] = rval & 0xff; |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
384 } |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
385 else |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
386 { |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
387 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff; |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
388 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff; |
048ebb85f6ef
Added 8 bit external references for base page addressing mode
lost
parents:
187
diff
changeset
|
389 } |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
390 } |
120 | 391 } |
392 } | |
184
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
393 |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
394 if (symerr) |
220a760ec654
Make lwlink display all undefined references instead of bailing after the first one
lost
parents:
173
diff
changeset
|
395 exit(1); |
120 | 396 } |
205 | 397 |
398 /* | |
399 This is just a pared down version of the algo for resolving references. | |
400 */ | |
401 void resolve_files(void) | |
402 { | |
403 int sn; | |
404 int fn; | |
405 reloc_t *rl; | |
206
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
406 lw_expr_stack_t *te; |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
407 |
205 | 408 int rval; |
409 | |
410 // resolve entry point if required | |
411 // this must resolve to an *exported* symbol and will resolve to the | |
412 // first instance of that symbol | |
413 if (linkscript.execsym) | |
414 { | |
415 lw_expr_stack_t *s; | |
416 | |
417 s = resolve_sym(linkscript.execsym, 0, NULL); | |
418 if (!s) | |
419 { | |
420 fprintf(stderr, "Cannot resolve exec address '%s'\n", linkscript.execsym); | |
421 symerr = 1; | |
422 } | |
423 } | |
424 | |
425 do | |
426 { | |
427 nforced = 0; | |
428 for (fn = 0; fn < ninputfiles; fn++) | |
429 { | |
430 if (inputfiles[fn] -> forced == 0) | |
431 continue; | |
432 | |
433 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++) | |
434 { | |
435 for (rl = inputfiles[fn] -> sections[sn].incompletes; rl; rl = rl -> next) | |
436 { | |
437 // do a "simplify" on the expression | |
206
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
438 te = lw_expr_stack_dup(rl -> expr); |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
439 rval = lw_expr_reval(te, resolve_sym, &(inputfiles[fn] -> sections[sn])); |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
440 |
205 | 441 // is it constant? error out if not |
206
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
442 if (rval != 0 || !lw_expr_is_constant(te)) |
205 | 443 { |
444 fprintf(stderr, "Incomplete reference at %s:%s+%02X\n", inputfiles[fn] -> filename, inputfiles[fn] -> sections[sn].name, rl -> offset); | |
445 symerr = 1; | |
446 } | |
206
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
447 lw_expr_stack_free(te); |
205 | 448 } |
449 } | |
450 } | |
451 } | |
452 while (nforced == 1); | |
453 | |
454 if (symerr) | |
455 exit(1); | |
456 | |
457 // theoretically, all files referenced by other files now have "forced" set to 1 | |
206
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
458 for (fn = 0; fn < ninputfiles; fn++) |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
459 { |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
460 if (inputfiles[fn] -> forced == 1) |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
461 continue; |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
462 |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
463 fprintf(stderr, "Warning: %s (%d) does not resolve any symbols\n", inputfiles[fn] -> filename, fn); |
299c5d793aca
Made lwlink smarter about not included unneeded (unreferenced) members of a library file
lost
parents:
205
diff
changeset
|
464 } |
205 | 465 } |