1 /* This module handles expression trees.
2 Copyright 1991-2013 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 /* Print the string representation of the given token. Surround it
52 with spaces if INFIX_P is TRUE. */
55 exp_print_token (token_code_type code
, int infix_p
)
82 { LOG2CEIL
, "LOG2CEIL" },
90 { SECTIONS
, "SECTIONS" },
91 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
93 { DEFINED
, "DEFINED" },
94 { TARGET_K
, "TARGET" },
95 { SEARCH_DIR
, "SEARCH_DIR" },
99 { ALIGNOF
, "ALIGNOF" },
100 { SIZEOF
, "SIZEOF" },
102 { LOADADDR
, "LOADADDR" },
103 { CONSTANT
, "CONSTANT" },
104 { ABSOLUTE
, "ABSOLUTE" },
107 { ASSERT_K
, "ASSERT" },
108 { REL
, "relocatable" },
109 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
110 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
111 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
112 { ORIGIN
, "ORIGIN" },
113 { LENGTH
, "LENGTH" },
114 { SEGMENT_START
, "SEGMENT_START" }
118 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
119 if (table
[idx
].code
== code
)
123 fputc (' ', config
.map_file
);
125 if (idx
< ARRAY_SIZE (table
))
126 fputs (table
[idx
].name
, config
.map_file
);
128 fputc (code
, config
.map_file
);
130 fprintf (config
.map_file
, "<code %d>", code
);
133 fputc (' ', config
.map_file
);
139 bfd_vma value
= expld
.result
.value
;
141 bfd_boolean round_up
= FALSE
;
146 /* If more than one bit is set in the value we will need to round up. */
147 if ((value
> 1) && (value
& 1))
154 expld
.result
.section
= NULL
;
155 expld
.result
.value
= result
;
161 if (expld
.result
.section
!= NULL
)
162 expld
.result
.value
+= expld
.result
.section
->vma
;
163 expld
.result
.section
= bfd_abs_section_ptr
;
167 new_abs (bfd_vma value
)
169 expld
.result
.valid_p
= TRUE
;
170 expld
.result
.section
= bfd_abs_section_ptr
;
171 expld
.result
.value
= value
;
172 expld
.result
.str
= NULL
;
176 exp_intop (bfd_vma value
)
178 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->value
));
179 new_e
->type
.node_code
= INT
;
180 new_e
->type
.filename
= ldlex_filename ();
181 new_e
->type
.lineno
= lineno
;
182 new_e
->value
.value
= value
;
183 new_e
->value
.str
= NULL
;
184 new_e
->type
.node_class
= etree_value
;
189 exp_bigintop (bfd_vma value
, char *str
)
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
= str
;
197 new_e
->type
.node_class
= etree_value
;
201 /* Build an expression representing an unnamed relocatable value. */
204 exp_relop (asection
*section
, bfd_vma value
)
206 etree_type
*new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->rel
));
207 new_e
->type
.node_code
= REL
;
208 new_e
->type
.filename
= ldlex_filename ();
209 new_e
->type
.lineno
= lineno
;
210 new_e
->type
.node_class
= etree_rel
;
211 new_e
->rel
.section
= section
;
212 new_e
->rel
.value
= value
;
217 new_number (bfd_vma value
)
219 expld
.result
.valid_p
= TRUE
;
220 expld
.result
.value
= value
;
221 expld
.result
.str
= NULL
;
222 expld
.result
.section
= NULL
;
226 new_rel (bfd_vma value
, asection
*section
)
228 expld
.result
.valid_p
= TRUE
;
229 expld
.result
.value
= value
;
230 expld
.result
.str
= NULL
;
231 expld
.result
.section
= section
;
235 new_rel_from_abs (bfd_vma value
)
237 asection
*s
= expld
.section
;
239 if (s
== bfd_abs_section_ptr
&& expld
.phase
== lang_final_phase_enum
)
240 s
= section_for_dot ();
241 expld
.result
.valid_p
= TRUE
;
242 expld
.result
.value
= value
- s
->vma
;
243 expld
.result
.str
= NULL
;
244 expld
.result
.section
= s
;
248 fold_unary (etree_type
*tree
)
250 exp_fold_tree_1 (tree
->unary
.child
);
251 if (expld
.result
.valid_p
)
253 switch (tree
->type
.node_code
)
256 if (expld
.phase
!= lang_first_phase_enum
)
257 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
259 expld
.result
.valid_p
= FALSE
;
271 expld
.result
.value
= ~expld
.result
.value
;
275 expld
.result
.value
= !expld
.result
.value
;
279 expld
.result
.value
= -expld
.result
.value
;
283 /* Return next place aligned to value. */
284 if (expld
.phase
!= lang_first_phase_enum
)
287 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
290 expld
.result
.valid_p
= FALSE
;
293 case DATA_SEGMENT_END
:
294 if (expld
.phase
== lang_first_phase_enum
295 || expld
.section
!= bfd_abs_section_ptr
)
297 expld
.result
.valid_p
= FALSE
;
299 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
300 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
302 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
303 expld
.dataseg
.end
= expld
.result
.value
;
305 else if (expld
.dataseg
.phase
== exp_dataseg_done
306 || expld
.dataseg
.phase
== exp_dataseg_adjust
307 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
312 expld
.result
.valid_p
= FALSE
;
323 fold_binary (etree_type
*tree
)
325 etree_value_type lhs
;
326 exp_fold_tree_1 (tree
->binary
.lhs
);
328 /* The SEGMENT_START operator is special because its first
329 operand is a string, not the name of a symbol. Note that the
330 operands have been swapped, so binary.lhs is second (default)
331 operand, binary.rhs is first operand. */
332 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
334 const char *segment_name
;
337 /* Check to see if the user has overridden the default
339 segment_name
= tree
->binary
.rhs
->name
.name
;
340 for (seg
= segments
; seg
; seg
= seg
->next
)
341 if (strcmp (seg
->name
, segment_name
) == 0)
344 && config
.magic_demand_paged
345 && (seg
->value
% config
.maxpagesize
) != 0)
346 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
349 new_rel_from_abs (seg
->value
);
356 exp_fold_tree_1 (tree
->binary
.rhs
);
357 expld
.result
.valid_p
&= lhs
.valid_p
;
359 if (expld
.result
.valid_p
)
361 if (lhs
.section
!= expld
.result
.section
)
363 /* If the values are from different sections, and neither is
364 just a number, make both the source arguments absolute. */
365 if (expld
.result
.section
!= NULL
366 && lhs
.section
!= NULL
)
369 lhs
.value
+= lhs
.section
->vma
;
370 lhs
.section
= bfd_abs_section_ptr
;
373 /* If the rhs is just a number, keep the lhs section. */
374 else if (expld
.result
.section
== NULL
)
376 expld
.result
.section
= lhs
.section
;
377 /* Make this NULL so that we know one of the operands
378 was just a number, for later tests. */
382 /* At this point we know that both operands have the same
383 section, or at least one of them is a plain number. */
385 switch (tree
->type
.node_code
)
387 /* Arithmetic operators, bitwise AND, bitwise OR and XOR
388 keep the section of one of their operands only when the
389 other operand is a plain number. Losing the section when
390 operating on two symbols, ie. a result of a plain number,
391 is required for subtraction and XOR. It's justifiable
392 for the other operations on the grounds that adding,
393 multiplying etc. two section relative values does not
394 really make sense unless they are just treated as
396 The same argument could be made for many expressions
397 involving one symbol and a number. For example,
398 "1 << x" and "100 / x" probably should not be given the
399 section of x. The trouble is that if we fuss about such
400 things the rules become complex and it is onerous to
401 document ld expression evaluation. */
404 expld.result.value = lhs.value y expld.result.value; \
405 if (expld.result.section == lhs.section) \
406 expld.result.section = NULL; \
409 /* Comparison operators, logical AND, and logical OR always
410 return a plain number. */
413 expld.result.value = lhs.value y expld.result.value; \
414 expld.result.section = NULL; \
435 if (expld
.result
.value
!= 0)
436 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
437 % (bfd_signed_vma
) expld
.result
.value
);
438 else if (expld
.phase
!= lang_mark_phase_enum
)
439 einfo (_("%F%S %% by zero\n"), tree
->binary
.rhs
);
440 if (expld
.result
.section
== lhs
.section
)
441 expld
.result
.section
= NULL
;
445 if (expld
.result
.value
!= 0)
446 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
447 / (bfd_signed_vma
) expld
.result
.value
);
448 else if (expld
.phase
!= lang_mark_phase_enum
)
449 einfo (_("%F%S / by zero\n"), tree
->binary
.rhs
);
450 if (expld
.result
.section
== lhs
.section
)
451 expld
.result
.section
= NULL
;
455 if (lhs
.value
> expld
.result
.value
)
456 expld
.result
.value
= lhs
.value
;
460 if (lhs
.value
< expld
.result
.value
)
461 expld
.result
.value
= lhs
.value
;
465 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
468 case DATA_SEGMENT_ALIGN
:
469 expld
.dataseg
.relro
= exp_dataseg_relro_start
;
470 if (expld
.phase
== lang_first_phase_enum
471 || expld
.section
!= bfd_abs_section_ptr
)
472 expld
.result
.valid_p
= FALSE
;
475 bfd_vma maxpage
= lhs
.value
;
476 bfd_vma commonpage
= expld
.result
.value
;
478 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
479 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
480 expld
.result
.value
= expld
.dataseg
.base
;
481 else if (expld
.dataseg
.phase
== exp_dataseg_adjust
)
483 if (commonpage
< maxpage
)
484 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
485 & (maxpage
- commonpage
));
489 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
490 if (expld
.dataseg
.phase
== exp_dataseg_done
)
494 else if (expld
.dataseg
.phase
== exp_dataseg_none
)
496 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
497 expld
.dataseg
.min_base
= expld
.dot
;
498 expld
.dataseg
.base
= expld
.result
.value
;
499 expld
.dataseg
.pagesize
= commonpage
;
500 expld
.dataseg
.maxpagesize
= maxpage
;
501 expld
.dataseg
.relro_end
= 0;
504 expld
.result
.valid_p
= FALSE
;
509 case DATA_SEGMENT_RELRO_END
:
510 expld
.dataseg
.relro
= exp_dataseg_relro_end
;
511 if (expld
.phase
== lang_first_phase_enum
512 || expld
.section
!= bfd_abs_section_ptr
)
513 expld
.result
.valid_p
= FALSE
;
514 else if (expld
.dataseg
.phase
== exp_dataseg_align_seen
515 || expld
.dataseg
.phase
== exp_dataseg_adjust
516 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
517 || expld
.dataseg
.phase
== exp_dataseg_done
)
519 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
520 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
521 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
523 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
524 && (expld
.dataseg
.relro_end
525 & (expld
.dataseg
.pagesize
- 1)))
527 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
528 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
529 expld
.result
.value
= (expld
.dataseg
.relro_end
530 - expld
.result
.value
);
533 expld
.result
.value
= lhs
.value
;
535 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
536 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
539 expld
.result
.valid_p
= FALSE
;
549 fold_trinary (etree_type
*tree
)
551 exp_fold_tree_1 (tree
->trinary
.cond
);
552 if (expld
.result
.valid_p
)
553 exp_fold_tree_1 (expld
.result
.value
555 : tree
->trinary
.rhs
);
559 fold_name (etree_type
*tree
)
561 memset (&expld
.result
, 0, sizeof (expld
.result
));
563 switch (tree
->type
.node_code
)
566 if (expld
.phase
!= lang_first_phase_enum
)
568 bfd_vma hdr_size
= 0;
569 /* Don't find the real header size if only marking sections;
570 The bfd function may cache incorrect data. */
571 if (expld
.phase
!= lang_mark_phase_enum
)
572 hdr_size
= bfd_sizeof_headers (link_info
.output_bfd
, &link_info
);
573 new_number (hdr_size
);
578 if (expld
.phase
== lang_first_phase_enum
)
579 lang_track_definedness (tree
->name
.name
);
582 struct bfd_link_hash_entry
*h
;
584 = lang_symbol_definition_iteration (tree
->name
.name
);
586 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
590 new_number (h
!= NULL
591 && (h
->type
== bfd_link_hash_defined
592 || h
->type
== bfd_link_hash_defweak
593 || h
->type
== bfd_link_hash_common
)
594 && (def_iteration
== lang_statement_iteration
595 || def_iteration
== -1));
600 if (expld
.assign_name
!= NULL
601 && strcmp (expld
.assign_name
, tree
->name
.name
) == 0)
602 expld
.assign_name
= NULL
;
603 if (expld
.phase
== lang_first_phase_enum
)
605 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
606 new_rel_from_abs (expld
.dot
);
609 struct bfd_link_hash_entry
*h
;
611 h
= bfd_wrapped_link_hash_lookup (link_info
.output_bfd
,
616 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
617 else if (h
->type
== bfd_link_hash_defined
618 || h
->type
== bfd_link_hash_defweak
)
620 asection
*output_section
;
622 output_section
= h
->u
.def
.section
->output_section
;
623 if (output_section
== NULL
)
625 if (expld
.phase
== lang_mark_phase_enum
)
626 new_rel (h
->u
.def
.value
, h
->u
.def
.section
);
628 einfo (_("%X%S: unresolvable symbol `%s'"
629 " referenced in expression\n"),
630 tree
, tree
->name
.name
);
632 else if (output_section
== bfd_abs_section_ptr
633 && (expld
.section
!= bfd_abs_section_ptr
634 || config
.sane_expr
))
635 new_number (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
);
637 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
640 else if (expld
.phase
== lang_final_phase_enum
641 || (expld
.phase
!= lang_mark_phase_enum
642 && expld
.assigning_to_dot
))
643 einfo (_("%F%S: undefined symbol `%s'"
644 " referenced in expression\n"),
645 tree
, tree
->name
.name
);
646 else if (h
->type
== bfd_link_hash_new
)
648 h
->type
= bfd_link_hash_undefined
;
649 h
->u
.undef
.abfd
= NULL
;
650 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
651 bfd_link_add_undef (link_info
.hash
, h
);
657 if (expld
.phase
!= lang_first_phase_enum
)
659 lang_output_section_statement_type
*os
;
661 os
= lang_output_section_find (tree
->name
.name
);
664 if (expld
.phase
== lang_final_phase_enum
)
665 einfo (_("%F%S: undefined section `%s'"
666 " referenced in expression\n"),
667 tree
, tree
->name
.name
);
669 else if (os
->processed_vma
)
670 new_rel (0, os
->bfd_section
);
675 if (expld
.phase
!= lang_first_phase_enum
)
677 lang_output_section_statement_type
*os
;
679 os
= lang_output_section_find (tree
->name
.name
);
682 if (expld
.phase
== lang_final_phase_enum
)
683 einfo (_("%F%S: undefined section `%s'"
684 " referenced in expression\n"),
685 tree
, tree
->name
.name
);
687 else if (os
->processed_lma
)
689 if (os
->load_base
== NULL
)
690 new_abs (os
->bfd_section
->lma
);
693 exp_fold_tree_1 (os
->load_base
);
694 if (expld
.result
.valid_p
)
703 if (expld
.phase
!= lang_first_phase_enum
)
705 lang_output_section_statement_type
*os
;
707 os
= lang_output_section_find (tree
->name
.name
);
710 if (expld
.phase
== lang_final_phase_enum
)
711 einfo (_("%F%S: undefined section `%s'"
712 " referenced in expression\n"),
713 tree
, tree
->name
.name
);
716 else if (os
->bfd_section
!= NULL
)
720 if (tree
->type
.node_code
== SIZEOF
)
721 val
= (os
->bfd_section
->size
722 / bfd_octets_per_byte (link_info
.output_bfd
));
724 val
= (bfd_vma
)1 << os
->bfd_section
->alignment_power
;
735 lang_memory_region_type
*mem
;
737 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
739 new_number (mem
->length
);
741 einfo (_("%F%S: undefined MEMORY region `%s'"
742 " referenced in expression\n"),
743 tree
, tree
->name
.name
);
748 if (expld
.phase
!= lang_first_phase_enum
)
750 lang_memory_region_type
*mem
;
752 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
754 new_rel_from_abs (mem
->origin
);
756 einfo (_("%F%S: undefined MEMORY region `%s'"
757 " referenced in expression\n"),
758 tree
, tree
->name
.name
);
763 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
764 new_number (config
.maxpagesize
);
765 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
766 new_number (config
.commonpagesize
);
768 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
769 tree
, tree
->name
.name
);
779 exp_fold_tree_1 (etree_type
*tree
)
783 memset (&expld
.result
, 0, sizeof (expld
.result
));
787 switch (tree
->type
.node_class
)
790 if (expld
.section
== bfd_abs_section_ptr
791 && !config
.sane_expr
)
792 new_abs (tree
->value
.value
);
794 new_number (tree
->value
.value
);
795 expld
.result
.str
= tree
->value
.str
;
799 if (expld
.phase
!= lang_first_phase_enum
)
801 asection
*output_section
= tree
->rel
.section
->output_section
;
802 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
806 memset (&expld
.result
, 0, sizeof (expld
.result
));
810 exp_fold_tree_1 (tree
->assert_s
.child
);
811 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
812 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
830 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
832 if (tree
->type
.node_class
!= etree_assign
)
833 einfo (_("%F%S can not PROVIDE assignment to"
834 " location counter\n"), tree
);
835 if (expld
.phase
!= lang_first_phase_enum
)
837 /* Notify the folder that this is an assignment to dot. */
838 expld
.assigning_to_dot
= TRUE
;
839 exp_fold_tree_1 (tree
->assign
.src
);
840 expld
.assigning_to_dot
= FALSE
;
842 if (!expld
.result
.valid_p
)
844 if (expld
.phase
!= lang_mark_phase_enum
)
845 einfo (_("%F%S invalid assignment to"
846 " location counter\n"), tree
);
848 else if (expld
.dotp
== NULL
)
849 einfo (_("%F%S assignment to location counter"
850 " invalid outside of SECTIONS\n"), tree
);
852 /* After allocation, assignment to dot should not be
853 done inside an output section since allocation adds a
854 padding statement that effectively duplicates the
856 else if (expld
.phase
<= lang_allocating_phase_enum
857 || expld
.section
== bfd_abs_section_ptr
)
861 nextdot
= expld
.result
.value
;
862 if (expld
.result
.section
!= NULL
)
863 nextdot
+= expld
.result
.section
->vma
;
865 nextdot
+= expld
.section
->vma
;
866 if (nextdot
< expld
.dot
867 && expld
.section
!= bfd_abs_section_ptr
)
868 einfo (_("%F%S cannot move location counter backwards"
869 " (from %V to %V)\n"),
870 tree
, expld
.dot
, nextdot
);
874 *expld
.dotp
= nextdot
;
879 memset (&expld
.result
, 0, sizeof (expld
.result
));
883 struct bfd_link_hash_entry
*h
= NULL
;
885 if (tree
->type
.node_class
== etree_provide
)
887 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
890 || (h
->type
!= bfd_link_hash_new
891 && h
->type
!= bfd_link_hash_undefined
892 && h
->type
!= bfd_link_hash_common
))
894 /* Do nothing. The symbol was never referenced, or was
895 defined by some object. */
900 expld
.assign_name
= tree
->assign
.dst
;
901 exp_fold_tree_1 (tree
->assign
.src
);
902 /* expld.assign_name remaining equal to tree->assign.dst
903 below indicates the evaluation of tree->assign.src did
904 not use the value of tree->assign.dst. We don't allow
905 self assignment until the final phase for two reasons:
906 1) Expressions are evaluated multiple times. With
907 relaxation, the number of times may vary.
908 2) Section relative symbol values cannot be correctly
909 converted to absolute values, as is required by many
910 expressions, until final section sizing is complete. */
911 if ((expld
.result
.valid_p
912 && (expld
.phase
== lang_final_phase_enum
913 || expld
.assign_name
!= NULL
))
914 || (expld
.phase
<= lang_mark_phase_enum
915 && tree
->type
.node_class
== etree_assign
916 && tree
->assign
.defsym
))
920 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
923 einfo (_("%P%F:%s: hash creation failed\n"),
927 /* FIXME: Should we worry if the symbol is already
929 lang_update_definedness (tree
->assign
.dst
, h
);
930 h
->type
= bfd_link_hash_defined
;
931 h
->u
.def
.value
= expld
.result
.value
;
932 if (expld
.result
.section
== NULL
)
933 expld
.result
.section
= expld
.section
;
934 h
->u
.def
.section
= expld
.result
.section
;
935 if (tree
->type
.node_class
== etree_provide
)
936 tree
->type
.node_class
= etree_provided
;
938 /* Copy the symbol type if this is a simple assignment of
939 one symbol to another. This could be more general
940 (e.g. a ?: operator with NAMEs in each branch). */
941 if (tree
->assign
.src
->type
.node_class
== etree_name
)
943 struct bfd_link_hash_entry
*hsrc
;
945 hsrc
= bfd_link_hash_lookup (link_info
.hash
,
946 tree
->assign
.src
->name
.name
,
949 bfd_copy_link_hash_symbol_type (link_info
.output_bfd
, h
,
953 else if (expld
.phase
== lang_final_phase_enum
)
955 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
958 && h
->type
== bfd_link_hash_new
)
959 h
->type
= bfd_link_hash_undefined
;
961 expld
.assign_name
= NULL
;
971 memset (&expld
.result
, 0, sizeof (expld
.result
));
977 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
981 expld
.section
= current_section
;
982 exp_fold_tree_1 (tree
);
986 exp_fold_tree_no_dot (etree_type
*tree
)
990 expld
.section
= bfd_abs_section_ptr
;
991 exp_fold_tree_1 (tree
);
995 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
997 etree_type value
, *new_e
;
999 value
.type
.node_code
= code
;
1000 value
.type
.filename
= lhs
->type
.filename
;
1001 value
.type
.lineno
= lhs
->type
.lineno
;
1002 value
.binary
.lhs
= lhs
;
1003 value
.binary
.rhs
= rhs
;
1004 value
.type
.node_class
= etree_binary
;
1005 exp_fold_tree_no_dot (&value
);
1006 if (expld
.result
.valid_p
)
1007 return exp_intop (expld
.result
.value
);
1009 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->binary
));
1010 memcpy (new_e
, &value
, sizeof (new_e
->binary
));
1015 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
1017 etree_type value
, *new_e
;
1019 value
.type
.node_code
= code
;
1020 value
.type
.filename
= cond
->type
.filename
;
1021 value
.type
.lineno
= cond
->type
.lineno
;
1022 value
.trinary
.lhs
= lhs
;
1023 value
.trinary
.cond
= cond
;
1024 value
.trinary
.rhs
= rhs
;
1025 value
.type
.node_class
= etree_trinary
;
1026 exp_fold_tree_no_dot (&value
);
1027 if (expld
.result
.valid_p
)
1028 return exp_intop (expld
.result
.value
);
1030 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->trinary
));
1031 memcpy (new_e
, &value
, sizeof (new_e
->trinary
));
1036 exp_unop (int code
, etree_type
*child
)
1038 etree_type value
, *new_e
;
1040 value
.unary
.type
.node_code
= code
;
1041 value
.unary
.type
.filename
= child
->type
.filename
;
1042 value
.unary
.type
.lineno
= child
->type
.lineno
;
1043 value
.unary
.child
= child
;
1044 value
.unary
.type
.node_class
= etree_unary
;
1045 exp_fold_tree_no_dot (&value
);
1046 if (expld
.result
.valid_p
)
1047 return exp_intop (expld
.result
.value
);
1049 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->unary
));
1050 memcpy (new_e
, &value
, sizeof (new_e
->unary
));
1055 exp_nameop (int code
, const char *name
)
1057 etree_type value
, *new_e
;
1059 value
.name
.type
.node_code
= code
;
1060 value
.name
.type
.filename
= ldlex_filename ();
1061 value
.name
.type
.lineno
= lineno
;
1062 value
.name
.name
= name
;
1063 value
.name
.type
.node_class
= etree_name
;
1065 exp_fold_tree_no_dot (&value
);
1066 if (expld
.result
.valid_p
)
1067 return exp_intop (expld
.result
.value
);
1069 new_e
= (etree_type
*) stat_alloc (sizeof (new_e
->name
));
1070 memcpy (new_e
, &value
, sizeof (new_e
->name
));
1076 exp_assop (const char *dst
,
1078 enum node_tree_enum
class,
1084 n
= (etree_type
*) stat_alloc (sizeof (n
->assign
));
1085 n
->assign
.type
.node_code
= '=';
1086 n
->assign
.type
.filename
= src
->type
.filename
;
1087 n
->assign
.type
.lineno
= src
->type
.lineno
;
1088 n
->assign
.type
.node_class
= class;
1089 n
->assign
.src
= src
;
1090 n
->assign
.dst
= dst
;
1091 n
->assign
.defsym
= defsym
;
1092 n
->assign
.hidden
= hidden
;
1096 /* Handle linker script assignments and HIDDEN. */
1099 exp_assign (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1101 return exp_assop (dst
, src
, etree_assign
, FALSE
, hidden
);
1104 /* Handle --defsym command-line option. */
1107 exp_defsym (const char *dst
, etree_type
*src
)
1109 return exp_assop (dst
, src
, etree_assign
, TRUE
, FALSE
);
1112 /* Handle PROVIDE. */
1115 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
1117 return exp_assop (dst
, src
, etree_provide
, FALSE
, hidden
);
1120 /* Handle ASSERT. */
1123 exp_assert (etree_type
*exp
, const char *message
)
1127 n
= (etree_type
*) stat_alloc (sizeof (n
->assert_s
));
1128 n
->assert_s
.type
.node_code
= '!';
1129 n
->assert_s
.type
.filename
= exp
->type
.filename
;
1130 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
1131 n
->assert_s
.type
.node_class
= etree_assert
;
1132 n
->assert_s
.child
= exp
;
1133 n
->assert_s
.message
= message
;
1138 exp_print_tree (etree_type
*tree
)
1140 bfd_boolean function_like
;
1142 if (config
.map_file
== NULL
)
1143 config
.map_file
= stderr
;
1147 minfo ("NULL TREE\n");
1151 switch (tree
->type
.node_class
)
1154 minfo ("0x%v", tree
->value
.value
);
1157 if (tree
->rel
.section
->owner
!= NULL
)
1158 minfo ("%B:", tree
->rel
.section
->owner
);
1159 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
1162 fputs (tree
->assign
.dst
, config
.map_file
);
1163 exp_print_token (tree
->type
.node_code
, TRUE
);
1164 exp_print_tree (tree
->assign
.src
);
1167 case etree_provided
:
1168 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
1169 exp_print_tree (tree
->assign
.src
);
1170 fputc (')', config
.map_file
);
1173 function_like
= FALSE
;
1174 switch (tree
->type
.node_code
)
1179 case DATA_SEGMENT_ALIGN
:
1180 case DATA_SEGMENT_RELRO_END
:
1181 function_like
= TRUE
;
1184 /* Special handling because arguments are in reverse order and
1185 the segment name is quoted. */
1186 exp_print_token (tree
->type
.node_code
, FALSE
);
1187 fputs (" (\"", config
.map_file
);
1188 exp_print_tree (tree
->binary
.rhs
);
1189 fputs ("\", ", config
.map_file
);
1190 exp_print_tree (tree
->binary
.lhs
);
1191 fputc (')', config
.map_file
);
1196 exp_print_token (tree
->type
.node_code
, FALSE
);
1197 fputc (' ', config
.map_file
);
1199 fputc ('(', config
.map_file
);
1200 exp_print_tree (tree
->binary
.lhs
);
1202 fprintf (config
.map_file
, ", ");
1204 exp_print_token (tree
->type
.node_code
, TRUE
);
1205 exp_print_tree (tree
->binary
.rhs
);
1206 fputc (')', config
.map_file
);
1209 exp_print_tree (tree
->trinary
.cond
);
1210 fputc ('?', config
.map_file
);
1211 exp_print_tree (tree
->trinary
.lhs
);
1212 fputc (':', config
.map_file
);
1213 exp_print_tree (tree
->trinary
.rhs
);
1216 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1217 if (tree
->unary
.child
)
1219 fprintf (config
.map_file
, " (");
1220 exp_print_tree (tree
->unary
.child
);
1221 fputc (')', config
.map_file
);
1226 fprintf (config
.map_file
, "ASSERT (");
1227 exp_print_tree (tree
->assert_s
.child
);
1228 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1232 if (tree
->type
.node_code
== NAME
)
1233 fputs (tree
->name
.name
, config
.map_file
);
1236 exp_print_token (tree
->type
.node_code
, FALSE
);
1237 if (tree
->name
.name
)
1238 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1248 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1252 exp_fold_tree_no_dot (tree
);
1253 if (expld
.result
.valid_p
)
1254 return expld
.result
.value
;
1255 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1256 einfo (_("%F%S: nonconstant expression for %s\n"),
1263 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1265 return exp_get_vma (tree
, def
, name
);
1269 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1278 exp_fold_tree_no_dot (tree
);
1279 if (!expld
.result
.valid_p
)
1281 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1282 einfo (_("%F%S: nonconstant expression for %s\n"),
1287 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1291 fill
= (fill_type
*) xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1292 fill
->size
= (len
+ 1) / 2;
1294 s
= (unsigned char *) expld
.result
.str
;
1302 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1316 fill
= (fill_type
*) xmalloc (4 + sizeof (*fill
) - 1);
1317 val
= expld
.result
.value
;
1318 fill
->data
[0] = (val
>> 24) & 0xff;
1319 fill
->data
[1] = (val
>> 16) & 0xff;
1320 fill
->data
[2] = (val
>> 8) & 0xff;
1321 fill
->data
[3] = (val
>> 0) & 0xff;
1328 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1332 exp_fold_tree_no_dot (tree
);
1334 if (expld
.result
.valid_p
)
1336 if (expld
.result
.section
!= NULL
)
1337 expld
.result
.value
+= expld
.result
.section
->vma
;
1338 return expld
.result
.value
;
1340 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1342 einfo (_("%F%S: nonconstant expression for %s\n"),
1350 align_n (bfd_vma value
, bfd_vma align
)
1355 value
= (value
+ align
- 1) / align
;
1356 return value
* align
;