1 /* This module handles expression trees.
2 Copyright (C) 1991, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support (sac@cygnus.com).
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 This module is in charge of working out the contents of expressions.
24 It has to keep track of the relative/absness of a symbol etc. This is
25 done by keeping all values in a struct (an etree_value_type) which
26 contains a value, a section to which it is relative and a valid bit.
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
)
77 { REL
, "relocateable" },
97 { SECTIONS
,"SECTIONS" },
98 { SIZEOF_HEADERS
,"SIZEOF_HEADERS" },
102 { LOADADDR
,"LOADADDR" },
104 { DEFINED
,"DEFINED" },
105 { TARGET_K
,"TARGET" },
106 { SEARCH_DIR
,"SEARCH_DIR" },
117 for (idx
= 0; table
[idx
].name
!= (char*)NULL
; idx
++) {
118 if (table
[idx
].code
== code
) {
119 fprintf(config
.map_file
, "%s", table
[idx
].name
);
123 /* Not in table, just print it alone */
124 fprintf(config
.map_file
, "%c",code
);
129 etree_value_type
*ptr
;
131 asection
*s
= ptr
->section
->bfd_section
;
132 ptr
->value
+= s
->vma
;
133 ptr
->section
= abs_output_section
;
136 static etree_value_type
140 etree_value_type
new;
142 new.section
= abs_output_section
;
149 lang_output_section_statement_type
*os
;
154 einfo ("%F%P: %s uses undefined section %s\n", op
, name
);
156 einfo ("%F%P: %s forward reference of section %s\n", op
, name
);
163 etree_type
*new = (etree_type
*) stat_alloc(sizeof(new->value
));
164 new->type
.node_code
= INT
;
165 new->value
.value
= value
;
166 new->type
.node_class
= etree_value
;
171 /* Build an expression representing an unnamed relocateable value. */
174 exp_relop (section
, value
)
178 etree_type
*new = (etree_type
*) stat_alloc (sizeof (new->rel
));
179 new->type
.node_code
= REL
;
180 new->type
.node_class
= etree_rel
;
181 new->rel
.section
= section
;
182 new->rel
.value
= value
;
186 static etree_value_type
187 new_rel (value
, section
)
189 lang_output_section_statement_type
*section
;
191 etree_value_type
new;
194 new.section
= section
;
198 static etree_value_type
199 new_rel_from_section (value
, section
)
201 lang_output_section_statement_type
*section
;
203 etree_value_type
new;
206 new.section
= section
;
208 new.value
-= section
->bfd_section
->vma
;
213 static etree_value_type
214 fold_binary (tree
, current_section
, allocation_done
, dot
, dotp
)
216 lang_output_section_statement_type
*current_section
;
217 lang_phase_type allocation_done
;
221 etree_value_type result
;
223 result
= exp_fold_tree (tree
->binary
.lhs
, current_section
,
224 allocation_done
, dot
, dotp
);
227 etree_value_type other
;
229 other
= exp_fold_tree (tree
->binary
.rhs
,
231 allocation_done
, dot
,dotp
) ;
234 /* If the values are from different sections, or this is an
235 absolute expression, make both the source arguments
236 absolute. However, adding or subtracting an absolute
237 value from a relative value is meaningful, and is an
239 if (current_section
!= abs_output_section
240 && (other
.section
== abs_output_section
241 || (result
.section
== abs_output_section
242 && tree
->type
.node_code
== '+'))
243 && (tree
->type
.node_code
== '+'
244 || tree
->type
.node_code
== '-'))
246 etree_value_type hold
;
248 /* If there is only one absolute term, make sure it is the
250 if (other
.section
!= abs_output_section
)
257 else if (result
.section
!= other
.section
258 || current_section
== abs_output_section
)
264 switch (tree
->type
.node_code
)
267 if (other
.value
== 0)
268 einfo ("%F%S %% by zero\n");
269 result
.value
= ((bfd_signed_vma
) result
.value
270 % (bfd_signed_vma
) other
.value
);
274 if (other
.value
== 0)
275 einfo ("%F%S / by zero\n");
276 result
.value
= ((bfd_signed_vma
) result
.value
277 / (bfd_signed_vma
) other
.value
);
280 #define BOP(x,y) case x : result.value = result.value y other.value; break;
304 result
.valid
= false;
314 etree_value_type
new;
319 static etree_value_type
320 fold_name (tree
, current_section
, allocation_done
, dot
)
322 lang_output_section_statement_type
*current_section
;
323 lang_phase_type allocation_done
;
326 etree_value_type result
;
327 switch (tree
->type
.node_code
)
330 if (allocation_done
!= lang_first_phase_enum
)
332 result
= new_abs ((bfd_vma
)
333 bfd_sizeof_headers (output_bfd
,
334 link_info
.relocateable
));
338 result
.valid
= false;
342 if (allocation_done
== lang_first_phase_enum
)
343 result
.valid
= false;
346 struct bfd_link_hash_entry
*h
;
348 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
351 result
.value
= (h
!= (struct bfd_link_hash_entry
*) NULL
352 && (h
->type
== bfd_link_hash_defined
353 || h
->type
== bfd_link_hash_defweak
354 || h
->type
== bfd_link_hash_common
));
360 result
.valid
= false;
361 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
363 if (allocation_done
!= lang_first_phase_enum
)
364 result
= new_rel_from_section(dot
, current_section
);
368 else if (allocation_done
!= lang_first_phase_enum
)
370 struct bfd_link_hash_entry
*h
;
372 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
376 && (h
->type
== bfd_link_hash_defined
377 || h
->type
== bfd_link_hash_defweak
))
379 if (bfd_is_abs_section (h
->u
.def
.section
))
380 result
= new_abs (h
->u
.def
.value
);
381 else if (allocation_done
== lang_final_phase_enum
382 || allocation_done
== lang_allocating_phase_enum
)
384 lang_output_section_statement_type
*os
;
386 os
= (lang_output_section_statement_lookup
387 (h
->u
.def
.section
->output_section
->name
));
389 /* FIXME: Is this correct if this section is being
391 result
= new_rel ((h
->u
.def
.value
392 + h
->u
.def
.section
->output_offset
),
396 else if (allocation_done
== lang_final_phase_enum
)
397 einfo ("%F%S: undefined symbol `%s' referenced in expression\n",
403 if (allocation_done
!= lang_first_phase_enum
)
405 lang_output_section_statement_type
*os
;
407 os
= lang_output_section_find (tree
->name
.name
);
408 check (os
, tree
->name
.name
, "ADDR");
409 result
= new_rel (0, os
);
416 if (allocation_done
!= lang_first_phase_enum
)
418 lang_output_section_statement_type
*os
;
420 os
= lang_output_section_find (tree
->name
.name
);
421 check (os
, tree
->name
.name
, "LOADADDR");
422 if (os
->load_base
== NULL
)
423 result
= new_rel (0, os
);
425 result
= exp_fold_tree_no_dot (os
->load_base
,
434 if (allocation_done
!= lang_first_phase_enum
)
436 lang_output_section_statement_type
*os
;
438 os
= lang_output_section_find (tree
->name
.name
);
439 check (os
, tree
->name
.name
, "SIZEOF");
440 result
= new_abs (os
->bfd_section
->_raw_size
);
454 exp_fold_tree (tree
, current_section
, allocation_done
, dot
, dotp
)
456 lang_output_section_statement_type
*current_section
;
457 lang_phase_type allocation_done
;
461 etree_value_type result
;
465 result
.valid
= false;
469 switch (tree
->type
.node_class
)
472 result
= new_rel (tree
->value
.value
, current_section
);
476 if (allocation_done
!= lang_final_phase_enum
)
477 result
.valid
= false;
479 result
= new_rel ((tree
->rel
.value
480 + tree
->rel
.section
->output_section
->vma
481 + tree
->rel
.section
->output_offset
),
486 result
= exp_fold_tree (tree
->unary
.child
,
488 allocation_done
, dot
, dotp
);
491 switch (tree
->type
.node_code
)
494 if (allocation_done
!= lang_first_phase_enum
)
495 result
= new_rel_from_section (ALIGN_N (dot
, result
.value
),
498 result
.valid
= false;
502 if (allocation_done
!= lang_first_phase_enum
&& result
.valid
)
504 result
.value
+= result
.section
->bfd_section
->vma
;
505 result
.section
= abs_output_section
;
508 result
.valid
= false;
513 result
.value
= ~result
.value
;
518 result
.value
= !result
.value
;
523 result
.value
= -result
.value
;
527 /* Return next place aligned to value. */
528 if (allocation_done
== lang_allocating_phase_enum
)
531 result
.value
= ALIGN_N (dot
, result
.value
);
534 result
.valid
= false;
545 result
= exp_fold_tree (tree
->trinary
.cond
, current_section
,
546 allocation_done
, dot
, dotp
);
548 result
= exp_fold_tree ((result
.value
550 : tree
->trinary
.rhs
),
552 allocation_done
, dot
, dotp
);
556 result
= fold_binary (tree
, current_section
, allocation_done
,
562 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
564 /* Assignment to dot can only be done during allocation */
565 if (tree
->type
.node_class
== etree_provide
)
566 einfo ("%F%S can not PROVIDE assignment to location counter\n");
567 if (allocation_done
== lang_allocating_phase_enum
568 || (allocation_done
== lang_final_phase_enum
569 && current_section
== abs_output_section
))
571 result
= exp_fold_tree (tree
->assign
.src
,
573 lang_allocating_phase_enum
, dot
,
576 einfo ("%F%S invalid assignment to location counter\n");
579 if (current_section
== NULL
)
580 einfo ("%F%S assignment to location counter invalid outside of SECTION\n");
585 nextdot
= (result
.value
586 + current_section
->bfd_section
->vma
);
588 && current_section
!= abs_output_section
)
590 einfo ("%F%S cannot move location counter backwards (from %V to %V)\n",
601 result
= exp_fold_tree (tree
->assign
.src
,
602 current_section
, allocation_done
,
607 struct bfd_link_hash_entry
*h
;
609 if (tree
->type
.node_class
== etree_assign
)
613 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
614 create
, false, false);
615 if (h
== (struct bfd_link_hash_entry
*) NULL
)
617 if (tree
->type
.node_class
== etree_assign
)
618 einfo ("%P%F:%s: hash creation failed\n",
621 else if (tree
->type
.node_class
== etree_provide
622 && h
->type
!= bfd_link_hash_undefined
623 && h
->type
!= bfd_link_hash_common
)
625 /* Do nothing. The symbol was defined by some
630 /* FIXME: Should we worry if the symbol is already
632 h
->type
= bfd_link_hash_defined
;
633 h
->u
.def
.value
= result
.value
;
634 h
->u
.def
.section
= result
.section
->bfd_section
;
641 result
= fold_name (tree
, current_section
, allocation_done
, dot
);
652 static etree_value_type
653 exp_fold_tree_no_dot (tree
, current_section
, allocation_done
)
655 lang_output_section_statement_type
*current_section
;
656 lang_phase_type allocation_done
;
658 return exp_fold_tree(tree
, current_section
, allocation_done
, (bfd_vma
)
663 exp_binop (code
, lhs
, rhs
)
668 etree_type value
, *new;
671 value
.type
.node_code
= code
;
672 value
.binary
.lhs
= lhs
;
673 value
.binary
.rhs
= rhs
;
674 value
.type
.node_class
= etree_binary
;
675 r
= exp_fold_tree_no_dot(&value
,
677 lang_first_phase_enum
);
680 return exp_intop(r
.value
);
682 new = (etree_type
*) stat_alloc (sizeof (new->binary
));
683 memcpy((char *)new, (char *)&value
, sizeof(new->binary
));
688 exp_trinop (code
, cond
, lhs
, rhs
)
694 etree_type value
, *new;
696 value
.type
.node_code
= code
;
697 value
.trinary
.lhs
= lhs
;
698 value
.trinary
.cond
= cond
;
699 value
.trinary
.rhs
= rhs
;
700 value
.type
.node_class
= etree_trinary
;
701 r
= exp_fold_tree_no_dot(&value
, (lang_output_section_statement_type
702 *)NULL
,lang_first_phase_enum
);
704 return exp_intop(r
.value
);
706 new = (etree_type
*) stat_alloc (sizeof (new->trinary
));
707 memcpy((char *)new,(char *) &value
, sizeof(new->trinary
));
713 exp_unop (code
, child
)
717 etree_type value
, *new;
720 value
.unary
.type
.node_code
= code
;
721 value
.unary
.child
= child
;
722 value
.unary
.type
.node_class
= etree_unary
;
723 r
= exp_fold_tree_no_dot(&value
,abs_output_section
,
724 lang_first_phase_enum
);
726 return exp_intop(r
.value
);
728 new = (etree_type
*) stat_alloc (sizeof (new->unary
));
729 memcpy((char *)new, (char *)&value
, sizeof(new->unary
));
735 exp_nameop (code
, name
)
739 etree_type value
, *new;
741 value
.name
.type
.node_code
= code
;
742 value
.name
.name
= name
;
743 value
.name
.type
.node_class
= etree_name
;
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->name
));
753 memcpy((char *)new, (char *)&value
, sizeof(new->name
));
762 exp_assop (code
, dst
, src
)
767 etree_type value
, *new;
769 value
.assign
.type
.node_code
= code
;
772 value
.assign
.src
= src
;
773 value
.assign
.dst
= dst
;
774 value
.assign
.type
.node_class
= etree_assign
;
777 if (exp_fold_tree_no_dot(&value
, &result
)) {
778 return exp_intop(result
);
781 new = (etree_type
*) stat_alloc (sizeof (new->assign
));
782 memcpy((char *)new, (char *)&value
, sizeof(new->assign
));
786 /* Handle PROVIDE. */
789 exp_provide (dst
, src
)
795 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
796 n
->assign
.type
.node_code
= '=';
797 n
->assign
.type
.node_class
= etree_provide
;
804 exp_print_tree (tree
)
807 switch (tree
->type
.node_class
) {
809 minfo ("0x%v", tree
->value
.value
);
812 if (tree
->rel
.section
->owner
!= NULL
)
813 minfo ("%B:", tree
->rel
.section
->owner
);
814 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
818 if (tree
->assign
.dst
->sdefs
!= (asymbol
*)NULL
){
819 fprintf(config
.map_file
,"%s (%x) ",tree
->assign
.dst
->name
,
820 tree
->assign
.dst
->sdefs
->value
);
823 fprintf(config
.map_file
,"%s (UNDEFINED)",tree
->assign
.dst
->name
);
826 fprintf(config
.map_file
,"%s",tree
->assign
.dst
);
827 exp_print_token(tree
->type
.node_code
);
828 exp_print_tree(tree
->assign
.src
);
831 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
832 exp_print_tree (tree
->assign
.src
);
833 fprintf (config
.map_file
, ")");
836 fprintf(config
.map_file
,"(");
837 exp_print_tree(tree
->binary
.lhs
);
838 exp_print_token(tree
->type
.node_code
);
839 exp_print_tree(tree
->binary
.rhs
);
840 fprintf(config
.map_file
,")");
843 exp_print_tree(tree
->trinary
.cond
);
844 fprintf(config
.map_file
,"?");
845 exp_print_tree(tree
->trinary
.lhs
);
846 fprintf(config
.map_file
,":");
847 exp_print_tree(tree
->trinary
.rhs
);
850 exp_print_token(tree
->unary
.type
.node_code
);
851 if (tree
->unary
.child
)
854 fprintf(config
.map_file
,"(");
855 exp_print_tree(tree
->unary
.child
);
856 fprintf(config
.map_file
,")");
861 fprintf(config
.map_file
,"????????");
864 if (tree
->type
.node_code
== NAME
) {
865 fprintf(config
.map_file
,"%s", tree
->name
.name
);
868 exp_print_token(tree
->type
.node_code
);
870 fprintf(config
.map_file
,"(%s)", tree
->name
.name
);
880 exp_get_vma (tree
, def
, name
, allocation_done
)
884 lang_phase_type allocation_done
;
890 r
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
891 if (! r
.valid
&& name
!= NULL
)
892 einfo ("%F%S nonconstant expression for %s\n", name
);
900 exp_get_value_int (tree
,def
,name
, allocation_done
)
904 lang_phase_type allocation_done
;
906 return (int)exp_get_vma(tree
,(bfd_vma
)def
,name
, allocation_done
);
911 exp_get_abs_int (tree
, def
, name
, allocation_done
)
915 lang_phase_type allocation_done
;
917 etree_value_type res
;
918 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
922 res
.value
+= res
.section
->bfd_section
->vma
;
925 einfo ("%F%S non constant expression for %s\n",name
);