1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
7 This file is part of GLD, the Gnu Linker.
9 GLD is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GLD is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GLD; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
24 /* This module is in charge of working out the contents of expressions.
26 It has to keep track of the relative/absness of a symbol etc. This
27 is done by keeping all values in a struct (an etree_value_type)
28 which contains a value, a section to which it is relative and a
42 #include "libiberty.h"
43 #include "safe-ctype.h"
45 static void exp_fold_tree_1 (etree_type
*);
46 static void exp_fold_tree_no_dot (etree_type
*);
47 static bfd_vma
align_n (bfd_vma
, bfd_vma
);
49 segment_type
*segments
;
51 struct ldexp_control expld
;
53 /* Print the string representation of the given token. Surround it
54 with spaces if INFIX_P is TRUE. */
57 exp_print_token (token_code_type code
, int infix_p
)
91 { SECTIONS
, "SECTIONS" },
92 { SIZEOF_HEADERS
, "SIZEOF_HEADERS" },
94 { DEFINED
, "DEFINED" },
95 { TARGET_K
, "TARGET" },
96 { SEARCH_DIR
, "SEARCH_DIR" },
100 { SIZEOF
, "SIZEOF" },
102 { LOADADDR
, "LOADADDR" },
103 { CONSTANT
, "CONSTANT" },
105 { REL
, "relocatable" },
106 { DATA_SEGMENT_ALIGN
, "DATA_SEGMENT_ALIGN" },
107 { DATA_SEGMENT_RELRO_END
, "DATA_SEGMENT_RELRO_END" },
108 { DATA_SEGMENT_END
, "DATA_SEGMENT_END" },
109 { ORIGIN
, "ORIGIN" },
110 { LENGTH
, "LENGTH" },
111 { SEGMENT_START
, "SEGMENT_START" }
115 for (idx
= 0; idx
< ARRAY_SIZE (table
); idx
++)
116 if (table
[idx
].code
== code
)
120 fputc (' ', config
.map_file
);
122 if (idx
< ARRAY_SIZE (table
))
123 fputs (table
[idx
].name
, config
.map_file
);
125 fputc (code
, config
.map_file
);
127 fprintf (config
.map_file
, "<code %d>", code
);
130 fputc (' ', config
.map_file
);
136 expld
.result
.value
+= expld
.result
.section
->vma
;
137 expld
.result
.section
= bfd_abs_section_ptr
;
141 new_abs (bfd_vma value
)
143 expld
.result
.valid_p
= TRUE
;
144 expld
.result
.section
= bfd_abs_section_ptr
;
145 expld
.result
.value
= value
;
146 expld
.result
.str
= NULL
;
150 exp_intop (bfd_vma value
)
152 etree_type
*new = stat_alloc (sizeof (new->value
));
153 new->type
.node_code
= INT
;
154 new->type
.lineno
= lineno
;
155 new->value
.value
= value
;
156 new->value
.str
= NULL
;
157 new->type
.node_class
= etree_value
;
162 exp_bigintop (bfd_vma value
, char *str
)
164 etree_type
*new = stat_alloc (sizeof (new->value
));
165 new->type
.node_code
= INT
;
166 new->type
.lineno
= lineno
;
167 new->value
.value
= value
;
168 new->value
.str
= str
;
169 new->type
.node_class
= etree_value
;
173 /* Build an expression representing an unnamed relocatable value. */
176 exp_relop (asection
*section
, bfd_vma value
)
178 etree_type
*new = stat_alloc (sizeof (new->rel
));
179 new->type
.node_code
= REL
;
180 new->type
.lineno
= lineno
;
181 new->type
.node_class
= etree_rel
;
182 new->rel
.section
= section
;
183 new->rel
.value
= value
;
188 new_rel (bfd_vma value
, char *str
, asection
*section
)
190 expld
.result
.valid_p
= TRUE
;
191 expld
.result
.value
= value
;
192 expld
.result
.str
= str
;
193 expld
.result
.section
= section
;
197 new_rel_from_abs (bfd_vma value
)
199 expld
.result
.valid_p
= TRUE
;
200 expld
.result
.value
= value
- expld
.section
->vma
;
201 expld
.result
.str
= NULL
;
202 expld
.result
.section
= expld
.section
;
206 fold_unary (etree_type
*tree
)
208 exp_fold_tree_1 (tree
->unary
.child
);
209 if (expld
.result
.valid_p
)
211 switch (tree
->type
.node_code
)
214 if (expld
.phase
!= lang_first_phase_enum
)
215 new_rel_from_abs (align_n (expld
.dot
, expld
.result
.value
));
217 expld
.result
.valid_p
= FALSE
;
226 expld
.result
.value
= ~expld
.result
.value
;
231 expld
.result
.value
= !expld
.result
.value
;
236 expld
.result
.value
= -expld
.result
.value
;
240 /* Return next place aligned to value. */
241 if (expld
.phase
!= lang_first_phase_enum
)
244 expld
.result
.value
= align_n (expld
.dot
, expld
.result
.value
);
247 expld
.result
.valid_p
= FALSE
;
250 case DATA_SEGMENT_END
:
251 if (expld
.phase
!= lang_first_phase_enum
252 && expld
.section
== bfd_abs_section_ptr
253 && (expld
.dataseg
.phase
== exp_dataseg_align_seen
254 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
255 || expld
.dataseg
.phase
== exp_dataseg_adjust
256 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
257 || expld
.phase
== lang_final_phase_enum
))
259 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
260 || expld
.dataseg
.phase
== exp_dataseg_relro_seen
)
262 expld
.dataseg
.phase
= exp_dataseg_end_seen
;
263 expld
.dataseg
.end
= expld
.result
.value
;
267 expld
.result
.valid_p
= FALSE
;
278 fold_binary (etree_type
*tree
)
280 exp_fold_tree_1 (tree
->binary
.lhs
);
282 /* The SEGMENT_START operator is special because its first
283 operand is a string, not the name of a symbol. */
284 if (expld
.result
.valid_p
&& tree
->type
.node_code
== SEGMENT_START
)
286 const char *segment_name
;
288 /* Check to see if the user has overridden the default
290 segment_name
= tree
->binary
.rhs
->name
.name
;
291 for (seg
= segments
; seg
; seg
= seg
->next
)
292 if (strcmp (seg
->name
, segment_name
) == 0)
295 expld
.result
.value
= seg
->value
;
296 expld
.result
.str
= NULL
;
297 expld
.result
.section
= NULL
;
301 else if (expld
.result
.valid_p
)
303 etree_value_type lhs
= expld
.result
;
305 exp_fold_tree_1 (tree
->binary
.rhs
);
306 if (expld
.result
.valid_p
)
308 /* If the values are from different sections, or this is an
309 absolute expression, make both the source arguments
310 absolute. However, adding or subtracting an absolute
311 value from a relative value is meaningful, and is an
313 if (expld
.section
!= bfd_abs_section_ptr
314 && lhs
.section
== bfd_abs_section_ptr
315 && tree
->type
.node_code
== '+')
317 /* Keep the section of the rhs term. */
318 expld
.result
.value
= lhs
.value
+ expld
.result
.value
;
321 else if (expld
.section
!= bfd_abs_section_ptr
322 && expld
.result
.section
== bfd_abs_section_ptr
323 && (tree
->type
.node_code
== '+'
324 || tree
->type
.node_code
== '-'))
326 /* Keep the section of the lhs term. */
327 expld
.result
.section
= lhs
.section
;
329 else if (expld
.result
.section
!= lhs
.section
330 || expld
.section
== bfd_abs_section_ptr
)
333 lhs
.value
+= lhs
.section
->vma
;
336 switch (tree
->type
.node_code
)
339 if (expld
.result
.value
!= 0)
340 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
341 % (bfd_signed_vma
) expld
.result
.value
);
342 else if (expld
.phase
!= lang_mark_phase_enum
)
343 einfo (_("%F%S %% by zero\n"));
347 if (expld
.result
.value
!= 0)
348 expld
.result
.value
= ((bfd_signed_vma
) lhs
.value
349 / (bfd_signed_vma
) expld
.result
.value
);
350 else if (expld
.phase
!= lang_mark_phase_enum
)
351 einfo (_("%F%S / by zero\n"));
356 expld.result.value = lhs.value y expld.result.value; \
377 if (lhs
.value
> expld
.result
.value
)
378 expld
.result
.value
= lhs
.value
;
382 if (lhs
.value
< expld
.result
.value
)
383 expld
.result
.value
= lhs
.value
;
387 expld
.result
.value
= align_n (lhs
.value
, expld
.result
.value
);
390 case DATA_SEGMENT_ALIGN
:
391 if (expld
.phase
!= lang_first_phase_enum
392 && expld
.section
== bfd_abs_section_ptr
393 && (expld
.dataseg
.phase
== exp_dataseg_none
394 || expld
.dataseg
.phase
== exp_dataseg_adjust
395 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
396 || expld
.phase
== lang_final_phase_enum
))
398 bfd_vma maxpage
= lhs
.value
;
399 bfd_vma commonpage
= expld
.result
.value
;
401 expld
.result
.value
= align_n (expld
.dot
, maxpage
);
402 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
403 expld
.result
.value
= expld
.dataseg
.base
;
404 else if (expld
.dataseg
.phase
!= exp_dataseg_adjust
)
406 expld
.result
.value
+= expld
.dot
& (maxpage
- 1);
407 if (expld
.phase
== lang_allocating_phase_enum
)
409 expld
.dataseg
.phase
= exp_dataseg_align_seen
;
410 expld
.dataseg
.min_base
= align_n (expld
.dot
, maxpage
);
411 expld
.dataseg
.base
= expld
.result
.value
;
412 expld
.dataseg
.pagesize
= commonpage
;
413 expld
.dataseg
.maxpagesize
= maxpage
;
414 expld
.dataseg
.relro_end
= 0;
417 else if (commonpage
< maxpage
)
418 expld
.result
.value
+= ((expld
.dot
+ commonpage
- 1)
419 & (maxpage
- commonpage
));
422 expld
.result
.valid_p
= FALSE
;
425 case DATA_SEGMENT_RELRO_END
:
426 if (expld
.phase
!= lang_first_phase_enum
427 && (expld
.dataseg
.phase
== exp_dataseg_align_seen
428 || expld
.dataseg
.phase
== exp_dataseg_adjust
429 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
430 || expld
.phase
== lang_final_phase_enum
))
432 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
433 || expld
.dataseg
.phase
== exp_dataseg_relro_adjust
)
434 expld
.dataseg
.relro_end
= lhs
.value
+ expld
.result
.value
;
436 if (expld
.dataseg
.phase
== exp_dataseg_relro_adjust
437 && (expld
.dataseg
.relro_end
438 & (expld
.dataseg
.pagesize
- 1)))
440 expld
.dataseg
.relro_end
+= expld
.dataseg
.pagesize
- 1;
441 expld
.dataseg
.relro_end
&= ~(expld
.dataseg
.pagesize
- 1);
442 expld
.result
.value
= (expld
.dataseg
.relro_end
443 - expld
.result
.value
);
446 expld
.result
.value
= lhs
.value
;
448 if (expld
.dataseg
.phase
== exp_dataseg_align_seen
)
449 expld
.dataseg
.phase
= exp_dataseg_relro_seen
;
452 expld
.result
.valid_p
= FALSE
;
460 expld
.result
.valid_p
= FALSE
;
465 fold_trinary (etree_type
*tree
)
467 exp_fold_tree_1 (tree
->trinary
.cond
);
468 if (expld
.result
.valid_p
)
469 exp_fold_tree_1 (expld
.result
.value
471 : tree
->trinary
.rhs
);
475 fold_name (etree_type
*tree
)
477 memset (&expld
.result
, 0, sizeof (expld
.result
));
479 switch (tree
->type
.node_code
)
482 if (expld
.phase
!= lang_first_phase_enum
)
484 bfd_vma hdr_size
= 0;
485 /* Don't find the real header size if only marking sections;
486 The bfd function may cache incorrect data. */
487 if (expld
.phase
!= lang_mark_phase_enum
)
488 hdr_size
= bfd_sizeof_headers (output_bfd
, &link_info
);
494 if (expld
.phase
== lang_first_phase_enum
)
495 lang_track_definedness (tree
->name
.name
);
498 struct bfd_link_hash_entry
*h
;
500 = lang_symbol_definition_iteration (tree
->name
.name
);
502 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
505 expld
.result
.value
= (h
!= NULL
506 && (h
->type
== bfd_link_hash_defined
507 || h
->type
== bfd_link_hash_defweak
508 || h
->type
== bfd_link_hash_common
)
509 && (def_iteration
== lang_statement_iteration
510 || def_iteration
== -1));
511 expld
.result
.section
= bfd_abs_section_ptr
;
512 expld
.result
.valid_p
= TRUE
;
517 if (expld
.phase
== lang_first_phase_enum
)
519 else if (tree
->name
.name
[0] == '.' && tree
->name
.name
[1] == 0)
520 new_rel_from_abs (expld
.dot
);
523 struct bfd_link_hash_entry
*h
;
525 h
= bfd_wrapped_link_hash_lookup (output_bfd
, &link_info
,
529 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
530 else if (h
->type
== bfd_link_hash_defined
531 || h
->type
== bfd_link_hash_defweak
)
533 if (bfd_is_abs_section (h
->u
.def
.section
))
534 new_abs (h
->u
.def
.value
);
537 asection
*output_section
;
539 output_section
= h
->u
.def
.section
->output_section
;
540 if (output_section
== NULL
)
542 if (expld
.phase
!= lang_mark_phase_enum
)
543 einfo (_("%X%S: unresolvable symbol `%s'"
544 " referenced in expression\n"),
548 new_rel (h
->u
.def
.value
+ h
->u
.def
.section
->output_offset
,
549 NULL
, output_section
);
552 else if (expld
.phase
== lang_final_phase_enum
553 || expld
.assigning_to_dot
)
554 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
556 else if (h
->type
== bfd_link_hash_new
)
558 h
->type
= bfd_link_hash_undefined
;
559 h
->u
.undef
.abfd
= NULL
;
560 if (h
->u
.undef
.next
== NULL
&& h
!= link_info
.hash
->undefs_tail
)
561 bfd_link_add_undef (link_info
.hash
, h
);
567 if (expld
.phase
!= lang_first_phase_enum
)
569 lang_output_section_statement_type
*os
;
571 os
= lang_output_section_find (tree
->name
.name
);
574 if (expld
.phase
== lang_final_phase_enum
)
575 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
578 else if (os
->processed_vma
)
579 new_rel (0, NULL
, os
->bfd_section
);
584 if (expld
.phase
!= lang_first_phase_enum
)
586 lang_output_section_statement_type
*os
;
588 os
= lang_output_section_find (tree
->name
.name
);
591 if (expld
.phase
== lang_final_phase_enum
)
592 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
595 else if (os
->processed_lma
)
597 if (os
->load_base
== NULL
)
598 new_abs (os
->bfd_section
->lma
);
601 exp_fold_tree_1 (os
->load_base
);
609 if (expld
.phase
!= lang_first_phase_enum
)
611 int opb
= bfd_octets_per_byte (output_bfd
);
612 lang_output_section_statement_type
*os
;
614 os
= lang_output_section_find (tree
->name
.name
);
617 if (expld
.phase
== lang_final_phase_enum
)
618 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
622 else if (os
->processed_vma
)
623 new_abs (os
->bfd_section
->size
/ opb
);
629 lang_memory_region_type
*mem
;
631 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
633 new_abs (mem
->length
);
635 einfo (_("%F%S: undefined MEMORY region `%s'"
636 " referenced in expression\n"), tree
->name
.name
);
642 lang_memory_region_type
*mem
;
644 mem
= lang_memory_region_lookup (tree
->name
.name
, FALSE
);
646 new_abs (mem
->origin
);
648 einfo (_("%F%S: undefined MEMORY region `%s'"
649 " referenced in expression\n"), tree
->name
.name
);
654 if (strcmp (tree
->name
.name
, "MAXPAGESIZE") == 0)
655 new_abs (bfd_emul_get_maxpagesize (default_target
));
656 else if (strcmp (tree
->name
.name
, "COMMONPAGESIZE") == 0)
657 new_abs (bfd_emul_get_commonpagesize (default_target
));
659 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
670 exp_fold_tree_1 (etree_type
*tree
)
674 memset (&expld
.result
, 0, sizeof (expld
.result
));
678 switch (tree
->type
.node_class
)
681 new_rel (tree
->value
.value
, tree
->value
.str
, expld
.section
);
685 if (expld
.phase
!= lang_first_phase_enum
)
687 asection
*output_section
= tree
->rel
.section
->output_section
;
688 new_rel (tree
->rel
.value
+ tree
->rel
.section
->output_offset
,
689 NULL
, output_section
);
692 memset (&expld
.result
, 0, sizeof (expld
.result
));
696 exp_fold_tree_1 (tree
->assert_s
.child
);
697 if (expld
.phase
== lang_final_phase_enum
&& !expld
.result
.value
)
698 einfo ("%X%P: %s\n", tree
->assert_s
.message
);
716 if (tree
->assign
.dst
[0] == '.' && tree
->assign
.dst
[1] == 0)
718 /* Assignment to dot can only be done during allocation. */
719 if (tree
->type
.node_class
!= etree_assign
)
720 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
721 if (expld
.phase
== lang_mark_phase_enum
722 || expld
.phase
== lang_allocating_phase_enum
723 || (expld
.phase
== lang_final_phase_enum
724 && expld
.section
== bfd_abs_section_ptr
))
726 /* Notify the folder that this is an assignment to dot. */
727 expld
.assigning_to_dot
= TRUE
;
728 exp_fold_tree_1 (tree
->assign
.src
);
729 expld
.assigning_to_dot
= FALSE
;
731 if (!expld
.result
.valid_p
)
733 if (expld
.phase
!= lang_mark_phase_enum
)
734 einfo (_("%F%S invalid assignment to location counter\n"));
736 else if (expld
.dotp
== NULL
)
737 einfo (_("%F%S assignment to location counter"
738 " invalid outside of SECTION\n"));
743 nextdot
= expld
.result
.value
+ expld
.section
->vma
;
744 if (nextdot
< expld
.dot
745 && expld
.section
!= bfd_abs_section_ptr
)
746 einfo (_("%F%S cannot move location counter backwards"
747 " (from %V to %V)\n"), expld
.dot
, nextdot
);
751 *expld
.dotp
= nextdot
;
756 memset (&expld
.result
, 0, sizeof (expld
.result
));
760 struct bfd_link_hash_entry
*h
= NULL
;
762 if (tree
->type
.node_class
== etree_provide
)
764 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
767 || (h
->type
!= bfd_link_hash_new
768 && h
->type
!= bfd_link_hash_undefined
769 && h
->type
!= bfd_link_hash_common
))
771 /* Do nothing. The symbol was never referenced, or was
772 defined by some object. */
777 exp_fold_tree_1 (tree
->assign
.src
);
778 if (expld
.result
.valid_p
)
782 h
= bfd_link_hash_lookup (link_info
.hash
, tree
->assign
.dst
,
785 einfo (_("%P%F:%s: hash creation failed\n"),
789 /* FIXME: Should we worry if the symbol is already
791 lang_update_definedness (tree
->assign
.dst
, h
);
792 h
->type
= bfd_link_hash_defined
;
793 h
->u
.def
.value
= expld
.result
.value
;
794 h
->u
.def
.section
= expld
.result
.section
;
795 if (tree
->type
.node_class
== etree_provide
)
796 tree
->type
.node_class
= etree_provided
;
807 memset (&expld
.result
, 0, sizeof (expld
.result
));
813 exp_fold_tree (etree_type
*tree
, asection
*current_section
, bfd_vma
*dotp
)
817 expld
.section
= current_section
;
818 exp_fold_tree_1 (tree
);
822 exp_fold_tree_no_dot (etree_type
*tree
)
826 expld
.section
= bfd_abs_section_ptr
;
827 exp_fold_tree_1 (tree
);
831 exp_binop (int code
, etree_type
*lhs
, etree_type
*rhs
)
833 etree_type value
, *new;
835 value
.type
.node_code
= code
;
836 value
.type
.lineno
= lhs
->type
.lineno
;
837 value
.binary
.lhs
= lhs
;
838 value
.binary
.rhs
= rhs
;
839 value
.type
.node_class
= etree_binary
;
840 exp_fold_tree_no_dot (&value
);
841 if (expld
.result
.valid_p
)
842 return exp_intop (expld
.result
.value
);
844 new = stat_alloc (sizeof (new->binary
));
845 memcpy (new, &value
, sizeof (new->binary
));
850 exp_trinop (int code
, etree_type
*cond
, etree_type
*lhs
, etree_type
*rhs
)
852 etree_type value
, *new;
854 value
.type
.node_code
= code
;
855 value
.type
.lineno
= lhs
->type
.lineno
;
856 value
.trinary
.lhs
= lhs
;
857 value
.trinary
.cond
= cond
;
858 value
.trinary
.rhs
= rhs
;
859 value
.type
.node_class
= etree_trinary
;
860 exp_fold_tree_no_dot (&value
);
861 if (expld
.result
.valid_p
)
862 return exp_intop (expld
.result
.value
);
864 new = stat_alloc (sizeof (new->trinary
));
865 memcpy (new, &value
, sizeof (new->trinary
));
870 exp_unop (int code
, etree_type
*child
)
872 etree_type value
, *new;
874 value
.unary
.type
.node_code
= code
;
875 value
.unary
.type
.lineno
= child
->type
.lineno
;
876 value
.unary
.child
= child
;
877 value
.unary
.type
.node_class
= etree_unary
;
878 exp_fold_tree_no_dot (&value
);
879 if (expld
.result
.valid_p
)
880 return exp_intop (expld
.result
.value
);
882 new = stat_alloc (sizeof (new->unary
));
883 memcpy (new, &value
, sizeof (new->unary
));
888 exp_nameop (int code
, const char *name
)
890 etree_type value
, *new;
892 value
.name
.type
.node_code
= code
;
893 value
.name
.type
.lineno
= lineno
;
894 value
.name
.name
= name
;
895 value
.name
.type
.node_class
= etree_name
;
897 exp_fold_tree_no_dot (&value
);
898 if (expld
.result
.valid_p
)
899 return exp_intop (expld
.result
.value
);
901 new = stat_alloc (sizeof (new->name
));
902 memcpy (new, &value
, sizeof (new->name
));
908 exp_assop (int code
, const char *dst
, etree_type
*src
)
912 new = stat_alloc (sizeof (new->assign
));
913 new->type
.node_code
= code
;
914 new->type
.lineno
= src
->type
.lineno
;
915 new->type
.node_class
= etree_assign
;
916 new->assign
.src
= src
;
917 new->assign
.dst
= dst
;
921 /* Handle PROVIDE. */
924 exp_provide (const char *dst
, etree_type
*src
, bfd_boolean hidden
)
928 n
= stat_alloc (sizeof (n
->assign
));
929 n
->assign
.type
.node_code
= '=';
930 n
->assign
.type
.lineno
= src
->type
.lineno
;
931 n
->assign
.type
.node_class
= etree_provide
;
934 n
->assign
.hidden
= hidden
;
941 exp_assert (etree_type
*exp
, const char *message
)
945 n
= stat_alloc (sizeof (n
->assert_s
));
946 n
->assert_s
.type
.node_code
= '!';
947 n
->assert_s
.type
.lineno
= exp
->type
.lineno
;
948 n
->assert_s
.type
.node_class
= etree_assert
;
949 n
->assert_s
.child
= exp
;
950 n
->assert_s
.message
= message
;
955 exp_print_tree (etree_type
*tree
)
957 if (config
.map_file
== NULL
)
958 config
.map_file
= stderr
;
962 minfo ("NULL TREE\n");
966 switch (tree
->type
.node_class
)
969 minfo ("0x%v", tree
->value
.value
);
972 if (tree
->rel
.section
->owner
!= NULL
)
973 minfo ("%B:", tree
->rel
.section
->owner
);
974 minfo ("%s+0x%v", tree
->rel
.section
->name
, tree
->rel
.value
);
977 fprintf (config
.map_file
, "%s", tree
->assign
.dst
);
978 exp_print_token (tree
->type
.node_code
, TRUE
);
979 exp_print_tree (tree
->assign
.src
);
983 fprintf (config
.map_file
, "PROVIDE (%s, ", tree
->assign
.dst
);
984 exp_print_tree (tree
->assign
.src
);
985 fprintf (config
.map_file
, ")");
988 fprintf (config
.map_file
, "(");
989 exp_print_tree (tree
->binary
.lhs
);
990 exp_print_token (tree
->type
.node_code
, TRUE
);
991 exp_print_tree (tree
->binary
.rhs
);
992 fprintf (config
.map_file
, ")");
995 exp_print_tree (tree
->trinary
.cond
);
996 fprintf (config
.map_file
, "?");
997 exp_print_tree (tree
->trinary
.lhs
);
998 fprintf (config
.map_file
, ":");
999 exp_print_tree (tree
->trinary
.rhs
);
1002 exp_print_token (tree
->unary
.type
.node_code
, FALSE
);
1003 if (tree
->unary
.child
)
1005 fprintf (config
.map_file
, " (");
1006 exp_print_tree (tree
->unary
.child
);
1007 fprintf (config
.map_file
, ")");
1012 fprintf (config
.map_file
, "ASSERT (");
1013 exp_print_tree (tree
->assert_s
.child
);
1014 fprintf (config
.map_file
, ", %s)", tree
->assert_s
.message
);
1018 if (tree
->type
.node_code
== NAME
)
1020 fprintf (config
.map_file
, "%s", tree
->name
.name
);
1024 exp_print_token (tree
->type
.node_code
, FALSE
);
1025 if (tree
->name
.name
)
1026 fprintf (config
.map_file
, " (%s)", tree
->name
.name
);
1036 exp_get_vma (etree_type
*tree
, bfd_vma def
, char *name
)
1040 exp_fold_tree_no_dot (tree
);
1041 if (expld
.result
.valid_p
)
1042 return expld
.result
.value
;
1043 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1044 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1050 exp_get_value_int (etree_type
*tree
, int def
, char *name
)
1052 return exp_get_vma (tree
, def
, name
);
1056 exp_get_fill (etree_type
*tree
, fill_type
*def
, char *name
)
1065 exp_fold_tree_no_dot (tree
);
1066 if (!expld
.result
.valid_p
)
1068 if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1069 einfo (_("%F%S nonconstant expression for %s\n"), name
);
1073 if (expld
.result
.str
!= NULL
&& (len
= strlen (expld
.result
.str
)) != 0)
1077 fill
= xmalloc ((len
+ 1) / 2 + sizeof (*fill
) - 1);
1078 fill
->size
= (len
+ 1) / 2;
1080 s
= (unsigned char *) expld
.result
.str
;
1088 digit
= (digit
- 'A' + '0' + 10) & 0xf;
1102 fill
= xmalloc (4 + sizeof (*fill
) - 1);
1103 val
= expld
.result
.value
;
1104 fill
->data
[0] = (val
>> 24) & 0xff;
1105 fill
->data
[1] = (val
>> 16) & 0xff;
1106 fill
->data
[2] = (val
>> 8) & 0xff;
1107 fill
->data
[3] = (val
>> 0) & 0xff;
1114 exp_get_abs_int (etree_type
*tree
, int def
, char *name
)
1118 exp_fold_tree_no_dot (tree
);
1120 if (expld
.result
.valid_p
)
1122 expld
.result
.value
+= expld
.result
.section
->vma
;
1123 return expld
.result
.value
;
1125 else if (name
!= NULL
&& expld
.phase
!= lang_mark_phase_enum
)
1127 lineno
= tree
->type
.lineno
;
1128 einfo (_("%F%S: nonconstant expression for %s\n"), name
);
1135 align_n (bfd_vma value
, bfd_vma align
)
1140 value
= (value
+ align
- 1) / align
;
1141 return value
* align
;