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