* ldexp.h (etree_value_type): Use "asection *" in place of
[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,
20c2cbe3 3 2001, 2002, 2003, 2004, 2005
87f2a346 4 Free Software Foundation, Inc.
8c95a62e 5 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
252b5132 6
3ec57632 7 This file is part of GLD, the Gnu Linker.
252b5132 8
3ec57632
NC
9 GLD is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
252b5132 13
3ec57632
NC
14 GLD is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
252b5132 18
3ec57632
NC
19 You should have received a copy of the GNU General Public License
20 along with GLD; see the file COPYING. If not, write to the Free
75be928b
NC
21 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
22 02110-1301, USA. */
252b5132 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
75ff4589 44static etree_value_type exp_fold_tree_1
408082ec 45 (etree_type *, asection *, lang_phase_type, bfd_vma, bfd_vma *, bfd_boolean);
252b5132 46static etree_value_type exp_fold_tree_no_dot
408082ec 47 (etree_type *, asection *, lang_phase_type, bfd_boolean);
e5caa5e0
AM
48static bfd_vma align_n
49 (bfd_vma, bfd_vma);
252b5132 50
2d20f7bf
JJ
51struct exp_data_seg exp_data_seg;
52
ba916c8a
MM
53segment_type *segments;
54
fbbb9ac5
ZW
55/* Principally used for diagnostics. */
56static bfd_boolean assigning_to_dot = FALSE;
57
7b17f854 58/* Print the string representation of the given token. Surround it
b34976b6 59 with spaces if INFIX_P is TRUE. */
7b17f854 60
252b5132 61static void
1579bae1 62exp_print_token (token_code_type code, int infix_p)
252b5132 63{
4da711b1 64 static const struct
c7d701b0 65 {
8c95a62e 66 token_code_type code;
c7d701b0
NC
67 char * name;
68 }
69 table[] =
70 {
8c95a62e 71 { INT, "int" },
8c95a62e
KH
72 { NAME, "NAME" },
73 { PLUSEQ, "+=" },
74 { MINUSEQ, "-=" },
75 { MULTEQ, "*=" },
76 { DIVEQ, "/=" },
77 { LSHIFTEQ, "<<=" },
78 { RSHIFTEQ, ">>=" },
79 { ANDEQ, "&=" },
80 { OREQ, "|=" },
81 { OROR, "||" },
82 { ANDAND, "&&" },
83 { EQ, "==" },
84 { NE, "!=" },
85 { LE, "<=" },
86 { GE, ">=" },
87 { LSHIFT, "<<" },
7cecdbff 88 { RSHIFT, ">>" },
8c95a62e
KH
89 { ALIGN_K, "ALIGN" },
90 { BLOCK, "BLOCK" },
c7d701b0
NC
91 { QUAD, "QUAD" },
92 { SQUAD, "SQUAD" },
93 { LONG, "LONG" },
94 { SHORT, "SHORT" },
95 { BYTE, "BYTE" },
8c95a62e
KH
96 { SECTIONS, "SECTIONS" },
97 { SIZEOF_HEADERS, "SIZEOF_HEADERS" },
8c95a62e
KH
98 { MEMORY, "MEMORY" },
99 { DEFINED, "DEFINED" },
100 { TARGET_K, "TARGET" },
101 { SEARCH_DIR, "SEARCH_DIR" },
102 { MAP, "MAP" },
8c95a62e 103 { ENTRY, "ENTRY" },
c7d701b0
NC
104 { NEXT, "NEXT" },
105 { SIZEOF, "SIZEOF" },
106 { ADDR, "ADDR" },
107 { LOADADDR, "LOADADDR" },
108 { MAX_K, "MAX_K" },
1049f94e 109 { REL, "relocatable" },
2d20f7bf 110 { DATA_SEGMENT_ALIGN, "DATA_SEGMENT_ALIGN" },
8c37241b 111 { DATA_SEGMENT_RELRO_END, "DATA_SEGMENT_RELRO_END" },
ba916c8a 112 { DATA_SEGMENT_END, "DATA_SEGMENT_END" },
3ec57632
NC
113 { ORIGIN, "ORIGIN" },
114 { LENGTH, "LENGTH" },
ba916c8a 115 { SEGMENT_START, "SEGMENT_START" }
8c95a62e 116 };
252b5132
RH
117 unsigned int idx;
118
7b17f854
RS
119 for (idx = 0; idx < ARRAY_SIZE (table); idx++)
120 if (table[idx].code == code)
121 break;
c7d701b0 122
7b17f854
RS
123 if (infix_p)
124 fputc (' ', config.map_file);
125
126 if (idx < ARRAY_SIZE (table))
127 fputs (table[idx].name, config.map_file);
128 else if (code < 127)
129 fputc (code, config.map_file);
c7d701b0 130 else
7b17f854
RS
131 fprintf (config.map_file, "<code %d>", code);
132
133 if (infix_p)
134 fputc (' ', config.map_file);
252b5132
RH
135}
136
4de2d33d 137static void
1579bae1 138make_abs (etree_value_type *ptr)
252b5132 139{
408082ec
AM
140 ptr->value += ptr->section->vma;
141 ptr->section = bfd_abs_section_ptr;
252b5132
RH
142}
143
144static etree_value_type
1579bae1 145new_abs (bfd_vma value)
252b5132
RH
146{
147 etree_value_type new;
b34976b6 148 new.valid_p = TRUE;
408082ec 149 new.section = bfd_abs_section_ptr;
252b5132 150 new.value = value;
20c2cbe3 151 new.str = NULL;
252b5132
RH
152 return new;
153}
154
252b5132 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,
408082ec 193 asection *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
408082ec 204new_rel_from_section (bfd_vma value, asection *section)
252b5132
RH
205{
206 etree_value_type new;
b34976b6 207 new.valid_p = TRUE;
252b5132 208 new.value = value;
2c382fb6 209 new.str = NULL;
252b5132
RH
210 new.section = section;
211
408082ec 212 new.value -= section->vma;
252b5132
RH
213
214 return new;
215}
216
0ae1cf52 217static etree_value_type
1579bae1 218fold_unary (etree_type *tree,
408082ec 219 asection *current_section,
1579bae1
AM
220 lang_phase_type allocation_done,
221 bfd_vma dot,
75ff4589
L
222 bfd_vma *dotp,
223 bfd_boolean mark_used)
0ae1cf52
AM
224{
225 etree_value_type result;
226
75ff4589
L
227 result = exp_fold_tree_1 (tree->unary.child,
228 current_section,
229 allocation_done, dot, dotp, mark_used);
0ae1cf52
AM
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 {
408082ec
AM
245 result.value += result.section->vma;
246 result.section = bfd_abs_section_ptr;
0ae1cf52
AM
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
408082ec 280 && current_section == bfd_abs_section_ptr
0ae1cf52 281 && (exp_data_seg.phase == exp_dataseg_align_seen
8c37241b 282 || exp_data_seg.phase == exp_dataseg_relro_seen
0ae1cf52 283 || exp_data_seg.phase == exp_dataseg_adjust
8c37241b 284 || exp_data_seg.phase == exp_dataseg_relro_adjust
0ae1cf52
AM
285 || allocation_done != lang_allocating_phase_enum))
286 {
8c37241b
JJ
287 if (exp_data_seg.phase == exp_dataseg_align_seen
288 || exp_data_seg.phase == exp_dataseg_relro_seen)
0ae1cf52
AM
289 {
290 exp_data_seg.phase = exp_dataseg_end_seen;
291 exp_data_seg.end = result.value;
292 }
293 }
294 else
b34976b6 295 result.valid_p = FALSE;
0ae1cf52
AM
296 break;
297
298 default:
299 FAIL ();
300 break;
301 }
302 }
303
304 return result;
305}
306
4de2d33d 307static etree_value_type
1579bae1 308fold_binary (etree_type *tree,
408082ec 309 asection *current_section,
1579bae1
AM
310 lang_phase_type allocation_done,
311 bfd_vma dot,
75ff4589
L
312 bfd_vma *dotp,
313 bfd_boolean mark_used)
252b5132
RH
314{
315 etree_value_type result;
316
75ff4589
L
317 result = exp_fold_tree_1 (tree->binary.lhs, current_section,
318 allocation_done, dot, dotp, mark_used);
ba916c8a
MM
319
320 /* The SEGMENT_START operator is special because its first
321 operand is a string, not the name of a symbol. */
322 if (result.valid_p && tree->type.node_code == SEGMENT_START)
323 {
324 const char *segment_name;
325 segment_type *seg;
326 /* Check to see if the user has overridden the default
327 value. */
328 segment_name = tree->binary.rhs->name.name;
329 for (seg = segments; seg; seg = seg->next)
330 if (strcmp (seg->name, segment_name) == 0)
331 {
332 seg->used = TRUE;
333 result.value = seg->value;
334 result.str = NULL;
335 result.section = NULL;
336 break;
337 }
338 }
339 else if (result.valid_p)
252b5132
RH
340 {
341 etree_value_type other;
342
75ff4589
L
343 other = exp_fold_tree_1 (tree->binary.rhs,
344 current_section,
345 allocation_done,
346 dot, dotp, mark_used);
252b5132
RH
347 if (other.valid_p)
348 {
349 /* If the values are from different sections, or this is an
350 absolute expression, make both the source arguments
351 absolute. However, adding or subtracting an absolute
352 value from a relative value is meaningful, and is an
353 exception. */
408082ec
AM
354 if (current_section != bfd_abs_section_ptr
355 && (other.section == bfd_abs_section_ptr
356 || (result.section == bfd_abs_section_ptr
252b5132
RH
357 && tree->type.node_code == '+'))
358 && (tree->type.node_code == '+'
359 || tree->type.node_code == '-'))
360 {
408082ec 361 if (other.section != bfd_abs_section_ptr)
252b5132 362 {
0ae1cf52
AM
363 /* Keep the section of the other term. */
364 if (tree->type.node_code == '+')
365 other.value = result.value + other.value;
366 else
367 other.value = result.value - other.value;
368 return other;
252b5132
RH
369 }
370 }
371 else if (result.section != other.section
408082ec 372 || current_section == bfd_abs_section_ptr)
252b5132 373 {
8c95a62e
KH
374 make_abs (&result);
375 make_abs (&other);
252b5132
RH
376 }
377
4de2d33d 378 switch (tree->type.node_code)
252b5132
RH
379 {
380 case '%':
381 if (other.value == 0)
382 einfo (_("%F%S %% by zero\n"));
383 result.value = ((bfd_signed_vma) result.value
384 % (bfd_signed_vma) other.value);
385 break;
386
387 case '/':
388 if (other.value == 0)
389 einfo (_("%F%S / by zero\n"));
390 result.value = ((bfd_signed_vma) result.value
391 / (bfd_signed_vma) other.value);
392 break;
393
394#define BOP(x,y) case x : result.value = result.value y other.value; break;
8c95a62e
KH
395 BOP ('+', +);
396 BOP ('*', *);
397 BOP ('-', -);
398 BOP (LSHIFT, <<);
399 BOP (RSHIFT, >>);
400 BOP (EQ, ==);
401 BOP (NE, !=);
402 BOP ('<', <);
403 BOP ('>', >);
404 BOP (LE, <=);
405 BOP (GE, >=);
406 BOP ('&', &);
407 BOP ('^', ^);
408 BOP ('|', |);
409 BOP (ANDAND, &&);
410 BOP (OROR, ||);
252b5132
RH
411
412 case MAX_K:
413 if (result.value < other.value)
414 result = other;
415 break;
416
417 case MIN_K:
418 if (result.value > other.value)
419 result = other;
420 break;
421
876f4090
NS
422 case ALIGN_K:
423 result.value = align_n (result.value, other.value);
424 break;
c468c8bc 425
2d20f7bf
JJ
426 case DATA_SEGMENT_ALIGN:
427 if (allocation_done != lang_first_phase_enum
408082ec 428 && current_section == bfd_abs_section_ptr
2d20f7bf
JJ
429 && (exp_data_seg.phase == exp_dataseg_none
430 || exp_data_seg.phase == exp_dataseg_adjust
8c37241b 431 || exp_data_seg.phase == exp_dataseg_relro_adjust
2d20f7bf
JJ
432 || allocation_done != lang_allocating_phase_enum))
433 {
434 bfd_vma maxpage = result.value;
435
c553bb91 436 result.value = align_n (dot, maxpage);
8c37241b 437 if (exp_data_seg.phase == exp_dataseg_relro_adjust)
a4f5ad88 438 result.value = exp_data_seg.base;
8c37241b 439 else if (exp_data_seg.phase != exp_dataseg_adjust)
2d20f7bf
JJ
440 {
441 result.value += dot & (maxpage - 1);
442 if (allocation_done == lang_allocating_phase_enum)
443 {
444 exp_data_seg.phase = exp_dataseg_align_seen;
e3070fef 445 exp_data_seg.min_base = align_n (dot, maxpage);
2d20f7bf
JJ
446 exp_data_seg.base = result.value;
447 exp_data_seg.pagesize = other.value;
e3070fef 448 exp_data_seg.maxpagesize = maxpage;
8c37241b 449 exp_data_seg.relro_end = 0;
2d20f7bf
JJ
450 }
451 }
452 else if (other.value < maxpage)
50e60fb5
JJ
453 result.value += (dot + other.value - 1)
454 & (maxpage - other.value);
2d20f7bf
JJ
455 }
456 else
b34976b6 457 result.valid_p = FALSE;
2d20f7bf
JJ
458 break;
459
a4f5ad88
JJ
460 case DATA_SEGMENT_RELRO_END:
461 if (allocation_done != lang_first_phase_enum
462 && (exp_data_seg.phase == exp_dataseg_align_seen
463 || exp_data_seg.phase == exp_dataseg_adjust
464 || exp_data_seg.phase == exp_dataseg_relro_adjust
465 || allocation_done != lang_allocating_phase_enum))
466 {
467 if (exp_data_seg.phase == exp_dataseg_align_seen
468 || exp_data_seg.phase == exp_dataseg_relro_adjust)
469 exp_data_seg.relro_end
470 = result.value + other.value;
471 if (exp_data_seg.phase == exp_dataseg_relro_adjust
472 && (exp_data_seg.relro_end
473 & (exp_data_seg.pagesize - 1)))
474 {
475 exp_data_seg.relro_end += exp_data_seg.pagesize - 1;
476 exp_data_seg.relro_end &= ~(exp_data_seg.pagesize - 1);
477 result.value = exp_data_seg.relro_end - other.value;
478 }
479 if (exp_data_seg.phase == exp_dataseg_align_seen)
480 exp_data_seg.phase = exp_dataseg_relro_seen;
481 }
482 else
483 result.valid_p = FALSE;
484 break;
485
252b5132 486 default:
8c95a62e 487 FAIL ();
252b5132
RH
488 }
489 }
490 else
491 {
b34976b6 492 result.valid_p = FALSE;
252b5132
RH
493 }
494 }
495
496 return result;
497}
498
0ae1cf52 499static etree_value_type
1579bae1 500fold_trinary (etree_type *tree,
408082ec 501 asection *current_section,
1579bae1
AM
502 lang_phase_type allocation_done,
503 bfd_vma dot,
75ff4589
L
504 bfd_vma *dotp,
505 bfd_boolean mark_used)
0ae1cf52
AM
506{
507 etree_value_type result;
508
75ff4589
L
509 result = exp_fold_tree_1 (tree->trinary.cond, current_section,
510 allocation_done, dot, dotp, mark_used);
0ae1cf52 511 if (result.valid_p)
75ff4589
L
512 result = exp_fold_tree_1 ((result.value
513 ? tree->trinary.lhs
514 : tree->trinary.rhs),
515 current_section,
516 allocation_done,
517 dot, dotp, mark_used);
0ae1cf52
AM
518
519 return result;
520}
521
4de2d33d 522static etree_value_type
1579bae1 523fold_name (etree_type *tree,
408082ec 524 asection *current_section,
1579bae1 525 lang_phase_type allocation_done,
75ff4589
L
526 bfd_vma dot,
527 bfd_boolean mark_used)
252b5132
RH
528{
529 etree_value_type result;
c7d701b0 530
408082ec 531 memset (&result, 0, sizeof (result));
c468c8bc 532
4de2d33d 533 switch (tree->type.node_code)
8c95a62e
KH
534 {
535 case SIZEOF_HEADERS:
536 if (allocation_done != lang_first_phase_enum)
1b493742
NS
537 result = new_abs (bfd_sizeof_headers (output_bfd,
538 link_info.relocatable));
8c95a62e
KH
539 break;
540 case DEFINED:
541 if (allocation_done == lang_first_phase_enum)
1b493742 542 lang_track_definedness (tree->name.name);
8c95a62e
KH
543 else
544 {
545 struct bfd_link_hash_entry *h;
420e579c
HPN
546 int def_iteration
547 = lang_symbol_definition_iteration (tree->name.name);
8c95a62e
KH
548
549 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
550 tree->name.name,
b34976b6 551 FALSE, FALSE, TRUE);
1579bae1 552 result.value = (h != NULL
8c95a62e
KH
553 && (h->type == bfd_link_hash_defined
554 || h->type == bfd_link_hash_defweak
420e579c
HPN
555 || h->type == bfd_link_hash_common)
556 && (def_iteration == lang_statement_iteration
557 || def_iteration == -1));
408082ec 558 result.section = bfd_abs_section_ptr;
b34976b6 559 result.valid_p = TRUE;
8c95a62e
KH
560 }
561 break;
562 case NAME:
8c95a62e
KH
563 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
564 {
565 if (allocation_done != lang_first_phase_enum)
566 result = new_rel_from_section (dot, current_section);
8c95a62e
KH
567 }
568 else if (allocation_done != lang_first_phase_enum)
569 {
570 struct bfd_link_hash_entry *h;
571
572 h = bfd_wrapped_link_hash_lookup (output_bfd, &link_info,
573 tree->name.name,
1b493742
NS
574 TRUE, FALSE, TRUE);
575 if (!h)
576 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
577 else if (h->type == bfd_link_hash_defined
578 || h->type == bfd_link_hash_defweak)
8c95a62e
KH
579 {
580 if (bfd_is_abs_section (h->u.def.section))
581 result = new_abs (h->u.def.value);
582 else if (allocation_done == lang_final_phase_enum
583 || allocation_done == lang_allocating_phase_enum)
584 {
585 asection *output_section;
586
587 output_section = h->u.def.section->output_section;
588 if (output_section == NULL)
589 einfo (_("%X%S: unresolvable symbol `%s' referenced in expression\n"),
590 tree->name.name);
591 else
592 {
8c95a62e
KH
593 /* FIXME: Is this correct if this section is
594 being linked with -R? */
595 result = new_rel ((h->u.def.value
596 + h->u.def.section->output_offset),
2c382fb6 597 NULL,
408082ec
AM
598 output_section);
599 output_section->flags |= SEC_KEEP;
8c95a62e
KH
600 }
601 }
602 }
fbbb9ac5
ZW
603 else if (allocation_done == lang_final_phase_enum
604 || assigning_to_dot)
8c95a62e
KH
605 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
606 tree->name.name);
1b493742
NS
607 else if (h->type == bfd_link_hash_new)
608 {
609 h->type = bfd_link_hash_undefined;
610 h->u.undef.abfd = NULL;
3eda52aa 611 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
a010d60f 612 bfd_link_add_undef (link_info.hash, h);
1b493742 613 }
8c95a62e
KH
614 }
615 break;
616
617 case ADDR:
618 if (allocation_done != lang_first_phase_enum)
619 {
620 lang_output_section_statement_type *os;
621
622 os = lang_output_section_find (tree->name.name);
75ff4589
L
623 if (os)
624 {
625 os->bfd_section->flags |= SEC_KEEP;
626 if (os->processed > 0)
408082ec 627 result = new_rel (0, NULL, os->bfd_section);
75ff4589 628 }
8c95a62e 629 }
8c95a62e
KH
630 break;
631
632 case LOADADDR:
633 if (allocation_done != lang_first_phase_enum)
634 {
635 lang_output_section_statement_type *os;
636
637 os = lang_output_section_find (tree->name.name);
75ff4589 638 if (os)
1b493742 639 {
75ff4589
L
640 os->bfd_section->flags |= SEC_KEEP;
641 if (os->processed != 0)
642 {
643 if (os->load_base == NULL)
408082ec 644 result = new_rel (0, NULL, os->bfd_section);
75ff4589
L
645 else
646 result = exp_fold_tree_no_dot (os->load_base,
408082ec 647 bfd_abs_section_ptr,
75ff4589
L
648 allocation_done,
649 mark_used);
650 }
1b493742 651 }
8c95a62e 652 }
8c95a62e
KH
653 break;
654
655 case SIZEOF:
656 if (allocation_done != lang_first_phase_enum)
657 {
658 int opb = bfd_octets_per_byte (output_bfd);
659 lang_output_section_statement_type *os;
660
661 os = lang_output_section_find (tree->name.name);
75ff4589
L
662 if (os)
663 {
664 os->bfd_section->flags |= SEC_KEEP;
665 if (os->processed > 0)
666 result = new_abs (os->bfd_section->size / opb);
667 }
8c95a62e 668 }
8c95a62e
KH
669 break;
670
3ec57632
NC
671 case LENGTH:
672 {
673 lang_memory_region_type *mem;
674
675 mem = lang_memory_region_lookup (tree->name.name, FALSE);
676 if (mem != NULL)
677 result = new_abs (mem->length);
678 else
679 einfo (_("%F%S: undefined MEMORY region `%s' referenced in expression\n"),
680 tree->name.name);
681 }
682 break;
683
684 case ORIGIN:
685 {
686 lang_memory_region_type *mem;
687
688 mem = lang_memory_region_lookup (tree->name.name, FALSE);
689 if (mem != NULL)
690 result = new_abs (mem->origin);
691 else
692 einfo (_("%F%S: undefined MEMORY region `%s' referenced in expression\n"),
693 tree->name.name);
694 }
695 break;
696
8c95a62e
KH
697 default:
698 FAIL ();
699 break;
700 }
252b5132
RH
701
702 return result;
703}
8c95a62e 704
75ff4589
L
705static etree_value_type
706exp_fold_tree_1 (etree_type *tree,
408082ec 707 asection *current_section,
75ff4589
L
708 lang_phase_type allocation_done,
709 bfd_vma dot,
710 bfd_vma *dotp,
711 bfd_boolean mark_used)
252b5132
RH
712{
713 etree_value_type result;
714
715 if (tree == NULL)
716 {
8b3d8fa8 717 memset (&result, 0, sizeof (result));
252b5132
RH
718 return result;
719 }
720
4de2d33d 721 switch (tree->type.node_class)
252b5132
RH
722 {
723 case etree_value:
2c382fb6 724 result = new_rel (tree->value.value, tree->value.str, current_section);
252b5132
RH
725 break;
726
727 case etree_rel:
728 if (allocation_done != lang_final_phase_enum)
8b3d8fa8 729 memset (&result, 0, sizeof (result));
252b5132
RH
730 else
731 result = new_rel ((tree->rel.value
732 + tree->rel.section->output_section->vma
733 + tree->rel.section->output_offset),
2c382fb6 734 NULL,
252b5132
RH
735 current_section);
736 break;
737
738 case etree_assert:
75ff4589
L
739 result = exp_fold_tree_1 (tree->assert_s.child,
740 current_section,
741 allocation_done, dot, dotp,
742 mark_used);
743 if (result.valid_p)
744 {
745 if (mark_used)
746 /* We don't care if assert fails or not when we are just
747 marking if a section is used or not. */
748 result.value = 1;
749 else if (!result.value)
750 einfo ("%X%P: %s\n", tree->assert_s.message);
751 }
252b5132
RH
752 break;
753
754 case etree_unary:
0ae1cf52 755 result = fold_unary (tree, current_section, allocation_done,
75ff4589 756 dot, dotp, mark_used);
252b5132
RH
757 break;
758
759 case etree_binary:
760 result = fold_binary (tree, current_section, allocation_done,
75ff4589 761 dot, dotp, mark_used);
252b5132 762 break;
0ae1cf52
AM
763
764 case etree_trinary:
765 result = fold_trinary (tree, current_section, allocation_done,
75ff4589 766 dot, dotp, mark_used);
0ae1cf52 767 break;
252b5132
RH
768
769 case etree_assign:
770 case etree_provide:
b46a87b1 771 case etree_provided:
252b5132
RH
772 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
773 {
c7d701b0 774 /* Assignment to dot can only be done during allocation. */
b46a87b1 775 if (tree->type.node_class != etree_assign)
252b5132
RH
776 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
777 if (allocation_done == lang_allocating_phase_enum
778 || (allocation_done == lang_final_phase_enum
408082ec 779 && current_section == bfd_abs_section_ptr))
252b5132 780 {
fbbb9ac5
ZW
781 /* Notify the folder that this is an assignment to dot. */
782 assigning_to_dot = TRUE;
75ff4589
L
783 result = exp_fold_tree_1 (tree->assign.src,
784 current_section,
785 allocation_done,
786 dot, dotp, mark_used);
fbbb9ac5
ZW
787 assigning_to_dot = FALSE;
788
252b5132
RH
789 if (! result.valid_p)
790 einfo (_("%F%S invalid assignment to location counter\n"));
791 else
792 {
793 if (current_section == NULL)
794 einfo (_("%F%S assignment to location counter invalid outside of SECTION\n"));
795 else
796 {
797 bfd_vma nextdot;
798
408082ec 799 nextdot = result.value + current_section->vma;
252b5132 800 if (nextdot < dot
408082ec 801 && current_section != bfd_abs_section_ptr)
c7d701b0
NC
802 einfo (_("%F%S cannot move location counter backwards (from %V to %V)\n"),
803 dot, nextdot);
252b5132 804 else
4de2d33d 805 *dotp = nextdot;
252b5132
RH
806 }
807 }
808 }
8b3d8fa8
AM
809 else
810 memset (&result, 0, sizeof (result));
252b5132
RH
811 }
812 else
813 {
75ff4589
L
814 result = exp_fold_tree_1 (tree->assign.src,
815 current_section, allocation_done,
816 dot, dotp, mark_used);
252b5132
RH
817 if (result.valid_p)
818 {
b34976b6 819 bfd_boolean create;
252b5132
RH
820 struct bfd_link_hash_entry *h;
821
822 if (tree->type.node_class == etree_assign)
b34976b6 823 create = TRUE;
252b5132 824 else
b34976b6 825 create = FALSE;
252b5132 826 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1b493742 827 create, FALSE, TRUE);
1579bae1 828 if (h == NULL)
67010b46 829 {
1b493742 830 if (create)
67010b46
NC
831 einfo (_("%P%F:%s: hash creation failed\n"),
832 tree->assign.dst);
833 }
834 else if (tree->type.node_class == etree_provide
1b493742 835 && h->type != bfd_link_hash_new
67010b46
NC
836 && h->type != bfd_link_hash_undefined
837 && h->type != bfd_link_hash_common)
838 {
839 /* Do nothing. The symbol was defined by some
840 object. */
841 }
252b5132
RH
842 else
843 {
844 /* FIXME: Should we worry if the symbol is already
845 defined? */
420e579c 846 lang_update_definedness (tree->assign.dst, h);
252b5132
RH
847 h->type = bfd_link_hash_defined;
848 h->u.def.value = result.value;
408082ec 849 h->u.def.section = result.section;
b46a87b1
L
850 if (tree->type.node_class == etree_provide)
851 tree->type.node_class = etree_provided;
252b5132
RH
852 }
853 }
854 }
855 break;
856
857 case etree_name:
75ff4589
L
858 result = fold_name (tree, current_section, allocation_done, dot,
859 mark_used);
252b5132
RH
860 break;
861
862 default:
863 FAIL ();
8b3d8fa8 864 memset (&result, 0, sizeof (result));
252b5132
RH
865 break;
866 }
867
868 return result;
869}
870
75ff4589
L
871etree_value_type
872exp_fold_tree (etree_type *tree,
408082ec 873 asection *current_section,
75ff4589
L
874 lang_phase_type allocation_done,
875 bfd_vma dot,
876 bfd_vma *dotp)
877{
878 return exp_fold_tree_1 (tree, current_section, allocation_done,
879 dot, dotp, FALSE);
880}
881
4de2d33d 882static etree_value_type
1579bae1 883exp_fold_tree_no_dot (etree_type *tree,
408082ec 884 asection *current_section,
75ff4589
L
885 lang_phase_type allocation_done,
886 bfd_boolean mark_used)
252b5132 887{
75ff4589
L
888 return exp_fold_tree_1 (tree, current_section, allocation_done, 0,
889 NULL, mark_used);
252b5132
RH
890}
891
892etree_type *
1579bae1 893exp_binop (int code, etree_type *lhs, etree_type *rhs)
252b5132
RH
894{
895 etree_type value, *new;
896 etree_value_type r;
897
898 value.type.node_code = code;
899 value.binary.lhs = lhs;
900 value.binary.rhs = rhs;
901 value.type.node_class = etree_binary;
8c95a62e 902 r = exp_fold_tree_no_dot (&value,
408082ec 903 bfd_abs_section_ptr,
75ff4589 904 lang_first_phase_enum, FALSE);
252b5132
RH
905 if (r.valid_p)
906 {
8c95a62e 907 return exp_intop (r.value);
252b5132 908 }
1579bae1
AM
909 new = stat_alloc (sizeof (new->binary));
910 memcpy (new, &value, sizeof (new->binary));
252b5132
RH
911 return new;
912}
913
914etree_type *
1579bae1 915exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
252b5132
RH
916{
917 etree_type value, *new;
918 etree_value_type r;
919 value.type.node_code = code;
920 value.trinary.lhs = lhs;
921 value.trinary.cond = cond;
922 value.trinary.rhs = rhs;
923 value.type.node_class = etree_trinary;
75ff4589 924 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum, FALSE);
8c95a62e 925 if (r.valid_p)
c7d701b0
NC
926 return exp_intop (r.value);
927
1579bae1
AM
928 new = stat_alloc (sizeof (new->trinary));
929 memcpy (new, &value, sizeof (new->trinary));
252b5132
RH
930 return new;
931}
932
252b5132 933etree_type *
1579bae1 934exp_unop (int code, etree_type *child)
252b5132
RH
935{
936 etree_type value, *new;
937
938 etree_value_type r;
939 value.unary.type.node_code = code;
940 value.unary.child = child;
941 value.unary.type.node_class = etree_unary;
408082ec 942 r = exp_fold_tree_no_dot (&value, bfd_abs_section_ptr,
75ff4589 943 lang_first_phase_enum, FALSE);
8c95a62e 944 if (r.valid_p)
c7d701b0
NC
945 return exp_intop (r.value);
946
1579bae1
AM
947 new = stat_alloc (sizeof (new->unary));
948 memcpy (new, &value, sizeof (new->unary));
252b5132
RH
949 return new;
950}
951
252b5132 952etree_type *
1579bae1 953exp_nameop (int code, const char *name)
252b5132
RH
954{
955 etree_type value, *new;
956 etree_value_type r;
957 value.name.type.node_code = code;
958 value.name.name = name;
959 value.name.type.node_class = etree_name;
960
75ff4589 961 r = exp_fold_tree_no_dot (&value, NULL, lang_first_phase_enum, FALSE);
8c95a62e 962 if (r.valid_p)
c7d701b0
NC
963 return exp_intop (r.value);
964
1579bae1
AM
965 new = stat_alloc (sizeof (new->name));
966 memcpy (new, &value, sizeof (new->name));
252b5132
RH
967 return new;
968
969}
970
252b5132 971etree_type *
1579bae1 972exp_assop (int code, const char *dst, etree_type *src)
252b5132
RH
973{
974 etree_type value, *new;
975
976 value.assign.type.node_code = code;
977
252b5132
RH
978 value.assign.src = src;
979 value.assign.dst = dst;
980 value.assign.type.node_class = etree_assign;
981
1579bae1
AM
982 new = stat_alloc (sizeof (new->assign));
983 memcpy (new, &value, sizeof (new->assign));
252b5132
RH
984 return new;
985}
986
987/* Handle PROVIDE. */
988
989etree_type *
1579bae1 990exp_provide (const char *dst, etree_type *src)
252b5132
RH
991{
992 etree_type *n;
993
1579bae1 994 n = stat_alloc (sizeof (n->assign));
252b5132
RH
995 n->assign.type.node_code = '=';
996 n->assign.type.node_class = etree_provide;
997 n->assign.src = src;
998 n->assign.dst = dst;
999 return n;
1000}
1001
1002/* Handle ASSERT. */
1003
1004etree_type *
1579bae1 1005exp_assert (etree_type *exp, const char *message)
252b5132
RH
1006{
1007 etree_type *n;
1008
1579bae1 1009 n = stat_alloc (sizeof (n->assert_s));
252b5132
RH
1010 n->assert_s.type.node_code = '!';
1011 n->assert_s.type.node_class = etree_assert;
1012 n->assert_s.child = exp;
1013 n->assert_s.message = message;
1014 return n;
1015}
1016
4de2d33d 1017void
1579bae1 1018exp_print_tree (etree_type *tree)
252b5132 1019{
c7d701b0
NC
1020 if (config.map_file == NULL)
1021 config.map_file = stderr;
b7a26f91 1022
c7d701b0
NC
1023 if (tree == NULL)
1024 {
1025 minfo ("NULL TREE\n");
1026 return;
1027 }
b7a26f91 1028
8c95a62e
KH
1029 switch (tree->type.node_class)
1030 {
1031 case etree_value:
1032 minfo ("0x%v", tree->value.value);
1033 return;
1034 case etree_rel:
1035 if (tree->rel.section->owner != NULL)
1036 minfo ("%B:", tree->rel.section->owner);
1037 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1038 return;
1039 case etree_assign:
8c95a62e 1040 fprintf (config.map_file, "%s", tree->assign.dst);
b34976b6 1041 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1042 exp_print_tree (tree->assign.src);
1043 break;
1044 case etree_provide:
b46a87b1 1045 case etree_provided:
8c95a62e
KH
1046 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1047 exp_print_tree (tree->assign.src);
1048 fprintf (config.map_file, ")");
1049 break;
1050 case etree_binary:
1051 fprintf (config.map_file, "(");
1052 exp_print_tree (tree->binary.lhs);
b34976b6 1053 exp_print_token (tree->type.node_code, TRUE);
8c95a62e
KH
1054 exp_print_tree (tree->binary.rhs);
1055 fprintf (config.map_file, ")");
1056 break;
1057 case etree_trinary:
1058 exp_print_tree (tree->trinary.cond);
1059 fprintf (config.map_file, "?");
1060 exp_print_tree (tree->trinary.lhs);
1061 fprintf (config.map_file, ":");
1062 exp_print_tree (tree->trinary.rhs);
1063 break;
1064 case etree_unary:
b34976b6 1065 exp_print_token (tree->unary.type.node_code, FALSE);
8c95a62e
KH
1066 if (tree->unary.child)
1067 {
7b17f854 1068 fprintf (config.map_file, " (");
8c95a62e
KH
1069 exp_print_tree (tree->unary.child);
1070 fprintf (config.map_file, ")");
1071 }
1072 break;
1073
1074 case etree_assert:
1075 fprintf (config.map_file, "ASSERT (");
1076 exp_print_tree (tree->assert_s.child);
1077 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1078 break;
1079
1080 case etree_undef:
1081 fprintf (config.map_file, "????????");
1082 break;
1083 case etree_name:
1084 if (tree->type.node_code == NAME)
1085 {
1086 fprintf (config.map_file, "%s", tree->name.name);
1087 }
1088 else
1089 {
b34976b6 1090 exp_print_token (tree->type.node_code, FALSE);
8c95a62e 1091 if (tree->name.name)
7b17f854 1092 fprintf (config.map_file, " (%s)", tree->name.name);
8c95a62e
KH
1093 }
1094 break;
1095 default:
1096 FAIL ();
1097 break;
252b5132 1098 }
252b5132
RH
1099}
1100
1101bfd_vma
1579bae1
AM
1102exp_get_vma (etree_type *tree,
1103 bfd_vma def,
1104 char *name,
1105 lang_phase_type allocation_done)
252b5132
RH
1106{
1107 etree_value_type r;
1108
1109 if (tree != NULL)
1110 {
408082ec 1111 r = exp_fold_tree_no_dot (tree, bfd_abs_section_ptr,
75ff4589 1112 allocation_done, FALSE);
252b5132
RH
1113 if (! r.valid_p && name != NULL)
1114 einfo (_("%F%S nonconstant expression for %s\n"), name);
1115 return r.value;
1116 }
1117 else
1118 return def;
1119}
1120
4de2d33d 1121int
1579bae1
AM
1122exp_get_value_int (etree_type *tree,
1123 int def,
1124 char *name,
1125 lang_phase_type allocation_done)
252b5132 1126{
1579bae1 1127 return exp_get_vma (tree, def, name, allocation_done);
252b5132
RH
1128}
1129
2c382fb6 1130fill_type *
1579bae1
AM
1131exp_get_fill (etree_type *tree,
1132 fill_type *def,
1133 char *name,
1134 lang_phase_type allocation_done)
2c382fb6
AM
1135{
1136 fill_type *fill;
1137 etree_value_type r;
1138 size_t len;
1139 unsigned int val;
1140
1141 if (tree == NULL)
1142 return def;
1143
408082ec 1144 r = exp_fold_tree_no_dot (tree, bfd_abs_section_ptr, allocation_done,
75ff4589 1145 FALSE);
2c382fb6
AM
1146 if (! r.valid_p && name != NULL)
1147 einfo (_("%F%S nonconstant expression for %s\n"), name);
1148
1149 if (r.str != NULL && (len = strlen (r.str)) != 0)
1150 {
1151 unsigned char *dst;
1152 unsigned char *s;
1579bae1 1153 fill = xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
2c382fb6
AM
1154 fill->size = (len + 1) / 2;
1155 dst = fill->data;
5718918d 1156 s = (unsigned char *) r.str;
2c382fb6
AM
1157 val = 0;
1158 do
1159 {
1160 unsigned int digit;
1161
1162 digit = *s++ - '0';
1163 if (digit > 9)
1164 digit = (digit - 'A' + '0' + 10) & 0xf;
1165 val <<= 4;
1166 val += digit;
1167 --len;
1168 if ((len & 1) == 0)
1169 {
1170 *dst++ = val;
1171 val = 0;
1172 }
1173 }
1174 while (len != 0);
1175 }
1176 else
1177 {
1579bae1 1178 fill = xmalloc (4 + sizeof (*fill) - 1);
2c382fb6
AM
1179 val = r.value;
1180 fill->data[0] = (val >> 24) & 0xff;
1181 fill->data[1] = (val >> 16) & 0xff;
1182 fill->data[2] = (val >> 8) & 0xff;
1183 fill->data[3] = (val >> 0) & 0xff;
1184 fill->size = 4;
1185 }
1186 return fill;
1187}
1188
252b5132 1189bfd_vma
1579bae1
AM
1190exp_get_abs_int (etree_type *tree,
1191 int def ATTRIBUTE_UNUSED,
1192 char *name,
1193 lang_phase_type allocation_done)
252b5132
RH
1194{
1195 etree_value_type res;
408082ec 1196 res = exp_fold_tree_no_dot (tree, bfd_abs_section_ptr, allocation_done,
75ff4589 1197 FALSE);
252b5132
RH
1198
1199 if (res.valid_p)
408082ec 1200 res.value += res.section->vma;
8c95a62e 1201 else
c7d701b0
NC
1202 einfo (_("%F%S non constant expression for %s\n"), name);
1203
252b5132
RH
1204 return res.value;
1205}
c553bb91 1206
e5caa5e0
AM
1207static bfd_vma
1208align_n (bfd_vma value, bfd_vma align)
c553bb91
AM
1209{
1210 if (align <= 1)
1211 return value;
1212
1213 value = (value + align - 1) / align;
1214 return value * align;
1215}
75ff4589
L
1216
1217void
408082ec 1218exp_mark_used_section (etree_type *tree, asection *current_section)
75ff4589
L
1219{
1220 switch (tree->type.node_class)
1221 {
1222 case etree_value:
1223 break;
1224
1225 case etree_rel:
1226 break;
1227
1228 case etree_assert:
1229 break;
1230
1231 case etree_unary:
1232 break;
1233
1234 case etree_binary:
1235 break;
1236
1237 case etree_trinary:
1238 break;
1239
1240 case etree_assign:
1241 case etree_provide:
1242 case etree_provided:
1243 if (tree->assign.dst[0] != '.' || tree->assign.dst[1] != 0)
1244 {
1245 etree_value_type result;
1246 bfd_vma dot = 0;
1247
1248 result = exp_fold_tree_1 (tree->assign.src,
1249 current_section,
1250 lang_allocating_phase_enum,
1251 dot, &dot, TRUE);
1252 if (result.valid_p)
1253 {
1254 bfd_boolean create;
1255 struct bfd_link_hash_entry *h;
1256
1257 if (tree->type.node_class == etree_assign)
1258 create = TRUE;
1259 else
1260 create = FALSE;
1261 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
1262 create, FALSE, TRUE);
1263 if (h == NULL)
1264 {
1265 if (create)
1266 einfo (_("%P%F:%s: hash creation failed\n"),
1267 tree->assign.dst);
1268 }
1269 else if (tree->type.node_class == etree_provide
1270 && h->type != bfd_link_hash_new
1271 && h->type != bfd_link_hash_undefined
1272 && h->type != bfd_link_hash_common)
1273 {
1274 /* Do nothing. The symbol was defined by some
1275 object. */
1276 }
1277 else
1278 {
1279 /* FIXME: Should we worry if the symbol is already
1280 defined? */
1281 lang_update_definedness (tree->assign.dst, h);
1282 h->type = bfd_link_hash_defined;
1283 h->u.def.value = result.value;
408082ec 1284 h->u.def.section = result.section;
75ff4589
L
1285 if (tree->type.node_class == etree_provide)
1286 tree->type.node_class = etree_provided;
1287 }
1288 }
1289 }
1290 break;
1291
1292 case etree_name:
1293 fold_name (tree, current_section, lang_allocating_phase_enum, 0,
1294 TRUE);
1295 break;
1296
1297 default:
1298 abort ();
1299 break;
1300 }
1301}
This page took 0.351444 seconds and 4 git commands to generate.