Checkpoint before a merge
[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
145static void check(os)
146lang_output_section_statement_type *os;
147{
148 if (os == (lang_output_section_statement_type *)NULL) {
149 info("%F%P undefined section");
150 }
151 if (os->processed == false) {
152 info("%F%P forward reference of section");
153 }
154}
155
156etree_type *exp_intop(value)
157bfd_vma value;
158{
159 etree_type *new = (etree_type *)ldmalloc(sizeof(new->value));
160 new->type.node_code = INT;
161 new->value.value = value;
162 new->type.node_class = etree_value;
163 return new;
164
165}
166
167
168static
169etree_value_type new_rel(value, section)
170bfd_vma value;
171lang_output_section_statement_type *section;
172{
173 etree_value_type new;
174 new.valid = true;
175 new.value = value;
176 new.section = section;
177 return new;
178}
179
180static
181etree_value_type new_rel_from_section(value, section)
182bfd_vma value;
183lang_output_section_statement_type *section;
184{
185 etree_value_type new;
186 new.valid = true;
187 new.value = value;
188 new.section = section;
189 if (new.section != (lang_output_section_statement_type *)NULL) {
190 new.value -= section->bfd_section->vma;
191 }
192 return new;
193}
194
195static etree_value_type
196fold_binary(tree, current_section, allocation_done, dot, dotp)
197etree_type *tree;
198lang_output_section_statement_type *current_section;
199lang_phase_type allocation_done;
200bfd_vma dot;
201bfd_vma *dotp;
202{
203 etree_value_type result;
204
205 result = exp_fold_tree(tree->binary.lhs, current_section,
206 allocation_done, dot, dotp);
207 if (result.valid) {
208 etree_value_type other;
209 other = exp_fold_tree(tree->binary.rhs,
210 current_section,
211 allocation_done, dot,dotp) ;
212 if (other.valid) {
213 /* If values are from different sections, or this is an */
214 /* absolute expression, make both source args absolute */
215 if (result.section != other.section ||
216 current_section == (lang_output_section_statement_type *)NULL) {
217
218 make_abs(&result);
219 make_abs(&other);
220 }
221
222 switch (tree->type.node_code)
223 {
224 case '%':
225 /* Mod, both absolule*/
226
227 if (other.value == 0) {
228 info("%F%S % by zero\n");
229 }
230 result.value %= other.value;
231 break;
232 case '/':
233 if (other.value == 0) {
234 info("%F%S / by zero\n");
235 }
236 result.value /= other.value;
237 break;
238#define BOP(x,y) case x : result.value = result.value y other.value;break;
239 BOP('+',+);
240 BOP('*',*);
241 BOP('-',-);
242 BOP(LSHIFT,<<);
243 BOP(RSHIFT,>>);
244 BOP(EQ,==);
245 BOP(NE,!=);
246 BOP('<',<);
247 BOP('>',>);
248 BOP(LE,<=);
249 BOP(GE,>=);
250 BOP('&',&);
251 BOP('^',^);
252 BOP('|',|);
253 BOP(ANDAND,&&);
254 BOP(OROR,||);
255 default:
256 FAIL();
257 }
258 }
259 }
260 return result;
261}
262etree_value_type invalid()
263{
264 etree_value_type new;
265 new.valid = false;
266 return new;
267}
268
269etree_value_type fold_name(tree, current_section, allocation_done, dot)
270etree_type *tree;
271lang_output_section_statement_type *current_section;
272lang_phase_type allocation_done;
273bfd_vma dot;
274
275{
276 etree_value_type result;
277 switch (tree->type.node_code)
278 {
279 case DEFINED:
280 result.value =
281 ldsym_get_soft(tree->name.name) != (ldsym_type *)NULL;
282 result.section = 0;
283 result.valid = true;
284 break;
285 case NAME:
286 result.valid = false;
287 if (tree->name.name[0] == '.' && tree->name.name[1] == 0) {
288
289 if (allocation_done != lang_first_phase_enum) {
290 result = new_rel_from_section(dot, current_section);
291 }
292 else {
293 result = invalid();
294 }
295 }
296 else {
297 if (allocation_done == lang_final_phase_enum) {
298 ldsym_type *sy = ldsym_get_soft(tree->name.name);
299
300 if (sy) {
301 asymbol **sdefp = sy->sdefs_chain;
302
303 if (sdefp) {
304 asymbol *sdef = *sdefp;
305 if (sdef->section == (asection *)NULL) {
306 /* This is an absolute symbol */
307 result = new_abs(sdef->value);
308 }
309 else {
310 lang_output_section_statement_type *os =
311 lang_output_section_statement_lookup( sdef->section->output_section->name);
312 result = new_rel(sdef->value, os);
313 }
314 }
315 }
316 if (result.valid == false) {
317 info("%F%S: undefined symbol `%s' referenced in expression.\n",
318 tree->name.name);
319 }
320
321 }
322 }
323
324 break;
325
326 case ADDR:
327
328 if (allocation_done != lang_first_phase_enum) {
329 lang_output_section_statement_type *os =
330 lang_output_section_find(tree->name.name);
331 check(os);
332 result = new_rel((bfd_vma)0, os);
333 }
334 else {
335 result = invalid();
336 }
337 break;
338 case SIZEOF:
339 if(allocation_done != lang_first_phase_enum) {
340 lang_output_section_statement_type *os =
341 lang_output_section_find(tree->name.name);
342 check(os);
343 result = new_abs((bfd_vma)(os->bfd_section->size));
344 }
345 else {
346 result = invalid();
347 }
348 break;
349
350 default:
351 FAIL();
352 break;
353 }
354
355 return result;
356}
357etree_value_type exp_fold_tree(tree, current_section, allocation_done,
358 dot, dotp)
359etree_type *tree;
360lang_output_section_statement_type *current_section;
361lang_phase_type allocation_done;
362bfd_vma dot;
363bfd_vma *dotp;
364{
365 etree_value_type result;
366
367 if (tree == (etree_type *)NULL) {
368 result.valid = false;
369 }
370 else {
371 switch (tree->type.node_class)
372 {
373 case etree_value:
374 result = new_rel(tree->value.value, current_section);
375 break;
376 case etree_unary:
377 result = exp_fold_tree(tree->unary.child,
378 current_section,
379 allocation_done, dot, dotp);
380 if (result.valid == true)
381 {
382 switch(tree->type.node_code)
383 {
384 case ALIGN_K:
385 if (allocation_done != lang_first_phase_enum) {
386 result = new_rel_from_section(ALIGN(dot,
387 result.value) ,
388 current_section);
389
390 }
391 else {
392 result.valid = false;
393 }
394 break;
395 case '-':
396 result.value = -result.value;
397 break;
398 case NEXT:
399 result.valid = false;
400 break;
401 default:
402 FAIL();
403 }
404 }
405
406 break;
407 case etree_trinary:
408
409 result = exp_fold_tree(tree->trinary.cond,
410 current_section,
411 allocation_done, dot, dotp);
412 if (result.valid) {
413 result = exp_fold_tree(result.value ?
414 tree->trinary.lhs:tree->trinary.rhs,
415 current_section,
416 allocation_done, dot, dotp);
417 }
418
419 break;
420 case etree_binary:
421 result = fold_binary(tree, current_section, allocation_done,
422 dot, dotp);
423 break;
424 case etree_assign:
425 if (tree->assign.dst[0] == '.' && tree->assign.dst[1] == 0) {
426 /* Assignment to dot can only be done during allocation */
427 if (allocation_done == lang_allocating_phase_enum) {
428 result = exp_fold_tree(tree->assign.src,
429 current_section,
430 lang_allocating_phase_enum, dot, dotp);
431 if (result.valid == false) {
432 info("%F%S invalid assignment to location counter\n");
433 }
434 else {
435 if (current_section ==
436 (lang_output_section_statement_type *)NULL) {
437 info("%F%S assignment to location counter invalid outside of SECTION\n");
438 }
439 else {
440 unsigned long nextdot =result.value +
441 current_section->bfd_section->vma;
442 if (nextdot < dot) {
443 info("%F%S cannot move location counter backwards");
444 }
445 else {
446 *dotp = nextdot;
447 }
448 }
449 }
450 }
451 }
452 else {
453 ldsym_type *sy = ldsym_get(tree->assign.dst);
454
455 /* If this symbol has just been created then we'll place it into
456 * a section of our choice
457 */
458 result = exp_fold_tree(tree->assign.src,
459 current_section, allocation_done,
460 dot, dotp);
461 if (result.valid)
462 {
463 asymbol *def;
464 asymbol **def_ptr = (asymbol **)ldmalloc(sizeof(asymbol **));
465 /* Add this definition to script file */
466 def = (asymbol *)bfd_make_empty_symbol(script_file->the_bfd);
467 *def_ptr = def;
468
469
470 def->value = result.value;
471 if (result.section !=
472 (lang_output_section_statement_type *)NULL) {
473 if (current_section !=
474 (lang_output_section_statement_type *)NULL) {
475
476 def->section = result.section->bfd_section;
477 def->flags = BSF_GLOBAL | BSF_EXPORT;
478 }
479 else {
480 /* Force to absolute */
481 def->value += result.section->bfd_section->vma;
482 def->section = (asection *)NULL;
483 def->flags = BSF_GLOBAL | BSF_EXPORT | BSF_ABSOLUTE;
484 }
485
486
487 }
488 else {
489 def->section = (asection *)NULL;
490 def->flags = BSF_GLOBAL | BSF_EXPORT | BSF_ABSOLUTE;
491 }
492
493
1418c83b 494 def->udata = (PTR)NULL;
2fa0b342
DHW
495 def->name = sy->name;
496 Q_enter_global_ref(def_ptr);
497 }
498
499 }
500
501
502 break;
503 case etree_name:
504 result = fold_name(tree, current_section, allocation_done, dot);
505 break;
506 default:
507 info("%F%S Need more of these %d",tree->type.node_class );
508
509 }
510 }
511
512 return result;
513}
514
515
516etree_value_type exp_fold_tree_no_dot(tree, current_section, allocation_done)
517etree_type *tree;
518lang_output_section_statement_type *current_section;
519lang_phase_type allocation_done;
520{
521return exp_fold_tree(tree, current_section, allocation_done, (bfd_vma)
522 0, (bfd_vma *)NULL);
523}
524
525etree_type *
526exp_binop(code, lhs, rhs)
527int code;
528etree_type *lhs;
529etree_type *rhs;
530{
531 etree_type value, *new;
532 etree_value_type r;
533
534 value.type.node_code = code;
535 value.binary.lhs = lhs;
536 value.binary.rhs = rhs;
537 value.type.node_class = etree_binary;
538 r = exp_fold_tree_no_dot(&value, (lang_output_section_statement_type *)NULL,
539 lang_first_phase_enum );
540 if (r.valid)
541 {
542 return exp_intop(r.value);
543 }
544 new = (etree_type *)ldmalloc(sizeof(new->binary));
545 memcpy((char *)new, (char *)&value, sizeof(new->binary));
546 return new;
547}
548
549etree_type *
550exp_trinop(code, cond, lhs, rhs)
551int code;
552etree_type *cond;
553etree_type *lhs;
554etree_type *rhs;
555{
556 etree_type value, *new;
557 etree_value_type r;
558 value.type.node_code = code;
559 value.trinary.lhs = lhs;
560 value.trinary.cond = cond;
561 value.trinary.rhs = rhs;
562 value.type.node_class = etree_trinary;
563 r= exp_fold_tree_no_dot(&value, (lang_output_section_statement_type
564 *)NULL,lang_first_phase_enum);
565 if (r.valid) {
566 return exp_intop(r.value);
567 }
568 new = (etree_type *)ldmalloc(sizeof(new->trinary));
569 memcpy((char *)new,(char *) &value, sizeof(new->trinary));
570 return new;
571}
572
573
574etree_type *
575exp_unop(code, child)
576int code;
577etree_type *child;
578{
579 etree_type value, *new;
580
581 etree_value_type r;
582 value.unary.type.node_code = code;
583 value.unary.child = child;
584 value.unary.type.node_class = etree_unary;
585r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL,
586 lang_first_phase_enum);
587if (r.valid) {
588 return exp_intop(r.value);
589 }
590 new = (etree_type *)ldmalloc(sizeof(new->unary));
591 memcpy((char *)new, (char *)&value, sizeof(new->unary));
592 return new;
593}
594
595
596etree_type *
597exp_nameop(code, name)
598int code;
599char *name;
600{
601
602 etree_type value, *new;
603
604 etree_value_type r;
605 value.name.type.node_code = code;
606 value.name.name = name;
607 value.name.type.node_class = etree_name;
608
609
610 r = exp_fold_tree_no_dot(&value,(lang_output_section_statement_type *)NULL,
611 lang_first_phase_enum);
612 if (r.valid) {
613 return exp_intop(r.value);
614 }
615 new = (etree_type *)ldmalloc(sizeof(new->name));
616 memcpy((char *)new, (char *)&value, sizeof(new->name));
617 return new;
618
619}
620
621
622
623
624etree_type *
625exp_assop(code, dst, src)
626int code;
627char *dst;
628etree_type *src;
629{
630 etree_type value, *new;
631
632 value.assign.type.node_code = code;
633
634
635 value.assign.src = src;
636 value.assign.dst = dst;
637 value.assign.type.node_class = etree_assign;
638
639#if 0
640 if (exp_fold_tree_no_dot(&value, &result)) {
641 return exp_intop(result);
642 }
643#endif
644 new = (etree_type*)ldmalloc(sizeof(new->assign));
645 memcpy((char *)new, (char *)&value, sizeof(new->assign));
646 return new;
647}
648
649void
650exp_print_tree(outfile, tree)
651FILE *outfile;
652etree_type *tree;
653{
654 switch (tree->type.node_class) {
655 case etree_value:
656 fprintf(outfile,"0x%08lx",(bfd_vma)(tree->value.value));
657 return;
658 case etree_assign:
659#if 0
660 if (tree->assign.dst->sdefs != (asymbol *)NULL){
661 fprintf(outfile,"%s (%x) ",tree->assign.dst->name,
662 tree->assign.dst->sdefs->value);
663 }
664 else {
665 fprintf(outfile,"%s (UNDEFINED)",tree->assign.dst->name);
666 }
667#endif
668 fprintf(outfile,"%s ",tree->assign.dst);
669 exp_print_token(outfile,tree->type.node_code);
670 exp_print_tree(outfile,tree->assign.src);
671 break;
672 case etree_binary:
673 exp_print_tree(outfile,tree->binary.lhs);
674 exp_print_token(outfile,tree->type.node_code);
675 exp_print_tree(outfile,tree->binary.rhs);
676 break;
677 case etree_trinary:
678 exp_print_tree(outfile,tree->trinary.cond);
679 fprintf(outfile,"?");
680 exp_print_tree(outfile,tree->trinary.lhs);
681 fprintf(outfile,":");
682 exp_print_tree(outfile,tree->trinary.rhs);
683 break;
684 case etree_unary:
685 exp_print_token(outfile,tree->unary.type.node_code);
686 fprintf(outfile,"(");
687 exp_print_tree(outfile,tree->unary.child);
688 fprintf(outfile,")");
689 break;
690 case etree_undef:
691 fprintf(outfile,"????????");
692 break;
693 case etree_name:
694 if (tree->type.node_code == NAME) {
695 fprintf(outfile,"%s", tree->name.name);
696 }
697 else {
698 exp_print_token(outfile,tree->type.node_code);
699 fprintf(outfile,"(%s)", tree->name.name);
700 }
701 break;
702 default:
703 FAIL();
704 break;
705 }
706}
707
708
709
710
711bfd_vma
712exp_get_vma(tree, def, name, allocation_done)
713etree_type *tree;
714bfd_vma def;
715char *name;
716lang_phase_type allocation_done;
717{
718 etree_value_type r;
719
720 if (tree != (etree_type *)NULL) {
721 r = exp_fold_tree_no_dot(tree,
722 (lang_output_section_statement_type *)NULL,
723 allocation_done);
724 if (r.valid == false && name) {
725 info("%F%S Nonconstant expression for %s\n",name);
726 }
727 return r.value;
728 }
729 else {
730 return def;
731 }
732}
733
734int
735exp_get_value_int(tree,def,name, allocation_done)
736etree_type *tree;
737int def;
738char *name;
739lang_phase_type allocation_done;
740{
741 return (int)exp_get_vma(tree,(bfd_vma)def,name, allocation_done);
742}
This page took 0.047961 seconds and 4 git commands to generate.