1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005
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 { ORIGIN, "ORIGIN" },
109 { LENGTH, "LENGTH" },
110 { SEGMENT_START, "SEGMENT_START" }
114 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
115 if (table[idx].code == code)
119 fputc (' ', config.map_file);
121 if (idx < ARRAY_SIZE (table))
122 fputs (table[idx].name, config.map_file);
124 fputc (code, config.map_file);
126 fprintf (config.map_file, "<code %d>", code);
129 fputc (' ', config.map_file);
133 make_abs (etree_value_type *ptr)
135 asection *s = ptr->section->bfd_section;
136 ptr->value += s->vma;
137 ptr->section = abs_output_section;
140 static etree_value_type
141 new_abs (bfd_vma value)
143 etree_value_type new;
145 new.section = abs_output_section;
152 exp_intop (bfd_vma value)
154 etree_type *new = stat_alloc (sizeof (new->value));
155 new->type.node_code = INT;
156 new->value.value = value;
157 new->value.str = NULL;
158 new->type.node_class = etree_value;
163 exp_bigintop (bfd_vma value, char *str)
165 etree_type *new = stat_alloc (sizeof (new->value));
166 new->type.node_code = INT;
167 new->value.value = value;
168 new->value.str = str;
169 new->type.node_class = etree_value;
173 /* Build an expression representing an unnamed relocatable value. */
176 exp_relop (asection *section, bfd_vma value)
178 etree_type *new = 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 (bfd_vma value,
189 lang_output_section_statement_type *section)
191 etree_value_type new;
195 new.section = section;
199 static etree_value_type
200 new_rel_from_section (bfd_vma value,
201 lang_output_section_statement_type *section)
203 etree_value_type new;
207 new.section = section;
209 new.value -= section->bfd_section->vma;
214 static etree_value_type
215 fold_unary (etree_type *tree,
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->unary.child,
225 allocation_done, dot, dotp);
228 switch (tree->type.node_code)
231 if (allocation_done != lang_first_phase_enum)
232 result = new_rel_from_section (align_n (dot, result.value),
235 result.valid_p = FALSE;
239 if (allocation_done != lang_first_phase_enum)
241 result.value += result.section->bfd_section->vma;
242 result.section = abs_output_section;
245 result.valid_p = FALSE;
250 result.value = ~result.value;
255 result.value = !result.value;
260 result.value = -result.value;
264 /* Return next place aligned to value. */
265 if (allocation_done == lang_allocating_phase_enum)
268 result.value = align_n (dot, result.value);
271 result.valid_p = FALSE;
274 case DATA_SEGMENT_END:
275 if (allocation_done != lang_first_phase_enum
276 && current_section == abs_output_section
277 && (exp_data_seg.phase == exp_dataseg_align_seen
278 || exp_data_seg.phase == exp_dataseg_relro_seen
279 || exp_data_seg.phase == exp_dataseg_adjust
280 || exp_data_seg.phase == exp_dataseg_relro_adjust
281 || allocation_done != lang_allocating_phase_enum))
283 if (exp_data_seg.phase == exp_dataseg_align_seen
284 || exp_data_seg.phase == exp_dataseg_relro_seen)
286 exp_data_seg.phase = exp_dataseg_end_seen;
287 exp_data_seg.end = result.value;
291 result.valid_p = FALSE;
303 static etree_value_type
304 fold_binary (etree_type *tree,
305 lang_output_section_statement_type *current_section,
306 lang_phase_type allocation_done,
310 etree_value_type result;
312 result = exp_fold_tree (tree->binary.lhs, current_section,
313 allocation_done, dot, dotp);
315 /* The SEGMENT_START operator is special because its first
316 operand is a string, not the name of a symbol. */
317 if (result.valid_p && tree->type.node_code == SEGMENT_START)
319 const char *segment_name;
321 /* Check to see if the user has overridden the default
323 segment_name = tree->binary.rhs->name.name;
324 for (seg = segments; seg; seg = seg->next)
325 if (strcmp (seg->name, segment_name) == 0)
328 result.value = seg->value;
330 result.section = NULL;
334 else if (result.valid_p)
336 etree_value_type other;
338 other = exp_fold_tree (tree->binary.rhs,
340 allocation_done, dot, dotp);
343 /* If the values are from different sections, or this is an
344 absolute expression, make both the source arguments
345 absolute. However, adding or subtracting an absolute
346 value from a relative value is meaningful, and is an
348 if (current_section != abs_output_section
349 && (other.section == abs_output_section
350 || (result.section == abs_output_section
351 && tree->type.node_code == '+'))
352 && (tree->type.node_code == '+'
353 || tree->type.node_code == '-'))
355 if (other.section != abs_output_section)
357 /* Keep the section of the other term. */
358 if (tree->type.node_code == '+')
359 other.value = result.value + other.value;
361 other.value = result.value - other.value;
365 else if (result.section != other.section
366 || current_section == abs_output_section)
372 switch (tree->type.node_code)
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);
382 if (other.value == 0)
383 einfo (_("%F%S / by zero\n"));
384 result.value = ((bfd_signed_vma) result.value
385 / (bfd_signed_vma) other.value);
388 #define BOP(x,y) case x : result.value = result.value y other.value; break;
407 if (result.value < other.value)
412 if (result.value > other.value)
417 result.value = align_n (result.value, other.value);
420 case DATA_SEGMENT_ALIGN:
421 if (allocation_done != lang_first_phase_enum
422 && current_section == abs_output_section
423 && (exp_data_seg.phase == exp_dataseg_none
424 || exp_data_seg.phase == exp_dataseg_adjust
425 || exp_data_seg.phase == exp_dataseg_relro_adjust
426 || allocation_done != lang_allocating_phase_enum))
428 bfd_vma maxpage = result.value;
430 result.value = align_n (dot, maxpage);
431 if (exp_data_seg.phase == exp_dataseg_relro_adjust)
432 result.value = exp_data_seg.base;
433 else if (exp_data_seg.phase != exp_dataseg_adjust)
435 result.value += dot & (maxpage - 1);
436 if (allocation_done == lang_allocating_phase_enum)
438 exp_data_seg.phase = exp_dataseg_align_seen;
439 exp_data_seg.min_base = align_n (dot, maxpage);
440 exp_data_seg.base = result.value;
441 exp_data_seg.pagesize = other.value;
442 exp_data_seg.maxpagesize = maxpage;
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;
454 case DATA_SEGMENT_RELRO_END:
455 if (allocation_done != lang_first_phase_enum
456 && (exp_data_seg.phase == exp_dataseg_align_seen
457 || exp_data_seg.phase == exp_dataseg_adjust
458 || exp_data_seg.phase == exp_dataseg_relro_adjust
459 || allocation_done != lang_allocating_phase_enum))
461 if (exp_data_seg.phase == exp_dataseg_align_seen
462 || exp_data_seg.phase == exp_dataseg_relro_adjust)
463 exp_data_seg.relro_end
464 = result.value + other.value;
465 if (exp_data_seg.phase == exp_dataseg_relro_adjust
466 && (exp_data_seg.relro_end
467 & (exp_data_seg.pagesize - 1)))
469 exp_data_seg.relro_end += exp_data_seg.pagesize - 1;
470 exp_data_seg.relro_end &= ~(exp_data_seg.pagesize - 1);
471 result.value = exp_data_seg.relro_end - other.value;
473 if (exp_data_seg.phase == exp_dataseg_align_seen)
474 exp_data_seg.phase = exp_dataseg_relro_seen;
477 result.valid_p = FALSE;
486 result.valid_p = FALSE;
493 static etree_value_type
494 fold_trinary (etree_type *tree,
495 lang_output_section_statement_type *current_section,
496 lang_phase_type allocation_done,
500 etree_value_type result;
502 result = exp_fold_tree (tree->trinary.cond, current_section,
503 allocation_done, dot, dotp);
505 result = exp_fold_tree ((result.value
507 : tree->trinary.rhs),
509 allocation_done, dot, dotp);
514 static etree_value_type
515 fold_name (etree_type *tree,
516 lang_output_section_statement_type *current_section,
517 lang_phase_type allocation_done,
520 etree_value_type result;
522 result.valid_p = FALSE;
524 switch (tree->type.node_code)
527 if (allocation_done != lang_first_phase_enum)
528 result = new_abs (bfd_sizeof_headers (output_bfd,
529 link_info.relocatable));
532 if (allocation_done == lang_first_phase_enum)
533 lang_track_definedness (tree->name.name);
536 struct bfd_link_hash_entry *h;
538 = lang_symbol_definition_iteration (tree->name.name);
540 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
543 result.value = (h != NULL
544 && (h->type == bfd_link_hash_defined
545 || h->type == bfd_link_hash_defweak
546 || h->type == bfd_link_hash_common)
547 && (def_iteration == lang_statement_iteration
548 || def_iteration == -1));
549 result.section = abs_output_section;
550 result.valid_p = TRUE;
554 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
556 if (allocation_done != lang_first_phase_enum)
557 result = new_rel_from_section (dot, current_section);
559 else if (allocation_done != lang_first_phase_enum)
561 struct bfd_link_hash_entry *h;
563 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
567 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
568 else if (h->type == bfd_link_hash_defined
569 || h->type == bfd_link_hash_defweak)
571 if (bfd_is_abs_section (h->u.def.section))
572 result = new_abs (h->u.def.value);
573 else if (allocation_done == lang_final_phase_enum
574 || allocation_done == lang_allocating_phase_enum)
576 asection *output_section;
578 output_section = h->u.def.section->output_section;
579 if (output_section == NULL)
580 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
584 lang_output_section_statement_type *os;
586 os = (lang_output_section_statement_lookup
587 (bfd_get_section_name (output_bfd,
590 /* FIXME: Is this correct if this section is
591 being linked with -R? */
592 result = new_rel ((h->u.def.value
593 + h->u.def.section->output_offset),
599 else if (allocation_done == lang_final_phase_enum)
600 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
602 else if (h->type == bfd_link_hash_new)
604 h->type = bfd_link_hash_undefined;
605 h->u.undef.abfd = NULL;
606 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
607 bfd_link_add_undef (link_info.hash, h);
613 if (allocation_done != lang_first_phase_enum)
615 lang_output_section_statement_type *os;
617 os = lang_output_section_find (tree->name.name);
618 if (os && os->processed > 0)
619 result = new_rel (0, NULL, os);
624 if (allocation_done != lang_first_phase_enum)
626 lang_output_section_statement_type *os;
628 os = lang_output_section_find (tree->name.name);
629 if (os && os->processed != 0)
631 if (os->load_base == NULL)
632 result = new_rel (0, NULL, os);
634 result = exp_fold_tree_no_dot (os->load_base,
642 if (allocation_done != lang_first_phase_enum)
644 int opb = bfd_octets_per_byte (output_bfd);
645 lang_output_section_statement_type *os;
647 os = lang_output_section_find (tree->name.name);
648 if (os && os->processed > 0)
649 result = new_abs (os->bfd_section->size / opb);
655 lang_memory_region_type *mem;
657 mem = lang_memory_region_lookup (tree->name.name, FALSE);
659 result = new_abs (mem->length);
661 einfo (_("%F%S: undefined MEMORY region `%s' referenced in expression\n"),
668 lang_memory_region_type *mem;
670 mem = lang_memory_region_lookup (tree->name.name, FALSE);
672 result = new_abs (mem->origin);
674 einfo (_("%F%S: undefined MEMORY region `%s' referenced in expression\n"),
688 exp_fold_tree (etree_type *tree,
689 lang_output_section_statement_type *current_section,
690 lang_phase_type allocation_done,
694 etree_value_type result;
698 result.valid_p = FALSE;
702 switch (tree->type.node_class)
705 result = new_rel (tree->value.value, tree->value.str, current_section);
709 if (allocation_done != lang_final_phase_enum)
710 result.valid_p = FALSE;
712 result = new_rel ((tree->rel.value
713 + tree->rel.section->output_section->vma
714 + tree->rel.section->output_offset),
720 result = exp_fold_tree (tree->assert_s.child,
722 allocation_done, dot, dotp);
726 einfo ("%X%P: %s\n", tree->assert_s.message);
732 result = fold_unary (tree, current_section, allocation_done,
737 result = fold_binary (tree, current_section, allocation_done,
742 result = fold_trinary (tree, current_section, allocation_done,
749 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
751 /* Assignment to dot can only be done during allocation. */
752 if (tree->type.node_class != etree_assign)
753 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
754 if (allocation_done == lang_allocating_phase_enum
755 || (allocation_done == lang_final_phase_enum
756 && current_section == abs_output_section))
758 result = exp_fold_tree (tree->assign.src,
760 allocation_done, dot,
762 if (! result.valid_p)
763 einfo (_("%F%S invalid assignment to location counter\n"));
766 if (current_section == NULL)
767 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
772 nextdot = (result.value
773 + current_section->bfd_section->vma);
775 && current_section != abs_output_section)
776 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
786 result = exp_fold_tree (tree->assign.src,
787 current_section, allocation_done,
792 struct bfd_link_hash_entry *h;
794 if (tree->type.node_class == etree_assign)
798 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
799 create, FALSE, TRUE);
803 einfo (_("%P%F:%s: hash creation failed\n"),
806 else if (tree->type.node_class == etree_provide
807 && h->type != bfd_link_hash_new
808 && h->type != bfd_link_hash_undefined
809 && h->type != bfd_link_hash_common)
811 /* Do nothing. The symbol was defined by some
816 /* FIXME: Should we worry if the symbol is already
818 lang_update_definedness (tree->assign.dst, h);
819 h->type = bfd_link_hash_defined;
820 h->u.def.value = result.value;
821 h->u.def.section = result.section->bfd_section;
822 if (tree->type.node_class == etree_provide)
823 tree->type.node_class = etree_provided;
830 result = fold_name (tree, current_section, allocation_done, dot);
841 static etree_value_type
842 exp_fold_tree_no_dot (etree_type *tree,
843 lang_output_section_statement_type *current_section,
844 lang_phase_type allocation_done)
846 return exp_fold_tree (tree, current_section, allocation_done, 0, NULL);
850 exp_binop (int code, etree_type *lhs, etree_type *rhs)
852 etree_type value, *new;
855 value.type.node_code = code;
856 value.binary.lhs = lhs;
857 value.binary.rhs = rhs;
858 value.type.node_class = etree_binary;
859 r = exp_fold_tree_no_dot (&value,
861 lang_first_phase_enum);
864 return exp_intop (r.value);
866 new = stat_alloc (sizeof (new->binary));
867 memcpy (new, &value, sizeof (new->binary));
872 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
874 etree_type value, *new;
876 value.type.node_code = code;
877 value.trinary.lhs = lhs;
878 value.trinary.cond = cond;
879 value.trinary.rhs = rhs;
880 value.type.node_class = etree_trinary;
881 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
883 return exp_intop (r.value);
885 new = stat_alloc (sizeof (new->trinary));
886 memcpy (new, &value, sizeof (new->trinary));
891 exp_unop (int code, etree_type *child)
893 etree_type value, *new;
896 value.unary.type.node_code = code;
897 value.unary.child = child;
898 value.unary.type.node_class = etree_unary;
899 r = exp_fold_tree_no_dot (&value, abs_output_section,
900 lang_first_phase_enum);
902 return exp_intop (r.value);
904 new = stat_alloc (sizeof (new->unary));
905 memcpy (new, &value, sizeof (new->unary));
910 exp_nameop (int code, const char *name)
912 etree_type value, *new;
914 value.name.type.node_code = code;
915 value.name.name = name;
916 value.name.type.node_class = etree_name;
918 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
920 return exp_intop (r.value);
922 new = stat_alloc (sizeof (new->name));
923 memcpy (new, &value, sizeof (new->name));
929 exp_assop (int code, const char *dst, etree_type *src)
931 etree_type value, *new;
933 value.assign.type.node_code = code;
935 value.assign.src = src;
936 value.assign.dst = dst;
937 value.assign.type.node_class = etree_assign;
939 new = stat_alloc (sizeof (new->assign));
940 memcpy (new, &value, sizeof (new->assign));
944 /* Handle PROVIDE. */
947 exp_provide (const char *dst, etree_type *src)
951 n = stat_alloc (sizeof (n->assign));
952 n->assign.type.node_code = '=';
953 n->assign.type.node_class = etree_provide;
962 exp_assert (etree_type *exp, const char *message)
966 n = stat_alloc (sizeof (n->assert_s));
967 n->assert_s.type.node_code = '!';
968 n->assert_s.type.node_class = etree_assert;
969 n->assert_s.child = exp;
970 n->assert_s.message = message;
975 exp_print_tree (etree_type *tree)
977 if (config.map_file == NULL)
978 config.map_file = stderr;
982 minfo ("NULL TREE\n");
986 switch (tree->type.node_class)
989 minfo ("0x%v", tree->value.value);
992 if (tree->rel.section->owner != NULL)
993 minfo ("%B:", tree->rel.section->owner);
994 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
997 fprintf (config.map_file, "%s", tree->assign.dst);
998 exp_print_token (tree->type.node_code, TRUE);
999 exp_print_tree (tree->assign.src);
1002 case etree_provided:
1003 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1004 exp_print_tree (tree->assign.src);
1005 fprintf (config.map_file, ")");
1008 fprintf (config.map_file, "(");
1009 exp_print_tree (tree->binary.lhs);
1010 exp_print_token (tree->type.node_code, TRUE);
1011 exp_print_tree (tree->binary.rhs);
1012 fprintf (config.map_file, ")");
1015 exp_print_tree (tree->trinary.cond);
1016 fprintf (config.map_file, "?");
1017 exp_print_tree (tree->trinary.lhs);
1018 fprintf (config.map_file, ":");
1019 exp_print_tree (tree->trinary.rhs);
1022 exp_print_token (tree->unary.type.node_code, FALSE);
1023 if (tree->unary.child)
1025 fprintf (config.map_file, " (");
1026 exp_print_tree (tree->unary.child);
1027 fprintf (config.map_file, ")");
1032 fprintf (config.map_file, "ASSERT (");
1033 exp_print_tree (tree->assert_s.child);
1034 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1038 fprintf (config.map_file, "????????");
1041 if (tree->type.node_code == NAME)
1043 fprintf (config.map_file, "%s", tree->name.name);
1047 exp_print_token (tree->type.node_code, FALSE);
1048 if (tree->name.name)
1049 fprintf (config.map_file, " (%s)", tree->name.name);
1059 exp_get_vma (etree_type *tree,
1062 lang_phase_type allocation_done)
1068 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1069 if (! r.valid_p && name != NULL)
1070 einfo (_("%F%S nonconstant expression for %s\n"), name);
1078 exp_get_value_int (etree_type *tree,
1081 lang_phase_type allocation_done)
1083 return exp_get_vma (tree, def, name, allocation_done);
1087 exp_get_fill (etree_type *tree,
1090 lang_phase_type allocation_done)
1100 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1101 if (! r.valid_p && name != NULL)
1102 einfo (_("%F%S nonconstant expression for %s\n"), name);
1104 if (r.str != NULL && (len = strlen (r.str)) != 0)
1108 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1109 fill->size = (len + 1) / 2;
1119 digit = (digit - 'A' + '0' + 10) & 0xf;
1133 fill = xmalloc (4 + sizeof (*fill) - 1);
1135 fill->data[0] = (val >> 24) & 0xff;
1136 fill->data[1] = (val >> 16) & 0xff;
1137 fill->data[2] = (val >> 8) & 0xff;
1138 fill->data[3] = (val >> 0) & 0xff;
1145 exp_get_abs_int (etree_type *tree,
1146 int def ATTRIBUTE_UNUSED,
1148 lang_phase_type allocation_done)
1150 etree_value_type res;
1151 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1154 res.value += res.section->bfd_section->vma;
1156 einfo (_("%F%S non constant expression for %s\n"), name);
1162 align_n (bfd_vma value, bfd_vma align)
1167 value = (value + align - 1) / align;
1168 return value * align;