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