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