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