* Makefile.am: Run dep-am.
[binutils.git] / ld / ldexp.c
blobe45c47068c988484d957bea5f7337625a65d7cd3
1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002
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 void exp_print_token PARAMS ((token_code_type code, int infix_p));
45 static void make_abs PARAMS ((etree_value_type *ptr));
46 static etree_value_type new_abs PARAMS ((bfd_vma value));
47 static void check PARAMS ((lang_output_section_statement_type *os,
48 const char *name, const char *op));
49 static etree_value_type new_rel
50 PARAMS ((bfd_vma, char *, lang_output_section_statement_type *section));
51 static etree_value_type new_rel_from_section
52 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
53 static etree_value_type fold_unary
54 PARAMS ((etree_type *tree,
55 lang_output_section_statement_type *current_section,
56 lang_phase_type allocation_done,
57 bfd_vma dot, bfd_vma *dotp));
58 static etree_value_type fold_binary
59 PARAMS ((etree_type *tree,
60 lang_output_section_statement_type *current_section,
61 lang_phase_type allocation_done,
62 bfd_vma dot, bfd_vma *dotp));
63 static etree_value_type fold_trinary
64 PARAMS ((etree_type *tree,
65 lang_output_section_statement_type *current_section,
66 lang_phase_type allocation_done,
67 bfd_vma dot, bfd_vma *dotp));
68 static etree_value_type fold_name
69 PARAMS ((etree_type *tree,
70 lang_output_section_statement_type *current_section,
71 lang_phase_type allocation_done,
72 bfd_vma dot));
73 static etree_value_type exp_fold_tree_no_dot
74 PARAMS ((etree_type *tree,
75 lang_output_section_statement_type *current_section,
76 lang_phase_type allocation_done));
78 struct exp_data_seg exp_data_seg;
80 /* Print the string representation of the given token. Surround it
81 with spaces if INFIX_P is true. */
83 static void
84 exp_print_token (code, infix_p)
85 token_code_type code;
86 int infix_p;
88 static const struct
90 token_code_type code;
91 char * name;
93 table[] =
95 { INT, "int" },
96 { NAME, "NAME" },
97 { PLUSEQ, "+=" },
98 { MINUSEQ, "-=" },
99 { MULTEQ, "*=" },
100 { DIVEQ, "/=" },
101 { LSHIFTEQ, "<<=" },
102 { RSHIFTEQ, ">>=" },
103 { ANDEQ, "&=" },
104 { OREQ, "|=" },
105 { OROR, "||" },
106 { ANDAND, "&&" },
107 { EQ, "==" },
108 { NE, "!=" },
109 { LE, "<=" },
110 { GE, ">=" },
111 { LSHIFT, "<<" },
112 { RSHIFT, ">>" },
113 { ALIGN_K, "ALIGN" },
114 { BLOCK, "BLOCK" },
115 { QUAD, "QUAD" },
116 { SQUAD, "SQUAD" },
117 { LONG, "LONG" },
118 { SHORT, "SHORT" },
119 { BYTE, "BYTE" },
120 { SECTIONS, "SECTIONS" },
121 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
122 { MEMORY, "MEMORY" },
123 { DEFINED, "DEFINED" },
124 { TARGET_K, "TARGET" },
125 { SEARCH_DIR, "SEARCH_DIR" },
126 { MAP, "MAP" },
127 { ENTRY, "ENTRY" },
128 { NEXT, "NEXT" },
129 { SIZEOF, "SIZEOF" },
130 { ADDR, "ADDR" },
131 { LOADADDR, "LOADADDR" },
132 { MAX_K, "MAX_K" },
133 { REL, "relocateable" },
134 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
135 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
137 unsigned int idx;
139 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
140 if (table[idx].code == code)
141 break;
143 if (infix_p)
144 fputc (' ', config.map_file);
146 if (idx < ARRAY_SIZE (table))
147 fputs (table[idx].name, config.map_file);
148 else if (code < 127)
149 fputc (code, config.map_file);
150 else
151 fprintf (config.map_file, "<code %d>", code);
153 if (infix_p)
154 fputc (' ', config.map_file);
157 static void
158 make_abs (ptr)
159 etree_value_type *ptr;
161 asection *s = ptr->section->bfd_section;
162 ptr->value += s->vma;
163 ptr->section = abs_output_section;
166 static etree_value_type
167 new_abs (value)
168 bfd_vma value;
170 etree_value_type new;
171 new.valid_p = true;
172 new.section = abs_output_section;
173 new.value = value;
174 return new;
177 static void
178 check (os, name, op)
179 lang_output_section_statement_type *os;
180 const char *name;
181 const char *op;
183 if (os == NULL)
184 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
185 if (! os->processed)
186 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
189 etree_type *
190 exp_intop (value)
191 bfd_vma value;
193 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
194 new->type.node_code = INT;
195 new->value.value = value;
196 new->value.str = NULL;
197 new->type.node_class = etree_value;
198 return new;
201 etree_type *
202 exp_bigintop (value, str)
203 bfd_vma value;
204 char *str;
206 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
207 new->type.node_code = INT;
208 new->value.value = value;
209 new->value.str = str;
210 new->type.node_class = etree_value;
211 return new;
214 /* Build an expression representing an unnamed relocateable value. */
216 etree_type *
217 exp_relop (section, value)
218 asection *section;
219 bfd_vma value;
221 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
222 new->type.node_code = REL;
223 new->type.node_class = etree_rel;
224 new->rel.section = section;
225 new->rel.value = value;
226 return new;
229 static etree_value_type
230 new_rel (value, str, section)
231 bfd_vma value;
232 char *str;
233 lang_output_section_statement_type *section;
235 etree_value_type new;
236 new.valid_p = true;
237 new.value = value;
238 new.str = str;
239 new.section = section;
240 return new;
243 static etree_value_type
244 new_rel_from_section (value, section)
245 bfd_vma value;
246 lang_output_section_statement_type *section;
248 etree_value_type new;
249 new.valid_p = true;
250 new.value = value;
251 new.str = NULL;
252 new.section = section;
254 new.value -= section->bfd_section->vma;
256 return new;
259 static etree_value_type
260 fold_unary (tree, current_section, allocation_done, dot, dotp)
261 etree_type *tree;
262 lang_output_section_statement_type *current_section;
263 lang_phase_type allocation_done;
264 bfd_vma dot;
265 bfd_vma *dotp;
267 etree_value_type result;
269 result = exp_fold_tree (tree->unary.child,
270 current_section,
271 allocation_done, dot, dotp);
272 if (result.valid_p)
274 switch (tree->type.node_code)
276 case ALIGN_K:
277 if (allocation_done != lang_first_phase_enum)
278 result = new_rel_from_section (align_n (dot, result.value),
279 current_section);
280 else
281 result.valid_p = false;
282 break;
284 case ABSOLUTE:
285 if (allocation_done != lang_first_phase_enum)
287 result.value += result.section->bfd_section->vma;
288 result.section = abs_output_section;
290 else
291 result.valid_p = false;
292 break;
294 case '~':
295 make_abs (&result);
296 result.value = ~result.value;
297 break;
299 case '!':
300 make_abs (&result);
301 result.value = !result.value;
302 break;
304 case '-':
305 make_abs (&result);
306 result.value = -result.value;
307 break;
309 case NEXT:
310 /* Return next place aligned to value. */
311 if (allocation_done == lang_allocating_phase_enum)
313 make_abs (&result);
314 result.value = align_n (dot, result.value);
316 else
317 result.valid_p = false;
318 break;
320 case DATA_SEGMENT_END:
321 if (allocation_done != lang_first_phase_enum
322 && current_section == abs_output_section
323 && (exp_data_seg.phase == exp_dataseg_align_seen
324 || exp_data_seg.phase == exp_dataseg_adjust
325 || allocation_done != lang_allocating_phase_enum))
327 if (exp_data_seg.phase == exp_dataseg_align_seen)
329 exp_data_seg.phase = exp_dataseg_end_seen;
330 exp_data_seg.end = result.value;
333 else
334 result.valid_p = false;
335 break;
337 default:
338 FAIL ();
339 break;
343 return result;
346 static etree_value_type
347 fold_binary (tree, current_section, allocation_done, dot, dotp)
348 etree_type *tree;
349 lang_output_section_statement_type *current_section;
350 lang_phase_type allocation_done;
351 bfd_vma dot;
352 bfd_vma *dotp;
354 etree_value_type result;
356 result = exp_fold_tree (tree->binary.lhs, current_section,
357 allocation_done, dot, dotp);
358 if (result.valid_p)
360 etree_value_type other;
362 other = exp_fold_tree (tree->binary.rhs,
363 current_section,
364 allocation_done, dot, dotp);
365 if (other.valid_p)
367 /* If the values are from different sections, or this is an
368 absolute expression, make both the source arguments
369 absolute. However, adding or subtracting an absolute
370 value from a relative value is meaningful, and is an
371 exception. */
372 if (current_section != abs_output_section
373 && (other.section == abs_output_section
374 || (result.section == abs_output_section
375 && tree->type.node_code == '+'))
376 && (tree->type.node_code == '+'
377 || tree->type.node_code == '-'))
379 if (other.section != abs_output_section)
381 /* Keep the section of the other term. */
382 if (tree->type.node_code == '+')
383 other.value = result.value + other.value;
384 else
385 other.value = result.value - other.value;
386 return other;
389 else if (result.section != other.section
390 || current_section == abs_output_section)
392 make_abs (&result);
393 make_abs (&other);
396 switch (tree->type.node_code)
398 case '%':
399 if (other.value == 0)
400 einfo (_("%F%S %% by zero\n"));
401 result.value = ((bfd_signed_vma) result.value
402 % (bfd_signed_vma) other.value);
403 break;
405 case '/':
406 if (other.value == 0)
407 einfo (_("%F%S / by zero\n"));
408 result.value = ((bfd_signed_vma) result.value
409 / (bfd_signed_vma) other.value);
410 break;
412 #define BOP(x,y) case x : result.value = result.value y other.value; break;
413 BOP ('+', +);
414 BOP ('*', *);
415 BOP ('-', -);
416 BOP (LSHIFT, <<);
417 BOP (RSHIFT, >>);
418 BOP (EQ, ==);
419 BOP (NE, !=);
420 BOP ('<', <);
421 BOP ('>', >);
422 BOP (LE, <=);
423 BOP (GE, >=);
424 BOP ('&', &);
425 BOP ('^', ^);
426 BOP ('|', |);
427 BOP (ANDAND, &&);
428 BOP (OROR, ||);
430 case MAX_K:
431 if (result.value < other.value)
432 result = other;
433 break;
435 case MIN_K:
436 if (result.value > other.value)
437 result = other;
438 break;
440 case DATA_SEGMENT_ALIGN:
441 if (allocation_done != lang_first_phase_enum
442 && current_section == abs_output_section
443 && (exp_data_seg.phase == exp_dataseg_none
444 || exp_data_seg.phase == exp_dataseg_adjust
445 || allocation_done != lang_allocating_phase_enum))
447 bfd_vma maxpage = result.value;
449 result.value = align_n (dot, maxpage);
450 if (exp_data_seg.phase != exp_dataseg_adjust)
452 result.value += dot & (maxpage - 1);
453 if (allocation_done == lang_allocating_phase_enum)
455 exp_data_seg.phase = exp_dataseg_align_seen;
456 exp_data_seg.base = result.value;
457 exp_data_seg.pagesize = other.value;
460 else if (other.value < maxpage)
461 result.value += (dot + other.value - 1)
462 & (maxpage - other.value);
464 else
465 result.valid_p = false;
466 break;
468 default:
469 FAIL ();
472 else
474 result.valid_p = false;
478 return result;
481 static etree_value_type
482 fold_trinary (tree, current_section, allocation_done, dot, dotp)
483 etree_type *tree;
484 lang_output_section_statement_type *current_section;
485 lang_phase_type allocation_done;
486 bfd_vma dot;
487 bfd_vma *dotp;
489 etree_value_type result;
491 result = exp_fold_tree (tree->trinary.cond, current_section,
492 allocation_done, dot, dotp);
493 if (result.valid_p)
494 result = exp_fold_tree ((result.value
495 ? tree->trinary.lhs
496 : tree->trinary.rhs),
497 current_section,
498 allocation_done, dot, dotp);
500 return result;
503 etree_value_type
504 invalid ()
506 etree_value_type new;
507 new.valid_p = false;
508 return new;
511 static etree_value_type
512 fold_name (tree, current_section, allocation_done, dot)
513 etree_type *tree;
514 lang_output_section_statement_type *current_section;
515 lang_phase_type allocation_done;
516 bfd_vma dot;
518 etree_value_type result;
520 switch (tree->type.node_code)
522 case SIZEOF_HEADERS:
523 if (allocation_done != lang_first_phase_enum)
525 result = new_abs ((bfd_vma)
526 bfd_sizeof_headers (output_bfd,
527 link_info.relocateable));
529 else
531 result.valid_p = false;
533 break;
534 case DEFINED:
535 if (allocation_done == lang_first_phase_enum)
536 result.valid_p = false;
537 else
539 struct bfd_link_hash_entry *h;
541 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
542 tree->name.name,
543 false, false, true);
544 result.value = (h != (struct bfd_link_hash_entry *) NULL
545 && (h->type == bfd_link_hash_defined
546 || h->type == bfd_link_hash_defweak
547 || h->type == bfd_link_hash_common));
548 result.section = 0;
549 result.valid_p = true;
551 break;
552 case NAME:
553 result.valid_p = false;
554 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
556 if (allocation_done != lang_first_phase_enum)
557 result = new_rel_from_section (dot, current_section);
558 else
559 result = invalid ();
561 else if (allocation_done != lang_first_phase_enum)
563 struct bfd_link_hash_entry *h;
565 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
566 tree->name.name,
567 false, false, true);
568 if (h != NULL
569 && (h->type == bfd_link_hash_defined
570 || h->type == bfd_link_hash_defweak))
572 if (bfd_is_abs_section (h->u.def.section))
573 result = new_abs (h->u.def.value);
574 else if (allocation_done == lang_final_phase_enum
575 || allocation_done == lang_allocating_phase_enum)
577 asection *output_section;
579 output_section = h->u.def.section->output_section;
580 if (output_section == NULL)
581 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
582 tree->name.name);
583 else
585 lang_output_section_statement_type *os;
587 os = (lang_output_section_statement_lookup
588 (bfd_get_section_name (output_bfd,
589 output_section)));
591 /* FIXME: Is this correct if this section is
592 being linked with -R? */
593 result = new_rel ((h->u.def.value
594 + h->u.def.section->output_offset),
595 NULL,
596 os);
600 else if (allocation_done == lang_final_phase_enum)
601 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
602 tree->name.name);
604 break;
606 case ADDR:
607 if (allocation_done != lang_first_phase_enum)
609 lang_output_section_statement_type *os;
611 os = lang_output_section_find (tree->name.name);
612 check (os, tree->name.name, "ADDR");
613 result = new_rel (0, NULL, os);
615 else
616 result = invalid ();
617 break;
619 case LOADADDR:
620 if (allocation_done != lang_first_phase_enum)
622 lang_output_section_statement_type *os;
624 os = lang_output_section_find (tree->name.name);
625 check (os, tree->name.name, "LOADADDR");
626 if (os->load_base == NULL)
627 result = new_rel (0, NULL, os);
628 else
629 result = exp_fold_tree_no_dot (os->load_base,
630 abs_output_section,
631 allocation_done);
633 else
634 result = invalid ();
635 break;
637 case SIZEOF:
638 if (allocation_done != lang_first_phase_enum)
640 int opb = bfd_octets_per_byte (output_bfd);
641 lang_output_section_statement_type *os;
643 os = lang_output_section_find (tree->name.name);
644 check (os, tree->name.name, "SIZEOF");
645 result = new_abs (os->bfd_section->_raw_size / opb);
647 else
648 result = invalid ();
649 break;
651 default:
652 FAIL ();
653 break;
656 return result;
659 etree_value_type
660 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
661 etree_type *tree;
662 lang_output_section_statement_type *current_section;
663 lang_phase_type allocation_done;
664 bfd_vma dot;
665 bfd_vma *dotp;
667 etree_value_type result;
669 if (tree == NULL)
671 result.valid_p = false;
672 return result;
675 switch (tree->type.node_class)
677 case etree_value:
678 result = new_rel (tree->value.value, tree->value.str, current_section);
679 break;
681 case etree_rel:
682 if (allocation_done != lang_final_phase_enum)
683 result.valid_p = false;
684 else
685 result = new_rel ((tree->rel.value
686 + tree->rel.section->output_section->vma
687 + tree->rel.section->output_offset),
688 NULL,
689 current_section);
690 break;
692 case etree_assert:
693 result = exp_fold_tree (tree->assert_s.child,
694 current_section,
695 allocation_done, dot, dotp);
696 if (result.valid_p)
698 if (! result.value)
699 einfo ("%F%P: %s\n", tree->assert_s.message);
700 return result;
702 break;
704 case etree_unary:
705 result = fold_unary (tree, current_section, allocation_done,
706 dot, dotp);
707 break;
709 case etree_binary:
710 result = fold_binary (tree, current_section, allocation_done,
711 dot, dotp);
712 break;
714 case etree_trinary:
715 result = fold_trinary (tree, current_section, allocation_done,
716 dot, dotp);
717 break;
719 case etree_assign:
720 case etree_provide:
721 case etree_provided:
722 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
724 /* Assignment to dot can only be done during allocation. */
725 if (tree->type.node_class != etree_assign)
726 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
727 if (allocation_done == lang_allocating_phase_enum
728 || (allocation_done == lang_final_phase_enum
729 && current_section == abs_output_section))
731 result = exp_fold_tree (tree->assign.src,
732 current_section,
733 allocation_done, dot,
734 dotp);
735 if (! result.valid_p)
736 einfo (_("%F%S invalid assignment to location counter\n"));
737 else
739 if (current_section == NULL)
740 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
741 else
743 bfd_vma nextdot;
745 nextdot = (result.value
746 + current_section->bfd_section->vma);
747 if (nextdot < dot
748 && current_section != abs_output_section)
749 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
750 dot, nextdot);
751 else
752 *dotp = nextdot;
757 else
759 result = exp_fold_tree (tree->assign.src,
760 current_section, allocation_done,
761 dot, dotp);
762 if (result.valid_p)
764 boolean create;
765 struct bfd_link_hash_entry *h;
767 if (tree->type.node_class == etree_assign)
768 create = true;
769 else
770 create = false;
771 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
772 create, false, false);
773 if (h == (struct bfd_link_hash_entry *) NULL)
775 if (tree->type.node_class == etree_assign)
776 einfo (_("%P%F:%s: hash creation failed\n"),
777 tree->assign.dst);
779 else if (tree->type.node_class == etree_provide
780 && h->type != bfd_link_hash_undefined
781 && h->type != bfd_link_hash_common)
783 /* Do nothing. The symbol was defined by some
784 object. */
786 else
788 /* FIXME: Should we worry if the symbol is already
789 defined? */
790 h->type = bfd_link_hash_defined;
791 h->u.def.value = result.value;
792 h->u.def.section = result.section->bfd_section;
793 if (tree->type.node_class == etree_provide)
794 tree->type.node_class = etree_provided;
798 break;
800 case etree_name:
801 result = fold_name (tree, current_section, allocation_done, dot);
802 break;
804 default:
805 FAIL ();
806 break;
809 return result;
812 static etree_value_type
813 exp_fold_tree_no_dot (tree, current_section, allocation_done)
814 etree_type *tree;
815 lang_output_section_statement_type *current_section;
816 lang_phase_type allocation_done;
818 return exp_fold_tree (tree, current_section, allocation_done,
819 (bfd_vma) 0, (bfd_vma *) NULL);
822 etree_type *
823 exp_binop (code, lhs, rhs)
824 int code;
825 etree_type *lhs;
826 etree_type *rhs;
828 etree_type value, *new;
829 etree_value_type r;
831 value.type.node_code = code;
832 value.binary.lhs = lhs;
833 value.binary.rhs = rhs;
834 value.type.node_class = etree_binary;
835 r = exp_fold_tree_no_dot (&value,
836 abs_output_section,
837 lang_first_phase_enum);
838 if (r.valid_p)
840 return exp_intop (r.value);
842 new = (etree_type *) stat_alloc (sizeof (new->binary));
843 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
844 return new;
847 etree_type *
848 exp_trinop (code, cond, lhs, rhs)
849 int code;
850 etree_type *cond;
851 etree_type *lhs;
852 etree_type *rhs;
854 etree_type value, *new;
855 etree_value_type r;
856 value.type.node_code = code;
857 value.trinary.lhs = lhs;
858 value.trinary.cond = cond;
859 value.trinary.rhs = rhs;
860 value.type.node_class = etree_trinary;
861 r = exp_fold_tree_no_dot (&value,
862 (lang_output_section_statement_type *) NULL,
863 lang_first_phase_enum);
864 if (r.valid_p)
865 return exp_intop (r.value);
867 new = (etree_type *) stat_alloc (sizeof (new->trinary));
868 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
869 return new;
872 etree_type *
873 exp_unop (code, child)
874 int code;
875 etree_type *child;
877 etree_type value, *new;
879 etree_value_type r;
880 value.unary.type.node_code = code;
881 value.unary.child = child;
882 value.unary.type.node_class = etree_unary;
883 r = exp_fold_tree_no_dot (&value, abs_output_section,
884 lang_first_phase_enum);
885 if (r.valid_p)
886 return exp_intop (r.value);
888 new = (etree_type *) stat_alloc (sizeof (new->unary));
889 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
890 return new;
893 etree_type *
894 exp_nameop (code, name)
895 int code;
896 const char *name;
898 etree_type value, *new;
899 etree_value_type r;
900 value.name.type.node_code = code;
901 value.name.name = name;
902 value.name.type.node_class = etree_name;
904 r = exp_fold_tree_no_dot (&value,
905 (lang_output_section_statement_type *) NULL,
906 lang_first_phase_enum);
907 if (r.valid_p)
908 return exp_intop (r.value);
910 new = (etree_type *) stat_alloc (sizeof (new->name));
911 memcpy ((char *) new, (char *) &value, sizeof (new->name));
912 return new;
916 etree_type *
917 exp_assop (code, dst, src)
918 int code;
919 const char *dst;
920 etree_type *src;
922 etree_type value, *new;
924 value.assign.type.node_code = code;
926 value.assign.src = src;
927 value.assign.dst = dst;
928 value.assign.type.node_class = etree_assign;
930 #if 0
931 if (exp_fold_tree_no_dot (&value, &result))
932 return exp_intop (result);
933 #endif
934 new = (etree_type *) stat_alloc (sizeof (new->assign));
935 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
936 return new;
939 /* Handle PROVIDE. */
941 etree_type *
942 exp_provide (dst, src)
943 const char *dst;
944 etree_type *src;
946 etree_type *n;
948 n = (etree_type *) stat_alloc (sizeof (n->assign));
949 n->assign.type.node_code = '=';
950 n->assign.type.node_class = etree_provide;
951 n->assign.src = src;
952 n->assign.dst = dst;
953 return n;
956 /* Handle ASSERT. */
958 etree_type *
959 exp_assert (exp, message)
960 etree_type *exp;
961 const char *message;
963 etree_type *n;
965 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
966 n->assert_s.type.node_code = '!';
967 n->assert_s.type.node_class = etree_assert;
968 n->assert_s.child = exp;
969 n->assert_s.message = message;
970 return n;
973 void
974 exp_print_tree (tree)
975 etree_type *tree;
977 if (config.map_file == NULL)
978 config.map_file = stderr;
980 if (tree == NULL)
982 minfo ("NULL TREE\n");
983 return;
986 switch (tree->type.node_class)
988 case etree_value:
989 minfo ("0x%v", tree->value.value);
990 return;
991 case etree_rel:
992 if (tree->rel.section->owner != NULL)
993 minfo ("%B:", tree->rel.section->owner);
994 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
995 return;
996 case etree_assign:
997 #if 0
998 if (tree->assign.dst->sdefs != (asymbol *) NULL)
999 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
1000 tree->assign.dst->sdefs->value);
1001 else
1002 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
1003 #endif
1004 fprintf (config.map_file, "%s", tree->assign.dst);
1005 exp_print_token (tree->type.node_code, true);
1006 exp_print_tree (tree->assign.src);
1007 break;
1008 case etree_provide:
1009 case etree_provided:
1010 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1011 exp_print_tree (tree->assign.src);
1012 fprintf (config.map_file, ")");
1013 break;
1014 case etree_binary:
1015 fprintf (config.map_file, "(");
1016 exp_print_tree (tree->binary.lhs);
1017 exp_print_token (tree->type.node_code, true);
1018 exp_print_tree (tree->binary.rhs);
1019 fprintf (config.map_file, ")");
1020 break;
1021 case etree_trinary:
1022 exp_print_tree (tree->trinary.cond);
1023 fprintf (config.map_file, "?");
1024 exp_print_tree (tree->trinary.lhs);
1025 fprintf (config.map_file, ":");
1026 exp_print_tree (tree->trinary.rhs);
1027 break;
1028 case etree_unary:
1029 exp_print_token (tree->unary.type.node_code, false);
1030 if (tree->unary.child)
1032 fprintf (config.map_file, " (");
1033 exp_print_tree (tree->unary.child);
1034 fprintf (config.map_file, ")");
1036 break;
1038 case etree_assert:
1039 fprintf (config.map_file, "ASSERT (");
1040 exp_print_tree (tree->assert_s.child);
1041 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1042 break;
1044 case etree_undef:
1045 fprintf (config.map_file, "????????");
1046 break;
1047 case etree_name:
1048 if (tree->type.node_code == NAME)
1050 fprintf (config.map_file, "%s", tree->name.name);
1052 else
1054 exp_print_token (tree->type.node_code, false);
1055 if (tree->name.name)
1056 fprintf (config.map_file, " (%s)", tree->name.name);
1058 break;
1059 default:
1060 FAIL ();
1061 break;
1065 bfd_vma
1066 exp_get_vma (tree, def, name, allocation_done)
1067 etree_type *tree;
1068 bfd_vma def;
1069 char *name;
1070 lang_phase_type allocation_done;
1072 etree_value_type r;
1074 if (tree != NULL)
1076 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1077 if (! r.valid_p && name != NULL)
1078 einfo (_("%F%S nonconstant expression for %s\n"), name);
1079 return r.value;
1081 else
1082 return def;
1086 exp_get_value_int (tree, def, name, allocation_done)
1087 etree_type *tree;
1088 int def;
1089 char *name;
1090 lang_phase_type allocation_done;
1092 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1095 fill_type *
1096 exp_get_fill (tree, def, name, allocation_done)
1097 etree_type *tree;
1098 fill_type *def;
1099 char *name;
1100 lang_phase_type allocation_done;
1102 fill_type *fill;
1103 etree_value_type r;
1104 size_t len;
1105 unsigned int val;
1107 if (tree == NULL)
1108 return def;
1110 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1111 if (! r.valid_p && name != NULL)
1112 einfo (_("%F%S nonconstant expression for %s\n"), name);
1114 if (r.str != NULL && (len = strlen (r.str)) != 0)
1116 unsigned char *dst;
1117 unsigned char *s;
1118 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1119 fill->size = (len + 1) / 2;
1120 dst = fill->data;
1121 s = r.str;
1122 val = 0;
1125 unsigned int digit;
1127 digit = *s++ - '0';
1128 if (digit > 9)
1129 digit = (digit - 'A' + '0' + 10) & 0xf;
1130 val <<= 4;
1131 val += digit;
1132 --len;
1133 if ((len & 1) == 0)
1135 *dst++ = val;
1136 val = 0;
1139 while (len != 0);
1141 else
1143 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1144 val = r.value;
1145 fill->data[0] = (val >> 24) & 0xff;
1146 fill->data[1] = (val >> 16) & 0xff;
1147 fill->data[2] = (val >> 8) & 0xff;
1148 fill->data[3] = (val >> 0) & 0xff;
1149 fill->size = 4;
1151 return fill;
1154 bfd_vma
1155 exp_get_abs_int (tree, def, name, allocation_done)
1156 etree_type *tree;
1157 int def ATTRIBUTE_UNUSED;
1158 char *name;
1159 lang_phase_type allocation_done;
1161 etree_value_type res;
1162 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1164 if (res.valid_p)
1165 res.value += res.section->bfd_section->vma;
1166 else
1167 einfo (_("%F%S non constant expression for %s\n"), name);
1169 return res.value;
1172 bfd_vma align_n (value, align)
1173 bfd_vma value;
1174 bfd_vma align;
1176 if (align <= 1)
1177 return value;
1179 value = (value + align - 1) / align;
1180 return value * align;