2002-02-19 Martin Schwidefsky <schwidefsky@de.ibm.com>
[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
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, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, 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_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));
67
68 struct exp_data_seg exp_data_seg;
69
70 static void
71 exp_print_token (code)
72 token_code_type code;
73 {
74 static CONST struct
75 {
76 token_code_type code;
77 char * name;
78 }
79 table[] =
80 {
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" }
122 };
123 unsigned int idx;
124
125 for (idx = ARRAY_SIZE (table); idx--;)
126 {
127 if (table[idx].code == code)
128 {
129 fprintf (config.map_file, " %s ", table[idx].name);
130 return;
131 }
132 }
133
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);
139 }
140
141 static void
142 make_abs (ptr)
143 etree_value_type *ptr;
144 {
145 asection *s = ptr->section->bfd_section;
146 ptr->value += s->vma;
147 ptr->section = abs_output_section;
148 }
149
150 static etree_value_type
151 new_abs (value)
152 bfd_vma value;
153 {
154 etree_value_type new;
155 new.valid_p = true;
156 new.section = abs_output_section;
157 new.value = value;
158 return new;
159 }
160
161 static void
162 check (os, name, op)
163 lang_output_section_statement_type *os;
164 const char *name;
165 const char *op;
166 {
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);
171 }
172
173 etree_type *
174 exp_intop (value)
175 bfd_vma value;
176 {
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;
183 }
184
185 etree_type *
186 exp_bigintop (value, str)
187 bfd_vma value;
188 char *str;
189 {
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;
196 }
197
198 /* Build an expression representing an unnamed relocateable value. */
199
200 etree_type *
201 exp_relop (section, value)
202 asection *section;
203 bfd_vma value;
204 {
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;
211 }
212
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;
218 {
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;
225 }
226
227 static etree_value_type
228 new_rel_from_section (value, section)
229 bfd_vma value;
230 lang_output_section_statement_type *section;
231 {
232 etree_value_type new;
233 new.valid_p = true;
234 new.value = value;
235 new.str = NULL;
236 new.section = section;
237
238 new.value -= section->bfd_section->vma;
239
240 return new;
241 }
242
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;
250 {
251 etree_value_type result;
252
253 result = exp_fold_tree (tree->binary.lhs, current_section,
254 allocation_done, dot, dotp);
255 if (result.valid_p)
256 {
257 etree_value_type other;
258
259 other = exp_fold_tree (tree->binary.rhs,
260 current_section,
261 allocation_done, dot, dotp);
262 if (other.valid_p)
263 {
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 == '-'))
275 {
276 etree_value_type hold;
277
278 /* If there is only one absolute term, make sure it is the
279 second one. */
280 if (other.section != abs_output_section)
281 {
282 hold = result;
283 result = other;
284 other = hold;
285 }
286 }
287 else if (result.section != other.section
288 || current_section == abs_output_section)
289 {
290 make_abs (&result);
291 make_abs (&other);
292 }
293
294 switch (tree->type.node_code)
295 {
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;
302
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;
309
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, ||);
327
328 case MAX_K:
329 if (result.value < other.value)
330 result = other;
331 break;
332
333 case MIN_K:
334 if (result.value > other.value)
335 result = other;
336 break;
337
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))
344 {
345 bfd_vma maxpage = result.value;
346
347 result.value = ALIGN_N (dot, maxpage);
348 if (exp_data_seg.phase != exp_dataseg_adjust)
349 {
350 result.value += dot & (maxpage - 1);
351 if (allocation_done == lang_allocating_phase_enum)
352 {
353 exp_data_seg.phase = exp_dataseg_align_seen;
354 exp_data_seg.base = result.value;
355 exp_data_seg.pagesize = other.value;
356 }
357 }
358 else if (other.value < maxpage)
359 result.value += dot & (maxpage - other.value);
360 }
361 else
362 result.valid_p = false;
363 break;
364
365 default:
366 FAIL ();
367 }
368 }
369 else
370 {
371 result.valid_p = false;
372 }
373 }
374
375 return result;
376 }
377
378 etree_value_type
379 invalid ()
380 {
381 etree_value_type new;
382 new.valid_p = false;
383 return new;
384 }
385
386 static etree_value_type
387 fold_name (tree, current_section, allocation_done, dot)
388 etree_type *tree;
389 lang_output_section_statement_type *current_section;
390 lang_phase_type allocation_done;
391 bfd_vma dot;
392 {
393 etree_value_type result;
394
395 switch (tree->type.node_code)
396 {
397 case SIZEOF_HEADERS:
398 if (allocation_done != lang_first_phase_enum)
399 {
400 result = new_abs ((bfd_vma)
401 bfd_sizeof_headers (output_bfd,
402 link_info.relocateable));
403 }
404 else
405 {
406 result.valid_p = false;
407 }
408 break;
409 case DEFINED:
410 if (allocation_done == lang_first_phase_enum)
411 result.valid_p = false;
412 else
413 {
414 struct bfd_link_hash_entry *h;
415
416 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
417 tree->name.name,
418 false, false, true);
419 result.value = (h != (struct bfd_link_hash_entry *) NULL
420 && (h->type == bfd_link_hash_defined
421 || h->type == bfd_link_hash_defweak
422 || h->type == bfd_link_hash_common));
423 result.section = 0;
424 result.valid_p = true;
425 }
426 break;
427 case NAME:
428 result.valid_p = false;
429 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
430 {
431 if (allocation_done != lang_first_phase_enum)
432 result = new_rel_from_section (dot, current_section);
433 else
434 result = invalid ();
435 }
436 else if (allocation_done != lang_first_phase_enum)
437 {
438 struct bfd_link_hash_entry *h;
439
440 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
441 tree->name.name,
442 false, false, true);
443 if (h != NULL
444 && (h->type == bfd_link_hash_defined
445 || h->type == bfd_link_hash_defweak))
446 {
447 if (bfd_is_abs_section (h->u.def.section))
448 result = new_abs (h->u.def.value);
449 else if (allocation_done == lang_final_phase_enum
450 || allocation_done == lang_allocating_phase_enum)
451 {
452 asection *output_section;
453
454 output_section = h->u.def.section->output_section;
455 if (output_section == NULL)
456 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
457 tree->name.name);
458 else
459 {
460 lang_output_section_statement_type *os;
461
462 os = (lang_output_section_statement_lookup
463 (bfd_get_section_name (output_bfd,
464 output_section)));
465
466 /* FIXME: Is this correct if this section is
467 being linked with -R? */
468 result = new_rel ((h->u.def.value
469 + h->u.def.section->output_offset),
470 NULL,
471 os);
472 }
473 }
474 }
475 else if (allocation_done == lang_final_phase_enum)
476 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
477 tree->name.name);
478 }
479 break;
480
481 case ADDR:
482 if (allocation_done != lang_first_phase_enum)
483 {
484 lang_output_section_statement_type *os;
485
486 os = lang_output_section_find (tree->name.name);
487 check (os, tree->name.name, "ADDR");
488 result = new_rel (0, NULL, os);
489 }
490 else
491 result = invalid ();
492 break;
493
494 case LOADADDR:
495 if (allocation_done != lang_first_phase_enum)
496 {
497 lang_output_section_statement_type *os;
498
499 os = lang_output_section_find (tree->name.name);
500 check (os, tree->name.name, "LOADADDR");
501 if (os->load_base == NULL)
502 result = new_rel (0, NULL, os);
503 else
504 result = exp_fold_tree_no_dot (os->load_base,
505 abs_output_section,
506 allocation_done);
507 }
508 else
509 result = invalid ();
510 break;
511
512 case SIZEOF:
513 if (allocation_done != lang_first_phase_enum)
514 {
515 int opb = bfd_octets_per_byte (output_bfd);
516 lang_output_section_statement_type *os;
517
518 os = lang_output_section_find (tree->name.name);
519 check (os, tree->name.name, "SIZEOF");
520 result = new_abs (os->bfd_section->_raw_size / opb);
521 }
522 else
523 result = invalid ();
524 break;
525
526 default:
527 FAIL ();
528 break;
529 }
530
531 return result;
532 }
533
534 etree_value_type
535 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
536 etree_type *tree;
537 lang_output_section_statement_type *current_section;
538 lang_phase_type allocation_done;
539 bfd_vma dot;
540 bfd_vma *dotp;
541 {
542 etree_value_type result;
543
544 if (tree == NULL)
545 {
546 result.valid_p = false;
547 return result;
548 }
549
550 switch (tree->type.node_class)
551 {
552 case etree_value:
553 result = new_rel (tree->value.value, tree->value.str, current_section);
554 break;
555
556 case etree_rel:
557 if (allocation_done != lang_final_phase_enum)
558 result.valid_p = false;
559 else
560 result = new_rel ((tree->rel.value
561 + tree->rel.section->output_section->vma
562 + tree->rel.section->output_offset),
563 NULL,
564 current_section);
565 break;
566
567 case etree_assert:
568 result = exp_fold_tree (tree->assert_s.child,
569 current_section,
570 allocation_done, dot, dotp);
571 if (result.valid_p)
572 {
573 if (! result.value)
574 einfo ("%F%P: %s\n", tree->assert_s.message);
575 return result;
576 }
577 break;
578
579 case etree_unary:
580 result = exp_fold_tree (tree->unary.child,
581 current_section,
582 allocation_done, dot, dotp);
583 if (result.valid_p)
584 {
585 switch (tree->type.node_code)
586 {
587 case ALIGN_K:
588 if (allocation_done != lang_first_phase_enum)
589 result = new_rel_from_section (ALIGN_N (dot, result.value),
590 current_section);
591 else
592 result.valid_p = false;
593 break;
594
595 case ABSOLUTE:
596 if (allocation_done != lang_first_phase_enum && result.valid_p)
597 {
598 result.value += result.section->bfd_section->vma;
599 result.section = abs_output_section;
600 }
601 else
602 result.valid_p = false;
603 break;
604
605 case '~':
606 make_abs (&result);
607 result.value = ~result.value;
608 break;
609
610 case '!':
611 make_abs (&result);
612 result.value = !result.value;
613 break;
614
615 case '-':
616 make_abs (&result);
617 result.value = -result.value;
618 break;
619
620 case NEXT:
621 /* Return next place aligned to value. */
622 if (allocation_done == lang_allocating_phase_enum)
623 {
624 make_abs (&result);
625 result.value = ALIGN_N (dot, result.value);
626 }
627 else
628 result.valid_p = false;
629 break;
630
631 case DATA_SEGMENT_END:
632 if (allocation_done != lang_first_phase_enum
633 && current_section == abs_output_section
634 && (exp_data_seg.phase == exp_dataseg_align_seen
635 || exp_data_seg.phase == exp_dataseg_adjust
636 || allocation_done != lang_allocating_phase_enum))
637 {
638 if (exp_data_seg.phase == exp_dataseg_align_seen)
639 {
640 exp_data_seg.phase = exp_dataseg_end_seen;
641 exp_data_seg.end = result.value;
642 }
643 }
644 else
645 result.valid_p = false;
646 break;
647
648 default:
649 FAIL ();
650 break;
651 }
652 }
653 break;
654
655 case etree_trinary:
656 result = exp_fold_tree (tree->trinary.cond, current_section,
657 allocation_done, dot, dotp);
658 if (result.valid_p)
659 result = exp_fold_tree ((result.value
660 ? tree->trinary.lhs
661 : tree->trinary.rhs),
662 current_section,
663 allocation_done, dot, dotp);
664 break;
665
666 case etree_binary:
667 result = fold_binary (tree, current_section, allocation_done,
668 dot, dotp);
669 break;
670
671 case etree_assign:
672 case etree_provide:
673 case etree_provided:
674 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
675 {
676 /* Assignment to dot can only be done during allocation. */
677 if (tree->type.node_class != etree_assign)
678 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
679 if (allocation_done == lang_allocating_phase_enum
680 || (allocation_done == lang_final_phase_enum
681 && current_section == abs_output_section))
682 {
683 result = exp_fold_tree (tree->assign.src,
684 current_section,
685 allocation_done, dot,
686 dotp);
687 if (! result.valid_p)
688 einfo (_("%F%S invalid assignment to location counter\n"));
689 else
690 {
691 if (current_section == NULL)
692 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
693 else
694 {
695 bfd_vma nextdot;
696
697 nextdot = (result.value
698 + current_section->bfd_section->vma);
699 if (nextdot < dot
700 && current_section != abs_output_section)
701 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
702 dot, nextdot);
703 else
704 *dotp = nextdot;
705 }
706 }
707 }
708 }
709 else
710 {
711 result = exp_fold_tree (tree->assign.src,
712 current_section, allocation_done,
713 dot, dotp);
714 if (result.valid_p)
715 {
716 boolean create;
717 struct bfd_link_hash_entry *h;
718
719 if (tree->type.node_class == etree_assign)
720 create = true;
721 else
722 create = false;
723 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
724 create, false, false);
725 if (h == (struct bfd_link_hash_entry *) NULL)
726 {
727 if (tree->type.node_class == etree_assign)
728 einfo (_("%P%F:%s: hash creation failed\n"),
729 tree->assign.dst);
730 }
731 else if (tree->type.node_class == etree_provide
732 && h->type != bfd_link_hash_undefined
733 && h->type != bfd_link_hash_common)
734 {
735 /* Do nothing. The symbol was defined by some
736 object. */
737 }
738 else
739 {
740 /* FIXME: Should we worry if the symbol is already
741 defined? */
742 h->type = bfd_link_hash_defined;
743 h->u.def.value = result.value;
744 h->u.def.section = result.section->bfd_section;
745 if (tree->type.node_class == etree_provide)
746 tree->type.node_class = etree_provided;
747 }
748 }
749 }
750 break;
751
752 case etree_name:
753 result = fold_name (tree, current_section, allocation_done, dot);
754 break;
755
756 default:
757 FAIL ();
758 break;
759 }
760
761 return result;
762 }
763
764 static etree_value_type
765 exp_fold_tree_no_dot (tree, current_section, allocation_done)
766 etree_type *tree;
767 lang_output_section_statement_type *current_section;
768 lang_phase_type allocation_done;
769 {
770 return exp_fold_tree (tree, current_section, allocation_done,
771 (bfd_vma) 0, (bfd_vma *) NULL);
772 }
773
774 etree_type *
775 exp_binop (code, lhs, rhs)
776 int code;
777 etree_type *lhs;
778 etree_type *rhs;
779 {
780 etree_type value, *new;
781 etree_value_type r;
782
783 value.type.node_code = code;
784 value.binary.lhs = lhs;
785 value.binary.rhs = rhs;
786 value.type.node_class = etree_binary;
787 r = exp_fold_tree_no_dot (&value,
788 abs_output_section,
789 lang_first_phase_enum);
790 if (r.valid_p)
791 {
792 return exp_intop (r.value);
793 }
794 new = (etree_type *) stat_alloc (sizeof (new->binary));
795 memcpy ((char *) new, (char *) &value, sizeof (new->binary));
796 return new;
797 }
798
799 etree_type *
800 exp_trinop (code, cond, lhs, rhs)
801 int code;
802 etree_type *cond;
803 etree_type *lhs;
804 etree_type *rhs;
805 {
806 etree_type value, *new;
807 etree_value_type r;
808 value.type.node_code = code;
809 value.trinary.lhs = lhs;
810 value.trinary.cond = cond;
811 value.trinary.rhs = rhs;
812 value.type.node_class = etree_trinary;
813 r = exp_fold_tree_no_dot (&value,
814 (lang_output_section_statement_type *) NULL,
815 lang_first_phase_enum);
816 if (r.valid_p)
817 return exp_intop (r.value);
818
819 new = (etree_type *) stat_alloc (sizeof (new->trinary));
820 memcpy ((char *) new, (char *) &value, sizeof (new->trinary));
821 return new;
822 }
823
824 etree_type *
825 exp_unop (code, child)
826 int code;
827 etree_type *child;
828 {
829 etree_type value, *new;
830
831 etree_value_type r;
832 value.unary.type.node_code = code;
833 value.unary.child = child;
834 value.unary.type.node_class = etree_unary;
835 r = exp_fold_tree_no_dot (&value, abs_output_section,
836 lang_first_phase_enum);
837 if (r.valid_p)
838 return exp_intop (r.value);
839
840 new = (etree_type *) stat_alloc (sizeof (new->unary));
841 memcpy ((char *) new, (char *) &value, sizeof (new->unary));
842 return new;
843 }
844
845 etree_type *
846 exp_nameop (code, name)
847 int code;
848 CONST char *name;
849 {
850 etree_type value, *new;
851 etree_value_type r;
852 value.name.type.node_code = code;
853 value.name.name = name;
854 value.name.type.node_class = etree_name;
855
856 r = exp_fold_tree_no_dot (&value,
857 (lang_output_section_statement_type *) NULL,
858 lang_first_phase_enum);
859 if (r.valid_p)
860 return exp_intop (r.value);
861
862 new = (etree_type *) stat_alloc (sizeof (new->name));
863 memcpy ((char *) new, (char *) &value, sizeof (new->name));
864 return new;
865
866 }
867
868 etree_type *
869 exp_assop (code, dst, src)
870 int code;
871 CONST char *dst;
872 etree_type *src;
873 {
874 etree_type value, *new;
875
876 value.assign.type.node_code = code;
877
878 value.assign.src = src;
879 value.assign.dst = dst;
880 value.assign.type.node_class = etree_assign;
881
882 #if 0
883 if (exp_fold_tree_no_dot (&value, &result))
884 return exp_intop (result);
885 #endif
886 new = (etree_type *) stat_alloc (sizeof (new->assign));
887 memcpy ((char *) new, (char *) &value, sizeof (new->assign));
888 return new;
889 }
890
891 /* Handle PROVIDE. */
892
893 etree_type *
894 exp_provide (dst, src)
895 const char *dst;
896 etree_type *src;
897 {
898 etree_type *n;
899
900 n = (etree_type *) stat_alloc (sizeof (n->assign));
901 n->assign.type.node_code = '=';
902 n->assign.type.node_class = etree_provide;
903 n->assign.src = src;
904 n->assign.dst = dst;
905 return n;
906 }
907
908 /* Handle ASSERT. */
909
910 etree_type *
911 exp_assert (exp, message)
912 etree_type *exp;
913 const char *message;
914 {
915 etree_type *n;
916
917 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
918 n->assert_s.type.node_code = '!';
919 n->assert_s.type.node_class = etree_assert;
920 n->assert_s.child = exp;
921 n->assert_s.message = message;
922 return n;
923 }
924
925 void
926 exp_print_tree (tree)
927 etree_type *tree;
928 {
929 if (config.map_file == NULL)
930 config.map_file = stderr;
931
932 if (tree == NULL)
933 {
934 minfo ("NULL TREE\n");
935 return;
936 }
937
938 switch (tree->type.node_class)
939 {
940 case etree_value:
941 minfo ("0x%v", tree->value.value);
942 return;
943 case etree_rel:
944 if (tree->rel.section->owner != NULL)
945 minfo ("%B:", tree->rel.section->owner);
946 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
947 return;
948 case etree_assign:
949 #if 0
950 if (tree->assign.dst->sdefs != (asymbol *) NULL)
951 fprintf (config.map_file, "%s (%x) ", tree->assign.dst->name,
952 tree->assign.dst->sdefs->value);
953 else
954 fprintf (config.map_file, "%s (UNDEFINED)", tree->assign.dst->name);
955 #endif
956 fprintf (config.map_file, "%s", tree->assign.dst);
957 exp_print_token (tree->type.node_code);
958 exp_print_tree (tree->assign.src);
959 break;
960 case etree_provide:
961 case etree_provided:
962 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
963 exp_print_tree (tree->assign.src);
964 fprintf (config.map_file, ")");
965 break;
966 case etree_binary:
967 fprintf (config.map_file, "(");
968 exp_print_tree (tree->binary.lhs);
969 exp_print_token (tree->type.node_code);
970 exp_print_tree (tree->binary.rhs);
971 fprintf (config.map_file, ")");
972 break;
973 case etree_trinary:
974 exp_print_tree (tree->trinary.cond);
975 fprintf (config.map_file, "?");
976 exp_print_tree (tree->trinary.lhs);
977 fprintf (config.map_file, ":");
978 exp_print_tree (tree->trinary.rhs);
979 break;
980 case etree_unary:
981 exp_print_token (tree->unary.type.node_code);
982 if (tree->unary.child)
983 {
984 fprintf (config.map_file, "(");
985 exp_print_tree (tree->unary.child);
986 fprintf (config.map_file, ")");
987 }
988 break;
989
990 case etree_assert:
991 fprintf (config.map_file, "ASSERT (");
992 exp_print_tree (tree->assert_s.child);
993 fprintf (config.map_file, ", %s)", tree->assert_s.message);
994 break;
995
996 case etree_undef:
997 fprintf (config.map_file, "????????");
998 break;
999 case etree_name:
1000 if (tree->type.node_code == NAME)
1001 {
1002 fprintf (config.map_file, "%s", tree->name.name);
1003 }
1004 else
1005 {
1006 exp_print_token (tree->type.node_code);
1007 if (tree->name.name)
1008 fprintf (config.map_file, "(%s)", tree->name.name);
1009 }
1010 break;
1011 default:
1012 FAIL ();
1013 break;
1014 }
1015 }
1016
1017 bfd_vma
1018 exp_get_vma (tree, def, name, allocation_done)
1019 etree_type *tree;
1020 bfd_vma def;
1021 char *name;
1022 lang_phase_type allocation_done;
1023 {
1024 etree_value_type r;
1025
1026 if (tree != NULL)
1027 {
1028 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1029 if (! r.valid_p && name != NULL)
1030 einfo (_("%F%S nonconstant expression for %s\n"), name);
1031 return r.value;
1032 }
1033 else
1034 return def;
1035 }
1036
1037 int
1038 exp_get_value_int (tree, def, name, allocation_done)
1039 etree_type *tree;
1040 int def;
1041 char *name;
1042 lang_phase_type allocation_done;
1043 {
1044 return (int) exp_get_vma (tree, (bfd_vma) def, name, allocation_done);
1045 }
1046
1047 fill_type *
1048 exp_get_fill (tree, def, name, allocation_done)
1049 etree_type *tree;
1050 fill_type *def;
1051 char *name;
1052 lang_phase_type allocation_done;
1053 {
1054 fill_type *fill;
1055 etree_value_type r;
1056 size_t len;
1057 unsigned int val;
1058
1059 if (tree == NULL)
1060 return def;
1061
1062 r = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1063 if (! r.valid_p && name != NULL)
1064 einfo (_("%F%S nonconstant expression for %s\n"), name);
1065
1066 if (r.str != NULL && (len = strlen (r.str)) != 0)
1067 {
1068 unsigned char *dst;
1069 unsigned char *s;
1070 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1071 fill->size = (len + 1) / 2;
1072 dst = fill->data;
1073 s = r.str;
1074 val = 0;
1075 do
1076 {
1077 unsigned int digit;
1078
1079 digit = *s++ - '0';
1080 if (digit > 9)
1081 digit = (digit - 'A' + '0' + 10) & 0xf;
1082 val <<= 4;
1083 val += digit;
1084 --len;
1085 if ((len & 1) == 0)
1086 {
1087 *dst++ = val;
1088 val = 0;
1089 }
1090 }
1091 while (len != 0);
1092 }
1093 else
1094 {
1095 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1096 val = r.value;
1097 fill->data[0] = (val >> 24) & 0xff;
1098 fill->data[1] = (val >> 16) & 0xff;
1099 fill->data[2] = (val >> 8) & 0xff;
1100 fill->data[3] = (val >> 0) & 0xff;
1101 fill->size = 4;
1102 }
1103 return fill;
1104 }
1105
1106 bfd_vma
1107 exp_get_abs_int (tree, def, name, allocation_done)
1108 etree_type *tree;
1109 int def ATTRIBUTE_UNUSED;
1110 char *name;
1111 lang_phase_type allocation_done;
1112 {
1113 etree_value_type res;
1114 res = exp_fold_tree_no_dot (tree, abs_output_section, allocation_done);
1115
1116 if (res.valid_p)
1117 res.value += res.section->bfd_section->vma;
1118 else
1119 einfo (_("%F%S non constant expression for %s\n"), name);
1120
1121 return res.value;
1122 }