LD: Avoid a division by zero page size with SEGMENT_START handling
[deliverable/binutils-gdb.git] / ld / ldexp.c
CommitLineData
252b5132 1/* This module handles expression trees.
219d1afa 2 Copyright (C) 1991-2018 Free Software Foundation, Inc.
8c95a62e 3 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
252b5132 4
f96b4a7b 5 This file is part of the GNU Binutils.
252b5132 6
f96b4a7b 7 This program is free software; you can redistribute it and/or modify
3ec57632 8 it under the terms of the GNU General Public License as published by
f96b4a7b
NC
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
252b5132 11
f96b4a7b 12 This program is distributed in the hope that it will be useful,
3ec57632
NC
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
252b5132 16
3ec57632 17 You should have received a copy of the GNU General Public License
f96b4a7b
NC
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
252b5132 22
8c95a62e 23/* This module is in charge of working out the contents of expressions.
252b5132 24
8c95a62e
KH
25 It has to keep track of the relative/absness of a symbol etc. This
26 is done by keeping all values in a struct (an etree_value_type)
27 which contains a value, a section to which it is relative and a
28 valid bit. */
252b5132 29
252b5132 30#include "sysdep.h"
3db64b00 31#include "bfd.h"
252b5132
RH
32#include "bfdlink.h"
33
34#include "ld.h"
35#include "ldmain.h"
36#include "ldmisc.h"
37#include "ldexp.h"
f856040a 38#include "ldlex.h"
df2a7313 39#include <ldgram.h>
252b5132 40#include "ldlang.h"
c7d701b0 41#include "libiberty.h"
2c382fb6 42#include "safe-ctype.h"
252b5132 43
e9ee469a 44static void exp_fold_tree_1 (etree_type *);
e9ee469a 45static bfd_vma align_n (bfd_vma, bfd_vma);
2d20f7bf 46
ba916c8a
MM
47segment_type *segments;
48
e9ee469a 49struct ldexp_control expld;
fbbb9ac5 50
18d6a79d 51/* This structure records symbols for which we need to keep track of
975f8a9e
AM
52 definedness for use in the DEFINED () test. It is also used in
53 making absolute symbols section relative late in the link. */
18d6a79d
AM
54
55struct definedness_hash_entry
56{
57 struct bfd_hash_entry root;
975f8a9e
AM
58
59 /* If this symbol was assigned from "dot" outside of an output
60 section statement, the section we'd like it relative to. */
61 asection *final_sec;
62
165f707a
AM
63 /* Low bits of iteration count. Symbols with matching iteration have
64 been defined in this pass over the script. */
65 unsigned int iteration : 8;
66
975f8a9e 67 /* Symbol was defined by an object file. */
18d6a79d 68 unsigned int by_object : 1;
18d6a79d
AM
69};
70
71static struct bfd_hash_table definedness_table;
72
7b17f854 73/* Print the string representation of the given token. Surround it
b34976b6 74 with spaces if INFIX_P is TRUE. */
7b17f854 75
252b5132 76static void
1579bae1 77exp_print_token (token_code_type code, int infix_p)
252b5132 78{
4da711b1 79 static const struct
c7d701b0 80 {
8c95a62e 81 token_code_type code;
0aa7f586 82 const char *name;
c7d701b0
NC
83 }
84 table[] =
85 {
8c95a62e 86 { INT, "int" },
8c95a62e
KH
87 { NAME, "NAME" },
88 { PLUSEQ, "+=" },
89 { MINUSEQ, "-=" },
90 { MULTEQ, "*=" },
91 { DIVEQ, "/=" },
92 { LSHIFTEQ, "<<=" },
93 { RSHIFTEQ, ">>=" },
94 { ANDEQ, "&=" },
95 { OREQ, "|=" },
96 { OROR, "||" },
97 { ANDAND, "&&" },
98 { EQ, "==" },
99 { NE, "!=" },
100 { LE, "<=" },
101 { GE, ">=" },
102 { LSHIFT, "<<" },
7cecdbff 103 { RSHIFT, ">>" },
2e53f7d6 104 { LOG2CEIL, "LOG2CEIL" },
8c95a62e
KH
105 { ALIGN_K, "ALIGN" },
106 { BLOCK, "BLOCK" },
c7d701b0
NC
107 { QUAD, "QUAD" },
108 { SQUAD, "SQUAD" },
109 { LONG, "LONG" },
110 { SHORT, "SHORT" },
111 { BYTE, "BYTE" },
8c95a62e
KH
112 { SECTIONS, "SECTIONS" },
113 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
8c95a62e
KH
114 { MEMORY, "MEMORY" },
115 { DEFINED, "DEFINED" },
116 { TARGET_K, "TARGET" },
117 { SEARCH_DIR, "SEARCH_DIR" },
118 { MAP, "MAP" },
8c95a62e 119 { ENTRY, "ENTRY" },
c7d701b0 120 { NEXT, "NEXT" },
362c1d1a 121 { ALIGNOF, "ALIGNOF" },
c7d701b0
NC
122 { SIZEOF, "SIZEOF" },
123 { ADDR, "ADDR" },
124 { LOADADDR, "LOADADDR" },
24718e3b 125 { CONSTANT, "CONSTANT" },
8c0848b5
AM
126 { ABSOLUTE, "ABSOLUTE" },
127 { MAX_K, "MAX" },
128 { MIN_K, "MIN" },
129 { ASSERT_K, "ASSERT" },
1049f94e 130 { REL, "relocatable" },
2d20f7bf 131 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
8c37241b 132 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
ba916c8a 133 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
3ec57632
NC
134 { ORIGIN, "ORIGIN" },
135 { LENGTH, "LENGTH" },
ba916c8a 136 { SEGMENT_START, "SEGMENT_START" }
8c95a62e 137 };
252b5132
RH
138 unsigned int idx;
139
7b17f854
RS
140 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
141 if (table[idx].code == code)
142 break;
c7d701b0 143
7b17f854
RS
144 if (infix_p)
145 fputc (' ', config.map_file);
146
147 if (idx < ARRAY_SIZE (table))
148 fputs (table[idx].name, config.map_file);
149 else if (code < 127)
150 fputc (code, config.map_file);
c7d701b0 151 else
7b17f854
RS
152 fprintf (config.map_file, "<code %d>", code);
153
154 if (infix_p)
155 fputc (' ', config.map_file);
252b5132
RH
156}
157
2e53f7d6
NC
158static void
159make_log2ceil (void)
160{
161 bfd_vma value = expld.result.value;
162 bfd_vma result = -1;
163 bfd_boolean round_up = FALSE;
164
165 do
166 {
167 result++;
168 /* If more than one bit is set in the value we will need to round up. */
169 if ((value > 1) && (value & 1))
170 round_up = TRUE;
171 }
172 while (value >>= 1);
173
174 if (round_up)
175 result += 1;
176 expld.result.section = NULL;
177 expld.result.value = result;
178}
179
4de2d33d 180static void
e9ee469a 181make_abs (void)
252b5132 182{
7542af2a
AM
183 if (expld.result.section != NULL)
184 expld.result.value += expld.result.section->vma;
e9ee469a 185 expld.result.section = bfd_abs_section_ptr;
975f8a9e 186 expld.rel_from_abs = FALSE;
252b5132
RH
187}
188
e9ee469a 189static void
1579bae1 190new_abs (bfd_vma value)
252b5132 191{
e9ee469a
AM
192 expld.result.valid_p = TRUE;
193 expld.result.section = bfd_abs_section_ptr;
194 expld.result.value = value;
195 expld.result.str = NULL;
252b5132
RH
196}
197
252b5132 198etree_type *
1579bae1 199exp_intop (bfd_vma value)
252b5132 200{
d3ce72d0
NC
201 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
202 new_e->type.node_code = INT;
dab69f68 203 new_e->type.filename = ldlex_filename ();
d3ce72d0
NC
204 new_e->type.lineno = lineno;
205 new_e->value.value = value;
206 new_e->value.str = NULL;
207 new_e->type.node_class = etree_value;
208 return new_e;
2c382fb6 209}
252b5132 210
2c382fb6 211etree_type *
1579bae1 212exp_bigintop (bfd_vma value, char *str)
2c382fb6 213{
d3ce72d0
NC
214 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
215 new_e->type.node_code = INT;
dab69f68 216 new_e->type.filename = ldlex_filename ();
d3ce72d0
NC
217 new_e->type.lineno = lineno;
218 new_e->value.value = value;
219 new_e->value.str = str;
220 new_e->type.node_class = etree_value;
221 return new_e;
252b5132
RH
222}
223
1049f94e 224/* Build an expression representing an unnamed relocatable value. */
252b5132
RH
225
226etree_type *
1579bae1 227exp_relop (asection *section, bfd_vma value)
252b5132 228{
d3ce72d0
NC
229 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
230 new_e->type.node_code = REL;
dab69f68 231 new_e->type.filename = ldlex_filename ();
d3ce72d0
NC
232 new_e->type.lineno = lineno;
233 new_e->type.node_class = etree_rel;
234 new_e->rel.section = section;
235 new_e->rel.value = value;
236 return new_e;
252b5132
RH
237}
238
7542af2a
AM
239static void
240new_number (bfd_vma value)
241{
242 expld.result.valid_p = TRUE;
243 expld.result.value = value;
244 expld.result.str = NULL;
245 expld.result.section = NULL;
246}
247
e9ee469a 248static void
5942515f 249new_rel (bfd_vma value, asection *section)
252b5132 250{
e9ee469a
AM
251 expld.result.valid_p = TRUE;
252 expld.result.value = value;
5942515f 253 expld.result.str = NULL;
e9ee469a 254 expld.result.section = section;
252b5132
RH
255}
256
e9ee469a
AM
257static void
258new_rel_from_abs (bfd_vma value)
252b5132 259{
d2667025
AM
260 asection *s = expld.section;
261
975f8a9e 262 expld.rel_from_abs = TRUE;
e9ee469a 263 expld.result.valid_p = TRUE;
d2667025 264 expld.result.value = value - s->vma;
e9ee469a 265 expld.result.str = NULL;
d2667025 266 expld.result.section = s;
252b5132
RH
267}
268
18d6a79d
AM
269/* New-function for the definedness hash table. */
270
271static struct bfd_hash_entry *
272definedness_newfunc (struct bfd_hash_entry *entry,
273 struct bfd_hash_table *table ATTRIBUTE_UNUSED,
274 const char *name ATTRIBUTE_UNUSED)
275{
276 struct definedness_hash_entry *ret = (struct definedness_hash_entry *) entry;
277
278 if (ret == NULL)
279 ret = (struct definedness_hash_entry *)
280 bfd_hash_allocate (table, sizeof (struct definedness_hash_entry));
281
282 if (ret == NULL)
df5f2391 283 einfo (_("%F%P: bfd_hash_allocate failed creating symbol %s\n"), name);
18d6a79d
AM
284
285 ret->by_object = 0;
18d6a79d
AM
286 ret->iteration = 0;
287 return &ret->root;
288}
289
290/* Called during processing of linker script script expressions.
291 For symbols assigned in a linker script, return a struct describing
292 where the symbol is defined relative to the current expression,
293 otherwise return NULL. */
294
295static struct definedness_hash_entry *
296symbol_defined (const char *name)
297{
298 return ((struct definedness_hash_entry *)
299 bfd_hash_lookup (&definedness_table, name, FALSE, FALSE));
300}
301
422f1c65
AM
302/* Update the definedness state of NAME. Return FALSE if script symbol
303 is multiply defining a strong symbol in an object. */
18d6a79d 304
422f1c65 305static bfd_boolean
18d6a79d
AM
306update_definedness (const char *name, struct bfd_link_hash_entry *h)
307{
422f1c65 308 bfd_boolean ret;
18d6a79d
AM
309 struct definedness_hash_entry *defentry
310 = (struct definedness_hash_entry *)
311 bfd_hash_lookup (&definedness_table, name, TRUE, FALSE);
312
313 if (defentry == NULL)
df5f2391 314 einfo (_("%F%P: bfd_hash_lookup failed creating symbol %s\n"), name);
18d6a79d
AM
315
316 /* If the symbol was already defined, and not by a script, then it
cd8e2bcf 317 must be defined by an object file or by the linker target code. */
422f1c65 318 ret = TRUE;
165f707a 319 if (!h->ldscript_def
cd8e2bcf
AM
320 && (h->type == bfd_link_hash_defined
321 || h->type == bfd_link_hash_defweak
322 || h->type == bfd_link_hash_common))
422f1c65
AM
323 {
324 defentry->by_object = 1;
325 if (h->type == bfd_link_hash_defined
326 && h->u.def.section->output_section != NULL
327 && !h->linker_def)
328 ret = FALSE;
329 }
18d6a79d 330
18d6a79d 331 defentry->iteration = lang_statement_iteration;
975f8a9e
AM
332 defentry->final_sec = bfd_abs_section_ptr;
333 if (expld.phase == lang_final_phase_enum
334 && expld.rel_from_abs
335 && expld.result.section == bfd_abs_section_ptr)
336 defentry->final_sec = section_for_dot ();
422f1c65 337 return ret;
18d6a79d
AM
338}
339
d7475e03
L
340static void
341fold_segment_end (seg_align_type *seg)
342{
343 if (expld.phase == lang_first_phase_enum
344 || expld.section != bfd_abs_section_ptr)
345 {
346 expld.result.valid_p = FALSE;
347 }
348 else if (seg->phase == exp_seg_align_seen
349 || seg->phase == exp_seg_relro_seen)
350 {
351 seg->phase = exp_seg_end_seen;
352 seg->end = expld.result.value;
353 }
354 else if (seg->phase == exp_seg_done
355 || seg->phase == exp_seg_adjust
356 || seg->phase == exp_seg_relro_adjust)
357 {
358 /* OK. */
359 }
360 else
361 expld.result.valid_p = FALSE;
362}
363
e9ee469a
AM
364static void
365fold_unary (etree_type *tree)
0ae1cf52 366{
e9ee469a
AM
367 exp_fold_tree_1 (tree->unary.child);
368 if (expld.result.valid_p)
0ae1cf52
AM
369 {
370 switch (tree->type.node_code)
371 {
372 case ALIGN_K:
e9ee469a 373 if (expld.phase != lang_first_phase_enum)
e0a3af22 374 new_rel_from_abs (align_n (expld.dot, expld.result.value));
0ae1cf52 375 else
e9ee469a 376 expld.result.valid_p = FALSE;
0ae1cf52
AM
377 break;
378
379 case ABSOLUTE:
e9ee469a 380 make_abs ();
0ae1cf52
AM
381 break;
382
2e53f7d6
NC
383 case LOG2CEIL:
384 make_log2ceil ();
385 break;
386
0ae1cf52 387 case '~':
e9ee469a 388 expld.result.value = ~expld.result.value;
0ae1cf52
AM
389 break;
390
391 case '!':
e9ee469a 392 expld.result.value = !expld.result.value;
0ae1cf52
AM
393 break;
394
395 case '-':
e9ee469a 396 expld.result.value = -expld.result.value;
0ae1cf52
AM
397 break;
398
399 case NEXT:
400 /* Return next place aligned to value. */
e9ee469a 401 if (expld.phase != lang_first_phase_enum)
0ae1cf52 402 {
e9ee469a 403 make_abs ();
e0a3af22 404 expld.result.value = align_n (expld.dot, expld.result.value);
0ae1cf52
AM
405 }
406 else
e9ee469a 407 expld.result.valid_p = FALSE;
0ae1cf52
AM
408 break;
409
410 case DATA_SEGMENT_END:
d7475e03 411 fold_segment_end (&expld.dataseg);
0ae1cf52
AM
412 break;
413
414 default:
415 FAIL ();
416 break;
417 }
418 }
0ae1cf52
AM
419}
420
94b41882
AM
421/* Arithmetic operators, bitwise AND, bitwise OR and XOR keep the
422 section of one of their operands only when the other operand is a
423 plain number. Losing the section when operating on two symbols,
424 ie. a result of a plain number, is required for subtraction and
425 XOR. It's justifiable for the other operations on the grounds that
426 adding, multiplying etc. two section relative values does not
427 really make sense unless they are just treated as numbers.
428 The same argument could be made for many expressions involving one
429 symbol and a number. For example, "1 << x" and "100 / x" probably
430 should not be given the section of x. The trouble is that if we
431 fuss about such things the rules become complex and it is onerous
432 to document ld expression evaluation. */
433static void
434arith_result_section (const etree_value_type *lhs)
435{
436 if (expld.result.section == lhs->section)
437 {
438 if (expld.section == bfd_abs_section_ptr
439 && !config.sane_expr)
440 /* Duplicate the insanity in exp_fold_tree_1 case etree_value. */
441 expld.result.section = bfd_abs_section_ptr;
442 else
443 expld.result.section = NULL;
444 }
445}
446
d7475e03
L
447static void
448fold_segment_align (seg_align_type *seg, etree_value_type *lhs)
449{
450 seg->relro = exp_seg_relro_start;
451 if (expld.phase == lang_first_phase_enum
452 || expld.section != bfd_abs_section_ptr)
453 expld.result.valid_p = FALSE;
454 else
455 {
456 bfd_vma maxpage = lhs->value;
457 bfd_vma commonpage = expld.result.value;
458
459 expld.result.value = align_n (expld.dot, maxpage);
460 if (seg->phase == exp_seg_relro_adjust)
461 expld.result.value = seg->base;
462 else if (seg->phase == exp_seg_adjust)
463 {
464 if (commonpage < maxpage)
465 expld.result.value += ((expld.dot + commonpage - 1)
466 & (maxpage - commonpage));
467 }
468 else
469 {
470 expld.result.value += expld.dot & (maxpage - 1);
471 if (seg->phase == exp_seg_done)
472 {
473 /* OK. */
474 }
475 else if (seg->phase == exp_seg_none)
476 {
477 seg->phase = exp_seg_align_seen;
478 seg->base = expld.result.value;
479 seg->pagesize = commonpage;
480 seg->maxpagesize = maxpage;
481 seg->relro_end = 0;
482 }
483 else
484 expld.result.valid_p = FALSE;
485 }
486 }
487}
488
489static void
490fold_segment_relro_end (seg_align_type *seg, etree_value_type *lhs)
491{
492 /* Operands swapped! XXX_SEGMENT_RELRO_END(offset,exp) has offset
493 in expld.result and exp in lhs. */
494 seg->relro = exp_seg_relro_end;
495 seg->relro_offset = expld.result.value;
496 if (expld.phase == lang_first_phase_enum
497 || expld.section != bfd_abs_section_ptr)
498 expld.result.valid_p = FALSE;
499 else if (seg->phase == exp_seg_align_seen
500 || seg->phase == exp_seg_adjust
501 || seg->phase == exp_seg_relro_adjust
502 || seg->phase == exp_seg_done)
503 {
504 if (seg->phase == exp_seg_align_seen
505 || seg->phase == exp_seg_relro_adjust)
506 seg->relro_end = lhs->value + expld.result.value;
507
508 if (seg->phase == exp_seg_relro_adjust
509 && (seg->relro_end & (seg->pagesize - 1)))
510 {
511 seg->relro_end += seg->pagesize - 1;
512 seg->relro_end &= ~(seg->pagesize - 1);
513 expld.result.value = seg->relro_end - expld.result.value;
514 }
515 else
516 expld.result.value = lhs->value;
517
518 if (seg->phase == exp_seg_align_seen)
519 seg->phase = exp_seg_relro_seen;
520 }
521 else
522 expld.result.valid_p = FALSE;
523}
524
e9ee469a
AM
525static void
526fold_binary (etree_type *tree)
252b5132 527{
4ac0c898 528 etree_value_type lhs;
e9ee469a 529 exp_fold_tree_1 (tree->binary.lhs);
ba916c8a
MM
530
531 /* The SEGMENT_START operator is special because its first
8c0848b5
AM
532 operand is a string, not the name of a symbol. Note that the
533 operands have been swapped, so binary.lhs is second (default)
534 operand, binary.rhs is first operand. */
e9ee469a 535 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
ba916c8a
MM
536 {
537 const char *segment_name;
538 segment_type *seg;
7542af2a 539
ba916c8a
MM
540 /* Check to see if the user has overridden the default
541 value. */
542 segment_name = tree->binary.rhs->name.name;
e4492aa0 543 for (seg = segments; seg; seg = seg->next)
ba916c8a
MM
544 if (strcmp (seg->name, segment_name) == 0)
545 {
c8ce5710
L
546 if (!seg->used
547 && config.magic_demand_paged
a000f881 548 && config.maxpagesize != 0
c8ce5710 549 && (seg->value % config.maxpagesize) != 0)
0aa7f586
AM
550 einfo (_("%P: warning: address of `%s' "
551 "isn't multiple of maximum page size\n"),
c8ce5710 552 segment_name);
ba916c8a 553 seg->used = TRUE;
7542af2a 554 new_rel_from_abs (seg->value);
ba916c8a
MM
555 break;
556 }
4ac0c898 557 return;
ba916c8a 558 }
252b5132 559
4ac0c898
AM
560 lhs = expld.result;
561 exp_fold_tree_1 (tree->binary.rhs);
562 expld.result.valid_p &= lhs.valid_p;
563
564 if (expld.result.valid_p)
565 {
7542af2a 566 if (lhs.section != expld.result.section)
252b5132 567 {
7542af2a
AM
568 /* If the values are from different sections, and neither is
569 just a number, make both the source arguments absolute. */
570 if (expld.result.section != NULL
571 && lhs.section != NULL)
572 {
573 make_abs ();
574 lhs.value += lhs.section->vma;
9bc8bb33 575 lhs.section = bfd_abs_section_ptr;
7542af2a
AM
576 }
577
578 /* If the rhs is just a number, keep the lhs section. */
579 else if (expld.result.section == NULL)
9bc8bb33
AM
580 {
581 expld.result.section = lhs.section;
582 /* Make this NULL so that we know one of the operands
583 was just a number, for later tests. */
584 lhs.section = NULL;
585 }
4ac0c898 586 }
9bc8bb33
AM
587 /* At this point we know that both operands have the same
588 section, or at least one of them is a plain number. */
252b5132 589
4ac0c898
AM
590 switch (tree->type.node_code)
591 {
e9ee469a 592#define BOP(x, y) \
7542af2a
AM
593 case x: \
594 expld.result.value = lhs.value y expld.result.value; \
94b41882 595 arith_result_section (&lhs); \
7542af2a
AM
596 break;
597
9bc8bb33
AM
598 /* Comparison operators, logical AND, and logical OR always
599 return a plain number. */
7542af2a
AM
600#define BOPN(x, y) \
601 case x: \
602 expld.result.value = lhs.value y expld.result.value; \
603 expld.result.section = NULL; \
604 break;
e9ee469a 605
4ac0c898
AM
606 BOP ('+', +);
607 BOP ('*', *);
608 BOP ('-', -);
609 BOP (LSHIFT, <<);
610 BOP (RSHIFT, >>);
4ac0c898
AM
611 BOP ('&', &);
612 BOP ('^', ^);
613 BOP ('|', |);
7542af2a
AM
614 BOPN (EQ, ==);
615 BOPN (NE, !=);
616 BOPN ('<', <);
617 BOPN ('>', >);
618 BOPN (LE, <=);
619 BOPN (GE, >=);
620 BOPN (ANDAND, &&);
621 BOPN (OROR, ||);
4ac0c898 622
9bc8bb33
AM
623 case '%':
624 if (expld.result.value != 0)
625 expld.result.value = ((bfd_signed_vma) lhs.value
626 % (bfd_signed_vma) expld.result.value);
627 else if (expld.phase != lang_mark_phase_enum)
df5f2391 628 einfo (_("%F%P:%pS %% by zero\n"), tree->binary.rhs);
94b41882 629 arith_result_section (&lhs);
9bc8bb33
AM
630 break;
631
632 case '/':
633 if (expld.result.value != 0)
634 expld.result.value = ((bfd_signed_vma) lhs.value
635 / (bfd_signed_vma) expld.result.value);
636 else if (expld.phase != lang_mark_phase_enum)
df5f2391 637 einfo (_("%F%P:%pS / by zero\n"), tree->binary.rhs);
94b41882 638 arith_result_section (&lhs);
9bc8bb33
AM
639 break;
640
4ac0c898
AM
641 case MAX_K:
642 if (lhs.value > expld.result.value)
643 expld.result.value = lhs.value;
644 break;
252b5132 645
4ac0c898
AM
646 case MIN_K:
647 if (lhs.value < expld.result.value)
648 expld.result.value = lhs.value;
649 break;
252b5132 650
4ac0c898
AM
651 case ALIGN_K:
652 expld.result.value = align_n (lhs.value, expld.result.value);
653 break;
c468c8bc 654
4ac0c898 655 case DATA_SEGMENT_ALIGN:
d7475e03 656 fold_segment_align (&expld.dataseg, &lhs);
4ac0c898 657 break;
e9ee469a 658
4ac0c898 659 case DATA_SEGMENT_RELRO_END:
d7475e03 660 fold_segment_relro_end (&expld.dataseg, &lhs);
4ac0c898
AM
661 break;
662
663 default:
664 FAIL ();
252b5132 665 }
252b5132 666 }
252b5132
RH
667}
668
e9ee469a
AM
669static void
670fold_trinary (etree_type *tree)
0ae1cf52 671{
cd5b2bab
AM
672 struct bfd_link_hash_entry *save = expld.assign_src;
673
e9ee469a 674 exp_fold_tree_1 (tree->trinary.cond);
cd5b2bab 675 expld.assign_src = save;
e9ee469a
AM
676 if (expld.result.valid_p)
677 exp_fold_tree_1 (expld.result.value
678 ? tree->trinary.lhs
679 : tree->trinary.rhs);
0ae1cf52
AM
680}
681
e9ee469a
AM
682static void
683fold_name (etree_type *tree)
252b5132 684{
165f707a
AM
685 struct bfd_link_hash_entry *h;
686 struct definedness_hash_entry *def;
687
e9ee469a 688 memset (&expld.result, 0, sizeof (expld.result));
c468c8bc 689
4de2d33d 690 switch (tree->type.node_code)
8c95a62e
KH
691 {
692 case SIZEOF_HEADERS:
e9ee469a
AM
693 if (expld.phase != lang_first_phase_enum)
694 {
695 bfd_vma hdr_size = 0;
696 /* Don't find the real header size if only marking sections;
697 The bfd function may cache incorrect data. */
698 if (expld.phase != lang_mark_phase_enum)
f13a99db 699 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
7542af2a 700 new_number (hdr_size);
e9ee469a 701 }
8c95a62e 702 break;
67469e1f 703
8c95a62e 704 case DEFINED:
165f707a
AM
705 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
706 &link_info,
707 tree->name.name,
708 FALSE, FALSE, TRUE);
709 new_number (h != NULL
710 && (h->type == bfd_link_hash_defined
711 || h->type == bfd_link_hash_defweak
712 || h->type == bfd_link_hash_common)
713 && (!h->ldscript_def
714 || (def = symbol_defined (tree->name.name)) == NULL
715 || def->by_object
716 || def->iteration == (lang_statement_iteration & 255)));
8c95a62e 717 break;
67469e1f 718
8c95a62e 719 case NAME:
4194268f
AM
720 if (expld.assign_name != NULL
721 && strcmp (expld.assign_name, tree->name.name) == 0)
fa72205c
AM
722 {
723 /* Self-assignment is only allowed for absolute symbols
724 defined in a linker script. */
fa72205c
AM
725 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
726 &link_info,
727 tree->name.name,
728 FALSE, FALSE, TRUE);
729 if (!(h != NULL
730 && (h->type == bfd_link_hash_defined
731 || h->type == bfd_link_hash_defweak)
732 && h->u.def.section == bfd_abs_section_ptr
18d6a79d 733 && (def = symbol_defined (tree->name.name)) != NULL
165f707a 734 && def->iteration == (lang_statement_iteration & 255)))
fa72205c
AM
735 expld.assign_name = NULL;
736 }
165f707a 737 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
e9ee469a
AM
738 new_rel_from_abs (expld.dot);
739 else
8c95a62e 740 {
f13a99db
AM
741 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
742 &link_info,
8c95a62e 743 tree->name.name,
1b493742
NS
744 TRUE, FALSE, TRUE);
745 if (!h)
df5f2391 746 einfo (_("%F%P: bfd_link_hash_lookup failed: %E\n"));
1b493742
NS
747 else if (h->type == bfd_link_hash_defined
748 || h->type == bfd_link_hash_defweak)
8c95a62e 749 {
7542af2a 750 asection *output_section;
8c95a62e 751
7542af2a
AM
752 output_section = h->u.def.section->output_section;
753 if (output_section == NULL)
754 {
165f707a 755 if (expld.phase <= lang_mark_phase_enum)
2aa9aad9
NC
756 new_rel (h->u.def.value, h->u.def.section);
757 else
df5f2391 758 einfo (_("%X%P:%pS: unresolvable symbol `%s'"
7542af2a 759 " referenced in expression\n"),
dab69f68 760 tree, tree->name.name);
8c95a62e 761 }
5c3049d2
AM
762 else if (output_section == bfd_abs_section_ptr
763 && (expld.section != bfd_abs_section_ptr
01554a74 764 || config.sane_expr))
abf4be64 765 new_number (h->u.def.value + h->u.def.section->output_offset);
7542af2a
AM
766 else
767 new_rel (h->u.def.value + h->u.def.section->output_offset,
768 output_section);
8c95a62e 769 }
e9ee469a 770 else if (expld.phase == lang_final_phase_enum
ec8460b5
AM
771 || (expld.phase != lang_mark_phase_enum
772 && expld.assigning_to_dot))
df5f2391 773 einfo (_("%F%P:%pS: undefined symbol `%s'"
dab69f68
AM
774 " referenced in expression\n"),
775 tree, tree->name.name);
1b493742
NS
776 else if (h->type == bfd_link_hash_new)
777 {
778 h->type = bfd_link_hash_undefined;
779 h->u.undef.abfd = NULL;
3eda52aa 780 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
a010d60f 781 bfd_link_add_undef (link_info.hash, h);
1b493742 782 }
cd5b2bab
AM
783 if (expld.assign_src == NULL)
784 expld.assign_src = h;
785 else
e99955cd 786 expld.assign_src = (struct bfd_link_hash_entry *) - 1;
8c95a62e
KH
787 }
788 break;
789
790 case ADDR:
e9ee469a 791 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
792 {
793 lang_output_section_statement_type *os;
794
795 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
796 if (os == NULL)
797 {
798 if (expld.phase == lang_final_phase_enum)
df5f2391 799 einfo (_("%F%P:%pS: undefined section `%s'"
dab69f68
AM
800 " referenced in expression\n"),
801 tree, tree->name.name);
cc3e2771
NS
802 }
803 else if (os->processed_vma)
5942515f 804 new_rel (0, os->bfd_section);
8c95a62e 805 }
8c95a62e
KH
806 break;
807
808 case LOADADDR:
e9ee469a 809 if (expld.phase != lang_first_phase_enum)
8c95a62e
KH
810 {
811 lang_output_section_statement_type *os;
812
813 os = lang_output_section_find (tree->name.name);
cc3e2771
NS
814 if (os == NULL)
815 {
816 if (expld.phase == lang_final_phase_enum)
df5f2391 817 einfo (_("%F%P:%pS: undefined section `%s'"
dab69f68
AM
818 " referenced in expression\n"),
819 tree, tree->name.name);
cc3e2771
NS
820 }
821 else if (os->processed_lma)
1b493742 822 {
e9ee469a 823 if (os->load_base == NULL)
3e23777d 824 new_abs (os->bfd_section->lma);
e9ee469a 825 else
67469e1f
AM
826 {
827 exp_fold_tree_1 (os->load_base);
819da74e
AM
828 if (expld.result.valid_p)
829 make_abs ();
67469e1f 830 }
1b493742 831 }
8c95a62e 832 }
8c95a62e
KH
833 break;
834
835 case SIZEOF:
362c1d1a 836 case ALIGNOF:
e9ee469a 837 if (expld.phase != lang_first_phase_enum)
8c95a62e 838 {
8c95a62e
KH
839 lang_output_section_statement_type *os;
840
841 os = lang_output_section_find (tree->name.name);
5397b1fe 842 if (os == NULL)
cc3e2771
NS
843 {
844 if (expld.phase == lang_final_phase_enum)
df5f2391 845 einfo (_("%F%P:%pS: undefined section `%s'"
dab69f68
AM
846 " referenced in expression\n"),
847 tree, tree->name.name);
7542af2a 848 new_number (0);
cc3e2771 849 }
17d6eea5 850 else if (os->bfd_section != NULL)
362c1d1a
NS
851 {
852 bfd_vma val;
853
854 if (tree->type.node_code == SIZEOF)
f13a99db
AM
855 val = (os->bfd_section->size
856 / bfd_octets_per_byte (link_info.output_bfd));
362c1d1a
NS
857 else
858 val = (bfd_vma)1 << os->bfd_section->alignment_power;
e4492aa0 859
7542af2a 860 new_number (val);
362c1d1a 861 }
17d6eea5
L
862 else
863 new_number (0);
8c95a62e 864 }
8c95a62e
KH
865 break;
866
3ec57632
NC
867 case LENGTH:
868 {
cc9ad334 869 if (expld.phase != lang_first_phase_enum)
0aa7f586
AM
870 {
871 lang_memory_region_type *mem;
872
873 mem = lang_memory_region_lookup (tree->name.name, FALSE);
874 if (mem != NULL)
875 new_number (mem->length);
876 else
df5f2391 877 einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
0aa7f586
AM
878 " referenced in expression\n"),
879 tree, tree->name.name);
880 }
3ec57632
NC
881 }
882 break;
883
884 case ORIGIN:
7542af2a
AM
885 if (expld.phase != lang_first_phase_enum)
886 {
887 lang_memory_region_type *mem;
e4492aa0
L
888
889 mem = lang_memory_region_lookup (tree->name.name, FALSE);
890 if (mem != NULL)
7542af2a 891 new_rel_from_abs (mem->origin);
e4492aa0 892 else
df5f2391 893 einfo (_("%F%P:%pS: undefined MEMORY region `%s'"
dab69f68
AM
894 " referenced in expression\n"),
895 tree, tree->name.name);
7542af2a 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
AB
1157 if (expld.phase == lang_final_phase_enum
1158 || expld.assign_name != NULL)
e9ee469a 1159 {
eab62f2f
AB
1160 if (tree->type.node_class == etree_provide)
1161 tree->type.node_class = etree_provided;
1162
1579bae1 1163 if (h == NULL)
67010b46 1164 {
e9ee469a
AM
1165 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1166 TRUE, FALSE, TRUE);
1167 if (h == NULL)
df5f2391 1168 einfo (_("%F%P:%s: hash creation failed\n"),
67010b46
NC
1169 tree->assign.dst);
1170 }
e9ee469a 1171
eab62f2f
AB
1172 /* If the expression is not valid then fake a zero value. In
1173 the final phase any errors will already have been raised,
1174 in earlier phases we want to create this definition so
1175 that it can be seen by other expressions. */
1176 if (!expld.result.valid_p
1177 && h->type == bfd_link_hash_new)
1178 {
1179 expld.result.value = 0;
1180 expld.result.section = NULL;
1181 expld.result.valid_p = TRUE;
1182 }
1183
1184 if (expld.result.valid_p)
422f1c65 1185 {
eab62f2f
AB
1186 if (expld.result.section == NULL)
1187 expld.result.section = expld.section;
1188 if (!update_definedness (tree->assign.dst, h) && 0)
1189 {
1190 /* Symbol was already defined. For now this error
1191 is disabled because it causes failures in the ld
1192 testsuite: ld-elf/var1, ld-scripts/defined5, and
1193 ld-scripts/pr14962. Some of these no doubt
1194 reflect scripts used in the wild. */
1195 (*link_info.callbacks->multiple_definition)
1196 (&link_info, h, link_info.output_bfd,
1197 expld.result.section, expld.result.value);
1198 }
1199 h->type = bfd_link_hash_defined;
1200 h->u.def.value = expld.result.value;
1201 h->u.def.section = expld.result.section;
1202 h->linker_def = ! tree->assign.type.lineno;
1203 h->ldscript_def = 1;
d5c928c0 1204 h->rel_from_abs = expld.rel_from_abs;
34a87bb0
L
1205 if (tree->assign.hidden)
1206 bfd_link_hide_symbol (link_info.output_bfd,
1207 &link_info, h);
eab62f2f
AB
1208
1209 /* Copy the symbol type if this is an expression only
1210 referencing a single symbol. (If the expression
1211 contains ternary conditions, ignoring symbols on
1212 false branches.) */
1213 if (expld.assign_src != NULL
1214 && (expld.assign_src
e99955cd 1215 != (struct bfd_link_hash_entry *) -1))
eab62f2f
AB
1216 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
1217 expld.assign_src);
422f1c65 1218 }
e092cb30 1219 }
4194268f 1220 expld.assign_name = NULL;
252b5132
RH
1221 }
1222 break;
1223
1224 case etree_name:
e9ee469a 1225 fold_name (tree);
252b5132
RH
1226 break;
1227
1228 default:
1229 FAIL ();
e9ee469a 1230 memset (&expld.result, 0, sizeof (expld.result));
252b5132
RH
1231 break;
1232 }
252b5132
RH
1233}
1234
e9ee469a
AM
1235void
1236exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
75ff4589 1237{
975f8a9e 1238 expld.rel_from_abs = FALSE;
e9ee469a
AM
1239 expld.dot = *dotp;
1240 expld.dotp = dotp;
1241 expld.section = current_section;
1242 exp_fold_tree_1 (tree);
75ff4589
L
1243}
1244
e759c116 1245void
e9ee469a 1246exp_fold_tree_no_dot (etree_type *tree)
252b5132 1247{
975f8a9e 1248 expld.rel_from_abs = FALSE;
e9ee469a
AM
1249 expld.dot = 0;
1250 expld.dotp = NULL;
1251 expld.section = bfd_abs_section_ptr;
1252 exp_fold_tree_1 (tree);
252b5132
RH
1253}
1254
9ad39107
AM
1255static void
1256exp_value_fold (etree_type *tree)
1257{
1258 exp_fold_tree_no_dot (tree);
1259 if (expld.result.valid_p)
1260 {
1261 tree->type.node_code = INT;
1262 tree->value.value = expld.result.value;
1263 tree->value.str = NULL;
1264 tree->type.node_class = etree_value;
1265 }
1266}
1267
769553e6
AM
1268#define MAX(a, b) ((a) > (b) ? (a) : (b))
1269
252b5132 1270etree_type *
1579bae1 1271exp_binop (int code, etree_type *lhs, etree_type *rhs)
252b5132 1272{
769553e6
AM
1273 etree_type *new_e = (etree_type *) stat_alloc (MAX (sizeof (new_e->binary),
1274 sizeof (new_e->value)));
b751e639
AM
1275 new_e->type.node_code = code;
1276 new_e->type.filename = lhs->type.filename;
1277 new_e->type.lineno = lhs->type.lineno;
1278 new_e->binary.lhs = lhs;
1279 new_e->binary.rhs = rhs;
1280 new_e->type.node_class = etree_binary;
9ad39107
AM
1281 if (lhs->type.node_class == etree_value
1282 && rhs->type.node_class == etree_value
1283 && code != ALIGN_K
1284 && code != DATA_SEGMENT_ALIGN
1285 && code != DATA_SEGMENT_RELRO_END)
1286 exp_value_fold (new_e);
d3ce72d0 1287 return new_e;
252b5132
RH
1288}
1289
1290etree_type *
1579bae1 1291exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
252b5132 1292{
769553e6
AM
1293 etree_type *new_e = (etree_type *) stat_alloc (MAX (sizeof (new_e->trinary),
1294 sizeof (new_e->value)));
b751e639
AM
1295 new_e->type.node_code = code;
1296 new_e->type.filename = cond->type.filename;
1297 new_e->type.lineno = cond->type.lineno;
1298 new_e->trinary.lhs = lhs;
1299 new_e->trinary.cond = cond;
1300 new_e->trinary.rhs = rhs;
1301 new_e->type.node_class = etree_trinary;
9ad39107
AM
1302 if (cond->type.node_class == etree_value
1303 && lhs->type.node_class == etree_value
1304 && rhs->type.node_class == etree_value)
1305 exp_value_fold (new_e);
d3ce72d0 1306 return new_e;
252b5132
RH
1307}
1308
252b5132 1309etree_type *
1579bae1 1310exp_unop (int code, etree_type *child)
252b5132 1311{
769553e6
AM
1312 etree_type *new_e = (etree_type *) stat_alloc (MAX (sizeof (new_e->unary),
1313 sizeof (new_e->value)));
b751e639
AM
1314 new_e->unary.type.node_code = code;
1315 new_e->unary.type.filename = child->type.filename;
1316 new_e->unary.type.lineno = child->type.lineno;
1317 new_e->unary.child = child;
1318 new_e->unary.type.node_class = etree_unary;
9ad39107
AM
1319 if (child->type.node_class == etree_value
1320 && code != ALIGN_K
1321 && code != ABSOLUTE
1322 && code != NEXT
1323 && code != DATA_SEGMENT_END)
1324 exp_value_fold (new_e);
d3ce72d0 1325 return new_e;
252b5132
RH
1326}
1327
252b5132 1328etree_type *
1579bae1 1329exp_nameop (int code, const char *name)
252b5132 1330{
b751e639 1331 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
c7d701b0 1332
b751e639
AM
1333 new_e->name.type.node_code = code;
1334 new_e->name.type.filename = ldlex_filename ();
1335 new_e->name.type.lineno = lineno;
1336 new_e->name.name = name;
1337 new_e->name.type.node_class = etree_name;
d3ce72d0 1338 return new_e;
252b5132
RH
1339
1340}
1341
2e57b2af
AM
1342static etree_type *
1343exp_assop (const char *dst,
1344 etree_type *src,
1345 enum node_tree_enum class,
1346 bfd_boolean hidden)
252b5132
RH
1347{
1348 etree_type *n;
1349
1e9cc1c2 1350 n = (etree_type *) stat_alloc (sizeof (n->assign));
252b5132 1351 n->assign.type.node_code = '=';
dab69f68 1352 n->assign.type.filename = src->type.filename;
f856040a 1353 n->assign.type.lineno = src->type.lineno;
2e57b2af 1354 n->assign.type.node_class = class;
252b5132
RH
1355 n->assign.src = src;
1356 n->assign.dst = dst;
7af8e998 1357 n->assign.hidden = hidden;
252b5132
RH
1358 return n;
1359}
1360
eb8476a6
MR
1361/* Handle linker script assignments and HIDDEN. */
1362
2e57b2af 1363etree_type *
eb8476a6 1364exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
2e57b2af 1365{
65f14869 1366 return exp_assop (dst, src, etree_assign, hidden);
2e57b2af
AM
1367}
1368
eb8476a6
MR
1369/* Handle --defsym command-line option. */
1370
2e57b2af
AM
1371etree_type *
1372exp_defsym (const char *dst, etree_type *src)
1373{
65f14869 1374 return exp_assop (dst, src, etree_assign, FALSE);
2e57b2af
AM
1375}
1376
1377/* Handle PROVIDE. */
1378
1379etree_type *
1380exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1381{
65f14869 1382 return exp_assop (dst, src, etree_provide, hidden);
2e57b2af
AM
1383}
1384
252b5132
RH
1385/* Handle ASSERT. */
1386
1387etree_type *
1579bae1 1388exp_assert (etree_type *exp, const char *message)
252b5132
RH
1389{
1390 etree_type *n;
1391
1e9cc1c2 1392 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
252b5132 1393 n->assert_s.type.node_code = '!';
dab69f68 1394 n->assert_s.type.filename = exp->type.filename;
f856040a 1395 n->assert_s.type.lineno = exp->type.lineno;
252b5132
RH
1396 n->assert_s.type.node_class = etree_assert;
1397 n->assert_s.child = exp;
1398 n->assert_s.message = message;
1399 return n;
1400}
1401
4de2d33d 1402void
1579bae1 1403exp_print_tree (etree_type *tree)
252b5132 1404{
ae78bbeb
AM
1405 bfd_boolean function_like;
1406
c7d701b0
NC
1407 if (config.map_file == NULL)
1408 config.map_file = stderr;
b7a26f91 1409
c7d701b0
NC
1410 if (tree == NULL)
1411 {
1412 minfo ("NULL TREE\n");
1413 return;
1414 }
b7a26f91 1415
8c95a62e
KH
1416 switch (tree->type.node_class)
1417 {
1418 case etree_value:
1419 minfo ("0x%v", tree->value.value);
1420 return;
1421 case etree_rel:
1422 if (tree->rel.section->owner != NULL)
871b3ab2 1423 minfo ("%pB:", tree->rel.section->owner);
8c95a62e
KH
1424 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1425 return;
1426 case etree_assign:
ae78bbeb 1427 fputs (tree->assign.dst, config.map_file);
b34976b6 1428 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1429 exp_print_tree (tree->assign.src);
1430 break;
1431 case etree_provide:
b46a87b1 1432 case etree_provided:
b3b9f8d3 1433 fprintf (config.map_file, "PROVIDE (%s = ", tree->assign.dst);
8c95a62e 1434 exp_print_tree (tree->assign.src);
ae78bbeb 1435 fputc (')', config.map_file);
8c95a62e
KH
1436 break;
1437 case etree_binary:
ae78bbeb
AM
1438 function_like = FALSE;
1439 switch (tree->type.node_code)
1440 {
1441 case MAX_K:
1442 case MIN_K:
1443 case ALIGN_K:
1444 case DATA_SEGMENT_ALIGN:
1445 case DATA_SEGMENT_RELRO_END:
1446 function_like = TRUE;
67baf8c4
TG
1447 break;
1448 case SEGMENT_START:
1449 /* Special handling because arguments are in reverse order and
1450 the segment name is quoted. */
1451 exp_print_token (tree->type.node_code, FALSE);
1452 fputs (" (\"", config.map_file);
1453 exp_print_tree (tree->binary.rhs);
1454 fputs ("\", ", config.map_file);
1455 exp_print_tree (tree->binary.lhs);
1456 fputc (')', config.map_file);
1457 return;
ae78bbeb
AM
1458 }
1459 if (function_like)
1460 {
1461 exp_print_token (tree->type.node_code, FALSE);
1462 fputc (' ', config.map_file);
1463 }
1464 fputc ('(', config.map_file);
8c95a62e 1465 exp_print_tree (tree->binary.lhs);
ae78bbeb
AM
1466 if (function_like)
1467 fprintf (config.map_file, ", ");
1468 else
1469 exp_print_token (tree->type.node_code, TRUE);
8c95a62e 1470 exp_print_tree (tree->binary.rhs);
ae78bbeb 1471 fputc (')', config.map_file);
8c95a62e
KH
1472 break;
1473 case etree_trinary:
1474 exp_print_tree (tree->trinary.cond);
ae78bbeb 1475 fputc ('?', config.map_file);
8c95a62e 1476 exp_print_tree (tree->trinary.lhs);
ae78bbeb 1477 fputc (':', config.map_file);
8c95a62e
KH
1478 exp_print_tree (tree->trinary.rhs);
1479 break;
1480 case etree_unary:
b34976b6 1481 exp_print_token (tree->unary.type.node_code, FALSE);
8c95a62e
KH
1482 if (tree->unary.child)
1483 {
7b17f854 1484 fprintf (config.map_file, " (");
8c95a62e 1485 exp_print_tree (tree->unary.child);
ae78bbeb 1486 fputc (')', config.map_file);
8c95a62e
KH
1487 }
1488 break;
1489
1490 case etree_assert:
1491 fprintf (config.map_file, "ASSERT (");
1492 exp_print_tree (tree->assert_s.child);
1493 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1494 break;
1495
8c95a62e
KH
1496 case etree_name:
1497 if (tree->type.node_code == NAME)
ae78bbeb 1498 fputs (tree->name.name, config.map_file);
8c95a62e
KH
1499 else
1500 {
b34976b6 1501 exp_print_token (tree->type.node_code, FALSE);
8c95a62e 1502 if (tree->name.name)
7b17f854 1503 fprintf (config.map_file, " (%s)", tree->name.name);
8c95a62e
KH
1504 }
1505 break;
1506 default:
1507 FAIL ();
1508 break;
252b5132 1509 }
252b5132
RH
1510}
1511
1512bfd_vma
e9ee469a 1513exp_get_vma (etree_type *tree, bfd_vma def, char *name)
252b5132 1514{
252b5132
RH
1515 if (tree != NULL)
1516 {
e9ee469a
AM
1517 exp_fold_tree_no_dot (tree);
1518 if (expld.result.valid_p)
1519 return expld.result.value;
1520 else if (name != NULL && expld.phase != lang_mark_phase_enum)
df5f2391 1521 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
dab69f68 1522 tree, name);
252b5132 1523 }
e9ee469a 1524 return def;
252b5132
RH
1525}
1526
3d9c8f6b
AM
1527/* Return the smallest non-negative integer such that two raised to
1528 that power is at least as large as the vma evaluated at TREE, if
1529 TREE is a non-NULL expression that can be resolved. If TREE is
1530 NULL or cannot be resolved, return -1. */
1531
4de2d33d 1532int
3d9c8f6b 1533exp_get_power (etree_type *tree, char *name)
252b5132 1534{
3d9c8f6b
AM
1535 bfd_vma x = exp_get_vma (tree, -1, name);
1536 bfd_vma p2;
1537 int n;
1538
1539 if (x == (bfd_vma) -1)
1540 return -1;
1541
1542 for (n = 0, p2 = 1; p2 < x; ++n, p2 <<= 1)
1543 if (p2 == 0)
1544 break;
1545
1546 return n;
252b5132
RH
1547}
1548
2c382fb6 1549fill_type *
e9ee469a 1550exp_get_fill (etree_type *tree, fill_type *def, char *name)
2c382fb6
AM
1551{
1552 fill_type *fill;
2c382fb6
AM
1553 size_t len;
1554 unsigned int val;
1555
1556 if (tree == NULL)
1557 return def;
1558
e9ee469a
AM
1559 exp_fold_tree_no_dot (tree);
1560 if (!expld.result.valid_p)
1561 {
1562 if (name != NULL && expld.phase != lang_mark_phase_enum)
df5f2391 1563 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
dab69f68 1564 tree, name);
e9ee469a
AM
1565 return def;
1566 }
2c382fb6 1567
e9ee469a 1568 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
2c382fb6
AM
1569 {
1570 unsigned char *dst;
1571 unsigned char *s;
1e9cc1c2 1572 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
2c382fb6
AM
1573 fill->size = (len + 1) / 2;
1574 dst = fill->data;
e9ee469a 1575 s = (unsigned char *) expld.result.str;
2c382fb6
AM
1576 val = 0;
1577 do
1578 {
1579 unsigned int digit;
1580
1581 digit = *s++ - '0';
1582 if (digit > 9)
1583 digit = (digit - 'A' + '0' + 10) & 0xf;
1584 val <<= 4;
1585 val += digit;
1586 --len;
1587 if ((len & 1) == 0)
1588 {
1589 *dst++ = val;
1590 val = 0;
1591 }
1592 }
1593 while (len != 0);
1594 }
1595 else
1596 {
1e9cc1c2 1597 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
e9ee469a 1598 val = expld.result.value;
2c382fb6
AM
1599 fill->data[0] = (val >> 24) & 0xff;
1600 fill->data[1] = (val >> 16) & 0xff;
1601 fill->data[2] = (val >> 8) & 0xff;
1602 fill->data[3] = (val >> 0) & 0xff;
1603 fill->size = 4;
1604 }
1605 return fill;
1606}
1607
252b5132 1608bfd_vma
e9ee469a 1609exp_get_abs_int (etree_type *tree, int def, char *name)
252b5132 1610{
e9ee469a
AM
1611 if (tree != NULL)
1612 {
1613 exp_fold_tree_no_dot (tree);
c7d701b0 1614
e9ee469a
AM
1615 if (expld.result.valid_p)
1616 {
7542af2a
AM
1617 if (expld.result.section != NULL)
1618 expld.result.value += expld.result.section->vma;
e9ee469a
AM
1619 return expld.result.value;
1620 }
1621 else if (name != NULL && expld.phase != lang_mark_phase_enum)
f856040a 1622 {
df5f2391 1623 einfo (_("%F%P:%pS: nonconstant expression for %s\n"),
dab69f68 1624 tree, name);
f856040a 1625 }
e9ee469a
AM
1626 }
1627 return def;
252b5132 1628}
c553bb91 1629
e5caa5e0
AM
1630static bfd_vma
1631align_n (bfd_vma value, bfd_vma align)
c553bb91
AM
1632{
1633 if (align <= 1)
1634 return value;
1635
1636 value = (value + align - 1) / align;
1637 return value * align;
1638}
18d6a79d
AM
1639
1640void
1641ldexp_init (void)
1642{
1643 /* The value "13" is ad-hoc, somewhat related to the expected number of
1644 assignments in a linker script. */
1645 if (!bfd_hash_table_init_n (&definedness_table,
1646 definedness_newfunc,
1647 sizeof (struct definedness_hash_entry),
1648 13))
df5f2391 1649 einfo (_("%F%P: can not create hash table: %E\n"));
18d6a79d
AM
1650}
1651
975f8a9e
AM
1652/* Convert absolute symbols defined by a script from "dot" (also
1653 SEGMENT_START or ORIGIN) outside of an output section statement,
1654 to section relative. */
1655
1656static bfd_boolean
1657set_sym_sections (struct bfd_hash_entry *bh, void *inf ATTRIBUTE_UNUSED)
1658{
1659 struct definedness_hash_entry *def = (struct definedness_hash_entry *) bh;
1660 if (def->final_sec != bfd_abs_section_ptr)
1661 {
1662 struct bfd_link_hash_entry *h;
1663 h = bfd_link_hash_lookup (link_info.hash, bh->string,
1664 FALSE, FALSE, TRUE);
1665 if (h != NULL
1666 && h->type == bfd_link_hash_defined
1667 && h->u.def.section == bfd_abs_section_ptr)
1668 {
1669 h->u.def.value -= def->final_sec->vma;
1670 h->u.def.section = def->final_sec;
1671 }
1672 }
1673 return TRUE;
1674}
1675
1676void
1677ldexp_finalize_syms (void)
1678{
1679 bfd_hash_traverse (&definedness_table, set_sym_sections, NULL);
1680}
1681
18d6a79d
AM
1682void
1683ldexp_finish (void)
1684{
1685 bfd_hash_table_free (&definedness_table);
1686}
This page took 1.061914 seconds and 4 git commands to generate.