The default script is now compiled in; the others are still in the filesystem.
[deliverable/binutils-gdb.git] / ld / ldexp.c
1 /* This module handles expression trees.
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support (sac@cygnus.com).
4
5 This file is part of GLD, the Gnu Linker.
6
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)
10 any later version.
11
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.
16
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. */
20
21 /*
22 This module is in charge of working out the contents of expressions.
23
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.
27
28 */
29
30
31 #include "bfd.h"
32 #include "sysdep.h"
33
34 #include "ld.h"
35 #include "ldmain.h"
36 #include "ldmisc.h"
37 #include "ldexp.h"
38 #include "ldgram.h"
39 #include "ldsym.h"
40 #include "ldlang.h"
41
42 extern char *output_filename;
43 extern unsigned int undefined_global_sym_count;
44 extern unsigned int defined_global_sym_count;
45 extern bfd *output_bfd;
46 extern bfd_size_type largest_section;
47 extern lang_statement_list_type file_chain;
48 extern args_type command_line;
49 extern ld_config_type config;
50
51 extern lang_input_statement_type *script_file;
52 extern unsigned int defined_global_sym_count;
53 extern lang_output_section_statement_type *abs_output_section;
54 extern bfd_vma print_dot;
55
56
57 static void
58 DEFUN(exp_print_token,( code),
59 token_code_type code)
60 {
61 static struct {
62 token_code_type code;
63 char *name;
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 SECTIONS,"SECTIONS",
87 SIZEOF_HEADERS,"SIZEOF_HEADERS",
88 NEXT,"NEXT",
89 SIZEOF,"SIZEOF",
90 ADDR,"ADDR",
91 MEMORY,"MEMORY",
92
93
94
95
96
97 DEFINED,"DEFINED",
98 TARGET_K,"TARGET",
99 SEARCH_DIR,"SEARCH_DIR",
100 MAP,"MAP",
101 LONG,"LONG",
102 SHORT,"SHORT",
103 BYTE,"BYTE",
104 ENTRY,"ENTRY",
105 0,(char *)NULL} ;
106
107
108
109 unsigned int idx;
110 for (idx = 0; table[idx].name != (char*)NULL; idx++) {
111 if (table[idx].code == code) {
112 fprintf(config.map_file, "%s", table[idx].name);
113 return;
114 }
115 }
116 /* Not in table, just print it alone */
117 fprintf(config.map_file, "%c",code);
118 }
119
120 static void
121 DEFUN(make_abs,(ptr),
122 etree_value_type *ptr)
123 {
124 asection *s = ptr->section->bfd_section;
125 ptr->value += s->vma;
126 ptr->section = abs_output_section;
127 }
128
129 static
130 DEFUN(etree_value_type new_abs,(value),
131 bfd_vma value)
132 {
133
134
135 etree_value_type new;
136 new.valid = true;
137 new.section = abs_output_section;
138 new.value = value;
139 return new;
140 }
141
142 static void
143 DEFUN(check, (os, name, op),
144 lang_output_section_statement_type *os AND
145 CONST char *name AND
146 CONST char *op)
147 {
148 if (os == (lang_output_section_statement_type *)NULL) {
149 einfo("%F%P %s uses undefined section %s\n", op, name);
150 }
151 if (os->processed == false) {
152 einfo("%F%P %s forward reference of section %s\n",op, name);
153 }
154 }
155
156 etree_type *
157 DEFUN(exp_intop,(value),
158 bfd_vma value)
159 {
160 etree_type *new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->value)));
161 new->type.node_code = INT;
162 new->value.value = value;
163 new->type.node_class = etree_value;
164 return new;
165
166 }
167
168
169 static
170 DEFUN(etree_value_type new_rel,(value, section),
171 bfd_vma value AND
172 lang_output_section_statement_type *section)
173 {
174 etree_value_type new;
175 new.valid = true;
176 new.value = value;
177 new.section = section;
178 return new;
179 }
180
181 static
182 DEFUN(etree_value_type
183 new_rel_from_section, (value, section),
184 bfd_vma value AND
185 lang_output_section_statement_type *section)
186 {
187 etree_value_type new;
188 new.valid = true;
189 new.value = value;
190 new.section = section;
191
192 new.value -= section->bfd_section->vma;
193
194 return new;
195 }
196
197 static etree_value_type
198 DEFUN(fold_binary,(tree, current_section, allocation_done, dot, dotp),
199 etree_type *tree AND
200 lang_output_section_statement_type *current_section AND
201 lang_phase_type allocation_done AND
202 bfd_vma dot AND
203 bfd_vma *dotp)
204 {
205 etree_value_type result;
206
207 result = exp_fold_tree(tree->binary.lhs, current_section,
208 allocation_done, dot, dotp);
209 if (result.valid) {
210 etree_value_type other;
211 other = exp_fold_tree(tree->binary.rhs,
212 current_section,
213 allocation_done, dot,dotp) ;
214 if (other.valid) {
215 /* If values are from different sections, or this is an */
216 /* absolute expression, make both source args absolute */
217 if (result.section != other.section ||
218 current_section == abs_output_section)
219 {
220 make_abs(&result);
221 make_abs(&other);
222 }
223
224 switch (tree->type.node_code)
225 {
226 case '%':
227 /* Mod, both absolule*/
228
229 if (other.value == 0) {
230 einfo("%F%S % by zero\n");
231 }
232 result.value = (int)result.value % (int)other.value;
233 break;
234 case '/':
235 if (other.value == 0) {
236 einfo("%F%S / by zero\n");
237 }
238 result.value = (int)result.value / (int) other.value;
239 break;
240 #define BOP(x,y) case x : result.value = result.value y other.value;break;
241 BOP('+',+);
242 BOP('*',*);
243 BOP('-',-);
244 BOP(LSHIFT,<<);
245 BOP(RSHIFT,>>);
246 BOP(EQ,==);
247 BOP(NE,!=);
248 BOP('<',<);
249 BOP('>',>);
250 BOP(LE,<=);
251 BOP(GE,>=);
252 BOP('&',&);
253 BOP('^',^);
254 BOP('|',|);
255 BOP(ANDAND,&&);
256 BOP(OROR,||);
257 default:
258 FAIL();
259 }
260 }
261 else {
262 result.valid = false;
263 }
264 }
265 return result;
266 }
267 etree_value_type
268 DEFUN_VOID(invalid)
269 {
270 etree_value_type new;
271 new.valid = false;
272 return new;
273 }
274
275 etree_value_type
276 DEFUN(fold_name, (tree, current_section, allocation_done, dot),
277 etree_type *tree AND
278 lang_output_section_statement_type *current_section AND
279 lang_phase_type allocation_done AND
280 bfd_vma dot)
281 {
282 etree_value_type result;
283 switch (tree->type.node_code)
284 {
285 case SIZEOF_HEADERS:
286 if (allocation_done != lang_first_phase_enum)
287 {
288 result = new_abs(bfd_sizeof_headers(output_bfd,
289 config.relocateable_output));
290
291 }
292 else {
293 result.valid = false;
294 }
295 break;
296 case DEFINED:
297 result.value =
298 ldsym_get_soft(tree->name.name) != (ldsym_type *)NULL;
299 result.section = 0;
300 result.valid = true;
301 break;
302 case NAME:
303 result.valid = false;
304 if (tree->name.name[0] == '.' && tree->name.name[1] == 0) {
305
306 if (allocation_done != lang_first_phase_enum) {
307 result = new_rel_from_section(dot, current_section);
308 }
309 else {
310 result = invalid();
311 }
312 }
313 else {
314 if (allocation_done == lang_final_phase_enum) {
315 ldsym_type *sy = ldsym_get_soft(tree->name.name);
316
317 if (sy) {
318 asymbol **sdefp = sy->sdefs_chain;
319
320 if (sdefp) {
321 asymbol *sdef = *sdefp;
322 #if 0
323 if (sdef->section == (asection *)NULL) {
324 /* This is an absolute symbol */
325 result = new_abs(sdef->value);
326 }
327 else
328 #endif
329 {
330 lang_output_section_statement_type *os =
331 lang_output_section_statement_lookup(
332 sdef->section->output_section->name);
333 /* If the symbol is from a file which we are not
334 relocating (-R) then return an absolute for its
335 value */
336 if (sdef->the_bfd->usrdata &&
337 ((lang_input_statement_type*)(sdef->the_bfd->usrdata))->just_syms_flag == true)
338 {
339 result = new_abs(sdef->value +sdef->section->vma);
340
341 }
342 else {
343 result = new_rel(sdef->value + sdef->section->output_offset, os);
344 }
345 }
346 }
347 }
348 if (result.valid == false) {
349 einfo("%F%S: undefined symbol `%s' referenced in expression.\n",
350 tree->name.name);
351 }
352
353 }
354 }
355
356 break;
357
358 case ADDR:
359
360 if (allocation_done != lang_first_phase_enum) {
361 lang_output_section_statement_type *os =
362 lang_output_section_find(tree->name.name);
363 check(os,tree->name.name,"ADDR");
364 result = new_rel((bfd_vma)0, os);
365 }
366 else {
367 result = invalid();
368 }
369 break;
370 case SIZEOF:
371 if(allocation_done != lang_first_phase_enum) {
372 lang_output_section_statement_type *os =
373 lang_output_section_find(tree->name.name);
374 check(os,tree->name.name,"SIZEOF");
375 result = new_abs((bfd_vma)(os->bfd_section->_raw_size));
376 }
377 else {
378 result = invalid();
379 }
380 break;
381
382 default:
383 FAIL();
384 break;
385 }
386
387 return result;
388 }
389 etree_value_type
390 DEFUN(exp_fold_tree,(tree, current_section, allocation_done,
391 dot, dotp),
392 etree_type *tree AND
393 lang_output_section_statement_type *current_section AND
394 lang_phase_type allocation_done AND
395 bfd_vma dot AND
396 bfd_vma *dotp)
397 {
398 etree_value_type result;
399
400 if (tree == (etree_type *)NULL) {
401 result.valid = false;
402 }
403 else {
404 switch (tree->type.node_class)
405 {
406 case etree_value:
407 result = new_rel(tree->value.value, current_section);
408 break;
409 case etree_unary:
410 result = exp_fold_tree(tree->unary.child,
411 current_section,
412 allocation_done, dot, dotp);
413 if (result.valid == true)
414 {
415 switch(tree->type.node_code)
416 {
417 case ALIGN_K:
418 if (allocation_done != lang_first_phase_enum) {
419 result = new_rel_from_section(ALIGN(dot,
420 result.value) ,
421 current_section);
422
423 }
424 else {
425 result.valid = false;
426 }
427 break;
428 case '~':
429 make_abs(&result);
430 result.value = ~result.value;
431 break;
432 case '!':
433 make_abs(&result);
434 result.value = !result.value;
435 break;
436 case '-':
437 make_abs(&result);
438 result.value = -result.value;
439 break;
440 case NEXT:
441 if (allocation_done ==lang_allocating_phase_enum) {
442 make_abs(&result);
443 result.value = ALIGN(dot, result.value);
444 }
445 else {
446 /* Return next place aligned to value */
447 result.valid = false;
448 }
449 break;
450 default:
451 FAIL();
452 }
453 }
454
455 break;
456 case etree_trinary:
457
458 result = exp_fold_tree(tree->trinary.cond,
459 current_section,
460 allocation_done, dot, dotp);
461 if (result.valid) {
462 result = exp_fold_tree(result.value ?
463 tree->trinary.lhs:tree->trinary.rhs,
464 current_section,
465 allocation_done, dot, dotp);
466 }
467
468 break;
469 case etree_binary:
470 result = fold_binary(tree, current_section, allocation_done,
471 dot, dotp);
472 break;
473 case etree_assign:
474 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) {
475 /* Assignment to dot can only be done during allocation */
476 if (allocation_done == lang_allocating_phase_enum) {
477 result = exp_fold_tree(tree->assign.src,
478 current_section,
479 lang_allocating_phase_enum, dot, dotp);
480 if (result.valid == false) {
481 einfo("%F%S invalid assignment to location counter\n");
482 }
483 else {
484 if (current_section ==
485 (lang_output_section_statement_type *)NULL) {
486 einfo("%F%S assignment to location counter invalid outside of SECTION\n");
487 }
488 else {
489 unsigned long nextdot =result.value +
490 current_section->bfd_section->vma;
491 if (nextdot < dot) {
492 einfo("%F%S cannot move location counter backwards");
493 }
494 else {
495 *dotp = nextdot;
496 }
497 }
498 }
499 }
500 }
501 else {
502 ldsym_type *sy = ldsym_get(tree->assign.dst);
503
504 /* If this symbol has just been created then we'll place it into
505 * a section of our choice
506 */
507 result = exp_fold_tree(tree->assign.src,
508 current_section, allocation_done,
509 dot, dotp);
510 if (result.valid)
511 {
512 asymbol *def;
513 asymbol **def_ptr ;
514 /* Add this definition to script file */
515 if (sy->sdefs_chain)
516 {
517 def_ptr = sy->sdefs_chain;
518 def = *def_ptr;
519
520 }
521 else
522 {
523 def_ptr = (asymbol **)stat_alloc((bfd_size_type)(sizeof(asymbol **)));
524 def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
525
526
527 def->flags = 0;
528
529 sy->sdefs_chain = def_ptr;
530 *def_ptr = def;
531 }
532
533 def->value = result.value;
534
535 def->section = result.section->bfd_section;
536 def->flags = BSF_GLOBAL | BSF_EXPORT;
537
538
539 def->udata = (PTR)NULL;
540 def->name = sy->name;
541
542 if (sy->sdefs_chain == 0) Q_enter_global_ref(def_ptr);
543 }
544
545 }
546
547
548 break;
549 case etree_name:
550 result = fold_name(tree, current_section, allocation_done, dot);
551 break;
552 default:
553 einfo("%F%S Need more of these %d",tree->type.node_class );
554
555 }
556 }
557
558 return result;
559 }
560
561
562 etree_value_type
563 DEFUN(exp_fold_tree_no_dot,(tree, current_section, allocation_done),
564 etree_type *tree AND
565 lang_output_section_statement_type *current_section AND
566 lang_phase_type allocation_done)
567 {
568 return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
569 0, (bfd_vma *)NULL);
570 }
571
572 etree_type *
573 DEFUN(exp_binop,(code, lhs, rhs),
574 int code AND
575 etree_type *lhs AND
576 etree_type *rhs)
577 {
578 etree_type value, *new;
579 etree_value_type r;
580
581 value.type.node_code = code;
582 value.binary.lhs = lhs;
583 value.binary.rhs = rhs;
584 value.type.node_class = etree_binary;
585 r = exp_fold_tree_no_dot(&value,
586 abs_output_section,
587 lang_first_phase_enum );
588 if (r.valid)
589 {
590 return exp_intop(r.value);
591 }
592 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->binary)));
593 memcpy((char *)new, (char *)&value, sizeof(new->binary));
594 return new;
595 }
596
597 etree_type *
598 DEFUN(exp_trinop,(code, cond, lhs, rhs),
599 int code AND
600 etree_type *cond AND
601 etree_type *lhs AND
602 etree_type *rhs)
603 {
604 etree_type value, *new;
605 etree_value_type r;
606 value.type.node_code = code;
607 value.trinary.lhs = lhs;
608 value.trinary.cond = cond;
609 value.trinary.rhs = rhs;
610 value.type.node_class = etree_trinary;
611 r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type
612 *)NULL,lang_first_phase_enum);
613 if (r.valid) {
614 return exp_intop(r.value);
615 }
616 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->trinary)));
617 memcpy((char *)new,(char *) &value, sizeof(new->trinary));
618 return new;
619 }
620
621
622 etree_type *
623 DEFUN(exp_unop,(code, child),
624 int code AND
625 etree_type *child)
626 {
627 etree_type value, *new;
628
629 etree_value_type r;
630 value.unary.type.node_code = code;
631 value.unary.child = child;
632 value.unary.type.node_class = etree_unary;
633 r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL,
634 lang_first_phase_enum);
635 if (r.valid) {
636 return exp_intop(r.value);
637 }
638 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->unary)));
639 memcpy((char *)new, (char *)&value, sizeof(new->unary));
640 return new;
641 }
642
643
644 etree_type *
645 DEFUN(exp_nameop,(code, name),
646 int code AND
647 CONST char *name)
648 {
649
650 etree_type value, *new;
651 etree_value_type r;
652 value.name.type.node_code = code;
653 value.name.name = name;
654 value.name.type.node_class = etree_name;
655
656
657 r = exp_fold_tree_no_dot(&value,
658 (lang_output_section_statement_type *)NULL,
659 lang_first_phase_enum);
660 if (r.valid) {
661 return exp_intop(r.value);
662 }
663 new = (etree_type *)stat_alloc((bfd_size_type)(sizeof(new->name)));
664 memcpy((char *)new, (char *)&value, sizeof(new->name));
665 return new;
666
667 }
668
669
670
671
672 etree_type *
673 DEFUN(exp_assop,(code, dst, src),
674 int code AND
675 CONST char *dst AND
676 etree_type *src)
677 {
678 etree_type value, *new;
679
680 value.assign.type.node_code = code;
681
682
683 value.assign.src = src;
684 value.assign.dst = dst;
685 value.assign.type.node_class = etree_assign;
686
687 #if 0
688 if (exp_fold_tree_no_dot(&value, &result)) {
689 return exp_intop(result);
690 }
691 #endif
692 new = (etree_type*)stat_alloc((bfd_size_type)(sizeof(new->assign)));
693 memcpy((char *)new, (char *)&value, sizeof(new->assign));
694 return new;
695 }
696
697 void
698 DEFUN(exp_print_tree,(tree),
699 etree_type *tree)
700 {
701 switch (tree->type.node_class) {
702 case etree_value:
703 print_address(tree->value.value);
704 return;
705
706 case etree_assign:
707 #if 0
708 if (tree->assign.dst->sdefs != (asymbol *)NULL){
709 fprintf(config.map_file,"%s (%x) ",tree->assign.dst->name,
710 tree->assign.dst->sdefs->value);
711 }
712 else {
713 fprintf(config.map_file,"%s (UNDEFINED)",tree->assign.dst->name);
714 }
715 #endif
716 fprintf(config.map_file,"%s ",tree->assign.dst);
717 exp_print_token(tree->type.node_code);
718 exp_print_tree(tree->assign.src);
719 break;
720 case etree_binary:
721 fprintf(config.map_file,"(");
722 exp_print_tree(tree->binary.lhs);
723 exp_print_token(tree->type.node_code);
724 exp_print_tree(tree->binary.rhs);
725 fprintf(config.map_file,")");
726 break;
727 case etree_trinary:
728 exp_print_tree(tree->trinary.cond);
729 fprintf(config.map_file,"?");
730 exp_print_tree(tree->trinary.lhs);
731 fprintf(config.map_file,":");
732 exp_print_tree(tree->trinary.rhs);
733 break;
734 case etree_unary:
735 exp_print_token(tree->unary.type.node_code);
736 if (tree->unary.child)
737 {
738
739 fprintf(config.map_file,"(");
740 exp_print_tree(tree->unary.child);
741 fprintf(config.map_file,")");
742 }
743
744 break;
745 case etree_undef:
746 fprintf(config.map_file,"????????");
747 break;
748 case etree_name:
749 if (tree->type.node_code == NAME) {
750 fprintf(config.map_file,"%s", tree->name.name);
751 }
752 else {
753 exp_print_token(tree->type.node_code);
754 if (tree->name.name)
755 fprintf(config.map_file,"(%s)", tree->name.name);
756 }
757 break;
758 default:
759 FAIL();
760 break;
761 }
762 }
763
764
765
766
767 bfd_vma
768 DEFUN(exp_get_vma,(tree, def, name, allocation_done),
769 etree_type *tree AND
770 bfd_vma def AND
771 char *name AND
772 lang_phase_type allocation_done)
773 {
774 etree_value_type r;
775
776 if (tree != (etree_type *)NULL) {
777 r = exp_fold_tree_no_dot(tree,
778 abs_output_section,
779 allocation_done);
780 if (r.valid == false && name) {
781 einfo("%F%S Nonconstant expression for %s\n",name);
782 }
783 return r.value;
784 }
785 else {
786 return def;
787 }
788 }
789
790 int
791 DEFUN(exp_get_value_int,(tree,def,name, allocation_done),
792 etree_type *tree AND
793 int def AND
794 char *name AND
795 lang_phase_type allocation_done)
796 {
797 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);
798 }
799
This page took 0.045281 seconds and 4 git commands to generate.