1 /* This module handles expression trees.
2 Copyright (C) 1991, 1993, 1994, 1995 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" },
103 { DEFINED
,"DEFINED" },
104 { TARGET_K
,"TARGET" },
105 { SEARCH_DIR
,"SEARCH_DIR" },
116 for (idx
= 0; table
[idx
].name
!= (char*)NULL
; idx
++) {
117 if (table
[idx
].code
== code
) {
118 fprintf(config
.map_file
, "%s", table
[idx
].name
);
122 /* Not in table, just print it alone */
123 fprintf(config
.map_file
, "%c",code
);
128 etree_value_type
*ptr
;
130 asection
*s
= ptr
->section
->bfd_section
;
131 ptr
->value
+= s
->vma
;
132 ptr
->section
= abs_output_section
;
135 static etree_value_type
139 etree_value_type
new;
141 new.section
= abs_output_section
;
148 lang_output_section_statement_type
*os
;
152 if (os
== (lang_output_section_statement_type
*)NULL
) {
153 einfo("%F%P: %s uses undefined section %s\n", op
, name
);
155 if (os
->processed
== false) {
156 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;
305 result
.valid
= false;
315 etree_value_type
new;
320 static etree_value_type
321 fold_name (tree
, current_section
, allocation_done
, dot
)
323 lang_output_section_statement_type
*current_section
;
324 lang_phase_type allocation_done
;
327 etree_value_type result
;
328 switch (tree
->type
.node_code
)
331 if (allocation_done
!= lang_first_phase_enum
)
333 result
= new_abs ((bfd_vma
)
334 bfd_sizeof_headers (output_bfd
,
335 link_info
.relocateable
));
339 result
.valid
= false;
343 if (allocation_done
== lang_first_phase_enum
)
344 result
.valid
= false;
347 struct bfd_link_hash_entry
*h
;
349 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
352 result
.value
= (h
!= (struct bfd_link_hash_entry
*) NULL
353 && (h
->type
== bfd_link_hash_defined
354 || h
->type
== bfd_link_hash_defweak
355 || h
->type
== bfd_link_hash_common
));
361 result
.valid
= false;
362 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
364 if (allocation_done
!= lang_first_phase_enum
)
365 result
= new_rel_from_section(dot
, current_section
);
369 else if (allocation_done
!= lang_first_phase_enum
)
371 struct bfd_link_hash_entry
*h
;
373 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
377 && (h
->type
== bfd_link_hash_defined
378 || h
->type
== bfd_link_hash_defweak
))
380 if (bfd_is_abs_section (h
->u
.def
.section
))
381 result
= new_abs (h
->u
.def
.value
);
382 else if (allocation_done
== lang_final_phase_enum
383 || allocation_done
== lang_allocating_phase_enum
)
385 lang_output_section_statement_type
*os
;
387 os
= (lang_output_section_statement_lookup
388 (h
->u
.def
.section
->output_section
->name
));
390 /* FIXME: Is this correct if this section is being
392 result
= new_rel ((h
->u
.def
.value
393 + h
->u
.def
.section
->output_offset
),
397 else if (allocation_done
== lang_final_phase_enum
)
398 einfo ("%F%S: undefined symbol `%s' referenced in expression\n",
405 if (allocation_done
!= lang_first_phase_enum
) {
406 lang_output_section_statement_type
*os
=
407 lang_output_section_find(tree
->name
.name
);
408 check(os
,tree
->name
.name
,"ADDR");
409 result
= new_rel((bfd_vma
)0, os
);
416 if(allocation_done
!= lang_first_phase_enum
) {
417 lang_output_section_statement_type
*os
=
418 lang_output_section_find(tree
->name
.name
);
419 check(os
,tree
->name
.name
,"SIZEOF");
420 result
= new_abs((bfd_vma
)(os
->bfd_section
->_raw_size
));
435 exp_fold_tree (tree
, current_section
, allocation_done
, dot
, dotp
)
437 lang_output_section_statement_type
*current_section
;
438 lang_phase_type allocation_done
;
442 etree_value_type result
;
444 if (tree
== (etree_type
*)NULL
) {
445 result
.valid
= false;
448 switch (tree
->type
.node_class
)
451 result
= new_rel(tree
->value
.value
, current_section
);
454 if (allocation_done
!= lang_final_phase_enum
)
455 result
.valid
= false;
457 result
= new_rel ((tree
->rel
.value
458 + tree
->rel
.section
->output_section
->vma
459 + tree
->rel
.section
->output_offset
),
463 result
= exp_fold_tree(tree
->unary
.child
,
465 allocation_done
, dot
, dotp
);
466 if (result
.valid
== true)
468 switch(tree
->type
.node_code
)
471 if (allocation_done
!= lang_first_phase_enum
) {
472 result
= new_rel_from_section(ALIGN_N(dot
,
478 result
.valid
= false;
482 if (allocation_done
!= lang_first_phase_enum
&& result
.valid
)
484 result
.value
+= result
.section
->bfd_section
->vma
;
485 result
.section
= abs_output_section
;
489 result
.valid
= false;
494 result
.value
= ~result
.value
;
498 result
.value
= !result
.value
;
502 result
.value
= -result
.value
;
505 if (allocation_done
==lang_allocating_phase_enum
) {
507 result
.value
= ALIGN_N(dot
, result
.value
);
510 /* Return next place aligned to value */
511 result
.valid
= false;
522 result
= exp_fold_tree(tree
->trinary
.cond
,
524 allocation_done
, dot
, dotp
);
526 result
= exp_fold_tree(result
.value
?
527 tree
->trinary
.lhs
:tree
->trinary
.rhs
,
529 allocation_done
, dot
, dotp
);
534 result
= fold_binary(tree
, current_section
, allocation_done
,
539 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0) {
540 /* Assignment to dot can only be done during allocation */
541 if (tree
->type
.node_class
== etree_provide
)
542 einfo ("%F%S can not PROVIDE assignment to location counter\n");
543 if (allocation_done
== lang_allocating_phase_enum
544 || (allocation_done
== lang_final_phase_enum
545 && current_section
== abs_output_section
)) {
546 result
= exp_fold_tree(tree
->assign
.src
,
548 lang_allocating_phase_enum
, dot
, dotp
);
549 if (result
.valid
== false) {
550 einfo("%F%S invalid assignment to location counter\n");
553 if (current_section
==
554 (lang_output_section_statement_type
*)NULL
) {
555 einfo("%F%S assignment to location counter invalid outside of SECTION\n");
558 bfd_vma nextdot
=result
.value
+
559 current_section
->bfd_section
->vma
;
560 if (nextdot
< dot
&& current_section
!= abs_output_section
) {
561 einfo("%F%S cannot move location counter backwards (from %V to %V)\n", dot
, nextdot
);
572 result
= exp_fold_tree (tree
->assign
.src
,
573 current_section
, allocation_done
,
577 struct bfd_link_hash_entry
*h
;
579 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
580 (tree
->type
.node_class
== etree_assign
583 if (h
== (struct bfd_link_hash_entry
*) NULL
)
585 if (tree
->type
.node_class
== etree_assign
)
586 einfo ("%P%F:%s: hash creation failed\n",
589 else if (tree
->type
.node_class
== etree_provide
590 && h
->type
!= bfd_link_hash_undefined
591 && h
->type
!= bfd_link_hash_common
)
593 /* Do nothing. The symbol was defined by some
598 /* FIXME: Should we worry if the symbol is already
600 h
->type
= bfd_link_hash_defined
;
601 h
->u
.def
.value
= result
.value
;
602 h
->u
.def
.section
= result
.section
->bfd_section
;
608 result
= fold_name(tree
, current_section
, allocation_done
, dot
);
611 einfo("%F%S need more of these %d\n",tree
->type
.node_class
);
620 static etree_value_type
621 exp_fold_tree_no_dot (tree
, current_section
, allocation_done
)
623 lang_output_section_statement_type
*current_section
;
624 lang_phase_type allocation_done
;
626 return exp_fold_tree(tree
, current_section
, allocation_done
, (bfd_vma
)
631 exp_binop (code
, lhs
, rhs
)
636 etree_type value
, *new;
639 value
.type
.node_code
= code
;
640 value
.binary
.lhs
= lhs
;
641 value
.binary
.rhs
= rhs
;
642 value
.type
.node_class
= etree_binary
;
643 r
= exp_fold_tree_no_dot(&value
,
645 lang_first_phase_enum
);
648 return exp_intop(r
.value
);
650 new = (etree_type
*) stat_alloc (sizeof (new->binary
));
651 memcpy((char *)new, (char *)&value
, sizeof(new->binary
));
656 exp_trinop (code
, cond
, lhs
, rhs
)
662 etree_type value
, *new;
664 value
.type
.node_code
= code
;
665 value
.trinary
.lhs
= lhs
;
666 value
.trinary
.cond
= cond
;
667 value
.trinary
.rhs
= rhs
;
668 value
.type
.node_class
= etree_trinary
;
669 r
= exp_fold_tree_no_dot(&value
, (lang_output_section_statement_type
670 *)NULL
,lang_first_phase_enum
);
672 return exp_intop(r
.value
);
674 new = (etree_type
*) stat_alloc (sizeof (new->trinary
));
675 memcpy((char *)new,(char *) &value
, sizeof(new->trinary
));
681 exp_unop (code
, child
)
685 etree_type value
, *new;
688 value
.unary
.type
.node_code
= code
;
689 value
.unary
.child
= child
;
690 value
.unary
.type
.node_class
= etree_unary
;
691 r
= exp_fold_tree_no_dot(&value
,abs_output_section
,
692 lang_first_phase_enum
);
694 return exp_intop(r
.value
);
696 new = (etree_type
*) stat_alloc (sizeof (new->unary
));
697 memcpy((char *)new, (char *)&value
, sizeof(new->unary
));
703 exp_nameop (code
, name
)
707 etree_type value
, *new;
709 value
.name
.type
.node_code
= code
;
710 value
.name
.name
= name
;
711 value
.name
.type
.node_class
= etree_name
;
714 r
= exp_fold_tree_no_dot(&value
,
715 (lang_output_section_statement_type
*)NULL
,
716 lang_first_phase_enum
);
718 return exp_intop(r
.value
);
720 new = (etree_type
*) stat_alloc (sizeof (new->name
));
721 memcpy((char *)new, (char *)&value
, sizeof(new->name
));
730 exp_assop (code
, dst
, src
)
735 etree_type value
, *new;
737 value
.assign
.type
.node_code
= code
;
740 value
.assign
.src
= src
;
741 value
.assign
.dst
= dst
;
742 value
.assign
.type
.node_class
= etree_assign
;
745 if (exp_fold_tree_no_dot(&value
, &result
)) {
746 return exp_intop(result
);
749 new = (etree_type
*) stat_alloc (sizeof (new->assign
));
750 memcpy((char *)new, (char *)&value
, sizeof(new->assign
));
754 /* Handle PROVIDE. */
757 exp_provide (dst
, src
)
763 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
764 n
->assign
.type
.node_code
= '=';
765 n
->assign
.type
.node_class
= etree_provide
;
772 exp_print_tree (tree
)
775 switch (tree
->type
.node_class
) {
777 print_address(tree
->value
.value
);
780 if (tree
->rel
.section
->owner
!= NULL
)
781 fprintf (config
.map_file
, "%s:",
782 bfd_get_filename (tree
->rel
.section
->owner
));
783 fprintf (config
.map_file
, "%s+", tree
->rel
.section
->name
);
784 print_address (tree
->rel
.value
);
788 if (tree
->assign
.dst
->sdefs
!= (asymbol
*)NULL
){
789 fprintf(config
.map_file
,"%s (%x) ",tree
->assign
.dst
->name
,
790 tree
->assign
.dst
->sdefs
->value
);
793 fprintf(config
.map_file
,"%s (UNDEFINED)",tree
->assign
.dst
->name
);
796 fprintf(config
.map_file
,"%s ",tree
->assign
.dst
);
797 exp_print_token(tree
->type
.node_code
);
798 exp_print_tree(tree
->assign
.src
);
801 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
802 exp_print_tree (tree
->assign
.src
);
803 fprintf (config
.map_file
, ")");
806 fprintf(config
.map_file
,"(");
807 exp_print_tree(tree
->binary
.lhs
);
808 exp_print_token(tree
->type
.node_code
);
809 exp_print_tree(tree
->binary
.rhs
);
810 fprintf(config
.map_file
,")");
813 exp_print_tree(tree
->trinary
.cond
);
814 fprintf(config
.map_file
,"?");
815 exp_print_tree(tree
->trinary
.lhs
);
816 fprintf(config
.map_file
,":");
817 exp_print_tree(tree
->trinary
.rhs
);
820 exp_print_token(tree
->unary
.type
.node_code
);
821 if (tree
->unary
.child
)
824 fprintf(config
.map_file
,"(");
825 exp_print_tree(tree
->unary
.child
);
826 fprintf(config
.map_file
,")");
831 fprintf(config
.map_file
,"????????");
834 if (tree
->type
.node_code
== NAME
) {
835 fprintf(config
.map_file
,"%s", tree
->name
.name
);
838 exp_print_token(tree
->type
.node_code
);
840 fprintf(config
.map_file
,"(%s)", tree
->name
.name
);
853 exp_get_vma (tree
, def
, name
, allocation_done
)
857 lang_phase_type allocation_done
;
861 if (tree
!= (etree_type
*)NULL
) {
862 r
= exp_fold_tree_no_dot(tree
,
865 if (r
.valid
== false && name
) {
866 einfo("%F%S nonconstant expression for %s\n",name
);
876 exp_get_value_int (tree
,def
,name
, allocation_done
)
880 lang_phase_type allocation_done
;
882 return (int)exp_get_vma(tree
,(bfd_vma
)def
,name
, allocation_done
);
887 exp_get_abs_int (tree
, def
, name
, allocation_done
)
891 lang_phase_type allocation_done
;
893 etree_value_type res
;
894 res
= exp_fold_tree_no_dot (tree
, abs_output_section
, allocation_done
);
898 res
.value
+= res
.section
->bfd_section
->vma
;
901 einfo ("%F%S non constant expression for %s\n",name
);