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