comparison lwlink/link.c @ 157:4682460aed00

Correct bug with handling symbol resolution and constant section handling
author lost@l-w.ca
date Sun, 28 Aug 2011 11:45:38 -0600
parents fc8386b13399
children d389adbcc4ab
comparison
equal deleted inserted replaced
156:fc8386b13399 157:4682460aed00
33 void check_os9(void); 33 void check_os9(void);
34 34
35 struct section_list *sectlist = NULL; 35 struct section_list *sectlist = NULL;
36 int nsects = 0; 36 int nsects = 0;
37 static int nforced = 0; 37 static int nforced = 0;
38 static int resolveonly = 0;
38 39
39 void check_section_name(char *name, int *base, fileinfo_t *fn) 40 void check_section_name(char *name, int *base, fileinfo_t *fn)
40 { 41 {
41 int sn; 42 int sn;
42 43
44 // fprintf(stderr, "Considering sections in %s (%d) for %s\n", fn -> filename, fn -> forced, name);
43 if (fn -> forced == 0) 45 if (fn -> forced == 0)
44 return; 46 return;
45 47
46 for (sn = 0; sn < fn -> nsections; sn++) 48 for (sn = 0; sn < fn -> nsections; sn++)
47 { 49 {
48 if (!strcmp(name, (char *)(fn -> sections[sn].name)) && (fn -> sections[sn].flags | SECTION_CONST) == 0) 50 // fprintf(stderr, " Considering section %s\n", fn -> sections[sn].name);
49 { 51 if (!strcmp(name, (char *)(fn -> sections[sn].name)))
52 {
53 if (fn -> sections[sn].flags & SECTION_CONST)
54 continue;
50 // we have a match 55 // we have a match
56 // fprintf(stderr, " Found\n");
51 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1)); 57 sectlist = lw_realloc(sectlist, sizeof(struct section_list) * (nsects + 1));
52 sectlist[nsects].ptr = &(fn -> sections[sn]); 58 sectlist[nsects].ptr = &(fn -> sections[sn]);
53 59
54 fn -> sections[sn].processed = 1; 60 fn -> sections[sn].processed = 1;
55 fn -> sections[sn].loadaddress = *base; 61 fn -> sections[sn].loadaddress = *base;
56 *base += fn -> sections[sn].codesize; 62 *base += fn -> sections[sn].codesize;
57 nsects++; 63 nsects++;
64 // fprintf(stderr, "Adding section %s (%s)\n",fn -> sections[sn].name, fn -> filename);
58 } 65 }
59 } 66 }
60 for (sn = 0; sn < fn -> nsubs; sn++) 67 for (sn = 0; sn < fn -> nsubs; sn++)
61 { 68 {
62 check_section_name(name, base, fn -> subs[sn]); 69 check_section_name(name, base, fn -> subs[sn]);
66 void add_matching_sections(char *name, int yesflags, int noflags, int *base); 73 void add_matching_sections(char *name, int yesflags, int noflags, int *base);
67 void check_section_flags(int yesflags, int noflags, int *base, fileinfo_t *fn) 74 void check_section_flags(int yesflags, int noflags, int *base, fileinfo_t *fn)
68 { 75 {
69 int sn; 76 int sn;
70 77
78 // fprintf(stderr, "Considering sections in %s (%d) for %x/%x\n", fn -> filename, fn -> forced, yesflags, noflags);
79
71 if (fn -> forced == 0) 80 if (fn -> forced == 0)
72 return; 81 return;
73 82
74 for (sn = 0; sn < fn -> nsections; sn++) 83 for (sn = 0; sn < fn -> nsections; sn++)
75 { 84 {
76 // ignore "constant" sections 85 // ignore "constant" sections - they're added during the file resolve stage
77 if (fn -> sections[sn].flags & SECTION_CONST) 86 if (fn -> sections[sn].flags & SECTION_CONST)
78 continue; 87 continue;
79 // ignore if the noflags tell us to 88 // ignore if the noflags tell us to
80 if (noflags && (fn -> sections[sn].flags & noflags)) 89 if (noflags && (fn -> sections[sn].flags & noflags))
81 continue; 90 continue;
85 // ignore it if already processed 94 // ignore it if already processed
86 if (fn -> sections[sn].processed) 95 if (fn -> sections[sn].processed)
87 continue; 96 continue;
88 97
89 // we have a match - now collect *all* sections of the same name! 98 // we have a match - now collect *all* sections of the same name!
99 // fprintf(stderr, " Found\n");
90 add_matching_sections((char *)(fn -> sections[sn].name), 0, 0, base); 100 add_matching_sections((char *)(fn -> sections[sn].name), 0, 0, base);
91 101
92 // and then continue looking for sections 102 // and then continue looking for sections
93 } 103 }
94 for (sn = 0; sn < fn -> nsubs; sn++) 104 for (sn = 0; sn < fn -> nsubs; sn++)
215 225
216 for (sn = 0; sn < fn -> nsections; sn++) 226 for (sn = 0; sn < fn -> nsections; sn++)
217 { 227 {
218 for (se = fn -> sections[sn].exportedsyms; se; se = se -> next) 228 for (se = fn -> sections[sn].exportedsyms; se; se = se -> next)
219 { 229 {
230 // fprintf(stderr, " Considering %s\n", se -> sym);
220 if (!strcmp(sym, (char *)(se -> sym))) 231 if (!strcmp(sym, (char *)(se -> sym)))
221 { 232 {
233 // fprintf(stderr, " Match (%d)\n", fn -> sections[sn].processed);
222 // if the section was not previously processed and is CONSTANT, force it in 234 // if the section was not previously processed and is CONSTANT, force it in
223 // otherwise error out if it is not being processed 235 // otherwise error out if it is not being processed
224 if (fn -> sections[sn].processed == 0) 236 if (fn -> sections[sn].processed == 0)
225 { 237 {
226 if (fn -> sections[sn].flags & SECTION_CONST) 238 if (fn -> sections[sn].flags & SECTION_CONST)
232 fn -> sections[sn].loadaddress = 0; 244 fn -> sections[sn].loadaddress = 0;
233 nsects++; 245 nsects++;
234 } 246 }
235 else 247 else
236 { 248 {
237 // if we're in a non-processed section, bail! 249 if (resolveonly == 0)
238 continue; 250 {
251 fprintf(stderr, "Symbol %s found in section %s (%s) which is not going to be included\n", sym, fn -> sections[sn].name, fn -> filename);
252 continue;
253 }
239 } 254 }
240 } 255 }
241 // fprintf(stderr, "Found symbol %s in %s\n", sym, fn -> filename); 256 // fprintf(stderr, "Found symbol %s in %s\n", sym, fn -> filename);
242 if (!(fn -> forced)) 257 if (!(fn -> forced))
243 { 258 {
259 } 274 }
260 } 275 }
261 276
262 for (sn = 0; sn < fn -> nsubs; sn++) 277 for (sn = 0; sn < fn -> nsubs; sn++)
263 { 278 {
279 // fprintf(stderr, "Looking in %s\n", fn -> subs[sn] -> filename);
264 r = find_external_sym_recurse(sym, fn -> subs[sn]); 280 r = find_external_sym_recurse(sym, fn -> subs[sn]);
265 if (r) 281 if (r)
266 { 282 {
267 // fprintf(stderr, "Found symbol %s in %s\n", sym, fn -> filename); 283 // fprintf(stderr, "Found symbol %s in %s\n", sym, fn -> filename);
268 if (!(fn -> forced)) 284 if (!(fn -> forced))
338 // read all files in order until found (or not found) 354 // read all files in order until found (or not found)
339 if (sect) 355 if (sect)
340 { 356 {
341 for (fp = sect -> file; fp; fp = fp -> parent) 357 for (fp = sect -> file; fp; fp = fp -> parent)
342 { 358 {
359 // fprintf(stderr, "Looking in %s\n", fp -> filename);
343 s = find_external_sym_recurse(sym, fp); 360 s = find_external_sym_recurse(sym, fp);
344 if (s) 361 if (s)
345 return s; 362 return s;
346 } 363 }
347 } 364 }
348 365
349 for (fn = 0; fn < ninputfiles; fn++) 366 for (fn = 0; fn < ninputfiles; fn++)
350 { 367 {
368 // fprintf(stderr, "Looking in %s\n", inputfiles[fn] -> filename);
351 s = find_external_sym_recurse(sym, inputfiles[fn]); 369 s = find_external_sym_recurse(sym, inputfiles[fn]);
352 if (s) 370 if (s)
353 return s; 371 return s;
354 } 372 }
355 if (sect) 373 if (sect)
478 */ 496 */
479 void resolve_files(void) 497 void resolve_files(void)
480 { 498 {
481 int fn; 499 int fn;
482 500
501 resolveonly = 1;
502
483 // resolve entry point if required 503 // resolve entry point if required
484 // this must resolve to an *exported* symbol and will resolve to the 504 // this must resolve to an *exported* symbol and will resolve to the
485 // first instance of that symbol 505 // first instance of that symbol
486 if (linkscript.execsym) 506 // if (linkscript.execsym)
487 { 507 // {
488 lw_expr_stack_t *s; 508 // lw_expr_stack_t *s;
489 509 //
490 s = resolve_sym(linkscript.execsym, 0, NULL); 510 // s = resolve_sym(linkscript.execsym, 0, NULL);
491 if (!s) 511 // if (!s)
492 { 512 // {
493 fprintf(stderr, "Cannot resolve exec address '%s'\n", sanitize_symbol(linkscript.execsym)); 513 // fprintf(stderr, "Cannot resolve exec address '%s'\n", sanitize_symbol(linkscript.execsym));
494 symerr = 1; 514 // symerr = 1;
495 } 515 // }
496 } 516 // }
497 517
498 do 518 do
499 { 519 {
500 nforced = 0; 520 nforced = 0;
501 for (fn = 0; fn < ninputfiles; fn++) 521 for (fn = 0; fn < ninputfiles; fn++)
503 resolve_files_aux(inputfiles[fn]); 523 resolve_files_aux(inputfiles[fn]);
504 } 524 }
505 } 525 }
506 while (nforced == 1); 526 while (nforced == 1);
507 527
508 if (symerr) 528 resolveonly = 0;
509 exit(1); 529
510 530 // if (symerr)
531 // exit(1);
532
533 symerr = 0;
511 // theoretically, all files referenced by other files now have "forced" set to 1 534 // theoretically, all files referenced by other files now have "forced" set to 1
512 for (fn = 0; fn < ninputfiles; fn++) 535 for (fn = 0; fn < ninputfiles; fn++)
513 { 536 {
514 if (inputfiles[fn] -> forced == 1) 537 if (inputfiles[fn] -> forced == 1)
515 continue; 538 continue;