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