daily update
[binutils.git] / ld / ldexp.c
blob2b973c3402ce14a27409c188e251265de19147d2
1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004
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)
12 any later version.
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, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
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
29 valid bit. */
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "bfdlink.h"
35 #include "ld.h"
36 #include "ldmain.h"
37 #include "ldmisc.h"
38 #include "ldexp.h"
39 #include <ldgram.h>
40 #include "ldlang.h"
41 #include "libiberty.h"
42 #include "safe-ctype.h"
44 static etree_value_type exp_fold_tree_no_dot
45 (etree_type *, lang_output_section_statement_type *, lang_phase_type);
46 static bfd_vma align_n
47 (bfd_vma, bfd_vma);
49 struct exp_data_seg exp_data_seg;
51 /* Print the string representation of the given token. Surround it
52 with spaces if INFIX_P is TRUE. */
54 static void
55 exp_print_token (token_code_type code, int infix_p)
57 static const struct
59 token_code_type code;
60 char * name;
62 table[] =
64 { INT, "int" },
65 { NAME, "NAME" },
66 { PLUSEQ, "+=" },
67 { MINUSEQ, "-=" },
68 { MULTEQ, "*=" },
69 { DIVEQ, "/=" },
70 { LSHIFTEQ, "<<=" },
71 { RSHIFTEQ, ">>=" },
72 { ANDEQ, "&=" },
73 { OREQ, "|=" },
74 { OROR, "||" },
75 { ANDAND, "&&" },
76 { EQ, "==" },
77 { NE, "!=" },
78 { LE, "<=" },
79 { GE, ">=" },
80 { LSHIFT, "<<" },
81 { RSHIFT, ">>" },
82 { ALIGN_K, "ALIGN" },
83 { BLOCK, "BLOCK" },
84 { QUAD, "QUAD" },
85 { SQUAD, "SQUAD" },
86 { LONG, "LONG" },
87 { SHORT, "SHORT" },
88 { BYTE, "BYTE" },
89 { SECTIONS, "SECTIONS" },
90 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
91 { MEMORY, "MEMORY" },
92 { DEFINED, "DEFINED" },
93 { TARGET_K, "TARGET" },
94 { SEARCH_DIR, "SEARCH_DIR" },
95 { MAP, "MAP" },
96 { ENTRY, "ENTRY" },
97 { NEXT, "NEXT" },
98 { SIZEOF, "SIZEOF" },
99 { ADDR, "ADDR" },
100 { LOADADDR, "LOADADDR" },
101 { MAX_K, "MAX_K" },
102 { REL, "relocatable" },
103 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
104 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
106 unsigned int idx;
108 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
109 if (table[idx].code == code)
110 break;
112 if (infix_p)
113 fputc (' ', config.map_file);
115 if (idx < ARRAY_SIZE (table))
116 fputs (table[idx].name, config.map_file);
117 else if (code < 127)
118 fputc (code, config.map_file);
119 else
120 fprintf (config.map_file, "<code %d>", code);
122 if (infix_p)
123 fputc (' ', config.map_file);
126 static void
127 make_abs (etree_value_type *ptr)
129 asection *s = ptr->section->bfd_section;
130 ptr->value += s->vma;
131 ptr->section = abs_output_section;
134 static etree_value_type
135 new_abs (bfd_vma value)
137 etree_value_type new;
138 new.valid_p = TRUE;
139 new.section = abs_output_section;
140 new.value = value;
141 return new;
144 static void
145 check (lang_output_section_statement_type *os,
146 const char *name,
147 const char *op)
149 if (os == NULL)
150 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
151 if (! os->processed)
152 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
155 etree_type *
156 exp_intop (bfd_vma value)
158 etree_type *new = stat_alloc (sizeof (new->value));
159 new->type.node_code = INT;
160 new->value.value = value;
161 new->value.str = NULL;
162 new->type.node_class = etree_value;
163 return new;
166 etree_type *
167 exp_bigintop (bfd_vma value, char *str)
169 etree_type *new = stat_alloc (sizeof (new->value));
170 new->type.node_code = INT;
171 new->value.value = value;
172 new->value.str = str;
173 new->type.node_class = etree_value;
174 return new;
177 /* Build an expression representing an unnamed relocatable value. */
179 etree_type *
180 exp_relop (asection *section, bfd_vma value)
182 etree_type *new = stat_alloc (sizeof (new->rel));
183 new->type.node_code = REL;
184 new->type.node_class = etree_rel;
185 new->rel.section = section;
186 new->rel.value = value;
187 return new;
190 static etree_value_type
191 new_rel (bfd_vma value,
192 char *str,
193 lang_output_section_statement_type *section)
195 etree_value_type new;
196 new.valid_p = TRUE;
197 new.value = value;
198 new.str = str;
199 new.section = section;
200 return new;
203 static etree_value_type
204 new_rel_from_section (bfd_vma value,
205 lang_output_section_statement_type *section)
207 etree_value_type new;
208 new.valid_p = TRUE;
209 new.value = value;
210 new.str = NULL;
211 new.section = section;
213 new.value -= section->bfd_section->vma;
215 return new;
218 static etree_value_type
219 fold_unary (etree_type *tree,
220 lang_output_section_statement_type *current_section,
221 lang_phase_type allocation_done,
222 bfd_vma dot,
223 bfd_vma *dotp)
225 etree_value_type result;
227 result = exp_fold_tree (tree->unary.child,
228 current_section,
229 allocation_done, dot, dotp);
230 if (result.valid_p)
232 switch (tree->type.node_code)
234 case ALIGN_K:
235 if (allocation_done != lang_first_phase_enum)
236 result = new_rel_from_section (align_n (dot, result.value),
237 current_section);
238 else
239 result.valid_p = FALSE;
240 break;
242 case ABSOLUTE:
243 if (allocation_done != lang_first_phase_enum)
245 result.value += result.section->bfd_section->vma;
246 result.section = abs_output_section;
248 else
249 result.valid_p = FALSE;
250 break;
252 case '~':
253 make_abs (&result);
254 result.value = ~result.value;
255 break;
257 case '!':
258 make_abs (&result);
259 result.value = !result.value;
260 break;
262 case '-':
263 make_abs (&result);
264 result.value = -result.value;
265 break;
267 case NEXT:
268 /* Return next place aligned to value. */
269 if (allocation_done == lang_allocating_phase_enum)
271 make_abs (&result);
272 result.value = align_n (dot, result.value);
274 else
275 result.valid_p = FALSE;
276 break;
278 case DATA_SEGMENT_END:
279 if (allocation_done != lang_first_phase_enum
280 && current_section == abs_output_section
281 && (exp_data_seg.phase == exp_dataseg_align_seen
282 || exp_data_seg.phase == exp_dataseg_adjust
283 || allocation_done != lang_allocating_phase_enum))
285 if (exp_data_seg.phase == exp_dataseg_align_seen)
287 exp_data_seg.phase = exp_dataseg_end_seen;
288 exp_data_seg.end = result.value;
291 else
292 result.valid_p = FALSE;
293 break;
295 default:
296 FAIL ();
297 break;
301 return result;
304 static etree_value_type
305 fold_binary (etree_type *tree,
306 lang_output_section_statement_type *current_section,
307 lang_phase_type allocation_done,
308 bfd_vma dot,
309 bfd_vma *dotp)
311 etree_value_type result;
313 result = exp_fold_tree (tree->binary.lhs, current_section,
314 allocation_done, dot, dotp);
315 if (result.valid_p)
317 etree_value_type other;
319 other = exp_fold_tree (tree->binary.rhs,
320 current_section,
321 allocation_done, dot, dotp);
322 if (other.valid_p)
324 /* If the values are from different sections, or this is an
325 absolute expression, make both the source arguments
326 absolute. However, adding or subtracting an absolute
327 value from a relative value is meaningful, and is an
328 exception. */
329 if (current_section != abs_output_section
330 && (other.section == abs_output_section
331 || (result.section == abs_output_section
332 && tree->type.node_code == '+'))
333 && (tree->type.node_code == '+'
334 || tree->type.node_code == '-'))
336 if (other.section != abs_output_section)
338 /* Keep the section of the other term. */
339 if (tree->type.node_code == '+')
340 other.value = result.value + other.value;
341 else
342 other.value = result.value - other.value;
343 return other;
346 else if (result.section != other.section
347 || current_section == abs_output_section)
349 make_abs (&result);
350 make_abs (&other);
353 switch (tree->type.node_code)
355 case '%':
356 if (other.value == 0)
357 einfo (_("%F%S %% by zero\n"));
358 result.value = ((bfd_signed_vma) result.value
359 % (bfd_signed_vma) other.value);
360 break;
362 case '/':
363 if (other.value == 0)
364 einfo (_("%F%S / by zero\n"));
365 result.value = ((bfd_signed_vma) result.value
366 / (bfd_signed_vma) other.value);
367 break;
369 #define BOP(x,y) case x : result.value = result.value y other.value; break;
370 BOP ('+', +);
371 BOP ('*', *);
372 BOP ('-', -);
373 BOP (LSHIFT, <<);
374 BOP (RSHIFT, >>);
375 BOP (EQ, ==);
376 BOP (NE, !=);
377 BOP ('<', <);
378 BOP ('>', >);
379 BOP (LE, <=);
380 BOP (GE, >=);
381 BOP ('&', &);
382 BOP ('^', ^);
383 BOP ('|', |);
384 BOP (ANDAND, &&);
385 BOP (OROR, ||);
387 case MAX_K:
388 if (result.value < other.value)
389 result = other;
390 break;
392 case MIN_K:
393 if (result.value > other.value)
394 result = other;
395 break;
397 case DATA_SEGMENT_ALIGN:
398 if (allocation_done != lang_first_phase_enum
399 && current_section == abs_output_section
400 && (exp_data_seg.phase == exp_dataseg_none
401 || exp_data_seg.phase == exp_dataseg_adjust
402 || allocation_done != lang_allocating_phase_enum))
404 bfd_vma maxpage = result.value;
406 result.value = align_n (dot, maxpage);
407 if (exp_data_seg.phase != exp_dataseg_adjust)
409 result.value += dot & (maxpage - 1);
410 if (allocation_done == lang_allocating_phase_enum)
412 exp_data_seg.phase = exp_dataseg_align_seen;
413 exp_data_seg.base = result.value;
414 exp_data_seg.pagesize = other.value;
417 else if (other.value < maxpage)
418 result.value += (dot + other.value - 1)
419 & (maxpage - other.value);
421 else
422 result.valid_p = FALSE;
423 break;
425 default:
426 FAIL ();
429 else
431 result.valid_p = FALSE;
435 return result;
438 static etree_value_type
439 fold_trinary (etree_type *tree,
440 lang_output_section_statement_type *current_section,
441 lang_phase_type allocation_done,
442 bfd_vma dot,
443 bfd_vma *dotp)
445 etree_value_type result;
447 result = exp_fold_tree (tree->trinary.cond, current_section,
448 allocation_done, dot, dotp);
449 if (result.valid_p)
450 result = exp_fold_tree ((result.value
451 ? tree->trinary.lhs
452 : tree->trinary.rhs),
453 current_section,
454 allocation_done, dot, dotp);
456 return result;
459 etree_value_type
460 invalid (void)
462 etree_value_type new;
463 new.valid_p = FALSE;
464 return new;
467 static etree_value_type
468 fold_name (etree_type *tree,
469 lang_output_section_statement_type *current_section,
470 lang_phase_type allocation_done,
471 bfd_vma dot)
473 etree_value_type result;
475 switch (tree->type.node_code)
477 case SIZEOF_HEADERS:
478 if (allocation_done != lang_first_phase_enum)
480 result = new_abs (bfd_sizeof_headers (output_bfd,
481 link_info.relocatable));
483 else
485 result.valid_p = FALSE;
487 break;
488 case DEFINED:
489 if (allocation_done == lang_first_phase_enum)
491 lang_track_definedness (tree->name.name);
492 result.valid_p = FALSE;
494 else
496 struct bfd_link_hash_entry *h;
497 int def_iteration
498 = lang_symbol_definition_iteration (tree->name.name);
500 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
501 tree->name.name,
502 FALSE, FALSE, TRUE);
503 result.value = (h != NULL
504 && (h->type == bfd_link_hash_defined
505 || h->type == bfd_link_hash_defweak
506 || h->type == bfd_link_hash_common)
507 && (def_iteration == lang_statement_iteration
508 || def_iteration == -1));
509 result.section = abs_output_section;
510 result.valid_p = TRUE;
512 break;
513 case NAME:
514 result.valid_p = FALSE;
515 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
517 if (allocation_done != lang_first_phase_enum)
518 result = new_rel_from_section (dot, current_section);
519 else
520 result = invalid ();
522 else if (allocation_done != lang_first_phase_enum)
524 struct bfd_link_hash_entry *h;
526 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
527 tree->name.name,
528 FALSE, FALSE, TRUE);
529 if (h != NULL
530 && (h->type == bfd_link_hash_defined
531 || h->type == bfd_link_hash_defweak))
533 if (bfd_is_abs_section (h->u.def.section))
534 result = new_abs (h->u.def.value);
535 else if (allocation_done == lang_final_phase_enum
536 || allocation_done == lang_allocating_phase_enum)
538 asection *output_section;
540 output_section = h->u.def.section->output_section;
541 if (output_section == NULL)
542 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
543 tree->name.name);
544 else
546 lang_output_section_statement_type *os;
548 os = (lang_output_section_statement_lookup
549 (bfd_get_section_name (output_bfd,
550 output_section)));
552 /* FIXME: Is this correct if this section is
553 being linked with -R? */
554 result = new_rel ((h->u.def.value
555 + h->u.def.section->output_offset),
556 NULL,
557 os);
561 else if (allocation_done == lang_final_phase_enum)
562 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
563 tree->name.name);
565 break;
567 case ADDR:
568 if (allocation_done != lang_first_phase_enum)
570 lang_output_section_statement_type *os;
572 os = lang_output_section_find (tree->name.name);
573 check (os, tree->name.name, "ADDR");
574 result = new_rel (0, NULL, os);
576 else
577 result = invalid ();
578 break;
580 case LOADADDR:
581 if (allocation_done != lang_first_phase_enum)
583 lang_output_section_statement_type *os;
585 os = lang_output_section_find (tree->name.name);
586 check (os, tree->name.name, "LOADADDR");
587 if (os->load_base == NULL)
588 result = new_rel (0, NULL, os);
589 else
590 result = exp_fold_tree_no_dot (os->load_base,
591 abs_output_section,
592 allocation_done);
594 else
595 result = invalid ();
596 break;
598 case SIZEOF:
599 if (allocation_done != lang_first_phase_enum)
601 int opb = bfd_octets_per_byte (output_bfd);
602 lang_output_section_statement_type *os;
604 os = lang_output_section_find (tree->name.name);
605 check (os, tree->name.name, "SIZEOF");
606 result = new_abs (os->bfd_section->_raw_size / opb);
608 else
609 result = invalid ();
610 break;
612 default:
613 FAIL ();
614 break;
617 return result;
620 etree_value_type
621 exp_fold_tree (etree_type *tree,
622 lang_output_section_statement_type *current_section,
623 lang_phase_type allocation_done,
624 bfd_vma dot,
625 bfd_vma *dotp)
627 etree_value_type result;
629 if (tree == NULL)
631 result.valid_p = FALSE;
632 return result;
635 switch (tree->type.node_class)
637 case etree_value:
638 result = new_rel (tree->value.value, tree->value.str, current_section);
639 break;
641 case etree_rel:
642 if (allocation_done != lang_final_phase_enum)
643 result.valid_p = FALSE;
644 else
645 result = new_rel ((tree->rel.value
646 + tree->rel.section->output_section->vma
647 + tree->rel.section->output_offset),
648 NULL,
649 current_section);
650 break;
652 case etree_assert:
653 result = exp_fold_tree (tree->assert_s.child,
654 current_section,
655 allocation_done, dot, dotp);
656 if (result.valid_p)
658 if (! result.value)
659 einfo ("%F%P: %s\n", tree->assert_s.message);
660 return result;
662 break;
664 case etree_unary:
665 result = fold_unary (tree, current_section, allocation_done,
666 dot, dotp);
667 break;
669 case etree_binary:
670 result = fold_binary (tree, current_section, allocation_done,
671 dot, dotp);
672 break;
674 case etree_trinary:
675 result = fold_trinary (tree, current_section, allocation_done,
676 dot, dotp);
677 break;
679 case etree_assign:
680 case etree_provide:
681 case etree_provided:
682 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
684 /* Assignment to dot can only be done during allocation. */
685 if (tree->type.node_class != etree_assign)
686 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
687 if (allocation_done == lang_allocating_phase_enum
688 || (allocation_done == lang_final_phase_enum
689 && current_section == abs_output_section))
691 result = exp_fold_tree (tree->assign.src,
692 current_section,
693 allocation_done, dot,
694 dotp);
695 if (! result.valid_p)
696 einfo (_("%F%S invalid assignment to location counter\n"));
697 else
699 if (current_section == NULL)
700 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
701 else
703 bfd_vma nextdot;
705 nextdot = (result.value
706 + current_section->bfd_section->vma);
707 if (nextdot < dot
708 && current_section != abs_output_section)
709 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
710 dot, nextdot);
711 else
712 *dotp = nextdot;
717 else
719 result = exp_fold_tree (tree->assign.src,
720 current_section, allocation_done,
721 dot, dotp);
722 if (result.valid_p)
724 bfd_boolean create;
725 struct bfd_link_hash_entry *h;
727 if (tree->type.node_class == etree_assign)
728 create = TRUE;
729 else
730 create = FALSE;
731 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
732 create, FALSE, FALSE);
733 if (h == NULL)
735 if (tree->type.node_class == etree_assign)
736 einfo (_("%P%F:%s: hash creation failed\n"),
737 tree->assign.dst);
739 else if (tree->type.node_class == etree_provide
740 && h->type != bfd_link_hash_undefined
741 && h->type != bfd_link_hash_common)
743 /* Do nothing. The symbol was defined by some
744 object. */
746 else
748 /* FIXME: Should we worry if the symbol is already
749 defined? */
750 lang_update_definedness (tree->assign.dst, h);
751 h->type = bfd_link_hash_defined;
752 h->u.def.value = result.value;
753 h->u.def.section = result.section->bfd_section;
754 if (tree->type.node_class == etree_provide)
755 tree->type.node_class = etree_provided;
759 break;
761 case etree_name:
762 result = fold_name (tree, current_section, allocation_done, dot);
763 break;
765 default:
766 FAIL ();
767 break;
770 return result;
773 static etree_value_type
774 exp_fold_tree_no_dot (etree_type *tree,
775 lang_output_section_statement_type *current_section,
776 lang_phase_type allocation_done)
778 return exp_fold_tree (tree, current_section, allocation_done, 0, NULL);
781 etree_type *
782 exp_binop (int code, etree_type *lhs, etree_type *rhs)
784 etree_type value, *new;
785 etree_value_type r;
787 value.type.node_code = code;
788 value.binary.lhs = lhs;
789 value.binary.rhs = rhs;
790 value.type.node_class = etree_binary;
791 r = exp_fold_tree_no_dot (&value,
792 abs_output_section,
793 lang_first_phase_enum);
794 if (r.valid_p)
796 return exp_intop (r.value);
798 new = stat_alloc (sizeof (new->binary));
799 memcpy (new, &value, sizeof (new->binary));
800 return new;
803 etree_type *
804 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
806 etree_type value, *new;
807 etree_value_type r;
808 value.type.node_code = code;
809 value.trinary.lhs = lhs;
810 value.trinary.cond = cond;
811 value.trinary.rhs = rhs;
812 value.type.node_class = etree_trinary;
813 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
814 if (r.valid_p)
815 return exp_intop (r.value);
817 new = stat_alloc (sizeof (new->trinary));
818 memcpy (new, &value, sizeof (new->trinary));
819 return new;
822 etree_type *
823 exp_unop (int code, etree_type *child)
825 etree_type value, *new;
827 etree_value_type r;
828 value.unary.type.node_code = code;
829 value.unary.child = child;
830 value.unary.type.node_class = etree_unary;
831 r = exp_fold_tree_no_dot (&value, abs_output_section,
832 lang_first_phase_enum);
833 if (r.valid_p)
834 return exp_intop (r.value);
836 new = stat_alloc (sizeof (new->unary));
837 memcpy (new, &value, sizeof (new->unary));
838 return new;
841 etree_type *
842 exp_nameop (int code, const char *name)
844 etree_type value, *new;
845 etree_value_type r;
846 value.name.type.node_code = code;
847 value.name.name = name;
848 value.name.type.node_class = etree_name;
850 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum);
851 if (r.valid_p)
852 return exp_intop (r.value);
854 new = stat_alloc (sizeof (new->name));
855 memcpy (new, &value, sizeof (new->name));
856 return new;
860 etree_type *
861 exp_assop (int code, const char *dst, etree_type *src)
863 etree_type value, *new;
865 value.assign.type.node_code = code;
867 value.assign.src = src;
868 value.assign.dst = dst;
869 value.assign.type.node_class = etree_assign;
871 #if 0
872 if (exp_fold_tree_no_dot (&value, &result))
873 return exp_intop (result);
874 #endif
875 new = stat_alloc (sizeof (new->assign));
876 memcpy (new, &value, sizeof (new->assign));
877 return new;
880 /* Handle PROVIDE. */
882 etree_type *
883 exp_provide (const char *dst, etree_type *src)
885 etree_type *n;
887 n = stat_alloc (sizeof (n->assign));
888 n->assign.type.node_code = '=';
889 n->assign.type.node_class = etree_provide;
890 n->assign.src = src;
891 n->assign.dst = dst;
892 return n;
895 /* Handle ASSERT. */
897 etree_type *
898 exp_assert (etree_type *exp, const char *message)
900 etree_type *n;
902 n = stat_alloc (sizeof (n->assert_s));
903 n->assert_s.type.node_code = '!';
904 n->assert_s.type.node_class = etree_assert;
905 n->assert_s.child = exp;
906 n->assert_s.message = message;
907 return n;
910 void
911 exp_print_tree (etree_type *tree)
913 if (config.map_file == NULL)
914 config.map_file = stderr;
916 if (tree == NULL)
918 minfo ("NULL TREE\n");
919 return;
922 switch (tree->type.node_class)
924 case etree_value:
925 minfo ("0x%v", tree->value.value);
926 return;
927 case etree_rel:
928 if (tree->rel.section->owner != NULL)
929 minfo ("%B:", tree->rel.section->owner);
930 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
931 return;
932 case etree_assign:
933 #if 0
934 if (tree->assign.dst->sdefs != NULL)
935 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
936 tree->assign.dst->sdefs->value);
937 else
938 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
939 #endif
940 fprintf (config.map_file, "%s", tree->assign.dst);
941 exp_print_token (tree->type.node_code, TRUE);
942 exp_print_tree (tree->assign.src);
943 break;
944 case etree_provide:
945 case etree_provided:
946 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
947 exp_print_tree (tree->assign.src);
948 fprintf (config.map_file, ")");
949 break;
950 case etree_binary:
951 fprintf (config.map_file, "(");
952 exp_print_tree (tree->binary.lhs);
953 exp_print_token (tree->type.node_code, TRUE);
954 exp_print_tree (tree->binary.rhs);
955 fprintf (config.map_file, ")");
956 break;
957 case etree_trinary:
958 exp_print_tree (tree->trinary.cond);
959 fprintf (config.map_file, "?");
960 exp_print_tree (tree->trinary.lhs);
961 fprintf (config.map_file, ":");
962 exp_print_tree (tree->trinary.rhs);
963 break;
964 case etree_unary:
965 exp_print_token (tree->unary.type.node_code, FALSE);
966 if (tree->unary.child)
968 fprintf (config.map_file, " (");
969 exp_print_tree (tree->unary.child);
970 fprintf (config.map_file, ")");
972 break;
974 case etree_assert:
975 fprintf (config.map_file, "ASSERT (");
976 exp_print_tree (tree->assert_s.child);
977 fprintf (config.map_file, ", %s)", tree->assert_s.message);
978 break;
980 case etree_undef:
981 fprintf (config.map_file, "????????");
982 break;
983 case etree_name:
984 if (tree->type.node_code == NAME)
986 fprintf (config.map_file, "%s", tree->name.name);
988 else
990 exp_print_token (tree->type.node_code, FALSE);
991 if (tree->name.name)
992 fprintf (config.map_file, " (%s)", tree->name.name);
994 break;
995 default:
996 FAIL ();
997 break;
1001 bfd_vma
1002 exp_get_vma (etree_type *tree,
1003 bfd_vma def,
1004 char *name,
1005 lang_phase_type allocation_done)
1007 etree_value_type r;
1009 if (tree != NULL)
1011 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1012 if (! r.valid_p && name != NULL)
1013 einfo (_("%F%S nonconstant expression for %s\n"), name);
1014 return r.value;
1016 else
1017 return def;
1021 exp_get_value_int (etree_type *tree,
1022 int def,
1023 char *name,
1024 lang_phase_type allocation_done)
1026 return exp_get_vma (tree, def, name, allocation_done);
1029 fill_type *
1030 exp_get_fill (etree_type *tree,
1031 fill_type *def,
1032 char *name,
1033 lang_phase_type allocation_done)
1035 fill_type *fill;
1036 etree_value_type r;
1037 size_t len;
1038 unsigned int val;
1040 if (tree == NULL)
1041 return def;
1043 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1044 if (! r.valid_p && name != NULL)
1045 einfo (_("%F%S nonconstant expression for %s\n"), name);
1047 if (r.str != NULL && (len = strlen (r.str)) != 0)
1049 unsigned char *dst;
1050 unsigned char *s;
1051 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1052 fill->size = (len + 1) / 2;
1053 dst = fill->data;
1054 s = r.str;
1055 val = 0;
1058 unsigned int digit;
1060 digit = *s++ - '0';
1061 if (digit > 9)
1062 digit = (digit - 'A' + '0' + 10) & 0xf;
1063 val <<= 4;
1064 val += digit;
1065 --len;
1066 if ((len & 1) == 0)
1068 *dst++ = val;
1069 val = 0;
1072 while (len != 0);
1074 else
1076 fill = xmalloc (4 + sizeof (*fill) - 1);
1077 val = r.value;
1078 fill->data[0] = (val >> 24) & 0xff;
1079 fill->data[1] = (val >> 16) & 0xff;
1080 fill->data[2] = (val >> 8) & 0xff;
1081 fill->data[3] = (val >> 0) & 0xff;
1082 fill->size = 4;
1084 return fill;
1087 bfd_vma
1088 exp_get_abs_int (etree_type *tree,
1089 int def ATTRIBUTE_UNUSED,
1090 char *name,
1091 lang_phase_type allocation_done)
1093 etree_value_type res;
1094 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1096 if (res.valid_p)
1097 res.value += res.section->bfd_section->vma;
1098 else
1099 einfo (_("%F%S non constant expression for %s\n"), name);
1101 return res.value;
1104 static bfd_vma
1105 align_n (bfd_vma value, bfd_vma align)
1107 if (align <= 1)
1108 return value;
1110 value = (value + align - 1) / align;
1111 return value * align;