1 /* This module handles expression trees.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support (sac@cygnus.com).
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 This module is in charge of working out the contents of expressions.
25 It has to keep track of the relative/absness of a symbol etc. This is
26 done by keeping all values in a struct (an etree_value_type) which
27 contains a value, a section to which it is relative and a valid bit.
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));
68 exp_print_token (code)
78 { REL, "relocateable" },
98 { SECTIONS,"SECTIONS" },
99 { SIZEOF_HEADERS,"SIZEOF_HEADERS" },
103 { LOADADDR,"LOADADDR" },
105 { DEFINED,"DEFINED" },
106 { TARGET_K,"TARGET" },
107 { SEARCH_DIR,"SEARCH_DIR" },
119 for (idx = 0; table[idx].name != (char*)NULL; idx++) {
120 if (table[idx].code == code) {
121 fprintf(config.map_file, "%s", table[idx].name);
125 /* Not in table, just print it alone */
126 fprintf(config.map_file, "%c",code);
131 etree_value_type *ptr;
133 asection *s = ptr->section->bfd_section;
134 ptr->value += s->vma;
135 ptr->section = abs_output_section;
138 static etree_value_type
142 etree_value_type new;
144 new.section = abs_output_section;
151 lang_output_section_statement_type *os;
156 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
158 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
165 etree_type *new = (etree_type *) stat_alloc(sizeof(new->value));
166 new->type.node_code = INT;
167 new->value.value = value;
168 new->type.node_class = etree_value;
173 /* Build an expression representing an unnamed relocateable value. */
176 exp_relop (section, value)
180 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
181 new->type.node_code = REL;
182 new->type.node_class = etree_rel;
183 new->rel.section = section;
184 new->rel.value = value;
188 static etree_value_type
189 new_rel (value, section)
191 lang_output_section_statement_type *section;
193 etree_value_type new;
196 new.section = section;
200 static etree_value_type
201 new_rel_from_section (value, section)
203 lang_output_section_statement_type *section;
205 etree_value_type new;
208 new.section = section;
210 new.value -= section->bfd_section->vma;
215 static etree_value_type
216 fold_binary (tree, current_section, allocation_done, dot, dotp)
218 lang_output_section_statement_type *current_section;
219 lang_phase_type allocation_done;
223 etree_value_type result;
225 result = exp_fold_tree (tree->binary.lhs, current_section,
226 allocation_done, dot, dotp);
229 etree_value_type other;
231 other = exp_fold_tree (tree->binary.rhs,
233 allocation_done, dot,dotp) ;
236 /* If the values are from different sections, or this is an
237 absolute expression, make both the source arguments
238 absolute. However, adding or subtracting an absolute
239 value from a relative value is meaningful, and is an
241 if (current_section != abs_output_section
242 && (other.section == abs_output_section
243 || (result.section == abs_output_section
244 && tree->type.node_code == '+'))
245 && (tree->type.node_code == '+'
246 || tree->type.node_code == '-'))
248 etree_value_type hold;
250 /* If there is only one absolute term, make sure it is the
252 if (other.section != abs_output_section)
259 else if (result.section != other.section
260 || current_section == abs_output_section)
266 switch (tree->type.node_code)
269 if (other.value == 0)
270 einfo (_("%F%S %% by zero\n"));
271 result.value = ((bfd_signed_vma) result.value
272 % (bfd_signed_vma) other.value);
276 if (other.value == 0)
277 einfo (_("%F%S / by zero\n"));
278 result.value = ((bfd_signed_vma) result.value
279 / (bfd_signed_vma) other.value);
282 #define BOP(x,y) case x : result.value = result.value y other.value; break;
301 if (result.value < other.value)
306 if (result.value > other.value)
316 result.valid = false;
326 etree_value_type new;
331 static etree_value_type
332 fold_name (tree, current_section, allocation_done, dot)
334 lang_output_section_statement_type *current_section;
335 lang_phase_type allocation_done;
338 etree_value_type result;
339 switch (tree->type.node_code)
342 if (allocation_done != lang_first_phase_enum)
344 result = new_abs ((bfd_vma)
345 bfd_sizeof_headers (output_bfd,
346 link_info.relocateable));
350 result.valid = false;
354 if (allocation_done == lang_first_phase_enum)
355 result.valid = false;
358 struct bfd_link_hash_entry *h;
360 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
363 result.value = (h != (struct bfd_link_hash_entry *) NULL
364 && (h->type == bfd_link_hash_defined
365 || h->type == bfd_link_hash_defweak
366 || h->type == bfd_link_hash_common));
372 result.valid = false;
373 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
375 if (allocation_done != lang_first_phase_enum)
376 result = new_rel_from_section(dot, current_section);
380 else if (allocation_done != lang_first_phase_enum)
382 struct bfd_link_hash_entry *h;
384 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
388 && (h->type == bfd_link_hash_defined
389 || h->type == bfd_link_hash_defweak))
391 if (bfd_is_abs_section (h->u.def.section))
392 result = new_abs (h->u.def.value);
393 else if (allocation_done == lang_final_phase_enum
394 || allocation_done == lang_allocating_phase_enum)
396 asection *output_section;
398 output_section = h->u.def.section->output_section;
399 if (output_section == NULL)
400 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
404 lang_output_section_statement_type *os;
406 os = (lang_output_section_statement_lookup
407 (bfd_get_section_name (output_bfd,
410 /* FIXME: Is this correct if this section is
411 being linked with -R? */
412 result = new_rel ((h->u.def.value
413 + h->u.def.section->output_offset),
418 else if (allocation_done == lang_final_phase_enum)
419 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
425 if (allocation_done != lang_first_phase_enum)
427 lang_output_section_statement_type *os;
429 os = lang_output_section_find (tree->name.name);
430 check (os, tree->name.name, "ADDR");
431 result = new_rel (0, os);
438 if (allocation_done != lang_first_phase_enum)
440 lang_output_section_statement_type *os;
442 os = lang_output_section_find (tree->name.name);
443 check (os, tree->name.name, "LOADADDR");
444 if (os->load_base == NULL)
445 result = new_rel (0, os);
447 result = exp_fold_tree_no_dot (os->load_base,
456 if (allocation_done != lang_first_phase_enum)
458 lang_output_section_statement_type *os;
460 os = lang_output_section_find (tree->name.name);
461 check (os, tree->name.name, "SIZEOF");
462 result = new_abs (os->bfd_section->_raw_size);
476 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
478 lang_output_section_statement_type *current_section;
479 lang_phase_type allocation_done;
483 etree_value_type result;
487 result.valid = false;
491 switch (tree->type.node_class)
494 result = new_rel (tree->value.value, current_section);
498 if (allocation_done != lang_final_phase_enum)
499 result.valid = false;
501 result = new_rel ((tree->rel.value
502 + tree->rel.section->output_section->vma
503 + tree->rel.section->output_offset),
508 result = exp_fold_tree (tree->unary.child,
510 allocation_done, dot, dotp);
513 switch (tree->type.node_code)
516 if (allocation_done != lang_first_phase_enum)
517 result = new_rel_from_section (ALIGN_N (dot, result.value),
520 result.valid = false;
524 if (allocation_done != lang_first_phase_enum && result.valid)
526 result.value += result.section->bfd_section->vma;
527 result.section = abs_output_section;
530 result.valid = false;
535 result.value = ~result.value;
540 result.value = !result.value;
545 result.value = -result.value;
549 /* Return next place aligned to value. */
550 if (allocation_done == lang_allocating_phase_enum)
553 result.value = ALIGN_N (dot, result.value);
556 result.valid = false;
567 result = exp_fold_tree (tree->trinary.cond, current_section,
568 allocation_done, dot, dotp);
570 result = exp_fold_tree ((result.value
572 : tree->trinary.rhs),
574 allocation_done, dot, dotp);
578 result = fold_binary (tree, current_section, allocation_done,
584 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
586 /* Assignment to dot can only be done during allocation */
587 if (tree->type.node_class == etree_provide)
588 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
589 if (allocation_done == lang_allocating_phase_enum
590 || (allocation_done == lang_final_phase_enum
591 && current_section == abs_output_section))
593 result = exp_fold_tree (tree->assign.src,
595 lang_allocating_phase_enum, dot,
598 einfo (_("%F%S invalid assignment to location counter\n"));
601 if (current_section == NULL)
602 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
607 nextdot = (result.value
608 + current_section->bfd_section->vma);
610 && current_section != abs_output_section)
612 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
623 result = exp_fold_tree (tree->assign.src,
624 current_section, allocation_done,
629 struct bfd_link_hash_entry *h;
631 if (tree->type.node_class == etree_assign)
635 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
636 create, false, false);
637 if (h == (struct bfd_link_hash_entry *) NULL)
639 if (tree->type.node_class == etree_assign)
640 einfo (_("%P%F:%s: hash creation failed\n"),
643 else if (tree->type.node_class == etree_provide
644 && h->type != bfd_link_hash_undefined
645 && h->type != bfd_link_hash_common)
647 /* Do nothing. The symbol was defined by some
652 /* FIXME: Should we worry if the symbol is already
654 h->type = bfd_link_hash_defined;
655 h->u.def.value = result.value;
656 h->u.def.section = result.section->bfd_section;
663 result = fold_name (tree, current_section, allocation_done, dot);
674 static etree_value_type
675 exp_fold_tree_no_dot (tree, current_section, allocation_done)
677 lang_output_section_statement_type *current_section;
678 lang_phase_type allocation_done;
680 return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
685 exp_binop (code, lhs, rhs)
690 etree_type value, *new;
693 value.type.node_code = code;
694 value.binary.lhs = lhs;
695 value.binary.rhs = rhs;
696 value.type.node_class = etree_binary;
697 r = exp_fold_tree_no_dot(&value,
699 lang_first_phase_enum );
702 return exp_intop(r.value);
704 new = (etree_type *) stat_alloc (sizeof (new->binary));
705 memcpy((char *)new, (char *)&value, sizeof(new->binary));
710 exp_trinop (code, cond, lhs, rhs)
716 etree_type value, *new;
718 value.type.node_code = code;
719 value.trinary.lhs = lhs;
720 value.trinary.cond = cond;
721 value.trinary.rhs = rhs;
722 value.type.node_class = etree_trinary;
723 r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type
724 *)NULL,lang_first_phase_enum);
726 return exp_intop(r.value);
728 new = (etree_type *) stat_alloc (sizeof (new->trinary));
729 memcpy((char *)new,(char *) &value, sizeof(new->trinary));
735 exp_unop (code, child)
739 etree_type value, *new;
742 value.unary.type.node_code = code;
743 value.unary.child = child;
744 value.unary.type.node_class = etree_unary;
745 r = exp_fold_tree_no_dot(&value,abs_output_section,
746 lang_first_phase_enum);
748 return exp_intop(r.value);
750 new = (etree_type *) stat_alloc (sizeof (new->unary));
751 memcpy((char *)new, (char *)&value, sizeof(new->unary));
757 exp_nameop (code, name)
761 etree_type value, *new;
763 value.name.type.node_code = code;
764 value.name.name = name;
765 value.name.type.node_class = etree_name;
768 r = exp_fold_tree_no_dot(&value,
769 (lang_output_section_statement_type *)NULL,
770 lang_first_phase_enum);
772 return exp_intop(r.value);
774 new = (etree_type *) stat_alloc (sizeof (new->name));
775 memcpy((char *)new, (char *)&value, sizeof(new->name));
784 exp_assop (code, dst, src)
789 etree_type value, *new;
791 value.assign.type.node_code = code;
794 value.assign.src = src;
795 value.assign.dst = dst;
796 value.assign.type.node_class = etree_assign;
799 if (exp_fold_tree_no_dot(&value, &result)) {
800 return exp_intop(result);
803 new = (etree_type*) stat_alloc (sizeof (new->assign));
804 memcpy((char *)new, (char *)&value, sizeof(new->assign));
808 /* Handle PROVIDE. */
811 exp_provide (dst, src)
817 n = (etree_type *) stat_alloc (sizeof (n->assign));
818 n->assign.type.node_code = '=';
819 n->assign.type.node_class = etree_provide;
826 exp_print_tree (tree)
829 switch (tree->type.node_class) {
831 minfo ("0x%v", tree->value.value);
834 if (tree->rel.section->owner != NULL)
835 minfo ("%B:", tree->rel.section->owner);
836 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
840 if (tree->assign.dst->sdefs != (asymbol *)NULL){
841 fprintf(config.map_file,"%s (%x) ",tree->assign.dst->name,
842 tree->assign.dst->sdefs->value);
845 fprintf(config.map_file,"%s (UNDEFINED)",tree->assign.dst->name);
848 fprintf(config.map_file,"%s",tree->assign.dst);
849 exp_print_token(tree->type.node_code);
850 exp_print_tree(tree->assign.src);
853 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
854 exp_print_tree (tree->assign.src);
855 fprintf (config.map_file, ")");
858 fprintf(config.map_file,"(");
859 exp_print_tree(tree->binary.lhs);
860 exp_print_token(tree->type.node_code);
861 exp_print_tree(tree->binary.rhs);
862 fprintf(config.map_file,")");
865 exp_print_tree(tree->trinary.cond);
866 fprintf(config.map_file,"?");
867 exp_print_tree(tree->trinary.lhs);
868 fprintf(config.map_file,":");
869 exp_print_tree(tree->trinary.rhs);
872 exp_print_token(tree->unary.type.node_code);
873 if (tree->unary.child)
876 fprintf(config.map_file,"(");
877 exp_print_tree(tree->unary.child);
878 fprintf(config.map_file,")");
883 fprintf(config.map_file,"????????");
886 if (tree->type.node_code == NAME) {
887 fprintf(config.map_file,"%s", tree->name.name);
890 exp_print_token(tree->type.node_code);
892 fprintf(config.map_file,"(%s)", tree->name.name);
902 exp_get_vma (tree, def, name, allocation_done)
906 lang_phase_type allocation_done;
912 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
913 if (! r.valid && name != NULL)
914 einfo (_("%F%S nonconstant expression for %s\n"), name);
922 exp_get_value_int (tree,def,name, allocation_done)
926 lang_phase_type allocation_done;
928 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);
933 exp_get_abs_int (tree, def, name, allocation_done)
937 lang_phase_type allocation_done;
939 etree_value_type res;
940 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
944 res.value += res.section->bfd_section->vma;
947 einfo (_("%F%S non constant expression for %s\n"),name);