Add SORT_BY_INIT_PRIORITY.
[binutils.git] / ld / ldexp.c
blobde7f9f29bc18e5490b60147a67ad0aa93ab126e7
1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
7 This file is part of the GNU Binutils.
9 This program 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 3 of the License, or
12 (at your option) any later version.
14 This program 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.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
25 /* This module is in charge of working out the contents of expressions.
27 It has to keep track of the relative/absness of a symbol etc. This
28 is done by keeping all values in a struct (an etree_value_type)
29 which contains a value, a section to which it is relative and a
30 valid bit. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "bfdlink.h"
36 #include "ld.h"
37 #include "ldmain.h"
38 #include "ldmisc.h"
39 #include "ldexp.h"
40 #include "ldlex.h"
41 #include <ldgram.h>
42 #include "ldlang.h"
43 #include "libiberty.h"
44 #include "safe-ctype.h"
46 static void exp_fold_tree_1 (etree_type *);
47 static void exp_fold_tree_no_dot (etree_type *);
48 static bfd_vma align_n (bfd_vma, bfd_vma);
50 segment_type *segments;
52 struct ldexp_control expld;
54 /* Print the string representation of the given token. Surround it
55 with spaces if INFIX_P is TRUE. */
57 static void
58 exp_print_token (token_code_type code, int infix_p)
60 static const struct
62 token_code_type code;
63 char * name;
65 table[] =
67 { INT, "int" },
68 { NAME, "NAME" },
69 { PLUSEQ, "+=" },
70 { MINUSEQ, "-=" },
71 { MULTEQ, "*=" },
72 { DIVEQ, "/=" },
73 { LSHIFTEQ, "<<=" },
74 { RSHIFTEQ, ">>=" },
75 { ANDEQ, "&=" },
76 { OREQ, "|=" },
77 { OROR, "||" },
78 { ANDAND, "&&" },
79 { EQ, "==" },
80 { NE, "!=" },
81 { LE, "<=" },
82 { GE, ">=" },
83 { LSHIFT, "<<" },
84 { RSHIFT, ">>" },
85 { ALIGN_K, "ALIGN" },
86 { BLOCK, "BLOCK" },
87 { QUAD, "QUAD" },
88 { SQUAD, "SQUAD" },
89 { LONG, "LONG" },
90 { SHORT, "SHORT" },
91 { BYTE, "BYTE" },
92 { SECTIONS, "SECTIONS" },
93 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
94 { MEMORY, "MEMORY" },
95 { DEFINED, "DEFINED" },
96 { TARGET_K, "TARGET" },
97 { SEARCH_DIR, "SEARCH_DIR" },
98 { MAP, "MAP" },
99 { ENTRY, "ENTRY" },
100 { NEXT, "NEXT" },
101 { ALIGNOF, "ALIGNOF" },
102 { SIZEOF, "SIZEOF" },
103 { ADDR, "ADDR" },
104 { LOADADDR, "LOADADDR" },
105 { CONSTANT, "CONSTANT" },
106 { ABSOLUTE, "ABSOLUTE" },
107 { MAX_K, "MAX" },
108 { MIN_K, "MIN" },
109 { ASSERT_K, "ASSERT" },
110 { REL, "relocatable" },
111 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
112 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
113 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
114 { ORIGIN, "ORIGIN" },
115 { LENGTH, "LENGTH" },
116 { SEGMENT_START, "SEGMENT_START" }
118 unsigned int idx;
120 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
121 if (table[idx].code == code)
122 break;
124 if (infix_p)
125 fputc (' ', config.map_file);
127 if (idx < ARRAY_SIZE (table))
128 fputs (table[idx].name, config.map_file);
129 else if (code < 127)
130 fputc (code, config.map_file);
131 else
132 fprintf (config.map_file, "<code %d>", code);
134 if (infix_p)
135 fputc (' ', config.map_file);
138 static void
139 make_abs (void)
141 if (expld.result.section != NULL)
142 expld.result.value += expld.result.section->vma;
143 expld.result.section = bfd_abs_section_ptr;
146 static void
147 new_abs (bfd_vma value)
149 expld.result.valid_p = TRUE;
150 expld.result.section = bfd_abs_section_ptr;
151 expld.result.value = value;
152 expld.result.str = NULL;
155 etree_type *
156 exp_intop (bfd_vma value)
158 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
159 new_e->type.node_code = INT;
160 new_e->type.lineno = lineno;
161 new_e->value.value = value;
162 new_e->value.str = NULL;
163 new_e->type.node_class = etree_value;
164 return new_e;
167 etree_type *
168 exp_bigintop (bfd_vma value, char *str)
170 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
171 new_e->type.node_code = INT;
172 new_e->type.lineno = lineno;
173 new_e->value.value = value;
174 new_e->value.str = str;
175 new_e->type.node_class = etree_value;
176 return new_e;
179 /* Build an expression representing an unnamed relocatable value. */
181 etree_type *
182 exp_relop (asection *section, bfd_vma value)
184 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
185 new_e->type.node_code = REL;
186 new_e->type.lineno = lineno;
187 new_e->type.node_class = etree_rel;
188 new_e->rel.section = section;
189 new_e->rel.value = value;
190 return new_e;
193 static void
194 new_number (bfd_vma value)
196 expld.result.valid_p = TRUE;
197 expld.result.value = value;
198 expld.result.str = NULL;
199 expld.result.section = NULL;
202 static void
203 new_rel (bfd_vma value, asection *section)
205 expld.result.valid_p = TRUE;
206 expld.result.value = value;
207 expld.result.str = NULL;
208 expld.result.section = section;
211 static void
212 new_rel_from_abs (bfd_vma value)
214 expld.result.valid_p = TRUE;
215 expld.result.value = value - expld.section->vma;
216 expld.result.str = NULL;
217 expld.result.section = expld.section;
220 static void
221 fold_unary (etree_type *tree)
223 exp_fold_tree_1 (tree->unary.child);
224 if (expld.result.valid_p)
226 switch (tree->type.node_code)
228 case ALIGN_K:
229 if (expld.phase != lang_first_phase_enum)
230 new_rel_from_abs (align_n (expld.dot, expld.result.value));
231 else
232 expld.result.valid_p = FALSE;
233 break;
235 case ABSOLUTE:
236 make_abs ();
237 break;
239 case '~':
240 expld.result.value = ~expld.result.value;
241 break;
243 case '!':
244 expld.result.value = !expld.result.value;
245 break;
247 case '-':
248 expld.result.value = -expld.result.value;
249 break;
251 case NEXT:
252 /* Return next place aligned to value. */
253 if (expld.phase != lang_first_phase_enum)
255 make_abs ();
256 expld.result.value = align_n (expld.dot, expld.result.value);
258 else
259 expld.result.valid_p = FALSE;
260 break;
262 case DATA_SEGMENT_END:
263 if (expld.phase != lang_first_phase_enum
264 && expld.section == bfd_abs_section_ptr
265 && (expld.dataseg.phase == exp_dataseg_align_seen
266 || expld.dataseg.phase == exp_dataseg_relro_seen
267 || expld.dataseg.phase == exp_dataseg_adjust
268 || expld.dataseg.phase == exp_dataseg_relro_adjust
269 || expld.phase == lang_final_phase_enum))
271 if (expld.dataseg.phase == exp_dataseg_align_seen
272 || expld.dataseg.phase == exp_dataseg_relro_seen)
274 expld.dataseg.phase = exp_dataseg_end_seen;
275 expld.dataseg.end = expld.result.value;
278 else
279 expld.result.valid_p = FALSE;
280 break;
282 default:
283 FAIL ();
284 break;
289 static void
290 fold_binary (etree_type *tree)
292 etree_value_type lhs;
293 exp_fold_tree_1 (tree->binary.lhs);
295 /* The SEGMENT_START operator is special because its first
296 operand is a string, not the name of a symbol. Note that the
297 operands have been swapped, so binary.lhs is second (default)
298 operand, binary.rhs is first operand. */
299 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
301 const char *segment_name;
302 segment_type *seg;
304 /* Check to see if the user has overridden the default
305 value. */
306 segment_name = tree->binary.rhs->name.name;
307 for (seg = segments; seg; seg = seg->next)
308 if (strcmp (seg->name, segment_name) == 0)
310 if (!seg->used
311 && config.magic_demand_paged
312 && (seg->value % config.maxpagesize) != 0)
313 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
314 segment_name);
315 seg->used = TRUE;
316 new_rel_from_abs (seg->value);
317 break;
319 return;
322 lhs = expld.result;
323 exp_fold_tree_1 (tree->binary.rhs);
324 expld.result.valid_p &= lhs.valid_p;
326 if (expld.result.valid_p)
328 if (lhs.section != expld.result.section)
330 /* If the values are from different sections, and neither is
331 just a number, make both the source arguments absolute. */
332 if (expld.result.section != NULL
333 && lhs.section != NULL)
335 make_abs ();
336 lhs.value += lhs.section->vma;
339 /* If the rhs is just a number, keep the lhs section. */
340 else if (expld.result.section == NULL)
341 expld.result.section = lhs.section;
344 switch (tree->type.node_code)
346 case '%':
347 if (expld.result.value != 0)
348 expld.result.value = ((bfd_signed_vma) lhs.value
349 % (bfd_signed_vma) expld.result.value);
350 else if (expld.phase != lang_mark_phase_enum)
351 einfo (_("%F%S %% by zero\n"));
352 break;
354 case '/':
355 if (expld.result.value != 0)
356 expld.result.value = ((bfd_signed_vma) lhs.value
357 / (bfd_signed_vma) expld.result.value);
358 else if (expld.phase != lang_mark_phase_enum)
359 einfo (_("%F%S / by zero\n"));
360 break;
362 #define BOP(x, y) \
363 case x: \
364 expld.result.value = lhs.value y expld.result.value; \
365 break;
367 #define BOPN(x, y) \
368 case x: \
369 expld.result.value = lhs.value y expld.result.value; \
370 expld.result.section = NULL; \
371 break;
373 BOP ('+', +);
374 BOP ('*', *);
375 BOP ('-', -);
376 BOP (LSHIFT, <<);
377 BOP (RSHIFT, >>);
378 BOP ('&', &);
379 BOP ('^', ^);
380 BOP ('|', |);
381 BOPN (EQ, ==);
382 BOPN (NE, !=);
383 BOPN ('<', <);
384 BOPN ('>', >);
385 BOPN (LE, <=);
386 BOPN (GE, >=);
387 BOPN (ANDAND, &&);
388 BOPN (OROR, ||);
390 case MAX_K:
391 if (lhs.value > expld.result.value)
392 expld.result.value = lhs.value;
393 break;
395 case MIN_K:
396 if (lhs.value < expld.result.value)
397 expld.result.value = lhs.value;
398 break;
400 case ALIGN_K:
401 expld.result.value = align_n (lhs.value, expld.result.value);
402 break;
404 case DATA_SEGMENT_ALIGN:
405 expld.dataseg.relro = exp_dataseg_relro_start;
406 if (expld.phase != lang_first_phase_enum
407 && expld.section == bfd_abs_section_ptr
408 && (expld.dataseg.phase == exp_dataseg_none
409 || expld.dataseg.phase == exp_dataseg_adjust
410 || expld.dataseg.phase == exp_dataseg_relro_adjust
411 || expld.phase == lang_final_phase_enum))
413 bfd_vma maxpage = lhs.value;
414 bfd_vma commonpage = expld.result.value;
416 expld.result.value = align_n (expld.dot, maxpage);
417 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
418 expld.result.value = expld.dataseg.base;
419 else if (expld.dataseg.phase != exp_dataseg_adjust)
421 expld.result.value += expld.dot & (maxpage - 1);
422 if (expld.phase == lang_allocating_phase_enum)
424 expld.dataseg.phase = exp_dataseg_align_seen;
425 expld.dataseg.min_base = expld.dot;
426 expld.dataseg.base = expld.result.value;
427 expld.dataseg.pagesize = commonpage;
428 expld.dataseg.maxpagesize = maxpage;
429 expld.dataseg.relro_end = 0;
432 else if (commonpage < maxpage)
433 expld.result.value += ((expld.dot + commonpage - 1)
434 & (maxpage - commonpage));
436 else
437 expld.result.valid_p = FALSE;
438 break;
440 case DATA_SEGMENT_RELRO_END:
441 expld.dataseg.relro = exp_dataseg_relro_end;
442 if (expld.phase != lang_first_phase_enum
443 && (expld.dataseg.phase == exp_dataseg_align_seen
444 || expld.dataseg.phase == exp_dataseg_adjust
445 || expld.dataseg.phase == exp_dataseg_relro_adjust
446 || expld.phase == lang_final_phase_enum))
448 if (expld.dataseg.phase == exp_dataseg_align_seen
449 || expld.dataseg.phase == exp_dataseg_relro_adjust)
450 expld.dataseg.relro_end = lhs.value + expld.result.value;
452 if (expld.dataseg.phase == exp_dataseg_relro_adjust
453 && (expld.dataseg.relro_end
454 & (expld.dataseg.pagesize - 1)))
456 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
457 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
458 expld.result.value = (expld.dataseg.relro_end
459 - expld.result.value);
461 else
462 expld.result.value = lhs.value;
464 if (expld.dataseg.phase == exp_dataseg_align_seen)
465 expld.dataseg.phase = exp_dataseg_relro_seen;
467 else
468 expld.result.valid_p = FALSE;
469 break;
471 default:
472 FAIL ();
477 static void
478 fold_trinary (etree_type *tree)
480 exp_fold_tree_1 (tree->trinary.cond);
481 if (expld.result.valid_p)
482 exp_fold_tree_1 (expld.result.value
483 ? tree->trinary.lhs
484 : tree->trinary.rhs);
487 static void
488 fold_name (etree_type *tree)
490 memset (&expld.result, 0, sizeof (expld.result));
492 switch (tree->type.node_code)
494 case SIZEOF_HEADERS:
495 if (expld.phase != lang_first_phase_enum)
497 bfd_vma hdr_size = 0;
498 /* Don't find the real header size if only marking sections;
499 The bfd function may cache incorrect data. */
500 if (expld.phase != lang_mark_phase_enum)
501 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
502 new_number (hdr_size);
504 break;
506 case DEFINED:
507 if (expld.phase == lang_first_phase_enum)
508 lang_track_definedness (tree->name.name);
509 else
511 struct bfd_link_hash_entry *h;
512 int def_iteration
513 = lang_symbol_definition_iteration (tree->name.name);
515 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
516 &link_info,
517 tree->name.name,
518 FALSE, FALSE, TRUE);
519 new_number (h != NULL
520 && (h->type == bfd_link_hash_defined
521 || h->type == bfd_link_hash_defweak
522 || h->type == bfd_link_hash_common)
523 && (def_iteration == lang_statement_iteration
524 || def_iteration == -1));
526 break;
528 case NAME:
529 if (expld.phase == lang_first_phase_enum)
531 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
532 new_rel_from_abs (expld.dot);
533 else
535 struct bfd_link_hash_entry *h;
537 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
538 &link_info,
539 tree->name.name,
540 TRUE, FALSE, TRUE);
541 if (!h)
542 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
543 else if (h->type == bfd_link_hash_defined
544 || h->type == bfd_link_hash_defweak)
546 asection *output_section;
548 output_section = h->u.def.section->output_section;
549 if (output_section == NULL)
551 if (expld.phase != lang_mark_phase_enum)
552 einfo (_("%X%S: unresolvable symbol `%s'"
553 " referenced in expression\n"),
554 tree->name.name);
556 else if (output_section == bfd_abs_section_ptr)
557 new_number (h->u.def.value + h->u.def.section->output_offset);
558 else
559 new_rel (h->u.def.value + h->u.def.section->output_offset,
560 output_section);
562 else if (expld.phase == lang_final_phase_enum
563 || expld.assigning_to_dot)
564 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
565 tree->name.name);
566 else if (h->type == bfd_link_hash_new)
568 h->type = bfd_link_hash_undefined;
569 h->u.undef.abfd = NULL;
570 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
571 bfd_link_add_undef (link_info.hash, h);
574 break;
576 case ADDR:
577 if (expld.phase != lang_first_phase_enum)
579 lang_output_section_statement_type *os;
581 os = lang_output_section_find (tree->name.name);
582 if (os == NULL)
584 if (expld.phase == lang_final_phase_enum)
585 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
586 tree->name.name);
588 else if (os->processed_vma)
589 new_rel (0, os->bfd_section);
591 break;
593 case LOADADDR:
594 if (expld.phase != lang_first_phase_enum)
596 lang_output_section_statement_type *os;
598 os = lang_output_section_find (tree->name.name);
599 if (os == NULL)
601 if (expld.phase == lang_final_phase_enum)
602 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
603 tree->name.name);
605 else if (os->processed_lma)
607 if (os->load_base == NULL)
608 new_abs (os->bfd_section->lma);
609 else
611 exp_fold_tree_1 (os->load_base);
612 if (expld.result.valid_p)
613 make_abs ();
617 break;
619 case SIZEOF:
620 case ALIGNOF:
621 if (expld.phase != lang_first_phase_enum)
623 lang_output_section_statement_type *os;
625 os = lang_output_section_find (tree->name.name);
626 if (os == NULL)
628 if (expld.phase == lang_final_phase_enum)
629 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
630 tree->name.name);
631 new_number (0);
633 else if (os->processed_vma)
635 bfd_vma val;
637 if (tree->type.node_code == SIZEOF)
638 val = (os->bfd_section->size
639 / bfd_octets_per_byte (link_info.output_bfd));
640 else
641 val = (bfd_vma)1 << os->bfd_section->alignment_power;
643 new_number (val);
646 break;
648 case LENGTH:
650 lang_memory_region_type *mem;
652 mem = lang_memory_region_lookup (tree->name.name, FALSE);
653 if (mem != NULL)
654 new_number (mem->length);
655 else
656 einfo (_("%F%S: undefined MEMORY region `%s'"
657 " referenced in expression\n"), tree->name.name);
659 break;
661 case ORIGIN:
662 if (expld.phase != lang_first_phase_enum)
664 lang_memory_region_type *mem;
666 mem = lang_memory_region_lookup (tree->name.name, FALSE);
667 if (mem != NULL)
668 new_rel_from_abs (mem->origin);
669 else
670 einfo (_("%F%S: undefined MEMORY region `%s'"
671 " referenced in expression\n"), tree->name.name);
673 break;
675 case CONSTANT:
676 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
677 new_number (config.maxpagesize);
678 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
679 new_number (config.commonpagesize);
680 else
681 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
682 tree->name.name);
683 break;
685 default:
686 FAIL ();
687 break;
691 static void
692 exp_fold_tree_1 (etree_type *tree)
694 if (tree == NULL)
696 memset (&expld.result, 0, sizeof (expld.result));
697 return;
700 switch (tree->type.node_class)
702 case etree_value:
703 new_number (tree->value.value);
704 expld.result.str = tree->value.str;
705 break;
707 case etree_rel:
708 if (expld.phase != lang_first_phase_enum)
710 asection *output_section = tree->rel.section->output_section;
711 new_rel (tree->rel.value + tree->rel.section->output_offset,
712 output_section);
714 else
715 memset (&expld.result, 0, sizeof (expld.result));
716 break;
718 case etree_assert:
719 exp_fold_tree_1 (tree->assert_s.child);
720 if (expld.phase == lang_final_phase_enum && !expld.result.value)
721 einfo ("%X%P: %s\n", tree->assert_s.message);
722 break;
724 case etree_unary:
725 fold_unary (tree);
726 break;
728 case etree_binary:
729 fold_binary (tree);
730 break;
732 case etree_trinary:
733 fold_trinary (tree);
734 break;
736 case etree_assign:
737 case etree_provide:
738 case etree_provided:
739 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
741 /* Assignment to dot can only be done during allocation. */
742 if (tree->type.node_class != etree_assign)
743 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
744 if (expld.phase == lang_mark_phase_enum
745 || expld.phase == lang_allocating_phase_enum
746 || (expld.phase == lang_final_phase_enum
747 && expld.section == bfd_abs_section_ptr))
749 /* Notify the folder that this is an assignment to dot. */
750 expld.assigning_to_dot = TRUE;
751 exp_fold_tree_1 (tree->assign.src);
752 expld.assigning_to_dot = FALSE;
754 if (!expld.result.valid_p)
756 if (expld.phase != lang_mark_phase_enum)
757 einfo (_("%F%S invalid assignment to location counter\n"));
759 else if (expld.dotp == NULL)
760 einfo (_("%F%S assignment to location counter"
761 " invalid outside of SECTION\n"));
762 else
764 bfd_vma nextdot;
766 nextdot = expld.result.value;
767 if (expld.result.section != NULL)
768 nextdot += expld.result.section->vma;
769 else
770 nextdot += expld.section->vma;
771 if (nextdot < expld.dot
772 && expld.section != bfd_abs_section_ptr)
773 einfo (_("%F%S cannot move location counter backwards"
774 " (from %V to %V)\n"), expld.dot, nextdot);
775 else
777 expld.dot = nextdot;
778 *expld.dotp = nextdot;
782 else
783 memset (&expld.result, 0, sizeof (expld.result));
785 else
787 struct bfd_link_hash_entry *h = NULL;
789 if (tree->type.node_class == etree_provide)
791 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
792 FALSE, FALSE, TRUE);
793 if (h == NULL
794 || (h->type != bfd_link_hash_new
795 && h->type != bfd_link_hash_undefined
796 && h->type != bfd_link_hash_common))
798 /* Do nothing. The symbol was never referenced, or was
799 defined by some object. */
800 break;
804 exp_fold_tree_1 (tree->assign.src);
805 if (expld.result.valid_p)
807 if (h == NULL)
809 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
810 TRUE, FALSE, TRUE);
811 if (h == NULL)
812 einfo (_("%P%F:%s: hash creation failed\n"),
813 tree->assign.dst);
816 /* FIXME: Should we worry if the symbol is already
817 defined? */
818 lang_update_definedness (tree->assign.dst, h);
819 h->type = bfd_link_hash_defined;
820 h->u.def.value = expld.result.value;
821 if (expld.result.section == NULL)
822 expld.result.section = expld.section;
823 h->u.def.section = expld.result.section;
824 if (tree->type.node_class == etree_provide)
825 tree->type.node_class = etree_provided;
827 /* Copy the symbol type if this is a simple assignment of
828 one symbol to annother. */
829 if (tree->assign.src->type.node_class == etree_name)
831 struct bfd_link_hash_entry *hsrc;
833 hsrc = bfd_link_hash_lookup (link_info.hash,
834 tree->assign.src->name.name,
835 FALSE, FALSE, TRUE);
836 if (hsrc)
837 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
838 hsrc);
841 else if (expld.phase == lang_final_phase_enum)
843 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
844 FALSE, FALSE, TRUE);
845 if (h != NULL
846 && h->type == bfd_link_hash_new)
847 h->type = bfd_link_hash_undefined;
850 break;
852 case etree_name:
853 fold_name (tree);
854 break;
856 default:
857 FAIL ();
858 memset (&expld.result, 0, sizeof (expld.result));
859 break;
862 /* Any value not inside an output section statement is an
863 absolute value. */
864 if (expld.result.valid_p
865 && expld.section == bfd_abs_section_ptr)
866 make_abs ();
869 void
870 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
872 expld.dot = *dotp;
873 expld.dotp = dotp;
874 expld.section = current_section;
875 exp_fold_tree_1 (tree);
878 static void
879 exp_fold_tree_no_dot (etree_type *tree)
881 expld.dot = 0;
882 expld.dotp = NULL;
883 expld.section = bfd_abs_section_ptr;
884 exp_fold_tree_1 (tree);
887 etree_type *
888 exp_binop (int code, etree_type *lhs, etree_type *rhs)
890 etree_type value, *new_e;
892 value.type.node_code = code;
893 value.type.lineno = lhs->type.lineno;
894 value.binary.lhs = lhs;
895 value.binary.rhs = rhs;
896 value.type.node_class = etree_binary;
897 exp_fold_tree_no_dot (&value);
898 if (expld.result.valid_p)
899 return exp_intop (expld.result.value);
901 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
902 memcpy (new_e, &value, sizeof (new_e->binary));
903 return new_e;
906 etree_type *
907 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
909 etree_type value, *new_e;
911 value.type.node_code = code;
912 value.type.lineno = lhs->type.lineno;
913 value.trinary.lhs = lhs;
914 value.trinary.cond = cond;
915 value.trinary.rhs = rhs;
916 value.type.node_class = etree_trinary;
917 exp_fold_tree_no_dot (&value);
918 if (expld.result.valid_p)
919 return exp_intop (expld.result.value);
921 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
922 memcpy (new_e, &value, sizeof (new_e->trinary));
923 return new_e;
926 etree_type *
927 exp_unop (int code, etree_type *child)
929 etree_type value, *new_e;
931 value.unary.type.node_code = code;
932 value.unary.type.lineno = child->type.lineno;
933 value.unary.child = child;
934 value.unary.type.node_class = etree_unary;
935 exp_fold_tree_no_dot (&value);
936 if (expld.result.valid_p)
937 return exp_intop (expld.result.value);
939 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
940 memcpy (new_e, &value, sizeof (new_e->unary));
941 return new_e;
944 etree_type *
945 exp_nameop (int code, const char *name)
947 etree_type value, *new_e;
949 value.name.type.node_code = code;
950 value.name.type.lineno = lineno;
951 value.name.name = name;
952 value.name.type.node_class = etree_name;
954 exp_fold_tree_no_dot (&value);
955 if (expld.result.valid_p)
956 return exp_intop (expld.result.value);
958 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
959 memcpy (new_e, &value, sizeof (new_e->name));
960 return new_e;
964 etree_type *
965 exp_assop (int code, const char *dst, etree_type *src)
967 etree_type *new_e;
969 new_e = (etree_type *) stat_alloc (sizeof (new_e->assign));
970 new_e->type.node_code = code;
971 new_e->type.lineno = src->type.lineno;
972 new_e->type.node_class = etree_assign;
973 new_e->assign.src = src;
974 new_e->assign.dst = dst;
975 return new_e;
978 /* Handle PROVIDE. */
980 etree_type *
981 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
983 etree_type *n;
985 n = (etree_type *) stat_alloc (sizeof (n->assign));
986 n->assign.type.node_code = '=';
987 n->assign.type.lineno = src->type.lineno;
988 n->assign.type.node_class = etree_provide;
989 n->assign.src = src;
990 n->assign.dst = dst;
991 n->assign.hidden = hidden;
992 return n;
995 /* Handle ASSERT. */
997 etree_type *
998 exp_assert (etree_type *exp, const char *message)
1000 etree_type *n;
1002 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1003 n->assert_s.type.node_code = '!';
1004 n->assert_s.type.lineno = exp->type.lineno;
1005 n->assert_s.type.node_class = etree_assert;
1006 n->assert_s.child = exp;
1007 n->assert_s.message = message;
1008 return n;
1011 void
1012 exp_print_tree (etree_type *tree)
1014 bfd_boolean function_like;
1016 if (config.map_file == NULL)
1017 config.map_file = stderr;
1019 if (tree == NULL)
1021 minfo ("NULL TREE\n");
1022 return;
1025 switch (tree->type.node_class)
1027 case etree_value:
1028 minfo ("0x%v", tree->value.value);
1029 return;
1030 case etree_rel:
1031 if (tree->rel.section->owner != NULL)
1032 minfo ("%B:", tree->rel.section->owner);
1033 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1034 return;
1035 case etree_assign:
1036 fputs (tree->assign.dst, config.map_file);
1037 exp_print_token (tree->type.node_code, TRUE);
1038 exp_print_tree (tree->assign.src);
1039 break;
1040 case etree_provide:
1041 case etree_provided:
1042 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1043 exp_print_tree (tree->assign.src);
1044 fputc (')', config.map_file);
1045 break;
1046 case etree_binary:
1047 function_like = FALSE;
1048 switch (tree->type.node_code)
1050 case MAX_K:
1051 case MIN_K:
1052 case ALIGN_K:
1053 case DATA_SEGMENT_ALIGN:
1054 case DATA_SEGMENT_RELRO_END:
1055 function_like = TRUE;
1057 if (function_like)
1059 exp_print_token (tree->type.node_code, FALSE);
1060 fputc (' ', config.map_file);
1062 fputc ('(', config.map_file);
1063 exp_print_tree (tree->binary.lhs);
1064 if (function_like)
1065 fprintf (config.map_file, ", ");
1066 else
1067 exp_print_token (tree->type.node_code, TRUE);
1068 exp_print_tree (tree->binary.rhs);
1069 fputc (')', config.map_file);
1070 break;
1071 case etree_trinary:
1072 exp_print_tree (tree->trinary.cond);
1073 fputc ('?', config.map_file);
1074 exp_print_tree (tree->trinary.lhs);
1075 fputc (':', config.map_file);
1076 exp_print_tree (tree->trinary.rhs);
1077 break;
1078 case etree_unary:
1079 exp_print_token (tree->unary.type.node_code, FALSE);
1080 if (tree->unary.child)
1082 fprintf (config.map_file, " (");
1083 exp_print_tree (tree->unary.child);
1084 fputc (')', config.map_file);
1086 break;
1088 case etree_assert:
1089 fprintf (config.map_file, "ASSERT (");
1090 exp_print_tree (tree->assert_s.child);
1091 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1092 break;
1094 case etree_name:
1095 if (tree->type.node_code == NAME)
1096 fputs (tree->name.name, config.map_file);
1097 else
1099 exp_print_token (tree->type.node_code, FALSE);
1100 if (tree->name.name)
1101 fprintf (config.map_file, " (%s)", tree->name.name);
1103 break;
1104 default:
1105 FAIL ();
1106 break;
1110 bfd_vma
1111 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1113 if (tree != NULL)
1115 exp_fold_tree_no_dot (tree);
1116 if (expld.result.valid_p)
1117 return expld.result.value;
1118 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1119 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1121 return def;
1125 exp_get_value_int (etree_type *tree, int def, char *name)
1127 return exp_get_vma (tree, def, name);
1130 fill_type *
1131 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1133 fill_type *fill;
1134 size_t len;
1135 unsigned int val;
1137 if (tree == NULL)
1138 return def;
1140 exp_fold_tree_no_dot (tree);
1141 if (!expld.result.valid_p)
1143 if (name != NULL && expld.phase != lang_mark_phase_enum)
1144 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1145 return def;
1148 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1150 unsigned char *dst;
1151 unsigned char *s;
1152 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1153 fill->size = (len + 1) / 2;
1154 dst = fill->data;
1155 s = (unsigned char *) expld.result.str;
1156 val = 0;
1159 unsigned int digit;
1161 digit = *s++ - '0';
1162 if (digit > 9)
1163 digit = (digit - 'A' + '0' + 10) & 0xf;
1164 val <<= 4;
1165 val += digit;
1166 --len;
1167 if ((len & 1) == 0)
1169 *dst++ = val;
1170 val = 0;
1173 while (len != 0);
1175 else
1177 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1178 val = expld.result.value;
1179 fill->data[0] = (val >> 24) & 0xff;
1180 fill->data[1] = (val >> 16) & 0xff;
1181 fill->data[2] = (val >> 8) & 0xff;
1182 fill->data[3] = (val >> 0) & 0xff;
1183 fill->size = 4;
1185 return fill;
1188 bfd_vma
1189 exp_get_abs_int (etree_type *tree, int def, char *name)
1191 if (tree != NULL)
1193 exp_fold_tree_no_dot (tree);
1195 if (expld.result.valid_p)
1197 if (expld.result.section != NULL)
1198 expld.result.value += expld.result.section->vma;
1199 return expld.result.value;
1201 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1203 lineno = tree->type.lineno;
1204 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1207 return def;
1210 static bfd_vma
1211 align_n (bfd_vma value, bfd_vma align)
1213 if (align <= 1)
1214 return value;
1216 value = (value + align - 1) / align;
1217 return value * align;