Correct local label doc
[binutils-gdb.git] / ld / ldexp.c
1 /* This module handles expression trees.
2 Copyright (C) 1991-2015 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
4
5 This file is part of the GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
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.
16
17 You should have received a copy of the GNU General Public License
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
22
23 /* This module is in charge of working out the contents of expressions.
24
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. */
29
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "bfdlink.h"
33
34 #include "ld.h"
35 #include "ldmain.h"
36 #include "ldmisc.h"
37 #include "ldexp.h"
38 #include "ldlex.h"
39 #include <ldgram.h>
40 #include "ldlang.h"
41 #include "libiberty.h"
42 #include "safe-ctype.h"
43
44 static void exp_fold_tree_1 (etree_type *);
45 static bfd_vma align_n (bfd_vma, bfd_vma);
46
47 segment_type *segments;
48
49 struct ldexp_control expld;
50
51 /* This structure records symbols for which we need to keep track of
52 definedness for use in the DEFINED () test. */
53
54 struct 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
62 static struct bfd_hash_table definedness_table;
63
64 /* Print the string representation of the given token. Surround it
65 with spaces if INFIX_P is TRUE. */
66
67 static void
68 exp_print_token (token_code_type code, int infix_p)
69 {
70 static const struct
71 {
72 token_code_type code;
73 const char * name;
74 }
75 table[] =
76 {
77 { INT, "int" },
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, "<<" },
94 { RSHIFT, ">>" },
95 { LOG2CEIL, "LOG2CEIL" },
96 { ALIGN_K, "ALIGN" },
97 { BLOCK, "BLOCK" },
98 { QUAD, "QUAD" },
99 { SQUAD, "SQUAD" },
100 { LONG, "LONG" },
101 { SHORT, "SHORT" },
102 { BYTE, "BYTE" },
103 { SECTIONS, "SECTIONS" },
104 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
105 { MEMORY, "MEMORY" },
106 { DEFINED, "DEFINED" },
107 { TARGET_K, "TARGET" },
108 { SEARCH_DIR, "SEARCH_DIR" },
109 { MAP, "MAP" },
110 { ENTRY, "ENTRY" },
111 { NEXT, "NEXT" },
112 { ALIGNOF, "ALIGNOF" },
113 { SIZEOF, "SIZEOF" },
114 { ADDR, "ADDR" },
115 { LOADADDR, "LOADADDR" },
116 { CONSTANT, "CONSTANT" },
117 { ABSOLUTE, "ABSOLUTE" },
118 { MAX_K, "MAX" },
119 { MIN_K, "MIN" },
120 { ASSERT_K, "ASSERT" },
121 { REL, "relocatable" },
122 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
123 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
124 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
125 { ORIGIN, "ORIGIN" },
126 { LENGTH, "LENGTH" },
127 { SEGMENT_START, "SEGMENT_START" }
128 };
129 unsigned int idx;
130
131 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
132 if (table[idx].code == code)
133 break;
134
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);
142 else
143 fprintf (config.map_file, "<code %d>", code);
144
145 if (infix_p)
146 fputc (' ', config.map_file);
147 }
148
149 static void
150 make_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
171 static void
172 make_abs (void)
173 {
174 if (expld.result.section != NULL)
175 expld.result.value += expld.result.section->vma;
176 expld.result.section = bfd_abs_section_ptr;
177 }
178
179 static void
180 new_abs (bfd_vma value)
181 {
182 expld.result.valid_p = TRUE;
183 expld.result.section = bfd_abs_section_ptr;
184 expld.result.value = value;
185 expld.result.str = NULL;
186 }
187
188 etree_type *
189 exp_intop (bfd_vma value)
190 {
191 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
192 new_e->type.node_code = INT;
193 new_e->type.filename = ldlex_filename ();
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;
199 }
200
201 etree_type *
202 exp_bigintop (bfd_vma value, char *str)
203 {
204 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
205 new_e->type.node_code = INT;
206 new_e->type.filename = ldlex_filename ();
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;
212 }
213
214 /* Build an expression representing an unnamed relocatable value. */
215
216 etree_type *
217 exp_relop (asection *section, bfd_vma value)
218 {
219 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
220 new_e->type.node_code = REL;
221 new_e->type.filename = ldlex_filename ();
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;
227 }
228
229 static void
230 new_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
238 static void
239 new_rel (bfd_vma value, asection *section)
240 {
241 expld.result.valid_p = TRUE;
242 expld.result.value = value;
243 expld.result.str = NULL;
244 expld.result.section = section;
245 }
246
247 static void
248 new_rel_from_abs (bfd_vma value)
249 {
250 asection *s = expld.section;
251
252 if (s == bfd_abs_section_ptr && expld.phase == lang_final_phase_enum)
253 s = section_for_dot ();
254 expld.result.valid_p = TRUE;
255 expld.result.value = value - s->vma;
256 expld.result.str = NULL;
257 expld.result.section = s;
258 }
259
260 /* New-function for the definedness hash table. */
261
262 static struct bfd_hash_entry *
263 definedness_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
287 static struct definedness_hash_entry *
288 symbol_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. Return FALSE if script symbol
295 is multiply defining a strong symbol in an object. */
296
297 static bfd_boolean
298 update_definedness (const char *name, struct bfd_link_hash_entry *h)
299 {
300 bfd_boolean ret;
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
309 must be defined by an object file or by the linker target code. */
310 ret = TRUE;
311 if (!defentry->by_script
312 && (h->type == bfd_link_hash_defined
313 || h->type == bfd_link_hash_defweak
314 || h->type == bfd_link_hash_common))
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 }
322
323 defentry->by_script = 1;
324 defentry->iteration = lang_statement_iteration;
325 return ret;
326 }
327
328 static void
329 fold_unary (etree_type *tree)
330 {
331 exp_fold_tree_1 (tree->unary.child);
332 if (expld.result.valid_p)
333 {
334 switch (tree->type.node_code)
335 {
336 case ALIGN_K:
337 if (expld.phase != lang_first_phase_enum)
338 new_rel_from_abs (align_n (expld.dot, expld.result.value));
339 else
340 expld.result.valid_p = FALSE;
341 break;
342
343 case ABSOLUTE:
344 make_abs ();
345 break;
346
347 case LOG2CEIL:
348 make_log2ceil ();
349 break;
350
351 case '~':
352 expld.result.value = ~expld.result.value;
353 break;
354
355 case '!':
356 expld.result.value = !expld.result.value;
357 break;
358
359 case '-':
360 expld.result.value = -expld.result.value;
361 break;
362
363 case NEXT:
364 /* Return next place aligned to value. */
365 if (expld.phase != lang_first_phase_enum)
366 {
367 make_abs ();
368 expld.result.value = align_n (expld.dot, expld.result.value);
369 }
370 else
371 expld.result.valid_p = FALSE;
372 break;
373
374 case DATA_SEGMENT_END:
375 if (expld.phase == lang_first_phase_enum
376 || expld.section != bfd_abs_section_ptr)
377 {
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. */
391 }
392 else
393 expld.result.valid_p = FALSE;
394 break;
395
396 default:
397 FAIL ();
398 break;
399 }
400 }
401 }
402
403 static void
404 fold_binary (etree_type *tree)
405 {
406 etree_value_type lhs;
407 exp_fold_tree_1 (tree->binary.lhs);
408
409 /* The SEGMENT_START operator is special because its first
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. */
413 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
414 {
415 const char *segment_name;
416 segment_type *seg;
417
418 /* Check to see if the user has overridden the default
419 value. */
420 segment_name = tree->binary.rhs->name.name;
421 for (seg = segments; seg; seg = seg->next)
422 if (strcmp (seg->name, segment_name) == 0)
423 {
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);
429 seg->used = TRUE;
430 new_rel_from_abs (seg->value);
431 break;
432 }
433 return;
434 }
435
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 {
442 if (lhs.section != expld.result.section)
443 {
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;
451 lhs.section = bfd_abs_section_ptr;
452 }
453
454 /* If the rhs is just a number, keep the lhs section. */
455 else if (expld.result.section == NULL)
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 }
462 }
463 /* At this point we know that both operands have the same
464 section, or at least one of them is a plain number. */
465
466 switch (tree->type.node_code)
467 {
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. */
483 #define BOP(x, y) \
484 case x: \
485 expld.result.value = lhs.value y expld.result.value; \
486 if (expld.result.section == lhs.section) \
487 expld.result.section = NULL; \
488 break;
489
490 /* Comparison operators, logical AND, and logical OR always
491 return a plain number. */
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;
497
498 BOP ('+', +);
499 BOP ('*', *);
500 BOP ('-', -);
501 BOP (LSHIFT, <<);
502 BOP (RSHIFT, >>);
503 BOP ('&', &);
504 BOP ('^', ^);
505 BOP ('|', |);
506 BOPN (EQ, ==);
507 BOPN (NE, !=);
508 BOPN ('<', <);
509 BOPN ('>', >);
510 BOPN (LE, <=);
511 BOPN (GE, >=);
512 BOPN (ANDAND, &&);
513 BOPN (OROR, ||);
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)
520 einfo (_("%F%S %% by zero\n"), tree->binary.rhs);
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)
530 einfo (_("%F%S / by zero\n"), tree->binary.rhs);
531 if (expld.result.section == lhs.section)
532 expld.result.section = NULL;
533 break;
534
535 case MAX_K:
536 if (lhs.value > expld.result.value)
537 expld.result.value = lhs.value;
538 break;
539
540 case MIN_K:
541 if (lhs.value < expld.result.value)
542 expld.result.value = lhs.value;
543 break;
544
545 case ALIGN_K:
546 expld.result.value = align_n (lhs.value, expld.result.value);
547 break;
548
549 case DATA_SEGMENT_ALIGN:
550 expld.dataseg.relro = exp_dataseg_relro_start;
551 if (expld.phase == lang_first_phase_enum
552 || expld.section != bfd_abs_section_ptr)
553 expld.result.valid_p = FALSE;
554 else
555 {
556 bfd_vma maxpage = lhs.value;
557 bfd_vma commonpage = expld.result.value;
558
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;
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
569 {
570 expld.result.value += expld.dot & (maxpage - 1);
571 if (expld.dataseg.phase == exp_dataseg_done)
572 {
573 /* OK. */
574 }
575 else if (expld.dataseg.phase == exp_dataseg_none)
576 {
577 expld.dataseg.phase = exp_dataseg_align_seen;
578 expld.dataseg.base = expld.result.value;
579 expld.dataseg.pagesize = commonpage;
580 expld.dataseg.maxpagesize = maxpage;
581 expld.dataseg.relro_end = 0;
582 }
583 else
584 expld.result.valid_p = FALSE;
585 }
586 }
587 break;
588
589 case DATA_SEGMENT_RELRO_END:
590 /* Operands swapped! DATA_SEGMENT_RELRO_END(offset,exp)
591 has offset in expld.result and exp in lhs. */
592 expld.dataseg.relro = exp_dataseg_relro_end;
593 expld.dataseg.relro_offset = expld.result.value;
594 if (expld.phase == lang_first_phase_enum
595 || expld.section != bfd_abs_section_ptr)
596 expld.result.valid_p = FALSE;
597 else if (expld.dataseg.phase == exp_dataseg_align_seen
598 || expld.dataseg.phase == exp_dataseg_adjust
599 || expld.dataseg.phase == exp_dataseg_relro_adjust
600 || expld.dataseg.phase == exp_dataseg_done)
601 {
602 if (expld.dataseg.phase == exp_dataseg_align_seen
603 || expld.dataseg.phase == exp_dataseg_relro_adjust)
604 expld.dataseg.relro_end = lhs.value + expld.result.value;
605
606 if (expld.dataseg.phase == exp_dataseg_relro_adjust
607 && (expld.dataseg.relro_end
608 & (expld.dataseg.pagesize - 1)))
609 {
610 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
611 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
612 expld.result.value = (expld.dataseg.relro_end
613 - expld.result.value);
614 }
615 else
616 expld.result.value = lhs.value;
617
618 if (expld.dataseg.phase == exp_dataseg_align_seen)
619 expld.dataseg.phase = exp_dataseg_relro_seen;
620 }
621 else
622 expld.result.valid_p = FALSE;
623 break;
624
625 default:
626 FAIL ();
627 }
628 }
629 }
630
631 static void
632 fold_trinary (etree_type *tree)
633 {
634 exp_fold_tree_1 (tree->trinary.cond);
635 if (expld.result.valid_p)
636 exp_fold_tree_1 (expld.result.value
637 ? tree->trinary.lhs
638 : tree->trinary.rhs);
639 }
640
641 static void
642 fold_name (etree_type *tree)
643 {
644 memset (&expld.result, 0, sizeof (expld.result));
645
646 switch (tree->type.node_code)
647 {
648 case SIZEOF_HEADERS:
649 if (expld.phase != lang_first_phase_enum)
650 {
651 bfd_vma hdr_size = 0;
652 /* Don't find the real header size if only marking sections;
653 The bfd function may cache incorrect data. */
654 if (expld.phase != lang_mark_phase_enum)
655 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
656 new_number (hdr_size);
657 }
658 break;
659
660 case DEFINED:
661 if (expld.phase != lang_first_phase_enum)
662 {
663 struct bfd_link_hash_entry *h;
664 struct definedness_hash_entry *def;
665
666 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
667 &link_info,
668 tree->name.name,
669 FALSE, FALSE, TRUE);
670 new_number (h != NULL
671 && (h->type == bfd_link_hash_defined
672 || h->type == bfd_link_hash_defweak
673 || h->type == bfd_link_hash_common)
674 && ((def = symbol_defined (tree->name.name)) == NULL
675 || def->by_object
676 || def->iteration == (lang_statement_iteration & 1)));
677 }
678 break;
679
680 case NAME:
681 if (expld.assign_name != NULL
682 && strcmp (expld.assign_name, tree->name.name) == 0)
683 {
684 /* Self-assignment is only allowed for absolute symbols
685 defined in a linker script. */
686 struct bfd_link_hash_entry *h;
687 struct definedness_hash_entry *def;
688
689 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
690 &link_info,
691 tree->name.name,
692 FALSE, FALSE, TRUE);
693 if (!(h != NULL
694 && (h->type == bfd_link_hash_defined
695 || h->type == bfd_link_hash_defweak)
696 && h->u.def.section == bfd_abs_section_ptr
697 && (def = symbol_defined (tree->name.name)) != NULL
698 && def->iteration == (lang_statement_iteration & 1)))
699 expld.assign_name = NULL;
700 }
701 if (expld.phase == lang_first_phase_enum)
702 ;
703 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
704 new_rel_from_abs (expld.dot);
705 else
706 {
707 struct bfd_link_hash_entry *h;
708
709 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
710 &link_info,
711 tree->name.name,
712 TRUE, FALSE, TRUE);
713 if (!h)
714 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
715 else if (h->type == bfd_link_hash_defined
716 || h->type == bfd_link_hash_defweak)
717 {
718 asection *output_section;
719
720 output_section = h->u.def.section->output_section;
721 if (output_section == NULL)
722 {
723 if (expld.phase == lang_mark_phase_enum)
724 new_rel (h->u.def.value, h->u.def.section);
725 else
726 einfo (_("%X%S: unresolvable symbol `%s'"
727 " referenced in expression\n"),
728 tree, tree->name.name);
729 }
730 else if (output_section == bfd_abs_section_ptr
731 && (expld.section != bfd_abs_section_ptr
732 || config.sane_expr))
733 new_number (h->u.def.value + h->u.def.section->output_offset);
734 else
735 new_rel (h->u.def.value + h->u.def.section->output_offset,
736 output_section);
737 }
738 else if (expld.phase == lang_final_phase_enum
739 || (expld.phase != lang_mark_phase_enum
740 && expld.assigning_to_dot))
741 einfo (_("%F%S: undefined symbol `%s'"
742 " referenced in expression\n"),
743 tree, tree->name.name);
744 else if (h->type == bfd_link_hash_new)
745 {
746 h->type = bfd_link_hash_undefined;
747 h->u.undef.abfd = NULL;
748 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
749 bfd_link_add_undef (link_info.hash, h);
750 }
751 }
752 break;
753
754 case ADDR:
755 if (expld.phase != lang_first_phase_enum)
756 {
757 lang_output_section_statement_type *os;
758
759 os = lang_output_section_find (tree->name.name);
760 if (os == NULL)
761 {
762 if (expld.phase == lang_final_phase_enum)
763 einfo (_("%F%S: undefined section `%s'"
764 " referenced in expression\n"),
765 tree, tree->name.name);
766 }
767 else if (os->processed_vma)
768 new_rel (0, os->bfd_section);
769 }
770 break;
771
772 case LOADADDR:
773 if (expld.phase != lang_first_phase_enum)
774 {
775 lang_output_section_statement_type *os;
776
777 os = lang_output_section_find (tree->name.name);
778 if (os == NULL)
779 {
780 if (expld.phase == lang_final_phase_enum)
781 einfo (_("%F%S: undefined section `%s'"
782 " referenced in expression\n"),
783 tree, tree->name.name);
784 }
785 else if (os->processed_lma)
786 {
787 if (os->load_base == NULL)
788 new_abs (os->bfd_section->lma);
789 else
790 {
791 exp_fold_tree_1 (os->load_base);
792 if (expld.result.valid_p)
793 make_abs ();
794 }
795 }
796 }
797 break;
798
799 case SIZEOF:
800 case ALIGNOF:
801 if (expld.phase != lang_first_phase_enum)
802 {
803 lang_output_section_statement_type *os;
804
805 os = lang_output_section_find (tree->name.name);
806 if (os == NULL)
807 {
808 if (expld.phase == lang_final_phase_enum)
809 einfo (_("%F%S: undefined section `%s'"
810 " referenced in expression\n"),
811 tree, tree->name.name);
812 new_number (0);
813 }
814 else if (os->bfd_section != NULL)
815 {
816 bfd_vma val;
817
818 if (tree->type.node_code == SIZEOF)
819 val = (os->bfd_section->size
820 / bfd_octets_per_byte (link_info.output_bfd));
821 else
822 val = (bfd_vma)1 << os->bfd_section->alignment_power;
823
824 new_number (val);
825 }
826 else
827 new_number (0);
828 }
829 break;
830
831 case LENGTH:
832 {
833 if (expld.phase != lang_first_phase_enum)
834 {
835 lang_memory_region_type *mem;
836
837 mem = lang_memory_region_lookup (tree->name.name, FALSE);
838 if (mem != NULL)
839 new_number (mem->length);
840 else
841 einfo (_("%F%S: undefined MEMORY region `%s'"
842 " referenced in expression\n"),
843 tree, tree->name.name);
844 }
845 }
846 break;
847
848 case ORIGIN:
849 if (expld.phase != lang_first_phase_enum)
850 {
851 lang_memory_region_type *mem;
852
853 mem = lang_memory_region_lookup (tree->name.name, FALSE);
854 if (mem != NULL)
855 new_rel_from_abs (mem->origin);
856 else
857 einfo (_("%F%S: undefined MEMORY region `%s'"
858 " referenced in expression\n"),
859 tree, tree->name.name);
860 }
861 break;
862
863 case CONSTANT:
864 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
865 new_number (config.maxpagesize);
866 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
867 new_number (config.commonpagesize);
868 else
869 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
870 tree, tree->name.name);
871 break;
872
873 default:
874 FAIL ();
875 break;
876 }
877 }
878
879 /* Return true if TREE is '.'. */
880
881 static bfd_boolean
882 is_dot (const etree_type *tree)
883 {
884 return (tree->type.node_class == etree_name
885 && tree->type.node_code == NAME
886 && tree->name.name[0] == '.'
887 && tree->name.name[1] == 0);
888 }
889
890 /* Return true if TREE is a constant equal to VAL. */
891
892 static bfd_boolean
893 is_value (const etree_type *tree, bfd_vma val)
894 {
895 return (tree->type.node_class == etree_value
896 && tree->value.value == val);
897 }
898
899 /* Return true if TREE is an absolute symbol equal to VAL defined in
900 a linker script. */
901
902 static bfd_boolean
903 is_sym_value (const etree_type *tree, bfd_vma val)
904 {
905 struct bfd_link_hash_entry *h;
906 struct definedness_hash_entry *def;
907
908 return (tree->type.node_class == etree_name
909 && tree->type.node_code == NAME
910 && (def = symbol_defined (tree->name.name)) != NULL
911 && def->by_script
912 && def->iteration == (lang_statement_iteration & 1)
913 && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
914 &link_info,
915 tree->name.name,
916 FALSE, FALSE, TRUE)) != NULL
917 && h->type == bfd_link_hash_defined
918 && h->u.def.section == bfd_abs_section_ptr
919 && h->u.def.value == val);
920 }
921
922 /* Return true if TREE is ". != 0". */
923
924 static bfd_boolean
925 is_dot_ne_0 (const etree_type *tree)
926 {
927 return (tree->type.node_class == etree_binary
928 && tree->type.node_code == NE
929 && is_dot (tree->binary.lhs)
930 && is_value (tree->binary.rhs, 0));
931 }
932
933 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
934 absolute constant with value 0 defined in a linker script. */
935
936 static bfd_boolean
937 is_dot_plus_0 (const etree_type *tree)
938 {
939 return (tree->type.node_class == etree_binary
940 && tree->type.node_code == '+'
941 && is_dot (tree->binary.lhs)
942 && (is_value (tree->binary.rhs, 0)
943 || is_sym_value (tree->binary.rhs, 0)));
944 }
945
946 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
947
948 static bfd_boolean
949 is_align_conditional (const etree_type *tree)
950 {
951 if (tree->type.node_class == etree_unary
952 && tree->type.node_code == ALIGN_K)
953 {
954 tree = tree->unary.child;
955 return (tree->type.node_class == etree_trinary
956 && is_dot_ne_0 (tree->trinary.cond)
957 && is_value (tree->trinary.rhs, 1));
958 }
959 return 0;
960 }
961
962 static void
963 exp_fold_tree_1 (etree_type *tree)
964 {
965 if (tree == NULL)
966 {
967 memset (&expld.result, 0, sizeof (expld.result));
968 return;
969 }
970
971 switch (tree->type.node_class)
972 {
973 case etree_value:
974 if (expld.section == bfd_abs_section_ptr
975 && !config.sane_expr)
976 new_abs (tree->value.value);
977 else
978 new_number (tree->value.value);
979 expld.result.str = tree->value.str;
980 break;
981
982 case etree_rel:
983 if (expld.phase != lang_first_phase_enum)
984 {
985 asection *output_section = tree->rel.section->output_section;
986 new_rel (tree->rel.value + tree->rel.section->output_offset,
987 output_section);
988 }
989 else
990 memset (&expld.result, 0, sizeof (expld.result));
991 break;
992
993 case etree_assert:
994 exp_fold_tree_1 (tree->assert_s.child);
995 if (expld.phase == lang_final_phase_enum && !expld.result.value)
996 einfo ("%X%P: %s\n", tree->assert_s.message);
997 break;
998
999 case etree_unary:
1000 fold_unary (tree);
1001 break;
1002
1003 case etree_binary:
1004 fold_binary (tree);
1005 break;
1006
1007 case etree_trinary:
1008 fold_trinary (tree);
1009 break;
1010
1011 case etree_assign:
1012 case etree_provide:
1013 case etree_provided:
1014 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
1015 {
1016 if (tree->type.node_class != etree_assign)
1017 einfo (_("%F%S can not PROVIDE assignment to"
1018 " location counter\n"), tree);
1019 if (expld.phase != lang_first_phase_enum)
1020 {
1021 /* Notify the folder that this is an assignment to dot. */
1022 expld.assigning_to_dot = TRUE;
1023 exp_fold_tree_1 (tree->assign.src);
1024 expld.assigning_to_dot = FALSE;
1025
1026 /* If we are assigning to dot inside an output section
1027 arrange to keep the section, except for certain
1028 expressions that evaluate to zero. We ignore . = 0,
1029 . = . + 0, and . = ALIGN (. != 0 ? expr : 1).
1030 We can't ignore all expressions that evaluate to zero
1031 because an otherwise empty section might have padding
1032 added by an alignment expression that changes with
1033 relaxation. Such a section might have zero size
1034 before relaxation and so be stripped incorrectly. */
1035 if (expld.phase == lang_mark_phase_enum
1036 && expld.section != bfd_abs_section_ptr
1037 && !(expld.result.valid_p
1038 && expld.result.value == 0
1039 && (is_value (tree->assign.src, 0)
1040 || is_sym_value (tree->assign.src, 0)
1041 || is_dot_plus_0 (tree->assign.src)
1042 || is_align_conditional (tree->assign.src))))
1043 expld.section->flags |= SEC_KEEP;
1044
1045 if (!expld.result.valid_p)
1046 {
1047 if (expld.phase != lang_mark_phase_enum)
1048 einfo (_("%F%S invalid assignment to"
1049 " location counter\n"), tree);
1050 }
1051 else if (expld.dotp == NULL)
1052 einfo (_("%F%S assignment to location counter"
1053 " invalid outside of SECTIONS\n"), tree);
1054
1055 /* After allocation, assignment to dot should not be
1056 done inside an output section since allocation adds a
1057 padding statement that effectively duplicates the
1058 assignment. */
1059 else if (expld.phase <= lang_allocating_phase_enum
1060 || expld.section == bfd_abs_section_ptr)
1061 {
1062 bfd_vma nextdot;
1063
1064 nextdot = expld.result.value;
1065 if (expld.result.section != NULL)
1066 nextdot += expld.result.section->vma;
1067 else
1068 nextdot += expld.section->vma;
1069 if (nextdot < expld.dot
1070 && expld.section != bfd_abs_section_ptr)
1071 einfo (_("%F%S cannot move location counter backwards"
1072 " (from %V to %V)\n"),
1073 tree, expld.dot, nextdot);
1074 else
1075 {
1076 expld.dot = nextdot;
1077 *expld.dotp = nextdot;
1078 }
1079 }
1080 }
1081 else
1082 memset (&expld.result, 0, sizeof (expld.result));
1083 }
1084 else
1085 {
1086 struct bfd_link_hash_entry *h = NULL;
1087
1088 if (tree->type.node_class == etree_provide)
1089 {
1090 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1091 FALSE, FALSE, TRUE);
1092 if (h == NULL
1093 || !(h->type == bfd_link_hash_new
1094 || h->type == bfd_link_hash_undefined
1095 || h->linker_def))
1096 {
1097 /* Do nothing. The symbol was never referenced, or
1098 was defined in some object file. Undefined weak
1099 symbols stay undefined. */
1100 break;
1101 }
1102 }
1103
1104 expld.assign_name = tree->assign.dst;
1105 exp_fold_tree_1 (tree->assign.src);
1106 /* expld.assign_name remaining equal to tree->assign.dst
1107 below indicates the evaluation of tree->assign.src did
1108 not use the value of tree->assign.dst. We don't allow
1109 self assignment until the final phase for two reasons:
1110 1) Expressions are evaluated multiple times. With
1111 relaxation, the number of times may vary.
1112 2) Section relative symbol values cannot be correctly
1113 converted to absolute values, as is required by many
1114 expressions, until final section sizing is complete. */
1115 if ((expld.result.valid_p
1116 && (expld.phase == lang_final_phase_enum
1117 || expld.assign_name != NULL))
1118 || (expld.phase <= lang_mark_phase_enum
1119 && tree->type.node_class == etree_assign
1120 && tree->assign.defsym))
1121 {
1122 if (h == NULL)
1123 {
1124 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1125 TRUE, FALSE, TRUE);
1126 if (h == NULL)
1127 einfo (_("%P%F:%s: hash creation failed\n"),
1128 tree->assign.dst);
1129 }
1130
1131 if (expld.result.section == NULL)
1132 expld.result.section = expld.section;
1133 if (!update_definedness (tree->assign.dst, h) && 0)
1134 {
1135 /* Symbol was already defined. For now this error
1136 is disabled because it causes failures in the ld
1137 testsuite: ld-elf/var1, ld-scripts/defined5, and
1138 ld-scripts/pr14962. Some of these no doubt
1139 reflect scripts used in the wild. */
1140 (*link_info.callbacks->multiple_definition)
1141 (&link_info, h, link_info.output_bfd,
1142 expld.result.section, expld.result.value);
1143 }
1144 h->type = bfd_link_hash_defined;
1145 h->u.def.value = expld.result.value;
1146 h->u.def.section = expld.result.section;
1147 if (tree->type.node_class == etree_provide)
1148 tree->type.node_class = etree_provided;
1149
1150 /* Copy the symbol type if this is a simple assignment of
1151 one symbol to another. This could be more general
1152 (e.g. a ?: operator with NAMEs in each branch). */
1153 if (tree->assign.src->type.node_class == etree_name)
1154 {
1155 struct bfd_link_hash_entry *hsrc;
1156
1157 hsrc = bfd_link_hash_lookup (link_info.hash,
1158 tree->assign.src->name.name,
1159 FALSE, FALSE, TRUE);
1160 if (hsrc)
1161 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
1162 hsrc);
1163 }
1164 }
1165 else if (expld.phase == lang_final_phase_enum)
1166 {
1167 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1168 FALSE, FALSE, TRUE);
1169 if (h != NULL
1170 && h->type == bfd_link_hash_new)
1171 h->type = bfd_link_hash_undefined;
1172 }
1173 expld.assign_name = NULL;
1174 }
1175 break;
1176
1177 case etree_name:
1178 fold_name (tree);
1179 break;
1180
1181 default:
1182 FAIL ();
1183 memset (&expld.result, 0, sizeof (expld.result));
1184 break;
1185 }
1186 }
1187
1188 void
1189 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
1190 {
1191 expld.dot = *dotp;
1192 expld.dotp = dotp;
1193 expld.section = current_section;
1194 exp_fold_tree_1 (tree);
1195 }
1196
1197 void
1198 exp_fold_tree_no_dot (etree_type *tree)
1199 {
1200 expld.dot = 0;
1201 expld.dotp = NULL;
1202 expld.section = bfd_abs_section_ptr;
1203 exp_fold_tree_1 (tree);
1204 }
1205
1206 etree_type *
1207 exp_binop (int code, etree_type *lhs, etree_type *rhs)
1208 {
1209 etree_type value, *new_e;
1210
1211 value.type.node_code = code;
1212 value.type.filename = lhs->type.filename;
1213 value.type.lineno = lhs->type.lineno;
1214 value.binary.lhs = lhs;
1215 value.binary.rhs = rhs;
1216 value.type.node_class = etree_binary;
1217 exp_fold_tree_no_dot (&value);
1218 if (expld.result.valid_p)
1219 return exp_intop (expld.result.value);
1220
1221 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
1222 memcpy (new_e, &value, sizeof (new_e->binary));
1223 return new_e;
1224 }
1225
1226 etree_type *
1227 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
1228 {
1229 etree_type value, *new_e;
1230
1231 value.type.node_code = code;
1232 value.type.filename = cond->type.filename;
1233 value.type.lineno = cond->type.lineno;
1234 value.trinary.lhs = lhs;
1235 value.trinary.cond = cond;
1236 value.trinary.rhs = rhs;
1237 value.type.node_class = etree_trinary;
1238 exp_fold_tree_no_dot (&value);
1239 if (expld.result.valid_p)
1240 return exp_intop (expld.result.value);
1241
1242 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
1243 memcpy (new_e, &value, sizeof (new_e->trinary));
1244 return new_e;
1245 }
1246
1247 etree_type *
1248 exp_unop (int code, etree_type *child)
1249 {
1250 etree_type value, *new_e;
1251
1252 value.unary.type.node_code = code;
1253 value.unary.type.filename = child->type.filename;
1254 value.unary.type.lineno = child->type.lineno;
1255 value.unary.child = child;
1256 value.unary.type.node_class = etree_unary;
1257 exp_fold_tree_no_dot (&value);
1258 if (expld.result.valid_p)
1259 return exp_intop (expld.result.value);
1260
1261 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
1262 memcpy (new_e, &value, sizeof (new_e->unary));
1263 return new_e;
1264 }
1265
1266 etree_type *
1267 exp_nameop (int code, const char *name)
1268 {
1269 etree_type value, *new_e;
1270
1271 value.name.type.node_code = code;
1272 value.name.type.filename = ldlex_filename ();
1273 value.name.type.lineno = lineno;
1274 value.name.name = name;
1275 value.name.type.node_class = etree_name;
1276
1277 exp_fold_tree_no_dot (&value);
1278 if (expld.result.valid_p)
1279 return exp_intop (expld.result.value);
1280
1281 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1282 memcpy (new_e, &value, sizeof (new_e->name));
1283 return new_e;
1284
1285 }
1286
1287 static etree_type *
1288 exp_assop (const char *dst,
1289 etree_type *src,
1290 enum node_tree_enum class,
1291 bfd_boolean defsym,
1292 bfd_boolean hidden)
1293 {
1294 etree_type *n;
1295
1296 n = (etree_type *) stat_alloc (sizeof (n->assign));
1297 n->assign.type.node_code = '=';
1298 n->assign.type.filename = src->type.filename;
1299 n->assign.type.lineno = src->type.lineno;
1300 n->assign.type.node_class = class;
1301 n->assign.src = src;
1302 n->assign.dst = dst;
1303 n->assign.defsym = defsym;
1304 n->assign.hidden = hidden;
1305 return n;
1306 }
1307
1308 /* Handle linker script assignments and HIDDEN. */
1309
1310 etree_type *
1311 exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
1312 {
1313 return exp_assop (dst, src, etree_assign, FALSE, hidden);
1314 }
1315
1316 /* Handle --defsym command-line option. */
1317
1318 etree_type *
1319 exp_defsym (const char *dst, etree_type *src)
1320 {
1321 return exp_assop (dst, src, etree_assign, TRUE, FALSE);
1322 }
1323
1324 /* Handle PROVIDE. */
1325
1326 etree_type *
1327 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1328 {
1329 return exp_assop (dst, src, etree_provide, FALSE, hidden);
1330 }
1331
1332 /* Handle ASSERT. */
1333
1334 etree_type *
1335 exp_assert (etree_type *exp, const char *message)
1336 {
1337 etree_type *n;
1338
1339 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1340 n->assert_s.type.node_code = '!';
1341 n->assert_s.type.filename = exp->type.filename;
1342 n->assert_s.type.lineno = exp->type.lineno;
1343 n->assert_s.type.node_class = etree_assert;
1344 n->assert_s.child = exp;
1345 n->assert_s.message = message;
1346 return n;
1347 }
1348
1349 void
1350 exp_print_tree (etree_type *tree)
1351 {
1352 bfd_boolean function_like;
1353
1354 if (config.map_file == NULL)
1355 config.map_file = stderr;
1356
1357 if (tree == NULL)
1358 {
1359 minfo ("NULL TREE\n");
1360 return;
1361 }
1362
1363 switch (tree->type.node_class)
1364 {
1365 case etree_value:
1366 minfo ("0x%v", tree->value.value);
1367 return;
1368 case etree_rel:
1369 if (tree->rel.section->owner != NULL)
1370 minfo ("%B:", tree->rel.section->owner);
1371 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1372 return;
1373 case etree_assign:
1374 fputs (tree->assign.dst, config.map_file);
1375 exp_print_token (tree->type.node_code, TRUE);
1376 exp_print_tree (tree->assign.src);
1377 break;
1378 case etree_provide:
1379 case etree_provided:
1380 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1381 exp_print_tree (tree->assign.src);
1382 fputc (')', config.map_file);
1383 break;
1384 case etree_binary:
1385 function_like = FALSE;
1386 switch (tree->type.node_code)
1387 {
1388 case MAX_K:
1389 case MIN_K:
1390 case ALIGN_K:
1391 case DATA_SEGMENT_ALIGN:
1392 case DATA_SEGMENT_RELRO_END:
1393 function_like = TRUE;
1394 break;
1395 case SEGMENT_START:
1396 /* Special handling because arguments are in reverse order and
1397 the segment name is quoted. */
1398 exp_print_token (tree->type.node_code, FALSE);
1399 fputs (" (\"", config.map_file);
1400 exp_print_tree (tree->binary.rhs);
1401 fputs ("\", ", config.map_file);
1402 exp_print_tree (tree->binary.lhs);
1403 fputc (')', config.map_file);
1404 return;
1405 }
1406 if (function_like)
1407 {
1408 exp_print_token (tree->type.node_code, FALSE);
1409 fputc (' ', config.map_file);
1410 }
1411 fputc ('(', config.map_file);
1412 exp_print_tree (tree->binary.lhs);
1413 if (function_like)
1414 fprintf (config.map_file, ", ");
1415 else
1416 exp_print_token (tree->type.node_code, TRUE);
1417 exp_print_tree (tree->binary.rhs);
1418 fputc (')', config.map_file);
1419 break;
1420 case etree_trinary:
1421 exp_print_tree (tree->trinary.cond);
1422 fputc ('?', config.map_file);
1423 exp_print_tree (tree->trinary.lhs);
1424 fputc (':', config.map_file);
1425 exp_print_tree (tree->trinary.rhs);
1426 break;
1427 case etree_unary:
1428 exp_print_token (tree->unary.type.node_code, FALSE);
1429 if (tree->unary.child)
1430 {
1431 fprintf (config.map_file, " (");
1432 exp_print_tree (tree->unary.child);
1433 fputc (')', config.map_file);
1434 }
1435 break;
1436
1437 case etree_assert:
1438 fprintf (config.map_file, "ASSERT (");
1439 exp_print_tree (tree->assert_s.child);
1440 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1441 break;
1442
1443 case etree_name:
1444 if (tree->type.node_code == NAME)
1445 fputs (tree->name.name, config.map_file);
1446 else
1447 {
1448 exp_print_token (tree->type.node_code, FALSE);
1449 if (tree->name.name)
1450 fprintf (config.map_file, " (%s)", tree->name.name);
1451 }
1452 break;
1453 default:
1454 FAIL ();
1455 break;
1456 }
1457 }
1458
1459 bfd_vma
1460 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1461 {
1462 if (tree != NULL)
1463 {
1464 exp_fold_tree_no_dot (tree);
1465 if (expld.result.valid_p)
1466 return expld.result.value;
1467 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1468 einfo (_("%F%S: nonconstant expression for %s\n"),
1469 tree, name);
1470 }
1471 return def;
1472 }
1473
1474 int
1475 exp_get_value_int (etree_type *tree, int def, char *name)
1476 {
1477 return exp_get_vma (tree, def, name);
1478 }
1479
1480 fill_type *
1481 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1482 {
1483 fill_type *fill;
1484 size_t len;
1485 unsigned int val;
1486
1487 if (tree == NULL)
1488 return def;
1489
1490 exp_fold_tree_no_dot (tree);
1491 if (!expld.result.valid_p)
1492 {
1493 if (name != NULL && expld.phase != lang_mark_phase_enum)
1494 einfo (_("%F%S: nonconstant expression for %s\n"),
1495 tree, name);
1496 return def;
1497 }
1498
1499 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1500 {
1501 unsigned char *dst;
1502 unsigned char *s;
1503 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1504 fill->size = (len + 1) / 2;
1505 dst = fill->data;
1506 s = (unsigned char *) expld.result.str;
1507 val = 0;
1508 do
1509 {
1510 unsigned int digit;
1511
1512 digit = *s++ - '0';
1513 if (digit > 9)
1514 digit = (digit - 'A' + '0' + 10) & 0xf;
1515 val <<= 4;
1516 val += digit;
1517 --len;
1518 if ((len & 1) == 0)
1519 {
1520 *dst++ = val;
1521 val = 0;
1522 }
1523 }
1524 while (len != 0);
1525 }
1526 else
1527 {
1528 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1529 val = expld.result.value;
1530 fill->data[0] = (val >> 24) & 0xff;
1531 fill->data[1] = (val >> 16) & 0xff;
1532 fill->data[2] = (val >> 8) & 0xff;
1533 fill->data[3] = (val >> 0) & 0xff;
1534 fill->size = 4;
1535 }
1536 return fill;
1537 }
1538
1539 bfd_vma
1540 exp_get_abs_int (etree_type *tree, int def, char *name)
1541 {
1542 if (tree != NULL)
1543 {
1544 exp_fold_tree_no_dot (tree);
1545
1546 if (expld.result.valid_p)
1547 {
1548 if (expld.result.section != NULL)
1549 expld.result.value += expld.result.section->vma;
1550 return expld.result.value;
1551 }
1552 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1553 {
1554 einfo (_("%F%S: nonconstant expression for %s\n"),
1555 tree, name);
1556 }
1557 }
1558 return def;
1559 }
1560
1561 static bfd_vma
1562 align_n (bfd_vma value, bfd_vma align)
1563 {
1564 if (align <= 1)
1565 return value;
1566
1567 value = (value + align - 1) / align;
1568 return value * align;
1569 }
1570
1571 void
1572 ldexp_init (void)
1573 {
1574 /* The value "13" is ad-hoc, somewhat related to the expected number of
1575 assignments in a linker script. */
1576 if (!bfd_hash_table_init_n (&definedness_table,
1577 definedness_newfunc,
1578 sizeof (struct definedness_hash_entry),
1579 13))
1580 einfo (_("%P%F: can not create hash table: %E\n"));
1581 }
1582
1583 void
1584 ldexp_finish (void)
1585 {
1586 bfd_hash_table_free (&definedness_table);
1587 }