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