1 /* This module handles expression trees.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
5 This file is part of the GNU Binutils.
7 This program 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 3 of the License, or
10 (at your option) any later version.
12 This program 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* This module is in charge of working out the contents of expressions.
25 It has to keep track of the relative/absness of a symbol etc. This
26 is done by keeping all values in a struct (an etree_value_type)
27 which contains a value, a section to which it is relative and a
41 #include "libiberty.h"
42 #include "safe-ctype.h"
44 static void exp_fold_tree_1 (etree_type
*);
45 static bfd_vma
align_n (bfd_vma
, bfd_vma
);
47 segment_type
*segments
;
49 struct ldexp_control expld
;
51 /* This structure records symbols for which we need to keep track of
52 definedness for use in the DEFINED () test. */
54 struct definedness_hash_entry
56 struct bfd_hash_entry root
;
57 unsigned int by_object
: 1;
58 unsigned int by_script
: 1;
59 unsigned int iteration
: 1;
62 static struct bfd_hash_table definedness_table
;
64 /* Print the string representation of the given token. Surround it
65 with spaces if INFIX_P is TRUE. */
68 exp_print_token (token_code_type code
, int infix_p
)
95 { LOG2CEIL
, "LOG2CEIL" },
103 { SECTIONS
, "SECTIONS" },
104 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
105 { MEMORY
, "MEMORY" },
106 { DEFINED
, "DEFINED" },
107 { TARGET_K
, "TARGET" },
108 { SEARCH_DIR
, "SEARCH_DIR" },
112 { ALIGNOF
, "ALIGNOF" },
113 { SIZEOF
, "SIZEOF" },
115 { LOADADDR
, "LOADADDR" },
116 { CONSTANT
, "CONSTANT" },
117 { ABSOLUTE
, "ABSOLUTE" },
120 { ASSERT_K
, "ASSERT" },
121 { REL
, "relocatable" },
122 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
123 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
124 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
125 { ORIGIN
, "ORIGIN" },
126 { LENGTH
, "LENGTH" },
127 { SEGMENT_START
, "SEGMENT_START" }
131 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
132 if (table
[idx
].code
== code
)
136 fputc (' ', config
.map_file
);
138 if (idx
< ARRAY_SIZE (table
))
139 fputs (table
[idx
].name
, config
.map_file
);
141 fputc (code
, config
.map_file
);
143 fprintf (config
.map_file
, "<code %d>", code
);
146 fputc (' ', config
.map_file
);
152 bfd_vma value
= expld
.result
.value
;
154 bfd_boolean round_up
= FALSE
;
159 /* If more than one bit is set in the value we will need to round up. */
160 if ((value
> 1) && (value
& 1))
167 expld
.result
.section
= NULL
;
168 expld
.result
.value
= result
;
174 if (expld
.result
.section
!= NULL
)
175 expld
.result
.value
+= expld
.result
.section
->vma
;
176 expld
.result
.section
= bfd_abs_section_ptr
;
180 new_abs (bfd_vma value
)
182 expld
.result
.valid_p
= TRUE
;
183 expld
.result
.section
= bfd_abs_section_ptr
;
184 expld
.result
.value
= value
;
185 expld
.result
.str
= NULL
;
189 exp_intop (bfd_vma value
)
191 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
192 new_e
->type
.node_code
= INT
;
193 new_e
->type
.filename
= ldlex_filename ();
194 new_e
->type
.lineno
= lineno
;
195 new_e
->value
.value
= value
;
196 new_e
->value
.str
= NULL
;
197 new_e
->type
.node_class
= etree_value
;
202 exp_bigintop (bfd_vma value
, char *str
)
204 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
205 new_e
->type
.node_code
= INT
;
206 new_e
->type
.filename
= ldlex_filename ();
207 new_e
->type
.lineno
= lineno
;
208 new_e
->value
.value
= value
;
209 new_e
->value
.str
= str
;
210 new_e
->type
.node_class
= etree_value
;
214 /* Build an expression representing an unnamed relocatable value. */
217 exp_relop (asection
*section
, bfd_vma value
)
219 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
220 new_e
->type
.node_code
= REL
;
221 new_e
->type
.filename
= ldlex_filename ();
222 new_e
->type
.lineno
= lineno
;
223 new_e
->type
.node_class
= etree_rel
;
224 new_e
->rel
.section
= section
;
225 new_e
->rel
.value
= value
;
230 new_number (bfd_vma value
)
232 expld
.result
.valid_p
= TRUE
;
233 expld
.result
.value
= value
;
234 expld
.result
.str
= NULL
;
235 expld
.result
.section
= NULL
;
239 new_rel (bfd_vma value
, asection
*section
)
241 expld
.result
.valid_p
= TRUE
;
242 expld
.result
.value
= value
;
243 expld
.result
.str
= NULL
;
244 expld
.result
.section
= section
;
248 new_rel_from_abs (bfd_vma value
)
250 asection
*s
= expld
.section
;
252 if (s
== bfd_abs_section_ptr
&& expld
.phase
== lang_final_phase_enum
)
253 s
= section_for_dot ();
254 expld
.result
.valid_p
= TRUE
;
255 expld
.result
.value
= value
- s
->vma
;
256 expld
.result
.str
= NULL
;
257 expld
.result
.section
= s
;
261 align_dot_val (bfd_vma align
)
263 bfd_vma base
= expld
.section
->vma
;
265 new_rel_from_abs (base
+ align_n (expld
.dot
- base
, align
));
268 /* New-function for the definedness hash table. */
270 static struct bfd_hash_entry
*
271 definedness_newfunc (struct bfd_hash_entry
*entry
,
272 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
273 const char *name ATTRIBUTE_UNUSED
)
275 struct definedness_hash_entry
*ret
= (struct definedness_hash_entry
*) entry
;
278 ret
= (struct definedness_hash_entry
*)
279 bfd_hash_allocate (table
, sizeof (struct definedness_hash_entry
));
282 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
290 /* Called during processing of linker script script expressions.
291 For symbols assigned in a linker script, return a struct describing
292 where the symbol is defined relative to the current expression,
293 otherwise return NULL. */
295 static struct definedness_hash_entry
*
296 symbol_defined (const char *name
)
298 return ((struct definedness_hash_entry
*)
299 bfd_hash_lookup (&definedness_table
, name
, FALSE
, FALSE
));
302 /* Update the definedness state of NAME. Return FALSE if script symbol
303 is multiply defining a strong symbol in an object. */
306 update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
309 struct definedness_hash_entry
*defentry
310 = (struct definedness_hash_entry
*)
311 bfd_hash_lookup (&definedness_table
, name
, TRUE
, FALSE
);
313 if (defentry
== NULL
)
314 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
316 /* If the symbol was already defined, and not by a script, then it
317 must be defined by an object file or by the linker target code. */
319 if (!defentry
->by_script
320 && (h
->type
== bfd_link_hash_defined
321 || h
->type
== bfd_link_hash_defweak
322 || h
->type
== bfd_link_hash_common
))
324 defentry
->by_object
= 1;
325 if (h
->type
== bfd_link_hash_defined
326 && h
->u
.def
.section
->output_section
!= NULL
331 defentry
->by_script
= 1;
332 defentry
->iteration
= lang_statement_iteration
;
337 fold_unary (etree_type
*tree
)
339 exp_fold_tree_1 (tree
->unary
.child
);
340 if (expld
.result
.valid_p
)
342 switch (tree
->type
.node_code
)
345 if (expld
.phase
!= lang_first_phase_enum
)
346 align_dot_val (expld
.result
.value
);
348 expld
.result
.valid_p
= FALSE
;
360 expld
.result
.value
= ~expld
.result
.value
;
364 expld
.result
.value
= !expld
.result
.value
;
368 expld
.result
.value
= -expld
.result
.value
;
372 /* Return next place aligned to value. */
373 if (expld
.phase
!= lang_first_phase_enum
)
376 align_dot_val (expld
.result
.value
);
379 expld
.result
.valid_p
= FALSE
;
382 case DATA_SEGMENT_END
:
383 if (expld
.phase
== lang_first_phase_enum
384 || expld
.section
!= bfd_abs_section_ptr
)
386 expld
.result
.valid_p
= FALSE
;
388 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
389 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
391 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
392 expld
.dataseg
.end
= expld
.result
.value
;
394 else if (expld
.dataseg
.phase
== exp_dataseg_done
395 || expld
.dataseg
.phase
== exp_dataseg_adjust
396 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
401 expld
.result
.valid_p
= FALSE
;
412 fold_binary (etree_type
*tree
)
414 etree_value_type lhs
;
415 exp_fold_tree_1 (tree
->binary
.lhs
);
417 /* The SEGMENT_START operator is special because its first
418 operand is a string, not the name of a symbol. Note that the
419 operands have been swapped, so binary.lhs is second (default)
420 operand, binary.rhs is first operand. */
421 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
423 const char *segment_name
;
426 /* Check to see if the user has overridden the default
428 segment_name
= tree
->binary
.rhs
->name
.name
;
429 for (seg
= segments
; seg
; seg
= seg
->next
)
430 if (strcmp (seg
->name
, segment_name
) == 0)
433 && config
.magic_demand_paged
434 && (seg
->value
% config
.maxpagesize
) != 0)
435 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
438 new_rel_from_abs (seg
->value
);
445 exp_fold_tree_1 (tree
->binary
.rhs
);
446 expld
.result
.valid_p
&= lhs
.valid_p
;
448 if (expld
.result
.valid_p
)
450 if (lhs
.section
!= expld
.result
.section
)
452 /* If the values are from different sections, and neither is
453 just a number, make both the source arguments absolute. */
454 if (expld
.result
.section
!= NULL
455 && lhs
.section
!= NULL
)
458 lhs
.value
+= lhs
.section
->vma
;
459 lhs
.section
= bfd_abs_section_ptr
;
462 /* If the rhs is just a number, keep the lhs section. */
463 else if (expld
.result
.section
== NULL
)
465 expld
.result
.section
= lhs
.section
;
466 /* Make this NULL so that we know one of the operands
467 was just a number, for later tests. */
471 /* At this point we know that both operands have the same
472 section, or at least one of them is a plain number. */
474 switch (tree
->type
.node_code
)
476 /* Arithmetic operators, bitwise AND, bitwise OR and XOR
477 keep the section of one of their operands only when the
478 other operand is a plain number. Losing the section when
479 operating on two symbols, ie. a result of a plain number,
480 is required for subtraction and XOR. It's justifiable
481 for the other operations on the grounds that adding,
482 multiplying etc. two section relative values does not
483 really make sense unless they are just treated as
485 The same argument could be made for many expressions
486 involving one symbol and a number. For example,
487 "1 << x" and "100 / x" probably should not be given the
488 section of x. The trouble is that if we fuss about such
489 things the rules become complex and it is onerous to
490 document ld expression evaluation. */
493 expld.result.value = lhs.value y expld.result.value; \
494 if (expld.result.section == lhs.section) \
495 expld.result.section = NULL; \
498 /* Comparison operators, logical AND, and logical OR always
499 return a plain number. */
502 expld.result.value = lhs.value y expld.result.value; \
503 expld.result.section = NULL; \
524 if (expld
.result
.value
!= 0)
525 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
526 % (bfd_signed_vma
) expld
.result
.value
);
527 else if (expld
.phase
!= lang_mark_phase_enum
)
528 einfo (_("%F%S %% by zero\n"), tree
->binary
.rhs
);
529 if (expld
.result
.section
== lhs
.section
)
530 expld
.result
.section
= NULL
;
534 if (expld
.result
.value
!= 0)
535 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
536 / (bfd_signed_vma
) expld
.result
.value
);
537 else if (expld
.phase
!= lang_mark_phase_enum
)
538 einfo (_("%F%S / by zero\n"), tree
->binary
.rhs
);
539 if (expld
.result
.section
== lhs
.section
)
540 expld
.result
.section
= NULL
;
544 if (lhs
.value
> expld
.result
.value
)
545 expld
.result
.value
= lhs
.value
;
549 if (lhs
.value
< expld
.result
.value
)
550 expld
.result
.value
= lhs
.value
;
554 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
557 case DATA_SEGMENT_ALIGN
:
558 expld
.dataseg
.relro
= exp_dataseg_relro_start
;
559 if (expld
.phase
== lang_first_phase_enum
560 || expld
.section
!= bfd_abs_section_ptr
)
561 expld
.result
.valid_p
= FALSE
;
564 bfd_vma maxpage
= lhs
.value
;
565 bfd_vma commonpage
= expld
.result
.value
;
567 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
568 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
569 expld
.result
.value
= expld
.dataseg
.base
;
570 else if (expld
.dataseg
.phase
== exp_dataseg_adjust
)
572 if (commonpage
< maxpage
)
573 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
574 & (maxpage
- commonpage
));
578 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
579 if (expld
.dataseg
.phase
== exp_dataseg_done
)
583 else if (expld
.dataseg
.phase
== exp_dataseg_none
)
585 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
586 expld
.dataseg
.base
= expld
.result
.value
;
587 expld
.dataseg
.pagesize
= commonpage
;
588 expld
.dataseg
.maxpagesize
= maxpage
;
589 expld
.dataseg
.relro_end
= 0;
592 expld
.result
.valid_p
= FALSE
;
597 case DATA_SEGMENT_RELRO_END
:
598 /* Operands swapped! DATA_SEGMENT_RELRO_END(offset,exp)
599 has offset in expld.result and exp in lhs. */
600 expld
.dataseg
.relro
= exp_dataseg_relro_end
;
601 expld
.dataseg
.relro_offset
= expld
.result
.value
;
602 if (expld
.phase
== lang_first_phase_enum
603 || expld
.section
!= bfd_abs_section_ptr
)
604 expld
.result
.valid_p
= FALSE
;
605 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
606 || expld
.dataseg
.phase
== exp_dataseg_adjust
607 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
608 || expld
.dataseg
.phase
== exp_dataseg_done
)
610 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
611 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
612 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
614 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
615 && (expld
.dataseg
.relro_end
616 & (expld
.dataseg
.pagesize
- 1)))
618 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
619 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
620 expld
.result
.value
= (expld
.dataseg
.relro_end
621 - expld
.result
.value
);
624 expld
.result
.value
= lhs
.value
;
626 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
627 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
630 expld
.result
.valid_p
= FALSE
;
640 fold_trinary (etree_type
*tree
)
642 exp_fold_tree_1 (tree
->trinary
.cond
);
643 if (expld
.result
.valid_p
)
644 exp_fold_tree_1 (expld
.result
.value
646 : tree
->trinary
.rhs
);
650 fold_name (etree_type
*tree
)
652 memset (&expld
.result
, 0, sizeof (expld
.result
));
654 switch (tree
->type
.node_code
)
657 if (expld
.phase
!= lang_first_phase_enum
)
659 bfd_vma hdr_size
= 0;
660 /* Don't find the real header size if only marking sections;
661 The bfd function may cache incorrect data. */
662 if (expld
.phase
!= lang_mark_phase_enum
)
663 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
664 new_number (hdr_size
);
669 if (expld
.phase
!= lang_first_phase_enum
)
671 struct bfd_link_hash_entry
*h
;
672 struct definedness_hash_entry
*def
;
674 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
678 new_number (h
!= NULL
679 && (h
->type
== bfd_link_hash_defined
680 || h
->type
== bfd_link_hash_defweak
681 || h
->type
== bfd_link_hash_common
)
682 && ((def
= symbol_defined (tree
->name
.name
)) == NULL
684 || def
->iteration
== (lang_statement_iteration
& 1)));
689 if (expld
.assign_name
!= NULL
690 && strcmp (expld
.assign_name
, tree
->name
.name
) == 0)
692 /* Self-assignment is only allowed for absolute symbols
693 defined in a linker script. */
694 struct bfd_link_hash_entry
*h
;
695 struct definedness_hash_entry
*def
;
697 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
702 && (h
->type
== bfd_link_hash_defined
703 || h
->type
== bfd_link_hash_defweak
)
704 && h
->u
.def
.section
== bfd_abs_section_ptr
705 && (def
= symbol_defined (tree
->name
.name
)) != NULL
706 && def
->iteration
== (lang_statement_iteration
& 1)))
707 expld
.assign_name
= NULL
;
709 if (expld
.phase
== lang_first_phase_enum
)
711 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
712 new_rel_from_abs (expld
.dot
);
715 struct bfd_link_hash_entry
*h
;
717 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
722 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
723 else if (h
->type
== bfd_link_hash_defined
724 || h
->type
== bfd_link_hash_defweak
)
726 asection
*output_section
;
728 output_section
= h
->u
.def
.section
->output_section
;
729 if (output_section
== NULL
)
731 if (expld
.phase
== lang_mark_phase_enum
)
732 new_rel (h
->u
.def
.value
, h
->u
.def
.section
);
734 einfo (_("%X%S: unresolvable symbol `%s'"
735 " referenced in expression\n"),
736 tree
, tree
->name
.name
);
738 else if (output_section
== bfd_abs_section_ptr
739 && (expld
.section
!= bfd_abs_section_ptr
740 || config
.sane_expr
))
741 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
743 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
746 else if (expld
.phase
== lang_final_phase_enum
747 || (expld
.phase
!= lang_mark_phase_enum
748 && expld
.assigning_to_dot
))
749 einfo (_("%F%S: undefined symbol `%s'"
750 " referenced in expression\n"),
751 tree
, tree
->name
.name
);
752 else if (h
->type
== bfd_link_hash_new
)
754 h
->type
= bfd_link_hash_undefined
;
755 h
->u
.undef
.abfd
= NULL
;
756 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
757 bfd_link_add_undef (link_info
.hash
, h
);
763 if (expld
.phase
!= lang_first_phase_enum
)
765 lang_output_section_statement_type
*os
;
767 os
= lang_output_section_find (tree
->name
.name
);
770 if (expld
.phase
== lang_final_phase_enum
)
771 einfo (_("%F%S: undefined section `%s'"
772 " referenced in expression\n"),
773 tree
, tree
->name
.name
);
775 else if (os
->processed_vma
)
776 new_rel (0, os
->bfd_section
);
781 if (expld
.phase
!= lang_first_phase_enum
)
783 lang_output_section_statement_type
*os
;
785 os
= lang_output_section_find (tree
->name
.name
);
788 if (expld
.phase
== lang_final_phase_enum
)
789 einfo (_("%F%S: undefined section `%s'"
790 " referenced in expression\n"),
791 tree
, tree
->name
.name
);
793 else if (os
->processed_lma
)
795 if (os
->load_base
== NULL
)
796 new_abs (os
->bfd_section
->lma
);
799 exp_fold_tree_1 (os
->load_base
);
800 if (expld
.result
.valid_p
)
809 if (expld
.phase
!= lang_first_phase_enum
)
811 lang_output_section_statement_type
*os
;
813 os
= lang_output_section_find (tree
->name
.name
);
816 if (expld
.phase
== lang_final_phase_enum
)
817 einfo (_("%F%S: undefined section `%s'"
818 " referenced in expression\n"),
819 tree
, tree
->name
.name
);
822 else if (os
->bfd_section
!= NULL
)
826 if (tree
->type
.node_code
== SIZEOF
)
827 val
= (os
->bfd_section
->size
828 / bfd_octets_per_byte (link_info
.output_bfd
));
830 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
841 if (expld
.phase
!= lang_first_phase_enum
)
843 lang_memory_region_type
*mem
;
845 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
847 new_number (mem
->length
);
849 einfo (_("%F%S: undefined MEMORY region `%s'"
850 " referenced in expression\n"),
851 tree
, tree
->name
.name
);
857 if (expld
.phase
!= lang_first_phase_enum
)
859 lang_memory_region_type
*mem
;
861 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
863 new_rel_from_abs (mem
->origin
);
865 einfo (_("%F%S: undefined MEMORY region `%s'"
866 " referenced in expression\n"),
867 tree
, tree
->name
.name
);
872 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
873 new_number (config
.maxpagesize
);
874 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
875 new_number (config
.commonpagesize
);
877 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
878 tree
, tree
->name
.name
);
887 /* Return true if TREE is '.'. */
890 is_dot (const etree_type
*tree
)
892 return (tree
->type
.node_class
== etree_name
893 && tree
->type
.node_code
== NAME
894 && tree
->name
.name
[0] == '.'
895 && tree
->name
.name
[1] == 0);
898 /* Return true if TREE is a constant equal to VAL. */
901 is_value (const etree_type
*tree
, bfd_vma val
)
903 return (tree
->type
.node_class
== etree_value
904 && tree
->value
.value
== val
);
907 /* Return true if TREE is an absolute symbol equal to VAL defined in
911 is_sym_value (const etree_type
*tree
, bfd_vma val
)
913 struct bfd_link_hash_entry
*h
;
914 struct definedness_hash_entry
*def
;
916 return (tree
->type
.node_class
== etree_name
917 && tree
->type
.node_code
== NAME
918 && (def
= symbol_defined (tree
->name
.name
)) != NULL
920 && def
->iteration
== (lang_statement_iteration
& 1)
921 && (h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
924 FALSE
, FALSE
, TRUE
)) != NULL
925 && h
->type
== bfd_link_hash_defined
926 && h
->u
.def
.section
== bfd_abs_section_ptr
927 && h
->u
.def
.value
== val
);
930 /* Return true if TREE is ". != 0". */
933 is_dot_ne_0 (const etree_type
*tree
)
935 return (tree
->type
.node_class
== etree_binary
936 && tree
->type
.node_code
== NE
937 && is_dot (tree
->binary
.lhs
)
938 && is_value (tree
->binary
.rhs
, 0));
941 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
942 absolute constant with value 0 defined in a linker script. */
945 is_dot_plus_0 (const etree_type
*tree
)
947 return (tree
->type
.node_class
== etree_binary
948 && tree
->type
.node_code
== '+'
949 && is_dot (tree
->binary
.lhs
)
950 && (is_value (tree
->binary
.rhs
, 0)
951 || is_sym_value (tree
->binary
.rhs
, 0)));
954 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)",
955 or equivalent binary ALIGN expressions. */
958 is_align_conditional (const etree_type
*tree
)
960 if (tree
->type
.node_code
!= ALIGN_K
)
962 else if (tree
->type
.node_class
== etree_unary
)
963 tree
= tree
->unary
.child
;
964 else if (tree
->type
.node_class
== etree_binary
965 && (is_dot (tree
->binary
.lhs
)
966 || (tree
->binary
.lhs
->type
.node_class
== etree_unary
967 && tree
->binary
.lhs
->type
.node_code
== ABSOLUTE
968 && is_dot (tree
->binary
.lhs
->unary
.child
))))
969 tree
= tree
->binary
.rhs
;
973 return (tree
->type
.node_class
== etree_trinary
974 && is_dot_ne_0 (tree
->trinary
.cond
)
975 && is_value (tree
->trinary
.rhs
, 1));
979 exp_fold_tree_1 (etree_type
*tree
)
983 memset (&expld
.result
, 0, sizeof (expld
.result
));
987 switch (tree
->type
.node_class
)
990 if (expld
.section
== bfd_abs_section_ptr
991 && !config
.sane_expr
)
992 new_abs (tree
->value
.value
);
994 new_number (tree
->value
.value
);
995 expld
.result
.str
= tree
->value
.str
;
999 if (expld
.phase
!= lang_first_phase_enum
)
1001 asection
*output_section
= tree
->rel
.section
->output_section
;
1002 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
1006 memset (&expld
.result
, 0, sizeof (expld
.result
));
1010 exp_fold_tree_1 (tree
->assert_s
.child
);
1011 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
1012 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
1024 fold_trinary (tree
);
1029 case etree_provided
:
1030 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
1032 if (tree
->type
.node_class
!= etree_assign
)
1033 einfo (_("%F%S can not PROVIDE assignment to"
1034 " location counter\n"), tree
);
1035 if (expld
.phase
!= lang_first_phase_enum
)
1037 /* Notify the folder that this is an assignment to dot. */
1038 expld
.assigning_to_dot
= TRUE
;
1039 exp_fold_tree_1 (tree
->assign
.src
);
1040 expld
.assigning_to_dot
= FALSE
;
1042 if (!expld
.result
.valid_p
)
1044 if (expld
.phase
!= lang_mark_phase_enum
)
1045 einfo (_("%F%S invalid assignment to"
1046 " location counter\n"), tree
);
1047 else if (expld
.section
!= bfd_abs_section_ptr
)
1048 expld
.section
->flags
|= SEC_KEEP
;
1050 else if (expld
.dotp
== NULL
)
1051 einfo (_("%F%S assignment to location counter"
1052 " invalid outside of SECTIONS\n"), tree
);
1054 /* After allocation, assignment to dot should not be
1055 done inside an output section since allocation adds a
1056 padding statement that effectively duplicates the
1058 else if (expld
.phase
<= lang_allocating_phase_enum
1059 || expld
.section
== bfd_abs_section_ptr
)
1063 nextdot
= expld
.result
.value
;
1064 if (expld
.result
.section
!= NULL
)
1065 nextdot
+= expld
.result
.section
->vma
;
1067 nextdot
+= expld
.section
->vma
;
1069 /* If we are assigning to dot inside an output
1070 section arrange to keep the section, except for
1071 certain expressions that evaluate to zero. We
1072 can't ignore all expressions that evaluate to
1073 zero because an otherwise empty section might
1074 have padding added by an alignment expression
1075 that changes with relaxation. Such a section
1076 might have zero size before relaxation and so be
1077 stripped incorrectly. */
1078 if (expld
.phase
== lang_mark_phase_enum
1079 && expld
.section
!= bfd_abs_section_ptr
1080 && !(nextdot
== expld
.section
->vma
1081 && (is_value (tree
->assign
.src
, 0)
1082 || is_sym_value (tree
->assign
.src
, 0)
1083 || is_dot_plus_0 (tree
->assign
.src
)
1084 || is_align_conditional (tree
->assign
.src
))))
1085 expld
.section
->flags
|= SEC_KEEP
;
1087 if (nextdot
< expld
.dot
1088 && expld
.section
!= bfd_abs_section_ptr
)
1089 einfo (_("%F%S cannot move location counter backwards"
1090 " (from %V to %V)\n"),
1091 tree
, expld
.dot
, nextdot
);
1094 expld
.dot
= nextdot
;
1095 *expld
.dotp
= nextdot
;
1100 memset (&expld
.result
, 0, sizeof (expld
.result
));
1104 struct bfd_link_hash_entry
*h
= NULL
;
1106 if (tree
->type
.node_class
== etree_provide
)
1108 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1109 FALSE
, FALSE
, TRUE
);
1111 || !(h
->type
== bfd_link_hash_new
1112 || h
->type
== bfd_link_hash_undefined
1115 /* Do nothing. The symbol was never referenced, or
1116 was defined in some object file. Undefined weak
1117 symbols stay undefined. */
1122 expld
.assign_name
= tree
->assign
.dst
;
1123 exp_fold_tree_1 (tree
->assign
.src
);
1124 /* expld.assign_name remaining equal to tree->assign.dst
1125 below indicates the evaluation of tree->assign.src did
1126 not use the value of tree->assign.dst. We don't allow
1127 self assignment until the final phase for two reasons:
1128 1) Expressions are evaluated multiple times. With
1129 relaxation, the number of times may vary.
1130 2) Section relative symbol values cannot be correctly
1131 converted to absolute values, as is required by many
1132 expressions, until final section sizing is complete. */
1133 if ((expld
.result
.valid_p
1134 && (expld
.phase
== lang_final_phase_enum
1135 || expld
.assign_name
!= NULL
))
1136 || (expld
.phase
<= lang_mark_phase_enum
1137 && tree
->type
.node_class
== etree_assign
1138 && tree
->assign
.defsym
))
1142 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1145 einfo (_("%P%F:%s: hash creation failed\n"),
1149 if (expld
.result
.section
== NULL
)
1150 expld
.result
.section
= expld
.section
;
1151 if (!update_definedness (tree
->assign
.dst
, h
) && 0)
1153 /* Symbol was already defined. For now this error
1154 is disabled because it causes failures in the ld
1155 testsuite: ld-elf/var1, ld-scripts/defined5, and
1156 ld-scripts/pr14962. Some of these no doubt
1157 reflect scripts used in the wild. */
1158 (*link_info
.callbacks
->multiple_definition
)
1159 (&link_info
, h
, link_info
.output_bfd
,
1160 expld
.result
.section
, expld
.result
.value
);
1162 h
->type
= bfd_link_hash_defined
;
1163 h
->u
.def
.value
= expld
.result
.value
;
1164 h
->u
.def
.section
= expld
.result
.section
;
1165 if (tree
->type
.node_class
== etree_provide
)
1166 tree
->type
.node_class
= etree_provided
;
1168 /* Copy the symbol type if this is a simple assignment of
1169 one symbol to another. This could be more general
1170 (e.g. a ?: operator with NAMEs in each branch). */
1171 if (tree
->assign
.src
->type
.node_class
== etree_name
)
1173 struct bfd_link_hash_entry
*hsrc
;
1175 hsrc
= bfd_link_hash_lookup (link_info
.hash
,
1176 tree
->assign
.src
->name
.name
,
1177 FALSE
, FALSE
, TRUE
);
1179 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
, h
,
1183 else if (expld
.phase
== lang_final_phase_enum
)
1185 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1186 FALSE
, FALSE
, TRUE
);
1188 && h
->type
== bfd_link_hash_new
)
1189 h
->type
= bfd_link_hash_undefined
;
1191 expld
.assign_name
= NULL
;
1201 memset (&expld
.result
, 0, sizeof (expld
.result
));
1207 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
1211 expld
.section
= current_section
;
1212 exp_fold_tree_1 (tree
);
1216 exp_fold_tree_no_dot (etree_type
*tree
)
1220 expld
.section
= bfd_abs_section_ptr
;
1221 exp_fold_tree_1 (tree
);
1225 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
1227 etree_type value
, *new_e
;
1229 value
.type
.node_code
= code
;
1230 value
.type
.filename
= lhs
->type
.filename
;
1231 value
.type
.lineno
= lhs
->type
.lineno
;
1232 value
.binary
.lhs
= lhs
;
1233 value
.binary
.rhs
= rhs
;
1234 value
.type
.node_class
= etree_binary
;
1235 exp_fold_tree_no_dot (&value
);
1236 if (expld
.result
.valid_p
)
1237 return exp_intop (expld
.result
.value
);
1239 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->binary
));
1240 memcpy (new_e
, &value
, sizeof (new_e
->binary
));
1245 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
1247 etree_type value
, *new_e
;
1249 value
.type
.node_code
= code
;
1250 value
.type
.filename
= cond
->type
.filename
;
1251 value
.type
.lineno
= cond
->type
.lineno
;
1252 value
.trinary
.lhs
= lhs
;
1253 value
.trinary
.cond
= cond
;
1254 value
.trinary
.rhs
= rhs
;
1255 value
.type
.node_class
= etree_trinary
;
1256 exp_fold_tree_no_dot (&value
);
1257 if (expld
.result
.valid_p
)
1258 return exp_intop (expld
.result
.value
);
1260 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->trinary
));
1261 memcpy (new_e
, &value
, sizeof (new_e
->trinary
));
1266 exp_unop (int code
, etree_type
*child
)
1268 etree_type value
, *new_e
;
1270 value
.unary
.type
.node_code
= code
;
1271 value
.unary
.type
.filename
= child
->type
.filename
;
1272 value
.unary
.type
.lineno
= child
->type
.lineno
;
1273 value
.unary
.child
= child
;
1274 value
.unary
.type
.node_class
= etree_unary
;
1275 exp_fold_tree_no_dot (&value
);
1276 if (expld
.result
.valid_p
)
1277 return exp_intop (expld
.result
.value
);
1279 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->unary
));
1280 memcpy (new_e
, &value
, sizeof (new_e
->unary
));
1285 exp_nameop (int code
, const char *name
)
1287 etree_type value
, *new_e
;
1289 value
.name
.type
.node_code
= code
;
1290 value
.name
.type
.filename
= ldlex_filename ();
1291 value
.name
.type
.lineno
= lineno
;
1292 value
.name
.name
= name
;
1293 value
.name
.type
.node_class
= etree_name
;
1295 exp_fold_tree_no_dot (&value
);
1296 if (expld
.result
.valid_p
)
1297 return exp_intop (expld
.result
.value
);
1299 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
1300 memcpy (new_e
, &value
, sizeof (new_e
->name
));
1306 exp_assop (const char *dst
,
1308 enum node_tree_enum
class,
1314 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
1315 n
->assign
.type
.node_code
= '=';
1316 n
->assign
.type
.filename
= src
->type
.filename
;
1317 n
->assign
.type
.lineno
= src
->type
.lineno
;
1318 n
->assign
.type
.node_class
= class;
1319 n
->assign
.src
= src
;
1320 n
->assign
.dst
= dst
;
1321 n
->assign
.defsym
= defsym
;
1322 n
->assign
.hidden
= hidden
;
1326 /* Handle linker script assignments and HIDDEN. */
1329 exp_assign (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1331 return exp_assop (dst
, src
, etree_assign
, FALSE
, hidden
);
1334 /* Handle --defsym command-line option. */
1337 exp_defsym (const char *dst
, etree_type
*src
)
1339 return exp_assop (dst
, src
, etree_assign
, TRUE
, FALSE
);
1342 /* Handle PROVIDE. */
1345 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1347 return exp_assop (dst
, src
, etree_provide
, FALSE
, hidden
);
1350 /* Handle ASSERT. */
1353 exp_assert (etree_type
*exp
, const char *message
)
1357 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1358 n
->assert_s
.type
.node_code
= '!';
1359 n
->assert_s
.type
.filename
= exp
->type
.filename
;
1360 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1361 n
->assert_s
.type
.node_class
= etree_assert
;
1362 n
->assert_s
.child
= exp
;
1363 n
->assert_s
.message
= message
;
1368 exp_print_tree (etree_type
*tree
)
1370 bfd_boolean function_like
;
1372 if (config
.map_file
== NULL
)
1373 config
.map_file
= stderr
;
1377 minfo ("NULL TREE\n");
1381 switch (tree
->type
.node_class
)
1384 minfo ("0x%v", tree
->value
.value
);
1387 if (tree
->rel
.section
->owner
!= NULL
)
1388 minfo ("%B:", tree
->rel
.section
->owner
);
1389 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1392 fputs (tree
->assign
.dst
, config
.map_file
);
1393 exp_print_token (tree
->type
.node_code
, TRUE
);
1394 exp_print_tree (tree
->assign
.src
);
1397 case etree_provided
:
1398 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
1399 exp_print_tree (tree
->assign
.src
);
1400 fputc (')', config
.map_file
);
1403 function_like
= FALSE
;
1404 switch (tree
->type
.node_code
)
1409 case DATA_SEGMENT_ALIGN
:
1410 case DATA_SEGMENT_RELRO_END
:
1411 function_like
= TRUE
;
1414 /* Special handling because arguments are in reverse order and
1415 the segment name is quoted. */
1416 exp_print_token (tree
->type
.node_code
, FALSE
);
1417 fputs (" (\"", config
.map_file
);
1418 exp_print_tree (tree
->binary
.rhs
);
1419 fputs ("\", ", config
.map_file
);
1420 exp_print_tree (tree
->binary
.lhs
);
1421 fputc (')', config
.map_file
);
1426 exp_print_token (tree
->type
.node_code
, FALSE
);
1427 fputc (' ', config
.map_file
);
1429 fputc ('(', config
.map_file
);
1430 exp_print_tree (tree
->binary
.lhs
);
1432 fprintf (config
.map_file
, ", ");
1434 exp_print_token (tree
->type
.node_code
, TRUE
);
1435 exp_print_tree (tree
->binary
.rhs
);
1436 fputc (')', config
.map_file
);
1439 exp_print_tree (tree
->trinary
.cond
);
1440 fputc ('?', config
.map_file
);
1441 exp_print_tree (tree
->trinary
.lhs
);
1442 fputc (':', config
.map_file
);
1443 exp_print_tree (tree
->trinary
.rhs
);
1446 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1447 if (tree
->unary
.child
)
1449 fprintf (config
.map_file
, " (");
1450 exp_print_tree (tree
->unary
.child
);
1451 fputc (')', config
.map_file
);
1456 fprintf (config
.map_file
, "ASSERT (");
1457 exp_print_tree (tree
->assert_s
.child
);
1458 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1462 if (tree
->type
.node_code
== NAME
)
1463 fputs (tree
->name
.name
, config
.map_file
);
1466 exp_print_token (tree
->type
.node_code
, FALSE
);
1467 if (tree
->name
.name
)
1468 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1478 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1482 exp_fold_tree_no_dot (tree
);
1483 if (expld
.result
.valid_p
)
1484 return expld
.result
.value
;
1485 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1486 einfo (_("%F%S: nonconstant expression for %s\n"),
1493 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1495 return exp_get_vma (tree
, def
, name
);
1499 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1508 exp_fold_tree_no_dot (tree
);
1509 if (!expld
.result
.valid_p
)
1511 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1512 einfo (_("%F%S: nonconstant expression for %s\n"),
1517 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1521 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1522 fill
->size
= (len
+ 1) / 2;
1524 s
= (unsigned char *) expld
.result
.str
;
1532 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1546 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1547 val
= expld
.result
.value
;
1548 fill
->data
[0] = (val
>> 24) & 0xff;
1549 fill
->data
[1] = (val
>> 16) & 0xff;
1550 fill
->data
[2] = (val
>> 8) & 0xff;
1551 fill
->data
[3] = (val
>> 0) & 0xff;
1558 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1562 exp_fold_tree_no_dot (tree
);
1564 if (expld
.result
.valid_p
)
1566 if (expld
.result
.section
!= NULL
)
1567 expld
.result
.value
+= expld
.result
.section
->vma
;
1568 return expld
.result
.value
;
1570 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1572 einfo (_("%F%S: nonconstant expression for %s\n"),
1580 align_n (bfd_vma value
, bfd_vma align
)
1585 value
= (value
+ align
- 1) / align
;
1586 return value
* align
;
1592 /* The value "13" is ad-hoc, somewhat related to the expected number of
1593 assignments in a linker script. */
1594 if (!bfd_hash_table_init_n (&definedness_table
,
1595 definedness_newfunc
,
1596 sizeof (struct definedness_hash_entry
),
1598 einfo (_("%P%F: can not create hash table: %E\n"));
1604 bfd_hash_table_free (&definedness_table
);