1 /* This module handles expression trees.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support (sac@cygnus.com).
6 This file is part of GLD, the Gnu Linker.
8 GLD is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GLD is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GLD; see the file COPYING. If not, write to the Free
20 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 is
27 done by keeping all values in a struct (an etree_value_type) which
28 contains a value, a section to which it is relative and a valid bit.
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 value, 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));
69 exp_print_token (code)
79 { REL, "relocateable" },
99 { SECTIONS,"SECTIONS" },
100 { SIZEOF_HEADERS,"SIZEOF_HEADERS" },
104 { LOADADDR,"LOADADDR" },
106 { DEFINED,"DEFINED" },
107 { TARGET_K,"TARGET" },
108 { SEARCH_DIR,"SEARCH_DIR" },
120 for (idx = 0; table[idx].name != (char*)NULL; idx++) {
121 if (table[idx].code == code) {
122 fprintf(config.map_file, "%s", table[idx].name);
126 /* Not in table, just print it alone */
127 fprintf(config.map_file, "%c",code);
132 etree_value_type *ptr;
134 asection *s = ptr->section->bfd_section;
135 ptr->value += s->vma;
136 ptr->section = abs_output_section;
139 static etree_value_type
143 etree_value_type new;
145 new.section = abs_output_section;
152 lang_output_section_statement_type *os;
157 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
159 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
166 etree_type *new = (etree_type *) stat_alloc(sizeof(new->value));
167 new->type.node_code = INT;
168 new->value.value = value;
169 new->type.node_class = etree_value;
174 /* Build an expression representing an unnamed relocateable value. */
177 exp_relop (section, value)
181 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
182 new->type.node_code = REL;
183 new->type.node_class = etree_rel;
184 new->rel.section = section;
185 new->rel.value = value;
189 static etree_value_type
190 new_rel (value, section)
192 lang_output_section_statement_type *section;
194 etree_value_type new;
197 new.section = section;
201 static etree_value_type
202 new_rel_from_section (value, section)
204 lang_output_section_statement_type *section;
206 etree_value_type new;
209 new.section = section;
211 new.value -= section->bfd_section->vma;
216 static etree_value_type
217 fold_binary (tree, current_section, allocation_done, dot, dotp)
219 lang_output_section_statement_type *current_section;
220 lang_phase_type allocation_done;
224 etree_value_type result;
226 result = exp_fold_tree (tree->binary.lhs, current_section,
227 allocation_done, dot, dotp);
230 etree_value_type other;
232 other = exp_fold_tree (tree->binary.rhs,
234 allocation_done, dot,dotp) ;
237 /* If the values are from different sections, or this is an
238 absolute expression, make both the source arguments
239 absolute. However, adding or subtracting an absolute
240 value from a relative value is meaningful, and is an
242 if (current_section != abs_output_section
243 && (other.section == abs_output_section
244 || (result.section == abs_output_section
245 && tree->type.node_code == '+'))
246 && (tree->type.node_code == '+'
247 || tree->type.node_code == '-'))
249 etree_value_type hold;
251 /* If there is only one absolute term, make sure it is the
253 if (other.section != abs_output_section)
260 else if (result.section != other.section
261 || current_section == abs_output_section)
267 switch (tree->type.node_code)
270 if (other.value == 0)
271 einfo (_("%F%S %% by zero\n"));
272 result.value = ((bfd_signed_vma) result.value
273 % (bfd_signed_vma) other.value);
277 if (other.value == 0)
278 einfo (_("%F%S / by zero\n"));
279 result.value = ((bfd_signed_vma) result.value
280 / (bfd_signed_vma) other.value);
283 #define BOP(x,y) case x : result.value = result.value y other.value; break;
302 if (result.value < other.value)
307 if (result.value > other.value)
317 result.valid_p = false;
327 etree_value_type new;
332 static etree_value_type
333 fold_name (tree, current_section, allocation_done, dot)
335 lang_output_section_statement_type *current_section;
336 lang_phase_type allocation_done;
339 etree_value_type result;
340 switch (tree->type.node_code)
343 if (allocation_done != lang_first_phase_enum)
345 result = new_abs ((bfd_vma)
346 bfd_sizeof_headers (output_bfd,
347 link_info.relocateable));
351 result.valid_p = false;
355 if (allocation_done == lang_first_phase_enum)
356 result.valid_p = false;
359 struct bfd_link_hash_entry *h;
361 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
364 result.value = (h != (struct bfd_link_hash_entry *) NULL
365 && (h->type == bfd_link_hash_defined
366 || h->type == bfd_link_hash_defweak
367 || h->type == bfd_link_hash_common));
369 result.valid_p = true;
373 result.valid_p = false;
374 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
376 if (allocation_done != lang_first_phase_enum)
377 result = new_rel_from_section(dot, current_section);
381 else if (allocation_done != lang_first_phase_enum)
383 struct bfd_link_hash_entry *h;
385 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
389 && (h->type == bfd_link_hash_defined
390 || h->type == bfd_link_hash_defweak))
392 if (bfd_is_abs_section (h->u.def.section))
393 result = new_abs (h->u.def.value);
394 else if (allocation_done == lang_final_phase_enum
395 || allocation_done == lang_allocating_phase_enum)
397 asection *output_section;
399 output_section = h->u.def.section->output_section;
400 if (output_section == NULL)
401 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
405 lang_output_section_statement_type *os;
407 os = (lang_output_section_statement_lookup
408 (bfd_get_section_name (output_bfd,
411 /* FIXME: Is this correct if this section is
412 being linked with -R? */
413 result = new_rel ((h->u.def.value
414 + h->u.def.section->output_offset),
419 else if (allocation_done == lang_final_phase_enum)
420 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
426 if (allocation_done != lang_first_phase_enum)
428 lang_output_section_statement_type *os;
430 os = lang_output_section_find (tree->name.name);
431 check (os, tree->name.name, "ADDR");
432 result = new_rel (0, os);
439 if (allocation_done != lang_first_phase_enum)
441 lang_output_section_statement_type *os;
443 os = lang_output_section_find (tree->name.name);
444 check (os, tree->name.name, "LOADADDR");
445 if (os->load_base == NULL)
446 result = new_rel (0, os);
448 result = exp_fold_tree_no_dot (os->load_base,
457 if (allocation_done != lang_first_phase_enum)
459 int opb = bfd_octets_per_byte (output_bfd);
460 lang_output_section_statement_type *os;
462 os = lang_output_section_find (tree->name.name);
463 check (os, tree->name.name, "SIZEOF");
464 result = new_abs (os->bfd_section->_raw_size / opb);
478 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
480 lang_output_section_statement_type *current_section;
481 lang_phase_type allocation_done;
485 etree_value_type result;
489 result.valid_p = false;
493 switch (tree->type.node_class)
496 result = new_rel (tree->value.value, current_section);
500 if (allocation_done != lang_final_phase_enum)
501 result.valid_p = false;
503 result = new_rel ((tree->rel.value
504 + tree->rel.section->output_section->vma
505 + tree->rel.section->output_offset),
510 result = exp_fold_tree (tree->assert_s.child,
512 allocation_done, dot, dotp);
516 einfo ("%F%P: %s\n", tree->assert_s.message);
522 result = exp_fold_tree (tree->unary.child,
524 allocation_done, dot, dotp);
527 switch (tree->type.node_code)
530 if (allocation_done != lang_first_phase_enum)
531 result = new_rel_from_section (ALIGN_N (dot, result.value),
534 result.valid_p = false;
538 if (allocation_done != lang_first_phase_enum && result.valid_p)
540 result.value += result.section->bfd_section->vma;
541 result.section = abs_output_section;
544 result.valid_p = false;
549 result.value = ~result.value;
554 result.value = !result.value;
559 result.value = -result.value;
563 /* Return next place aligned to value. */
564 if (allocation_done == lang_allocating_phase_enum)
567 result.value = ALIGN_N (dot, result.value);
570 result.valid_p = false;
581 result = exp_fold_tree (tree->trinary.cond, current_section,
582 allocation_done, dot, dotp);
584 result = exp_fold_tree ((result.value
586 : tree->trinary.rhs),
588 allocation_done, dot, dotp);
592 result = fold_binary (tree, current_section, allocation_done,
598 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
600 /* Assignment to dot can only be done during allocation */
601 if (tree->type.node_class == etree_provide)
602 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
603 if (allocation_done == lang_allocating_phase_enum
604 || (allocation_done == lang_final_phase_enum
605 && current_section == abs_output_section))
607 result = exp_fold_tree (tree->assign.src,
609 lang_allocating_phase_enum, dot,
611 if (! result.valid_p)
612 einfo (_("%F%S invalid assignment to location counter\n"));
615 if (current_section == NULL)
616 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
621 nextdot = (result.value
622 + current_section->bfd_section->vma);
624 && current_section != abs_output_section)
626 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
637 result = exp_fold_tree (tree->assign.src,
638 current_section, allocation_done,
643 struct bfd_link_hash_entry *h;
645 if (tree->type.node_class == etree_assign)
649 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
650 create, false, false);
651 if (h == (struct bfd_link_hash_entry *) NULL)
653 if (tree->type.node_class == etree_assign)
654 einfo (_("%P%F:%s: hash creation failed\n"),
657 else if (tree->type.node_class == etree_provide
658 && h->type != bfd_link_hash_undefined
659 && h->type != bfd_link_hash_common)
661 /* Do nothing. The symbol was defined by some
666 /* FIXME: Should we worry if the symbol is already
668 h->type = bfd_link_hash_defined;
669 h->u.def.value = result.value;
670 h->u.def.section = result.section->bfd_section;
677 result = fold_name (tree, current_section, allocation_done, dot);
688 static etree_value_type
689 exp_fold_tree_no_dot (tree, current_section, allocation_done)
691 lang_output_section_statement_type *current_section;
692 lang_phase_type allocation_done;
694 return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
699 exp_binop (code, lhs, rhs)
704 etree_type value, *new;
707 value.type.node_code = code;
708 value.binary.lhs = lhs;
709 value.binary.rhs = rhs;
710 value.type.node_class = etree_binary;
711 r = exp_fold_tree_no_dot(&value,
713 lang_first_phase_enum );
716 return exp_intop(r.value);
718 new = (etree_type *) stat_alloc (sizeof (new->binary));
719 memcpy((char *)new, (char *)&value, sizeof(new->binary));
724 exp_trinop (code, cond, lhs, rhs)
730 etree_type value, *new;
732 value.type.node_code = code;
733 value.trinary.lhs = lhs;
734 value.trinary.cond = cond;
735 value.trinary.rhs = rhs;
736 value.type.node_class = etree_trinary;
737 r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type
738 *)NULL,lang_first_phase_enum);
740 return exp_intop(r.value);
742 new = (etree_type *) stat_alloc (sizeof (new->trinary));
743 memcpy((char *)new,(char *) &value, sizeof(new->trinary));
749 exp_unop (code, child)
753 etree_type value, *new;
756 value.unary.type.node_code = code;
757 value.unary.child = child;
758 value.unary.type.node_class = etree_unary;
759 r = exp_fold_tree_no_dot(&value,abs_output_section,
760 lang_first_phase_enum);
762 return exp_intop(r.value);
764 new = (etree_type *) stat_alloc (sizeof (new->unary));
765 memcpy((char *)new, (char *)&value, sizeof(new->unary));
771 exp_nameop (code, name)
775 etree_type value, *new;
777 value.name.type.node_code = code;
778 value.name.name = name;
779 value.name.type.node_class = etree_name;
782 r = exp_fold_tree_no_dot(&value,
783 (lang_output_section_statement_type *)NULL,
784 lang_first_phase_enum);
786 return exp_intop(r.value);
788 new = (etree_type *) stat_alloc (sizeof (new->name));
789 memcpy((char *)new, (char *)&value, sizeof(new->name));
798 exp_assop (code, dst, src)
803 etree_type value, *new;
805 value.assign.type.node_code = code;
808 value.assign.src = src;
809 value.assign.dst = dst;
810 value.assign.type.node_class = etree_assign;
813 if (exp_fold_tree_no_dot(&value, &result)) {
814 return exp_intop(result);
817 new = (etree_type*) stat_alloc (sizeof (new->assign));
818 memcpy((char *)new, (char *)&value, sizeof(new->assign));
822 /* Handle PROVIDE. */
825 exp_provide (dst, src)
831 n = (etree_type *) stat_alloc (sizeof (n->assign));
832 n->assign.type.node_code = '=';
833 n->assign.type.node_class = etree_provide;
842 exp_assert (exp, message)
848 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
849 n->assert_s.type.node_code = '!';
850 n->assert_s.type.node_class = etree_assert;
851 n->assert_s.child = exp;
852 n->assert_s.message = message;
857 exp_print_tree (tree)
860 switch (tree->type.node_class) {
862 minfo ("0x%v", tree->value.value);
865 if (tree->rel.section->owner != NULL)
866 minfo ("%B:", tree->rel.section->owner);
867 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
871 if (tree->assign.dst->sdefs != (asymbol *)NULL){
872 fprintf(config.map_file,"%s (%x) ",tree->assign.dst->name,
873 tree->assign.dst->sdefs->value);
876 fprintf(config.map_file,"%s (UNDEFINED)",tree->assign.dst->name);
879 fprintf(config.map_file,"%s",tree->assign.dst);
880 exp_print_token(tree->type.node_code);
881 exp_print_tree(tree->assign.src);
884 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
885 exp_print_tree (tree->assign.src);
886 fprintf (config.map_file, ")");
889 fprintf(config.map_file,"(");
890 exp_print_tree(tree->binary.lhs);
891 exp_print_token(tree->type.node_code);
892 exp_print_tree(tree->binary.rhs);
893 fprintf(config.map_file,")");
896 exp_print_tree(tree->trinary.cond);
897 fprintf(config.map_file,"?");
898 exp_print_tree(tree->trinary.lhs);
899 fprintf(config.map_file,":");
900 exp_print_tree(tree->trinary.rhs);
903 exp_print_token(tree->unary.type.node_code);
904 if (tree->unary.child)
906 fprintf(config.map_file,"(");
907 exp_print_tree(tree->unary.child);
908 fprintf(config.map_file,")");
914 fprintf (config.map_file, "ASSERT (");
915 exp_print_tree (tree->assert_s.child);
916 fprintf (config.map_file, ", %s)", tree->assert_s.message);
920 fprintf(config.map_file,"????????");
923 if (tree->type.node_code == NAME) {
924 fprintf(config.map_file,"%s", tree->name.name);
927 exp_print_token(tree->type.node_code);
929 fprintf(config.map_file,"(%s)", tree->name.name);
939 exp_get_vma (tree, def, name, allocation_done)
943 lang_phase_type allocation_done;
949 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
950 if (! r.valid_p && name != NULL)
951 einfo (_("%F%S nonconstant expression for %s\n"), name);
959 exp_get_value_int (tree,def,name, allocation_done)
963 lang_phase_type allocation_done;
965 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);
970 exp_get_abs_int (tree, def, name, allocation_done)
972 int def ATTRIBUTE_UNUSED;
974 lang_phase_type allocation_done;
976 etree_value_type res;
977 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
981 res.value += res.section->bfd_section->vma;
984 einfo (_("%F%S non constant expression for %s\n"),name);