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