PR ld/13258
[binutils.git] / ld / ldexp.c
blob326188424efeb60ebbb5d535c409325cef19453f
1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
7 This file is part of the GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
25 /* This module is in charge of working out the contents of expressions.
27 It has to keep track of the relative/absness of a symbol etc. This
28 is done by keeping all values in a struct (an etree_value_type)
29 which contains a value, a section to which it is relative and a
30 valid bit. */
32 #include "sysdep.h"
33 #include "bfd.h"
34 #include "bfdlink.h"
36 #include "ld.h"
37 #include "ldmain.h"
38 #include "ldmisc.h"
39 #include "ldexp.h"
40 #include "ldlex.h"
41 #include <ldgram.h>
42 #include "ldlang.h"
43 #include "libiberty.h"
44 #include "safe-ctype.h"
46 static void exp_fold_tree_1 (etree_type *);
47 static bfd_vma align_n (bfd_vma, bfd_vma);
49 segment_type *segments;
51 struct ldexp_control expld;
53 /* Print the string representation of the given token. Surround it
54 with spaces if INFIX_P is TRUE. */
56 static void
57 exp_print_token (token_code_type code, int infix_p)
59 static const struct
61 token_code_type code;
62 char * name;
64 table[] =
66 { INT, "int" },
67 { NAME, "NAME" },
68 { PLUSEQ, "+=" },
69 { MINUSEQ, "-=" },
70 { MULTEQ, "*=" },
71 { DIVEQ, "/=" },
72 { LSHIFTEQ, "<<=" },
73 { RSHIFTEQ, ">>=" },
74 { ANDEQ, "&=" },
75 { OREQ, "|=" },
76 { OROR, "||" },
77 { ANDAND, "&&" },
78 { EQ, "==" },
79 { NE, "!=" },
80 { LE, "<=" },
81 { GE, ">=" },
82 { LSHIFT, "<<" },
83 { RSHIFT, ">>" },
84 { ALIGN_K, "ALIGN" },
85 { BLOCK, "BLOCK" },
86 { QUAD, "QUAD" },
87 { SQUAD, "SQUAD" },
88 { LONG, "LONG" },
89 { SHORT, "SHORT" },
90 { BYTE, "BYTE" },
91 { SECTIONS, "SECTIONS" },
92 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
93 { MEMORY, "MEMORY" },
94 { DEFINED, "DEFINED" },
95 { TARGET_K, "TARGET" },
96 { SEARCH_DIR, "SEARCH_DIR" },
97 { MAP, "MAP" },
98 { ENTRY, "ENTRY" },
99 { NEXT, "NEXT" },
100 { ALIGNOF, "ALIGNOF" },
101 { SIZEOF, "SIZEOF" },
102 { ADDR, "ADDR" },
103 { LOADADDR, "LOADADDR" },
104 { CONSTANT, "CONSTANT" },
105 { ABSOLUTE, "ABSOLUTE" },
106 { MAX_K, "MAX" },
107 { MIN_K, "MIN" },
108 { ASSERT_K, "ASSERT" },
109 { REL, "relocatable" },
110 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
111 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
112 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
113 { ORIGIN, "ORIGIN" },
114 { LENGTH, "LENGTH" },
115 { SEGMENT_START, "SEGMENT_START" }
117 unsigned int idx;
119 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
120 if (table[idx].code == code)
121 break;
123 if (infix_p)
124 fputc (' ', config.map_file);
126 if (idx < ARRAY_SIZE (table))
127 fputs (table[idx].name, config.map_file);
128 else if (code < 127)
129 fputc (code, config.map_file);
130 else
131 fprintf (config.map_file, "<code %d>", code);
133 if (infix_p)
134 fputc (' ', config.map_file);
137 static void
138 make_abs (void)
140 if (expld.result.section != NULL)
141 expld.result.value += expld.result.section->vma;
142 expld.result.section = bfd_abs_section_ptr;
145 static void
146 new_abs (bfd_vma value)
148 expld.result.valid_p = TRUE;
149 expld.result.section = bfd_abs_section_ptr;
150 expld.result.value = value;
151 expld.result.str = NULL;
154 etree_type *
155 exp_intop (bfd_vma value)
157 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
158 new_e->type.node_code = INT;
159 new_e->type.lineno = lineno;
160 new_e->value.value = value;
161 new_e->value.str = NULL;
162 new_e->type.node_class = etree_value;
163 return new_e;
166 etree_type *
167 exp_bigintop (bfd_vma value, char *str)
169 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
170 new_e->type.node_code = INT;
171 new_e->type.lineno = lineno;
172 new_e->value.value = value;
173 new_e->value.str = str;
174 new_e->type.node_class = etree_value;
175 return new_e;
178 /* Build an expression representing an unnamed relocatable value. */
180 etree_type *
181 exp_relop (asection *section, bfd_vma value)
183 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
184 new_e->type.node_code = REL;
185 new_e->type.lineno = lineno;
186 new_e->type.node_class = etree_rel;
187 new_e->rel.section = section;
188 new_e->rel.value = value;
189 return new_e;
192 static void
193 new_number (bfd_vma value)
195 expld.result.valid_p = TRUE;
196 expld.result.value = value;
197 expld.result.str = NULL;
198 expld.result.section = NULL;
201 static void
202 new_rel (bfd_vma value, asection *section)
204 expld.result.valid_p = TRUE;
205 expld.result.value = value;
206 expld.result.str = NULL;
207 expld.result.section = section;
210 static void
211 new_rel_from_abs (bfd_vma value)
213 expld.result.valid_p = TRUE;
214 expld.result.value = value - expld.section->vma;
215 expld.result.str = NULL;
216 expld.result.section = expld.section;
219 static void
220 fold_unary (etree_type *tree)
222 exp_fold_tree_1 (tree->unary.child);
223 if (expld.result.valid_p)
225 switch (tree->type.node_code)
227 case ALIGN_K:
228 if (expld.phase != lang_first_phase_enum)
229 new_rel_from_abs (align_n (expld.dot, expld.result.value));
230 else
231 expld.result.valid_p = FALSE;
232 break;
234 case ABSOLUTE:
235 make_abs ();
236 break;
238 case '~':
239 expld.result.value = ~expld.result.value;
240 break;
242 case '!':
243 expld.result.value = !expld.result.value;
244 break;
246 case '-':
247 expld.result.value = -expld.result.value;
248 break;
250 case NEXT:
251 /* Return next place aligned to value. */
252 if (expld.phase != lang_first_phase_enum)
254 make_abs ();
255 expld.result.value = align_n (expld.dot, expld.result.value);
257 else
258 expld.result.valid_p = FALSE;
259 break;
261 case DATA_SEGMENT_END:
262 if (expld.phase != lang_first_phase_enum
263 && expld.section == bfd_abs_section_ptr
264 && (expld.dataseg.phase == exp_dataseg_align_seen
265 || expld.dataseg.phase == exp_dataseg_relro_seen
266 || expld.dataseg.phase == exp_dataseg_adjust
267 || expld.dataseg.phase == exp_dataseg_relro_adjust
268 || expld.phase == lang_final_phase_enum))
270 if (expld.dataseg.phase == exp_dataseg_align_seen
271 || expld.dataseg.phase == exp_dataseg_relro_seen)
273 expld.dataseg.phase = exp_dataseg_end_seen;
274 expld.dataseg.end = expld.result.value;
277 else
278 expld.result.valid_p = FALSE;
279 break;
281 default:
282 FAIL ();
283 break;
288 static void
289 fold_binary (etree_type *tree)
291 etree_value_type lhs;
292 exp_fold_tree_1 (tree->binary.lhs);
294 /* The SEGMENT_START operator is special because its first
295 operand is a string, not the name of a symbol. Note that the
296 operands have been swapped, so binary.lhs is second (default)
297 operand, binary.rhs is first operand. */
298 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
300 const char *segment_name;
301 segment_type *seg;
303 /* Check to see if the user has overridden the default
304 value. */
305 segment_name = tree->binary.rhs->name.name;
306 for (seg = segments; seg; seg = seg->next)
307 if (strcmp (seg->name, segment_name) == 0)
309 if (!seg->used
310 && config.magic_demand_paged
311 && (seg->value % config.maxpagesize) != 0)
312 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
313 segment_name);
314 seg->used = TRUE;
315 new_rel_from_abs (seg->value);
316 break;
318 return;
321 lhs = expld.result;
322 exp_fold_tree_1 (tree->binary.rhs);
323 expld.result.valid_p &= lhs.valid_p;
325 if (expld.result.valid_p)
327 if (lhs.section != expld.result.section)
329 /* If the values are from different sections, and neither is
330 just a number, make both the source arguments absolute. */
331 if (expld.result.section != NULL
332 && lhs.section != NULL)
334 make_abs ();
335 lhs.value += lhs.section->vma;
338 /* If the rhs is just a number, keep the lhs section. */
339 else if (expld.result.section == NULL)
340 expld.result.section = lhs.section;
343 switch (tree->type.node_code)
345 case '%':
346 if (expld.result.value != 0)
347 expld.result.value = ((bfd_signed_vma) lhs.value
348 % (bfd_signed_vma) expld.result.value);
349 else if (expld.phase != lang_mark_phase_enum)
350 einfo (_("%F%S %% by zero\n"));
351 break;
353 case '/':
354 if (expld.result.value != 0)
355 expld.result.value = ((bfd_signed_vma) lhs.value
356 / (bfd_signed_vma) expld.result.value);
357 else if (expld.phase != lang_mark_phase_enum)
358 einfo (_("%F%S / by zero\n"));
359 break;
361 #define BOP(x, y) \
362 case x: \
363 expld.result.value = lhs.value y expld.result.value; \
364 break;
366 #define BOPN(x, y) \
367 case x: \
368 expld.result.value = lhs.value y expld.result.value; \
369 expld.result.section = NULL; \
370 break;
372 BOP ('+', +);
373 BOP ('*', *);
374 BOP ('-', -);
375 BOP (LSHIFT, <<);
376 BOP (RSHIFT, >>);
377 BOP ('&', &);
378 BOP ('^', ^);
379 BOP ('|', |);
380 BOPN (EQ, ==);
381 BOPN (NE, !=);
382 BOPN ('<', <);
383 BOPN ('>', >);
384 BOPN (LE, <=);
385 BOPN (GE, >=);
386 BOPN (ANDAND, &&);
387 BOPN (OROR, ||);
389 case MAX_K:
390 if (lhs.value > expld.result.value)
391 expld.result.value = lhs.value;
392 break;
394 case MIN_K:
395 if (lhs.value < expld.result.value)
396 expld.result.value = lhs.value;
397 break;
399 case ALIGN_K:
400 expld.result.value = align_n (lhs.value, expld.result.value);
401 break;
403 case DATA_SEGMENT_ALIGN:
404 expld.dataseg.relro = exp_dataseg_relro_start;
405 if (expld.phase != lang_first_phase_enum
406 && expld.section == bfd_abs_section_ptr
407 && (expld.dataseg.phase == exp_dataseg_none
408 || expld.dataseg.phase == exp_dataseg_adjust
409 || expld.dataseg.phase == exp_dataseg_relro_adjust
410 || expld.phase == lang_final_phase_enum))
412 bfd_vma maxpage = lhs.value;
413 bfd_vma commonpage = expld.result.value;
415 expld.result.value = align_n (expld.dot, maxpage);
416 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
417 expld.result.value = expld.dataseg.base;
418 else if (expld.dataseg.phase != exp_dataseg_adjust)
420 expld.result.value += expld.dot & (maxpage - 1);
421 if (expld.phase == lang_allocating_phase_enum)
423 expld.dataseg.phase = exp_dataseg_align_seen;
424 expld.dataseg.min_base = expld.dot;
425 expld.dataseg.base = expld.result.value;
426 expld.dataseg.pagesize = commonpage;
427 expld.dataseg.maxpagesize = maxpage;
428 expld.dataseg.relro_end = 0;
431 else if (commonpage < maxpage)
432 expld.result.value += ((expld.dot + commonpage - 1)
433 & (maxpage - commonpage));
435 else
436 expld.result.valid_p = FALSE;
437 break;
439 case DATA_SEGMENT_RELRO_END:
440 expld.dataseg.relro = exp_dataseg_relro_end;
441 if (expld.phase != lang_first_phase_enum
442 && (expld.dataseg.phase == exp_dataseg_align_seen
443 || expld.dataseg.phase == exp_dataseg_adjust
444 || expld.dataseg.phase == exp_dataseg_relro_adjust
445 || expld.phase == lang_final_phase_enum))
447 if (expld.dataseg.phase == exp_dataseg_align_seen
448 || expld.dataseg.phase == exp_dataseg_relro_adjust)
449 expld.dataseg.relro_end = lhs.value + expld.result.value;
451 if (expld.dataseg.phase == exp_dataseg_relro_adjust
452 && (expld.dataseg.relro_end
453 & (expld.dataseg.pagesize - 1)))
455 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
456 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
457 expld.result.value = (expld.dataseg.relro_end
458 - expld.result.value);
460 else
461 expld.result.value = lhs.value;
463 if (expld.dataseg.phase == exp_dataseg_align_seen)
464 expld.dataseg.phase = exp_dataseg_relro_seen;
466 else
467 expld.result.valid_p = FALSE;
468 break;
470 default:
471 FAIL ();
476 static void
477 fold_trinary (etree_type *tree)
479 exp_fold_tree_1 (tree->trinary.cond);
480 if (expld.result.valid_p)
481 exp_fold_tree_1 (expld.result.value
482 ? tree->trinary.lhs
483 : tree->trinary.rhs);
486 static void
487 fold_name (etree_type *tree)
489 memset (&expld.result, 0, sizeof (expld.result));
491 switch (tree->type.node_code)
493 case SIZEOF_HEADERS:
494 if (expld.phase != lang_first_phase_enum)
496 bfd_vma hdr_size = 0;
497 /* Don't find the real header size if only marking sections;
498 The bfd function may cache incorrect data. */
499 if (expld.phase != lang_mark_phase_enum)
500 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
501 new_number (hdr_size);
503 break;
505 case DEFINED:
506 expld.uses_defined = TRUE;
507 if (expld.phase == lang_first_phase_enum)
508 lang_track_definedness (tree->name.name);
509 else
511 struct bfd_link_hash_entry *h;
512 int def_iteration
513 = lang_symbol_definition_iteration (tree->name.name);
515 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
516 &link_info,
517 tree->name.name,
518 FALSE, FALSE, TRUE);
519 new_number (h != NULL
520 && (h->type == bfd_link_hash_defined
521 || h->type == bfd_link_hash_defweak
522 || h->type == bfd_link_hash_common)
523 && (def_iteration == lang_statement_iteration
524 || def_iteration == -1));
526 break;
528 case NAME:
529 if (expld.phase == lang_first_phase_enum)
531 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
532 new_rel_from_abs (expld.dot);
533 else
535 struct bfd_link_hash_entry *h;
537 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
538 &link_info,
539 tree->name.name,
540 TRUE, FALSE, TRUE);
541 if (!h)
542 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
543 else if (h->type == bfd_link_hash_defined
544 || h->type == bfd_link_hash_defweak)
546 asection *output_section;
548 output_section = h->u.def.section->output_section;
549 if (output_section == NULL)
551 if (expld.phase != lang_mark_phase_enum)
552 einfo (_("%X%S: unresolvable symbol `%s'"
553 " referenced in expression\n"),
554 tree->name.name);
556 else if (output_section == bfd_abs_section_ptr
557 && (expld.section != bfd_abs_section_ptr
558 || ld_compatibility >= 221))
559 new_number (h->u.def.value + h->u.def.section->output_offset);
560 else
561 new_rel (h->u.def.value + h->u.def.section->output_offset,
562 output_section);
564 else if (expld.phase == lang_final_phase_enum
565 || expld.assigning_to_dot)
566 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
567 tree->name.name);
568 else if (h->type == bfd_link_hash_new)
570 h->type = bfd_link_hash_undefined;
571 h->u.undef.abfd = NULL;
572 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
573 bfd_link_add_undef (link_info.hash, h);
576 break;
578 case ADDR:
579 if (expld.phase != lang_first_phase_enum)
581 lang_output_section_statement_type *os;
583 os = lang_output_section_find (tree->name.name);
584 if (os == NULL)
586 if (expld.phase == lang_final_phase_enum)
587 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
588 tree->name.name);
590 else if (os->processed_vma)
591 new_rel (0, os->bfd_section);
593 break;
595 case LOADADDR:
596 if (expld.phase != lang_first_phase_enum)
598 lang_output_section_statement_type *os;
600 os = lang_output_section_find (tree->name.name);
601 if (os == NULL)
603 if (expld.phase == lang_final_phase_enum)
604 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
605 tree->name.name);
607 else if (os->processed_lma)
609 if (os->load_base == NULL)
610 new_abs (os->bfd_section->lma);
611 else
613 exp_fold_tree_1 (os->load_base);
614 if (expld.result.valid_p)
615 make_abs ();
619 break;
621 case SIZEOF:
622 case ALIGNOF:
623 if (expld.phase != lang_first_phase_enum)
625 lang_output_section_statement_type *os;
627 os = lang_output_section_find (tree->name.name);
628 if (os == NULL)
630 if (expld.phase == lang_final_phase_enum)
631 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
632 tree->name.name);
633 new_number (0);
635 else if (os->processed_vma)
637 bfd_vma val;
639 if (tree->type.node_code == SIZEOF)
640 val = (os->bfd_section->size
641 / bfd_octets_per_byte (link_info.output_bfd));
642 else
643 val = (bfd_vma)1 << os->bfd_section->alignment_power;
645 new_number (val);
648 break;
650 case LENGTH:
652 lang_memory_region_type *mem;
654 mem = lang_memory_region_lookup (tree->name.name, FALSE);
655 if (mem != NULL)
656 new_number (mem->length);
657 else
658 einfo (_("%F%S: undefined MEMORY region `%s'"
659 " referenced in expression\n"), tree->name.name);
661 break;
663 case ORIGIN:
664 if (expld.phase != lang_first_phase_enum)
666 lang_memory_region_type *mem;
668 mem = lang_memory_region_lookup (tree->name.name, FALSE);
669 if (mem != NULL)
670 new_rel_from_abs (mem->origin);
671 else
672 einfo (_("%F%S: undefined MEMORY region `%s'"
673 " referenced in expression\n"), tree->name.name);
675 break;
677 case CONSTANT:
678 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
679 new_number (config.maxpagesize);
680 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
681 new_number (config.commonpagesize);
682 else
683 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
684 tree->name.name);
685 break;
687 default:
688 FAIL ();
689 break;
693 static void
694 exp_fold_tree_1 (etree_type *tree)
696 if (tree == NULL)
698 memset (&expld.result, 0, sizeof (expld.result));
699 return;
702 switch (tree->type.node_class)
704 case etree_value:
705 if (expld.section == bfd_abs_section_ptr
706 && ld_compatibility < 221)
707 new_abs (tree->value.value);
708 else
709 new_number (tree->value.value);
710 expld.result.str = tree->value.str;
711 break;
713 case etree_rel:
714 if (expld.phase != lang_first_phase_enum)
716 asection *output_section = tree->rel.section->output_section;
717 new_rel (tree->rel.value + tree->rel.section->output_offset,
718 output_section);
720 else
721 memset (&expld.result, 0, sizeof (expld.result));
722 break;
724 case etree_assert:
725 exp_fold_tree_1 (tree->assert_s.child);
726 if (expld.phase == lang_final_phase_enum && !expld.result.value)
727 einfo ("%X%P: %s\n", tree->assert_s.message);
728 break;
730 case etree_unary:
731 fold_unary (tree);
732 break;
734 case etree_binary:
735 fold_binary (tree);
736 break;
738 case etree_trinary:
739 fold_trinary (tree);
740 break;
742 case etree_assign:
743 case etree_provide:
744 case etree_provided:
745 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
747 /* Assignment to dot can only be done during allocation. */
748 if (tree->type.node_class != etree_assign)
749 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
750 if (expld.phase == lang_mark_phase_enum
751 || expld.phase == lang_allocating_phase_enum
752 || (expld.phase == lang_final_phase_enum
753 && expld.section == bfd_abs_section_ptr))
755 /* Notify the folder that this is an assignment to dot. */
756 expld.assigning_to_dot = TRUE;
757 exp_fold_tree_1 (tree->assign.src);
758 expld.assigning_to_dot = FALSE;
760 if (!expld.result.valid_p)
762 if (expld.phase != lang_mark_phase_enum)
763 einfo (_("%F%S invalid assignment to location counter\n"));
765 else if (expld.dotp == NULL)
766 einfo (_("%F%S assignment to location counter"
767 " invalid outside of SECTION\n"));
768 else
770 bfd_vma nextdot;
772 nextdot = expld.result.value;
773 if (expld.result.section != NULL)
774 nextdot += expld.result.section->vma;
775 else
776 nextdot += expld.section->vma;
777 if (nextdot < expld.dot
778 && expld.section != bfd_abs_section_ptr)
779 einfo (_("%F%S cannot move location counter backwards"
780 " (from %V to %V)\n"), expld.dot, nextdot);
781 else
783 expld.dot = nextdot;
784 *expld.dotp = nextdot;
788 else
789 memset (&expld.result, 0, sizeof (expld.result));
791 else
793 struct bfd_link_hash_entry *h = NULL;
795 if (tree->type.node_class == etree_provide)
797 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
798 FALSE, FALSE, TRUE);
799 if (h == NULL
800 || (h->type != bfd_link_hash_new
801 && h->type != bfd_link_hash_undefined
802 && h->type != bfd_link_hash_common))
804 /* Do nothing. The symbol was never referenced, or was
805 defined by some object. */
806 break;
810 exp_fold_tree_1 (tree->assign.src);
811 if (expld.result.valid_p
812 || (expld.phase == lang_first_phase_enum
813 && !expld.uses_defined))
815 if (h == NULL)
817 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
818 TRUE, FALSE, TRUE);
819 if (h == NULL)
820 einfo (_("%P%F:%s: hash creation failed\n"),
821 tree->assign.dst);
824 /* FIXME: Should we worry if the symbol is already
825 defined? */
826 lang_update_definedness (tree->assign.dst, h);
827 h->type = bfd_link_hash_defined;
828 h->u.def.value = expld.result.value;
829 if (expld.result.section == NULL)
830 expld.result.section = expld.section;
831 h->u.def.section = expld.result.section;
832 if (tree->type.node_class == etree_provide)
833 tree->type.node_class = etree_provided;
835 /* Copy the symbol type if this is a simple assignment of
836 one symbol to annother. */
837 if (tree->assign.src->type.node_class == etree_name)
839 struct bfd_link_hash_entry *hsrc;
841 hsrc = bfd_link_hash_lookup (link_info.hash,
842 tree->assign.src->name.name,
843 FALSE, FALSE, TRUE);
844 if (hsrc)
845 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
846 hsrc);
849 else if (expld.phase == lang_final_phase_enum)
851 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
852 FALSE, FALSE, TRUE);
853 if (h != NULL
854 && h->type == bfd_link_hash_new)
855 h->type = bfd_link_hash_undefined;
858 break;
860 case etree_name:
861 fold_name (tree);
862 break;
864 default:
865 FAIL ();
866 memset (&expld.result, 0, sizeof (expld.result));
867 break;
871 void
872 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
874 expld.dot = *dotp;
875 expld.dotp = dotp;
876 expld.section = current_section;
877 expld.uses_defined = FALSE;
878 exp_fold_tree_1 (tree);
881 void
882 exp_fold_tree_no_dot (etree_type *tree)
884 expld.dot = 0;
885 expld.dotp = NULL;
886 expld.section = bfd_abs_section_ptr;
887 expld.uses_defined = FALSE;
888 exp_fold_tree_1 (tree);
891 etree_type *
892 exp_binop (int code, etree_type *lhs, etree_type *rhs)
894 etree_type value, *new_e;
896 value.type.node_code = code;
897 value.type.lineno = lhs->type.lineno;
898 value.binary.lhs = lhs;
899 value.binary.rhs = rhs;
900 value.type.node_class = etree_binary;
901 exp_fold_tree_no_dot (&value);
902 if (expld.result.valid_p)
903 return exp_intop (expld.result.value);
905 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
906 memcpy (new_e, &value, sizeof (new_e->binary));
907 return new_e;
910 etree_type *
911 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
913 etree_type value, *new_e;
915 value.type.node_code = code;
916 value.type.lineno = lhs->type.lineno;
917 value.trinary.lhs = lhs;
918 value.trinary.cond = cond;
919 value.trinary.rhs = rhs;
920 value.type.node_class = etree_trinary;
921 exp_fold_tree_no_dot (&value);
922 if (expld.result.valid_p)
923 return exp_intop (expld.result.value);
925 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
926 memcpy (new_e, &value, sizeof (new_e->trinary));
927 return new_e;
930 etree_type *
931 exp_unop (int code, etree_type *child)
933 etree_type value, *new_e;
935 value.unary.type.node_code = code;
936 value.unary.type.lineno = child->type.lineno;
937 value.unary.child = child;
938 value.unary.type.node_class = etree_unary;
939 exp_fold_tree_no_dot (&value);
940 if (expld.result.valid_p)
941 return exp_intop (expld.result.value);
943 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
944 memcpy (new_e, &value, sizeof (new_e->unary));
945 return new_e;
948 etree_type *
949 exp_nameop (int code, const char *name)
951 etree_type value, *new_e;
953 value.name.type.node_code = code;
954 value.name.type.lineno = lineno;
955 value.name.name = name;
956 value.name.type.node_class = etree_name;
958 exp_fold_tree_no_dot (&value);
959 if (expld.result.valid_p)
960 return exp_intop (expld.result.value);
962 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
963 memcpy (new_e, &value, sizeof (new_e->name));
964 return new_e;
968 etree_type *
969 exp_assop (int code, const char *dst, etree_type *src)
971 etree_type *new_e;
973 new_e = (etree_type *) stat_alloc (sizeof (new_e->assign));
974 new_e->type.node_code = code;
975 new_e->type.lineno = src->type.lineno;
976 new_e->type.node_class = etree_assign;
977 new_e->assign.src = src;
978 new_e->assign.dst = dst;
979 return new_e;
982 /* Handle PROVIDE. */
984 etree_type *
985 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
987 etree_type *n;
989 n = (etree_type *) stat_alloc (sizeof (n->assign));
990 n->assign.type.node_code = '=';
991 n->assign.type.lineno = src->type.lineno;
992 n->assign.type.node_class = etree_provide;
993 n->assign.src = src;
994 n->assign.dst = dst;
995 n->assign.hidden = hidden;
996 return n;
999 /* Handle ASSERT. */
1001 etree_type *
1002 exp_assert (etree_type *exp, const char *message)
1004 etree_type *n;
1006 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1007 n->assert_s.type.node_code = '!';
1008 n->assert_s.type.lineno = exp->type.lineno;
1009 n->assert_s.type.node_class = etree_assert;
1010 n->assert_s.child = exp;
1011 n->assert_s.message = message;
1012 return n;
1015 void
1016 exp_print_tree (etree_type *tree)
1018 bfd_boolean function_like;
1020 if (config.map_file == NULL)
1021 config.map_file = stderr;
1023 if (tree == NULL)
1025 minfo ("NULL TREE\n");
1026 return;
1029 switch (tree->type.node_class)
1031 case etree_value:
1032 minfo ("0x%v", tree->value.value);
1033 return;
1034 case etree_rel:
1035 if (tree->rel.section->owner != NULL)
1036 minfo ("%B:", tree->rel.section->owner);
1037 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1038 return;
1039 case etree_assign:
1040 fputs (tree->assign.dst, config.map_file);
1041 exp_print_token (tree->type.node_code, TRUE);
1042 exp_print_tree (tree->assign.src);
1043 break;
1044 case etree_provide:
1045 case etree_provided:
1046 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1047 exp_print_tree (tree->assign.src);
1048 fputc (')', config.map_file);
1049 break;
1050 case etree_binary:
1051 function_like = FALSE;
1052 switch (tree->type.node_code)
1054 case MAX_K:
1055 case MIN_K:
1056 case ALIGN_K:
1057 case DATA_SEGMENT_ALIGN:
1058 case DATA_SEGMENT_RELRO_END:
1059 function_like = TRUE;
1061 if (function_like)
1063 exp_print_token (tree->type.node_code, FALSE);
1064 fputc (' ', config.map_file);
1066 fputc ('(', config.map_file);
1067 exp_print_tree (tree->binary.lhs);
1068 if (function_like)
1069 fprintf (config.map_file, ", ");
1070 else
1071 exp_print_token (tree->type.node_code, TRUE);
1072 exp_print_tree (tree->binary.rhs);
1073 fputc (')', config.map_file);
1074 break;
1075 case etree_trinary:
1076 exp_print_tree (tree->trinary.cond);
1077 fputc ('?', config.map_file);
1078 exp_print_tree (tree->trinary.lhs);
1079 fputc (':', config.map_file);
1080 exp_print_tree (tree->trinary.rhs);
1081 break;
1082 case etree_unary:
1083 exp_print_token (tree->unary.type.node_code, FALSE);
1084 if (tree->unary.child)
1086 fprintf (config.map_file, " (");
1087 exp_print_tree (tree->unary.child);
1088 fputc (')', config.map_file);
1090 break;
1092 case etree_assert:
1093 fprintf (config.map_file, "ASSERT (");
1094 exp_print_tree (tree->assert_s.child);
1095 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1096 break;
1098 case etree_name:
1099 if (tree->type.node_code == NAME)
1100 fputs (tree->name.name, config.map_file);
1101 else
1103 exp_print_token (tree->type.node_code, FALSE);
1104 if (tree->name.name)
1105 fprintf (config.map_file, " (%s)", tree->name.name);
1107 break;
1108 default:
1109 FAIL ();
1110 break;
1114 bfd_vma
1115 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1117 if (tree != NULL)
1119 exp_fold_tree_no_dot (tree);
1120 if (expld.result.valid_p)
1121 return expld.result.value;
1122 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1123 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1125 return def;
1129 exp_get_value_int (etree_type *tree, int def, char *name)
1131 return exp_get_vma (tree, def, name);
1134 fill_type *
1135 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1137 fill_type *fill;
1138 size_t len;
1139 unsigned int val;
1141 if (tree == NULL)
1142 return def;
1144 exp_fold_tree_no_dot (tree);
1145 if (!expld.result.valid_p)
1147 if (name != NULL && expld.phase != lang_mark_phase_enum)
1148 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1149 return def;
1152 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1154 unsigned char *dst;
1155 unsigned char *s;
1156 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1157 fill->size = (len + 1) / 2;
1158 dst = fill->data;
1159 s = (unsigned char *) expld.result.str;
1160 val = 0;
1163 unsigned int digit;
1165 digit = *s++ - '0';
1166 if (digit > 9)
1167 digit = (digit - 'A' + '0' + 10) & 0xf;
1168 val <<= 4;
1169 val += digit;
1170 --len;
1171 if ((len & 1) == 0)
1173 *dst++ = val;
1174 val = 0;
1177 while (len != 0);
1179 else
1181 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1182 val = expld.result.value;
1183 fill->data[0] = (val >> 24) & 0xff;
1184 fill->data[1] = (val >> 16) & 0xff;
1185 fill->data[2] = (val >> 8) & 0xff;
1186 fill->data[3] = (val >> 0) & 0xff;
1187 fill->size = 4;
1189 return fill;
1192 bfd_vma
1193 exp_get_abs_int (etree_type *tree, int def, char *name)
1195 if (tree != NULL)
1197 exp_fold_tree_no_dot (tree);
1199 if (expld.result.valid_p)
1201 if (expld.result.section != NULL)
1202 expld.result.value += expld.result.section->vma;
1203 return expld.result.value;
1205 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1207 lineno = tree->type.lineno;
1208 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1211 return def;
1214 static bfd_vma
1215 align_n (bfd_vma value, bfd_vma align)
1217 if (align <= 1)
1218 return value;
1220 value = (value + align - 1) / align;
1221 return value * align;