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