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