Added some depdenencies and fixed etags
[deliverable/binutils-gdb.git] / ld / ldexp.c
CommitLineData
2fa0b342
DHW
1/* Copyright (C) 1991 Free Software Foundation, Inc.
2
3This file is part of GLD, the Gnu Linker.
4
5GLD is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 1, or (at your option)
8any later version.
9
10GLD is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with GLD; see the file COPYING. If not, write to
17the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18
19/*
20 $Id$
2fa0b342
DHW
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
42extern char *output_filename;
43extern unsigned int undefined_global_sym_count;
44extern unsigned int defined_global_sym_count;
45extern bfd *output_bfd;
46extern size_t largest_section;
47extern lang_statement_list_type file_chain;
48extern args_type command_line;
49extern ld_config_type config;
50
51extern lang_input_statement_type *script_file;
52extern unsigned int defined_global_sym_count;
53
54extern bfd_vma print_dot;
55
56
57static void
58exp_print_token(outfile, code)
59FILE *outfile;
60token_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
123static void
124make_abs(ptr)
125etree_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}
134static
135etree_value_type new_abs(value)
136bfd_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
3a399523
SC
145static void
146DEFUN(check, (os, name, op),
147 lang_output_section_statement_type *os AND
148 CONST char *name AND
149 CONST char *op)
2fa0b342
DHW
150{
151 if (os == (lang_output_section_statement_type *)NULL) {
3a399523 152 info("%F%P %s uses undefined section %s\n", op, name);
2fa0b342
DHW
153 }
154 if (os->processed == false) {
3a399523 155 info("%F%P %s forward reference of section %s\n",op, name);
2fa0b342
DHW
156 }
157}
158
159etree_type *exp_intop(value)
160bfd_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
171static
172etree_value_type new_rel(value, section)
173bfd_vma value;
174lang_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
183static
184etree_value_type new_rel_from_section(value, section)
185bfd_vma value;
186lang_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
198static etree_value_type
199fold_binary(tree, current_section, allocation_done, dot, dotp)
200etree_type *tree;
201lang_output_section_statement_type *current_section;
202lang_phase_type allocation_done;
203bfd_vma dot;
204bfd_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 }
070aa819
SC
262 else {
263 result.valid = false;
264 }
2fa0b342
DHW
265 }
266 return result;
267}
268etree_value_type invalid()
269{
270 etree_value_type new;
271 new.valid = false;
272 return new;
273}
274
275etree_value_type fold_name(tree, current_section, allocation_done, dot)
276etree_type *tree;
277lang_output_section_statement_type *current_section;
278lang_phase_type allocation_done;
279bfd_vma dot;
280
281{
282 etree_value_type result;
283 switch (tree->type.node_code)
ac004870
SC
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 }
2fa0b342
DHW
301 }
302 else {
ac004870
SC
303 if (allocation_done == lang_final_phase_enum) {
304 ldsym_type *sy = ldsym_get_soft(tree->name.name);
2fa0b342 305
ac004870
SC
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 {
b0f36869
SC
325 result = new_abs(sdef->value + (sdef->section ?
326 sdef->section->vma : 0));
ac004870
SC
327 }
328 else {
a37cc0c0 329 result = new_rel(sdef->value + sdef->section->output_offset, os);
ac004870
SC
330 }
331 }
2fa0b342
DHW
332 }
333 }
ac004870
SC
334 if (result.valid == false) {
335 info("%F%S: undefined symbol `%s' referenced in expression.\n",
2fa0b342 336 tree->name.name);
ac004870 337 }
2fa0b342 338
ac004870 339 }
2fa0b342 340 }
2fa0b342 341
ac004870 342 break;
2fa0b342 343
ac004870 344 case ADDR:
2fa0b342 345
ac004870
SC
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;
2fa0b342 367
ac004870
SC
368 default:
369 FAIL();
370 break;
371 }
2fa0b342
DHW
372
373 return result;
374}
375etree_value_type exp_fold_tree(tree, current_section, allocation_done,
376 dot, dotp)
377etree_type *tree;
378lang_output_section_statement_type *current_section;
379lang_phase_type allocation_done;
380bfd_vma dot;
381bfd_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)
d4c02e29
SC
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)
2fa0b342 399 {
d4c02e29
SC
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 }
2fa0b342 431 }
2fa0b342 432
d4c02e29
SC
433 break;
434 case etree_trinary:
2fa0b342 435
d4c02e29 436 result = exp_fold_tree(tree->trinary.cond,
2fa0b342
DHW
437 current_section,
438 allocation_done, dot, dotp);
d4c02e29
SC
439 if (result.valid) {
440 result = exp_fold_tree(result.value ?
441 tree->trinary.lhs:tree->trinary.rhs,
2fa0b342 442 current_section,
d4c02e29
SC
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");
2fa0b342
DHW
460 }
461 else {
d4c02e29
SC
462 if (current_section ==
463 (lang_output_section_statement_type *)NULL) {
464 info("%F%S assignment to location counter invalid outside of SECTION\n");
2fa0b342
DHW
465 }
466 else {
d4c02e29
SC
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 }
2fa0b342
DHW
475 }
476 }
477 }
478 }
d4c02e29
SC
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) {
2fa0b342 502
d4c02e29
SC
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);
2fa0b342 524 }
2fa0b342 525
d4c02e29 526 }
2fa0b342
DHW
527
528
d4c02e29
SC
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 );
2fa0b342 535
d4c02e29 536 }
2fa0b342
DHW
537 }
538
539 return result;
540}
541
542
543etree_value_type exp_fold_tree_no_dot(tree, current_section, allocation_done)
544etree_type *tree;
545lang_output_section_statement_type *current_section;
546lang_phase_type allocation_done;
547{
548return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
549 0, (bfd_vma *)NULL);
550}
551
552etree_type *
553exp_binop(code, lhs, rhs)
554int code;
555etree_type *lhs;
556etree_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
576etree_type *
577exp_trinop(code, cond, lhs, rhs)
578int code;
579etree_type *cond;
580etree_type *lhs;
581etree_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
601etree_type *
602exp_unop(code, child)
603int code;
604etree_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;
612r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL,
613 lang_first_phase_enum);
614if (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
623etree_type *
624exp_nameop(code, name)
625int code;
626char *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
651etree_type *
652exp_assop(code, dst, src)
653int code;
654char *dst;
655etree_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
676void
677exp_print_tree(outfile, tree)
678FILE *outfile;
679etree_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
738bfd_vma
739exp_get_vma(tree, def, name, allocation_done)
740etree_type *tree;
741bfd_vma def;
742char *name;
743lang_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
761int
762exp_get_value_int(tree,def,name, allocation_done)
763etree_type *tree;
764int def;
765char *name;
766lang_phase_type allocation_done;
767{
768 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);
769}
This page took 0.052659 seconds and 4 git commands to generate.