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_END
, "DATA_SEGMENT_END" }
108 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
109 if (table
[idx
].code
== code
)
113 fputc (' ', config
.map_file
);
115 if (idx
< ARRAY_SIZE (table
))
116 fputs (table
[idx
].name
, config
.map_file
);
118 fputc (code
, config
.map_file
);
120 fprintf (config
.map_file
, "<code %d>", code
);
123 fputc (' ', config
.map_file
);
127 make_abs (etree_value_type
*ptr
)
129 asection
*s
= ptr
->section
->bfd_section
;
130 ptr
->value
+= s
->vma
;
131 ptr
->section
= abs_output_section
;
134 static etree_value_type
135 new_abs (bfd_vma value
)
137 etree_value_type
new;
139 new.section
= abs_output_section
;
145 exp_intop (bfd_vma value
)
147 etree_type
*new = stat_alloc (sizeof (new->value
));
148 new->type
.node_code
= INT
;
149 new->value
.value
= value
;
150 new->value
.str
= NULL
;
151 new->type
.node_class
= etree_value
;
156 exp_bigintop (bfd_vma value
, char *str
)
158 etree_type
*new = stat_alloc (sizeof (new->value
));
159 new->type
.node_code
= INT
;
160 new->value
.value
= value
;
161 new->value
.str
= str
;
162 new->type
.node_class
= etree_value
;
166 /* Build an expression representing an unnamed relocatable value. */
169 exp_relop (asection
*section
, bfd_vma value
)
171 etree_type
*new = stat_alloc (sizeof (new->rel
));
172 new->type
.node_code
= REL
;
173 new->type
.node_class
= etree_rel
;
174 new->rel
.section
= section
;
175 new->rel
.value
= value
;
179 static etree_value_type
180 new_rel (bfd_vma value
,
182 lang_output_section_statement_type
*section
)
184 etree_value_type
new;
188 new.section
= section
;
192 static etree_value_type
193 new_rel_from_section (bfd_vma value
,
194 lang_output_section_statement_type
*section
)
196 etree_value_type
new;
200 new.section
= section
;
202 new.value
-= section
->bfd_section
->vma
;
207 static etree_value_type
208 fold_unary (etree_type
*tree
,
209 lang_output_section_statement_type
*current_section
,
210 lang_phase_type allocation_done
,
214 etree_value_type result
;
216 result
= exp_fold_tree (tree
->unary
.child
,
218 allocation_done
, dot
, dotp
);
221 switch (tree
->type
.node_code
)
224 if (allocation_done
!= lang_first_phase_enum
)
225 result
= new_rel_from_section (align_n (dot
, result
.value
),
228 result
.valid_p
= FALSE
;
232 if (allocation_done
!= lang_first_phase_enum
)
234 result
.value
+= result
.section
->bfd_section
->vma
;
235 result
.section
= abs_output_section
;
238 result
.valid_p
= FALSE
;
243 result
.value
= ~result
.value
;
248 result
.value
= !result
.value
;
253 result
.value
= -result
.value
;
257 /* Return next place aligned to value. */
258 if (allocation_done
== lang_allocating_phase_enum
)
261 result
.value
= align_n (dot
, result
.value
);
264 result
.valid_p
= FALSE
;
267 case DATA_SEGMENT_END
:
268 if (allocation_done
!= lang_first_phase_enum
269 && current_section
== abs_output_section
270 && (exp_data_seg
.phase
== exp_dataseg_align_seen
271 || exp_data_seg
.phase
== exp_dataseg_adjust
272 || allocation_done
!= lang_allocating_phase_enum
))
274 if (exp_data_seg
.phase
== exp_dataseg_align_seen
)
276 exp_data_seg
.phase
= exp_dataseg_end_seen
;
277 exp_data_seg
.end
= result
.value
;
281 result
.valid_p
= FALSE
;
293 static etree_value_type
294 fold_binary (etree_type
*tree
,
295 lang_output_section_statement_type
*current_section
,
296 lang_phase_type allocation_done
,
300 etree_value_type result
;
302 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
303 allocation_done
, dot
, dotp
);
306 etree_value_type other
;
308 other
= exp_fold_tree (tree
->binary
.rhs
,
310 allocation_done
, dot
, dotp
);
313 /* If the values are from different sections, or this is an
314 absolute expression, make both the source arguments
315 absolute. However, adding or subtracting an absolute
316 value from a relative value is meaningful, and is an
318 if (current_section
!= abs_output_section
319 && (other
.section
== abs_output_section
320 || (result
.section
== abs_output_section
321 && tree
->type
.node_code
== '+'))
322 && (tree
->type
.node_code
== '+'
323 || tree
->type
.node_code
== '-'))
325 if (other
.section
!= abs_output_section
)
327 /* Keep the section of the other term. */
328 if (tree
->type
.node_code
== '+')
329 other
.value
= result
.value
+ other
.value
;
331 other
.value
= result
.value
- other
.value
;
335 else if (result
.section
!= other
.section
336 || current_section
== abs_output_section
)
342 switch (tree
->type
.node_code
)
345 if (other
.value
== 0)
346 einfo (_("%F%S %% by zero\n"));
347 result
.value
= ((bfd_signed_vma
) result
.value
348 % (bfd_signed_vma
) other
.value
);
352 if (other
.value
== 0)
353 einfo (_("%F%S / by zero\n"));
354 result
.value
= ((bfd_signed_vma
) result
.value
355 / (bfd_signed_vma
) other
.value
);
358 #define BOP(x,y) case x : result.value = result.value y other.value; break;
377 if (result
.value
< other
.value
)
382 if (result
.value
> other
.value
)
387 result
.value
= align_n (result
.value
, other
.value
);
390 case DATA_SEGMENT_ALIGN
:
391 if (allocation_done
!= lang_first_phase_enum
392 && current_section
== abs_output_section
393 && (exp_data_seg
.phase
== exp_dataseg_none
394 || exp_data_seg
.phase
== exp_dataseg_adjust
395 || allocation_done
!= lang_allocating_phase_enum
))
397 bfd_vma maxpage
= result
.value
;
399 result
.value
= align_n (dot
, maxpage
);
400 if (exp_data_seg
.phase
!= exp_dataseg_adjust
)
402 result
.value
+= dot
& (maxpage
- 1);
403 if (allocation_done
== lang_allocating_phase_enum
)
405 exp_data_seg
.phase
= exp_dataseg_align_seen
;
406 exp_data_seg
.base
= result
.value
;
407 exp_data_seg
.pagesize
= other
.value
;
410 else if (other
.value
< maxpage
)
411 result
.value
+= (dot
+ other
.value
- 1)
412 & (maxpage
- other
.value
);
415 result
.valid_p
= FALSE
;
424 result
.valid_p
= FALSE
;
431 static etree_value_type
432 fold_trinary (etree_type
*tree
,
433 lang_output_section_statement_type
*current_section
,
434 lang_phase_type allocation_done
,
438 etree_value_type result
;
440 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
441 allocation_done
, dot
, dotp
);
443 result
= exp_fold_tree ((result
.value
445 : tree
->trinary
.rhs
),
447 allocation_done
, dot
, dotp
);
452 static etree_value_type
453 fold_name (etree_type
*tree
,
454 lang_output_section_statement_type
*current_section
,
455 lang_phase_type allocation_done
,
458 etree_value_type result
;
460 result
.valid_p
= FALSE
;
462 switch (tree
->type
.node_code
)
465 if (allocation_done
!= lang_first_phase_enum
)
466 result
= new_abs (bfd_sizeof_headers (output_bfd
,
467 link_info
.relocatable
));
470 if (allocation_done
== lang_first_phase_enum
)
471 lang_track_definedness (tree
->name
.name
);
474 struct bfd_link_hash_entry
*h
;
476 = lang_symbol_definition_iteration (tree
->name
.name
);
478 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
481 result
.value
= (h
!= NULL
482 && (h
->type
== bfd_link_hash_defined
483 || h
->type
== bfd_link_hash_defweak
484 || h
->type
== bfd_link_hash_common
)
485 && (def_iteration
== lang_statement_iteration
486 || def_iteration
== -1));
487 result
.section
= abs_output_section
;
488 result
.valid_p
= TRUE
;
492 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
494 if (allocation_done
!= lang_first_phase_enum
)
495 result
= new_rel_from_section (dot
, current_section
);
497 else if (allocation_done
!= lang_first_phase_enum
)
499 struct bfd_link_hash_entry
*h
;
501 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
505 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
506 else if (h
->type
== bfd_link_hash_defined
507 || h
->type
== bfd_link_hash_defweak
)
509 if (bfd_is_abs_section (h
->u
.def
.section
))
510 result
= new_abs (h
->u
.def
.value
);
511 else if (allocation_done
== lang_final_phase_enum
512 || allocation_done
== lang_allocating_phase_enum
)
514 asection
*output_section
;
516 output_section
= h
->u
.def
.section
->output_section
;
517 if (output_section
== NULL
)
518 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
522 lang_output_section_statement_type
*os
;
524 os
= (lang_output_section_statement_lookup
525 (bfd_get_section_name (output_bfd
,
528 /* FIXME: Is this correct if this section is
529 being linked with -R? */
530 result
= new_rel ((h
->u
.def
.value
531 + h
->u
.def
.section
->output_offset
),
537 else if (allocation_done
== lang_final_phase_enum
)
538 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
540 else if (h
->type
== bfd_link_hash_new
)
542 h
->type
= bfd_link_hash_undefined
;
543 h
->u
.undef
.abfd
= NULL
;
544 bfd_link_add_undef (link_info
.hash
, h
);
550 if (allocation_done
!= lang_first_phase_enum
)
552 lang_output_section_statement_type
*os
;
554 os
= lang_output_section_find (tree
->name
.name
);
555 if (os
&& os
->processed
> 0)
556 result
= new_rel (0, NULL
, os
);
561 if (allocation_done
!= lang_first_phase_enum
)
563 lang_output_section_statement_type
*os
;
565 os
= lang_output_section_find (tree
->name
.name
);
566 if (os
&& os
->processed
!= 0)
568 if (os
->load_base
== NULL
)
569 result
= new_rel (0, NULL
, os
);
571 result
= exp_fold_tree_no_dot (os
->load_base
,
579 if (allocation_done
!= lang_first_phase_enum
)
581 int opb
= bfd_octets_per_byte (output_bfd
);
582 lang_output_section_statement_type
*os
;
584 os
= lang_output_section_find (tree
->name
.name
);
585 if (os
&& os
->processed
> 0)
586 result
= new_abs (os
->bfd_section
->_raw_size
/ opb
);
599 exp_fold_tree (etree_type
*tree
,
600 lang_output_section_statement_type
*current_section
,
601 lang_phase_type allocation_done
,
605 etree_value_type result
;
609 result
.valid_p
= FALSE
;
613 switch (tree
->type
.node_class
)
616 result
= new_rel (tree
->value
.value
, tree
->value
.str
, current_section
);
620 if (allocation_done
!= lang_final_phase_enum
)
621 result
.valid_p
= FALSE
;
623 result
= new_rel ((tree
->rel
.value
624 + tree
->rel
.section
->output_section
->vma
625 + tree
->rel
.section
->output_offset
),
631 result
= exp_fold_tree (tree
->assert_s
.child
,
633 allocation_done
, dot
, dotp
);
637 einfo ("%F%P: %s\n", tree
->assert_s
.message
);
643 result
= fold_unary (tree
, current_section
, allocation_done
,
648 result
= fold_binary (tree
, current_section
, allocation_done
,
653 result
= fold_trinary (tree
, current_section
, allocation_done
,
660 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
662 /* Assignment to dot can only be done during allocation. */
663 if (tree
->type
.node_class
!= etree_assign
)
664 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
665 if (allocation_done
== lang_allocating_phase_enum
666 || (allocation_done
== lang_final_phase_enum
667 && current_section
== abs_output_section
))
669 result
= exp_fold_tree (tree
->assign
.src
,
671 allocation_done
, dot
,
673 if (! result
.valid_p
)
674 einfo (_("%F%S invalid assignment to location counter\n"));
677 if (current_section
== NULL
)
678 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
683 nextdot
= (result
.value
684 + current_section
->bfd_section
->vma
);
686 && current_section
!= abs_output_section
)
687 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
697 result
= exp_fold_tree (tree
->assign
.src
,
698 current_section
, allocation_done
,
703 struct bfd_link_hash_entry
*h
;
705 if (tree
->type
.node_class
== etree_assign
)
709 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
710 create
, FALSE
, TRUE
);
714 einfo (_("%P%F:%s: hash creation failed\n"),
717 else if (tree
->type
.node_class
== etree_provide
718 && h
->type
!= bfd_link_hash_new
719 && h
->type
!= bfd_link_hash_undefined
720 && h
->type
!= bfd_link_hash_common
)
722 /* Do nothing. The symbol was defined by some
727 /* FIXME: Should we worry if the symbol is already
729 lang_update_definedness (tree
->assign
.dst
, h
);
730 h
->type
= bfd_link_hash_defined
;
731 h
->u
.def
.value
= result
.value
;
732 h
->u
.def
.section
= result
.section
->bfd_section
;
733 if (tree
->type
.node_class
== etree_provide
)
734 tree
->type
.node_class
= etree_provided
;
741 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
752 static etree_value_type
753 exp_fold_tree_no_dot (etree_type
*tree
,
754 lang_output_section_statement_type
*current_section
,
755 lang_phase_type allocation_done
)
757 return exp_fold_tree (tree
, current_section
, allocation_done
, 0, NULL
);
761 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
763 etree_type value
, *new;
766 value
.type
.node_code
= code
;
767 value
.binary
.lhs
= lhs
;
768 value
.binary
.rhs
= rhs
;
769 value
.type
.node_class
= etree_binary
;
770 r
= exp_fold_tree_no_dot (&value
,
772 lang_first_phase_enum
);
775 return exp_intop (r
.value
);
777 new = stat_alloc (sizeof (new->binary
));
778 memcpy (new, &value
, sizeof (new->binary
));
783 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
785 etree_type value
, *new;
787 value
.type
.node_code
= code
;
788 value
.trinary
.lhs
= lhs
;
789 value
.trinary
.cond
= cond
;
790 value
.trinary
.rhs
= rhs
;
791 value
.type
.node_class
= etree_trinary
;
792 r
= exp_fold_tree_no_dot (&value
, NULL
, lang_first_phase_enum
);
794 return exp_intop (r
.value
);
796 new = stat_alloc (sizeof (new->trinary
));
797 memcpy (new, &value
, sizeof (new->trinary
));
802 exp_unop (int code
, etree_type
*child
)
804 etree_type value
, *new;
807 value
.unary
.type
.node_code
= code
;
808 value
.unary
.child
= child
;
809 value
.unary
.type
.node_class
= etree_unary
;
810 r
= exp_fold_tree_no_dot (&value
, abs_output_section
,
811 lang_first_phase_enum
);
813 return exp_intop (r
.value
);
815 new = stat_alloc (sizeof (new->unary
));
816 memcpy (new, &value
, sizeof (new->unary
));
821 exp_nameop (int code
, const char *name
)
823 etree_type value
, *new;
825 value
.name
.type
.node_code
= code
;
826 value
.name
.name
= name
;
827 value
.name
.type
.node_class
= etree_name
;
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->name
));
834 memcpy (new, &value
, sizeof (new->name
));
840 exp_assop (int code
, const char *dst
, etree_type
*src
)
842 etree_type value
, *new;
844 value
.assign
.type
.node_code
= code
;
846 value
.assign
.src
= src
;
847 value
.assign
.dst
= dst
;
848 value
.assign
.type
.node_class
= etree_assign
;
851 if (exp_fold_tree_no_dot (&value
, &result
))
852 return exp_intop (result
);
854 new = stat_alloc (sizeof (new->assign
));
855 memcpy (new, &value
, sizeof (new->assign
));
859 /* Handle PROVIDE. */
862 exp_provide (const char *dst
, etree_type
*src
)
866 n
= stat_alloc (sizeof (n
->assign
));
867 n
->assign
.type
.node_code
= '=';
868 n
->assign
.type
.node_class
= etree_provide
;
877 exp_assert (etree_type
*exp
, const char *message
)
881 n
= stat_alloc (sizeof (n
->assert_s
));
882 n
->assert_s
.type
.node_code
= '!';
883 n
->assert_s
.type
.node_class
= etree_assert
;
884 n
->assert_s
.child
= exp
;
885 n
->assert_s
.message
= message
;
890 exp_print_tree (etree_type
*tree
)
892 if (config
.map_file
== NULL
)
893 config
.map_file
= stderr
;
897 minfo ("NULL TREE\n");
901 switch (tree
->type
.node_class
)
904 minfo ("0x%v", tree
->value
.value
);
907 if (tree
->rel
.section
->owner
!= NULL
)
908 minfo ("%B:", tree
->rel
.section
->owner
);
909 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
913 if (tree
->assign
.dst
->sdefs
!= NULL
)
914 fprintf (config
.map_file
, "%s (%x) ", tree
->assign
.dst
->name
,
915 tree
->assign
.dst
->sdefs
->value
);
917 fprintf (config
.map_file
, "%s (UNDEFINED)", tree
->assign
.dst
->name
);
919 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
920 exp_print_token (tree
->type
.node_code
, TRUE
);
921 exp_print_tree (tree
->assign
.src
);
925 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
926 exp_print_tree (tree
->assign
.src
);
927 fprintf (config
.map_file
, ")");
930 fprintf (config
.map_file
, "(");
931 exp_print_tree (tree
->binary
.lhs
);
932 exp_print_token (tree
->type
.node_code
, TRUE
);
933 exp_print_tree (tree
->binary
.rhs
);
934 fprintf (config
.map_file
, ")");
937 exp_print_tree (tree
->trinary
.cond
);
938 fprintf (config
.map_file
, "?");
939 exp_print_tree (tree
->trinary
.lhs
);
940 fprintf (config
.map_file
, ":");
941 exp_print_tree (tree
->trinary
.rhs
);
944 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
945 if (tree
->unary
.child
)
947 fprintf (config
.map_file
, " (");
948 exp_print_tree (tree
->unary
.child
);
949 fprintf (config
.map_file
, ")");
954 fprintf (config
.map_file
, "ASSERT (");
955 exp_print_tree (tree
->assert_s
.child
);
956 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
960 fprintf (config
.map_file
, "????????");
963 if (tree
->type
.node_code
== NAME
)
965 fprintf (config
.map_file
, "%s", tree
->name
.name
);
969 exp_print_token (tree
->type
.node_code
, FALSE
);
971 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
981 exp_get_vma (etree_type
*tree
,
984 lang_phase_type allocation_done
)
990 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
991 if (! r
.valid_p
&& name
!= NULL
)
992 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1000 exp_get_value_int (etree_type
*tree
,
1003 lang_phase_type allocation_done
)
1005 return exp_get_vma (tree
, def
, name
, allocation_done
);
1009 exp_get_fill (etree_type
*tree
,
1012 lang_phase_type allocation_done
)
1022 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1023 if (! r
.valid_p
&& name
!= NULL
)
1024 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1026 if (r
.str
!= NULL
&& (len
= strlen (r
.str
)) != 0)
1030 fill
= xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1031 fill
->size
= (len
+ 1) / 2;
1041 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1055 fill
= xmalloc (4 + sizeof (*fill
) - 1);
1057 fill
->data
[0] = (val
>> 24) & 0xff;
1058 fill
->data
[1] = (val
>> 16) & 0xff;
1059 fill
->data
[2] = (val
>> 8) & 0xff;
1060 fill
->data
[3] = (val
>> 0) & 0xff;
1067 exp_get_abs_int (etree_type
*tree
,
1068 int def ATTRIBUTE_UNUSED
,
1070 lang_phase_type allocation_done
)
1072 etree_value_type res
;
1073 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1076 res
.value
+= res
.section
->bfd_section
->vma
;
1078 einfo (_("%F%S non constant expression for %s\n"), name
);
1084 align_n (bfd_vma value
, bfd_vma align
)
1089 value
= (value
+ align
- 1) / align
;
1090 return value
* align
;