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
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 void exp_fold_tree_no_dot (etree_type
*);
48 static bfd_vma
align_n (bfd_vma
, bfd_vma
);
50 segment_type
*segments
;
52 struct ldexp_control expld
;
54 /* Print the string representation of the given token. Surround it
55 with spaces if INFIX_P is TRUE. */
58 exp_print_token (token_code_type code
, int infix_p
)
92 { SECTIONS
, "SECTIONS" },
93 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
95 { DEFINED
, "DEFINED" },
96 { TARGET_K
, "TARGET" },
97 { SEARCH_DIR
, "SEARCH_DIR" },
101 { ALIGNOF
, "ALIGNOF" },
102 { SIZEOF
, "SIZEOF" },
104 { LOADADDR
, "LOADADDR" },
105 { CONSTANT
, "CONSTANT" },
106 { ABSOLUTE
, "ABSOLUTE" },
109 { ASSERT_K
, "ASSERT" },
110 { REL
, "relocatable" },
111 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
112 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
113 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
114 { ORIGIN
, "ORIGIN" },
115 { LENGTH
, "LENGTH" },
116 { SEGMENT_START
, "SEGMENT_START" }
120 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
121 if (table
[idx
].code
== code
)
125 fputc (' ', config
.map_file
);
127 if (idx
< ARRAY_SIZE (table
))
128 fputs (table
[idx
].name
, config
.map_file
);
130 fputc (code
, config
.map_file
);
132 fprintf (config
.map_file
, "<code %d>", code
);
135 fputc (' ', config
.map_file
);
141 if (expld
.result
.section
!= NULL
)
142 expld
.result
.value
+= expld
.result
.section
->vma
;
143 expld
.result
.section
= bfd_abs_section_ptr
;
147 new_abs (bfd_vma value
)
149 expld
.result
.valid_p
= TRUE
;
150 expld
.result
.section
= bfd_abs_section_ptr
;
151 expld
.result
.value
= value
;
152 expld
.result
.str
= NULL
;
156 exp_intop (bfd_vma value
)
158 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
159 new_e
->type
.node_code
= INT
;
160 new_e
->type
.lineno
= lineno
;
161 new_e
->value
.value
= value
;
162 new_e
->value
.str
= NULL
;
163 new_e
->type
.node_class
= etree_value
;
168 exp_bigintop (bfd_vma value
, char *str
)
170 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
171 new_e
->type
.node_code
= INT
;
172 new_e
->type
.lineno
= lineno
;
173 new_e
->value
.value
= value
;
174 new_e
->value
.str
= str
;
175 new_e
->type
.node_class
= etree_value
;
179 /* Build an expression representing an unnamed relocatable value. */
182 exp_relop (asection
*section
, bfd_vma value
)
184 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
185 new_e
->type
.node_code
= REL
;
186 new_e
->type
.lineno
= lineno
;
187 new_e
->type
.node_class
= etree_rel
;
188 new_e
->rel
.section
= section
;
189 new_e
->rel
.value
= value
;
194 new_number (bfd_vma value
)
196 expld
.result
.valid_p
= TRUE
;
197 expld
.result
.value
= value
;
198 expld
.result
.str
= NULL
;
199 expld
.result
.section
= NULL
;
203 new_rel (bfd_vma value
, asection
*section
)
205 expld
.result
.valid_p
= TRUE
;
206 expld
.result
.value
= value
;
207 expld
.result
.str
= NULL
;
208 expld
.result
.section
= section
;
212 new_rel_from_abs (bfd_vma value
)
214 expld
.result
.valid_p
= TRUE
;
215 expld
.result
.value
= value
- expld
.section
->vma
;
216 expld
.result
.str
= NULL
;
217 expld
.result
.section
= expld
.section
;
221 fold_unary (etree_type
*tree
)
223 exp_fold_tree_1 (tree
->unary
.child
);
224 if (expld
.result
.valid_p
)
226 switch (tree
->type
.node_code
)
229 if (expld
.phase
!= lang_first_phase_enum
)
230 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
232 expld
.result
.valid_p
= FALSE
;
240 expld
.result
.value
= ~expld
.result
.value
;
244 expld
.result
.value
= !expld
.result
.value
;
248 expld
.result
.value
= -expld
.result
.value
;
252 /* Return next place aligned to value. */
253 if (expld
.phase
!= lang_first_phase_enum
)
256 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
259 expld
.result
.valid_p
= FALSE
;
262 case DATA_SEGMENT_END
:
263 if (expld
.phase
!= lang_first_phase_enum
264 && expld
.section
== bfd_abs_section_ptr
265 && (expld
.dataseg
.phase
== exp_dataseg_align_seen
266 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
267 || expld
.dataseg
.phase
== exp_dataseg_adjust
268 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
269 || expld
.phase
== lang_final_phase_enum
))
271 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
272 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
274 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
275 expld
.dataseg
.end
= expld
.result
.value
;
279 expld
.result
.valid_p
= FALSE
;
290 fold_binary (etree_type
*tree
)
292 etree_value_type lhs
;
293 exp_fold_tree_1 (tree
->binary
.lhs
);
295 /* The SEGMENT_START operator is special because its first
296 operand is a string, not the name of a symbol. Note that the
297 operands have been swapped, so binary.lhs is second (default)
298 operand, binary.rhs is first operand. */
299 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
301 const char *segment_name
;
304 /* Check to see if the user has overridden the default
306 segment_name
= tree
->binary
.rhs
->name
.name
;
307 for (seg
= segments
; seg
; seg
= seg
->next
)
308 if (strcmp (seg
->name
, segment_name
) == 0)
311 && config
.magic_demand_paged
312 && (seg
->value
% config
.maxpagesize
) != 0)
313 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
316 new_rel_from_abs (seg
->value
);
323 exp_fold_tree_1 (tree
->binary
.rhs
);
324 expld
.result
.valid_p
&= lhs
.valid_p
;
326 if (expld
.result
.valid_p
)
328 if (lhs
.section
!= expld
.result
.section
)
330 /* If the values are from different sections, and neither is
331 just a number, make both the source arguments absolute. */
332 if (expld
.result
.section
!= NULL
333 && lhs
.section
!= NULL
)
336 lhs
.value
+= lhs
.section
->vma
;
339 /* If the rhs is just a number, keep the lhs section. */
340 else if (expld
.result
.section
== NULL
)
341 expld
.result
.section
= lhs
.section
;
344 switch (tree
->type
.node_code
)
347 if (expld
.result
.value
!= 0)
348 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
349 % (bfd_signed_vma
) expld
.result
.value
);
350 else if (expld
.phase
!= lang_mark_phase_enum
)
351 einfo (_("%F%S %% by zero\n"));
355 if (expld
.result
.value
!= 0)
356 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
357 / (bfd_signed_vma
) expld
.result
.value
);
358 else if (expld
.phase
!= lang_mark_phase_enum
)
359 einfo (_("%F%S / by zero\n"));
364 expld.result.value = lhs.value y expld.result.value; \
369 expld.result.value = lhs.value y expld.result.value; \
370 expld.result.section = NULL; \
391 if (lhs
.value
> expld
.result
.value
)
392 expld
.result
.value
= lhs
.value
;
396 if (lhs
.value
< expld
.result
.value
)
397 expld
.result
.value
= lhs
.value
;
401 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
404 case DATA_SEGMENT_ALIGN
:
405 expld
.dataseg
.relro
= exp_dataseg_relro_start
;
406 if (expld
.phase
!= lang_first_phase_enum
407 && expld
.section
== bfd_abs_section_ptr
408 && (expld
.dataseg
.phase
== exp_dataseg_none
409 || expld
.dataseg
.phase
== exp_dataseg_adjust
410 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
411 || expld
.phase
== lang_final_phase_enum
))
413 bfd_vma maxpage
= lhs
.value
;
414 bfd_vma commonpage
= expld
.result
.value
;
416 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
417 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
418 expld
.result
.value
= expld
.dataseg
.base
;
419 else if (expld
.dataseg
.phase
!= exp_dataseg_adjust
)
421 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
422 if (expld
.phase
== lang_allocating_phase_enum
)
424 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
425 expld
.dataseg
.min_base
= expld
.dot
;
426 expld
.dataseg
.base
= expld
.result
.value
;
427 expld
.dataseg
.pagesize
= commonpage
;
428 expld
.dataseg
.maxpagesize
= maxpage
;
429 expld
.dataseg
.relro_end
= 0;
432 else if (commonpage
< maxpage
)
433 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
434 & (maxpage
- commonpage
));
437 expld
.result
.valid_p
= FALSE
;
440 case DATA_SEGMENT_RELRO_END
:
441 expld
.dataseg
.relro
= exp_dataseg_relro_end
;
442 if (expld
.phase
!= lang_first_phase_enum
443 && (expld
.dataseg
.phase
== exp_dataseg_align_seen
444 || expld
.dataseg
.phase
== exp_dataseg_adjust
445 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
446 || expld
.phase
== lang_final_phase_enum
))
448 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
449 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
450 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
452 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
453 && (expld
.dataseg
.relro_end
454 & (expld
.dataseg
.pagesize
- 1)))
456 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
457 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
458 expld
.result
.value
= (expld
.dataseg
.relro_end
459 - expld
.result
.value
);
462 expld
.result
.value
= lhs
.value
;
464 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
465 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
468 expld
.result
.valid_p
= FALSE
;
478 fold_trinary (etree_type
*tree
)
480 exp_fold_tree_1 (tree
->trinary
.cond
);
481 if (expld
.result
.valid_p
)
482 exp_fold_tree_1 (expld
.result
.value
484 : tree
->trinary
.rhs
);
488 fold_name (etree_type
*tree
)
490 memset (&expld
.result
, 0, sizeof (expld
.result
));
492 switch (tree
->type
.node_code
)
495 if (expld
.phase
!= lang_first_phase_enum
)
497 bfd_vma hdr_size
= 0;
498 /* Don't find the real header size if only marking sections;
499 The bfd function may cache incorrect data. */
500 if (expld
.phase
!= lang_mark_phase_enum
)
501 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
502 new_number (hdr_size
);
507 if (expld
.phase
== lang_first_phase_enum
)
508 lang_track_definedness (tree
->name
.name
);
511 struct bfd_link_hash_entry
*h
;
513 = lang_symbol_definition_iteration (tree
->name
.name
);
515 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
519 new_number (h
!= NULL
520 && (h
->type
== bfd_link_hash_defined
521 || h
->type
== bfd_link_hash_defweak
522 || h
->type
== bfd_link_hash_common
)
523 && (def_iteration
== lang_statement_iteration
524 || def_iteration
== -1));
529 if (expld
.phase
== lang_first_phase_enum
)
531 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
532 new_rel_from_abs (expld
.dot
);
535 struct bfd_link_hash_entry
*h
;
537 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
542 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
543 else if (h
->type
== bfd_link_hash_defined
544 || h
->type
== bfd_link_hash_defweak
)
546 asection
*output_section
;
548 output_section
= h
->u
.def
.section
->output_section
;
549 if (output_section
== NULL
)
551 if (expld
.phase
!= lang_mark_phase_enum
)
552 einfo (_("%X%S: unresolvable symbol `%s'"
553 " referenced in expression\n"),
557 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
560 else if (expld
.phase
== lang_final_phase_enum
561 || expld
.assigning_to_dot
)
562 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
564 else if (h
->type
== bfd_link_hash_new
)
566 h
->type
= bfd_link_hash_undefined
;
567 h
->u
.undef
.abfd
= NULL
;
568 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
569 bfd_link_add_undef (link_info
.hash
, h
);
575 if (expld
.phase
!= lang_first_phase_enum
)
577 lang_output_section_statement_type
*os
;
579 os
= lang_output_section_find (tree
->name
.name
);
582 if (expld
.phase
== lang_final_phase_enum
)
583 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
586 else if (os
->processed_vma
)
587 new_rel (0, os
->bfd_section
);
592 if (expld
.phase
!= lang_first_phase_enum
)
594 lang_output_section_statement_type
*os
;
596 os
= lang_output_section_find (tree
->name
.name
);
599 if (expld
.phase
== lang_final_phase_enum
)
600 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
603 else if (os
->processed_lma
)
605 if (os
->load_base
== NULL
)
606 new_abs (os
->bfd_section
->lma
);
609 exp_fold_tree_1 (os
->load_base
);
610 if (expld
.result
.valid_p
)
619 if (expld
.phase
!= lang_first_phase_enum
)
621 lang_output_section_statement_type
*os
;
623 os
= lang_output_section_find (tree
->name
.name
);
626 if (expld
.phase
== lang_final_phase_enum
)
627 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
631 else if (os
->processed_vma
)
635 if (tree
->type
.node_code
== SIZEOF
)
636 val
= (os
->bfd_section
->size
637 / bfd_octets_per_byte (link_info
.output_bfd
));
639 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
648 lang_memory_region_type
*mem
;
650 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
652 new_number (mem
->length
);
654 einfo (_("%F%S: undefined MEMORY region `%s'"
655 " referenced in expression\n"), tree
->name
.name
);
660 if (expld
.phase
!= lang_first_phase_enum
)
662 lang_memory_region_type
*mem
;
664 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
666 new_rel_from_abs (mem
->origin
);
668 einfo (_("%F%S: undefined MEMORY region `%s'"
669 " referenced in expression\n"), tree
->name
.name
);
674 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
675 new_number (config
.maxpagesize
);
676 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
677 new_number (config
.commonpagesize
);
679 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
690 exp_fold_tree_1 (etree_type
*tree
)
694 memset (&expld
.result
, 0, sizeof (expld
.result
));
698 switch (tree
->type
.node_class
)
701 new_number (tree
->value
.value
);
702 expld
.result
.str
= tree
->value
.str
;
706 if (expld
.phase
!= lang_first_phase_enum
)
708 asection
*output_section
= tree
->rel
.section
->output_section
;
709 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
713 memset (&expld
.result
, 0, sizeof (expld
.result
));
717 exp_fold_tree_1 (tree
->assert_s
.child
);
718 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
719 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
737 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
739 /* Assignment to dot can only be done during allocation. */
740 if (tree
->type
.node_class
!= etree_assign
)
741 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
742 if (expld
.phase
== lang_mark_phase_enum
743 || expld
.phase
== lang_allocating_phase_enum
744 || (expld
.phase
== lang_final_phase_enum
745 && expld
.section
== bfd_abs_section_ptr
))
747 /* Notify the folder that this is an assignment to dot. */
748 expld
.assigning_to_dot
= TRUE
;
749 exp_fold_tree_1 (tree
->assign
.src
);
750 expld
.assigning_to_dot
= FALSE
;
752 if (!expld
.result
.valid_p
)
754 if (expld
.phase
!= lang_mark_phase_enum
)
755 einfo (_("%F%S invalid assignment to location counter\n"));
757 else if (expld
.dotp
== NULL
)
758 einfo (_("%F%S assignment to location counter"
759 " invalid outside of SECTION\n"));
764 nextdot
= expld
.result
.value
;
765 if (expld
.result
.section
!= NULL
)
766 nextdot
+= expld
.result
.section
->vma
;
768 nextdot
+= expld
.section
->vma
;
769 if (nextdot
< expld
.dot
770 && expld
.section
!= bfd_abs_section_ptr
)
771 einfo (_("%F%S cannot move location counter backwards"
772 " (from %V to %V)\n"), expld
.dot
, nextdot
);
776 *expld
.dotp
= nextdot
;
781 memset (&expld
.result
, 0, sizeof (expld
.result
));
785 struct bfd_link_hash_entry
*h
= NULL
;
787 if (tree
->type
.node_class
== etree_provide
)
789 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
792 || (h
->type
!= bfd_link_hash_new
793 && h
->type
!= bfd_link_hash_undefined
794 && h
->type
!= bfd_link_hash_common
))
796 /* Do nothing. The symbol was never referenced, or was
797 defined by some object. */
802 exp_fold_tree_1 (tree
->assign
.src
);
803 if (expld
.result
.valid_p
)
807 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
810 einfo (_("%P%F:%s: hash creation failed\n"),
814 /* FIXME: Should we worry if the symbol is already
816 lang_update_definedness (tree
->assign
.dst
, h
);
817 h
->type
= bfd_link_hash_defined
;
818 h
->u
.def
.value
= expld
.result
.value
;
819 if (expld
.result
.section
== NULL
)
820 expld
.result
.section
= expld
.section
;
821 h
->u
.def
.section
= expld
.result
.section
;
822 if (tree
->type
.node_class
== etree_provide
)
823 tree
->type
.node_class
= etree_provided
;
825 /* Copy the symbol type if this is a simple assignment of
826 one symbol to annother. */
827 if (tree
->assign
.src
->type
.node_class
== etree_name
)
829 struct bfd_link_hash_entry
*hsrc
;
831 hsrc
= bfd_link_hash_lookup (link_info
.hash
,
832 tree
->assign
.src
->name
.name
,
835 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
, h
,
839 else if (expld
.phase
== lang_final_phase_enum
)
841 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
844 && h
->type
== bfd_link_hash_new
)
845 h
->type
= bfd_link_hash_undefined
;
856 memset (&expld
.result
, 0, sizeof (expld
.result
));
860 /* Any value not inside an output section statement is an
862 if (expld
.result
.valid_p
863 && expld
.section
== bfd_abs_section_ptr
)
868 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
872 expld
.section
= current_section
;
873 exp_fold_tree_1 (tree
);
877 exp_fold_tree_no_dot (etree_type
*tree
)
881 expld
.section
= bfd_abs_section_ptr
;
882 exp_fold_tree_1 (tree
);
886 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
888 etree_type value
, *new_e
;
890 value
.type
.node_code
= code
;
891 value
.type
.lineno
= lhs
->type
.lineno
;
892 value
.binary
.lhs
= lhs
;
893 value
.binary
.rhs
= rhs
;
894 value
.type
.node_class
= etree_binary
;
895 exp_fold_tree_no_dot (&value
);
896 if (expld
.result
.valid_p
)
897 return exp_intop (expld
.result
.value
);
899 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->binary
));
900 memcpy (new_e
, &value
, sizeof (new_e
->binary
));
905 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
907 etree_type value
, *new_e
;
909 value
.type
.node_code
= code
;
910 value
.type
.lineno
= lhs
->type
.lineno
;
911 value
.trinary
.lhs
= lhs
;
912 value
.trinary
.cond
= cond
;
913 value
.trinary
.rhs
= rhs
;
914 value
.type
.node_class
= etree_trinary
;
915 exp_fold_tree_no_dot (&value
);
916 if (expld
.result
.valid_p
)
917 return exp_intop (expld
.result
.value
);
919 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->trinary
));
920 memcpy (new_e
, &value
, sizeof (new_e
->trinary
));
925 exp_unop (int code
, etree_type
*child
)
927 etree_type value
, *new_e
;
929 value
.unary
.type
.node_code
= code
;
930 value
.unary
.type
.lineno
= child
->type
.lineno
;
931 value
.unary
.child
= child
;
932 value
.unary
.type
.node_class
= etree_unary
;
933 exp_fold_tree_no_dot (&value
);
934 if (expld
.result
.valid_p
)
935 return exp_intop (expld
.result
.value
);
937 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->unary
));
938 memcpy (new_e
, &value
, sizeof (new_e
->unary
));
943 exp_nameop (int code
, const char *name
)
945 etree_type value
, *new_e
;
947 value
.name
.type
.node_code
= code
;
948 value
.name
.type
.lineno
= lineno
;
949 value
.name
.name
= name
;
950 value
.name
.type
.node_class
= etree_name
;
952 exp_fold_tree_no_dot (&value
);
953 if (expld
.result
.valid_p
)
954 return exp_intop (expld
.result
.value
);
956 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
957 memcpy (new_e
, &value
, sizeof (new_e
->name
));
963 exp_assop (int code
, const char *dst
, etree_type
*src
)
967 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->assign
));
968 new_e
->type
.node_code
= code
;
969 new_e
->type
.lineno
= src
->type
.lineno
;
970 new_e
->type
.node_class
= etree_assign
;
971 new_e
->assign
.src
= src
;
972 new_e
->assign
.dst
= dst
;
976 /* Handle PROVIDE. */
979 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
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
= etree_provide
;
989 n
->assign
.hidden
= hidden
;
996 exp_assert (etree_type
*exp
, const char *message
)
1000 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1001 n
->assert_s
.type
.node_code
= '!';
1002 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1003 n
->assert_s
.type
.node_class
= etree_assert
;
1004 n
->assert_s
.child
= exp
;
1005 n
->assert_s
.message
= message
;
1010 exp_print_tree (etree_type
*tree
)
1012 bfd_boolean function_like
;
1014 if (config
.map_file
== NULL
)
1015 config
.map_file
= stderr
;
1019 minfo ("NULL TREE\n");
1023 switch (tree
->type
.node_class
)
1026 minfo ("0x%v", tree
->value
.value
);
1029 if (tree
->rel
.section
->owner
!= NULL
)
1030 minfo ("%B:", tree
->rel
.section
->owner
);
1031 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1034 fputs (tree
->assign
.dst
, config
.map_file
);
1035 exp_print_token (tree
->type
.node_code
, TRUE
);
1036 exp_print_tree (tree
->assign
.src
);
1039 case etree_provided
:
1040 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
1041 exp_print_tree (tree
->assign
.src
);
1042 fputc (')', config
.map_file
);
1045 function_like
= FALSE
;
1046 switch (tree
->type
.node_code
)
1051 case DATA_SEGMENT_ALIGN
:
1052 case DATA_SEGMENT_RELRO_END
:
1053 function_like
= TRUE
;
1057 exp_print_token (tree
->type
.node_code
, FALSE
);
1058 fputc (' ', config
.map_file
);
1060 fputc ('(', config
.map_file
);
1061 exp_print_tree (tree
->binary
.lhs
);
1063 fprintf (config
.map_file
, ", ");
1065 exp_print_token (tree
->type
.node_code
, TRUE
);
1066 exp_print_tree (tree
->binary
.rhs
);
1067 fputc (')', config
.map_file
);
1070 exp_print_tree (tree
->trinary
.cond
);
1071 fputc ('?', config
.map_file
);
1072 exp_print_tree (tree
->trinary
.lhs
);
1073 fputc (':', config
.map_file
);
1074 exp_print_tree (tree
->trinary
.rhs
);
1077 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1078 if (tree
->unary
.child
)
1080 fprintf (config
.map_file
, " (");
1081 exp_print_tree (tree
->unary
.child
);
1082 fputc (')', config
.map_file
);
1087 fprintf (config
.map_file
, "ASSERT (");
1088 exp_print_tree (tree
->assert_s
.child
);
1089 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1093 if (tree
->type
.node_code
== NAME
)
1094 fputs (tree
->name
.name
, config
.map_file
);
1097 exp_print_token (tree
->type
.node_code
, FALSE
);
1098 if (tree
->name
.name
)
1099 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1109 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1113 exp_fold_tree_no_dot (tree
);
1114 if (expld
.result
.valid_p
)
1115 return expld
.result
.value
;
1116 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1117 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1123 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1125 return exp_get_vma (tree
, def
, name
);
1129 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1138 exp_fold_tree_no_dot (tree
);
1139 if (!expld
.result
.valid_p
)
1141 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1142 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1146 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1150 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1151 fill
->size
= (len
+ 1) / 2;
1153 s
= (unsigned char *) expld
.result
.str
;
1161 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1175 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1176 val
= expld
.result
.value
;
1177 fill
->data
[0] = (val
>> 24) & 0xff;
1178 fill
->data
[1] = (val
>> 16) & 0xff;
1179 fill
->data
[2] = (val
>> 8) & 0xff;
1180 fill
->data
[3] = (val
>> 0) & 0xff;
1187 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1191 exp_fold_tree_no_dot (tree
);
1193 if (expld
.result
.valid_p
)
1195 if (expld
.result
.section
!= NULL
)
1196 expld
.result
.value
+= expld
.result
.section
->vma
;
1197 return expld
.result
.value
;
1199 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1201 lineno
= tree
->type
.lineno
;
1202 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1209 align_n (bfd_vma value
, bfd_vma align
)
1214 value
= (value
+ align
- 1) / align
;
1215 return value
* align
;