1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
7 This file is part of the GNU Binutils.
9 This program 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 3 of the License, or
12 (at your option) any later version.
14 This program 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 this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
25 /* This module is in charge of working out the contents of expressions.
27 It has to keep track of the relative/absness of a symbol etc. This
28 is done by keeping all values in a struct (an etree_value_type)
29 which contains a value, a section to which it is relative and a
43 #include "libiberty.h"
44 #include "safe-ctype.h"
46 static void exp_fold_tree_1 (etree_type
*);
47 static bfd_vma
align_n (bfd_vma
, bfd_vma
);
49 segment_type
*segments
;
51 struct ldexp_control expld
;
53 /* Print the string representation of the given token. Surround it
54 with spaces if INFIX_P is TRUE. */
57 exp_print_token (token_code_type code
, int infix_p
)
91 { SECTIONS
, "SECTIONS" },
92 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
94 { DEFINED
, "DEFINED" },
95 { TARGET_K
, "TARGET" },
96 { SEARCH_DIR
, "SEARCH_DIR" },
100 { ALIGNOF
, "ALIGNOF" },
101 { SIZEOF
, "SIZEOF" },
103 { LOADADDR
, "LOADADDR" },
104 { CONSTANT
, "CONSTANT" },
105 { ABSOLUTE
, "ABSOLUTE" },
108 { ASSERT_K
, "ASSERT" },
109 { REL
, "relocatable" },
110 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
111 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
112 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
113 { ORIGIN
, "ORIGIN" },
114 { LENGTH
, "LENGTH" },
115 { SEGMENT_START
, "SEGMENT_START" }
119 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
120 if (table
[idx
].code
== code
)
124 fputc (' ', config
.map_file
);
126 if (idx
< ARRAY_SIZE (table
))
127 fputs (table
[idx
].name
, config
.map_file
);
129 fputc (code
, config
.map_file
);
131 fprintf (config
.map_file
, "<code %d>", code
);
134 fputc (' ', config
.map_file
);
140 if (expld
.result
.section
!= NULL
)
141 expld
.result
.value
+= expld
.result
.section
->vma
;
142 expld
.result
.section
= bfd_abs_section_ptr
;
146 new_abs (bfd_vma value
)
148 expld
.result
.valid_p
= TRUE
;
149 expld
.result
.section
= bfd_abs_section_ptr
;
150 expld
.result
.value
= value
;
151 expld
.result
.str
= NULL
;
155 exp_intop (bfd_vma value
)
157 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
158 new_e
->type
.node_code
= INT
;
159 new_e
->type
.lineno
= lineno
;
160 new_e
->value
.value
= value
;
161 new_e
->value
.str
= NULL
;
162 new_e
->type
.node_class
= etree_value
;
167 exp_bigintop (bfd_vma value
, char *str
)
169 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
170 new_e
->type
.node_code
= INT
;
171 new_e
->type
.lineno
= lineno
;
172 new_e
->value
.value
= value
;
173 new_e
->value
.str
= str
;
174 new_e
->type
.node_class
= etree_value
;
178 /* Build an expression representing an unnamed relocatable value. */
181 exp_relop (asection
*section
, bfd_vma value
)
183 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
184 new_e
->type
.node_code
= REL
;
185 new_e
->type
.lineno
= lineno
;
186 new_e
->type
.node_class
= etree_rel
;
187 new_e
->rel
.section
= section
;
188 new_e
->rel
.value
= value
;
193 new_number (bfd_vma value
)
195 expld
.result
.valid_p
= TRUE
;
196 expld
.result
.value
= value
;
197 expld
.result
.str
= NULL
;
198 expld
.result
.section
= NULL
;
202 new_rel (bfd_vma value
, asection
*section
)
204 expld
.result
.valid_p
= TRUE
;
205 expld
.result
.value
= value
;
206 expld
.result
.str
= NULL
;
207 expld
.result
.section
= section
;
211 new_rel_from_abs (bfd_vma value
)
213 expld
.result
.valid_p
= TRUE
;
214 expld
.result
.value
= value
- expld
.section
->vma
;
215 expld
.result
.str
= NULL
;
216 expld
.result
.section
= expld
.section
;
220 fold_unary (etree_type
*tree
)
222 exp_fold_tree_1 (tree
->unary
.child
);
223 if (expld
.result
.valid_p
)
225 switch (tree
->type
.node_code
)
228 if (expld
.phase
!= lang_first_phase_enum
)
229 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
231 expld
.result
.valid_p
= FALSE
;
239 expld
.result
.value
= ~expld
.result
.value
;
243 expld
.result
.value
= !expld
.result
.value
;
247 expld
.result
.value
= -expld
.result
.value
;
251 /* Return next place aligned to value. */
252 if (expld
.phase
!= lang_first_phase_enum
)
255 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
258 expld
.result
.valid_p
= FALSE
;
261 case DATA_SEGMENT_END
:
262 if (expld
.phase
== lang_first_phase_enum
263 || expld
.section
!= bfd_abs_section_ptr
)
265 expld
.result
.valid_p
= FALSE
;
267 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
268 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
270 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
271 expld
.dataseg
.end
= expld
.result
.value
;
273 else if (expld
.dataseg
.phase
== exp_dataseg_done
274 || expld
.dataseg
.phase
== exp_dataseg_adjust
275 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
280 expld
.result
.valid_p
= FALSE
;
291 fold_binary (etree_type
*tree
)
293 etree_value_type lhs
;
294 exp_fold_tree_1 (tree
->binary
.lhs
);
296 /* The SEGMENT_START operator is special because its first
297 operand is a string, not the name of a symbol. Note that the
298 operands have been swapped, so binary.lhs is second (default)
299 operand, binary.rhs is first operand. */
300 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
302 const char *segment_name
;
305 /* Check to see if the user has overridden the default
307 segment_name
= tree
->binary
.rhs
->name
.name
;
308 for (seg
= segments
; seg
; seg
= seg
->next
)
309 if (strcmp (seg
->name
, segment_name
) == 0)
312 && config
.magic_demand_paged
313 && (seg
->value
% config
.maxpagesize
) != 0)
314 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
317 new_rel_from_abs (seg
->value
);
324 exp_fold_tree_1 (tree
->binary
.rhs
);
325 expld
.result
.valid_p
&= lhs
.valid_p
;
327 if (expld
.result
.valid_p
)
329 if (lhs
.section
!= expld
.result
.section
)
331 /* If the values are from different sections, and neither is
332 just a number, make both the source arguments absolute. */
333 if (expld
.result
.section
!= NULL
334 && lhs
.section
!= NULL
)
337 lhs
.value
+= lhs
.section
->vma
;
340 /* If the rhs is just a number, keep the lhs section. */
341 else if (expld
.result
.section
== NULL
)
342 expld
.result
.section
= lhs
.section
;
345 switch (tree
->type
.node_code
)
348 if (expld
.result
.value
!= 0)
349 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
350 % (bfd_signed_vma
) expld
.result
.value
);
351 else if (expld
.phase
!= lang_mark_phase_enum
)
352 einfo (_("%F%S %% by zero\n"));
356 if (expld
.result
.value
!= 0)
357 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
358 / (bfd_signed_vma
) expld
.result
.value
);
359 else if (expld
.phase
!= lang_mark_phase_enum
)
360 einfo (_("%F%S / by zero\n"));
365 expld.result.value = lhs.value y expld.result.value; \
370 expld.result.value = lhs.value y expld.result.value; \
371 expld.result.section = NULL; \
392 if (lhs
.value
> expld
.result
.value
)
393 expld
.result
.value
= lhs
.value
;
397 if (lhs
.value
< expld
.result
.value
)
398 expld
.result
.value
= lhs
.value
;
402 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
405 case DATA_SEGMENT_ALIGN
:
406 expld
.dataseg
.relro
= exp_dataseg_relro_start
;
407 if (expld
.phase
== lang_first_phase_enum
408 || expld
.section
!= bfd_abs_section_ptr
)
409 expld
.result
.valid_p
= FALSE
;
412 bfd_vma maxpage
= lhs
.value
;
413 bfd_vma commonpage
= expld
.result
.value
;
415 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
416 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
417 expld
.result
.value
= expld
.dataseg
.base
;
418 else if (expld
.dataseg
.phase
== exp_dataseg_adjust
)
420 if (commonpage
< maxpage
)
421 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
422 & (maxpage
- commonpage
));
426 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
427 if (expld
.dataseg
.phase
== exp_dataseg_done
)
431 else if (expld
.dataseg
.phase
== exp_dataseg_none
)
433 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
434 expld
.dataseg
.min_base
= expld
.dot
;
435 expld
.dataseg
.base
= expld
.result
.value
;
436 expld
.dataseg
.pagesize
= commonpage
;
437 expld
.dataseg
.maxpagesize
= maxpage
;
438 expld
.dataseg
.relro_end
= 0;
441 expld
.result
.valid_p
= FALSE
;
446 case DATA_SEGMENT_RELRO_END
:
447 expld
.dataseg
.relro
= exp_dataseg_relro_end
;
448 if (expld
.phase
== lang_first_phase_enum
449 || expld
.section
!= bfd_abs_section_ptr
)
450 expld
.result
.valid_p
= FALSE
;
451 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
452 || expld
.dataseg
.phase
== exp_dataseg_adjust
453 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
454 || expld
.dataseg
.phase
== exp_dataseg_done
)
456 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
457 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
458 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
460 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
461 && (expld
.dataseg
.relro_end
462 & (expld
.dataseg
.pagesize
- 1)))
464 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
465 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
466 expld
.result
.value
= (expld
.dataseg
.relro_end
467 - expld
.result
.value
);
470 expld
.result
.value
= lhs
.value
;
472 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
473 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
476 expld
.result
.valid_p
= FALSE
;
486 fold_trinary (etree_type
*tree
)
488 exp_fold_tree_1 (tree
->trinary
.cond
);
489 if (expld
.result
.valid_p
)
490 exp_fold_tree_1 (expld
.result
.value
492 : tree
->trinary
.rhs
);
496 fold_name (etree_type
*tree
)
498 memset (&expld
.result
, 0, sizeof (expld
.result
));
500 switch (tree
->type
.node_code
)
503 if (expld
.phase
!= lang_first_phase_enum
)
505 bfd_vma hdr_size
= 0;
506 /* Don't find the real header size if only marking sections;
507 The bfd function may cache incorrect data. */
508 if (expld
.phase
!= lang_mark_phase_enum
)
509 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
510 new_number (hdr_size
);
515 if (expld
.phase
== lang_first_phase_enum
)
516 lang_track_definedness (tree
->name
.name
);
519 struct bfd_link_hash_entry
*h
;
521 = lang_symbol_definition_iteration (tree
->name
.name
);
523 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
527 new_number (h
!= NULL
528 && (h
->type
== bfd_link_hash_defined
529 || h
->type
== bfd_link_hash_defweak
530 || h
->type
== bfd_link_hash_common
)
531 && (def_iteration
== lang_statement_iteration
532 || def_iteration
== -1));
537 if (expld
.phase
== lang_first_phase_enum
)
539 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
540 new_rel_from_abs (expld
.dot
);
543 struct bfd_link_hash_entry
*h
;
545 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
550 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
551 else if (h
->type
== bfd_link_hash_defined
552 || h
->type
== bfd_link_hash_defweak
)
554 asection
*output_section
;
556 output_section
= h
->u
.def
.section
->output_section
;
557 if (output_section
== NULL
)
559 if (expld
.phase
!= lang_mark_phase_enum
)
560 einfo (_("%X%S: unresolvable symbol `%s'"
561 " referenced in expression\n"),
564 else if (output_section
== bfd_abs_section_ptr
565 && (expld
.section
!= bfd_abs_section_ptr
566 || config
.sane_expr
))
567 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
569 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
572 else if (expld
.phase
== lang_final_phase_enum
573 || expld
.assigning_to_dot
)
574 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
576 else if (h
->type
== bfd_link_hash_new
)
578 h
->type
= bfd_link_hash_undefined
;
579 h
->u
.undef
.abfd
= NULL
;
580 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
581 bfd_link_add_undef (link_info
.hash
, h
);
587 if (expld
.phase
!= lang_first_phase_enum
)
589 lang_output_section_statement_type
*os
;
591 os
= lang_output_section_find (tree
->name
.name
);
594 if (expld
.phase
== lang_final_phase_enum
)
595 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
598 else if (os
->processed_vma
)
599 new_rel (0, os
->bfd_section
);
604 if (expld
.phase
!= lang_first_phase_enum
)
606 lang_output_section_statement_type
*os
;
608 os
= lang_output_section_find (tree
->name
.name
);
611 if (expld
.phase
== lang_final_phase_enum
)
612 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
615 else if (os
->processed_lma
)
617 if (os
->load_base
== NULL
)
618 new_abs (os
->bfd_section
->lma
);
621 exp_fold_tree_1 (os
->load_base
);
622 if (expld
.result
.valid_p
)
631 if (expld
.phase
!= lang_first_phase_enum
)
633 lang_output_section_statement_type
*os
;
635 os
= lang_output_section_find (tree
->name
.name
);
638 if (expld
.phase
== lang_final_phase_enum
)
639 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
643 else if (os
->processed_vma
)
647 if (tree
->type
.node_code
== SIZEOF
)
648 val
= (os
->bfd_section
->size
649 / bfd_octets_per_byte (link_info
.output_bfd
));
651 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
660 lang_memory_region_type
*mem
;
662 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
664 new_number (mem
->length
);
666 einfo (_("%F%S: undefined MEMORY region `%s'"
667 " referenced in expression\n"), tree
->name
.name
);
672 if (expld
.phase
!= lang_first_phase_enum
)
674 lang_memory_region_type
*mem
;
676 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
678 new_rel_from_abs (mem
->origin
);
680 einfo (_("%F%S: undefined MEMORY region `%s'"
681 " referenced in expression\n"), tree
->name
.name
);
686 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
687 new_number (config
.maxpagesize
);
688 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
689 new_number (config
.commonpagesize
);
691 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
702 exp_fold_tree_1 (etree_type
*tree
)
706 memset (&expld
.result
, 0, sizeof (expld
.result
));
710 switch (tree
->type
.node_class
)
713 if (expld
.section
== bfd_abs_section_ptr
714 && !config
.sane_expr
)
715 new_abs (tree
->value
.value
);
717 new_number (tree
->value
.value
);
718 expld
.result
.str
= tree
->value
.str
;
722 if (expld
.phase
!= lang_first_phase_enum
)
724 asection
*output_section
= tree
->rel
.section
->output_section
;
725 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
729 memset (&expld
.result
, 0, sizeof (expld
.result
));
733 exp_fold_tree_1 (tree
->assert_s
.child
);
734 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
735 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
753 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
755 /* Assignment to dot can only be done during allocation. */
756 if (tree
->type
.node_class
!= etree_assign
)
757 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
758 if (expld
.phase
== lang_mark_phase_enum
759 || expld
.phase
== lang_allocating_phase_enum
760 || (expld
.phase
== lang_final_phase_enum
761 && expld
.section
== bfd_abs_section_ptr
))
763 /* Notify the folder that this is an assignment to dot. */
764 expld
.assigning_to_dot
= TRUE
;
765 exp_fold_tree_1 (tree
->assign
.src
);
766 expld
.assigning_to_dot
= FALSE
;
768 if (!expld
.result
.valid_p
)
770 if (expld
.phase
!= lang_mark_phase_enum
)
771 einfo (_("%F%S invalid assignment to location counter\n"));
773 else if (expld
.dotp
== NULL
)
774 einfo (_("%F%S assignment to location counter"
775 " invalid outside of SECTION\n"));
780 nextdot
= expld
.result
.value
;
781 if (expld
.result
.section
!= NULL
)
782 nextdot
+= expld
.result
.section
->vma
;
784 nextdot
+= expld
.section
->vma
;
785 if (nextdot
< expld
.dot
786 && expld
.section
!= bfd_abs_section_ptr
)
787 einfo (_("%F%S cannot move location counter backwards"
788 " (from %V to %V)\n"), expld
.dot
, nextdot
);
792 *expld
.dotp
= nextdot
;
797 memset (&expld
.result
, 0, sizeof (expld
.result
));
801 struct bfd_link_hash_entry
*h
= NULL
;
803 if (tree
->type
.node_class
== etree_provide
)
805 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
808 || (h
->type
!= bfd_link_hash_new
809 && h
->type
!= bfd_link_hash_undefined
810 && h
->type
!= bfd_link_hash_common
))
812 /* Do nothing. The symbol was never referenced, or was
813 defined by some object. */
818 exp_fold_tree_1 (tree
->assign
.src
);
819 if (expld
.result
.valid_p
820 || (expld
.phase
== lang_first_phase_enum
821 && tree
->type
.node_class
== etree_assign
822 && tree
->assign
.hidden
))
826 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
829 einfo (_("%P%F:%s: hash creation failed\n"),
833 /* FIXME: Should we worry if the symbol is already
835 lang_update_definedness (tree
->assign
.dst
, h
);
836 h
->type
= bfd_link_hash_defined
;
837 h
->u
.def
.value
= expld
.result
.value
;
838 if (expld
.result
.section
== NULL
)
839 expld
.result
.section
= expld
.section
;
840 h
->u
.def
.section
= expld
.result
.section
;
841 if (tree
->type
.node_class
== etree_provide
)
842 tree
->type
.node_class
= etree_provided
;
844 /* Copy the symbol type if this is a simple assignment of
845 one symbol to annother. */
846 if (tree
->assign
.src
->type
.node_class
== etree_name
)
848 struct bfd_link_hash_entry
*hsrc
;
850 hsrc
= bfd_link_hash_lookup (link_info
.hash
,
851 tree
->assign
.src
->name
.name
,
854 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
, h
,
858 else if (expld
.phase
== lang_final_phase_enum
)
860 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
863 && h
->type
== bfd_link_hash_new
)
864 h
->type
= bfd_link_hash_undefined
;
875 memset (&expld
.result
, 0, sizeof (expld
.result
));
881 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
885 expld
.section
= current_section
;
886 exp_fold_tree_1 (tree
);
890 exp_fold_tree_no_dot (etree_type
*tree
)
894 expld
.section
= bfd_abs_section_ptr
;
895 exp_fold_tree_1 (tree
);
899 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
901 etree_type value
, *new_e
;
903 value
.type
.node_code
= code
;
904 value
.type
.lineno
= lhs
->type
.lineno
;
905 value
.binary
.lhs
= lhs
;
906 value
.binary
.rhs
= rhs
;
907 value
.type
.node_class
= etree_binary
;
908 exp_fold_tree_no_dot (&value
);
909 if (expld
.result
.valid_p
)
910 return exp_intop (expld
.result
.value
);
912 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->binary
));
913 memcpy (new_e
, &value
, sizeof (new_e
->binary
));
918 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
920 etree_type value
, *new_e
;
922 value
.type
.node_code
= code
;
923 value
.type
.lineno
= lhs
->type
.lineno
;
924 value
.trinary
.lhs
= lhs
;
925 value
.trinary
.cond
= cond
;
926 value
.trinary
.rhs
= rhs
;
927 value
.type
.node_class
= etree_trinary
;
928 exp_fold_tree_no_dot (&value
);
929 if (expld
.result
.valid_p
)
930 return exp_intop (expld
.result
.value
);
932 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->trinary
));
933 memcpy (new_e
, &value
, sizeof (new_e
->trinary
));
938 exp_unop (int code
, etree_type
*child
)
940 etree_type value
, *new_e
;
942 value
.unary
.type
.node_code
= code
;
943 value
.unary
.type
.lineno
= child
->type
.lineno
;
944 value
.unary
.child
= child
;
945 value
.unary
.type
.node_class
= etree_unary
;
946 exp_fold_tree_no_dot (&value
);
947 if (expld
.result
.valid_p
)
948 return exp_intop (expld
.result
.value
);
950 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->unary
));
951 memcpy (new_e
, &value
, sizeof (new_e
->unary
));
956 exp_nameop (int code
, const char *name
)
958 etree_type value
, *new_e
;
960 value
.name
.type
.node_code
= code
;
961 value
.name
.type
.lineno
= lineno
;
962 value
.name
.name
= name
;
963 value
.name
.type
.node_class
= etree_name
;
965 exp_fold_tree_no_dot (&value
);
966 if (expld
.result
.valid_p
)
967 return exp_intop (expld
.result
.value
);
969 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
970 memcpy (new_e
, &value
, sizeof (new_e
->name
));
976 exp_assop (const char *dst
,
978 enum node_tree_enum
class,
983 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
984 n
->assign
.type
.node_code
= '=';
985 n
->assign
.type
.lineno
= src
->type
.lineno
;
986 n
->assign
.type
.node_class
= class;
989 n
->assign
.hidden
= hidden
;
994 exp_assign (const char *dst
, etree_type
*src
)
996 return exp_assop (dst
, src
, etree_assign
, FALSE
);
1000 exp_defsym (const char *dst
, etree_type
*src
)
1002 return exp_assop (dst
, src
, etree_assign
, TRUE
);
1005 /* Handle PROVIDE. */
1008 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1010 return exp_assop (dst
, src
, etree_provide
, hidden
);
1013 /* Handle ASSERT. */
1016 exp_assert (etree_type
*exp
, const char *message
)
1020 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1021 n
->assert_s
.type
.node_code
= '!';
1022 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1023 n
->assert_s
.type
.node_class
= etree_assert
;
1024 n
->assert_s
.child
= exp
;
1025 n
->assert_s
.message
= message
;
1030 exp_print_tree (etree_type
*tree
)
1032 bfd_boolean function_like
;
1034 if (config
.map_file
== NULL
)
1035 config
.map_file
= stderr
;
1039 minfo ("NULL TREE\n");
1043 switch (tree
->type
.node_class
)
1046 minfo ("0x%v", tree
->value
.value
);
1049 if (tree
->rel
.section
->owner
!= NULL
)
1050 minfo ("%B:", tree
->rel
.section
->owner
);
1051 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1054 fputs (tree
->assign
.dst
, config
.map_file
);
1055 exp_print_token (tree
->type
.node_code
, TRUE
);
1056 exp_print_tree (tree
->assign
.src
);
1059 case etree_provided
:
1060 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
1061 exp_print_tree (tree
->assign
.src
);
1062 fputc (')', config
.map_file
);
1065 function_like
= FALSE
;
1066 switch (tree
->type
.node_code
)
1071 case DATA_SEGMENT_ALIGN
:
1072 case DATA_SEGMENT_RELRO_END
:
1073 function_like
= TRUE
;
1077 exp_print_token (tree
->type
.node_code
, FALSE
);
1078 fputc (' ', config
.map_file
);
1080 fputc ('(', config
.map_file
);
1081 exp_print_tree (tree
->binary
.lhs
);
1083 fprintf (config
.map_file
, ", ");
1085 exp_print_token (tree
->type
.node_code
, TRUE
);
1086 exp_print_tree (tree
->binary
.rhs
);
1087 fputc (')', config
.map_file
);
1090 exp_print_tree (tree
->trinary
.cond
);
1091 fputc ('?', config
.map_file
);
1092 exp_print_tree (tree
->trinary
.lhs
);
1093 fputc (':', config
.map_file
);
1094 exp_print_tree (tree
->trinary
.rhs
);
1097 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1098 if (tree
->unary
.child
)
1100 fprintf (config
.map_file
, " (");
1101 exp_print_tree (tree
->unary
.child
);
1102 fputc (')', config
.map_file
);
1107 fprintf (config
.map_file
, "ASSERT (");
1108 exp_print_tree (tree
->assert_s
.child
);
1109 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1113 if (tree
->type
.node_code
== NAME
)
1114 fputs (tree
->name
.name
, config
.map_file
);
1117 exp_print_token (tree
->type
.node_code
, FALSE
);
1118 if (tree
->name
.name
)
1119 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1129 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1133 exp_fold_tree_no_dot (tree
);
1134 if (expld
.result
.valid_p
)
1135 return expld
.result
.value
;
1136 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1137 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1143 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1145 return exp_get_vma (tree
, def
, name
);
1149 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1158 exp_fold_tree_no_dot (tree
);
1159 if (!expld
.result
.valid_p
)
1161 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1162 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1166 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1170 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1171 fill
->size
= (len
+ 1) / 2;
1173 s
= (unsigned char *) expld
.result
.str
;
1181 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1195 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1196 val
= expld
.result
.value
;
1197 fill
->data
[0] = (val
>> 24) & 0xff;
1198 fill
->data
[1] = (val
>> 16) & 0xff;
1199 fill
->data
[2] = (val
>> 8) & 0xff;
1200 fill
->data
[3] = (val
>> 0) & 0xff;
1207 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1211 exp_fold_tree_no_dot (tree
);
1213 if (expld
.result
.valid_p
)
1215 if (expld
.result
.section
!= NULL
)
1216 expld
.result
.value
+= expld
.result
.section
->vma
;
1217 return expld
.result
.value
;
1219 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1221 lineno
= tree
->type
.lineno
;
1222 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1229 align_n (bfd_vma value
, bfd_vma align
)
1234 value
= (value
+ align
- 1) / align
;
1235 return value
* align
;