1 /* This module handles expression trees.
2 Copyright (C) 1991-2021 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
42 #include "libiberty.h"
43 #include "safe-ctype.h"
45 static void exp_fold_tree_1 (etree_type
*);
46 static bfd_vma
align_n (bfd_vma
, bfd_vma
);
48 segment_type
*segments
;
50 struct ldexp_control expld
;
52 /* This structure records symbols for which we need to keep track of
53 definedness for use in the DEFINED () test. It is also used in
54 making absolute symbols section relative late in the link. */
56 struct definedness_hash_entry
58 struct bfd_hash_entry root
;
60 /* If this symbol was assigned from "dot" outside of an output
61 section statement, the section we'd like it relative to. */
64 /* Low bits of iteration count. Symbols with matching iteration have
65 been defined in this pass over the script. */
66 unsigned int iteration
: 8;
68 /* Symbol was defined by an object file. */
69 unsigned int by_object
: 1;
72 static struct bfd_hash_table definedness_table
;
74 /* Print the string representation of the given token. Surround it
75 with spaces if INFIX_P is TRUE. */
78 exp_print_token (token_code_type code
, int infix_p
)
105 { LOG2CEIL
, "LOG2CEIL" },
106 { ALIGN_K
, "ALIGN" },
113 { SECTIONS
, "SECTIONS" },
114 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
115 { MEMORY
, "MEMORY" },
116 { DEFINED
, "DEFINED" },
117 { TARGET_K
, "TARGET" },
118 { SEARCH_DIR
, "SEARCH_DIR" },
122 { ALIGNOF
, "ALIGNOF" },
123 { SIZEOF
, "SIZEOF" },
125 { LOADADDR
, "LOADADDR" },
126 { CONSTANT
, "CONSTANT" },
127 { ABSOLUTE
, "ABSOLUTE" },
130 { ASSERT_K
, "ASSERT" },
131 { REL
, "relocatable" },
132 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
133 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
134 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
135 { ORIGIN
, "ORIGIN" },
136 { LENGTH
, "LENGTH" },
137 { SEGMENT_START
, "SEGMENT_START" }
141 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
142 if (table
[idx
].code
== code
)
146 fputc (' ', config
.map_file
);
148 if (idx
< ARRAY_SIZE (table
))
149 fputs (table
[idx
].name
, config
.map_file
);
151 fputc (code
, config
.map_file
);
153 fprintf (config
.map_file
, "<code %d>", code
);
156 fputc (' ', config
.map_file
);
162 bfd_vma value
= expld
.result
.value
;
164 bool round_up
= false;
169 /* If more than one bit is set in the value we will need to round up. */
170 if ((value
> 1) && (value
& 1))
177 expld
.result
.section
= NULL
;
178 expld
.result
.value
= result
;
184 if (expld
.result
.section
!= NULL
)
185 expld
.result
.value
+= expld
.result
.section
->vma
;
186 expld
.result
.section
= bfd_abs_section_ptr
;
187 expld
.rel_from_abs
= false;
191 new_abs (bfd_vma value
)
193 expld
.result
.valid_p
= true;
194 expld
.result
.section
= bfd_abs_section_ptr
;
195 expld
.result
.value
= value
;
196 expld
.result
.str
= NULL
;
200 exp_intop (bfd_vma value
)
202 etree_type
*new_e
= stat_alloc (sizeof (new_e
->value
));
203 new_e
->type
.node_code
= INT
;
204 new_e
->type
.filename
= ldlex_filename ();
205 new_e
->type
.lineno
= lineno
;
206 new_e
->value
.value
= value
;
207 new_e
->value
.str
= NULL
;
208 new_e
->type
.node_class
= etree_value
;
213 exp_bigintop (bfd_vma value
, char *str
)
215 etree_type
*new_e
= stat_alloc (sizeof (new_e
->value
));
216 new_e
->type
.node_code
= INT
;
217 new_e
->type
.filename
= ldlex_filename ();
218 new_e
->type
.lineno
= lineno
;
219 new_e
->value
.value
= value
;
220 new_e
->value
.str
= str
;
221 new_e
->type
.node_class
= etree_value
;
225 /* Build an expression representing an unnamed relocatable value. */
228 exp_relop (asection
*section
, bfd_vma value
)
230 etree_type
*new_e
= stat_alloc (sizeof (new_e
->rel
));
231 new_e
->type
.node_code
= REL
;
232 new_e
->type
.filename
= ldlex_filename ();
233 new_e
->type
.lineno
= lineno
;
234 new_e
->type
.node_class
= etree_rel
;
235 new_e
->rel
.section
= section
;
236 new_e
->rel
.value
= value
;
241 new_number (bfd_vma value
)
243 expld
.result
.valid_p
= true;
244 expld
.result
.value
= value
;
245 expld
.result
.str
= NULL
;
246 expld
.result
.section
= NULL
;
250 new_rel (bfd_vma value
, asection
*section
)
252 expld
.result
.valid_p
= true;
253 expld
.result
.value
= value
;
254 expld
.result
.str
= NULL
;
255 expld
.result
.section
= section
;
259 new_rel_from_abs (bfd_vma value
)
261 asection
*s
= expld
.section
;
263 expld
.rel_from_abs
= true;
264 expld
.result
.valid_p
= true;
265 expld
.result
.value
= value
- s
->vma
;
266 expld
.result
.str
= NULL
;
267 expld
.result
.section
= s
;
270 /* New-function for the definedness hash table. */
272 static struct bfd_hash_entry
*
273 definedness_newfunc (struct bfd_hash_entry
*entry
,
274 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
275 const char *name ATTRIBUTE_UNUSED
)
277 struct definedness_hash_entry
*ret
= (struct definedness_hash_entry
*) entry
;
280 ret
= (struct definedness_hash_entry
*)
281 bfd_hash_allocate (table
, sizeof (struct definedness_hash_entry
));
284 einfo (_("%F%P: bfd_hash_allocate failed creating symbol %s\n"), name
);
291 /* Called during processing of linker script script expressions.
292 For symbols assigned in a linker script, return a struct describing
293 where the symbol is defined relative to the current expression,
294 otherwise return NULL. */
296 static struct definedness_hash_entry
*
297 symbol_defined (const char *name
)
299 return ((struct definedness_hash_entry
*)
300 bfd_hash_lookup (&definedness_table
, name
, false, false));
303 /* Update the definedness state of NAME. Return FALSE if script symbol
304 is multiply defining a strong symbol in an object. */
307 update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
310 struct definedness_hash_entry
*defentry
311 = (struct definedness_hash_entry
*)
312 bfd_hash_lookup (&definedness_table
, name
, true, false);
314 if (defentry
== NULL
)
315 einfo (_("%F%P: bfd_hash_lookup failed creating symbol %s\n"), name
);
317 /* If the symbol was already defined, and not by a script, then it
318 must be defined by an object file or by the linker target code. */
321 && (h
->type
== bfd_link_hash_defined
322 || h
->type
== bfd_link_hash_defweak
323 || h
->type
== bfd_link_hash_common
))
325 defentry
->by_object
= 1;
326 if (h
->type
== bfd_link_hash_defined
327 && h
->u
.def
.section
->output_section
!= NULL
332 defentry
->iteration
= lang_statement_iteration
;
333 defentry
->final_sec
= bfd_abs_section_ptr
;
334 if (expld
.phase
== lang_final_phase_enum
335 && expld
.rel_from_abs
336 && expld
.result
.section
== bfd_abs_section_ptr
)
337 defentry
->final_sec
= section_for_dot ();
342 fold_segment_end (seg_align_type
*seg
)
344 if (expld
.phase
== lang_first_phase_enum
345 || expld
.section
!= bfd_abs_section_ptr
)
347 expld
.result
.valid_p
= false;
349 else if (seg
->phase
== exp_seg_align_seen
350 || seg
->phase
== exp_seg_relro_seen
)
352 seg
->phase
= exp_seg_end_seen
;
353 seg
->end
= expld
.result
.value
;
355 else if (seg
->phase
== exp_seg_done
356 || seg
->phase
== exp_seg_adjust
357 || seg
->phase
== exp_seg_relro_adjust
)
362 expld
.result
.valid_p
= false;
366 fold_unary (etree_type
*tree
)
368 exp_fold_tree_1 (tree
->unary
.child
);
369 if (expld
.result
.valid_p
)
371 switch (tree
->type
.node_code
)
374 if (expld
.phase
!= lang_first_phase_enum
)
375 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
377 expld
.result
.valid_p
= false;
389 expld
.result
.value
= ~expld
.result
.value
;
393 expld
.result
.value
= !expld
.result
.value
;
397 expld
.result
.value
= -expld
.result
.value
;
401 /* Return next place aligned to value. */
402 if (expld
.phase
!= lang_first_phase_enum
)
405 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
408 expld
.result
.valid_p
= false;
411 case DATA_SEGMENT_END
:
412 fold_segment_end (&expld
.dataseg
);
422 /* Arithmetic operators, bitwise AND, bitwise OR and XOR keep the
423 section of one of their operands only when the other operand is a
424 plain number. Losing the section when operating on two symbols,
425 ie. a result of a plain number, is required for subtraction and
426 XOR. It's justifiable for the other operations on the grounds that
427 adding, multiplying etc. two section relative values does not
428 really make sense unless they are just treated as numbers.
429 The same argument could be made for many expressions involving one
430 symbol and a number. For example, "1 << x" and "100 / x" probably
431 should not be given the section of x. The trouble is that if we
432 fuss about such things the rules become complex and it is onerous
433 to document ld expression evaluation. */
435 arith_result_section (const etree_value_type
*lhs
)
437 if (expld
.result
.section
== lhs
->section
)
439 if (expld
.section
== bfd_abs_section_ptr
440 && !config
.sane_expr
)
441 /* Duplicate the insanity in exp_fold_tree_1 case etree_value. */
442 expld
.result
.section
= bfd_abs_section_ptr
;
444 expld
.result
.section
= NULL
;
449 fold_segment_align (seg_align_type
*seg
, etree_value_type
*lhs
)
451 seg
->relro
= exp_seg_relro_start
;
452 if (expld
.phase
== lang_first_phase_enum
453 || expld
.section
!= bfd_abs_section_ptr
)
454 expld
.result
.valid_p
= false;
457 bfd_vma maxpage
= lhs
->value
;
458 bfd_vma commonpage
= expld
.result
.value
;
460 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
461 if (seg
->phase
== exp_seg_relro_adjust
)
462 expld
.result
.value
= seg
->base
;
463 else if (seg
->phase
== exp_seg_adjust
)
465 if (commonpage
< maxpage
)
466 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
467 & (maxpage
- commonpage
));
471 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
472 if (seg
->phase
== exp_seg_done
)
476 else if (seg
->phase
== exp_seg_none
)
478 seg
->phase
= exp_seg_align_seen
;
479 seg
->base
= expld
.result
.value
;
480 seg
->pagesize
= commonpage
;
481 seg
->maxpagesize
= maxpage
;
485 expld
.result
.valid_p
= false;
491 fold_segment_relro_end (seg_align_type
*seg
, etree_value_type
*lhs
)
493 /* Operands swapped! XXX_SEGMENT_RELRO_END(offset,exp) has offset
494 in expld.result and exp in lhs. */
495 seg
->relro
= exp_seg_relro_end
;
496 seg
->relro_offset
= expld
.result
.value
;
497 if (expld
.phase
== lang_first_phase_enum
498 || expld
.section
!= bfd_abs_section_ptr
)
499 expld
.result
.valid_p
= false;
500 else if (seg
->phase
== exp_seg_align_seen
501 || seg
->phase
== exp_seg_adjust
502 || seg
->phase
== exp_seg_relro_adjust
503 || seg
->phase
== exp_seg_done
)
505 if (seg
->phase
== exp_seg_align_seen
506 || seg
->phase
== exp_seg_relro_adjust
)
507 seg
->relro_end
= lhs
->value
+ expld
.result
.value
;
509 if (seg
->phase
== exp_seg_relro_adjust
510 && (seg
->relro_end
& (seg
->pagesize
- 1)))
512 seg
->relro_end
+= seg
->pagesize
- 1;
513 seg
->relro_end
&= ~(seg
->pagesize
- 1);
514 expld
.result
.value
= seg
->relro_end
- expld
.result
.value
;
517 expld
.result
.value
= lhs
->value
;
519 if (seg
->phase
== exp_seg_align_seen
)
520 seg
->phase
= exp_seg_relro_seen
;
523 expld
.result
.valid_p
= false;
527 fold_binary (etree_type
*tree
)
529 etree_value_type lhs
;
530 exp_fold_tree_1 (tree
->binary
.lhs
);
532 /* The SEGMENT_START operator is special because its first
533 operand is a string, not the name of a symbol. Note that the
534 operands have been swapped, so binary.lhs is second (default)
535 operand, binary.rhs is first operand. */
536 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
538 bfd_vma value
= expld
.result
.value
;
539 const char *segment_name
;
542 /* Check to see if the user has overridden the default
544 segment_name
= tree
->binary
.rhs
->name
.name
;
545 for (seg
= segments
; seg
; seg
= seg
->next
)
546 if (strcmp (seg
->name
, segment_name
) == 0)
549 && config
.magic_demand_paged
550 && link_info
.maxpagesize
!= 0
551 && (seg
->value
% link_info
.maxpagesize
) != 0)
552 einfo (_("%P: warning: address of `%s' "
553 "isn't multiple of maximum page size\n"),
559 new_rel_from_abs (value
);
564 exp_fold_tree_1 (tree
->binary
.rhs
);
565 expld
.result
.valid_p
&= lhs
.valid_p
;
567 if (expld
.result
.valid_p
)
569 if (lhs
.section
!= expld
.result
.section
)
571 /* If the values are from different sections, and neither is
572 just a number, make both the source arguments absolute. */
573 if (expld
.result
.section
!= NULL
574 && lhs
.section
!= NULL
)
577 lhs
.value
+= lhs
.section
->vma
;
578 lhs
.section
= bfd_abs_section_ptr
;
581 /* If the rhs is just a number, keep the lhs section. */
582 else if (expld
.result
.section
== NULL
)
584 expld
.result
.section
= lhs
.section
;
585 /* Make this NULL so that we know one of the operands
586 was just a number, for later tests. */
590 /* At this point we know that both operands have the same
591 section, or at least one of them is a plain number. */
593 switch (tree
->type
.node_code
)
597 expld.result.value = lhs.value y expld.result.value; \
598 arith_result_section (&lhs); \
601 /* Comparison operators, logical AND, and logical OR always
602 return a plain number. */
605 expld.result.value = lhs.value y expld.result.value; \
606 expld.result.section = NULL; \
627 if (expld
.result
.value
!= 0)
628 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
629 % (bfd_signed_vma
) expld
.result
.value
);
630 else if (expld
.phase
!= lang_mark_phase_enum
)
631 einfo (_("%F%P:%pS %% by zero\n"), tree
->binary
.rhs
);
632 arith_result_section (&lhs
);
636 if (expld
.result
.value
!= 0)
637 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
638 / (bfd_signed_vma
) expld
.result
.value
);
639 else if (expld
.phase
!= lang_mark_phase_enum
)
640 einfo (_("%F%P:%pS / by zero\n"), tree
->binary
.rhs
);
641 arith_result_section (&lhs
);
645 if (lhs
.value
> expld
.result
.value
)
646 expld
.result
.value
= lhs
.value
;
650 if (lhs
.value
< expld
.result
.value
)
651 expld
.result
.value
= lhs
.value
;
655 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
658 case DATA_SEGMENT_ALIGN
:
659 fold_segment_align (&expld
.dataseg
, &lhs
);
662 case DATA_SEGMENT_RELRO_END
:
663 fold_segment_relro_end (&expld
.dataseg
, &lhs
);
673 fold_trinary (etree_type
*tree
)
675 struct bfd_link_hash_entry
*save
= expld
.assign_src
;
677 exp_fold_tree_1 (tree
->trinary
.cond
);
678 expld
.assign_src
= save
;
679 if (expld
.result
.valid_p
)
680 exp_fold_tree_1 (expld
.result
.value
682 : tree
->trinary
.rhs
);
686 fold_name (etree_type
*tree
)
688 struct bfd_link_hash_entry
*h
;
689 struct definedness_hash_entry
*def
;
691 memset (&expld
.result
, 0, sizeof (expld
.result
));
693 switch (tree
->type
.node_code
)
696 link_info
.load_phdrs
= 1;
697 if (expld
.phase
!= lang_first_phase_enum
)
699 bfd_vma hdr_size
= 0;
700 /* Don't find the real header size if only marking sections;
701 The bfd function may cache incorrect data. */
702 if (expld
.phase
!= lang_mark_phase_enum
)
703 hdr_size
= (bfd_sizeof_headers (link_info
.output_bfd
, &link_info
)
704 / bfd_octets_per_byte (link_info
.output_bfd
, NULL
));
705 new_number (hdr_size
);
710 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
714 new_number (h
!= NULL
715 && (h
->type
== bfd_link_hash_defined
716 || h
->type
== bfd_link_hash_defweak
717 || h
->type
== bfd_link_hash_common
)
719 || (def
= symbol_defined (tree
->name
.name
)) == NULL
721 || def
->iteration
== (lang_statement_iteration
& 255)));
725 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
726 new_rel_from_abs (expld
.dot
);
729 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
735 if (expld
.phase
!= lang_first_phase_enum
)
736 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
738 else if (h
->type
== bfd_link_hash_defined
739 || h
->type
== bfd_link_hash_defweak
)
741 asection
*output_section
;
743 output_section
= h
->u
.def
.section
->output_section
;
744 if (output_section
== NULL
)
746 if (expld
.phase
<= lang_mark_phase_enum
)
747 new_rel (h
->u
.def
.value
, h
->u
.def
.section
);
749 einfo (_("%X%P:%pS: unresolvable symbol `%s'"
750 " referenced in expression\n"),
751 tree
, tree
->name
.name
);
753 else if (output_section
== bfd_abs_section_ptr
754 && (expld
.section
!= bfd_abs_section_ptr
755 || config
.sane_expr
))
756 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
758 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
761 else if (expld
.phase
== lang_final_phase_enum
762 || (expld
.phase
!= lang_mark_phase_enum
763 && expld
.assigning_to_dot
))
764 einfo (_("%F%P:%pS: undefined symbol `%s'"
765 " referenced in expression\n"),
766 tree
, tree
->name
.name
);
767 else if (h
->type
== bfd_link_hash_new
)
769 h
->type
= bfd_link_hash_undefined
;
770 h
->u
.undef
.abfd
= NULL
;
771 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
772 bfd_link_add_undef (link_info
.hash
, h
);
774 if (expld
.assign_src
== NULL
)
775 expld
.assign_src
= h
;
777 expld
.assign_src
= (struct bfd_link_hash_entry
*) - 1;
779 /* Self-assignment is only allowed for absolute symbols
780 defined in a linker script. */
781 if (expld
.assign_name
!= NULL
782 && strcmp (expld
.assign_name
, tree
->name
.name
) == 0
784 && (h
->type
== bfd_link_hash_defined
785 || h
->type
== bfd_link_hash_defweak
)
786 && h
->u
.def
.section
== bfd_abs_section_ptr
787 && (def
= symbol_defined (tree
->name
.name
)) != NULL
788 && def
->iteration
== (lang_statement_iteration
& 255)))
789 expld
.assign_name
= NULL
;
794 if (expld
.phase
!= lang_first_phase_enum
)
796 lang_output_section_statement_type
*os
;
798 os
= lang_output_section_find (tree
->name
.name
);
801 if (expld
.phase
== lang_final_phase_enum
)
802 einfo (_("%F%P:%pS: undefined section `%s'"
803 " referenced in expression\n"),
804 tree
, tree
->name
.name
);
806 else if (os
->processed_vma
)
807 new_rel (0, os
->bfd_section
);
812 if (expld
.phase
!= lang_first_phase_enum
)
814 lang_output_section_statement_type
*os
;
816 os
= lang_output_section_find (tree
->name
.name
);
819 if (expld
.phase
== lang_final_phase_enum
)
820 einfo (_("%F%P:%pS: undefined section `%s'"
821 " referenced in expression\n"),
822 tree
, tree
->name
.name
);
824 else if (os
->processed_lma
)
826 if (os
->load_base
== NULL
)
827 new_abs (os
->bfd_section
->lma
);
830 exp_fold_tree_1 (os
->load_base
);
831 if (expld
.result
.valid_p
)
840 if (expld
.phase
!= lang_first_phase_enum
)
842 lang_output_section_statement_type
*os
;
844 os
= lang_output_section_find (tree
->name
.name
);
847 if (expld
.phase
== lang_final_phase_enum
)
848 einfo (_("%F%P:%pS: undefined section `%s'"
849 " referenced in expression\n"),
850 tree
, tree
->name
.name
);
853 else if (os
->bfd_section
!= NULL
)
857 if (tree
->type
.node_code
== SIZEOF
)
858 val
= (os
->bfd_section
->size
859 / bfd_octets_per_byte (link_info
.output_bfd
,
862 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
873 lang_memory_region_type
*mem
;
875 mem
= lang_memory_region_lookup (tree
->name
.name
, false);
877 new_number (mem
->length
);
879 einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
880 " referenced in expression\n"),
881 tree
, tree
->name
.name
);
887 lang_memory_region_type
*mem
;
889 mem
= lang_memory_region_lookup (tree
->name
.name
, false);
891 new_rel_from_abs (mem
->origin
);
893 einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
894 " referenced in expression\n"),
895 tree
, tree
->name
.name
);
900 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
901 new_number (link_info
.maxpagesize
);
902 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
903 new_number (link_info
.commonpagesize
);
905 einfo (_("%F%P:%pS: unknown constant `%s' referenced in expression\n"),
906 tree
, tree
->name
.name
);
915 /* Return true if TREE is '.'. */
918 is_dot (const etree_type
*tree
)
920 return (tree
->type
.node_class
== etree_name
921 && tree
->type
.node_code
== NAME
922 && tree
->name
.name
[0] == '.'
923 && tree
->name
.name
[1] == 0);
926 /* Return true if TREE is a constant equal to VAL. */
929 is_value (const etree_type
*tree
, bfd_vma val
)
931 return (tree
->type
.node_class
== etree_value
932 && tree
->value
.value
== val
);
935 /* Return true if TREE is an absolute symbol equal to VAL defined in
939 is_sym_value (const etree_type
*tree
, bfd_vma val
)
941 struct bfd_link_hash_entry
*h
;
942 struct definedness_hash_entry
*def
;
944 return (tree
->type
.node_class
== etree_name
945 && tree
->type
.node_code
== NAME
946 && (def
= symbol_defined (tree
->name
.name
)) != NULL
947 && def
->iteration
== (lang_statement_iteration
& 255)
948 && (h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
951 false, false, true)) != NULL
953 && h
->type
== bfd_link_hash_defined
954 && h
->u
.def
.section
== bfd_abs_section_ptr
955 && h
->u
.def
.value
== val
);
958 /* Return true if TREE is ". != 0". */
961 is_dot_ne_0 (const etree_type
*tree
)
963 return (tree
->type
.node_class
== etree_binary
964 && tree
->type
.node_code
== NE
965 && is_dot (tree
->binary
.lhs
)
966 && is_value (tree
->binary
.rhs
, 0));
969 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
970 absolute constant with value 0 defined in a linker script. */
973 is_dot_plus_0 (const etree_type
*tree
)
975 return (tree
->type
.node_class
== etree_binary
976 && tree
->type
.node_code
== '+'
977 && is_dot (tree
->binary
.lhs
)
978 && (is_value (tree
->binary
.rhs
, 0)
979 || is_sym_value (tree
->binary
.rhs
, 0)));
982 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
985 is_align_conditional (const etree_type
*tree
)
987 if (tree
->type
.node_class
== etree_unary
988 && tree
->type
.node_code
== ALIGN_K
)
990 tree
= tree
->unary
.child
;
991 return (tree
->type
.node_class
== etree_trinary
992 && is_dot_ne_0 (tree
->trinary
.cond
)
993 && is_value (tree
->trinary
.rhs
, 1));
999 exp_fold_tree_1 (etree_type
*tree
)
1003 memset (&expld
.result
, 0, sizeof (expld
.result
));
1007 switch (tree
->type
.node_class
)
1010 if (expld
.section
== bfd_abs_section_ptr
1011 && !config
.sane_expr
)
1012 new_abs (tree
->value
.value
);
1014 new_number (tree
->value
.value
);
1015 expld
.result
.str
= tree
->value
.str
;
1019 if (expld
.phase
!= lang_first_phase_enum
)
1021 asection
*output_section
= tree
->rel
.section
->output_section
;
1022 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
1026 memset (&expld
.result
, 0, sizeof (expld
.result
));
1030 exp_fold_tree_1 (tree
->assert_s
.child
);
1031 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
1032 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
1044 fold_trinary (tree
);
1049 case etree_provided
:
1050 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
1052 if (tree
->type
.node_class
!= etree_assign
)
1053 einfo (_("%F%P:%pS can not PROVIDE assignment to"
1054 " location counter\n"), tree
);
1055 if (expld
.phase
!= lang_first_phase_enum
)
1057 /* Notify the folder that this is an assignment to dot. */
1058 expld
.assigning_to_dot
= true;
1059 exp_fold_tree_1 (tree
->assign
.src
);
1060 expld
.assigning_to_dot
= false;
1062 /* If we are assigning to dot inside an output section
1063 arrange to keep the section, except for certain
1064 expressions that evaluate to zero. We ignore . = 0,
1065 . = . + 0, and . = ALIGN (. != 0 ? expr : 1).
1066 We can't ignore all expressions that evaluate to zero
1067 because an otherwise empty section might have padding
1068 added by an alignment expression that changes with
1069 relaxation. Such a section might have zero size
1070 before relaxation and so be stripped incorrectly. */
1071 if (expld
.phase
== lang_mark_phase_enum
1072 && expld
.section
!= bfd_abs_section_ptr
1073 && expld
.section
!= bfd_und_section_ptr
1074 && !(expld
.result
.valid_p
1075 && expld
.result
.value
== 0
1076 && (is_value (tree
->assign
.src
, 0)
1077 || is_sym_value (tree
->assign
.src
, 0)
1078 || is_dot_plus_0 (tree
->assign
.src
)
1079 || is_align_conditional (tree
->assign
.src
))))
1080 expld
.section
->flags
|= SEC_KEEP
;
1082 if (!expld
.result
.valid_p
1083 || expld
.section
== bfd_und_section_ptr
)
1085 if (expld
.phase
!= lang_mark_phase_enum
)
1086 einfo (_("%F%P:%pS invalid assignment to"
1087 " location counter\n"), tree
);
1089 else if (expld
.dotp
== NULL
)
1090 einfo (_("%F%P:%pS assignment to location counter"
1091 " invalid outside of SECTIONS\n"), tree
);
1093 /* After allocation, assignment to dot should not be
1094 done inside an output section since allocation adds a
1095 padding statement that effectively duplicates the
1097 else if (expld
.phase
<= lang_allocating_phase_enum
1098 || expld
.section
== bfd_abs_section_ptr
)
1102 nextdot
= expld
.result
.value
;
1103 if (expld
.result
.section
!= NULL
)
1104 nextdot
+= expld
.result
.section
->vma
;
1106 nextdot
+= expld
.section
->vma
;
1107 if (nextdot
< expld
.dot
1108 && expld
.section
!= bfd_abs_section_ptr
)
1109 einfo (_("%F%P:%pS cannot move location counter backwards"
1110 " (from %V to %V)\n"),
1111 tree
, expld
.dot
, nextdot
);
1114 expld
.dot
= nextdot
;
1115 *expld
.dotp
= nextdot
;
1120 memset (&expld
.result
, 0, sizeof (expld
.result
));
1124 struct bfd_link_hash_entry
*h
= NULL
;
1126 if (tree
->type
.node_class
== etree_provide
)
1128 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1129 false, false, true);
1131 || !(h
->type
== bfd_link_hash_new
1132 || h
->type
== bfd_link_hash_undefined
1133 || h
->type
== bfd_link_hash_undefweak
1136 /* Do nothing. The symbol was never referenced, or
1137 was defined in some object file. Note that
1138 undefweak symbols are defined by PROVIDE. This
1139 is to support glibc use of __rela_iplt_start and
1140 similar weak references. */
1145 expld
.assign_name
= tree
->assign
.dst
;
1146 expld
.assign_src
= NULL
;
1147 exp_fold_tree_1 (tree
->assign
.src
);
1148 /* expld.assign_name remaining equal to tree->assign.dst
1149 below indicates the evaluation of tree->assign.src did
1150 not use the value of tree->assign.dst. We don't allow
1151 self assignment until the final phase for two reasons:
1152 1) Expressions are evaluated multiple times. With
1153 relaxation, the number of times may vary.
1154 2) Section relative symbol values cannot be correctly
1155 converted to absolute values, as is required by many
1156 expressions, until final section sizing is complete. */
1157 if (expld
.phase
== lang_final_phase_enum
1158 || expld
.phase
== lang_fixed_phase_enum
1159 || expld
.assign_name
!= NULL
)
1161 if (tree
->type
.node_class
== etree_provide
)
1162 tree
->type
.node_class
= etree_provided
;
1166 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1169 einfo (_("%F%P:%s: hash creation failed\n"),
1173 /* If the expression is not valid then fake a zero value. In
1174 the final phase any errors will already have been raised,
1175 in earlier phases we want to create this definition so
1176 that it can be seen by other expressions. */
1177 if (!expld
.result
.valid_p
1178 && h
->type
== bfd_link_hash_new
)
1180 expld
.result
.value
= 0;
1181 expld
.result
.section
= NULL
;
1182 expld
.result
.valid_p
= true;
1185 if (expld
.result
.valid_p
)
1187 if (expld
.result
.section
== NULL
)
1188 expld
.result
.section
= expld
.section
;
1189 if (!update_definedness (tree
->assign
.dst
, h
)
1190 && expld
.assign_name
!= NULL
)
1192 /* Symbol was already defined, and the script isn't
1193 modifying the symbol value for some reason as in
1194 ld-elf/var1 and ld-scripts/pr14962.
1195 For now this is only a warning. */
1196 unsigned int warn
= link_info
.warn_multiple_definition
;
1197 link_info
.warn_multiple_definition
= 1;
1198 (*link_info
.callbacks
->multiple_definition
)
1199 (&link_info
, h
, link_info
.output_bfd
,
1200 expld
.result
.section
, expld
.result
.value
);
1201 link_info
.warn_multiple_definition
= warn
;
1203 if (expld
.phase
== lang_fixed_phase_enum
)
1205 if (h
->type
== bfd_link_hash_defined
)
1207 expld
.result
.value
= h
->u
.def
.value
;
1208 expld
.result
.section
= h
->u
.def
.section
;
1213 h
->type
= bfd_link_hash_defined
;
1214 h
->u
.def
.value
= expld
.result
.value
;
1215 h
->u
.def
.section
= expld
.result
.section
;
1216 h
->linker_def
= ! tree
->assign
.type
.lineno
;
1217 h
->ldscript_def
= 1;
1218 h
->rel_from_abs
= expld
.rel_from_abs
;
1219 if (tree
->assign
.hidden
)
1220 bfd_link_hide_symbol (link_info
.output_bfd
,
1223 /* Copy the symbol type and set non_ir_ref_regular
1224 on the source if this is an expression only
1225 referencing a single symbol. (If the expression
1226 contains ternary conditions, ignoring symbols on
1228 if (expld
.assign_src
!= NULL
1229 && (expld
.assign_src
1230 != (struct bfd_link_hash_entry
*) -1))
1232 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
,
1233 h
, expld
.assign_src
);
1234 expld
.assign_src
->non_ir_ref_regular
= true;
1239 if (expld
.phase
!= lang_fixed_phase_enum
)
1240 expld
.assign_name
= NULL
;
1250 memset (&expld
.result
, 0, sizeof (expld
.result
));
1256 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
1258 expld
.rel_from_abs
= false;
1261 expld
.section
= current_section
;
1262 exp_fold_tree_1 (tree
);
1266 exp_fold_tree_no_dot (etree_type
*tree
)
1268 expld
.rel_from_abs
= false;
1271 expld
.section
= bfd_abs_section_ptr
;
1272 exp_fold_tree_1 (tree
);
1276 exp_value_fold (etree_type
*tree
)
1278 exp_fold_tree_no_dot (tree
);
1279 if (expld
.result
.valid_p
)
1281 tree
->type
.node_code
= INT
;
1282 tree
->value
.value
= expld
.result
.value
;
1283 tree
->value
.str
= NULL
;
1284 tree
->type
.node_class
= etree_value
;
1288 #define MAX(a, b) ((a) > (b) ? (a) : (b))
1291 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
1293 etree_type
*new_e
= stat_alloc (MAX (sizeof (new_e
->binary
),
1294 sizeof (new_e
->value
)));
1295 new_e
->type
.node_code
= code
;
1296 new_e
->type
.filename
= lhs
->type
.filename
;
1297 new_e
->type
.lineno
= lhs
->type
.lineno
;
1298 new_e
->binary
.lhs
= lhs
;
1299 new_e
->binary
.rhs
= rhs
;
1300 new_e
->type
.node_class
= etree_binary
;
1301 if (lhs
->type
.node_class
== etree_value
1302 && rhs
->type
.node_class
== etree_value
1304 && code
!= DATA_SEGMENT_ALIGN
1305 && code
!= DATA_SEGMENT_RELRO_END
)
1306 exp_value_fold (new_e
);
1311 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
1313 etree_type
*new_e
= stat_alloc (MAX (sizeof (new_e
->trinary
),
1314 sizeof (new_e
->value
)));
1315 new_e
->type
.node_code
= code
;
1316 new_e
->type
.filename
= cond
->type
.filename
;
1317 new_e
->type
.lineno
= cond
->type
.lineno
;
1318 new_e
->trinary
.lhs
= lhs
;
1319 new_e
->trinary
.cond
= cond
;
1320 new_e
->trinary
.rhs
= rhs
;
1321 new_e
->type
.node_class
= etree_trinary
;
1322 if (cond
->type
.node_class
== etree_value
1323 && lhs
->type
.node_class
== etree_value
1324 && rhs
->type
.node_class
== etree_value
)
1325 exp_value_fold (new_e
);
1330 exp_unop (int code
, etree_type
*child
)
1332 etree_type
*new_e
= stat_alloc (MAX (sizeof (new_e
->unary
),
1333 sizeof (new_e
->value
)));
1334 new_e
->unary
.type
.node_code
= code
;
1335 new_e
->unary
.type
.filename
= child
->type
.filename
;
1336 new_e
->unary
.type
.lineno
= child
->type
.lineno
;
1337 new_e
->unary
.child
= child
;
1338 new_e
->unary
.type
.node_class
= etree_unary
;
1339 if (child
->type
.node_class
== etree_value
1343 && code
!= DATA_SEGMENT_END
)
1344 exp_value_fold (new_e
);
1349 exp_nameop (int code
, const char *name
)
1351 etree_type
*new_e
= stat_alloc (sizeof (new_e
->name
));
1353 new_e
->name
.type
.node_code
= code
;
1354 new_e
->name
.type
.filename
= ldlex_filename ();
1355 new_e
->name
.type
.lineno
= lineno
;
1356 new_e
->name
.name
= name
;
1357 new_e
->name
.type
.node_class
= etree_name
;
1363 exp_assop (const char *dst
,
1365 enum node_tree_enum
class,
1370 n
= stat_alloc (sizeof (n
->assign
));
1371 n
->assign
.type
.node_code
= '=';
1372 n
->assign
.type
.filename
= src
->type
.filename
;
1373 n
->assign
.type
.lineno
= src
->type
.lineno
;
1374 n
->assign
.type
.node_class
= class;
1375 n
->assign
.src
= src
;
1376 n
->assign
.dst
= dst
;
1377 n
->assign
.hidden
= hidden
;
1381 /* Handle linker script assignments and HIDDEN. */
1384 exp_assign (const char *dst
, etree_type
*src
, bool hidden
)
1386 return exp_assop (dst
, src
, etree_assign
, hidden
);
1389 /* Handle --defsym command-line option. */
1392 exp_defsym (const char *dst
, etree_type
*src
)
1394 return exp_assop (dst
, src
, etree_assign
, false);
1397 /* Handle PROVIDE. */
1400 exp_provide (const char *dst
, etree_type
*src
, bool hidden
)
1402 return exp_assop (dst
, src
, etree_provide
, hidden
);
1405 /* Handle ASSERT. */
1408 exp_assert (etree_type
*exp
, const char *message
)
1412 n
= stat_alloc (sizeof (n
->assert_s
));
1413 n
->assert_s
.type
.node_code
= '!';
1414 n
->assert_s
.type
.filename
= exp
->type
.filename
;
1415 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1416 n
->assert_s
.type
.node_class
= etree_assert
;
1417 n
->assert_s
.child
= exp
;
1418 n
->assert_s
.message
= message
;
1423 exp_print_tree (etree_type
*tree
)
1427 if (config
.map_file
== NULL
)
1428 config
.map_file
= stderr
;
1432 minfo ("NULL TREE\n");
1436 switch (tree
->type
.node_class
)
1439 minfo ("0x%v", tree
->value
.value
);
1442 if (tree
->rel
.section
->owner
!= NULL
)
1443 minfo ("%pB:", tree
->rel
.section
->owner
);
1444 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1447 fputs (tree
->assign
.dst
, config
.map_file
);
1448 exp_print_token (tree
->type
.node_code
, true);
1449 exp_print_tree (tree
->assign
.src
);
1452 case etree_provided
:
1453 fprintf (config
.map_file
, "PROVIDE (%s = ", tree
->assign
.dst
);
1454 exp_print_tree (tree
->assign
.src
);
1455 fputc (')', config
.map_file
);
1458 function_like
= false;
1459 switch (tree
->type
.node_code
)
1464 case DATA_SEGMENT_ALIGN
:
1465 case DATA_SEGMENT_RELRO_END
:
1466 function_like
= true;
1469 /* Special handling because arguments are in reverse order and
1470 the segment name is quoted. */
1471 exp_print_token (tree
->type
.node_code
, false);
1472 fputs (" (\"", config
.map_file
);
1473 exp_print_tree (tree
->binary
.rhs
);
1474 fputs ("\", ", config
.map_file
);
1475 exp_print_tree (tree
->binary
.lhs
);
1476 fputc (')', config
.map_file
);
1481 exp_print_token (tree
->type
.node_code
, false);
1482 fputc (' ', config
.map_file
);
1484 fputc ('(', config
.map_file
);
1485 exp_print_tree (tree
->binary
.lhs
);
1487 fprintf (config
.map_file
, ", ");
1489 exp_print_token (tree
->type
.node_code
, true);
1490 exp_print_tree (tree
->binary
.rhs
);
1491 fputc (')', config
.map_file
);
1494 exp_print_tree (tree
->trinary
.cond
);
1495 fputc ('?', config
.map_file
);
1496 exp_print_tree (tree
->trinary
.lhs
);
1497 fputc (':', config
.map_file
);
1498 exp_print_tree (tree
->trinary
.rhs
);
1501 exp_print_token (tree
->unary
.type
.node_code
, false);
1502 if (tree
->unary
.child
)
1504 fprintf (config
.map_file
, " (");
1505 exp_print_tree (tree
->unary
.child
);
1506 fputc (')', config
.map_file
);
1511 fprintf (config
.map_file
, "ASSERT (");
1512 exp_print_tree (tree
->assert_s
.child
);
1513 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1517 if (tree
->type
.node_code
== NAME
)
1518 fputs (tree
->name
.name
, config
.map_file
);
1521 exp_print_token (tree
->type
.node_code
, false);
1522 if (tree
->name
.name
)
1523 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1533 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1537 exp_fold_tree_no_dot (tree
);
1538 if (expld
.result
.valid_p
)
1539 return expld
.result
.value
;
1540 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1541 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
1547 /* Return the smallest non-negative integer such that two raised to
1548 that power is at least as large as the vma evaluated at TREE, if
1549 TREE is a non-NULL expression that can be resolved. If TREE is
1550 NULL or cannot be resolved, return -1. */
1553 exp_get_power (etree_type
*tree
, char *name
)
1555 bfd_vma x
= exp_get_vma (tree
, -1, name
);
1559 if (x
== (bfd_vma
) -1)
1562 for (n
= 0, p2
= 1; p2
< x
; ++n
, p2
<<= 1)
1570 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1579 exp_fold_tree_no_dot (tree
);
1580 if (!expld
.result
.valid_p
)
1582 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1583 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
1588 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1592 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1593 fill
->size
= (len
+ 1) / 2;
1595 s
= (unsigned char *) expld
.result
.str
;
1603 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1617 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1618 val
= expld
.result
.value
;
1619 fill
->data
[0] = (val
>> 24) & 0xff;
1620 fill
->data
[1] = (val
>> 16) & 0xff;
1621 fill
->data
[2] = (val
>> 8) & 0xff;
1622 fill
->data
[3] = (val
>> 0) & 0xff;
1629 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1633 exp_fold_tree_no_dot (tree
);
1635 if (expld
.result
.valid_p
)
1637 if (expld
.result
.section
!= NULL
)
1638 expld
.result
.value
+= expld
.result
.section
->vma
;
1639 return expld
.result
.value
;
1641 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1643 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
1651 align_n (bfd_vma value
, bfd_vma align
)
1656 value
= (value
+ align
- 1) / align
;
1657 return value
* align
;
1663 /* The value "13" is ad-hoc, somewhat related to the expected number of
1664 assignments in a linker script. */
1665 if (!bfd_hash_table_init_n (&definedness_table
,
1666 definedness_newfunc
,
1667 sizeof (struct definedness_hash_entry
),
1669 einfo (_("%F%P: can not create hash table: %E\n"));
1672 /* Convert absolute symbols defined by a script from "dot" (also
1673 SEGMENT_START or ORIGIN) outside of an output section statement,
1674 to section relative. */
1677 set_sym_sections (struct bfd_hash_entry
*bh
, void *inf ATTRIBUTE_UNUSED
)
1679 struct definedness_hash_entry
*def
= (struct definedness_hash_entry
*) bh
;
1680 if (def
->final_sec
!= bfd_abs_section_ptr
)
1682 struct bfd_link_hash_entry
*h
;
1683 h
= bfd_link_hash_lookup (link_info
.hash
, bh
->string
,
1684 false, false, true);
1686 && h
->type
== bfd_link_hash_defined
1687 && h
->u
.def
.section
== bfd_abs_section_ptr
)
1689 h
->u
.def
.value
-= def
->final_sec
->vma
;
1690 h
->u
.def
.section
= def
->final_sec
;
1697 ldexp_finalize_syms (void)
1699 bfd_hash_traverse (&definedness_table
, set_sym_sections
, NULL
);
1702 /* Determine whether a symbol is going to remain absolute even after
1703 ldexp_finalize_syms() has run. */
1706 ldexp_is_final_sym_absolute (const struct bfd_link_hash_entry
*h
)
1708 if (h
->type
== bfd_link_hash_defined
1709 && h
->u
.def
.section
== bfd_abs_section_ptr
)
1711 const struct definedness_hash_entry
*def
;
1713 if (!h
->ldscript_def
)
1716 def
= symbol_defined (h
->root
.string
);
1718 return def
->final_sec
== bfd_abs_section_ptr
;
1727 bfd_hash_table_free (&definedness_table
);