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 check (lang_output_section_statement_type
*os
,
150 einfo (_("%F%P: %s uses undefined section %s\n"), op
, name
);
152 einfo (_("%F%P: %s forward reference of section %s\n"), op
, name
);
156 exp_intop (bfd_vma value
)
158 etree_type
*new = stat_alloc (sizeof (new->value
));
159 new->type
.node_code
= INT
;
160 new->value
.value
= value
;
161 new->value
.str
= NULL
;
162 new->type
.node_class
= etree_value
;
167 exp_bigintop (bfd_vma value
, char *str
)
169 etree_type
*new = stat_alloc (sizeof (new->value
));
170 new->type
.node_code
= INT
;
171 new->value
.value
= value
;
172 new->value
.str
= str
;
173 new->type
.node_class
= etree_value
;
177 /* Build an expression representing an unnamed relocatable value. */
180 exp_relop (asection
*section
, bfd_vma value
)
182 etree_type
*new = stat_alloc (sizeof (new->rel
));
183 new->type
.node_code
= REL
;
184 new->type
.node_class
= etree_rel
;
185 new->rel
.section
= section
;
186 new->rel
.value
= value
;
190 static etree_value_type
191 new_rel (bfd_vma value
,
193 lang_output_section_statement_type
*section
)
195 etree_value_type
new;
199 new.section
= section
;
203 static etree_value_type
204 new_rel_from_section (bfd_vma value
,
205 lang_output_section_statement_type
*section
)
207 etree_value_type
new;
211 new.section
= section
;
213 new.value
-= section
->bfd_section
->vma
;
218 static etree_value_type
219 fold_unary (etree_type
*tree
,
220 lang_output_section_statement_type
*current_section
,
221 lang_phase_type allocation_done
,
225 etree_value_type result
;
227 result
= exp_fold_tree (tree
->unary
.child
,
229 allocation_done
, dot
, dotp
);
232 switch (tree
->type
.node_code
)
235 if (allocation_done
!= lang_first_phase_enum
)
236 result
= new_rel_from_section (align_n (dot
, result
.value
),
239 result
.valid_p
= FALSE
;
243 if (allocation_done
!= lang_first_phase_enum
)
245 result
.value
+= result
.section
->bfd_section
->vma
;
246 result
.section
= abs_output_section
;
249 result
.valid_p
= FALSE
;
254 result
.value
= ~result
.value
;
259 result
.value
= !result
.value
;
264 result
.value
= -result
.value
;
268 /* Return next place aligned to value. */
269 if (allocation_done
== lang_allocating_phase_enum
)
272 result
.value
= align_n (dot
, result
.value
);
275 result
.valid_p
= FALSE
;
278 case DATA_SEGMENT_END
:
279 if (allocation_done
!= lang_first_phase_enum
280 && current_section
== abs_output_section
281 && (exp_data_seg
.phase
== exp_dataseg_align_seen
282 || exp_data_seg
.phase
== exp_dataseg_adjust
283 || allocation_done
!= lang_allocating_phase_enum
))
285 if (exp_data_seg
.phase
== exp_dataseg_align_seen
)
287 exp_data_seg
.phase
= exp_dataseg_end_seen
;
288 exp_data_seg
.end
= result
.value
;
292 result
.valid_p
= FALSE
;
304 static etree_value_type
305 fold_binary (etree_type
*tree
,
306 lang_output_section_statement_type
*current_section
,
307 lang_phase_type allocation_done
,
311 etree_value_type result
;
313 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
314 allocation_done
, dot
, dotp
);
317 etree_value_type other
;
319 other
= exp_fold_tree (tree
->binary
.rhs
,
321 allocation_done
, dot
, dotp
);
324 /* If the values are from different sections, or this is an
325 absolute expression, make both the source arguments
326 absolute. However, adding or subtracting an absolute
327 value from a relative value is meaningful, and is an
329 if (current_section
!= abs_output_section
330 && (other
.section
== abs_output_section
331 || (result
.section
== abs_output_section
332 && tree
->type
.node_code
== '+'))
333 && (tree
->type
.node_code
== '+'
334 || tree
->type
.node_code
== '-'))
336 if (other
.section
!= abs_output_section
)
338 /* Keep the section of the other term. */
339 if (tree
->type
.node_code
== '+')
340 other
.value
= result
.value
+ other
.value
;
342 other
.value
= result
.value
- other
.value
;
346 else if (result
.section
!= other
.section
347 || current_section
== abs_output_section
)
353 switch (tree
->type
.node_code
)
356 if (other
.value
== 0)
357 einfo (_("%F%S %% by zero\n"));
358 result
.value
= ((bfd_signed_vma
) result
.value
359 % (bfd_signed_vma
) other
.value
);
363 if (other
.value
== 0)
364 einfo (_("%F%S / by zero\n"));
365 result
.value
= ((bfd_signed_vma
) result
.value
366 / (bfd_signed_vma
) other
.value
);
369 #define BOP(x,y) case x : result.value = result.value y other.value; break;
388 if (result
.value
< other
.value
)
393 if (result
.value
> other
.value
)
397 case DATA_SEGMENT_ALIGN
:
398 if (allocation_done
!= lang_first_phase_enum
399 && current_section
== abs_output_section
400 && (exp_data_seg
.phase
== exp_dataseg_none
401 || exp_data_seg
.phase
== exp_dataseg_adjust
402 || allocation_done
!= lang_allocating_phase_enum
))
404 bfd_vma maxpage
= result
.value
;
406 result
.value
= align_n (dot
, maxpage
);
407 if (exp_data_seg
.phase
!= exp_dataseg_adjust
)
409 result
.value
+= dot
& (maxpage
- 1);
410 if (allocation_done
== lang_allocating_phase_enum
)
412 exp_data_seg
.phase
= exp_dataseg_align_seen
;
413 exp_data_seg
.base
= result
.value
;
414 exp_data_seg
.pagesize
= other
.value
;
417 else if (other
.value
< maxpage
)
418 result
.value
+= (dot
+ other
.value
- 1)
419 & (maxpage
- other
.value
);
422 result
.valid_p
= FALSE
;
431 result
.valid_p
= FALSE
;
438 static etree_value_type
439 fold_trinary (etree_type
*tree
,
440 lang_output_section_statement_type
*current_section
,
441 lang_phase_type allocation_done
,
445 etree_value_type result
;
447 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
448 allocation_done
, dot
, dotp
);
450 result
= exp_fold_tree ((result
.value
452 : tree
->trinary
.rhs
),
454 allocation_done
, dot
, dotp
);
462 etree_value_type
new;
467 static etree_value_type
468 fold_name (etree_type
*tree
,
469 lang_output_section_statement_type
*current_section
,
470 lang_phase_type allocation_done
,
473 etree_value_type result
;
475 switch (tree
->type
.node_code
)
478 if (allocation_done
!= lang_first_phase_enum
)
480 result
= new_abs (bfd_sizeof_headers (output_bfd
,
481 link_info
.relocatable
));
485 result
.valid_p
= FALSE
;
489 if (allocation_done
== lang_first_phase_enum
)
491 lang_track_definedness (tree
->name
.name
);
492 result
.valid_p
= FALSE
;
496 struct bfd_link_hash_entry
*h
;
498 = lang_symbol_definition_iteration (tree
->name
.name
);
500 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
503 result
.value
= (h
!= NULL
504 && (h
->type
== bfd_link_hash_defined
505 || h
->type
== bfd_link_hash_defweak
506 || h
->type
== bfd_link_hash_common
)
507 && (def_iteration
== lang_statement_iteration
508 || def_iteration
== -1));
509 result
.section
= abs_output_section
;
510 result
.valid_p
= TRUE
;
514 result
.valid_p
= FALSE
;
515 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
517 if (allocation_done
!= lang_first_phase_enum
)
518 result
= new_rel_from_section (dot
, current_section
);
522 else if (allocation_done
!= lang_first_phase_enum
)
524 struct bfd_link_hash_entry
*h
;
526 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
530 && (h
->type
== bfd_link_hash_defined
531 || h
->type
== bfd_link_hash_defweak
))
533 if (bfd_is_abs_section (h
->u
.def
.section
))
534 result
= new_abs (h
->u
.def
.value
);
535 else if (allocation_done
== lang_final_phase_enum
536 || allocation_done
== lang_allocating_phase_enum
)
538 asection
*output_section
;
540 output_section
= h
->u
.def
.section
->output_section
;
541 if (output_section
== NULL
)
542 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
546 lang_output_section_statement_type
*os
;
548 os
= (lang_output_section_statement_lookup
549 (bfd_get_section_name (output_bfd
,
552 /* FIXME: Is this correct if this section is
553 being linked with -R? */
554 result
= new_rel ((h
->u
.def
.value
555 + h
->u
.def
.section
->output_offset
),
561 else if (allocation_done
== lang_final_phase_enum
)
562 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
568 if (allocation_done
!= lang_first_phase_enum
)
570 lang_output_section_statement_type
*os
;
572 os
= lang_output_section_find (tree
->name
.name
);
573 check (os
, tree
->name
.name
, "ADDR");
574 result
= new_rel (0, NULL
, os
);
581 if (allocation_done
!= lang_first_phase_enum
)
583 lang_output_section_statement_type
*os
;
585 os
= lang_output_section_find (tree
->name
.name
);
586 check (os
, tree
->name
.name
, "LOADADDR");
587 if (os
->load_base
== NULL
)
588 result
= new_rel (0, NULL
, os
);
590 result
= exp_fold_tree_no_dot (os
->load_base
,
599 if (allocation_done
!= lang_first_phase_enum
)
601 int opb
= bfd_octets_per_byte (output_bfd
);
602 lang_output_section_statement_type
*os
;
604 os
= lang_output_section_find (tree
->name
.name
);
605 check (os
, tree
->name
.name
, "SIZEOF");
606 result
= new_abs (os
->bfd_section
->_raw_size
/ opb
);
621 exp_fold_tree (etree_type
*tree
,
622 lang_output_section_statement_type
*current_section
,
623 lang_phase_type allocation_done
,
627 etree_value_type result
;
631 result
.valid_p
= FALSE
;
635 switch (tree
->type
.node_class
)
638 result
= new_rel (tree
->value
.value
, tree
->value
.str
, current_section
);
642 if (allocation_done
!= lang_final_phase_enum
)
643 result
.valid_p
= FALSE
;
645 result
= new_rel ((tree
->rel
.value
646 + tree
->rel
.section
->output_section
->vma
647 + tree
->rel
.section
->output_offset
),
653 result
= exp_fold_tree (tree
->assert_s
.child
,
655 allocation_done
, dot
, dotp
);
659 einfo ("%F%P: %s\n", tree
->assert_s
.message
);
665 result
= fold_unary (tree
, current_section
, allocation_done
,
670 result
= fold_binary (tree
, current_section
, allocation_done
,
675 result
= fold_trinary (tree
, current_section
, allocation_done
,
682 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
684 /* Assignment to dot can only be done during allocation. */
685 if (tree
->type
.node_class
!= etree_assign
)
686 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
687 if (allocation_done
== lang_allocating_phase_enum
688 || (allocation_done
== lang_final_phase_enum
689 && current_section
== abs_output_section
))
691 result
= exp_fold_tree (tree
->assign
.src
,
693 allocation_done
, dot
,
695 if (! result
.valid_p
)
696 einfo (_("%F%S invalid assignment to location counter\n"));
699 if (current_section
== NULL
)
700 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
705 nextdot
= (result
.value
706 + current_section
->bfd_section
->vma
);
708 && current_section
!= abs_output_section
)
709 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
719 result
= exp_fold_tree (tree
->assign
.src
,
720 current_section
, allocation_done
,
725 struct bfd_link_hash_entry
*h
;
727 if (tree
->type
.node_class
== etree_assign
)
731 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
732 create
, FALSE
, FALSE
);
735 if (tree
->type
.node_class
== etree_assign
)
736 einfo (_("%P%F:%s: hash creation failed\n"),
739 else if (tree
->type
.node_class
== etree_provide
740 && h
->type
!= bfd_link_hash_undefined
741 && h
->type
!= bfd_link_hash_common
)
743 /* Do nothing. The symbol was defined by some
748 /* FIXME: Should we worry if the symbol is already
750 lang_update_definedness (tree
->assign
.dst
, h
);
751 h
->type
= bfd_link_hash_defined
;
752 h
->u
.def
.value
= result
.value
;
753 h
->u
.def
.section
= result
.section
->bfd_section
;
754 if (tree
->type
.node_class
== etree_provide
)
755 tree
->type
.node_class
= etree_provided
;
762 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
773 static etree_value_type
774 exp_fold_tree_no_dot (etree_type
*tree
,
775 lang_output_section_statement_type
*current_section
,
776 lang_phase_type allocation_done
)
778 return exp_fold_tree (tree
, current_section
, allocation_done
, 0, NULL
);
782 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
784 etree_type value
, *new;
787 value
.type
.node_code
= code
;
788 value
.binary
.lhs
= lhs
;
789 value
.binary
.rhs
= rhs
;
790 value
.type
.node_class
= etree_binary
;
791 r
= exp_fold_tree_no_dot (&value
,
793 lang_first_phase_enum
);
796 return exp_intop (r
.value
);
798 new = stat_alloc (sizeof (new->binary
));
799 memcpy (new, &value
, sizeof (new->binary
));
804 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
806 etree_type value
, *new;
808 value
.type
.node_code
= code
;
809 value
.trinary
.lhs
= lhs
;
810 value
.trinary
.cond
= cond
;
811 value
.trinary
.rhs
= rhs
;
812 value
.type
.node_class
= etree_trinary
;
813 r
= exp_fold_tree_no_dot (&value
, NULL
, lang_first_phase_enum
);
815 return exp_intop (r
.value
);
817 new = stat_alloc (sizeof (new->trinary
));
818 memcpy (new, &value
, sizeof (new->trinary
));
823 exp_unop (int code
, etree_type
*child
)
825 etree_type value
, *new;
828 value
.unary
.type
.node_code
= code
;
829 value
.unary
.child
= child
;
830 value
.unary
.type
.node_class
= etree_unary
;
831 r
= exp_fold_tree_no_dot (&value
, abs_output_section
,
832 lang_first_phase_enum
);
834 return exp_intop (r
.value
);
836 new = stat_alloc (sizeof (new->unary
));
837 memcpy (new, &value
, sizeof (new->unary
));
842 exp_nameop (int code
, const char *name
)
844 etree_type value
, *new;
846 value
.name
.type
.node_code
= code
;
847 value
.name
.name
= name
;
848 value
.name
.type
.node_class
= etree_name
;
850 r
= exp_fold_tree_no_dot (&value
, NULL
, lang_first_phase_enum
);
852 return exp_intop (r
.value
);
854 new = stat_alloc (sizeof (new->name
));
855 memcpy (new, &value
, sizeof (new->name
));
861 exp_assop (int code
, const char *dst
, etree_type
*src
)
863 etree_type value
, *new;
865 value
.assign
.type
.node_code
= code
;
867 value
.assign
.src
= src
;
868 value
.assign
.dst
= dst
;
869 value
.assign
.type
.node_class
= etree_assign
;
872 if (exp_fold_tree_no_dot (&value
, &result
))
873 return exp_intop (result
);
875 new = stat_alloc (sizeof (new->assign
));
876 memcpy (new, &value
, sizeof (new->assign
));
880 /* Handle PROVIDE. */
883 exp_provide (const char *dst
, etree_type
*src
)
887 n
= stat_alloc (sizeof (n
->assign
));
888 n
->assign
.type
.node_code
= '=';
889 n
->assign
.type
.node_class
= etree_provide
;
898 exp_assert (etree_type
*exp
, const char *message
)
902 n
= stat_alloc (sizeof (n
->assert_s
));
903 n
->assert_s
.type
.node_code
= '!';
904 n
->assert_s
.type
.node_class
= etree_assert
;
905 n
->assert_s
.child
= exp
;
906 n
->assert_s
.message
= message
;
911 exp_print_tree (etree_type
*tree
)
913 if (config
.map_file
== NULL
)
914 config
.map_file
= stderr
;
918 minfo ("NULL TREE\n");
922 switch (tree
->type
.node_class
)
925 minfo ("0x%v", tree
->value
.value
);
928 if (tree
->rel
.section
->owner
!= NULL
)
929 minfo ("%B:", tree
->rel
.section
->owner
);
930 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
934 if (tree
->assign
.dst
->sdefs
!= NULL
)
935 fprintf (config
.map_file
, "%s (%x) ", tree
->assign
.dst
->name
,
936 tree
->assign
.dst
->sdefs
->value
);
938 fprintf (config
.map_file
, "%s (UNDEFINED)", tree
->assign
.dst
->name
);
940 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
941 exp_print_token (tree
->type
.node_code
, TRUE
);
942 exp_print_tree (tree
->assign
.src
);
946 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
947 exp_print_tree (tree
->assign
.src
);
948 fprintf (config
.map_file
, ")");
951 fprintf (config
.map_file
, "(");
952 exp_print_tree (tree
->binary
.lhs
);
953 exp_print_token (tree
->type
.node_code
, TRUE
);
954 exp_print_tree (tree
->binary
.rhs
);
955 fprintf (config
.map_file
, ")");
958 exp_print_tree (tree
->trinary
.cond
);
959 fprintf (config
.map_file
, "?");
960 exp_print_tree (tree
->trinary
.lhs
);
961 fprintf (config
.map_file
, ":");
962 exp_print_tree (tree
->trinary
.rhs
);
965 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
966 if (tree
->unary
.child
)
968 fprintf (config
.map_file
, " (");
969 exp_print_tree (tree
->unary
.child
);
970 fprintf (config
.map_file
, ")");
975 fprintf (config
.map_file
, "ASSERT (");
976 exp_print_tree (tree
->assert_s
.child
);
977 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
981 fprintf (config
.map_file
, "????????");
984 if (tree
->type
.node_code
== NAME
)
986 fprintf (config
.map_file
, "%s", tree
->name
.name
);
990 exp_print_token (tree
->type
.node_code
, FALSE
);
992 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1002 exp_get_vma (etree_type
*tree
,
1005 lang_phase_type allocation_done
)
1011 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1012 if (! r
.valid_p
&& name
!= NULL
)
1013 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1021 exp_get_value_int (etree_type
*tree
,
1024 lang_phase_type allocation_done
)
1026 return exp_get_vma (tree
, def
, name
, allocation_done
);
1030 exp_get_fill (etree_type
*tree
,
1033 lang_phase_type allocation_done
)
1043 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1044 if (! r
.valid_p
&& name
!= NULL
)
1045 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1047 if (r
.str
!= NULL
&& (len
= strlen (r
.str
)) != 0)
1051 fill
= xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1052 fill
->size
= (len
+ 1) / 2;
1062 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1076 fill
= xmalloc (4 + sizeof (*fill
) - 1);
1078 fill
->data
[0] = (val
>> 24) & 0xff;
1079 fill
->data
[1] = (val
>> 16) & 0xff;
1080 fill
->data
[2] = (val
>> 8) & 0xff;
1081 fill
->data
[3] = (val
>> 0) & 0xff;
1088 exp_get_abs_int (etree_type
*tree
,
1089 int def ATTRIBUTE_UNUSED
,
1091 lang_phase_type allocation_done
)
1093 etree_value_type res
;
1094 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1097 res
.value
+= res
.section
->bfd_section
->vma
;
1099 einfo (_("%F%S non constant expression for %s\n"), name
);
1105 align_n (bfd_vma value
, bfd_vma align
)
1110 value
= (value
+ align
- 1) / align
;
1111 return value
* align
;