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