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