4023639a56b2b1a4c3c67a37550832ba5ec58a34
[platform/upstream/binutils.git] / ld / ldexp.c
1 /* This module handles expression trees.
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001
4    Free Software Foundation, Inc.
5    Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
6
7 This file is part of GLD, the Gnu Linker.
8
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.
13
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.
18
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.  */
23
24 /* This module is in charge of working out the contents of expressions.
25
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.  */
30
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "bfdlink.h"
34
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
43 static void exp_print_token PARAMS ((token_code_type code));
44 static void make_abs PARAMS ((etree_value_type *ptr));
45 static etree_value_type new_abs PARAMS ((bfd_vma value));
46 static void check PARAMS ((lang_output_section_statement_type *os,
47                            const char *name, const char *op));
48 static etree_value_type new_rel
49   PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
50 static etree_value_type new_rel_from_section
51   PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
52 static etree_value_type fold_binary
53   PARAMS ((etree_type *tree,
54            lang_output_section_statement_type *current_section,
55            lang_phase_type allocation_done,
56            bfd_vma dot, bfd_vma *dotp));
57 static etree_value_type fold_name
58   PARAMS ((etree_type *tree,
59            lang_output_section_statement_type *current_section,
60            lang_phase_type allocation_done,
61            bfd_vma dot));
62 static etree_value_type exp_fold_tree_no_dot
63   PARAMS ((etree_type *tree,
64            lang_output_section_statement_type *current_section,
65            lang_phase_type allocation_done));
66
67 struct exp_data_seg exp_data_seg;
68
69 static void
70 exp_print_token (code)
71      token_code_type code;
72 {
73   static CONST struct
74   {
75     token_code_type code;
76     char * name;
77   }
78   table[] =
79   {
80     { INT, "int" },
81     { NAME, "NAME" },
82     { PLUSEQ, "+=" },
83     { MINUSEQ, "-=" },
84     { MULTEQ, "*=" },
85     { DIVEQ, "/=" },
86     { LSHIFTEQ, "<<=" },
87     { RSHIFTEQ, ">>=" },
88     { ANDEQ, "&=" },
89     { OREQ, "|=" },
90     { OROR, "||" },
91     { ANDAND, "&&" },
92     { EQ, "==" },
93     { NE, "!=" },
94     { LE, "<=" },
95     { GE, ">=" },
96     { LSHIFT, "<<" },
97     { RSHIFT, ">>" },
98     { ALIGN_K, "ALIGN" },
99     { BLOCK, "BLOCK" },
100     { QUAD, "QUAD" },
101     { SQUAD, "SQUAD" },
102     { LONG, "LONG" },
103     { SHORT, "SHORT" },
104     { BYTE, "BYTE" },
105     { SECTIONS, "SECTIONS" },
106     { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
107     { MEMORY, "MEMORY" },
108     { DEFINED, "DEFINED" },
109     { TARGET_K, "TARGET" },
110     { SEARCH_DIR, "SEARCH_DIR" },
111     { MAP, "MAP" },
112     { ENTRY, "ENTRY" },
113     { NEXT, "NEXT" },
114     { SIZEOF, "SIZEOF" },
115     { ADDR, "ADDR" },
116     { LOADADDR, "LOADADDR" },
117     { MAX_K, "MAX_K" },
118     { REL, "relocateable" },
119     { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
120     { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
121   };
122   unsigned int idx;
123
124   for (idx = ARRAY_SIZE (table); idx--;)
125     {
126       if (table[idx].code == code)
127         {
128           fprintf (config.map_file, " %s ", table[idx].name);
129           return;
130         }
131     }
132
133   /* Not in table, just print it alone.  */
134   if (code < 127)
135     fprintf (config.map_file, " %c ", code);
136   else
137     fprintf (config.map_file, " <code %d> ", code);
138 }
139
140 static void
141 make_abs (ptr)
142      etree_value_type *ptr;
143 {
144   asection *s = ptr->section->bfd_section;
145   ptr->value += s->vma;
146   ptr->section = abs_output_section;
147 }
148
149 static etree_value_type
150 new_abs (value)
151      bfd_vma value;
152 {
153   etree_value_type new;
154   new.valid_p = true;
155   new.section = abs_output_section;
156   new.value = value;
157   return new;
158 }
159
160 static void
161 check (os, name, op)
162      lang_output_section_statement_type *os;
163      const char *name;
164      const char *op;
165 {
166   if (os == NULL)
167     einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
168   if (! os->processed)
169     einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
170 }
171
172 etree_type *
173 exp_intop (value)
174      bfd_vma value;
175 {
176   etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
177   new->type.node_code = INT;
178   new->value.value = value;
179   new->type.node_class = etree_value;
180   return new;
181
182 }
183
184 /* Build an expression representing an unnamed relocateable value.  */
185
186 etree_type *
187 exp_relop (section, value)
188      asection *section;
189      bfd_vma value;
190 {
191   etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
192   new->type.node_code = REL;
193   new->type.node_class = etree_rel;
194   new->rel.section = section;
195   new->rel.value = value;
196   return new;
197 }
198
199 static etree_value_type
200 new_rel (value, section)
201      bfd_vma value;
202      lang_output_section_statement_type *section;
203 {
204   etree_value_type new;
205   new.valid_p = true;
206   new.value = value;
207   new.section = section;
208   return new;
209 }
210
211 static etree_value_type
212 new_rel_from_section (value, section)
213      bfd_vma value;
214      lang_output_section_statement_type *section;
215 {
216   etree_value_type new;
217   new.valid_p = true;
218   new.value = value;
219   new.section = section;
220
221   new.value -= section->bfd_section->vma;
222
223   return new;
224 }
225
226 static etree_value_type
227 fold_binary (tree, current_section, allocation_done, dot, dotp)
228      etree_type *tree;
229      lang_output_section_statement_type *current_section;
230      lang_phase_type allocation_done;
231      bfd_vma dot;
232      bfd_vma *dotp;
233 {
234   etree_value_type result;
235
236   result = exp_fold_tree (tree->binary.lhs, current_section,
237                           allocation_done, dot, dotp);
238   if (result.valid_p)
239     {
240       etree_value_type other;
241
242       other = exp_fold_tree (tree->binary.rhs,
243                              current_section,
244                              allocation_done, dot, dotp);
245       if (other.valid_p)
246         {
247           /* If the values are from different sections, or this is an
248              absolute expression, make both the source arguments
249              absolute.  However, adding or subtracting an absolute
250              value from a relative value is meaningful, and is an
251              exception.  */
252           if (current_section != abs_output_section
253               && (other.section == abs_output_section
254                   || (result.section == abs_output_section
255                       && tree->type.node_code == '+'))
256               && (tree->type.node_code == '+'
257                   || tree->type.node_code == '-'))
258             {
259               etree_value_type hold;
260
261               /* If there is only one absolute term, make sure it is the
262                  second one.  */
263               if (other.section != abs_output_section)
264                 {
265                   hold = result;
266                   result = other;
267                   other = hold;
268                 }
269             }
270           else if (result.section != other.section
271                    || current_section == abs_output_section)
272             {
273               make_abs (&result);
274               make_abs (&other);
275             }
276
277           switch (tree->type.node_code)
278             {
279             case '%':
280               if (other.value == 0)
281                 einfo (_("%F%S %% by zero\n"));
282               result.value = ((bfd_signed_vma) result.value
283                               % (bfd_signed_vma) other.value);
284               break;
285
286             case '/':
287               if (other.value == 0)
288                 einfo (_("%F%S / by zero\n"));
289               result.value = ((bfd_signed_vma) result.value
290                               / (bfd_signed_vma) other.value);
291               break;
292
293 #define BOP(x,y) case x : result.value = result.value y other.value; break;
294               BOP ('+', +);
295               BOP ('*', *);
296               BOP ('-', -);
297               BOP (LSHIFT, <<);
298               BOP (RSHIFT, >>);
299               BOP (EQ, ==);
300               BOP (NE, !=);
301               BOP ('<', <);
302               BOP ('>', >);
303               BOP (LE, <=);
304               BOP (GE, >=);
305               BOP ('&', &);
306               BOP ('^', ^);
307               BOP ('|', |);
308               BOP (ANDAND, &&);
309               BOP (OROR, ||);
310
311             case MAX_K:
312               if (result.value < other.value)
313                 result = other;
314               break;
315
316             case MIN_K:
317               if (result.value > other.value)
318                 result = other;
319               break;
320
321             case DATA_SEGMENT_ALIGN:
322               if (allocation_done != lang_first_phase_enum
323                   && current_section == abs_output_section
324                   && (exp_data_seg.phase == exp_dataseg_none
325                       || exp_data_seg.phase == exp_dataseg_adjust
326                       || allocation_done != lang_allocating_phase_enum))
327                 {
328                   bfd_vma maxpage = result.value;
329
330                   result.value = ALIGN_N (dot, maxpage);
331                   if (exp_data_seg.phase != exp_dataseg_adjust)
332                     {
333                       result.value += dot & (maxpage - 1);
334                       if (allocation_done == lang_allocating_phase_enum)
335                         {
336                           exp_data_seg.phase = exp_dataseg_align_seen;
337                           exp_data_seg.base = result.value;
338                           exp_data_seg.pagesize = other.value;
339                         }
340                     }
341                   else if (other.value < maxpage)
342                     result.value += dot & (maxpage - other.value);
343                 }
344               else
345                 result.valid_p = false;
346               break;
347
348             default:
349               FAIL ();
350             }
351         }
352       else
353         {
354           result.valid_p = false;
355         }
356     }
357
358   return result;
359 }
360
361 etree_value_type
362 invalid ()
363 {
364   etree_value_type new;
365   new.valid_p = false;
366   return new;
367 }
368
369 static etree_value_type
370 fold_name (tree, current_section, allocation_done, dot)
371      etree_type *tree;
372      lang_output_section_statement_type *current_section;
373      lang_phase_type allocation_done;
374      bfd_vma dot;
375 {
376   etree_value_type result;
377
378   switch (tree->type.node_code)
379     {
380     case SIZEOF_HEADERS:
381       if (allocation_done != lang_first_phase_enum)
382         {
383           result = new_abs ((bfd_vma)
384                             bfd_sizeof_headers (output_bfd,
385                                                 link_info.relocateable));
386         }
387       else
388         {
389           result.valid_p = false;
390         }
391       break;
392     case DEFINED:
393       if (allocation_done == lang_first_phase_enum)
394         result.valid_p = false;
395       else
396         {
397           struct bfd_link_hash_entry *h;
398
399           h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
400                                             tree->name.name,
401                                             false, false, true);
402           result.value = (h != (struct bfd_link_hash_entry *) NULL
403                           && (h->type == bfd_link_hash_defined
404                               || h->type == bfd_link_hash_defweak
405                               || h->type == bfd_link_hash_common));
406           result.section = 0;
407           result.valid_p = true;
408         }
409       break;
410     case NAME:
411       result.valid_p = false;
412       if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
413         {
414           if (allocation_done != lang_first_phase_enum)
415             result = new_rel_from_section (dot, current_section);
416           else
417             result = invalid ();
418         }
419       else if (allocation_done != lang_first_phase_enum)
420         {
421           struct bfd_link_hash_entry *h;
422
423           h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
424                                             tree->name.name,
425                                             false, false, true);
426           if (h != NULL
427               && (h->type == bfd_link_hash_defined
428                   || h->type == bfd_link_hash_defweak))
429             {
430               if (bfd_is_abs_section (h->u.def.section))
431                 result = new_abs (h->u.def.value);
432               else if (allocation_done == lang_final_phase_enum
433                        || allocation_done == lang_allocating_phase_enum)
434                 {
435                   asection *output_section;
436
437                   output_section = h->u.def.section->output_section;
438                   if (output_section == NULL)
439                     einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
440                            tree->name.name);
441                   else
442                     {
443                       lang_output_section_statement_type *os;
444
445                       os = (lang_output_section_statement_lookup
446                             (bfd_get_section_name (output_bfd,
447                                                    output_section)));
448
449                       /* FIXME: Is this correct if this section is
450                          being linked with -R?  */
451                       result = new_rel ((h->u.def.value
452                                          + h->u.def.section->output_offset),
453                                         os);
454                     }
455                 }
456             }
457           else if (allocation_done == lang_final_phase_enum)
458             einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
459                    tree->name.name);
460         }
461       break;
462
463     case ADDR:
464       if (allocation_done != lang_first_phase_enum)
465         {
466           lang_output_section_statement_type *os;
467
468           os = lang_output_section_find (tree->name.name);
469           check (os, tree->name.name, "ADDR");
470           result = new_rel (0, os);
471         }
472       else
473         result = invalid ();
474       break;
475
476     case LOADADDR:
477       if (allocation_done != lang_first_phase_enum)
478         {
479           lang_output_section_statement_type *os;
480
481           os = lang_output_section_find (tree->name.name);
482           check (os, tree->name.name, "LOADADDR");
483           if (os->load_base == NULL)
484             result = new_rel (0, os);
485           else
486             result = exp_fold_tree_no_dot (os->load_base,
487                                            abs_output_section,
488                                            allocation_done);
489         }
490       else
491         result = invalid ();
492       break;
493
494     case SIZEOF:
495       if (allocation_done != lang_first_phase_enum)
496         {
497           int opb = bfd_octets_per_byte (output_bfd);
498           lang_output_section_statement_type *os;
499
500           os = lang_output_section_find (tree->name.name);
501           check (os, tree->name.name, "SIZEOF");
502           result = new_abs (os->bfd_section->_raw_size / opb);
503         }
504       else
505         result = invalid ();
506       break;
507
508     default:
509       FAIL ();
510       break;
511     }
512
513   return result;
514 }
515
516 etree_value_type
517 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
518      etree_type *tree;
519      lang_output_section_statement_type *current_section;
520      lang_phase_type allocation_done;
521      bfd_vma dot;
522      bfd_vma *dotp;
523 {
524   etree_value_type result;
525
526   if (tree == NULL)
527     {
528       result.valid_p = false;
529       return result;
530     }
531
532   switch (tree->type.node_class)
533     {
534     case etree_value:
535       result = new_rel (tree->value.value, current_section);
536       break;
537
538     case etree_rel:
539       if (allocation_done != lang_final_phase_enum)
540         result.valid_p = false;
541       else
542         result = new_rel ((tree->rel.value
543                            + tree->rel.section->output_section->vma
544                            + tree->rel.section->output_offset),
545                           current_section);
546       break;
547
548     case etree_assert:
549       result = exp_fold_tree (tree->assert_s.child,
550                               current_section,
551                               allocation_done, dot, dotp);
552       if (result.valid_p)
553         {
554           if (! result.value)
555             einfo ("%F%P: %s\n", tree->assert_s.message);
556           return result;
557         }
558       break;
559
560     case etree_unary:
561       result = exp_fold_tree (tree->unary.child,
562                               current_section,
563                               allocation_done, dot, dotp);
564       if (result.valid_p)
565         {
566           switch (tree->type.node_code)
567             {
568             case ALIGN_K:
569               if (allocation_done != lang_first_phase_enum)
570                 result = new_rel_from_section (ALIGN_N (dot, result.value),
571                                                current_section);
572               else
573                 result.valid_p = false;
574               break;
575
576             case ABSOLUTE:
577               if (allocation_done != lang_first_phase_enum && result.valid_p)
578                 {
579                   result.value += result.section->bfd_section->vma;
580                   result.section = abs_output_section;
581                 }
582               else
583                 result.valid_p = false;
584               break;
585
586             case '~':
587               make_abs (&result);
588               result.value = ~result.value;
589               break;
590
591             case '!':
592               make_abs (&result);
593               result.value = !result.value;
594               break;
595
596             case '-':
597               make_abs (&result);
598               result.value = -result.value;
599               break;
600
601             case NEXT:
602               /* Return next place aligned to value.  */
603               if (allocation_done == lang_allocating_phase_enum)
604                 {
605                   make_abs (&result);
606                   result.value = ALIGN_N (dot, result.value);
607                 }
608               else
609                 result.valid_p = false;
610               break;
611
612             case DATA_SEGMENT_END:
613               if (allocation_done != lang_first_phase_enum
614                   && current_section == abs_output_section
615                   && (exp_data_seg.phase == exp_dataseg_align_seen
616                       || exp_data_seg.phase == exp_dataseg_adjust
617                       || allocation_done != lang_allocating_phase_enum))
618                 {
619                   if (exp_data_seg.phase == exp_dataseg_align_seen)
620                     {
621                       exp_data_seg.phase = exp_dataseg_end_seen;
622                       exp_data_seg.end = result.value;
623                     }
624                 }
625               else
626                 result.valid_p = false;
627               break;
628
629             default:
630               FAIL ();
631               break;
632             }
633         }
634       break;
635
636     case etree_trinary:
637       result = exp_fold_tree (tree->trinary.cond, current_section,
638                               allocation_done, dot, dotp);
639       if (result.valid_p)
640         result = exp_fold_tree ((result.value
641                                  ? tree->trinary.lhs
642                                  : tree->trinary.rhs),
643                                 current_section,
644                                 allocation_done, dot, dotp);
645       break;
646
647     case etree_binary:
648       result = fold_binary (tree, current_section, allocation_done,
649                             dot, dotp);
650       break;
651
652     case etree_assign:
653     case etree_provide:
654     case etree_provided:
655       if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
656         {
657           /* Assignment to dot can only be done during allocation.  */
658           if (tree->type.node_class != etree_assign)
659             einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
660           if (allocation_done == lang_allocating_phase_enum
661               || (allocation_done == lang_final_phase_enum
662                   && current_section == abs_output_section))
663             {
664               result = exp_fold_tree (tree->assign.src,
665                                       current_section,
666                                       allocation_done, dot,
667                                       dotp);
668               if (! result.valid_p)
669                 einfo (_("%F%S invalid assignment to location counter\n"));
670               else
671                 {
672                   if (current_section == NULL)
673                     einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
674                   else
675                     {
676                       bfd_vma nextdot;
677
678                       nextdot = (result.value
679                                  + current_section->bfd_section->vma);
680                       if (nextdot < dot
681                           && current_section != abs_output_section)
682                         einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
683                                dot, nextdot);
684                       else
685                         *dotp = nextdot;
686                     }
687                 }
688             }
689         }
690       else
691         {
692           result = exp_fold_tree (tree->assign.src,
693                                   current_section, allocation_done,
694                                   dot, dotp);
695           if (result.valid_p)
696             {
697               boolean create;
698               struct bfd_link_hash_entry *h;
699
700               if (tree->type.node_class == etree_assign)
701                 create = true;
702               else
703                 create = false;
704               h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
705                                         create, false, false);
706               if (h == (struct bfd_link_hash_entry *) NULL)
707                 {
708                   if (tree->type.node_class == etree_assign)
709                     einfo (_("%P%F:%s: hash creation failed\n"),
710                            tree->assign.dst);
711                 }
712               else if (tree->type.node_class == etree_provide
713                        && h->type != bfd_link_hash_undefined
714                        && h->type != bfd_link_hash_common)
715                 {
716                   /* Do nothing.  The symbol was defined by some
717                      object.  */
718                 }
719               else
720                 {
721                   /* FIXME: Should we worry if the symbol is already
722                      defined?  */
723                   h->type = bfd_link_hash_defined;
724                   h->u.def.value = result.value;
725                   h->u.def.section = result.section->bfd_section;
726                   if (tree->type.node_class == etree_provide)
727                     tree->type.node_class = etree_provided;
728                 }
729             }
730         }
731       break;
732
733     case etree_name:
734       result = fold_name (tree, current_section, allocation_done, dot);
735       break;
736
737     default:
738       FAIL ();
739       break;
740     }
741
742   return result;
743 }
744
745 static etree_value_type
746 exp_fold_tree_no_dot (tree, current_section, allocation_done)
747      etree_type *tree;
748      lang_output_section_statement_type *current_section;
749      lang_phase_type allocation_done;
750 {
751   return exp_fold_tree (tree, current_section, allocation_done,
752                         (bfd_vma) 0, (bfd_vma *) NULL);
753 }
754
755 etree_type *
756 exp_binop (code, lhs, rhs)
757      int code;
758      etree_type *lhs;
759      etree_type *rhs;
760 {
761   etree_type value, *new;
762   etree_value_type r;
763
764   value.type.node_code = code;
765   value.binary.lhs = lhs;
766   value.binary.rhs = rhs;
767   value.type.node_class = etree_binary;
768   r = exp_fold_tree_no_dot (&value,
769                             abs_output_section,
770                             lang_first_phase_enum);
771   if (r.valid_p)
772     {
773       return exp_intop (r.value);
774     }
775   new = (etree_type *) stat_alloc (sizeof (new->binary));
776   memcpy ((char *) new, (char *) &value, sizeof (new->binary));
777   return new;
778 }
779
780 etree_type *
781 exp_trinop (code, cond, lhs, rhs)
782      int code;
783      etree_type *cond;
784      etree_type *lhs;
785      etree_type *rhs;
786 {
787   etree_type value, *new;
788   etree_value_type r;
789   value.type.node_code = code;
790   value.trinary.lhs = lhs;
791   value.trinary.cond = cond;
792   value.trinary.rhs = rhs;
793   value.type.node_class = etree_trinary;
794   r = exp_fold_tree_no_dot (&value,
795                             (lang_output_section_statement_type *) NULL,
796                             lang_first_phase_enum);
797   if (r.valid_p)
798     return exp_intop (r.value);
799
800   new = (etree_type *) stat_alloc (sizeof (new->trinary));
801   memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
802   return new;
803 }
804
805 etree_type *
806 exp_unop (code, child)
807      int code;
808      etree_type *child;
809 {
810   etree_type value, *new;
811
812   etree_value_type r;
813   value.unary.type.node_code = code;
814   value.unary.child = child;
815   value.unary.type.node_class = etree_unary;
816   r = exp_fold_tree_no_dot (&value, abs_output_section,
817                             lang_first_phase_enum);
818   if (r.valid_p)
819     return exp_intop (r.value);
820
821   new = (etree_type *) stat_alloc (sizeof (new->unary));
822   memcpy ((char *) new, (char *) &value, sizeof (new->unary));
823   return new;
824 }
825
826 etree_type *
827 exp_nameop (code, name)
828      int code;
829      CONST char *name;
830 {
831   etree_type value, *new;
832   etree_value_type r;
833   value.name.type.node_code = code;
834   value.name.name = name;
835   value.name.type.node_class = etree_name;
836
837   r = exp_fold_tree_no_dot (&value,
838                             (lang_output_section_statement_type *) NULL,
839                             lang_first_phase_enum);
840   if (r.valid_p)
841     return exp_intop (r.value);
842
843   new = (etree_type *) stat_alloc (sizeof (new->name));
844   memcpy ((char *) new, (char *) &value, sizeof (new->name));
845   return new;
846
847 }
848
849 etree_type *
850 exp_assop (code, dst, src)
851      int code;
852      CONST char *dst;
853      etree_type *src;
854 {
855   etree_type value, *new;
856
857   value.assign.type.node_code = code;
858
859   value.assign.src = src;
860   value.assign.dst = dst;
861   value.assign.type.node_class = etree_assign;
862
863 #if 0
864   if (exp_fold_tree_no_dot (&value, &result))
865     return exp_intop (result);
866 #endif
867   new = (etree_type *) stat_alloc (sizeof (new->assign));
868   memcpy ((char *) new, (char *) &value, sizeof (new->assign));
869   return new;
870 }
871
872 /* Handle PROVIDE.  */
873
874 etree_type *
875 exp_provide (dst, src)
876      const char *dst;
877      etree_type *src;
878 {
879   etree_type *n;
880
881   n = (etree_type *) stat_alloc (sizeof (n->assign));
882   n->assign.type.node_code = '=';
883   n->assign.type.node_class = etree_provide;
884   n->assign.src = src;
885   n->assign.dst = dst;
886   return n;
887 }
888
889 /* Handle ASSERT.  */
890
891 etree_type *
892 exp_assert (exp, message)
893      etree_type *exp;
894      const char *message;
895 {
896   etree_type *n;
897
898   n = (etree_type *) stat_alloc (sizeof (n->assert_s));
899   n->assert_s.type.node_code = '!';
900   n->assert_s.type.node_class = etree_assert;
901   n->assert_s.child = exp;
902   n->assert_s.message = message;
903   return n;
904 }
905
906 void
907 exp_print_tree (tree)
908      etree_type *tree;
909 {
910   if (config.map_file == NULL)
911     config.map_file = stderr;
912   
913   if (tree == NULL)
914     {
915       minfo ("NULL TREE\n");
916       return;
917     }
918   
919   switch (tree->type.node_class)
920     {
921     case etree_value:
922       minfo ("0x%v", tree->value.value);
923       return;
924     case etree_rel:
925       if (tree->rel.section->owner != NULL)
926         minfo ("%B:", tree->rel.section->owner);
927       minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
928       return;
929     case etree_assign:
930 #if 0
931       if (tree->assign.dst->sdefs != (asymbol *) NULL)
932         fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
933                  tree->assign.dst->sdefs->value);
934       else
935         fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
936 #endif
937       fprintf (config.map_file, "%s", tree->assign.dst);
938       exp_print_token (tree->type.node_code);
939       exp_print_tree (tree->assign.src);
940       break;
941     case etree_provide:
942     case etree_provided:
943       fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
944       exp_print_tree (tree->assign.src);
945       fprintf (config.map_file, ")");
946       break;
947     case etree_binary:
948       fprintf (config.map_file, "(");
949       exp_print_tree (tree->binary.lhs);
950       exp_print_token (tree->type.node_code);
951       exp_print_tree (tree->binary.rhs);
952       fprintf (config.map_file, ")");
953       break;
954     case etree_trinary:
955       exp_print_tree (tree->trinary.cond);
956       fprintf (config.map_file, "?");
957       exp_print_tree (tree->trinary.lhs);
958       fprintf (config.map_file, ":");
959       exp_print_tree (tree->trinary.rhs);
960       break;
961     case etree_unary:
962       exp_print_token (tree->unary.type.node_code);
963       if (tree->unary.child)
964         {
965           fprintf (config.map_file, "(");
966           exp_print_tree (tree->unary.child);
967           fprintf (config.map_file, ")");
968         }
969       break;
970
971     case etree_assert:
972       fprintf (config.map_file, "ASSERT (");
973       exp_print_tree (tree->assert_s.child);
974       fprintf (config.map_file, ", %s)", tree->assert_s.message);
975       break;
976
977     case etree_undef:
978       fprintf (config.map_file, "????????");
979       break;
980     case etree_name:
981       if (tree->type.node_code == NAME)
982         {
983           fprintf (config.map_file, "%s", tree->name.name);
984         }
985       else
986         {
987           exp_print_token (tree->type.node_code);
988           if (tree->name.name)
989             fprintf (config.map_file, "(%s)", tree->name.name);
990         }
991       break;
992     default:
993       FAIL ();
994       break;
995     }
996 }
997
998 bfd_vma
999 exp_get_vma (tree, def, name, allocation_done)
1000      etree_type *tree;
1001      bfd_vma def;
1002      char *name;
1003      lang_phase_type allocation_done;
1004 {
1005   etree_value_type r;
1006
1007   if (tree != NULL)
1008     {
1009       r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1010       if (! r.valid_p && name != NULL)
1011         einfo (_("%F%S nonconstant expression for %s\n"), name);
1012       return r.value;
1013     }
1014   else
1015     return def;
1016 }
1017
1018 int
1019 exp_get_value_int (tree, def, name, allocation_done)
1020      etree_type *tree;
1021      int def;
1022      char *name;
1023      lang_phase_type allocation_done;
1024 {
1025   return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1026 }
1027
1028 bfd_vma
1029 exp_get_abs_int (tree, def, name, allocation_done)
1030      etree_type *tree;
1031      int def ATTRIBUTE_UNUSED;
1032      char *name;
1033      lang_phase_type allocation_done;
1034 {
1035   etree_value_type res;
1036   res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1037
1038   if (res.valid_p)
1039     res.value += res.section->bfd_section->vma;
1040   else
1041     einfo (_("%F%S non constant expression for %s\n"), name);
1042
1043   return res.value;
1044 }