X
[deliverable/binutils-gdb.git] / gas / symbols.c
CommitLineData
252b5132
RH
1/* symbols.c -symbol table-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GAS, the GNU Assembler.
6
7 GAS 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 GAS 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 GAS; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22/* #define DEBUG_SYMS / * to debug symbol list maintenance */
23
24#include <ctype.h>
25
26#include "as.h"
27
28#include "obstack.h" /* For "symbols.h" */
29#include "subsegs.h"
30
31/* This is non-zero if symbols are case sensitive, which is the
32 default. */
33int symbols_case_sensitive = 1;
34
35#ifndef WORKING_DOT_WORD
36extern int new_broken_words;
37#endif
38
39/* symbol-name => struct symbol pointer */
40static struct hash_control *sy_hash;
41
42/* Below are commented in "symbols.h". */
43symbolS *symbol_rootP;
44symbolS *symbol_lastP;
45symbolS abs_symbol;
46
47#ifdef DEBUG_SYMS
48#define debug_verify_symchain verify_symbol_chain
49#else
50#define debug_verify_symchain(root, last) ((void) 0)
51#endif
52
53struct obstack notes;
54
55static void fb_label_init PARAMS ((void));
56static long dollar_label_instance PARAMS ((long));
57static long fb_label_instance PARAMS ((long));
58
59static void print_binary PARAMS ((FILE *, const char *, expressionS *));
60
61/* symbol_new()
62
63 Return a pointer to a new symbol. Die if we can't make a new
64 symbol. Fill in the symbol's values. Add symbol to end of symbol
65 chain.
66
67 This function should be called in the general case of creating a
68 symbol. However, if the output file symbol table has already been
69 set, and you are certain that this symbol won't be wanted in the
70 output file, you can call symbol_create. */
71
72symbolS *
73symbol_new (name, segment, valu, frag)
74 const char *name;
75 segT segment;
76 valueT valu;
77 fragS *frag;
78{
79 symbolS *symbolP = symbol_create (name, segment, valu, frag);
80
81 /*
82 * Link to end of symbol chain.
83 */
84#ifdef BFD_ASSEMBLER
85 {
86 extern int symbol_table_frozen;
87 if (symbol_table_frozen)
88 abort ();
89 }
90#endif
91 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
92
93 return symbolP;
94}
95
96symbolS *
97symbol_create (name, segment, valu, frag)
98 const char *name; /* It is copied, the caller can destroy/modify */
99 segT segment; /* Segment identifier (SEG_<something>) */
100 valueT valu; /* Symbol value */
101 fragS *frag; /* Associated fragment */
102{
103 unsigned int name_length;
104 char *preserved_copy_of_name;
105 symbolS *symbolP;
106
107 name_length = strlen (name) + 1; /* +1 for \0 */
108 obstack_grow (&notes, name, name_length);
109 preserved_copy_of_name = obstack_finish (&notes);
110#ifdef STRIP_UNDERSCORE
111 if (preserved_copy_of_name[0] == '_')
112 preserved_copy_of_name++;
113#endif
114
115#ifdef tc_canonicalize_symbol_name
116 preserved_copy_of_name =
117 tc_canonicalize_symbol_name (preserved_copy_of_name);
118#endif
119
120 if (! symbols_case_sensitive)
121 {
122 unsigned char *s;
123
124 for (s = (unsigned char *) preserved_copy_of_name; *s != '\0'; s++)
125 if (islower (*s))
126 *s = toupper (*s);
127 }
128
129 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
130
131 /* symbol must be born in some fixed state. This seems as good as any. */
132 memset (symbolP, 0, sizeof (symbolS));
133
134#ifdef BFD_ASSEMBLER
135 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
136 if (symbolP->bsym == NULL)
137 as_perror ("%s", "bfd_make_empty_symbol");
138 symbolP->bsym->udata.p = (PTR) symbolP;
139#endif
140 S_SET_NAME (symbolP, preserved_copy_of_name);
141
142 S_SET_SEGMENT (symbolP, segment);
143 S_SET_VALUE (symbolP, valu);
144 symbol_clear_list_pointers (symbolP);
145
146 symbolP->sy_frag = frag;
147#ifndef BFD_ASSEMBLER
148 symbolP->sy_number = ~0;
149 symbolP->sy_name_offset = (unsigned int) ~0;
150#endif
151
152 obj_symbol_new_hook (symbolP);
153
154#ifdef tc_symbol_new_hook
155 tc_symbol_new_hook (symbolP);
156#endif
157
158 return symbolP;
159}
160\f
161
162/*
163 * colon()
164 *
165 * We have just seen "<name>:".
166 * Creates a struct symbol unless it already exists.
167 *
168 * Gripes if we are redefining a symbol incompatibly (and ignores it).
169 *
170 */
171symbolS *
172colon (sym_name) /* just seen "x:" - rattle symbols & frags */
173 const char *sym_name; /* symbol name, as a cannonical string */
174 /* We copy this string: OK to alter later. */
175{
176 register symbolS *symbolP; /* symbol we are working with */
177
178 /* Sun local labels go out of scope whenever a non-local symbol is
179 defined. */
180 if (LOCAL_LABELS_DOLLAR)
181 {
182 int local;
183
184#ifdef BFD_ASSEMBLER
185 local = bfd_is_local_label_name (stdoutput, sym_name);
186#else
187 local = LOCAL_LABEL (sym_name);
188#endif
189
190 if (! local)
191 dollar_label_clear ();
192 }
193
194#ifndef WORKING_DOT_WORD
195 if (new_broken_words)
196 {
197 struct broken_word *a;
198 int possible_bytes;
199 fragS *frag_tmp;
200 char *frag_opcode;
201
202 extern const int md_short_jump_size;
203 extern const int md_long_jump_size;
204 possible_bytes = (md_short_jump_size
205 + new_broken_words * md_long_jump_size);
206
207 frag_tmp = frag_now;
208 frag_opcode = frag_var (rs_broken_word,
209 possible_bytes,
210 possible_bytes,
211 (relax_substateT) 0,
212 (symbolS *) broken_words,
213 (offsetT) 0,
214 NULL);
215
216 /* We want to store the pointer to where to insert the jump table in the
217 fr_opcode of the rs_broken_word frag. This requires a little
218 hackery. */
219 while (frag_tmp
220 && (frag_tmp->fr_type != rs_broken_word
221 || frag_tmp->fr_opcode))
222 frag_tmp = frag_tmp->fr_next;
223 know (frag_tmp);
224 frag_tmp->fr_opcode = frag_opcode;
225 new_broken_words = 0;
226
227 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
228 a->dispfrag = frag_tmp;
229 }
230#endif /* WORKING_DOT_WORD */
231
232 if ((symbolP = symbol_find (sym_name)) != 0)
233 {
234#ifdef RESOLVE_SYMBOL_REDEFINITION
235 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
236 return symbolP;
237#endif
238 /*
239 * Now check for undefined symbols
240 */
241 if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
242 {
243 if (S_GET_VALUE (symbolP) == 0)
244 {
245 symbolP->sy_frag = frag_now;
246#ifdef OBJ_VMS
247 S_SET_OTHER(symbolP, const_flag);
248#endif
249 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
250 S_SET_SEGMENT (symbolP, now_seg);
251#ifdef N_UNDF
252 know (N_UNDF == 0);
253#endif /* if we have one, it better be zero. */
254
255 }
256 else
257 {
258 /*
259 * There are still several cases to check:
260 * A .comm/.lcomm symbol being redefined as
261 * initialized data is OK
262 * A .comm/.lcomm symbol being redefined with
263 * a larger size is also OK
264 *
265 * This only used to be allowed on VMS gas, but Sun cc
266 * on the sparc also depends on it.
267 */
268
269 if (((!S_IS_DEBUG (symbolP)
270 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
271 && S_IS_EXTERNAL (symbolP))
272 || S_GET_SEGMENT (symbolP) == bss_section)
273 && (now_seg == data_section
274 || now_seg == S_GET_SEGMENT (symbolP)))
275 {
276 /*
277 * Select which of the 2 cases this is
278 */
279 if (now_seg != data_section)
280 {
281 /*
282 * New .comm for prev .comm symbol.
283 * If the new size is larger we just
284 * change its value. If the new size
285 * is smaller, we ignore this symbol
286 */
287 if (S_GET_VALUE (symbolP)
288 < ((unsigned) frag_now_fix ()))
289 {
290 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
291 }
292 }
293 else
294 {
295 /* It is a .comm/.lcomm being converted to initialized
296 data. */
297 symbolP->sy_frag = frag_now;
298#ifdef OBJ_VMS
299 S_SET_OTHER(symbolP, const_flag);
300#endif
301 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
302 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
303 }
304 }
305 else
306 {
307#if defined (S_GET_OTHER) && defined (S_GET_DESC)
308 as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld."),
309 sym_name,
310 segment_name (S_GET_SEGMENT (symbolP)),
311 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
312 (long) S_GET_VALUE (symbolP));
313#else
314 as_fatal (_("Symbol \"%s\" is already defined as \"%s\"/%ld."),
315 sym_name,
316 segment_name (S_GET_SEGMENT (symbolP)),
317 (long) S_GET_VALUE (symbolP));
318#endif
319 }
320 } /* if the undefined symbol has no value */
321 }
322 else
323 {
324 /* Don't blow up if the definition is the same */
325 if (!(frag_now == symbolP->sy_frag
326 && S_GET_VALUE (symbolP) == frag_now_fix ()
327 && S_GET_SEGMENT (symbolP) == now_seg))
328 as_fatal (_("Symbol %s already defined."), sym_name);
329 } /* if this symbol is not yet defined */
330
331 }
332 else
333 {
334 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
335 frag_now);
336#ifdef OBJ_VMS
337 S_SET_OTHER (symbolP, const_flag);
338#endif /* OBJ_VMS */
339
340 symbol_table_insert (symbolP);
341 } /* if we have seen this symbol before */
342
343 if (mri_common_symbol != NULL)
344 {
345 /* This symbol is actually being defined within an MRI common
346 section. This requires special handling. */
347 symbolP->sy_value.X_op = O_symbol;
348 symbolP->sy_value.X_add_symbol = mri_common_symbol;
349 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
350 symbolP->sy_frag = &zero_address_frag;
351 S_SET_SEGMENT (symbolP, expr_section);
352 symbolP->sy_mri_common = 1;
353 }
354
355#ifdef tc_frob_label
356 tc_frob_label (symbolP);
357#endif
358#ifdef obj_frob_label
359 obj_frob_label (symbolP);
360#endif
361
362 return symbolP;
363}
364\f
365
366/*
367 * symbol_table_insert()
368 *
369 * Die if we can't insert the symbol.
370 *
371 */
372
373void
374symbol_table_insert (symbolP)
375 symbolS *symbolP;
376{
377 register const char *error_string;
378
379 know (symbolP);
380 know (S_GET_NAME (symbolP));
381
382 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
383 {
384 as_fatal (_("Inserting \"%s\" into symbol table failed: %s"),
385 S_GET_NAME (symbolP), error_string);
386 } /* on error */
387} /* symbol_table_insert() */
388\f
389/*
390 * symbol_find_or_make()
391 *
392 * If a symbol name does not exist, create it as undefined, and insert
393 * it into the symbol table. Return a pointer to it.
394 */
395symbolS *
396symbol_find_or_make (name)
397 const char *name;
398{
399 register symbolS *symbolP;
400
401 symbolP = symbol_find (name);
402
403 if (symbolP == NULL)
404 {
405 symbolP = symbol_make (name);
406
407 symbol_table_insert (symbolP);
408 } /* if symbol wasn't found */
409
410 return (symbolP);
411} /* symbol_find_or_make() */
412
413symbolS *
414symbol_make (name)
415 CONST char *name;
416{
417 symbolS *symbolP;
418
419 /* Let the machine description default it, e.g. for register names. */
420 symbolP = md_undefined_symbol ((char *) name);
421
422 if (!symbolP)
423 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
424
425 return (symbolP);
426} /* symbol_make() */
427
428/*
429 * symbol_find()
430 *
431 * Implement symbol table lookup.
432 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
433 * Out: NULL if the name was not in the symbol table, else the address
434 * of a struct symbol associated with that name.
435 */
436
437symbolS *
438symbol_find (name)
439 CONST char *name;
440{
441#ifdef STRIP_UNDERSCORE
442 return (symbol_find_base (name, 1));
443#else /* STRIP_UNDERSCORE */
444 return (symbol_find_base (name, 0));
445#endif /* STRIP_UNDERSCORE */
446} /* symbol_find() */
447
448symbolS *
449symbol_find_base (name, strip_underscore)
450 CONST char *name;
451 int strip_underscore;
452{
453 if (strip_underscore && *name == '_')
454 name++;
455
456#ifdef tc_canonicalize_symbol_name
457 {
458 char *copy;
03987ced 459 size_t len = strlen (name) + 1;
252b5132 460
03987ced
RH
461 copy = (char *) alloca (len);
462 memcpy (copy, name, len);
252b5132
RH
463 name = tc_canonicalize_symbol_name (copy);
464 }
465#endif
466
467 if (! symbols_case_sensitive)
468 {
03987ced
RH
469 char *copy;
470 const char *orig;
471 unsigned char c;
472
473 orig = name;
474 name = copy = (char *) alloca (strlen (name) + 1);
475
476 while ((c = *orig++) != '\0')
477 {
478 if (islower (c))
479 c = toupper (c);
480 *copy++ = c;
481 }
482 *copy = '\0';
252b5132
RH
483 }
484
485 return ((symbolS *) hash_find (sy_hash, name));
486}
487
488/*
489 * Once upon a time, symbols were kept in a singly linked list. At
490 * least coff needs to be able to rearrange them from time to time, for
491 * which a doubly linked list is much more convenient. Loic did these
492 * as macros which seemed dangerous to me so they're now functions.
493 * xoxorich.
494 */
495
496/* Link symbol ADDME after symbol TARGET in the chain. */
497void
498symbol_append (addme, target, rootPP, lastPP)
499 symbolS *addme;
500 symbolS *target;
501 symbolS **rootPP;
502 symbolS **lastPP;
503{
504 if (target == NULL)
505 {
506 know (*rootPP == NULL);
507 know (*lastPP == NULL);
508 addme->sy_next = NULL;
509#ifdef SYMBOLS_NEED_BACKPOINTERS
510 addme->sy_previous = NULL;
511#endif
512 *rootPP = addme;
513 *lastPP = addme;
514 return;
515 } /* if the list is empty */
516
517 if (target->sy_next != NULL)
518 {
519#ifdef SYMBOLS_NEED_BACKPOINTERS
520 target->sy_next->sy_previous = addme;
521#endif /* SYMBOLS_NEED_BACKPOINTERS */
522 }
523 else
524 {
525 know (*lastPP == target);
526 *lastPP = addme;
527 } /* if we have a next */
528
529 addme->sy_next = target->sy_next;
530 target->sy_next = addme;
531
532#ifdef SYMBOLS_NEED_BACKPOINTERS
533 addme->sy_previous = target;
534#endif /* SYMBOLS_NEED_BACKPOINTERS */
535
536 debug_verify_symchain (symbol_rootP, symbol_lastP);
537}
538
539/* Set the chain pointers of SYMBOL to null. */
540void
541symbol_clear_list_pointers (symbolP)
542 symbolS *symbolP;
543{
544 symbolP->sy_next = NULL;
545#ifdef SYMBOLS_NEED_BACKPOINTERS
546 symbolP->sy_previous = NULL;
547#endif
548}
549
550#ifdef SYMBOLS_NEED_BACKPOINTERS
551/* Remove SYMBOLP from the list. */
552void
553symbol_remove (symbolP, rootPP, lastPP)
554 symbolS *symbolP;
555 symbolS **rootPP;
556 symbolS **lastPP;
557{
558 if (symbolP == *rootPP)
559 {
560 *rootPP = symbolP->sy_next;
561 } /* if it was the root */
562
563 if (symbolP == *lastPP)
564 {
565 *lastPP = symbolP->sy_previous;
566 } /* if it was the tail */
567
568 if (symbolP->sy_next != NULL)
569 {
570 symbolP->sy_next->sy_previous = symbolP->sy_previous;
571 } /* if not last */
572
573 if (symbolP->sy_previous != NULL)
574 {
575 symbolP->sy_previous->sy_next = symbolP->sy_next;
576 } /* if not first */
577
578 debug_verify_symchain (*rootPP, *lastPP);
579}
580
581/* Link symbol ADDME before symbol TARGET in the chain. */
582void
583symbol_insert (addme, target, rootPP, lastPP)
584 symbolS *addme;
585 symbolS *target;
586 symbolS **rootPP;
587 symbolS **lastPP;
588{
589 if (target->sy_previous != NULL)
590 {
591 target->sy_previous->sy_next = addme;
592 }
593 else
594 {
595 know (*rootPP == target);
596 *rootPP = addme;
597 } /* if not first */
598
599 addme->sy_previous = target->sy_previous;
600 target->sy_previous = addme;
601 addme->sy_next = target;
602
603 debug_verify_symchain (*rootPP, *lastPP);
604}
605
606#endif /* SYMBOLS_NEED_BACKPOINTERS */
607
608void
609verify_symbol_chain (rootP, lastP)
610 symbolS *rootP;
611 symbolS *lastP;
612{
613 symbolS *symbolP = rootP;
614
615 if (symbolP == NULL)
616 return;
617
618 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
619 {
620#ifdef SYMBOLS_NEED_BACKPOINTERS
621 assert (symbolP->sy_next->sy_previous == symbolP);
622#else
623 /* Walk the list anyways, to make sure pointers are still good. */
624 ;
625#endif /* SYMBOLS_NEED_BACKPOINTERS */
626 }
627
628 assert (lastP == symbolP);
629}
630
631void
632verify_symbol_chain_2 (sym)
633 symbolS *sym;
634{
635 symbolS *p = sym, *n = sym;
636#ifdef SYMBOLS_NEED_BACKPOINTERS
637 while (symbol_previous (p))
638 p = symbol_previous (p);
639#endif
640 while (symbol_next (n))
641 n = symbol_next (n);
642 verify_symbol_chain (p, n);
643}
644
645/* Resolve the value of a symbol. This is called during the final
646 pass over the symbol table to resolve any symbols with complex
647 values. */
648
649valueT
650resolve_symbol_value (symp, finalize)
651 symbolS *symp;
652 int finalize;
653{
654 int resolved;
655 valueT final_val;
656 segT final_seg;
657
658 if (symp->sy_resolved)
659 {
660 if (symp->sy_value.X_op == O_constant)
661 return (valueT) symp->sy_value.X_add_number;
662 else
663 return 0;
664 }
665
666 resolved = 0;
667 final_seg = S_GET_SEGMENT (symp);
668
669 if (symp->sy_resolving)
670 {
671 if (finalize)
672 as_bad (_("Symbol definition loop encountered at %s"), S_GET_NAME (symp));
673 final_val = 0;
674 resolved = 1;
675 }
676 else
677 {
678 symbolS *add_symbol, *op_symbol;
679 offsetT left, right;
680 segT seg_left, seg_right;
681 operatorT op;
682
683 symp->sy_resolving = 1;
684
685 /* Help out with CSE. */
686 add_symbol = symp->sy_value.X_add_symbol;
687 op_symbol = symp->sy_value.X_op_symbol;
688 final_val = symp->sy_value.X_add_number;
689 op = symp->sy_value.X_op;
690
691 switch (op)
692 {
693 default:
694 BAD_CASE (op);
695 break;
696
697 case O_absent:
698 final_val = 0;
699 /* Fall through. */
700
701 case O_constant:
702 final_val += symp->sy_frag->fr_address;
703 if (final_seg == expr_section)
704 final_seg = absolute_section;
705 resolved = 1;
706 break;
707
708 case O_symbol:
709 case O_symbol_rva:
710 left = resolve_symbol_value (add_symbol, finalize);
711 do_symbol:
712
713 if (symp->sy_mri_common)
714 {
715 /* This is a symbol inside an MRI common section. The
716 relocation routines are going to handle it specially.
717 Don't change the value. */
718 resolved = add_symbol->sy_resolved;
719 break;
720 }
721
722 if (finalize && final_val == 0)
723 copy_symbol_attributes (symp, add_symbol);
724
725 /* If we have equated this symbol to an undefined symbol, we
726 keep X_op set to O_symbol, and we don't change
727 X_add_number. This permits the routine which writes out
728 relocation to detect this case, and convert the
729 relocation to be against the symbol to which this symbol
730 is equated. */
731 if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
732 {
733 if (finalize)
734 {
735 S_SET_SEGMENT (symp, S_GET_SEGMENT (add_symbol));
736 symp->sy_value.X_op = O_symbol;
737 symp->sy_value.X_add_symbol = add_symbol;
738 symp->sy_value.X_add_number = final_val;
739 }
740 final_val = 0;
741 resolved = add_symbol->sy_resolved;
742 goto exit_dont_set_value;
743 }
744 else
745 {
746 final_val += symp->sy_frag->fr_address + left;
747 if (final_seg == expr_section || final_seg == undefined_section)
748 final_seg = S_GET_SEGMENT (add_symbol);
749 }
750
751 resolved = add_symbol->sy_resolved;
752 break;
753
754 case O_uminus:
755 case O_bit_not:
756 case O_logical_not:
757 left = resolve_symbol_value (add_symbol, finalize);
758
759 if (op == O_uminus)
760 left = -left;
761 else if (op == O_logical_not)
762 left = !left;
763 else
764 left = ~left;
765
766 final_val += left + symp->sy_frag->fr_address;
767 if (final_seg == expr_section || final_seg == undefined_section)
768 final_seg = absolute_section;
769
770 resolved = add_symbol->sy_resolved;
771 break;
772
773 case O_multiply:
774 case O_divide:
775 case O_modulus:
776 case O_left_shift:
777 case O_right_shift:
778 case O_bit_inclusive_or:
779 case O_bit_or_not:
780 case O_bit_exclusive_or:
781 case O_bit_and:
782 case O_add:
783 case O_subtract:
784 case O_eq:
785 case O_ne:
786 case O_lt:
787 case O_le:
788 case O_ge:
789 case O_gt:
790 case O_logical_and:
791 case O_logical_or:
792 left = resolve_symbol_value (add_symbol, finalize);
793 right = resolve_symbol_value (op_symbol, finalize);
794 seg_left = S_GET_SEGMENT (add_symbol);
795 seg_right = S_GET_SEGMENT (op_symbol);
796
797 /* Simplify addition or subtraction of a constant by folding the
798 constant into X_add_number. */
799 if (op == O_add || op == O_subtract)
800 {
801 if (seg_right == absolute_section)
802 {
803 if (op == O_add)
804 final_val += right;
805 else
806 final_val -= right;
807 op = O_symbol;
808 op_symbol = NULL;
809 goto do_symbol;
810 }
811 else if (seg_left == absolute_section && op == O_add)
812 {
813 op = O_symbol;
814 final_val += left;
815 add_symbol = op_symbol;
816 left = right;
817 op_symbol = NULL;
818 goto do_symbol;
819 }
820 }
821
822 /* Subtraction is permitted if both operands are in the same
823 section. Otherwise, both operands must be absolute. We
824 already handled the case of addition or subtraction of a
825 constant above. This will probably need to be changed
826 for an object file format which supports arbitrary
827 expressions, such as IEEE-695. */
828 /* Don't emit messages unless we're finalizing the symbol value,
829 otherwise we may get the same message multiple times. */
830 if ((seg_left != absolute_section || seg_right != absolute_section)
831 && (op != O_subtract || seg_left != seg_right)
832 && finalize)
833 {
834 char *file;
835 unsigned int line;
836
837 if (expr_symbol_where (symp, &file, &line))
838 {
839 if (seg_left == undefined_section)
840 as_bad_where (file, line,
841 _("undefined symbol %s in operation"),
842 S_GET_NAME (symp->sy_value.X_add_symbol));
843 if (seg_right == undefined_section)
844 as_bad_where (file, line,
845 _("undefined symbol %s in operation"),
846 S_GET_NAME (symp->sy_value.X_op_symbol));
847 if (seg_left != undefined_section
848 && seg_right != undefined_section)
849 as_bad_where (file, line, _("invalid section for operation"));
850 }
851 else
852 {
853 if (seg_left == undefined_section)
854 as_bad (_("undefined symbol %s in operation setting %s"),
855 S_GET_NAME (symp->sy_value.X_add_symbol),
856 S_GET_NAME (symp));
857 if (seg_right == undefined_section)
858 as_bad (_("undefined symbol %s in operation setting %s"),
859 S_GET_NAME (symp->sy_value.X_op_symbol),
860 S_GET_NAME (symp));
861 if (seg_left != undefined_section
862 && seg_right != undefined_section)
863 as_bad (_("invalid section for operation setting %s"),
864 S_GET_NAME (symp));
865 }
866 }
867
868 /* Check for division by zero. */
869 if ((op == O_divide || op == O_modulus) && right == 0)
870 {
871 /* If seg_right is not absolute_section, then we've
872 already issued a warning about using a bad symbol. */
873 if (seg_right == absolute_section && finalize)
874 {
875 char *file;
876 unsigned int line;
877
878 if (expr_symbol_where (symp, &file, &line))
879 as_bad_where (file, line, _("division by zero"));
880 else
881 as_bad (_("division by zero when setting %s"),
882 S_GET_NAME (symp));
883 }
884
885 right = 1;
886 }
887
888 switch (symp->sy_value.X_op)
889 {
890 case O_multiply: left *= right; break;
891 case O_divide: left /= right; break;
892 case O_modulus: left %= right; break;
893 case O_left_shift: left <<= right; break;
894 case O_right_shift: left >>= right; break;
895 case O_bit_inclusive_or: left |= right; break;
896 case O_bit_or_not: left |= ~right; break;
897 case O_bit_exclusive_or: left ^= right; break;
898 case O_bit_and: left &= right; break;
899 case O_add: left += right; break;
900 case O_subtract: left -= right; break;
901 case O_eq: left = left == right ? ~ (offsetT) 0 : 0; break;
902 case O_ne: left = left != right ? ~ (offsetT) 0 : 0; break;
903 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
904 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
905 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
906 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
907 case O_logical_and: left = left && right; break;
908 case O_logical_or: left = left || right; break;
909 default: abort ();
910 }
911
912 final_val += symp->sy_frag->fr_address + left;
913 if (final_seg == expr_section || final_seg == undefined_section)
914 final_seg = absolute_section;
915 resolved = (add_symbol->sy_resolved && op_symbol->sy_resolved);
916 break;
917
918 case O_register:
919 case O_big:
920 case O_illegal:
921 /* Give an error (below) if not in expr_section. We don't
922 want to worry about expr_section symbols, because they
923 are fictional (they are created as part of expression
924 resolution), and any problems may not actually mean
925 anything. */
926 break;
927 }
928
929 symp->sy_resolving = 0;
930 }
931
932 if (finalize)
933 {
934 S_SET_VALUE (symp, final_val);
935
936#if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
937 /* The old a.out backend does not handle S_SET_SEGMENT correctly
938 for a stab symbol, so we use this bad hack. */
939 if (final_seg != S_GET_SEGMENT (symp))
940#endif
941 S_SET_SEGMENT (symp, final_seg);
942 }
943
944exit_dont_set_value:
945 /* Don't worry if we can't resolve an expr_section symbol. */
946 if (finalize)
947 {
948 if (resolved)
949 symp->sy_resolved = 1;
950 else if (S_GET_SEGMENT (symp) != expr_section)
951 {
952 as_bad (_("can't resolve value for symbol \"%s\""), S_GET_NAME (symp));
953 symp->sy_resolved = 1;
954 }
955 }
956
957 return final_val;
958}
959
960/* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
961 They are *really* local. That is, they go out of scope whenever we see a
962 label that isn't local. Also, like fb labels, there can be multiple
963 instances of a dollar label. Therefor, we name encode each instance with
964 the instance number, keep a list of defined symbols separate from the real
965 symbol table, and we treat these buggers as a sparse array. */
966
967static long *dollar_labels;
968static long *dollar_label_instances;
969static char *dollar_label_defines;
970static unsigned long dollar_label_count;
971static unsigned long dollar_label_max;
972
973int
974dollar_label_defined (label)
975 long label;
976{
977 long *i;
978
979 know ((dollar_labels != NULL) || (dollar_label_count == 0));
980
981 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
982 if (*i == label)
983 return dollar_label_defines[i - dollar_labels];
984
985 /* if we get here, label isn't defined */
986 return 0;
987} /* dollar_label_defined() */
988
989static long
990dollar_label_instance (label)
991 long label;
992{
993 long *i;
994
995 know ((dollar_labels != NULL) || (dollar_label_count == 0));
996
997 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
998 if (*i == label)
999 return (dollar_label_instances[i - dollar_labels]);
1000
1001 /* If we get here, we haven't seen the label before, therefore its instance
1002 count is zero. */
1003 return 0;
1004}
1005
1006void
1007dollar_label_clear ()
1008{
1009 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1010}
1011
1012#define DOLLAR_LABEL_BUMP_BY 10
1013
1014void
1015define_dollar_label (label)
1016 long label;
1017{
1018 long *i;
1019
1020 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1021 if (*i == label)
1022 {
1023 ++dollar_label_instances[i - dollar_labels];
1024 dollar_label_defines[i - dollar_labels] = 1;
1025 return;
1026 }
1027
1028 /* if we get to here, we don't have label listed yet. */
1029
1030 if (dollar_labels == NULL)
1031 {
1032 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1033 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1034 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1035 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1036 dollar_label_count = 0;
1037 }
1038 else if (dollar_label_count == dollar_label_max)
1039 {
1040 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1041 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1042 dollar_label_max * sizeof (long));
1043 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1044 dollar_label_max * sizeof (long));
1045 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1046 } /* if we needed to grow */
1047
1048 dollar_labels[dollar_label_count] = label;
1049 dollar_label_instances[dollar_label_count] = 1;
1050 dollar_label_defines[dollar_label_count] = 1;
1051 ++dollar_label_count;
1052}
1053
1054/*
1055 * dollar_label_name()
1056 *
1057 * Caller must copy returned name: we re-use the area for the next name.
1058 *
1059 * The mth occurence of label n: is turned into the symbol "Ln^Am"
1060 * where n is the label number and m is the instance number. "L" makes
1061 * it a label discarded unless debugging and "^A"('\1') ensures no
1062 * ordinary symbol SHOULD get the same name as a local label
1063 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1064 *
1065 * fb labels get the same treatment, except that ^B is used in place of ^A.
1066 */
1067
1068char * /* Return local label name. */
1069dollar_label_name (n, augend)
1070 register long n; /* we just saw "n$:" : n a number */
1071 register int augend; /* 0 for current instance, 1 for new instance */
1072{
1073 long i;
1074 /* Returned to caller, then copied. used for created names ("4f") */
1075 static char symbol_name_build[24];
1076 register char *p;
1077 register char *q;
1078 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1079
1080 know (n >= 0);
1081 know (augend == 0 || augend == 1);
1082 p = symbol_name_build;
1083 *p++ = 'L';
1084
1085 /* Next code just does sprintf( {}, "%d", n); */
1086 /* label number */
1087 q = symbol_name_temporary;
1088 for (*q++ = 0, i = n; i; ++q)
1089 {
1090 *q = i % 10 + '0';
1091 i /= 10;
1092 }
1093 while ((*p = *--q) != '\0')
1094 ++p;
1095
1096 *p++ = 1; /* ^A */
1097
1098 /* instance number */
1099 q = symbol_name_temporary;
1100 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1101 {
1102 *q = i % 10 + '0';
1103 i /= 10;
1104 }
1105 while ((*p++ = *--q) != '\0');;
1106
1107 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1108 return symbol_name_build;
1109}
1110
1111/*
1112 * Sombody else's idea of local labels. They are made by "n:" where n
1113 * is any decimal digit. Refer to them with
1114 * "nb" for previous (backward) n:
1115 * or "nf" for next (forward) n:.
1116 *
1117 * We do a little better and let n be any number, not just a single digit, but
1118 * since the other guy's assembler only does ten, we treat the first ten
1119 * specially.
1120 *
1121 * Like someone else's assembler, we have one set of local label counters for
1122 * entire assembly, not one set per (sub)segment like in most assemblers. This
1123 * implies that one can refer to a label in another segment, and indeed some
1124 * crufty compilers have done just that.
1125 *
1126 * Since there could be a LOT of these things, treat them as a sparse array.
1127 */
1128
1129#define FB_LABEL_SPECIAL (10)
1130
1131static long fb_low_counter[FB_LABEL_SPECIAL];
1132static long *fb_labels;
1133static long *fb_label_instances;
1134static long fb_label_count;
1135static long fb_label_max;
1136
1137/* this must be more than FB_LABEL_SPECIAL */
1138#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1139
1140static void
1141fb_label_init ()
1142{
1143 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1144} /* fb_label_init() */
1145
1146/* add one to the instance number of this fb label */
1147void
1148fb_label_instance_inc (label)
1149 long label;
1150{
1151 long *i;
1152
1153 if (label < FB_LABEL_SPECIAL)
1154 {
1155 ++fb_low_counter[label];
1156 return;
1157 }
1158
1159 if (fb_labels != NULL)
1160 {
1161 for (i = fb_labels + FB_LABEL_SPECIAL;
1162 i < fb_labels + fb_label_count; ++i)
1163 {
1164 if (*i == label)
1165 {
1166 ++fb_label_instances[i - fb_labels];
1167 return;
1168 } /* if we find it */
1169 } /* for each existing label */
1170 }
1171
1172 /* if we get to here, we don't have label listed yet. */
1173
1174 if (fb_labels == NULL)
1175 {
1176 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1177 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1178 fb_label_max = FB_LABEL_BUMP_BY;
1179 fb_label_count = FB_LABEL_SPECIAL;
1180
1181 }
1182 else if (fb_label_count == fb_label_max)
1183 {
1184 fb_label_max += FB_LABEL_BUMP_BY;
1185 fb_labels = (long *) xrealloc ((char *) fb_labels,
1186 fb_label_max * sizeof (long));
1187 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1188 fb_label_max * sizeof (long));
1189 } /* if we needed to grow */
1190
1191 fb_labels[fb_label_count] = label;
1192 fb_label_instances[fb_label_count] = 1;
1193 ++fb_label_count;
1194}
1195
1196static long
1197fb_label_instance (label)
1198 long label;
1199{
1200 long *i;
1201
1202 if (label < FB_LABEL_SPECIAL)
1203 {
1204 return (fb_low_counter[label]);
1205 }
1206
1207 if (fb_labels != NULL)
1208 {
1209 for (i = fb_labels + FB_LABEL_SPECIAL;
1210 i < fb_labels + fb_label_count; ++i)
1211 {
1212 if (*i == label)
1213 {
1214 return (fb_label_instances[i - fb_labels]);
1215 } /* if we find it */
1216 } /* for each existing label */
1217 }
1218
1219 /* We didn't find the label, so this must be a reference to the
1220 first instance. */
1221 return 0;
1222}
1223
1224/*
1225 * fb_label_name()
1226 *
1227 * Caller must copy returned name: we re-use the area for the next name.
1228 *
1229 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1230 * where n is the label number and m is the instance number. "L" makes
1231 * it a label discarded unless debugging and "^B"('\2') ensures no
1232 * ordinary symbol SHOULD get the same name as a local label
1233 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1234 *
1235 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1236
1237char * /* Return local label name. */
1238fb_label_name (n, augend)
1239 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1240 long augend; /* 0 for nb, 1 for n:, nf */
1241{
1242 long i;
1243 /* Returned to caller, then copied. used for created names ("4f") */
1244 static char symbol_name_build[24];
1245 register char *p;
1246 register char *q;
1247 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1248
1249 know (n >= 0);
1250 know (augend == 0 || augend == 1);
1251 p = symbol_name_build;
1252 *p++ = 'L';
1253
1254 /* Next code just does sprintf( {}, "%d", n); */
1255 /* label number */
1256 q = symbol_name_temporary;
1257 for (*q++ = 0, i = n; i; ++q)
1258 {
1259 *q = i % 10 + '0';
1260 i /= 10;
1261 }
1262 while ((*p = *--q) != '\0')
1263 ++p;
1264
1265 *p++ = 2; /* ^B */
1266
1267 /* instance number */
1268 q = symbol_name_temporary;
1269 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1270 {
1271 *q = i % 10 + '0';
1272 i /= 10;
1273 }
1274 while ((*p++ = *--q) != '\0');;
1275
1276 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1277 return (symbol_name_build);
1278} /* fb_label_name() */
1279
1280/*
1281 * decode name that may have been generated by foo_label_name() above. If
1282 * the name wasn't generated by foo_label_name(), then return it unaltered.
1283 * This is used for error messages.
1284 */
1285
1286char *
1287decode_local_label_name (s)
1288 char *s;
1289{
1290 char *p;
1291 char *symbol_decode;
1292 int label_number;
1293 int instance_number;
1294 char *type;
1295 const char *message_format = _("\"%d\" (instance number %d of a %s label)");
1296
1297 if (s[0] != 'L')
1298 return s;
1299
1300 for (label_number = 0, p = s + 1; isdigit ((unsigned char) *p); ++p)
1301 label_number = (10 * label_number) + *p - '0';
1302
1303 if (*p == 1)
1304 type = "dollar";
1305 else if (*p == 2)
1306 type = "fb";
1307 else
1308 return s;
1309
1310 for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
1311 instance_number = (10 * instance_number) + *p - '0';
1312
1313 symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1314 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1315
1316 return symbol_decode;
1317}
1318
1319/* Get the value of a symbol. */
1320
1321valueT
1322S_GET_VALUE (s)
1323 symbolS *s;
1324{
1325 if (!s->sy_resolved && s->sy_value.X_op != O_constant)
1326 resolve_symbol_value (s, 1);
1327 if (s->sy_value.X_op != O_constant)
1328 {
1329 static symbolS *recur;
1330
1331 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1332 may call S_GET_VALUE. We use a static symbol to avoid the
1333 immediate recursion. */
1334 if (recur == s)
1335 return (valueT) s->sy_value.X_add_number;
1336 recur = s;
1337 if (! s->sy_resolved
1338 || s->sy_value.X_op != O_symbol
1339 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1340 as_bad (_("Attempt to get value of unresolved symbol %s"),
1341 S_GET_NAME (s));
1342 recur = NULL;
1343 }
1344 return (valueT) s->sy_value.X_add_number;
1345}
1346
1347/* Set the value of a symbol. */
1348
1349void
1350S_SET_VALUE (s, val)
1351 symbolS *s;
1352 valueT val;
1353{
1354 s->sy_value.X_op = O_constant;
1355 s->sy_value.X_add_number = (offsetT) val;
1356 s->sy_value.X_unsigned = 0;
1357}
1358
1359void
1360copy_symbol_attributes (dest, src)
1361 symbolS *dest, *src;
1362{
1363#ifdef BFD_ASSEMBLER
1364 /* In an expression, transfer the settings of these flags.
1365 The user can override later, of course. */
1366#define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1367 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1368#endif
1369
1370#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1371 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1372#endif
1373}
1374
1375#ifdef BFD_ASSEMBLER
1376
1377int
1378S_IS_FUNCTION (s)
1379 symbolS *s;
1380{
1381 flagword flags = s->bsym->flags;
1382
1383 return (flags & BSF_FUNCTION) != 0;
1384}
1385
1386int
1387S_IS_EXTERNAL (s)
1388 symbolS *s;
1389{
1390 flagword flags = s->bsym->flags;
1391
1392 /* sanity check */
1393 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1394 abort ();
1395
1396 return (flags & BSF_GLOBAL) != 0;
1397}
1398
1399int
1400S_IS_WEAK (s)
1401 symbolS *s;
1402{
1403 return (s->bsym->flags & BSF_WEAK) != 0;
1404}
1405
1406int
1407S_IS_COMMON (s)
1408 symbolS *s;
1409{
1410 return bfd_is_com_section (s->bsym->section);
1411}
1412
1413int
1414S_IS_DEFINED (s)
1415 symbolS *s;
1416{
1417 return s->bsym->section != undefined_section;
1418}
1419
1420int
1421S_IS_DEBUG (s)
1422 symbolS *s;
1423{
1424 if (s->bsym->flags & BSF_DEBUGGING)
1425 return 1;
1426 return 0;
1427}
1428
1429int
1430S_IS_LOCAL (s)
1431 symbolS *s;
1432{
1433 flagword flags = s->bsym->flags;
1434 const char *name;
1435
1436 /* sanity check */
1437 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1438 abort ();
1439
1440 if (bfd_get_section (s->bsym) == reg_section)
1441 return 1;
1442
1443 if (flag_strip_local_absolute
1444 && (flags & BSF_GLOBAL) == 0
1445 && bfd_get_section (s->bsym) == absolute_section)
1446 return 1;
1447
1448 name = S_GET_NAME (s);
1449 return (name != NULL
1450 && ! S_IS_DEBUG (s)
1451 && (strchr (name, '\001')
1452 || strchr (name, '\002')
1453 || (! flag_keep_locals
1454 && (bfd_is_local_label (stdoutput, s->bsym)
1455 || (flag_mri
1456 && name[0] == '?'
1457 && name[1] == '?')))));
1458}
1459
1460int
1461S_IS_EXTERN (s)
1462 symbolS *s;
1463{
1464 return S_IS_EXTERNAL (s);
1465}
1466
1467int
1468S_IS_STABD (s)
1469 symbolS *s;
1470{
1471 return S_GET_NAME (s) == 0;
1472}
1473
1474CONST char *
1475S_GET_NAME (s)
1476 symbolS *s;
1477{
1478 return s->bsym->name;
1479}
1480
1481segT
1482S_GET_SEGMENT (s)
1483 symbolS *s;
1484{
1485 return s->bsym->section;
1486}
1487
1488void
1489S_SET_SEGMENT (s, seg)
1490 symbolS *s;
1491 segT seg;
1492{
1493 /* Don't reassign section symbols. The direct reason is to prevent seg
1494 faults assigning back to const global symbols such as *ABS*, but it
1495 shouldn't happen anyway. */
1496
1497 if (s->bsym->flags & BSF_SECTION_SYM)
1498 {
1499 if (s->bsym->section != seg)
1500 abort();
1501 }
1502 else
1503 s->bsym->section = seg;
1504}
1505
1506void
1507S_SET_EXTERNAL (s)
1508 symbolS *s;
1509{
1510 if ((s->bsym->flags & BSF_WEAK) != 0)
1511 {
1512 /* Let .weak override .global. */
1513 return;
1514 }
1515 s->bsym->flags |= BSF_GLOBAL;
1516 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1517}
1518
1519void
1520S_CLEAR_EXTERNAL (s)
1521 symbolS *s;
1522{
1523 if ((s->bsym->flags & BSF_WEAK) != 0)
1524 {
1525 /* Let .weak override. */
1526 return;
1527 }
1528 s->bsym->flags |= BSF_LOCAL;
1529 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1530}
1531
1532void
1533S_SET_WEAK (s)
1534 symbolS *s;
1535{
1536 s->bsym->flags |= BSF_WEAK;
1537 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1538}
1539
1540void
1541S_SET_NAME (s, name)
1542 symbolS *s;
1543 char *name;
1544{
1545 s->bsym->name = name;
1546}
1547#endif /* BFD_ASSEMBLER */
1548
1549void
1550symbol_begin ()
1551{
1552 symbol_lastP = NULL;
1553 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
1554 sy_hash = hash_new ();
1555
1556 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1557#ifdef BFD_ASSEMBLER
1558#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1559 abs_symbol.bsym = bfd_abs_section.symbol;
1560#endif
1561#else
1562 /* Can't initialise a union. Sigh. */
1563 S_SET_SEGMENT (&abs_symbol, absolute_section);
1564#endif
1565 abs_symbol.sy_value.X_op = O_constant;
1566 abs_symbol.sy_frag = &zero_address_frag;
1567
1568 if (LOCAL_LABELS_FB)
1569 fb_label_init ();
1570}
1571
1572\f
1573int indent_level;
1574
1575/* Maximum indent level.
1576 Available for modification inside a gdb session. */
1577int max_indent_level = 8;
1578
1579#if 0
1580
1581static void
1582indent ()
1583{
1584 printf ("%*s", indent_level * 4, "");
1585}
1586
1587#endif
1588
1589void
1590print_symbol_value_1 (file, sym)
1591 FILE *file;
1592 symbolS *sym;
1593{
1594 const char *name = S_GET_NAME (sym);
1595 if (!name || !name[0])
1596 name = "(unnamed)";
1597 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
1598 if (sym->sy_frag != &zero_address_frag)
1599 fprintf (file, " frag %lx", (long) sym->sy_frag);
1600 if (sym->written)
1601 fprintf (file, " written");
1602 if (sym->sy_resolved)
1603 fprintf (file, " resolved");
1604 else if (sym->sy_resolving)
1605 fprintf (file, " resolving");
1606 if (sym->sy_used_in_reloc)
1607 fprintf (file, " used-in-reloc");
1608 if (sym->sy_used)
1609 fprintf (file, " used");
1610 if (S_IS_LOCAL (sym))
1611 fprintf (file, " local");
1612 if (S_IS_EXTERN (sym))
1613 fprintf (file, " extern");
1614 if (S_IS_DEBUG (sym))
1615 fprintf (file, " debug");
1616 if (S_IS_DEFINED (sym))
1617 fprintf (file, " defined");
1618 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
1619 if (sym->sy_resolved)
1620 {
1621 segT s = S_GET_SEGMENT (sym);
1622
1623 if (s != undefined_section
1624 && s != expr_section)
1625 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
1626 }
1627 else if (indent_level < max_indent_level
1628 && S_GET_SEGMENT (sym) != undefined_section)
1629 {
1630 indent_level++;
1631 fprintf (file, "\n%*s<", indent_level * 4, "");
1632 print_expr_1 (file, &sym->sy_value);
1633 fprintf (file, ">");
1634 indent_level--;
1635 }
1636 fflush (file);
1637}
1638
1639void
1640print_symbol_value (sym)
1641 symbolS *sym;
1642{
1643 indent_level = 0;
1644 print_symbol_value_1 (stderr, sym);
1645 fprintf (stderr, "\n");
1646}
1647
1648static void
1649print_binary (file, name, exp)
1650 FILE *file;
1651 const char * name;
1652 expressionS *exp;
1653{
1654 indent_level++;
1655 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
1656 print_symbol_value_1 (file, exp->X_add_symbol);
1657 fprintf (file, ">\n%*s<", indent_level * 4, "");
1658 print_symbol_value_1 (file, exp->X_op_symbol);
1659 fprintf (file, ">");
1660 indent_level--;
1661}
1662
1663void
1664print_expr_1 (file, exp)
1665 FILE *file;
1666 expressionS *exp;
1667{
1668 fprintf (file, "expr %lx ", (long) exp);
1669 switch (exp->X_op)
1670 {
1671 case O_illegal:
1672 fprintf (file, "illegal");
1673 break;
1674 case O_absent:
1675 fprintf (file, "absent");
1676 break;
1677 case O_constant:
1678 fprintf (file, "constant %lx", (long) exp->X_add_number);
1679 break;
1680 case O_symbol:
1681 indent_level++;
1682 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
1683 print_symbol_value_1 (file, exp->X_add_symbol);
1684 fprintf (file, ">");
1685 maybe_print_addnum:
1686 if (exp->X_add_number)
1687 fprintf (file, "\n%*s%lx", indent_level * 4, "",
1688 (long) exp->X_add_number);
1689 indent_level--;
1690 break;
1691 case O_register:
1692 fprintf (file, "register #%d", (int) exp->X_add_number);
1693 break;
1694 case O_big:
1695 fprintf (file, "big");
1696 break;
1697 case O_uminus:
1698 fprintf (file, "uminus -<");
1699 indent_level++;
1700 print_symbol_value_1 (file, exp->X_add_symbol);
1701 fprintf (file, ">");
1702 goto maybe_print_addnum;
1703 case O_bit_not:
1704 fprintf (file, "bit_not");
1705 break;
1706 case O_multiply:
1707 print_binary (file, "multiply", exp);
1708 break;
1709 case O_divide:
1710 print_binary (file, "divide", exp);
1711 break;
1712 case O_modulus:
1713 print_binary (file, "modulus", exp);
1714 break;
1715 case O_left_shift:
1716 print_binary (file, "lshift", exp);
1717 break;
1718 case O_right_shift:
1719 print_binary (file, "rshift", exp);
1720 break;
1721 case O_bit_inclusive_or:
1722 print_binary (file, "bit_ior", exp);
1723 break;
1724 case O_bit_exclusive_or:
1725 print_binary (file, "bit_xor", exp);
1726 break;
1727 case O_bit_and:
1728 print_binary (file, "bit_and", exp);
1729 break;
1730 case O_eq:
1731 print_binary (file, "eq", exp);
1732 break;
1733 case O_ne:
1734 print_binary (file, "ne", exp);
1735 break;
1736 case O_lt:
1737 print_binary (file, "lt", exp);
1738 break;
1739 case O_le:
1740 print_binary (file, "le", exp);
1741 break;
1742 case O_ge:
1743 print_binary (file, "ge", exp);
1744 break;
1745 case O_gt:
1746 print_binary (file, "gt", exp);
1747 break;
1748 case O_logical_and:
1749 print_binary (file, "logical_and", exp);
1750 break;
1751 case O_logical_or:
1752 print_binary (file, "logical_or", exp);
1753 break;
1754 case O_add:
1755 indent_level++;
1756 fprintf (file, "add\n%*s<", indent_level * 4, "");
1757 print_symbol_value_1 (file, exp->X_add_symbol);
1758 fprintf (file, ">\n%*s<", indent_level * 4, "");
1759 print_symbol_value_1 (file, exp->X_op_symbol);
1760 fprintf (file, ">");
1761 goto maybe_print_addnum;
1762 case O_subtract:
1763 indent_level++;
1764 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
1765 print_symbol_value_1 (file, exp->X_add_symbol);
1766 fprintf (file, ">\n%*s<", indent_level * 4, "");
1767 print_symbol_value_1 (file, exp->X_op_symbol);
1768 fprintf (file, ">");
1769 goto maybe_print_addnum;
1770 default:
1771 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
1772 break;
1773 }
1774 fflush (stdout);
1775}
1776
1777void
1778print_expr (exp)
1779 expressionS *exp;
1780{
1781 print_expr_1 (stderr, exp);
1782 fprintf (stderr, "\n");
1783}
1784
1785void
1786symbol_print_statistics (file)
1787 FILE *file;
1788{
1789 hash_print_statistics (file, "symbol table", sy_hash);
1790}
1791
1792/* end of symbols.c */
This page took 0.084956 seconds and 4 git commands to generate.