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