1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
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"
42 #include "safe-ctype.h"
44 static void exp_print_token PARAMS ((token_code_type code, int infix_p));
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
50 PARAMS ((bfd_vma, char *, lang_output_section_statement_type *section));
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,
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));
68 struct exp_data_seg exp_data_seg;
70 /* Print the string representation of the given token. Surround it
71 with spaces if INFIX_P is true. */
74 exp_print_token (code, infix_p)
103 { ALIGN_K, "ALIGN" },
110 { SECTIONS, "SECTIONS" },
111 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
112 { MEMORY, "MEMORY" },
113 { DEFINED, "DEFINED" },
114 { TARGET_K, "TARGET" },
115 { SEARCH_DIR, "SEARCH_DIR" },
119 { SIZEOF, "SIZEOF" },
121 { LOADADDR, "LOADADDR" },
123 { REL, "relocateable" },
124 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
125 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
129 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
130 if (table[idx].code == code)
134 fputc (' ', config.map_file);
136 if (idx < ARRAY_SIZE (table))
137 fputs (table[idx].name, config.map_file);
139 fputc (code, config.map_file);
141 fprintf (config.map_file, "<code %d>", code);
144 fputc (' ', config.map_file);
149 etree_value_type *ptr;
151 asection *s = ptr->section->bfd_section;
152 ptr->value += s->vma;
153 ptr->section = abs_output_section;
156 static etree_value_type
160 etree_value_type new;
162 new.section = abs_output_section;
169 lang_output_section_statement_type *os;
174 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
176 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
183 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
184 new->type.node_code = INT;
185 new->value.value = value;
186 new->value.str = NULL;
187 new->type.node_class = etree_value;
192 exp_bigintop (value, str)
196 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
197 new->type.node_code = INT;
198 new->value.value = value;
199 new->value.str = str;
200 new->type.node_class = etree_value;
204 /* Build an expression representing an unnamed relocateable value. */
207 exp_relop (section, value)
211 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
212 new->type.node_code = REL;
213 new->type.node_class = etree_rel;
214 new->rel.section = section;
215 new->rel.value = value;
219 static etree_value_type
220 new_rel (value, str, section)
223 lang_output_section_statement_type *section;
225 etree_value_type new;
229 new.section = section;
233 static etree_value_type
234 new_rel_from_section (value, section)
236 lang_output_section_statement_type *section;
238 etree_value_type new;
242 new.section = section;
244 new.value -= section->bfd_section->vma;
249 static etree_value_type
250 fold_binary (tree, current_section, allocation_done, dot, dotp)
252 lang_output_section_statement_type *current_section;
253 lang_phase_type allocation_done;
257 etree_value_type result;
259 result = exp_fold_tree (tree->binary.lhs, current_section,
260 allocation_done, dot, dotp);
263 etree_value_type other;
265 other = exp_fold_tree (tree->binary.rhs,
267 allocation_done, dot, dotp);
270 /* If the values are from different sections, or this is an
271 absolute expression, make both the source arguments
272 absolute. However, adding or subtracting an absolute
273 value from a relative value is meaningful, and is an
275 if (current_section != abs_output_section
276 && (other.section == abs_output_section
277 || (result.section == abs_output_section
278 && tree->type.node_code == '+'))
279 && (tree->type.node_code == '+'
280 || tree->type.node_code == '-'))
282 etree_value_type hold;
284 /* If there is only one absolute term, make sure it is the
286 if (other.section != abs_output_section)
293 else if (result.section != other.section
294 || current_section == abs_output_section)
300 switch (tree->type.node_code)
303 if (other.value == 0)
304 einfo (_("%F%S %% by zero\n"));
305 result.value = ((bfd_signed_vma) result.value
306 % (bfd_signed_vma) other.value);
310 if (other.value == 0)
311 einfo (_("%F%S / by zero\n"));
312 result.value = ((bfd_signed_vma) result.value
313 / (bfd_signed_vma) other.value);
316 #define BOP(x,y) case x : result.value = result.value y other.value; break;
335 if (result.value < other.value)
340 if (result.value > other.value)
344 case DATA_SEGMENT_ALIGN:
345 if (allocation_done != lang_first_phase_enum
346 && current_section == abs_output_section
347 && (exp_data_seg.phase == exp_dataseg_none
348 || exp_data_seg.phase == exp_dataseg_adjust
349 || allocation_done != lang_allocating_phase_enum))
351 bfd_vma maxpage = result.value;
353 result.value = align_n (dot, maxpage);
354 if (exp_data_seg.phase != exp_dataseg_adjust)
356 result.value += dot & (maxpage - 1);
357 if (allocation_done == lang_allocating_phase_enum)
359 exp_data_seg.phase = exp_dataseg_align_seen;
360 exp_data_seg.base = result.value;
361 exp_data_seg.pagesize = other.value;
364 else if (other.value < maxpage)
365 result.value += (dot + other.value - 1)
366 & (maxpage - other.value);
369 result.valid_p = false;
378 result.valid_p = false;
388 etree_value_type new;
393 static etree_value_type
394 fold_name (tree, current_section, allocation_done, dot)
396 lang_output_section_statement_type *current_section;
397 lang_phase_type allocation_done;
400 etree_value_type result;
402 switch (tree->type.node_code)
405 if (allocation_done != lang_first_phase_enum)
407 result = new_abs ((bfd_vma)
408 bfd_sizeof_headers (output_bfd,
409 link_info.relocateable));
413 result.valid_p = false;
417 if (allocation_done == lang_first_phase_enum)
418 result.valid_p = false;
421 struct bfd_link_hash_entry *h;
423 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
426 result.value = (h != (struct bfd_link_hash_entry *) NULL
427 && (h->type == bfd_link_hash_defined
428 || h->type == bfd_link_hash_defweak
429 || h->type == bfd_link_hash_common));
431 result.valid_p = true;
435 result.valid_p = false;
436 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
438 if (allocation_done != lang_first_phase_enum)
439 result = new_rel_from_section (dot, current_section);
443 else if (allocation_done != lang_first_phase_enum)
445 struct bfd_link_hash_entry *h;
447 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
451 && (h->type == bfd_link_hash_defined
452 || h->type == bfd_link_hash_defweak))
454 if (bfd_is_abs_section (h->u.def.section))
455 result = new_abs (h->u.def.value);
456 else if (allocation_done == lang_final_phase_enum
457 || allocation_done == lang_allocating_phase_enum)
459 asection *output_section;
461 output_section = h->u.def.section->output_section;
462 if (output_section == NULL)
463 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
467 lang_output_section_statement_type *os;
469 os = (lang_output_section_statement_lookup
470 (bfd_get_section_name (output_bfd,
473 /* FIXME: Is this correct if this section is
474 being linked with -R? */
475 result = new_rel ((h->u.def.value
476 + h->u.def.section->output_offset),
482 else if (allocation_done == lang_final_phase_enum)
483 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
489 if (allocation_done != lang_first_phase_enum)
491 lang_output_section_statement_type *os;
493 os = lang_output_section_find (tree->name.name);
494 check (os, tree->name.name, "ADDR");
495 result = new_rel (0, NULL, os);
502 if (allocation_done != lang_first_phase_enum)
504 lang_output_section_statement_type *os;
506 os = lang_output_section_find (tree->name.name);
507 check (os, tree->name.name, "LOADADDR");
508 if (os->load_base == NULL)
509 result = new_rel (0, NULL, os);
511 result = exp_fold_tree_no_dot (os->load_base,
520 if (allocation_done != lang_first_phase_enum)
522 int opb = bfd_octets_per_byte (output_bfd);
523 lang_output_section_statement_type *os;
525 os = lang_output_section_find (tree->name.name);
526 check (os, tree->name.name, "SIZEOF");
527 result = new_abs (os->bfd_section->_raw_size / opb);
542 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
544 lang_output_section_statement_type *current_section;
545 lang_phase_type allocation_done;
549 etree_value_type result;
553 result.valid_p = false;
557 switch (tree->type.node_class)
560 result = new_rel (tree->value.value, tree->value.str, current_section);
564 if (allocation_done != lang_final_phase_enum)
565 result.valid_p = false;
567 result = new_rel ((tree->rel.value
568 + tree->rel.section->output_section->vma
569 + tree->rel.section->output_offset),
575 result = exp_fold_tree (tree->assert_s.child,
577 allocation_done, dot, dotp);
581 einfo ("%F%P: %s\n", tree->assert_s.message);
587 result = exp_fold_tree (tree->unary.child,
589 allocation_done, dot, dotp);
592 switch (tree->type.node_code)
595 if (allocation_done != lang_first_phase_enum)
596 result = new_rel_from_section (align_n (dot, result.value),
599 result.valid_p = false;
603 if (allocation_done != lang_first_phase_enum)
605 result.value += result.section->bfd_section->vma;
606 result.section = abs_output_section;
609 result.valid_p = false;
614 result.value = ~result.value;
619 result.value = !result.value;
624 result.value = -result.value;
628 /* Return next place aligned to value. */
629 if (allocation_done == lang_allocating_phase_enum)
632 result.value = align_n (dot, result.value);
635 result.valid_p = false;
638 case DATA_SEGMENT_END:
639 if (allocation_done != lang_first_phase_enum
640 && current_section == abs_output_section
641 && (exp_data_seg.phase == exp_dataseg_align_seen
642 || exp_data_seg.phase == exp_dataseg_adjust
643 || allocation_done != lang_allocating_phase_enum))
645 if (exp_data_seg.phase == exp_dataseg_align_seen)
647 exp_data_seg.phase = exp_dataseg_end_seen;
648 exp_data_seg.end = result.value;
652 result.valid_p = false;
663 result = exp_fold_tree (tree->trinary.cond, current_section,
664 allocation_done, dot, dotp);
666 result = exp_fold_tree ((result.value
668 : tree->trinary.rhs),
670 allocation_done, dot, dotp);
674 result = fold_binary (tree, current_section, allocation_done,
681 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
683 /* Assignment to dot can only be done during allocation. */
684 if (tree->type.node_class != etree_assign)
685 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
686 if (allocation_done == lang_allocating_phase_enum
687 || (allocation_done == lang_final_phase_enum
688 && current_section == abs_output_section))
690 result = exp_fold_tree (tree->assign.src,
692 allocation_done, dot,
694 if (! result.valid_p)
695 einfo (_("%F%S invalid assignment to location counter\n"));
698 if (current_section == NULL)
699 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
704 nextdot = (result.value
705 + current_section->bfd_section->vma);
707 && current_section != abs_output_section)
708 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
718 result = exp_fold_tree (tree->assign.src,
719 current_section, allocation_done,
724 struct bfd_link_hash_entry *h;
726 if (tree->type.node_class == etree_assign)
730 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
731 create, false, false);
732 if (h == (struct bfd_link_hash_entry *) NULL)
734 if (tree->type.node_class == etree_assign)
735 einfo (_("%P%F:%s: hash creation failed\n"),
738 else if (tree->type.node_class == etree_provide
739 && h->type != bfd_link_hash_undefined
740 && h->type != bfd_link_hash_common)
742 /* Do nothing. The symbol was defined by some
747 /* FIXME: Should we worry if the symbol is already
749 h->type = bfd_link_hash_defined;
750 h->u.def.value = result.value;
751 h->u.def.section = result.section->bfd_section;
752 if (tree->type.node_class == etree_provide)
753 tree->type.node_class = etree_provided;
760 result = fold_name (tree, current_section, allocation_done, dot);
771 static etree_value_type
772 exp_fold_tree_no_dot (tree, current_section, allocation_done)
774 lang_output_section_statement_type *current_section;
775 lang_phase_type allocation_done;
777 return exp_fold_tree (tree, current_section, allocation_done,
778 (bfd_vma) 0, (bfd_vma *) NULL);
782 exp_binop (code, lhs, rhs)
787 etree_type value, *new;
790 value.type.node_code = code;
791 value.binary.lhs = lhs;
792 value.binary.rhs = rhs;
793 value.type.node_class = etree_binary;
794 r = exp_fold_tree_no_dot (&value,
796 lang_first_phase_enum);
799 return exp_intop (r.value);
801 new = (etree_type *) stat_alloc (sizeof (new->binary));
802 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
807 exp_trinop (code, cond, lhs, rhs)
813 etree_type value, *new;
815 value.type.node_code = code;
816 value.trinary.lhs = lhs;
817 value.trinary.cond = cond;
818 value.trinary.rhs = rhs;
819 value.type.node_class = etree_trinary;
820 r = exp_fold_tree_no_dot (&value,
821 (lang_output_section_statement_type *) NULL,
822 lang_first_phase_enum);
824 return exp_intop (r.value);
826 new = (etree_type *) stat_alloc (sizeof (new->trinary));
827 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
832 exp_unop (code, child)
836 etree_type value, *new;
839 value.unary.type.node_code = code;
840 value.unary.child = child;
841 value.unary.type.node_class = etree_unary;
842 r = exp_fold_tree_no_dot (&value, abs_output_section,
843 lang_first_phase_enum);
845 return exp_intop (r.value);
847 new = (etree_type *) stat_alloc (sizeof (new->unary));
848 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
853 exp_nameop (code, name)
857 etree_type value, *new;
859 value.name.type.node_code = code;
860 value.name.name = name;
861 value.name.type.node_class = etree_name;
863 r = exp_fold_tree_no_dot (&value,
864 (lang_output_section_statement_type *) NULL,
865 lang_first_phase_enum);
867 return exp_intop (r.value);
869 new = (etree_type *) stat_alloc (sizeof (new->name));
870 memcpy ((char *) new, (char *) &value, sizeof (new->name));
876 exp_assop (code, dst, src)
881 etree_type value, *new;
883 value.assign.type.node_code = code;
885 value.assign.src = src;
886 value.assign.dst = dst;
887 value.assign.type.node_class = etree_assign;
890 if (exp_fold_tree_no_dot (&value, &result))
891 return exp_intop (result);
893 new = (etree_type *) stat_alloc (sizeof (new->assign));
894 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
898 /* Handle PROVIDE. */
901 exp_provide (dst, src)
907 n = (etree_type *) stat_alloc (sizeof (n->assign));
908 n->assign.type.node_code = '=';
909 n->assign.type.node_class = etree_provide;
918 exp_assert (exp, message)
924 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
925 n->assert_s.type.node_code = '!';
926 n->assert_s.type.node_class = etree_assert;
927 n->assert_s.child = exp;
928 n->assert_s.message = message;
933 exp_print_tree (tree)
936 if (config.map_file == NULL)
937 config.map_file = stderr;
941 minfo ("NULL TREE\n");
945 switch (tree->type.node_class)
948 minfo ("0x%v", tree->value.value);
951 if (tree->rel.section->owner != NULL)
952 minfo ("%B:", tree->rel.section->owner);
953 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
957 if (tree->assign.dst->sdefs != (asymbol *) NULL)
958 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
959 tree->assign.dst->sdefs->value);
961 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
963 fprintf (config.map_file, "%s", tree->assign.dst);
964 exp_print_token (tree->type.node_code, true);
965 exp_print_tree (tree->assign.src);
969 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
970 exp_print_tree (tree->assign.src);
971 fprintf (config.map_file, ")");
974 fprintf (config.map_file, "(");
975 exp_print_tree (tree->binary.lhs);
976 exp_print_token (tree->type.node_code, true);
977 exp_print_tree (tree->binary.rhs);
978 fprintf (config.map_file, ")");
981 exp_print_tree (tree->trinary.cond);
982 fprintf (config.map_file, "?");
983 exp_print_tree (tree->trinary.lhs);
984 fprintf (config.map_file, ":");
985 exp_print_tree (tree->trinary.rhs);
988 exp_print_token (tree->unary.type.node_code, false);
989 if (tree->unary.child)
991 fprintf (config.map_file, " (");
992 exp_print_tree (tree->unary.child);
993 fprintf (config.map_file, ")");
998 fprintf (config.map_file, "ASSERT (");
999 exp_print_tree (tree->assert_s.child);
1000 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1004 fprintf (config.map_file, "????????");
1007 if (tree->type.node_code == NAME)
1009 fprintf (config.map_file, "%s", tree->name.name);
1013 exp_print_token (tree->type.node_code, false);
1014 if (tree->name.name)
1015 fprintf (config.map_file, " (%s)", tree->name.name);
1025 exp_get_vma (tree, def, name, allocation_done)
1029 lang_phase_type allocation_done;
1035 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1036 if (! r.valid_p && name != NULL)
1037 einfo (_("%F%S nonconstant expression for %s\n"), name);
1045 exp_get_value_int (tree, def, name, allocation_done)
1049 lang_phase_type allocation_done;
1051 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1055 exp_get_fill (tree, def, name, allocation_done)
1059 lang_phase_type allocation_done;
1069 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1070 if (! r.valid_p && name != NULL)
1071 einfo (_("%F%S nonconstant expression for %s\n"), name);
1073 if (r.str != NULL && (len = strlen (r.str)) != 0)
1077 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1078 fill->size = (len + 1) / 2;
1088 digit = (digit - 'A' + '0' + 10) & 0xf;
1102 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1104 fill->data[0] = (val >> 24) & 0xff;
1105 fill->data[1] = (val >> 16) & 0xff;
1106 fill->data[2] = (val >> 8) & 0xff;
1107 fill->data[3] = (val >> 0) & 0xff;
1114 exp_get_abs_int (tree, def, name, allocation_done)
1116 int def ATTRIBUTE_UNUSED;
1118 lang_phase_type allocation_done;
1120 etree_value_type res;
1121 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1124 res.value += res.section->bfd_section->vma;
1126 einfo (_("%F%S non constant expression for %s\n"), name);
1131 bfd_vma align_n (value, align)
1138 value = (value + align - 1) / align;
1139 return value * align;