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