Removed extern declarations
[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( sdef->section->output_section->name);
318 result = new_rel(sdef->value, os);
319 }
320 }
321 }
322 if (result.valid == false) {
323 info("%F%S: undefined symbol `%s' referenced in expression.\n",
324 tree->name.name);
325 }
326
327 }
328 }
329
330 break;
331
332 case ADDR:
333
334 if (allocation_done != lang_first_phase_enum) {
335 lang_output_section_statement_type *os =
336 lang_output_section_find(tree->name.name);
337 check(os,tree->name.name,"ADDR");
338 result = new_rel((bfd_vma)0, os);
339 }
340 else {
341 result = invalid();
342 }
343 break;
344 case SIZEOF:
345 if(allocation_done != lang_first_phase_enum) {
346 lang_output_section_statement_type *os =
347 lang_output_section_find(tree->name.name);
348 check(os,tree->name.name,"SIZEOF");
349 result = new_abs((bfd_vma)(os->bfd_section->size));
350 }
351 else {
352 result = invalid();
353 }
354 break;
355
356 default:
357 FAIL();
358 break;
359 }
360
361 return result;
362 }
363 etree_value_type exp_fold_tree(tree, current_section, allocation_done,
364 dot, dotp)
365 etree_type *tree;
366 lang_output_section_statement_type *current_section;
367 lang_phase_type allocation_done;
368 bfd_vma dot;
369 bfd_vma *dotp;
370 {
371 etree_value_type result;
372
373 if (tree == (etree_type *)NULL) {
374 result.valid = false;
375 }
376 else {
377 switch (tree->type.node_class)
378 {
379 case etree_value:
380 result = new_rel(tree->value.value, current_section);
381 break;
382 case etree_unary:
383 result = exp_fold_tree(tree->unary.child,
384 current_section,
385 allocation_done, dot, dotp);
386 if (result.valid == true)
387 {
388 switch(tree->type.node_code)
389 {
390 case ALIGN_K:
391 if (allocation_done != lang_first_phase_enum) {
392 result = new_rel_from_section(ALIGN(dot,
393 result.value) ,
394 current_section);
395
396 }
397 else {
398 result.valid = false;
399 }
400 break;
401 case '-':
402 result.value = -result.value;
403 break;
404 case NEXT:
405 result.valid = false;
406 break;
407 default:
408 FAIL();
409 }
410 }
411
412 break;
413 case etree_trinary:
414
415 result = exp_fold_tree(tree->trinary.cond,
416 current_section,
417 allocation_done, dot, dotp);
418 if (result.valid) {
419 result = exp_fold_tree(result.value ?
420 tree->trinary.lhs:tree->trinary.rhs,
421 current_section,
422 allocation_done, dot, dotp);
423 }
424
425 break;
426 case etree_binary:
427 result = fold_binary(tree, current_section, allocation_done,
428 dot, dotp);
429 break;
430 case etree_assign:
431 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) {
432 /* Assignment to dot can only be done during allocation */
433 if (allocation_done == lang_allocating_phase_enum) {
434 result = exp_fold_tree(tree->assign.src,
435 current_section,
436 lang_allocating_phase_enum, dot, dotp);
437 if (result.valid == false) {
438 info("%F%S invalid assignment to location counter\n");
439 }
440 else {
441 if (current_section ==
442 (lang_output_section_statement_type *)NULL) {
443 info("%F%S assignment to location counter invalid outside of SECTION\n");
444 }
445 else {
446 unsigned long nextdot =result.value +
447 current_section->bfd_section->vma;
448 if (nextdot < dot) {
449 info("%F%S cannot move location counter backwards");
450 }
451 else {
452 *dotp = nextdot;
453 }
454 }
455 }
456 }
457 }
458 else {
459 ldsym_type *sy = ldsym_get(tree->assign.dst);
460
461 /* If this symbol has just been created then we'll place it into
462 * a section of our choice
463 */
464 result = exp_fold_tree(tree->assign.src,
465 current_section, allocation_done,
466 dot, dotp);
467 if (result.valid)
468 {
469 asymbol *def;
470 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
471 /* Add this definition to script file */
472 def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
473 *def_ptr = def;
474
475
476 def->value = result.value;
477 if (result.section !=
478 (lang_output_section_statement_type *)NULL) {
479 if (current_section !=
480 (lang_output_section_statement_type *)NULL) {
481
482 def->section = result.section->bfd_section;
483 def->flags = BSF_GLOBAL | BSF_EXPORT;
484 }
485 else {
486 /* Force to absolute */
487 def->value += result.section->bfd_section->vma;
488 def->section = (asection *)NULL;
489 def->flags = BSF_GLOBAL | BSF_EXPORT | BSF_ABSOLUTE;
490 }
491
492
493 }
494 else {
495 def->section = (asection *)NULL;
496 def->flags = BSF_GLOBAL | BSF_EXPORT | BSF_ABSOLUTE;
497 }
498
499
500 def->udata = (PTR)NULL;
501 def->name = sy->name;
502 Q_enter_global_ref(def_ptr);
503 }
504
505 }
506
507
508 break;
509 case etree_name:
510 result = fold_name(tree, current_section, allocation_done, dot);
511 break;
512 default:
513 info("%F%S Need more of these %d",tree->type.node_class );
514
515 }
516 }
517
518 return result;
519 }
520
521
522 etree_value_type exp_fold_tree_no_dot(tree, current_section, allocation_done)
523 etree_type *tree;
524 lang_output_section_statement_type *current_section;
525 lang_phase_type allocation_done;
526 {
527 return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
528 0, (bfd_vma *)NULL);
529 }
530
531 etree_type *
532 exp_binop(code, lhs, rhs)
533 int code;
534 etree_type *lhs;
535 etree_type *rhs;
536 {
537 etree_type value, *new;
538 etree_value_type r;
539
540 value.type.node_code = code;
541 value.binary.lhs = lhs;
542 value.binary.rhs = rhs;
543 value.type.node_class = etree_binary;
544 r = exp_fold_tree_no_dot(&value, (lang_output_section_statement_type *)NULL,
545 lang_first_phase_enum );
546 if (r.valid)
547 {
548 return exp_intop(r.value);
549 }
550 new = (etree_type *)ldmalloc(sizeof(new->binary));
551 memcpy((char *)new, (char *)&value, sizeof(new->binary));
552 return new;
553 }
554
555 etree_type *
556 exp_trinop(code, cond, lhs, rhs)
557 int code;
558 etree_type *cond;
559 etree_type *lhs;
560 etree_type *rhs;
561 {
562 etree_type value, *new;
563 etree_value_type r;
564 value.type.node_code = code;
565 value.trinary.lhs = lhs;
566 value.trinary.cond = cond;
567 value.trinary.rhs = rhs;
568 value.type.node_class = etree_trinary;
569 r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type
570 *)NULL,lang_first_phase_enum);
571 if (r.valid) {
572 return exp_intop(r.value);
573 }
574 new = (etree_type *)ldmalloc(sizeof(new->trinary));
575 memcpy((char *)new,(char *) &value, sizeof(new->trinary));
576 return new;
577 }
578
579
580 etree_type *
581 exp_unop(code, child)
582 int code;
583 etree_type *child;
584 {
585 etree_type value, *new;
586
587 etree_value_type r;
588 value.unary.type.node_code = code;
589 value.unary.child = child;
590 value.unary.type.node_class = etree_unary;
591 r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL,
592 lang_first_phase_enum);
593 if (r.valid) {
594 return exp_intop(r.value);
595 }
596 new = (etree_type *)ldmalloc(sizeof(new->unary));
597 memcpy((char *)new, (char *)&value, sizeof(new->unary));
598 return new;
599 }
600
601
602 etree_type *
603 exp_nameop(code, name)
604 int code;
605 char *name;
606 {
607
608 etree_type value, *new;
609
610 etree_value_type r;
611 value.name.type.node_code = code;
612 value.name.name = name;
613 value.name.type.node_class = etree_name;
614
615
616 r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL,
617 lang_first_phase_enum);
618 if (r.valid) {
619 return exp_intop(r.value);
620 }
621 new = (etree_type *)ldmalloc(sizeof(new->name));
622 memcpy((char *)new, (char *)&value, sizeof(new->name));
623 return new;
624
625 }
626
627
628
629
630 etree_type *
631 exp_assop(code, dst, src)
632 int code;
633 char *dst;
634 etree_type *src;
635 {
636 etree_type value, *new;
637
638 value.assign.type.node_code = code;
639
640
641 value.assign.src = src;
642 value.assign.dst = dst;
643 value.assign.type.node_class = etree_assign;
644
645 #if 0
646 if (exp_fold_tree_no_dot(&value, &result)) {
647 return exp_intop(result);
648 }
649 #endif
650 new = (etree_type*)ldmalloc(sizeof(new->assign));
651 memcpy((char *)new, (char *)&value, sizeof(new->assign));
652 return new;
653 }
654
655 void
656 exp_print_tree(outfile, tree)
657 FILE *outfile;
658 etree_type *tree;
659 {
660 switch (tree->type.node_class) {
661 case etree_value:
662 fprintf(outfile,"0x%08lx",(bfd_vma)(tree->value.value));
663 return;
664 case etree_assign:
665 #if 0
666 if (tree->assign.dst->sdefs != (asymbol *)NULL){
667 fprintf(outfile,"%s (%x) ",tree->assign.dst->name,
668 tree->assign.dst->sdefs->value);
669 }
670 else {
671 fprintf(outfile,"%s (UNDEFINED)",tree->assign.dst->name);
672 }
673 #endif
674 fprintf(outfile,"%s ",tree->assign.dst);
675 exp_print_token(outfile,tree->type.node_code);
676 exp_print_tree(outfile,tree->assign.src);
677 break;
678 case etree_binary:
679 exp_print_tree(outfile,tree->binary.lhs);
680 exp_print_token(outfile,tree->type.node_code);
681 exp_print_tree(outfile,tree->binary.rhs);
682 break;
683 case etree_trinary:
684 exp_print_tree(outfile,tree->trinary.cond);
685 fprintf(outfile,"?");
686 exp_print_tree(outfile,tree->trinary.lhs);
687 fprintf(outfile,":");
688 exp_print_tree(outfile,tree->trinary.rhs);
689 break;
690 case etree_unary:
691 exp_print_token(outfile,tree->unary.type.node_code);
692 fprintf(outfile,"(");
693 exp_print_tree(outfile,tree->unary.child);
694 fprintf(outfile,")");
695 break;
696 case etree_undef:
697 fprintf(outfile,"????????");
698 break;
699 case etree_name:
700 if (tree->type.node_code == NAME) {
701 fprintf(outfile,"%s", tree->name.name);
702 }
703 else {
704 exp_print_token(outfile,tree->type.node_code);
705 fprintf(outfile,"(%s)", tree->name.name);
706 }
707 break;
708 default:
709 FAIL();
710 break;
711 }
712 }
713
714
715
716
717 bfd_vma
718 exp_get_vma(tree, def, name, allocation_done)
719 etree_type *tree;
720 bfd_vma def;
721 char *name;
722 lang_phase_type allocation_done;
723 {
724 etree_value_type r;
725
726 if (tree != (etree_type *)NULL) {
727 r = exp_fold_tree_no_dot(tree,
728 (lang_output_section_statement_type *)NULL,
729 allocation_done);
730 if (r.valid == false && name) {
731 info("%F%S Nonconstant expression for %s\n",name);
732 }
733 return r.value;
734 }
735 else {
736 return def;
737 }
738 }
739
740 int
741 exp_get_value_int(tree,def,name, allocation_done)
742 etree_type *tree;
743 int def;
744 char *name;
745 lang_phase_type allocation_done;
746 {
747 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);
748 }
This page took 0.04563 seconds and 4 git commands to generate.