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