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