1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
7 This file is part of GLD, the Gnu Linker.
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)
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.
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
24 /* This module is in charge of working out the contents of expressions.
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
41 #include "libiberty.h"
43 static void exp_print_token PARAMS ((token_code_type code));
44 static void make_abs PARAMS ((etree_value_type *ptr));
45 static etree_value_type new_abs PARAMS ((bfd_vma value));
46 static void check PARAMS ((lang_output_section_statement_type *os,
47 const char *name, const char *op));
48 static etree_value_type new_rel
49 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
50 static etree_value_type new_rel_from_section
51 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
52 static etree_value_type fold_binary
53 PARAMS ((etree_type *tree,
54 lang_output_section_statement_type *current_section,
55 lang_phase_type allocation_done,
56 bfd_vma dot, bfd_vma *dotp));
57 static etree_value_type fold_name
58 PARAMS ((etree_type *tree,
59 lang_output_section_statement_type *current_section,
60 lang_phase_type allocation_done,
62 static etree_value_type exp_fold_tree_no_dot
63 PARAMS ((etree_type *tree,
64 lang_output_section_statement_type *current_section,
65 lang_phase_type allocation_done));
67 struct exp_data_seg exp_data_seg;
70 exp_print_token (code)
105 { SECTIONS, "SECTIONS" },
106 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
107 { MEMORY, "MEMORY" },
108 { DEFINED, "DEFINED" },
109 { TARGET_K, "TARGET" },
110 { SEARCH_DIR, "SEARCH_DIR" },
114 { SIZEOF, "SIZEOF" },
116 { LOADADDR, "LOADADDR" },
118 { REL, "relocateable" },
119 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
120 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
124 for (idx = ARRAY_SIZE (table); idx--;)
126 if (table[idx].code == code)
128 fprintf (config.map_file, " %s ", table[idx].name);
133 /* Not in table, just print it alone. */
135 fprintf (config.map_file, " %c ", code);
137 fprintf (config.map_file, " <code %d> ", code);
142 etree_value_type *ptr;
144 asection *s = ptr->section->bfd_section;
145 ptr->value += s->vma;
146 ptr->section = abs_output_section;
149 static etree_value_type
153 etree_value_type new;
155 new.section = abs_output_section;
162 lang_output_section_statement_type *os;
167 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
169 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
176 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
177 new->type.node_code = INT;
178 new->value.value = value;
179 new->type.node_class = etree_value;
184 /* Build an expression representing an unnamed relocateable value. */
187 exp_relop (section, value)
191 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
192 new->type.node_code = REL;
193 new->type.node_class = etree_rel;
194 new->rel.section = section;
195 new->rel.value = value;
199 static etree_value_type
200 new_rel (value, section)
202 lang_output_section_statement_type *section;
204 etree_value_type new;
207 new.section = section;
211 static etree_value_type
212 new_rel_from_section (value, section)
214 lang_output_section_statement_type *section;
216 etree_value_type new;
219 new.section = section;
221 new.value -= section->bfd_section->vma;
226 static etree_value_type
227 fold_binary (tree, current_section, allocation_done, dot, dotp)
229 lang_output_section_statement_type *current_section;
230 lang_phase_type allocation_done;
234 etree_value_type result;
236 result = exp_fold_tree (tree->binary.lhs, current_section,
237 allocation_done, dot, dotp);
240 etree_value_type other;
242 other = exp_fold_tree (tree->binary.rhs,
244 allocation_done, dot, dotp);
247 /* If the values are from different sections, or this is an
248 absolute expression, make both the source arguments
249 absolute. However, adding or subtracting an absolute
250 value from a relative value is meaningful, and is an
252 if (current_section != abs_output_section
253 && (other.section == abs_output_section
254 || (result.section == abs_output_section
255 && tree->type.node_code == '+'))
256 && (tree->type.node_code == '+'
257 || tree->type.node_code == '-'))
259 etree_value_type hold;
261 /* If there is only one absolute term, make sure it is the
263 if (other.section != abs_output_section)
270 else if (result.section != other.section
271 || current_section == abs_output_section)
277 switch (tree->type.node_code)
280 if (other.value == 0)
281 einfo (_("%F%S %% by zero\n"));
282 result.value = ((bfd_signed_vma) result.value
283 % (bfd_signed_vma) other.value);
287 if (other.value == 0)
288 einfo (_("%F%S / by zero\n"));
289 result.value = ((bfd_signed_vma) result.value
290 / (bfd_signed_vma) other.value);
293 #define BOP(x,y) case x : result.value = result.value y other.value; break;
312 if (result.value < other.value)
317 if (result.value > other.value)
321 case DATA_SEGMENT_ALIGN:
322 if (allocation_done != lang_first_phase_enum
323 && current_section == abs_output_section
324 && (exp_data_seg.phase == exp_dataseg_none
325 || exp_data_seg.phase == exp_dataseg_adjust
326 || allocation_done != lang_allocating_phase_enum))
328 bfd_vma maxpage = result.value;
330 result.value = ALIGN_N (dot, maxpage);
331 if (exp_data_seg.phase != exp_dataseg_adjust)
333 result.value += dot & (maxpage - 1);
334 if (allocation_done == lang_allocating_phase_enum)
336 exp_data_seg.phase = exp_dataseg_align_seen;
337 exp_data_seg.base = result.value;
338 exp_data_seg.pagesize = other.value;
341 else if (other.value < maxpage)
342 result.value += dot & (maxpage - other.value);
345 result.valid_p = false;
354 result.valid_p = false;
364 etree_value_type new;
369 static etree_value_type
370 fold_name (tree, current_section, allocation_done, dot)
372 lang_output_section_statement_type *current_section;
373 lang_phase_type allocation_done;
376 etree_value_type result;
378 switch (tree->type.node_code)
381 if (allocation_done != lang_first_phase_enum)
383 result = new_abs ((bfd_vma)
384 bfd_sizeof_headers (output_bfd,
385 link_info.relocateable));
389 result.valid_p = false;
393 if (allocation_done == lang_first_phase_enum)
394 result.valid_p = false;
397 struct bfd_link_hash_entry *h;
399 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
402 result.value = (h != (struct bfd_link_hash_entry *) NULL
403 && (h->type == bfd_link_hash_defined
404 || h->type == bfd_link_hash_defweak
405 || h->type == bfd_link_hash_common));
407 result.valid_p = true;
411 result.valid_p = false;
412 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
414 if (allocation_done != lang_first_phase_enum)
415 result = new_rel_from_section (dot, current_section);
419 else if (allocation_done != lang_first_phase_enum)
421 struct bfd_link_hash_entry *h;
423 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
427 && (h->type == bfd_link_hash_defined
428 || h->type == bfd_link_hash_defweak))
430 if (bfd_is_abs_section (h->u.def.section))
431 result = new_abs (h->u.def.value);
432 else if (allocation_done == lang_final_phase_enum
433 || allocation_done == lang_allocating_phase_enum)
435 asection *output_section;
437 output_section = h->u.def.section->output_section;
438 if (output_section == NULL)
439 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
443 lang_output_section_statement_type *os;
445 os = (lang_output_section_statement_lookup
446 (bfd_get_section_name (output_bfd,
449 /* FIXME: Is this correct if this section is
450 being linked with -R? */
451 result = new_rel ((h->u.def.value
452 + h->u.def.section->output_offset),
457 else if (allocation_done == lang_final_phase_enum)
458 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
464 if (allocation_done != lang_first_phase_enum)
466 lang_output_section_statement_type *os;
468 os = lang_output_section_find (tree->name.name);
469 check (os, tree->name.name, "ADDR");
470 result = new_rel (0, os);
477 if (allocation_done != lang_first_phase_enum)
479 lang_output_section_statement_type *os;
481 os = lang_output_section_find (tree->name.name);
482 check (os, tree->name.name, "LOADADDR");
483 if (os->load_base == NULL)
484 result = new_rel (0, os);
486 result = exp_fold_tree_no_dot (os->load_base,
495 if (allocation_done != lang_first_phase_enum)
497 int opb = bfd_octets_per_byte (output_bfd);
498 lang_output_section_statement_type *os;
500 os = lang_output_section_find (tree->name.name);
501 check (os, tree->name.name, "SIZEOF");
502 result = new_abs (os->bfd_section->_raw_size / opb);
517 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
519 lang_output_section_statement_type *current_section;
520 lang_phase_type allocation_done;
524 etree_value_type result;
528 result.valid_p = false;
532 switch (tree->type.node_class)
535 result = new_rel (tree->value.value, current_section);
539 if (allocation_done != lang_final_phase_enum)
540 result.valid_p = false;
542 result = new_rel ((tree->rel.value
543 + tree->rel.section->output_section->vma
544 + tree->rel.section->output_offset),
549 result = exp_fold_tree (tree->assert_s.child,
551 allocation_done, dot, dotp);
555 einfo ("%F%P: %s\n", tree->assert_s.message);
561 result = exp_fold_tree (tree->unary.child,
563 allocation_done, dot, dotp);
566 switch (tree->type.node_code)
569 if (allocation_done != lang_first_phase_enum)
570 result = new_rel_from_section (ALIGN_N (dot, result.value),
573 result.valid_p = false;
577 if (allocation_done != lang_first_phase_enum && result.valid_p)
579 result.value += result.section->bfd_section->vma;
580 result.section = abs_output_section;
583 result.valid_p = false;
588 result.value = ~result.value;
593 result.value = !result.value;
598 result.value = -result.value;
602 /* Return next place aligned to value. */
603 if (allocation_done == lang_allocating_phase_enum)
606 result.value = ALIGN_N (dot, result.value);
609 result.valid_p = false;
612 case DATA_SEGMENT_END:
613 if (allocation_done != lang_first_phase_enum
614 && current_section == abs_output_section
615 && (exp_data_seg.phase == exp_dataseg_align_seen
616 || exp_data_seg.phase == exp_dataseg_adjust
617 || allocation_done != lang_allocating_phase_enum))
619 if (exp_data_seg.phase == exp_dataseg_align_seen)
621 exp_data_seg.phase = exp_dataseg_end_seen;
622 exp_data_seg.end = result.value;
626 result.valid_p = false;
637 result = exp_fold_tree (tree->trinary.cond, current_section,
638 allocation_done, dot, dotp);
640 result = exp_fold_tree ((result.value
642 : tree->trinary.rhs),
644 allocation_done, dot, dotp);
648 result = fold_binary (tree, current_section, allocation_done,
655 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
657 /* Assignment to dot can only be done during allocation. */
658 if (tree->type.node_class != etree_assign)
659 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
660 if (allocation_done == lang_allocating_phase_enum
661 || (allocation_done == lang_final_phase_enum
662 && current_section == abs_output_section))
664 result = exp_fold_tree (tree->assign.src,
666 allocation_done, dot,
668 if (! result.valid_p)
669 einfo (_("%F%S invalid assignment to location counter\n"));
672 if (current_section == NULL)
673 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
678 nextdot = (result.value
679 + current_section->bfd_section->vma);
681 && current_section != abs_output_section)
682 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
692 result = exp_fold_tree (tree->assign.src,
693 current_section, allocation_done,
698 struct bfd_link_hash_entry *h;
700 if (tree->type.node_class == etree_assign)
704 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
705 create, false, false);
706 if (h == (struct bfd_link_hash_entry *) NULL)
708 if (tree->type.node_class == etree_assign)
709 einfo (_("%P%F:%s: hash creation failed\n"),
712 else if (tree->type.node_class == etree_provide
713 && h->type != bfd_link_hash_undefined
714 && h->type != bfd_link_hash_common)
716 /* Do nothing. The symbol was defined by some
721 /* FIXME: Should we worry if the symbol is already
723 h->type = bfd_link_hash_defined;
724 h->u.def.value = result.value;
725 h->u.def.section = result.section->bfd_section;
726 if (tree->type.node_class == etree_provide)
727 tree->type.node_class = etree_provided;
734 result = fold_name (tree, current_section, allocation_done, dot);
745 static etree_value_type
746 exp_fold_tree_no_dot (tree, current_section, allocation_done)
748 lang_output_section_statement_type *current_section;
749 lang_phase_type allocation_done;
751 return exp_fold_tree (tree, current_section, allocation_done,
752 (bfd_vma) 0, (bfd_vma *) NULL);
756 exp_binop (code, lhs, rhs)
761 etree_type value, *new;
764 value.type.node_code = code;
765 value.binary.lhs = lhs;
766 value.binary.rhs = rhs;
767 value.type.node_class = etree_binary;
768 r = exp_fold_tree_no_dot (&value,
770 lang_first_phase_enum);
773 return exp_intop (r.value);
775 new = (etree_type *) stat_alloc (sizeof (new->binary));
776 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
781 exp_trinop (code, cond, lhs, rhs)
787 etree_type value, *new;
789 value.type.node_code = code;
790 value.trinary.lhs = lhs;
791 value.trinary.cond = cond;
792 value.trinary.rhs = rhs;
793 value.type.node_class = etree_trinary;
794 r = exp_fold_tree_no_dot (&value,
795 (lang_output_section_statement_type *) NULL,
796 lang_first_phase_enum);
798 return exp_intop (r.value);
800 new = (etree_type *) stat_alloc (sizeof (new->trinary));
801 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
806 exp_unop (code, child)
810 etree_type value, *new;
813 value.unary.type.node_code = code;
814 value.unary.child = child;
815 value.unary.type.node_class = etree_unary;
816 r = exp_fold_tree_no_dot (&value, abs_output_section,
817 lang_first_phase_enum);
819 return exp_intop (r.value);
821 new = (etree_type *) stat_alloc (sizeof (new->unary));
822 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
827 exp_nameop (code, name)
831 etree_type value, *new;
833 value.name.type.node_code = code;
834 value.name.name = name;
835 value.name.type.node_class = etree_name;
837 r = exp_fold_tree_no_dot (&value,
838 (lang_output_section_statement_type *) NULL,
839 lang_first_phase_enum);
841 return exp_intop (r.value);
843 new = (etree_type *) stat_alloc (sizeof (new->name));
844 memcpy ((char *) new, (char *) &value, sizeof (new->name));
850 exp_assop (code, dst, src)
855 etree_type value, *new;
857 value.assign.type.node_code = code;
859 value.assign.src = src;
860 value.assign.dst = dst;
861 value.assign.type.node_class = etree_assign;
864 if (exp_fold_tree_no_dot (&value, &result))
865 return exp_intop (result);
867 new = (etree_type *) stat_alloc (sizeof (new->assign));
868 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
872 /* Handle PROVIDE. */
875 exp_provide (dst, src)
881 n = (etree_type *) stat_alloc (sizeof (n->assign));
882 n->assign.type.node_code = '=';
883 n->assign.type.node_class = etree_provide;
892 exp_assert (exp, message)
898 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
899 n->assert_s.type.node_code = '!';
900 n->assert_s.type.node_class = etree_assert;
901 n->assert_s.child = exp;
902 n->assert_s.message = message;
907 exp_print_tree (tree)
910 if (config.map_file == NULL)
911 config.map_file = stderr;
915 minfo ("NULL TREE\n");
919 switch (tree->type.node_class)
922 minfo ("0x%v", tree->value.value);
925 if (tree->rel.section->owner != NULL)
926 minfo ("%B:", tree->rel.section->owner);
927 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
931 if (tree->assign.dst->sdefs != (asymbol *) NULL)
932 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
933 tree->assign.dst->sdefs->value);
935 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
937 fprintf (config.map_file, "%s", tree->assign.dst);
938 exp_print_token (tree->type.node_code);
939 exp_print_tree (tree->assign.src);
943 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
944 exp_print_tree (tree->assign.src);
945 fprintf (config.map_file, ")");
948 fprintf (config.map_file, "(");
949 exp_print_tree (tree->binary.lhs);
950 exp_print_token (tree->type.node_code);
951 exp_print_tree (tree->binary.rhs);
952 fprintf (config.map_file, ")");
955 exp_print_tree (tree->trinary.cond);
956 fprintf (config.map_file, "?");
957 exp_print_tree (tree->trinary.lhs);
958 fprintf (config.map_file, ":");
959 exp_print_tree (tree->trinary.rhs);
962 exp_print_token (tree->unary.type.node_code);
963 if (tree->unary.child)
965 fprintf (config.map_file, "(");
966 exp_print_tree (tree->unary.child);
967 fprintf (config.map_file, ")");
972 fprintf (config.map_file, "ASSERT (");
973 exp_print_tree (tree->assert_s.child);
974 fprintf (config.map_file, ", %s)", tree->assert_s.message);
978 fprintf (config.map_file, "????????");
981 if (tree->type.node_code == NAME)
983 fprintf (config.map_file, "%s", tree->name.name);
987 exp_print_token (tree->type.node_code);
989 fprintf (config.map_file, "(%s)", tree->name.name);
999 exp_get_vma (tree, def, name, allocation_done)
1003 lang_phase_type allocation_done;
1009 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1010 if (! r.valid_p && name != NULL)
1011 einfo (_("%F%S nonconstant expression for %s\n"), name);
1019 exp_get_value_int (tree, def, name, allocation_done)
1023 lang_phase_type allocation_done;
1025 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1029 exp_get_abs_int (tree, def, name, allocation_done)
1031 int def ATTRIBUTE_UNUSED;
1033 lang_phase_type allocation_done;
1035 etree_value_type res;
1036 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1039 res.value += res.section->bfd_section->vma;
1041 einfo (_("%F%S non constant expression for %s\n"), name);