]>
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) | |
359 | result.value += dot & (maxpage - other.value); | |
360 | } | |
361 | else | |
362 | result.valid_p = false; | |
363 | break; | |
364 | ||
252b5132 | 365 | default: |
8c95a62e | 366 | FAIL (); |
252b5132 RH |
367 | } |
368 | } | |
369 | else | |
370 | { | |
371 | result.valid_p = false; | |
372 | } | |
373 | } | |
374 | ||
375 | return result; | |
376 | } | |
377 | ||
4de2d33d | 378 | etree_value_type |
252b5132 RH |
379 | invalid () |
380 | { | |
381 | etree_value_type new; | |
382 | new.valid_p = false; | |
383 | return new; | |
384 | } | |
385 | ||
4de2d33d | 386 | static etree_value_type |
252b5132 RH |
387 | fold_name (tree, current_section, allocation_done, dot) |
388 | etree_type *tree; | |
389 | lang_output_section_statement_type *current_section; | |
8c95a62e | 390 | lang_phase_type allocation_done; |
252b5132 RH |
391 | bfd_vma dot; |
392 | { | |
393 | etree_value_type result; | |
c7d701b0 | 394 | |
4de2d33d | 395 | switch (tree->type.node_code) |
8c95a62e KH |
396 | { |
397 | case SIZEOF_HEADERS: | |
398 | if (allocation_done != lang_first_phase_enum) | |
399 | { | |
400 | result = new_abs ((bfd_vma) | |
401 | bfd_sizeof_headers (output_bfd, | |
402 | link_info.relocateable)); | |
403 | } | |
404 | else | |
405 | { | |
252b5132 | 406 | result.valid_p = false; |
8c95a62e KH |
407 | } |
408 | break; | |
409 | case DEFINED: | |
410 | if (allocation_done == lang_first_phase_enum) | |
252b5132 | 411 | result.valid_p = false; |
8c95a62e KH |
412 | else |
413 | { | |
414 | struct bfd_link_hash_entry *h; | |
415 | ||
416 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
417 | tree->name.name, | |
418 | false, false, true); | |
419 | result.value = (h != (struct bfd_link_hash_entry *) NULL | |
420 | && (h->type == bfd_link_hash_defined | |
421 | || h->type == bfd_link_hash_defweak | |
422 | || h->type == bfd_link_hash_common)); | |
423 | result.section = 0; | |
424 | result.valid_p = true; | |
425 | } | |
426 | break; | |
427 | case NAME: | |
428 | result.valid_p = false; | |
429 | if (tree->name.name[0] == '.' && tree->name.name[1] == 0) | |
430 | { | |
431 | if (allocation_done != lang_first_phase_enum) | |
432 | result = new_rel_from_section (dot, current_section); | |
433 | else | |
434 | result = invalid (); | |
435 | } | |
436 | else if (allocation_done != lang_first_phase_enum) | |
437 | { | |
438 | struct bfd_link_hash_entry *h; | |
439 | ||
440 | h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info, | |
441 | tree->name.name, | |
442 | false, false, true); | |
443 | if (h != NULL | |
444 | && (h->type == bfd_link_hash_defined | |
445 | || h->type == bfd_link_hash_defweak)) | |
446 | { | |
447 | if (bfd_is_abs_section (h->u.def.section)) | |
448 | result = new_abs (h->u.def.value); | |
449 | else if (allocation_done == lang_final_phase_enum | |
450 | || allocation_done == lang_allocating_phase_enum) | |
451 | { | |
452 | asection *output_section; | |
453 | ||
454 | output_section = h->u.def.section->output_section; | |
455 | if (output_section == NULL) | |
456 | einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"), | |
457 | tree->name.name); | |
458 | else | |
459 | { | |
460 | lang_output_section_statement_type *os; | |
461 | ||
462 | os = (lang_output_section_statement_lookup | |
463 | (bfd_get_section_name (output_bfd, | |
464 | output_section))); | |
465 | ||
466 | /* FIXME: Is this correct if this section is | |
467 | being linked with -R? */ | |
468 | result = new_rel ((h->u.def.value | |
469 | + h->u.def.section->output_offset), | |
2c382fb6 | 470 | NULL, |
8c95a62e KH |
471 | os); |
472 | } | |
473 | } | |
474 | } | |
475 | else if (allocation_done == lang_final_phase_enum) | |
476 | einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"), | |
477 | tree->name.name); | |
478 | } | |
479 | break; | |
480 | ||
481 | case ADDR: | |
482 | if (allocation_done != lang_first_phase_enum) | |
483 | { | |
484 | lang_output_section_statement_type *os; | |
485 | ||
486 | os = lang_output_section_find (tree->name.name); | |
487 | check (os, tree->name.name, "ADDR"); | |
2c382fb6 | 488 | result = new_rel (0, NULL, os); |
8c95a62e KH |
489 | } |
490 | else | |
491 | result = invalid (); | |
492 | break; | |
493 | ||
494 | case LOADADDR: | |
495 | if (allocation_done != lang_first_phase_enum) | |
496 | { | |
497 | lang_output_section_statement_type *os; | |
498 | ||
499 | os = lang_output_section_find (tree->name.name); | |
500 | check (os, tree->name.name, "LOADADDR"); | |
501 | if (os->load_base == NULL) | |
2c382fb6 | 502 | result = new_rel (0, NULL, os); |
8c95a62e KH |
503 | else |
504 | result = exp_fold_tree_no_dot (os->load_base, | |
505 | abs_output_section, | |
506 | allocation_done); | |
507 | } | |
508 | else | |
509 | result = invalid (); | |
510 | break; | |
511 | ||
512 | case SIZEOF: | |
513 | if (allocation_done != lang_first_phase_enum) | |
514 | { | |
515 | int opb = bfd_octets_per_byte (output_bfd); | |
516 | lang_output_section_statement_type *os; | |
517 | ||
518 | os = lang_output_section_find (tree->name.name); | |
519 | check (os, tree->name.name, "SIZEOF"); | |
520 | result = new_abs (os->bfd_section->_raw_size / opb); | |
521 | } | |
522 | else | |
523 | result = invalid (); | |
524 | break; | |
525 | ||
526 | default: | |
527 | FAIL (); | |
528 | break; | |
529 | } | |
252b5132 RH |
530 | |
531 | return result; | |
532 | } | |
8c95a62e | 533 | |
4de2d33d | 534 | etree_value_type |
252b5132 RH |
535 | exp_fold_tree (tree, current_section, allocation_done, dot, dotp) |
536 | etree_type *tree; | |
537 | lang_output_section_statement_type *current_section; | |
8c95a62e | 538 | lang_phase_type allocation_done; |
252b5132 RH |
539 | bfd_vma dot; |
540 | bfd_vma *dotp; | |
541 | { | |
542 | etree_value_type result; | |
543 | ||
544 | if (tree == NULL) | |
545 | { | |
546 | result.valid_p = false; | |
547 | return result; | |
548 | } | |
549 | ||
4de2d33d | 550 | switch (tree->type.node_class) |
252b5132 RH |
551 | { |
552 | case etree_value: | |
2c382fb6 | 553 | result = new_rel (tree->value.value, tree->value.str, current_section); |
252b5132 RH |
554 | break; |
555 | ||
556 | case etree_rel: | |
557 | if (allocation_done != lang_final_phase_enum) | |
558 | result.valid_p = false; | |
559 | else | |
560 | result = new_rel ((tree->rel.value | |
561 | + tree->rel.section->output_section->vma | |
562 | + tree->rel.section->output_offset), | |
2c382fb6 | 563 | NULL, |
252b5132 RH |
564 | current_section); |
565 | break; | |
566 | ||
567 | case etree_assert: | |
568 | result = exp_fold_tree (tree->assert_s.child, | |
8c95a62e KH |
569 | current_section, |
570 | allocation_done, dot, dotp); | |
252b5132 RH |
571 | if (result.valid_p) |
572 | { | |
573 | if (! result.value) | |
574 | einfo ("%F%P: %s\n", tree->assert_s.message); | |
575 | return result; | |
576 | } | |
577 | break; | |
578 | ||
579 | case etree_unary: | |
580 | result = exp_fold_tree (tree->unary.child, | |
581 | current_section, | |
582 | allocation_done, dot, dotp); | |
583 | if (result.valid_p) | |
584 | { | |
4de2d33d | 585 | switch (tree->type.node_code) |
252b5132 RH |
586 | { |
587 | case ALIGN_K: | |
588 | if (allocation_done != lang_first_phase_enum) | |
589 | result = new_rel_from_section (ALIGN_N (dot, result.value), | |
590 | current_section); | |
591 | else | |
592 | result.valid_p = false; | |
593 | break; | |
594 | ||
595 | case ABSOLUTE: | |
596 | if (allocation_done != lang_first_phase_enum && result.valid_p) | |
597 | { | |
598 | result.value += result.section->bfd_section->vma; | |
599 | result.section = abs_output_section; | |
600 | } | |
4de2d33d | 601 | else |
252b5132 RH |
602 | result.valid_p = false; |
603 | break; | |
604 | ||
605 | case '~': | |
606 | make_abs (&result); | |
607 | result.value = ~result.value; | |
608 | break; | |
609 | ||
610 | case '!': | |
611 | make_abs (&result); | |
612 | result.value = !result.value; | |
613 | break; | |
614 | ||
615 | case '-': | |
616 | make_abs (&result); | |
617 | result.value = -result.value; | |
618 | break; | |
619 | ||
620 | case NEXT: | |
621 | /* Return next place aligned to value. */ | |
622 | if (allocation_done == lang_allocating_phase_enum) | |
623 | { | |
624 | make_abs (&result); | |
625 | result.value = ALIGN_N (dot, result.value); | |
626 | } | |
627 | else | |
628 | result.valid_p = false; | |
629 | break; | |
630 | ||
2d20f7bf JJ |
631 | case DATA_SEGMENT_END: |
632 | if (allocation_done != lang_first_phase_enum | |
633 | && current_section == abs_output_section | |
634 | && (exp_data_seg.phase == exp_dataseg_align_seen | |
635 | || exp_data_seg.phase == exp_dataseg_adjust | |
636 | || allocation_done != lang_allocating_phase_enum)) | |
637 | { | |
638 | if (exp_data_seg.phase == exp_dataseg_align_seen) | |
639 | { | |
640 | exp_data_seg.phase = exp_dataseg_end_seen; | |
641 | exp_data_seg.end = result.value; | |
642 | } | |
643 | } | |
644 | else | |
645 | result.valid_p = false; | |
646 | break; | |
647 | ||
252b5132 RH |
648 | default: |
649 | FAIL (); | |
650 | break; | |
651 | } | |
652 | } | |
653 | break; | |
654 | ||
655 | case etree_trinary: | |
656 | result = exp_fold_tree (tree->trinary.cond, current_section, | |
657 | allocation_done, dot, dotp); | |
658 | if (result.valid_p) | |
659 | result = exp_fold_tree ((result.value | |
660 | ? tree->trinary.lhs | |
661 | : tree->trinary.rhs), | |
662 | current_section, | |
663 | allocation_done, dot, dotp); | |
664 | break; | |
665 | ||
666 | case etree_binary: | |
667 | result = fold_binary (tree, current_section, allocation_done, | |
668 | dot, dotp); | |
669 | break; | |
670 | ||
671 | case etree_assign: | |
672 | case etree_provide: | |
b46a87b1 | 673 | case etree_provided: |
252b5132 RH |
674 | if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) |
675 | { | |
c7d701b0 | 676 | /* Assignment to dot can only be done during allocation. */ |
b46a87b1 | 677 | if (tree->type.node_class != etree_assign) |
252b5132 RH |
678 | einfo (_("%F%S can not PROVIDE assignment to location counter\n")); |
679 | if (allocation_done == lang_allocating_phase_enum | |
680 | || (allocation_done == lang_final_phase_enum | |
681 | && current_section == abs_output_section)) | |
682 | { | |
683 | result = exp_fold_tree (tree->assign.src, | |
684 | current_section, | |
2d20f7bf | 685 | allocation_done, dot, |
252b5132 RH |
686 | dotp); |
687 | if (! result.valid_p) | |
688 | einfo (_("%F%S invalid assignment to location counter\n")); | |
689 | else | |
690 | { | |
691 | if (current_section == NULL) | |
692 | einfo (_("%F%S assignment to location counter invalid outside of SECTION\n")); | |
693 | else | |
694 | { | |
695 | bfd_vma nextdot; | |
696 | ||
697 | nextdot = (result.value | |
698 | + current_section->bfd_section->vma); | |
699 | if (nextdot < dot | |
700 | && current_section != abs_output_section) | |
c7d701b0 NC |
701 | einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"), |
702 | dot, nextdot); | |
252b5132 | 703 | else |
4de2d33d | 704 | *dotp = nextdot; |
252b5132 RH |
705 | } |
706 | } | |
707 | } | |
708 | } | |
709 | else | |
710 | { | |
711 | result = exp_fold_tree (tree->assign.src, | |
712 | current_section, allocation_done, | |
713 | dot, dotp); | |
714 | if (result.valid_p) | |
715 | { | |
716 | boolean create; | |
717 | struct bfd_link_hash_entry *h; | |
718 | ||
719 | if (tree->type.node_class == etree_assign) | |
720 | create = true; | |
721 | else | |
722 | create = false; | |
723 | h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst, | |
724 | create, false, false); | |
725 | if (h == (struct bfd_link_hash_entry *) NULL) | |
726 | { | |
727 | if (tree->type.node_class == etree_assign) | |
728 | einfo (_("%P%F:%s: hash creation failed\n"), | |
729 | tree->assign.dst); | |
730 | } | |
731 | else if (tree->type.node_class == etree_provide | |
732 | && h->type != bfd_link_hash_undefined | |
733 | && h->type != bfd_link_hash_common) | |
734 | { | |
735 | /* Do nothing. The symbol was defined by some | |
736 | object. */ | |
737 | } | |
738 | else | |
739 | { | |
740 | /* FIXME: Should we worry if the symbol is already | |
741 | defined? */ | |
742 | h->type = bfd_link_hash_defined; | |
743 | h->u.def.value = result.value; | |
744 | h->u.def.section = result.section->bfd_section; | |
b46a87b1 L |
745 | if (tree->type.node_class == etree_provide) |
746 | tree->type.node_class = etree_provided; | |
252b5132 RH |
747 | } |
748 | } | |
749 | } | |
750 | break; | |
751 | ||
752 | case etree_name: | |
753 | result = fold_name (tree, current_section, allocation_done, dot); | |
754 | break; | |
755 | ||
756 | default: | |
757 | FAIL (); | |
758 | break; | |
759 | } | |
760 | ||
761 | return result; | |
762 | } | |
763 | ||
4de2d33d | 764 | static etree_value_type |
252b5132 RH |
765 | exp_fold_tree_no_dot (tree, current_section, allocation_done) |
766 | etree_type *tree; | |
767 | lang_output_section_statement_type *current_section; | |
768 | lang_phase_type allocation_done; | |
769 | { | |
8c95a62e KH |
770 | return exp_fold_tree (tree, current_section, allocation_done, |
771 | (bfd_vma) 0, (bfd_vma *) NULL); | |
252b5132 RH |
772 | } |
773 | ||
774 | etree_type * | |
775 | exp_binop (code, lhs, rhs) | |
776 | int code; | |
777 | etree_type *lhs; | |
778 | etree_type *rhs; | |
779 | { | |
780 | etree_type value, *new; | |
781 | etree_value_type r; | |
782 | ||
783 | value.type.node_code = code; | |
784 | value.binary.lhs = lhs; | |
785 | value.binary.rhs = rhs; | |
786 | value.type.node_class = etree_binary; | |
8c95a62e KH |
787 | r = exp_fold_tree_no_dot (&value, |
788 | abs_output_section, | |
789 | lang_first_phase_enum); | |
252b5132 RH |
790 | if (r.valid_p) |
791 | { | |
8c95a62e | 792 | return exp_intop (r.value); |
252b5132 RH |
793 | } |
794 | new = (etree_type *) stat_alloc (sizeof (new->binary)); | |
8c95a62e | 795 | memcpy ((char *) new, (char *) &value, sizeof (new->binary)); |
252b5132 RH |
796 | return new; |
797 | } | |
798 | ||
799 | etree_type * | |
800 | exp_trinop (code, cond, lhs, rhs) | |
801 | int code; | |
802 | etree_type *cond; | |
803 | etree_type *lhs; | |
804 | etree_type *rhs; | |
805 | { | |
806 | etree_type value, *new; | |
807 | etree_value_type r; | |
808 | value.type.node_code = code; | |
809 | value.trinary.lhs = lhs; | |
810 | value.trinary.cond = cond; | |
811 | value.trinary.rhs = rhs; | |
812 | value.type.node_class = etree_trinary; | |
8c95a62e KH |
813 | r = exp_fold_tree_no_dot (&value, |
814 | (lang_output_section_statement_type *) NULL, | |
815 | lang_first_phase_enum); | |
816 | if (r.valid_p) | |
c7d701b0 NC |
817 | return exp_intop (r.value); |
818 | ||
252b5132 | 819 | new = (etree_type *) stat_alloc (sizeof (new->trinary)); |
8c95a62e | 820 | memcpy ((char *) new, (char *) &value, sizeof (new->trinary)); |
252b5132 RH |
821 | return new; |
822 | } | |
823 | ||
252b5132 RH |
824 | etree_type * |
825 | exp_unop (code, child) | |
826 | int code; | |
827 | etree_type *child; | |
828 | { | |
829 | etree_type value, *new; | |
830 | ||
831 | etree_value_type r; | |
832 | value.unary.type.node_code = code; | |
833 | value.unary.child = child; | |
834 | value.unary.type.node_class = etree_unary; | |
8c95a62e KH |
835 | r = exp_fold_tree_no_dot (&value, abs_output_section, |
836 | lang_first_phase_enum); | |
837 | if (r.valid_p) | |
c7d701b0 NC |
838 | return exp_intop (r.value); |
839 | ||
252b5132 | 840 | new = (etree_type *) stat_alloc (sizeof (new->unary)); |
8c95a62e | 841 | memcpy ((char *) new, (char *) &value, sizeof (new->unary)); |
252b5132 RH |
842 | return new; |
843 | } | |
844 | ||
252b5132 RH |
845 | etree_type * |
846 | exp_nameop (code, name) | |
847 | int code; | |
848 | CONST char *name; | |
849 | { | |
850 | etree_type value, *new; | |
851 | etree_value_type r; | |
852 | value.name.type.node_code = code; | |
853 | value.name.name = name; | |
854 | value.name.type.node_class = etree_name; | |
855 | ||
8c95a62e KH |
856 | r = exp_fold_tree_no_dot (&value, |
857 | (lang_output_section_statement_type *) NULL, | |
858 | lang_first_phase_enum); | |
859 | if (r.valid_p) | |
c7d701b0 NC |
860 | return exp_intop (r.value); |
861 | ||
252b5132 | 862 | new = (etree_type *) stat_alloc (sizeof (new->name)); |
8c95a62e | 863 | memcpy ((char *) new, (char *) &value, sizeof (new->name)); |
252b5132 RH |
864 | return new; |
865 | ||
866 | } | |
867 | ||
252b5132 RH |
868 | etree_type * |
869 | exp_assop (code, dst, src) | |
870 | int code; | |
871 | CONST char *dst; | |
872 | etree_type *src; | |
873 | { | |
874 | etree_type value, *new; | |
875 | ||
876 | value.assign.type.node_code = code; | |
877 | ||
252b5132 RH |
878 | value.assign.src = src; |
879 | value.assign.dst = dst; | |
880 | value.assign.type.node_class = etree_assign; | |
881 | ||
882 | #if 0 | |
8c95a62e | 883 | if (exp_fold_tree_no_dot (&value, &result)) |
c7d701b0 | 884 | return exp_intop (result); |
252b5132 | 885 | #endif |
8c95a62e KH |
886 | new = (etree_type *) stat_alloc (sizeof (new->assign)); |
887 | memcpy ((char *) new, (char *) &value, sizeof (new->assign)); | |
252b5132 RH |
888 | return new; |
889 | } | |
890 | ||
891 | /* Handle PROVIDE. */ | |
892 | ||
893 | etree_type * | |
894 | exp_provide (dst, src) | |
895 | const char *dst; | |
896 | etree_type *src; | |
897 | { | |
898 | etree_type *n; | |
899 | ||
900 | n = (etree_type *) stat_alloc (sizeof (n->assign)); | |
901 | n->assign.type.node_code = '='; | |
902 | n->assign.type.node_class = etree_provide; | |
903 | n->assign.src = src; | |
904 | n->assign.dst = dst; | |
905 | return n; | |
906 | } | |
907 | ||
908 | /* Handle ASSERT. */ | |
909 | ||
910 | etree_type * | |
911 | exp_assert (exp, message) | |
912 | etree_type *exp; | |
913 | const char *message; | |
914 | { | |
915 | etree_type *n; | |
916 | ||
917 | n = (etree_type *) stat_alloc (sizeof (n->assert_s)); | |
918 | n->assert_s.type.node_code = '!'; | |
919 | n->assert_s.type.node_class = etree_assert; | |
920 | n->assert_s.child = exp; | |
921 | n->assert_s.message = message; | |
922 | return n; | |
923 | } | |
924 | ||
4de2d33d | 925 | void |
252b5132 RH |
926 | exp_print_tree (tree) |
927 | etree_type *tree; | |
928 | { | |
c7d701b0 NC |
929 | if (config.map_file == NULL) |
930 | config.map_file = stderr; | |
931 | ||
932 | if (tree == NULL) | |
933 | { | |
934 | minfo ("NULL TREE\n"); | |
935 | return; | |
936 | } | |
937 | ||
8c95a62e KH |
938 | switch (tree->type.node_class) |
939 | { | |
940 | case etree_value: | |
941 | minfo ("0x%v", tree->value.value); | |
942 | return; | |
943 | case etree_rel: | |
944 | if (tree->rel.section->owner != NULL) | |
945 | minfo ("%B:", tree->rel.section->owner); | |
946 | minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value); | |
947 | return; | |
948 | case etree_assign: | |
252b5132 | 949 | #if 0 |
8c95a62e | 950 | if (tree->assign.dst->sdefs != (asymbol *) NULL) |
c7d701b0 NC |
951 | fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name, |
952 | tree->assign.dst->sdefs->value); | |
8c95a62e | 953 | else |
c7d701b0 | 954 | fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name); |
252b5132 | 955 | #endif |
8c95a62e KH |
956 | fprintf (config.map_file, "%s", tree->assign.dst); |
957 | exp_print_token (tree->type.node_code); | |
958 | exp_print_tree (tree->assign.src); | |
959 | break; | |
960 | case etree_provide: | |
b46a87b1 | 961 | case etree_provided: |
8c95a62e KH |
962 | fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst); |
963 | exp_print_tree (tree->assign.src); | |
964 | fprintf (config.map_file, ")"); | |
965 | break; | |
966 | case etree_binary: | |
967 | fprintf (config.map_file, "("); | |
968 | exp_print_tree (tree->binary.lhs); | |
969 | exp_print_token (tree->type.node_code); | |
970 | exp_print_tree (tree->binary.rhs); | |
971 | fprintf (config.map_file, ")"); | |
972 | break; | |
973 | case etree_trinary: | |
974 | exp_print_tree (tree->trinary.cond); | |
975 | fprintf (config.map_file, "?"); | |
976 | exp_print_tree (tree->trinary.lhs); | |
977 | fprintf (config.map_file, ":"); | |
978 | exp_print_tree (tree->trinary.rhs); | |
979 | break; | |
980 | case etree_unary: | |
981 | exp_print_token (tree->unary.type.node_code); | |
982 | if (tree->unary.child) | |
983 | { | |
984 | fprintf (config.map_file, "("); | |
985 | exp_print_tree (tree->unary.child); | |
986 | fprintf (config.map_file, ")"); | |
987 | } | |
988 | break; | |
989 | ||
990 | case etree_assert: | |
991 | fprintf (config.map_file, "ASSERT ("); | |
992 | exp_print_tree (tree->assert_s.child); | |
993 | fprintf (config.map_file, ", %s)", tree->assert_s.message); | |
994 | break; | |
995 | ||
996 | case etree_undef: | |
997 | fprintf (config.map_file, "????????"); | |
998 | break; | |
999 | case etree_name: | |
1000 | if (tree->type.node_code == NAME) | |
1001 | { | |
1002 | fprintf (config.map_file, "%s", tree->name.name); | |
1003 | } | |
1004 | else | |
1005 | { | |
1006 | exp_print_token (tree->type.node_code); | |
1007 | if (tree->name.name) | |
1008 | fprintf (config.map_file, "(%s)", tree->name.name); | |
1009 | } | |
1010 | break; | |
1011 | default: | |
1012 | FAIL (); | |
1013 | break; | |
252b5132 | 1014 | } |
252b5132 RH |
1015 | } |
1016 | ||
1017 | bfd_vma | |
1018 | exp_get_vma (tree, def, name, allocation_done) | |
1019 | etree_type *tree; | |
1020 | bfd_vma def; | |
1021 | char *name; | |
1022 | lang_phase_type allocation_done; | |
1023 | { | |
1024 | etree_value_type r; | |
1025 | ||
1026 | if (tree != NULL) | |
1027 | { | |
1028 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1029 | if (! r.valid_p && name != NULL) | |
1030 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
1031 | return r.value; | |
1032 | } | |
1033 | else | |
1034 | return def; | |
1035 | } | |
1036 | ||
4de2d33d | 1037 | int |
8c95a62e | 1038 | exp_get_value_int (tree, def, name, allocation_done) |
252b5132 RH |
1039 | etree_type *tree; |
1040 | int def; | |
1041 | char *name; | |
1042 | lang_phase_type allocation_done; | |
1043 | { | |
8c95a62e | 1044 | return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done); |
252b5132 RH |
1045 | } |
1046 | ||
2c382fb6 AM |
1047 | fill_type * |
1048 | exp_get_fill (tree, def, name, allocation_done) | |
1049 | etree_type *tree; | |
1050 | fill_type *def; | |
1051 | char *name; | |
1052 | lang_phase_type allocation_done; | |
1053 | { | |
1054 | fill_type *fill; | |
1055 | etree_value_type r; | |
1056 | size_t len; | |
1057 | unsigned int val; | |
1058 | ||
1059 | if (tree == NULL) | |
1060 | return def; | |
1061 | ||
1062 | r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1063 | if (! r.valid_p && name != NULL) | |
1064 | einfo (_("%F%S nonconstant expression for %s\n"), name); | |
1065 | ||
1066 | if (r.str != NULL && (len = strlen (r.str)) != 0) | |
1067 | { | |
1068 | unsigned char *dst; | |
1069 | unsigned char *s; | |
1070 | fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1); | |
1071 | fill->size = (len + 1) / 2; | |
1072 | dst = fill->data; | |
1073 | s = r.str; | |
1074 | val = 0; | |
1075 | do | |
1076 | { | |
1077 | unsigned int digit; | |
1078 | ||
1079 | digit = *s++ - '0'; | |
1080 | if (digit > 9) | |
1081 | digit = (digit - 'A' + '0' + 10) & 0xf; | |
1082 | val <<= 4; | |
1083 | val += digit; | |
1084 | --len; | |
1085 | if ((len & 1) == 0) | |
1086 | { | |
1087 | *dst++ = val; | |
1088 | val = 0; | |
1089 | } | |
1090 | } | |
1091 | while (len != 0); | |
1092 | } | |
1093 | else | |
1094 | { | |
1095 | fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1); | |
1096 | val = r.value; | |
1097 | fill->data[0] = (val >> 24) & 0xff; | |
1098 | fill->data[1] = (val >> 16) & 0xff; | |
1099 | fill->data[2] = (val >> 8) & 0xff; | |
1100 | fill->data[3] = (val >> 0) & 0xff; | |
1101 | fill->size = 4; | |
1102 | } | |
1103 | return fill; | |
1104 | } | |
1105 | ||
252b5132 RH |
1106 | bfd_vma |
1107 | exp_get_abs_int (tree, def, name, allocation_done) | |
1108 | etree_type *tree; | |
87f2a346 | 1109 | int def ATTRIBUTE_UNUSED; |
252b5132 RH |
1110 | char *name; |
1111 | lang_phase_type allocation_done; | |
1112 | { | |
1113 | etree_value_type res; | |
1114 | res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done); | |
1115 | ||
1116 | if (res.valid_p) | |
c7d701b0 | 1117 | res.value += res.section->bfd_section->vma; |
8c95a62e | 1118 | else |
c7d701b0 NC |
1119 | einfo (_("%F%S non constant expression for %s\n"), name); |
1120 | ||
252b5132 RH |
1121 | return res.value; |
1122 | } |