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