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