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
45 PARAMS ((token_code_type code, int infix_p));
47 PARAMS ((etree_value_type *ptr));
48 static etree_value_type new_abs
49 PARAMS ((bfd_vma value));
51 PARAMS ((lang_output_section_statement_type *os, const char *name,
53 static etree_value_type new_rel
54 PARAMS ((bfd_vma, char *, lang_output_section_statement_type *section));
55 static etree_value_type new_rel_from_section
56 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
57 static etree_value_type fold_unary
58 PARAMS ((etree_type *tree,
59 lang_output_section_statement_type *current_section,
60 lang_phase_type allocation_done,
61 bfd_vma dot, bfd_vma *dotp));
62 static etree_value_type fold_binary
63 PARAMS ((etree_type *tree,
64 lang_output_section_statement_type *current_section,
65 lang_phase_type allocation_done,
66 bfd_vma dot, bfd_vma *dotp));
67 static etree_value_type fold_trinary
68 PARAMS ((etree_type *tree,
69 lang_output_section_statement_type *current_section,
70 lang_phase_type allocation_done,
71 bfd_vma dot, bfd_vma *dotp));
72 static etree_value_type fold_name
73 PARAMS ((etree_type *tree,
74 lang_output_section_statement_type *current_section,
75 lang_phase_type allocation_done,
77 static etree_value_type exp_fold_tree_no_dot
78 PARAMS ((etree_type *tree,
79 lang_output_section_statement_type *current_section,
80 lang_phase_type allocation_done));
82 struct exp_data_seg exp_data_seg;
84 /* Print the string representation of the given token. Surround it
85 with spaces if INFIX_P is TRUE. */
88 exp_print_token (code, infix_p)
117 { ALIGN_K, "ALIGN" },
124 { SECTIONS, "SECTIONS" },
125 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
126 { MEMORY, "MEMORY" },
127 { DEFINED, "DEFINED" },
128 { TARGET_K, "TARGET" },
129 { SEARCH_DIR, "SEARCH_DIR" },
133 { SIZEOF, "SIZEOF" },
135 { LOADADDR, "LOADADDR" },
137 { REL, "relocateable" },
138 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
139 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
143 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
144 if (table[idx].code == code)
148 fputc (' ', config.map_file);
150 if (idx < ARRAY_SIZE (table))
151 fputs (table[idx].name, config.map_file);
153 fputc (code, config.map_file);
155 fprintf (config.map_file, "<code %d>", code);
158 fputc (' ', config.map_file);
163 etree_value_type *ptr;
165 asection *s = ptr->section->bfd_section;
166 ptr->value += s->vma;
167 ptr->section = abs_output_section;
170 static etree_value_type
174 etree_value_type new;
176 new.section = abs_output_section;
183 lang_output_section_statement_type *os;
188 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
190 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
197 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
198 new->type.node_code = INT;
199 new->value.value = value;
200 new->value.str = NULL;
201 new->type.node_class = etree_value;
206 exp_bigintop (value, str)
210 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
211 new->type.node_code = INT;
212 new->value.value = value;
213 new->value.str = str;
214 new->type.node_class = etree_value;
218 /* Build an expression representing an unnamed relocateable value. */
221 exp_relop (section, value)
225 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
226 new->type.node_code = REL;
227 new->type.node_class = etree_rel;
228 new->rel.section = section;
229 new->rel.value = value;
233 static etree_value_type
234 new_rel (value, str, section)
237 lang_output_section_statement_type *section;
239 etree_value_type new;
243 new.section = section;
247 static etree_value_type
248 new_rel_from_section (value, section)
250 lang_output_section_statement_type *section;
252 etree_value_type new;
256 new.section = section;
258 new.value -= section->bfd_section->vma;
263 static etree_value_type
264 fold_unary (tree, current_section, allocation_done, dot, dotp)
266 lang_output_section_statement_type *current_section;
267 lang_phase_type allocation_done;
271 etree_value_type result;
273 result = exp_fold_tree (tree->unary.child,
275 allocation_done, dot, dotp);
278 switch (tree->type.node_code)
281 if (allocation_done != lang_first_phase_enum)
282 result = new_rel_from_section (align_n (dot, result.value),
285 result.valid_p = FALSE;
289 if (allocation_done != lang_first_phase_enum)
291 result.value += result.section->bfd_section->vma;
292 result.section = abs_output_section;
295 result.valid_p = FALSE;
300 result.value = ~result.value;
305 result.value = !result.value;
310 result.value = -result.value;
314 /* Return next place aligned to value. */
315 if (allocation_done == lang_allocating_phase_enum)
318 result.value = align_n (dot, result.value);
321 result.valid_p = FALSE;
324 case DATA_SEGMENT_END:
325 if (allocation_done != lang_first_phase_enum
326 && current_section == abs_output_section
327 && (exp_data_seg.phase == exp_dataseg_align_seen
328 || exp_data_seg.phase == exp_dataseg_adjust
329 || allocation_done != lang_allocating_phase_enum))
331 if (exp_data_seg.phase == exp_dataseg_align_seen)
333 exp_data_seg.phase = exp_dataseg_end_seen;
334 exp_data_seg.end = result.value;
338 result.valid_p = FALSE;
350 static etree_value_type
351 fold_binary (tree, current_section, allocation_done, dot, dotp)
353 lang_output_section_statement_type *current_section;
354 lang_phase_type allocation_done;
358 etree_value_type result;
360 result = exp_fold_tree (tree->binary.lhs, current_section,
361 allocation_done, dot, dotp);
364 etree_value_type other;
366 other = exp_fold_tree (tree->binary.rhs,
368 allocation_done, dot, dotp);
371 /* If the values are from different sections, or this is an
372 absolute expression, make both the source arguments
373 absolute. However, adding or subtracting an absolute
374 value from a relative value is meaningful, and is an
376 if (current_section != abs_output_section
377 && (other.section == abs_output_section
378 || (result.section == abs_output_section
379 && tree->type.node_code == '+'))
380 && (tree->type.node_code == '+'
381 || tree->type.node_code == '-'))
383 if (other.section != abs_output_section)
385 /* Keep the section of the other term. */
386 if (tree->type.node_code == '+')
387 other.value = result.value + other.value;
389 other.value = result.value - other.value;
393 else if (result.section != other.section
394 || current_section == abs_output_section)
400 switch (tree->type.node_code)
403 if (other.value == 0)
404 einfo (_("%F%S %% by zero\n"));
405 result.value = ((bfd_signed_vma) result.value
406 % (bfd_signed_vma) other.value);
410 if (other.value == 0)
411 einfo (_("%F%S / by zero\n"));
412 result.value = ((bfd_signed_vma) result.value
413 / (bfd_signed_vma) other.value);
416 #define BOP(x,y) case x : result.value = result.value y other.value; break;
435 if (result.value < other.value)
440 if (result.value > other.value)
444 case DATA_SEGMENT_ALIGN:
445 if (allocation_done != lang_first_phase_enum
446 && current_section == abs_output_section
447 && (exp_data_seg.phase == exp_dataseg_none
448 || exp_data_seg.phase == exp_dataseg_adjust
449 || allocation_done != lang_allocating_phase_enum))
451 bfd_vma maxpage = result.value;
453 result.value = align_n (dot, maxpage);
454 if (exp_data_seg.phase != exp_dataseg_adjust)
456 result.value += dot & (maxpage - 1);
457 if (allocation_done == lang_allocating_phase_enum)
459 exp_data_seg.phase = exp_dataseg_align_seen;
460 exp_data_seg.base = result.value;
461 exp_data_seg.pagesize = other.value;
464 else if (other.value < maxpage)
465 result.value += (dot + other.value - 1)
466 & (maxpage - other.value);
469 result.valid_p = FALSE;
478 result.valid_p = FALSE;
485 static etree_value_type
486 fold_trinary (tree, current_section, allocation_done, dot, dotp)
488 lang_output_section_statement_type *current_section;
489 lang_phase_type allocation_done;
493 etree_value_type result;
495 result = exp_fold_tree (tree->trinary.cond, current_section,
496 allocation_done, dot, dotp);
498 result = exp_fold_tree ((result.value
500 : tree->trinary.rhs),
502 allocation_done, dot, dotp);
510 etree_value_type new;
515 static etree_value_type
516 fold_name (tree, current_section, allocation_done, dot)
518 lang_output_section_statement_type *current_section;
519 lang_phase_type allocation_done;
522 etree_value_type result;
524 switch (tree->type.node_code)
527 if (allocation_done != lang_first_phase_enum)
529 result = new_abs ((bfd_vma)
530 bfd_sizeof_headers (output_bfd,
531 link_info.relocateable));
535 result.valid_p = FALSE;
539 if (allocation_done == lang_first_phase_enum)
540 result.valid_p = FALSE;
543 struct bfd_link_hash_entry *h;
545 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
548 result.value = (h != (struct bfd_link_hash_entry *) NULL
549 && (h->type == bfd_link_hash_defined
550 || h->type == bfd_link_hash_defweak
551 || h->type == bfd_link_hash_common));
553 result.valid_p = TRUE;
557 result.valid_p = FALSE;
558 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
560 if (allocation_done != lang_first_phase_enum)
561 result = new_rel_from_section (dot, current_section);
565 else if (allocation_done != lang_first_phase_enum)
567 struct bfd_link_hash_entry *h;
569 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
573 && (h->type == bfd_link_hash_defined
574 || h->type == bfd_link_hash_defweak))
576 if (bfd_is_abs_section (h->u.def.section))
577 result = new_abs (h->u.def.value);
578 else if (allocation_done == lang_final_phase_enum
579 || allocation_done == lang_allocating_phase_enum)
581 asection *output_section;
583 output_section = h->u.def.section->output_section;
584 if (output_section == NULL)
585 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
589 lang_output_section_statement_type *os;
591 os = (lang_output_section_statement_lookup
592 (bfd_get_section_name (output_bfd,
595 /* FIXME: Is this correct if this section is
596 being linked with -R? */
597 result = new_rel ((h->u.def.value
598 + h->u.def.section->output_offset),
604 else if (allocation_done == lang_final_phase_enum)
605 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
611 if (allocation_done != lang_first_phase_enum)
613 lang_output_section_statement_type *os;
615 os = lang_output_section_find (tree->name.name);
616 check (os, tree->name.name, "ADDR");
617 result = new_rel (0, NULL, os);
624 if (allocation_done != lang_first_phase_enum)
626 lang_output_section_statement_type *os;
628 os = lang_output_section_find (tree->name.name);
629 check (os, tree->name.name, "LOADADDR");
630 if (os->load_base == NULL)
631 result = new_rel (0, NULL, os);
633 result = exp_fold_tree_no_dot (os->load_base,
642 if (allocation_done != lang_first_phase_enum)
644 int opb = bfd_octets_per_byte (output_bfd);
645 lang_output_section_statement_type *os;
647 os = lang_output_section_find (tree->name.name);
648 check (os, tree->name.name, "SIZEOF");
649 result = new_abs (os->bfd_section->_raw_size / opb);
664 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
666 lang_output_section_statement_type *current_section;
667 lang_phase_type allocation_done;
671 etree_value_type result;
675 result.valid_p = FALSE;
679 switch (tree->type.node_class)
682 result = new_rel (tree->value.value, tree->value.str, current_section);
686 if (allocation_done != lang_final_phase_enum)
687 result.valid_p = FALSE;
689 result = new_rel ((tree->rel.value
690 + tree->rel.section->output_section->vma
691 + tree->rel.section->output_offset),
697 result = exp_fold_tree (tree->assert_s.child,
699 allocation_done, dot, dotp);
703 einfo ("%F%P: %s\n", tree->assert_s.message);
709 result = fold_unary (tree, current_section, allocation_done,
714 result = fold_binary (tree, current_section, allocation_done,
719 result = fold_trinary (tree, current_section, allocation_done,
726 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
728 /* Assignment to dot can only be done during allocation. */
729 if (tree->type.node_class != etree_assign)
730 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
731 if (allocation_done == lang_allocating_phase_enum
732 || (allocation_done == lang_final_phase_enum
733 && current_section == abs_output_section))
735 result = exp_fold_tree (tree->assign.src,
737 allocation_done, dot,
739 if (! result.valid_p)
740 einfo (_("%F%S invalid assignment to location counter\n"));
743 if (current_section == NULL)
744 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
749 nextdot = (result.value
750 + current_section->bfd_section->vma);
752 && current_section != abs_output_section)
753 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
763 result = exp_fold_tree (tree->assign.src,
764 current_section, allocation_done,
769 struct bfd_link_hash_entry *h;
771 if (tree->type.node_class == etree_assign)
775 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
776 create, FALSE, FALSE);
777 if (h == (struct bfd_link_hash_entry *) NULL)
779 if (tree->type.node_class == etree_assign)
780 einfo (_("%P%F:%s: hash creation failed\n"),
783 else if (tree->type.node_class == etree_provide
784 && h->type != bfd_link_hash_undefined
785 && h->type != bfd_link_hash_common)
787 /* Do nothing. The symbol was defined by some
792 /* FIXME: Should we worry if the symbol is already
794 h->type = bfd_link_hash_defined;
795 h->u.def.value = result.value;
796 h->u.def.section = result.section->bfd_section;
797 if (tree->type.node_class == etree_provide)
798 tree->type.node_class = etree_provided;
805 result = fold_name (tree, current_section, allocation_done, dot);
816 static etree_value_type
817 exp_fold_tree_no_dot (tree, current_section, allocation_done)
819 lang_output_section_statement_type *current_section;
820 lang_phase_type allocation_done;
822 return exp_fold_tree (tree, current_section, allocation_done,
823 (bfd_vma) 0, (bfd_vma *) NULL);
827 exp_binop (code, lhs, rhs)
832 etree_type value, *new;
835 value.type.node_code = code;
836 value.binary.lhs = lhs;
837 value.binary.rhs = rhs;
838 value.type.node_class = etree_binary;
839 r = exp_fold_tree_no_dot (&value,
841 lang_first_phase_enum);
844 return exp_intop (r.value);
846 new = (etree_type *) stat_alloc (sizeof (new->binary));
847 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
852 exp_trinop (code, cond, lhs, rhs)
858 etree_type value, *new;
860 value.type.node_code = code;
861 value.trinary.lhs = lhs;
862 value.trinary.cond = cond;
863 value.trinary.rhs = rhs;
864 value.type.node_class = etree_trinary;
865 r = exp_fold_tree_no_dot (&value,
866 (lang_output_section_statement_type *) NULL,
867 lang_first_phase_enum);
869 return exp_intop (r.value);
871 new = (etree_type *) stat_alloc (sizeof (new->trinary));
872 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
877 exp_unop (code, child)
881 etree_type value, *new;
884 value.unary.type.node_code = code;
885 value.unary.child = child;
886 value.unary.type.node_class = etree_unary;
887 r = exp_fold_tree_no_dot (&value, abs_output_section,
888 lang_first_phase_enum);
890 return exp_intop (r.value);
892 new = (etree_type *) stat_alloc (sizeof (new->unary));
893 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
898 exp_nameop (code, name)
902 etree_type value, *new;
904 value.name.type.node_code = code;
905 value.name.name = name;
906 value.name.type.node_class = etree_name;
908 r = exp_fold_tree_no_dot (&value,
909 (lang_output_section_statement_type *) NULL,
910 lang_first_phase_enum);
912 return exp_intop (r.value);
914 new = (etree_type *) stat_alloc (sizeof (new->name));
915 memcpy ((char *) new, (char *) &value, sizeof (new->name));
921 exp_assop (code, dst, src)
926 etree_type value, *new;
928 value.assign.type.node_code = code;
930 value.assign.src = src;
931 value.assign.dst = dst;
932 value.assign.type.node_class = etree_assign;
935 if (exp_fold_tree_no_dot (&value, &result))
936 return exp_intop (result);
938 new = (etree_type *) stat_alloc (sizeof (new->assign));
939 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
943 /* Handle PROVIDE. */
946 exp_provide (dst, src)
952 n = (etree_type *) stat_alloc (sizeof (n->assign));
953 n->assign.type.node_code = '=';
954 n->assign.type.node_class = etree_provide;
963 exp_assert (exp, message)
969 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
970 n->assert_s.type.node_code = '!';
971 n->assert_s.type.node_class = etree_assert;
972 n->assert_s.child = exp;
973 n->assert_s.message = message;
978 exp_print_tree (tree)
981 if (config.map_file == NULL)
982 config.map_file = stderr;
986 minfo ("NULL TREE\n");
990 switch (tree->type.node_class)
993 minfo ("0x%v", tree->value.value);
996 if (tree->rel.section->owner != NULL)
997 minfo ("%B:", tree->rel.section->owner);
998 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1002 if (tree->assign.dst->sdefs != (asymbol *) NULL)
1003 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
1004 tree->assign.dst->sdefs->value);
1006 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
1008 fprintf (config.map_file, "%s", tree->assign.dst);
1009 exp_print_token (tree->type.node_code, TRUE);
1010 exp_print_tree (tree->assign.src);
1013 case etree_provided:
1014 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1015 exp_print_tree (tree->assign.src);
1016 fprintf (config.map_file, ")");
1019 fprintf (config.map_file, "(");
1020 exp_print_tree (tree->binary.lhs);
1021 exp_print_token (tree->type.node_code, TRUE);
1022 exp_print_tree (tree->binary.rhs);
1023 fprintf (config.map_file, ")");
1026 exp_print_tree (tree->trinary.cond);
1027 fprintf (config.map_file, "?");
1028 exp_print_tree (tree->trinary.lhs);
1029 fprintf (config.map_file, ":");
1030 exp_print_tree (tree->trinary.rhs);
1033 exp_print_token (tree->unary.type.node_code, FALSE);
1034 if (tree->unary.child)
1036 fprintf (config.map_file, " (");
1037 exp_print_tree (tree->unary.child);
1038 fprintf (config.map_file, ")");
1043 fprintf (config.map_file, "ASSERT (");
1044 exp_print_tree (tree->assert_s.child);
1045 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1049 fprintf (config.map_file, "????????");
1052 if (tree->type.node_code == NAME)
1054 fprintf (config.map_file, "%s", tree->name.name);
1058 exp_print_token (tree->type.node_code, FALSE);
1059 if (tree->name.name)
1060 fprintf (config.map_file, " (%s)", tree->name.name);
1070 exp_get_vma (tree, def, name, allocation_done)
1074 lang_phase_type allocation_done;
1080 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1081 if (! r.valid_p && name != NULL)
1082 einfo (_("%F%S nonconstant expression for %s\n"), name);
1090 exp_get_value_int (tree, def, name, allocation_done)
1094 lang_phase_type allocation_done;
1096 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1100 exp_get_fill (tree, def, name, allocation_done)
1104 lang_phase_type allocation_done;
1114 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1115 if (! r.valid_p && name != NULL)
1116 einfo (_("%F%S nonconstant expression for %s\n"), name);
1118 if (r.str != NULL && (len = strlen (r.str)) != 0)
1122 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1123 fill->size = (len + 1) / 2;
1133 digit = (digit - 'A' + '0' + 10) & 0xf;
1147 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1149 fill->data[0] = (val >> 24) & 0xff;
1150 fill->data[1] = (val >> 16) & 0xff;
1151 fill->data[2] = (val >> 8) & 0xff;
1152 fill->data[3] = (val >> 0) & 0xff;
1159 exp_get_abs_int (tree, def, name, allocation_done)
1161 int def ATTRIBUTE_UNUSED;
1163 lang_phase_type allocation_done;
1165 etree_value_type res;
1166 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1169 res.value += res.section->bfd_section->vma;
1171 einfo (_("%F%S non constant expression for %s\n"), name);
1176 bfd_vma align_n (value, align)
1183 value = (value + align - 1) / align;
1184 return value * align;