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"
42 #include "safe-ctype.h"
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
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;
71 exp_print_token (code)
106 { SECTIONS, "SECTIONS" },
107 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
108 { MEMORY, "MEMORY" },
109 { DEFINED, "DEFINED" },
110 { TARGET_K, "TARGET" },
111 { SEARCH_DIR, "SEARCH_DIR" },
115 { SIZEOF, "SIZEOF" },
117 { LOADADDR, "LOADADDR" },
119 { REL, "relocateable" },
120 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
121 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
125 for (idx = ARRAY_SIZE (table); idx--;)
127 if (table[idx].code == code)
129 fprintf (config.map_file, " %s ", table[idx].name);
134 /* Not in table, just print it alone. */
136 fprintf (config.map_file, " %c ", code);
138 fprintf (config.map_file, " <code %d> ", code);
143 etree_value_type *ptr;
145 asection *s = ptr->section->bfd_section;
146 ptr->value += s->vma;
147 ptr->section = abs_output_section;
150 static etree_value_type
154 etree_value_type new;
156 new.section = abs_output_section;
163 lang_output_section_statement_type *os;
168 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
170 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
177 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
178 new->type.node_code = INT;
179 new->value.value = value;
180 new->value.str = NULL;
181 new->type.node_class = etree_value;
186 exp_bigintop (value, str)
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;
198 /* Build an expression representing an unnamed relocateable value. */
201 exp_relop (section, value)
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;
213 static etree_value_type
214 new_rel (value, str, section)
217 lang_output_section_statement_type *section;
219 etree_value_type new;
223 new.section = section;
227 static etree_value_type
228 new_rel_from_section (value, section)
230 lang_output_section_statement_type *section;
232 etree_value_type new;
236 new.section = section;
238 new.value -= section->bfd_section->vma;
243 static etree_value_type
244 fold_binary (tree, current_section, allocation_done, dot, dotp)
246 lang_output_section_statement_type *current_section;
247 lang_phase_type allocation_done;
251 etree_value_type result;
253 result = exp_fold_tree (tree->binary.lhs, current_section,
254 allocation_done, dot, dotp);
257 etree_value_type other;
259 other = exp_fold_tree (tree->binary.rhs,
261 allocation_done, dot, dotp);
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
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 == '-'))
276 etree_value_type hold;
278 /* If there is only one absolute term, make sure it is the
280 if (other.section != abs_output_section)
287 else if (result.section != other.section
288 || current_section == abs_output_section)
294 switch (tree->type.node_code)
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);
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);
310 #define BOP(x,y) case x : result.value = result.value y other.value; break;
329 if (result.value < other.value)
334 if (result.value > other.value)
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))
345 bfd_vma maxpage = result.value;
347 result.value = ALIGN_N (dot, maxpage);
348 if (exp_data_seg.phase != exp_dataseg_adjust)
350 result.value += dot & (maxpage - 1);
351 if (allocation_done == lang_allocating_phase_enum)
353 exp_data_seg.phase = exp_dataseg_align_seen;
354 exp_data_seg.base = result.value;
355 exp_data_seg.pagesize = other.value;
358 else if (other.value < maxpage)
359 result.value += dot & (maxpage - other.value);
362 result.valid_p = false;
371 result.valid_p = false;
381 etree_value_type new;
386 static etree_value_type
387 fold_name (tree, current_section, allocation_done, dot)
389 lang_output_section_statement_type *current_section;
390 lang_phase_type allocation_done;
393 etree_value_type result;
395 switch (tree->type.node_code)
398 if (allocation_done != lang_first_phase_enum)
400 result = new_abs ((bfd_vma)
401 bfd_sizeof_headers (output_bfd,
402 link_info.relocateable));
406 result.valid_p = false;
410 if (allocation_done == lang_first_phase_enum)
411 result.valid_p = false;
414 struct bfd_link_hash_entry *h;
416 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
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));
424 result.valid_p = true;
428 result.valid_p = false;
429 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
431 if (allocation_done != lang_first_phase_enum)
432 result = new_rel_from_section (dot, current_section);
436 else if (allocation_done != lang_first_phase_enum)
438 struct bfd_link_hash_entry *h;
440 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
444 && (h->type == bfd_link_hash_defined
445 || h->type == bfd_link_hash_defweak))
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)
452 asection *output_section;
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"),
460 lang_output_section_statement_type *os;
462 os = (lang_output_section_statement_lookup
463 (bfd_get_section_name (output_bfd,
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),
475 else if (allocation_done == lang_final_phase_enum)
476 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
482 if (allocation_done != lang_first_phase_enum)
484 lang_output_section_statement_type *os;
486 os = lang_output_section_find (tree->name.name);
487 check (os, tree->name.name, "ADDR");
488 result = new_rel (0, NULL, os);
495 if (allocation_done != lang_first_phase_enum)
497 lang_output_section_statement_type *os;
499 os = lang_output_section_find (tree->name.name);
500 check (os, tree->name.name, "LOADADDR");
501 if (os->load_base == NULL)
502 result = new_rel (0, NULL, os);
504 result = exp_fold_tree_no_dot (os->load_base,
513 if (allocation_done != lang_first_phase_enum)
515 int opb = bfd_octets_per_byte (output_bfd);
516 lang_output_section_statement_type *os;
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);
535 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
537 lang_output_section_statement_type *current_section;
538 lang_phase_type allocation_done;
542 etree_value_type result;
546 result.valid_p = false;
550 switch (tree->type.node_class)
553 result = new_rel (tree->value.value, tree->value.str, current_section);
557 if (allocation_done != lang_final_phase_enum)
558 result.valid_p = false;
560 result = new_rel ((tree->rel.value
561 + tree->rel.section->output_section->vma
562 + tree->rel.section->output_offset),
568 result = exp_fold_tree (tree->assert_s.child,
570 allocation_done, dot, dotp);
574 einfo ("%F%P: %s\n", tree->assert_s.message);
580 result = exp_fold_tree (tree->unary.child,
582 allocation_done, dot, dotp);
585 switch (tree->type.node_code)
588 if (allocation_done != lang_first_phase_enum)
589 result = new_rel_from_section (ALIGN_N (dot, result.value),
592 result.valid_p = false;
596 if (allocation_done != lang_first_phase_enum && result.valid_p)
598 result.value += result.section->bfd_section->vma;
599 result.section = abs_output_section;
602 result.valid_p = false;
607 result.value = ~result.value;
612 result.value = !result.value;
617 result.value = -result.value;
621 /* Return next place aligned to value. */
622 if (allocation_done == lang_allocating_phase_enum)
625 result.value = ALIGN_N (dot, result.value);
628 result.valid_p = false;
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))
638 if (exp_data_seg.phase == exp_dataseg_align_seen)
640 exp_data_seg.phase = exp_dataseg_end_seen;
641 exp_data_seg.end = result.value;
645 result.valid_p = false;
656 result = exp_fold_tree (tree->trinary.cond, current_section,
657 allocation_done, dot, dotp);
659 result = exp_fold_tree ((result.value
661 : tree->trinary.rhs),
663 allocation_done, dot, dotp);
667 result = fold_binary (tree, current_section, allocation_done,
674 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
676 /* Assignment to dot can only be done during allocation. */
677 if (tree->type.node_class != etree_assign)
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))
683 result = exp_fold_tree (tree->assign.src,
685 allocation_done, dot,
687 if (! result.valid_p)
688 einfo (_("%F%S invalid assignment to location counter\n"));
691 if (current_section == NULL)
692 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
697 nextdot = (result.value
698 + current_section->bfd_section->vma);
700 && current_section != abs_output_section)
701 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
711 result = exp_fold_tree (tree->assign.src,
712 current_section, allocation_done,
717 struct bfd_link_hash_entry *h;
719 if (tree->type.node_class == etree_assign)
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)
727 if (tree->type.node_class == etree_assign)
728 einfo (_("%P%F:%s: hash creation failed\n"),
731 else if (tree->type.node_class == etree_provide
732 && h->type != bfd_link_hash_undefined
733 && h->type != bfd_link_hash_common)
735 /* Do nothing. The symbol was defined by some
740 /* FIXME: Should we worry if the symbol is already
742 h->type = bfd_link_hash_defined;
743 h->u.def.value = result.value;
744 h->u.def.section = result.section->bfd_section;
745 if (tree->type.node_class == etree_provide)
746 tree->type.node_class = etree_provided;
753 result = fold_name (tree, current_section, allocation_done, dot);
764 static etree_value_type
765 exp_fold_tree_no_dot (tree, current_section, allocation_done)
767 lang_output_section_statement_type *current_section;
768 lang_phase_type allocation_done;
770 return exp_fold_tree (tree, current_section, allocation_done,
771 (bfd_vma) 0, (bfd_vma *) NULL);
775 exp_binop (code, lhs, rhs)
780 etree_type value, *new;
783 value.type.node_code = code;
784 value.binary.lhs = lhs;
785 value.binary.rhs = rhs;
786 value.type.node_class = etree_binary;
787 r = exp_fold_tree_no_dot (&value,
789 lang_first_phase_enum);
792 return exp_intop (r.value);
794 new = (etree_type *) stat_alloc (sizeof (new->binary));
795 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
800 exp_trinop (code, cond, lhs, rhs)
806 etree_type value, *new;
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;
813 r = exp_fold_tree_no_dot (&value,
814 (lang_output_section_statement_type *) NULL,
815 lang_first_phase_enum);
817 return exp_intop (r.value);
819 new = (etree_type *) stat_alloc (sizeof (new->trinary));
820 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
825 exp_unop (code, child)
829 etree_type value, *new;
832 value.unary.type.node_code = code;
833 value.unary.child = child;
834 value.unary.type.node_class = etree_unary;
835 r = exp_fold_tree_no_dot (&value, abs_output_section,
836 lang_first_phase_enum);
838 return exp_intop (r.value);
840 new = (etree_type *) stat_alloc (sizeof (new->unary));
841 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
846 exp_nameop (code, name)
850 etree_type value, *new;
852 value.name.type.node_code = code;
853 value.name.name = name;
854 value.name.type.node_class = etree_name;
856 r = exp_fold_tree_no_dot (&value,
857 (lang_output_section_statement_type *) NULL,
858 lang_first_phase_enum);
860 return exp_intop (r.value);
862 new = (etree_type *) stat_alloc (sizeof (new->name));
863 memcpy ((char *) new, (char *) &value, sizeof (new->name));
869 exp_assop (code, dst, src)
874 etree_type value, *new;
876 value.assign.type.node_code = code;
878 value.assign.src = src;
879 value.assign.dst = dst;
880 value.assign.type.node_class = etree_assign;
883 if (exp_fold_tree_no_dot (&value, &result))
884 return exp_intop (result);
886 new = (etree_type *) stat_alloc (sizeof (new->assign));
887 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
891 /* Handle PROVIDE. */
894 exp_provide (dst, src)
900 n = (etree_type *) stat_alloc (sizeof (n->assign));
901 n->assign.type.node_code = '=';
902 n->assign.type.node_class = etree_provide;
911 exp_assert (exp, message)
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;
926 exp_print_tree (tree)
929 if (config.map_file == NULL)
930 config.map_file = stderr;
934 minfo ("NULL TREE\n");
938 switch (tree->type.node_class)
941 minfo ("0x%v", tree->value.value);
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);
950 if (tree->assign.dst->sdefs != (asymbol *) NULL)
951 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
952 tree->assign.dst->sdefs->value);
954 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
956 fprintf (config.map_file, "%s", tree->assign.dst);
957 exp_print_token (tree->type.node_code);
958 exp_print_tree (tree->assign.src);
962 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
963 exp_print_tree (tree->assign.src);
964 fprintf (config.map_file, ")");
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, ")");
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);
981 exp_print_token (tree->unary.type.node_code);
982 if (tree->unary.child)
984 fprintf (config.map_file, "(");
985 exp_print_tree (tree->unary.child);
986 fprintf (config.map_file, ")");
991 fprintf (config.map_file, "ASSERT (");
992 exp_print_tree (tree->assert_s.child);
993 fprintf (config.map_file, ", %s)", tree->assert_s.message);
997 fprintf (config.map_file, "????????");
1000 if (tree->type.node_code == NAME)
1002 fprintf (config.map_file, "%s", tree->name.name);
1006 exp_print_token (tree->type.node_code);
1007 if (tree->name.name)
1008 fprintf (config.map_file, "(%s)", tree->name.name);
1018 exp_get_vma (tree, def, name, allocation_done)
1022 lang_phase_type allocation_done;
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);
1038 exp_get_value_int (tree, def, name, allocation_done)
1042 lang_phase_type allocation_done;
1044 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1048 exp_get_fill (tree, def, name, allocation_done)
1052 lang_phase_type allocation_done;
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);
1066 if (r.str != NULL && (len = strlen (r.str)) != 0)
1070 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1071 fill->size = (len + 1) / 2;
1081 digit = (digit - 'A' + '0' + 10) & 0xf;
1095 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
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;
1107 exp_get_abs_int (tree, def, name, allocation_done)
1109 int def ATTRIBUTE_UNUSED;
1111 lang_phase_type allocation_done;
1113 etree_value_type res;
1114 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1117 res.value += res.section->bfd_section->vma;
1119 einfo (_("%F%S non constant expression for %s\n"), name);