1 /* This module handles expression trees.
2 Copyright (C) 1991, 1993 Free Software Foundation, Inc.
5 This file is part of GLD, the Gnu Linker.
7 GLD is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GLD is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 This module is in charge of working out the contents of expressions.
24 It has to keep track of the relative/absness of a symbol etc. This is
25 done by keeping all values in a struct (an etree_value_type) which
26 contains a value, a section to which it is relative and a valid bit.
42 static void exp_print_token PARAMS ((token_code_type code));
43 static void make_abs PARAMS ((etree_value_type *ptr));
44 static etree_value_type new_abs PARAMS ((bfd_vma value));
45 static void check PARAMS ((lang_output_section_statement_type *os,
46 const char *name, const char *op));
47 static etree_value_type new_rel
48 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
49 static etree_value_type new_rel_from_section
50 PARAMS ((bfd_vma value, lang_output_section_statement_type *section));
51 static etree_value_type fold_binary
52 PARAMS ((etree_type *tree,
53 lang_output_section_statement_type *current_section,
54 lang_phase_type allocation_done,
55 bfd_vma dot, bfd_vma *dotp));
56 static etree_value_type fold_name
57 PARAMS ((etree_type *tree,
58 lang_output_section_statement_type *current_section,
59 lang_phase_type allocation_done,
61 static etree_value_type exp_fold_tree_no_dot
62 PARAMS ((etree_type *tree,
63 lang_output_section_statement_type *current_section,
64 lang_phase_type allocation_done));
67 exp_print_token (code)
77 { REL, "relocateable" },
97 { SECTIONS,"SECTIONS" },
98 { SIZEOF_HEADERS,"SIZEOF_HEADERS" },
103 { DEFINED,"DEFINED" },
104 { TARGET_K,"TARGET" },
105 { SEARCH_DIR,"SEARCH_DIR" },
116 for (idx = 0; table[idx].name != (char*)NULL; idx++) {
117 if (table[idx].code == code) {
118 fprintf(config.map_file, "%s", table[idx].name);
122 /* Not in table, just print it alone */
123 fprintf(config.map_file, "%c",code);
128 etree_value_type *ptr;
130 asection *s = ptr->section->bfd_section;
131 ptr->value += s->vma;
132 ptr->section = abs_output_section;
135 static etree_value_type
139 etree_value_type new;
141 new.section = abs_output_section;
148 lang_output_section_statement_type *os;
152 if (os == (lang_output_section_statement_type *)NULL) {
153 einfo("%F%P: %s uses undefined section %s\n", op, name);
155 if (os->processed == false) {
156 einfo("%F%P: %s forward reference of section %s\n",op, name);
164 etree_type *new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->value)));
165 new->type.node_code = INT;
166 new->value.value = value;
167 new->type.node_class = etree_value;
172 /* Build an expression representing an unnamed relocateable value. */
175 exp_relop (section, value)
179 etree_type *new = (etree_type *) stat_alloc (sizeof (new->rel));
180 new->type.node_code = REL;
181 new->type.node_class = etree_rel;
182 new->rel.section = section;
183 new->rel.value = value;
187 static etree_value_type
188 new_rel (value, section)
190 lang_output_section_statement_type *section;
192 etree_value_type new;
195 new.section = section;
199 static etree_value_type
200 new_rel_from_section (value, section)
202 lang_output_section_statement_type *section;
204 etree_value_type new;
207 new.section = section;
209 new.value -= section->bfd_section->vma;
214 static etree_value_type
215 fold_binary (tree, current_section, allocation_done, dot, dotp)
217 lang_output_section_statement_type *current_section;
218 lang_phase_type allocation_done;
222 etree_value_type result;
224 result = exp_fold_tree(tree->binary.lhs, current_section,
225 allocation_done, dot, dotp);
227 etree_value_type other;
228 other = exp_fold_tree(tree->binary.rhs,
230 allocation_done, dot,dotp) ;
232 /* If values are from different sections, or this is an */
233 /* absolute expression, make both source args absolute */
234 if (result.section != other.section ||
235 current_section == abs_output_section)
241 switch (tree->type.node_code)
244 /* Mod, both absolule*/
246 if (other.value == 0) {
247 einfo("%F%S %% by zero\n");
249 result.value = (int)result.value % (int)other.value;
252 if (other.value == 0) {
253 einfo("%F%S / by zero\n");
255 result.value = (int)result.value / (int) other.value;
257 #define BOP(x,y) case x : result.value = result.value y other.value;break;
279 result.valid = false;
287 etree_value_type new;
292 static etree_value_type
293 fold_name (tree, current_section, allocation_done, dot)
295 lang_output_section_statement_type *current_section;
296 lang_phase_type allocation_done;
299 etree_value_type result;
300 switch (tree->type.node_code)
303 if (allocation_done != lang_first_phase_enum)
305 result = new_abs ((bfd_vma)
306 bfd_sizeof_headers (output_bfd,
307 link_info.relocateable));
311 result.valid = false;
316 struct bfd_link_hash_entry *h;
318 h = bfd_link_hash_lookup (link_info.hash, tree->name.name,
320 result.value = (h != (struct bfd_link_hash_entry *) NULL
321 && (h->type == bfd_link_hash_defined
322 || h->type == bfd_link_hash_common));
328 result.valid = false;
329 if (tree->name.name[0] == '.' && tree->name.name[1] == 0)
331 if (allocation_done != lang_first_phase_enum)
332 result = new_rel_from_section(dot, current_section);
336 else if (allocation_done == lang_final_phase_enum)
338 struct bfd_link_hash_entry *h;
340 h = bfd_link_hash_lookup (link_info.hash, tree->name.name,
342 if (h != (struct bfd_link_hash_entry *) NULL
343 && h->type == bfd_link_hash_defined)
345 lang_output_section_statement_type *os;
347 os = (lang_output_section_statement_lookup
348 (h->u.def.section->output_section->name));
350 /* FIXME: Is this correct if this section is being
352 result = new_rel ((h->u.def.value
353 + h->u.def.section->output_offset),
356 if (result.valid == false)
357 einfo("%F%S: undefined symbol `%s' referenced in expression\n",
364 if (allocation_done != lang_first_phase_enum) {
365 lang_output_section_statement_type *os =
366 lang_output_section_find(tree->name.name);
367 check(os,tree->name.name,"ADDR");
368 result = new_rel((bfd_vma)0, os);
375 if(allocation_done != lang_first_phase_enum) {
376 lang_output_section_statement_type *os =
377 lang_output_section_find(tree->name.name);
378 check(os,tree->name.name,"SIZEOF");
379 result = new_abs((bfd_vma)(os->bfd_section->_raw_size));
394 exp_fold_tree (tree, current_section, allocation_done, dot, dotp)
396 lang_output_section_statement_type *current_section;
397 lang_phase_type allocation_done;
401 etree_value_type result;
403 if (tree == (etree_type *)NULL) {
404 result.valid = false;
407 switch (tree->type.node_class)
410 result = new_rel(tree->value.value, current_section);
413 if (allocation_done != lang_final_phase_enum)
414 result.valid = false;
416 result = new_rel ((tree->rel.value
417 + tree->rel.section->output_section->vma
418 + tree->rel.section->output_offset),
422 result = exp_fold_tree(tree->unary.child,
424 allocation_done, dot, dotp);
425 if (result.valid == true)
427 switch(tree->type.node_code)
430 if (allocation_done != lang_first_phase_enum) {
431 result = new_rel_from_section(ALIGN_N(dot,
437 result.valid = false;
441 if (allocation_done != lang_first_phase_enum)
444 == (lang_output_section_statement_type*)NULL)
446 /* Outside a section, so it's all ok */
450 /* Inside a section, subtract the base of the section,
451 so when it's added again (in an assignment), everything comes out fine
453 result.section = abs_output_section;
454 result.value -= current_section->bfd_section->vma;
460 result.valid = false;
466 result.value = ~result.value;
470 result.value = !result.value;
474 result.value = -result.value;
477 if (allocation_done ==lang_allocating_phase_enum) {
479 result.value = ALIGN_N(dot, result.value);
482 /* Return next place aligned to value */
483 result.valid = false;
494 result = exp_fold_tree(tree->trinary.cond,
496 allocation_done, dot, dotp);
498 result = exp_fold_tree(result.value ?
499 tree->trinary.lhs:tree->trinary.rhs,
501 allocation_done, dot, dotp);
506 result = fold_binary(tree, current_section, allocation_done,
511 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) {
512 /* Assignment to dot can only be done during allocation */
513 if (tree->type.node_class == etree_provide)
514 einfo ("%F%S can not PROVIDE assignment to location counter\n");
515 if (allocation_done == lang_allocating_phase_enum) {
516 result = exp_fold_tree(tree->assign.src,
518 lang_allocating_phase_enum, dot, dotp);
519 if (result.valid == false) {
520 einfo("%F%S invalid assignment to location counter\n");
523 if (current_section ==
524 (lang_output_section_statement_type *)NULL) {
525 einfo("%F%S assignment to location counter invalid outside of SECTION\n");
528 bfd_vma nextdot =result.value +
529 current_section->bfd_section->vma;
531 einfo("%F%S cannot move location counter backwards (from %V to %V)\n", dot, nextdot);
542 result = exp_fold_tree (tree->assign.src,
543 current_section, allocation_done,
547 struct bfd_link_hash_entry *h;
549 h = bfd_link_hash_lookup (link_info.hash, tree->assign.dst,
550 (tree->type.node_class == etree_assign
553 if (h == (struct bfd_link_hash_entry *) NULL)
555 if (tree->type.node_class == etree_assign)
556 einfo ("%P%F:%s: hash creation failed\n",
559 else if (tree->type.node_class == etree_provide
560 && h->type != bfd_link_hash_undefined
561 && h->type != bfd_link_hash_common)
563 /* Do nothing. The symbol was defined by some
568 /* FIXME: Should we worry if the symbol is already
570 h->type = bfd_link_hash_defined;
571 h->u.def.value = result.value;
572 h->u.def.section = result.section->bfd_section;
578 result = fold_name(tree, current_section, allocation_done, dot);
581 einfo("%F%S need more of these %d\n",tree->type.node_class );
590 static etree_value_type
591 exp_fold_tree_no_dot (tree, current_section, allocation_done)
593 lang_output_section_statement_type *current_section;
594 lang_phase_type allocation_done;
596 return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
601 exp_binop (code, lhs, rhs)
606 etree_type value, *new;
609 value.type.node_code = code;
610 value.binary.lhs = lhs;
611 value.binary.rhs = rhs;
612 value.type.node_class = etree_binary;
613 r = exp_fold_tree_no_dot(&value,
615 lang_first_phase_enum );
618 return exp_intop(r.value);
620 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->binary)));
621 memcpy((char *)new, (char *)&value, sizeof(new->binary));
626 exp_trinop (code, cond, lhs, rhs)
632 etree_type value, *new;
634 value.type.node_code = code;
635 value.trinary.lhs = lhs;
636 value.trinary.cond = cond;
637 value.trinary.rhs = rhs;
638 value.type.node_class = etree_trinary;
639 r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type
640 *)NULL,lang_first_phase_enum);
642 return exp_intop(r.value);
644 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->trinary)));
645 memcpy((char *)new,(char *) &value, sizeof(new->trinary));
651 exp_unop (code, child)
655 etree_type value, *new;
658 value.unary.type.node_code = code;
659 value.unary.child = child;
660 value.unary.type.node_class = etree_unary;
661 r = exp_fold_tree_no_dot(&value,abs_output_section,
662 lang_first_phase_enum);
664 return exp_intop(r.value);
666 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->unary)));
667 memcpy((char *)new, (char *)&value, sizeof(new->unary));
673 exp_nameop (code, name)
677 etree_type value, *new;
679 value.name.type.node_code = code;
680 value.name.name = name;
681 value.name.type.node_class = etree_name;
684 r = exp_fold_tree_no_dot(&value,
685 (lang_output_section_statement_type *)NULL,
686 lang_first_phase_enum);
688 return exp_intop(r.value);
690 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->name)));
691 memcpy((char *)new, (char *)&value, sizeof(new->name));
700 exp_assop (code, dst, src)
705 etree_type value, *new;
707 value.assign.type.node_code = code;
710 value.assign.src = src;
711 value.assign.dst = dst;
712 value.assign.type.node_class = etree_assign;
715 if (exp_fold_tree_no_dot(&value, &result)) {
716 return exp_intop(result);
719 new = (etree_type*)stat_alloc((bfd_size_type)(sizeof(new->assign)));
720 memcpy((char *)new, (char *)&value, sizeof(new->assign));
724 /* Handle PROVIDE. */
727 exp_provide (dst, src)
733 n = (etree_type *) stat_alloc (sizeof (n->assign));
734 n->assign.type.node_code = '=';
735 n->assign.type.node_class = etree_provide;
742 exp_print_tree (tree)
745 switch (tree->type.node_class) {
747 print_address(tree->value.value);
750 if (tree->rel.section->owner != NULL)
751 fprintf (config.map_file, "%s:",
752 bfd_get_filename (tree->rel.section->owner));
753 fprintf (config.map_file, "%s+", tree->rel.section->name);
754 print_address (tree->rel.value);
758 if (tree->assign.dst->sdefs != (asymbol *)NULL){
759 fprintf(config.map_file,"%s (%x) ",tree->assign.dst->name,
760 tree->assign.dst->sdefs->value);
763 fprintf(config.map_file,"%s (UNDEFINED)",tree->assign.dst->name);
766 fprintf(config.map_file,"%s ",tree->assign.dst);
767 exp_print_token(tree->type.node_code);
768 exp_print_tree(tree->assign.src);
771 fprintf (config.map_file, "PROVIDE (%s, ", tree->assign.dst);
772 exp_print_tree (tree->assign.src);
773 fprintf (config.map_file, ")");
776 fprintf(config.map_file,"(");
777 exp_print_tree(tree->binary.lhs);
778 exp_print_token(tree->type.node_code);
779 exp_print_tree(tree->binary.rhs);
780 fprintf(config.map_file,")");
783 exp_print_tree(tree->trinary.cond);
784 fprintf(config.map_file,"?");
785 exp_print_tree(tree->trinary.lhs);
786 fprintf(config.map_file,":");
787 exp_print_tree(tree->trinary.rhs);
790 exp_print_token(tree->unary.type.node_code);
791 if (tree->unary.child)
794 fprintf(config.map_file,"(");
795 exp_print_tree(tree->unary.child);
796 fprintf(config.map_file,")");
801 fprintf(config.map_file,"????????");
804 if (tree->type.node_code == NAME) {
805 fprintf(config.map_file,"%s", tree->name.name);
808 exp_print_token(tree->type.node_code);
810 fprintf(config.map_file,"(%s)", tree->name.name);
823 exp_get_vma (tree, def, name, allocation_done)
827 lang_phase_type allocation_done;
831 if (tree != (etree_type *)NULL) {
832 r = exp_fold_tree_no_dot(tree,
835 if (r.valid == false && name) {
836 einfo("%F%S nonconstant expression for %s\n",name);
846 exp_get_value_int (tree,def,name, allocation_done)
850 lang_phase_type allocation_done;
852 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);