Commit | Line | Data |
---|---|---|
c611e285 | 1 | /* This module handles expression trees. |
31ddb156 | 2 | Copyright (C) 1991, 1993 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 | |
18 | along with GLD; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | /* | |
c611e285 | 22 | This module is in charge of working out the contents of expressions. |
2fa0b342 | 23 | |
c611e285 SC |
24 | It has to keep track of the relative/absness of a symbol etc. This is |
25 | done by keeping all values in a struct (an etree_value_type) which | |
26 | contains a value, a section to which it is relative and a valid bit. | |
27 | ||
28 | */ | |
2fa0b342 DHW |
29 | |
30 | ||
2fa0b342 | 31 | #include "bfd.h" |
c611e285 | 32 | #include "sysdep.h" |
31ddb156 | 33 | #include "bfdlink.h" |
2fa0b342 DHW |
34 | |
35 | #include "ld.h" | |
36 | #include "ldmain.h" | |
37 | #include "ldmisc.h" | |
38 | #include "ldexp.h" | |
c611e285 | 39 | #include "ldgram.h" |
2fa0b342 DHW |
40 | #include "ldlang.h" |
41 | ||
31ddb156 ILT |
42 | static void exp_print_token PARAMS ((token_code_type code)); |
43 | static void make_abs PARAMS ((etree_value_type *ptr)); | |
44 | static etree_value_type new_abs PARAMS ((bfd_vma value)); | |
45 | static void check PARAMS ((lang_output_section_statement_type *os, | |
46 | const char *name, const char *op)); | |
47 | static etree_value_type new_rel | |
48 | PARAMS ((bfd_vma value, lang_output_section_statement_type *section)); | |
49 | static etree_value_type new_rel_from_section | |
50 | PARAMS ((bfd_vma value, lang_output_section_statement_type *section)); | |
51 | static etree_value_type fold_binary | |
52 | PARAMS ((etree_type *tree, | |
53 | lang_output_section_statement_type *current_section, | |
54 | lang_phase_type allocation_done, | |
55 | bfd_vma dot, bfd_vma *dotp)); | |
56 | static etree_value_type fold_name | |
57 | PARAMS ((etree_type *tree, | |
58 | lang_output_section_statement_type *current_section, | |
59 | lang_phase_type allocation_done, | |
60 | bfd_vma dot)); | |
61 | static etree_value_type exp_fold_tree_no_dot | |
62 | PARAMS ((etree_type *tree, | |
63 | lang_output_section_statement_type *current_section, | |
64 | lang_phase_type allocation_done)); | |
65 | ||
2fa0b342 | 66 | static void |
8bf66be8 DM |
67 | exp_print_token (code) |
68 | token_code_type code; | |
2fa0b342 | 69 | { |
31ddb156 ILT |
70 | static CONST struct |
71 | { | |
72 | token_code_type code; | |
73 | char *name; | |
74 | } table[] = | |
c611e285 | 75 | { |
31ddb156 ILT |
76 | { INT, "int" }, |
77 | { REL, "relocateable" }, | |
78 | { NAME,"NAME" }, | |
79 | { PLUSEQ,"+=" }, | |
80 | { MINUSEQ,"-=" }, | |
81 | { MULTEQ,"*=" }, | |
82 | { DIVEQ,"/=" }, | |
83 | { LSHIFTEQ,"<<=" }, | |
84 | { RSHIFTEQ,">>=" }, | |
85 | { ANDEQ,"&=" }, | |
86 | { OREQ,"|=" }, | |
87 | { OROR,"||" }, | |
88 | { ANDAND,"&&" }, | |
89 | { EQ,"==" }, | |
90 | { NE,"!=" }, | |
91 | { LE,"<=" }, | |
92 | { GE,">=" }, | |
93 | { LSHIFT,"<<" }, | |
94 | { RSHIFT,">>=" }, | |
95 | { ALIGN_K,"ALIGN" }, | |
96 | { BLOCK,"BLOCK" }, | |
97 | { SECTIONS,"SECTIONS" }, | |
98 | { SIZEOF_HEADERS,"SIZEOF_HEADERS" }, | |
99 | { NEXT,"NEXT" }, | |
100 | { SIZEOF,"SIZEOF" }, | |
101 | { ADDR,"ADDR" }, | |
102 | { MEMORY,"MEMORY" }, | |
103 | { DEFINED,"DEFINED" }, | |
104 | { TARGET_K,"TARGET" }, | |
105 | { SEARCH_DIR,"SEARCH_DIR" }, | |
106 | { MAP,"MAP" }, | |
107 | { QUAD,"QUAD" }, | |
108 | { LONG,"LONG" }, | |
109 | { SHORT,"SHORT" }, | |
110 | { BYTE,"BYTE" }, | |
111 | { ENTRY,"ENTRY" }, | |
112 | { 0,(char *)NULL } | |
113 | }; | |
2fa0b342 | 114 | unsigned int idx; |
31ddb156 | 115 | |
2fa0b342 DHW |
116 | for (idx = 0; table[idx].name != (char*)NULL; idx++) { |
117 | if (table[idx].code == code) { | |
2e2bf962 | 118 | fprintf(config.map_file, "%s", table[idx].name); |
2fa0b342 DHW |
119 | return; |
120 | } | |
121 | } | |
122 | /* Not in table, just print it alone */ | |
2e2bf962 | 123 | fprintf(config.map_file, "%c",code); |
2fa0b342 DHW |
124 | } |
125 | ||
126 | static void | |
8bf66be8 DM |
127 | make_abs (ptr) |
128 | etree_value_type *ptr; | |
2fa0b342 | 129 | { |
2fa0b342 DHW |
130 | asection *s = ptr->section->bfd_section; |
131 | ptr->value += s->vma; | |
76971f0d | 132 | ptr->section = abs_output_section; |
2fa0b342 | 133 | } |
c611e285 | 134 | |
8bf66be8 DM |
135 | static etree_value_type |
136 | new_abs (value) | |
137 | bfd_vma value; | |
2fa0b342 DHW |
138 | { |
139 | etree_value_type new; | |
140 | new.valid = true; | |
76971f0d | 141 | new.section = abs_output_section; |
2fa0b342 DHW |
142 | new.value = value; |
143 | return new; | |
144 | } | |
145 | ||
3a399523 | 146 | static void |
8bf66be8 DM |
147 | check (os, name, op) |
148 | lang_output_section_statement_type *os; | |
149 | CONST char *name; | |
150 | CONST char *op; | |
2fa0b342 DHW |
151 | { |
152 | if (os == (lang_output_section_statement_type *)NULL) { | |
fcf276c4 | 153 | einfo("%F%P: %s uses undefined section %s\n", op, name); |
2fa0b342 DHW |
154 | } |
155 | if (os->processed == false) { | |
fcf276c4 | 156 | einfo("%F%P: %s forward reference of section %s\n",op, name); |
2fa0b342 DHW |
157 | } |
158 | } | |
159 | ||
c611e285 | 160 | etree_type * |
8bf66be8 DM |
161 | exp_intop (value) |
162 | bfd_vma value; | |
2fa0b342 | 163 | { |
bfbdc80f | 164 | etree_type *new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->value))); |
2fa0b342 DHW |
165 | new->type.node_code = INT; |
166 | new->value.value = value; | |
167 | new->type.node_class = etree_value; | |
168 | return new; | |
169 | ||
170 | } | |
171 | ||
31ddb156 ILT |
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 | } | |
2fa0b342 | 186 | |
8bf66be8 DM |
187 | static etree_value_type |
188 | new_rel (value, section) | |
189 | bfd_vma value; | |
190 | lang_output_section_statement_type *section; | |
2fa0b342 DHW |
191 | { |
192 | etree_value_type new; | |
193 | new.valid = true; | |
194 | new.value = value; | |
195 | new.section = section; | |
196 | return new; | |
197 | } | |
198 | ||
8bf66be8 DM |
199 | static etree_value_type |
200 | new_rel_from_section (value, section) | |
201 | bfd_vma value; | |
202 | lang_output_section_statement_type *section; | |
2fa0b342 DHW |
203 | { |
204 | etree_value_type new; | |
205 | new.valid = true; | |
206 | new.value = value; | |
207 | new.section = section; | |
76971f0d | 208 | |
2fa0b342 | 209 | new.value -= section->bfd_section->vma; |
76971f0d | 210 | |
2fa0b342 DHW |
211 | return new; |
212 | } | |
213 | ||
214 | static etree_value_type | |
8bf66be8 DM |
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; | |
2fa0b342 DHW |
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) { | |
227 | etree_value_type other; | |
228 | other = exp_fold_tree(tree->binary.rhs, | |
229 | current_section, | |
230 | allocation_done, dot,dotp) ; | |
231 | if (other.valid) { | |
232 | /* If values are from different sections, or this is an */ | |
233 | /* absolute expression, make both source args absolute */ | |
234 | if (result.section != other.section || | |
76971f0d SC |
235 | current_section == abs_output_section) |
236 | { | |
2fa0b342 DHW |
237 | make_abs(&result); |
238 | make_abs(&other); | |
239 | } | |
240 | ||
241 | switch (tree->type.node_code) | |
242 | { | |
243 | case '%': | |
244 | /* Mod, both absolule*/ | |
245 | ||
246 | if (other.value == 0) { | |
fcf276c4 | 247 | einfo("%F%S %% by zero\n"); |
2fa0b342 | 248 | } |
76971f0d | 249 | result.value = (int)result.value % (int)other.value; |
2fa0b342 DHW |
250 | break; |
251 | case '/': | |
252 | if (other.value == 0) { | |
c611e285 | 253 | einfo("%F%S / by zero\n"); |
2fa0b342 | 254 | } |
76971f0d | 255 | result.value = (int)result.value / (int) other.value; |
2fa0b342 DHW |
256 | break; |
257 | #define BOP(x,y) case x : result.value = result.value y other.value;break; | |
258 | BOP('+',+); | |
259 | BOP('*',*); | |
260 | BOP('-',-); | |
261 | BOP(LSHIFT,<<); | |
262 | BOP(RSHIFT,>>); | |
263 | BOP(EQ,==); | |
264 | BOP(NE,!=); | |
265 | BOP('<',<); | |
266 | BOP('>',>); | |
267 | BOP(LE,<=); | |
268 | BOP(GE,>=); | |
269 | BOP('&',&); | |
270 | BOP('^',^); | |
271 | BOP('|',|); | |
272 | BOP(ANDAND,&&); | |
273 | BOP(OROR,||); | |
274 | default: | |
275 | FAIL(); | |
276 | } | |
277 | } | |
070aa819 SC |
278 | else { |
279 | result.valid = false; | |
280 | } | |
2fa0b342 DHW |
281 | } |
282 | return result; | |
283 | } | |
c611e285 | 284 | etree_value_type |
8bf66be8 | 285 | invalid () |
2fa0b342 DHW |
286 | { |
287 | etree_value_type new; | |
288 | new.valid = false; | |
289 | return new; | |
290 | } | |
291 | ||
31ddb156 | 292 | static etree_value_type |
8bf66be8 DM |
293 | fold_name (tree, current_section, allocation_done, dot) |
294 | etree_type *tree; | |
295 | lang_output_section_statement_type *current_section; | |
296 | lang_phase_type allocation_done; | |
297 | bfd_vma dot; | |
2fa0b342 DHW |
298 | { |
299 | etree_value_type result; | |
300 | switch (tree->type.node_code) | |
ac004870 | 301 | { |
65c552e3 SC |
302 | case SIZEOF_HEADERS: |
303 | if (allocation_done != lang_first_phase_enum) | |
31ddb156 ILT |
304 | { |
305 | result = new_abs ((bfd_vma) | |
306 | bfd_sizeof_headers (output_bfd, | |
307 | link_info.relocateable)); | |
308 | } | |
309 | else | |
310 | { | |
311 | result.valid = false; | |
312 | } | |
65c552e3 | 313 | break; |
ac004870 | 314 | case DEFINED: |
31ddb156 ILT |
315 | { |
316 | struct bfd_link_hash_entry *h; | |
317 | ||
318 | h = bfd_link_hash_lookup (link_info.hash, tree->name.name, | |
319 | false, false, true); | |
320 | result.value = (h != (struct bfd_link_hash_entry *) NULL | |
321 | && (h->type == bfd_link_hash_defined | |
322 | || h->type == bfd_link_hash_common)); | |
323 | result.section = 0; | |
324 | result.valid = true; | |
325 | } | |
ac004870 SC |
326 | break; |
327 | case NAME: | |
328 | result.valid = false; | |
31ddb156 ILT |
329 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) |
330 | { | |
331 | if (allocation_done != lang_first_phase_enum) | |
332 | result = new_rel_from_section(dot, current_section); | |
333 | else | |
334 | result = invalid(); | |
ac004870 | 335 | } |
31ddb156 ILT |
336 | else if (allocation_done == lang_final_phase_enum) |
337 | { | |
338 | struct bfd_link_hash_entry *h; | |
339 | ||
340 | h = bfd_link_hash_lookup (link_info.hash, tree->name.name, | |
341 | false, false, true); | |
342 | if (h != (struct bfd_link_hash_entry *) NULL | |
343 | && h->type == bfd_link_hash_defined) | |
344 | { | |
345 | lang_output_section_statement_type *os; | |
346 | ||
347 | os = (lang_output_section_statement_lookup | |
348 | (h->u.def.section->output_section->name)); | |
349 | ||
350 | /* FIXME: Is this correct if this section is being | |
351 | linked with -R? */ | |
352 | result = new_rel ((h->u.def.value | |
353 | + h->u.def.section->output_offset), | |
354 | os); | |
2fa0b342 | 355 | } |
31ddb156 | 356 | if (result.valid == false) |
fcf276c4 | 357 | einfo("%F%S: undefined symbol `%s' referenced in expression\n", |
31ddb156 | 358 | tree->name.name); |
ac004870 | 359 | } |
ac004870 | 360 | break; |
2fa0b342 | 361 | |
ac004870 | 362 | case ADDR: |
2fa0b342 | 363 | |
ac004870 SC |
364 | if (allocation_done != lang_first_phase_enum) { |
365 | lang_output_section_statement_type *os = | |
366 | lang_output_section_find(tree->name.name); | |
367 | check(os,tree->name.name,"ADDR"); | |
368 | result = new_rel((bfd_vma)0, os); | |
369 | } | |
370 | else { | |
371 | result = invalid(); | |
372 | } | |
373 | break; | |
374 | case SIZEOF: | |
375 | if(allocation_done != lang_first_phase_enum) { | |
376 | lang_output_section_statement_type *os = | |
377 | lang_output_section_find(tree->name.name); | |
378 | check(os,tree->name.name,"SIZEOF"); | |
c611e285 | 379 | result = new_abs((bfd_vma)(os->bfd_section->_raw_size)); |
ac004870 SC |
380 | } |
381 | else { | |
382 | result = invalid(); | |
383 | } | |
384 | break; | |
2fa0b342 | 385 | |
ac004870 SC |
386 | default: |
387 | FAIL(); | |
388 | break; | |
389 | } | |
2fa0b342 DHW |
390 | |
391 | return result; | |
392 | } | |
c611e285 | 393 | etree_value_type |
8bf66be8 DM |
394 | exp_fold_tree (tree, current_section, allocation_done, dot, dotp) |
395 | etree_type *tree; | |
396 | lang_output_section_statement_type *current_section; | |
397 | lang_phase_type allocation_done; | |
398 | bfd_vma dot; | |
399 | bfd_vma *dotp; | |
2fa0b342 DHW |
400 | { |
401 | etree_value_type result; | |
402 | ||
403 | if (tree == (etree_type *)NULL) { | |
8bf66be8 DM |
404 | result.valid = false; |
405 | } | |
2fa0b342 | 406 | else { |
8bf66be8 DM |
407 | switch (tree->type.node_class) |
408 | { | |
409 | case etree_value: | |
410 | result = new_rel(tree->value.value, current_section); | |
411 | break; | |
31ddb156 ILT |
412 | case etree_rel: |
413 | if (allocation_done != lang_final_phase_enum) | |
414 | result.valid = false; | |
415 | else | |
416 | result = new_rel ((tree->rel.value | |
417 | + tree->rel.section->output_section->vma | |
418 | + tree->rel.section->output_offset), | |
419 | current_section); | |
420 | break; | |
8bf66be8 DM |
421 | case etree_unary: |
422 | result = exp_fold_tree(tree->unary.child, | |
423 | current_section, | |
424 | allocation_done, dot, dotp); | |
425 | if (result.valid == true) | |
c611e285 | 426 | { |
8bf66be8 | 427 | switch(tree->type.node_code) |
d4c02e29 | 428 | { |
8bf66be8 DM |
429 | case ALIGN_K: |
430 | if (allocation_done != lang_first_phase_enum) { | |
431 | result = new_rel_from_section(ALIGN_N(dot, | |
432 | result.value) , | |
433 | current_section); | |
434 | ||
435 | } | |
436 | else { | |
437 | result.valid = false; | |
438 | } | |
439 | break; | |
440 | case ABSOLUTE: | |
441 | if (allocation_done != lang_first_phase_enum) | |
c611e285 | 442 | { |
8bf66be8 DM |
443 | if (current_section |
444 | == (lang_output_section_statement_type*)NULL) | |
445 | { | |
446 | /* Outside a section, so it's all ok */ | |
d4c02e29 | 447 | |
8bf66be8 | 448 | } |
c611e285 | 449 | else { |
8bf66be8 DM |
450 | /* Inside a section, subtract the base of the section, |
451 | so when it's added again (in an assignment), everything comes out fine | |
452 | */ | |
453 | result.section = abs_output_section; | |
454 | result.value -= current_section->bfd_section->vma; | |
455 | result.valid = true; | |
456 | } | |
457 | } | |
458 | else | |
459 | { | |
460 | result.valid = false; | |
461 | } | |
462 | ||
463 | break; | |
464 | case '~': | |
465 | make_abs(&result); | |
466 | result.value = ~result.value; | |
467 | break; | |
468 | case '!': | |
469 | make_abs(&result); | |
470 | result.value = !result.value; | |
471 | break; | |
472 | case '-': | |
473 | make_abs(&result); | |
474 | result.value = -result.value; | |
475 | break; | |
476 | case NEXT: | |
477 | if (allocation_done ==lang_allocating_phase_enum) { | |
c611e285 | 478 | make_abs(&result); |
8bf66be8 | 479 | result.value = ALIGN_N(dot, result.value); |
c611e285 | 480 | } |
8bf66be8 DM |
481 | else { |
482 | /* Return next place aligned to value */ | |
483 | result.valid = false; | |
484 | } | |
485 | break; | |
486 | default: | |
487 | FAIL(); | |
c611e285 | 488 | } |
8bf66be8 | 489 | } |
2fa0b342 | 490 | |
8bf66be8 DM |
491 | break; |
492 | case etree_trinary: | |
2fa0b342 | 493 | |
8bf66be8 DM |
494 | result = exp_fold_tree(tree->trinary.cond, |
495 | current_section, | |
496 | allocation_done, dot, dotp); | |
497 | if (result.valid) { | |
498 | result = exp_fold_tree(result.value ? | |
499 | tree->trinary.lhs:tree->trinary.rhs, | |
c611e285 SC |
500 | current_section, |
501 | allocation_done, dot, dotp); | |
8bf66be8 | 502 | } |
d4c02e29 | 503 | |
8bf66be8 DM |
504 | break; |
505 | case etree_binary: | |
506 | result = fold_binary(tree, current_section, allocation_done, | |
507 | dot, dotp); | |
508 | break; | |
509 | case etree_assign: | |
31ddb156 | 510 | case etree_provide: |
8bf66be8 DM |
511 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) { |
512 | /* Assignment to dot can only be done during allocation */ | |
31ddb156 ILT |
513 | if (tree->type.node_class == etree_provide) |
514 | einfo ("%F%S can not PROVIDE assignment to location counter\n"); | |
8bf66be8 DM |
515 | if (allocation_done == lang_allocating_phase_enum) { |
516 | result = exp_fold_tree(tree->assign.src, | |
517 | current_section, | |
518 | lang_allocating_phase_enum, dot, dotp); | |
519 | if (result.valid == false) { | |
520 | einfo("%F%S invalid assignment to location counter\n"); | |
521 | } | |
522 | else { | |
523 | if (current_section == | |
524 | (lang_output_section_statement_type *)NULL) { | |
525 | einfo("%F%S assignment to location counter invalid outside of SECTION\n"); | |
526 | } | |
527 | else { | |
528 | bfd_vma nextdot =result.value + | |
529 | current_section->bfd_section->vma; | |
530 | if (nextdot < dot) { | |
531 | einfo("%F%S cannot move location counter backwards (from %V to %V)\n", dot, nextdot); | |
532 | } | |
533 | else { | |
534 | *dotp = nextdot; | |
2fa0b342 | 535 | } |
8bf66be8 | 536 | } |
2fa0b342 | 537 | } |
8bf66be8 DM |
538 | } |
539 | } | |
31ddb156 | 540 | else |
8bf66be8 | 541 | { |
31ddb156 ILT |
542 | result = exp_fold_tree (tree->assign.src, |
543 | current_section, allocation_done, | |
544 | dot, dotp); | |
545 | if (result.valid) | |
546 | { | |
547 | struct bfd_link_hash_entry *h; | |
548 | ||
549 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, | |
550 | (tree->type.node_class == etree_assign | |
551 | ? true : false), | |
552 | false, false); | |
553 | if (h == (struct bfd_link_hash_entry *) NULL) | |
554 | { | |
555 | if (tree->type.node_class == etree_assign) | |
556 | einfo ("%P%F:%s: hash creation failed\n", | |
557 | tree->assign.dst); | |
558 | } | |
559 | else if (tree->type.node_class == etree_provide | |
560 | && h->type != bfd_link_hash_undefined | |
561 | && h->type != bfd_link_hash_common) | |
562 | { | |
563 | /* Do nothing. The symbol was defined by some | |
564 | object. */ | |
565 | } | |
566 | else | |
567 | { | |
568 | /* FIXME: Should we worry if the symbol is already | |
569 | defined? */ | |
570 | h->type = bfd_link_hash_defined; | |
571 | h->u.def.value = result.value; | |
572 | h->u.def.section = result.section->bfd_section; | |
573 | } | |
574 | } | |
575 | } | |
8bf66be8 DM |
576 | break; |
577 | case etree_name: | |
578 | result = fold_name(tree, current_section, allocation_done, dot); | |
579 | break; | |
580 | default: | |
fcf276c4 | 581 | einfo("%F%S need more of these %d\n",tree->type.node_class ); |
2fa0b342 | 582 | |
c611e285 | 583 | } |
8bf66be8 | 584 | } |
2fa0b342 DHW |
585 | |
586 | return result; | |
587 | } | |
588 | ||
589 | ||
31ddb156 | 590 | static etree_value_type |
8bf66be8 DM |
591 | exp_fold_tree_no_dot (tree, current_section, allocation_done) |
592 | etree_type *tree; | |
593 | lang_output_section_statement_type *current_section; | |
594 | lang_phase_type allocation_done; | |
2fa0b342 DHW |
595 | { |
596 | return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma) | |
597 | 0, (bfd_vma *)NULL); | |
598 | } | |
599 | ||
600 | etree_type * | |
8bf66be8 DM |
601 | exp_binop (code, lhs, rhs) |
602 | int code; | |
603 | etree_type *lhs; | |
604 | etree_type *rhs; | |
2fa0b342 DHW |
605 | { |
606 | etree_type value, *new; | |
607 | etree_value_type r; | |
608 | ||
609 | value.type.node_code = code; | |
610 | value.binary.lhs = lhs; | |
611 | value.binary.rhs = rhs; | |
612 | value.type.node_class = etree_binary; | |
76971f0d SC |
613 | r = exp_fold_tree_no_dot(&value, |
614 | abs_output_section, | |
2fa0b342 DHW |
615 | lang_first_phase_enum ); |
616 | if (r.valid) | |
617 | { | |
618 | return exp_intop(r.value); | |
619 | } | |
bfbdc80f | 620 | new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->binary))); |
2fa0b342 DHW |
621 | memcpy((char *)new, (char *)&value, sizeof(new->binary)); |
622 | return new; | |
623 | } | |
624 | ||
625 | etree_type * | |
8bf66be8 DM |
626 | exp_trinop (code, cond, lhs, rhs) |
627 | int code; | |
628 | etree_type *cond; | |
629 | etree_type *lhs; | |
630 | etree_type *rhs; | |
2fa0b342 DHW |
631 | { |
632 | etree_type value, *new; | |
633 | etree_value_type r; | |
634 | value.type.node_code = code; | |
635 | value.trinary.lhs = lhs; | |
636 | value.trinary.cond = cond; | |
637 | value.trinary.rhs = rhs; | |
638 | value.type.node_class = etree_trinary; | |
639 | r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type | |
640 | *)NULL,lang_first_phase_enum); | |
641 | if (r.valid) { | |
642 | return exp_intop(r.value); | |
643 | } | |
bfbdc80f | 644 | new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->trinary))); |
2fa0b342 DHW |
645 | memcpy((char *)new,(char *) &value, sizeof(new->trinary)); |
646 | return new; | |
647 | } | |
648 | ||
649 | ||
650 | etree_type * | |
8bf66be8 DM |
651 | exp_unop (code, child) |
652 | int code; | |
653 | etree_type *child; | |
2fa0b342 DHW |
654 | { |
655 | etree_type value, *new; | |
656 | ||
657 | etree_value_type r; | |
658 | value.unary.type.node_code = code; | |
659 | value.unary.child = child; | |
660 | value.unary.type.node_class = etree_unary; | |
8bf66be8 | 661 | r = exp_fold_tree_no_dot(&value,abs_output_section, |
c611e285 SC |
662 | lang_first_phase_enum); |
663 | if (r.valid) { | |
2fa0b342 DHW |
664 | return exp_intop(r.value); |
665 | } | |
bfbdc80f | 666 | new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->unary))); |
2fa0b342 DHW |
667 | memcpy((char *)new, (char *)&value, sizeof(new->unary)); |
668 | return new; | |
669 | } | |
670 | ||
671 | ||
672 | etree_type * | |
8bf66be8 DM |
673 | exp_nameop (code, name) |
674 | int code; | |
675 | CONST char *name; | |
2fa0b342 | 676 | { |
2fa0b342 | 677 | etree_type value, *new; |
2fa0b342 DHW |
678 | etree_value_type r; |
679 | value.name.type.node_code = code; | |
680 | value.name.name = name; | |
681 | value.name.type.node_class = etree_name; | |
682 | ||
683 | ||
76971f0d SC |
684 | r = exp_fold_tree_no_dot(&value, |
685 | (lang_output_section_statement_type *)NULL, | |
686 | lang_first_phase_enum); | |
2fa0b342 DHW |
687 | if (r.valid) { |
688 | return exp_intop(r.value); | |
689 | } | |
bfbdc80f | 690 | new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->name))); |
2fa0b342 DHW |
691 | memcpy((char *)new, (char *)&value, sizeof(new->name)); |
692 | return new; | |
693 | ||
694 | } | |
695 | ||
696 | ||
697 | ||
698 | ||
699 | etree_type * | |
8bf66be8 DM |
700 | exp_assop (code, dst, src) |
701 | int code; | |
702 | CONST char *dst; | |
703 | etree_type *src; | |
2fa0b342 DHW |
704 | { |
705 | etree_type value, *new; | |
706 | ||
707 | value.assign.type.node_code = code; | |
708 | ||
709 | ||
710 | value.assign.src = src; | |
711 | value.assign.dst = dst; | |
712 | value.assign.type.node_class = etree_assign; | |
713 | ||
714 | #if 0 | |
715 | if (exp_fold_tree_no_dot(&value, &result)) { | |
716 | return exp_intop(result); | |
717 | } | |
718 | #endif | |
bfbdc80f | 719 | new = (etree_type*)stat_alloc((bfd_size_type)(sizeof(new->assign))); |
2fa0b342 DHW |
720 | memcpy((char *)new, (char *)&value, sizeof(new->assign)); |
721 | return new; | |
722 | } | |
723 | ||
31ddb156 ILT |
724 | /* Handle PROVIDE. */ |
725 | ||
726 | etree_type * | |
727 | exp_provide (dst, src) | |
728 | const char *dst; | |
729 | etree_type *src; | |
730 | { | |
731 | etree_type *n; | |
732 | ||
733 | n = (etree_type *) stat_alloc (sizeof (n->assign)); | |
734 | n->assign.type.node_code = '='; | |
735 | n->assign.type.node_class = etree_provide; | |
736 | n->assign.src = src; | |
737 | n->assign.dst = dst; | |
738 | return n; | |
739 | } | |
740 | ||
2fa0b342 | 741 | void |
8bf66be8 DM |
742 | exp_print_tree (tree) |
743 | etree_type *tree; | |
2fa0b342 DHW |
744 | { |
745 | switch (tree->type.node_class) { | |
746 | case etree_value: | |
c611e285 | 747 | print_address(tree->value.value); |
2fa0b342 | 748 | return; |
31ddb156 ILT |
749 | case etree_rel: |
750 | if (tree->rel.section->owner != NULL) | |
751 | fprintf (config.map_file, "%s:", | |
752 | bfd_get_filename (tree->rel.section->owner)); | |
753 | fprintf (config.map_file, "%s+", tree->rel.section->name); | |
754 | print_address (tree->rel.value); | |
755 | return; | |
2fa0b342 DHW |
756 | case etree_assign: |
757 | #if 0 | |
758 | if (tree->assign.dst->sdefs != (asymbol *)NULL){ | |
2e2bf962 | 759 | fprintf(config.map_file,"%s (%x) ",tree->assign.dst->name, |
2fa0b342 DHW |
760 | tree->assign.dst->sdefs->value); |
761 | } | |
762 | else { | |
2e2bf962 | 763 | fprintf(config.map_file,"%s (UNDEFINED)",tree->assign.dst->name); |
2fa0b342 DHW |
764 | } |
765 | #endif | |
2e2bf962 SC |
766 | fprintf(config.map_file,"%s ",tree->assign.dst); |
767 | exp_print_token(tree->type.node_code); | |
768 | exp_print_tree(tree->assign.src); | |
2fa0b342 | 769 | break; |
31ddb156 ILT |
770 | case etree_provide: |
771 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); | |
772 | exp_print_tree (tree->assign.src); | |
773 | fprintf (config.map_file, ")"); | |
774 | break; | |
2fa0b342 | 775 | case etree_binary: |
bfbdc80f | 776 | fprintf(config.map_file,"("); |
2e2bf962 SC |
777 | exp_print_tree(tree->binary.lhs); |
778 | exp_print_token(tree->type.node_code); | |
779 | exp_print_tree(tree->binary.rhs); | |
bfbdc80f | 780 | fprintf(config.map_file,")"); |
2fa0b342 DHW |
781 | break; |
782 | case etree_trinary: | |
2e2bf962 SC |
783 | exp_print_tree(tree->trinary.cond); |
784 | fprintf(config.map_file,"?"); | |
785 | exp_print_tree(tree->trinary.lhs); | |
786 | fprintf(config.map_file,":"); | |
787 | exp_print_tree(tree->trinary.rhs); | |
2fa0b342 DHW |
788 | break; |
789 | case etree_unary: | |
2e2bf962 | 790 | exp_print_token(tree->unary.type.node_code); |
bfbdc80f SC |
791 | if (tree->unary.child) |
792 | { | |
793 | ||
2e2bf962 SC |
794 | fprintf(config.map_file,"("); |
795 | exp_print_tree(tree->unary.child); | |
796 | fprintf(config.map_file,")"); | |
bfbdc80f SC |
797 | } |
798 | ||
2fa0b342 DHW |
799 | break; |
800 | case etree_undef: | |
2e2bf962 | 801 | fprintf(config.map_file,"????????"); |
2fa0b342 DHW |
802 | break; |
803 | case etree_name: | |
804 | if (tree->type.node_code == NAME) { | |
2e2bf962 | 805 | fprintf(config.map_file,"%s", tree->name.name); |
2fa0b342 DHW |
806 | } |
807 | else { | |
2e2bf962 | 808 | exp_print_token(tree->type.node_code); |
bfbdc80f | 809 | if (tree->name.name) |
2e2bf962 | 810 | fprintf(config.map_file,"(%s)", tree->name.name); |
2fa0b342 DHW |
811 | } |
812 | break; | |
813 | default: | |
814 | FAIL(); | |
815 | break; | |
816 | } | |
817 | } | |
818 | ||
819 | ||
820 | ||
821 | ||
822 | bfd_vma | |
8bf66be8 DM |
823 | exp_get_vma (tree, def, name, allocation_done) |
824 | etree_type *tree; | |
825 | bfd_vma def; | |
826 | char *name; | |
827 | lang_phase_type allocation_done; | |
2fa0b342 DHW |
828 | { |
829 | etree_value_type r; | |
830 | ||
831 | if (tree != (etree_type *)NULL) { | |
832 | r = exp_fold_tree_no_dot(tree, | |
bfbdc80f | 833 | abs_output_section, |
2fa0b342 DHW |
834 | allocation_done); |
835 | if (r.valid == false && name) { | |
fcf276c4 | 836 | einfo("%F%S nonconstant expression for %s\n",name); |
2fa0b342 DHW |
837 | } |
838 | return r.value; | |
839 | } | |
840 | else { | |
841 | return def; | |
842 | } | |
843 | } | |
844 | ||
845 | int | |
8bf66be8 DM |
846 | exp_get_value_int (tree,def,name, allocation_done) |
847 | etree_type *tree; | |
848 | int def; | |
849 | char *name; | |
850 | lang_phase_type allocation_done; | |
2fa0b342 DHW |
851 | { |
852 | return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done); | |
853 | } | |
65c552e3 | 854 |