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