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
);
47 struct exp_data_seg exp_data_seg
;
49 /* Print the string representation of the given token. Surround it
50 with spaces if INFIX_P is TRUE. */
53 exp_print_token (token_code_type code
, int infix_p
)
87 { SECTIONS
, "SECTIONS" },
88 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
90 { DEFINED
, "DEFINED" },
91 { TARGET_K
, "TARGET" },
92 { SEARCH_DIR
, "SEARCH_DIR" },
98 { LOADADDR
, "LOADADDR" },
100 { REL
, "relocatable" },
101 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
102 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" }
106 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
107 if (table
[idx
].code
== code
)
111 fputc (' ', config
.map_file
);
113 if (idx
< ARRAY_SIZE (table
))
114 fputs (table
[idx
].name
, config
.map_file
);
116 fputc (code
, config
.map_file
);
118 fprintf (config
.map_file
, "<code %d>", code
);
121 fputc (' ', config
.map_file
);
125 make_abs (etree_value_type
*ptr
)
127 asection
*s
= ptr
->section
->bfd_section
;
128 ptr
->value
+= s
->vma
;
129 ptr
->section
= abs_output_section
;
132 static etree_value_type
133 new_abs (bfd_vma value
)
135 etree_value_type
new;
137 new.section
= abs_output_section
;
143 check (lang_output_section_statement_type
*os
,
148 einfo (_("%F%P: %s uses undefined section %s\n"), op
, name
);
150 einfo (_("%F%P: %s forward reference of section %s\n"), op
, name
);
154 exp_intop (bfd_vma value
)
156 etree_type
*new = stat_alloc (sizeof (new->value
));
157 new->type
.node_code
= INT
;
158 new->value
.value
= value
;
159 new->value
.str
= NULL
;
160 new->type
.node_class
= etree_value
;
165 exp_bigintop (bfd_vma value
, char *str
)
167 etree_type
*new = stat_alloc (sizeof (new->value
));
168 new->type
.node_code
= INT
;
169 new->value
.value
= value
;
170 new->value
.str
= str
;
171 new->type
.node_class
= etree_value
;
175 /* Build an expression representing an unnamed relocatable value. */
178 exp_relop (asection
*section
, bfd_vma value
)
180 etree_type
*new = stat_alloc (sizeof (new->rel
));
181 new->type
.node_code
= REL
;
182 new->type
.node_class
= etree_rel
;
183 new->rel
.section
= section
;
184 new->rel
.value
= value
;
188 static etree_value_type
189 new_rel (bfd_vma value
,
191 lang_output_section_statement_type
*section
)
193 etree_value_type
new;
197 new.section
= section
;
201 static etree_value_type
202 new_rel_from_section (bfd_vma value
,
203 lang_output_section_statement_type
*section
)
205 etree_value_type
new;
209 new.section
= section
;
211 new.value
-= section
->bfd_section
->vma
;
216 static etree_value_type
217 fold_unary (etree_type
*tree
,
218 lang_output_section_statement_type
*current_section
,
219 lang_phase_type allocation_done
,
223 etree_value_type result
;
225 result
= exp_fold_tree (tree
->unary
.child
,
227 allocation_done
, dot
, dotp
);
230 switch (tree
->type
.node_code
)
233 if (allocation_done
!= lang_first_phase_enum
)
234 result
= new_rel_from_section (align_n (dot
, result
.value
),
237 result
.valid_p
= FALSE
;
241 if (allocation_done
!= lang_first_phase_enum
)
243 result
.value
+= result
.section
->bfd_section
->vma
;
244 result
.section
= abs_output_section
;
247 result
.valid_p
= FALSE
;
252 result
.value
= ~result
.value
;
257 result
.value
= !result
.value
;
262 result
.value
= -result
.value
;
266 /* Return next place aligned to value. */
267 if (allocation_done
== lang_allocating_phase_enum
)
270 result
.value
= align_n (dot
, result
.value
);
273 result
.valid_p
= FALSE
;
276 case DATA_SEGMENT_END
:
277 if (allocation_done
!= lang_first_phase_enum
278 && current_section
== abs_output_section
279 && (exp_data_seg
.phase
== exp_dataseg_align_seen
280 || exp_data_seg
.phase
== exp_dataseg_adjust
281 || allocation_done
!= lang_allocating_phase_enum
))
283 if (exp_data_seg
.phase
== exp_dataseg_align_seen
)
285 exp_data_seg
.phase
= exp_dataseg_end_seen
;
286 exp_data_seg
.end
= result
.value
;
290 result
.valid_p
= FALSE
;
302 static etree_value_type
303 fold_binary (etree_type
*tree
,
304 lang_output_section_statement_type
*current_section
,
305 lang_phase_type allocation_done
,
309 etree_value_type result
;
311 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
312 allocation_done
, dot
, dotp
);
315 etree_value_type other
;
317 other
= exp_fold_tree (tree
->binary
.rhs
,
319 allocation_done
, dot
, dotp
);
322 /* If the values are from different sections, or this is an
323 absolute expression, make both the source arguments
324 absolute. However, adding or subtracting an absolute
325 value from a relative value is meaningful, and is an
327 if (current_section
!= abs_output_section
328 && (other
.section
== abs_output_section
329 || (result
.section
== abs_output_section
330 && tree
->type
.node_code
== '+'))
331 && (tree
->type
.node_code
== '+'
332 || tree
->type
.node_code
== '-'))
334 if (other
.section
!= abs_output_section
)
336 /* Keep the section of the other term. */
337 if (tree
->type
.node_code
== '+')
338 other
.value
= result
.value
+ other
.value
;
340 other
.value
= result
.value
- other
.value
;
344 else if (result
.section
!= other
.section
345 || current_section
== abs_output_section
)
351 switch (tree
->type
.node_code
)
354 if (other
.value
== 0)
355 einfo (_("%F%S %% by zero\n"));
356 result
.value
= ((bfd_signed_vma
) result
.value
357 % (bfd_signed_vma
) other
.value
);
361 if (other
.value
== 0)
362 einfo (_("%F%S / by zero\n"));
363 result
.value
= ((bfd_signed_vma
) result
.value
364 / (bfd_signed_vma
) other
.value
);
367 #define BOP(x,y) case x : result.value = result.value y other.value; break;
386 if (result
.value
< other
.value
)
391 if (result
.value
> other
.value
)
395 case DATA_SEGMENT_ALIGN
:
396 if (allocation_done
!= lang_first_phase_enum
397 && current_section
== abs_output_section
398 && (exp_data_seg
.phase
== exp_dataseg_none
399 || exp_data_seg
.phase
== exp_dataseg_adjust
400 || allocation_done
!= lang_allocating_phase_enum
))
402 bfd_vma maxpage
= result
.value
;
404 result
.value
= align_n (dot
, maxpage
);
405 if (exp_data_seg
.phase
!= exp_dataseg_adjust
)
407 result
.value
+= dot
& (maxpage
- 1);
408 if (allocation_done
== lang_allocating_phase_enum
)
410 exp_data_seg
.phase
= exp_dataseg_align_seen
;
411 exp_data_seg
.base
= result
.value
;
412 exp_data_seg
.pagesize
= other
.value
;
415 else if (other
.value
< maxpage
)
416 result
.value
+= (dot
+ other
.value
- 1)
417 & (maxpage
- other
.value
);
420 result
.valid_p
= FALSE
;
429 result
.valid_p
= FALSE
;
436 static etree_value_type
437 fold_trinary (etree_type
*tree
,
438 lang_output_section_statement_type
*current_section
,
439 lang_phase_type allocation_done
,
443 etree_value_type result
;
445 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
446 allocation_done
, dot
, dotp
);
448 result
= exp_fold_tree ((result
.value
450 : tree
->trinary
.rhs
),
452 allocation_done
, dot
, dotp
);
460 etree_value_type
new;
465 static etree_value_type
466 fold_name (etree_type
*tree
,
467 lang_output_section_statement_type
*current_section
,
468 lang_phase_type allocation_done
,
471 etree_value_type result
;
473 switch (tree
->type
.node_code
)
476 if (allocation_done
!= lang_first_phase_enum
)
478 result
= new_abs (bfd_sizeof_headers (output_bfd
,
479 link_info
.relocatable
));
483 result
.valid_p
= FALSE
;
487 if (allocation_done
== lang_first_phase_enum
)
489 lang_track_definedness (tree
->name
.name
);
490 result
.valid_p
= FALSE
;
494 struct bfd_link_hash_entry
*h
;
496 = lang_symbol_definition_iteration (tree
->name
.name
);
498 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
501 result
.value
= (h
!= NULL
502 && (h
->type
== bfd_link_hash_defined
503 || h
->type
== bfd_link_hash_defweak
504 || h
->type
== bfd_link_hash_common
)
505 && (def_iteration
== lang_statement_iteration
506 || def_iteration
== -1));
507 result
.section
= abs_output_section
;
508 result
.valid_p
= TRUE
;
512 result
.valid_p
= FALSE
;
513 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
515 if (allocation_done
!= lang_first_phase_enum
)
516 result
= new_rel_from_section (dot
, current_section
);
520 else if (allocation_done
!= lang_first_phase_enum
)
522 struct bfd_link_hash_entry
*h
;
524 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
528 && (h
->type
== bfd_link_hash_defined
529 || h
->type
== bfd_link_hash_defweak
))
531 if (bfd_is_abs_section (h
->u
.def
.section
))
532 result
= new_abs (h
->u
.def
.value
);
533 else if (allocation_done
== lang_final_phase_enum
534 || allocation_done
== lang_allocating_phase_enum
)
536 asection
*output_section
;
538 output_section
= h
->u
.def
.section
->output_section
;
539 if (output_section
== NULL
)
540 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
544 lang_output_section_statement_type
*os
;
546 os
= (lang_output_section_statement_lookup
547 (bfd_get_section_name (output_bfd
,
550 /* FIXME: Is this correct if this section is
551 being linked with -R? */
552 result
= new_rel ((h
->u
.def
.value
553 + h
->u
.def
.section
->output_offset
),
559 else if (allocation_done
== lang_final_phase_enum
)
560 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
566 if (allocation_done
!= lang_first_phase_enum
)
568 lang_output_section_statement_type
*os
;
570 os
= lang_output_section_find (tree
->name
.name
);
571 check (os
, tree
->name
.name
, "ADDR");
572 result
= new_rel (0, NULL
, os
);
579 if (allocation_done
!= lang_first_phase_enum
)
581 lang_output_section_statement_type
*os
;
583 os
= lang_output_section_find (tree
->name
.name
);
584 check (os
, tree
->name
.name
, "LOADADDR");
585 if (os
->load_base
== NULL
)
586 result
= new_rel (0, NULL
, os
);
588 result
= exp_fold_tree_no_dot (os
->load_base
,
597 if (allocation_done
!= lang_first_phase_enum
)
599 int opb
= bfd_octets_per_byte (output_bfd
);
600 lang_output_section_statement_type
*os
;
602 os
= lang_output_section_find (tree
->name
.name
);
603 check (os
, tree
->name
.name
, "SIZEOF");
604 result
= new_abs (os
->bfd_section
->_raw_size
/ opb
);
619 exp_fold_tree (etree_type
*tree
,
620 lang_output_section_statement_type
*current_section
,
621 lang_phase_type allocation_done
,
625 etree_value_type result
;
629 result
.valid_p
= FALSE
;
633 switch (tree
->type
.node_class
)
636 result
= new_rel (tree
->value
.value
, tree
->value
.str
, current_section
);
640 if (allocation_done
!= lang_final_phase_enum
)
641 result
.valid_p
= FALSE
;
643 result
= new_rel ((tree
->rel
.value
644 + tree
->rel
.section
->output_section
->vma
645 + tree
->rel
.section
->output_offset
),
651 result
= exp_fold_tree (tree
->assert_s
.child
,
653 allocation_done
, dot
, dotp
);
657 einfo ("%F%P: %s\n", tree
->assert_s
.message
);
663 result
= fold_unary (tree
, current_section
, allocation_done
,
668 result
= fold_binary (tree
, current_section
, allocation_done
,
673 result
= fold_trinary (tree
, current_section
, allocation_done
,
680 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
682 /* Assignment to dot can only be done during allocation. */
683 if (tree
->type
.node_class
!= etree_assign
)
684 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
685 if (allocation_done
== lang_allocating_phase_enum
686 || (allocation_done
== lang_final_phase_enum
687 && current_section
== abs_output_section
))
689 result
= exp_fold_tree (tree
->assign
.src
,
691 allocation_done
, dot
,
693 if (! result
.valid_p
)
694 einfo (_("%F%S invalid assignment to location counter\n"));
697 if (current_section
== NULL
)
698 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
703 nextdot
= (result
.value
704 + current_section
->bfd_section
->vma
);
706 && current_section
!= abs_output_section
)
707 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
717 result
= exp_fold_tree (tree
->assign
.src
,
718 current_section
, allocation_done
,
723 struct bfd_link_hash_entry
*h
;
725 if (tree
->type
.node_class
== etree_assign
)
729 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
730 create
, FALSE
, FALSE
);
733 if (tree
->type
.node_class
== etree_assign
)
734 einfo (_("%P%F:%s: hash creation failed\n"),
737 else if (tree
->type
.node_class
== etree_provide
738 && h
->type
!= bfd_link_hash_undefined
739 && h
->type
!= bfd_link_hash_common
)
741 /* Do nothing. The symbol was defined by some
746 /* FIXME: Should we worry if the symbol is already
748 lang_update_definedness (tree
->assign
.dst
, h
);
749 h
->type
= bfd_link_hash_defined
;
750 h
->u
.def
.value
= result
.value
;
751 h
->u
.def
.section
= result
.section
->bfd_section
;
752 if (tree
->type
.node_class
== etree_provide
)
753 tree
->type
.node_class
= etree_provided
;
760 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
771 static etree_value_type
772 exp_fold_tree_no_dot (etree_type
*tree
,
773 lang_output_section_statement_type
*current_section
,
774 lang_phase_type allocation_done
)
776 return exp_fold_tree (tree
, current_section
, allocation_done
, 0, NULL
);
780 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
782 etree_type value
, *new;
785 value
.type
.node_code
= code
;
786 value
.binary
.lhs
= lhs
;
787 value
.binary
.rhs
= rhs
;
788 value
.type
.node_class
= etree_binary
;
789 r
= exp_fold_tree_no_dot (&value
,
791 lang_first_phase_enum
);
794 return exp_intop (r
.value
);
796 new = stat_alloc (sizeof (new->binary
));
797 memcpy (new, &value
, sizeof (new->binary
));
802 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
804 etree_type value
, *new;
806 value
.type
.node_code
= code
;
807 value
.trinary
.lhs
= lhs
;
808 value
.trinary
.cond
= cond
;
809 value
.trinary
.rhs
= rhs
;
810 value
.type
.node_class
= etree_trinary
;
811 r
= exp_fold_tree_no_dot (&value
, NULL
, lang_first_phase_enum
);
813 return exp_intop (r
.value
);
815 new = stat_alloc (sizeof (new->trinary
));
816 memcpy (new, &value
, sizeof (new->trinary
));
821 exp_unop (int code
, etree_type
*child
)
823 etree_type value
, *new;
826 value
.unary
.type
.node_code
= code
;
827 value
.unary
.child
= child
;
828 value
.unary
.type
.node_class
= etree_unary
;
829 r
= exp_fold_tree_no_dot (&value
, abs_output_section
,
830 lang_first_phase_enum
);
832 return exp_intop (r
.value
);
834 new = stat_alloc (sizeof (new->unary
));
835 memcpy (new, &value
, sizeof (new->unary
));
840 exp_nameop (int code
, const char *name
)
842 etree_type value
, *new;
844 value
.name
.type
.node_code
= code
;
845 value
.name
.name
= name
;
846 value
.name
.type
.node_class
= etree_name
;
848 r
= exp_fold_tree_no_dot (&value
, NULL
, lang_first_phase_enum
);
850 return exp_intop (r
.value
);
852 new = stat_alloc (sizeof (new->name
));
853 memcpy (new, &value
, sizeof (new->name
));
859 exp_assop (int code
, const char *dst
, etree_type
*src
)
861 etree_type value
, *new;
863 value
.assign
.type
.node_code
= code
;
865 value
.assign
.src
= src
;
866 value
.assign
.dst
= dst
;
867 value
.assign
.type
.node_class
= etree_assign
;
870 if (exp_fold_tree_no_dot (&value
, &result
))
871 return exp_intop (result
);
873 new = stat_alloc (sizeof (new->assign
));
874 memcpy (new, &value
, sizeof (new->assign
));
878 /* Handle PROVIDE. */
881 exp_provide (const char *dst
, etree_type
*src
)
885 n
= stat_alloc (sizeof (n
->assign
));
886 n
->assign
.type
.node_code
= '=';
887 n
->assign
.type
.node_class
= etree_provide
;
896 exp_assert (etree_type
*exp
, const char *message
)
900 n
= stat_alloc (sizeof (n
->assert_s
));
901 n
->assert_s
.type
.node_code
= '!';
902 n
->assert_s
.type
.node_class
= etree_assert
;
903 n
->assert_s
.child
= exp
;
904 n
->assert_s
.message
= message
;
909 exp_print_tree (etree_type
*tree
)
911 if (config
.map_file
== NULL
)
912 config
.map_file
= stderr
;
916 minfo ("NULL TREE\n");
920 switch (tree
->type
.node_class
)
923 minfo ("0x%v", tree
->value
.value
);
926 if (tree
->rel
.section
->owner
!= NULL
)
927 minfo ("%B:", tree
->rel
.section
->owner
);
928 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
932 if (tree
->assign
.dst
->sdefs
!= NULL
)
933 fprintf (config
.map_file
, "%s (%x) ", tree
->assign
.dst
->name
,
934 tree
->assign
.dst
->sdefs
->value
);
936 fprintf (config
.map_file
, "%s (UNDEFINED)", tree
->assign
.dst
->name
);
938 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
939 exp_print_token (tree
->type
.node_code
, TRUE
);
940 exp_print_tree (tree
->assign
.src
);
944 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
945 exp_print_tree (tree
->assign
.src
);
946 fprintf (config
.map_file
, ")");
949 fprintf (config
.map_file
, "(");
950 exp_print_tree (tree
->binary
.lhs
);
951 exp_print_token (tree
->type
.node_code
, TRUE
);
952 exp_print_tree (tree
->binary
.rhs
);
953 fprintf (config
.map_file
, ")");
956 exp_print_tree (tree
->trinary
.cond
);
957 fprintf (config
.map_file
, "?");
958 exp_print_tree (tree
->trinary
.lhs
);
959 fprintf (config
.map_file
, ":");
960 exp_print_tree (tree
->trinary
.rhs
);
963 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
964 if (tree
->unary
.child
)
966 fprintf (config
.map_file
, " (");
967 exp_print_tree (tree
->unary
.child
);
968 fprintf (config
.map_file
, ")");
973 fprintf (config
.map_file
, "ASSERT (");
974 exp_print_tree (tree
->assert_s
.child
);
975 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
979 fprintf (config
.map_file
, "????????");
982 if (tree
->type
.node_code
== NAME
)
984 fprintf (config
.map_file
, "%s", tree
->name
.name
);
988 exp_print_token (tree
->type
.node_code
, FALSE
);
990 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1000 exp_get_vma (etree_type
*tree
,
1003 lang_phase_type allocation_done
)
1009 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1010 if (! r
.valid_p
&& name
!= NULL
)
1011 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1019 exp_get_value_int (etree_type
*tree
,
1022 lang_phase_type allocation_done
)
1024 return exp_get_vma (tree
, def
, name
, allocation_done
);
1028 exp_get_fill (etree_type
*tree
,
1031 lang_phase_type allocation_done
)
1041 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1042 if (! r
.valid_p
&& name
!= NULL
)
1043 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1045 if (r
.str
!= NULL
&& (len
= strlen (r
.str
)) != 0)
1049 fill
= xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1050 fill
->size
= (len
+ 1) / 2;
1060 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1074 fill
= xmalloc (4 + sizeof (*fill
) - 1);
1076 fill
->data
[0] = (val
>> 24) & 0xff;
1077 fill
->data
[1] = (val
>> 16) & 0xff;
1078 fill
->data
[2] = (val
>> 8) & 0xff;
1079 fill
->data
[3] = (val
>> 0) & 0xff;
1086 exp_get_abs_int (etree_type
*tree
,
1087 int def ATTRIBUTE_UNUSED
,
1089 lang_phase_type allocation_done
)
1091 etree_value_type res
;
1092 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
1095 res
.value
+= res
.section
->bfd_section
->vma
;
1097 einfo (_("%F%S non constant expression for %s\n"), name
);
1102 bfd_vma
align_n (bfd_vma value
, bfd_vma align
)
1107 value
= (value
+ align
- 1) / align
;
1108 return value
* align
;