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