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