Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* This module handles expression trees. |
a2b64bed | 2 | Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
1049f94e | 3 | 2001, 2002, 2003 |
87f2a346 | 4 | Free Software Foundation, Inc. |
8c95a62e | 5 | Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>. |
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" | |
df2a7313 | 39 | #include <ldgram.h> |
252b5132 | 40 | #include "ldlang.h" |
c7d701b0 | 41 | #include "libiberty.h" |
2c382fb6 | 42 | #include "safe-ctype.h" |
252b5132 | 43 | |
252b5132 | 44 | static etree_value_type exp_fold_tree_no_dot |
1579bae1 | 45 | (etree_type *, lang_output_section_statement_type *, lang_phase_type); |
252b5132 | 46 | |
2d20f7bf JJ |
47 | struct exp_data_seg exp_data_seg; |
48 | ||
7b17f854 | 49 | /* Print the string representation of the given token. Surround it |
b34976b6 | 50 | with spaces if INFIX_P is TRUE. */ |
7b17f854 | 51 | |
252b5132 | 52 | static void |
1579bae1 | 53 | exp_print_token (token_code_type code, int infix_p) |
252b5132 | 54 | { |
4da711b1 | 55 | static const struct |
c7d701b0 | 56 | { |
8c95a62e | 57 | token_code_type code; |
c7d701b0 NC |
58 | char * name; |
59 | } | |
60 | table[] = | |
61 | { | |
8c95a62e | 62 | { INT, "int" }, |
8c95a62e KH |
63 | { NAME, "NAME" }, |
64 | { PLUSEQ, "+=" }, | |
65 | { MINUSEQ, "-=" }, | |
66 | { MULTEQ, "*=" }, | |
67 | { DIVEQ, "/=" }, | |
68 | { LSHIFTEQ, "<<=" }, | |
69 | { RSHIFTEQ, ">>=" }, | |
70 | { ANDEQ, "&=" }, | |
71 | { OREQ, "|=" }, | |
72 | { OROR, "||" }, | |
73 | { ANDAND, "&&" }, | |
74 | { EQ, "==" }, | |
75 | { NE, "!=" }, | |
76 | { LE, "<=" }, | |
77 | { GE, ">=" }, | |
78 | { LSHIFT, "<<" }, | |
7cecdbff | 79 | { RSHIFT, ">>" }, |
8c95a62e KH |
80 | { ALIGN_K, "ALIGN" }, |
81 | { BLOCK, "BLOCK" }, | |
c7d701b0 NC |
82 | { QUAD, "QUAD" }, |
83 | { SQUAD, "SQUAD" }, | |
84 | { LONG, "LONG" }, | |
85 | { SHORT, "SHORT" }, | |
86 | { BYTE, "BYTE" }, | |
8c95a62e KH |
87 | { SECTIONS, "SECTIONS" }, |
88 | { SIZEOF_HEADERS, "SIZEOF_HEADERS" }, | |
8c95a62e KH |
89 | { MEMORY, "MEMORY" }, |
90 | { DEFINED, "DEFINED" }, | |
91 | { TARGET_K, "TARGET" }, | |
92 | { SEARCH_DIR, "SEARCH_DIR" }, | |
93 | { MAP, "MAP" }, | |
8c95a62e | 94 | { ENTRY, "ENTRY" }, |
c7d701b0 NC |
95 | { NEXT, "NEXT" }, |
96 | { SIZEOF, "SIZEOF" }, | |
97 | { ADDR, "ADDR" }, | |
98 | { LOADADDR, "LOADADDR" }, | |
99 | { MAX_K, "MAX_K" }, | |
1049f94e | 100 | { REL, "relocatable" }, |
2d20f7bf JJ |
101 | { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" }, |
102 | { DATA_SEGMENT_END, "DATA_SEGMENT_END" } | |
8c95a62e | 103 | }; |
252b5132 RH |
104 | unsigned int idx; |
105 | ||
7b17f854 RS |
106 | for (idx = 0; idx < ARRAY_SIZE (table); idx++) |
107 | if (table[idx].code == code) | |
108 | break; | |
c7d701b0 | 109 | |
7b17f854 RS |
110 | if (infix_p) |
111 | fputc (' ', config.map_file); | |
112 | ||
113 | if (idx < ARRAY_SIZE (table)) | |
114 | fputs (table[idx].name, config.map_file); | |
115 | else if (code < 127) | |
116 | fputc (code, config.map_file); | |
c7d701b0 | 117 | else |
7b17f854 RS |
118 | fprintf (config.map_file, "<code %d>", code); |
119 | ||
120 | if (infix_p) | |
121 | fputc (' ', config.map_file); | |
252b5132 RH |
122 | } |
123 | ||
4de2d33d | 124 | static void |
1579bae1 | 125 | make_abs (etree_value_type *ptr) |
252b5132 | 126 | { |
8c95a62e KH |
127 | asection *s = ptr->section->bfd_section; |
128 | ptr->value += s->vma; | |
129 | ptr->section = abs_output_section; | |
252b5132 RH |
130 | } |
131 | ||
132 | static etree_value_type | |
1579bae1 | 133 | new_abs (bfd_vma value) |
252b5132 RH |
134 | { |
135 | etree_value_type new; | |
b34976b6 | 136 | new.valid_p = TRUE; |
252b5132 RH |
137 | new.section = abs_output_section; |
138 | new.value = value; | |
139 | return new; | |
140 | } | |
141 | ||
4de2d33d | 142 | static void |
1579bae1 AM |
143 | check (lang_output_section_statement_type *os, |
144 | const char *name, | |
145 | const char *op) | |
252b5132 RH |
146 | { |
147 | if (os == NULL) | |
148 | einfo (_("%F%P: %s uses undefined section %s\n"), op, name); | |
149 | if (! os->processed) | |
150 | einfo (_("%F%P: %s forward reference of section %s\n"), op, name); | |
151 | } | |
152 | ||
153 | etree_type * | |
1579bae1 | 154 | exp_intop (bfd_vma value) |
252b5132 | 155 | { |
1579bae1 | 156 | etree_type *new = stat_alloc (sizeof (new->value)); |
252b5132 RH |
157 | new->type.node_code = INT; |
158 | new->value.value = value; | |
2c382fb6 | 159 | new->value.str = NULL; |
252b5132 RH |
160 | new->type.node_class = etree_value; |
161 | return new; | |
2c382fb6 | 162 | } |
252b5132 | 163 | |
2c382fb6 | 164 | etree_type * |
1579bae1 | 165 | exp_bigintop (bfd_vma value, char *str) |
2c382fb6 | 166 | { |
1579bae1 | 167 | etree_type *new = stat_alloc (sizeof (new->value)); |
2c382fb6 AM |
168 | new->type.node_code = INT; |
169 | new->value.value = value; | |
170 | new->value.str = str; | |
171 | new->type.node_class = etree_value; | |
172 | return new; | |
252b5132 RH |
173 | } |
174 | ||
1049f94e | 175 | /* Build an expression representing an unnamed relocatable value. */ |
252b5132 RH |
176 | |
177 | etree_type * | |
1579bae1 | 178 | exp_relop (asection *section, bfd_vma value) |
252b5132 | 179 | { |
1579bae1 | 180 | etree_type *new = stat_alloc (sizeof (new->rel)); |
252b5132 RH |
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 | } | |
187 | ||
188 | static etree_value_type | |
1579bae1 AM |
189 | new_rel (bfd_vma value, |
190 | char *str, | |
191 | lang_output_section_statement_type *section) | |
252b5132 RH |
192 | { |
193 | etree_value_type new; | |
b34976b6 | 194 | new.valid_p = TRUE; |
252b5132 | 195 | new.value = value; |
2c382fb6 | 196 | new.str = str; |
252b5132 RH |
197 | new.section = section; |
198 | return new; | |
199 | } | |
200 | ||
201 | static etree_value_type | |
1579bae1 AM |
202 | new_rel_from_section (bfd_vma value, |
203 | lang_output_section_statement_type *section) | |
252b5132 RH |
204 | { |
205 | etree_value_type new; | |
b34976b6 | 206 | new.valid_p = TRUE; |
252b5132 | 207 | new.value = value; |
2c382fb6 | 208 | new.str = NULL; |
252b5132 RH |
209 | new.section = section; |
210 | ||
8c95a62e | 211 | new.value -= section->bfd_section->vma; |
252b5132 RH |
212 | |
213 | return new; | |
214 | } | |
215 | ||
0ae1cf52 | 216 | static etree_value_type |
1579bae1 AM |
217 | fold_unary (etree_type *tree, |
218 | lang_output_section_statement_type *current_section, | |
219 | lang_phase_type allocation_done, | |
220 | bfd_vma dot, | |
221 | bfd_vma *dotp) | |
0ae1cf52 AM |
222 | { |
223 | etree_value_type result; | |
224 | ||
225 | result = exp_fold_tree (tree->unary.child, | |
226 | current_section, | |
227 | allocation_done, dot, dotp); | |
228 | if (result.valid_p) | |
229 | { | |
230 | switch (tree->type.node_code) | |
231 | { | |
232 | case ALIGN_K: | |
233 | if (allocation_done != lang_first_phase_enum) | |
234 | result = new_rel_from_section (align_n (dot, result.value), | |
235 | current_section); | |
236 | else | |
b34976b6 | 237 | result.valid_p = FALSE; |
0ae1cf52 AM |
238 | break; |
239 | ||
240 | case ABSOLUTE: | |
241 | if (allocation_done != lang_first_phase_enum) | |
242 | { | |
243 | result.value += result.section->bfd_section->vma; | |
244 | result.section = abs_output_section; | |
245 | } | |
246 | else | |
b34976b6 | 247 | result.valid_p = FALSE; |
0ae1cf52 AM |
248 | break; |
249 | ||
250 | case '~': | |
251 | make_abs (&result); | |
252 | result.value = ~result.value; | |
253 | break; | |
254 | ||
255 | case '!': | |
256 | make_abs (&result); | |
257 | result.value = !result.value; | |
258 | break; | |
259 | ||
260 | case '-': | |
261 | make_abs (&result); | |
262 | result.value = -result.value; | |
263 | break; | |
264 | ||
265 | case NEXT: | |
266 | /* Return next place aligned to value. */ | |
267 | if (allocation_done == lang_allocating_phase_enum) | |
268 | { | |
269 | make_abs (&result); | |
270 | result.value = align_n (dot, result.value); | |
271 | } | |
272 | else | |
b34976b6 | 273 | result.valid_p = FALSE; |
0ae1cf52 AM |
274 | break; |
275 | ||
276 | case DATA_SEGMENT_END: | |
277 | if (allocation_done != lang_first_phase_enum | |
278 | && current_section == abs_output_section | |
279 | && (exp_data_seg.phase == exp_dataseg_align_seen | |
280 | || exp_data_seg.phase == exp_dataseg_adjust | |
281 | || allocation_done != lang_allocating_phase_enum)) | |
282 | { | |
283 | if (exp_data_seg.phase == exp_dataseg_align_seen) | |
284 | { | |
285 | exp_data_seg.phase = exp_dataseg_end_seen; | |
286 | exp_data_seg.end = result.value; | |
287 | } | |
288 | } | |
289 | else | |
b34976b6 | 290 | result.valid_p = FALSE; |
0ae1cf52 AM |
291 | break; |
292 | ||
293 | default: | |
294 | FAIL (); | |
295 | break; | |
296 | } | |
297 | } | |
298 | ||
299 | return result; | |
300 | } | |
301 | ||
4de2d33d | 302 | static etree_value_type |
1579bae1 AM |
303 | fold_binary (etree_type *tree, |
304 | lang_output_section_statement_type *current_section, | |
305 | lang_phase_type allocation_done, | |
306 | bfd_vma dot, | |
307 | bfd_vma *dotp) | |
252b5132 RH |
308 | { |
309 | etree_value_type result; | |
310 | ||
311 | result = exp_fold_tree (tree->binary.lhs, current_section, | |
312 | allocation_done, dot, dotp); | |
313 | if (result.valid_p) | |
314 | { | |
315 | etree_value_type other; | |
316 | ||
317 | other = exp_fold_tree (tree->binary.rhs, | |
318 | current_section, | |
8c95a62e | 319 | allocation_done, dot, dotp); |
252b5132 RH |
320 | if (other.valid_p) |
321 | { | |
322 | /* If the values are from different sections, or this is an | |
323 | absolute expression, make both the source arguments | |
324 | absolute. However, adding or subtracting an absolute | |
325 | value from a relative value is meaningful, and is an | |
326 | exception. */ | |
327 | if (current_section != abs_output_section | |
328 | && (other.section == abs_output_section | |
329 | || (result.section == abs_output_section | |
330 | && tree->type.node_code == '+')) | |
331 | && (tree->type.node_code == '+' | |
332 | || tree->type.node_code == '-')) | |
333 | { | |
252b5132 RH |
334 | if (other.section != abs_output_section) |
335 | { | |
0ae1cf52 AM |
336 | /* Keep the section of the other term. */ |
337 | if (tree->type.node_code == '+') | |
338 | other.value = result.value + other.value; | |
339 | else | |
340 | other.value = result.value - other.value; | |
341 | return other; | |
252b5132 RH |
342 | } |
343 | } | |
344 | else if (result.section != other.section | |
345 | || current_section == abs_output_section) | |
346 | { | |
8c95a62e KH |
347 | make_abs (&result); |
348 | make_abs (&other); | |
252b5132 RH |
349 | } |
350 | ||
4de2d33d | 351 | switch (tree->type.node_code) |
252b5132 RH |
352 | { |
353 | case '%': | |
354 | if (other.value == 0) | |
355 | einfo (_("%F%S %% by zero\n")); | |
356 | result.value = ((bfd_signed_vma) result.value | |
357 | % (bfd_signed_vma) other.value); | |
358 | break; | |
359 | ||
360 | case '/': | |
361 | if (other.value == 0) | |
362 | einfo (_("%F%S / by zero\n")); | |
363 | result.value = ((bfd_signed_vma) result.value | |
364 | / (bfd_signed_vma) other.value); | |
365 | break; | |
366 | ||
367 | #define BOP(x,y) case x : result.value = result.value y other.value; break; | |
8c95a62e KH |
368 | BOP ('+', +); |
369 | BOP ('*', *); | |
370 | BOP ('-', -); | |
371 | BOP (LSHIFT, <<); | |
372 | BOP (RSHIFT, >>); | |
373 | BOP (EQ, ==); | |
374 | BOP (NE, !=); | |
375 | BOP ('<', <); | |
376 | BOP ('>', >); | |
377 | BOP (LE, <=); | |
378 | BOP (GE, >=); | |
379 | BOP ('&', &); | |
380 | BOP ('^', ^); | |
381 | BOP ('|', |); | |
382 | BOP (ANDAND, &&); | |
383 | BOP (OROR, ||); | |
252b5132 RH |
384 | |
385 | case MAX_K: | |
386 | if (result.value < other.value) | |
387 | result = other; | |
388 | break; | |
389 | ||
390 | case MIN_K: | |
391 | if (result.value > other.value) | |
392 | result = other; | |
393 | break; | |
394 | ||
2d20f7bf JJ |
395 | case DATA_SEGMENT_ALIGN: |
396 | if (allocation_done != lang_first_phase_enum | |
397 | && current_section == abs_output_section | |
398 | && (exp_data_seg.phase == exp_dataseg_none | |
399 | || exp_data_seg.phase == exp_dataseg_adjust | |
400 | || allocation_done != lang_allocating_phase_enum)) | |
401 | { | |
402 | bfd_vma maxpage = result.value; | |
403 | ||
c553bb91 | 404 | result.value = align_n (dot, maxpage); |
2d20f7bf JJ |
405 | if (exp_data_seg.phase != exp_dataseg_adjust) |
406 | { | |
407 | result.value += dot & (maxpage - 1); | |
408 | if (allocation_done == lang_allocating_phase_enum) | |
409 | { | |
410 | exp_data_seg.phase = exp_dataseg_align_seen; | |
411 | exp_data_seg.base = result.value; | |
412 | exp_data_seg.pagesize = other.value; | |
413 | } | |
414 | } | |
415 | else if (other.value < maxpage) | |
50e60fb5 JJ |
416 | result.value += (dot + other.value - 1) |
417 | & (maxpage - other.value); | |
2d20f7bf JJ |
418 | } |
419 | else | |
b34976b6 | 420 | result.valid_p = FALSE; |
2d20f7bf JJ |
421 | break; |
422 | ||
252b5132 | 423 | default: |
8c95a62e | 424 | FAIL (); |
252b5132 RH |
425 | } |
426 | } | |
427 | else | |
428 | { | |
b34976b6 | 429 | result.valid_p = FALSE; |
252b5132 RH |
430 | } |
431 | } | |
432 | ||
433 | return result; | |
434 | } | |
435 | ||
0ae1cf52 | 436 | static etree_value_type |
1579bae1 AM |
437 | fold_trinary (etree_type *tree, |
438 | lang_output_section_statement_type *current_section, | |
439 | lang_phase_type allocation_done, | |
440 | bfd_vma dot, | |
441 | bfd_vma *dotp) | |
0ae1cf52 AM |
442 | { |
443 | etree_value_type result; | |
444 | ||
445 | result = exp_fold_tree (tree->trinary.cond, current_section, | |
446 | allocation_done, dot, dotp); | |
447 | if (result.valid_p) | |
448 | result = exp_fold_tree ((result.value | |
449 | ? tree->trinary.lhs | |
450 | : tree->trinary.rhs), | |
451 | current_section, | |
452 | allocation_done, dot, dotp); | |
453 | ||
454 | return result; | |
455 | } | |
456 | ||
4de2d33d | 457 | etree_value_type |
1579bae1 | 458 | invalid (void) |
252b5132 RH |
459 | { |
460 | etree_value_type new; | |
b34976b6 | 461 | new.valid_p = FALSE; |
252b5132 RH |
462 | return new; |
463 | } | |
464 | ||
4de2d33d | 465 | static etree_value_type |
1579bae1 AM |
466 | fold_name (etree_type *tree, |
467 | lang_output_section_statement_type *current_section, | |
468 | lang_phase_type allocation_done, | |
469 | bfd_vma dot) | |
252b5132 RH |
470 | { |
471 | etree_value_type result; | |
c7d701b0 | 472 | |
4de2d33d | 473 | switch (tree->type.node_code) |
8c95a62e KH |
474 | { |
475 | case SIZEOF_HEADERS: | |
476 | if (allocation_done != lang_first_phase_enum) | |
477 | { | |
1579bae1 | 478 | result = new_abs (bfd_sizeof_headers (output_bfd, |
1049f94e | 479 | link_info.relocatable)); |
8c95a62e KH |
480 | } |
481 | else | |
482 | { | |
b34976b6 | 483 | result.valid_p = FALSE; |
8c95a62e KH |
484 | } |
485 | break; | |
486 | case DEFINED: | |
487 | if (allocation_done == lang_first_phase_enum) | |
420e579c HPN |
488 | { |
489 | lang_track_definedness (tree->name.name); | |
490 | result.valid_p = FALSE; | |
491 | } | |
8c95a62e KH |
492 | else |
493 | { | |
494 | struct bfd_link_hash_entry *h; | |
420e579c HPN |
495 | int def_iteration |
496 | = lang_symbol_definition_iteration (tree->name.name); | |
8c95a62e KH |
497 | |
498 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
499 | tree->name.name, | |
b34976b6 | 500 | FALSE, FALSE, TRUE); |
1579bae1 | 501 | result.value = (h != NULL |
8c95a62e KH |
502 | && (h->type == bfd_link_hash_defined |
503 | || h->type == bfd_link_hash_defweak | |
420e579c HPN |
504 | || h->type == bfd_link_hash_common) |
505 | && (def_iteration == lang_statement_iteration | |
506 | || def_iteration == -1)); | |
6a33aff4 | 507 | result.section = abs_output_section; |
b34976b6 | 508 | result.valid_p = TRUE; |
8c95a62e KH |
509 | } |
510 | break; | |
511 | case NAME: | |
b34976b6 | 512 | result.valid_p = FALSE; |
8c95a62e KH |
513 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) |
514 | { | |
515 | if (allocation_done != lang_first_phase_enum) | |
516 | result = new_rel_from_section (dot, current_section); | |
517 | else | |
518 | result = invalid (); | |
519 | } | |
520 | else if (allocation_done != lang_first_phase_enum) | |
521 | { | |
522 | struct bfd_link_hash_entry *h; | |
523 | ||
524 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
525 | tree->name.name, | |
b34976b6 | 526 | FALSE, FALSE, TRUE); |
8c95a62e KH |
527 | if (h != NULL |
528 | && (h->type == bfd_link_hash_defined | |
529 | || h->type == bfd_link_hash_defweak)) | |
530 | { | |
531 | if (bfd_is_abs_section (h->u.def.section)) | |
532 | result = new_abs (h->u.def.value); | |
533 | else if (allocation_done == lang_final_phase_enum | |
534 | || allocation_done == lang_allocating_phase_enum) | |
535 | { | |
536 | asection *output_section; | |
537 | ||
538 | output_section = h->u.def.section->output_section; | |
539 | if (output_section == NULL) | |
540 | einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"), | |
541 | tree->name.name); | |
542 | else | |
543 | { | |
544 | lang_output_section_statement_type *os; | |
545 | ||
546 | os = (lang_output_section_statement_lookup | |
547 | (bfd_get_section_name (output_bfd, | |
548 | output_section))); | |
549 | ||
550 | /* FIXME: Is this correct if this section is | |
551 | being linked with -R? */ | |
552 | result = new_rel ((h->u.def.value | |
553 | + h->u.def.section->output_offset), | |
2c382fb6 | 554 | NULL, |
8c95a62e KH |
555 | os); |
556 | } | |
557 | } | |
558 | } | |
559 | else if (allocation_done == lang_final_phase_enum) | |
560 | einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"), | |
561 | tree->name.name); | |
562 | } | |
563 | break; | |
564 | ||
565 | case ADDR: | |
566 | if (allocation_done != lang_first_phase_enum) | |
567 | { | |
568 | lang_output_section_statement_type *os; | |
569 | ||
570 | os = lang_output_section_find (tree->name.name); | |
571 | check (os, tree->name.name, "ADDR"); | |
2c382fb6 | 572 | result = new_rel (0, NULL, os); |
8c95a62e KH |
573 | } |
574 | else | |
575 | result = invalid (); | |
576 | break; | |
577 | ||
578 | case LOADADDR: | |
579 | if (allocation_done != lang_first_phase_enum) | |
580 | { | |
581 | lang_output_section_statement_type *os; | |
582 | ||
583 | os = lang_output_section_find (tree->name.name); | |
584 | check (os, tree->name.name, "LOADADDR"); | |
585 | if (os->load_base == NULL) | |
2c382fb6 | 586 | result = new_rel (0, NULL, os); |
8c95a62e KH |
587 | else |
588 | result = exp_fold_tree_no_dot (os->load_base, | |
589 | abs_output_section, | |
590 | allocation_done); | |
591 | } | |
592 | else | |
593 | result = invalid (); | |
594 | break; | |
595 | ||
596 | case SIZEOF: | |
597 | if (allocation_done != lang_first_phase_enum) | |
598 | { | |
599 | int opb = bfd_octets_per_byte (output_bfd); | |
600 | lang_output_section_statement_type *os; | |
601 | ||
602 | os = lang_output_section_find (tree->name.name); | |
603 | check (os, tree->name.name, "SIZEOF"); | |
604 | result = new_abs (os->bfd_section->_raw_size / opb); | |
605 | } | |
606 | else | |
607 | result = invalid (); | |
608 | break; | |
609 | ||
610 | default: | |
611 | FAIL (); | |
612 | break; | |
613 | } | |
252b5132 RH |
614 | |
615 | return result; | |
616 | } | |
8c95a62e | 617 | |
4de2d33d | 618 | etree_value_type |
1579bae1 AM |
619 | exp_fold_tree (etree_type *tree, |
620 | lang_output_section_statement_type *current_section, | |
621 | lang_phase_type allocation_done, | |
622 | bfd_vma dot, | |
623 | bfd_vma *dotp) | |
252b5132 RH |
624 | { |
625 | etree_value_type result; | |
626 | ||
627 | if (tree == NULL) | |
628 | { | |
b34976b6 | 629 | result.valid_p = FALSE; |
252b5132 RH |
630 | return result; |
631 | } | |
632 | ||
4de2d33d | 633 | switch (tree->type.node_class) |
252b5132 RH |
634 | { |
635 | case etree_value: | |
2c382fb6 | 636 | result = new_rel (tree->value.value, tree->value.str, current_section); |
252b5132 RH |
637 | break; |
638 | ||
639 | case etree_rel: | |
640 | if (allocation_done != lang_final_phase_enum) | |
b34976b6 | 641 | result.valid_p = FALSE; |
252b5132 RH |
642 | else |
643 | result = new_rel ((tree->rel.value | |
644 | + tree->rel.section->output_section->vma | |
645 | + tree->rel.section->output_offset), | |
2c382fb6 | 646 | NULL, |
252b5132 RH |
647 | current_section); |
648 | break; | |
649 | ||
650 | case etree_assert: | |
651 | result = exp_fold_tree (tree->assert_s.child, | |
8c95a62e KH |
652 | current_section, |
653 | allocation_done, dot, dotp); | |
252b5132 RH |
654 | if (result.valid_p) |
655 | { | |
656 | if (! result.value) | |
657 | einfo ("%F%P: %s\n", tree->assert_s.message); | |
658 | return result; | |
659 | } | |
660 | break; | |
661 | ||
662 | case etree_unary: | |
0ae1cf52 AM |
663 | result = fold_unary (tree, current_section, allocation_done, |
664 | dot, dotp); | |
252b5132 RH |
665 | break; |
666 | ||
667 | case etree_binary: | |
668 | result = fold_binary (tree, current_section, allocation_done, | |
669 | dot, dotp); | |
670 | break; | |
0ae1cf52 AM |
671 | |
672 | case etree_trinary: | |
673 | result = fold_trinary (tree, current_section, allocation_done, | |
674 | dot, dotp); | |
675 | break; | |
252b5132 RH |
676 | |
677 | case etree_assign: | |
678 | case etree_provide: | |
b46a87b1 | 679 | case etree_provided: |
252b5132 RH |
680 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) |
681 | { | |
c7d701b0 | 682 | /* Assignment to dot can only be done during allocation. */ |
b46a87b1 | 683 | if (tree->type.node_class != etree_assign) |
252b5132 RH |
684 | einfo (_("%F%S can not PROVIDE assignment to location counter\n")); |
685 | if (allocation_done == lang_allocating_phase_enum | |
686 | || (allocation_done == lang_final_phase_enum | |
687 | && current_section == abs_output_section)) | |
688 | { | |
689 | result = exp_fold_tree (tree->assign.src, | |
690 | current_section, | |
2d20f7bf | 691 | allocation_done, dot, |
252b5132 RH |
692 | dotp); |
693 | if (! result.valid_p) | |
694 | einfo (_("%F%S invalid assignment to location counter\n")); | |
695 | else | |
696 | { | |
697 | if (current_section == NULL) | |
698 | einfo (_("%F%S assignment to location counter invalid outside of SECTION\n")); | |
699 | else | |
700 | { | |
701 | bfd_vma nextdot; | |
702 | ||
703 | nextdot = (result.value | |
704 | + current_section->bfd_section->vma); | |
705 | if (nextdot < dot | |
706 | && current_section != abs_output_section) | |
c7d701b0 NC |
707 | einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"), |
708 | dot, nextdot); | |
252b5132 | 709 | else |
4de2d33d | 710 | *dotp = nextdot; |
252b5132 RH |
711 | } |
712 | } | |
713 | } | |
714 | } | |
715 | else | |
716 | { | |
717 | result = exp_fold_tree (tree->assign.src, | |
718 | current_section, allocation_done, | |
719 | dot, dotp); | |
720 | if (result.valid_p) | |
721 | { | |
b34976b6 | 722 | bfd_boolean create; |
252b5132 RH |
723 | struct bfd_link_hash_entry *h; |
724 | ||
725 | if (tree->type.node_class == etree_assign) | |
b34976b6 | 726 | create = TRUE; |
252b5132 | 727 | else |
b34976b6 | 728 | create = FALSE; |
252b5132 | 729 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, |
b34976b6 | 730 | create, FALSE, FALSE); |
1579bae1 | 731 | if (h == NULL) |
67010b46 NC |
732 | { |
733 | if (tree->type.node_class == etree_assign) | |
734 | einfo (_("%P%F:%s: hash creation failed\n"), | |
735 | tree->assign.dst); | |
736 | } | |
737 | else if (tree->type.node_class == etree_provide | |
738 | && h->type != bfd_link_hash_undefined | |
739 | && h->type != bfd_link_hash_common) | |
740 | { | |
741 | /* Do nothing. The symbol was defined by some | |
742 | object. */ | |
743 | } | |
252b5132 RH |
744 | else |
745 | { | |
746 | /* FIXME: Should we worry if the symbol is already | |
747 | defined? */ | |
420e579c | 748 | lang_update_definedness (tree->assign.dst, h); |
252b5132 RH |
749 | h->type = bfd_link_hash_defined; |
750 | h->u.def.value = result.value; | |
751 | h->u.def.section = result.section->bfd_section; | |
b46a87b1 L |
752 | if (tree->type.node_class == etree_provide) |
753 | tree->type.node_class = etree_provided; | |
252b5132 RH |
754 | } |
755 | } | |
756 | } | |
757 | break; | |
758 | ||
759 | case etree_name: | |
760 | result = fold_name (tree, current_section, allocation_done, dot); | |
761 | break; | |
762 | ||
763 | default: | |
764 | FAIL (); | |
765 | break; | |
766 | } | |
767 | ||
768 | return result; | |
769 | } | |
770 | ||
4de2d33d | 771 | static etree_value_type |
1579bae1 AM |
772 | exp_fold_tree_no_dot (etree_type *tree, |
773 | lang_output_section_statement_type *current_section, | |
774 | lang_phase_type allocation_done) | |
252b5132 | 775 | { |
1579bae1 | 776 | return exp_fold_tree (tree, current_section, allocation_done, 0, NULL); |
252b5132 RH |
777 | } |
778 | ||
779 | etree_type * | |
1579bae1 | 780 | exp_binop (int code, etree_type *lhs, etree_type *rhs) |
252b5132 RH |
781 | { |
782 | etree_type value, *new; | |
783 | etree_value_type r; | |
784 | ||
785 | value.type.node_code = code; | |
786 | value.binary.lhs = lhs; | |
787 | value.binary.rhs = rhs; | |
788 | value.type.node_class = etree_binary; | |
8c95a62e KH |
789 | r = exp_fold_tree_no_dot (&value, |
790 | abs_output_section, | |
791 | lang_first_phase_enum); | |
252b5132 RH |
792 | if (r.valid_p) |
793 | { | |
8c95a62e | 794 | return exp_intop (r.value); |
252b5132 | 795 | } |
1579bae1 AM |
796 | new = stat_alloc (sizeof (new->binary)); |
797 | memcpy (new, &value, sizeof (new->binary)); | |
252b5132 RH |
798 | return new; |
799 | } | |
800 | ||
801 | etree_type * | |
1579bae1 | 802 | exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs) |
252b5132 RH |
803 | { |
804 | etree_type value, *new; | |
805 | etree_value_type r; | |
806 | value.type.node_code = code; | |
807 | value.trinary.lhs = lhs; | |
808 | value.trinary.cond = cond; | |
809 | value.trinary.rhs = rhs; | |
810 | value.type.node_class = etree_trinary; | |
1579bae1 | 811 | r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum); |
8c95a62e | 812 | if (r.valid_p) |
c7d701b0 NC |
813 | return exp_intop (r.value); |
814 | ||
1579bae1 AM |
815 | new = stat_alloc (sizeof (new->trinary)); |
816 | memcpy (new, &value, sizeof (new->trinary)); | |
252b5132 RH |
817 | return new; |
818 | } | |
819 | ||
252b5132 | 820 | etree_type * |
1579bae1 | 821 | exp_unop (int code, etree_type *child) |
252b5132 RH |
822 | { |
823 | etree_type value, *new; | |
824 | ||
825 | etree_value_type r; | |
826 | value.unary.type.node_code = code; | |
827 | value.unary.child = child; | |
828 | value.unary.type.node_class = etree_unary; | |
8c95a62e KH |
829 | r = exp_fold_tree_no_dot (&value, abs_output_section, |
830 | lang_first_phase_enum); | |
831 | if (r.valid_p) | |
c7d701b0 NC |
832 | return exp_intop (r.value); |
833 | ||
1579bae1 AM |
834 | new = stat_alloc (sizeof (new->unary)); |
835 | memcpy (new, &value, sizeof (new->unary)); | |
252b5132 RH |
836 | return new; |
837 | } | |
838 | ||
252b5132 | 839 | etree_type * |
1579bae1 | 840 | exp_nameop (int code, const char *name) |
252b5132 RH |
841 | { |
842 | etree_type value, *new; | |
843 | etree_value_type r; | |
844 | value.name.type.node_code = code; | |
845 | value.name.name = name; | |
846 | value.name.type.node_class = etree_name; | |
847 | ||
1579bae1 | 848 | r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum); |
8c95a62e | 849 | if (r.valid_p) |
c7d701b0 NC |
850 | return exp_intop (r.value); |
851 | ||
1579bae1 AM |
852 | new = stat_alloc (sizeof (new->name)); |
853 | memcpy (new, &value, sizeof (new->name)); | |
252b5132 RH |
854 | return new; |
855 | ||
856 | } | |
857 | ||
252b5132 | 858 | etree_type * |
1579bae1 | 859 | exp_assop (int code, const char *dst, etree_type *src) |
252b5132 RH |
860 | { |
861 | etree_type value, *new; | |
862 | ||
863 | value.assign.type.node_code = code; | |
864 | ||
252b5132 RH |
865 | value.assign.src = src; |
866 | value.assign.dst = dst; | |
867 | value.assign.type.node_class = etree_assign; | |
868 | ||
869 | #if 0 | |
8c95a62e | 870 | if (exp_fold_tree_no_dot (&value, &result)) |
c7d701b0 | 871 | return exp_intop (result); |
252b5132 | 872 | #endif |
1579bae1 AM |
873 | new = stat_alloc (sizeof (new->assign)); |
874 | memcpy (new, &value, sizeof (new->assign)); | |
252b5132 RH |
875 | return new; |
876 | } | |
877 | ||
878 | /* Handle PROVIDE. */ | |
879 | ||
880 | etree_type * | |
1579bae1 | 881 | exp_provide (const char *dst, etree_type *src) |
252b5132 RH |
882 | { |
883 | etree_type *n; | |
884 | ||
1579bae1 | 885 | n = stat_alloc (sizeof (n->assign)); |
252b5132 RH |
886 | n->assign.type.node_code = '='; |
887 | n->assign.type.node_class = etree_provide; | |
888 | n->assign.src = src; | |
889 | n->assign.dst = dst; | |
890 | return n; | |
891 | } | |
892 | ||
893 | /* Handle ASSERT. */ | |
894 | ||
895 | etree_type * | |
1579bae1 | 896 | exp_assert (etree_type *exp, const char *message) |
252b5132 RH |
897 | { |
898 | etree_type *n; | |
899 | ||
1579bae1 | 900 | n = stat_alloc (sizeof (n->assert_s)); |
252b5132 RH |
901 | n->assert_s.type.node_code = '!'; |
902 | n->assert_s.type.node_class = etree_assert; | |
903 | n->assert_s.child = exp; | |
904 | n->assert_s.message = message; | |
905 | return n; | |
906 | } | |
907 | ||
4de2d33d | 908 | void |
1579bae1 | 909 | exp_print_tree (etree_type *tree) |
252b5132 | 910 | { |
c7d701b0 NC |
911 | if (config.map_file == NULL) |
912 | config.map_file = stderr; | |
b7a26f91 | 913 | |
c7d701b0 NC |
914 | if (tree == NULL) |
915 | { | |
916 | minfo ("NULL TREE\n"); | |
917 | return; | |
918 | } | |
b7a26f91 | 919 | |
8c95a62e KH |
920 | switch (tree->type.node_class) |
921 | { | |
922 | case etree_value: | |
923 | minfo ("0x%v", tree->value.value); | |
924 | return; | |
925 | case etree_rel: | |
926 | if (tree->rel.section->owner != NULL) | |
927 | minfo ("%B:", tree->rel.section->owner); | |
928 | minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value); | |
929 | return; | |
930 | case etree_assign: | |
252b5132 | 931 | #if 0 |
1579bae1 | 932 | if (tree->assign.dst->sdefs != NULL) |
c7d701b0 NC |
933 | fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name, |
934 | tree->assign.dst->sdefs->value); | |
8c95a62e | 935 | else |
c7d701b0 | 936 | fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name); |
252b5132 | 937 | #endif |
8c95a62e | 938 | fprintf (config.map_file, "%s", tree->assign.dst); |
b34976b6 | 939 | exp_print_token (tree->type.node_code, TRUE); |
8c95a62e KH |
940 | exp_print_tree (tree->assign.src); |
941 | break; | |
942 | case etree_provide: | |
b46a87b1 | 943 | case etree_provided: |
8c95a62e KH |
944 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); |
945 | exp_print_tree (tree->assign.src); | |
946 | fprintf (config.map_file, ")"); | |
947 | break; | |
948 | case etree_binary: | |
949 | fprintf (config.map_file, "("); | |
950 | exp_print_tree (tree->binary.lhs); | |
b34976b6 | 951 | exp_print_token (tree->type.node_code, TRUE); |
8c95a62e KH |
952 | exp_print_tree (tree->binary.rhs); |
953 | fprintf (config.map_file, ")"); | |
954 | break; | |
955 | case etree_trinary: | |
956 | exp_print_tree (tree->trinary.cond); | |
957 | fprintf (config.map_file, "?"); | |
958 | exp_print_tree (tree->trinary.lhs); | |
959 | fprintf (config.map_file, ":"); | |
960 | exp_print_tree (tree->trinary.rhs); | |
961 | break; | |
962 | case etree_unary: | |
b34976b6 | 963 | exp_print_token (tree->unary.type.node_code, FALSE); |
8c95a62e KH |
964 | if (tree->unary.child) |
965 | { | |
7b17f854 | 966 | fprintf (config.map_file, " ("); |
8c95a62e KH |
967 | exp_print_tree (tree->unary.child); |
968 | fprintf (config.map_file, ")"); | |
969 | } | |
970 | break; | |
971 | ||
972 | case etree_assert: | |
973 | fprintf (config.map_file, "ASSERT ("); | |
974 | exp_print_tree (tree->assert_s.child); | |
975 | fprintf (config.map_file, ", %s)", tree->assert_s.message); | |
976 | break; | |
977 | ||
978 | case etree_undef: | |
979 | fprintf (config.map_file, "????????"); | |
980 | break; | |
981 | case etree_name: | |
982 | if (tree->type.node_code == NAME) | |
983 | { | |
984 | fprintf (config.map_file, "%s", tree->name.name); | |
985 | } | |
986 | else | |
987 | { | |
b34976b6 | 988 | exp_print_token (tree->type.node_code, FALSE); |
8c95a62e | 989 | if (tree->name.name) |
7b17f854 | 990 | fprintf (config.map_file, " (%s)", tree->name.name); |
8c95a62e KH |
991 | } |
992 | break; | |
993 | default: | |
994 | FAIL (); | |
995 | break; | |
252b5132 | 996 | } |
252b5132 RH |
997 | } |
998 | ||
999 | bfd_vma | |
1579bae1 AM |
1000 | exp_get_vma (etree_type *tree, |
1001 | bfd_vma def, | |
1002 | char *name, | |
1003 | lang_phase_type allocation_done) | |
252b5132 RH |
1004 | { |
1005 | etree_value_type r; | |
1006 | ||
1007 | if (tree != NULL) | |
1008 | { | |
1009 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1010 | if (! r.valid_p && name != NULL) | |
1011 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
1012 | return r.value; | |
1013 | } | |
1014 | else | |
1015 | return def; | |
1016 | } | |
1017 | ||
4de2d33d | 1018 | int |
1579bae1 AM |
1019 | exp_get_value_int (etree_type *tree, |
1020 | int def, | |
1021 | char *name, | |
1022 | lang_phase_type allocation_done) | |
252b5132 | 1023 | { |
1579bae1 | 1024 | return exp_get_vma (tree, def, name, allocation_done); |
252b5132 RH |
1025 | } |
1026 | ||
2c382fb6 | 1027 | fill_type * |
1579bae1 AM |
1028 | exp_get_fill (etree_type *tree, |
1029 | fill_type *def, | |
1030 | char *name, | |
1031 | lang_phase_type allocation_done) | |
2c382fb6 AM |
1032 | { |
1033 | fill_type *fill; | |
1034 | etree_value_type r; | |
1035 | size_t len; | |
1036 | unsigned int val; | |
1037 | ||
1038 | if (tree == NULL) | |
1039 | return def; | |
1040 | ||
1041 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1042 | if (! r.valid_p && name != NULL) | |
1043 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
1044 | ||
1045 | if (r.str != NULL && (len = strlen (r.str)) != 0) | |
1046 | { | |
1047 | unsigned char *dst; | |
1048 | unsigned char *s; | |
1579bae1 | 1049 | fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1); |
2c382fb6 AM |
1050 | fill->size = (len + 1) / 2; |
1051 | dst = fill->data; | |
1052 | s = r.str; | |
1053 | val = 0; | |
1054 | do | |
1055 | { | |
1056 | unsigned int digit; | |
1057 | ||
1058 | digit = *s++ - '0'; | |
1059 | if (digit > 9) | |
1060 | digit = (digit - 'A' + '0' + 10) & 0xf; | |
1061 | val <<= 4; | |
1062 | val += digit; | |
1063 | --len; | |
1064 | if ((len & 1) == 0) | |
1065 | { | |
1066 | *dst++ = val; | |
1067 | val = 0; | |
1068 | } | |
1069 | } | |
1070 | while (len != 0); | |
1071 | } | |
1072 | else | |
1073 | { | |
1579bae1 | 1074 | fill = xmalloc (4 + sizeof (*fill) - 1); |
2c382fb6 AM |
1075 | val = r.value; |
1076 | fill->data[0] = (val >> 24) & 0xff; | |
1077 | fill->data[1] = (val >> 16) & 0xff; | |
1078 | fill->data[2] = (val >> 8) & 0xff; | |
1079 | fill->data[3] = (val >> 0) & 0xff; | |
1080 | fill->size = 4; | |
1081 | } | |
1082 | return fill; | |
1083 | } | |
1084 | ||
252b5132 | 1085 | bfd_vma |
1579bae1 AM |
1086 | exp_get_abs_int (etree_type *tree, |
1087 | int def ATTRIBUTE_UNUSED, | |
1088 | char *name, | |
1089 | lang_phase_type allocation_done) | |
252b5132 RH |
1090 | { |
1091 | etree_value_type res; | |
1092 | res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1093 | ||
1094 | if (res.valid_p) | |
c7d701b0 | 1095 | res.value += res.section->bfd_section->vma; |
8c95a62e | 1096 | else |
c7d701b0 NC |
1097 | einfo (_("%F%S non constant expression for %s\n"), name); |
1098 | ||
252b5132 RH |
1099 | return res.value; |
1100 | } | |
c553bb91 | 1101 | |
1579bae1 | 1102 | bfd_vma align_n (bfd_vma value, bfd_vma align) |
c553bb91 AM |
1103 | { |
1104 | if (align <= 1) | |
1105 | return value; | |
1106 | ||
1107 | value = (value + align - 1) / align; | |
1108 | return value * align; | |
1109 | } |