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 /* Print the string representation of the given token. Surround it
52 with spaces if INFIX_P is TRUE. */
55 exp_print_token (token_code_type code, int infix_p)
89 { SECTIONS, "SECTIONS" },
90 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
92 { DEFINED, "DEFINED" },
93 { TARGET_K, "TARGET" },
94 { SEARCH_DIR, "SEARCH_DIR" },
100 { LOADADDR, "LOADADDR" },
102 { REL, "relocatable" },
103 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
104 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
105 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
109 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
110 if (table[idx].code == code)
114 fputc (' ', config.map_file);
116 if (idx < ARRAY_SIZE (table))
117 fputs (table[idx].name, config.map_file);
119 fputc (code, config.map_file);
121 fprintf (config.map_file, "<code %d>", code);
124 fputc (' ', config.map_file);
128 make_abs (etree_value_type *ptr)
130 asection *s = ptr->section->bfd_section;
131 ptr->value += s->vma;
132 ptr->section = abs_output_section;
135 static etree_value_type
136 new_abs (bfd_vma value)
138 etree_value_type new;
140 new.section = abs_output_section;
146 exp_intop (bfd_vma value)
148 etree_type *new = stat_alloc (sizeof (new->value));
149 new->type.node_code = INT;
150 new->value.value = value;
151 new->value.str = NULL;
152 new->type.node_class = etree_value;
157 exp_bigintop (bfd_vma value, char *str)
159 etree_type *new = stat_alloc (sizeof (new->value));
160 new->type.node_code = INT;
161 new->value.value = value;
162 new->value.str = str;
163 new->type.node_class = etree_value;
167 /* Build an expression representing an unnamed relocatable value. */
170 exp_relop (asection *section, bfd_vma value)
172 etree_type *new = stat_alloc (sizeof (new->rel));
173 new->type.node_code = REL;
174 new->type.node_class = etree_rel;
175 new->rel.section = section;
176 new->rel.value = value;
180 static etree_value_type
181 new_rel (bfd_vma value,
183 lang_output_section_statement_type *section)
185 etree_value_type new;
189 new.section = section;
193 static etree_value_type
194 new_rel_from_section (bfd_vma value,
195 lang_output_section_statement_type *section)
197 etree_value_type new;
201 new.section = section;
203 new.value -= section->bfd_section->vma;
208 static etree_value_type
209 fold_unary (etree_type *tree,
210 lang_output_section_statement_type *current_section,
211 lang_phase_type allocation_done,
215 etree_value_type result;
217 result = exp_fold_tree (tree->unary.child,
219 allocation_done, dot, dotp);
222 switch (tree->type.node_code)
225 if (allocation_done != lang_first_phase_enum)
226 result = new_rel_from_section (align_n (dot, result.value),
229 result.valid_p = FALSE;
233 if (allocation_done != lang_first_phase_enum)
235 result.value += result.section->bfd_section->vma;
236 result.section = abs_output_section;
239 result.valid_p = FALSE;
244 result.value = ~result.value;
249 result.value = !result.value;
254 result.value = -result.value;
258 /* Return next place aligned to value. */
259 if (allocation_done == lang_allocating_phase_enum)
262 result.value = align_n (dot, result.value);
265 result.valid_p = FALSE;
268 case DATA_SEGMENT_RELRO_END:
269 if (allocation_done != lang_first_phase_enum
270 && (exp_data_seg.phase == exp_dataseg_align_seen
271 || exp_data_seg.phase == exp_dataseg_adjust
272 || exp_data_seg.phase == exp_dataseg_relro_adjust
273 || allocation_done != lang_allocating_phase_enum))
275 if (exp_data_seg.phase == exp_dataseg_align_seen
276 || exp_data_seg.phase == exp_dataseg_relro_adjust)
277 exp_data_seg.relro_end
278 = result.value + current_section->bfd_section->vma;
279 if (exp_data_seg.phase == exp_dataseg_align_seen)
280 exp_data_seg.phase = exp_dataseg_relro_seen;
281 result.value = dot - current_section->bfd_section->vma;
284 result.valid_p = FALSE;
287 case DATA_SEGMENT_END:
288 if (allocation_done != lang_first_phase_enum
289 && current_section == abs_output_section
290 && (exp_data_seg.phase == exp_dataseg_align_seen
291 || exp_data_seg.phase == exp_dataseg_relro_seen
292 || exp_data_seg.phase == exp_dataseg_adjust
293 || exp_data_seg.phase == exp_dataseg_relro_adjust
294 || allocation_done != lang_allocating_phase_enum))
296 if (exp_data_seg.phase == exp_dataseg_align_seen
297 || exp_data_seg.phase == exp_dataseg_relro_seen)
299 exp_data_seg.phase = exp_dataseg_end_seen;
300 exp_data_seg.end = result.value;
304 result.valid_p = FALSE;
316 static etree_value_type
317 fold_binary (etree_type *tree,
318 lang_output_section_statement_type *current_section,
319 lang_phase_type allocation_done,
323 etree_value_type result;
325 result = exp_fold_tree (tree->binary.lhs, current_section,
326 allocation_done, dot, dotp);
329 etree_value_type other;
331 other = exp_fold_tree (tree->binary.rhs,
333 allocation_done, dot, dotp);
336 /* If the values are from different sections, or this is an
337 absolute expression, make both the source arguments
338 absolute. However, adding or subtracting an absolute
339 value from a relative value is meaningful, and is an
341 if (current_section != abs_output_section
342 && (other.section == abs_output_section
343 || (result.section == abs_output_section
344 && tree->type.node_code == '+'))
345 && (tree->type.node_code == '+'
346 || tree->type.node_code == '-'))
348 if (other.section != abs_output_section)
350 /* Keep the section of the other term. */
351 if (tree->type.node_code == '+')
352 other.value = result.value + other.value;
354 other.value = result.value - other.value;
358 else if (result.section != other.section
359 || current_section == abs_output_section)
365 switch (tree->type.node_code)
368 if (other.value == 0)
369 einfo (_("%F%S %% by zero\n"));
370 result.value = ((bfd_signed_vma) result.value
371 % (bfd_signed_vma) other.value);
375 if (other.value == 0)
376 einfo (_("%F%S / by zero\n"));
377 result.value = ((bfd_signed_vma) result.value
378 / (bfd_signed_vma) other.value);
381 #define BOP(x,y) case x : result.value = result.value y other.value; break;
400 if (result.value < other.value)
405 if (result.value > other.value)
410 result.value = align_n (result.value, other.value);
413 case DATA_SEGMENT_ALIGN:
414 if (allocation_done != lang_first_phase_enum
415 && current_section == abs_output_section
416 && (exp_data_seg.phase == exp_dataseg_none
417 || exp_data_seg.phase == exp_dataseg_adjust
418 || exp_data_seg.phase == exp_dataseg_relro_adjust
419 || allocation_done != lang_allocating_phase_enum))
421 bfd_vma maxpage = result.value;
423 result.value = align_n (dot, maxpage);
424 if (exp_data_seg.phase == exp_dataseg_relro_adjust)
426 /* Attempt to align DATA_SEGMENT_RELRO_END at
427 a common page boundary. */
430 result.value += dot & (maxpage - 1);
431 relro = exp_data_seg.relro_end - exp_data_seg.base;
432 result.value += -relro & (other.value - 1);
433 exp_data_seg.base = result.value;
435 else if (exp_data_seg.phase != exp_dataseg_adjust)
437 result.value += dot & (maxpage - 1);
438 if (allocation_done == lang_allocating_phase_enum)
440 exp_data_seg.phase = exp_dataseg_align_seen;
441 exp_data_seg.base = result.value;
442 exp_data_seg.pagesize = other.value;
443 exp_data_seg.relro_end = 0;
446 else if (other.value < maxpage)
447 result.value += (dot + other.value - 1)
448 & (maxpage - other.value);
451 result.valid_p = FALSE;
460 result.valid_p = FALSE;
467 static etree_value_type
468 fold_trinary (etree_type *tree,
469 lang_output_section_statement_type *current_section,
470 lang_phase_type allocation_done,
474 etree_value_type result;
476 result = exp_fold_tree (tree->trinary.cond, current_section,
477 allocation_done, dot, dotp);
479 result = exp_fold_tree ((result.value
481 : tree->trinary.rhs),
483 allocation_done, dot, dotp);
488 static etree_value_type
489 fold_name (etree_type *tree,
490 lang_output_section_statement_type *current_section,
491 lang_phase_type allocation_done,
494 etree_value_type result;
496 result.valid_p = FALSE;
498 switch (tree->type.node_code)
501 if (allocation_done != lang_first_phase_enum)
502 result = new_abs (bfd_sizeof_headers (output_bfd,
503 link_info.relocatable));
506 if (allocation_done == lang_first_phase_enum)
507 lang_track_definedness (tree->name.name);
510 struct bfd_link_hash_entry *h;
512 = lang_symbol_definition_iteration (tree->name.name);
514 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
517 result.value = (h != NULL
518 && (h->type == bfd_link_hash_defined
519 || h->type == bfd_link_hash_defweak
520 || h->type == bfd_link_hash_common)
521 && (def_iteration == lang_statement_iteration
522 || def_iteration == -1));
523 result.section = abs_output_section;
524 result.valid_p = TRUE;
528 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
530 if (allocation_done != lang_first_phase_enum)
531 result = new_rel_from_section (dot, current_section);
533 else if (allocation_done != lang_first_phase_enum)
535 struct bfd_link_hash_entry *h;
537 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
541 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
542 else if (h->type == bfd_link_hash_defined
543 || h->type == bfd_link_hash_defweak)
545 if (bfd_is_abs_section (h->u.def.section))
546 result = new_abs (h->u.def.value);
547 else if (allocation_done == lang_final_phase_enum
548 || allocation_done == lang_allocating_phase_enum)
550 asection *output_section;
552 output_section = h->u.def.section->output_section;
553 if (output_section == NULL)
554 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
558 lang_output_section_statement_type *os;
560 os = (lang_output_section_statement_lookup
561 (bfd_get_section_name (output_bfd,
564 /* FIXME: Is this correct if this section is
565 being linked with -R? */
566 result = new_rel ((h->u.def.value
567 + h->u.def.section->output_offset),
573 else if (allocation_done == lang_final_phase_enum)
574 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
576 else if (h->type == bfd_link_hash_new)
578 h->type = bfd_link_hash_undefined;
579 h->u.undef.abfd = NULL;
580 if (h->u.undef.next == NULL)
581 bfd_link_add_undef (link_info.hash, h);
587 if (allocation_done != lang_first_phase_enum)
589 lang_output_section_statement_type *os;
591 os = lang_output_section_find (tree->name.name);
592 if (os && os->processed > 0)
593 result = new_rel (0, NULL, os);
598 if (allocation_done != lang_first_phase_enum)
600 lang_output_section_statement_type *os;
602 os = lang_output_section_find (tree->name.name);
603 if (os && os->processed != 0)
605 if (os->load_base == NULL)
606 result = new_rel (0, NULL, os);
608 result = exp_fold_tree_no_dot (os->load_base,
616 if (allocation_done != lang_first_phase_enum)
618 int opb = bfd_octets_per_byte (output_bfd);
619 lang_output_section_statement_type *os;
621 os = lang_output_section_find (tree->name.name);
622 if (os && os->processed > 0)
623 result = new_abs (os->bfd_section->size / opb);
636 exp_fold_tree (etree_type *tree,
637 lang_output_section_statement_type *current_section,
638 lang_phase_type allocation_done,
642 etree_value_type result;
646 result.valid_p = FALSE;
650 switch (tree->type.node_class)
653 result = new_rel (tree->value.value, tree->value.str, current_section);
657 if (allocation_done != lang_final_phase_enum)
658 result.valid_p = FALSE;
660 result = new_rel ((tree->rel.value
661 + tree->rel.section->output_section->vma
662 + tree->rel.section->output_offset),
668 result = exp_fold_tree (tree->assert_s.child,
670 allocation_done, dot, dotp);
674 einfo ("%F%P: %s\n", tree->assert_s.message);
680 result = fold_unary (tree, current_section, allocation_done,
685 result = fold_binary (tree, current_section, allocation_done,
690 result = fold_trinary (tree, current_section, allocation_done,
697 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
699 /* Assignment to dot can only be done during allocation. */
700 if (tree->type.node_class != etree_assign)
701 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
702 if (allocation_done == lang_allocating_phase_enum
703 || (allocation_done == lang_final_phase_enum
704 && current_section == abs_output_section))
706 result = exp_fold_tree (tree->assign.src,
708 allocation_done, dot,
710 if (! result.valid_p)
711 einfo (_("%F%S invalid assignment to location counter\n"));
714 if (current_section == NULL)
715 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
720 nextdot = (result.value
721 + current_section->bfd_section->vma);
723 && current_section != abs_output_section)
724 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
734 result = exp_fold_tree (tree->assign.src,
735 current_section, allocation_done,
740 struct bfd_link_hash_entry *h;
742 if (tree->type.node_class == etree_assign)
746 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
747 create, FALSE, TRUE);
751 einfo (_("%P%F:%s: hash creation failed\n"),
754 else if (tree->type.node_class == etree_provide
755 && h->type != bfd_link_hash_new
756 && h->type != bfd_link_hash_undefined
757 && h->type != bfd_link_hash_common)
759 /* Do nothing. The symbol was defined by some
764 /* FIXME: Should we worry if the symbol is already
766 lang_update_definedness (tree->assign.dst, h);
767 h->type = bfd_link_hash_defined;
768 h->u.def.value = result.value;
769 h->u.def.section = result.section->bfd_section;
770 if (tree->type.node_class == etree_provide)
771 tree->type.node_class = etree_provided;
778 result = fold_name (tree, current_section, allocation_done, dot);
789 static etree_value_type
790 exp_fold_tree_no_dot (etree_type *tree,
791 lang_output_section_statement_type *current_section,
792 lang_phase_type allocation_done)
794 return exp_fold_tree (tree, current_section, allocation_done, 0, NULL);
798 exp_binop (int code, etree_type *lhs, etree_type *rhs)
800 etree_type value, *new;
803 value.type.node_code = code;
804 value.binary.lhs = lhs;
805 value.binary.rhs = rhs;
806 value.type.node_class = etree_binary;
807 r = exp_fold_tree_no_dot (&value,
809 lang_first_phase_enum);
812 return exp_intop (r.value);
814 new = stat_alloc (sizeof (new->binary));
815 memcpy (new, &value, sizeof (new->binary));
820 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
822 etree_type value, *new;
824 value.type.node_code = code;
825 value.trinary.lhs = lhs;
826 value.trinary.cond = cond;
827 value.trinary.rhs = rhs;
828 value.type.node_class = etree_trinary;
829 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
831 return exp_intop (r.value);
833 new = stat_alloc (sizeof (new->trinary));
834 memcpy (new, &value, sizeof (new->trinary));
839 exp_unop (int code, etree_type *child)
841 etree_type value, *new;
844 value.unary.type.node_code = code;
845 value.unary.child = child;
846 value.unary.type.node_class = etree_unary;
847 r = exp_fold_tree_no_dot (&value, abs_output_section,
848 lang_first_phase_enum);
850 return exp_intop (r.value);
852 new = stat_alloc (sizeof (new->unary));
853 memcpy (new, &value, sizeof (new->unary));
858 exp_nameop (int code, const char *name)
860 etree_type value, *new;
862 value.name.type.node_code = code;
863 value.name.name = name;
864 value.name.type.node_class = etree_name;
866 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
868 return exp_intop (r.value);
870 new = stat_alloc (sizeof (new->name));
871 memcpy (new, &value, sizeof (new->name));
877 exp_assop (int code, const char *dst, etree_type *src)
879 etree_type value, *new;
881 value.assign.type.node_code = code;
883 value.assign.src = src;
884 value.assign.dst = dst;
885 value.assign.type.node_class = etree_assign;
888 if (exp_fold_tree_no_dot (&value, &result))
889 return exp_intop (result);
891 new = stat_alloc (sizeof (new->assign));
892 memcpy (new, &value, sizeof (new->assign));
896 /* Handle PROVIDE. */
899 exp_provide (const char *dst, etree_type *src)
903 n = stat_alloc (sizeof (n->assign));
904 n->assign.type.node_code = '=';
905 n->assign.type.node_class = etree_provide;
914 exp_assert (etree_type *exp, const char *message)
918 n = stat_alloc (sizeof (n->assert_s));
919 n->assert_s.type.node_code = '!';
920 n->assert_s.type.node_class = etree_assert;
921 n->assert_s.child = exp;
922 n->assert_s.message = message;
927 exp_print_tree (etree_type *tree)
929 if (config.map_file == NULL)
930 config.map_file = stderr;
934 minfo ("NULL TREE\n");
938 switch (tree->type.node_class)
941 minfo ("0x%v", tree->value.value);
944 if (tree->rel.section->owner != NULL)
945 minfo ("%B:", tree->rel.section->owner);
946 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
950 if (tree->assign.dst->sdefs != NULL)
951 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
952 tree->assign.dst->sdefs->value);
954 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
956 fprintf (config.map_file, "%s", tree->assign.dst);
957 exp_print_token (tree->type.node_code, TRUE);
958 exp_print_tree (tree->assign.src);
962 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
963 exp_print_tree (tree->assign.src);
964 fprintf (config.map_file, ")");
967 fprintf (config.map_file, "(");
968 exp_print_tree (tree->binary.lhs);
969 exp_print_token (tree->type.node_code, TRUE);
970 exp_print_tree (tree->binary.rhs);
971 fprintf (config.map_file, ")");
974 exp_print_tree (tree->trinary.cond);
975 fprintf (config.map_file, "?");
976 exp_print_tree (tree->trinary.lhs);
977 fprintf (config.map_file, ":");
978 exp_print_tree (tree->trinary.rhs);
981 exp_print_token (tree->unary.type.node_code, FALSE);
982 if (tree->unary.child)
984 fprintf (config.map_file, " (");
985 exp_print_tree (tree->unary.child);
986 fprintf (config.map_file, ")");
991 fprintf (config.map_file, "ASSERT (");
992 exp_print_tree (tree->assert_s.child);
993 fprintf (config.map_file, ", %s)", tree->assert_s.message);
997 fprintf (config.map_file, "????????");
1000 if (tree->type.node_code == NAME)
1002 fprintf (config.map_file, "%s", tree->name.name);
1006 exp_print_token (tree->type.node_code, FALSE);
1007 if (tree->name.name)
1008 fprintf (config.map_file, " (%s)", tree->name.name);
1018 exp_get_vma (etree_type *tree,
1021 lang_phase_type allocation_done)
1027 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1028 if (! r.valid_p && name != NULL)
1029 einfo (_("%F%S nonconstant expression for %s\n"), name);
1037 exp_get_value_int (etree_type *tree,
1040 lang_phase_type allocation_done)
1042 return exp_get_vma (tree, def, name, allocation_done);
1046 exp_get_fill (etree_type *tree,
1049 lang_phase_type allocation_done)
1059 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1060 if (! r.valid_p && name != NULL)
1061 einfo (_("%F%S nonconstant expression for %s\n"), name);
1063 if (r.str != NULL && (len = strlen (r.str)) != 0)
1067 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1068 fill->size = (len + 1) / 2;
1078 digit = (digit - 'A' + '0' + 10) & 0xf;
1092 fill = xmalloc (4 + sizeof (*fill) - 1);
1094 fill->data[0] = (val >> 24) & 0xff;
1095 fill->data[1] = (val >> 16) & 0xff;
1096 fill->data[2] = (val >> 8) & 0xff;
1097 fill->data[3] = (val >> 0) & 0xff;
1104 exp_get_abs_int (etree_type *tree,
1105 int def ATTRIBUTE_UNUSED,
1107 lang_phase_type allocation_done)
1109 etree_value_type res;
1110 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1113 res.value += res.section->bfd_section->vma;
1115 einfo (_("%F%S non constant expression for %s\n"), name);
1121 align_n (bfd_vma value, bfd_vma align)
1126 value = (value + align - 1) / align;
1127 return value * align;