]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/ldexp.c
PR ld/12380
[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
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 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.lineno = lineno;
160 new_e->value.value = value;
161 new_e->value.str = NULL;
162 new_e->type.node_class = etree_value;
163 return new_e;
164 }
165
166 etree_type *
167 exp_bigintop (bfd_vma value, char *str)
168 {
169 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->value));
170 new_e->type.node_code = INT;
171 new_e->type.lineno = lineno;
172 new_e->value.value = value;
173 new_e->value.str = str;
174 new_e->type.node_class = etree_value;
175 return new_e;
176 }
177
178 /* Build an expression representing an unnamed relocatable value. */
179
180 etree_type *
181 exp_relop (asection *section, bfd_vma value)
182 {
183 etree_type *new_e = (etree_type *) stat_alloc (sizeof (new_e->rel));
184 new_e->type.node_code = REL;
185 new_e->type.lineno = lineno;
186 new_e->type.node_class = etree_rel;
187 new_e->rel.section = section;
188 new_e->rel.value = value;
189 return new_e;
190 }
191
192 static void
193 new_number (bfd_vma value)
194 {
195 expld.result.valid_p = TRUE;
196 expld.result.value = value;
197 expld.result.str = NULL;
198 expld.result.section = NULL;
199 }
200
201 static void
202 new_rel (bfd_vma value, asection *section)
203 {
204 expld.result.valid_p = TRUE;
205 expld.result.value = value;
206 expld.result.str = NULL;
207 expld.result.section = section;
208 }
209
210 static void
211 new_rel_from_abs (bfd_vma value)
212 {
213 expld.result.valid_p = TRUE;
214 expld.result.value = value - expld.section->vma;
215 expld.result.str = NULL;
216 expld.result.section = expld.section;
217 }
218
219 static void
220 fold_unary (etree_type *tree)
221 {
222 exp_fold_tree_1 (tree->unary.child);
223 if (expld.result.valid_p)
224 {
225 switch (tree->type.node_code)
226 {
227 case ALIGN_K:
228 if (expld.phase != lang_first_phase_enum)
229 new_rel_from_abs (align_n (expld.dot, expld.result.value));
230 else
231 expld.result.valid_p = FALSE;
232 break;
233
234 case ABSOLUTE:
235 make_abs ();
236 break;
237
238 case '~':
239 expld.result.value = ~expld.result.value;
240 break;
241
242 case '!':
243 expld.result.value = !expld.result.value;
244 break;
245
246 case '-':
247 expld.result.value = -expld.result.value;
248 break;
249
250 case NEXT:
251 /* Return next place aligned to value. */
252 if (expld.phase != lang_first_phase_enum)
253 {
254 make_abs ();
255 expld.result.value = align_n (expld.dot, expld.result.value);
256 }
257 else
258 expld.result.valid_p = FALSE;
259 break;
260
261 case DATA_SEGMENT_END:
262 if (expld.phase == lang_first_phase_enum
263 || expld.section != bfd_abs_section_ptr)
264 {
265 expld.result.valid_p = FALSE;
266 }
267 else if (expld.dataseg.phase == exp_dataseg_align_seen
268 || expld.dataseg.phase == exp_dataseg_relro_seen)
269 {
270 expld.dataseg.phase = exp_dataseg_end_seen;
271 expld.dataseg.end = expld.result.value;
272 }
273 else if (expld.dataseg.phase == exp_dataseg_done
274 || expld.dataseg.phase == exp_dataseg_adjust
275 || expld.dataseg.phase == exp_dataseg_relro_adjust)
276 {
277 /* OK. */
278 }
279 else
280 expld.result.valid_p = FALSE;
281 break;
282
283 default:
284 FAIL ();
285 break;
286 }
287 }
288 }
289
290 static void
291 fold_binary (etree_type *tree)
292 {
293 etree_value_type lhs;
294 exp_fold_tree_1 (tree->binary.lhs);
295
296 /* The SEGMENT_START operator is special because its first
297 operand is a string, not the name of a symbol. Note that the
298 operands have been swapped, so binary.lhs is second (default)
299 operand, binary.rhs is first operand. */
300 if (expld.result.valid_p && tree->type.node_code == SEGMENT_START)
301 {
302 const char *segment_name;
303 segment_type *seg;
304
305 /* Check to see if the user has overridden the default
306 value. */
307 segment_name = tree->binary.rhs->name.name;
308 for (seg = segments; seg; seg = seg->next)
309 if (strcmp (seg->name, segment_name) == 0)
310 {
311 if (!seg->used
312 && config.magic_demand_paged
313 && (seg->value % config.maxpagesize) != 0)
314 einfo (_("%P: warning: address of `%s' isn't multiple of maximum page size\n"),
315 segment_name);
316 seg->used = TRUE;
317 new_rel_from_abs (seg->value);
318 break;
319 }
320 return;
321 }
322
323 lhs = expld.result;
324 exp_fold_tree_1 (tree->binary.rhs);
325 expld.result.valid_p &= lhs.valid_p;
326
327 if (expld.result.valid_p)
328 {
329 if (lhs.section != expld.result.section)
330 {
331 /* If the values are from different sections, and neither is
332 just a number, make both the source arguments absolute. */
333 if (expld.result.section != NULL
334 && lhs.section != NULL)
335 {
336 make_abs ();
337 lhs.value += lhs.section->vma;
338 }
339
340 /* If the rhs is just a number, keep the lhs section. */
341 else if (expld.result.section == NULL)
342 expld.result.section = lhs.section;
343 }
344
345 switch (tree->type.node_code)
346 {
347 case '%':
348 if (expld.result.value != 0)
349 expld.result.value = ((bfd_signed_vma) lhs.value
350 % (bfd_signed_vma) expld.result.value);
351 else if (expld.phase != lang_mark_phase_enum)
352 einfo (_("%F%S %% by zero\n"));
353 break;
354
355 case '/':
356 if (expld.result.value != 0)
357 expld.result.value = ((bfd_signed_vma) lhs.value
358 / (bfd_signed_vma) expld.result.value);
359 else if (expld.phase != lang_mark_phase_enum)
360 einfo (_("%F%S / by zero\n"));
361 break;
362
363 #define BOP(x, y) \
364 case x: \
365 expld.result.value = lhs.value y expld.result.value; \
366 break;
367
368 #define BOPN(x, y) \
369 case x: \
370 expld.result.value = lhs.value y expld.result.value; \
371 expld.result.section = NULL; \
372 break;
373
374 BOP ('+', +);
375 BOP ('*', *);
376 BOP ('-', -);
377 BOP (LSHIFT, <<);
378 BOP (RSHIFT, >>);
379 BOP ('&', &);
380 BOP ('^', ^);
381 BOP ('|', |);
382 BOPN (EQ, ==);
383 BOPN (NE, !=);
384 BOPN ('<', <);
385 BOPN ('>', >);
386 BOPN (LE, <=);
387 BOPN (GE, >=);
388 BOPN (ANDAND, &&);
389 BOPN (OROR, ||);
390
391 case MAX_K:
392 if (lhs.value > expld.result.value)
393 expld.result.value = lhs.value;
394 break;
395
396 case MIN_K:
397 if (lhs.value < expld.result.value)
398 expld.result.value = lhs.value;
399 break;
400
401 case ALIGN_K:
402 expld.result.value = align_n (lhs.value, expld.result.value);
403 break;
404
405 case DATA_SEGMENT_ALIGN:
406 expld.dataseg.relro = exp_dataseg_relro_start;
407 if (expld.phase == lang_first_phase_enum
408 || expld.section != bfd_abs_section_ptr)
409 expld.result.valid_p = FALSE;
410 else
411 {
412 bfd_vma maxpage = lhs.value;
413 bfd_vma commonpage = expld.result.value;
414
415 expld.result.value = align_n (expld.dot, maxpage);
416 if (expld.dataseg.phase == exp_dataseg_relro_adjust)
417 expld.result.value = expld.dataseg.base;
418 else if (expld.dataseg.phase == exp_dataseg_adjust)
419 {
420 if (commonpage < maxpage)
421 expld.result.value += ((expld.dot + commonpage - 1)
422 & (maxpage - commonpage));
423 }
424 else
425 {
426 expld.result.value += expld.dot & (maxpage - 1);
427 if (expld.dataseg.phase == exp_dataseg_done)
428 {
429 /* OK. */
430 }
431 else if (expld.dataseg.phase == exp_dataseg_none)
432 {
433 expld.dataseg.phase = exp_dataseg_align_seen;
434 expld.dataseg.min_base = expld.dot;
435 expld.dataseg.base = expld.result.value;
436 expld.dataseg.pagesize = commonpage;
437 expld.dataseg.maxpagesize = maxpage;
438 expld.dataseg.relro_end = 0;
439 }
440 else
441 expld.result.valid_p = FALSE;
442 }
443 }
444 break;
445
446 case DATA_SEGMENT_RELRO_END:
447 expld.dataseg.relro = exp_dataseg_relro_end;
448 if (expld.phase == lang_first_phase_enum
449 || expld.section != bfd_abs_section_ptr)
450 expld.result.valid_p = FALSE;
451 else if (expld.dataseg.phase == exp_dataseg_align_seen
452 || expld.dataseg.phase == exp_dataseg_adjust
453 || expld.dataseg.phase == exp_dataseg_relro_adjust
454 || expld.dataseg.phase == exp_dataseg_done)
455 {
456 if (expld.dataseg.phase == exp_dataseg_align_seen
457 || expld.dataseg.phase == exp_dataseg_relro_adjust)
458 expld.dataseg.relro_end = lhs.value + expld.result.value;
459
460 if (expld.dataseg.phase == exp_dataseg_relro_adjust
461 && (expld.dataseg.relro_end
462 & (expld.dataseg.pagesize - 1)))
463 {
464 expld.dataseg.relro_end += expld.dataseg.pagesize - 1;
465 expld.dataseg.relro_end &= ~(expld.dataseg.pagesize - 1);
466 expld.result.value = (expld.dataseg.relro_end
467 - expld.result.value);
468 }
469 else
470 expld.result.value = lhs.value;
471
472 if (expld.dataseg.phase == exp_dataseg_align_seen)
473 expld.dataseg.phase = exp_dataseg_relro_seen;
474 }
475 else
476 expld.result.valid_p = FALSE;
477 break;
478
479 default:
480 FAIL ();
481 }
482 }
483 }
484
485 static void
486 fold_trinary (etree_type *tree)
487 {
488 exp_fold_tree_1 (tree->trinary.cond);
489 if (expld.result.valid_p)
490 exp_fold_tree_1 (expld.result.value
491 ? tree->trinary.lhs
492 : tree->trinary.rhs);
493 }
494
495 static void
496 fold_name (etree_type *tree)
497 {
498 memset (&expld.result, 0, sizeof (expld.result));
499
500 switch (tree->type.node_code)
501 {
502 case SIZEOF_HEADERS:
503 if (expld.phase != lang_first_phase_enum)
504 {
505 bfd_vma hdr_size = 0;
506 /* Don't find the real header size if only marking sections;
507 The bfd function may cache incorrect data. */
508 if (expld.phase != lang_mark_phase_enum)
509 hdr_size = bfd_sizeof_headers (link_info.output_bfd, &link_info);
510 new_number (hdr_size);
511 }
512 break;
513
514 case DEFINED:
515 expld.uses_defined = TRUE;
516 if (expld.phase == lang_first_phase_enum)
517 lang_track_definedness (tree->name.name);
518 else
519 {
520 struct bfd_link_hash_entry *h;
521 int def_iteration
522 = lang_symbol_definition_iteration (tree->name.name);
523
524 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
525 &link_info,
526 tree->name.name,
527 FALSE, FALSE, TRUE);
528 new_number (h != NULL
529 && (h->type == bfd_link_hash_defined
530 || h->type == bfd_link_hash_defweak
531 || h->type == bfd_link_hash_common)
532 && (def_iteration == lang_statement_iteration
533 || def_iteration == -1));
534 }
535 break;
536
537 case NAME:
538 if (expld.phase == lang_first_phase_enum)
539 ;
540 else if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
541 new_rel_from_abs (expld.dot);
542 else
543 {
544 struct bfd_link_hash_entry *h;
545
546 h = bfd_wrapped_link_hash_lookup (link_info.output_bfd,
547 &link_info,
548 tree->name.name,
549 TRUE, FALSE, TRUE);
550 if (!h)
551 einfo (_("%P%F: bfd_link_hash_lookup failed: %E\n"));
552 else if (h->type == bfd_link_hash_defined
553 || h->type == bfd_link_hash_defweak)
554 {
555 asection *output_section;
556
557 output_section = h->u.def.section->output_section;
558 if (output_section == NULL)
559 {
560 if (expld.phase != lang_mark_phase_enum)
561 einfo (_("%X%S: unresolvable symbol `%s'"
562 " referenced in expression\n"),
563 tree->name.name);
564 }
565 else if (output_section == bfd_abs_section_ptr
566 && (expld.section != bfd_abs_section_ptr
567 || ld_compatibility >= 221))
568 new_number (h->u.def.value + h->u.def.section->output_offset);
569 else
570 new_rel (h->u.def.value + h->u.def.section->output_offset,
571 output_section);
572 }
573 else if (expld.phase == lang_final_phase_enum
574 || expld.assigning_to_dot)
575 einfo (_("%F%S: undefined symbol `%s' referenced in expression\n"),
576 tree->name.name);
577 else if (h->type == bfd_link_hash_new)
578 {
579 h->type = bfd_link_hash_undefined;
580 h->u.undef.abfd = NULL;
581 if (h->u.undef.next == NULL && h != link_info.hash->undefs_tail)
582 bfd_link_add_undef (link_info.hash, h);
583 }
584 }
585 break;
586
587 case ADDR:
588 if (expld.phase != lang_first_phase_enum)
589 {
590 lang_output_section_statement_type *os;
591
592 os = lang_output_section_find (tree->name.name);
593 if (os == NULL)
594 {
595 if (expld.phase == lang_final_phase_enum)
596 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
597 tree->name.name);
598 }
599 else if (os->processed_vma)
600 new_rel (0, os->bfd_section);
601 }
602 break;
603
604 case LOADADDR:
605 if (expld.phase != lang_first_phase_enum)
606 {
607 lang_output_section_statement_type *os;
608
609 os = lang_output_section_find (tree->name.name);
610 if (os == NULL)
611 {
612 if (expld.phase == lang_final_phase_enum)
613 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
614 tree->name.name);
615 }
616 else if (os->processed_lma)
617 {
618 if (os->load_base == NULL)
619 new_abs (os->bfd_section->lma);
620 else
621 {
622 exp_fold_tree_1 (os->load_base);
623 if (expld.result.valid_p)
624 make_abs ();
625 }
626 }
627 }
628 break;
629
630 case SIZEOF:
631 case ALIGNOF:
632 if (expld.phase != lang_first_phase_enum)
633 {
634 lang_output_section_statement_type *os;
635
636 os = lang_output_section_find (tree->name.name);
637 if (os == NULL)
638 {
639 if (expld.phase == lang_final_phase_enum)
640 einfo (_("%F%S: undefined section `%s' referenced in expression\n"),
641 tree->name.name);
642 new_number (0);
643 }
644 else if (os->processed_vma)
645 {
646 bfd_vma val;
647
648 if (tree->type.node_code == SIZEOF)
649 val = (os->bfd_section->size
650 / bfd_octets_per_byte (link_info.output_bfd));
651 else
652 val = (bfd_vma)1 << os->bfd_section->alignment_power;
653
654 new_number (val);
655 }
656 }
657 break;
658
659 case LENGTH:
660 {
661 lang_memory_region_type *mem;
662
663 mem = lang_memory_region_lookup (tree->name.name, FALSE);
664 if (mem != NULL)
665 new_number (mem->length);
666 else
667 einfo (_("%F%S: undefined MEMORY region `%s'"
668 " referenced in expression\n"), tree->name.name);
669 }
670 break;
671
672 case ORIGIN:
673 if (expld.phase != lang_first_phase_enum)
674 {
675 lang_memory_region_type *mem;
676
677 mem = lang_memory_region_lookup (tree->name.name, FALSE);
678 if (mem != NULL)
679 new_rel_from_abs (mem->origin);
680 else
681 einfo (_("%F%S: undefined MEMORY region `%s'"
682 " referenced in expression\n"), tree->name.name);
683 }
684 break;
685
686 case CONSTANT:
687 if (strcmp (tree->name.name, "MAXPAGESIZE") == 0)
688 new_number (config.maxpagesize);
689 else if (strcmp (tree->name.name, "COMMONPAGESIZE") == 0)
690 new_number (config.commonpagesize);
691 else
692 einfo (_("%F%S: unknown constant `%s' referenced in expression\n"),
693 tree->name.name);
694 break;
695
696 default:
697 FAIL ();
698 break;
699 }
700 }
701
702 static void
703 exp_fold_tree_1 (etree_type *tree)
704 {
705 if (tree == NULL)
706 {
707 memset (&expld.result, 0, sizeof (expld.result));
708 return;
709 }
710
711 switch (tree->type.node_class)
712 {
713 case etree_value:
714 if (expld.section == bfd_abs_section_ptr
715 && ld_compatibility < 221)
716 new_abs (tree->value.value);
717 else
718 new_number (tree->value.value);
719 expld.result.str = tree->value.str;
720 break;
721
722 case etree_rel:
723 if (expld.phase != lang_first_phase_enum)
724 {
725 asection *output_section = tree->rel.section->output_section;
726 new_rel (tree->rel.value + tree->rel.section->output_offset,
727 output_section);
728 }
729 else
730 memset (&expld.result, 0, sizeof (expld.result));
731 break;
732
733 case etree_assert:
734 exp_fold_tree_1 (tree->assert_s.child);
735 if (expld.phase == lang_final_phase_enum && !expld.result.value)
736 einfo ("%X%P: %s\n", tree->assert_s.message);
737 break;
738
739 case etree_unary:
740 fold_unary (tree);
741 break;
742
743 case etree_binary:
744 fold_binary (tree);
745 break;
746
747 case etree_trinary:
748 fold_trinary (tree);
749 break;
750
751 case etree_assign:
752 case etree_provide:
753 case etree_provided:
754 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0)
755 {
756 /* Assignment to dot can only be done during allocation. */
757 if (tree->type.node_class != etree_assign)
758 einfo (_("%F%S can not PROVIDE assignment to location counter\n"));
759 if (expld.phase == lang_mark_phase_enum
760 || expld.phase == lang_allocating_phase_enum
761 || (expld.phase == lang_final_phase_enum
762 && expld.section == bfd_abs_section_ptr))
763 {
764 /* Notify the folder that this is an assignment to dot. */
765 expld.assigning_to_dot = TRUE;
766 exp_fold_tree_1 (tree->assign.src);
767 expld.assigning_to_dot = FALSE;
768
769 if (!expld.result.valid_p)
770 {
771 if (expld.phase != lang_mark_phase_enum)
772 einfo (_("%F%S invalid assignment to location counter\n"));
773 }
774 else if (expld.dotp == NULL)
775 einfo (_("%F%S assignment to location counter"
776 " invalid outside of SECTION\n"));
777 else
778 {
779 bfd_vma nextdot;
780
781 nextdot = expld.result.value;
782 if (expld.result.section != NULL)
783 nextdot += expld.result.section->vma;
784 else
785 nextdot += expld.section->vma;
786 if (nextdot < expld.dot
787 && expld.section != bfd_abs_section_ptr)
788 einfo (_("%F%S cannot move location counter backwards"
789 " (from %V to %V)\n"), expld.dot, nextdot);
790 else
791 {
792 expld.dot = nextdot;
793 *expld.dotp = nextdot;
794 }
795 }
796 }
797 else
798 memset (&expld.result, 0, sizeof (expld.result));
799 }
800 else
801 {
802 struct bfd_link_hash_entry *h = NULL;
803
804 if (tree->type.node_class == etree_provide)
805 {
806 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
807 FALSE, FALSE, TRUE);
808 if (h == NULL
809 || (h->type != bfd_link_hash_new
810 && h->type != bfd_link_hash_undefined
811 && h->type != bfd_link_hash_common))
812 {
813 /* Do nothing. The symbol was never referenced, or was
814 defined by some object. */
815 break;
816 }
817 }
818
819 exp_fold_tree_1 (tree->assign.src);
820 if (expld.result.valid_p
821 || (expld.phase == lang_first_phase_enum
822 && !expld.uses_defined))
823 {
824 if (h == NULL)
825 {
826 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
827 TRUE, FALSE, TRUE);
828 if (h == NULL)
829 einfo (_("%P%F:%s: hash creation failed\n"),
830 tree->assign.dst);
831 }
832
833 /* FIXME: Should we worry if the symbol is already
834 defined? */
835 lang_update_definedness (tree->assign.dst, h);
836 h->type = bfd_link_hash_defined;
837 h->u.def.value = expld.result.value;
838 if (expld.result.section == NULL)
839 expld.result.section = expld.section;
840 h->u.def.section = expld.result.section;
841 if (tree->type.node_class == etree_provide)
842 tree->type.node_class = etree_provided;
843
844 /* Copy the symbol type if this is a simple assignment of
845 one symbol to annother. */
846 if (tree->assign.src->type.node_class == etree_name)
847 {
848 struct bfd_link_hash_entry *hsrc;
849
850 hsrc = bfd_link_hash_lookup (link_info.hash,
851 tree->assign.src->name.name,
852 FALSE, FALSE, TRUE);
853 if (hsrc)
854 bfd_copy_link_hash_symbol_type (link_info.output_bfd, h,
855 hsrc);
856 }
857 }
858 else if (expld.phase == lang_final_phase_enum)
859 {
860 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
861 FALSE, FALSE, TRUE);
862 if (h != NULL
863 && h->type == bfd_link_hash_new)
864 h->type = bfd_link_hash_undefined;
865 }
866 }
867 break;
868
869 case etree_name:
870 fold_name (tree);
871 break;
872
873 default:
874 FAIL ();
875 memset (&expld.result, 0, sizeof (expld.result));
876 break;
877 }
878 }
879
880 void
881 exp_fold_tree (etree_type *tree, asection *current_section, bfd_vma *dotp)
882 {
883 expld.dot = *dotp;
884 expld.dotp = dotp;
885 expld.section = current_section;
886 expld.uses_defined = FALSE;
887 exp_fold_tree_1 (tree);
888 }
889
890 void
891 exp_fold_tree_no_dot (etree_type *tree)
892 {
893 expld.dot = 0;
894 expld.dotp = NULL;
895 expld.section = bfd_abs_section_ptr;
896 expld.uses_defined = FALSE;
897 exp_fold_tree_1 (tree);
898 }
899
900 etree_type *
901 exp_binop (int code, etree_type *lhs, etree_type *rhs)
902 {
903 etree_type value, *new_e;
904
905 value.type.node_code = code;
906 value.type.lineno = lhs->type.lineno;
907 value.binary.lhs = lhs;
908 value.binary.rhs = rhs;
909 value.type.node_class = etree_binary;
910 exp_fold_tree_no_dot (&value);
911 if (expld.result.valid_p)
912 return exp_intop (expld.result.value);
913
914 new_e = (etree_type *) stat_alloc (sizeof (new_e->binary));
915 memcpy (new_e, &value, sizeof (new_e->binary));
916 return new_e;
917 }
918
919 etree_type *
920 exp_trinop (int code, etree_type *cond, etree_type *lhs, etree_type *rhs)
921 {
922 etree_type value, *new_e;
923
924 value.type.node_code = code;
925 value.type.lineno = lhs->type.lineno;
926 value.trinary.lhs = lhs;
927 value.trinary.cond = cond;
928 value.trinary.rhs = rhs;
929 value.type.node_class = etree_trinary;
930 exp_fold_tree_no_dot (&value);
931 if (expld.result.valid_p)
932 return exp_intop (expld.result.value);
933
934 new_e = (etree_type *) stat_alloc (sizeof (new_e->trinary));
935 memcpy (new_e, &value, sizeof (new_e->trinary));
936 return new_e;
937 }
938
939 etree_type *
940 exp_unop (int code, etree_type *child)
941 {
942 etree_type value, *new_e;
943
944 value.unary.type.node_code = code;
945 value.unary.type.lineno = child->type.lineno;
946 value.unary.child = child;
947 value.unary.type.node_class = etree_unary;
948 exp_fold_tree_no_dot (&value);
949 if (expld.result.valid_p)
950 return exp_intop (expld.result.value);
951
952 new_e = (etree_type *) stat_alloc (sizeof (new_e->unary));
953 memcpy (new_e, &value, sizeof (new_e->unary));
954 return new_e;
955 }
956
957 etree_type *
958 exp_nameop (int code, const char *name)
959 {
960 etree_type value, *new_e;
961
962 value.name.type.node_code = code;
963 value.name.type.lineno = lineno;
964 value.name.name = name;
965 value.name.type.node_class = etree_name;
966
967 exp_fold_tree_no_dot (&value);
968 if (expld.result.valid_p)
969 return exp_intop (expld.result.value);
970
971 new_e = (etree_type *) stat_alloc (sizeof (new_e->name));
972 memcpy (new_e, &value, sizeof (new_e->name));
973 return new_e;
974
975 }
976
977 etree_type *
978 exp_assop (int code, const char *dst, etree_type *src)
979 {
980 etree_type *new_e;
981
982 new_e = (etree_type *) stat_alloc (sizeof (new_e->assign));
983 new_e->type.node_code = code;
984 new_e->type.lineno = src->type.lineno;
985 new_e->type.node_class = etree_assign;
986 new_e->assign.src = src;
987 new_e->assign.dst = dst;
988 return new_e;
989 }
990
991 /* Handle PROVIDE. */
992
993 etree_type *
994 exp_provide (const char *dst, etree_type *src, bfd_boolean hidden)
995 {
996 etree_type *n;
997
998 n = (etree_type *) stat_alloc (sizeof (n->assign));
999 n->assign.type.node_code = '=';
1000 n->assign.type.lineno = src->type.lineno;
1001 n->assign.type.node_class = etree_provide;
1002 n->assign.src = src;
1003 n->assign.dst = dst;
1004 n->assign.hidden = hidden;
1005 return n;
1006 }
1007
1008 /* Handle ASSERT. */
1009
1010 etree_type *
1011 exp_assert (etree_type *exp, const char *message)
1012 {
1013 etree_type *n;
1014
1015 n = (etree_type *) stat_alloc (sizeof (n->assert_s));
1016 n->assert_s.type.node_code = '!';
1017 n->assert_s.type.lineno = exp->type.lineno;
1018 n->assert_s.type.node_class = etree_assert;
1019 n->assert_s.child = exp;
1020 n->assert_s.message = message;
1021 return n;
1022 }
1023
1024 void
1025 exp_print_tree (etree_type *tree)
1026 {
1027 bfd_boolean function_like;
1028
1029 if (config.map_file == NULL)
1030 config.map_file = stderr;
1031
1032 if (tree == NULL)
1033 {
1034 minfo ("NULL TREE\n");
1035 return;
1036 }
1037
1038 switch (tree->type.node_class)
1039 {
1040 case etree_value:
1041 minfo ("0x%v", tree->value.value);
1042 return;
1043 case etree_rel:
1044 if (tree->rel.section->owner != NULL)
1045 minfo ("%B:", tree->rel.section->owner);
1046 minfo ("%s+0x%v", tree->rel.section->name, tree->rel.value);
1047 return;
1048 case etree_assign:
1049 fputs (tree->assign.dst, config.map_file);
1050 exp_print_token (tree->type.node_code, TRUE);
1051 exp_print_tree (tree->assign.src);
1052 break;
1053 case etree_provide:
1054 case etree_provided:
1055 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
1056 exp_print_tree (tree->assign.src);
1057 fputc (')', config.map_file);
1058 break;
1059 case etree_binary:
1060 function_like = FALSE;
1061 switch (tree->type.node_code)
1062 {
1063 case MAX_K:
1064 case MIN_K:
1065 case ALIGN_K:
1066 case DATA_SEGMENT_ALIGN:
1067 case DATA_SEGMENT_RELRO_END:
1068 function_like = TRUE;
1069 }
1070 if (function_like)
1071 {
1072 exp_print_token (tree->type.node_code, FALSE);
1073 fputc (' ', config.map_file);
1074 }
1075 fputc ('(', config.map_file);
1076 exp_print_tree (tree->binary.lhs);
1077 if (function_like)
1078 fprintf (config.map_file, ", ");
1079 else
1080 exp_print_token (tree->type.node_code, TRUE);
1081 exp_print_tree (tree->binary.rhs);
1082 fputc (')', config.map_file);
1083 break;
1084 case etree_trinary:
1085 exp_print_tree (tree->trinary.cond);
1086 fputc ('?', config.map_file);
1087 exp_print_tree (tree->trinary.lhs);
1088 fputc (':', config.map_file);
1089 exp_print_tree (tree->trinary.rhs);
1090 break;
1091 case etree_unary:
1092 exp_print_token (tree->unary.type.node_code, FALSE);
1093 if (tree->unary.child)
1094 {
1095 fprintf (config.map_file, " (");
1096 exp_print_tree (tree->unary.child);
1097 fputc (')', config.map_file);
1098 }
1099 break;
1100
1101 case etree_assert:
1102 fprintf (config.map_file, "ASSERT (");
1103 exp_print_tree (tree->assert_s.child);
1104 fprintf (config.map_file, ", %s)", tree->assert_s.message);
1105 break;
1106
1107 case etree_name:
1108 if (tree->type.node_code == NAME)
1109 fputs (tree->name.name, config.map_file);
1110 else
1111 {
1112 exp_print_token (tree->type.node_code, FALSE);
1113 if (tree->name.name)
1114 fprintf (config.map_file, " (%s)", tree->name.name);
1115 }
1116 break;
1117 default:
1118 FAIL ();
1119 break;
1120 }
1121 }
1122
1123 bfd_vma
1124 exp_get_vma (etree_type *tree, bfd_vma def, char *name)
1125 {
1126 if (tree != NULL)
1127 {
1128 exp_fold_tree_no_dot (tree);
1129 if (expld.result.valid_p)
1130 return expld.result.value;
1131 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1132 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1133 }
1134 return def;
1135 }
1136
1137 int
1138 exp_get_value_int (etree_type *tree, int def, char *name)
1139 {
1140 return exp_get_vma (tree, def, name);
1141 }
1142
1143 fill_type *
1144 exp_get_fill (etree_type *tree, fill_type *def, char *name)
1145 {
1146 fill_type *fill;
1147 size_t len;
1148 unsigned int val;
1149
1150 if (tree == NULL)
1151 return def;
1152
1153 exp_fold_tree_no_dot (tree);
1154 if (!expld.result.valid_p)
1155 {
1156 if (name != NULL && expld.phase != lang_mark_phase_enum)
1157 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1158 return def;
1159 }
1160
1161 if (expld.result.str != NULL && (len = strlen (expld.result.str)) != 0)
1162 {
1163 unsigned char *dst;
1164 unsigned char *s;
1165 fill = (fill_type *) xmalloc ((len + 1) / 2 + sizeof (*fill) - 1);
1166 fill->size = (len + 1) / 2;
1167 dst = fill->data;
1168 s = (unsigned char *) expld.result.str;
1169 val = 0;
1170 do
1171 {
1172 unsigned int digit;
1173
1174 digit = *s++ - '0';
1175 if (digit > 9)
1176 digit = (digit - 'A' + '0' + 10) & 0xf;
1177 val <<= 4;
1178 val += digit;
1179 --len;
1180 if ((len & 1) == 0)
1181 {
1182 *dst++ = val;
1183 val = 0;
1184 }
1185 }
1186 while (len != 0);
1187 }
1188 else
1189 {
1190 fill = (fill_type *) xmalloc (4 + sizeof (*fill) - 1);
1191 val = expld.result.value;
1192 fill->data[0] = (val >> 24) & 0xff;
1193 fill->data[1] = (val >> 16) & 0xff;
1194 fill->data[2] = (val >> 8) & 0xff;
1195 fill->data[3] = (val >> 0) & 0xff;
1196 fill->size = 4;
1197 }
1198 return fill;
1199 }
1200
1201 bfd_vma
1202 exp_get_abs_int (etree_type *tree, int def, char *name)
1203 {
1204 if (tree != NULL)
1205 {
1206 exp_fold_tree_no_dot (tree);
1207
1208 if (expld.result.valid_p)
1209 {
1210 if (expld.result.section != NULL)
1211 expld.result.value += expld.result.section->vma;
1212 return expld.result.value;
1213 }
1214 else if (name != NULL && expld.phase != lang_mark_phase_enum)
1215 {
1216 lineno = tree->type.lineno;
1217 einfo (_("%F%S: nonconstant expression for %s\n"), name);
1218 }
1219 }
1220 return def;
1221 }
1222
1223 static bfd_vma
1224 align_n (bfd_vma value, bfd_vma align)
1225 {
1226 if (align <= 1)
1227 return value;
1228
1229 value = (value + align - 1) / align;
1230 return value * align;
1231 }