1 /* This module handles expression trees.
2 Copyright (C) 1991-2016 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 /* Symbol was defined by an object file. */
64 unsigned int by_object
: 1;
66 /* Symbols was defined by a script. */
67 unsigned int by_script
: 1;
69 /* Low bit of iteration count. Symbols with matching iteration have
70 been defined in this pass over the script. */
71 unsigned int iteration
: 1;
74 static struct bfd_hash_table definedness_table
;
76 /* Print the string representation of the given token. Surround it
77 with spaces if INFIX_P is TRUE. */
80 exp_print_token (token_code_type code
, int infix_p
)
107 { LOG2CEIL
, "LOG2CEIL" },
108 { ALIGN_K
, "ALIGN" },
115 { SECTIONS
, "SECTIONS" },
116 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
117 { MEMORY
, "MEMORY" },
118 { DEFINED
, "DEFINED" },
119 { TARGET_K
, "TARGET" },
120 { SEARCH_DIR
, "SEARCH_DIR" },
124 { ALIGNOF
, "ALIGNOF" },
125 { SIZEOF
, "SIZEOF" },
127 { LOADADDR
, "LOADADDR" },
128 { CONSTANT
, "CONSTANT" },
129 { ABSOLUTE
, "ABSOLUTE" },
132 { ASSERT_K
, "ASSERT" },
133 { REL
, "relocatable" },
134 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
135 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
136 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
137 { ORIGIN
, "ORIGIN" },
138 { LENGTH
, "LENGTH" },
139 { SEGMENT_START
, "SEGMENT_START" }
143 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
144 if (table
[idx
].code
== code
)
148 fputc (' ', config
.map_file
);
150 if (idx
< ARRAY_SIZE (table
))
151 fputs (table
[idx
].name
, config
.map_file
);
153 fputc (code
, config
.map_file
);
155 fprintf (config
.map_file
, "<code %d>", code
);
158 fputc (' ', config
.map_file
);
164 bfd_vma value
= expld
.result
.value
;
166 bfd_boolean round_up
= FALSE
;
171 /* If more than one bit is set in the value we will need to round up. */
172 if ((value
> 1) && (value
& 1))
179 expld
.result
.section
= NULL
;
180 expld
.result
.value
= result
;
186 if (expld
.result
.section
!= NULL
)
187 expld
.result
.value
+= expld
.result
.section
->vma
;
188 expld
.result
.section
= bfd_abs_section_ptr
;
189 expld
.rel_from_abs
= FALSE
;
193 new_abs (bfd_vma value
)
195 expld
.result
.valid_p
= TRUE
;
196 expld
.result
.section
= bfd_abs_section_ptr
;
197 expld
.result
.value
= value
;
198 expld
.result
.str
= NULL
;
202 exp_intop (bfd_vma value
)
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
= NULL
;
210 new_e
->type
.node_class
= etree_value
;
215 exp_bigintop (bfd_vma value
, char *str
)
217 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
218 new_e
->type
.node_code
= INT
;
219 new_e
->type
.filename
= ldlex_filename ();
220 new_e
->type
.lineno
= lineno
;
221 new_e
->value
.value
= value
;
222 new_e
->value
.str
= str
;
223 new_e
->type
.node_class
= etree_value
;
227 /* Build an expression representing an unnamed relocatable value. */
230 exp_relop (asection
*section
, bfd_vma value
)
232 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
233 new_e
->type
.node_code
= REL
;
234 new_e
->type
.filename
= ldlex_filename ();
235 new_e
->type
.lineno
= lineno
;
236 new_e
->type
.node_class
= etree_rel
;
237 new_e
->rel
.section
= section
;
238 new_e
->rel
.value
= value
;
243 new_number (bfd_vma value
)
245 expld
.result
.valid_p
= TRUE
;
246 expld
.result
.value
= value
;
247 expld
.result
.str
= NULL
;
248 expld
.result
.section
= NULL
;
252 new_rel (bfd_vma value
, asection
*section
)
254 expld
.result
.valid_p
= TRUE
;
255 expld
.result
.value
= value
;
256 expld
.result
.str
= NULL
;
257 expld
.result
.section
= section
;
261 new_rel_from_abs (bfd_vma value
)
263 asection
*s
= expld
.section
;
265 expld
.rel_from_abs
= TRUE
;
266 expld
.result
.valid_p
= TRUE
;
267 expld
.result
.value
= value
- s
->vma
;
268 expld
.result
.str
= NULL
;
269 expld
.result
.section
= s
;
272 /* New-function for the definedness hash table. */
274 static struct bfd_hash_entry
*
275 definedness_newfunc (struct bfd_hash_entry
*entry
,
276 struct bfd_hash_table
*table ATTRIBUTE_UNUSED
,
277 const char *name ATTRIBUTE_UNUSED
)
279 struct definedness_hash_entry
*ret
= (struct definedness_hash_entry
*) entry
;
282 ret
= (struct definedness_hash_entry
*)
283 bfd_hash_allocate (table
, sizeof (struct definedness_hash_entry
));
286 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name
);
294 /* Called during processing of linker script script expressions.
295 For symbols assigned in a linker script, return a struct describing
296 where the symbol is defined relative to the current expression,
297 otherwise return NULL. */
299 static struct definedness_hash_entry
*
300 symbol_defined (const char *name
)
302 return ((struct definedness_hash_entry
*)
303 bfd_hash_lookup (&definedness_table
, name
, FALSE
, FALSE
));
306 /* Update the definedness state of NAME. Return FALSE if script symbol
307 is multiply defining a strong symbol in an object. */
310 update_definedness (const char *name
, struct bfd_link_hash_entry
*h
)
313 struct definedness_hash_entry
*defentry
314 = (struct definedness_hash_entry
*)
315 bfd_hash_lookup (&definedness_table
, name
, TRUE
, FALSE
);
317 if (defentry
== NULL
)
318 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name
);
320 /* If the symbol was already defined, and not by a script, then it
321 must be defined by an object file or by the linker target code. */
323 if (!defentry
->by_script
324 && (h
->type
== bfd_link_hash_defined
325 || h
->type
== bfd_link_hash_defweak
326 || h
->type
== bfd_link_hash_common
))
328 defentry
->by_object
= 1;
329 if (h
->type
== bfd_link_hash_defined
330 && h
->u
.def
.section
->output_section
!= NULL
335 defentry
->by_script
= 1;
336 defentry
->iteration
= lang_statement_iteration
;
337 defentry
->final_sec
= bfd_abs_section_ptr
;
338 if (expld
.phase
== lang_final_phase_enum
339 && expld
.rel_from_abs
340 && expld
.result
.section
== bfd_abs_section_ptr
)
341 defentry
->final_sec
= section_for_dot ();
346 fold_unary (etree_type
*tree
)
348 exp_fold_tree_1 (tree
->unary
.child
);
349 if (expld
.result
.valid_p
)
351 switch (tree
->type
.node_code
)
354 if (expld
.phase
!= lang_first_phase_enum
)
355 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
357 expld
.result
.valid_p
= FALSE
;
369 expld
.result
.value
= ~expld
.result
.value
;
373 expld
.result
.value
= !expld
.result
.value
;
377 expld
.result
.value
= -expld
.result
.value
;
381 /* Return next place aligned to value. */
382 if (expld
.phase
!= lang_first_phase_enum
)
385 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
388 expld
.result
.valid_p
= FALSE
;
391 case DATA_SEGMENT_END
:
392 if (expld
.phase
== lang_first_phase_enum
393 || expld
.section
!= bfd_abs_section_ptr
)
395 expld
.result
.valid_p
= FALSE
;
397 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
398 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
400 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
401 expld
.dataseg
.end
= expld
.result
.value
;
403 else if (expld
.dataseg
.phase
== exp_dataseg_done
404 || expld
.dataseg
.phase
== exp_dataseg_adjust
405 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
410 expld
.result
.valid_p
= FALSE
;
420 /* Arithmetic operators, bitwise AND, bitwise OR and XOR keep the
421 section of one of their operands only when the other operand is a
422 plain number. Losing the section when operating on two symbols,
423 ie. a result of a plain number, is required for subtraction and
424 XOR. It's justifiable for the other operations on the grounds that
425 adding, multiplying etc. two section relative values does not
426 really make sense unless they are just treated as numbers.
427 The same argument could be made for many expressions involving one
428 symbol and a number. For example, "1 << x" and "100 / x" probably
429 should not be given the section of x. The trouble is that if we
430 fuss about such things the rules become complex and it is onerous
431 to document ld expression evaluation. */
433 arith_result_section (const etree_value_type
*lhs
)
435 if (expld
.result
.section
== lhs
->section
)
437 if (expld
.section
== bfd_abs_section_ptr
438 && !config
.sane_expr
)
439 /* Duplicate the insanity in exp_fold_tree_1 case etree_value. */
440 expld
.result
.section
= bfd_abs_section_ptr
;
442 expld
.result
.section
= NULL
;
447 fold_binary (etree_type
*tree
)
449 etree_value_type lhs
;
450 exp_fold_tree_1 (tree
->binary
.lhs
);
452 /* The SEGMENT_START operator is special because its first
453 operand is a string, not the name of a symbol. Note that the
454 operands have been swapped, so binary.lhs is second (default)
455 operand, binary.rhs is first operand. */
456 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
458 const char *segment_name
;
461 /* Check to see if the user has overridden the default
463 segment_name
= tree
->binary
.rhs
->name
.name
;
464 for (seg
= segments
; seg
; seg
= seg
->next
)
465 if (strcmp (seg
->name
, segment_name
) == 0)
468 && config
.magic_demand_paged
469 && (seg
->value
% config
.maxpagesize
) != 0)
470 einfo (_("%P: warning: address of `%s' "
471 "isn't multiple of maximum page size\n"),
474 new_rel_from_abs (seg
->value
);
481 exp_fold_tree_1 (tree
->binary
.rhs
);
482 expld
.result
.valid_p
&= lhs
.valid_p
;
484 if (expld
.result
.valid_p
)
486 if (lhs
.section
!= expld
.result
.section
)
488 /* If the values are from different sections, and neither is
489 just a number, make both the source arguments absolute. */
490 if (expld
.result
.section
!= NULL
491 && lhs
.section
!= NULL
)
494 lhs
.value
+= lhs
.section
->vma
;
495 lhs
.section
= bfd_abs_section_ptr
;
498 /* If the rhs is just a number, keep the lhs section. */
499 else if (expld
.result
.section
== NULL
)
501 expld
.result
.section
= lhs
.section
;
502 /* Make this NULL so that we know one of the operands
503 was just a number, for later tests. */
507 /* At this point we know that both operands have the same
508 section, or at least one of them is a plain number. */
510 switch (tree
->type
.node_code
)
514 expld.result.value = lhs.value y expld.result.value; \
515 arith_result_section (&lhs); \
518 /* Comparison operators, logical AND, and logical OR always
519 return a plain number. */
522 expld.result.value = lhs.value y expld.result.value; \
523 expld.result.section = NULL; \
544 if (expld
.result
.value
!= 0)
545 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
546 % (bfd_signed_vma
) expld
.result
.value
);
547 else if (expld
.phase
!= lang_mark_phase_enum
)
548 einfo (_("%F%S %% by zero\n"), tree
->binary
.rhs
);
549 arith_result_section (&lhs
);
553 if (expld
.result
.value
!= 0)
554 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
555 / (bfd_signed_vma
) expld
.result
.value
);
556 else if (expld
.phase
!= lang_mark_phase_enum
)
557 einfo (_("%F%S / by zero\n"), tree
->binary
.rhs
);
558 arith_result_section (&lhs
);
562 if (lhs
.value
> expld
.result
.value
)
563 expld
.result
.value
= lhs
.value
;
567 if (lhs
.value
< expld
.result
.value
)
568 expld
.result
.value
= lhs
.value
;
572 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
575 case DATA_SEGMENT_ALIGN
:
576 expld
.dataseg
.relro
= exp_dataseg_relro_start
;
577 if (expld
.phase
== lang_first_phase_enum
578 || expld
.section
!= bfd_abs_section_ptr
)
579 expld
.result
.valid_p
= FALSE
;
582 bfd_vma maxpage
= lhs
.value
;
583 bfd_vma commonpage
= expld
.result
.value
;
585 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
586 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
587 expld
.result
.value
= expld
.dataseg
.base
;
588 else if (expld
.dataseg
.phase
== exp_dataseg_adjust
)
590 if (commonpage
< maxpage
)
591 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
592 & (maxpage
- commonpage
));
596 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
597 if (expld
.dataseg
.phase
== exp_dataseg_done
)
601 else if (expld
.dataseg
.phase
== exp_dataseg_none
)
603 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
604 expld
.dataseg
.base
= expld
.result
.value
;
605 expld
.dataseg
.pagesize
= commonpage
;
606 expld
.dataseg
.maxpagesize
= maxpage
;
607 expld
.dataseg
.relro_end
= 0;
610 expld
.result
.valid_p
= FALSE
;
615 case DATA_SEGMENT_RELRO_END
:
616 /* Operands swapped! DATA_SEGMENT_RELRO_END(offset,exp)
617 has offset in expld.result and exp in lhs. */
618 expld
.dataseg
.relro
= exp_dataseg_relro_end
;
619 expld
.dataseg
.relro_offset
= expld
.result
.value
;
620 if (expld
.phase
== lang_first_phase_enum
621 || expld
.section
!= bfd_abs_section_ptr
)
622 expld
.result
.valid_p
= FALSE
;
623 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
624 || expld
.dataseg
.phase
== exp_dataseg_adjust
625 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
626 || expld
.dataseg
.phase
== exp_dataseg_done
)
628 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
629 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
630 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
632 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
633 && (expld
.dataseg
.relro_end
634 & (expld
.dataseg
.pagesize
- 1)))
636 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
637 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
638 expld
.result
.value
= (expld
.dataseg
.relro_end
639 - expld
.result
.value
);
642 expld
.result
.value
= lhs
.value
;
644 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
645 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
648 expld
.result
.valid_p
= FALSE
;
658 fold_trinary (etree_type
*tree
)
660 exp_fold_tree_1 (tree
->trinary
.cond
);
661 if (expld
.result
.valid_p
)
662 exp_fold_tree_1 (expld
.result
.value
664 : tree
->trinary
.rhs
);
668 fold_name (etree_type
*tree
)
670 memset (&expld
.result
, 0, sizeof (expld
.result
));
672 switch (tree
->type
.node_code
)
675 if (expld
.phase
!= lang_first_phase_enum
)
677 bfd_vma hdr_size
= 0;
678 /* Don't find the real header size if only marking sections;
679 The bfd function may cache incorrect data. */
680 if (expld
.phase
!= lang_mark_phase_enum
)
681 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
682 new_number (hdr_size
);
687 if (expld
.phase
!= lang_first_phase_enum
)
689 struct bfd_link_hash_entry
*h
;
690 struct definedness_hash_entry
*def
;
692 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
696 new_number (h
!= NULL
697 && (h
->type
== bfd_link_hash_defined
698 || h
->type
== bfd_link_hash_defweak
699 || h
->type
== bfd_link_hash_common
)
700 && ((def
= symbol_defined (tree
->name
.name
)) == NULL
702 || def
->iteration
== (lang_statement_iteration
& 1)));
707 if (expld
.assign_name
!= NULL
708 && strcmp (expld
.assign_name
, tree
->name
.name
) == 0)
710 /* Self-assignment is only allowed for absolute symbols
711 defined in a linker script. */
712 struct bfd_link_hash_entry
*h
;
713 struct definedness_hash_entry
*def
;
715 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
720 && (h
->type
== bfd_link_hash_defined
721 || h
->type
== bfd_link_hash_defweak
)
722 && h
->u
.def
.section
== bfd_abs_section_ptr
723 && (def
= symbol_defined (tree
->name
.name
)) != NULL
724 && def
->iteration
== (lang_statement_iteration
& 1)))
725 expld
.assign_name
= NULL
;
727 if (expld
.phase
== lang_first_phase_enum
)
729 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
730 new_rel_from_abs (expld
.dot
);
733 struct bfd_link_hash_entry
*h
;
735 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
740 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
741 else if (h
->type
== bfd_link_hash_defined
742 || h
->type
== bfd_link_hash_defweak
)
744 asection
*output_section
;
746 output_section
= h
->u
.def
.section
->output_section
;
747 if (output_section
== NULL
)
749 if (expld
.phase
== lang_mark_phase_enum
)
750 new_rel (h
->u
.def
.value
, h
->u
.def
.section
);
752 einfo (_("%X%S: unresolvable symbol `%s'"
753 " referenced in expression\n"),
754 tree
, tree
->name
.name
);
756 else if (output_section
== bfd_abs_section_ptr
757 && (expld
.section
!= bfd_abs_section_ptr
758 || config
.sane_expr
))
759 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
761 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
764 else if (expld
.phase
== lang_final_phase_enum
765 || (expld
.phase
!= lang_mark_phase_enum
766 && expld
.assigning_to_dot
))
767 einfo (_("%F%S: undefined symbol `%s'"
768 " referenced in expression\n"),
769 tree
, tree
->name
.name
);
770 else if (h
->type
== bfd_link_hash_new
)
772 h
->type
= bfd_link_hash_undefined
;
773 h
->u
.undef
.abfd
= NULL
;
774 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
775 bfd_link_add_undef (link_info
.hash
, h
);
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_vma
)
794 new_rel (0, os
->bfd_section
);
799 if (expld
.phase
!= lang_first_phase_enum
)
801 lang_output_section_statement_type
*os
;
803 os
= lang_output_section_find (tree
->name
.name
);
806 if (expld
.phase
== lang_final_phase_enum
)
807 einfo (_("%F%S: undefined section `%s'"
808 " referenced in expression\n"),
809 tree
, tree
->name
.name
);
811 else if (os
->processed_lma
)
813 if (os
->load_base
== NULL
)
814 new_abs (os
->bfd_section
->lma
);
817 exp_fold_tree_1 (os
->load_base
);
818 if (expld
.result
.valid_p
)
827 if (expld
.phase
!= lang_first_phase_enum
)
829 lang_output_section_statement_type
*os
;
831 os
= lang_output_section_find (tree
->name
.name
);
834 if (expld
.phase
== lang_final_phase_enum
)
835 einfo (_("%F%S: undefined section `%s'"
836 " referenced in expression\n"),
837 tree
, tree
->name
.name
);
840 else if (os
->bfd_section
!= NULL
)
844 if (tree
->type
.node_code
== SIZEOF
)
845 val
= (os
->bfd_section
->size
846 / bfd_octets_per_byte (link_info
.output_bfd
));
848 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
859 if (expld
.phase
!= lang_first_phase_enum
)
861 lang_memory_region_type
*mem
;
863 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
865 new_number (mem
->length
);
867 einfo (_("%F%S: undefined MEMORY region `%s'"
868 " referenced in expression\n"),
869 tree
, tree
->name
.name
);
875 if (expld
.phase
!= lang_first_phase_enum
)
877 lang_memory_region_type
*mem
;
879 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
881 new_rel_from_abs (mem
->origin
);
883 einfo (_("%F%S: undefined MEMORY region `%s'"
884 " referenced in expression\n"),
885 tree
, tree
->name
.name
);
890 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
891 new_number (config
.maxpagesize
);
892 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
893 new_number (config
.commonpagesize
);
895 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
896 tree
, tree
->name
.name
);
905 /* Return true if TREE is '.'. */
908 is_dot (const etree_type
*tree
)
910 return (tree
->type
.node_class
== etree_name
911 && tree
->type
.node_code
== NAME
912 && tree
->name
.name
[0] == '.'
913 && tree
->name
.name
[1] == 0);
916 /* Return true if TREE is a constant equal to VAL. */
919 is_value (const etree_type
*tree
, bfd_vma val
)
921 return (tree
->type
.node_class
== etree_value
922 && tree
->value
.value
== val
);
925 /* Return true if TREE is an absolute symbol equal to VAL defined in
929 is_sym_value (const etree_type
*tree
, bfd_vma val
)
931 struct bfd_link_hash_entry
*h
;
932 struct definedness_hash_entry
*def
;
934 return (tree
->type
.node_class
== etree_name
935 && tree
->type
.node_code
== NAME
936 && (def
= symbol_defined (tree
->name
.name
)) != NULL
938 && def
->iteration
== (lang_statement_iteration
& 1)
939 && (h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
942 FALSE
, FALSE
, TRUE
)) != NULL
943 && h
->type
== bfd_link_hash_defined
944 && h
->u
.def
.section
== bfd_abs_section_ptr
945 && h
->u
.def
.value
== val
);
948 /* Return true if TREE is ". != 0". */
951 is_dot_ne_0 (const etree_type
*tree
)
953 return (tree
->type
.node_class
== etree_binary
954 && tree
->type
.node_code
== NE
955 && is_dot (tree
->binary
.lhs
)
956 && is_value (tree
->binary
.rhs
, 0));
959 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
960 absolute constant with value 0 defined in a linker script. */
963 is_dot_plus_0 (const etree_type
*tree
)
965 return (tree
->type
.node_class
== etree_binary
966 && tree
->type
.node_code
== '+'
967 && is_dot (tree
->binary
.lhs
)
968 && (is_value (tree
->binary
.rhs
, 0)
969 || is_sym_value (tree
->binary
.rhs
, 0)));
972 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
975 is_align_conditional (const etree_type
*tree
)
977 if (tree
->type
.node_class
== etree_unary
978 && tree
->type
.node_code
== ALIGN_K
)
980 tree
= tree
->unary
.child
;
981 return (tree
->type
.node_class
== etree_trinary
982 && is_dot_ne_0 (tree
->trinary
.cond
)
983 && is_value (tree
->trinary
.rhs
, 1));
988 /* Subroutine of exp_fold_tree_1 for copying a symbol type. */
991 try_copy_symbol_type (struct bfd_link_hash_entry
*h
, etree_type
*src
)
993 if (src
->type
.node_class
== etree_name
)
995 struct bfd_link_hash_entry
*hsrc
;
997 hsrc
= bfd_link_hash_lookup (link_info
.hash
, src
->name
.name
,
1000 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
, h
,
1006 exp_fold_tree_1 (etree_type
*tree
)
1010 memset (&expld
.result
, 0, sizeof (expld
.result
));
1014 switch (tree
->type
.node_class
)
1017 if (expld
.section
== bfd_abs_section_ptr
1018 && !config
.sane_expr
)
1019 new_abs (tree
->value
.value
);
1021 new_number (tree
->value
.value
);
1022 expld
.result
.str
= tree
->value
.str
;
1026 if (expld
.phase
!= lang_first_phase_enum
)
1028 asection
*output_section
= tree
->rel
.section
->output_section
;
1029 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
1033 memset (&expld
.result
, 0, sizeof (expld
.result
));
1037 exp_fold_tree_1 (tree
->assert_s
.child
);
1038 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
1039 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
1051 fold_trinary (tree
);
1056 case etree_provided
:
1057 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
1059 if (tree
->type
.node_class
!= etree_assign
)
1060 einfo (_("%F%S can not PROVIDE assignment to"
1061 " location counter\n"), tree
);
1062 if (expld
.phase
!= lang_first_phase_enum
)
1064 /* Notify the folder that this is an assignment to dot. */
1065 expld
.assigning_to_dot
= TRUE
;
1066 exp_fold_tree_1 (tree
->assign
.src
);
1067 expld
.assigning_to_dot
= FALSE
;
1069 /* If we are assigning to dot inside an output section
1070 arrange to keep the section, except for certain
1071 expressions that evaluate to zero. We ignore . = 0,
1072 . = . + 0, and . = ALIGN (. != 0 ? expr : 1).
1073 We can't ignore all expressions that evaluate to zero
1074 because an otherwise empty section might have padding
1075 added by an alignment expression that changes with
1076 relaxation. Such a section might have zero size
1077 before relaxation and so be stripped incorrectly. */
1078 if (expld
.phase
== lang_mark_phase_enum
1079 && expld
.section
!= bfd_abs_section_ptr
1080 && expld
.section
!= bfd_und_section_ptr
1081 && !(expld
.result
.valid_p
1082 && expld
.result
.value
== 0
1083 && (is_value (tree
->assign
.src
, 0)
1084 || is_sym_value (tree
->assign
.src
, 0)
1085 || is_dot_plus_0 (tree
->assign
.src
)
1086 || is_align_conditional (tree
->assign
.src
))))
1087 expld
.section
->flags
|= SEC_KEEP
;
1089 if (!expld
.result
.valid_p
1090 || expld
.section
== bfd_und_section_ptr
)
1092 if (expld
.phase
!= lang_mark_phase_enum
)
1093 einfo (_("%F%S invalid assignment to"
1094 " location counter\n"), tree
);
1096 else if (expld
.dotp
== NULL
)
1097 einfo (_("%F%S assignment to location counter"
1098 " invalid outside of SECTIONS\n"), tree
);
1100 /* After allocation, assignment to dot should not be
1101 done inside an output section since allocation adds a
1102 padding statement that effectively duplicates the
1104 else if (expld
.phase
<= lang_allocating_phase_enum
1105 || expld
.section
== bfd_abs_section_ptr
)
1109 nextdot
= expld
.result
.value
;
1110 if (expld
.result
.section
!= NULL
)
1111 nextdot
+= expld
.result
.section
->vma
;
1113 nextdot
+= expld
.section
->vma
;
1114 if (nextdot
< expld
.dot
1115 && expld
.section
!= bfd_abs_section_ptr
)
1116 einfo (_("%F%S cannot move location counter backwards"
1117 " (from %V to %V)\n"),
1118 tree
, expld
.dot
, nextdot
);
1121 expld
.dot
= nextdot
;
1122 *expld
.dotp
= nextdot
;
1127 memset (&expld
.result
, 0, sizeof (expld
.result
));
1131 struct bfd_link_hash_entry
*h
= NULL
;
1133 if (tree
->type
.node_class
== etree_provide
)
1135 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1136 FALSE
, FALSE
, TRUE
);
1138 || !(h
->type
== bfd_link_hash_new
1139 || h
->type
== bfd_link_hash_undefined
1140 || h
->type
== bfd_link_hash_undefweak
1143 /* Do nothing. The symbol was never referenced, or
1144 was defined in some object file. Note that
1145 undefweak symbols are defined by PROVIDE. This
1146 is to support glibc use of __rela_iplt_start and
1147 similar weak references. */
1152 expld
.assign_name
= tree
->assign
.dst
;
1153 exp_fold_tree_1 (tree
->assign
.src
);
1154 /* expld.assign_name remaining equal to tree->assign.dst
1155 below indicates the evaluation of tree->assign.src did
1156 not use the value of tree->assign.dst. We don't allow
1157 self assignment until the final phase for two reasons:
1158 1) Expressions are evaluated multiple times. With
1159 relaxation, the number of times may vary.
1160 2) Section relative symbol values cannot be correctly
1161 converted to absolute values, as is required by many
1162 expressions, until final section sizing is complete. */
1163 if ((expld
.result
.valid_p
1164 && (expld
.phase
== lang_final_phase_enum
1165 || expld
.assign_name
!= NULL
))
1166 || (expld
.phase
<= lang_mark_phase_enum
1167 && tree
->type
.node_class
== etree_assign
1168 && tree
->assign
.defsym
))
1172 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1175 einfo (_("%P%F:%s: hash creation failed\n"),
1179 if (expld
.result
.section
== NULL
)
1180 expld
.result
.section
= expld
.section
;
1181 if (!update_definedness (tree
->assign
.dst
, h
) && 0)
1183 /* Symbol was already defined. For now this error
1184 is disabled because it causes failures in the ld
1185 testsuite: ld-elf/var1, ld-scripts/defined5, and
1186 ld-scripts/pr14962. Some of these no doubt
1187 reflect scripts used in the wild. */
1188 (*link_info
.callbacks
->multiple_definition
)
1189 (&link_info
, h
, link_info
.output_bfd
,
1190 expld
.result
.section
, expld
.result
.value
);
1192 h
->type
= bfd_link_hash_defined
;
1193 h
->u
.def
.value
= expld
.result
.value
;
1194 h
->u
.def
.section
= expld
.result
.section
;
1195 h
->linker_def
= ! tree
->assign
.type
.lineno
;
1196 h
->ldscript_def
= 1;
1197 if (tree
->type
.node_class
== etree_provide
)
1198 tree
->type
.node_class
= etree_provided
;
1200 /* Copy the symbol type if this is a simple assignment of
1201 one symbol to another. Also, handle the case of a foldable
1202 ternary conditional with names on either side. */
1203 if (tree
->assign
.src
->type
.node_class
== etree_name
)
1204 try_copy_symbol_type (h
, tree
->assign
.src
);
1205 else if (tree
->assign
.src
->type
.node_class
== etree_trinary
)
1207 exp_fold_tree_1 (tree
->assign
.src
->trinary
.cond
);
1208 if (expld
.result
.valid_p
)
1210 if (expld
.result
.value
1211 && tree
->assign
.src
->trinary
.lhs
->type
.node_class
1213 try_copy_symbol_type (h
, tree
->assign
.src
->trinary
.lhs
);
1215 if (!expld
.result
.value
1216 && tree
->assign
.src
->trinary
.rhs
->type
.node_class
1218 try_copy_symbol_type (h
, tree
->assign
.src
->trinary
.rhs
);
1222 else if (expld
.phase
== lang_final_phase_enum
)
1224 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
1225 FALSE
, FALSE
, TRUE
);
1227 && h
->type
== bfd_link_hash_new
)
1228 h
->type
= bfd_link_hash_undefined
;
1230 expld
.assign_name
= NULL
;
1240 memset (&expld
.result
, 0, sizeof (expld
.result
));
1246 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
1248 expld
.rel_from_abs
= FALSE
;
1251 expld
.section
= current_section
;
1252 exp_fold_tree_1 (tree
);
1256 exp_fold_tree_no_dot (etree_type
*tree
)
1258 expld
.rel_from_abs
= FALSE
;
1261 expld
.section
= bfd_abs_section_ptr
;
1262 exp_fold_tree_1 (tree
);
1266 exp_value_fold (etree_type
*tree
)
1268 exp_fold_tree_no_dot (tree
);
1269 if (expld
.result
.valid_p
)
1271 tree
->type
.node_code
= INT
;
1272 tree
->value
.value
= expld
.result
.value
;
1273 tree
->value
.str
= NULL
;
1274 tree
->type
.node_class
= etree_value
;
1278 #define MAX(a, b) ((a) > (b) ? (a) : (b))
1281 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
1283 etree_type
*new_e
= (etree_type
*) stat_alloc (MAX (sizeof (new_e
->binary
),
1284 sizeof (new_e
->value
)));
1285 new_e
->type
.node_code
= code
;
1286 new_e
->type
.filename
= lhs
->type
.filename
;
1287 new_e
->type
.lineno
= lhs
->type
.lineno
;
1288 new_e
->binary
.lhs
= lhs
;
1289 new_e
->binary
.rhs
= rhs
;
1290 new_e
->type
.node_class
= etree_binary
;
1291 if (lhs
->type
.node_class
== etree_value
1292 && rhs
->type
.node_class
== etree_value
1294 && code
!= DATA_SEGMENT_ALIGN
1295 && code
!= DATA_SEGMENT_RELRO_END
)
1296 exp_value_fold (new_e
);
1301 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
1303 etree_type
*new_e
= (etree_type
*) stat_alloc (MAX (sizeof (new_e
->trinary
),
1304 sizeof (new_e
->value
)));
1305 new_e
->type
.node_code
= code
;
1306 new_e
->type
.filename
= cond
->type
.filename
;
1307 new_e
->type
.lineno
= cond
->type
.lineno
;
1308 new_e
->trinary
.lhs
= lhs
;
1309 new_e
->trinary
.cond
= cond
;
1310 new_e
->trinary
.rhs
= rhs
;
1311 new_e
->type
.node_class
= etree_trinary
;
1312 if (cond
->type
.node_class
== etree_value
1313 && lhs
->type
.node_class
== etree_value
1314 && rhs
->type
.node_class
== etree_value
)
1315 exp_value_fold (new_e
);
1320 exp_unop (int code
, etree_type
*child
)
1322 etree_type
*new_e
= (etree_type
*) stat_alloc (MAX (sizeof (new_e
->unary
),
1323 sizeof (new_e
->value
)));
1324 new_e
->unary
.type
.node_code
= code
;
1325 new_e
->unary
.type
.filename
= child
->type
.filename
;
1326 new_e
->unary
.type
.lineno
= child
->type
.lineno
;
1327 new_e
->unary
.child
= child
;
1328 new_e
->unary
.type
.node_class
= etree_unary
;
1329 if (child
->type
.node_class
== etree_value
1333 && code
!= DATA_SEGMENT_END
)
1334 exp_value_fold (new_e
);
1339 exp_nameop (int code
, const char *name
)
1341 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
1343 new_e
->name
.type
.node_code
= code
;
1344 new_e
->name
.type
.filename
= ldlex_filename ();
1345 new_e
->name
.type
.lineno
= lineno
;
1346 new_e
->name
.name
= name
;
1347 new_e
->name
.type
.node_class
= etree_name
;
1353 exp_assop (const char *dst
,
1355 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
.defsym
= defsym
;
1369 n
->assign
.hidden
= hidden
;
1373 /* Handle linker script assignments and HIDDEN. */
1376 exp_assign (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1378 return exp_assop (dst
, src
, etree_assign
, FALSE
, hidden
);
1381 /* Handle --defsym command-line option. */
1384 exp_defsym (const char *dst
, etree_type
*src
)
1386 return exp_assop (dst
, src
, etree_assign
, TRUE
, FALSE
);
1389 /* Handle PROVIDE. */
1392 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1394 return exp_assop (dst
, src
, etree_provide
, FALSE
, hidden
);
1397 /* Handle ASSERT. */
1400 exp_assert (etree_type
*exp
, const char *message
)
1404 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1405 n
->assert_s
.type
.node_code
= '!';
1406 n
->assert_s
.type
.filename
= exp
->type
.filename
;
1407 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1408 n
->assert_s
.type
.node_class
= etree_assert
;
1409 n
->assert_s
.child
= exp
;
1410 n
->assert_s
.message
= message
;
1415 exp_print_tree (etree_type
*tree
)
1417 bfd_boolean function_like
;
1419 if (config
.map_file
== NULL
)
1420 config
.map_file
= stderr
;
1424 minfo ("NULL TREE\n");
1428 switch (tree
->type
.node_class
)
1431 minfo ("0x%v", tree
->value
.value
);
1434 if (tree
->rel
.section
->owner
!= NULL
)
1435 minfo ("%B:", tree
->rel
.section
->owner
);
1436 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1439 fputs (tree
->assign
.dst
, config
.map_file
);
1440 exp_print_token (tree
->type
.node_code
, TRUE
);
1441 exp_print_tree (tree
->assign
.src
);
1444 case etree_provided
:
1445 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
1446 exp_print_tree (tree
->assign
.src
);
1447 fputc (')', config
.map_file
);
1450 function_like
= FALSE
;
1451 switch (tree
->type
.node_code
)
1456 case DATA_SEGMENT_ALIGN
:
1457 case DATA_SEGMENT_RELRO_END
:
1458 function_like
= TRUE
;
1461 /* Special handling because arguments are in reverse order and
1462 the segment name is quoted. */
1463 exp_print_token (tree
->type
.node_code
, FALSE
);
1464 fputs (" (\"", config
.map_file
);
1465 exp_print_tree (tree
->binary
.rhs
);
1466 fputs ("\", ", config
.map_file
);
1467 exp_print_tree (tree
->binary
.lhs
);
1468 fputc (')', config
.map_file
);
1473 exp_print_token (tree
->type
.node_code
, FALSE
);
1474 fputc (' ', config
.map_file
);
1476 fputc ('(', config
.map_file
);
1477 exp_print_tree (tree
->binary
.lhs
);
1479 fprintf (config
.map_file
, ", ");
1481 exp_print_token (tree
->type
.node_code
, TRUE
);
1482 exp_print_tree (tree
->binary
.rhs
);
1483 fputc (')', config
.map_file
);
1486 exp_print_tree (tree
->trinary
.cond
);
1487 fputc ('?', config
.map_file
);
1488 exp_print_tree (tree
->trinary
.lhs
);
1489 fputc (':', config
.map_file
);
1490 exp_print_tree (tree
->trinary
.rhs
);
1493 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1494 if (tree
->unary
.child
)
1496 fprintf (config
.map_file
, " (");
1497 exp_print_tree (tree
->unary
.child
);
1498 fputc (')', config
.map_file
);
1503 fprintf (config
.map_file
, "ASSERT (");
1504 exp_print_tree (tree
->assert_s
.child
);
1505 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1509 if (tree
->type
.node_code
== NAME
)
1510 fputs (tree
->name
.name
, config
.map_file
);
1513 exp_print_token (tree
->type
.node_code
, FALSE
);
1514 if (tree
->name
.name
)
1515 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1525 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1529 exp_fold_tree_no_dot (tree
);
1530 if (expld
.result
.valid_p
)
1531 return expld
.result
.value
;
1532 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1533 einfo (_("%F%S: nonconstant expression for %s\n"),
1540 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1542 return exp_get_vma (tree
, def
, name
);
1546 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1555 exp_fold_tree_no_dot (tree
);
1556 if (!expld
.result
.valid_p
)
1558 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1559 einfo (_("%F%S: nonconstant expression for %s\n"),
1564 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1568 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1569 fill
->size
= (len
+ 1) / 2;
1571 s
= (unsigned char *) expld
.result
.str
;
1579 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1593 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1594 val
= expld
.result
.value
;
1595 fill
->data
[0] = (val
>> 24) & 0xff;
1596 fill
->data
[1] = (val
>> 16) & 0xff;
1597 fill
->data
[2] = (val
>> 8) & 0xff;
1598 fill
->data
[3] = (val
>> 0) & 0xff;
1605 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1609 exp_fold_tree_no_dot (tree
);
1611 if (expld
.result
.valid_p
)
1613 if (expld
.result
.section
!= NULL
)
1614 expld
.result
.value
+= expld
.result
.section
->vma
;
1615 return expld
.result
.value
;
1617 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1619 einfo (_("%F%S: nonconstant expression for %s\n"),
1627 align_n (bfd_vma value
, bfd_vma align
)
1632 value
= (value
+ align
- 1) / align
;
1633 return value
* align
;
1639 /* The value "13" is ad-hoc, somewhat related to the expected number of
1640 assignments in a linker script. */
1641 if (!bfd_hash_table_init_n (&definedness_table
,
1642 definedness_newfunc
,
1643 sizeof (struct definedness_hash_entry
),
1645 einfo (_("%P%F: can not create hash table: %E\n"));
1648 /* Convert absolute symbols defined by a script from "dot" (also
1649 SEGMENT_START or ORIGIN) outside of an output section statement,
1650 to section relative. */
1653 set_sym_sections (struct bfd_hash_entry
*bh
, void *inf ATTRIBUTE_UNUSED
)
1655 struct definedness_hash_entry
*def
= (struct definedness_hash_entry
*) bh
;
1656 if (def
->final_sec
!= bfd_abs_section_ptr
)
1658 struct bfd_link_hash_entry
*h
;
1659 h
= bfd_link_hash_lookup (link_info
.hash
, bh
->string
,
1660 FALSE
, FALSE
, TRUE
);
1662 && h
->type
== bfd_link_hash_defined
1663 && h
->u
.def
.section
== bfd_abs_section_ptr
)
1665 h
->u
.def
.value
-= def
->final_sec
->vma
;
1666 h
->u
.def
.section
= def
->final_sec
;
1673 ldexp_finalize_syms (void)
1675 bfd_hash_traverse (&definedness_table
, set_sym_sections
, NULL
);
1681 bfd_hash_table_free (&definedness_table
);