1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 1990, 1991, 1992, 1993, 1994
3 Free Software Foundation, Inc.
5 This file is part of GAS, the GNU Assembler.
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)
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.
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
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
27 #include "obstack.h" /* For "symbols.h" */
30 #ifndef WORKING_DOT_WORD
31 extern int new_broken_words;
34 /* symbol-name => struct symbol pointer */
35 static struct hash_control *sy_hash;
37 /* Below are commented in "symbols.h". */
38 symbolS *symbol_rootP;
39 symbolS *symbol_lastP;
43 #define debug_verify_symchain verify_symbol_chain
45 #define debug_verify_symchain (void)
50 static void fb_label_init PARAMS ((void));
54 Return a pointer to a new symbol. Die if we can't make a new
55 symbol. Fill in the symbol's values. Add symbol to end of symbol
58 This function should be called in the general case of creating a
59 symbol. However, if the output file symbol table has already been
60 set, and you are certain that this symbol won't be wanted in the
61 output file, you can call symbol_create. */
64 symbol_new (name, segment, valu, frag)
70 symbolS *symbolP = symbol_create (name, segment, valu, frag);
73 * Link to end of symbol chain.
77 extern int symbol_table_frozen;
78 if (symbol_table_frozen)
82 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
83 debug_verify_symchain (symbol_rootP, symbol_lastP);
89 symbol_create (name, segment, valu, frag)
90 const char *name; /* It is copied, the caller can destroy/modify */
91 segT segment; /* Segment identifier (SEG_<something>) */
92 valueT valu; /* Symbol value */
93 fragS *frag; /* Associated fragment */
95 unsigned int name_length;
96 char *preserved_copy_of_name;
99 name_length = strlen (name) + 1; /* +1 for \0 */
100 obstack_grow (¬es, name, name_length);
101 preserved_copy_of_name = obstack_finish (¬es);
102 #ifdef STRIP_UNDERSCORE
103 if (preserved_copy_of_name[0] == '_')
104 preserved_copy_of_name++;
107 #ifdef tc_canonicalize_symbol_name
108 preserved_copy_of_name =
109 tc_canonicalize_symbol_name (preserved_copy_of_name);
112 symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS));
114 /* symbol must be born in some fixed state. This seems as good as any. */
115 memset (symbolP, 0, sizeof (symbolS));
118 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
119 assert (symbolP->bsym != 0);
120 symbolP->bsym->udata.p = (PTR) symbolP;
122 S_SET_NAME (symbolP, preserved_copy_of_name);
124 S_SET_SEGMENT (symbolP, segment);
125 S_SET_VALUE (symbolP, valu);
126 symbol_clear_list_pointers (symbolP);
128 symbolP->sy_frag = frag;
129 #ifndef BFD_ASSEMBLER
130 symbolP->sy_number = ~0;
131 symbolP->sy_name_offset = (unsigned int) ~0;
134 obj_symbol_new_hook (symbolP);
136 #ifdef tc_symbol_new_hook
137 tc_symbol_new_hook (symbolP);
147 * We have just seen "<name>:".
148 * Creates a struct symbol unless it already exists.
150 * Gripes if we are redefining a symbol incompatibly (and ignores it).
154 colon (sym_name) /* just seen "x:" - rattle symbols & frags */
155 register char *sym_name; /* symbol name, as a cannonical string */
156 /* We copy this string: OK to alter later. */
158 register symbolS *symbolP; /* symbol we are working with */
160 /* Sun local labels go out of scope whenever a non-local symbol is
162 if (LOCAL_LABELS_DOLLAR && *sym_name != 'L')
163 dollar_label_clear ();
165 #ifndef WORKING_DOT_WORD
166 if (new_broken_words)
168 struct broken_word *a;
173 extern const int md_short_jump_size;
174 extern const int md_long_jump_size;
175 possible_bytes = (md_short_jump_size
176 + new_broken_words * md_long_jump_size);
179 frag_opcode = frag_var (rs_broken_word,
183 (symbolS *) broken_words,
187 /* We want to store the pointer to where to insert the jump table in the
188 fr_opcode of the rs_broken_word frag. This requires a little
191 && (frag_tmp->fr_type != rs_broken_word
192 || frag_tmp->fr_opcode))
193 frag_tmp = frag_tmp->fr_next;
195 frag_tmp->fr_opcode = frag_opcode;
196 new_broken_words = 0;
198 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
199 a->dispfrag = frag_tmp;
201 #endif /* WORKING_DOT_WORD */
203 if ((symbolP = symbol_find (sym_name)) != 0)
205 #ifdef RESOLVE_SYMBOL_REDEFINITION
206 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
210 * Now check for undefined symbols
212 if (!S_IS_DEFINED (symbolP))
214 if (S_GET_VALUE (symbolP) == 0)
216 symbolP->sy_frag = frag_now;
218 S_GET_OTHER(symbolP) = const_flag;
220 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
221 S_SET_SEGMENT (symbolP, now_seg);
224 #endif /* if we have one, it better be zero. */
230 * There are still several cases to check:
231 * A .comm/.lcomm symbol being redefined as
232 * initialized data is OK
233 * A .comm/.lcomm symbol being redefined with
234 * a larger size is also OK
236 * This only used to be allowed on VMS gas, but Sun cc
237 * on the sparc also depends on it.
240 if (((!S_IS_DEBUG (symbolP)
241 && !S_IS_DEFINED (symbolP)
242 && S_IS_EXTERNAL (symbolP))
243 || S_GET_SEGMENT (symbolP) == bss_section)
244 && (now_seg == data_section
245 || now_seg == S_GET_SEGMENT (symbolP)))
248 * Select which of the 2 cases this is
250 if (now_seg != data_section)
253 * New .comm for prev .comm symbol.
254 * If the new size is larger we just
255 * change its value. If the new size
256 * is smaller, we ignore this symbol
258 if (S_GET_VALUE (symbolP)
259 < ((unsigned) frag_now_fix ()))
261 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
266 /* It is a .comm/.lcomm being converted to initialized
268 symbolP->sy_frag = frag_now;
270 S_GET_OTHER(symbolP) = const_flag;
272 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
273 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
278 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
279 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
281 segment_name (S_GET_SEGMENT (symbolP)),
282 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
283 (long) S_GET_VALUE (symbolP));
285 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
287 segment_name (S_GET_SEGMENT (symbolP)),
288 (long) S_GET_VALUE (symbolP));
291 } /* if the undefined symbol has no value */
295 /* Don't blow up if the definition is the same */
296 if (!(frag_now == symbolP->sy_frag
297 && S_GET_VALUE (symbolP) == frag_now_fix ()
298 && S_GET_SEGMENT (symbolP) == now_seg))
299 as_fatal ("Symbol %s already defined.", sym_name);
300 } /* if this symbol is not yet defined */
305 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
308 S_SET_OTHER (symbolP, const_flag);
311 symbol_table_insert (symbolP);
312 } /* if we have seen this symbol before */
315 tc_frob_label (symbolP);
321 * symbol_table_insert()
323 * Die if we can't insert the symbol.
328 symbol_table_insert (symbolP)
331 register const char *error_string;
334 know (S_GET_NAME (symbolP));
336 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
338 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
339 S_GET_NAME (symbolP), error_string);
341 } /* symbol_table_insert() */
344 * symbol_find_or_make()
346 * If a symbol name does not exist, create it as undefined, and insert
347 * it into the symbol table. Return a pointer to it.
350 symbol_find_or_make (name)
353 register symbolS *symbolP;
355 symbolP = symbol_find (name);
359 symbolP = symbol_make (name);
361 symbol_table_insert (symbolP);
362 } /* if symbol wasn't found */
365 } /* symbol_find_or_make() */
373 /* Let the machine description default it, e.g. for register names. */
374 symbolP = md_undefined_symbol ((char *) name);
377 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
380 } /* symbol_make() */
385 * Implement symbol table lookup.
386 * In: A symbol's name as a string: '\0' can't be part of a symbol name.
387 * Out: NULL if the name was not in the symbol table, else the address
388 * of a struct symbol associated with that name.
395 #ifdef STRIP_UNDERSCORE
396 return (symbol_find_base (name, 1));
397 #else /* STRIP_UNDERSCORE */
398 return (symbol_find_base (name, 0));
399 #endif /* STRIP_UNDERSCORE */
400 } /* symbol_find() */
403 symbol_find_base (name, strip_underscore)
405 int strip_underscore;
407 if (strip_underscore && *name == '_')
410 #ifdef tc_canonicalize_symbol_name
414 copy = (char *) alloca (strlen (name) + 1);
416 name = tc_canonicalize_symbol_name (copy);
420 return ((symbolS *) hash_find (sy_hash, name));
424 * Once upon a time, symbols were kept in a singly linked list. At
425 * least coff needs to be able to rearrange them from time to time, for
426 * which a doubly linked list is much more convenient. Loic did these
427 * as macros which seemed dangerous to me so they're now functions.
431 /* Link symbol ADDME after symbol TARGET in the chain. */
433 symbol_append (addme, target, rootPP, lastPP)
441 know (*rootPP == NULL);
442 know (*lastPP == NULL);
446 } /* if the list is empty */
448 if (target->sy_next != NULL)
450 #ifdef SYMBOLS_NEED_BACKPOINTERS
451 target->sy_next->sy_previous = addme;
452 #endif /* SYMBOLS_NEED_BACKPOINTERS */
456 know (*lastPP == target);
458 } /* if we have a next */
460 addme->sy_next = target->sy_next;
461 target->sy_next = addme;
463 #ifdef SYMBOLS_NEED_BACKPOINTERS
464 addme->sy_previous = target;
465 #endif /* SYMBOLS_NEED_BACKPOINTERS */
468 /* Set the chain pointers of SYMBOL to null. */
470 symbol_clear_list_pointers (symbolP)
473 symbolP->sy_next = NULL;
474 #ifdef SYMBOLS_NEED_BACKPOINTERS
475 symbolP->sy_previous = NULL;
479 #ifdef SYMBOLS_NEED_BACKPOINTERS
480 /* Remove SYMBOLP from the list. */
482 symbol_remove (symbolP, rootPP, lastPP)
487 if (symbolP == *rootPP)
489 *rootPP = symbolP->sy_next;
490 } /* if it was the root */
492 if (symbolP == *lastPP)
494 *lastPP = symbolP->sy_previous;
495 } /* if it was the tail */
497 if (symbolP->sy_next != NULL)
499 symbolP->sy_next->sy_previous = symbolP->sy_previous;
502 if (symbolP->sy_previous != NULL)
504 symbolP->sy_previous->sy_next = symbolP->sy_next;
507 debug_verify_symchain (*rootPP, *lastPP);
510 /* Link symbol ADDME before symbol TARGET in the chain. */
512 symbol_insert (addme, target, rootPP, lastPP)
518 if (target->sy_previous != NULL)
520 target->sy_previous->sy_next = addme;
524 know (*rootPP == target);
528 addme->sy_previous = target->sy_previous;
529 target->sy_previous = addme;
530 addme->sy_next = target;
532 debug_verify_symchain (*rootPP, *lastPP);
535 #endif /* SYMBOLS_NEED_BACKPOINTERS */
538 verify_symbol_chain (rootP, lastP)
542 symbolS *symbolP = rootP;
547 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
549 #ifdef SYMBOLS_NEED_BACKPOINTERS
550 know (symbolP->sy_next->sy_previous == symbolP);
552 /* Walk the list anyways, to make sure pointers are still good. */
554 #endif /* SYMBOLS_NEED_BACKPOINTERS */
557 assert (lastP == symbolP);
561 verify_symbol_chain_2 (sym)
564 symbolS *p = sym, *n = sym;
565 #ifdef SYMBOLS_NEED_BACKPOINTERS
566 while (symbol_previous (p))
567 p = symbol_previous (p);
569 while (symbol_next (n))
571 verify_symbol_chain (p, n);
574 /* Resolve the value of a symbol. This is called during the final
575 pass over the symbol table to resolve any symbols with complex
579 resolve_symbol_value (symp)
584 if (symp->sy_resolved)
589 if (symp->sy_resolving)
591 as_bad ("Symbol definition loop encountered at %s",
593 S_SET_VALUE (symp, (valueT) 0);
598 offsetT left, right, val;
599 segT seg_left, seg_right;
601 symp->sy_resolving = 1;
604 switch (symp->sy_value.X_op)
607 S_SET_VALUE (symp, 0);
610 S_SET_VALUE (symp, S_GET_VALUE (symp) + symp->sy_frag->fr_address);
611 if (S_GET_SEGMENT (symp) == expr_section)
612 S_SET_SEGMENT (symp, absolute_section);
617 resolve_symbol_value (symp->sy_value.X_add_symbol);
619 #if 0 /* I thought this was needed for some of the i386-svr4 PIC
620 support, but it appears I was wrong, and it breaks rs6000
622 if (S_GET_SEGMENT (symp->sy_value.X_add_symbol) != undefined_section
623 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section)
626 if (symp->sy_value.X_add_number == 0)
627 copy_symbol_attributes (symp, symp->sy_value.X_add_symbol);
630 (symp->sy_value.X_add_number
631 + symp->sy_frag->fr_address
632 + S_GET_VALUE (symp->sy_value.X_add_symbol)));
633 if (S_GET_SEGMENT (symp) == expr_section
634 || S_GET_SEGMENT (symp) == undefined_section)
636 S_GET_SEGMENT (symp->sy_value.X_add_symbol));
638 resolved = symp->sy_value.X_add_symbol->sy_resolved;
643 resolve_symbol_value (symp->sy_value.X_add_symbol);
644 if (symp->sy_value.X_op == O_uminus)
645 val = - S_GET_VALUE (symp->sy_value.X_add_symbol);
647 val = ~ S_GET_VALUE (symp->sy_value.X_add_symbol);
650 + symp->sy_value.X_add_number
651 + symp->sy_frag->fr_address));
652 if (S_GET_SEGMENT (symp) == expr_section
653 || S_GET_SEGMENT (symp) == undefined_section)
654 S_SET_SEGMENT (symp, absolute_section);
655 resolved = symp->sy_value.X_add_symbol->sy_resolved;
659 resolve_symbol_value (symp->sy_value.X_add_symbol);
660 resolve_symbol_value (symp->sy_value.X_op_symbol);
661 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
662 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
663 /* This case comes up with PIC support. */
665 symbolS *s_left = symp->sy_value.X_add_symbol;
666 symbolS *s_right = symp->sy_value.X_op_symbol;
668 if (seg_left == absolute_section)
676 seg_left = seg_right;
679 if (seg_right == absolute_section
680 && s_right->sy_resolved)
682 symp->sy_value.X_add_number += S_GET_VALUE (s_right);
683 symp->sy_value.X_op_symbol = 0;
684 symp->sy_value.X_add_symbol = s_left;
685 symp->sy_value.X_op = O_symbol;
696 case O_bit_inclusive_or:
698 case O_bit_exclusive_or:
701 resolve_symbol_value (symp->sy_value.X_add_symbol);
702 resolve_symbol_value (symp->sy_value.X_op_symbol);
703 seg_left = S_GET_SEGMENT (symp->sy_value.X_add_symbol);
704 seg_right = S_GET_SEGMENT (symp->sy_value.X_op_symbol);
705 if (seg_left != seg_right
706 && seg_left != undefined_section
707 && seg_right != undefined_section)
708 as_bad ("%s is operation on symbols in different sections",
710 if ((S_GET_SEGMENT (symp->sy_value.X_add_symbol)
712 && symp->sy_value.X_op != O_subtract)
713 as_bad ("%s is illegal operation on non-absolute symbols",
715 left = S_GET_VALUE (symp->sy_value.X_add_symbol);
716 right = S_GET_VALUE (symp->sy_value.X_op_symbol);
717 switch (symp->sy_value.X_op)
719 case O_multiply: val = left * right; break;
720 case O_divide: val = left / right; break;
721 case O_modulus: val = left % right; break;
722 case O_left_shift: val = left << right; break;
723 case O_right_shift: val = left >> right; break;
724 case O_bit_inclusive_or: val = left | right; break;
725 case O_bit_or_not: val = left |~ right; break;
726 case O_bit_exclusive_or: val = left ^ right; break;
727 case O_bit_and: val = left & right; break;
728 case O_add: val = left + right; break;
729 case O_subtract: val = left - right; break;
733 (symp->sy_value.X_add_number
734 + symp->sy_frag->fr_address
736 if (S_GET_SEGMENT (symp) == expr_section
737 || S_GET_SEGMENT (symp) == undefined_section)
738 S_SET_SEGMENT (symp, absolute_section);
739 resolved = (symp->sy_value.X_add_symbol->sy_resolved
740 && symp->sy_value.X_op_symbol->sy_resolved);
746 /* Give an error (below) if not in expr_section. We don't
747 want to worry about expr_section symbols, because they
748 are fictional (they are created as part of expression
749 resolution), and any problems may not actually mean
755 /* Don't worry if we can't resolve an expr_section symbol. */
757 symp->sy_resolved = 1;
758 else if (S_GET_SEGMENT (symp) != expr_section)
760 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp));
761 symp->sy_resolved = 1;
765 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
766 They are *really* local. That is, they go out of scope whenever we see a
767 label that isn't local. Also, like fb labels, there can be multiple
768 instances of a dollar label. Therefor, we name encode each instance with
769 the instance number, keep a list of defined symbols separate from the real
770 symbol table, and we treat these buggers as a sparse array. */
772 static long *dollar_labels;
773 static long *dollar_label_instances;
774 static char *dollar_label_defines;
775 static long dollar_label_count;
776 static unsigned long dollar_label_max;
779 dollar_label_defined (label)
784 know ((dollar_labels != NULL) || (dollar_label_count == 0));
786 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
788 return dollar_label_defines[i - dollar_labels];
790 /* if we get here, label isn't defined */
792 } /* dollar_label_defined() */
795 dollar_label_instance (label)
800 know ((dollar_labels != NULL) || (dollar_label_count == 0));
802 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
804 return (dollar_label_instances[i - dollar_labels]);
806 /* If we get here, we haven't seen the label before, therefore its instance
812 dollar_label_clear ()
814 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
817 #define DOLLAR_LABEL_BUMP_BY 10
820 define_dollar_label (label)
825 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
828 ++dollar_label_instances[i - dollar_labels];
829 dollar_label_defines[i - dollar_labels] = 1;
833 /* if we get to here, we don't have label listed yet. */
835 if (dollar_labels == NULL)
837 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
838 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
839 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
840 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
841 dollar_label_count = 0;
843 else if (dollar_label_count == dollar_label_max)
845 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
846 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
847 dollar_label_max * sizeof (long));
848 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
849 dollar_label_max * sizeof (long));
850 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
851 } /* if we needed to grow */
853 dollar_labels[dollar_label_count] = label;
854 dollar_label_instances[dollar_label_count] = 1;
855 dollar_label_defines[dollar_label_count] = 1;
856 ++dollar_label_count;
860 * dollar_label_name()
862 * Caller must copy returned name: we re-use the area for the next name.
864 * The mth occurence of label n: is turned into the symbol "Ln^Am"
865 * where n is the label number and m is the instance number. "L" makes
866 * it a label discarded unless debugging and "^A"('\1') ensures no
867 * ordinary symbol SHOULD get the same name as a local label
868 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
870 * fb labels get the same treatment, except that ^B is used in place of ^A.
873 char * /* Return local label name. */
874 dollar_label_name (n, augend)
875 register long n; /* we just saw "n$:" : n a number */
876 register int augend; /* 0 for current instance, 1 for new instance */
879 /* Returned to caller, then copied. used for created names ("4f") */
880 static char symbol_name_build[24];
883 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
886 know (augend == 0 || augend == 1);
887 p = symbol_name_build;
890 /* Next code just does sprintf( {}, "%d", n); */
892 q = symbol_name_temporary;
893 for (*q++ = 0, i = n; i; ++q)
898 while ((*p = *--q) != '\0')
903 /* instance number */
904 q = symbol_name_temporary;
905 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
910 while ((*p++ = *--q) != '\0');;
912 /* The label, as a '\0' ended string, starts at symbol_name_build. */
913 return symbol_name_build;
917 * Sombody else's idea of local labels. They are made by "n:" where n
918 * is any decimal digit. Refer to them with
919 * "nb" for previous (backward) n:
920 * or "nf" for next (forward) n:.
922 * We do a little better and let n be any number, not just a single digit, but
923 * since the other guy's assembler only does ten, we treat the first ten
926 * Like someone else's assembler, we have one set of local label counters for
927 * entire assembly, not one set per (sub)segment like in most assemblers. This
928 * implies that one can refer to a label in another segment, and indeed some
929 * crufty compilers have done just that.
931 * Since there could be a LOT of these things, treat them as a sparse array.
934 #define FB_LABEL_SPECIAL (10)
936 static long fb_low_counter[FB_LABEL_SPECIAL];
937 static long *fb_labels;
938 static long *fb_label_instances;
939 static long fb_label_count;
940 static long fb_label_max;
942 /* this must be more than FB_LABEL_SPECIAL */
943 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
948 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
949 } /* fb_label_init() */
951 /* add one to the instance number of this fb label */
953 fb_label_instance_inc (label)
958 if (label < FB_LABEL_SPECIAL)
960 ++fb_low_counter[label];
964 if (fb_labels != NULL)
966 for (i = fb_labels + FB_LABEL_SPECIAL;
967 i < fb_labels + fb_label_count; ++i)
971 ++fb_label_instances[i - fb_labels];
973 } /* if we find it */
974 } /* for each existing label */
977 /* if we get to here, we don't have label listed yet. */
979 if (fb_labels == NULL)
981 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
982 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
983 fb_label_max = FB_LABEL_BUMP_BY;
984 fb_label_count = FB_LABEL_SPECIAL;
987 else if (fb_label_count == fb_label_max)
989 fb_label_max += FB_LABEL_BUMP_BY;
990 fb_labels = (long *) xrealloc ((char *) fb_labels,
991 fb_label_max * sizeof (long));
992 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
993 fb_label_max * sizeof (long));
994 } /* if we needed to grow */
996 fb_labels[fb_label_count] = label;
997 fb_label_instances[fb_label_count] = 1;
1002 fb_label_instance (label)
1007 if (label < FB_LABEL_SPECIAL)
1009 return (fb_low_counter[label]);
1012 if (fb_labels != NULL)
1014 for (i = fb_labels + FB_LABEL_SPECIAL;
1015 i < fb_labels + fb_label_count; ++i)
1019 return (fb_label_instances[i - fb_labels]);
1020 } /* if we find it */
1021 } /* for each existing label */
1024 /* We didn't find the label, so this must be a reference to the
1032 * Caller must copy returned name: we re-use the area for the next name.
1034 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1035 * where n is the label number and m is the instance number. "L" makes
1036 * it a label discarded unless debugging and "^B"('\2') ensures no
1037 * ordinary symbol SHOULD get the same name as a local label
1038 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1040 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1042 char * /* Return local label name. */
1043 fb_label_name (n, augend)
1044 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1045 long augend; /* 0 for nb, 1 for n:, nf */
1048 /* Returned to caller, then copied. used for created names ("4f") */
1049 static char symbol_name_build[24];
1052 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1055 know (augend == 0 || augend == 1);
1056 p = symbol_name_build;
1059 /* Next code just does sprintf( {}, "%d", n); */
1061 q = symbol_name_temporary;
1062 for (*q++ = 0, i = n; i; ++q)
1067 while ((*p = *--q) != '\0')
1072 /* instance number */
1073 q = symbol_name_temporary;
1074 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1079 while ((*p++ = *--q) != '\0');;
1081 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1082 return (symbol_name_build);
1083 } /* fb_label_name() */
1086 * decode name that may have been generated by foo_label_name() above. If
1087 * the name wasn't generated by foo_label_name(), then return it unaltered.
1088 * This is used for error messages.
1092 decode_local_label_name (s)
1096 char *symbol_decode;
1098 int instance_number;
1100 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1105 for (label_number = 0, p = s + 1; isdigit (*p); ++p)
1106 label_number = (10 * label_number) + *p - '0';
1115 for (instance_number = 0, p++; isdigit (*p); ++p)
1116 instance_number = (10 * instance_number) + *p - '0';
1118 symbol_decode = obstack_alloc (¬es, strlen (message_format) + 30);
1119 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1121 return symbol_decode;
1124 /* Get the value of a symbol. */
1130 if (!s->sy_resolved && !s->sy_resolving && s->sy_value.X_op != O_constant)
1131 resolve_symbol_value (s);
1132 if (s->sy_value.X_op != O_constant)
1133 as_bad ("Attempt to get value of unresolved symbol %s", S_GET_NAME (s));
1134 return (valueT) s->sy_value.X_add_number;
1137 /* Set the value of a symbol. */
1140 S_SET_VALUE (s, val)
1144 s->sy_value.X_op = O_constant;
1145 s->sy_value.X_add_number = (offsetT) val;
1146 s->sy_value.X_unsigned = 0;
1150 copy_symbol_attributes (dest, src)
1151 symbolS *dest, *src;
1153 #ifdef BFD_ASSEMBLER
1154 /* In an expression, transfer the settings of these flags.
1155 The user can override later, of course. */
1156 #define COPIED_SYMFLAGS (BSF_FUNCTION)
1157 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1160 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1161 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1165 #ifdef BFD_ASSEMBLER
1171 flagword flags = s->bsym->flags;
1174 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
1177 return (flags & BSF_GLOBAL) != 0;
1184 return bfd_is_com_section (s->bsym->section);
1191 return s->bsym->section != undefined_section;
1198 if (s->bsym->flags & BSF_DEBUGGING)
1207 flagword flags = s->bsym->flags;
1210 if (flags & BSF_LOCAL && flags & BSF_GLOBAL)
1213 return (S_GET_NAME (s)
1215 && (strchr (S_GET_NAME (s), '\001')
1216 || strchr (S_GET_NAME (s), '\002')
1217 || (S_LOCAL_NAME (s)
1218 && !flag_keep_locals)));
1225 return S_IS_EXTERNAL (s);
1232 return S_GET_NAME (s) == 0;
1239 return s->bsym->name;
1246 return s->bsym->section;
1250 S_SET_SEGMENT (s, seg)
1254 s->bsym->section = seg;
1261 if ((s->bsym->flags & BSF_WEAK) != 0)
1262 as_warn ("%s already declared as weak", S_GET_NAME (s));
1263 s->bsym->flags |= BSF_GLOBAL;
1264 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1268 S_CLEAR_EXTERNAL (s)
1271 if ((s->bsym->flags & BSF_WEAK) != 0)
1272 as_warn ("%s already declared as weak", S_GET_NAME (s));
1273 s->bsym->flags |= BSF_LOCAL;
1274 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1281 if ((s->bsym->flags & BSF_GLOBAL) != 0)
1282 as_warn ("%s already declared as global", S_GET_NAME (s));
1283 s->bsym->flags |= BSF_WEAK;
1284 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1288 S_SET_NAME (s, name)
1292 s->bsym->name = name;
1294 #endif /* BFD_ASSEMBLER */
1299 symbol_lastP = NULL;
1300 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
1301 sy_hash = hash_new ();
1303 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1304 #ifdef BFD_ASSEMBLER
1305 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1306 abs_symbol.bsym = bfd_abs_section.symbol;
1309 /* Can't initialise a union. Sigh. */
1310 S_SET_SEGMENT (&abs_symbol, absolute_section);
1312 abs_symbol.sy_value.X_op = O_constant;
1314 if (LOCAL_LABELS_FB)
1326 printf ("%*s", indent_level * 4, "");
1331 void print_expr_1 PARAMS ((FILE *, expressionS *));
1332 void print_symbol_value_1 PARAMS ((FILE *, symbolS *));
1335 print_symbol_value_1 (file, sym)
1339 const char *name = S_GET_NAME (sym);
1340 if (!name || !name[0])
1342 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
1343 if (sym->sy_frag != &zero_address_frag)
1344 fprintf (file, " frag %lx", (long) sym->sy_frag);
1346 fprintf (file, " written");
1347 if (sym->sy_resolved)
1348 fprintf (file, " resolved");
1349 else if (sym->sy_resolving)
1350 fprintf (file, " resolving");
1351 if (sym->sy_used_in_reloc)
1352 fprintf (file, " used-in-reloc");
1354 fprintf (file, " used");
1355 if (S_IS_LOCAL (sym))
1356 fprintf (file, " local");
1357 if (S_IS_EXTERN (sym))
1358 fprintf (file, " extern");
1359 if (S_IS_DEBUG (sym))
1360 fprintf (file, " debug");
1361 if (S_IS_DEFINED (sym))
1362 fprintf (file, " defined");
1363 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
1364 if (sym->sy_resolved)
1366 segT s = S_GET_SEGMENT (sym);
1368 if (s != undefined_section
1369 && s != expr_section)
1370 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
1372 else if (indent_level < 8 && S_GET_SEGMENT (sym) != undefined_section)
1375 fprintf (file, "\n%*s<", indent_level * 4, "");
1376 print_expr_1 (file, &sym->sy_value);
1377 fprintf (file, ">");
1384 print_symbol_value (sym)
1388 print_symbol_value_1 (stderr, sym);
1389 fprintf (stderr, "\n");
1393 print_expr_1 (file, exp)
1397 fprintf (file, "expr %lx ", (long) exp);
1401 fprintf (file, "illegal");
1404 fprintf (file, "absent");
1407 fprintf (file, "constant %lx", (long) exp->X_add_number);
1411 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
1412 print_symbol_value_1 (file, exp->X_add_symbol);
1413 fprintf (file, ">");
1415 if (exp->X_add_number)
1416 fprintf (file, "\n%*s%lx", indent_level * 4, "",
1417 (long) exp->X_add_number);
1421 fprintf (file, "register #%d", (int) exp->X_add_number);
1424 fprintf (file, "big");
1427 fprintf (file, "uminus -<");
1429 print_symbol_value_1 (file, exp->X_add_symbol);
1430 fprintf (file, ">");
1431 goto maybe_print_addnum;
1433 fprintf (file, "bit_not");
1436 fprintf (file, "multiply");
1439 fprintf (file, "divide");
1442 fprintf (file, "modulus");
1445 fprintf (file, "lshift");
1448 fprintf (file, "rshift");
1450 case O_bit_inclusive_or:
1451 fprintf (file, "bit_ior");
1453 case O_bit_exclusive_or:
1454 fprintf (file, "bit_xor");
1457 fprintf (file, "bit_and");
1461 fprintf (file, "add\n%*s<", indent_level * 4, "");
1462 print_symbol_value_1 (file, exp->X_add_symbol);
1463 fprintf (file, ">\n%*s<", indent_level * 4, "");
1464 print_symbol_value_1 (file, exp->X_op_symbol);
1465 fprintf (file, ">");
1466 goto maybe_print_addnum;
1469 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
1470 print_symbol_value_1 (file, exp->X_add_symbol);
1471 fprintf (file, ">\n%*s<", indent_level * 4, "");
1472 print_symbol_value_1 (file, exp->X_op_symbol);
1473 fprintf (file, ">");
1474 goto maybe_print_addnum;
1476 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
1486 print_expr_1 (stderr, exp);
1487 fprintf (stderr, "\n");
1490 /* end of symbols.c */