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"
43 static void exp_print_token
PARAMS ((token_code_type code
));
44 static void make_abs
PARAMS ((etree_value_type
*ptr
));
45 static etree_value_type new_abs
PARAMS ((bfd_vma value
));
46 static void check
PARAMS ((lang_output_section_statement_type
*os
,
47 const char *name
, const char *op
));
48 static etree_value_type new_rel
49 PARAMS ((bfd_vma value
, lang_output_section_statement_type
*section
));
50 static etree_value_type new_rel_from_section
51 PARAMS ((bfd_vma value
, lang_output_section_statement_type
*section
));
52 static etree_value_type fold_binary
53 PARAMS ((etree_type
*tree
,
54 lang_output_section_statement_type
*current_section
,
55 lang_phase_type allocation_done
,
56 bfd_vma dot
, bfd_vma
*dotp
));
57 static etree_value_type fold_name
58 PARAMS ((etree_type
*tree
,
59 lang_output_section_statement_type
*current_section
,
60 lang_phase_type allocation_done
,
62 static etree_value_type exp_fold_tree_no_dot
63 PARAMS ((etree_type
*tree
,
64 lang_output_section_statement_type
*current_section
,
65 lang_phase_type allocation_done
));
68 exp_print_token (code
)
103 { SECTIONS
, "SECTIONS" },
104 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
105 { MEMORY
, "MEMORY" },
106 { DEFINED
, "DEFINED" },
107 { TARGET_K
, "TARGET" },
108 { SEARCH_DIR
, "SEARCH_DIR" },
112 { SIZEOF
, "SIZEOF" },
114 { LOADADDR
, "LOADADDR" },
116 { REL
, "relocateable" },
120 for (idx
= ARRAY_SIZE (table
); idx
--;)
122 if (table
[idx
].code
== code
)
124 fprintf (config
.map_file
, " %s ", table
[idx
].name
);
129 /* Not in table, just print it alone. */
131 fprintf (config
.map_file
, " %c ", code
);
133 fprintf (config
.map_file
, " <code %d> ", code
);
138 etree_value_type
*ptr
;
140 asection
*s
= ptr
->section
->bfd_section
;
141 ptr
->value
+= s
->vma
;
142 ptr
->section
= abs_output_section
;
145 static etree_value_type
149 etree_value_type
new;
151 new.section
= abs_output_section
;
158 lang_output_section_statement_type
*os
;
163 einfo (_("%F%P: %s uses undefined section %s\n"), op
, name
);
165 einfo (_("%F%P: %s forward reference of section %s\n"), op
, name
);
172 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->value
));
173 new->type
.node_code
= INT
;
174 new->value
.value
= value
;
175 new->type
.node_class
= etree_value
;
180 /* Build an expression representing an unnamed relocateable value. */
183 exp_relop (section
, value
)
187 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->rel
));
188 new->type
.node_code
= REL
;
189 new->type
.node_class
= etree_rel
;
190 new->rel
.section
= section
;
191 new->rel
.value
= value
;
195 static etree_value_type
196 new_rel (value
, section
)
198 lang_output_section_statement_type
*section
;
200 etree_value_type
new;
203 new.section
= section
;
207 static etree_value_type
208 new_rel_from_section (value
, section
)
210 lang_output_section_statement_type
*section
;
212 etree_value_type
new;
215 new.section
= section
;
217 new.value
-= section
->bfd_section
->vma
;
222 static etree_value_type
223 fold_binary (tree
, current_section
, allocation_done
, dot
, dotp
)
225 lang_output_section_statement_type
*current_section
;
226 lang_phase_type allocation_done
;
230 etree_value_type result
;
232 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
233 allocation_done
, dot
, dotp
);
236 etree_value_type other
;
238 other
= exp_fold_tree (tree
->binary
.rhs
,
240 allocation_done
, dot
, dotp
);
243 /* If the values are from different sections, or this is an
244 absolute expression, make both the source arguments
245 absolute. However, adding or subtracting an absolute
246 value from a relative value is meaningful, and is an
248 if (current_section
!= abs_output_section
249 && (other
.section
== abs_output_section
250 || (result
.section
== abs_output_section
251 && tree
->type
.node_code
== '+'))
252 && (tree
->type
.node_code
== '+'
253 || tree
->type
.node_code
== '-'))
255 etree_value_type hold
;
257 /* If there is only one absolute term, make sure it is the
259 if (other
.section
!= abs_output_section
)
266 else if (result
.section
!= other
.section
267 || current_section
== abs_output_section
)
273 switch (tree
->type
.node_code
)
276 if (other
.value
== 0)
277 einfo (_("%F%S %% by zero\n"));
278 result
.value
= ((bfd_signed_vma
) result
.value
279 % (bfd_signed_vma
) other
.value
);
283 if (other
.value
== 0)
284 einfo (_("%F%S / by zero\n"));
285 result
.value
= ((bfd_signed_vma
) result
.value
286 / (bfd_signed_vma
) other
.value
);
289 #define BOP(x,y) case x : result.value = result.value y other.value; break;
308 if (result
.value
< other
.value
)
313 if (result
.value
> other
.value
)
323 result
.valid_p
= false;
333 etree_value_type
new;
338 static etree_value_type
339 fold_name (tree
, current_section
, allocation_done
, dot
)
341 lang_output_section_statement_type
*current_section
;
342 lang_phase_type allocation_done
;
345 etree_value_type result
;
347 switch (tree
->type
.node_code
)
350 if (allocation_done
!= lang_first_phase_enum
)
352 result
= new_abs ((bfd_vma
)
353 bfd_sizeof_headers (output_bfd
,
354 link_info
.relocateable
));
358 result
.valid_p
= false;
362 if (allocation_done
== lang_first_phase_enum
)
363 result
.valid_p
= false;
366 struct bfd_link_hash_entry
*h
;
368 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
371 result
.value
= (h
!= (struct bfd_link_hash_entry
*) NULL
372 && (h
->type
== bfd_link_hash_defined
373 || h
->type
== bfd_link_hash_defweak
374 || h
->type
== bfd_link_hash_common
));
376 result
.valid_p
= true;
380 result
.valid_p
= false;
381 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
383 if (allocation_done
!= lang_first_phase_enum
)
384 result
= new_rel_from_section (dot
, current_section
);
388 else if (allocation_done
!= lang_first_phase_enum
)
390 struct bfd_link_hash_entry
*h
;
392 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
396 && (h
->type
== bfd_link_hash_defined
397 || h
->type
== bfd_link_hash_defweak
))
399 if (bfd_is_abs_section (h
->u
.def
.section
))
400 result
= new_abs (h
->u
.def
.value
);
401 else if (allocation_done
== lang_final_phase_enum
402 || allocation_done
== lang_allocating_phase_enum
)
404 asection
*output_section
;
406 output_section
= h
->u
.def
.section
->output_section
;
407 if (output_section
== NULL
)
408 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
412 lang_output_section_statement_type
*os
;
414 os
= (lang_output_section_statement_lookup
415 (bfd_get_section_name (output_bfd
,
418 /* FIXME: Is this correct if this section is
419 being linked with -R? */
420 result
= new_rel ((h
->u
.def
.value
421 + h
->u
.def
.section
->output_offset
),
426 else if (allocation_done
== lang_final_phase_enum
)
427 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
433 if (allocation_done
!= lang_first_phase_enum
)
435 lang_output_section_statement_type
*os
;
437 os
= lang_output_section_find (tree
->name
.name
);
438 check (os
, tree
->name
.name
, "ADDR");
439 result
= new_rel (0, os
);
446 if (allocation_done
!= lang_first_phase_enum
)
448 lang_output_section_statement_type
*os
;
450 os
= lang_output_section_find (tree
->name
.name
);
451 check (os
, tree
->name
.name
, "LOADADDR");
452 if (os
->load_base
== NULL
)
453 result
= new_rel (0, os
);
455 result
= exp_fold_tree_no_dot (os
->load_base
,
464 if (allocation_done
!= lang_first_phase_enum
)
466 int opb
= bfd_octets_per_byte (output_bfd
);
467 lang_output_section_statement_type
*os
;
469 os
= lang_output_section_find (tree
->name
.name
);
470 check (os
, tree
->name
.name
, "SIZEOF");
471 result
= new_abs (os
->bfd_section
->_raw_size
/ opb
);
486 exp_fold_tree (tree
, current_section
, allocation_done
, dot
, dotp
)
488 lang_output_section_statement_type
*current_section
;
489 lang_phase_type allocation_done
;
493 etree_value_type result
;
497 result
.valid_p
= false;
501 switch (tree
->type
.node_class
)
504 result
= new_rel (tree
->value
.value
, current_section
);
508 if (allocation_done
!= lang_final_phase_enum
)
509 result
.valid_p
= false;
511 result
= new_rel ((tree
->rel
.value
512 + tree
->rel
.section
->output_section
->vma
513 + tree
->rel
.section
->output_offset
),
518 result
= exp_fold_tree (tree
->assert_s
.child
,
520 allocation_done
, dot
, dotp
);
524 einfo ("%F%P: %s\n", tree
->assert_s
.message
);
530 result
= exp_fold_tree (tree
->unary
.child
,
532 allocation_done
, dot
, dotp
);
535 switch (tree
->type
.node_code
)
538 if (allocation_done
!= lang_first_phase_enum
)
539 result
= new_rel_from_section (ALIGN_N (dot
, result
.value
),
542 result
.valid_p
= false;
546 if (allocation_done
!= lang_first_phase_enum
&& result
.valid_p
)
548 result
.value
+= result
.section
->bfd_section
->vma
;
549 result
.section
= abs_output_section
;
552 result
.valid_p
= false;
557 result
.value
= ~result
.value
;
562 result
.value
= !result
.value
;
567 result
.value
= -result
.value
;
571 /* Return next place aligned to value. */
572 if (allocation_done
== lang_allocating_phase_enum
)
575 result
.value
= ALIGN_N (dot
, result
.value
);
578 result
.valid_p
= false;
589 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
590 allocation_done
, dot
, dotp
);
592 result
= exp_fold_tree ((result
.value
594 : tree
->trinary
.rhs
),
596 allocation_done
, dot
, dotp
);
600 result
= fold_binary (tree
, current_section
, allocation_done
,
607 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
609 /* Assignment to dot can only be done during allocation. */
610 if (tree
->type
.node_class
!= etree_assign
)
611 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
612 if (allocation_done
== lang_allocating_phase_enum
613 || (allocation_done
== lang_final_phase_enum
614 && current_section
== abs_output_section
))
616 result
= exp_fold_tree (tree
->assign
.src
,
618 lang_allocating_phase_enum
, dot
,
620 if (! result
.valid_p
)
621 einfo (_("%F%S invalid assignment to location counter\n"));
624 if (current_section
== NULL
)
625 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
630 nextdot
= (result
.value
631 + current_section
->bfd_section
->vma
);
633 && current_section
!= abs_output_section
)
634 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
644 result
= exp_fold_tree (tree
->assign
.src
,
645 current_section
, allocation_done
,
650 struct bfd_link_hash_entry
*h
;
652 if (tree
->type
.node_class
== etree_assign
)
656 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
657 create
, false, false);
658 if (h
== (struct bfd_link_hash_entry
*) NULL
)
660 if (tree
->type
.node_class
== etree_assign
)
661 einfo (_("%P%F:%s: hash creation failed\n"),
664 else if (tree
->type
.node_class
== etree_provide
665 && h
->type
!= bfd_link_hash_undefined
666 && h
->type
!= bfd_link_hash_common
)
668 /* Do nothing. The symbol was defined by some
673 /* FIXME: Should we worry if the symbol is already
675 h
->type
= bfd_link_hash_defined
;
676 h
->u
.def
.value
= result
.value
;
677 h
->u
.def
.section
= result
.section
->bfd_section
;
678 if (tree
->type
.node_class
== etree_provide
)
679 tree
->type
.node_class
= etree_provided
;
686 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
697 static etree_value_type
698 exp_fold_tree_no_dot (tree
, current_section
, allocation_done
)
700 lang_output_section_statement_type
*current_section
;
701 lang_phase_type allocation_done
;
703 return exp_fold_tree (tree
, current_section
, allocation_done
,
704 (bfd_vma
) 0, (bfd_vma
*) NULL
);
708 exp_binop (code
, lhs
, rhs
)
713 etree_type value
, *new;
716 value
.type
.node_code
= code
;
717 value
.binary
.lhs
= lhs
;
718 value
.binary
.rhs
= rhs
;
719 value
.type
.node_class
= etree_binary
;
720 r
= exp_fold_tree_no_dot (&value
,
722 lang_first_phase_enum
);
725 return exp_intop (r
.value
);
727 new = (etree_type
*) stat_alloc (sizeof (new->binary
));
728 memcpy ((char *) new, (char *) &value
, sizeof (new->binary
));
733 exp_trinop (code
, cond
, lhs
, rhs
)
739 etree_type value
, *new;
741 value
.type
.node_code
= code
;
742 value
.trinary
.lhs
= lhs
;
743 value
.trinary
.cond
= cond
;
744 value
.trinary
.rhs
= rhs
;
745 value
.type
.node_class
= etree_trinary
;
746 r
= exp_fold_tree_no_dot (&value
,
747 (lang_output_section_statement_type
*) NULL
,
748 lang_first_phase_enum
);
750 return exp_intop (r
.value
);
752 new = (etree_type
*) stat_alloc (sizeof (new->trinary
));
753 memcpy ((char *) new, (char *) &value
, sizeof (new->trinary
));
758 exp_unop (code
, child
)
762 etree_type value
, *new;
765 value
.unary
.type
.node_code
= code
;
766 value
.unary
.child
= child
;
767 value
.unary
.type
.node_class
= etree_unary
;
768 r
= exp_fold_tree_no_dot (&value
, abs_output_section
,
769 lang_first_phase_enum
);
771 return exp_intop (r
.value
);
773 new = (etree_type
*) stat_alloc (sizeof (new->unary
));
774 memcpy ((char *) new, (char *) &value
, sizeof (new->unary
));
779 exp_nameop (code
, name
)
783 etree_type value
, *new;
785 value
.name
.type
.node_code
= code
;
786 value
.name
.name
= name
;
787 value
.name
.type
.node_class
= etree_name
;
789 r
= exp_fold_tree_no_dot (&value
,
790 (lang_output_section_statement_type
*) NULL
,
791 lang_first_phase_enum
);
793 return exp_intop (r
.value
);
795 new = (etree_type
*) stat_alloc (sizeof (new->name
));
796 memcpy ((char *) new, (char *) &value
, sizeof (new->name
));
802 exp_assop (code
, dst
, src
)
807 etree_type value
, *new;
809 value
.assign
.type
.node_code
= code
;
811 value
.assign
.src
= src
;
812 value
.assign
.dst
= dst
;
813 value
.assign
.type
.node_class
= etree_assign
;
816 if (exp_fold_tree_no_dot (&value
, &result
))
817 return exp_intop (result
);
819 new = (etree_type
*) stat_alloc (sizeof (new->assign
));
820 memcpy ((char *) new, (char *) &value
, sizeof (new->assign
));
824 /* Handle PROVIDE. */
827 exp_provide (dst
, src
)
833 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
834 n
->assign
.type
.node_code
= '=';
835 n
->assign
.type
.node_class
= etree_provide
;
844 exp_assert (exp
, message
)
850 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
851 n
->assert_s
.type
.node_code
= '!';
852 n
->assert_s
.type
.node_class
= etree_assert
;
853 n
->assert_s
.child
= exp
;
854 n
->assert_s
.message
= message
;
859 exp_print_tree (tree
)
862 if (config
.map_file
== NULL
)
863 config
.map_file
= stderr
;
867 minfo ("NULL TREE\n");
871 switch (tree
->type
.node_class
)
874 minfo ("0x%v", tree
->value
.value
);
877 if (tree
->rel
.section
->owner
!= NULL
)
878 minfo ("%B:", tree
->rel
.section
->owner
);
879 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
883 if (tree
->assign
.dst
->sdefs
!= (asymbol
*) NULL
)
884 fprintf (config
.map_file
, "%s (%x) ", tree
->assign
.dst
->name
,
885 tree
->assign
.dst
->sdefs
->value
);
887 fprintf (config
.map_file
, "%s (UNDEFINED)", tree
->assign
.dst
->name
);
889 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
890 exp_print_token (tree
->type
.node_code
);
891 exp_print_tree (tree
->assign
.src
);
895 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
896 exp_print_tree (tree
->assign
.src
);
897 fprintf (config
.map_file
, ")");
900 fprintf (config
.map_file
, "(");
901 exp_print_tree (tree
->binary
.lhs
);
902 exp_print_token (tree
->type
.node_code
);
903 exp_print_tree (tree
->binary
.rhs
);
904 fprintf (config
.map_file
, ")");
907 exp_print_tree (tree
->trinary
.cond
);
908 fprintf (config
.map_file
, "?");
909 exp_print_tree (tree
->trinary
.lhs
);
910 fprintf (config
.map_file
, ":");
911 exp_print_tree (tree
->trinary
.rhs
);
914 exp_print_token (tree
->unary
.type
.node_code
);
915 if (tree
->unary
.child
)
917 fprintf (config
.map_file
, "(");
918 exp_print_tree (tree
->unary
.child
);
919 fprintf (config
.map_file
, ")");
924 fprintf (config
.map_file
, "ASSERT (");
925 exp_print_tree (tree
->assert_s
.child
);
926 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
930 fprintf (config
.map_file
, "????????");
933 if (tree
->type
.node_code
== NAME
)
935 fprintf (config
.map_file
, "%s", tree
->name
.name
);
939 exp_print_token (tree
->type
.node_code
);
941 fprintf (config
.map_file
, "(%s)", tree
->name
.name
);
951 exp_get_vma (tree
, def
, name
, allocation_done
)
955 lang_phase_type allocation_done
;
961 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
962 if (! r
.valid_p
&& name
!= NULL
)
963 einfo (_("%F%S nonconstant expression for %s\n"), name
);
971 exp_get_value_int (tree
, def
, name
, allocation_done
)
975 lang_phase_type allocation_done
;
977 return (int) exp_get_vma (tree
, (bfd_vma
) def
, name
, allocation_done
);
981 exp_get_abs_int (tree
, def
, name
, allocation_done
)
983 int def ATTRIBUTE_UNUSED
;
985 lang_phase_type allocation_done
;
987 etree_value_type res
;
988 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
991 res
.value
+= res
.section
->bfd_section
->vma
;
993 einfo (_("%F%S non constant expression for %s\n"), name
);