daily update
[binutils.git] / ld / ldexp.c
blobc4876a877e30f2d2032b0e85be1eca4974e326b0
1 /* This module handles expression trees.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002
4 Free Software Foundation, Inc.
5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
7 This file is part of GLD, the Gnu Linker.
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.
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.
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, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA. */
24 /* This module is in charge of working out the contents of expressions.
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. */
31 #include "bfd.h"
32 #include "sysdep.h"
33 #include "bfdlink.h"
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"
44 static void exp_print_token PARAMS ((token_code_type code));
45 static void make_abs PARAMS ((etree_value_type *ptr));
46 static etree_value_type new_abs PARAMS ((bfd_vma value));
47 static void check PARAMS ((lang_output_section_statement_type *os,
48 const char *name, const char *op));
49 static etree_value_type new_rel
50 PARAMS ((bfd_vma, char *, lang_output_section_statement_type *section));
51 static etree_value_type new_rel_from_section
52 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
53 static etree_value_type fold_binary
54 PARAMS ((etree_type *tree,
55 lang_output_section_statement_type *current_section,
56 lang_phase_type allocation_done,
57 bfd_vma dot, bfd_vma *dotp));
58 static etree_value_type fold_name
59 PARAMS ((etree_type *tree,
60 lang_output_section_statement_type *current_section,
61 lang_phase_type allocation_done,
62 bfd_vma dot));
63 static etree_value_type exp_fold_tree_no_dot
64 PARAMS ((etree_type *tree,
65 lang_output_section_statement_type *current_section,
66 lang_phase_type allocation_done));
68 struct exp_data_seg exp_data_seg;
70 static void
71 exp_print_token (code)
72 token_code_type code;
74 static CONST struct
76 token_code_type code;
77 char * name;
79 table[] =
81 { INT, "int" },
82 { NAME, "NAME" },
83 { PLUSEQ, "+=" },
84 { MINUSEQ, "-=" },
85 { MULTEQ, "*=" },
86 { DIVEQ, "/=" },
87 { LSHIFTEQ, "<<=" },
88 { RSHIFTEQ, ">>=" },
89 { ANDEQ, "&=" },
90 { OREQ, "|=" },
91 { OROR, "||" },
92 { ANDAND, "&&" },
93 { EQ, "==" },
94 { NE, "!=" },
95 { LE, "<=" },
96 { GE, ">=" },
97 { LSHIFT, "<<" },
98 { RSHIFT, ">>" },
99 { ALIGN_K, "ALIGN" },
100 { BLOCK, "BLOCK" },
101 { QUAD, "QUAD" },
102 { SQUAD, "SQUAD" },
103 { LONG, "LONG" },
104 { SHORT, "SHORT" },
105 { BYTE, "BYTE" },
106 { SECTIONS, "SECTIONS" },
107 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
108 { MEMORY, "MEMORY" },
109 { DEFINED, "DEFINED" },
110 { TARGET_K, "TARGET" },
111 { SEARCH_DIR, "SEARCH_DIR" },
112 { MAP, "MAP" },
113 { ENTRY, "ENTRY" },
114 { NEXT, "NEXT" },
115 { SIZEOF, "SIZEOF" },
116 { ADDR, "ADDR" },
117 { LOADADDR, "LOADADDR" },
118 { MAX_K, "MAX_K" },
119 { REL, "relocateable" },
120 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
121 { DATA_SEGMENT_END, "DATA_SEGMENT_END" }
123 unsigned int idx;
125 for (idx = ARRAY_SIZE (table); idx--;)
127 if (table[idx].code == code)
129 fprintf (config.map_file, " %s ", table[idx].name);
130 return;
134 /* Not in table, just print it alone. */
135 if (code < 127)
136 fprintf (config.map_file, " %c ", code);
137 else
138 fprintf (config.map_file, " <code %d> ", code);
141 static void
142 make_abs (ptr)
143 etree_value_type *ptr;
145 asection *s = ptr->section->bfd_section;
146 ptr->value += s->vma;
147 ptr->section = abs_output_section;
150 static etree_value_type
151 new_abs (value)
152 bfd_vma value;
154 etree_value_type new;
155 new.valid_p = true;
156 new.section = abs_output_section;
157 new.value = value;
158 return new;
161 static void
162 check (os, name, op)
163 lang_output_section_statement_type *os;
164 const char *name;
165 const char *op;
167 if (os == NULL)
168 einfo (_("%F%P: %s uses undefined section %s\n"), op, name);
169 if (! os->processed)
170 einfo (_("%F%P: %s forward reference of section %s\n"), op, name);
173 etree_type *
174 exp_intop (value)
175 bfd_vma value;
177 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
178 new->type.node_code = INT;
179 new->value.value = value;
180 new->value.str = NULL;
181 new->type.node_class = etree_value;
182 return new;
185 etree_type *
186 exp_bigintop (value, str)
187 bfd_vma value;
188 char *str;
190 etree_type *new = (etree_type *) stat_alloc (sizeof (new->value));
191 new->type.node_code = INT;
192 new->value.value = value;
193 new->value.str = str;
194 new->type.node_class = etree_value;
195 return new;
198 /* Build an expression representing an unnamed relocateable value. */
200 etree_type *
201 exp_relop (section, value)
202 asection *section;
203 bfd_vma value;
205 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
206 new->type.node_code = REL;
207 new->type.node_class = etree_rel;
208 new->rel.section = section;
209 new->rel.value = value;
210 return new;
213 static etree_value_type
214 new_rel (value, str, section)
215 bfd_vma value;
216 char *str;
217 lang_output_section_statement_type *section;
219 etree_value_type new;
220 new.valid_p = true;
221 new.value = value;
222 new.str = str;
223 new.section = section;
224 return new;
227 static etree_value_type
228 new_rel_from_section (value, section)
229 bfd_vma value;
230 lang_output_section_statement_type *section;
232 etree_value_type new;
233 new.valid_p = true;
234 new.value = value;
235 new.str = NULL;
236 new.section = section;
238 new.value -= section->bfd_section->vma;
240 return new;
243 static etree_value_type
244 fold_binary (tree, current_section, allocation_done, dot, dotp)
245 etree_type *tree;
246 lang_output_section_statement_type *current_section;
247 lang_phase_type allocation_done;
248 bfd_vma dot;
249 bfd_vma *dotp;
251 etree_value_type result;
253 result = exp_fold_tree (tree->binary.lhs, current_section,
254 allocation_done, dot, dotp);
255 if (result.valid_p)
257 etree_value_type other;
259 other = exp_fold_tree (tree->binary.rhs,
260 current_section,
261 allocation_done, dot, dotp);
262 if (other.valid_p)
264 /* If the values are from different sections, or this is an
265 absolute expression, make both the source arguments
266 absolute. However, adding or subtracting an absolute
267 value from a relative value is meaningful, and is an
268 exception. */
269 if (current_section != abs_output_section
270 && (other.section == abs_output_section
271 || (result.section == abs_output_section
272 && tree->type.node_code == '+'))
273 && (tree->type.node_code == '+'
274 || tree->type.node_code == '-'))
276 etree_value_type hold;
278 /* If there is only one absolute term, make sure it is the
279 second one. */
280 if (other.section != abs_output_section)
282 hold = result;
283 result = other;
284 other = hold;
287 else if (result.section != other.section
288 || current_section == abs_output_section)
290 make_abs (&result);
291 make_abs (&other);
294 switch (tree->type.node_code)
296 case '%':
297 if (other.value == 0)
298 einfo (_("%F%S %% by zero\n"));
299 result.value = ((bfd_signed_vma) result.value
300 % (bfd_signed_vma) other.value);
301 break;
303 case '/':
304 if (other.value == 0)
305 einfo (_("%F%S / by zero\n"));
306 result.value = ((bfd_signed_vma) result.value
307 / (bfd_signed_vma) other.value);
308 break;
310 #define BOP(x,y) case x : result.value = result.value y other.value; break;
311 BOP ('+', +);
312 BOP ('*', *);
313 BOP ('-', -);
314 BOP (LSHIFT, <<);
315 BOP (RSHIFT, >>);
316 BOP (EQ, ==);
317 BOP (NE, !=);
318 BOP ('<', <);
319 BOP ('>', >);
320 BOP (LE, <=);
321 BOP (GE, >=);
322 BOP ('&', &);
323 BOP ('^', ^);
324 BOP ('|', |);
325 BOP (ANDAND, &&);
326 BOP (OROR, ||);
328 case MAX_K:
329 if (result.value < other.value)
330 result = other;
331 break;
333 case MIN_K:
334 if (result.value > other.value)
335 result = other;
336 break;
338 case DATA_SEGMENT_ALIGN:
339 if (allocation_done != lang_first_phase_enum
340 && current_section == abs_output_section
341 && (exp_data_seg.phase == exp_dataseg_none
342 || exp_data_seg.phase == exp_dataseg_adjust
343 || allocation_done != lang_allocating_phase_enum))
345 bfd_vma maxpage = result.value;
347 result.value = ALIGN_N (dot, maxpage);
348 if (exp_data_seg.phase != exp_dataseg_adjust)
350 result.value += dot & (maxpage - 1);
351 if (allocation_done == lang_allocating_phase_enum)
353 exp_data_seg.phase = exp_dataseg_align_seen;
354 exp_data_seg.base = result.value;
355 exp_data_seg.pagesize = other.value;
358 else if (other.value < maxpage)
359 result.value += (dot + other.value - 1)
360 & (maxpage - other.value);
362 else
363 result.valid_p = false;
364 break;
366 default:
367 FAIL ();
370 else
372 result.valid_p = false;
376 return result;
379 etree_value_type
380 invalid ()
382 etree_value_type new;
383 new.valid_p = false;
384 return new;
387 static etree_value_type
388 fold_name (tree, current_section, allocation_done, dot)
389 etree_type *tree;
390 lang_output_section_statement_type *current_section;
391 lang_phase_type allocation_done;
392 bfd_vma dot;
394 etree_value_type result;
396 switch (tree->type.node_code)
398 case SIZEOF_HEADERS:
399 if (allocation_done != lang_first_phase_enum)
401 result = new_abs ((bfd_vma)
402 bfd_sizeof_headers (output_bfd,
403 link_info.relocateable));
405 else
407 result.valid_p = false;
409 break;
410 case DEFINED:
411 if (allocation_done == lang_first_phase_enum)
412 result.valid_p = false;
413 else
415 struct bfd_link_hash_entry *h;
417 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
418 tree->name.name,
419 false, false, true);
420 result.value = (h != (struct bfd_link_hash_entry *) NULL
421 && (h->type == bfd_link_hash_defined
422 || h->type == bfd_link_hash_defweak
423 || h->type == bfd_link_hash_common));
424 result.section = 0;
425 result.valid_p = true;
427 break;
428 case NAME:
429 result.valid_p = false;
430 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
432 if (allocation_done != lang_first_phase_enum)
433 result = new_rel_from_section (dot, current_section);
434 else
435 result = invalid ();
437 else if (allocation_done != lang_first_phase_enum)
439 struct bfd_link_hash_entry *h;
441 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
442 tree->name.name,
443 false, false, true);
444 if (h != NULL
445 && (h->type == bfd_link_hash_defined
446 || h->type == bfd_link_hash_defweak))
448 if (bfd_is_abs_section (h->u.def.section))
449 result = new_abs (h->u.def.value);
450 else if (allocation_done == lang_final_phase_enum
451 || allocation_done == lang_allocating_phase_enum)
453 asection *output_section;
455 output_section = h->u.def.section->output_section;
456 if (output_section == NULL)
457 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
458 tree->name.name);
459 else
461 lang_output_section_statement_type *os;
463 os = (lang_output_section_statement_lookup
464 (bfd_get_section_name (output_bfd,
465 output_section)));
467 /* FIXME: Is this correct if this section is
468 being linked with -R? */
469 result = new_rel ((h->u.def.value
470 + h->u.def.section->output_offset),
471 NULL,
472 os);
476 else if (allocation_done == lang_final_phase_enum)
477 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
478 tree->name.name);
480 break;
482 case ADDR:
483 if (allocation_done != lang_first_phase_enum)
485 lang_output_section_statement_type *os;
487 os = lang_output_section_find (tree->name.name);
488 check (os, tree->name.name, "ADDR");
489 result = new_rel (0, NULL, os);
491 else
492 result = invalid ();
493 break;
495 case LOADADDR:
496 if (allocation_done != lang_first_phase_enum)
498 lang_output_section_statement_type *os;
500 os = lang_output_section_find (tree->name.name);
501 check (os, tree->name.name, "LOADADDR");
502 if (os->load_base == NULL)
503 result = new_rel (0, NULL, os);
504 else
505 result = exp_fold_tree_no_dot (os->load_base,
506 abs_output_section,
507 allocation_done);
509 else
510 result = invalid ();
511 break;
513 case SIZEOF:
514 if (allocation_done != lang_first_phase_enum)
516 int opb = bfd_octets_per_byte (output_bfd);
517 lang_output_section_statement_type *os;
519 os = lang_output_section_find (tree->name.name);
520 check (os, tree->name.name, "SIZEOF");
521 result = new_abs (os->bfd_section->_raw_size / opb);
523 else
524 result = invalid ();
525 break;
527 default:
528 FAIL ();
529 break;
532 return result;
535 etree_value_type
536 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
537 etree_type *tree;
538 lang_output_section_statement_type *current_section;
539 lang_phase_type allocation_done;
540 bfd_vma dot;
541 bfd_vma *dotp;
543 etree_value_type result;
545 if (tree == NULL)
547 result.valid_p = false;
548 return result;
551 switch (tree->type.node_class)
553 case etree_value:
554 result = new_rel (tree->value.value, tree->value.str, current_section);
555 break;
557 case etree_rel:
558 if (allocation_done != lang_final_phase_enum)
559 result.valid_p = false;
560 else
561 result = new_rel ((tree->rel.value
562 + tree->rel.section->output_section->vma
563 + tree->rel.section->output_offset),
564 NULL,
565 current_section);
566 break;
568 case etree_assert:
569 result = exp_fold_tree (tree->assert_s.child,
570 current_section,
571 allocation_done, dot, dotp);
572 if (result.valid_p)
574 if (! result.value)
575 einfo ("%F%P: %s\n", tree->assert_s.message);
576 return result;
578 break;
580 case etree_unary:
581 result = exp_fold_tree (tree->unary.child,
582 current_section,
583 allocation_done, dot, dotp);
584 if (result.valid_p)
586 switch (tree->type.node_code)
588 case ALIGN_K:
589 if (allocation_done != lang_first_phase_enum)
590 result = new_rel_from_section (ALIGN_N (dot, result.value),
591 current_section);
592 else
593 result.valid_p = false;
594 break;
596 case ABSOLUTE:
597 if (allocation_done != lang_first_phase_enum && result.valid_p)
599 result.value += result.section->bfd_section->vma;
600 result.section = abs_output_section;
602 else
603 result.valid_p = false;
604 break;
606 case '~':
607 make_abs (&result);
608 result.value = ~result.value;
609 break;
611 case '!':
612 make_abs (&result);
613 result.value = !result.value;
614 break;
616 case '-':
617 make_abs (&result);
618 result.value = -result.value;
619 break;
621 case NEXT:
622 /* Return next place aligned to value. */
623 if (allocation_done == lang_allocating_phase_enum)
625 make_abs (&result);
626 result.value = ALIGN_N (dot, result.value);
628 else
629 result.valid_p = false;
630 break;
632 case DATA_SEGMENT_END:
633 if (allocation_done != lang_first_phase_enum
634 && current_section == abs_output_section
635 && (exp_data_seg.phase == exp_dataseg_align_seen
636 || exp_data_seg.phase == exp_dataseg_adjust
637 || allocation_done != lang_allocating_phase_enum))
639 if (exp_data_seg.phase == exp_dataseg_align_seen)
641 exp_data_seg.phase = exp_dataseg_end_seen;
642 exp_data_seg.end = result.value;
645 else
646 result.valid_p = false;
647 break;
649 default:
650 FAIL ();
651 break;
654 break;
656 case etree_trinary:
657 result = exp_fold_tree (tree->trinary.cond, current_section,
658 allocation_done, dot, dotp);
659 if (result.valid_p)
660 result = exp_fold_tree ((result.value
661 ? tree->trinary.lhs
662 : tree->trinary.rhs),
663 current_section,
664 allocation_done, dot, dotp);
665 break;
667 case etree_binary:
668 result = fold_binary (tree, current_section, allocation_done,
669 dot, dotp);
670 break;
672 case etree_assign:
673 case etree_provide:
674 case etree_provided:
675 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
677 /* Assignment to dot can only be done during allocation. */
678 if (tree->type.node_class != etree_assign)
679 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
680 if (allocation_done == lang_allocating_phase_enum
681 || (allocation_done == lang_final_phase_enum
682 && current_section == abs_output_section))
684 result = exp_fold_tree (tree->assign.src,
685 current_section,
686 allocation_done, dot,
687 dotp);
688 if (! result.valid_p)
689 einfo (_("%F%S invalid assignment to location counter\n"));
690 else
692 if (current_section == NULL)
693 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
694 else
696 bfd_vma nextdot;
698 nextdot = (result.value
699 + current_section->bfd_section->vma);
700 if (nextdot < dot
701 && current_section != abs_output_section)
702 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
703 dot, nextdot);
704 else
705 *dotp = nextdot;
710 else
712 result = exp_fold_tree (tree->assign.src,
713 current_section, allocation_done,
714 dot, dotp);
715 if (result.valid_p)
717 boolean create;
718 struct bfd_link_hash_entry *h;
720 if (tree->type.node_class == etree_assign)
721 create = true;
722 else
723 create = false;
724 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
725 create, false, false);
726 if (h == (struct bfd_link_hash_entry *) NULL)
728 if (tree->type.node_class == etree_assign)
729 einfo (_("%P%F:%s: hash creation failed\n"),
730 tree->assign.dst);
732 else if (tree->type.node_class == etree_provide
733 && h->type != bfd_link_hash_undefined
734 && h->type != bfd_link_hash_common)
736 /* Do nothing. The symbol was defined by some
737 object. */
739 else
741 /* FIXME: Should we worry if the symbol is already
742 defined? */
743 h->type = bfd_link_hash_defined;
744 h->u.def.value = result.value;
745 h->u.def.section = result.section->bfd_section;
746 if (tree->type.node_class == etree_provide)
747 tree->type.node_class = etree_provided;
751 break;
753 case etree_name:
754 result = fold_name (tree, current_section, allocation_done, dot);
755 break;
757 default:
758 FAIL ();
759 break;
762 return result;
765 static etree_value_type
766 exp_fold_tree_no_dot (tree, current_section, allocation_done)
767 etree_type *tree;
768 lang_output_section_statement_type *current_section;
769 lang_phase_type allocation_done;
771 return exp_fold_tree (tree, current_section, allocation_done,
772 (bfd_vma) 0, (bfd_vma *) NULL);
775 etree_type *
776 exp_binop (code, lhs, rhs)
777 int code;
778 etree_type *lhs;
779 etree_type *rhs;
781 etree_type value, *new;
782 etree_value_type r;
784 value.type.node_code = code;
785 value.binary.lhs = lhs;
786 value.binary.rhs = rhs;
787 value.type.node_class = etree_binary;
788 r = exp_fold_tree_no_dot (&value,
789 abs_output_section,
790 lang_first_phase_enum);
791 if (r.valid_p)
793 return exp_intop (r.value);
795 new = (etree_type *) stat_alloc (sizeof (new->binary));
796 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
797 return new;
800 etree_type *
801 exp_trinop (code, cond, lhs, rhs)
802 int code;
803 etree_type *cond;
804 etree_type *lhs;
805 etree_type *rhs;
807 etree_type value, *new;
808 etree_value_type r;
809 value.type.node_code = code;
810 value.trinary.lhs = lhs;
811 value.trinary.cond = cond;
812 value.trinary.rhs = rhs;
813 value.type.node_class = etree_trinary;
814 r = exp_fold_tree_no_dot (&value,
815 (lang_output_section_statement_type *) NULL,
816 lang_first_phase_enum);
817 if (r.valid_p)
818 return exp_intop (r.value);
820 new = (etree_type *) stat_alloc (sizeof (new->trinary));
821 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
822 return new;
825 etree_type *
826 exp_unop (code, child)
827 int code;
828 etree_type *child;
830 etree_type value, *new;
832 etree_value_type r;
833 value.unary.type.node_code = code;
834 value.unary.child = child;
835 value.unary.type.node_class = etree_unary;
836 r = exp_fold_tree_no_dot (&value, abs_output_section,
837 lang_first_phase_enum);
838 if (r.valid_p)
839 return exp_intop (r.value);
841 new = (etree_type *) stat_alloc (sizeof (new->unary));
842 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
843 return new;
846 etree_type *
847 exp_nameop (code, name)
848 int code;
849 CONST char *name;
851 etree_type value, *new;
852 etree_value_type r;
853 value.name.type.node_code = code;
854 value.name.name = name;
855 value.name.type.node_class = etree_name;
857 r = exp_fold_tree_no_dot (&value,
858 (lang_output_section_statement_type *) NULL,
859 lang_first_phase_enum);
860 if (r.valid_p)
861 return exp_intop (r.value);
863 new = (etree_type *) stat_alloc (sizeof (new->name));
864 memcpy ((char *) new, (char *) &value, sizeof (new->name));
865 return new;
869 etree_type *
870 exp_assop (code, dst, src)
871 int code;
872 CONST char *dst;
873 etree_type *src;
875 etree_type value, *new;
877 value.assign.type.node_code = code;
879 value.assign.src = src;
880 value.assign.dst = dst;
881 value.assign.type.node_class = etree_assign;
883 #if 0
884 if (exp_fold_tree_no_dot (&value, &result))
885 return exp_intop (result);
886 #endif
887 new = (etree_type *) stat_alloc (sizeof (new->assign));
888 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
889 return new;
892 /* Handle PROVIDE. */
894 etree_type *
895 exp_provide (dst, src)
896 const char *dst;
897 etree_type *src;
899 etree_type *n;
901 n = (etree_type *) stat_alloc (sizeof (n->assign));
902 n->assign.type.node_code = '=';
903 n->assign.type.node_class = etree_provide;
904 n->assign.src = src;
905 n->assign.dst = dst;
906 return n;
909 /* Handle ASSERT. */
911 etree_type *
912 exp_assert (exp, message)
913 etree_type *exp;
914 const char *message;
916 etree_type *n;
918 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
919 n->assert_s.type.node_code = '!';
920 n->assert_s.type.node_class = etree_assert;
921 n->assert_s.child = exp;
922 n->assert_s.message = message;
923 return n;
926 void
927 exp_print_tree (tree)
928 etree_type *tree;
930 if (config.map_file == NULL)
931 config.map_file = stderr;
933 if (tree == NULL)
935 minfo ("NULL TREE\n");
936 return;
939 switch (tree->type.node_class)
941 case etree_value:
942 minfo ("0x%v", tree->value.value);
943 return;
944 case etree_rel:
945 if (tree->rel.section->owner != NULL)
946 minfo ("%B:", tree->rel.section->owner);
947 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
948 return;
949 case etree_assign:
950 #if 0
951 if (tree->assign.dst->sdefs != (asymbol *) NULL)
952 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
953 tree->assign.dst->sdefs->value);
954 else
955 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
956 #endif
957 fprintf (config.map_file, "%s", tree->assign.dst);
958 exp_print_token (tree->type.node_code);
959 exp_print_tree (tree->assign.src);
960 break;
961 case etree_provide:
962 case etree_provided:
963 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
964 exp_print_tree (tree->assign.src);
965 fprintf (config.map_file, ")");
966 break;
967 case etree_binary:
968 fprintf (config.map_file, "(");
969 exp_print_tree (tree->binary.lhs);
970 exp_print_token (tree->type.node_code);
971 exp_print_tree (tree->binary.rhs);
972 fprintf (config.map_file, ")");
973 break;
974 case etree_trinary:
975 exp_print_tree (tree->trinary.cond);
976 fprintf (config.map_file, "?");
977 exp_print_tree (tree->trinary.lhs);
978 fprintf (config.map_file, ":");
979 exp_print_tree (tree->trinary.rhs);
980 break;
981 case etree_unary:
982 exp_print_token (tree->unary.type.node_code);
983 if (tree->unary.child)
985 fprintf (config.map_file, "(");
986 exp_print_tree (tree->unary.child);
987 fprintf (config.map_file, ")");
989 break;
991 case etree_assert:
992 fprintf (config.map_file, "ASSERT (");
993 exp_print_tree (tree->assert_s.child);
994 fprintf (config.map_file, ", %s)", tree->assert_s.message);
995 break;
997 case etree_undef:
998 fprintf (config.map_file, "????????");
999 break;
1000 case etree_name:
1001 if (tree->type.node_code == NAME)
1003 fprintf (config.map_file, "%s", tree->name.name);
1005 else
1007 exp_print_token (tree->type.node_code);
1008 if (tree->name.name)
1009 fprintf (config.map_file, "(%s)", tree->name.name);
1011 break;
1012 default:
1013 FAIL ();
1014 break;
1018 bfd_vma
1019 exp_get_vma (tree, def, name, allocation_done)
1020 etree_type *tree;
1021 bfd_vma def;
1022 char *name;
1023 lang_phase_type allocation_done;
1025 etree_value_type r;
1027 if (tree != NULL)
1029 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1030 if (! r.valid_p && name != NULL)
1031 einfo (_("%F%S nonconstant expression for %s\n"), name);
1032 return r.value;
1034 else
1035 return def;
1039 exp_get_value_int (tree, def, name, allocation_done)
1040 etree_type *tree;
1041 int def;
1042 char *name;
1043 lang_phase_type allocation_done;
1045 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1048 fill_type *
1049 exp_get_fill (tree, def, name, allocation_done)
1050 etree_type *tree;
1051 fill_type *def;
1052 char *name;
1053 lang_phase_type allocation_done;
1055 fill_type *fill;
1056 etree_value_type r;
1057 size_t len;
1058 unsigned int val;
1060 if (tree == NULL)
1061 return def;
1063 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1064 if (! r.valid_p && name != NULL)
1065 einfo (_("%F%S nonconstant expression for %s\n"), name);
1067 if (r.str != NULL && (len = strlen (r.str)) != 0)
1069 unsigned char *dst;
1070 unsigned char *s;
1071 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1072 fill->size = (len + 1) / 2;
1073 dst = fill->data;
1074 s = r.str;
1075 val = 0;
1078 unsigned int digit;
1080 digit = *s++ - '0';
1081 if (digit > 9)
1082 digit = (digit - 'A' + '0' + 10) & 0xf;
1083 val <<= 4;
1084 val += digit;
1085 --len;
1086 if ((len & 1) == 0)
1088 *dst++ = val;
1089 val = 0;
1092 while (len != 0);
1094 else
1096 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1097 val = r.value;
1098 fill->data[0] = (val >> 24) & 0xff;
1099 fill->data[1] = (val >> 16) & 0xff;
1100 fill->data[2] = (val >> 8) & 0xff;
1101 fill->data[3] = (val >> 0) & 0xff;
1102 fill->size = 4;
1104 return fill;
1107 bfd_vma
1108 exp_get_abs_int (tree, def, name, allocation_done)
1109 etree_type *tree;
1110 int def ATTRIBUTE_UNUSED;
1111 char *name;
1112 lang_phase_type allocation_done;
1114 etree_value_type res;
1115 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1117 if (res.valid_p)
1118 res.value += res.section->bfd_section->vma;
1119 else
1120 einfo (_("%F%S non constant expression for %s\n"), name);
1122 return res.value;