Mercurial > hg-old > index.cgi
annotate lwlink/link.c @ 395:54499b799779
Q&D sanitization of symbols in map files and error messages in lwlink
author | lost@l-w.ca |
---|---|
date | Fri, 23 Jul 2010 16:40:51 -0600 |
parents | 1c31e9005ff7 |
children |
rev | line source |
---|---|
339 | 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 | |
24 #include <config.h> | |
25 | |
26 #include <stdio.h> | |
27 #include <stdlib.h> | |
28 | |
29 #include "expr.h" | |
30 #include "lwlink.h" | |
31 #include "util.h" | |
32 | |
33 struct section_list *sectlist = NULL; | |
34 int nsects = 0; | |
35 static int nforced = 0; | |
36 | |
37 void check_section_name(char *name, int *base, fileinfo_t *fn) | |
38 { | |
39 int sn; | |
40 | |
41 if (fn -> forced == 0) | |
42 return; | |
43 | |
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 } | |
58 for (sn = 0; sn < fn -> nsubs; sn++) | |
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; | |
68 | |
69 if (fn -> forced == 0) | |
70 return; | |
71 | |
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 } | |
89 for (sn = 0; sn < fn -> nsubs; sn++) | |
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 | |
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 { | |
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 | |
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; | |
186 laddr += inputfiles[fn] -> sections[sn].codesize; | |
187 nsects++; | |
188 } | |
189 } | |
190 } | |
191 } | |
192 } | |
193 } | |
194 } | |
195 } | |
196 | |
197 // theoretically, all the base addresses are set now | |
198 } | |
199 | |
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 { | |
214 if (!(fn -> forced)) | |
215 { | |
216 fn -> forced = 1; | |
217 nforced = 1; | |
218 } | |
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) | |
233 { | |
234 if (!(fn -> forced)) | |
235 { | |
236 nforced = 1; | |
237 fn -> forced = 1; | |
238 } | |
239 return r; | |
240 } | |
241 } | |
242 return NULL; | |
243 } | |
244 | |
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; | |
255 fileinfo_t *fp; | |
256 | |
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 | |
288 symerr = 1; | |
395
54499b799779
Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents:
378
diff
changeset
|
289 fprintf(stderr, "Local symbol %s not found in %s:%s\n", sanitize_symbol(sym), sect -> file -> filename, sect -> name); |
339 | 290 goto outerr; |
291 } | |
292 else | |
293 { | |
294 // external symbol | |
295 // read all files in order until found (or not found) | |
296 if (sect) | |
297 { | |
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 } | |
304 } | |
305 | |
306 for (fn = 0; fn < ninputfiles; fn++) | |
307 { | |
308 s = find_external_sym_recurse(sym, inputfiles[fn]); | |
309 if (s) | |
310 return s; | |
311 } | |
312 if (sect) | |
313 { | |
395
54499b799779
Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents:
378
diff
changeset
|
314 fprintf(stderr, "External symbol %s not found in %s:%s\n", sanitize_symbol(sym), sect -> file -> filename, sect -> name); |
339 | 315 } |
316 else | |
317 { | |
318 fprintf(stderr, "External symbol %s not found\n", sym); | |
319 } | |
320 symerr = 1; | |
321 goto outerr; | |
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; | |
331 outerr: | |
332 return NULL; | |
333 } | |
334 | |
335 void resolve_references(void) | |
336 { | |
337 int sn; | |
338 reloc_t *rl; | |
339 int rval; | |
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); | |
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 } | |
359 } | |
360 | |
361 for (sn = 0; sn < nsects; sn++) | |
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 { | |
371 fprintf(stderr, "Incomplete reference at %s:%s+%02X\n", sectlist[sn].ptr -> file -> filename, sectlist[sn].ptr -> name, rl -> offset); | |
372 symerr = 1; | |
373 } | |
374 else | |
375 { | |
376 // put the value into the relocation address | |
377 rval = lw_expr_get_value(rl -> expr); | |
378 if (rl -> flags & RELOC_8BIT) | |
379 { | |
380 sectlist[sn].ptr -> code[rl -> offset] = rval & 0xff; | |
381 } | |
382 else | |
383 { | |
384 sectlist[sn].ptr -> code[rl -> offset] = (rval >> 8) & 0xff; | |
385 sectlist[sn].ptr -> code[rl -> offset + 1] = rval & 0xff; | |
386 } | |
387 } | |
388 } | |
389 } | |
390 | |
391 if (symerr) | |
392 exit(1); | |
393 } | |
394 | |
395 /* | |
396 This is just a pared down version of the algo for resolving references. | |
397 */ | |
398 void resolve_files(void) | |
399 { | |
400 int sn; | |
401 int fn; | |
402 reloc_t *rl; | |
403 lw_expr_stack_t *te; | |
404 | |
405 int rval; | |
406 | |
407 // resolve entry point if required | |
408 // this must resolve to an *exported* symbol and will resolve to the | |
409 // first instance of that symbol | |
410 if (linkscript.execsym) | |
411 { | |
412 lw_expr_stack_t *s; | |
413 | |
414 s = resolve_sym(linkscript.execsym, 0, NULL); | |
415 if (!s) | |
416 { | |
395
54499b799779
Q&D sanitization of symbols in map files and error messages in lwlink
lost@l-w.ca
parents:
378
diff
changeset
|
417 fprintf(stderr, "Cannot resolve exec address '%s'\n", sanitize_symbol(linkscript.execsym)); |
339 | 418 symerr = 1; |
419 } | |
420 } | |
421 | |
422 do | |
423 { | |
424 nforced = 0; | |
425 for (fn = 0; fn < ninputfiles; fn++) | |
426 { | |
427 if (inputfiles[fn] -> forced == 0) | |
428 continue; | |
429 | |
430 for (sn = 0; sn < inputfiles[fn] -> nsections; sn++) | |
431 { | |
432 for (rl = inputfiles[fn] -> sections[sn].incompletes; rl; rl = rl -> next) | |
433 { | |
434 // do a "simplify" on the expression | |
435 te = lw_expr_stack_dup(rl -> expr); | |
436 rval = lw_expr_reval(te, resolve_sym, &(inputfiles[fn] -> sections[sn])); | |
437 | |
438 // is it constant? error out if not | |
439 if (rval != 0 || !lw_expr_is_constant(te)) | |
440 { | |
441 fprintf(stderr, "Incomplete reference at %s:%s+%02X\n", inputfiles[fn] -> filename, inputfiles[fn] -> sections[sn].name, rl -> offset); | |
442 symerr = 1; | |
443 } | |
444 lw_expr_stack_free(te); | |
445 } | |
446 } | |
447 } | |
448 } | |
449 while (nforced == 1); | |
450 | |
451 if (symerr) | |
452 exit(1); | |
453 | |
454 // theoretically, all files referenced by other files now have "forced" set to 1 | |
455 for (fn = 0; fn < ninputfiles; fn++) | |
456 { | |
457 if (inputfiles[fn] -> forced == 1) | |
458 continue; | |
459 | |
460 fprintf(stderr, "Warning: %s (%d) does not resolve any symbols\n", inputfiles[fn] -> filename, fn); | |
461 } | |
462 } |