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