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