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
42 static void exp_print_token
PARAMS ((token_code_type code
));
43 static void make_abs
PARAMS ((etree_value_type
*ptr
));
44 static etree_value_type new_abs
PARAMS ((bfd_vma value
));
45 static void check
PARAMS ((lang_output_section_statement_type
*os
,
46 const char *name
, const char *op
));
47 static etree_value_type new_rel
48 PARAMS ((bfd_vma value
, lang_output_section_statement_type
*section
));
49 static etree_value_type new_rel_from_section
50 PARAMS ((bfd_vma value
, lang_output_section_statement_type
*section
));
51 static etree_value_type fold_binary
52 PARAMS ((etree_type
*tree
,
53 lang_output_section_statement_type
*current_section
,
54 lang_phase_type allocation_done
,
55 bfd_vma dot
, bfd_vma
*dotp
));
56 static etree_value_type fold_name
57 PARAMS ((etree_type
*tree
,
58 lang_output_section_statement_type
*current_section
,
59 lang_phase_type allocation_done
,
61 static etree_value_type exp_fold_tree_no_dot
62 PARAMS ((etree_type
*tree
,
63 lang_output_section_statement_type
*current_section
,
64 lang_phase_type allocation_done
));
67 exp_print_token (code
)
75 { REL
, "relocateable" },
95 { SECTIONS
, "SECTIONS" },
96 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
100 { LOADADDR
, "LOADADDR" },
101 { MEMORY
, "MEMORY" },
102 { DEFINED
, "DEFINED" },
103 { TARGET_K
, "TARGET" },
104 { SEARCH_DIR
, "SEARCH_DIR" },
116 for (idx
= 0; table
[idx
].name
!= (char *) NULL
; idx
++)
118 if (table
[idx
].code
== code
)
120 fprintf (config
.map_file
, "%s", table
[idx
].name
);
124 /* Not in table, just print it alone */
125 fprintf (config
.map_file
, "%c", code
);
130 etree_value_type
*ptr
;
132 asection
*s
= ptr
->section
->bfd_section
;
133 ptr
->value
+= s
->vma
;
134 ptr
->section
= abs_output_section
;
137 static etree_value_type
141 etree_value_type
new;
143 new.section
= abs_output_section
;
150 lang_output_section_statement_type
*os
;
155 einfo (_("%F%P: %s uses undefined section %s\n"), op
, name
);
157 einfo (_("%F%P: %s forward reference of section %s\n"), op
, name
);
164 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->value
));
165 new->type
.node_code
= INT
;
166 new->value
.value
= value
;
167 new->type
.node_class
= etree_value
;
172 /* Build an expression representing an unnamed relocateable value. */
175 exp_relop (section
, value
)
179 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->rel
));
180 new->type
.node_code
= REL
;
181 new->type
.node_class
= etree_rel
;
182 new->rel
.section
= section
;
183 new->rel
.value
= value
;
187 static etree_value_type
188 new_rel (value
, section
)
190 lang_output_section_statement_type
*section
;
192 etree_value_type
new;
195 new.section
= section
;
199 static etree_value_type
200 new_rel_from_section (value
, section
)
202 lang_output_section_statement_type
*section
;
204 etree_value_type
new;
207 new.section
= section
;
209 new.value
-= section
->bfd_section
->vma
;
214 static etree_value_type
215 fold_binary (tree
, current_section
, allocation_done
, dot
, dotp
)
217 lang_output_section_statement_type
*current_section
;
218 lang_phase_type allocation_done
;
222 etree_value_type result
;
224 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
225 allocation_done
, dot
, dotp
);
228 etree_value_type other
;
230 other
= exp_fold_tree (tree
->binary
.rhs
,
232 allocation_done
, dot
, dotp
);
235 /* If the values are from different sections, or this is an
236 absolute expression, make both the source arguments
237 absolute. However, adding or subtracting an absolute
238 value from a relative value is meaningful, and is an
240 if (current_section
!= abs_output_section
241 && (other
.section
== abs_output_section
242 || (result
.section
== abs_output_section
243 && tree
->type
.node_code
== '+'))
244 && (tree
->type
.node_code
== '+'
245 || tree
->type
.node_code
== '-'))
247 etree_value_type hold
;
249 /* If there is only one absolute term, make sure it is the
251 if (other
.section
!= abs_output_section
)
258 else if (result
.section
!= other
.section
259 || current_section
== abs_output_section
)
265 switch (tree
->type
.node_code
)
268 if (other
.value
== 0)
269 einfo (_("%F%S %% by zero\n"));
270 result
.value
= ((bfd_signed_vma
) result
.value
271 % (bfd_signed_vma
) other
.value
);
275 if (other
.value
== 0)
276 einfo (_("%F%S / by zero\n"));
277 result
.value
= ((bfd_signed_vma
) result
.value
278 / (bfd_signed_vma
) other
.value
);
281 #define BOP(x,y) case x : result.value = result.value y other.value; break;
300 if (result
.value
< other
.value
)
305 if (result
.value
> other
.value
)
315 result
.valid_p
= false;
325 etree_value_type
new;
330 static etree_value_type
331 fold_name (tree
, current_section
, allocation_done
, dot
)
333 lang_output_section_statement_type
*current_section
;
334 lang_phase_type allocation_done
;
337 etree_value_type result
;
338 switch (tree
->type
.node_code
)
341 if (allocation_done
!= lang_first_phase_enum
)
343 result
= new_abs ((bfd_vma
)
344 bfd_sizeof_headers (output_bfd
,
345 link_info
.relocateable
));
349 result
.valid_p
= false;
353 if (allocation_done
== lang_first_phase_enum
)
354 result
.valid_p
= false;
357 struct bfd_link_hash_entry
*h
;
359 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
362 result
.value
= (h
!= (struct bfd_link_hash_entry
*) NULL
363 && (h
->type
== bfd_link_hash_defined
364 || h
->type
== bfd_link_hash_defweak
365 || h
->type
== bfd_link_hash_common
));
367 result
.valid_p
= true;
371 result
.valid_p
= false;
372 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
374 if (allocation_done
!= lang_first_phase_enum
)
375 result
= new_rel_from_section (dot
, current_section
);
379 else if (allocation_done
!= lang_first_phase_enum
)
381 struct bfd_link_hash_entry
*h
;
383 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
387 && (h
->type
== bfd_link_hash_defined
388 || h
->type
== bfd_link_hash_defweak
))
390 if (bfd_is_abs_section (h
->u
.def
.section
))
391 result
= new_abs (h
->u
.def
.value
);
392 else if (allocation_done
== lang_final_phase_enum
393 || allocation_done
== lang_allocating_phase_enum
)
395 asection
*output_section
;
397 output_section
= h
->u
.def
.section
->output_section
;
398 if (output_section
== NULL
)
399 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
403 lang_output_section_statement_type
*os
;
405 os
= (lang_output_section_statement_lookup
406 (bfd_get_section_name (output_bfd
,
409 /* FIXME: Is this correct if this section is
410 being linked with -R? */
411 result
= new_rel ((h
->u
.def
.value
412 + h
->u
.def
.section
->output_offset
),
417 else if (allocation_done
== lang_final_phase_enum
)
418 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
424 if (allocation_done
!= lang_first_phase_enum
)
426 lang_output_section_statement_type
*os
;
428 os
= lang_output_section_find (tree
->name
.name
);
429 check (os
, tree
->name
.name
, "ADDR");
430 result
= new_rel (0, os
);
437 if (allocation_done
!= lang_first_phase_enum
)
439 lang_output_section_statement_type
*os
;
441 os
= lang_output_section_find (tree
->name
.name
);
442 check (os
, tree
->name
.name
, "LOADADDR");
443 if (os
->load_base
== NULL
)
444 result
= new_rel (0, os
);
446 result
= exp_fold_tree_no_dot (os
->load_base
,
455 if (allocation_done
!= lang_first_phase_enum
)
457 int opb
= bfd_octets_per_byte (output_bfd
);
458 lang_output_section_statement_type
*os
;
460 os
= lang_output_section_find (tree
->name
.name
);
461 check (os
, tree
->name
.name
, "SIZEOF");
462 result
= new_abs (os
->bfd_section
->_raw_size
/ opb
);
477 exp_fold_tree (tree
, current_section
, allocation_done
, dot
, dotp
)
479 lang_output_section_statement_type
*current_section
;
480 lang_phase_type allocation_done
;
484 etree_value_type result
;
488 result
.valid_p
= false;
492 switch (tree
->type
.node_class
)
495 result
= new_rel (tree
->value
.value
, current_section
);
499 if (allocation_done
!= lang_final_phase_enum
)
500 result
.valid_p
= false;
502 result
= new_rel ((tree
->rel
.value
503 + tree
->rel
.section
->output_section
->vma
504 + tree
->rel
.section
->output_offset
),
509 result
= exp_fold_tree (tree
->assert_s
.child
,
511 allocation_done
, dot
, dotp
);
515 einfo ("%F%P: %s\n", tree
->assert_s
.message
);
521 result
= exp_fold_tree (tree
->unary
.child
,
523 allocation_done
, dot
, dotp
);
526 switch (tree
->type
.node_code
)
529 if (allocation_done
!= lang_first_phase_enum
)
530 result
= new_rel_from_section (ALIGN_N (dot
, result
.value
),
533 result
.valid_p
= false;
537 if (allocation_done
!= lang_first_phase_enum
&& result
.valid_p
)
539 result
.value
+= result
.section
->bfd_section
->vma
;
540 result
.section
= abs_output_section
;
543 result
.valid_p
= false;
548 result
.value
= ~result
.value
;
553 result
.value
= !result
.value
;
558 result
.value
= -result
.value
;
562 /* Return next place aligned to value. */
563 if (allocation_done
== lang_allocating_phase_enum
)
566 result
.value
= ALIGN_N (dot
, result
.value
);
569 result
.valid_p
= false;
580 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
581 allocation_done
, dot
, dotp
);
583 result
= exp_fold_tree ((result
.value
585 : tree
->trinary
.rhs
),
587 allocation_done
, dot
, dotp
);
591 result
= fold_binary (tree
, current_section
, allocation_done
,
598 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
600 /* Assignment to dot can only be done during allocation */
601 if (tree
->type
.node_class
!= etree_assign
)
602 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
603 if (allocation_done
== lang_allocating_phase_enum
604 || (allocation_done
== lang_final_phase_enum
605 && current_section
== abs_output_section
))
607 result
= exp_fold_tree (tree
->assign
.src
,
609 lang_allocating_phase_enum
, dot
,
611 if (! result
.valid_p
)
612 einfo (_("%F%S invalid assignment to location counter\n"));
615 if (current_section
== NULL
)
616 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
621 nextdot
= (result
.value
622 + current_section
->bfd_section
->vma
);
624 && current_section
!= abs_output_section
)
626 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
637 result
= exp_fold_tree (tree
->assign
.src
,
638 current_section
, allocation_done
,
643 struct bfd_link_hash_entry
*h
;
645 if (tree
->type
.node_class
== etree_assign
)
649 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
650 create
, false, false);
651 if (h
== (struct bfd_link_hash_entry
*) NULL
)
653 if (tree
->type
.node_class
== etree_assign
)
654 einfo (_("%P%F:%s: hash creation failed\n"),
657 else if (tree
->type
.node_class
== etree_provide
658 && h
->type
!= bfd_link_hash_undefined
659 && h
->type
!= bfd_link_hash_common
)
661 /* Do nothing. The symbol was defined by some
666 /* FIXME: Should we worry if the symbol is already
668 h
->type
= bfd_link_hash_defined
;
669 h
->u
.def
.value
= result
.value
;
670 h
->u
.def
.section
= result
.section
->bfd_section
;
671 if (tree
->type
.node_class
== etree_provide
)
672 tree
->type
.node_class
= etree_provided
;
679 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
690 static etree_value_type
691 exp_fold_tree_no_dot (tree
, current_section
, allocation_done
)
693 lang_output_section_statement_type
*current_section
;
694 lang_phase_type allocation_done
;
696 return exp_fold_tree (tree
, current_section
, allocation_done
,
697 (bfd_vma
) 0, (bfd_vma
*) NULL
);
701 exp_binop (code
, lhs
, rhs
)
706 etree_type value
, *new;
709 value
.type
.node_code
= code
;
710 value
.binary
.lhs
= lhs
;
711 value
.binary
.rhs
= rhs
;
712 value
.type
.node_class
= etree_binary
;
713 r
= exp_fold_tree_no_dot (&value
,
715 lang_first_phase_enum
);
718 return exp_intop (r
.value
);
720 new = (etree_type
*) stat_alloc (sizeof (new->binary
));
721 memcpy ((char *) new, (char *) &value
, sizeof (new->binary
));
726 exp_trinop (code
, cond
, lhs
, rhs
)
732 etree_type value
, *new;
734 value
.type
.node_code
= code
;
735 value
.trinary
.lhs
= lhs
;
736 value
.trinary
.cond
= cond
;
737 value
.trinary
.rhs
= rhs
;
738 value
.type
.node_class
= etree_trinary
;
739 r
= exp_fold_tree_no_dot (&value
,
740 (lang_output_section_statement_type
*) NULL
,
741 lang_first_phase_enum
);
744 return exp_intop (r
.value
);
746 new = (etree_type
*) stat_alloc (sizeof (new->trinary
));
747 memcpy ((char *) new, (char *) &value
, sizeof (new->trinary
));
752 exp_unop (code
, child
)
756 etree_type value
, *new;
759 value
.unary
.type
.node_code
= code
;
760 value
.unary
.child
= child
;
761 value
.unary
.type
.node_class
= etree_unary
;
762 r
= exp_fold_tree_no_dot (&value
, abs_output_section
,
763 lang_first_phase_enum
);
766 return exp_intop (r
.value
);
768 new = (etree_type
*) stat_alloc (sizeof (new->unary
));
769 memcpy ((char *) new, (char *) &value
, sizeof (new->unary
));
774 exp_nameop (code
, name
)
778 etree_type value
, *new;
780 value
.name
.type
.node_code
= code
;
781 value
.name
.name
= name
;
782 value
.name
.type
.node_class
= etree_name
;
784 r
= exp_fold_tree_no_dot (&value
,
785 (lang_output_section_statement_type
*) NULL
,
786 lang_first_phase_enum
);
789 return exp_intop (r
.value
);
791 new = (etree_type
*) stat_alloc (sizeof (new->name
));
792 memcpy ((char *) new, (char *) &value
, sizeof (new->name
));
798 exp_assop (code
, dst
, src
)
803 etree_type value
, *new;
805 value
.assign
.type
.node_code
= code
;
807 value
.assign
.src
= src
;
808 value
.assign
.dst
= dst
;
809 value
.assign
.type
.node_class
= etree_assign
;
812 if (exp_fold_tree_no_dot (&value
, &result
))
814 return exp_intop (result
);
817 new = (etree_type
*) stat_alloc (sizeof (new->assign
));
818 memcpy ((char *) new, (char *) &value
, sizeof (new->assign
));
822 /* Handle PROVIDE. */
825 exp_provide (dst
, src
)
831 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
832 n
->assign
.type
.node_code
= '=';
833 n
->assign
.type
.node_class
= etree_provide
;
842 exp_assert (exp
, message
)
848 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
849 n
->assert_s
.type
.node_code
= '!';
850 n
->assert_s
.type
.node_class
= etree_assert
;
851 n
->assert_s
.child
= exp
;
852 n
->assert_s
.message
= message
;
857 exp_print_tree (tree
)
860 switch (tree
->type
.node_class
)
863 minfo ("0x%v", tree
->value
.value
);
866 if (tree
->rel
.section
->owner
!= NULL
)
867 minfo ("%B:", tree
->rel
.section
->owner
);
868 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
872 if (tree
->assign
.dst
->sdefs
!= (asymbol
*) NULL
)
874 fprintf (config
.map_file
, "%s (%x) ", tree
->assign
.dst
->name
,
875 tree
->assign
.dst
->sdefs
->value
);
879 fprintf (config
.map_file
, "%s (UNDEFINED)", tree
->assign
.dst
->name
);
882 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
883 exp_print_token (tree
->type
.node_code
);
884 exp_print_tree (tree
->assign
.src
);
888 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
889 exp_print_tree (tree
->assign
.src
);
890 fprintf (config
.map_file
, ")");
893 fprintf (config
.map_file
, "(");
894 exp_print_tree (tree
->binary
.lhs
);
895 exp_print_token (tree
->type
.node_code
);
896 exp_print_tree (tree
->binary
.rhs
);
897 fprintf (config
.map_file
, ")");
900 exp_print_tree (tree
->trinary
.cond
);
901 fprintf (config
.map_file
, "?");
902 exp_print_tree (tree
->trinary
.lhs
);
903 fprintf (config
.map_file
, ":");
904 exp_print_tree (tree
->trinary
.rhs
);
907 exp_print_token (tree
->unary
.type
.node_code
);
908 if (tree
->unary
.child
)
910 fprintf (config
.map_file
, "(");
911 exp_print_tree (tree
->unary
.child
);
912 fprintf (config
.map_file
, ")");
917 fprintf (config
.map_file
, "ASSERT (");
918 exp_print_tree (tree
->assert_s
.child
);
919 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
923 fprintf (config
.map_file
, "????????");
926 if (tree
->type
.node_code
== NAME
)
928 fprintf (config
.map_file
, "%s", tree
->name
.name
);
932 exp_print_token (tree
->type
.node_code
);
934 fprintf (config
.map_file
, "(%s)", tree
->name
.name
);
944 exp_get_vma (tree
, def
, name
, allocation_done
)
948 lang_phase_type allocation_done
;
954 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
955 if (! r
.valid_p
&& name
!= NULL
)
956 einfo (_("%F%S nonconstant expression for %s\n"), name
);
964 exp_get_value_int (tree
, def
, name
, allocation_done
)
968 lang_phase_type allocation_done
;
970 return (int) exp_get_vma (tree
, (bfd_vma
) def
, name
, allocation_done
);
974 exp_get_abs_int (tree
, def
, name
, allocation_done
)
976 int def ATTRIBUTE_UNUSED
;
978 lang_phase_type allocation_done
;
980 etree_value_type res
;
981 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
985 res
.value
+= res
.section
->bfd_section
->vma
;
989 einfo (_("%F%S non constant expression for %s\n"), name
);