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 void exp_print_token
PARAMS ((token_code_type code
));
45 static void make_abs
PARAMS ((etree_value_type
*ptr
));
46 static etree_value_type new_abs
PARAMS ((bfd_vma value
));
47 static void check
PARAMS ((lang_output_section_statement_type
*os
,
48 const char *name
, const char *op
));
49 static etree_value_type new_rel
50 PARAMS ((bfd_vma
, char *, lang_output_section_statement_type
*section
));
51 static etree_value_type new_rel_from_section
52 PARAMS ((bfd_vma value
, lang_output_section_statement_type
*section
));
53 static etree_value_type fold_binary
54 PARAMS ((etree_type
*tree
,
55 lang_output_section_statement_type
*current_section
,
56 lang_phase_type allocation_done
,
57 bfd_vma dot
, bfd_vma
*dotp
));
58 static etree_value_type fold_name
59 PARAMS ((etree_type
*tree
,
60 lang_output_section_statement_type
*current_section
,
61 lang_phase_type allocation_done
,
63 static etree_value_type exp_fold_tree_no_dot
64 PARAMS ((etree_type
*tree
,
65 lang_output_section_statement_type
*current_section
,
66 lang_phase_type allocation_done
));
68 struct exp_data_seg exp_data_seg
;
71 exp_print_token (code
)
106 { SECTIONS
, "SECTIONS" },
107 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
108 { MEMORY
, "MEMORY" },
109 { DEFINED
, "DEFINED" },
110 { TARGET_K
, "TARGET" },
111 { SEARCH_DIR
, "SEARCH_DIR" },
115 { SIZEOF
, "SIZEOF" },
117 { LOADADDR
, "LOADADDR" },
119 { REL
, "relocateable" },
120 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
121 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" }
125 for (idx
= ARRAY_SIZE (table
); idx
--;)
127 if (table
[idx
].code
== code
)
129 fprintf (config
.map_file
, " %s ", table
[idx
].name
);
134 /* Not in table, just print it alone. */
136 fprintf (config
.map_file
, " %c ", code
);
138 fprintf (config
.map_file
, " <code %d> ", code
);
143 etree_value_type
*ptr
;
145 asection
*s
= ptr
->section
->bfd_section
;
146 ptr
->value
+= s
->vma
;
147 ptr
->section
= abs_output_section
;
150 static etree_value_type
154 etree_value_type
new;
156 new.section
= abs_output_section
;
163 lang_output_section_statement_type
*os
;
168 einfo (_("%F%P: %s uses undefined section %s\n"), op
, name
);
170 einfo (_("%F%P: %s forward reference of section %s\n"), op
, name
);
177 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->value
));
178 new->type
.node_code
= INT
;
179 new->value
.value
= value
;
180 new->value
.str
= NULL
;
181 new->type
.node_class
= etree_value
;
186 exp_bigintop (value
, str
)
190 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->value
));
191 new->type
.node_code
= INT
;
192 new->value
.value
= value
;
193 new->value
.str
= str
;
194 new->type
.node_class
= etree_value
;
198 /* Build an expression representing an unnamed relocateable value. */
201 exp_relop (section
, value
)
205 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->rel
));
206 new->type
.node_code
= REL
;
207 new->type
.node_class
= etree_rel
;
208 new->rel
.section
= section
;
209 new->rel
.value
= value
;
213 static etree_value_type
214 new_rel (value
, str
, section
)
217 lang_output_section_statement_type
*section
;
219 etree_value_type
new;
223 new.section
= section
;
227 static etree_value_type
228 new_rel_from_section (value
, section
)
230 lang_output_section_statement_type
*section
;
232 etree_value_type
new;
236 new.section
= section
;
238 new.value
-= section
->bfd_section
->vma
;
243 static etree_value_type
244 fold_binary (tree
, current_section
, allocation_done
, dot
, dotp
)
246 lang_output_section_statement_type
*current_section
;
247 lang_phase_type allocation_done
;
251 etree_value_type result
;
253 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
254 allocation_done
, dot
, dotp
);
257 etree_value_type other
;
259 other
= exp_fold_tree (tree
->binary
.rhs
,
261 allocation_done
, dot
, dotp
);
264 /* If the values are from different sections, or this is an
265 absolute expression, make both the source arguments
266 absolute. However, adding or subtracting an absolute
267 value from a relative value is meaningful, and is an
269 if (current_section
!= abs_output_section
270 && (other
.section
== abs_output_section
271 || (result
.section
== abs_output_section
272 && tree
->type
.node_code
== '+'))
273 && (tree
->type
.node_code
== '+'
274 || tree
->type
.node_code
== '-'))
276 etree_value_type hold
;
278 /* If there is only one absolute term, make sure it is the
280 if (other
.section
!= abs_output_section
)
287 else if (result
.section
!= other
.section
288 || current_section
== abs_output_section
)
294 switch (tree
->type
.node_code
)
297 if (other
.value
== 0)
298 einfo (_("%F%S %% by zero\n"));
299 result
.value
= ((bfd_signed_vma
) result
.value
300 % (bfd_signed_vma
) other
.value
);
304 if (other
.value
== 0)
305 einfo (_("%F%S / by zero\n"));
306 result
.value
= ((bfd_signed_vma
) result
.value
307 / (bfd_signed_vma
) other
.value
);
310 #define BOP(x,y) case x : result.value = result.value y other.value; break;
329 if (result
.value
< other
.value
)
334 if (result
.value
> other
.value
)
338 case DATA_SEGMENT_ALIGN
:
339 if (allocation_done
!= lang_first_phase_enum
340 && current_section
== abs_output_section
341 && (exp_data_seg
.phase
== exp_dataseg_none
342 || exp_data_seg
.phase
== exp_dataseg_adjust
343 || allocation_done
!= lang_allocating_phase_enum
))
345 bfd_vma maxpage
= result
.value
;
347 result
.value
= ALIGN_N (dot
, maxpage
);
348 if (exp_data_seg
.phase
!= exp_dataseg_adjust
)
350 result
.value
+= dot
& (maxpage
- 1);
351 if (allocation_done
== lang_allocating_phase_enum
)
353 exp_data_seg
.phase
= exp_dataseg_align_seen
;
354 exp_data_seg
.base
= result
.value
;
355 exp_data_seg
.pagesize
= other
.value
;
358 else if (other
.value
< maxpage
)
359 result
.value
+= (dot
+ other
.value
- 1)
360 & (maxpage
- other
.value
);
363 result
.valid_p
= false;
372 result
.valid_p
= false;
382 etree_value_type
new;
387 static etree_value_type
388 fold_name (tree
, current_section
, allocation_done
, dot
)
390 lang_output_section_statement_type
*current_section
;
391 lang_phase_type allocation_done
;
394 etree_value_type result
;
396 switch (tree
->type
.node_code
)
399 if (allocation_done
!= lang_first_phase_enum
)
401 result
= new_abs ((bfd_vma
)
402 bfd_sizeof_headers (output_bfd
,
403 link_info
.relocateable
));
407 result
.valid_p
= false;
411 if (allocation_done
== lang_first_phase_enum
)
412 result
.valid_p
= false;
415 struct bfd_link_hash_entry
*h
;
417 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
420 result
.value
= (h
!= (struct bfd_link_hash_entry
*) NULL
421 && (h
->type
== bfd_link_hash_defined
422 || h
->type
== bfd_link_hash_defweak
423 || h
->type
== bfd_link_hash_common
));
425 result
.valid_p
= true;
429 result
.valid_p
= false;
430 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
432 if (allocation_done
!= lang_first_phase_enum
)
433 result
= new_rel_from_section (dot
, current_section
);
437 else if (allocation_done
!= lang_first_phase_enum
)
439 struct bfd_link_hash_entry
*h
;
441 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
445 && (h
->type
== bfd_link_hash_defined
446 || h
->type
== bfd_link_hash_defweak
))
448 if (bfd_is_abs_section (h
->u
.def
.section
))
449 result
= new_abs (h
->u
.def
.value
);
450 else if (allocation_done
== lang_final_phase_enum
451 || allocation_done
== lang_allocating_phase_enum
)
453 asection
*output_section
;
455 output_section
= h
->u
.def
.section
->output_section
;
456 if (output_section
== NULL
)
457 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
461 lang_output_section_statement_type
*os
;
463 os
= (lang_output_section_statement_lookup
464 (bfd_get_section_name (output_bfd
,
467 /* FIXME: Is this correct if this section is
468 being linked with -R? */
469 result
= new_rel ((h
->u
.def
.value
470 + h
->u
.def
.section
->output_offset
),
476 else if (allocation_done
== lang_final_phase_enum
)
477 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
483 if (allocation_done
!= lang_first_phase_enum
)
485 lang_output_section_statement_type
*os
;
487 os
= lang_output_section_find (tree
->name
.name
);
488 check (os
, tree
->name
.name
, "ADDR");
489 result
= new_rel (0, NULL
, os
);
496 if (allocation_done
!= lang_first_phase_enum
)
498 lang_output_section_statement_type
*os
;
500 os
= lang_output_section_find (tree
->name
.name
);
501 check (os
, tree
->name
.name
, "LOADADDR");
502 if (os
->load_base
== NULL
)
503 result
= new_rel (0, NULL
, os
);
505 result
= exp_fold_tree_no_dot (os
->load_base
,
514 if (allocation_done
!= lang_first_phase_enum
)
516 int opb
= bfd_octets_per_byte (output_bfd
);
517 lang_output_section_statement_type
*os
;
519 os
= lang_output_section_find (tree
->name
.name
);
520 check (os
, tree
->name
.name
, "SIZEOF");
521 result
= new_abs (os
->bfd_section
->_raw_size
/ opb
);
536 exp_fold_tree (tree
, current_section
, allocation_done
, dot
, dotp
)
538 lang_output_section_statement_type
*current_section
;
539 lang_phase_type allocation_done
;
543 etree_value_type result
;
547 result
.valid_p
= false;
551 switch (tree
->type
.node_class
)
554 result
= new_rel (tree
->value
.value
, tree
->value
.str
, current_section
);
558 if (allocation_done
!= lang_final_phase_enum
)
559 result
.valid_p
= false;
561 result
= new_rel ((tree
->rel
.value
562 + tree
->rel
.section
->output_section
->vma
563 + tree
->rel
.section
->output_offset
),
569 result
= exp_fold_tree (tree
->assert_s
.child
,
571 allocation_done
, dot
, dotp
);
575 einfo ("%F%P: %s\n", tree
->assert_s
.message
);
581 result
= exp_fold_tree (tree
->unary
.child
,
583 allocation_done
, dot
, dotp
);
586 switch (tree
->type
.node_code
)
589 if (allocation_done
!= lang_first_phase_enum
)
590 result
= new_rel_from_section (ALIGN_N (dot
, result
.value
),
593 result
.valid_p
= false;
597 if (allocation_done
!= lang_first_phase_enum
&& result
.valid_p
)
599 result
.value
+= result
.section
->bfd_section
->vma
;
600 result
.section
= abs_output_section
;
603 result
.valid_p
= false;
608 result
.value
= ~result
.value
;
613 result
.value
= !result
.value
;
618 result
.value
= -result
.value
;
622 /* Return next place aligned to value. */
623 if (allocation_done
== lang_allocating_phase_enum
)
626 result
.value
= ALIGN_N (dot
, result
.value
);
629 result
.valid_p
= false;
632 case DATA_SEGMENT_END
:
633 if (allocation_done
!= lang_first_phase_enum
634 && current_section
== abs_output_section
635 && (exp_data_seg
.phase
== exp_dataseg_align_seen
636 || exp_data_seg
.phase
== exp_dataseg_adjust
637 || allocation_done
!= lang_allocating_phase_enum
))
639 if (exp_data_seg
.phase
== exp_dataseg_align_seen
)
641 exp_data_seg
.phase
= exp_dataseg_end_seen
;
642 exp_data_seg
.end
= result
.value
;
646 result
.valid_p
= false;
657 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
658 allocation_done
, dot
, dotp
);
660 result
= exp_fold_tree ((result
.value
662 : tree
->trinary
.rhs
),
664 allocation_done
, dot
, dotp
);
668 result
= fold_binary (tree
, current_section
, allocation_done
,
675 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
677 /* Assignment to dot can only be done during allocation. */
678 if (tree
->type
.node_class
!= etree_assign
)
679 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
680 if (allocation_done
== lang_allocating_phase_enum
681 || (allocation_done
== lang_final_phase_enum
682 && current_section
== abs_output_section
))
684 result
= exp_fold_tree (tree
->assign
.src
,
686 allocation_done
, dot
,
688 if (! result
.valid_p
)
689 einfo (_("%F%S invalid assignment to location counter\n"));
692 if (current_section
== NULL
)
693 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
698 nextdot
= (result
.value
699 + current_section
->bfd_section
->vma
);
701 && current_section
!= abs_output_section
)
702 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
712 result
= exp_fold_tree (tree
->assign
.src
,
713 current_section
, allocation_done
,
718 struct bfd_link_hash_entry
*h
;
720 if (tree
->type
.node_class
== etree_assign
)
724 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
725 create
, false, false);
726 if (h
== (struct bfd_link_hash_entry
*) NULL
)
728 if (tree
->type
.node_class
== etree_assign
)
729 einfo (_("%P%F:%s: hash creation failed\n"),
732 else if (tree
->type
.node_class
== etree_provide
733 && h
->type
!= bfd_link_hash_undefined
734 && h
->type
!= bfd_link_hash_common
)
736 /* Do nothing. The symbol was defined by some
741 /* FIXME: Should we worry if the symbol is already
743 h
->type
= bfd_link_hash_defined
;
744 h
->u
.def
.value
= result
.value
;
745 h
->u
.def
.section
= result
.section
->bfd_section
;
746 if (tree
->type
.node_class
== etree_provide
)
747 tree
->type
.node_class
= etree_provided
;
754 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
765 static etree_value_type
766 exp_fold_tree_no_dot (tree
, current_section
, allocation_done
)
768 lang_output_section_statement_type
*current_section
;
769 lang_phase_type allocation_done
;
771 return exp_fold_tree (tree
, current_section
, allocation_done
,
772 (bfd_vma
) 0, (bfd_vma
*) NULL
);
776 exp_binop (code
, lhs
, rhs
)
781 etree_type value
, *new;
784 value
.type
.node_code
= code
;
785 value
.binary
.lhs
= lhs
;
786 value
.binary
.rhs
= rhs
;
787 value
.type
.node_class
= etree_binary
;
788 r
= exp_fold_tree_no_dot (&value
,
790 lang_first_phase_enum
);
793 return exp_intop (r
.value
);
795 new = (etree_type
*) stat_alloc (sizeof (new->binary
));
796 memcpy ((char *) new, (char *) &value
, sizeof (new->binary
));
801 exp_trinop (code
, cond
, lhs
, rhs
)
807 etree_type value
, *new;
809 value
.type
.node_code
= code
;
810 value
.trinary
.lhs
= lhs
;
811 value
.trinary
.cond
= cond
;
812 value
.trinary
.rhs
= rhs
;
813 value
.type
.node_class
= etree_trinary
;
814 r
= exp_fold_tree_no_dot (&value
,
815 (lang_output_section_statement_type
*) NULL
,
816 lang_first_phase_enum
);
818 return exp_intop (r
.value
);
820 new = (etree_type
*) stat_alloc (sizeof (new->trinary
));
821 memcpy ((char *) new, (char *) &value
, sizeof (new->trinary
));
826 exp_unop (code
, child
)
830 etree_type value
, *new;
833 value
.unary
.type
.node_code
= code
;
834 value
.unary
.child
= child
;
835 value
.unary
.type
.node_class
= etree_unary
;
836 r
= exp_fold_tree_no_dot (&value
, abs_output_section
,
837 lang_first_phase_enum
);
839 return exp_intop (r
.value
);
841 new = (etree_type
*) stat_alloc (sizeof (new->unary
));
842 memcpy ((char *) new, (char *) &value
, sizeof (new->unary
));
847 exp_nameop (code
, name
)
851 etree_type value
, *new;
853 value
.name
.type
.node_code
= code
;
854 value
.name
.name
= name
;
855 value
.name
.type
.node_class
= etree_name
;
857 r
= exp_fold_tree_no_dot (&value
,
858 (lang_output_section_statement_type
*) NULL
,
859 lang_first_phase_enum
);
861 return exp_intop (r
.value
);
863 new = (etree_type
*) stat_alloc (sizeof (new->name
));
864 memcpy ((char *) new, (char *) &value
, sizeof (new->name
));
870 exp_assop (code
, dst
, src
)
875 etree_type value
, *new;
877 value
.assign
.type
.node_code
= code
;
879 value
.assign
.src
= src
;
880 value
.assign
.dst
= dst
;
881 value
.assign
.type
.node_class
= etree_assign
;
884 if (exp_fold_tree_no_dot (&value
, &result
))
885 return exp_intop (result
);
887 new = (etree_type
*) stat_alloc (sizeof (new->assign
));
888 memcpy ((char *) new, (char *) &value
, sizeof (new->assign
));
892 /* Handle PROVIDE. */
895 exp_provide (dst
, src
)
901 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
902 n
->assign
.type
.node_code
= '=';
903 n
->assign
.type
.node_class
= etree_provide
;
912 exp_assert (exp
, message
)
918 n
= (etree_type
*) 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 (tree
)
930 if (config
.map_file
== NULL
)
931 config
.map_file
= stderr
;
935 minfo ("NULL TREE\n");
939 switch (tree
->type
.node_class
)
942 minfo ("0x%v", tree
->value
.value
);
945 if (tree
->rel
.section
->owner
!= NULL
)
946 minfo ("%B:", tree
->rel
.section
->owner
);
947 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
951 if (tree
->assign
.dst
->sdefs
!= (asymbol
*) NULL
)
952 fprintf (config
.map_file
, "%s (%x) ", tree
->assign
.dst
->name
,
953 tree
->assign
.dst
->sdefs
->value
);
955 fprintf (config
.map_file
, "%s (UNDEFINED)", tree
->assign
.dst
->name
);
957 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
958 exp_print_token (tree
->type
.node_code
);
959 exp_print_tree (tree
->assign
.src
);
963 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
964 exp_print_tree (tree
->assign
.src
);
965 fprintf (config
.map_file
, ")");
968 fprintf (config
.map_file
, "(");
969 exp_print_tree (tree
->binary
.lhs
);
970 exp_print_token (tree
->type
.node_code
);
971 exp_print_tree (tree
->binary
.rhs
);
972 fprintf (config
.map_file
, ")");
975 exp_print_tree (tree
->trinary
.cond
);
976 fprintf (config
.map_file
, "?");
977 exp_print_tree (tree
->trinary
.lhs
);
978 fprintf (config
.map_file
, ":");
979 exp_print_tree (tree
->trinary
.rhs
);
982 exp_print_token (tree
->unary
.type
.node_code
);
983 if (tree
->unary
.child
)
985 fprintf (config
.map_file
, "(");
986 exp_print_tree (tree
->unary
.child
);
987 fprintf (config
.map_file
, ")");
992 fprintf (config
.map_file
, "ASSERT (");
993 exp_print_tree (tree
->assert_s
.child
);
994 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
998 fprintf (config
.map_file
, "????????");
1001 if (tree
->type
.node_code
== NAME
)
1003 fprintf (config
.map_file
, "%s", tree
->name
.name
);
1007 exp_print_token (tree
->type
.node_code
);
1008 if (tree
->name
.name
)
1009 fprintf (config
.map_file
, "(%s)", tree
->name
.name
);
1019 exp_get_vma (tree
, def
, name
, allocation_done
)
1023 lang_phase_type allocation_done
;
1029 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1030 if (! r
.valid_p
&& name
!= NULL
)
1031 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1039 exp_get_value_int (tree
, def
, name
, allocation_done
)
1043 lang_phase_type allocation_done
;
1045 return (int) exp_get_vma (tree
, (bfd_vma
) def
, name
, allocation_done
);
1049 exp_get_fill (tree
, def
, name
, allocation_done
)
1053 lang_phase_type allocation_done
;
1063 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1064 if (! r
.valid_p
&& name
!= NULL
)
1065 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1067 if (r
.str
!= NULL
&& (len
= strlen (r
.str
)) != 0)
1071 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1072 fill
->size
= (len
+ 1) / 2;
1082 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1096 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1098 fill
->data
[0] = (val
>> 24) & 0xff;
1099 fill
->data
[1] = (val
>> 16) & 0xff;
1100 fill
->data
[2] = (val
>> 8) & 0xff;
1101 fill
->data
[3] = (val
>> 0) & 0xff;
1108 exp_get_abs_int (tree
, def
, name
, allocation_done
)
1110 int def ATTRIBUTE_UNUSED
;
1112 lang_phase_type allocation_done
;
1114 etree_value_type res
;
1115 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1118 res
.value
+= res
.section
->bfd_section
->vma
;
1120 einfo (_("%F%S non constant expression for %s\n"), name
);