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