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