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