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, 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_unary
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_binary
59 PARAMS ((etree_type *tree,
60 lang_output_section_statement_type *current_section,
61 lang_phase_type allocation_done,
62 bfd_vma dot, bfd_vma *dotp));
63 static etree_value_type fold_trinary
64 PARAMS ((etree_type *tree,
65 lang_output_section_statement_type *current_section,
66 lang_phase_type allocation_done,
67 bfd_vma dot, bfd_vma *dotp));
68 static etree_value_type fold_name
69 PARAMS ((etree_type *tree,
70 lang_output_section_statement_type *current_section,
71 lang_phase_type allocation_done,
73 static etree_value_type exp_fold_tree_no_dot
74 PARAMS ((etree_type *tree,
75 lang_output_section_statement_type *current_section,
76 lang_phase_type allocation_done));
78 struct exp_data_seg exp_data_seg;
80 /* Print the string representation of the given token. Surround it
81 with spaces if INFIX_P is true. */
84 exp_print_token (code, infix_p)
113 { ALIGN_K, "ALIGN" },
120 { SECTIONS, "SECTIONS" },
121 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
122 { MEMORY, "MEMORY" },
123 { DEFINED, "DEFINED" },
124 { TARGET_K, "TARGET" },
125 { SEARCH_DIR, "SEARCH_DIR" },
129 { SIZEOF, "SIZEOF" },
131 { LOADADDR, "LOADADDR" },
133 { REL, "relocateable" },
134 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
135 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
139 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
140 if (table[idx].code == code)
144 fputc (' ', config.map_file);
146 if (idx < ARRAY_SIZE (table))
147 fputs (table[idx].name, config.map_file);
149 fputc (code, config.map_file);
151 fprintf (config.map_file, "<code %d>", code);
154 fputc (' ', config.map_file);
159 etree_value_type *ptr;
161 asection *s = ptr->section->bfd_section;
162 ptr->value += s->vma;
163 ptr->section = abs_output_section;
166 static etree_value_type
170 etree_value_type new;
172 new.section = abs_output_section;
179 lang_output_section_statement_type *os;
184 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
186 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
193 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
194 new->type.node_code = INT;
195 new->value.value = value;
196 new->value.str = NULL;
197 new->type.node_class = etree_value;
202 exp_bigintop (value, str)
206 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
207 new->type.node_code = INT;
208 new->value.value = value;
209 new->value.str = str;
210 new->type.node_class = etree_value;
214 /* Build an expression representing an unnamed relocateable value. */
217 exp_relop (section, value)
221 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
222 new->type.node_code = REL;
223 new->type.node_class = etree_rel;
224 new->rel.section = section;
225 new->rel.value = value;
229 static etree_value_type
230 new_rel (value, str, section)
233 lang_output_section_statement_type *section;
235 etree_value_type new;
239 new.section = section;
243 static etree_value_type
244 new_rel_from_section (value, section)
246 lang_output_section_statement_type *section;
248 etree_value_type new;
252 new.section = section;
254 new.value -= section->bfd_section->vma;
259 static etree_value_type
260 fold_unary (tree, current_section, allocation_done, dot, dotp)
262 lang_output_section_statement_type *current_section;
263 lang_phase_type allocation_done;
267 etree_value_type result;
269 result = exp_fold_tree (tree->unary.child,
271 allocation_done, dot, dotp);
274 switch (tree->type.node_code)
277 if (allocation_done != lang_first_phase_enum)
278 result = new_rel_from_section (align_n (dot, result.value),
281 result.valid_p = false;
285 if (allocation_done != lang_first_phase_enum)
287 result.value += result.section->bfd_section->vma;
288 result.section = abs_output_section;
291 result.valid_p = false;
296 result.value = ~result.value;
301 result.value = !result.value;
306 result.value = -result.value;
310 /* Return next place aligned to value. */
311 if (allocation_done == lang_allocating_phase_enum)
314 result.value = align_n (dot, result.value);
317 result.valid_p = false;
320 case DATA_SEGMENT_END:
321 if (allocation_done != lang_first_phase_enum
322 && current_section == abs_output_section
323 && (exp_data_seg.phase == exp_dataseg_align_seen
324 || exp_data_seg.phase == exp_dataseg_adjust
325 || allocation_done != lang_allocating_phase_enum))
327 if (exp_data_seg.phase == exp_dataseg_align_seen)
329 exp_data_seg.phase = exp_dataseg_end_seen;
330 exp_data_seg.end = result.value;
334 result.valid_p = false;
346 static etree_value_type
347 fold_binary (tree, current_section, allocation_done, dot, dotp)
349 lang_output_section_statement_type *current_section;
350 lang_phase_type allocation_done;
354 etree_value_type result;
356 result = exp_fold_tree (tree->binary.lhs, current_section,
357 allocation_done, dot, dotp);
360 etree_value_type other;
362 other = exp_fold_tree (tree->binary.rhs,
364 allocation_done, dot, dotp);
367 /* If the values are from different sections, or this is an
368 absolute expression, make both the source arguments
369 absolute. However, adding or subtracting an absolute
370 value from a relative value is meaningful, and is an
372 if (current_section != abs_output_section
373 && (other.section == abs_output_section
374 || (result.section == abs_output_section
375 && tree->type.node_code == '+'))
376 && (tree->type.node_code == '+'
377 || tree->type.node_code == '-'))
379 if (other.section != abs_output_section)
381 /* Keep the section of the other term. */
382 if (tree->type.node_code == '+')
383 other.value = result.value + other.value;
385 other.value = result.value - other.value;
389 else if (result.section != other.section
390 || current_section == abs_output_section)
396 switch (tree->type.node_code)
399 if (other.value == 0)
400 einfo (_("%F%S %% by zero\n"));
401 result.value = ((bfd_signed_vma) result.value
402 % (bfd_signed_vma) other.value);
406 if (other.value == 0)
407 einfo (_("%F%S / by zero\n"));
408 result.value = ((bfd_signed_vma) result.value
409 / (bfd_signed_vma) other.value);
412 #define BOP(x,y) case x : result.value = result.value y other.value; break;
431 if (result.value < other.value)
436 if (result.value > other.value)
440 case DATA_SEGMENT_ALIGN:
441 if (allocation_done != lang_first_phase_enum
442 && current_section == abs_output_section
443 && (exp_data_seg.phase == exp_dataseg_none
444 || exp_data_seg.phase == exp_dataseg_adjust
445 || allocation_done != lang_allocating_phase_enum))
447 bfd_vma maxpage = result.value;
449 result.value = align_n (dot, maxpage);
450 if (exp_data_seg.phase != exp_dataseg_adjust)
452 result.value += dot & (maxpage - 1);
453 if (allocation_done == lang_allocating_phase_enum)
455 exp_data_seg.phase = exp_dataseg_align_seen;
456 exp_data_seg.base = result.value;
457 exp_data_seg.pagesize = other.value;
460 else if (other.value < maxpage)
461 result.value += (dot + other.value - 1)
462 & (maxpage - other.value);
465 result.valid_p = false;
474 result.valid_p = false;
481 static etree_value_type
482 fold_trinary (tree, current_section, allocation_done, dot, dotp)
484 lang_output_section_statement_type *current_section;
485 lang_phase_type allocation_done;
489 etree_value_type result;
491 result = exp_fold_tree (tree->trinary.cond, current_section,
492 allocation_done, dot, dotp);
494 result = exp_fold_tree ((result.value
496 : tree->trinary.rhs),
498 allocation_done, dot, dotp);
506 etree_value_type new;
511 static etree_value_type
512 fold_name (tree, current_section, allocation_done, dot)
514 lang_output_section_statement_type *current_section;
515 lang_phase_type allocation_done;
518 etree_value_type result;
520 switch (tree->type.node_code)
523 if (allocation_done != lang_first_phase_enum)
525 result = new_abs ((bfd_vma)
526 bfd_sizeof_headers (output_bfd,
527 link_info.relocateable));
531 result.valid_p = false;
535 if (allocation_done == lang_first_phase_enum)
536 result.valid_p = false;
539 struct bfd_link_hash_entry *h;
541 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
544 result.value = (h != (struct bfd_link_hash_entry *) NULL
545 && (h->type == bfd_link_hash_defined
546 || h->type == bfd_link_hash_defweak
547 || h->type == bfd_link_hash_common));
549 result.valid_p = true;
553 result.valid_p = false;
554 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
556 if (allocation_done != lang_first_phase_enum)
557 result = new_rel_from_section (dot, current_section);
561 else if (allocation_done != lang_first_phase_enum)
563 struct bfd_link_hash_entry *h;
565 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
569 && (h->type == bfd_link_hash_defined
570 || h->type == bfd_link_hash_defweak))
572 if (bfd_is_abs_section (h->u.def.section))
573 result = new_abs (h->u.def.value);
574 else if (allocation_done == lang_final_phase_enum
575 || allocation_done == lang_allocating_phase_enum)
577 asection *output_section;
579 output_section = h->u.def.section->output_section;
580 if (output_section == NULL)
581 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
585 lang_output_section_statement_type *os;
587 os = (lang_output_section_statement_lookup
588 (bfd_get_section_name (output_bfd,
591 /* FIXME: Is this correct if this section is
592 being linked with -R? */
593 result = new_rel ((h->u.def.value
594 + h->u.def.section->output_offset),
600 else if (allocation_done == lang_final_phase_enum)
601 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
607 if (allocation_done != lang_first_phase_enum)
609 lang_output_section_statement_type *os;
611 os = lang_output_section_find (tree->name.name);
612 check (os, tree->name.name, "ADDR");
613 result = new_rel (0, NULL, os);
620 if (allocation_done != lang_first_phase_enum)
622 lang_output_section_statement_type *os;
624 os = lang_output_section_find (tree->name.name);
625 check (os, tree->name.name, "LOADADDR");
626 if (os->load_base == NULL)
627 result = new_rel (0, NULL, os);
629 result = exp_fold_tree_no_dot (os->load_base,
638 if (allocation_done != lang_first_phase_enum)
640 int opb = bfd_octets_per_byte (output_bfd);
641 lang_output_section_statement_type *os;
643 os = lang_output_section_find (tree->name.name);
644 check (os, tree->name.name, "SIZEOF");
645 result = new_abs (os->bfd_section->_raw_size / opb);
660 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
662 lang_output_section_statement_type *current_section;
663 lang_phase_type allocation_done;
667 etree_value_type result;
671 result.valid_p = false;
675 switch (tree->type.node_class)
678 result = new_rel (tree->value.value, tree->value.str, current_section);
682 if (allocation_done != lang_final_phase_enum)
683 result.valid_p = false;
685 result = new_rel ((tree->rel.value
686 + tree->rel.section->output_section->vma
687 + tree->rel.section->output_offset),
693 result = exp_fold_tree (tree->assert_s.child,
695 allocation_done, dot, dotp);
699 einfo ("%F%P: %s\n", tree->assert_s.message);
705 result = fold_unary (tree, current_section, allocation_done,
710 result = fold_binary (tree, current_section, allocation_done,
715 result = fold_trinary (tree, current_section, allocation_done,
722 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
724 /* Assignment to dot can only be done during allocation. */
725 if (tree->type.node_class != etree_assign)
726 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
727 if (allocation_done == lang_allocating_phase_enum
728 || (allocation_done == lang_final_phase_enum
729 && current_section == abs_output_section))
731 result = exp_fold_tree (tree->assign.src,
733 allocation_done, dot,
735 if (! result.valid_p)
736 einfo (_("%F%S invalid assignment to location counter\n"));
739 if (current_section == NULL)
740 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
745 nextdot = (result.value
746 + current_section->bfd_section->vma);
748 && current_section != abs_output_section)
749 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
759 result = exp_fold_tree (tree->assign.src,
760 current_section, allocation_done,
765 struct bfd_link_hash_entry *h;
767 if (tree->type.node_class == etree_assign)
771 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
772 create, false, false);
773 if (h == (struct bfd_link_hash_entry *) NULL)
775 if (tree->type.node_class == etree_assign)
776 einfo (_("%P%F:%s: hash creation failed\n"),
779 else if (tree->type.node_class == etree_provide
780 && h->type != bfd_link_hash_undefined
781 && h->type != bfd_link_hash_common)
783 /* Do nothing. The symbol was defined by some
788 /* FIXME: Should we worry if the symbol is already
790 h->type = bfd_link_hash_defined;
791 h->u.def.value = result.value;
792 h->u.def.section = result.section->bfd_section;
793 if (tree->type.node_class == etree_provide)
794 tree->type.node_class = etree_provided;
801 result = fold_name (tree, current_section, allocation_done, dot);
812 static etree_value_type
813 exp_fold_tree_no_dot (tree, current_section, allocation_done)
815 lang_output_section_statement_type *current_section;
816 lang_phase_type allocation_done;
818 return exp_fold_tree (tree, current_section, allocation_done,
819 (bfd_vma) 0, (bfd_vma *) NULL);
823 exp_binop (code, lhs, rhs)
828 etree_type value, *new;
831 value.type.node_code = code;
832 value.binary.lhs = lhs;
833 value.binary.rhs = rhs;
834 value.type.node_class = etree_binary;
835 r = exp_fold_tree_no_dot (&value,
837 lang_first_phase_enum);
840 return exp_intop (r.value);
842 new = (etree_type *) stat_alloc (sizeof (new->binary));
843 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
848 exp_trinop (code, cond, lhs, rhs)
854 etree_type value, *new;
856 value.type.node_code = code;
857 value.trinary.lhs = lhs;
858 value.trinary.cond = cond;
859 value.trinary.rhs = rhs;
860 value.type.node_class = etree_trinary;
861 r = exp_fold_tree_no_dot (&value,
862 (lang_output_section_statement_type *) NULL,
863 lang_first_phase_enum);
865 return exp_intop (r.value);
867 new = (etree_type *) stat_alloc (sizeof (new->trinary));
868 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
873 exp_unop (code, child)
877 etree_type value, *new;
880 value.unary.type.node_code = code;
881 value.unary.child = child;
882 value.unary.type.node_class = etree_unary;
883 r = exp_fold_tree_no_dot (&value, abs_output_section,
884 lang_first_phase_enum);
886 return exp_intop (r.value);
888 new = (etree_type *) stat_alloc (sizeof (new->unary));
889 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
894 exp_nameop (code, name)
898 etree_type value, *new;
900 value.name.type.node_code = code;
901 value.name.name = name;
902 value.name.type.node_class = etree_name;
904 r = exp_fold_tree_no_dot (&value,
905 (lang_output_section_statement_type *) NULL,
906 lang_first_phase_enum);
908 return exp_intop (r.value);
910 new = (etree_type *) stat_alloc (sizeof (new->name));
911 memcpy ((char *) new, (char *) &value, sizeof (new->name));
917 exp_assop (code, dst, src)
922 etree_type value, *new;
924 value.assign.type.node_code = code;
926 value.assign.src = src;
927 value.assign.dst = dst;
928 value.assign.type.node_class = etree_assign;
931 if (exp_fold_tree_no_dot (&value, &result))
932 return exp_intop (result);
934 new = (etree_type *) stat_alloc (sizeof (new->assign));
935 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
939 /* Handle PROVIDE. */
942 exp_provide (dst, src)
948 n = (etree_type *) stat_alloc (sizeof (n->assign));
949 n->assign.type.node_code = '=';
950 n->assign.type.node_class = etree_provide;
959 exp_assert (exp, message)
965 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
966 n->assert_s.type.node_code = '!';
967 n->assert_s.type.node_class = etree_assert;
968 n->assert_s.child = exp;
969 n->assert_s.message = message;
974 exp_print_tree (tree)
977 if (config.map_file == NULL)
978 config.map_file = stderr;
982 minfo ("NULL TREE\n");
986 switch (tree->type.node_class)
989 minfo ("0x%v", tree->value.value);
992 if (tree->rel.section->owner != NULL)
993 minfo ("%B:", tree->rel.section->owner);
994 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
998 if (tree->assign.dst->sdefs != (asymbol *) NULL)
999 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
1000 tree->assign.dst->sdefs->value);
1002 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
1004 fprintf (config.map_file, "%s", tree->assign.dst);
1005 exp_print_token (tree->type.node_code, true);
1006 exp_print_tree (tree->assign.src);
1009 case etree_provided:
1010 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1011 exp_print_tree (tree->assign.src);
1012 fprintf (config.map_file, ")");
1015 fprintf (config.map_file, "(");
1016 exp_print_tree (tree->binary.lhs);
1017 exp_print_token (tree->type.node_code, true);
1018 exp_print_tree (tree->binary.rhs);
1019 fprintf (config.map_file, ")");
1022 exp_print_tree (tree->trinary.cond);
1023 fprintf (config.map_file, "?");
1024 exp_print_tree (tree->trinary.lhs);
1025 fprintf (config.map_file, ":");
1026 exp_print_tree (tree->trinary.rhs);
1029 exp_print_token (tree->unary.type.node_code, false);
1030 if (tree->unary.child)
1032 fprintf (config.map_file, " (");
1033 exp_print_tree (tree->unary.child);
1034 fprintf (config.map_file, ")");
1039 fprintf (config.map_file, "ASSERT (");
1040 exp_print_tree (tree->assert_s.child);
1041 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1045 fprintf (config.map_file, "????????");
1048 if (tree->type.node_code == NAME)
1050 fprintf (config.map_file, "%s", tree->name.name);
1054 exp_print_token (tree->type.node_code, false);
1055 if (tree->name.name)
1056 fprintf (config.map_file, " (%s)", tree->name.name);
1066 exp_get_vma (tree, def, name, allocation_done)
1070 lang_phase_type allocation_done;
1076 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1077 if (! r.valid_p && name != NULL)
1078 einfo (_("%F%S nonconstant expression for %s\n"), name);
1086 exp_get_value_int (tree, def, name, allocation_done)
1090 lang_phase_type allocation_done;
1092 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1096 exp_get_fill (tree, def, name, allocation_done)
1100 lang_phase_type allocation_done;
1110 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1111 if (! r.valid_p && name != NULL)
1112 einfo (_("%F%S nonconstant expression for %s\n"), name);
1114 if (r.str != NULL && (len = strlen (r.str)) != 0)
1118 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1119 fill->size = (len + 1) / 2;
1129 digit = (digit - 'A' + '0' + 10) & 0xf;
1143 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1145 fill->data[0] = (val >> 24) & 0xff;
1146 fill->data[1] = (val >> 16) & 0xff;
1147 fill->data[2] = (val >> 8) & 0xff;
1148 fill->data[3] = (val >> 0) & 0xff;
1155 exp_get_abs_int (tree, def, name, allocation_done)
1157 int def ATTRIBUTE_UNUSED;
1159 lang_phase_type allocation_done;
1161 etree_value_type res;
1162 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1165 res.value += res.section->bfd_section->vma;
1167 einfo (_("%F%S non constant expression for %s\n"), name);
1172 bfd_vma align_n (value, align)
1179 value = (value + align - 1) / align;
1180 return value * align;