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