Retrieve segment registers on Windows amd64
[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 /* 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.min_base = expld.dot;
579 expld.dataseg.base = expld.result.value;
580 expld.dataseg.pagesize = commonpage;
581 expld.dataseg.maxpagesize = maxpage;
582 expld.dataseg.relro_end = 0;
583 }
584 else
585 expld.result.valid_p = FALSE;
586 }
587 }
588 break;
589
590 case DATA_SEGMENT_RELRO_END:
591 expld.dataseg.relro = exp_dataseg_relro_end;
592 if (expld.phase == lang_first_phase_enum
593 || expld.section != bfd_abs_section_ptr)
594 expld.result.valid_p = FALSE;
595 else if (expld.dataseg.phase == exp_dataseg_align_seen
596 || expld.dataseg.phase == exp_dataseg_adjust
597 || expld.dataseg.phase == exp_dataseg_relro_adjust
598 || expld.dataseg.phase == exp_dataseg_done)
599 {
600 if (expld.dataseg.phase == exp_dataseg_align_seen
601 || expld.dataseg.phase == exp_dataseg_relro_adjust)
602 expld.dataseg.relro_end = lhs.value + expld.result.value;
603
604 if (expld.dataseg.phase == exp_dataseg_relro_adjust
605 && (expld.dataseg.relro_end
606 & (expld.dataseg.pagesize - 1)))
607 {
608 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
609 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
610 expld.result.value = (expld.dataseg.relro_end
611 - expld.result.value);
612 }
613 else
614 expld.result.value = lhs.value;
615
616 if (expld.dataseg.phase == exp_dataseg_align_seen)
617 expld.dataseg.phase = exp_dataseg_relro_seen;
618 }
619 else
620 expld.result.valid_p = FALSE;
621 break;
622
623 default:
624 FAIL ();
625 }
626 }
627 }
628
629 static void
630 fold_trinary (etree_type *tree)
631 {
632 exp_fold_tree_1 (tree->trinary.cond);
633 if (expld.result.valid_p)
634 exp_fold_tree_1 (expld.result.value
635 ? tree->trinary.lhs
636 : tree->trinary.rhs);
637 }
638
639 static void
640 fold_name (etree_type *tree)
641 {
642 memset (&expld.result, 0, sizeof (expld.result));
643
644 switch (tree->type.node_code)
645 {
646 case SIZEOF_HEADERS:
647 if (expld.phase != lang_first_phase_enum)
648 {
649 bfd_vma hdr_size = 0;
650 /* Don't find the real header size if only marking sections;
651 The bfd function may cache incorrect data. */
652 if (expld.phase != lang_mark_phase_enum)
653 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
654 new_number (hdr_size);
655 }
656 break;
657
658 case DEFINED:
659 if (expld.phase != lang_first_phase_enum)
660 {
661 struct bfd_link_hash_entry *h;
662 struct definedness_hash_entry *def;
663
664 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
665 &link_info,
666 tree->name.name,
667 FALSE, FALSE, TRUE);
668 new_number (h != NULL
669 && (h->type == bfd_link_hash_defined
670 || h->type == bfd_link_hash_defweak
671 || h->type == bfd_link_hash_common)
672 && ((def = symbol_defined (tree->name.name)) == NULL
673 || def->by_object
674 || def->iteration == (lang_statement_iteration & 1)));
675 }
676 break;
677
678 case NAME:
679 if (expld.assign_name != NULL
680 && strcmp (expld.assign_name, tree->name.name) == 0)
681 {
682 /* Self-assignment is only allowed for absolute symbols
683 defined in a linker script. */
684 struct bfd_link_hash_entry *h;
685 struct definedness_hash_entry *def;
686
687 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
688 &link_info,
689 tree->name.name,
690 FALSE, FALSE, TRUE);
691 if (!(h != NULL
692 && (h->type == bfd_link_hash_defined
693 || h->type == bfd_link_hash_defweak)
694 && h->u.def.section == bfd_abs_section_ptr
695 && (def = symbol_defined (tree->name.name)) != NULL
696 && def->iteration == (lang_statement_iteration & 1)))
697 expld.assign_name = NULL;
698 }
699 if (expld.phase == lang_first_phase_enum)
700 ;
701 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
702 new_rel_from_abs (expld.dot);
703 else
704 {
705 struct bfd_link_hash_entry *h;
706
707 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
708 &link_info,
709 tree->name.name,
710 TRUE, FALSE, TRUE);
711 if (!h)
712 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
713 else if (h->type == bfd_link_hash_defined
714 || h->type == bfd_link_hash_defweak)
715 {
716 asection *output_section;
717
718 output_section = h->u.def.section->output_section;
719 if (output_section == NULL)
720 {
721 if (expld.phase == lang_mark_phase_enum)
722 new_rel (h->u.def.value, h->u.def.section);
723 else
724 einfo (_("%X%S: unresolvable symbol `%s'"
725 " referenced in expression\n"),
726 tree, tree->name.name);
727 }
728 else if (output_section == bfd_abs_section_ptr
729 && (expld.section != bfd_abs_section_ptr
730 || config.sane_expr))
731 new_number (h->u.def.value + h->u.def.section->output_offset);
732 else
733 new_rel (h->u.def.value + h->u.def.section->output_offset,
734 output_section);
735 }
736 else if (expld.phase == lang_final_phase_enum
737 || (expld.phase != lang_mark_phase_enum
738 && expld.assigning_to_dot))
739 einfo (_("%F%S: undefined symbol `%s'"
740 " referenced in expression\n"),
741 tree, tree->name.name);
742 else if (h->type == bfd_link_hash_new)
743 {
744 h->type = bfd_link_hash_undefined;
745 h->u.undef.abfd = NULL;
746 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
747 bfd_link_add_undef (link_info.hash, h);
748 }
749 }
750 break;
751
752 case ADDR:
753 if (expld.phase != lang_first_phase_enum)
754 {
755 lang_output_section_statement_type *os;
756
757 os = lang_output_section_find (tree->name.name);
758 if (os == NULL)
759 {
760 if (expld.phase == lang_final_phase_enum)
761 einfo (_("%F%S: undefined section `%s'"
762 " referenced in expression\n"),
763 tree, tree->name.name);
764 }
765 else if (os->processed_vma)
766 new_rel (0, os->bfd_section);
767 }
768 break;
769
770 case LOADADDR:
771 if (expld.phase != lang_first_phase_enum)
772 {
773 lang_output_section_statement_type *os;
774
775 os = lang_output_section_find (tree->name.name);
776 if (os == NULL)
777 {
778 if (expld.phase == lang_final_phase_enum)
779 einfo (_("%F%S: undefined section `%s'"
780 " referenced in expression\n"),
781 tree, tree->name.name);
782 }
783 else if (os->processed_lma)
784 {
785 if (os->load_base == NULL)
786 new_abs (os->bfd_section->lma);
787 else
788 {
789 exp_fold_tree_1 (os->load_base);
790 if (expld.result.valid_p)
791 make_abs ();
792 }
793 }
794 }
795 break;
796
797 case SIZEOF:
798 case ALIGNOF:
799 if (expld.phase != lang_first_phase_enum)
800 {
801 lang_output_section_statement_type *os;
802
803 os = lang_output_section_find (tree->name.name);
804 if (os == NULL)
805 {
806 if (expld.phase == lang_final_phase_enum)
807 einfo (_("%F%S: undefined section `%s'"
808 " referenced in expression\n"),
809 tree, tree->name.name);
810 new_number (0);
811 }
812 else if (os->bfd_section != NULL)
813 {
814 bfd_vma val;
815
816 if (tree->type.node_code == SIZEOF)
817 val = (os->bfd_section->size
818 / bfd_octets_per_byte (link_info.output_bfd));
819 else
820 val = (bfd_vma)1 << os->bfd_section->alignment_power;
821
822 new_number (val);
823 }
824 else
825 new_number (0);
826 }
827 break;
828
829 case LENGTH:
830 {
831 if (expld.phase != lang_first_phase_enum)
832 {
833 lang_memory_region_type *mem;
834
835 mem = lang_memory_region_lookup (tree->name.name, FALSE);
836 if (mem != NULL)
837 new_number (mem->length);
838 else
839 einfo (_("%F%S: undefined MEMORY region `%s'"
840 " referenced in expression\n"),
841 tree, tree->name.name);
842 }
843 }
844 break;
845
846 case ORIGIN:
847 if (expld.phase != lang_first_phase_enum)
848 {
849 lang_memory_region_type *mem;
850
851 mem = lang_memory_region_lookup (tree->name.name, FALSE);
852 if (mem != NULL)
853 new_rel_from_abs (mem->origin);
854 else
855 einfo (_("%F%S: undefined MEMORY region `%s'"
856 " referenced in expression\n"),
857 tree, tree->name.name);
858 }
859 break;
860
861 case CONSTANT:
862 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
863 new_number (config.maxpagesize);
864 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
865 new_number (config.commonpagesize);
866 else
867 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
868 tree, tree->name.name);
869 break;
870
871 default:
872 FAIL ();
873 break;
874 }
875 }
876
877 /* Return true if TREE is '.'. */
878
879 static bfd_boolean
880 is_dot (const etree_type *tree)
881 {
882 return (tree->type.node_class == etree_name
883 && tree->type.node_code == NAME
884 && tree->name.name[0] == '.'
885 && tree->name.name[1] == 0);
886 }
887
888 /* Return true if TREE is a constant equal to VAL. */
889
890 static bfd_boolean
891 is_value (const etree_type *tree, bfd_vma val)
892 {
893 return (tree->type.node_class == etree_value
894 && tree->value.value == val);
895 }
896
897 /* Return true if TREE is an absolute symbol equal to VAL defined in
898 a linker script. */
899
900 static bfd_boolean
901 is_sym_value (const etree_type *tree, bfd_vma val)
902 {
903 struct bfd_link_hash_entry *h;
904 struct definedness_hash_entry *def;
905
906 return (tree->type.node_class == etree_name
907 && tree->type.node_code == NAME
908 && (def = symbol_defined (tree->name.name)) != NULL
909 && def->by_script
910 && def->iteration == (lang_statement_iteration & 1)
911 && (h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
912 &link_info,
913 tree->name.name,
914 FALSE, FALSE, TRUE)) != NULL
915 && h->type == bfd_link_hash_defined
916 && h->u.def.section == bfd_abs_section_ptr
917 && h->u.def.value == val);
918 }
919
920 /* Return true if TREE is ". != 0". */
921
922 static bfd_boolean
923 is_dot_ne_0 (const etree_type *tree)
924 {
925 return (tree->type.node_class == etree_binary
926 && tree->type.node_code == NE
927 && is_dot (tree->binary.lhs)
928 && is_value (tree->binary.rhs, 0));
929 }
930
931 /* Return true if TREE is ". = . + 0" or ". = . + sym" where sym is an
932 absolute constant with value 0 defined in a linker script. */
933
934 static bfd_boolean
935 is_dot_plus_0 (const etree_type *tree)
936 {
937 return (tree->type.node_class == etree_binary
938 && tree->type.node_code == '+'
939 && is_dot (tree->binary.lhs)
940 && (is_value (tree->binary.rhs, 0)
941 || is_sym_value (tree->binary.rhs, 0)));
942 }
943
944 /* Return true if TREE is "ALIGN (. != 0 ? some_expression : 1)". */
945
946 static bfd_boolean
947 is_align_conditional (const etree_type *tree)
948 {
949 if (tree->type.node_class == etree_unary
950 && tree->type.node_code == ALIGN_K)
951 {
952 tree = tree->unary.child;
953 return (tree->type.node_class == etree_trinary
954 && is_dot_ne_0 (tree->trinary.cond)
955 && is_value (tree->trinary.rhs, 1));
956 }
957 return 0;
958 }
959
960 static void
961 exp_fold_tree_1 (etree_type *tree)
962 {
963 if (tree == NULL)
964 {
965 memset (&expld.result, 0, sizeof (expld.result));
966 return;
967 }
968
969 switch (tree->type.node_class)
970 {
971 case etree_value:
972 if (expld.section == bfd_abs_section_ptr
973 && !config.sane_expr)
974 new_abs (tree->value.value);
975 else
976 new_number (tree->value.value);
977 expld.result.str = tree->value.str;
978 break;
979
980 case etree_rel:
981 if (expld.phase != lang_first_phase_enum)
982 {
983 asection *output_section = tree->rel.section->output_section;
984 new_rel (tree->rel.value + tree->rel.section->output_offset,
985 output_section);
986 }
987 else
988 memset (&expld.result, 0, sizeof (expld.result));
989 break;
990
991 case etree_assert:
992 exp_fold_tree_1 (tree->assert_s.child);
993 if (expld.phase == lang_final_phase_enum && !expld.result.value)
994 einfo ("%X%P: %s\n", tree->assert_s.message);
995 break;
996
997 case etree_unary:
998 fold_unary (tree);
999 break;
1000
1001 case etree_binary:
1002 fold_binary (tree);
1003 break;
1004
1005 case etree_trinary:
1006 fold_trinary (tree);
1007 break;
1008
1009 case etree_assign:
1010 case etree_provide:
1011 case etree_provided:
1012 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
1013 {
1014 if (tree->type.node_class != etree_assign)
1015 einfo (_("%F%S can not PROVIDE assignment to"
1016 " location counter\n"), tree);
1017 if (expld.phase != lang_first_phase_enum)
1018 {
1019 /* Notify the folder that this is an assignment to dot. */
1020 expld.assigning_to_dot = TRUE;
1021 exp_fold_tree_1 (tree->assign.src);
1022 expld.assigning_to_dot = FALSE;
1023
1024 /* If we are assigning to dot inside an output section
1025 arrange to keep the section, except for certain
1026 expressions that evaluate to zero. We ignore . = 0,
1027 . = . + 0, and . = ALIGN (. != 0 ? expr : 1). */
1028 if (expld.phase == lang_mark_phase_enum
1029 && expld.section != bfd_abs_section_ptr
1030 && !(expld.result.valid_p
1031 && expld.result.value == 0
1032 && (is_value (tree->assign.src, 0)
1033 || is_sym_value (tree->assign.src, 0)
1034 || is_dot_plus_0 (tree->assign.src)
1035 || is_align_conditional (tree->assign.src))))
1036 expld.section->flags |= SEC_KEEP;
1037
1038 if (!expld.result.valid_p)
1039 {
1040 if (expld.phase != lang_mark_phase_enum)
1041 einfo (_("%F%S invalid assignment to"
1042 " location counter\n"), tree);
1043 }
1044 else if (expld.dotp == NULL)
1045 einfo (_("%F%S assignment to location counter"
1046 " invalid outside of SECTIONS\n"), tree);
1047
1048 /* After allocation, assignment to dot should not be
1049 done inside an output section since allocation adds a
1050 padding statement that effectively duplicates the
1051 assignment. */
1052 else if (expld.phase <= lang_allocating_phase_enum
1053 || expld.section == bfd_abs_section_ptr)
1054 {
1055 bfd_vma nextdot;
1056
1057 nextdot = expld.result.value;
1058 if (expld.result.section != NULL)
1059 nextdot += expld.result.section->vma;
1060 else
1061 nextdot += expld.section->vma;
1062 if (nextdot < expld.dot
1063 && expld.section != bfd_abs_section_ptr)
1064 einfo (_("%F%S cannot move location counter backwards"
1065 " (from %V to %V)\n"),
1066 tree, expld.dot, nextdot);
1067 else
1068 {
1069 expld.dot = nextdot;
1070 *expld.dotp = nextdot;
1071 }
1072 }
1073 }
1074 else
1075 memset (&expld.result, 0, sizeof (expld.result));
1076 }
1077 else
1078 {
1079 struct bfd_link_hash_entry *h = NULL;
1080
1081 if (tree->type.node_class == etree_provide)
1082 {
1083 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1084 FALSE, FALSE, TRUE);
1085 if (h == NULL
1086 || !(h->type == bfd_link_hash_new
1087 || h->type == bfd_link_hash_undefined
1088 || h->linker_def))
1089 {
1090 /* Do nothing. The symbol was never referenced, or
1091 was defined in some object file. Undefined weak
1092 symbols stay undefined. */
1093 break;
1094 }
1095 }
1096
1097 expld.assign_name = tree->assign.dst;
1098 exp_fold_tree_1 (tree->assign.src);
1099 /* expld.assign_name remaining equal to tree->assign.dst
1100 below indicates the evaluation of tree->assign.src did
1101 not use the value of tree->assign.dst. We don't allow
1102 self assignment until the final phase for two reasons:
1103 1) Expressions are evaluated multiple times. With
1104 relaxation, the number of times may vary.
1105 2) Section relative symbol values cannot be correctly
1106 converted to absolute values, as is required by many
1107 expressions, until final section sizing is complete. */
1108 if ((expld.result.valid_p
1109 && (expld.phase == lang_final_phase_enum
1110 || expld.assign_name != NULL))
1111 || (expld.phase <= lang_mark_phase_enum
1112 && tree->type.node_class == etree_assign
1113 && tree->assign.defsym))
1114 {
1115 if (h == NULL)
1116 {
1117 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1118 TRUE, FALSE, TRUE);
1119 if (h == NULL)
1120 einfo (_("%P%F:%s: hash creation failed\n"),
1121 tree->assign.dst);
1122 }
1123
1124 if (expld.result.section == NULL)
1125 expld.result.section = expld.section;
1126 if (!update_definedness (tree->assign.dst, h) && 0)
1127 {
1128 /* Symbol was already defined. For now this error
1129 is disabled because it causes failures in the ld
1130 testsuite: ld-elf/var1, ld-scripts/defined5, and
1131 ld-scripts/pr14962. Some of these no doubt
1132 reflect scripts used in the wild. */
1133 (*link_info.callbacks->multiple_definition)
1134 (&link_info, h, link_info.output_bfd,
1135 expld.result.section, expld.result.value);
1136 }
1137 h->type = bfd_link_hash_defined;
1138 h->u.def.value = expld.result.value;
1139 h->u.def.section = expld.result.section;
1140 if (tree->type.node_class == etree_provide)
1141 tree->type.node_class = etree_provided;
1142
1143 /* Copy the symbol type if this is a simple assignment of
1144 one symbol to another. This could be more general
1145 (e.g. a ?: operator with NAMEs in each branch). */
1146 if (tree->assign.src->type.node_class == etree_name)
1147 {
1148 struct bfd_link_hash_entry *hsrc;
1149
1150 hsrc = bfd_link_hash_lookup (link_info.hash,
1151 tree->assign.src->name.name,
1152 FALSE, FALSE, TRUE);
1153 if (hsrc)
1154 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
1155 hsrc);
1156 }
1157 }
1158 else if (expld.phase == lang_final_phase_enum)
1159 {
1160 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1161 FALSE, FALSE, TRUE);
1162 if (h != NULL
1163 && h->type == bfd_link_hash_new)
1164 h->type = bfd_link_hash_undefined;
1165 }
1166 expld.assign_name = NULL;
1167 }
1168 break;
1169
1170 case etree_name:
1171 fold_name (tree);
1172 break;
1173
1174 default:
1175 FAIL ();
1176 memset (&expld.result, 0, sizeof (expld.result));
1177 break;
1178 }
1179 }
1180
1181 void
1182 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
1183 {
1184 expld.dot = *dotp;
1185 expld.dotp = dotp;
1186 expld.section = current_section;
1187 exp_fold_tree_1 (tree);
1188 }
1189
1190 void
1191 exp_fold_tree_no_dot (etree_type *tree)
1192 {
1193 expld.dot = 0;
1194 expld.dotp = NULL;
1195 expld.section = bfd_abs_section_ptr;
1196 exp_fold_tree_1 (tree);
1197 }
1198
1199 etree_type *
1200 exp_binop (int code, etree_type *lhs, etree_type *rhs)
1201 {
1202 etree_type value, *new_e;
1203
1204 value.type.node_code = code;
1205 value.type.filename = lhs->type.filename;
1206 value.type.lineno = lhs->type.lineno;
1207 value.binary.lhs = lhs;
1208 value.binary.rhs = rhs;
1209 value.type.node_class = etree_binary;
1210 exp_fold_tree_no_dot (&value);
1211 if (expld.result.valid_p)
1212 return exp_intop (expld.result.value);
1213
1214 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
1215 memcpy (new_e, &value, sizeof (new_e->binary));
1216 return new_e;
1217 }
1218
1219 etree_type *
1220 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
1221 {
1222 etree_type value, *new_e;
1223
1224 value.type.node_code = code;
1225 value.type.filename = cond->type.filename;
1226 value.type.lineno = cond->type.lineno;
1227 value.trinary.lhs = lhs;
1228 value.trinary.cond = cond;
1229 value.trinary.rhs = rhs;
1230 value.type.node_class = etree_trinary;
1231 exp_fold_tree_no_dot (&value);
1232 if (expld.result.valid_p)
1233 return exp_intop (expld.result.value);
1234
1235 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
1236 memcpy (new_e, &value, sizeof (new_e->trinary));
1237 return new_e;
1238 }
1239
1240 etree_type *
1241 exp_unop (int code, etree_type *child)
1242 {
1243 etree_type value, *new_e;
1244
1245 value.unary.type.node_code = code;
1246 value.unary.type.filename = child->type.filename;
1247 value.unary.type.lineno = child->type.lineno;
1248 value.unary.child = child;
1249 value.unary.type.node_class = etree_unary;
1250 exp_fold_tree_no_dot (&value);
1251 if (expld.result.valid_p)
1252 return exp_intop (expld.result.value);
1253
1254 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
1255 memcpy (new_e, &value, sizeof (new_e->unary));
1256 return new_e;
1257 }
1258
1259 etree_type *
1260 exp_nameop (int code, const char *name)
1261 {
1262 etree_type value, *new_e;
1263
1264 value.name.type.node_code = code;
1265 value.name.type.filename = ldlex_filename ();
1266 value.name.type.lineno = lineno;
1267 value.name.name = name;
1268 value.name.type.node_class = etree_name;
1269
1270 exp_fold_tree_no_dot (&value);
1271 if (expld.result.valid_p)
1272 return exp_intop (expld.result.value);
1273
1274 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
1275 memcpy (new_e, &value, sizeof (new_e->name));
1276 return new_e;
1277
1278 }
1279
1280 static etree_type *
1281 exp_assop (const char *dst,
1282 etree_type *src,
1283 enum node_tree_enum class,
1284 bfd_boolean defsym,
1285 bfd_boolean hidden)
1286 {
1287 etree_type *n;
1288
1289 n = (etree_type *) stat_alloc (sizeof (n->assign));
1290 n->assign.type.node_code = '=';
1291 n->assign.type.filename = src->type.filename;
1292 n->assign.type.lineno = src->type.lineno;
1293 n->assign.type.node_class = class;
1294 n->assign.src = src;
1295 n->assign.dst = dst;
1296 n->assign.defsym = defsym;
1297 n->assign.hidden = hidden;
1298 return n;
1299 }
1300
1301 /* Handle linker script assignments and HIDDEN. */
1302
1303 etree_type *
1304 exp_assign (const char *dst, etree_type *src, bfd_boolean hidden)
1305 {
1306 return exp_assop (dst, src, etree_assign, FALSE, hidden);
1307 }
1308
1309 /* Handle --defsym command-line option. */
1310
1311 etree_type *
1312 exp_defsym (const char *dst, etree_type *src)
1313 {
1314 return exp_assop (dst, src, etree_assign, TRUE, FALSE);
1315 }
1316
1317 /* Handle PROVIDE. */
1318
1319 etree_type *
1320 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
1321 {
1322 return exp_assop (dst, src, etree_provide, FALSE, hidden);
1323 }
1324
1325 /* Handle ASSERT. */
1326
1327 etree_type *
1328 exp_assert (etree_type *exp, const char *message)
1329 {
1330 etree_type *n;
1331
1332 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1333 n->assert_s.type.node_code = '!';
1334 n->assert_s.type.filename = exp->type.filename;
1335 n->assert_s.type.lineno = exp->type.lineno;
1336 n->assert_s.type.node_class = etree_assert;
1337 n->assert_s.child = exp;
1338 n->assert_s.message = message;
1339 return n;
1340 }
1341
1342 void
1343 exp_print_tree (etree_type *tree)
1344 {
1345 bfd_boolean function_like;
1346
1347 if (config.map_file == NULL)
1348 config.map_file = stderr;
1349
1350 if (tree == NULL)
1351 {
1352 minfo ("NULL TREE\n");
1353 return;
1354 }
1355
1356 switch (tree->type.node_class)
1357 {
1358 case etree_value:
1359 minfo ("0x%v", tree->value.value);
1360 return;
1361 case etree_rel:
1362 if (tree->rel.section->owner != NULL)
1363 minfo ("%B:", tree->rel.section->owner);
1364 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1365 return;
1366 case etree_assign:
1367 fputs (tree->assign.dst, config.map_file);
1368 exp_print_token (tree->type.node_code, TRUE);
1369 exp_print_tree (tree->assign.src);
1370 break;
1371 case etree_provide:
1372 case etree_provided:
1373 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1374 exp_print_tree (tree->assign.src);
1375 fputc (')', config.map_file);
1376 break;
1377 case etree_binary:
1378 function_like = FALSE;
1379 switch (tree->type.node_code)
1380 {
1381 case MAX_K:
1382 case MIN_K:
1383 case ALIGN_K:
1384 case DATA_SEGMENT_ALIGN:
1385 case DATA_SEGMENT_RELRO_END:
1386 function_like = TRUE;
1387 break;
1388 case SEGMENT_START:
1389 /* Special handling because arguments are in reverse order and
1390 the segment name is quoted. */
1391 exp_print_token (tree->type.node_code, FALSE);
1392 fputs (" (\"", config.map_file);
1393 exp_print_tree (tree->binary.rhs);
1394 fputs ("\", ", config.map_file);
1395 exp_print_tree (tree->binary.lhs);
1396 fputc (')', config.map_file);
1397 return;
1398 }
1399 if (function_like)
1400 {
1401 exp_print_token (tree->type.node_code, FALSE);
1402 fputc (' ', config.map_file);
1403 }
1404 fputc ('(', config.map_file);
1405 exp_print_tree (tree->binary.lhs);
1406 if (function_like)
1407 fprintf (config.map_file, ", ");
1408 else
1409 exp_print_token (tree->type.node_code, TRUE);
1410 exp_print_tree (tree->binary.rhs);
1411 fputc (')', config.map_file);
1412 break;
1413 case etree_trinary:
1414 exp_print_tree (tree->trinary.cond);
1415 fputc ('?', config.map_file);
1416 exp_print_tree (tree->trinary.lhs);
1417 fputc (':', config.map_file);
1418 exp_print_tree (tree->trinary.rhs);
1419 break;
1420 case etree_unary:
1421 exp_print_token (tree->unary.type.node_code, FALSE);
1422 if (tree->unary.child)
1423 {
1424 fprintf (config.map_file, " (");
1425 exp_print_tree (tree->unary.child);
1426 fputc (')', config.map_file);
1427 }
1428 break;
1429
1430 case etree_assert:
1431 fprintf (config.map_file, "ASSERT (");
1432 exp_print_tree (tree->assert_s.child);
1433 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1434 break;
1435
1436 case etree_name:
1437 if (tree->type.node_code == NAME)
1438 fputs (tree->name.name, config.map_file);
1439 else
1440 {
1441 exp_print_token (tree->type.node_code, FALSE);
1442 if (tree->name.name)
1443 fprintf (config.map_file, " (%s)", tree->name.name);
1444 }
1445 break;
1446 default:
1447 FAIL ();
1448 break;
1449 }
1450 }
1451
1452 bfd_vma
1453 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1454 {
1455 if (tree != NULL)
1456 {
1457 exp_fold_tree_no_dot (tree);
1458 if (expld.result.valid_p)
1459 return expld.result.value;
1460 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1461 einfo (_("%F%S: nonconstant expression for %s\n"),
1462 tree, name);
1463 }
1464 return def;
1465 }
1466
1467 int
1468 exp_get_value_int (etree_type *tree, int def, char *name)
1469 {
1470 return exp_get_vma (tree, def, name);
1471 }
1472
1473 fill_type *
1474 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1475 {
1476 fill_type *fill;
1477 size_t len;
1478 unsigned int val;
1479
1480 if (tree == NULL)
1481 return def;
1482
1483 exp_fold_tree_no_dot (tree);
1484 if (!expld.result.valid_p)
1485 {
1486 if (name != NULL && expld.phase != lang_mark_phase_enum)
1487 einfo (_("%F%S: nonconstant expression for %s\n"),
1488 tree, name);
1489 return def;
1490 }
1491
1492 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1493 {
1494 unsigned char *dst;
1495 unsigned char *s;
1496 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1497 fill->size = (len + 1) / 2;
1498 dst = fill->data;
1499 s = (unsigned char *) expld.result.str;
1500 val = 0;
1501 do
1502 {
1503 unsigned int digit;
1504
1505 digit = *s++ - '0';
1506 if (digit > 9)
1507 digit = (digit - 'A' + '0' + 10) & 0xf;
1508 val <<= 4;
1509 val += digit;
1510 --len;
1511 if ((len & 1) == 0)
1512 {
1513 *dst++ = val;
1514 val = 0;
1515 }
1516 }
1517 while (len != 0);
1518 }
1519 else
1520 {
1521 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1522 val = expld.result.value;
1523 fill->data[0] = (val >> 24) & 0xff;
1524 fill->data[1] = (val >> 16) & 0xff;
1525 fill->data[2] = (val >> 8) & 0xff;
1526 fill->data[3] = (val >> 0) & 0xff;
1527 fill->size = 4;
1528 }
1529 return fill;
1530 }
1531
1532 bfd_vma
1533 exp_get_abs_int (etree_type *tree, int def, char *name)
1534 {
1535 if (tree != NULL)
1536 {
1537 exp_fold_tree_no_dot (tree);
1538
1539 if (expld.result.valid_p)
1540 {
1541 if (expld.result.section != NULL)
1542 expld.result.value += expld.result.section->vma;
1543 return expld.result.value;
1544 }
1545 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1546 {
1547 einfo (_("%F%S: nonconstant expression for %s\n"),
1548 tree, name);
1549 }
1550 }
1551 return def;
1552 }
1553
1554 static bfd_vma
1555 align_n (bfd_vma value, bfd_vma align)
1556 {
1557 if (align <= 1)
1558 return value;
1559
1560 value = (value + align - 1) / align;
1561 return value * align;
1562 }
1563
1564 void
1565 ldexp_init (void)
1566 {
1567 /* The value "13" is ad-hoc, somewhat related to the expected number of
1568 assignments in a linker script. */
1569 if (!bfd_hash_table_init_n (&definedness_table,
1570 definedness_newfunc,
1571 sizeof (struct definedness_hash_entry),
1572 13))
1573 einfo (_("%P%F: can not create hash table: %E\n"));
1574 }
1575
1576 void
1577 ldexp_finish (void)
1578 {
1579 bfd_hash_table_free (&definedness_table);
1580 }
This page took 0.068409 seconds and 4 git commands to generate.