1 /* This module handles expression trees.
2 Copyright (C) 1991-2019 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. It is also used in
53 making absolute symbols section relative late in the link. */
55 struct definedness_hash_entry
57 struct bfd_hash_entry root
;
59 /* If this symbol was assigned from "dot" outside of an output
60 section statement, the section we'd like it relative to. */
63 /* Low bits of iteration count. Symbols with matching iteration have
64 been defined in this pass over the script. */
65 unsigned int iteration
: 8;
67 /* Symbol was defined by an object file. */
68 unsigned int by_object
: 1;
71 static struct bfd_hash_table definedness_table
;
73 /* Print the string representation of the given token. Surround it
74 with spaces if INFIX_P is TRUE. */
77 exp_print_token (token_code_type code
, int infix_p
)
104 { LOG2CEIL
, "LOG2CEIL" },
105 { ALIGN_K
, "ALIGN" },
112 { SECTIONS
, "SECTIONS" },
113 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
114 { MEMORY
, "MEMORY" },
115 { DEFINED
, "DEFINED" },
116 { TARGET_K
, "TARGET" },
117 { SEARCH_DIR
, "SEARCH_DIR" },
121 { ALIGNOF
, "ALIGNOF" },
122 { SIZEOF
, "SIZEOF" },
124 { LOADADDR
, "LOADADDR" },
125 { CONSTANT
, "CONSTANT" },
126 { ABSOLUTE
, "ABSOLUTE" },
129 { ASSERT_K
, "ASSERT" },
130 { REL
, "relocatable" },
131 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
132 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
133 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
134 { ORIGIN
, "ORIGIN" },
135 { LENGTH
, "LENGTH" },
136 { SEGMENT_START
, "SEGMENT_START" }
140 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
141 if (table
[idx
].code
== code
)
145 fputc (' ', config
.map_file
);
147 if (idx
< ARRAY_SIZE (table
))
148 fputs (table
[idx
].name
, config
.map_file
);
150 fputc (code
, config
.map_file
);
152 fprintf (config
.map_file
, "<code %d>", code
);
155 fputc (' ', config
.map_file
);
161 bfd_vma value
= expld
.result
.value
;
163 bfd_boolean round_up
= FALSE
;
168 /* If more than one bit is set in the value we will need to round up. */
169 if ((value
> 1) && (value
& 1))
176 expld
.result
.section
= NULL
;
177 expld
.result
.value
= result
;
183 if (expld
.result
.section
!= NULL
)
184 expld
.result
.value
+= expld
.result
.section
->vma
;
185 expld
.result
.section
= bfd_abs_section_ptr
;
186 expld
.rel_from_abs
= FALSE
;
190 new_abs (bfd_vma value
)
192 expld
.result
.valid_p
= TRUE
;
193 expld
.result
.section
= bfd_abs_section_ptr
;
194 expld
.result
.value
= value
;
195 expld
.result
.str
= NULL
;
199 exp_intop (bfd_vma value
)
201 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
202 new_e
->type
.node_code
= INT
;
203 new_e
->type
.filename
= ldlex_filename ();
204 new_e
->type
.lineno
= lineno
;
205 new_e
->value
.value
= value
;
206 new_e
->value
.str
= NULL
;
207 new_e
->type
.node_class
= etree_value
;
212 exp_bigintop (bfd_vma value
, char *str
)
214 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
215 new_e
->type
.node_code
= INT
;
216 new_e
->type
.filename
= ldlex_filename ();
217 new_e
->type
.lineno
= lineno
;
218 new_e
->value
.value
= value
;
219 new_e
->value
.str
= str
;
220 new_e
->type
.node_class
= etree_value
;
224 /* Build an expression representing an unnamed relocatable value. */
227 exp_relop (asection
*section
, bfd_vma value
)
229 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
230 new_e
->type
.node_code
= REL
;
231 new_e
->type
.filename
= ldlex_filename ();
232 new_e
->type
.lineno
= lineno
;
233 new_e
->type
.node_class
= etree_rel
;
234 new_e
->rel
.section
= section
;
235 new_e
->rel
.value
= value
;
240 new_number (bfd_vma value
)
242 expld
.result
.valid_p
= TRUE
;
243 expld
.result
.value
= value
;
244 expld
.result
.str
= NULL
;
245 expld
.result
.section
= NULL
;
249 new_rel (bfd_vma value
, asection
*section
)
251 expld
.result
.valid_p
= TRUE
;
252 expld
.result
.value
= value
;
253 expld
.result
.str
= NULL
;
254 expld
.result
.section
= section
;
258 new_rel_from_abs (bfd_vma value
)
260 asection
*s
= expld
.section
;
262 expld
.rel_from_abs
= TRUE
;
263 expld
.result
.valid_p
= TRUE
;
264 expld
.result
.value
= value
- s
->vma
;
265 expld
.result
.str
= NULL
;
266 expld
.result
.section
= s
;
269 /* New-function for the definedness hash table. */
271 static struct bfd_hash_entry
*
272 definedness_newfunc (struct bfd_hash_entry
*entry
,
273 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
274 const char *name ATTRIBUTE_UNUSED
)
276 struct definedness_hash_entry
*ret
= (struct definedness_hash_entry
*) entry
;
279 ret
= (struct definedness_hash_entry
*)
280 bfd_hash_allocate (table
, sizeof (struct definedness_hash_entry
));
283 einfo (_("%F%P: 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 (_("%F%P: 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. */
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
->iteration
= lang_statement_iteration
;
332 defentry
->final_sec
= bfd_abs_section_ptr
;
333 if (expld
.phase
== lang_final_phase_enum
334 && expld
.rel_from_abs
335 && expld
.result
.section
== bfd_abs_section_ptr
)
336 defentry
->final_sec
= section_for_dot ();
341 fold_segment_end (seg_align_type
*seg
)
343 if (expld
.phase
== lang_first_phase_enum
344 || expld
.section
!= bfd_abs_section_ptr
)
346 expld
.result
.valid_p
= FALSE
;
348 else if (seg
->phase
== exp_seg_align_seen
349 || seg
->phase
== exp_seg_relro_seen
)
351 seg
->phase
= exp_seg_end_seen
;
352 seg
->end
= expld
.result
.value
;
354 else if (seg
->phase
== exp_seg_done
355 || seg
->phase
== exp_seg_adjust
356 || seg
->phase
== exp_seg_relro_adjust
)
361 expld
.result
.valid_p
= FALSE
;
365 fold_unary (etree_type
*tree
)
367 exp_fold_tree_1 (tree
->unary
.child
);
368 if (expld
.result
.valid_p
)
370 switch (tree
->type
.node_code
)
373 if (expld
.phase
!= lang_first_phase_enum
)
374 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
376 expld
.result
.valid_p
= FALSE
;
388 expld
.result
.value
= ~expld
.result
.value
;
392 expld
.result
.value
= !expld
.result
.value
;
396 expld
.result
.value
= -expld
.result
.value
;
400 /* Return next place aligned to value. */
401 if (expld
.phase
!= lang_first_phase_enum
)
404 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
407 expld
.result
.valid_p
= FALSE
;
410 case DATA_SEGMENT_END
:
411 fold_segment_end (&expld
.dataseg
);
421 /* Arithmetic operators, bitwise AND, bitwise OR and XOR keep the
422 section of one of their operands only when the other operand is a
423 plain number. Losing the section when operating on two symbols,
424 ie. a result of a plain number, is required for subtraction and
425 XOR. It's justifiable for the other operations on the grounds that
426 adding, multiplying etc. two section relative values does not
427 really make sense unless they are just treated as numbers.
428 The same argument could be made for many expressions involving one
429 symbol and a number. For example, "1 << x" and "100 / x" probably
430 should not be given the section of x. The trouble is that if we
431 fuss about such things the rules become complex and it is onerous
432 to document ld expression evaluation. */
434 arith_result_section (const etree_value_type
*lhs
)
436 if (expld
.result
.section
== lhs
->section
)
438 if (expld
.section
== bfd_abs_section_ptr
439 && !config
.sane_expr
)
440 /* Duplicate the insanity in exp_fold_tree_1 case etree_value. */
441 expld
.result
.section
= bfd_abs_section_ptr
;
443 expld
.result
.section
= NULL
;
448 fold_segment_align (seg_align_type
*seg
, etree_value_type
*lhs
)
450 seg
->relro
= exp_seg_relro_start
;
451 if (expld
.phase
== lang_first_phase_enum
452 || expld
.section
!= bfd_abs_section_ptr
)
453 expld
.result
.valid_p
= FALSE
;
456 bfd_vma maxpage
= lhs
->value
;
457 bfd_vma commonpage
= expld
.result
.value
;
459 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
460 if (seg
->phase
== exp_seg_relro_adjust
)
461 expld
.result
.value
= seg
->base
;
462 else if (seg
->phase
== exp_seg_adjust
)
464 if (commonpage
< maxpage
)
465 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
466 & (maxpage
- commonpage
));
470 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
471 if (seg
->phase
== exp_seg_done
)
475 else if (seg
->phase
== exp_seg_none
)
477 seg
->phase
= exp_seg_align_seen
;
478 seg
->base
= expld
.result
.value
;
479 seg
->pagesize
= commonpage
;
480 seg
->maxpagesize
= maxpage
;
484 expld
.result
.valid_p
= FALSE
;
490 fold_segment_relro_end (seg_align_type
*seg
, etree_value_type
*lhs
)
492 /* Operands swapped! XXX_SEGMENT_RELRO_END(offset,exp) has offset
493 in expld.result and exp in lhs. */
494 seg
->relro
= exp_seg_relro_end
;
495 seg
->relro_offset
= expld
.result
.value
;
496 if (expld
.phase
== lang_first_phase_enum
497 || expld
.section
!= bfd_abs_section_ptr
)
498 expld
.result
.valid_p
= FALSE
;
499 else if (seg
->phase
== exp_seg_align_seen
500 || seg
->phase
== exp_seg_adjust
501 || seg
->phase
== exp_seg_relro_adjust
502 || seg
->phase
== exp_seg_done
)
504 if (seg
->phase
== exp_seg_align_seen
505 || seg
->phase
== exp_seg_relro_adjust
)
506 seg
->relro_end
= lhs
->value
+ expld
.result
.value
;
508 if (seg
->phase
== exp_seg_relro_adjust
509 && (seg
->relro_end
& (seg
->pagesize
- 1)))
511 seg
->relro_end
+= seg
->pagesize
- 1;
512 seg
->relro_end
&= ~(seg
->pagesize
- 1);
513 expld
.result
.value
= seg
->relro_end
- expld
.result
.value
;
516 expld
.result
.value
= lhs
->value
;
518 if (seg
->phase
== exp_seg_align_seen
)
519 seg
->phase
= exp_seg_relro_seen
;
522 expld
.result
.valid_p
= FALSE
;
526 fold_binary (etree_type
*tree
)
528 etree_value_type lhs
;
529 exp_fold_tree_1 (tree
->binary
.lhs
);
531 /* The SEGMENT_START operator is special because its first
532 operand is a string, not the name of a symbol. Note that the
533 operands have been swapped, so binary.lhs is second (default)
534 operand, binary.rhs is first operand. */
535 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
537 bfd_vma value
= expld
.result
.value
;
538 const char *segment_name
;
541 /* Check to see if the user has overridden the default
543 segment_name
= tree
->binary
.rhs
->name
.name
;
544 for (seg
= segments
; seg
; seg
= seg
->next
)
545 if (strcmp (seg
->name
, segment_name
) == 0)
548 && config
.magic_demand_paged
549 && config
.maxpagesize
!= 0
550 && (seg
->value
% config
.maxpagesize
) != 0)
551 einfo (_("%P: warning: address of `%s' "
552 "isn't multiple of maximum page size\n"),
558 new_rel_from_abs (value
);
563 exp_fold_tree_1 (tree
->binary
.rhs
);
564 expld
.result
.valid_p
&= lhs
.valid_p
;
566 if (expld
.result
.valid_p
)
568 if (lhs
.section
!= expld
.result
.section
)
570 /* If the values are from different sections, and neither is
571 just a number, make both the source arguments absolute. */
572 if (expld
.result
.section
!= NULL
573 && lhs
.section
!= NULL
)
576 lhs
.value
+= lhs
.section
->vma
;
577 lhs
.section
= bfd_abs_section_ptr
;
580 /* If the rhs is just a number, keep the lhs section. */
581 else if (expld
.result
.section
== NULL
)
583 expld
.result
.section
= lhs
.section
;
584 /* Make this NULL so that we know one of the operands
585 was just a number, for later tests. */
589 /* At this point we know that both operands have the same
590 section, or at least one of them is a plain number. */
592 switch (tree
->type
.node_code
)
596 expld.result.value = lhs.value y expld.result.value; \
597 arith_result_section (&lhs); \
600 /* Comparison operators, logical AND, and logical OR always
601 return a plain number. */
604 expld.result.value = lhs.value y expld.result.value; \
605 expld.result.section = NULL; \
626 if (expld
.result
.value
!= 0)
627 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
628 % (bfd_signed_vma
) expld
.result
.value
);
629 else if (expld
.phase
!= lang_mark_phase_enum
)
630 einfo (_("%F%P:%pS %% by zero\n"), tree
->binary
.rhs
);
631 arith_result_section (&lhs
);
635 if (expld
.result
.value
!= 0)
636 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
637 / (bfd_signed_vma
) expld
.result
.value
);
638 else if (expld
.phase
!= lang_mark_phase_enum
)
639 einfo (_("%F%P:%pS / by zero\n"), tree
->binary
.rhs
);
640 arith_result_section (&lhs
);
644 if (lhs
.value
> expld
.result
.value
)
645 expld
.result
.value
= lhs
.value
;
649 if (lhs
.value
< expld
.result
.value
)
650 expld
.result
.value
= lhs
.value
;
654 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
657 case DATA_SEGMENT_ALIGN
:
658 fold_segment_align (&expld
.dataseg
, &lhs
);
661 case DATA_SEGMENT_RELRO_END
:
662 fold_segment_relro_end (&expld
.dataseg
, &lhs
);
672 fold_trinary (etree_type
*tree
)
674 struct bfd_link_hash_entry
*save
= expld
.assign_src
;
676 exp_fold_tree_1 (tree
->trinary
.cond
);
677 expld
.assign_src
= save
;
678 if (expld
.result
.valid_p
)
679 exp_fold_tree_1 (expld
.result
.value
681 : tree
->trinary
.rhs
);
685 fold_name (etree_type
*tree
)
687 struct bfd_link_hash_entry
*h
;
688 struct definedness_hash_entry
*def
;
690 memset (&expld
.result
, 0, sizeof (expld
.result
));
692 switch (tree
->type
.node_code
)
695 link_info
.load_phdrs
= 1;
696 if (expld
.phase
!= lang_first_phase_enum
)
698 bfd_vma hdr_size
= 0;
699 /* Don't find the real header size if only marking sections;
700 The bfd function may cache incorrect data. */
701 if (expld
.phase
!= lang_mark_phase_enum
)
702 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
703 new_number (hdr_size
);
708 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
712 new_number (h
!= NULL
713 && (h
->type
== bfd_link_hash_defined
714 || h
->type
== bfd_link_hash_defweak
715 || h
->type
== bfd_link_hash_common
)
717 || (def
= symbol_defined (tree
->name
.name
)) == NULL
719 || def
->iteration
== (lang_statement_iteration
& 255)));
723 if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
724 new_rel_from_abs (expld
.dot
);
727 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
732 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
733 else if (h
->type
== bfd_link_hash_defined
734 || h
->type
== bfd_link_hash_defweak
)
736 asection
*output_section
;
738 output_section
= h
->u
.def
.section
->output_section
;
739 if (output_section
== NULL
)
741 if (expld
.phase
<= lang_mark_phase_enum
)
742 new_rel (h
->u
.def
.value
, h
->u
.def
.section
);
744 einfo (_("%X%P:%pS: unresolvable symbol `%s'"
745 " referenced in expression\n"),
746 tree
, tree
->name
.name
);
748 else if (output_section
== bfd_abs_section_ptr
749 && (expld
.section
!= bfd_abs_section_ptr
750 || config
.sane_expr
))
751 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
753 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
756 else if (expld
.phase
== lang_final_phase_enum
757 || (expld
.phase
!= lang_mark_phase_enum
758 && expld
.assigning_to_dot
))
759 einfo (_("%F%P:%pS: undefined symbol `%s'"
760 " referenced in expression\n"),
761 tree
, tree
->name
.name
);
762 else if (h
->type
== bfd_link_hash_new
)
764 h
->type
= bfd_link_hash_undefined
;
765 h
->u
.undef
.abfd
= NULL
;
766 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
767 bfd_link_add_undef (link_info
.hash
, h
);
769 if (expld
.assign_src
== NULL
)
770 expld
.assign_src
= h
;
772 expld
.assign_src
= (struct bfd_link_hash_entry
*) - 1;
774 /* Self-assignment is only allowed for absolute symbols
775 defined in a linker script. */
776 if (expld
.assign_name
!= NULL
777 && strcmp (expld
.assign_name
, tree
->name
.name
) == 0
779 && (h
->type
== bfd_link_hash_defined
780 || h
->type
== bfd_link_hash_defweak
)
781 && h
->u
.def
.section
== bfd_abs_section_ptr
782 && (def
= symbol_defined (tree
->name
.name
)) != NULL
783 && def
->iteration
== (lang_statement_iteration
& 255)))
784 expld
.assign_name
= NULL
;
789 if (expld
.phase
!= lang_first_phase_enum
)
791 lang_output_section_statement_type
*os
;
793 os
= lang_output_section_find (tree
->name
.name
);
796 if (expld
.phase
== lang_final_phase_enum
)
797 einfo (_("%F%P:%pS: undefined section `%s'"
798 " referenced in expression\n"),
799 tree
, tree
->name
.name
);
801 else if (os
->processed_vma
)
802 new_rel (0, os
->bfd_section
);
807 if (expld
.phase
!= lang_first_phase_enum
)
809 lang_output_section_statement_type
*os
;
811 os
= lang_output_section_find (tree
->name
.name
);
814 if (expld
.phase
== lang_final_phase_enum
)
815 einfo (_("%F%P:%pS: undefined section `%s'"
816 " referenced in expression\n"),
817 tree
, tree
->name
.name
);
819 else if (os
->processed_lma
)
821 if (os
->load_base
== NULL
)
822 new_abs (os
->bfd_section
->lma
);
825 exp_fold_tree_1 (os
->load_base
);
826 if (expld
.result
.valid_p
)
835 if (expld
.phase
!= lang_first_phase_enum
)
837 lang_output_section_statement_type
*os
;
839 os
= lang_output_section_find (tree
->name
.name
);
842 if (expld
.phase
== lang_final_phase_enum
)
843 einfo (_("%F%P:%pS: undefined section `%s'"
844 " referenced in expression\n"),
845 tree
, tree
->name
.name
);
848 else if (os
->bfd_section
!= NULL
)
852 if (tree
->type
.node_code
== SIZEOF
)
853 val
= (os
->bfd_section
->size
854 / bfd_octets_per_byte (link_info
.output_bfd
));
856 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
867 if (expld
.phase
!= lang_first_phase_enum
)
869 lang_memory_region_type
*mem
;
871 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
873 new_number (mem
->length
);
875 einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
876 " referenced in expression\n"),
877 tree
, tree
->name
.name
);
883 if (expld
.phase
!= lang_first_phase_enum
)
885 lang_memory_region_type
*mem
;
887 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
889 new_rel_from_abs (mem
->origin
);
891 einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
892 " referenced in expression\n"),
893 tree
, tree
->name
.name
);
898 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
899 new_number (config
.maxpagesize
);
900 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
901 new_number (config
.commonpagesize
);
903 einfo (_("%F%P:%pS: unknown constant `%s' referenced in expression\n"),
904 tree
, tree
->name
.name
);
913 /* Return true if TREE is '.'. */
916 is_dot (const etree_type
*tree
)
918 return (tree
->type
.node_class
== etree_name
919 && tree
->type
.node_code
== NAME
920 && tree
->name
.name
[0] == '.'
921 && tree
->name
.name
[1] == 0);
924 /* Return true if TREE is a constant equal to VAL. */
927 is_value (const etree_type
*tree
, bfd_vma val
)
929 return (tree
->type
.node_class
== etree_value
930 && tree
->value
.value
== val
);
933 /* Return true if TREE is an absolute symbol equal to VAL defined in
937 is_sym_value (const etree_type
*tree
, bfd_vma val
)
939 struct bfd_link_hash_entry
*h
;
940 struct definedness_hash_entry
*def
;
942 return (tree
->type
.node_class
== etree_name
943 && tree
->type
.node_code
== NAME
944 && (def
= symbol_defined (tree
->name
.name
)) != NULL
945 && def
->iteration
== (lang_statement_iteration
& 255)
946 && (h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
949 FALSE
, FALSE
, TRUE
)) != NULL
951 && h
->type
== bfd_link_hash_defined
952 && h
->u
.def
.section
== bfd_abs_section_ptr
953 && h
->u
.def
.value
== val
);
956 /* Return true if TREE is ". != 0". */
959 is_dot_ne_0 (const etree_type
*tree
)
961 return (tree
->type
.node_class
== etree_binary
962 && tree
->type
.node_code
== NE
963 && is_dot (tree
->binary
.lhs
)
964 && is_value (tree
->binary
.rhs
, 0));
967 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
968 absolute constant with value 0 defined in a linker script. */
971 is_dot_plus_0 (const etree_type
*tree
)
973 return (tree
->type
.node_class
== etree_binary
974 && tree
->type
.node_code
== '+'
975 && is_dot (tree
->binary
.lhs
)
976 && (is_value (tree
->binary
.rhs
, 0)
977 || is_sym_value (tree
->binary
.rhs
, 0)));
980 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
983 is_align_conditional (const etree_type
*tree
)
985 if (tree
->type
.node_class
== etree_unary
986 && tree
->type
.node_code
== ALIGN_K
)
988 tree
= tree
->unary
.child
;
989 return (tree
->type
.node_class
== etree_trinary
990 && is_dot_ne_0 (tree
->trinary
.cond
)
991 && is_value (tree
->trinary
.rhs
, 1));
997 exp_fold_tree_1 (etree_type
*tree
)
1001 memset (&expld
.result
, 0, sizeof (expld
.result
));
1005 switch (tree
->type
.node_class
)
1008 if (expld
.section
== bfd_abs_section_ptr
1009 && !config
.sane_expr
)
1010 new_abs (tree
->value
.value
);
1012 new_number (tree
->value
.value
);
1013 expld
.result
.str
= tree
->value
.str
;
1017 if (expld
.phase
!= lang_first_phase_enum
)
1019 asection
*output_section
= tree
->rel
.section
->output_section
;
1020 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
1024 memset (&expld
.result
, 0, sizeof (expld
.result
));
1028 exp_fold_tree_1 (tree
->assert_s
.child
);
1029 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
1030 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
1042 fold_trinary (tree
);
1047 case etree_provided
:
1048 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
1050 if (tree
->type
.node_class
!= etree_assign
)
1051 einfo (_("%F%P:%pS can not PROVIDE assignment to"
1052 " location counter\n"), tree
);
1053 if (expld
.phase
!= lang_first_phase_enum
)
1055 /* Notify the folder that this is an assignment to dot. */
1056 expld
.assigning_to_dot
= TRUE
;
1057 exp_fold_tree_1 (tree
->assign
.src
);
1058 expld
.assigning_to_dot
= FALSE
;
1060 /* If we are assigning to dot inside an output section
1061 arrange to keep the section, except for certain
1062 expressions that evaluate to zero. We ignore . = 0,
1063 . = . + 0, and . = ALIGN (. != 0 ? expr : 1).
1064 We can't ignore all expressions that evaluate to zero
1065 because an otherwise empty section might have padding
1066 added by an alignment expression that changes with
1067 relaxation. Such a section might have zero size
1068 before relaxation and so be stripped incorrectly. */
1069 if (expld
.phase
== lang_mark_phase_enum
1070 && expld
.section
!= bfd_abs_section_ptr
1071 && expld
.section
!= bfd_und_section_ptr
1072 && !(expld
.result
.valid_p
1073 && expld
.result
.value
== 0
1074 && (is_value (tree
->assign
.src
, 0)
1075 || is_sym_value (tree
->assign
.src
, 0)
1076 || is_dot_plus_0 (tree
->assign
.src
)
1077 || is_align_conditional (tree
->assign
.src
))))
1078 expld
.section
->flags
|= SEC_KEEP
;
1080 if (!expld
.result
.valid_p
1081 || expld
.section
== bfd_und_section_ptr
)
1083 if (expld
.phase
!= lang_mark_phase_enum
)
1084 einfo (_("%F%P:%pS invalid assignment to"
1085 " location counter\n"), tree
);
1087 else if (expld
.dotp
== NULL
)
1088 einfo (_("%F%P:%pS assignment to location counter"
1089 " invalid outside of SECTIONS\n"), tree
);
1091 /* After allocation, assignment to dot should not be
1092 done inside an output section since allocation adds a
1093 padding statement that effectively duplicates the
1095 else if (expld
.phase
<= lang_allocating_phase_enum
1096 || expld
.section
== bfd_abs_section_ptr
)
1100 nextdot
= expld
.result
.value
;
1101 if (expld
.result
.section
!= NULL
)
1102 nextdot
+= expld
.result
.section
->vma
;
1104 nextdot
+= expld
.section
->vma
;
1105 if (nextdot
< expld
.dot
1106 && expld
.section
!= bfd_abs_section_ptr
)
1107 einfo (_("%F%P:%pS cannot move location counter backwards"
1108 " (from %V to %V)\n"),
1109 tree
, expld
.dot
, nextdot
);
1112 expld
.dot
= nextdot
;
1113 *expld
.dotp
= nextdot
;
1118 memset (&expld
.result
, 0, sizeof (expld
.result
));
1122 struct bfd_link_hash_entry
*h
= NULL
;
1124 if (tree
->type
.node_class
== etree_provide
)
1126 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1127 FALSE
, FALSE
, TRUE
);
1129 || !(h
->type
== bfd_link_hash_new
1130 || h
->type
== bfd_link_hash_undefined
1131 || h
->type
== bfd_link_hash_undefweak
1134 /* Do nothing. The symbol was never referenced, or
1135 was defined in some object file. Note that
1136 undefweak symbols are defined by PROVIDE. This
1137 is to support glibc use of __rela_iplt_start and
1138 similar weak references. */
1143 expld
.assign_name
= tree
->assign
.dst
;
1144 expld
.assign_src
= NULL
;
1145 exp_fold_tree_1 (tree
->assign
.src
);
1146 /* expld.assign_name remaining equal to tree->assign.dst
1147 below indicates the evaluation of tree->assign.src did
1148 not use the value of tree->assign.dst. We don't allow
1149 self assignment until the final phase for two reasons:
1150 1) Expressions are evaluated multiple times. With
1151 relaxation, the number of times may vary.
1152 2) Section relative symbol values cannot be correctly
1153 converted to absolute values, as is required by many
1154 expressions, until final section sizing is complete. */
1155 if (expld
.phase
== lang_final_phase_enum
1156 || expld
.phase
== lang_fixed_phase_enum
1157 || expld
.assign_name
!= NULL
)
1159 if (tree
->type
.node_class
== etree_provide
)
1160 tree
->type
.node_class
= etree_provided
;
1164 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1167 einfo (_("%F%P:%s: hash creation failed\n"),
1171 /* If the expression is not valid then fake a zero value. In
1172 the final phase any errors will already have been raised,
1173 in earlier phases we want to create this definition so
1174 that it can be seen by other expressions. */
1175 if (!expld
.result
.valid_p
1176 && h
->type
== bfd_link_hash_new
)
1178 expld
.result
.value
= 0;
1179 expld
.result
.section
= NULL
;
1180 expld
.result
.valid_p
= TRUE
;
1183 if (expld
.result
.valid_p
)
1185 if (expld
.result
.section
== NULL
)
1186 expld
.result
.section
= expld
.section
;
1187 if (!update_definedness (tree
->assign
.dst
, h
) && 0)
1189 /* Symbol was already defined. For now this error
1190 is disabled because it causes failures in the ld
1191 testsuite: ld-elf/var1, ld-scripts/defined5, and
1192 ld-scripts/pr14962. Some of these no doubt
1193 reflect scripts used in the wild. */
1194 (*link_info
.callbacks
->multiple_definition
)
1195 (&link_info
, h
, link_info
.output_bfd
,
1196 expld
.result
.section
, expld
.result
.value
);
1198 if (expld
.phase
== lang_fixed_phase_enum
)
1200 if (h
->type
== bfd_link_hash_defined
)
1202 expld
.result
.value
= h
->u
.def
.value
;
1203 expld
.result
.section
= h
->u
.def
.section
;
1208 h
->type
= bfd_link_hash_defined
;
1209 h
->u
.def
.value
= expld
.result
.value
;
1210 h
->u
.def
.section
= expld
.result
.section
;
1211 h
->linker_def
= ! tree
->assign
.type
.lineno
;
1212 h
->ldscript_def
= 1;
1213 h
->rel_from_abs
= expld
.rel_from_abs
;
1214 if (tree
->assign
.hidden
)
1215 bfd_link_hide_symbol (link_info
.output_bfd
,
1218 /* Copy the symbol type if this is an expression only
1219 referencing a single symbol. (If the expression
1220 contains ternary conditions, ignoring symbols on
1222 if (expld
.assign_src
!= NULL
1223 && (expld
.assign_src
1224 != (struct bfd_link_hash_entry
*) -1))
1225 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
,
1226 h
, expld
.assign_src
);
1230 if (expld
.phase
!= lang_fixed_phase_enum
)
1231 expld
.assign_name
= NULL
;
1241 memset (&expld
.result
, 0, sizeof (expld
.result
));
1247 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
1249 expld
.rel_from_abs
= FALSE
;
1252 expld
.section
= current_section
;
1253 exp_fold_tree_1 (tree
);
1257 exp_fold_tree_no_dot (etree_type
*tree
)
1259 expld
.rel_from_abs
= FALSE
;
1262 expld
.section
= bfd_abs_section_ptr
;
1263 exp_fold_tree_1 (tree
);
1267 exp_value_fold (etree_type
*tree
)
1269 exp_fold_tree_no_dot (tree
);
1270 if (expld
.result
.valid_p
)
1272 tree
->type
.node_code
= INT
;
1273 tree
->value
.value
= expld
.result
.value
;
1274 tree
->value
.str
= NULL
;
1275 tree
->type
.node_class
= etree_value
;
1279 #define MAX(a, b) ((a) > (b) ? (a) : (b))
1282 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
1284 etree_type
*new_e
= (etree_type
*) stat_alloc (MAX (sizeof (new_e
->binary
),
1285 sizeof (new_e
->value
)));
1286 new_e
->type
.node_code
= code
;
1287 new_e
->type
.filename
= lhs
->type
.filename
;
1288 new_e
->type
.lineno
= lhs
->type
.lineno
;
1289 new_e
->binary
.lhs
= lhs
;
1290 new_e
->binary
.rhs
= rhs
;
1291 new_e
->type
.node_class
= etree_binary
;
1292 if (lhs
->type
.node_class
== etree_value
1293 && rhs
->type
.node_class
== etree_value
1295 && code
!= DATA_SEGMENT_ALIGN
1296 && code
!= DATA_SEGMENT_RELRO_END
)
1297 exp_value_fold (new_e
);
1302 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
1304 etree_type
*new_e
= (etree_type
*) stat_alloc (MAX (sizeof (new_e
->trinary
),
1305 sizeof (new_e
->value
)));
1306 new_e
->type
.node_code
= code
;
1307 new_e
->type
.filename
= cond
->type
.filename
;
1308 new_e
->type
.lineno
= cond
->type
.lineno
;
1309 new_e
->trinary
.lhs
= lhs
;
1310 new_e
->trinary
.cond
= cond
;
1311 new_e
->trinary
.rhs
= rhs
;
1312 new_e
->type
.node_class
= etree_trinary
;
1313 if (cond
->type
.node_class
== etree_value
1314 && lhs
->type
.node_class
== etree_value
1315 && rhs
->type
.node_class
== etree_value
)
1316 exp_value_fold (new_e
);
1321 exp_unop (int code
, etree_type
*child
)
1323 etree_type
*new_e
= (etree_type
*) stat_alloc (MAX (sizeof (new_e
->unary
),
1324 sizeof (new_e
->value
)));
1325 new_e
->unary
.type
.node_code
= code
;
1326 new_e
->unary
.type
.filename
= child
->type
.filename
;
1327 new_e
->unary
.type
.lineno
= child
->type
.lineno
;
1328 new_e
->unary
.child
= child
;
1329 new_e
->unary
.type
.node_class
= etree_unary
;
1330 if (child
->type
.node_class
== etree_value
1334 && code
!= DATA_SEGMENT_END
)
1335 exp_value_fold (new_e
);
1340 exp_nameop (int code
, const char *name
)
1342 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
1344 new_e
->name
.type
.node_code
= code
;
1345 new_e
->name
.type
.filename
= ldlex_filename ();
1346 new_e
->name
.type
.lineno
= lineno
;
1347 new_e
->name
.name
= name
;
1348 new_e
->name
.type
.node_class
= etree_name
;
1354 exp_assop (const char *dst
,
1356 enum node_tree_enum
class,
1361 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
1362 n
->assign
.type
.node_code
= '=';
1363 n
->assign
.type
.filename
= src
->type
.filename
;
1364 n
->assign
.type
.lineno
= src
->type
.lineno
;
1365 n
->assign
.type
.node_class
= class;
1366 n
->assign
.src
= src
;
1367 n
->assign
.dst
= dst
;
1368 n
->assign
.hidden
= hidden
;
1372 /* Handle linker script assignments and HIDDEN. */
1375 exp_assign (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1377 return exp_assop (dst
, src
, etree_assign
, hidden
);
1380 /* Handle --defsym command-line option. */
1383 exp_defsym (const char *dst
, etree_type
*src
)
1385 return exp_assop (dst
, src
, etree_assign
, FALSE
);
1388 /* Handle PROVIDE. */
1391 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1393 return exp_assop (dst
, src
, etree_provide
, hidden
);
1396 /* Handle ASSERT. */
1399 exp_assert (etree_type
*exp
, const char *message
)
1403 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1404 n
->assert_s
.type
.node_code
= '!';
1405 n
->assert_s
.type
.filename
= exp
->type
.filename
;
1406 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1407 n
->assert_s
.type
.node_class
= etree_assert
;
1408 n
->assert_s
.child
= exp
;
1409 n
->assert_s
.message
= message
;
1414 exp_print_tree (etree_type
*tree
)
1416 bfd_boolean function_like
;
1418 if (config
.map_file
== NULL
)
1419 config
.map_file
= stderr
;
1423 minfo ("NULL TREE\n");
1427 switch (tree
->type
.node_class
)
1430 minfo ("0x%v", tree
->value
.value
);
1433 if (tree
->rel
.section
->owner
!= NULL
)
1434 minfo ("%pB:", tree
->rel
.section
->owner
);
1435 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1438 fputs (tree
->assign
.dst
, config
.map_file
);
1439 exp_print_token (tree
->type
.node_code
, TRUE
);
1440 exp_print_tree (tree
->assign
.src
);
1443 case etree_provided
:
1444 fprintf (config
.map_file
, "PROVIDE (%s = ", tree
->assign
.dst
);
1445 exp_print_tree (tree
->assign
.src
);
1446 fputc (')', config
.map_file
);
1449 function_like
= FALSE
;
1450 switch (tree
->type
.node_code
)
1455 case DATA_SEGMENT_ALIGN
:
1456 case DATA_SEGMENT_RELRO_END
:
1457 function_like
= TRUE
;
1460 /* Special handling because arguments are in reverse order and
1461 the segment name is quoted. */
1462 exp_print_token (tree
->type
.node_code
, FALSE
);
1463 fputs (" (\"", config
.map_file
);
1464 exp_print_tree (tree
->binary
.rhs
);
1465 fputs ("\", ", config
.map_file
);
1466 exp_print_tree (tree
->binary
.lhs
);
1467 fputc (')', config
.map_file
);
1472 exp_print_token (tree
->type
.node_code
, FALSE
);
1473 fputc (' ', config
.map_file
);
1475 fputc ('(', config
.map_file
);
1476 exp_print_tree (tree
->binary
.lhs
);
1478 fprintf (config
.map_file
, ", ");
1480 exp_print_token (tree
->type
.node_code
, TRUE
);
1481 exp_print_tree (tree
->binary
.rhs
);
1482 fputc (')', config
.map_file
);
1485 exp_print_tree (tree
->trinary
.cond
);
1486 fputc ('?', config
.map_file
);
1487 exp_print_tree (tree
->trinary
.lhs
);
1488 fputc (':', config
.map_file
);
1489 exp_print_tree (tree
->trinary
.rhs
);
1492 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1493 if (tree
->unary
.child
)
1495 fprintf (config
.map_file
, " (");
1496 exp_print_tree (tree
->unary
.child
);
1497 fputc (')', config
.map_file
);
1502 fprintf (config
.map_file
, "ASSERT (");
1503 exp_print_tree (tree
->assert_s
.child
);
1504 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1508 if (tree
->type
.node_code
== NAME
)
1509 fputs (tree
->name
.name
, config
.map_file
);
1512 exp_print_token (tree
->type
.node_code
, FALSE
);
1513 if (tree
->name
.name
)
1514 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1524 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1528 exp_fold_tree_no_dot (tree
);
1529 if (expld
.result
.valid_p
)
1530 return expld
.result
.value
;
1531 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1532 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
1538 /* Return the smallest non-negative integer such that two raised to
1539 that power is at least as large as the vma evaluated at TREE, if
1540 TREE is a non-NULL expression that can be resolved. If TREE is
1541 NULL or cannot be resolved, return -1. */
1544 exp_get_power (etree_type
*tree
, char *name
)
1546 bfd_vma x
= exp_get_vma (tree
, -1, name
);
1550 if (x
== (bfd_vma
) -1)
1553 for (n
= 0, p2
= 1; p2
< x
; ++n
, p2
<<= 1)
1561 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1570 exp_fold_tree_no_dot (tree
);
1571 if (!expld
.result
.valid_p
)
1573 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1574 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
1579 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1583 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1584 fill
->size
= (len
+ 1) / 2;
1586 s
= (unsigned char *) expld
.result
.str
;
1594 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1608 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1609 val
= expld
.result
.value
;
1610 fill
->data
[0] = (val
>> 24) & 0xff;
1611 fill
->data
[1] = (val
>> 16) & 0xff;
1612 fill
->data
[2] = (val
>> 8) & 0xff;
1613 fill
->data
[3] = (val
>> 0) & 0xff;
1620 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1624 exp_fold_tree_no_dot (tree
);
1626 if (expld
.result
.valid_p
)
1628 if (expld
.result
.section
!= NULL
)
1629 expld
.result
.value
+= expld
.result
.section
->vma
;
1630 return expld
.result
.value
;
1632 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1634 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
1642 align_n (bfd_vma value
, bfd_vma align
)
1647 value
= (value
+ align
- 1) / align
;
1648 return value
* align
;
1654 /* The value "13" is ad-hoc, somewhat related to the expected number of
1655 assignments in a linker script. */
1656 if (!bfd_hash_table_init_n (&definedness_table
,
1657 definedness_newfunc
,
1658 sizeof (struct definedness_hash_entry
),
1660 einfo (_("%F%P: can not create hash table: %E\n"));
1663 /* Convert absolute symbols defined by a script from "dot" (also
1664 SEGMENT_START or ORIGIN) outside of an output section statement,
1665 to section relative. */
1668 set_sym_sections (struct bfd_hash_entry
*bh
, void *inf ATTRIBUTE_UNUSED
)
1670 struct definedness_hash_entry
*def
= (struct definedness_hash_entry
*) bh
;
1671 if (def
->final_sec
!= bfd_abs_section_ptr
)
1673 struct bfd_link_hash_entry
*h
;
1674 h
= bfd_link_hash_lookup (link_info
.hash
, bh
->string
,
1675 FALSE
, FALSE
, TRUE
);
1677 && h
->type
== bfd_link_hash_defined
1678 && h
->u
.def
.section
== bfd_abs_section_ptr
)
1680 h
->u
.def
.value
-= def
->final_sec
->vma
;
1681 h
->u
.def
.section
= def
->final_sec
;
1688 ldexp_finalize_syms (void)
1690 bfd_hash_traverse (&definedness_table
, set_sym_sections
, NULL
);
1696 bfd_hash_table_free (&definedness_table
);