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 etree_value_type exp_fold_tree_no_dot
45 (etree_type *, lang_output_section_statement_type *, lang_phase_type);
46 static bfd_vma align_n
49 struct exp_data_seg exp_data_seg;
51 segment_type *segments;
53 /* Print the string representation of the given token. Surround it
54 with spaces if INFIX_P is TRUE. */
57 exp_print_token (token_code_type code, int infix_p)
91 { SECTIONS, "SECTIONS" },
92 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
94 { DEFINED, "DEFINED" },
95 { TARGET_K, "TARGET" },
96 { SEARCH_DIR, "SEARCH_DIR" },
100 { SIZEOF, "SIZEOF" },
102 { LOADADDR, "LOADADDR" },
104 { REL, "relocatable" },
105 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
106 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
107 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
108 { SEGMENT_START, "SEGMENT_START" }
112 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
113 if (table[idx].code == code)
117 fputc (' ', config.map_file);
119 if (idx < ARRAY_SIZE (table))
120 fputs (table[idx].name, config.map_file);
122 fputc (code, config.map_file);
124 fprintf (config.map_file, "<code %d>", code);
127 fputc (' ', config.map_file);
131 make_abs (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
139 new_abs (bfd_vma value)
141 etree_value_type new;
143 new.section = abs_output_section;
149 exp_intop (bfd_vma value)
151 etree_type *new = stat_alloc (sizeof (new->value));
152 new->type.node_code = INT;
153 new->value.value = value;
154 new->value.str = NULL;
155 new->type.node_class = etree_value;
160 exp_bigintop (bfd_vma value, char *str)
162 etree_type *new = stat_alloc (sizeof (new->value));
163 new->type.node_code = INT;
164 new->value.value = value;
165 new->value.str = str;
166 new->type.node_class = etree_value;
170 /* Build an expression representing an unnamed relocatable value. */
173 exp_relop (asection *section, bfd_vma value)
175 etree_type *new = stat_alloc (sizeof (new->rel));
176 new->type.node_code = REL;
177 new->type.node_class = etree_rel;
178 new->rel.section = section;
179 new->rel.value = value;
183 static etree_value_type
184 new_rel (bfd_vma value,
186 lang_output_section_statement_type *section)
188 etree_value_type new;
192 new.section = section;
196 static etree_value_type
197 new_rel_from_section (bfd_vma value,
198 lang_output_section_statement_type *section)
200 etree_value_type new;
204 new.section = section;
206 new.value -= section->bfd_section->vma;
211 static etree_value_type
212 fold_unary (etree_type *tree,
213 lang_output_section_statement_type *current_section,
214 lang_phase_type allocation_done,
218 etree_value_type result;
220 result = exp_fold_tree (tree->unary.child,
222 allocation_done, dot, dotp);
225 switch (tree->type.node_code)
228 if (allocation_done != lang_first_phase_enum)
229 result = new_rel_from_section (align_n (dot, result.value),
232 result.valid_p = FALSE;
236 if (allocation_done != lang_first_phase_enum)
238 result.value += result.section->bfd_section->vma;
239 result.section = abs_output_section;
242 result.valid_p = FALSE;
247 result.value = ~result.value;
252 result.value = !result.value;
257 result.value = -result.value;
261 /* Return next place aligned to value. */
262 if (allocation_done == lang_allocating_phase_enum)
265 result.value = align_n (dot, result.value);
268 result.valid_p = FALSE;
271 case DATA_SEGMENT_END:
272 if (allocation_done != lang_first_phase_enum
273 && current_section == abs_output_section
274 && (exp_data_seg.phase == exp_dataseg_align_seen
275 || exp_data_seg.phase == exp_dataseg_relro_seen
276 || exp_data_seg.phase == exp_dataseg_adjust
277 || exp_data_seg.phase == exp_dataseg_relro_adjust
278 || allocation_done != lang_allocating_phase_enum))
280 if (exp_data_seg.phase == exp_dataseg_align_seen
281 || exp_data_seg.phase == exp_dataseg_relro_seen)
283 exp_data_seg.phase = exp_dataseg_end_seen;
284 exp_data_seg.end = result.value;
288 result.valid_p = FALSE;
300 static etree_value_type
301 fold_binary (etree_type *tree,
302 lang_output_section_statement_type *current_section,
303 lang_phase_type allocation_done,
307 etree_value_type result;
309 result = exp_fold_tree (tree->binary.lhs, current_section,
310 allocation_done, dot, dotp);
312 /* The SEGMENT_START operator is special because its first
313 operand is a string, not the name of a symbol. */
314 if (result.valid_p && tree->type.node_code == SEGMENT_START)
316 const char *segment_name;
318 /* Check to see if the user has overridden the default
320 segment_name = tree->binary.rhs->name.name;
321 for (seg = segments; seg; seg = seg->next)
322 if (strcmp (seg->name, segment_name) == 0)
325 result.value = seg->value;
327 result.section = NULL;
331 else if (result.valid_p)
333 etree_value_type other;
335 other = exp_fold_tree (tree->binary.rhs,
337 allocation_done, dot, dotp);
340 /* If the values are from different sections, or this is an
341 absolute expression, make both the source arguments
342 absolute. However, adding or subtracting an absolute
343 value from a relative value is meaningful, and is an
345 if (current_section != abs_output_section
346 && (other.section == abs_output_section
347 || (result.section == abs_output_section
348 && tree->type.node_code == '+'))
349 && (tree->type.node_code == '+'
350 || tree->type.node_code == '-'))
352 if (other.section != abs_output_section)
354 /* Keep the section of the other term. */
355 if (tree->type.node_code == '+')
356 other.value = result.value + other.value;
358 other.value = result.value - other.value;
362 else if (result.section != other.section
363 || current_section == abs_output_section)
369 switch (tree->type.node_code)
372 if (other.value == 0)
373 einfo (_("%F%S %% by zero\n"));
374 result.value = ((bfd_signed_vma) result.value
375 % (bfd_signed_vma) other.value);
379 if (other.value == 0)
380 einfo (_("%F%S / by zero\n"));
381 result.value = ((bfd_signed_vma) result.value
382 / (bfd_signed_vma) other.value);
385 #define BOP(x,y) case x : result.value = result.value y other.value; break;
404 if (result.value < other.value)
409 if (result.value > other.value)
414 result.value = align_n (result.value, other.value);
417 case DATA_SEGMENT_ALIGN:
418 if (allocation_done != lang_first_phase_enum
419 && current_section == abs_output_section
420 && (exp_data_seg.phase == exp_dataseg_none
421 || exp_data_seg.phase == exp_dataseg_adjust
422 || exp_data_seg.phase == exp_dataseg_relro_adjust
423 || allocation_done != lang_allocating_phase_enum))
425 bfd_vma maxpage = result.value;
427 result.value = align_n (dot, maxpage);
428 if (exp_data_seg.phase == exp_dataseg_relro_adjust)
429 result.value = exp_data_seg.base;
430 else if (exp_data_seg.phase != exp_dataseg_adjust)
432 result.value += dot & (maxpage - 1);
433 if (allocation_done == lang_allocating_phase_enum)
435 exp_data_seg.phase = exp_dataseg_align_seen;
436 exp_data_seg.base = result.value;
437 exp_data_seg.pagesize = other.value;
438 exp_data_seg.relro_end = 0;
441 else if (other.value < maxpage)
442 result.value += (dot + other.value - 1)
443 & (maxpage - other.value);
446 result.valid_p = FALSE;
449 case DATA_SEGMENT_RELRO_END:
450 if (allocation_done != lang_first_phase_enum
451 && (exp_data_seg.phase == exp_dataseg_align_seen
452 || exp_data_seg.phase == exp_dataseg_adjust
453 || exp_data_seg.phase == exp_dataseg_relro_adjust
454 || allocation_done != lang_allocating_phase_enum))
456 if (exp_data_seg.phase == exp_dataseg_align_seen
457 || exp_data_seg.phase == exp_dataseg_relro_adjust)
458 exp_data_seg.relro_end
459 = result.value + other.value;
460 if (exp_data_seg.phase == exp_dataseg_relro_adjust
461 && (exp_data_seg.relro_end
462 & (exp_data_seg.pagesize - 1)))
464 exp_data_seg.relro_end += exp_data_seg.pagesize - 1;
465 exp_data_seg.relro_end &= ~(exp_data_seg.pagesize - 1);
466 result.value = exp_data_seg.relro_end - other.value;
468 if (exp_data_seg.phase == exp_dataseg_align_seen)
469 exp_data_seg.phase = exp_dataseg_relro_seen;
472 result.valid_p = FALSE;
481 result.valid_p = FALSE;
488 static etree_value_type
489 fold_trinary (etree_type *tree,
490 lang_output_section_statement_type *current_section,
491 lang_phase_type allocation_done,
495 etree_value_type result;
497 result = exp_fold_tree (tree->trinary.cond, current_section,
498 allocation_done, dot, dotp);
500 result = exp_fold_tree ((result.value
502 : tree->trinary.rhs),
504 allocation_done, dot, dotp);
509 static etree_value_type
510 fold_name (etree_type *tree,
511 lang_output_section_statement_type *current_section,
512 lang_phase_type allocation_done,
515 etree_value_type result;
517 result.valid_p = FALSE;
519 switch (tree->type.node_code)
522 if (allocation_done != lang_first_phase_enum)
523 result = new_abs (bfd_sizeof_headers (output_bfd,
524 link_info.relocatable));
527 if (allocation_done == lang_first_phase_enum)
528 lang_track_definedness (tree->name.name);
531 struct bfd_link_hash_entry *h;
533 = lang_symbol_definition_iteration (tree->name.name);
535 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
538 result.value = (h != NULL
539 && (h->type == bfd_link_hash_defined
540 || h->type == bfd_link_hash_defweak
541 || h->type == bfd_link_hash_common)
542 && (def_iteration == lang_statement_iteration
543 || def_iteration == -1));
544 result.section = abs_output_section;
545 result.valid_p = TRUE;
549 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
551 if (allocation_done != lang_first_phase_enum)
552 result = new_rel_from_section (dot, current_section);
554 else if (allocation_done != lang_first_phase_enum)
556 struct bfd_link_hash_entry *h;
558 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
562 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
563 else if (h->type == bfd_link_hash_defined
564 || h->type == bfd_link_hash_defweak)
566 if (bfd_is_abs_section (h->u.def.section))
567 result = new_abs (h->u.def.value);
568 else if (allocation_done == lang_final_phase_enum
569 || allocation_done == lang_allocating_phase_enum)
571 asection *output_section;
573 output_section = h->u.def.section->output_section;
574 if (output_section == NULL)
575 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
579 lang_output_section_statement_type *os;
581 os = (lang_output_section_statement_lookup
582 (bfd_get_section_name (output_bfd,
585 /* FIXME: Is this correct if this section is
586 being linked with -R? */
587 result = new_rel ((h->u.def.value
588 + h->u.def.section->output_offset),
594 else if (allocation_done == lang_final_phase_enum)
595 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
597 else if (h->type == bfd_link_hash_new)
599 h->type = bfd_link_hash_undefined;
600 h->u.undef.abfd = NULL;
601 if (h->u.undef.next == NULL)
602 bfd_link_add_undef (link_info.hash, h);
608 if (allocation_done != lang_first_phase_enum)
610 lang_output_section_statement_type *os;
612 os = lang_output_section_find (tree->name.name);
613 if (os && os->processed > 0)
614 result = new_rel (0, NULL, os);
619 if (allocation_done != lang_first_phase_enum)
621 lang_output_section_statement_type *os;
623 os = lang_output_section_find (tree->name.name);
624 if (os && os->processed != 0)
626 if (os->load_base == NULL)
627 result = new_rel (0, NULL, os);
629 result = exp_fold_tree_no_dot (os->load_base,
637 if (allocation_done != lang_first_phase_enum)
639 int opb = bfd_octets_per_byte (output_bfd);
640 lang_output_section_statement_type *os;
642 os = lang_output_section_find (tree->name.name);
643 if (os && os->processed > 0)
644 result = new_abs (os->bfd_section->size / opb);
657 exp_fold_tree (etree_type *tree,
658 lang_output_section_statement_type *current_section,
659 lang_phase_type allocation_done,
663 etree_value_type result;
667 result.valid_p = FALSE;
671 switch (tree->type.node_class)
674 result = new_rel (tree->value.value, tree->value.str, current_section);
678 if (allocation_done != lang_final_phase_enum)
679 result.valid_p = FALSE;
681 result = new_rel ((tree->rel.value
682 + tree->rel.section->output_section->vma
683 + tree->rel.section->output_offset),
689 result = exp_fold_tree (tree->assert_s.child,
691 allocation_done, dot, dotp);
695 einfo ("%X%P: %s\n", tree->assert_s.message);
701 result = fold_unary (tree, current_section, allocation_done,
706 result = fold_binary (tree, current_section, allocation_done,
711 result = fold_trinary (tree, current_section, allocation_done,
718 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
720 /* Assignment to dot can only be done during allocation. */
721 if (tree->type.node_class != etree_assign)
722 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
723 if (allocation_done == lang_allocating_phase_enum
724 || (allocation_done == lang_final_phase_enum
725 && current_section == abs_output_section))
727 result = exp_fold_tree (tree->assign.src,
729 allocation_done, dot,
731 if (! result.valid_p)
732 einfo (_("%F%S invalid assignment to location counter\n"));
735 if (current_section == NULL)
736 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
741 nextdot = (result.value
742 + current_section->bfd_section->vma);
744 && current_section != abs_output_section)
745 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
755 result = exp_fold_tree (tree->assign.src,
756 current_section, allocation_done,
761 struct bfd_link_hash_entry *h;
763 if (tree->type.node_class == etree_assign)
767 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
768 create, FALSE, TRUE);
772 einfo (_("%P%F:%s: hash creation failed\n"),
775 else if (tree->type.node_class == etree_provide
776 && h->type != bfd_link_hash_new
777 && h->type != bfd_link_hash_undefined
778 && h->type != bfd_link_hash_common)
780 /* Do nothing. The symbol was defined by some
785 /* FIXME: Should we worry if the symbol is already
787 lang_update_definedness (tree->assign.dst, h);
788 h->type = bfd_link_hash_defined;
789 h->u.def.value = result.value;
790 h->u.def.section = result.section->bfd_section;
791 if (tree->type.node_class == etree_provide)
792 tree->type.node_class = etree_provided;
799 result = fold_name (tree, current_section, allocation_done, dot);
810 static etree_value_type
811 exp_fold_tree_no_dot (etree_type *tree,
812 lang_output_section_statement_type *current_section,
813 lang_phase_type allocation_done)
815 return exp_fold_tree (tree, current_section, allocation_done, 0, NULL);
819 exp_binop (int code, etree_type *lhs, etree_type *rhs)
821 etree_type value, *new;
824 value.type.node_code = code;
825 value.binary.lhs = lhs;
826 value.binary.rhs = rhs;
827 value.type.node_class = etree_binary;
828 r = exp_fold_tree_no_dot (&value,
830 lang_first_phase_enum);
833 return exp_intop (r.value);
835 new = stat_alloc (sizeof (new->binary));
836 memcpy (new, &value, sizeof (new->binary));
841 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
843 etree_type value, *new;
845 value.type.node_code = code;
846 value.trinary.lhs = lhs;
847 value.trinary.cond = cond;
848 value.trinary.rhs = rhs;
849 value.type.node_class = etree_trinary;
850 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
852 return exp_intop (r.value);
854 new = stat_alloc (sizeof (new->trinary));
855 memcpy (new, &value, sizeof (new->trinary));
860 exp_unop (int code, etree_type *child)
862 etree_type value, *new;
865 value.unary.type.node_code = code;
866 value.unary.child = child;
867 value.unary.type.node_class = etree_unary;
868 r = exp_fold_tree_no_dot (&value, abs_output_section,
869 lang_first_phase_enum);
871 return exp_intop (r.value);
873 new = stat_alloc (sizeof (new->unary));
874 memcpy (new, &value, sizeof (new->unary));
879 exp_nameop (int code, const char *name)
881 etree_type value, *new;
883 value.name.type.node_code = code;
884 value.name.name = name;
885 value.name.type.node_class = etree_name;
887 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
889 return exp_intop (r.value);
891 new = stat_alloc (sizeof (new->name));
892 memcpy (new, &value, sizeof (new->name));
898 exp_assop (int code, const char *dst, etree_type *src)
900 etree_type value, *new;
902 value.assign.type.node_code = code;
904 value.assign.src = src;
905 value.assign.dst = dst;
906 value.assign.type.node_class = etree_assign;
909 if (exp_fold_tree_no_dot (&value, &result))
910 return exp_intop (result);
912 new = stat_alloc (sizeof (new->assign));
913 memcpy (new, &value, sizeof (new->assign));
917 /* Handle PROVIDE. */
920 exp_provide (const char *dst, etree_type *src)
924 n = stat_alloc (sizeof (n->assign));
925 n->assign.type.node_code = '=';
926 n->assign.type.node_class = etree_provide;
935 exp_assert (etree_type *exp, const char *message)
939 n = stat_alloc (sizeof (n->assert_s));
940 n->assert_s.type.node_code = '!';
941 n->assert_s.type.node_class = etree_assert;
942 n->assert_s.child = exp;
943 n->assert_s.message = message;
948 exp_print_tree (etree_type *tree)
950 if (config.map_file == NULL)
951 config.map_file = stderr;
955 minfo ("NULL TREE\n");
959 switch (tree->type.node_class)
962 minfo ("0x%v", tree->value.value);
965 if (tree->rel.section->owner != NULL)
966 minfo ("%B:", tree->rel.section->owner);
967 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
971 if (tree->assign.dst->sdefs != NULL)
972 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
973 tree->assign.dst->sdefs->value);
975 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
977 fprintf (config.map_file, "%s", tree->assign.dst);
978 exp_print_token (tree->type.node_code, TRUE);
979 exp_print_tree (tree->assign.src);
983 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
984 exp_print_tree (tree->assign.src);
985 fprintf (config.map_file, ")");
988 fprintf (config.map_file, "(");
989 exp_print_tree (tree->binary.lhs);
990 exp_print_token (tree->type.node_code, TRUE);
991 exp_print_tree (tree->binary.rhs);
992 fprintf (config.map_file, ")");
995 exp_print_tree (tree->trinary.cond);
996 fprintf (config.map_file, "?");
997 exp_print_tree (tree->trinary.lhs);
998 fprintf (config.map_file, ":");
999 exp_print_tree (tree->trinary.rhs);
1002 exp_print_token (tree->unary.type.node_code, FALSE);
1003 if (tree->unary.child)
1005 fprintf (config.map_file, " (");
1006 exp_print_tree (tree->unary.child);
1007 fprintf (config.map_file, ")");
1012 fprintf (config.map_file, "ASSERT (");
1013 exp_print_tree (tree->assert_s.child);
1014 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1018 fprintf (config.map_file, "????????");
1021 if (tree->type.node_code == NAME)
1023 fprintf (config.map_file, "%s", tree->name.name);
1027 exp_print_token (tree->type.node_code, FALSE);
1028 if (tree->name.name)
1029 fprintf (config.map_file, " (%s)", tree->name.name);
1039 exp_get_vma (etree_type *tree,
1042 lang_phase_type allocation_done)
1048 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1049 if (! r.valid_p && name != NULL)
1050 einfo (_("%F%S nonconstant expression for %s\n"), name);
1058 exp_get_value_int (etree_type *tree,
1061 lang_phase_type allocation_done)
1063 return exp_get_vma (tree, def, name, allocation_done);
1067 exp_get_fill (etree_type *tree,
1070 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);
1084 if (r.str != NULL && (len = strlen (r.str)) != 0)
1088 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1089 fill->size = (len + 1) / 2;
1099 digit = (digit - 'A' + '0' + 10) & 0xf;
1113 fill = xmalloc (4 + sizeof (*fill) - 1);
1115 fill->data[0] = (val >> 24) & 0xff;
1116 fill->data[1] = (val >> 16) & 0xff;
1117 fill->data[2] = (val >> 8) & 0xff;
1118 fill->data[3] = (val >> 0) & 0xff;
1125 exp_get_abs_int (etree_type *tree,
1126 int def ATTRIBUTE_UNUSED,
1128 lang_phase_type allocation_done)
1130 etree_value_type res;
1131 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1134 res.value += res.section->bfd_section->vma;
1136 einfo (_("%F%S non constant expression for %s\n"), name);
1142 align_n (bfd_vma value, bfd_vma align)
1147 value = (value + align - 1) / align;
1148 return value * align;