* ld.h (lang_phase_type): Move to..
[deliverable/binutils-gdb.git] / ld / ldexp.c
1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
6
7 This file is part of GLD, the Gnu Linker.
8
9 GLD 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 2, or (at your option)
12 any later version.
13
14 GLD 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.
18
19 You should have received a copy of the GNU General Public License
20 along with GLD; see the file COPYING. If not, write to the Free
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
23
24 /* This module is in charge of working out the contents of expressions.
25
26 It has to keep track of the relative/absness of a symbol etc. This
27 is done by keeping all values in a struct (an etree_value_type)
28 which contains a value, a section to which it is relative and a
29 valid bit. */
30
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "bfdlink.h"
34
35 #include "ld.h"
36 #include "ldmain.h"
37 #include "ldmisc.h"
38 #include "ldexp.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 void exp_fold_tree_no_dot (etree_type *);
46 static bfd_vma align_n (bfd_vma, bfd_vma);
47
48 segment_type *segments;
49
50 struct ldexp_control expld;
51
52 /* Print the string representation of the given token. Surround it
53 with spaces if INFIX_P is TRUE. */
54
55 static void
56 exp_print_token (token_code_type code, int infix_p)
57 {
58 static const struct
59 {
60 token_code_type code;
61 char * name;
62 }
63 table[] =
64 {
65 { INT, "int" },
66 { NAME, "NAME" },
67 { PLUSEQ, "+=" },
68 { MINUSEQ, "-=" },
69 { MULTEQ, "*=" },
70 { DIVEQ, "/=" },
71 { LSHIFTEQ, "<<=" },
72 { RSHIFTEQ, ">>=" },
73 { ANDEQ, "&=" },
74 { OREQ, "|=" },
75 { OROR, "||" },
76 { ANDAND, "&&" },
77 { EQ, "==" },
78 { NE, "!=" },
79 { LE, "<=" },
80 { GE, ">=" },
81 { LSHIFT, "<<" },
82 { RSHIFT, ">>" },
83 { ALIGN_K, "ALIGN" },
84 { BLOCK, "BLOCK" },
85 { QUAD, "QUAD" },
86 { SQUAD, "SQUAD" },
87 { LONG, "LONG" },
88 { SHORT, "SHORT" },
89 { BYTE, "BYTE" },
90 { SECTIONS, "SECTIONS" },
91 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
92 { MEMORY, "MEMORY" },
93 { DEFINED, "DEFINED" },
94 { TARGET_K, "TARGET" },
95 { SEARCH_DIR, "SEARCH_DIR" },
96 { MAP, "MAP" },
97 { ENTRY, "ENTRY" },
98 { NEXT, "NEXT" },
99 { SIZEOF, "SIZEOF" },
100 { ADDR, "ADDR" },
101 { LOADADDR, "LOADADDR" },
102 { MAX_K, "MAX_K" },
103 { REL, "relocatable" },
104 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
105 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
106 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
107 { ORIGIN, "ORIGIN" },
108 { LENGTH, "LENGTH" },
109 { SEGMENT_START, "SEGMENT_START" }
110 };
111 unsigned int idx;
112
113 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
114 if (table[idx].code == code)
115 break;
116
117 if (infix_p)
118 fputc (' ', config.map_file);
119
120 if (idx < ARRAY_SIZE (table))
121 fputs (table[idx].name, config.map_file);
122 else if (code < 127)
123 fputc (code, config.map_file);
124 else
125 fprintf (config.map_file, "<code %d>", code);
126
127 if (infix_p)
128 fputc (' ', config.map_file);
129 }
130
131 static void
132 make_abs (void)
133 {
134 expld.result.value += expld.result.section->vma;
135 expld.result.section = bfd_abs_section_ptr;
136 }
137
138 static void
139 new_abs (bfd_vma value)
140 {
141 expld.result.valid_p = TRUE;
142 expld.result.section = bfd_abs_section_ptr;
143 expld.result.value = value;
144 expld.result.str = NULL;
145 }
146
147 etree_type *
148 exp_intop (bfd_vma value)
149 {
150 etree_type *new = stat_alloc (sizeof (new->value));
151 new->type.node_code = INT;
152 new->value.value = value;
153 new->value.str = NULL;
154 new->type.node_class = etree_value;
155 return new;
156 }
157
158 etree_type *
159 exp_bigintop (bfd_vma value, char *str)
160 {
161 etree_type *new = stat_alloc (sizeof (new->value));
162 new->type.node_code = INT;
163 new->value.value = value;
164 new->value.str = str;
165 new->type.node_class = etree_value;
166 return new;
167 }
168
169 /* Build an expression representing an unnamed relocatable value. */
170
171 etree_type *
172 exp_relop (asection *section, bfd_vma value)
173 {
174 etree_type *new = stat_alloc (sizeof (new->rel));
175 new->type.node_code = REL;
176 new->type.node_class = etree_rel;
177 new->rel.section = section;
178 new->rel.value = value;
179 return new;
180 }
181
182 static void
183 new_rel (bfd_vma value, char *str, asection *section)
184 {
185 expld.result.valid_p = TRUE;
186 expld.result.value = value;
187 expld.result.str = str;
188 expld.result.section = section;
189 }
190
191 static void
192 new_rel_from_abs (bfd_vma value)
193 {
194 expld.result.valid_p = TRUE;
195 expld.result.value = value - expld.section->vma;
196 expld.result.str = NULL;
197 expld.result.section = expld.section;
198 }
199
200 static void
201 fold_unary (etree_type *tree)
202 {
203 exp_fold_tree_1 (tree->unary.child);
204 if (expld.result.valid_p)
205 {
206 switch (tree->type.node_code)
207 {
208 case ALIGN_K:
209 if (expld.phase != lang_first_phase_enum)
210 {
211 make_abs ();
212 new_rel_from_abs (align_n (expld.dot, expld.result.value));
213 }
214 else
215 expld.result.valid_p = FALSE;
216 break;
217
218 case ABSOLUTE:
219 make_abs ();
220 break;
221
222 case '~':
223 make_abs ();
224 expld.result.value = ~expld.result.value;
225 break;
226
227 case '!':
228 make_abs ();
229 expld.result.value = !expld.result.value;
230 break;
231
232 case '-':
233 make_abs ();
234 expld.result.value = -expld.result.value;
235 break;
236
237 case NEXT:
238 /* Return next place aligned to value. */
239 if (expld.phase != lang_first_phase_enum)
240 {
241 make_abs ();
242 expld.result.value = align_n (expld.dot, expld.result.value);
243 }
244 else
245 expld.result.valid_p = FALSE;
246 break;
247
248 case DATA_SEGMENT_END:
249 if (expld.phase != lang_first_phase_enum
250 && expld.section == bfd_abs_section_ptr
251 && (expld.dataseg.phase == exp_dataseg_align_seen
252 || expld.dataseg.phase == exp_dataseg_relro_seen
253 || expld.dataseg.phase == exp_dataseg_adjust
254 || expld.dataseg.phase == exp_dataseg_relro_adjust
255 || expld.phase == lang_final_phase_enum))
256 {
257 if (expld.dataseg.phase == exp_dataseg_align_seen
258 || expld.dataseg.phase == exp_dataseg_relro_seen)
259 {
260 expld.dataseg.phase = exp_dataseg_end_seen;
261 expld.dataseg.end = expld.result.value;
262 }
263 }
264 else
265 expld.result.valid_p = FALSE;
266 break;
267
268 default:
269 FAIL ();
270 break;
271 }
272 }
273 }
274
275 static void
276 fold_binary (etree_type *tree)
277 {
278 exp_fold_tree_1 (tree->binary.lhs);
279
280 /* The SEGMENT_START operator is special because its first
281 operand is a string, not the name of a symbol. */
282 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
283 {
284 const char *segment_name;
285 segment_type *seg;
286 /* Check to see if the user has overridden the default
287 value. */
288 segment_name = tree->binary.rhs->name.name;
289 for (seg = segments; seg; seg = seg->next)
290 if (strcmp (seg->name, segment_name) == 0)
291 {
292 seg->used = TRUE;
293 expld.result.value = seg->value;
294 expld.result.str = NULL;
295 expld.result.section = NULL;
296 break;
297 }
298 }
299 else if (expld.result.valid_p)
300 {
301 etree_value_type lhs = expld.result;
302
303 exp_fold_tree_1 (tree->binary.rhs);
304 if (expld.result.valid_p)
305 {
306 /* If the values are from different sections, or this is an
307 absolute expression, make both the source arguments
308 absolute. However, adding or subtracting an absolute
309 value from a relative value is meaningful, and is an
310 exception. */
311 if (expld.section != bfd_abs_section_ptr
312 && lhs.section == bfd_abs_section_ptr
313 && tree->type.node_code == '+')
314 {
315 /* Keep the section of the rhs term. */
316 expld.result.value = lhs.value + expld.result.value;
317 return;
318 }
319 else if (expld.section != bfd_abs_section_ptr
320 && expld.result.section == bfd_abs_section_ptr
321 && (tree->type.node_code == '+'
322 || tree->type.node_code == '-'))
323 {
324 /* Keep the section of the lhs term. */
325 expld.result.section = lhs.section;
326 }
327 else if (expld.result.section != lhs.section
328 || expld.section == bfd_abs_section_ptr)
329 {
330 make_abs ();
331 lhs.value += lhs.section->vma;
332 }
333
334 switch (tree->type.node_code)
335 {
336 case '%':
337 if (expld.result.value != 0)
338 expld.result.value = ((bfd_signed_vma) lhs.value
339 % (bfd_signed_vma) expld.result.value);
340 else if (expld.phase != lang_mark_phase_enum)
341 einfo (_("%F%S %% by zero\n"));
342 break;
343
344 case '/':
345 if (expld.result.value != 0)
346 expld.result.value = ((bfd_signed_vma) lhs.value
347 / (bfd_signed_vma) expld.result.value);
348 else if (expld.phase != lang_mark_phase_enum)
349 einfo (_("%F%S / by zero\n"));
350 break;
351
352 #define BOP(x, y) \
353 case x: \
354 expld.result.value = lhs.value y expld.result.value; \
355 break;
356
357 BOP ('+', +);
358 BOP ('*', *);
359 BOP ('-', -);
360 BOP (LSHIFT, <<);
361 BOP (RSHIFT, >>);
362 BOP (EQ, ==);
363 BOP (NE, !=);
364 BOP ('<', <);
365 BOP ('>', >);
366 BOP (LE, <=);
367 BOP (GE, >=);
368 BOP ('&', &);
369 BOP ('^', ^);
370 BOP ('|', |);
371 BOP (ANDAND, &&);
372 BOP (OROR, ||);
373
374 case MAX_K:
375 if (lhs.value > expld.result.value)
376 expld.result.value = lhs.value;
377 break;
378
379 case MIN_K:
380 if (lhs.value < expld.result.value)
381 expld.result.value = lhs.value;
382 break;
383
384 case ALIGN_K:
385 expld.result.value = align_n (lhs.value, expld.result.value);
386 break;
387
388 case DATA_SEGMENT_ALIGN:
389 if (expld.phase != lang_first_phase_enum
390 && expld.section == bfd_abs_section_ptr
391 && (expld.dataseg.phase == exp_dataseg_none
392 || expld.dataseg.phase == exp_dataseg_adjust
393 || expld.dataseg.phase == exp_dataseg_relro_adjust
394 || expld.phase == lang_final_phase_enum))
395 {
396 bfd_vma maxpage = lhs.value;
397 bfd_vma commonpage = expld.result.value;
398
399 expld.result.value = align_n (expld.dot, maxpage);
400 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
401 expld.result.value = expld.dataseg.base;
402 else if (expld.dataseg.phase != exp_dataseg_adjust)
403 {
404 expld.result.value += expld.dot & (maxpage - 1);
405 if (expld.phase == lang_allocating_phase_enum)
406 {
407 expld.dataseg.phase = exp_dataseg_align_seen;
408 expld.dataseg.min_base = align_n (expld.dot, maxpage);
409 expld.dataseg.base = expld.result.value;
410 expld.dataseg.pagesize = commonpage;
411 expld.dataseg.maxpagesize = maxpage;
412 expld.dataseg.relro_end = 0;
413 }
414 }
415 else if (commonpage < maxpage)
416 expld.result.value += ((expld.dot + commonpage - 1)
417 & (maxpage - commonpage));
418 }
419 else
420 expld.result.valid_p = FALSE;
421 break;
422
423 case DATA_SEGMENT_RELRO_END:
424 if (expld.phase != lang_first_phase_enum
425 && (expld.dataseg.phase == exp_dataseg_align_seen
426 || expld.dataseg.phase == exp_dataseg_adjust
427 || expld.dataseg.phase == exp_dataseg_relro_adjust
428 || expld.phase == lang_final_phase_enum))
429 {
430 if (expld.dataseg.phase == exp_dataseg_align_seen
431 || expld.dataseg.phase == exp_dataseg_relro_adjust)
432 expld.dataseg.relro_end = lhs.value + expld.result.value;
433
434 if (expld.dataseg.phase == exp_dataseg_relro_adjust
435 && (expld.dataseg.relro_end
436 & (expld.dataseg.pagesize - 1)))
437 {
438 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
439 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
440 expld.result.value = (expld.dataseg.relro_end
441 - expld.result.value);
442 }
443 else
444 expld.result.value = lhs.value;
445
446 if (expld.dataseg.phase == exp_dataseg_align_seen)
447 expld.dataseg.phase = exp_dataseg_relro_seen;
448 }
449 else
450 expld.result.valid_p = FALSE;
451 break;
452
453 default:
454 FAIL ();
455 }
456 }
457 else
458 expld.result.valid_p = FALSE;
459 }
460 }
461
462 static void
463 fold_trinary (etree_type *tree)
464 {
465 exp_fold_tree_1 (tree->trinary.cond);
466 if (expld.result.valid_p)
467 exp_fold_tree_1 (expld.result.value
468 ? tree->trinary.lhs
469 : tree->trinary.rhs);
470 }
471
472 static void
473 fold_name (etree_type *tree)
474 {
475 memset (&expld.result, 0, sizeof (expld.result));
476
477 switch (tree->type.node_code)
478 {
479 case SIZEOF_HEADERS:
480 if (expld.phase != lang_first_phase_enum)
481 {
482 bfd_vma hdr_size = 0;
483 /* Don't find the real header size if only marking sections;
484 The bfd function may cache incorrect data. */
485 if (expld.phase != lang_mark_phase_enum)
486 hdr_size = bfd_sizeof_headers (output_bfd, link_info.relocatable);
487 new_abs (hdr_size);
488 }
489 break;
490 case DEFINED:
491 if (expld.phase == lang_first_phase_enum)
492 lang_track_definedness (tree->name.name);
493 else
494 {
495 struct bfd_link_hash_entry *h;
496 int def_iteration
497 = lang_symbol_definition_iteration (tree->name.name);
498
499 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
500 tree->name.name,
501 FALSE, FALSE, TRUE);
502 expld.result.value = (h != NULL
503 && (h->type == bfd_link_hash_defined
504 || h->type == bfd_link_hash_defweak
505 || h->type == bfd_link_hash_common)
506 && (def_iteration == lang_statement_iteration
507 || def_iteration == -1));
508 expld.result.section = bfd_abs_section_ptr;
509 expld.result.valid_p = TRUE;
510 }
511 break;
512 case NAME:
513 if (expld.phase == lang_first_phase_enum)
514 ;
515 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
516 new_rel_from_abs (expld.dot);
517 else
518 {
519 struct bfd_link_hash_entry *h;
520
521 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
522 tree->name.name,
523 TRUE, FALSE, TRUE);
524 if (!h)
525 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
526 else if (h->type == bfd_link_hash_defined
527 || h->type == bfd_link_hash_defweak)
528 {
529 if (bfd_is_abs_section (h->u.def.section))
530 new_abs (h->u.def.value);
531 else
532 {
533 asection *output_section;
534
535 output_section = h->u.def.section->output_section;
536 if (output_section == NULL)
537 {
538 if (expld.phase != lang_mark_phase_enum)
539 einfo (_("%X%S: unresolvable symbol `%s'"
540 " referenced in expression\n"),
541 tree->name.name);
542 }
543 else
544 new_rel (h->u.def.value + h->u.def.section->output_offset,
545 NULL, output_section);
546 }
547 }
548 else if (expld.phase == lang_final_phase_enum
549 || expld.assigning_to_dot)
550 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
551 tree->name.name);
552 else if (h->type == bfd_link_hash_new)
553 {
554 h->type = bfd_link_hash_undefined;
555 h->u.undef.abfd = NULL;
556 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
557 bfd_link_add_undef (link_info.hash, h);
558 }
559 }
560 break;
561
562 case ADDR:
563 if (expld.phase != lang_first_phase_enum)
564 {
565 lang_output_section_statement_type *os;
566
567 os = lang_output_section_find (tree->name.name);
568 if (os != NULL && os->processed > 0)
569 new_rel (0, NULL, os->bfd_section);
570 }
571 break;
572
573 case LOADADDR:
574 if (expld.phase != lang_first_phase_enum)
575 {
576 lang_output_section_statement_type *os;
577
578 os = lang_output_section_find (tree->name.name);
579 if (os != NULL && os->processed > 0)
580 {
581 if (os->load_base == NULL)
582 new_rel (0, NULL, os->bfd_section);
583 else
584 exp_fold_tree_1 (os->load_base);
585 }
586 }
587 break;
588
589 case SIZEOF:
590 if (expld.phase != lang_first_phase_enum)
591 {
592 int opb = bfd_octets_per_byte (output_bfd);
593 lang_output_section_statement_type *os;
594
595 os = lang_output_section_find (tree->name.name);
596 if (os != NULL && os->processed > 0)
597 new_abs (os->bfd_section->size / opb);
598 }
599 break;
600
601 case LENGTH:
602 {
603 lang_memory_region_type *mem;
604
605 mem = lang_memory_region_lookup (tree->name.name, FALSE);
606 if (mem != NULL)
607 new_abs (mem->length);
608 else
609 einfo (_("%F%S: undefined MEMORY region `%s'"
610 " referenced in expression\n"), tree->name.name);
611 }
612 break;
613
614 case ORIGIN:
615 {
616 lang_memory_region_type *mem;
617
618 mem = lang_memory_region_lookup (tree->name.name, FALSE);
619 if (mem != NULL)
620 new_abs (mem->origin);
621 else
622 einfo (_("%F%S: undefined MEMORY region `%s'"
623 " referenced in expression\n"), tree->name.name);
624 }
625 break;
626
627 default:
628 FAIL ();
629 break;
630 }
631 }
632
633 static void
634 exp_fold_tree_1 (etree_type *tree)
635 {
636 if (tree == NULL)
637 {
638 memset (&expld.result, 0, sizeof (expld.result));
639 return;
640 }
641
642 switch (tree->type.node_class)
643 {
644 case etree_value:
645 new_rel (tree->value.value, tree->value.str, expld.section);
646 break;
647
648 case etree_rel:
649 if (expld.phase != lang_first_phase_enum)
650 {
651 asection *output_section = tree->rel.section->output_section;
652 new_rel (tree->rel.value + tree->rel.section->output_offset,
653 NULL, output_section);
654 }
655 else
656 memset (&expld.result, 0, sizeof (expld.result));
657 break;
658
659 case etree_assert:
660 exp_fold_tree_1 (tree->assert_s.child);
661 if (expld.result.valid_p)
662 {
663 if (expld.phase == lang_mark_phase_enum)
664 /* We don't care if assert fails or not when we are just
665 marking if a section is used or not. */
666 expld.result.value = 1;
667 else if (!expld.result.value)
668 einfo ("%X%P: %s\n", tree->assert_s.message);
669 }
670 break;
671
672 case etree_unary:
673 fold_unary (tree);
674 break;
675
676 case etree_binary:
677 fold_binary (tree);
678 break;
679
680 case etree_trinary:
681 fold_trinary (tree);
682 break;
683
684 case etree_assign:
685 case etree_provide:
686 case etree_provided:
687 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
688 {
689 /* Assignment to dot can only be done during allocation. */
690 if (tree->type.node_class != etree_assign)
691 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
692 if (expld.phase == lang_mark_phase_enum
693 || expld.phase == lang_allocating_phase_enum
694 || (expld.phase == lang_final_phase_enum
695 && expld.section == bfd_abs_section_ptr))
696 {
697 /* Notify the folder that this is an assignment to dot. */
698 expld.assigning_to_dot = TRUE;
699 exp_fold_tree_1 (tree->assign.src);
700 expld.assigning_to_dot = FALSE;
701
702 if (!expld.result.valid_p)
703 {
704 if (expld.phase != lang_mark_phase_enum)
705 einfo (_("%F%S invalid assignment to location counter\n"));
706 }
707 else if (expld.dotp == NULL)
708 einfo (_("%F%S assignment to location counter"
709 " invalid outside of SECTION\n"));
710 else
711 {
712 bfd_vma nextdot;
713
714 nextdot = expld.result.value + expld.section->vma;
715 if (nextdot < expld.dot
716 && expld.section != bfd_abs_section_ptr)
717 einfo (_("%F%S cannot move location counter backwards"
718 " (from %V to %V)\n"), expld.dot, nextdot);
719 else
720 {
721 expld.dot = nextdot;
722 *expld.dotp = nextdot;
723 }
724 }
725 }
726 else
727 memset (&expld.result, 0, sizeof (expld.result));
728 }
729 else
730 {
731 struct bfd_link_hash_entry *h = NULL;
732
733 if (tree->type.node_class == etree_provide)
734 {
735 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
736 FALSE, FALSE, TRUE);
737 if (h == NULL
738 || (h->type != bfd_link_hash_new
739 && h->type != bfd_link_hash_undefined
740 && h->type != bfd_link_hash_common))
741 {
742 /* Do nothing. The symbol was never referenced, or was
743 defined by some object. */
744 break;
745 }
746 }
747
748 exp_fold_tree_1 (tree->assign.src);
749 if (expld.result.valid_p)
750 {
751 if (h == NULL)
752 {
753 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
754 TRUE, FALSE, TRUE);
755 if (h == NULL)
756 einfo (_("%P%F:%s: hash creation failed\n"),
757 tree->assign.dst);
758 }
759
760 /* FIXME: Should we worry if the symbol is already
761 defined? */
762 lang_update_definedness (tree->assign.dst, h);
763 h->type = bfd_link_hash_defined;
764 h->u.def.value = expld.result.value;
765 h->u.def.section = expld.result.section;
766 if (tree->type.node_class == etree_provide)
767 tree->type.node_class = etree_provided;
768 }
769 }
770 break;
771
772 case etree_name:
773 fold_name (tree);
774 break;
775
776 default:
777 FAIL ();
778 memset (&expld.result, 0, sizeof (expld.result));
779 break;
780 }
781 }
782
783 void
784 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
785 {
786 expld.dot = *dotp;
787 expld.dotp = dotp;
788 expld.section = current_section;
789 exp_fold_tree_1 (tree);
790 }
791
792 static void
793 exp_fold_tree_no_dot (etree_type *tree)
794 {
795 expld.dot = 0;
796 expld.dotp = NULL;
797 expld.section = bfd_abs_section_ptr;
798 exp_fold_tree_1 (tree);
799 }
800
801 etree_type *
802 exp_binop (int code, etree_type *lhs, etree_type *rhs)
803 {
804 etree_type value, *new;
805
806 value.type.node_code = code;
807 value.binary.lhs = lhs;
808 value.binary.rhs = rhs;
809 value.type.node_class = etree_binary;
810 exp_fold_tree_no_dot (&value);
811 if (expld.result.valid_p)
812 return exp_intop (expld.result.value);
813
814 new = stat_alloc (sizeof (new->binary));
815 memcpy (new, &value, sizeof (new->binary));
816 return new;
817 }
818
819 etree_type *
820 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
821 {
822 etree_type value, *new;
823
824 value.type.node_code = code;
825 value.trinary.lhs = lhs;
826 value.trinary.cond = cond;
827 value.trinary.rhs = rhs;
828 value.type.node_class = etree_trinary;
829 exp_fold_tree_no_dot (&value);
830 if (expld.result.valid_p)
831 return exp_intop (expld.result.value);
832
833 new = stat_alloc (sizeof (new->trinary));
834 memcpy (new, &value, sizeof (new->trinary));
835 return new;
836 }
837
838 etree_type *
839 exp_unop (int code, etree_type *child)
840 {
841 etree_type value, *new;
842
843 value.unary.type.node_code = code;
844 value.unary.child = child;
845 value.unary.type.node_class = etree_unary;
846 exp_fold_tree_no_dot (&value);
847 if (expld.result.valid_p)
848 return exp_intop (expld.result.value);
849
850 new = stat_alloc (sizeof (new->unary));
851 memcpy (new, &value, sizeof (new->unary));
852 return new;
853 }
854
855 etree_type *
856 exp_nameop (int code, const char *name)
857 {
858 etree_type value, *new;
859
860 value.name.type.node_code = code;
861 value.name.name = name;
862 value.name.type.node_class = etree_name;
863
864 exp_fold_tree_no_dot (&value);
865 if (expld.result.valid_p)
866 return exp_intop (expld.result.value);
867
868 new = stat_alloc (sizeof (new->name));
869 memcpy (new, &value, sizeof (new->name));
870 return new;
871
872 }
873
874 etree_type *
875 exp_assop (int code, const char *dst, etree_type *src)
876 {
877 etree_type *new;
878
879 new = stat_alloc (sizeof (new->assign));
880 new->type.node_code = code;
881 new->type.node_class = etree_assign;
882 new->assign.src = src;
883 new->assign.dst = dst;
884 return new;
885 }
886
887 /* Handle PROVIDE. */
888
889 etree_type *
890 exp_provide (const char *dst, etree_type *src)
891 {
892 etree_type *n;
893
894 n = stat_alloc (sizeof (n->assign));
895 n->assign.type.node_code = '=';
896 n->assign.type.node_class = etree_provide;
897 n->assign.src = src;
898 n->assign.dst = dst;
899 return n;
900 }
901
902 /* Handle ASSERT. */
903
904 etree_type *
905 exp_assert (etree_type *exp, const char *message)
906 {
907 etree_type *n;
908
909 n = stat_alloc (sizeof (n->assert_s));
910 n->assert_s.type.node_code = '!';
911 n->assert_s.type.node_class = etree_assert;
912 n->assert_s.child = exp;
913 n->assert_s.message = message;
914 return n;
915 }
916
917 void
918 exp_print_tree (etree_type *tree)
919 {
920 if (config.map_file == NULL)
921 config.map_file = stderr;
922
923 if (tree == NULL)
924 {
925 minfo ("NULL TREE\n");
926 return;
927 }
928
929 switch (tree->type.node_class)
930 {
931 case etree_value:
932 minfo ("0x%v", tree->value.value);
933 return;
934 case etree_rel:
935 if (tree->rel.section->owner != NULL)
936 minfo ("%B:", tree->rel.section->owner);
937 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
938 return;
939 case etree_assign:
940 fprintf (config.map_file, "%s", tree->assign.dst);
941 exp_print_token (tree->type.node_code, TRUE);
942 exp_print_tree (tree->assign.src);
943 break;
944 case etree_provide:
945 case etree_provided:
946 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
947 exp_print_tree (tree->assign.src);
948 fprintf (config.map_file, ")");
949 break;
950 case etree_binary:
951 fprintf (config.map_file, "(");
952 exp_print_tree (tree->binary.lhs);
953 exp_print_token (tree->type.node_code, TRUE);
954 exp_print_tree (tree->binary.rhs);
955 fprintf (config.map_file, ")");
956 break;
957 case etree_trinary:
958 exp_print_tree (tree->trinary.cond);
959 fprintf (config.map_file, "?");
960 exp_print_tree (tree->trinary.lhs);
961 fprintf (config.map_file, ":");
962 exp_print_tree (tree->trinary.rhs);
963 break;
964 case etree_unary:
965 exp_print_token (tree->unary.type.node_code, FALSE);
966 if (tree->unary.child)
967 {
968 fprintf (config.map_file, " (");
969 exp_print_tree (tree->unary.child);
970 fprintf (config.map_file, ")");
971 }
972 break;
973
974 case etree_assert:
975 fprintf (config.map_file, "ASSERT (");
976 exp_print_tree (tree->assert_s.child);
977 fprintf (config.map_file, ", %s)", tree->assert_s.message);
978 break;
979
980 case etree_name:
981 if (tree->type.node_code == NAME)
982 {
983 fprintf (config.map_file, "%s", tree->name.name);
984 }
985 else
986 {
987 exp_print_token (tree->type.node_code, FALSE);
988 if (tree->name.name)
989 fprintf (config.map_file, " (%s)", tree->name.name);
990 }
991 break;
992 default:
993 FAIL ();
994 break;
995 }
996 }
997
998 bfd_vma
999 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1000 {
1001 if (tree != NULL)
1002 {
1003 exp_fold_tree_no_dot (tree);
1004 if (expld.result.valid_p)
1005 return expld.result.value;
1006 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1007 einfo (_("%F%S nonconstant expression for %s\n"), name);
1008 }
1009 return def;
1010 }
1011
1012 int
1013 exp_get_value_int (etree_type *tree, int def, char *name)
1014 {
1015 return exp_get_vma (tree, def, name);
1016 }
1017
1018 fill_type *
1019 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1020 {
1021 fill_type *fill;
1022 size_t len;
1023 unsigned int val;
1024
1025 if (tree == NULL)
1026 return def;
1027
1028 exp_fold_tree_no_dot (tree);
1029 if (!expld.result.valid_p)
1030 {
1031 if (name != NULL && expld.phase != lang_mark_phase_enum)
1032 einfo (_("%F%S nonconstant expression for %s\n"), name);
1033 return def;
1034 }
1035
1036 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1037 {
1038 unsigned char *dst;
1039 unsigned char *s;
1040 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1041 fill->size = (len + 1) / 2;
1042 dst = fill->data;
1043 s = (unsigned char *) expld.result.str;
1044 val = 0;
1045 do
1046 {
1047 unsigned int digit;
1048
1049 digit = *s++ - '0';
1050 if (digit > 9)
1051 digit = (digit - 'A' + '0' + 10) & 0xf;
1052 val <<= 4;
1053 val += digit;
1054 --len;
1055 if ((len & 1) == 0)
1056 {
1057 *dst++ = val;
1058 val = 0;
1059 }
1060 }
1061 while (len != 0);
1062 }
1063 else
1064 {
1065 fill = xmalloc (4 + sizeof (*fill) - 1);
1066 val = expld.result.value;
1067 fill->data[0] = (val >> 24) & 0xff;
1068 fill->data[1] = (val >> 16) & 0xff;
1069 fill->data[2] = (val >> 8) & 0xff;
1070 fill->data[3] = (val >> 0) & 0xff;
1071 fill->size = 4;
1072 }
1073 return fill;
1074 }
1075
1076 bfd_vma
1077 exp_get_abs_int (etree_type *tree, int def, char *name)
1078 {
1079 if (tree != NULL)
1080 {
1081 exp_fold_tree_no_dot (tree);
1082
1083 if (expld.result.valid_p)
1084 {
1085 expld.result.value += expld.result.section->vma;
1086 return expld.result.value;
1087 }
1088 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1089 einfo (_("%F%S non constant expression for %s\n"), name);
1090 }
1091 return def;
1092 }
1093
1094 static bfd_vma
1095 align_n (bfd_vma value, bfd_vma align)
1096 {
1097 if (align <= 1)
1098 return value;
1099
1100 value = (value + align - 1) / align;
1101 return value * align;
1102 }
This page took 0.056118 seconds and 4 git commands to generate.