Merge branch 'vendor/DIFFUTILS'
[dragonfly.git] / contrib / binutils-2.25 / ld / ldexp.c
1 /* This module handles expression trees.
2    Copyright (C) 1991-2014 Free Software Foundation, Inc.
3    Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
4
5    This file is part of the GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22
23 /* This module is in charge of working out the contents of expressions.
24
25    It has to keep track of the relative/absness of a symbol etc. This
26    is done by keeping all values in a struct (an etree_value_type)
27    which contains a value, a section to which it is relative and a
28    valid bit.  */
29
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "bfdlink.h"
33
34 #include "ld.h"
35 #include "ldmain.h"
36 #include "ldmisc.h"
37 #include "ldexp.h"
38 #include "ldlex.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 bfd_vma align_n (bfd_vma, bfd_vma);
46
47 segment_type *segments;
48
49 struct ldexp_control expld;
50
51 /* This structure records symbols for which we need to keep track of
52    definedness for use in the DEFINED () test.  */
53
54 struct definedness_hash_entry
55 {
56   struct bfd_hash_entry root;
57   unsigned int by_object : 1;
58   unsigned int by_script : 1;
59   unsigned int iteration : 1;
60 };
61
62 static struct bfd_hash_table definedness_table;
63
64 /* Print the string representation of the given token.  Surround it
65    with spaces if INFIX_P is TRUE.  */
66
67 static void
68 exp_print_token (token_code_type code, int infix_p)
69 {
70   static const struct
71   {
72     token_code_type code;
73     const char * name;
74   }
75   table[] =
76   {
77     { INT, "int" },
78     { NAME, "NAME" },
79     { PLUSEQ, "+=" },
80     { MINUSEQ, "-=" },
81     { MULTEQ, "*=" },
82     { DIVEQ, "/=" },
83     { LSHIFTEQ, "<<=" },
84     { RSHIFTEQ, ">>=" },
85     { ANDEQ, "&=" },
86     { OREQ, "|=" },
87     { OROR, "||" },
88     { ANDAND, "&&" },
89     { EQ, "==" },
90     { NE, "!=" },
91     { LE, "<=" },
92     { GE, ">=" },
93     { LSHIFT, "<<" },
94     { RSHIFT, ">>" },
95     { LOG2CEIL, "LOG2CEIL" },
96     { ALIGN_K, "ALIGN" },
97     { BLOCK, "BLOCK" },
98     { QUAD, "QUAD" },
99     { SQUAD, "SQUAD" },
100     { LONG, "LONG" },
101     { SHORT, "SHORT" },
102     { BYTE, "BYTE" },
103     { SECTIONS, "SECTIONS" },
104     { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
105     { MEMORY, "MEMORY" },
106     { DEFINED, "DEFINED" },
107     { TARGET_K, "TARGET" },
108     { SEARCH_DIR, "SEARCH_DIR" },
109     { MAP, "MAP" },
110     { ENTRY, "ENTRY" },
111     { NEXT, "NEXT" },
112     { ALIGNOF, "ALIGNOF" },
113     { SIZEOF, "SIZEOF" },
114     { ADDR, "ADDR" },
115     { LOADADDR, "LOADADDR" },
116     { CONSTANT, "CONSTANT" },
117     { ABSOLUTE, "ABSOLUTE" },
118     { MAX_K, "MAX" },
119     { MIN_K, "MIN" },
120     { ASSERT_K, "ASSERT" },
121     { REL, "relocatable" },
122     { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
123     { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
124     { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
125     { ORIGIN, "ORIGIN" },
126     { LENGTH, "LENGTH" },
127     { SEGMENT_START, "SEGMENT_START" }
128   };
129   unsigned int idx;
130
131   for (idx = 0; idx < ARRAY_SIZE (table); idx++)
132     if (table[idx].code == code)
133       break;
134
135   if (infix_p)
136     fputc (' ', config.map_file);
137
138   if (idx < ARRAY_SIZE (table))
139     fputs (table[idx].name, config.map_file);
140   else if (code < 127)
141     fputc (code, config.map_file);
142   else
143     fprintf (config.map_file, "<code %d>", code);
144
145   if (infix_p)
146     fputc (' ', config.map_file);
147 }
148
149 static void
150 make_log2ceil (void)
151 {
152   bfd_vma value = expld.result.value;
153   bfd_vma result = -1;
154   bfd_boolean round_up = FALSE;
155
156   do
157     {
158       result++;
159       /* If more than one bit is set in the value we will need to round up.  */
160       if ((value > 1) && (value & 1))
161         round_up = TRUE;
162     }
163   while (value >>= 1);
164
165   if (round_up)
166     result += 1;
167   expld.result.section = NULL;
168   expld.result.value = result;
169 }
170
171 static void
172 make_abs (void)
173 {
174   if (expld.result.section != NULL)
175     expld.result.value += expld.result.section->vma;
176   expld.result.section = bfd_abs_section_ptr;
177 }
178
179 static void
180 new_abs (bfd_vma value)
181 {
182   expld.result.valid_p = TRUE;
183   expld.result.section = bfd_abs_section_ptr;
184   expld.result.value = value;
185   expld.result.str = NULL;
186 }
187
188 etree_type *
189 exp_intop (bfd_vma value)
190 {
191   etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
192   new_e->type.node_code = INT;
193   new_e->type.filename = ldlex_filename ();
194   new_e->type.lineno = lineno;
195   new_e->value.value = value;
196   new_e->value.str = NULL;
197   new_e->type.node_class = etree_value;
198   return new_e;
199 }
200
201 etree_type *
202 exp_bigintop (bfd_vma value, char *str)
203 {
204   etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
205   new_e->type.node_code = INT;
206   new_e->type.filename = ldlex_filename ();
207   new_e->type.lineno = lineno;
208   new_e->value.value = value;
209   new_e->value.str = str;
210   new_e->type.node_class = etree_value;
211   return new_e;
212 }
213
214 /* Build an expression representing an unnamed relocatable value.  */
215
216 etree_type *
217 exp_relop (asection *section, bfd_vma value)
218 {
219   etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
220   new_e->type.node_code = REL;
221   new_e->type.filename = ldlex_filename ();
222   new_e->type.lineno = lineno;
223   new_e->type.node_class = etree_rel;
224   new_e->rel.section = section;
225   new_e->rel.value = value;
226   return new_e;
227 }
228
229 static void
230 new_number (bfd_vma value)
231 {
232   expld.result.valid_p = TRUE;
233   expld.result.value = value;
234   expld.result.str = NULL;
235   expld.result.section = NULL;
236 }
237
238 static void
239 new_rel (bfd_vma value, asection *section)
240 {
241   expld.result.valid_p = TRUE;
242   expld.result.value = value;
243   expld.result.str = NULL;
244   expld.result.section = section;
245 }
246
247 static void
248 new_rel_from_abs (bfd_vma value)
249 {
250   asection *s = expld.section;
251
252   if (s == bfd_abs_section_ptr && expld.phase == lang_final_phase_enum)
253     s = section_for_dot ();
254   expld.result.valid_p = TRUE;
255   expld.result.value = value - s->vma;
256   expld.result.str = NULL;
257   expld.result.section = s;
258 }
259
260 /* New-function for the definedness hash table.  */
261
262 static struct bfd_hash_entry *
263 definedness_newfunc (struct bfd_hash_entry *entry,
264                      struct bfd_hash_table *table ATTRIBUTE_UNUSED,
265                      const char *name ATTRIBUTE_UNUSED)
266 {
267   struct definedness_hash_entry *ret = (struct definedness_hash_entry *) entry;
268
269   if (ret == NULL)
270     ret = (struct definedness_hash_entry *)
271       bfd_hash_allocate (table, sizeof (struct definedness_hash_entry));
272
273   if (ret == NULL)
274     einfo (_("%P%F: bfd_hash_allocate failed creating symbol %s\n"), name);
275
276   ret->by_object = 0;
277   ret->by_script = 0;
278   ret->iteration = 0;
279   return &ret->root;
280 }
281
282 /* Called during processing of linker script script expressions.
283    For symbols assigned in a linker script, return a struct describing
284    where the symbol is defined relative to the current expression,
285    otherwise return NULL.  */
286
287 static struct definedness_hash_entry *
288 symbol_defined (const char *name)
289 {
290   return ((struct definedness_hash_entry *)
291           bfd_hash_lookup (&definedness_table, name, FALSE, FALSE));
292 }
293
294 /* Update the definedness state of NAME.  */
295
296 static void
297 update_definedness (const char *name, struct bfd_link_hash_entry *h)
298 {
299   struct definedness_hash_entry *defentry
300     = (struct definedness_hash_entry *)
301     bfd_hash_lookup (&definedness_table, name, TRUE, FALSE);
302
303   if (defentry == NULL)
304     einfo (_("%P%F: bfd_hash_lookup failed creating symbol %s\n"), name);
305
306   /* If the symbol was already defined, and not by a script, then it
307      must be defined by an object file or by the linker target code.  */
308   if (!defentry->by_script
309       && (h->type == bfd_link_hash_defined
310           || h->type == bfd_link_hash_defweak
311           || h->type == bfd_link_hash_common))
312     defentry->by_object = 1;
313
314   defentry->by_script = 1;
315   defentry->iteration = lang_statement_iteration;
316 }
317
318 static void
319 fold_unary (etree_type *tree)
320 {
321   exp_fold_tree_1 (tree->unary.child);
322   if (expld.result.valid_p)
323     {
324       switch (tree->type.node_code)
325         {
326         case ALIGN_K:
327           if (expld.phase != lang_first_phase_enum)
328             new_rel_from_abs (align_n (expld.dot, expld.result.value));
329           else
330             expld.result.valid_p = FALSE;
331           break;
332
333         case ABSOLUTE:
334           make_abs ();
335           break;
336
337         case LOG2CEIL:
338           make_log2ceil ();
339           break;
340
341         case '~':
342           expld.result.value = ~expld.result.value;
343           break;
344
345         case '!':
346           expld.result.value = !expld.result.value;
347           break;
348
349         case '-':
350           expld.result.value = -expld.result.value;
351           break;
352
353         case NEXT:
354           /* Return next place aligned to value.  */
355           if (expld.phase != lang_first_phase_enum)
356             {
357               make_abs ();
358               expld.result.value = align_n (expld.dot, expld.result.value);
359             }
360           else
361             expld.result.valid_p = FALSE;
362           break;
363
364         case DATA_SEGMENT_END:
365           if (expld.phase == lang_first_phase_enum
366               || expld.section != bfd_abs_section_ptr)
367             {
368               expld.result.valid_p = FALSE;
369             }
370           else if (expld.dataseg.phase == exp_dataseg_align_seen
371                    || expld.dataseg.phase == exp_dataseg_relro_seen)
372             {
373               expld.dataseg.phase = exp_dataseg_end_seen;
374               expld.dataseg.end = expld.result.value;
375             }
376           else if (expld.dataseg.phase == exp_dataseg_done
377                    || expld.dataseg.phase == exp_dataseg_adjust
378                    || expld.dataseg.phase == exp_dataseg_relro_adjust)
379             {
380               /* OK.  */
381             }
382           else
383             expld.result.valid_p = FALSE;
384           break;
385
386         default:
387           FAIL ();
388           break;
389         }
390     }
391 }
392
393 static void
394 fold_binary (etree_type *tree)
395 {
396   etree_value_type lhs;
397   exp_fold_tree_1 (tree->binary.lhs);
398
399   /* The SEGMENT_START operator is special because its first
400      operand is a string, not the name of a symbol.  Note that the
401      operands have been swapped, so binary.lhs is second (default)
402      operand, binary.rhs is first operand.  */
403   if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
404     {
405       const char *segment_name;
406       segment_type *seg;
407
408       /* Check to see if the user has overridden the default
409          value.  */
410       segment_name = tree->binary.rhs->name.name;
411       for (seg = segments; seg; seg = seg->next)
412         if (strcmp (seg->name, segment_name) == 0)
413           {
414             if (!seg->used
415                 && config.magic_demand_paged
416                 && (seg->value % config.maxpagesize) != 0)
417               einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
418                      segment_name);
419             seg->used = TRUE;
420             new_rel_from_abs (seg->value);
421             break;
422           }
423       return;
424     }
425
426   lhs = expld.result;
427   exp_fold_tree_1 (tree->binary.rhs);
428   expld.result.valid_p &= lhs.valid_p;
429
430   if (expld.result.valid_p)
431     {
432       if (lhs.section != expld.result.section)
433         {
434           /* If the values are from different sections, and neither is
435              just a number, make both the source arguments absolute.  */
436           if (expld.result.section != NULL
437               && lhs.section != NULL)
438             {
439               make_abs ();
440               lhs.value += lhs.section->vma;
441               lhs.section = bfd_abs_section_ptr;
442             }
443
444           /* If the rhs is just a number, keep the lhs section.  */
445           else if (expld.result.section == NULL)
446             {
447               expld.result.section = lhs.section;
448               /* Make this NULL so that we know one of the operands
449                  was just a number, for later tests.  */
450               lhs.section = NULL;
451             }
452         }
453       /* At this point we know that both operands have the same
454          section, or at least one of them is a plain number.  */
455
456       switch (tree->type.node_code)
457         {
458           /* Arithmetic operators, bitwise AND, bitwise OR and XOR
459              keep the section of one of their operands only when the
460              other operand is a plain number.  Losing the section when
461              operating on two symbols, ie. a result of a plain number,
462              is required for subtraction and XOR.  It's justifiable
463              for the other operations on the grounds that adding,
464              multiplying etc. two section relative values does not
465              really make sense unless they are just treated as
466              numbers.
467              The same argument could be made for many expressions
468              involving one symbol and a number.  For example,
469              "1 << x" and "100 / x" probably should not be given the
470              section of x.  The trouble is that if we fuss about such
471              things the rules become complex and it is onerous to
472              document ld expression evaluation.  */
473 #define BOP(x, y) \
474         case x:                                                 \
475           expld.result.value = lhs.value y expld.result.value;  \
476           if (expld.result.section == lhs.section)              \
477             expld.result.section = NULL;                        \
478           break;
479
480           /* Comparison operators, logical AND, and logical OR always
481              return a plain number.  */
482 #define BOPN(x, y) \
483         case x:                                                 \
484           expld.result.value = lhs.value y expld.result.value;  \
485           expld.result.section = NULL;                          \
486           break;
487
488           BOP ('+', +);
489           BOP ('*', *);
490           BOP ('-', -);
491           BOP (LSHIFT, <<);
492           BOP (RSHIFT, >>);
493           BOP ('&', &);
494           BOP ('^', ^);
495           BOP ('|', |);
496           BOPN (EQ, ==);
497           BOPN (NE, !=);
498           BOPN ('<', <);
499           BOPN ('>', >);
500           BOPN (LE, <=);
501           BOPN (GE, >=);
502           BOPN (ANDAND, &&);
503           BOPN (OROR, ||);
504
505         case '%':
506           if (expld.result.value != 0)
507             expld.result.value = ((bfd_signed_vma) lhs.value
508                                   % (bfd_signed_vma) expld.result.value);
509           else if (expld.phase != lang_mark_phase_enum)
510             einfo (_("%F%S %% by zero\n"), tree->binary.rhs);
511           if (expld.result.section == lhs.section)
512             expld.result.section = NULL;
513           break;
514
515         case '/':
516           if (expld.result.value != 0)
517             expld.result.value = ((bfd_signed_vma) lhs.value
518                                   / (bfd_signed_vma) expld.result.value);
519           else if (expld.phase != lang_mark_phase_enum)
520             einfo (_("%F%S / by zero\n"), tree->binary.rhs);
521           if (expld.result.section == lhs.section)
522             expld.result.section = NULL;
523           break;
524
525         case MAX_K:
526           if (lhs.value > expld.result.value)
527             expld.result.value = lhs.value;
528           break;
529
530         case MIN_K:
531           if (lhs.value < expld.result.value)
532             expld.result.value = lhs.value;
533           break;
534
535         case ALIGN_K:
536           expld.result.value = align_n (lhs.value, expld.result.value);
537           break;
538
539         case DATA_SEGMENT_ALIGN:
540           expld.dataseg.relro = exp_dataseg_relro_start;
541           if (expld.phase == lang_first_phase_enum
542               || expld.section != bfd_abs_section_ptr)
543             expld.result.valid_p = FALSE;
544           else
545             {
546               bfd_vma maxpage = lhs.value;
547               bfd_vma commonpage = expld.result.value;
548
549               expld.result.value = align_n (expld.dot, maxpage);
550               if (expld.dataseg.phase == exp_dataseg_relro_adjust)
551                 expld.result.value = expld.dataseg.base;
552               else if (expld.dataseg.phase == exp_dataseg_adjust)
553                 {
554                   if (commonpage < maxpage)
555                     expld.result.value += ((expld.dot + commonpage - 1)
556                                            & (maxpage - commonpage));
557                 }
558               else
559                 {
560                   expld.result.value += expld.dot & (maxpage - 1);
561                   if (expld.dataseg.phase == exp_dataseg_done)
562                     {
563                       /* OK.  */
564                     }
565                   else if (expld.dataseg.phase == exp_dataseg_none)
566                     {
567                       expld.dataseg.phase = exp_dataseg_align_seen;
568                       expld.dataseg.base = expld.result.value;
569                       expld.dataseg.pagesize = commonpage;
570                       expld.dataseg.maxpagesize = maxpage;
571                       expld.dataseg.relro_end = 0;
572                     }
573                   else
574                     expld.result.valid_p = FALSE;
575                 }
576             }
577           break;
578
579         case DATA_SEGMENT_RELRO_END:
580           expld.dataseg.relro = exp_dataseg_relro_end;
581           expld.dataseg.relro_offset = expld.result.value;
582           if (expld.phase == lang_first_phase_enum
583               || expld.section != bfd_abs_section_ptr)
584             expld.result.valid_p = FALSE;
585           else if (expld.dataseg.phase == exp_dataseg_align_seen
586                    || expld.dataseg.phase == exp_dataseg_adjust
587                    || expld.dataseg.phase == exp_dataseg_relro_adjust
588                    || expld.dataseg.phase == exp_dataseg_done)
589             {
590               if (expld.dataseg.phase == exp_dataseg_align_seen
591                   || expld.dataseg.phase == exp_dataseg_relro_adjust)
592                 expld.dataseg.relro_end = lhs.value + expld.result.value;
593
594               if (expld.dataseg.phase == exp_dataseg_relro_adjust
595                   && (expld.dataseg.relro_end
596                       & (expld.dataseg.pagesize - 1)))
597                 {
598                   expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
599                   expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
600                   expld.result.value = (expld.dataseg.relro_end
601                                         - expld.result.value);
602                 }
603               else
604                 expld.result.value = lhs.value;
605
606               if (expld.dataseg.phase == exp_dataseg_align_seen)
607                 expld.dataseg.phase = exp_dataseg_relro_seen;
608             }
609           else
610             expld.result.valid_p = FALSE;
611           break;
612
613         default:
614           FAIL ();
615         }
616     }
617 }
618
619 static void
620 fold_trinary (etree_type *tree)
621 {
622   exp_fold_tree_1 (tree->trinary.cond);
623   if (expld.result.valid_p)
624     exp_fold_tree_1 (expld.result.value
625                      ? tree->trinary.lhs
626                      : tree->trinary.rhs);
627 }
628
629 static void
630 fold_name (etree_type *tree)
631 {
632   memset (&expld.result, 0, sizeof (expld.result));
633
634   switch (tree->type.node_code)
635     {
636     case SIZEOF_HEADERS:
637       if (expld.phase != lang_first_phase_enum)
638         {
639           bfd_vma hdr_size = 0;
640           /* Don't find the real header size if only marking sections;
641              The bfd function may cache incorrect data.  */
642           if (expld.phase != lang_mark_phase_enum)
643             hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
644           new_number (hdr_size);
645         }
646       break;
647
648     case DEFINED:
649       if (expld.phase != lang_first_phase_enum)
650         {
651           struct bfd_link_hash_entry *h;
652           struct definedness_hash_entry *def;
653
654           h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
655                                             &link_info,
656                                             tree->name.name,
657                                             FALSE, FALSE, TRUE);
658           new_number (h != NULL
659                       && (h->type == bfd_link_hash_defined
660                           || h->type == bfd_link_hash_defweak
661                           || h->type == bfd_link_hash_common)
662                       && ((def = symbol_defined (tree->name.name)) == NULL
663                           || def->by_object
664                           || def->iteration == (lang_statement_iteration & 1)));
665         }
666       break;
667
668     case NAME:
669       if (expld.assign_name != NULL
670           && strcmp (expld.assign_name, tree->name.name) == 0)
671         {
672           /* Self-assignment is only allowed for absolute symbols
673              defined in a linker script.  */
674           struct bfd_link_hash_entry *h;
675           struct definedness_hash_entry *def;
676
677           h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
678                                             &link_info,
679                                             tree->name.name,
680                                             FALSE, FALSE, TRUE);
681           if (!(h != NULL
682                 && (h->type == bfd_link_hash_defined
683                     || h->type == bfd_link_hash_defweak)
684                 && h->u.def.section == bfd_abs_section_ptr
685                 && (def = symbol_defined (tree->name.name)) != NULL
686                 && def->iteration == (lang_statement_iteration & 1)))
687             expld.assign_name = NULL;
688         }
689       if (expld.phase == lang_first_phase_enum)
690         ;
691       else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
692         new_rel_from_abs (expld.dot);
693       else
694         {
695           struct bfd_link_hash_entry *h;
696
697           h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
698                                             &link_info,
699                                             tree->name.name,
700                                             TRUE, FALSE, TRUE);
701           if (!h)
702             einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
703           else if (h->type == bfd_link_hash_defined
704                    || h->type == bfd_link_hash_defweak)
705             {
706               asection *output_section;
707
708               output_section = h->u.def.section->output_section;
709               if (output_section == NULL)
710                 {
711                   if (expld.phase == lang_mark_phase_enum)
712                     new_rel (h->u.def.value, h->u.def.section);
713                   else
714                     einfo (_("%X%S: unresolvable symbol `%s'"
715                              " referenced in expression\n"),
716                            tree, tree->name.name);
717                 }
718               else if (output_section == bfd_abs_section_ptr
719                        && (expld.section != bfd_abs_section_ptr
720                            || config.sane_expr))
721                 new_number (h->u.def.value + h->u.def.section->output_offset);
722               else
723                 new_rel (h->u.def.value + h->u.def.section->output_offset,
724                          output_section);
725             }
726           else if (expld.phase == lang_final_phase_enum
727                    || (expld.phase != lang_mark_phase_enum
728                        && expld.assigning_to_dot))
729             einfo (_("%F%S: undefined symbol `%s'"
730                      " referenced in expression\n"),
731                    tree, tree->name.name);
732           else if (h->type == bfd_link_hash_new)
733             {
734               h->type = bfd_link_hash_undefined;
735               h->u.undef.abfd = NULL;
736               if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
737                 bfd_link_add_undef (link_info.hash, h);
738             }
739         }
740       break;
741
742     case ADDR:
743       if (expld.phase != lang_first_phase_enum)
744         {
745           lang_output_section_statement_type *os;
746
747           os = lang_output_section_find (tree->name.name);
748           if (os == NULL)
749             {
750               if (expld.phase == lang_final_phase_enum)
751                 einfo (_("%F%S: undefined section `%s'"
752                          " referenced in expression\n"),
753                        tree, tree->name.name);
754             }
755           else if (os->processed_vma)
756             new_rel (0, os->bfd_section);
757         }
758       break;
759
760     case LOADADDR:
761       if (expld.phase != lang_first_phase_enum)
762         {
763           lang_output_section_statement_type *os;
764
765           os = lang_output_section_find (tree->name.name);
766           if (os == NULL)
767             {
768               if (expld.phase == lang_final_phase_enum)
769                 einfo (_("%F%S: undefined section `%s'"
770                          " referenced in expression\n"),
771                        tree, tree->name.name);
772             }
773           else if (os->processed_lma)
774             {
775               if (os->load_base == NULL)
776                 new_abs (os->bfd_section->lma);
777               else
778                 {
779                   exp_fold_tree_1 (os->load_base);
780                   if (expld.result.valid_p)
781                     make_abs ();
782                 }
783             }
784         }
785       break;
786
787     case SIZEOF:
788     case ALIGNOF:
789       if (expld.phase != lang_first_phase_enum)
790         {
791           lang_output_section_statement_type *os;
792
793           os = lang_output_section_find (tree->name.name);
794           if (os == NULL)
795             {
796               if (expld.phase == lang_final_phase_enum)
797                 einfo (_("%F%S: undefined section `%s'"
798                          " referenced in expression\n"),
799                        tree, tree->name.name);
800               new_number (0);
801             }
802           else if (os->bfd_section != NULL)
803             {
804               bfd_vma val;
805
806               if (tree->type.node_code == SIZEOF)
807                 val = (os->bfd_section->size
808                        / bfd_octets_per_byte (link_info.output_bfd));
809               else
810                 val = (bfd_vma)1 << os->bfd_section->alignment_power;
811
812               new_number (val);
813             }
814           else
815             new_number (0);
816         }
817       break;
818
819     case LENGTH:
820       {
821         lang_memory_region_type *mem;
822
823         mem = lang_memory_region_lookup (tree->name.name, FALSE);
824         if (mem != NULL)
825           new_number (mem->length);
826         else
827           einfo (_("%F%S: undefined MEMORY region `%s'"
828                    " referenced in expression\n"),
829                  tree, tree->name.name);
830       }
831       break;
832
833     case ORIGIN:
834       if (expld.phase != lang_first_phase_enum)
835         {
836           lang_memory_region_type *mem;
837
838           mem = lang_memory_region_lookup (tree->name.name, FALSE);
839           if (mem != NULL)
840             new_rel_from_abs (mem->origin);
841           else
842             einfo (_("%F%S: undefined MEMORY region `%s'"
843                      " referenced in expression\n"),
844                    tree, tree->name.name);
845         }
846       break;
847
848     case CONSTANT:
849       if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
850         new_number (config.maxpagesize);
851       else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
852         new_number (config.commonpagesize);
853       else
854         einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
855                tree, tree->name.name);
856       break;
857
858     default:
859       FAIL ();
860       break;
861     }
862 }
863
864 /* Return true if TREE is '.'.  */
865  
866 static bfd_boolean
867 is_dot (const etree_type *tree)
868 {
869   return (tree->type.node_class == etree_name
870           && tree->type.node_code == NAME
871           && tree->name.name[0] == '.'
872           && tree->name.name[1] == 0);
873 }
874
875 /* Return true if TREE is a constant equal to VAL.  */
876
877 static bfd_boolean
878 is_value (const etree_type *tree, bfd_vma val)
879 {
880   return (tree->type.node_class == etree_value
881           && tree->value.value == val);
882 }
883
884 /* Return true if TREE is an absolute symbol equal to VAL defined in
885    a linker script.  */
886
887 static bfd_boolean
888 is_sym_value (const etree_type *tree, bfd_vma val)
889 {
890   struct bfd_link_hash_entry *h;
891   struct definedness_hash_entry *def;
892
893   return (tree->type.node_class == etree_name
894           && tree->type.node_code == NAME
895           && (def = symbol_defined (tree->name.name)) != NULL
896           && def->by_script
897           && def->iteration == (lang_statement_iteration & 1)
898           && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
899                                                 &link_info,
900                                                 tree->name.name,
901                                                 FALSE, FALSE, TRUE)) != NULL
902           && h->type == bfd_link_hash_defined
903           && h->u.def.section == bfd_abs_section_ptr
904           && h->u.def.value == val);
905 }
906
907 /* Return true if TREE is ". != 0".  */
908
909 static bfd_boolean
910 is_dot_ne_0 (const etree_type *tree)
911 {
912   return (tree->type.node_class == etree_binary
913           && tree->type.node_code == NE
914           && is_dot (tree->binary.lhs)
915           && is_value (tree->binary.rhs, 0));
916 }
917
918 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
919    absolute constant with value 0 defined in a linker script.  */
920
921 static bfd_boolean
922 is_dot_plus_0 (const etree_type *tree)
923 {
924   return (tree->type.node_class == etree_binary
925           && tree->type.node_code == '+'
926           && is_dot (tree->binary.lhs)
927           && (is_value (tree->binary.rhs, 0)
928               || is_sym_value (tree->binary.rhs, 0)));
929 }
930
931 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)".  */
932
933 static bfd_boolean
934 is_align_conditional (const etree_type *tree)
935 {
936   if (tree->type.node_class == etree_unary
937       && tree->type.node_code == ALIGN_K)
938     {
939       tree = tree->unary.child;
940       return (tree->type.node_class == etree_trinary
941               && is_dot_ne_0 (tree->trinary.cond)
942               && is_value (tree->trinary.rhs, 1));
943     }
944   return 0;
945 }
946
947 static void
948 exp_fold_tree_1 (etree_type *tree)
949 {
950   if (tree == NULL)
951     {
952       memset (&expld.result, 0, sizeof (expld.result));
953       return;
954     }
955
956   switch (tree->type.node_class)
957     {
958     case etree_value:
959       if (expld.section == bfd_abs_section_ptr
960           && !config.sane_expr)
961         new_abs (tree->value.value);
962       else
963         new_number (tree->value.value);
964       expld.result.str = tree->value.str;
965       break;
966
967     case etree_rel:
968       if (expld.phase != lang_first_phase_enum)
969         {
970           asection *output_section = tree->rel.section->output_section;
971           new_rel (tree->rel.value + tree->rel.section->output_offset,
972                    output_section);
973         }
974       else
975         memset (&expld.result, 0, sizeof (expld.result));
976       break;
977
978     case etree_assert:
979       exp_fold_tree_1 (tree->assert_s.child);
980       if (expld.phase == lang_final_phase_enum && !expld.result.value)
981         einfo ("%X%P: %s\n", tree->assert_s.message);
982       break;
983
984     case etree_unary:
985       fold_unary (tree);
986       break;
987
988     case etree_binary:
989       fold_binary (tree);
990       break;
991
992     case etree_trinary:
993       fold_trinary (tree);
994       break;
995
996     case etree_assign:
997     case etree_provide:
998     case etree_provided:
999       if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
1000         {
1001           if (tree->type.node_class != etree_assign)
1002             einfo (_("%F%S can not PROVIDE assignment to"
1003                      " location counter\n"), tree);
1004           if (expld.phase != lang_first_phase_enum)
1005             {
1006               /* Notify the folder that this is an assignment to dot.  */
1007               expld.assigning_to_dot = TRUE;
1008               exp_fold_tree_1 (tree->assign.src);
1009               expld.assigning_to_dot = FALSE;
1010
1011               /* If we are assigning to dot inside an output section
1012                  arrange to keep the section, except for certain
1013                  expressions that evaluate to zero.  We ignore . = 0,
1014                  . = . + 0, and . = ALIGN (. != 0 ? expr : 1).  */
1015               if (expld.phase == lang_mark_phase_enum
1016                   && expld.section != bfd_abs_section_ptr
1017                   && !(expld.result.valid_p
1018                        && expld.result.value == 0
1019                        && (is_value (tree->assign.src, 0)
1020                            || is_sym_value (tree->assign.src, 0)
1021                            || is_dot_plus_0 (tree->assign.src)
1022                            || is_align_conditional (tree->assign.src))))
1023                 expld.section->flags |= SEC_KEEP;
1024
1025               if (!expld.result.valid_p)
1026                 {
1027                   if (expld.phase != lang_mark_phase_enum)
1028                     einfo (_("%F%S invalid assignment to"
1029                              " location counter\n"), tree);
1030                 }
1031               else if (expld.dotp == NULL)
1032                 einfo (_("%F%S assignment to location counter"
1033                          " invalid outside of SECTIONS\n"), tree);
1034
1035               /* After allocation, assignment to dot should not be
1036                  done inside an output section since allocation adds a
1037                  padding statement that effectively duplicates the
1038                  assignment.  */
1039               else if (expld.phase <= lang_allocating_phase_enum
1040                        || expld.section == bfd_abs_section_ptr)
1041                 {
1042                   bfd_vma nextdot;
1043
1044                   nextdot = expld.result.value;
1045                   if (expld.result.section != NULL)
1046                     nextdot += expld.result.section->vma;
1047                   else
1048                     nextdot += expld.section->vma;
1049                   if (nextdot < expld.dot
1050                       && expld.section != bfd_abs_section_ptr)
1051                     einfo (_("%F%S cannot move location counter backwards"
1052                              " (from %V to %V)\n"),
1053                            tree, expld.dot, nextdot);
1054                   else
1055                     {
1056                       expld.dot = nextdot;
1057                       *expld.dotp = nextdot;
1058                     }
1059                 }
1060             }
1061           else
1062             memset (&expld.result, 0, sizeof (expld.result));
1063         }
1064       else
1065         {
1066           struct bfd_link_hash_entry *h = NULL;
1067
1068           if (tree->type.node_class == etree_provide)
1069             {
1070               h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1071                                         FALSE, FALSE, TRUE);
1072               if (h == NULL
1073                   || !(h->type == bfd_link_hash_new
1074                        || h->type == bfd_link_hash_undefined
1075                        || h->linker_def))
1076                 {
1077                   /* Do nothing.  The symbol was never referenced, or
1078                      was defined in some object file.  Undefined weak
1079                      symbols stay undefined.  */
1080                   break;
1081                 }
1082             }
1083
1084           expld.assign_name = tree->assign.dst;
1085           exp_fold_tree_1 (tree->assign.src);
1086           /* expld.assign_name remaining equal to tree->assign.dst
1087              below indicates the evaluation of tree->assign.src did
1088              not use the value of tree->assign.dst.  We don't allow
1089              self assignment until the final phase for two reasons:
1090              1) Expressions are evaluated multiple times.  With
1091              relaxation, the number of times may vary.
1092              2) Section relative symbol values cannot be correctly
1093              converted to absolute values, as is required by many
1094              expressions, until final section sizing is complete.  */
1095           if ((expld.result.valid_p
1096                && (expld.phase == lang_final_phase_enum
1097                    || expld.assign_name != NULL))
1098               || (expld.phase <= lang_mark_phase_enum
1099                   && tree->type.node_class == etree_assign
1100                   && tree->assign.defsym))
1101             {
1102               if (h == NULL)
1103                 {
1104                   h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1105                                             TRUE, FALSE, TRUE);
1106                   if (h == NULL)
1107                     einfo (_("%P%F:%s: hash creation failed\n"),
1108                            tree->assign.dst);
1109                 }
1110
1111               /* FIXME: Should we worry if the symbol is already
1112                  defined?  */
1113               update_definedness (tree->assign.dst, h);
1114               h->type = bfd_link_hash_defined;
1115               h->u.def.value = expld.result.value;
1116               if (expld.result.section == NULL)
1117                 expld.result.section = expld.section;
1118               h->u.def.section = expld.result.section;
1119               if (tree->type.node_class == etree_provide)
1120                 tree->type.node_class = etree_provided;
1121
1122               /* Copy the symbol type if this is a simple assignment of
1123                  one symbol to another.  This could be more general
1124                  (e.g. a ?: operator with NAMEs in each branch).  */
1125               if (tree->assign.src->type.node_class == etree_name)
1126                 {
1127                   struct bfd_link_hash_entry *hsrc;
1128
1129                   hsrc = bfd_link_hash_lookup (link_info.hash,
1130                                                tree->assign.src->name.name,
1131                                                FALSE, FALSE, TRUE);
1132                   if (hsrc)
1133                     bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
1134                                                     hsrc);
1135                 }
1136             }
1137           else if (expld.phase == lang_final_phase_enum)
1138             {
1139               h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1140                                         FALSE, FALSE, TRUE);
1141               if (h != NULL
1142                   && h->type == bfd_link_hash_new)
1143                 h->type = bfd_link_hash_undefined;
1144             }
1145           expld.assign_name = NULL;
1146         }
1147       break;
1148
1149     case etree_name:
1150       fold_name (tree);
1151       break;
1152
1153     default:
1154       FAIL ();
1155       memset (&expld.result, 0, sizeof (expld.result));
1156       break;
1157     }
1158 }
1159
1160 void
1161 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
1162 {
1163   expld.dot = *dotp;
1164   expld.dotp = dotp;
1165   expld.section = current_section;
1166   exp_fold_tree_1 (tree);
1167 }
1168
1169 void
1170 exp_fold_tree_no_dot (etree_type *tree)
1171 {
1172   expld.dot = 0;
1173   expld.dotp = NULL;
1174   expld.section = bfd_abs_section_ptr;
1175   exp_fold_tree_1 (tree);
1176 }
1177
1178 etree_type *
1179 exp_binop (int code, etree_type *lhs, etree_type *rhs)
1180 {
1181   etree_type value, *new_e;
1182
1183   value.type.node_code = code;
1184   value.type.filename = lhs->type.filename;
1185   value.type.lineno = lhs->type.lineno;
1186   value.binary.lhs = lhs;
1187   value.binary.rhs = rhs;
1188   value.type.node_class = etree_binary;
1189   exp_fold_tree_no_dot (&value);
1190   if (expld.result.valid_p)
1191     return exp_intop (expld.result.value);
1192
1193   new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
1194   memcpy (new_e, &value, sizeof (new_e->binary));
1195   return new_e;
1196 }
1197
1198 etree_type *
1199 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
1200 {
1201   etree_type value, *new_e;
1202
1203   value.type.node_code = code;
1204   value.type.filename = cond->type.filename;
1205   value.type.lineno = cond->type.lineno;
1206   value.trinary.lhs = lhs;
1207   value.trinary.cond = cond;
1208   value.trinary.rhs = rhs;
1209   value.type.node_class = etree_trinary;
1210   exp_fold_tree_no_dot (&value);
1211   if (expld.result.valid_p)
1212     return exp_intop (expld.result.value);
1213
1214   new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
1215   memcpy (new_e, &value, sizeof (new_e->trinary));
1216   return new_e;
1217 }
1218
1219 etree_type *
1220 exp_unop (int code, etree_type *child)
1221 {
1222   etree_type value, *new_e;
1223
1224   value.unary.type.node_code = code;
1225   value.unary.type.filename = child->type.filename;
1226   value.unary.type.lineno = child->type.lineno;
1227   value.unary.child = child;
1228   value.unary.type.node_class = etree_unary;
1229   exp_fold_tree_no_dot (&value);
1230   if (expld.result.valid_p)
1231     return exp_intop (expld.result.value);
1232
1233   new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
1234   memcpy (new_e, &value, sizeof (new_e->unary));
1235   return new_e;
1236 }
1237
1238 etree_type *
1239 exp_nameop (int code, const char *name)
1240 {
1241   etree_type value, *new_e;
1242
1243   value.name.type.node_code = code;
1244   value.name.type.filename = ldlex_filename ();
1245   value.name.type.lineno = lineno;
1246   value.name.name = name;
1247   value.name.type.node_class = etree_name;
1248
1249   exp_fold_tree_no_dot (&value);
1250   if (expld.result.valid_p)
1251     return exp_intop (expld.result.value);
1252
1253   new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1254   memcpy (new_e, &value, sizeof (new_e->name));
1255   return new_e;
1256
1257 }
1258
1259 static etree_type *
1260 exp_assop (const char *dst,
1261            etree_type *src,
1262            enum node_tree_enum class,
1263            bfd_boolean defsym,
1264            bfd_boolean hidden)
1265 {
1266   etree_type *n;
1267
1268   n = (etree_type *) stat_alloc (sizeof (n->assign));
1269   n->assign.type.node_code = '=';
1270   n->assign.type.filename = src->type.filename;
1271   n->assign.type.lineno = src->type.lineno;
1272   n->assign.type.node_class = class;
1273   n->assign.src = src;
1274   n->assign.dst = dst;
1275   n->assign.defsym = defsym;
1276   n->assign.hidden = hidden;
1277   return n;
1278 }
1279
1280 /* Handle linker script assignments and HIDDEN.  */
1281
1282 etree_type *
1283 exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
1284 {
1285   return exp_assop (dst, src, etree_assign, FALSE, hidden);
1286 }
1287
1288 /* Handle --defsym command-line option.  */
1289
1290 etree_type *
1291 exp_defsym (const char *dst, etree_type *src)
1292 {
1293   return exp_assop (dst, src, etree_assign, TRUE, FALSE);
1294 }
1295
1296 /* Handle PROVIDE.  */
1297
1298 etree_type *
1299 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1300 {
1301   return exp_assop (dst, src, etree_provide, FALSE, hidden);
1302 }
1303
1304 /* Handle ASSERT.  */
1305
1306 etree_type *
1307 exp_assert (etree_type *exp, const char *message)
1308 {
1309   etree_type *n;
1310
1311   n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1312   n->assert_s.type.node_code = '!';
1313   n->assert_s.type.filename = exp->type.filename;
1314   n->assert_s.type.lineno = exp->type.lineno;
1315   n->assert_s.type.node_class = etree_assert;
1316   n->assert_s.child = exp;
1317   n->assert_s.message = message;
1318   return n;
1319 }
1320
1321 void
1322 exp_print_tree (etree_type *tree)
1323 {
1324   bfd_boolean function_like;
1325
1326   if (config.map_file == NULL)
1327     config.map_file = stderr;
1328
1329   if (tree == NULL)
1330     {
1331       minfo ("NULL TREE\n");
1332       return;
1333     }
1334
1335   switch (tree->type.node_class)
1336     {
1337     case etree_value:
1338       minfo ("0x%v", tree->value.value);
1339       return;
1340     case etree_rel:
1341       if (tree->rel.section->owner != NULL)
1342         minfo ("%B:", tree->rel.section->owner);
1343       minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1344       return;
1345     case etree_assign:
1346       fputs (tree->assign.dst, config.map_file);
1347       exp_print_token (tree->type.node_code, TRUE);
1348       exp_print_tree (tree->assign.src);
1349       break;
1350     case etree_provide:
1351     case etree_provided:
1352       fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1353       exp_print_tree (tree->assign.src);
1354       fputc (')', config.map_file);
1355       break;
1356     case etree_binary:
1357       function_like = FALSE;
1358       switch (tree->type.node_code)
1359         {
1360         case MAX_K:
1361         case MIN_K:
1362         case ALIGN_K:
1363         case DATA_SEGMENT_ALIGN:
1364         case DATA_SEGMENT_RELRO_END:
1365           function_like = TRUE;
1366           break;
1367         case SEGMENT_START:
1368           /* Special handling because arguments are in reverse order and
1369              the segment name is quoted.  */
1370           exp_print_token (tree->type.node_code, FALSE);
1371           fputs (" (\"", config.map_file);
1372           exp_print_tree (tree->binary.rhs);
1373           fputs ("\", ", config.map_file);
1374           exp_print_tree (tree->binary.lhs);
1375           fputc (')', config.map_file);
1376           return;
1377         }
1378       if (function_like)
1379         {
1380           exp_print_token (tree->type.node_code, FALSE);
1381           fputc (' ', config.map_file);
1382         }
1383       fputc ('(', config.map_file);
1384       exp_print_tree (tree->binary.lhs);
1385       if (function_like)
1386         fprintf (config.map_file, ", ");
1387       else
1388         exp_print_token (tree->type.node_code, TRUE);
1389       exp_print_tree (tree->binary.rhs);
1390       fputc (')', config.map_file);
1391       break;
1392     case etree_trinary:
1393       exp_print_tree (tree->trinary.cond);
1394       fputc ('?', config.map_file);
1395       exp_print_tree (tree->trinary.lhs);
1396       fputc (':', config.map_file);
1397       exp_print_tree (tree->trinary.rhs);
1398       break;
1399     case etree_unary:
1400       exp_print_token (tree->unary.type.node_code, FALSE);
1401       if (tree->unary.child)
1402         {
1403           fprintf (config.map_file, " (");
1404           exp_print_tree (tree->unary.child);
1405           fputc (')', config.map_file);
1406         }
1407       break;
1408
1409     case etree_assert:
1410       fprintf (config.map_file, "ASSERT (");
1411       exp_print_tree (tree->assert_s.child);
1412       fprintf (config.map_file, ", %s)", tree->assert_s.message);
1413       break;
1414
1415     case etree_name:
1416       if (tree->type.node_code == NAME)
1417         fputs (tree->name.name, config.map_file);
1418       else
1419         {
1420           exp_print_token (tree->type.node_code, FALSE);
1421           if (tree->name.name)
1422             fprintf (config.map_file, " (%s)", tree->name.name);
1423         }
1424       break;
1425     default:
1426       FAIL ();
1427       break;
1428     }
1429 }
1430
1431 bfd_vma
1432 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1433 {
1434   if (tree != NULL)
1435     {
1436       exp_fold_tree_no_dot (tree);
1437       if (expld.result.valid_p)
1438         return expld.result.value;
1439       else if (name != NULL && expld.phase != lang_mark_phase_enum)
1440         einfo (_("%F%S: nonconstant expression for %s\n"),
1441                tree, name);
1442     }
1443   return def;
1444 }
1445
1446 int
1447 exp_get_value_int (etree_type *tree, int def, char *name)
1448 {
1449   return exp_get_vma (tree, def, name);
1450 }
1451
1452 fill_type *
1453 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1454 {
1455   fill_type *fill;
1456   size_t len;
1457   unsigned int val;
1458
1459   if (tree == NULL)
1460     return def;
1461
1462   exp_fold_tree_no_dot (tree);
1463   if (!expld.result.valid_p)
1464     {
1465       if (name != NULL && expld.phase != lang_mark_phase_enum)
1466         einfo (_("%F%S: nonconstant expression for %s\n"),
1467                tree, name);
1468       return def;
1469     }
1470
1471   if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1472     {
1473       unsigned char *dst;
1474       unsigned char *s;
1475       fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1476       fill->size = (len + 1) / 2;
1477       dst = fill->data;
1478       s = (unsigned char *) expld.result.str;
1479       val = 0;
1480       do
1481         {
1482           unsigned int digit;
1483
1484           digit = *s++ - '0';
1485           if (digit > 9)
1486             digit = (digit - 'A' + '0' + 10) & 0xf;
1487           val <<= 4;
1488           val += digit;
1489           --len;
1490           if ((len & 1) == 0)
1491             {
1492               *dst++ = val;
1493               val = 0;
1494             }
1495         }
1496       while (len != 0);
1497     }
1498   else
1499     {
1500       fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1501       val = expld.result.value;
1502       fill->data[0] = (val >> 24) & 0xff;
1503       fill->data[1] = (val >> 16) & 0xff;
1504       fill->data[2] = (val >>  8) & 0xff;
1505       fill->data[3] = (val >>  0) & 0xff;
1506       fill->size = 4;
1507     }
1508   return fill;
1509 }
1510
1511 bfd_vma
1512 exp_get_abs_int (etree_type *tree, int def, char *name)
1513 {
1514   if (tree != NULL)
1515     {
1516       exp_fold_tree_no_dot (tree);
1517
1518       if (expld.result.valid_p)
1519         {
1520           if (expld.result.section != NULL)
1521             expld.result.value += expld.result.section->vma;
1522           return expld.result.value;
1523         }
1524       else if (name != NULL && expld.phase != lang_mark_phase_enum)
1525         {
1526           einfo (_("%F%S: nonconstant expression for %s\n"),
1527                  tree, name);
1528         }
1529     }
1530   return def;
1531 }
1532
1533 static bfd_vma
1534 align_n (bfd_vma value, bfd_vma align)
1535 {
1536   if (align <= 1)
1537     return value;
1538
1539   value = (value + align - 1) / align;
1540   return value * align;
1541 }
1542
1543 void
1544 ldexp_init (void)
1545 {
1546   /* The value "13" is ad-hoc, somewhat related to the expected number of
1547      assignments in a linker script.  */
1548   if (!bfd_hash_table_init_n (&definedness_table,
1549                               definedness_newfunc,
1550                               sizeof (struct definedness_hash_entry),
1551                               13))
1552     einfo (_("%P%F: can not create hash table: %E\n"));
1553 }
1554
1555 void
1556 ldexp_finish (void)
1557 {
1558   bfd_hash_table_free (&definedness_table);
1559 }