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, 2012
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
.filename
= ldlex_filename ();
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
.filename
= ldlex_filename ();
173 new_e
->type
.lineno
= lineno
;
174 new_e
->value
.value
= value
;
175 new_e
->value
.str
= str
;
176 new_e
->type
.node_class
= etree_value
;
180 /* Build an expression representing an unnamed relocatable value. */
183 exp_relop (asection
*section
, bfd_vma value
)
185 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
186 new_e
->type
.node_code
= REL
;
187 new_e
->type
.filename
= ldlex_filename ();
188 new_e
->type
.lineno
= lineno
;
189 new_e
->type
.node_class
= etree_rel
;
190 new_e
->rel
.section
= section
;
191 new_e
->rel
.value
= value
;
196 new_number (bfd_vma value
)
198 expld
.result
.valid_p
= TRUE
;
199 expld
.result
.value
= value
;
200 expld
.result
.str
= NULL
;
201 expld
.result
.section
= NULL
;
205 new_rel (bfd_vma value
, asection
*section
)
207 expld
.result
.valid_p
= TRUE
;
208 expld
.result
.value
= value
;
209 expld
.result
.str
= NULL
;
210 expld
.result
.section
= section
;
214 new_rel_from_abs (bfd_vma value
)
216 expld
.result
.valid_p
= TRUE
;
217 expld
.result
.value
= value
- expld
.section
->vma
;
218 expld
.result
.str
= NULL
;
219 expld
.result
.section
= expld
.section
;
223 fold_unary (etree_type
*tree
)
225 exp_fold_tree_1 (tree
->unary
.child
);
226 if (expld
.result
.valid_p
)
228 switch (tree
->type
.node_code
)
231 if (expld
.phase
!= lang_first_phase_enum
)
232 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
234 expld
.result
.valid_p
= FALSE
;
242 expld
.result
.value
= ~expld
.result
.value
;
246 expld
.result
.value
= !expld
.result
.value
;
250 expld
.result
.value
= -expld
.result
.value
;
254 /* Return next place aligned to value. */
255 if (expld
.phase
!= lang_first_phase_enum
)
258 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
261 expld
.result
.valid_p
= FALSE
;
264 case DATA_SEGMENT_END
:
265 if (expld
.phase
== lang_first_phase_enum
266 || expld
.section
!= bfd_abs_section_ptr
)
268 expld
.result
.valid_p
= FALSE
;
270 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
271 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
273 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
274 expld
.dataseg
.end
= expld
.result
.value
;
276 else if (expld
.dataseg
.phase
== exp_dataseg_done
277 || expld
.dataseg
.phase
== exp_dataseg_adjust
278 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
283 expld
.result
.valid_p
= FALSE
;
294 fold_binary (etree_type
*tree
)
296 etree_value_type lhs
;
297 exp_fold_tree_1 (tree
->binary
.lhs
);
299 /* The SEGMENT_START operator is special because its first
300 operand is a string, not the name of a symbol. Note that the
301 operands have been swapped, so binary.lhs is second (default)
302 operand, binary.rhs is first operand. */
303 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
305 const char *segment_name
;
308 /* Check to see if the user has overridden the default
310 segment_name
= tree
->binary
.rhs
->name
.name
;
311 for (seg
= segments
; seg
; seg
= seg
->next
)
312 if (strcmp (seg
->name
, segment_name
) == 0)
315 && config
.magic_demand_paged
316 && (seg
->value
% config
.maxpagesize
) != 0)
317 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
320 new_rel_from_abs (seg
->value
);
327 exp_fold_tree_1 (tree
->binary
.rhs
);
328 expld
.result
.valid_p
&= lhs
.valid_p
;
330 if (expld
.result
.valid_p
)
332 if (lhs
.section
!= expld
.result
.section
)
334 /* If the values are from different sections, and neither is
335 just a number, make both the source arguments absolute. */
336 if (expld
.result
.section
!= NULL
337 && lhs
.section
!= NULL
)
340 lhs
.value
+= lhs
.section
->vma
;
341 lhs
.section
= bfd_abs_section_ptr
;
344 /* If the rhs is just a number, keep the lhs section. */
345 else if (expld
.result
.section
== NULL
)
347 expld
.result
.section
= lhs
.section
;
348 /* Make this NULL so that we know one of the operands
349 was just a number, for later tests. */
353 /* At this point we know that both operands have the same
354 section, or at least one of them is a plain number. */
356 switch (tree
->type
.node_code
)
358 /* Arithmetic operators, bitwise AND, bitwise OR and XOR
359 keep the section of one of their operands only when the
360 other operand is a plain number. Losing the section when
361 operating on two symbols, ie. a result of a plain number,
362 is required for subtraction and XOR. It's justifiable
363 for the other operations on the grounds that adding,
364 multiplying etc. two section relative values does not
365 really make sense unless they are just treated as
367 The same argument could be made for many expressions
368 involving one symbol and a number. For example,
369 "1 << x" and "100 / x" probably should not be given the
370 section of x. The trouble is that if we fuss about such
371 things the rules become complex and it is onerous to
372 document ld expression evaluation. */
375 expld.result.value = lhs.value y expld.result.value; \
376 if (expld.result.section == lhs.section) \
377 expld.result.section = NULL; \
380 /* Comparison operators, logical AND, and logical OR always
381 return a plain number. */
384 expld.result.value = lhs.value y expld.result.value; \
385 expld.result.section = NULL; \
406 if (expld
.result
.value
!= 0)
407 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
408 % (bfd_signed_vma
) expld
.result
.value
);
409 else if (expld
.phase
!= lang_mark_phase_enum
)
410 einfo (_("%F%S %% by zero\n"), tree
->binary
.rhs
);
411 if (expld
.result
.section
== lhs
.section
)
412 expld
.result
.section
= NULL
;
416 if (expld
.result
.value
!= 0)
417 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
418 / (bfd_signed_vma
) expld
.result
.value
);
419 else if (expld
.phase
!= lang_mark_phase_enum
)
420 einfo (_("%F%S / by zero\n"), tree
->binary
.rhs
);
421 if (expld
.result
.section
== lhs
.section
)
422 expld
.result
.section
= NULL
;
426 if (lhs
.value
> expld
.result
.value
)
427 expld
.result
.value
= lhs
.value
;
431 if (lhs
.value
< expld
.result
.value
)
432 expld
.result
.value
= lhs
.value
;
436 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
439 case DATA_SEGMENT_ALIGN
:
440 expld
.dataseg
.relro
= exp_dataseg_relro_start
;
441 if (expld
.phase
== lang_first_phase_enum
442 || expld
.section
!= bfd_abs_section_ptr
)
443 expld
.result
.valid_p
= FALSE
;
446 bfd_vma maxpage
= lhs
.value
;
447 bfd_vma commonpage
= expld
.result
.value
;
449 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
450 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
451 expld
.result
.value
= expld
.dataseg
.base
;
452 else if (expld
.dataseg
.phase
== exp_dataseg_adjust
)
454 if (commonpage
< maxpage
)
455 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
456 & (maxpage
- commonpage
));
460 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
461 if (expld
.dataseg
.phase
== exp_dataseg_done
)
465 else if (expld
.dataseg
.phase
== exp_dataseg_none
)
467 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
468 expld
.dataseg
.min_base
= expld
.dot
;
469 expld
.dataseg
.base
= expld
.result
.value
;
470 expld
.dataseg
.pagesize
= commonpage
;
471 expld
.dataseg
.maxpagesize
= maxpage
;
472 expld
.dataseg
.relro_end
= 0;
475 expld
.result
.valid_p
= FALSE
;
480 case DATA_SEGMENT_RELRO_END
:
481 expld
.dataseg
.relro
= exp_dataseg_relro_end
;
482 if (expld
.phase
== lang_first_phase_enum
483 || expld
.section
!= bfd_abs_section_ptr
)
484 expld
.result
.valid_p
= FALSE
;
485 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
486 || expld
.dataseg
.phase
== exp_dataseg_adjust
487 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
488 || expld
.dataseg
.phase
== exp_dataseg_done
)
490 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
491 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
492 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
494 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
495 && (expld
.dataseg
.relro_end
496 & (expld
.dataseg
.pagesize
- 1)))
498 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
499 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
500 expld
.result
.value
= (expld
.dataseg
.relro_end
501 - expld
.result
.value
);
504 expld
.result
.value
= lhs
.value
;
506 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
507 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
510 expld
.result
.valid_p
= FALSE
;
520 fold_trinary (etree_type
*tree
)
522 exp_fold_tree_1 (tree
->trinary
.cond
);
523 if (expld
.result
.valid_p
)
524 exp_fold_tree_1 (expld
.result
.value
526 : tree
->trinary
.rhs
);
530 fold_name (etree_type
*tree
)
532 memset (&expld
.result
, 0, sizeof (expld
.result
));
534 switch (tree
->type
.node_code
)
537 if (expld
.phase
!= lang_first_phase_enum
)
539 bfd_vma hdr_size
= 0;
540 /* Don't find the real header size if only marking sections;
541 The bfd function may cache incorrect data. */
542 if (expld
.phase
!= lang_mark_phase_enum
)
543 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
544 new_number (hdr_size
);
549 if (expld
.phase
== lang_first_phase_enum
)
550 lang_track_definedness (tree
->name
.name
);
553 struct bfd_link_hash_entry
*h
;
555 = lang_symbol_definition_iteration (tree
->name
.name
);
557 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
561 new_number (h
!= NULL
562 && (h
->type
== bfd_link_hash_defined
563 || h
->type
== bfd_link_hash_defweak
564 || h
->type
== bfd_link_hash_common
)
565 && (def_iteration
== lang_statement_iteration
566 || def_iteration
== -1));
571 if (expld
.phase
== lang_first_phase_enum
)
573 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
574 new_rel_from_abs (expld
.dot
);
577 struct bfd_link_hash_entry
*h
;
579 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
584 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
585 else if (h
->type
== bfd_link_hash_defined
586 || h
->type
== bfd_link_hash_defweak
)
588 asection
*output_section
;
590 output_section
= h
->u
.def
.section
->output_section
;
591 if (output_section
== NULL
)
593 if (expld
.phase
!= lang_mark_phase_enum
)
594 einfo (_("%X%S: unresolvable symbol `%s'"
595 " referenced in expression\n"),
596 tree
, tree
->name
.name
);
598 else if (output_section
== bfd_abs_section_ptr
599 && (expld
.section
!= bfd_abs_section_ptr
600 || config
.sane_expr
))
601 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
603 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
606 else if (expld
.phase
== lang_final_phase_enum
607 || expld
.assigning_to_dot
)
608 einfo (_("%F%S: undefined symbol `%s'"
609 " referenced in expression\n"),
610 tree
, tree
->name
.name
);
611 else if (h
->type
== bfd_link_hash_new
)
613 h
->type
= bfd_link_hash_undefined
;
614 h
->u
.undef
.abfd
= NULL
;
615 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
616 bfd_link_add_undef (link_info
.hash
, h
);
622 if (expld
.phase
!= lang_first_phase_enum
)
624 lang_output_section_statement_type
*os
;
626 os
= lang_output_section_find (tree
->name
.name
);
629 if (expld
.phase
== lang_final_phase_enum
)
630 einfo (_("%F%S: undefined section `%s'"
631 " referenced in expression\n"),
632 tree
, tree
->name
.name
);
634 else if (os
->processed_vma
)
635 new_rel (0, os
->bfd_section
);
640 if (expld
.phase
!= lang_first_phase_enum
)
642 lang_output_section_statement_type
*os
;
644 os
= lang_output_section_find (tree
->name
.name
);
647 if (expld
.phase
== lang_final_phase_enum
)
648 einfo (_("%F%S: undefined section `%s'"
649 " referenced in expression\n"),
650 tree
, tree
->name
.name
);
652 else if (os
->processed_lma
)
654 if (os
->load_base
== NULL
)
655 new_abs (os
->bfd_section
->lma
);
658 exp_fold_tree_1 (os
->load_base
);
659 if (expld
.result
.valid_p
)
668 if (expld
.phase
!= lang_first_phase_enum
)
670 lang_output_section_statement_type
*os
;
672 os
= lang_output_section_find (tree
->name
.name
);
675 if (expld
.phase
== lang_final_phase_enum
)
676 einfo (_("%F%S: undefined section `%s'"
677 " referenced in expression\n"),
678 tree
, tree
->name
.name
);
681 else if (os
->processed_vma
)
685 if (tree
->type
.node_code
== SIZEOF
)
686 val
= (os
->bfd_section
->size
687 / bfd_octets_per_byte (link_info
.output_bfd
));
689 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
698 lang_memory_region_type
*mem
;
700 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
702 new_number (mem
->length
);
704 einfo (_("%F%S: undefined MEMORY region `%s'"
705 " referenced in expression\n"),
706 tree
, tree
->name
.name
);
711 if (expld
.phase
!= lang_first_phase_enum
)
713 lang_memory_region_type
*mem
;
715 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
717 new_rel_from_abs (mem
->origin
);
719 einfo (_("%F%S: undefined MEMORY region `%s'"
720 " referenced in expression\n"),
721 tree
, tree
->name
.name
);
726 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
727 new_number (config
.maxpagesize
);
728 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
729 new_number (config
.commonpagesize
);
731 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
732 tree
, tree
->name
.name
);
742 exp_fold_tree_1 (etree_type
*tree
)
746 memset (&expld
.result
, 0, sizeof (expld
.result
));
750 switch (tree
->type
.node_class
)
753 if (expld
.section
== bfd_abs_section_ptr
754 && !config
.sane_expr
)
755 new_abs (tree
->value
.value
);
757 new_number (tree
->value
.value
);
758 expld
.result
.str
= tree
->value
.str
;
762 if (expld
.phase
!= lang_first_phase_enum
)
764 asection
*output_section
= tree
->rel
.section
->output_section
;
765 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
769 memset (&expld
.result
, 0, sizeof (expld
.result
));
773 exp_fold_tree_1 (tree
->assert_s
.child
);
774 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
775 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
793 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
795 if (tree
->type
.node_class
!= etree_assign
)
796 einfo (_("%F%S can not PROVIDE assignment to"
797 " location counter\n"), tree
);
798 /* After allocation, assignment to dot should not be done inside
799 an output section since allocation adds a padding statement
800 that effectively duplicates the assignment. */
801 if (expld
.phase
== lang_mark_phase_enum
802 || expld
.phase
== lang_allocating_phase_enum
803 || ((expld
.phase
== lang_assigning_phase_enum
804 || expld
.phase
== lang_final_phase_enum
)
805 && expld
.section
== bfd_abs_section_ptr
))
807 /* Notify the folder that this is an assignment to dot. */
808 expld
.assigning_to_dot
= TRUE
;
809 exp_fold_tree_1 (tree
->assign
.src
);
810 expld
.assigning_to_dot
= FALSE
;
812 if (!expld
.result
.valid_p
)
814 if (expld
.phase
!= lang_mark_phase_enum
)
815 einfo (_("%F%S invalid assignment to"
816 " location counter\n"), tree
);
818 else if (expld
.dotp
== NULL
)
819 einfo (_("%F%S assignment to location counter"
820 " invalid outside of SECTION\n"), tree
);
825 nextdot
= expld
.result
.value
;
826 if (expld
.result
.section
!= NULL
)
827 nextdot
+= expld
.result
.section
->vma
;
829 nextdot
+= expld
.section
->vma
;
830 if (nextdot
< expld
.dot
831 && expld
.section
!= bfd_abs_section_ptr
)
832 einfo (_("%F%S cannot move location counter backwards"
833 " (from %V to %V)\n"),
834 tree
, expld
.dot
, nextdot
);
838 *expld
.dotp
= nextdot
;
843 memset (&expld
.result
, 0, sizeof (expld
.result
));
849 struct bfd_link_hash_entry
*h
= NULL
;
851 if (tree
->type
.node_class
== etree_provide
)
853 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
856 || (h
->type
!= bfd_link_hash_new
857 && h
->type
!= bfd_link_hash_undefined
858 && h
->type
!= bfd_link_hash_common
))
860 /* Do nothing. The symbol was never referenced, or was
861 defined by some object. */
866 name
= tree
->assign
.src
;
867 if (name
->type
.node_class
== etree_trinary
)
869 exp_fold_tree_1 (name
->trinary
.cond
);
870 if (expld
.result
.valid_p
)
871 name
= (expld
.result
.value
872 ? name
->trinary
.lhs
: name
->trinary
.rhs
);
875 if (name
->type
.node_class
== etree_name
876 && name
->type
.node_code
== NAME
877 && strcmp (tree
->assign
.dst
, name
->name
.name
) == 0)
878 /* Leave it alone. Do not replace a symbol with its own
879 output address, in case there is another section sizing
880 pass. Folding does not preserve input sections. */
883 exp_fold_tree_1 (tree
->assign
.src
);
884 if (expld
.result
.valid_p
885 || (expld
.phase
== lang_first_phase_enum
886 && tree
->type
.node_class
== etree_assign
887 && tree
->assign
.hidden
))
891 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
894 einfo (_("%P%F:%s: hash creation failed\n"),
898 /* FIXME: Should we worry if the symbol is already
900 lang_update_definedness (tree
->assign
.dst
, h
);
901 h
->type
= bfd_link_hash_defined
;
902 h
->u
.def
.value
= expld
.result
.value
;
903 if (expld
.result
.section
== NULL
)
904 expld
.result
.section
= expld
.section
;
905 h
->u
.def
.section
= expld
.result
.section
;
906 if (tree
->type
.node_class
== etree_provide
)
907 tree
->type
.node_class
= etree_provided
;
909 /* Copy the symbol type if this is a simple assignment of
910 one symbol to another. This could be more general
911 (e.g. a ?: operator with NAMEs in each branch). */
912 if (tree
->assign
.src
->type
.node_class
== etree_name
)
914 struct bfd_link_hash_entry
*hsrc
;
916 hsrc
= bfd_link_hash_lookup (link_info
.hash
,
917 tree
->assign
.src
->name
.name
,
920 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
, h
,
924 else if (expld
.phase
== lang_final_phase_enum
)
926 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
929 && h
->type
== bfd_link_hash_new
)
930 h
->type
= bfd_link_hash_undefined
;
941 memset (&expld
.result
, 0, sizeof (expld
.result
));
947 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
951 expld
.section
= current_section
;
952 exp_fold_tree_1 (tree
);
956 exp_fold_tree_no_dot (etree_type
*tree
)
960 expld
.section
= bfd_abs_section_ptr
;
961 exp_fold_tree_1 (tree
);
965 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
967 etree_type value
, *new_e
;
969 value
.type
.node_code
= code
;
970 value
.type
.filename
= lhs
->type
.filename
;
971 value
.type
.lineno
= lhs
->type
.lineno
;
972 value
.binary
.lhs
= lhs
;
973 value
.binary
.rhs
= rhs
;
974 value
.type
.node_class
= etree_binary
;
975 exp_fold_tree_no_dot (&value
);
976 if (expld
.result
.valid_p
)
977 return exp_intop (expld
.result
.value
);
979 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->binary
));
980 memcpy (new_e
, &value
, sizeof (new_e
->binary
));
985 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
987 etree_type value
, *new_e
;
989 value
.type
.node_code
= code
;
990 value
.type
.filename
= cond
->type
.filename
;
991 value
.type
.lineno
= cond
->type
.lineno
;
992 value
.trinary
.lhs
= lhs
;
993 value
.trinary
.cond
= cond
;
994 value
.trinary
.rhs
= rhs
;
995 value
.type
.node_class
= etree_trinary
;
996 exp_fold_tree_no_dot (&value
);
997 if (expld
.result
.valid_p
)
998 return exp_intop (expld
.result
.value
);
1000 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->trinary
));
1001 memcpy (new_e
, &value
, sizeof (new_e
->trinary
));
1006 exp_unop (int code
, etree_type
*child
)
1008 etree_type value
, *new_e
;
1010 value
.unary
.type
.node_code
= code
;
1011 value
.unary
.type
.filename
= child
->type
.filename
;
1012 value
.unary
.type
.lineno
= child
->type
.lineno
;
1013 value
.unary
.child
= child
;
1014 value
.unary
.type
.node_class
= etree_unary
;
1015 exp_fold_tree_no_dot (&value
);
1016 if (expld
.result
.valid_p
)
1017 return exp_intop (expld
.result
.value
);
1019 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->unary
));
1020 memcpy (new_e
, &value
, sizeof (new_e
->unary
));
1025 exp_nameop (int code
, const char *name
)
1027 etree_type value
, *new_e
;
1029 value
.name
.type
.node_code
= code
;
1030 value
.name
.type
.filename
= ldlex_filename ();
1031 value
.name
.type
.lineno
= lineno
;
1032 value
.name
.name
= name
;
1033 value
.name
.type
.node_class
= etree_name
;
1035 exp_fold_tree_no_dot (&value
);
1036 if (expld
.result
.valid_p
)
1037 return exp_intop (expld
.result
.value
);
1039 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
1040 memcpy (new_e
, &value
, sizeof (new_e
->name
));
1046 exp_assop (const char *dst
,
1048 enum node_tree_enum
class,
1053 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
1054 n
->assign
.type
.node_code
= '=';
1055 n
->assign
.type
.filename
= src
->type
.filename
;
1056 n
->assign
.type
.lineno
= src
->type
.lineno
;
1057 n
->assign
.type
.node_class
= class;
1058 n
->assign
.src
= src
;
1059 n
->assign
.dst
= dst
;
1060 n
->assign
.hidden
= hidden
;
1065 exp_assign (const char *dst
, etree_type
*src
)
1067 return exp_assop (dst
, src
, etree_assign
, FALSE
);
1071 exp_defsym (const char *dst
, etree_type
*src
)
1073 return exp_assop (dst
, src
, etree_assign
, TRUE
);
1076 /* Handle PROVIDE. */
1079 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1081 return exp_assop (dst
, src
, etree_provide
, hidden
);
1084 /* Handle ASSERT. */
1087 exp_assert (etree_type
*exp
, const char *message
)
1091 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1092 n
->assert_s
.type
.node_code
= '!';
1093 n
->assert_s
.type
.filename
= exp
->type
.filename
;
1094 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1095 n
->assert_s
.type
.node_class
= etree_assert
;
1096 n
->assert_s
.child
= exp
;
1097 n
->assert_s
.message
= message
;
1102 exp_print_tree (etree_type
*tree
)
1104 bfd_boolean function_like
;
1106 if (config
.map_file
== NULL
)
1107 config
.map_file
= stderr
;
1111 minfo ("NULL TREE\n");
1115 switch (tree
->type
.node_class
)
1118 minfo ("0x%v", tree
->value
.value
);
1121 if (tree
->rel
.section
->owner
!= NULL
)
1122 minfo ("%B:", tree
->rel
.section
->owner
);
1123 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1126 fputs (tree
->assign
.dst
, config
.map_file
);
1127 exp_print_token (tree
->type
.node_code
, TRUE
);
1128 exp_print_tree (tree
->assign
.src
);
1131 case etree_provided
:
1132 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
1133 exp_print_tree (tree
->assign
.src
);
1134 fputc (')', config
.map_file
);
1137 function_like
= FALSE
;
1138 switch (tree
->type
.node_code
)
1143 case DATA_SEGMENT_ALIGN
:
1144 case DATA_SEGMENT_RELRO_END
:
1145 function_like
= TRUE
;
1149 exp_print_token (tree
->type
.node_code
, FALSE
);
1150 fputc (' ', config
.map_file
);
1152 fputc ('(', config
.map_file
);
1153 exp_print_tree (tree
->binary
.lhs
);
1155 fprintf (config
.map_file
, ", ");
1157 exp_print_token (tree
->type
.node_code
, TRUE
);
1158 exp_print_tree (tree
->binary
.rhs
);
1159 fputc (')', config
.map_file
);
1162 exp_print_tree (tree
->trinary
.cond
);
1163 fputc ('?', config
.map_file
);
1164 exp_print_tree (tree
->trinary
.lhs
);
1165 fputc (':', config
.map_file
);
1166 exp_print_tree (tree
->trinary
.rhs
);
1169 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1170 if (tree
->unary
.child
)
1172 fprintf (config
.map_file
, " (");
1173 exp_print_tree (tree
->unary
.child
);
1174 fputc (')', config
.map_file
);
1179 fprintf (config
.map_file
, "ASSERT (");
1180 exp_print_tree (tree
->assert_s
.child
);
1181 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1185 if (tree
->type
.node_code
== NAME
)
1186 fputs (tree
->name
.name
, config
.map_file
);
1189 exp_print_token (tree
->type
.node_code
, FALSE
);
1190 if (tree
->name
.name
)
1191 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1201 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1205 exp_fold_tree_no_dot (tree
);
1206 if (expld
.result
.valid_p
)
1207 return expld
.result
.value
;
1208 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1209 einfo (_("%F%S: nonconstant expression for %s\n"),
1216 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1218 return exp_get_vma (tree
, def
, name
);
1222 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1231 exp_fold_tree_no_dot (tree
);
1232 if (!expld
.result
.valid_p
)
1234 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1235 einfo (_("%F%S: nonconstant expression for %s\n"),
1240 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1244 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1245 fill
->size
= (len
+ 1) / 2;
1247 s
= (unsigned char *) expld
.result
.str
;
1255 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1269 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1270 val
= expld
.result
.value
;
1271 fill
->data
[0] = (val
>> 24) & 0xff;
1272 fill
->data
[1] = (val
>> 16) & 0xff;
1273 fill
->data
[2] = (val
>> 8) & 0xff;
1274 fill
->data
[3] = (val
>> 0) & 0xff;
1281 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1285 exp_fold_tree_no_dot (tree
);
1287 if (expld
.result
.valid_p
)
1289 if (expld
.result
.section
!= NULL
)
1290 expld
.result
.value
+= expld
.result
.section
->vma
;
1291 return expld
.result
.value
;
1293 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1295 einfo (_("%F%S: nonconstant expression for %s\n"),
1303 align_n (bfd_vma value
, bfd_vma align
)
1308 value
= (value
+ align
- 1) / align
;
1309 return value
* align
;