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