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