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