1 /* This module handles expression trees.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
5 This file is part of the GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* This module is in charge of working out the contents of expressions.
25 It has to keep track of the relative/absness of a symbol etc. This
26 is done by keeping all values in a struct (an etree_value_type)
27 which contains a value, a section to which it is relative and a
41 #include "libiberty.h"
42 #include "safe-ctype.h"
44 static void exp_fold_tree_1 (etree_type *);
45 static bfd_vma align_n (bfd_vma, bfd_vma);
47 segment_type *segments;
49 struct ldexp_control expld;
51 /* This structure records symbols for which we need to keep track of
52 definedness for use in the DEFINED () test. */
54 struct definedness_hash_entry
56 struct bfd_hash_entry root;
57 unsigned int by_object : 1;
58 unsigned int by_script : 1;
59 unsigned int iteration : 1;
62 static struct bfd_hash_table definedness_table;
64 /* Print the string representation of the given token. Surround it
65 with spaces if INFIX_P is TRUE. */
68 exp_print_token (token_code_type code, int infix_p)
95 { LOG2CEIL, "LOG2CEIL" },
103 { SECTIONS, "SECTIONS" },
104 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
105 { MEMORY, "MEMORY" },
106 { DEFINED, "DEFINED" },
107 { TARGET_K, "TARGET" },
108 { SEARCH_DIR, "SEARCH_DIR" },
112 { ALIGNOF, "ALIGNOF" },
113 { SIZEOF, "SIZEOF" },
115 { LOADADDR, "LOADADDR" },
116 { CONSTANT, "CONSTANT" },
117 { ABSOLUTE, "ABSOLUTE" },
120 { ASSERT_K, "ASSERT" },
121 { REL, "relocatable" },
122 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
123 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
124 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
125 { ORIGIN, "ORIGIN" },
126 { LENGTH, "LENGTH" },
127 { SEGMENT_START, "SEGMENT_START" }
131 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
132 if (table[idx].code == code)
136 fputc (' ', config.map_file);
138 if (idx < ARRAY_SIZE (table))
139 fputs (table[idx].name, config.map_file);
141 fputc (code, config.map_file);
143 fprintf (config.map_file, "<code %d>", code);
146 fputc (' ', config.map_file);
152 bfd_vma value = expld.result.value;
154 bfd_boolean round_up = FALSE;
159 /* If more than one bit is set in the value we will need to round up. */
160 if ((value > 1) && (value & 1))
167 expld.result.section = NULL;
168 expld.result.value = result;
174 if (expld.result.section != NULL)
175 expld.result.value += expld.result.section->vma;
176 expld.result.section = bfd_abs_section_ptr;
180 new_abs (bfd_vma value)
182 expld.result.valid_p = TRUE;
183 expld.result.section = bfd_abs_section_ptr;
184 expld.result.value = value;
185 expld.result.str = NULL;
189 exp_intop (bfd_vma value)
191 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
192 new_e->type.node_code = INT;
193 new_e->type.filename = ldlex_filename ();
194 new_e->type.lineno = lineno;
195 new_e->value.value = value;
196 new_e->value.str = NULL;
197 new_e->type.node_class = etree_value;
202 exp_bigintop (bfd_vma value, char *str)
204 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
205 new_e->type.node_code = INT;
206 new_e->type.filename = ldlex_filename ();
207 new_e->type.lineno = lineno;
208 new_e->value.value = value;
209 new_e->value.str = str;
210 new_e->type.node_class = etree_value;
214 /* Build an expression representing an unnamed relocatable value. */
217 exp_relop (asection *section, bfd_vma value)
219 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
220 new_e->type.node_code = REL;
221 new_e->type.filename = ldlex_filename ();
222 new_e->type.lineno = lineno;
223 new_e->type.node_class = etree_rel;
224 new_e->rel.section = section;
225 new_e->rel.value = value;
230 new_number (bfd_vma value)
232 expld.result.valid_p = TRUE;
233 expld.result.value = value;
234 expld.result.str = NULL;
235 expld.result.section = NULL;
239 new_rel (bfd_vma value, asection *section)
241 expld.result.valid_p = TRUE;
242 expld.result.value = value;
243 expld.result.str = NULL;
244 expld.result.section = section;
248 new_rel_from_abs (bfd_vma value)
250 asection *s = expld.section;
252 if (s == bfd_abs_section_ptr && expld.phase == lang_final_phase_enum)
253 s = section_for_dot ();
254 expld.result.valid_p = TRUE;
255 expld.result.value = value - s->vma;
256 expld.result.str = NULL;
257 expld.result.section = s;
260 /* New-function for the definedness hash table. */
262 static struct bfd_hash_entry *
263 definedness_newfunc (struct bfd_hash_entry *entry,
264 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
265 const char *name ATTRIBUTE_UNUSED)
267 struct definedness_hash_entry *ret = (struct definedness_hash_entry *) entry;
270 ret = (struct definedness_hash_entry *)
271 bfd_hash_allocate (table, sizeof (struct definedness_hash_entry));
274 einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
282 /* Called during processing of linker script script expressions.
283 For symbols assigned in a linker script, return a struct describing
284 where the symbol is defined relative to the current expression,
285 otherwise return NULL. */
287 static struct definedness_hash_entry *
288 symbol_defined (const char *name)
290 return ((struct definedness_hash_entry *)
291 bfd_hash_lookup (&definedness_table, name, FALSE, FALSE));
294 /* Update the definedness state of NAME. Return FALSE if script symbol
295 is multiply defining a strong symbol in an object. */
298 update_definedness (const char *name, struct bfd_link_hash_entry *h)
301 struct definedness_hash_entry *defentry
302 = (struct definedness_hash_entry *)
303 bfd_hash_lookup (&definedness_table, name, TRUE, FALSE);
305 if (defentry == NULL)
306 einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
308 /* If the symbol was already defined, and not by a script, then it
309 must be defined by an object file or by the linker target code. */
311 if (!defentry->by_script
312 && (h->type == bfd_link_hash_defined
313 || h->type == bfd_link_hash_defweak
314 || h->type == bfd_link_hash_common))
316 defentry->by_object = 1;
317 if (h->type == bfd_link_hash_defined
318 && h->u.def.section->output_section != NULL
323 defentry->by_script = 1;
324 defentry->iteration = lang_statement_iteration;
329 fold_unary (etree_type *tree)
331 exp_fold_tree_1 (tree->unary.child);
332 if (expld.result.valid_p)
334 switch (tree->type.node_code)
337 if (expld.phase != lang_first_phase_enum)
338 new_rel_from_abs (align_n (expld.dot, expld.result.value));
340 expld.result.valid_p = FALSE;
352 expld.result.value = ~expld.result.value;
356 expld.result.value = !expld.result.value;
360 expld.result.value = -expld.result.value;
364 /* Return next place aligned to value. */
365 if (expld.phase != lang_first_phase_enum)
368 expld.result.value = align_n (expld.dot, expld.result.value);
371 expld.result.valid_p = FALSE;
374 case DATA_SEGMENT_END:
375 if (expld.phase == lang_first_phase_enum
376 || expld.section != bfd_abs_section_ptr)
378 expld.result.valid_p = FALSE;
380 else if (expld.dataseg.phase == exp_dataseg_align_seen
381 || expld.dataseg.phase == exp_dataseg_relro_seen)
383 expld.dataseg.phase = exp_dataseg_end_seen;
384 expld.dataseg.end = expld.result.value;
386 else if (expld.dataseg.phase == exp_dataseg_done
387 || expld.dataseg.phase == exp_dataseg_adjust
388 || expld.dataseg.phase == exp_dataseg_relro_adjust)
393 expld.result.valid_p = FALSE;
404 fold_binary (etree_type *tree)
406 etree_value_type lhs;
407 exp_fold_tree_1 (tree->binary.lhs);
409 /* The SEGMENT_START operator is special because its first
410 operand is a string, not the name of a symbol. Note that the
411 operands have been swapped, so binary.lhs is second (default)
412 operand, binary.rhs is first operand. */
413 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
415 const char *segment_name;
418 /* Check to see if the user has overridden the default
420 segment_name = tree->binary.rhs->name.name;
421 for (seg = segments; seg; seg = seg->next)
422 if (strcmp (seg->name, segment_name) == 0)
425 && config.magic_demand_paged
426 && (seg->value % config.maxpagesize) != 0)
427 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
430 new_rel_from_abs (seg->value);
437 exp_fold_tree_1 (tree->binary.rhs);
438 expld.result.valid_p &= lhs.valid_p;
440 if (expld.result.valid_p)
442 if (lhs.section != expld.result.section)
444 /* If the values are from different sections, and neither is
445 just a number, make both the source arguments absolute. */
446 if (expld.result.section != NULL
447 && lhs.section != NULL)
450 lhs.value += lhs.section->vma;
451 lhs.section = bfd_abs_section_ptr;
454 /* If the rhs is just a number, keep the lhs section. */
455 else if (expld.result.section == NULL)
457 expld.result.section = lhs.section;
458 /* Make this NULL so that we know one of the operands
459 was just a number, for later tests. */
463 /* At this point we know that both operands have the same
464 section, or at least one of them is a plain number. */
466 switch (tree->type.node_code)
468 /* Arithmetic operators, bitwise AND, bitwise OR and XOR
469 keep the section of one of their operands only when the
470 other operand is a plain number. Losing the section when
471 operating on two symbols, ie. a result of a plain number,
472 is required for subtraction and XOR. It's justifiable
473 for the other operations on the grounds that adding,
474 multiplying etc. two section relative values does not
475 really make sense unless they are just treated as
477 The same argument could be made for many expressions
478 involving one symbol and a number. For example,
479 "1 << x" and "100 / x" probably should not be given the
480 section of x. The trouble is that if we fuss about such
481 things the rules become complex and it is onerous to
482 document ld expression evaluation. */
485 expld.result.value = lhs.value y expld.result.value; \
486 if (expld.result.section == lhs.section) \
487 expld.result.section = NULL; \
490 /* Comparison operators, logical AND, and logical OR always
491 return a plain number. */
494 expld.result.value = lhs.value y expld.result.value; \
495 expld.result.section = NULL; \
516 if (expld.result.value != 0)
517 expld.result.value = ((bfd_signed_vma) lhs.value
518 % (bfd_signed_vma) expld.result.value);
519 else if (expld.phase != lang_mark_phase_enum)
520 einfo (_("%F%S %% by zero\n"), tree->binary.rhs);
521 if (expld.result.section == lhs.section)
522 expld.result.section = NULL;
526 if (expld.result.value != 0)
527 expld.result.value = ((bfd_signed_vma) lhs.value
528 / (bfd_signed_vma) expld.result.value);
529 else if (expld.phase != lang_mark_phase_enum)
530 einfo (_("%F%S / by zero\n"), tree->binary.rhs);
531 if (expld.result.section == lhs.section)
532 expld.result.section = NULL;
536 if (lhs.value > expld.result.value)
537 expld.result.value = lhs.value;
541 if (lhs.value < expld.result.value)
542 expld.result.value = lhs.value;
546 expld.result.value = align_n (lhs.value, expld.result.value);
549 case DATA_SEGMENT_ALIGN:
550 expld.dataseg.relro = exp_dataseg_relro_start;
551 if (expld.phase == lang_first_phase_enum
552 || expld.section != bfd_abs_section_ptr)
553 expld.result.valid_p = FALSE;
556 bfd_vma maxpage = lhs.value;
557 bfd_vma commonpage = expld.result.value;
559 expld.result.value = align_n (expld.dot, maxpage);
560 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
561 expld.result.value = expld.dataseg.base;
562 else if (expld.dataseg.phase == exp_dataseg_adjust)
564 if (commonpage < maxpage)
565 expld.result.value += ((expld.dot + commonpage - 1)
566 & (maxpage - commonpage));
570 expld.result.value += expld.dot & (maxpage - 1);
571 if (expld.dataseg.phase == exp_dataseg_done)
575 else if (expld.dataseg.phase == exp_dataseg_none)
577 expld.dataseg.phase = exp_dataseg_align_seen;
578 expld.dataseg.min_base = expld.dot;
579 expld.dataseg.base = expld.result.value;
580 expld.dataseg.pagesize = commonpage;
581 expld.dataseg.maxpagesize = maxpage;
582 expld.dataseg.relro_end = 0;
585 expld.result.valid_p = FALSE;
590 case DATA_SEGMENT_RELRO_END:
591 expld.dataseg.relro = exp_dataseg_relro_end;
592 if (expld.phase == lang_first_phase_enum
593 || expld.section != bfd_abs_section_ptr)
594 expld.result.valid_p = FALSE;
595 else if (expld.dataseg.phase == exp_dataseg_align_seen
596 || expld.dataseg.phase == exp_dataseg_adjust
597 || expld.dataseg.phase == exp_dataseg_relro_adjust
598 || expld.dataseg.phase == exp_dataseg_done)
600 if (expld.dataseg.phase == exp_dataseg_align_seen
601 || expld.dataseg.phase == exp_dataseg_relro_adjust)
602 expld.dataseg.relro_end = lhs.value + expld.result.value;
604 if (expld.dataseg.phase == exp_dataseg_relro_adjust
605 && (expld.dataseg.relro_end
606 & (expld.dataseg.pagesize - 1)))
608 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
609 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
610 expld.result.value = (expld.dataseg.relro_end
611 - expld.result.value);
614 expld.result.value = lhs.value;
616 if (expld.dataseg.phase == exp_dataseg_align_seen)
617 expld.dataseg.phase = exp_dataseg_relro_seen;
620 expld.result.valid_p = FALSE;
630 fold_trinary (etree_type *tree)
632 exp_fold_tree_1 (tree->trinary.cond);
633 if (expld.result.valid_p)
634 exp_fold_tree_1 (expld.result.value
636 : tree->trinary.rhs);
640 fold_name (etree_type *tree)
642 memset (&expld.result, 0, sizeof (expld.result));
644 switch (tree->type.node_code)
647 if (expld.phase != lang_first_phase_enum)
649 bfd_vma hdr_size = 0;
650 /* Don't find the real header size if only marking sections;
651 The bfd function may cache incorrect data. */
652 if (expld.phase != lang_mark_phase_enum)
653 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
654 new_number (hdr_size);
659 if (expld.phase != lang_first_phase_enum)
661 struct bfd_link_hash_entry *h;
662 struct definedness_hash_entry *def;
664 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
668 new_number (h != NULL
669 && (h->type == bfd_link_hash_defined
670 || h->type == bfd_link_hash_defweak
671 || h->type == bfd_link_hash_common)
672 && ((def = symbol_defined (tree->name.name)) == NULL
674 || def->iteration == (lang_statement_iteration & 1)));
679 if (expld.assign_name != NULL
680 && strcmp (expld.assign_name, tree->name.name) == 0)
682 /* Self-assignment is only allowed for absolute symbols
683 defined in a linker script. */
684 struct bfd_link_hash_entry *h;
685 struct definedness_hash_entry *def;
687 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
692 && (h->type == bfd_link_hash_defined
693 || h->type == bfd_link_hash_defweak)
694 && h->u.def.section == bfd_abs_section_ptr
695 && (def = symbol_defined (tree->name.name)) != NULL
696 && def->iteration == (lang_statement_iteration & 1)))
697 expld.assign_name = NULL;
699 if (expld.phase == lang_first_phase_enum)
701 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
702 new_rel_from_abs (expld.dot);
705 struct bfd_link_hash_entry *h;
707 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
712 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
713 else if (h->type == bfd_link_hash_defined
714 || h->type == bfd_link_hash_defweak)
716 asection *output_section;
718 output_section = h->u.def.section->output_section;
719 if (output_section == NULL)
721 if (expld.phase == lang_mark_phase_enum)
722 new_rel (h->u.def.value, h->u.def.section);
724 einfo (_("%X%S: unresolvable symbol `%s'"
725 " referenced in expression\n"),
726 tree, tree->name.name);
728 else if (output_section == bfd_abs_section_ptr
729 && (expld.section != bfd_abs_section_ptr
730 || config.sane_expr))
731 new_number (h->u.def.value + h->u.def.section->output_offset);
733 new_rel (h->u.def.value + h->u.def.section->output_offset,
736 else if (expld.phase == lang_final_phase_enum
737 || (expld.phase != lang_mark_phase_enum
738 && expld.assigning_to_dot))
739 einfo (_("%F%S: undefined symbol `%s'"
740 " referenced in expression\n"),
741 tree, tree->name.name);
742 else if (h->type == bfd_link_hash_new)
744 h->type = bfd_link_hash_undefined;
745 h->u.undef.abfd = NULL;
746 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
747 bfd_link_add_undef (link_info.hash, h);
753 if (expld.phase != lang_first_phase_enum)
755 lang_output_section_statement_type *os;
757 os = lang_output_section_find (tree->name.name);
760 if (expld.phase == lang_final_phase_enum)
761 einfo (_("%F%S: undefined section `%s'"
762 " referenced in expression\n"),
763 tree, tree->name.name);
765 else if (os->processed_vma)
766 new_rel (0, os->bfd_section);
771 if (expld.phase != lang_first_phase_enum)
773 lang_output_section_statement_type *os;
775 os = lang_output_section_find (tree->name.name);
778 if (expld.phase == lang_final_phase_enum)
779 einfo (_("%F%S: undefined section `%s'"
780 " referenced in expression\n"),
781 tree, tree->name.name);
783 else if (os->processed_lma)
785 if (os->load_base == NULL)
786 new_abs (os->bfd_section->lma);
789 exp_fold_tree_1 (os->load_base);
790 if (expld.result.valid_p)
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);
812 else if (os->bfd_section != NULL)
816 if (tree->type.node_code == SIZEOF)
817 val = (os->bfd_section->size
818 / bfd_octets_per_byte (link_info.output_bfd));
820 val = (bfd_vma)1 << os->bfd_section->alignment_power;
831 lang_memory_region_type *mem;
833 mem = lang_memory_region_lookup (tree->name.name, FALSE);
835 new_number (mem->length);
837 einfo (_("%F%S: undefined MEMORY region `%s'"
838 " referenced in expression\n"),
839 tree, tree->name.name);
844 if (expld.phase != lang_first_phase_enum)
846 lang_memory_region_type *mem;
848 mem = lang_memory_region_lookup (tree->name.name, FALSE);
850 new_rel_from_abs (mem->origin);
852 einfo (_("%F%S: undefined MEMORY region `%s'"
853 " referenced in expression\n"),
854 tree, tree->name.name);
859 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
860 new_number (config.maxpagesize);
861 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
862 new_number (config.commonpagesize);
864 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
865 tree, tree->name.name);
874 /* Return true if TREE is '.'. */
877 is_dot (const etree_type *tree)
879 return (tree->type.node_class == etree_name
880 && tree->type.node_code == NAME
881 && tree->name.name[0] == '.'
882 && tree->name.name[1] == 0);
885 /* Return true if TREE is a constant equal to VAL. */
888 is_value (const etree_type *tree, bfd_vma val)
890 return (tree->type.node_class == etree_value
891 && tree->value.value == val);
894 /* Return true if TREE is an absolute symbol equal to VAL defined in
898 is_sym_value (const etree_type *tree, bfd_vma val)
900 struct bfd_link_hash_entry *h;
901 struct definedness_hash_entry *def;
903 return (tree->type.node_class == etree_name
904 && tree->type.node_code == NAME
905 && (def = symbol_defined (tree->name.name)) != NULL
907 && def->iteration == (lang_statement_iteration & 1)
908 && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
911 FALSE, FALSE, TRUE)) != NULL
912 && h->type == bfd_link_hash_defined
913 && h->u.def.section == bfd_abs_section_ptr
914 && h->u.def.value == val);
917 /* Return true if TREE is ". != 0". */
920 is_dot_ne_0 (const etree_type *tree)
922 return (tree->type.node_class == etree_binary
923 && tree->type.node_code == NE
924 && is_dot (tree->binary.lhs)
925 && is_value (tree->binary.rhs, 0));
928 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
929 absolute constant with value 0 defined in a linker script. */
932 is_dot_plus_0 (const etree_type *tree)
934 return (tree->type.node_class == etree_binary
935 && tree->type.node_code == '+'
936 && is_dot (tree->binary.lhs)
937 && (is_value (tree->binary.rhs, 0)
938 || is_sym_value (tree->binary.rhs, 0)));
941 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
944 is_align_conditional (const etree_type *tree)
946 if (tree->type.node_class == etree_unary
947 && tree->type.node_code == ALIGN_K)
949 tree = tree->unary.child;
950 return (tree->type.node_class == etree_trinary
951 && is_dot_ne_0 (tree->trinary.cond)
952 && is_value (tree->trinary.rhs, 1));
958 exp_fold_tree_1 (etree_type *tree)
962 memset (&expld.result, 0, sizeof (expld.result));
966 switch (tree->type.node_class)
969 if (expld.section == bfd_abs_section_ptr
970 && !config.sane_expr)
971 new_abs (tree->value.value);
973 new_number (tree->value.value);
974 expld.result.str = tree->value.str;
978 if (expld.phase != lang_first_phase_enum)
980 asection *output_section = tree->rel.section->output_section;
981 new_rel (tree->rel.value + tree->rel.section->output_offset,
985 memset (&expld.result, 0, sizeof (expld.result));
989 exp_fold_tree_1 (tree->assert_s.child);
990 if (expld.phase == lang_final_phase_enum && !expld.result.value)
991 einfo ("%X%P: %s\n", tree->assert_s.message);
1003 fold_trinary (tree);
1008 case etree_provided:
1009 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
1011 if (tree->type.node_class != etree_assign)
1012 einfo (_("%F%S can not PROVIDE assignment to"
1013 " location counter\n"), tree);
1014 if (expld.phase != lang_first_phase_enum)
1016 /* Notify the folder that this is an assignment to dot. */
1017 expld.assigning_to_dot = TRUE;
1018 exp_fold_tree_1 (tree->assign.src);
1019 expld.assigning_to_dot = FALSE;
1021 /* If we are assigning to dot inside an output section
1022 arrange to keep the section, except for certain
1023 expressions that evaluate to zero. We ignore . = 0,
1024 . = . + 0, and . = ALIGN (. != 0 ? expr : 1). */
1025 if (expld.phase == lang_mark_phase_enum
1026 && expld.section != bfd_abs_section_ptr
1027 && !(expld.result.valid_p
1028 && expld.result.value == 0
1029 && (is_value (tree->assign.src, 0)
1030 || is_sym_value (tree->assign.src, 0)
1031 || is_dot_plus_0 (tree->assign.src)
1032 || is_align_conditional (tree->assign.src))))
1033 expld.section->flags |= SEC_KEEP;
1035 if (!expld.result.valid_p)
1037 if (expld.phase != lang_mark_phase_enum)
1038 einfo (_("%F%S invalid assignment to"
1039 " location counter\n"), tree);
1041 else if (expld.dotp == NULL)
1042 einfo (_("%F%S assignment to location counter"
1043 " invalid outside of SECTIONS\n"), tree);
1045 /* After allocation, assignment to dot should not be
1046 done inside an output section since allocation adds a
1047 padding statement that effectively duplicates the
1049 else if (expld.phase <= lang_allocating_phase_enum
1050 || expld.section == bfd_abs_section_ptr)
1054 nextdot = expld.result.value;
1055 if (expld.result.section != NULL)
1056 nextdot += expld.result.section->vma;
1058 nextdot += expld.section->vma;
1059 if (nextdot < expld.dot
1060 && expld.section != bfd_abs_section_ptr)
1061 einfo (_("%F%S cannot move location counter backwards"
1062 " (from %V to %V)\n"),
1063 tree, expld.dot, nextdot);
1066 expld.dot = nextdot;
1067 *expld.dotp = nextdot;
1072 memset (&expld.result, 0, sizeof (expld.result));
1076 struct bfd_link_hash_entry *h = NULL;
1078 if (tree->type.node_class == etree_provide)
1080 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1081 FALSE, FALSE, TRUE);
1083 || !(h->type == bfd_link_hash_new
1084 || h->type == bfd_link_hash_undefined
1087 /* Do nothing. The symbol was never referenced, or
1088 was defined in some object file. Undefined weak
1089 symbols stay undefined. */
1094 expld.assign_name = tree->assign.dst;
1095 exp_fold_tree_1 (tree->assign.src);
1096 /* expld.assign_name remaining equal to tree->assign.dst
1097 below indicates the evaluation of tree->assign.src did
1098 not use the value of tree->assign.dst. We don't allow
1099 self assignment until the final phase for two reasons:
1100 1) Expressions are evaluated multiple times. With
1101 relaxation, the number of times may vary.
1102 2) Section relative symbol values cannot be correctly
1103 converted to absolute values, as is required by many
1104 expressions, until final section sizing is complete. */
1105 if ((expld.result.valid_p
1106 && (expld.phase == lang_final_phase_enum
1107 || expld.assign_name != NULL))
1108 || (expld.phase <= lang_mark_phase_enum
1109 && tree->type.node_class == etree_assign
1110 && tree->assign.defsym))
1114 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1117 einfo (_("%P%F:%s: hash creation failed\n"),
1121 if (expld.result.section == NULL)
1122 expld.result.section = expld.section;
1123 if (!update_definedness (tree->assign.dst, h) && 0)
1125 /* Symbol was already defined. For now this error
1126 is disabled because it causes failures in the ld
1127 testsuite: ld-elf/var1, ld-scripts/defined5, and
1128 ld-scripts/pr14962. Some of these no doubt
1129 reflect scripts used in the wild. */
1130 (*link_info.callbacks->multiple_definition)
1131 (&link_info, h, link_info.output_bfd,
1132 expld.result.section, expld.result.value);
1134 h->type = bfd_link_hash_defined;
1135 h->u.def.value = expld.result.value;
1136 h->u.def.section = expld.result.section;
1137 if (tree->type.node_class == etree_provide)
1138 tree->type.node_class = etree_provided;
1140 /* Copy the symbol type if this is a simple assignment of
1141 one symbol to another. This could be more general
1142 (e.g. a ?: operator with NAMEs in each branch). */
1143 if (tree->assign.src->type.node_class == etree_name)
1145 struct bfd_link_hash_entry *hsrc;
1147 hsrc = bfd_link_hash_lookup (link_info.hash,
1148 tree->assign.src->name.name,
1149 FALSE, FALSE, TRUE);
1151 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
1155 else if (expld.phase == lang_final_phase_enum)
1157 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1158 FALSE, FALSE, TRUE);
1160 && h->type == bfd_link_hash_new)
1161 h->type = bfd_link_hash_undefined;
1163 expld.assign_name = NULL;
1173 memset (&expld.result, 0, sizeof (expld.result));
1179 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
1183 expld.section = current_section;
1184 exp_fold_tree_1 (tree);
1188 exp_fold_tree_no_dot (etree_type *tree)
1192 expld.section = bfd_abs_section_ptr;
1193 exp_fold_tree_1 (tree);
1197 exp_binop (int code, etree_type *lhs, etree_type *rhs)
1199 etree_type value, *new_e;
1201 value.type.node_code = code;
1202 value.type.filename = lhs->type.filename;
1203 value.type.lineno = lhs->type.lineno;
1204 value.binary.lhs = lhs;
1205 value.binary.rhs = rhs;
1206 value.type.node_class = etree_binary;
1207 exp_fold_tree_no_dot (&value);
1208 if (expld.result.valid_p)
1209 return exp_intop (expld.result.value);
1211 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
1212 memcpy (new_e, &value, sizeof (new_e->binary));
1217 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
1219 etree_type value, *new_e;
1221 value.type.node_code = code;
1222 value.type.filename = cond->type.filename;
1223 value.type.lineno = cond->type.lineno;
1224 value.trinary.lhs = lhs;
1225 value.trinary.cond = cond;
1226 value.trinary.rhs = rhs;
1227 value.type.node_class = etree_trinary;
1228 exp_fold_tree_no_dot (&value);
1229 if (expld.result.valid_p)
1230 return exp_intop (expld.result.value);
1232 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
1233 memcpy (new_e, &value, sizeof (new_e->trinary));
1238 exp_unop (int code, etree_type *child)
1240 etree_type value, *new_e;
1242 value.unary.type.node_code = code;
1243 value.unary.type.filename = child->type.filename;
1244 value.unary.type.lineno = child->type.lineno;
1245 value.unary.child = child;
1246 value.unary.type.node_class = etree_unary;
1247 exp_fold_tree_no_dot (&value);
1248 if (expld.result.valid_p)
1249 return exp_intop (expld.result.value);
1251 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
1252 memcpy (new_e, &value, sizeof (new_e->unary));
1257 exp_nameop (int code, const char *name)
1259 etree_type value, *new_e;
1261 value.name.type.node_code = code;
1262 value.name.type.filename = ldlex_filename ();
1263 value.name.type.lineno = lineno;
1264 value.name.name = name;
1265 value.name.type.node_class = etree_name;
1267 exp_fold_tree_no_dot (&value);
1268 if (expld.result.valid_p)
1269 return exp_intop (expld.result.value);
1271 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1272 memcpy (new_e, &value, sizeof (new_e->name));
1278 exp_assop (const char *dst,
1280 enum node_tree_enum class,
1286 n = (etree_type *) stat_alloc (sizeof (n->assign));
1287 n->assign.type.node_code = '=';
1288 n->assign.type.filename = src->type.filename;
1289 n->assign.type.lineno = src->type.lineno;
1290 n->assign.type.node_class = class;
1291 n->assign.src = src;
1292 n->assign.dst = dst;
1293 n->assign.defsym = defsym;
1294 n->assign.hidden = hidden;
1298 /* Handle linker script assignments and HIDDEN. */
1301 exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
1303 return exp_assop (dst, src, etree_assign, FALSE, hidden);
1306 /* Handle --defsym command-line option. */
1309 exp_defsym (const char *dst, etree_type *src)
1311 return exp_assop (dst, src, etree_assign, TRUE, FALSE);
1314 /* Handle PROVIDE. */
1317 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1319 return exp_assop (dst, src, etree_provide, FALSE, hidden);
1322 /* Handle ASSERT. */
1325 exp_assert (etree_type *exp, const char *message)
1329 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1330 n->assert_s.type.node_code = '!';
1331 n->assert_s.type.filename = exp->type.filename;
1332 n->assert_s.type.lineno = exp->type.lineno;
1333 n->assert_s.type.node_class = etree_assert;
1334 n->assert_s.child = exp;
1335 n->assert_s.message = message;
1340 exp_print_tree (etree_type *tree)
1342 bfd_boolean function_like;
1344 if (config.map_file == NULL)
1345 config.map_file = stderr;
1349 minfo ("NULL TREE\n");
1353 switch (tree->type.node_class)
1356 minfo ("0x%v", tree->value.value);
1359 if (tree->rel.section->owner != NULL)
1360 minfo ("%B:", tree->rel.section->owner);
1361 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1364 fputs (tree->assign.dst, config.map_file);
1365 exp_print_token (tree->type.node_code, TRUE);
1366 exp_print_tree (tree->assign.src);
1369 case etree_provided:
1370 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1371 exp_print_tree (tree->assign.src);
1372 fputc (')', config.map_file);
1375 function_like = FALSE;
1376 switch (tree->type.node_code)
1381 case DATA_SEGMENT_ALIGN:
1382 case DATA_SEGMENT_RELRO_END:
1383 function_like = TRUE;
1386 /* Special handling because arguments are in reverse order and
1387 the segment name is quoted. */
1388 exp_print_token (tree->type.node_code, FALSE);
1389 fputs (" (\"", config.map_file);
1390 exp_print_tree (tree->binary.rhs);
1391 fputs ("\", ", config.map_file);
1392 exp_print_tree (tree->binary.lhs);
1393 fputc (')', config.map_file);
1398 exp_print_token (tree->type.node_code, FALSE);
1399 fputc (' ', config.map_file);
1401 fputc ('(', config.map_file);
1402 exp_print_tree (tree->binary.lhs);
1404 fprintf (config.map_file, ", ");
1406 exp_print_token (tree->type.node_code, TRUE);
1407 exp_print_tree (tree->binary.rhs);
1408 fputc (')', config.map_file);
1411 exp_print_tree (tree->trinary.cond);
1412 fputc ('?', config.map_file);
1413 exp_print_tree (tree->trinary.lhs);
1414 fputc (':', config.map_file);
1415 exp_print_tree (tree->trinary.rhs);
1418 exp_print_token (tree->unary.type.node_code, FALSE);
1419 if (tree->unary.child)
1421 fprintf (config.map_file, " (");
1422 exp_print_tree (tree->unary.child);
1423 fputc (')', config.map_file);
1428 fprintf (config.map_file, "ASSERT (");
1429 exp_print_tree (tree->assert_s.child);
1430 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1434 if (tree->type.node_code == NAME)
1435 fputs (tree->name.name, config.map_file);
1438 exp_print_token (tree->type.node_code, FALSE);
1439 if (tree->name.name)
1440 fprintf (config.map_file, " (%s)", tree->name.name);
1450 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1454 exp_fold_tree_no_dot (tree);
1455 if (expld.result.valid_p)
1456 return expld.result.value;
1457 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1458 einfo (_("%F%S: nonconstant expression for %s\n"),
1465 exp_get_value_int (etree_type *tree, int def, char *name)
1467 return exp_get_vma (tree, def, name);
1471 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1480 exp_fold_tree_no_dot (tree);
1481 if (!expld.result.valid_p)
1483 if (name != NULL && expld.phase != lang_mark_phase_enum)
1484 einfo (_("%F%S: nonconstant expression for %s\n"),
1489 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1493 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1494 fill->size = (len + 1) / 2;
1496 s = (unsigned char *) expld.result.str;
1504 digit = (digit - 'A' + '0' + 10) & 0xf;
1518 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1519 val = expld.result.value;
1520 fill->data[0] = (val >> 24) & 0xff;
1521 fill->data[1] = (val >> 16) & 0xff;
1522 fill->data[2] = (val >> 8) & 0xff;
1523 fill->data[3] = (val >> 0) & 0xff;
1530 exp_get_abs_int (etree_type *tree, int def, char *name)
1534 exp_fold_tree_no_dot (tree);
1536 if (expld.result.valid_p)
1538 if (expld.result.section != NULL)
1539 expld.result.value += expld.result.section->vma;
1540 return expld.result.value;
1542 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1544 einfo (_("%F%S: nonconstant expression for %s\n"),
1552 align_n (bfd_vma value, bfd_vma align)
1557 value = (value + align - 1) / align;
1558 return value * align;
1564 /* The value "13" is ad-hoc, somewhat related to the expected number of
1565 assignments in a linker script. */
1566 if (!bfd_hash_table_init_n (&definedness_table,
1567 definedness_newfunc,
1568 sizeof (struct definedness_hash_entry),
1570 einfo (_("%P%F: can not create hash table: %E\n"));
1576 bfd_hash_table_free (&definedness_table);