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