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