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