1/* This module handles expression trees.
2   Copyright (C) 1991-2017 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
44static void exp_fold_tree_1 (etree_type *);
45static bfd_vma align_n (bfd_vma, bfd_vma);
46
47segment_type *segments;
48
49struct 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
55struct 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
74static 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
79static void
80exp_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
161static void
162make_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
183static void
184make_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
192static void
193new_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
201etree_type *
202exp_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
214etree_type *
215exp_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
229etree_type *
230exp_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
242static void
243new_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
251static void
252new_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
260static void
261new_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
274static struct bfd_hash_entry *
275definedness_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
299static struct definedness_hash_entry *
300symbol_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
309static bfd_boolean
310update_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
345static void
346fold_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/* Arithmetic operators, bitwise AND, bitwise OR and XOR keep the
421   section of one of their operands only when the other operand is a
422   plain number.  Losing the section when operating on two symbols,
423   ie. a result of a plain number, is required for subtraction and
424   XOR.  It's justifiable for the other operations on the grounds that
425   adding, multiplying etc. two section relative values does not
426   really make sense unless they are just treated as numbers.
427   The same argument could be made for many expressions involving one
428   symbol and a number.  For example, "1 << x" and "100 / x" probably
429   should not be given the section of x.  The trouble is that if we
430   fuss about such things the rules become complex and it is onerous
431   to document ld expression evaluation.  */
432static void
433arith_result_section (const etree_value_type *lhs)
434{
435  if (expld.result.section == lhs->section)
436    {
437      if (expld.section == bfd_abs_section_ptr
438	  && !config.sane_expr)
439	/* Duplicate the insanity in exp_fold_tree_1 case etree_value.  */
440	expld.result.section = bfd_abs_section_ptr;
441      else
442	expld.result.section = NULL;
443    }
444}
445
446static void
447fold_binary (etree_type *tree)
448{
449  etree_value_type lhs;
450  exp_fold_tree_1 (tree->binary.lhs);
451
452  /* The SEGMENT_START operator is special because its first
453     operand is a string, not the name of a symbol.  Note that the
454     operands have been swapped, so binary.lhs is second (default)
455     operand, binary.rhs is first operand.  */
456  if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
457    {
458      const char *segment_name;
459      segment_type *seg;
460
461      /* Check to see if the user has overridden the default
462	 value.  */
463      segment_name = tree->binary.rhs->name.name;
464      for (seg = segments; seg; seg = seg->next)
465	if (strcmp (seg->name, segment_name) == 0)
466	  {
467	    if (!seg->used
468		&& config.magic_demand_paged
469		&& (seg->value % config.maxpagesize) != 0)
470	      einfo (_("%P: warning: address of `%s' "
471		       "isn't multiple of maximum page size\n"),
472		     segment_name);
473	    seg->used = TRUE;
474	    new_rel_from_abs (seg->value);
475	    break;
476	  }
477      return;
478    }
479
480  lhs = expld.result;
481  exp_fold_tree_1 (tree->binary.rhs);
482  expld.result.valid_p &= lhs.valid_p;
483
484  if (expld.result.valid_p)
485    {
486      if (lhs.section != expld.result.section)
487	{
488	  /* If the values are from different sections, and neither is
489	     just a number, make both the source arguments absolute.  */
490	  if (expld.result.section != NULL
491	      && lhs.section != NULL)
492	    {
493	      make_abs ();
494	      lhs.value += lhs.section->vma;
495	      lhs.section = bfd_abs_section_ptr;
496	    }
497
498	  /* If the rhs is just a number, keep the lhs section.  */
499	  else if (expld.result.section == NULL)
500	    {
501	      expld.result.section = lhs.section;
502	      /* Make this NULL so that we know one of the operands
503		 was just a number, for later tests.  */
504	      lhs.section = NULL;
505	    }
506	}
507      /* At this point we know that both operands have the same
508	 section, or at least one of them is a plain number.  */
509
510      switch (tree->type.node_code)
511	{
512#define BOP(x, y) \
513	case x:							\
514	  expld.result.value = lhs.value y expld.result.value;	\
515	  arith_result_section (&lhs);				\
516	  break;
517
518	  /* Comparison operators, logical AND, and logical OR always
519	     return a plain number.  */
520#define BOPN(x, y) \
521	case x:							\
522	  expld.result.value = lhs.value y expld.result.value;	\
523	  expld.result.section = NULL;				\
524	  break;
525
526	  BOP ('+', +);
527	  BOP ('*', *);
528	  BOP ('-', -);
529	  BOP (LSHIFT, <<);
530	  BOP (RSHIFT, >>);
531	  BOP ('&', &);
532	  BOP ('^', ^);
533	  BOP ('|', |);
534	  BOPN (EQ, ==);
535	  BOPN (NE, !=);
536	  BOPN ('<', <);
537	  BOPN ('>', >);
538	  BOPN (LE, <=);
539	  BOPN (GE, >=);
540	  BOPN (ANDAND, &&);
541	  BOPN (OROR, ||);
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	  arith_result_section (&lhs);
550	  break;
551
552	case '/':
553	  if (expld.result.value != 0)
554	    expld.result.value = ((bfd_signed_vma) lhs.value
555				  / (bfd_signed_vma) expld.result.value);
556	  else if (expld.phase != lang_mark_phase_enum)
557	    einfo (_("%F%S / by zero\n"), tree->binary.rhs);
558	  arith_result_section (&lhs);
559	  break;
560
561	case MAX_K:
562	  if (lhs.value > expld.result.value)
563	    expld.result.value = lhs.value;
564	  break;
565
566	case MIN_K:
567	  if (lhs.value < expld.result.value)
568	    expld.result.value = lhs.value;
569	  break;
570
571	case ALIGN_K:
572	  expld.result.value = align_n (lhs.value, expld.result.value);
573	  break;
574
575	case DATA_SEGMENT_ALIGN:
576	  expld.dataseg.relro = exp_dataseg_relro_start;
577	  if (expld.phase == lang_first_phase_enum
578	      || expld.section != bfd_abs_section_ptr)
579	    expld.result.valid_p = FALSE;
580	  else
581	    {
582	      bfd_vma maxpage = lhs.value;
583	      bfd_vma commonpage = expld.result.value;
584
585	      expld.result.value = align_n (expld.dot, maxpage);
586	      if (expld.dataseg.phase == exp_dataseg_relro_adjust)
587		expld.result.value = expld.dataseg.base;
588	      else if (expld.dataseg.phase == exp_dataseg_adjust)
589		{
590		  if (commonpage < maxpage)
591		    expld.result.value += ((expld.dot + commonpage - 1)
592					   & (maxpage - commonpage));
593		}
594	      else
595		{
596		  expld.result.value += expld.dot & (maxpage - 1);
597		  if (expld.dataseg.phase == exp_dataseg_done)
598		    {
599		      /* OK.  */
600		    }
601		  else if (expld.dataseg.phase == exp_dataseg_none)
602		    {
603		      expld.dataseg.phase = exp_dataseg_align_seen;
604		      expld.dataseg.base = expld.result.value;
605		      expld.dataseg.pagesize = commonpage;
606		      expld.dataseg.maxpagesize = maxpage;
607		      expld.dataseg.relro_end = 0;
608		    }
609		  else
610		    expld.result.valid_p = FALSE;
611		}
612	    }
613	  break;
614
615	case DATA_SEGMENT_RELRO_END:
616	  /* Operands swapped!  DATA_SEGMENT_RELRO_END(offset,exp)
617	     has offset in expld.result and exp in lhs.  */
618	  expld.dataseg.relro = exp_dataseg_relro_end;
619	  expld.dataseg.relro_offset = expld.result.value;
620	  if (expld.phase == lang_first_phase_enum
621	      || expld.section != bfd_abs_section_ptr)
622	    expld.result.valid_p = FALSE;
623	  else if (expld.dataseg.phase == exp_dataseg_align_seen
624		   || expld.dataseg.phase == exp_dataseg_adjust
625		   || expld.dataseg.phase == exp_dataseg_relro_adjust
626		   || expld.dataseg.phase == exp_dataseg_done)
627	    {
628	      if (expld.dataseg.phase == exp_dataseg_align_seen
629		  || expld.dataseg.phase == exp_dataseg_relro_adjust)
630		expld.dataseg.relro_end = lhs.value + expld.result.value;
631
632	      if (expld.dataseg.phase == exp_dataseg_relro_adjust
633		  && (expld.dataseg.relro_end
634		      & (expld.dataseg.pagesize - 1)))
635		{
636		  expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
637		  expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
638		  expld.result.value = (expld.dataseg.relro_end
639					- expld.result.value);
640		}
641	      else
642		expld.result.value = lhs.value;
643
644	      if (expld.dataseg.phase == exp_dataseg_align_seen)
645		expld.dataseg.phase = exp_dataseg_relro_seen;
646	    }
647	  else
648	    expld.result.valid_p = FALSE;
649	  break;
650
651	default:
652	  FAIL ();
653	}
654    }
655}
656
657static void
658fold_trinary (etree_type *tree)
659{
660  exp_fold_tree_1 (tree->trinary.cond);
661  if (expld.result.valid_p)
662    exp_fold_tree_1 (expld.result.value
663		     ? tree->trinary.lhs
664		     : tree->trinary.rhs);
665}
666
667static void
668fold_name (etree_type *tree)
669{
670  memset (&expld.result, 0, sizeof (expld.result));
671
672  switch (tree->type.node_code)
673    {
674    case SIZEOF_HEADERS:
675      if (expld.phase != lang_first_phase_enum)
676	{
677	  bfd_vma hdr_size = 0;
678	  /* Don't find the real header size if only marking sections;
679	     The bfd function may cache incorrect data.  */
680	  if (expld.phase != lang_mark_phase_enum)
681	    hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
682	  new_number (hdr_size);
683	}
684      break;
685
686    case DEFINED:
687      if (expld.phase != lang_first_phase_enum)
688	{
689	  struct bfd_link_hash_entry *h;
690	  struct definedness_hash_entry *def;
691
692	  h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
693					    &link_info,
694					    tree->name.name,
695					    FALSE, FALSE, TRUE);
696	  new_number (h != NULL
697		      && (h->type == bfd_link_hash_defined
698			  || h->type == bfd_link_hash_defweak
699			  || h->type == bfd_link_hash_common)
700		      && ((def = symbol_defined (tree->name.name)) == NULL
701			  || def->by_object
702			  || def->iteration == (lang_statement_iteration & 1)));
703	}
704      break;
705
706    case NAME:
707      if (expld.assign_name != NULL
708	  && strcmp (expld.assign_name, tree->name.name) == 0)
709	{
710	  /* Self-assignment is only allowed for absolute symbols
711	     defined in a linker script.  */
712	  struct bfd_link_hash_entry *h;
713	  struct definedness_hash_entry *def;
714
715	  h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
716					    &link_info,
717					    tree->name.name,
718					    FALSE, FALSE, TRUE);
719	  if (!(h != NULL
720		&& (h->type == bfd_link_hash_defined
721		    || h->type == bfd_link_hash_defweak)
722		&& h->u.def.section == bfd_abs_section_ptr
723		&& (def = symbol_defined (tree->name.name)) != NULL
724		&& def->iteration == (lang_statement_iteration & 1)))
725	    expld.assign_name = NULL;
726	}
727      if (expld.phase == lang_first_phase_enum)
728	;
729      else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
730	new_rel_from_abs (expld.dot);
731      else
732	{
733	  struct bfd_link_hash_entry *h;
734
735	  h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
736					    &link_info,
737					    tree->name.name,
738					    TRUE, FALSE, TRUE);
739	  if (!h)
740	    einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
741	  else if (h->type == bfd_link_hash_defined
742		   || h->type == bfd_link_hash_defweak)
743	    {
744	      asection *output_section;
745
746	      output_section = h->u.def.section->output_section;
747	      if (output_section == NULL)
748		{
749		  if (expld.phase == lang_mark_phase_enum)
750		    new_rel (h->u.def.value, h->u.def.section);
751		  else
752		    einfo (_("%X%S: unresolvable symbol `%s'"
753			     " referenced in expression\n"),
754			   tree, tree->name.name);
755		}
756	      else if (output_section == bfd_abs_section_ptr
757		       && (expld.section != bfd_abs_section_ptr
758			   || config.sane_expr))
759		new_number (h->u.def.value + h->u.def.section->output_offset);
760	      else
761		new_rel (h->u.def.value + h->u.def.section->output_offset,
762			 output_section);
763	    }
764	  else if (expld.phase == lang_final_phase_enum
765		   || (expld.phase != lang_mark_phase_enum
766		       && expld.assigning_to_dot))
767	    einfo (_("%F%S: undefined symbol `%s'"
768		     " referenced in expression\n"),
769		   tree, tree->name.name);
770	  else if (h->type == bfd_link_hash_new)
771	    {
772	      h->type = bfd_link_hash_undefined;
773	      h->u.undef.abfd = NULL;
774	      if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
775		bfd_link_add_undef (link_info.hash, h);
776	    }
777	}
778      break;
779
780    case ADDR:
781      if (expld.phase != lang_first_phase_enum)
782	{
783	  lang_output_section_statement_type *os;
784
785	  os = lang_output_section_find (tree->name.name);
786	  if (os == NULL)
787	    {
788	      if (expld.phase == lang_final_phase_enum)
789		einfo (_("%F%S: undefined section `%s'"
790			 " referenced in expression\n"),
791		       tree, tree->name.name);
792	    }
793	  else if (os->processed_vma)
794	    new_rel (0, os->bfd_section);
795	}
796      break;
797
798    case LOADADDR:
799      if (expld.phase != lang_first_phase_enum)
800	{
801	  lang_output_section_statement_type *os;
802
803	  os = lang_output_section_find (tree->name.name);
804	  if (os == NULL)
805	    {
806	      if (expld.phase == lang_final_phase_enum)
807		einfo (_("%F%S: undefined section `%s'"
808			 " referenced in expression\n"),
809		       tree, tree->name.name);
810	    }
811	  else if (os->processed_lma)
812	    {
813	      if (os->load_base == NULL)
814		new_abs (os->bfd_section->lma);
815	      else
816		{
817		  exp_fold_tree_1 (os->load_base);
818		  if (expld.result.valid_p)
819		    make_abs ();
820		}
821	    }
822	}
823      break;
824
825    case SIZEOF:
826    case ALIGNOF:
827      if (expld.phase != lang_first_phase_enum)
828	{
829	  lang_output_section_statement_type *os;
830
831	  os = lang_output_section_find (tree->name.name);
832	  if (os == NULL)
833	    {
834	      if (expld.phase == lang_final_phase_enum)
835		einfo (_("%F%S: undefined section `%s'"
836			 " referenced in expression\n"),
837		       tree, tree->name.name);
838	      new_number (0);
839	    }
840	  else if (os->bfd_section != NULL)
841	    {
842	      bfd_vma val;
843
844	      if (tree->type.node_code == SIZEOF)
845		val = (os->bfd_section->size
846		       / bfd_octets_per_byte (link_info.output_bfd));
847	      else
848		val = (bfd_vma)1 << os->bfd_section->alignment_power;
849
850	      new_number (val);
851	    }
852	  else
853	    new_number (0);
854	}
855      break;
856
857    case LENGTH:
858      {
859      if (expld.phase != lang_first_phase_enum)
860	{
861	  lang_memory_region_type *mem;
862
863	  mem = lang_memory_region_lookup (tree->name.name, FALSE);
864	  if (mem != NULL)
865	    new_number (mem->length);
866	  else
867	    einfo (_("%F%S: undefined MEMORY region `%s'"
868		     " referenced in expression\n"),
869		   tree, tree->name.name);
870	}
871      }
872      break;
873
874    case ORIGIN:
875      if (expld.phase != lang_first_phase_enum)
876	{
877	  lang_memory_region_type *mem;
878
879	  mem = lang_memory_region_lookup (tree->name.name, FALSE);
880	  if (mem != NULL)
881	    new_rel_from_abs (mem->origin);
882	  else
883	    einfo (_("%F%S: undefined MEMORY region `%s'"
884		     " referenced in expression\n"),
885		   tree, tree->name.name);
886	}
887      break;
888
889    case CONSTANT:
890      if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
891	new_number (config.maxpagesize);
892      else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
893	new_number (config.commonpagesize);
894      else
895	einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
896	       tree, tree->name.name);
897      break;
898
899    default:
900      FAIL ();
901      break;
902    }
903}
904
905/* Return true if TREE is '.'.  */
906
907static bfd_boolean
908is_dot (const etree_type *tree)
909{
910  return (tree->type.node_class == etree_name
911	  && tree->type.node_code == NAME
912	  && tree->name.name[0] == '.'
913	  && tree->name.name[1] == 0);
914}
915
916/* Return true if TREE is a constant equal to VAL.  */
917
918static bfd_boolean
919is_value (const etree_type *tree, bfd_vma val)
920{
921  return (tree->type.node_class == etree_value
922	  && tree->value.value == val);
923}
924
925/* Return true if TREE is an absolute symbol equal to VAL defined in
926   a linker script.  */
927
928static bfd_boolean
929is_sym_value (const etree_type *tree, bfd_vma val)
930{
931  struct bfd_link_hash_entry *h;
932  struct definedness_hash_entry *def;
933
934  return (tree->type.node_class == etree_name
935	  && tree->type.node_code == NAME
936	  && (def = symbol_defined (tree->name.name)) != NULL
937	  && def->by_script
938	  && def->iteration == (lang_statement_iteration & 1)
939	  && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
940						&link_info,
941						tree->name.name,
942						FALSE, FALSE, TRUE)) != NULL
943	  && h->type == bfd_link_hash_defined
944	  && h->u.def.section == bfd_abs_section_ptr
945	  && h->u.def.value == val);
946}
947
948/* Return true if TREE is ". != 0".  */
949
950static bfd_boolean
951is_dot_ne_0 (const etree_type *tree)
952{
953  return (tree->type.node_class == etree_binary
954	  && tree->type.node_code == NE
955	  && is_dot (tree->binary.lhs)
956	  && is_value (tree->binary.rhs, 0));
957}
958
959/* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
960   absolute constant with value 0 defined in a linker script.  */
961
962static bfd_boolean
963is_dot_plus_0 (const etree_type *tree)
964{
965  return (tree->type.node_class == etree_binary
966	  && tree->type.node_code == '+'
967	  && is_dot (tree->binary.lhs)
968	  && (is_value (tree->binary.rhs, 0)
969	      || is_sym_value (tree->binary.rhs, 0)));
970}
971
972/* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)".  */
973
974static bfd_boolean
975is_align_conditional (const etree_type *tree)
976{
977  if (tree->type.node_class == etree_unary
978      && tree->type.node_code == ALIGN_K)
979    {
980      tree = tree->unary.child;
981      return (tree->type.node_class == etree_trinary
982	      && is_dot_ne_0 (tree->trinary.cond)
983	      && is_value (tree->trinary.rhs, 1));
984    }
985  return FALSE;
986}
987
988/* Subroutine of exp_fold_tree_1 for copying a symbol type.  */
989
990static void
991try_copy_symbol_type (struct bfd_link_hash_entry *h, etree_type *src)
992{
993  struct bfd_link_hash_entry *hsrc;
994
995  hsrc = bfd_link_hash_lookup (link_info.hash, src->name.name,
996			       FALSE, FALSE, TRUE);
997  if (hsrc != NULL)
998    bfd_copy_link_hash_symbol_type (link_info.output_bfd, h, hsrc);
999}
1000
1001static void
1002exp_fold_tree_1 (etree_type *tree)
1003{
1004  if (tree == NULL)
1005    {
1006      memset (&expld.result, 0, sizeof (expld.result));
1007      return;
1008    }
1009
1010  switch (tree->type.node_class)
1011    {
1012    case etree_value:
1013      if (expld.section == bfd_abs_section_ptr
1014	  && !config.sane_expr)
1015	new_abs (tree->value.value);
1016      else
1017	new_number (tree->value.value);
1018      expld.result.str = tree->value.str;
1019      break;
1020
1021    case etree_rel:
1022      if (expld.phase != lang_first_phase_enum)
1023	{
1024	  asection *output_section = tree->rel.section->output_section;
1025	  new_rel (tree->rel.value + tree->rel.section->output_offset,
1026		   output_section);
1027	}
1028      else
1029	memset (&expld.result, 0, sizeof (expld.result));
1030      break;
1031
1032    case etree_assert:
1033      exp_fold_tree_1 (tree->assert_s.child);
1034      if (expld.phase == lang_final_phase_enum && !expld.result.value)
1035	einfo ("%X%P: %s\n", tree->assert_s.message);
1036      break;
1037
1038    case etree_unary:
1039      fold_unary (tree);
1040      break;
1041
1042    case etree_binary:
1043      fold_binary (tree);
1044      break;
1045
1046    case etree_trinary:
1047      fold_trinary (tree);
1048      break;
1049
1050    case etree_assign:
1051    case etree_provide:
1052    case etree_provided:
1053      if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
1054	{
1055	  if (tree->type.node_class != etree_assign)
1056	    einfo (_("%F%S can not PROVIDE assignment to"
1057		     " location counter\n"), tree);
1058	  if (expld.phase != lang_first_phase_enum)
1059	    {
1060	      /* Notify the folder that this is an assignment to dot.  */
1061	      expld.assigning_to_dot = TRUE;
1062	      exp_fold_tree_1 (tree->assign.src);
1063	      expld.assigning_to_dot = FALSE;
1064
1065	      /* If we are assigning to dot inside an output section
1066		 arrange to keep the section, except for certain
1067		 expressions that evaluate to zero.  We ignore . = 0,
1068		 . = . + 0, and . = ALIGN (. != 0 ? expr : 1).
1069		 We can't ignore all expressions that evaluate to zero
1070		 because an otherwise empty section might have padding
1071		 added by an alignment expression that changes with
1072		 relaxation.  Such a section might have zero size
1073		 before relaxation and so be stripped incorrectly.  */
1074	      if (expld.phase == lang_mark_phase_enum
1075		  && expld.section != bfd_abs_section_ptr
1076		  && expld.section != bfd_und_section_ptr
1077		  && !(expld.result.valid_p
1078		       && expld.result.value == 0
1079		       && (is_value (tree->assign.src, 0)
1080			   || is_sym_value (tree->assign.src, 0)
1081			   || is_dot_plus_0 (tree->assign.src)
1082			   || is_align_conditional (tree->assign.src))))
1083		expld.section->flags |= SEC_KEEP;
1084
1085	      if (!expld.result.valid_p
1086		  || expld.section == bfd_und_section_ptr)
1087		{
1088		  if (expld.phase != lang_mark_phase_enum)
1089		    einfo (_("%F%S invalid assignment to"
1090			     " location counter\n"), tree);
1091		}
1092	      else if (expld.dotp == NULL)
1093		einfo (_("%F%S assignment to location counter"
1094			 " invalid outside of SECTIONS\n"), tree);
1095
1096	      /* After allocation, assignment to dot should not be
1097		 done inside an output section since allocation adds a
1098		 padding statement that effectively duplicates the
1099		 assignment.  */
1100	      else if (expld.phase <= lang_allocating_phase_enum
1101		       || expld.section == bfd_abs_section_ptr)
1102		{
1103		  bfd_vma nextdot;
1104
1105		  nextdot = expld.result.value;
1106		  if (expld.result.section != NULL)
1107		    nextdot += expld.result.section->vma;
1108		  else
1109		    nextdot += expld.section->vma;
1110		  if (nextdot < expld.dot
1111		      && expld.section != bfd_abs_section_ptr)
1112		    einfo (_("%F%S cannot move location counter backwards"
1113			     " (from %V to %V)\n"),
1114			   tree, expld.dot, nextdot);
1115		  else
1116		    {
1117		      expld.dot = nextdot;
1118		      *expld.dotp = nextdot;
1119		    }
1120		}
1121	    }
1122	  else
1123	    memset (&expld.result, 0, sizeof (expld.result));
1124	}
1125      else
1126	{
1127	  struct bfd_link_hash_entry *h = NULL;
1128
1129	  if (tree->type.node_class == etree_provide)
1130	    {
1131	      h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1132					FALSE, FALSE, TRUE);
1133	      if (h == NULL
1134		  || !(h->type == bfd_link_hash_new
1135		       || h->type == bfd_link_hash_undefined
1136		       || h->type == bfd_link_hash_undefweak
1137		       || h->linker_def))
1138		{
1139		  /* Do nothing.  The symbol was never referenced, or
1140		     was defined in some object file.  Note that
1141		     undefweak symbols are defined by PROVIDE.  This
1142		     is to support glibc use of __rela_iplt_start and
1143		     similar weak references.  */
1144		  break;
1145		}
1146	    }
1147
1148	  expld.assign_name = tree->assign.dst;
1149	  exp_fold_tree_1 (tree->assign.src);
1150	  /* expld.assign_name remaining equal to tree->assign.dst
1151	     below indicates the evaluation of tree->assign.src did
1152	     not use the value of tree->assign.dst.  We don't allow
1153	     self assignment until the final phase for two reasons:
1154	     1) Expressions are evaluated multiple times.  With
1155	     relaxation, the number of times may vary.
1156	     2) Section relative symbol values cannot be correctly
1157	     converted to absolute values, as is required by many
1158	     expressions, until final section sizing is complete.  */
1159	  if ((expld.result.valid_p
1160	       && (expld.phase == lang_final_phase_enum
1161		   || expld.assign_name != NULL))
1162	      || (expld.phase <= lang_mark_phase_enum
1163		  && tree->type.node_class == etree_assign
1164		  && tree->assign.defsym))
1165	    {
1166	      if (h == NULL)
1167		{
1168		  h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1169					    TRUE, FALSE, TRUE);
1170		  if (h == NULL)
1171		    einfo (_("%P%F:%s: hash creation failed\n"),
1172			   tree->assign.dst);
1173		}
1174
1175	      if (expld.result.section == NULL)
1176		expld.result.section = expld.section;
1177	      if (!update_definedness (tree->assign.dst, h) && 0)
1178		{
1179		  /* Symbol was already defined.  For now this error
1180		     is disabled because it causes failures in the ld
1181		     testsuite: ld-elf/var1, ld-scripts/defined5, and
1182		     ld-scripts/pr14962.  Some of these no doubt
1183		     reflect scripts used in the wild.  */
1184		  (*link_info.callbacks->multiple_definition)
1185		    (&link_info, h, link_info.output_bfd,
1186		     expld.result.section, expld.result.value);
1187		}
1188	      h->type = bfd_link_hash_defined;
1189	      h->u.def.value = expld.result.value;
1190	      h->u.def.section = expld.result.section;
1191	      h->linker_def = ! tree->assign.type.lineno;
1192	      h->ldscript_def = 1;
1193	      if (tree->type.node_class == etree_provide)
1194		tree->type.node_class = etree_provided;
1195
1196	      /* Copy the symbol type if this is a simple assignment of
1197		 one symbol to another.  Also, handle the case of a foldable
1198		 ternary conditional with names on either side.  */
1199	      if (tree->assign.src->type.node_class == etree_name)
1200		try_copy_symbol_type (h, tree->assign.src);
1201	      else if (tree->assign.src->type.node_class == etree_trinary)
1202		{
1203		  exp_fold_tree_1 (tree->assign.src->trinary.cond);
1204		  if (expld.result.valid_p)
1205		    {
1206		      if (expld.result.value
1207			  && tree->assign.src->trinary.lhs->type.node_class
1208			     == etree_name)
1209			try_copy_symbol_type (h, tree->assign.src->trinary.lhs);
1210
1211		      if (!expld.result.value
1212			  && tree->assign.src->trinary.rhs->type.node_class
1213			     == etree_name)
1214			try_copy_symbol_type (h, tree->assign.src->trinary.rhs);
1215		    }
1216		}
1217	    }
1218	  else if (expld.phase == lang_final_phase_enum)
1219	    {
1220	      h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1221					FALSE, FALSE, TRUE);
1222	      if (h != NULL
1223		  && h->type == bfd_link_hash_new)
1224		h->type = bfd_link_hash_undefined;
1225	    }
1226	  expld.assign_name = NULL;
1227	}
1228      break;
1229
1230    case etree_name:
1231      fold_name (tree);
1232      break;
1233
1234    default:
1235      FAIL ();
1236      memset (&expld.result, 0, sizeof (expld.result));
1237      break;
1238    }
1239}
1240
1241void
1242exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
1243{
1244  expld.rel_from_abs = FALSE;
1245  expld.dot = *dotp;
1246  expld.dotp = dotp;
1247  expld.section = current_section;
1248  exp_fold_tree_1 (tree);
1249}
1250
1251void
1252exp_fold_tree_no_dot (etree_type *tree)
1253{
1254  expld.rel_from_abs = FALSE;
1255  expld.dot = 0;
1256  expld.dotp = NULL;
1257  expld.section = bfd_abs_section_ptr;
1258  exp_fold_tree_1 (tree);
1259}
1260
1261static void
1262exp_value_fold (etree_type *tree)
1263{
1264  exp_fold_tree_no_dot (tree);
1265  if (expld.result.valid_p)
1266    {
1267      tree->type.node_code = INT;
1268      tree->value.value = expld.result.value;
1269      tree->value.str = NULL;
1270      tree->type.node_class = etree_value;
1271    }
1272}
1273
1274#define MAX(a, b) ((a) > (b) ? (a) : (b))
1275
1276etree_type *
1277exp_binop (int code, etree_type *lhs, etree_type *rhs)
1278{
1279  etree_type *new_e = (etree_type *) stat_alloc (MAX (sizeof (new_e->binary),
1280						      sizeof (new_e->value)));
1281  new_e->type.node_code = code;
1282  new_e->type.filename = lhs->type.filename;
1283  new_e->type.lineno = lhs->type.lineno;
1284  new_e->binary.lhs = lhs;
1285  new_e->binary.rhs = rhs;
1286  new_e->type.node_class = etree_binary;
1287  if (lhs->type.node_class == etree_value
1288      && rhs->type.node_class == etree_value
1289      && code != ALIGN_K
1290      && code != DATA_SEGMENT_ALIGN
1291      && code != DATA_SEGMENT_RELRO_END)
1292    exp_value_fold (new_e);
1293  return new_e;
1294}
1295
1296etree_type *
1297exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
1298{
1299  etree_type *new_e = (etree_type *) stat_alloc (MAX (sizeof (new_e->trinary),
1300						      sizeof (new_e->value)));
1301  new_e->type.node_code = code;
1302  new_e->type.filename = cond->type.filename;
1303  new_e->type.lineno = cond->type.lineno;
1304  new_e->trinary.lhs = lhs;
1305  new_e->trinary.cond = cond;
1306  new_e->trinary.rhs = rhs;
1307  new_e->type.node_class = etree_trinary;
1308  if (cond->type.node_class == etree_value
1309      && lhs->type.node_class == etree_value
1310      && rhs->type.node_class == etree_value)
1311    exp_value_fold (new_e);
1312  return new_e;
1313}
1314
1315etree_type *
1316exp_unop (int code, etree_type *child)
1317{
1318  etree_type *new_e = (etree_type *) stat_alloc (MAX (sizeof (new_e->unary),
1319						      sizeof (new_e->value)));
1320  new_e->unary.type.node_code = code;
1321  new_e->unary.type.filename = child->type.filename;
1322  new_e->unary.type.lineno = child->type.lineno;
1323  new_e->unary.child = child;
1324  new_e->unary.type.node_class = etree_unary;
1325  if (child->type.node_class == etree_value
1326      && code != ALIGN_K
1327      && code != ABSOLUTE
1328      && code != NEXT
1329      && code != DATA_SEGMENT_END)
1330    exp_value_fold (new_e);
1331  return new_e;
1332}
1333
1334etree_type *
1335exp_nameop (int code, const char *name)
1336{
1337  etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1338
1339  new_e->name.type.node_code = code;
1340  new_e->name.type.filename = ldlex_filename ();
1341  new_e->name.type.lineno = lineno;
1342  new_e->name.name = name;
1343  new_e->name.type.node_class = etree_name;
1344  return new_e;
1345
1346}
1347
1348static etree_type *
1349exp_assop (const char *dst,
1350	   etree_type *src,
1351	   enum node_tree_enum class,
1352	   bfd_boolean defsym,
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.defsym = defsym;
1365  n->assign.hidden = hidden;
1366  return n;
1367}
1368
1369/* Handle linker script assignments and HIDDEN.  */
1370
1371etree_type *
1372exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
1373{
1374  return exp_assop (dst, src, etree_assign, FALSE, hidden);
1375}
1376
1377/* Handle --defsym command-line option.  */
1378
1379etree_type *
1380exp_defsym (const char *dst, etree_type *src)
1381{
1382  return exp_assop (dst, src, etree_assign, TRUE, FALSE);
1383}
1384
1385/* Handle PROVIDE.  */
1386
1387etree_type *
1388exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1389{
1390  return exp_assop (dst, src, etree_provide, FALSE, hidden);
1391}
1392
1393/* Handle ASSERT.  */
1394
1395etree_type *
1396exp_assert (etree_type *exp, const char *message)
1397{
1398  etree_type *n;
1399
1400  n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1401  n->assert_s.type.node_code = '!';
1402  n->assert_s.type.filename = exp->type.filename;
1403  n->assert_s.type.lineno = exp->type.lineno;
1404  n->assert_s.type.node_class = etree_assert;
1405  n->assert_s.child = exp;
1406  n->assert_s.message = message;
1407  return n;
1408}
1409
1410void
1411exp_print_tree (etree_type *tree)
1412{
1413  bfd_boolean function_like;
1414
1415  if (config.map_file == NULL)
1416    config.map_file = stderr;
1417
1418  if (tree == NULL)
1419    {
1420      minfo ("NULL TREE\n");
1421      return;
1422    }
1423
1424  switch (tree->type.node_class)
1425    {
1426    case etree_value:
1427      minfo ("0x%v", tree->value.value);
1428      return;
1429    case etree_rel:
1430      if (tree->rel.section->owner != NULL)
1431	minfo ("%B:", tree->rel.section->owner);
1432      minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1433      return;
1434    case etree_assign:
1435      fputs (tree->assign.dst, config.map_file);
1436      exp_print_token (tree->type.node_code, TRUE);
1437      exp_print_tree (tree->assign.src);
1438      break;
1439    case etree_provide:
1440    case etree_provided:
1441      fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1442      exp_print_tree (tree->assign.src);
1443      fputc (')', config.map_file);
1444      break;
1445    case etree_binary:
1446      function_like = FALSE;
1447      switch (tree->type.node_code)
1448	{
1449	case MAX_K:
1450	case MIN_K:
1451	case ALIGN_K:
1452	case DATA_SEGMENT_ALIGN:
1453	case DATA_SEGMENT_RELRO_END:
1454	  function_like = TRUE;
1455	  break;
1456	case SEGMENT_START:
1457	  /* Special handling because arguments are in reverse order and
1458	     the segment name is quoted.  */
1459	  exp_print_token (tree->type.node_code, FALSE);
1460	  fputs (" (\"", config.map_file);
1461	  exp_print_tree (tree->binary.rhs);
1462	  fputs ("\", ", config.map_file);
1463	  exp_print_tree (tree->binary.lhs);
1464	  fputc (')', config.map_file);
1465	  return;
1466	}
1467      if (function_like)
1468	{
1469	  exp_print_token (tree->type.node_code, FALSE);
1470	  fputc (' ', config.map_file);
1471	}
1472      fputc ('(', config.map_file);
1473      exp_print_tree (tree->binary.lhs);
1474      if (function_like)
1475	fprintf (config.map_file, ", ");
1476      else
1477	exp_print_token (tree->type.node_code, TRUE);
1478      exp_print_tree (tree->binary.rhs);
1479      fputc (')', config.map_file);
1480      break;
1481    case etree_trinary:
1482      exp_print_tree (tree->trinary.cond);
1483      fputc ('?', config.map_file);
1484      exp_print_tree (tree->trinary.lhs);
1485      fputc (':', config.map_file);
1486      exp_print_tree (tree->trinary.rhs);
1487      break;
1488    case etree_unary:
1489      exp_print_token (tree->unary.type.node_code, FALSE);
1490      if (tree->unary.child)
1491	{
1492	  fprintf (config.map_file, " (");
1493	  exp_print_tree (tree->unary.child);
1494	  fputc (')', config.map_file);
1495	}
1496      break;
1497
1498    case etree_assert:
1499      fprintf (config.map_file, "ASSERT (");
1500      exp_print_tree (tree->assert_s.child);
1501      fprintf (config.map_file, ", %s)", tree->assert_s.message);
1502      break;
1503
1504    case etree_name:
1505      if (tree->type.node_code == NAME)
1506	fputs (tree->name.name, config.map_file);
1507      else
1508	{
1509	  exp_print_token (tree->type.node_code, FALSE);
1510	  if (tree->name.name)
1511	    fprintf (config.map_file, " (%s)", tree->name.name);
1512	}
1513      break;
1514    default:
1515      FAIL ();
1516      break;
1517    }
1518}
1519
1520bfd_vma
1521exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1522{
1523  if (tree != NULL)
1524    {
1525      exp_fold_tree_no_dot (tree);
1526      if (expld.result.valid_p)
1527	return expld.result.value;
1528      else if (name != NULL && expld.phase != lang_mark_phase_enum)
1529	einfo (_("%F%S: nonconstant expression for %s\n"),
1530	       tree, name);
1531    }
1532  return def;
1533}
1534
1535int
1536exp_get_value_int (etree_type *tree, int def, char *name)
1537{
1538  return exp_get_vma (tree, def, name);
1539}
1540
1541fill_type *
1542exp_get_fill (etree_type *tree, fill_type *def, char *name)
1543{
1544  fill_type *fill;
1545  size_t len;
1546  unsigned int val;
1547
1548  if (tree == NULL)
1549    return def;
1550
1551  exp_fold_tree_no_dot (tree);
1552  if (!expld.result.valid_p)
1553    {
1554      if (name != NULL && expld.phase != lang_mark_phase_enum)
1555	einfo (_("%F%S: nonconstant expression for %s\n"),
1556	       tree, name);
1557      return def;
1558    }
1559
1560  if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1561    {
1562      unsigned char *dst;
1563      unsigned char *s;
1564      fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1565      fill->size = (len + 1) / 2;
1566      dst = fill->data;
1567      s = (unsigned char *) expld.result.str;
1568      val = 0;
1569      do
1570	{
1571	  unsigned int digit;
1572
1573	  digit = *s++ - '0';
1574	  if (digit > 9)
1575	    digit = (digit - 'A' + '0' + 10) & 0xf;
1576	  val <<= 4;
1577	  val += digit;
1578	  --len;
1579	  if ((len & 1) == 0)
1580	    {
1581	      *dst++ = val;
1582	      val = 0;
1583	    }
1584	}
1585      while (len != 0);
1586    }
1587  else
1588    {
1589      fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1590      val = expld.result.value;
1591      fill->data[0] = (val >> 24) & 0xff;
1592      fill->data[1] = (val >> 16) & 0xff;
1593      fill->data[2] = (val >>  8) & 0xff;
1594      fill->data[3] = (val >>  0) & 0xff;
1595      fill->size = 4;
1596    }
1597  return fill;
1598}
1599
1600bfd_vma
1601exp_get_abs_int (etree_type *tree, int def, char *name)
1602{
1603  if (tree != NULL)
1604    {
1605      exp_fold_tree_no_dot (tree);
1606
1607      if (expld.result.valid_p)
1608	{
1609	  if (expld.result.section != NULL)
1610	    expld.result.value += expld.result.section->vma;
1611	  return expld.result.value;
1612	}
1613      else if (name != NULL && expld.phase != lang_mark_phase_enum)
1614	{
1615	  einfo (_("%F%S: nonconstant expression for %s\n"),
1616		 tree, name);
1617	}
1618    }
1619  return def;
1620}
1621
1622static bfd_vma
1623align_n (bfd_vma value, bfd_vma align)
1624{
1625  if (align <= 1)
1626    return value;
1627
1628  value = (value + align - 1) / align;
1629  return value * align;
1630}
1631
1632void
1633ldexp_init (void)
1634{
1635  /* The value "13" is ad-hoc, somewhat related to the expected number of
1636     assignments in a linker script.  */
1637  if (!bfd_hash_table_init_n (&definedness_table,
1638			      definedness_newfunc,
1639			      sizeof (struct definedness_hash_entry),
1640			      13))
1641    einfo (_("%P%F: can not create hash table: %E\n"));
1642}
1643
1644/* Convert absolute symbols defined by a script from "dot" (also
1645   SEGMENT_START or ORIGIN) outside of an output section statement,
1646   to section relative.  */
1647
1648static bfd_boolean
1649set_sym_sections (struct bfd_hash_entry *bh, void *inf ATTRIBUTE_UNUSED)
1650{
1651  struct definedness_hash_entry *def = (struct definedness_hash_entry *) bh;
1652  if (def->final_sec != bfd_abs_section_ptr)
1653    {
1654      struct bfd_link_hash_entry *h;
1655      h = bfd_link_hash_lookup (link_info.hash, bh->string,
1656				FALSE, FALSE, TRUE);
1657      if (h != NULL
1658	  && h->type == bfd_link_hash_defined
1659	  && h->u.def.section == bfd_abs_section_ptr)
1660	{
1661	  h->u.def.value -= def->final_sec->vma;
1662	  h->u.def.section = def->final_sec;
1663	}
1664    }
1665  return TRUE;
1666}
1667
1668void
1669ldexp_finalize_syms (void)
1670{
1671  bfd_hash_traverse (&definedness_table, set_sym_sections, NULL);
1672}
1673
1674void
1675ldexp_finish (void)
1676{
1677  bfd_hash_table_free (&definedness_table);
1678}
1679