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