Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* This module handles expression trees. |
8c95a62e | 2 | Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 |
87f2a346 | 3 | Free Software Foundation, Inc. |
8c95a62e | 4 | Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>. |
252b5132 RH |
5 | |
6 | This file is part of GLD, the Gnu Linker. | |
7 | ||
8 | GLD is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GLD is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GLD; see the file COPYING. If not, write to the Free | |
20 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
21 | 02111-1307, USA. */ | |
22 | ||
8c95a62e | 23 | /* This module is in charge of working out the contents of expressions. |
252b5132 | 24 | |
8c95a62e KH |
25 | It has to keep track of the relative/absness of a symbol etc. This |
26 | is done by keeping all values in a struct (an etree_value_type) | |
27 | which contains a value, a section to which it is relative and a | |
28 | valid bit. */ | |
252b5132 | 29 | |
252b5132 RH |
30 | #include "bfd.h" |
31 | #include "sysdep.h" | |
32 | #include "bfdlink.h" | |
33 | ||
34 | #include "ld.h" | |
35 | #include "ldmain.h" | |
36 | #include "ldmisc.h" | |
37 | #include "ldexp.h" | |
38 | #include "ldgram.h" | |
39 | #include "ldlang.h" | |
40 | ||
41 | static void exp_print_token PARAMS ((token_code_type code)); | |
42 | static void make_abs PARAMS ((etree_value_type *ptr)); | |
43 | static etree_value_type new_abs PARAMS ((bfd_vma value)); | |
44 | static void check PARAMS ((lang_output_section_statement_type *os, | |
45 | const char *name, const char *op)); | |
46 | static etree_value_type new_rel | |
47 | PARAMS ((bfd_vma value, lang_output_section_statement_type *section)); | |
48 | static etree_value_type new_rel_from_section | |
49 | PARAMS ((bfd_vma value, lang_output_section_statement_type *section)); | |
50 | static etree_value_type fold_binary | |
51 | PARAMS ((etree_type *tree, | |
52 | lang_output_section_statement_type *current_section, | |
53 | lang_phase_type allocation_done, | |
54 | bfd_vma dot, bfd_vma *dotp)); | |
55 | static etree_value_type fold_name | |
56 | PARAMS ((etree_type *tree, | |
57 | lang_output_section_statement_type *current_section, | |
58 | lang_phase_type allocation_done, | |
59 | bfd_vma dot)); | |
60 | static etree_value_type exp_fold_tree_no_dot | |
61 | PARAMS ((etree_type *tree, | |
62 | lang_output_section_statement_type *current_section, | |
63 | lang_phase_type allocation_done)); | |
64 | ||
65 | static void | |
66 | exp_print_token (code) | |
67 | token_code_type code; | |
68 | { | |
69 | static CONST struct | |
8c95a62e KH |
70 | { |
71 | token_code_type code; | |
72 | char *name; | |
73 | } table[] = { | |
74 | { INT, "int" }, | |
75 | { REL, "relocateable" }, | |
76 | { NAME, "NAME" }, | |
77 | { PLUSEQ, "+=" }, | |
78 | { MINUSEQ, "-=" }, | |
79 | { MULTEQ, "*=" }, | |
80 | { DIVEQ, "/=" }, | |
81 | { LSHIFTEQ, "<<=" }, | |
82 | { RSHIFTEQ, ">>=" }, | |
83 | { ANDEQ, "&=" }, | |
84 | { OREQ, "|=" }, | |
85 | { OROR, "||" }, | |
86 | { ANDAND, "&&" }, | |
87 | { EQ, "==" }, | |
88 | { NE, "!=" }, | |
89 | { LE, "<=" }, | |
90 | { GE, ">=" }, | |
91 | { LSHIFT, "<<" }, | |
92 | { RSHIFT, ">>=" }, | |
93 | { ALIGN_K, "ALIGN" }, | |
94 | { BLOCK, "BLOCK" }, | |
95 | { SECTIONS, "SECTIONS" }, | |
96 | { SIZEOF_HEADERS, "SIZEOF_HEADERS" }, | |
97 | { NEXT, "NEXT" }, | |
98 | { SIZEOF, "SIZEOF" }, | |
99 | { ADDR, "ADDR" }, | |
100 | { LOADADDR, "LOADADDR" }, | |
101 | { MEMORY, "MEMORY" }, | |
102 | { DEFINED, "DEFINED" }, | |
103 | { TARGET_K, "TARGET" }, | |
104 | { SEARCH_DIR, "SEARCH_DIR" }, | |
105 | { MAP, "MAP" }, | |
106 | { QUAD, "QUAD" }, | |
107 | { SQUAD, "SQUAD" }, | |
108 | { LONG, "LONG" }, | |
109 | { SHORT, "SHORT" }, | |
110 | { BYTE, "BYTE" }, | |
111 | { ENTRY, "ENTRY" }, | |
112 | { 0, (char *) NULL } | |
113 | }; | |
252b5132 RH |
114 | unsigned int idx; |
115 | ||
8c95a62e KH |
116 | for (idx = 0; table[idx].name != (char *) NULL; idx++) |
117 | { | |
118 | if (table[idx].code == code) | |
119 | { | |
120 | fprintf (config.map_file, "%s", table[idx].name); | |
121 | return; | |
122 | } | |
252b5132 | 123 | } |
252b5132 | 124 | /* Not in table, just print it alone */ |
8c95a62e | 125 | fprintf (config.map_file, "%c", code); |
252b5132 RH |
126 | } |
127 | ||
4de2d33d | 128 | static void |
252b5132 RH |
129 | make_abs (ptr) |
130 | etree_value_type *ptr; | |
131 | { | |
8c95a62e KH |
132 | asection *s = ptr->section->bfd_section; |
133 | ptr->value += s->vma; | |
134 | ptr->section = abs_output_section; | |
252b5132 RH |
135 | } |
136 | ||
137 | static etree_value_type | |
138 | new_abs (value) | |
139 | bfd_vma value; | |
140 | { | |
141 | etree_value_type new; | |
142 | new.valid_p = true; | |
143 | new.section = abs_output_section; | |
144 | new.value = value; | |
145 | return new; | |
146 | } | |
147 | ||
4de2d33d | 148 | static void |
252b5132 RH |
149 | check (os, name, op) |
150 | lang_output_section_statement_type *os; | |
151 | const char *name; | |
152 | const char *op; | |
153 | { | |
154 | if (os == NULL) | |
155 | einfo (_("%F%P: %s uses undefined section %s\n"), op, name); | |
156 | if (! os->processed) | |
157 | einfo (_("%F%P: %s forward reference of section %s\n"), op, name); | |
158 | } | |
159 | ||
160 | etree_type * | |
161 | exp_intop (value) | |
162 | bfd_vma value; | |
163 | { | |
8c95a62e | 164 | etree_type *new = (etree_type *) stat_alloc (sizeof (new->value)); |
252b5132 RH |
165 | new->type.node_code = INT; |
166 | new->value.value = value; | |
167 | new->type.node_class = etree_value; | |
168 | return new; | |
169 | ||
170 | } | |
171 | ||
172 | /* Build an expression representing an unnamed relocateable value. */ | |
173 | ||
174 | etree_type * | |
175 | exp_relop (section, value) | |
176 | asection *section; | |
177 | bfd_vma value; | |
178 | { | |
179 | etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel)); | |
180 | new->type.node_code = REL; | |
181 | new->type.node_class = etree_rel; | |
182 | new->rel.section = section; | |
183 | new->rel.value = value; | |
184 | return new; | |
185 | } | |
186 | ||
187 | static etree_value_type | |
188 | new_rel (value, section) | |
189 | bfd_vma value; | |
190 | lang_output_section_statement_type *section; | |
191 | { | |
192 | etree_value_type new; | |
193 | new.valid_p = true; | |
194 | new.value = value; | |
195 | new.section = section; | |
196 | return new; | |
197 | } | |
198 | ||
199 | static etree_value_type | |
200 | new_rel_from_section (value, section) | |
201 | bfd_vma value; | |
202 | lang_output_section_statement_type *section; | |
203 | { | |
204 | etree_value_type new; | |
205 | new.valid_p = true; | |
206 | new.value = value; | |
207 | new.section = section; | |
208 | ||
8c95a62e | 209 | new.value -= section->bfd_section->vma; |
252b5132 RH |
210 | |
211 | return new; | |
212 | } | |
213 | ||
4de2d33d | 214 | static etree_value_type |
252b5132 RH |
215 | fold_binary (tree, current_section, allocation_done, dot, dotp) |
216 | etree_type *tree; | |
217 | lang_output_section_statement_type *current_section; | |
218 | lang_phase_type allocation_done; | |
219 | bfd_vma dot; | |
220 | bfd_vma *dotp; | |
221 | { | |
222 | etree_value_type result; | |
223 | ||
224 | result = exp_fold_tree (tree->binary.lhs, current_section, | |
225 | allocation_done, dot, dotp); | |
226 | if (result.valid_p) | |
227 | { | |
228 | etree_value_type other; | |
229 | ||
230 | other = exp_fold_tree (tree->binary.rhs, | |
231 | current_section, | |
8c95a62e | 232 | allocation_done, dot, dotp); |
252b5132 RH |
233 | if (other.valid_p) |
234 | { | |
235 | /* If the values are from different sections, or this is an | |
236 | absolute expression, make both the source arguments | |
237 | absolute. However, adding or subtracting an absolute | |
238 | value from a relative value is meaningful, and is an | |
239 | exception. */ | |
240 | if (current_section != abs_output_section | |
241 | && (other.section == abs_output_section | |
242 | || (result.section == abs_output_section | |
243 | && tree->type.node_code == '+')) | |
244 | && (tree->type.node_code == '+' | |
245 | || tree->type.node_code == '-')) | |
246 | { | |
247 | etree_value_type hold; | |
248 | ||
249 | /* If there is only one absolute term, make sure it is the | |
250 | second one. */ | |
251 | if (other.section != abs_output_section) | |
252 | { | |
253 | hold = result; | |
254 | result = other; | |
255 | other = hold; | |
256 | } | |
257 | } | |
258 | else if (result.section != other.section | |
259 | || current_section == abs_output_section) | |
260 | { | |
8c95a62e KH |
261 | make_abs (&result); |
262 | make_abs (&other); | |
252b5132 RH |
263 | } |
264 | ||
4de2d33d | 265 | switch (tree->type.node_code) |
252b5132 RH |
266 | { |
267 | case '%': | |
268 | if (other.value == 0) | |
269 | einfo (_("%F%S %% by zero\n")); | |
270 | result.value = ((bfd_signed_vma) result.value | |
271 | % (bfd_signed_vma) other.value); | |
272 | break; | |
273 | ||
274 | case '/': | |
275 | if (other.value == 0) | |
276 | einfo (_("%F%S / by zero\n")); | |
277 | result.value = ((bfd_signed_vma) result.value | |
278 | / (bfd_signed_vma) other.value); | |
279 | break; | |
280 | ||
281 | #define BOP(x,y) case x : result.value = result.value y other.value; break; | |
8c95a62e KH |
282 | BOP ('+', +); |
283 | BOP ('*', *); | |
284 | BOP ('-', -); | |
285 | BOP (LSHIFT, <<); | |
286 | BOP (RSHIFT, >>); | |
287 | BOP (EQ, ==); | |
288 | BOP (NE, !=); | |
289 | BOP ('<', <); | |
290 | BOP ('>', >); | |
291 | BOP (LE, <=); | |
292 | BOP (GE, >=); | |
293 | BOP ('&', &); | |
294 | BOP ('^', ^); | |
295 | BOP ('|', |); | |
296 | BOP (ANDAND, &&); | |
297 | BOP (OROR, ||); | |
252b5132 RH |
298 | |
299 | case MAX_K: | |
300 | if (result.value < other.value) | |
301 | result = other; | |
302 | break; | |
303 | ||
304 | case MIN_K: | |
305 | if (result.value > other.value) | |
306 | result = other; | |
307 | break; | |
308 | ||
309 | default: | |
8c95a62e | 310 | FAIL (); |
252b5132 RH |
311 | } |
312 | } | |
313 | else | |
314 | { | |
315 | result.valid_p = false; | |
316 | } | |
317 | } | |
318 | ||
319 | return result; | |
320 | } | |
321 | ||
4de2d33d | 322 | etree_value_type |
252b5132 RH |
323 | invalid () |
324 | { | |
325 | etree_value_type new; | |
326 | new.valid_p = false; | |
327 | return new; | |
328 | } | |
329 | ||
4de2d33d | 330 | static etree_value_type |
252b5132 RH |
331 | fold_name (tree, current_section, allocation_done, dot) |
332 | etree_type *tree; | |
333 | lang_output_section_statement_type *current_section; | |
8c95a62e | 334 | lang_phase_type allocation_done; |
252b5132 RH |
335 | bfd_vma dot; |
336 | { | |
337 | etree_value_type result; | |
4de2d33d | 338 | switch (tree->type.node_code) |
8c95a62e KH |
339 | { |
340 | case SIZEOF_HEADERS: | |
341 | if (allocation_done != lang_first_phase_enum) | |
342 | { | |
343 | result = new_abs ((bfd_vma) | |
344 | bfd_sizeof_headers (output_bfd, | |
345 | link_info.relocateable)); | |
346 | } | |
347 | else | |
348 | { | |
252b5132 | 349 | result.valid_p = false; |
8c95a62e KH |
350 | } |
351 | break; | |
352 | case DEFINED: | |
353 | if (allocation_done == lang_first_phase_enum) | |
252b5132 | 354 | result.valid_p = false; |
8c95a62e KH |
355 | else |
356 | { | |
357 | struct bfd_link_hash_entry *h; | |
358 | ||
359 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
360 | tree->name.name, | |
361 | false, false, true); | |
362 | result.value = (h != (struct bfd_link_hash_entry *) NULL | |
363 | && (h->type == bfd_link_hash_defined | |
364 | || h->type == bfd_link_hash_defweak | |
365 | || h->type == bfd_link_hash_common)); | |
366 | result.section = 0; | |
367 | result.valid_p = true; | |
368 | } | |
369 | break; | |
370 | case NAME: | |
371 | result.valid_p = false; | |
372 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) | |
373 | { | |
374 | if (allocation_done != lang_first_phase_enum) | |
375 | result = new_rel_from_section (dot, current_section); | |
376 | else | |
377 | result = invalid (); | |
378 | } | |
379 | else if (allocation_done != lang_first_phase_enum) | |
380 | { | |
381 | struct bfd_link_hash_entry *h; | |
382 | ||
383 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
384 | tree->name.name, | |
385 | false, false, true); | |
386 | if (h != NULL | |
387 | && (h->type == bfd_link_hash_defined | |
388 | || h->type == bfd_link_hash_defweak)) | |
389 | { | |
390 | if (bfd_is_abs_section (h->u.def.section)) | |
391 | result = new_abs (h->u.def.value); | |
392 | else if (allocation_done == lang_final_phase_enum | |
393 | || allocation_done == lang_allocating_phase_enum) | |
394 | { | |
395 | asection *output_section; | |
396 | ||
397 | output_section = h->u.def.section->output_section; | |
398 | if (output_section == NULL) | |
399 | einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"), | |
400 | tree->name.name); | |
401 | else | |
402 | { | |
403 | lang_output_section_statement_type *os; | |
404 | ||
405 | os = (lang_output_section_statement_lookup | |
406 | (bfd_get_section_name (output_bfd, | |
407 | output_section))); | |
408 | ||
409 | /* FIXME: Is this correct if this section is | |
410 | being linked with -R? */ | |
411 | result = new_rel ((h->u.def.value | |
412 | + h->u.def.section->output_offset), | |
413 | os); | |
414 | } | |
415 | } | |
416 | } | |
417 | else if (allocation_done == lang_final_phase_enum) | |
418 | einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"), | |
419 | tree->name.name); | |
420 | } | |
421 | break; | |
422 | ||
423 | case ADDR: | |
424 | if (allocation_done != lang_first_phase_enum) | |
425 | { | |
426 | lang_output_section_statement_type *os; | |
427 | ||
428 | os = lang_output_section_find (tree->name.name); | |
429 | check (os, tree->name.name, "ADDR"); | |
430 | result = new_rel (0, os); | |
431 | } | |
432 | else | |
433 | result = invalid (); | |
434 | break; | |
435 | ||
436 | case LOADADDR: | |
437 | if (allocation_done != lang_first_phase_enum) | |
438 | { | |
439 | lang_output_section_statement_type *os; | |
440 | ||
441 | os = lang_output_section_find (tree->name.name); | |
442 | check (os, tree->name.name, "LOADADDR"); | |
443 | if (os->load_base == NULL) | |
252b5132 | 444 | result = new_rel (0, os); |
8c95a62e KH |
445 | else |
446 | result = exp_fold_tree_no_dot (os->load_base, | |
447 | abs_output_section, | |
448 | allocation_done); | |
449 | } | |
450 | else | |
451 | result = invalid (); | |
452 | break; | |
453 | ||
454 | case SIZEOF: | |
455 | if (allocation_done != lang_first_phase_enum) | |
456 | { | |
457 | int opb = bfd_octets_per_byte (output_bfd); | |
458 | lang_output_section_statement_type *os; | |
459 | ||
460 | os = lang_output_section_find (tree->name.name); | |
461 | check (os, tree->name.name, "SIZEOF"); | |
462 | result = new_abs (os->bfd_section->_raw_size / opb); | |
463 | } | |
464 | else | |
465 | result = invalid (); | |
466 | break; | |
467 | ||
468 | default: | |
469 | FAIL (); | |
470 | break; | |
471 | } | |
252b5132 RH |
472 | |
473 | return result; | |
474 | } | |
8c95a62e | 475 | |
4de2d33d | 476 | etree_value_type |
252b5132 RH |
477 | exp_fold_tree (tree, current_section, allocation_done, dot, dotp) |
478 | etree_type *tree; | |
479 | lang_output_section_statement_type *current_section; | |
8c95a62e | 480 | lang_phase_type allocation_done; |
252b5132 RH |
481 | bfd_vma dot; |
482 | bfd_vma *dotp; | |
483 | { | |
484 | etree_value_type result; | |
485 | ||
486 | if (tree == NULL) | |
487 | { | |
488 | result.valid_p = false; | |
489 | return result; | |
490 | } | |
491 | ||
4de2d33d | 492 | switch (tree->type.node_class) |
252b5132 RH |
493 | { |
494 | case etree_value: | |
495 | result = new_rel (tree->value.value, current_section); | |
496 | break; | |
497 | ||
498 | case etree_rel: | |
499 | if (allocation_done != lang_final_phase_enum) | |
500 | result.valid_p = false; | |
501 | else | |
502 | result = new_rel ((tree->rel.value | |
503 | + tree->rel.section->output_section->vma | |
504 | + tree->rel.section->output_offset), | |
505 | current_section); | |
506 | break; | |
507 | ||
508 | case etree_assert: | |
509 | result = exp_fold_tree (tree->assert_s.child, | |
8c95a62e KH |
510 | current_section, |
511 | allocation_done, dot, dotp); | |
252b5132 RH |
512 | if (result.valid_p) |
513 | { | |
514 | if (! result.value) | |
515 | einfo ("%F%P: %s\n", tree->assert_s.message); | |
516 | return result; | |
517 | } | |
518 | break; | |
519 | ||
520 | case etree_unary: | |
521 | result = exp_fold_tree (tree->unary.child, | |
522 | current_section, | |
523 | allocation_done, dot, dotp); | |
524 | if (result.valid_p) | |
525 | { | |
4de2d33d | 526 | switch (tree->type.node_code) |
252b5132 RH |
527 | { |
528 | case ALIGN_K: | |
529 | if (allocation_done != lang_first_phase_enum) | |
530 | result = new_rel_from_section (ALIGN_N (dot, result.value), | |
531 | current_section); | |
532 | else | |
533 | result.valid_p = false; | |
534 | break; | |
535 | ||
536 | case ABSOLUTE: | |
537 | if (allocation_done != lang_first_phase_enum && result.valid_p) | |
538 | { | |
539 | result.value += result.section->bfd_section->vma; | |
540 | result.section = abs_output_section; | |
541 | } | |
4de2d33d | 542 | else |
252b5132 RH |
543 | result.valid_p = false; |
544 | break; | |
545 | ||
546 | case '~': | |
547 | make_abs (&result); | |
548 | result.value = ~result.value; | |
549 | break; | |
550 | ||
551 | case '!': | |
552 | make_abs (&result); | |
553 | result.value = !result.value; | |
554 | break; | |
555 | ||
556 | case '-': | |
557 | make_abs (&result); | |
558 | result.value = -result.value; | |
559 | break; | |
560 | ||
561 | case NEXT: | |
562 | /* Return next place aligned to value. */ | |
563 | if (allocation_done == lang_allocating_phase_enum) | |
564 | { | |
565 | make_abs (&result); | |
566 | result.value = ALIGN_N (dot, result.value); | |
567 | } | |
568 | else | |
569 | result.valid_p = false; | |
570 | break; | |
571 | ||
572 | default: | |
573 | FAIL (); | |
574 | break; | |
575 | } | |
576 | } | |
577 | break; | |
578 | ||
579 | case etree_trinary: | |
580 | result = exp_fold_tree (tree->trinary.cond, current_section, | |
581 | allocation_done, dot, dotp); | |
582 | if (result.valid_p) | |
583 | result = exp_fold_tree ((result.value | |
584 | ? tree->trinary.lhs | |
585 | : tree->trinary.rhs), | |
586 | current_section, | |
587 | allocation_done, dot, dotp); | |
588 | break; | |
589 | ||
590 | case etree_binary: | |
591 | result = fold_binary (tree, current_section, allocation_done, | |
592 | dot, dotp); | |
593 | break; | |
594 | ||
595 | case etree_assign: | |
596 | case etree_provide: | |
597 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) | |
598 | { | |
599 | /* Assignment to dot can only be done during allocation */ | |
600 | if (tree->type.node_class == etree_provide) | |
601 | einfo (_("%F%S can not PROVIDE assignment to location counter\n")); | |
602 | if (allocation_done == lang_allocating_phase_enum | |
603 | || (allocation_done == lang_final_phase_enum | |
604 | && current_section == abs_output_section)) | |
605 | { | |
606 | result = exp_fold_tree (tree->assign.src, | |
607 | current_section, | |
608 | lang_allocating_phase_enum, dot, | |
609 | dotp); | |
610 | if (! result.valid_p) | |
611 | einfo (_("%F%S invalid assignment to location counter\n")); | |
612 | else | |
613 | { | |
614 | if (current_section == NULL) | |
615 | einfo (_("%F%S assignment to location counter invalid outside of SECTION\n")); | |
616 | else | |
617 | { | |
618 | bfd_vma nextdot; | |
619 | ||
620 | nextdot = (result.value | |
621 | + current_section->bfd_section->vma); | |
622 | if (nextdot < dot | |
623 | && current_section != abs_output_section) | |
624 | { | |
625 | einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"), | |
626 | dot, nextdot); | |
627 | } | |
628 | else | |
4de2d33d | 629 | *dotp = nextdot; |
252b5132 RH |
630 | } |
631 | } | |
632 | } | |
633 | } | |
634 | else | |
635 | { | |
636 | result = exp_fold_tree (tree->assign.src, | |
637 | current_section, allocation_done, | |
638 | dot, dotp); | |
639 | if (result.valid_p) | |
640 | { | |
641 | boolean create; | |
642 | struct bfd_link_hash_entry *h; | |
643 | ||
644 | if (tree->type.node_class == etree_assign) | |
645 | create = true; | |
646 | else | |
647 | create = false; | |
648 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, | |
649 | create, false, false); | |
650 | if (h == (struct bfd_link_hash_entry *) NULL) | |
651 | { | |
652 | if (tree->type.node_class == etree_assign) | |
653 | einfo (_("%P%F:%s: hash creation failed\n"), | |
654 | tree->assign.dst); | |
655 | } | |
656 | else if (tree->type.node_class == etree_provide | |
657 | && h->type != bfd_link_hash_undefined | |
658 | && h->type != bfd_link_hash_common) | |
659 | { | |
660 | /* Do nothing. The symbol was defined by some | |
661 | object. */ | |
662 | } | |
663 | else | |
664 | { | |
665 | /* FIXME: Should we worry if the symbol is already | |
666 | defined? */ | |
667 | h->type = bfd_link_hash_defined; | |
668 | h->u.def.value = result.value; | |
669 | h->u.def.section = result.section->bfd_section; | |
670 | } | |
671 | } | |
672 | } | |
673 | break; | |
674 | ||
675 | case etree_name: | |
676 | result = fold_name (tree, current_section, allocation_done, dot); | |
677 | break; | |
678 | ||
679 | default: | |
680 | FAIL (); | |
681 | break; | |
682 | } | |
683 | ||
684 | return result; | |
685 | } | |
686 | ||
4de2d33d | 687 | static etree_value_type |
252b5132 RH |
688 | exp_fold_tree_no_dot (tree, current_section, allocation_done) |
689 | etree_type *tree; | |
690 | lang_output_section_statement_type *current_section; | |
691 | lang_phase_type allocation_done; | |
692 | { | |
8c95a62e KH |
693 | return exp_fold_tree (tree, current_section, allocation_done, |
694 | (bfd_vma) 0, (bfd_vma *) NULL); | |
252b5132 RH |
695 | } |
696 | ||
697 | etree_type * | |
698 | exp_binop (code, lhs, rhs) | |
699 | int code; | |
700 | etree_type *lhs; | |
701 | etree_type *rhs; | |
702 | { | |
703 | etree_type value, *new; | |
704 | etree_value_type r; | |
705 | ||
706 | value.type.node_code = code; | |
707 | value.binary.lhs = lhs; | |
708 | value.binary.rhs = rhs; | |
709 | value.type.node_class = etree_binary; | |
8c95a62e KH |
710 | r = exp_fold_tree_no_dot (&value, |
711 | abs_output_section, | |
712 | lang_first_phase_enum); | |
252b5132 RH |
713 | if (r.valid_p) |
714 | { | |
8c95a62e | 715 | return exp_intop (r.value); |
252b5132 RH |
716 | } |
717 | new = (etree_type *) stat_alloc (sizeof (new->binary)); | |
8c95a62e | 718 | memcpy ((char *) new, (char *) &value, sizeof (new->binary)); |
252b5132 RH |
719 | return new; |
720 | } | |
721 | ||
722 | etree_type * | |
723 | exp_trinop (code, cond, lhs, rhs) | |
724 | int code; | |
725 | etree_type *cond; | |
726 | etree_type *lhs; | |
727 | etree_type *rhs; | |
728 | { | |
729 | etree_type value, *new; | |
730 | etree_value_type r; | |
731 | value.type.node_code = code; | |
732 | value.trinary.lhs = lhs; | |
733 | value.trinary.cond = cond; | |
734 | value.trinary.rhs = rhs; | |
735 | value.type.node_class = etree_trinary; | |
8c95a62e KH |
736 | r = exp_fold_tree_no_dot (&value, |
737 | (lang_output_section_statement_type *) NULL, | |
738 | lang_first_phase_enum); | |
739 | if (r.valid_p) | |
740 | { | |
741 | return exp_intop (r.value); | |
742 | } | |
252b5132 | 743 | new = (etree_type *) stat_alloc (sizeof (new->trinary)); |
8c95a62e | 744 | memcpy ((char *) new, (char *) &value, sizeof (new->trinary)); |
252b5132 RH |
745 | return new; |
746 | } | |
747 | ||
252b5132 RH |
748 | etree_type * |
749 | exp_unop (code, child) | |
750 | int code; | |
751 | etree_type *child; | |
752 | { | |
753 | etree_type value, *new; | |
754 | ||
755 | etree_value_type r; | |
756 | value.unary.type.node_code = code; | |
757 | value.unary.child = child; | |
758 | value.unary.type.node_class = etree_unary; | |
8c95a62e KH |
759 | r = exp_fold_tree_no_dot (&value, abs_output_section, |
760 | lang_first_phase_enum); | |
761 | if (r.valid_p) | |
762 | { | |
763 | return exp_intop (r.value); | |
764 | } | |
252b5132 | 765 | new = (etree_type *) stat_alloc (sizeof (new->unary)); |
8c95a62e | 766 | memcpy ((char *) new, (char *) &value, sizeof (new->unary)); |
252b5132 RH |
767 | return new; |
768 | } | |
769 | ||
252b5132 RH |
770 | etree_type * |
771 | exp_nameop (code, name) | |
772 | int code; | |
773 | CONST char *name; | |
774 | { | |
775 | etree_type value, *new; | |
776 | etree_value_type r; | |
777 | value.name.type.node_code = code; | |
778 | value.name.name = name; | |
779 | value.name.type.node_class = etree_name; | |
780 | ||
8c95a62e KH |
781 | r = exp_fold_tree_no_dot (&value, |
782 | (lang_output_section_statement_type *) NULL, | |
783 | lang_first_phase_enum); | |
784 | if (r.valid_p) | |
785 | { | |
786 | return exp_intop (r.value); | |
787 | } | |
252b5132 | 788 | new = (etree_type *) stat_alloc (sizeof (new->name)); |
8c95a62e | 789 | memcpy ((char *) new, (char *) &value, sizeof (new->name)); |
252b5132 RH |
790 | return new; |
791 | ||
792 | } | |
793 | ||
252b5132 RH |
794 | etree_type * |
795 | exp_assop (code, dst, src) | |
796 | int code; | |
797 | CONST char *dst; | |
798 | etree_type *src; | |
799 | { | |
800 | etree_type value, *new; | |
801 | ||
802 | value.assign.type.node_code = code; | |
803 | ||
252b5132 RH |
804 | value.assign.src = src; |
805 | value.assign.dst = dst; | |
806 | value.assign.type.node_class = etree_assign; | |
807 | ||
808 | #if 0 | |
8c95a62e KH |
809 | if (exp_fold_tree_no_dot (&value, &result)) |
810 | { | |
811 | return exp_intop (result); | |
812 | } | |
252b5132 | 813 | #endif |
8c95a62e KH |
814 | new = (etree_type *) stat_alloc (sizeof (new->assign)); |
815 | memcpy ((char *) new, (char *) &value, sizeof (new->assign)); | |
252b5132 RH |
816 | return new; |
817 | } | |
818 | ||
819 | /* Handle PROVIDE. */ | |
820 | ||
821 | etree_type * | |
822 | exp_provide (dst, src) | |
823 | const char *dst; | |
824 | etree_type *src; | |
825 | { | |
826 | etree_type *n; | |
827 | ||
828 | n = (etree_type *) stat_alloc (sizeof (n->assign)); | |
829 | n->assign.type.node_code = '='; | |
830 | n->assign.type.node_class = etree_provide; | |
831 | n->assign.src = src; | |
832 | n->assign.dst = dst; | |
833 | return n; | |
834 | } | |
835 | ||
836 | /* Handle ASSERT. */ | |
837 | ||
838 | etree_type * | |
839 | exp_assert (exp, message) | |
840 | etree_type *exp; | |
841 | const char *message; | |
842 | { | |
843 | etree_type *n; | |
844 | ||
845 | n = (etree_type *) stat_alloc (sizeof (n->assert_s)); | |
846 | n->assert_s.type.node_code = '!'; | |
847 | n->assert_s.type.node_class = etree_assert; | |
848 | n->assert_s.child = exp; | |
849 | n->assert_s.message = message; | |
850 | return n; | |
851 | } | |
852 | ||
4de2d33d | 853 | void |
252b5132 RH |
854 | exp_print_tree (tree) |
855 | etree_type *tree; | |
856 | { | |
8c95a62e KH |
857 | switch (tree->type.node_class) |
858 | { | |
859 | case etree_value: | |
860 | minfo ("0x%v", tree->value.value); | |
861 | return; | |
862 | case etree_rel: | |
863 | if (tree->rel.section->owner != NULL) | |
864 | minfo ("%B:", tree->rel.section->owner); | |
865 | minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value); | |
866 | return; | |
867 | case etree_assign: | |
252b5132 | 868 | #if 0 |
8c95a62e KH |
869 | if (tree->assign.dst->sdefs != (asymbol *) NULL) |
870 | { | |
871 | fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name, | |
872 | tree->assign.dst->sdefs->value); | |
873 | } | |
874 | else | |
875 | { | |
876 | fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name); | |
877 | } | |
252b5132 | 878 | #endif |
8c95a62e KH |
879 | fprintf (config.map_file, "%s", tree->assign.dst); |
880 | exp_print_token (tree->type.node_code); | |
881 | exp_print_tree (tree->assign.src); | |
882 | break; | |
883 | case etree_provide: | |
884 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); | |
885 | exp_print_tree (tree->assign.src); | |
886 | fprintf (config.map_file, ")"); | |
887 | break; | |
888 | case etree_binary: | |
889 | fprintf (config.map_file, "("); | |
890 | exp_print_tree (tree->binary.lhs); | |
891 | exp_print_token (tree->type.node_code); | |
892 | exp_print_tree (tree->binary.rhs); | |
893 | fprintf (config.map_file, ")"); | |
894 | break; | |
895 | case etree_trinary: | |
896 | exp_print_tree (tree->trinary.cond); | |
897 | fprintf (config.map_file, "?"); | |
898 | exp_print_tree (tree->trinary.lhs); | |
899 | fprintf (config.map_file, ":"); | |
900 | exp_print_tree (tree->trinary.rhs); | |
901 | break; | |
902 | case etree_unary: | |
903 | exp_print_token (tree->unary.type.node_code); | |
904 | if (tree->unary.child) | |
905 | { | |
906 | fprintf (config.map_file, "("); | |
907 | exp_print_tree (tree->unary.child); | |
908 | fprintf (config.map_file, ")"); | |
909 | } | |
910 | break; | |
911 | ||
912 | case etree_assert: | |
913 | fprintf (config.map_file, "ASSERT ("); | |
914 | exp_print_tree (tree->assert_s.child); | |
915 | fprintf (config.map_file, ", %s)", tree->assert_s.message); | |
916 | break; | |
917 | ||
918 | case etree_undef: | |
919 | fprintf (config.map_file, "????????"); | |
920 | break; | |
921 | case etree_name: | |
922 | if (tree->type.node_code == NAME) | |
923 | { | |
924 | fprintf (config.map_file, "%s", tree->name.name); | |
925 | } | |
926 | else | |
927 | { | |
928 | exp_print_token (tree->type.node_code); | |
929 | if (tree->name.name) | |
930 | fprintf (config.map_file, "(%s)", tree->name.name); | |
931 | } | |
932 | break; | |
933 | default: | |
934 | FAIL (); | |
935 | break; | |
252b5132 | 936 | } |
252b5132 RH |
937 | } |
938 | ||
939 | bfd_vma | |
940 | exp_get_vma (tree, def, name, allocation_done) | |
941 | etree_type *tree; | |
942 | bfd_vma def; | |
943 | char *name; | |
944 | lang_phase_type allocation_done; | |
945 | { | |
946 | etree_value_type r; | |
947 | ||
948 | if (tree != NULL) | |
949 | { | |
950 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
951 | if (! r.valid_p && name != NULL) | |
952 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
953 | return r.value; | |
954 | } | |
955 | else | |
956 | return def; | |
957 | } | |
958 | ||
4de2d33d | 959 | int |
8c95a62e | 960 | exp_get_value_int (tree, def, name, allocation_done) |
252b5132 RH |
961 | etree_type *tree; |
962 | int def; | |
963 | char *name; | |
964 | lang_phase_type allocation_done; | |
965 | { | |
8c95a62e | 966 | return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done); |
252b5132 RH |
967 | } |
968 | ||
252b5132 RH |
969 | bfd_vma |
970 | exp_get_abs_int (tree, def, name, allocation_done) | |
971 | etree_type *tree; | |
87f2a346 | 972 | int def ATTRIBUTE_UNUSED; |
252b5132 RH |
973 | char *name; |
974 | lang_phase_type allocation_done; | |
975 | { | |
976 | etree_value_type res; | |
977 | res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
978 | ||
979 | if (res.valid_p) | |
980 | { | |
981 | res.value += res.section->bfd_section->vma; | |
982 | } | |
8c95a62e KH |
983 | else |
984 | { | |
985 | einfo (_("%F%S non constant expression for %s\n"), name); | |
986 | } | |
252b5132 RH |
987 | return res.value; |
988 | } |