1 /* This module handles expression trees.
2 Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
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
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
26 is done by keeping all values in a struct (an etree_value_type)
27 which contains a value, a section to which it is relative and a
41 static void exp_print_token PARAMS ((token_code_type code));
42 static void make_abs PARAMS ((etree_value_type *ptr));
43 static etree_value_type new_abs PARAMS ((bfd_vma value));
44 static void check PARAMS ((lang_output_section_statement_type *os,
45 const char *name, const char *op));
46 static etree_value_type new_rel
47 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
48 static etree_value_type new_rel_from_section
49 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
50 static etree_value_type fold_binary
51 PARAMS ((etree_type *tree,
52 lang_output_section_statement_type *current_section,
53 lang_phase_type allocation_done,
54 bfd_vma dot, bfd_vma *dotp));
55 static etree_value_type fold_name
56 PARAMS ((etree_type *tree,
57 lang_output_section_statement_type *current_section,
58 lang_phase_type allocation_done,
60 static etree_value_type exp_fold_tree_no_dot
61 PARAMS ((etree_type *tree,
62 lang_output_section_statement_type *current_section,
63 lang_phase_type allocation_done));
66 exp_print_token (code)
74 { REL, "relocateable" },
94 { SECTIONS, "SECTIONS" },
95 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
99 { LOADADDR, "LOADADDR" },
100 { MEMORY, "MEMORY" },
101 { DEFINED, "DEFINED" },
102 { TARGET_K, "TARGET" },
103 { SEARCH_DIR, "SEARCH_DIR" },
115 for (idx = 0; table[idx].name != (char *) NULL; idx++)
117 if (table[idx].code == code)
119 fprintf (config.map_file, "%s", table[idx].name);
123 /* Not in table, just print it alone */
124 fprintf (config.map_file, "%c", code);
129 etree_value_type *ptr;
131 asection *s = ptr->section->bfd_section;
132 ptr->value += s->vma;
133 ptr->section = abs_output_section;
136 static etree_value_type
140 etree_value_type new;
142 new.section = abs_output_section;
149 lang_output_section_statement_type *os;
154 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
156 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
163 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
164 new->type.node_code = INT;
165 new->value.value = value;
166 new->type.node_class = etree_value;
171 /* Build an expression representing an unnamed relocateable value. */
174 exp_relop (section, value)
178 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
179 new->type.node_code = REL;
180 new->type.node_class = etree_rel;
181 new->rel.section = section;
182 new->rel.value = value;
186 static etree_value_type
187 new_rel (value, section)
189 lang_output_section_statement_type *section;
191 etree_value_type new;
194 new.section = section;
198 static etree_value_type
199 new_rel_from_section (value, section)
201 lang_output_section_statement_type *section;
203 etree_value_type new;
206 new.section = section;
208 new.value -= section->bfd_section->vma;
213 static etree_value_type
214 fold_binary (tree, current_section, allocation_done, dot, dotp)
216 lang_output_section_statement_type *current_section;
217 lang_phase_type allocation_done;
221 etree_value_type result;
223 result = exp_fold_tree (tree->binary.lhs, current_section,
224 allocation_done, dot, dotp);
227 etree_value_type other;
229 other = exp_fold_tree (tree->binary.rhs,
231 allocation_done, dot, dotp);
234 /* If the values are from different sections, or this is an
235 absolute expression, make both the source arguments
236 absolute. However, adding or subtracting an absolute
237 value from a relative value is meaningful, and is an
239 if (current_section != abs_output_section
240 && (other.section == abs_output_section
241 || (result.section == abs_output_section
242 && tree->type.node_code == '+'))
243 && (tree->type.node_code == '+'
244 || tree->type.node_code == '-'))
246 etree_value_type hold;
248 /* If there is only one absolute term, make sure it is the
250 if (other.section != abs_output_section)
257 else if (result.section != other.section
258 || current_section == abs_output_section)
264 switch (tree->type.node_code)
267 if (other.value == 0)
268 einfo (_("%F%S %% by zero\n"));
269 result.value = ((bfd_signed_vma) result.value
270 % (bfd_signed_vma) other.value);
274 if (other.value == 0)
275 einfo (_("%F%S / by zero\n"));
276 result.value = ((bfd_signed_vma) result.value
277 / (bfd_signed_vma) other.value);
280 #define BOP(x,y) case x : result.value = result.value y other.value; break;
299 if (result.value < other.value)
304 if (result.value > other.value)
314 result.valid_p = false;
324 etree_value_type new;
329 static etree_value_type
330 fold_name (tree, current_section, allocation_done, dot)
332 lang_output_section_statement_type *current_section;
333 lang_phase_type allocation_done;
336 etree_value_type result;
337 switch (tree->type.node_code)
340 if (allocation_done != lang_first_phase_enum)
342 result = new_abs ((bfd_vma)
343 bfd_sizeof_headers (output_bfd,
344 link_info.relocateable));
348 result.valid_p = false;
352 if (allocation_done == lang_first_phase_enum)
353 result.valid_p = false;
356 struct bfd_link_hash_entry *h;
358 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
361 result.value = (h != (struct bfd_link_hash_entry *) NULL
362 && (h->type == bfd_link_hash_defined
363 || h->type == bfd_link_hash_defweak
364 || h->type == bfd_link_hash_common));
366 result.valid_p = true;
370 result.valid_p = false;
371 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
373 if (allocation_done != lang_first_phase_enum)
374 result = new_rel_from_section (dot, current_section);
378 else if (allocation_done != lang_first_phase_enum)
380 struct bfd_link_hash_entry *h;
382 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
386 && (h->type == bfd_link_hash_defined
387 || h->type == bfd_link_hash_defweak))
389 if (bfd_is_abs_section (h->u.def.section))
390 result = new_abs (h->u.def.value);
391 else if (allocation_done == lang_final_phase_enum
392 || allocation_done == lang_allocating_phase_enum)
394 asection *output_section;
396 output_section = h->u.def.section->output_section;
397 if (output_section == NULL)
398 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
402 lang_output_section_statement_type *os;
404 os = (lang_output_section_statement_lookup
405 (bfd_get_section_name (output_bfd,
408 /* FIXME: Is this correct if this section is
409 being linked with -R? */
410 result = new_rel ((h->u.def.value
411 + h->u.def.section->output_offset),
416 else if (allocation_done == lang_final_phase_enum)
417 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
423 if (allocation_done != lang_first_phase_enum)
425 lang_output_section_statement_type *os;
427 os = lang_output_section_find (tree->name.name);
428 check (os, tree->name.name, "ADDR");
429 result = new_rel (0, os);
436 if (allocation_done != lang_first_phase_enum)
438 lang_output_section_statement_type *os;
440 os = lang_output_section_find (tree->name.name);
441 check (os, tree->name.name, "LOADADDR");
442 if (os->load_base == NULL)
443 result = new_rel (0, os);
445 result = exp_fold_tree_no_dot (os->load_base,
454 if (allocation_done != lang_first_phase_enum)
456 int opb = bfd_octets_per_byte (output_bfd);
457 lang_output_section_statement_type *os;
459 os = lang_output_section_find (tree->name.name);
460 check (os, tree->name.name, "SIZEOF");
461 result = new_abs (os->bfd_section->_raw_size / opb);
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_p = 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_p = 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->assert_s.child,
510 allocation_done, dot, dotp);
514 einfo ("%F%P: %s\n", tree->assert_s.message);
520 result = exp_fold_tree (tree->unary.child,
522 allocation_done, dot, dotp);
525 switch (tree->type.node_code)
528 if (allocation_done != lang_first_phase_enum)
529 result = new_rel_from_section (ALIGN_N (dot, result.value),
532 result.valid_p = false;
536 if (allocation_done != lang_first_phase_enum && result.valid_p)
538 result.value += result.section->bfd_section->vma;
539 result.section = abs_output_section;
542 result.valid_p = false;
547 result.value = ~result.value;
552 result.value = !result.value;
557 result.value = -result.value;
561 /* Return next place aligned to value. */
562 if (allocation_done == lang_allocating_phase_enum)
565 result.value = ALIGN_N (dot, result.value);
568 result.valid_p = false;
579 result = exp_fold_tree (tree->trinary.cond, current_section,
580 allocation_done, dot, dotp);
582 result = exp_fold_tree ((result.value
584 : tree->trinary.rhs),
586 allocation_done, dot, dotp);
590 result = fold_binary (tree, current_section, allocation_done,
597 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
599 /* Assignment to dot can only be done during allocation */
600 if (tree->type.node_class != etree_assign)
601 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
602 if (allocation_done == lang_allocating_phase_enum
603 || (allocation_done == lang_final_phase_enum
604 && current_section == abs_output_section))
606 result = exp_fold_tree (tree->assign.src,
608 lang_allocating_phase_enum, dot,
610 if (! result.valid_p)
611 einfo (_("%F%S invalid assignment to location counter\n"));
614 if (current_section == NULL)
615 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
620 nextdot = (result.value
621 + current_section->bfd_section->vma);
623 && current_section != abs_output_section)
625 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
636 result = exp_fold_tree (tree->assign.src,
637 current_section, allocation_done,
642 struct bfd_link_hash_entry *h;
644 if (tree->type.node_class == etree_assign)
648 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
649 create, false, false);
650 if (h == (struct bfd_link_hash_entry *) NULL)
652 if (tree->type.node_class == etree_assign)
653 einfo (_("%P%F:%s: hash creation failed\n"),
656 else if (tree->type.node_class == etree_provide
657 && h->type != bfd_link_hash_undefined
658 && h->type != bfd_link_hash_common)
660 /* Do nothing. The symbol was defined by some
665 /* FIXME: Should we worry if the symbol is already
667 h->type = bfd_link_hash_defined;
668 h->u.def.value = result.value;
669 h->u.def.section = result.section->bfd_section;
670 if (tree->type.node_class == etree_provide)
671 tree->type.node_class = etree_provided;
678 result = fold_name (tree, current_section, allocation_done, dot);
689 static etree_value_type
690 exp_fold_tree_no_dot (tree, current_section, allocation_done)
692 lang_output_section_statement_type *current_section;
693 lang_phase_type allocation_done;
695 return exp_fold_tree (tree, current_section, allocation_done,
696 (bfd_vma) 0, (bfd_vma *) NULL);
700 exp_binop (code, lhs, rhs)
705 etree_type value, *new;
708 value.type.node_code = code;
709 value.binary.lhs = lhs;
710 value.binary.rhs = rhs;
711 value.type.node_class = etree_binary;
712 r = exp_fold_tree_no_dot (&value,
714 lang_first_phase_enum);
717 return exp_intop (r.value);
719 new = (etree_type *) stat_alloc (sizeof (new->binary));
720 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
725 exp_trinop (code, cond, lhs, rhs)
731 etree_type value, *new;
733 value.type.node_code = code;
734 value.trinary.lhs = lhs;
735 value.trinary.cond = cond;
736 value.trinary.rhs = rhs;
737 value.type.node_class = etree_trinary;
738 r = exp_fold_tree_no_dot (&value,
739 (lang_output_section_statement_type *) NULL,
740 lang_first_phase_enum);
743 return exp_intop (r.value);
745 new = (etree_type *) stat_alloc (sizeof (new->trinary));
746 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
751 exp_unop (code, child)
755 etree_type value, *new;
758 value.unary.type.node_code = code;
759 value.unary.child = child;
760 value.unary.type.node_class = etree_unary;
761 r = exp_fold_tree_no_dot (&value, abs_output_section,
762 lang_first_phase_enum);
765 return exp_intop (r.value);
767 new = (etree_type *) stat_alloc (sizeof (new->unary));
768 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
773 exp_nameop (code, name)
777 etree_type value, *new;
779 value.name.type.node_code = code;
780 value.name.name = name;
781 value.name.type.node_class = etree_name;
783 r = exp_fold_tree_no_dot (&value,
784 (lang_output_section_statement_type *) NULL,
785 lang_first_phase_enum);
788 return exp_intop (r.value);
790 new = (etree_type *) stat_alloc (sizeof (new->name));
791 memcpy ((char *) new, (char *) &value, sizeof (new->name));
797 exp_assop (code, dst, src)
802 etree_type value, *new;
804 value.assign.type.node_code = code;
806 value.assign.src = src;
807 value.assign.dst = dst;
808 value.assign.type.node_class = etree_assign;
811 if (exp_fold_tree_no_dot (&value, &result))
813 return exp_intop (result);
816 new = (etree_type *) stat_alloc (sizeof (new->assign));
817 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
821 /* Handle PROVIDE. */
824 exp_provide (dst, src)
830 n = (etree_type *) stat_alloc (sizeof (n->assign));
831 n->assign.type.node_code = '=';
832 n->assign.type.node_class = etree_provide;
841 exp_assert (exp, message)
847 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
848 n->assert_s.type.node_code = '!';
849 n->assert_s.type.node_class = etree_assert;
850 n->assert_s.child = exp;
851 n->assert_s.message = message;
856 exp_print_tree (tree)
859 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)
873 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
874 tree->assign.dst->sdefs->value);
878 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
881 fprintf (config.map_file, "%s", tree->assign.dst);
882 exp_print_token (tree->type.node_code);
883 exp_print_tree (tree->assign.src);
887 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
888 exp_print_tree (tree->assign.src);
889 fprintf (config.map_file, ")");
892 fprintf (config.map_file, "(");
893 exp_print_tree (tree->binary.lhs);
894 exp_print_token (tree->type.node_code);
895 exp_print_tree (tree->binary.rhs);
896 fprintf (config.map_file, ")");
899 exp_print_tree (tree->trinary.cond);
900 fprintf (config.map_file, "?");
901 exp_print_tree (tree->trinary.lhs);
902 fprintf (config.map_file, ":");
903 exp_print_tree (tree->trinary.rhs);
906 exp_print_token (tree->unary.type.node_code);
907 if (tree->unary.child)
909 fprintf (config.map_file, "(");
910 exp_print_tree (tree->unary.child);
911 fprintf (config.map_file, ")");
916 fprintf (config.map_file, "ASSERT (");
917 exp_print_tree (tree->assert_s.child);
918 fprintf (config.map_file, ", %s)", tree->assert_s.message);
922 fprintf (config.map_file, "????????");
925 if (tree->type.node_code == NAME)
927 fprintf (config.map_file, "%s", tree->name.name);
931 exp_print_token (tree->type.node_code);
933 fprintf (config.map_file, "(%s)", tree->name.name);
943 exp_get_vma (tree, def, name, allocation_done)
947 lang_phase_type allocation_done;
953 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
954 if (! r.valid_p && name != NULL)
955 einfo (_("%F%S nonconstant expression for %s\n"), name);
963 exp_get_value_int (tree, def, name, allocation_done)
967 lang_phase_type allocation_done;
969 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
973 exp_get_abs_int (tree, def, name, allocation_done)
975 int def ATTRIBUTE_UNUSED;
977 lang_phase_type allocation_done;
979 etree_value_type res;
980 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
984 res.value += res.section->bfd_section->vma;
988 einfo (_("%F%S non constant expression for %s\n"), name);