1 /* symbols.c -symbol table-
2 Copyright (C) 1987, 90, 91, 92, 93, 94, 95, 96, 97, 1998
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 the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /* #define DEBUG_SYMS / * to debug symbol list maintenance */
28 #include "obstack.h" /* For "symbols.h" */
31 /* This is non-zero if symbols are case sensitive, which is the
33 int symbols_case_sensitive = 1;
35 #ifndef WORKING_DOT_WORD
36 extern int new_broken_words;
39 /* symbol-name => struct symbol pointer */
40 static struct hash_control *sy_hash;
42 /* Below are commented in "symbols.h". */
43 symbolS *symbol_rootP;
44 symbolS *symbol_lastP;
48 #define debug_verify_symchain verify_symbol_chain
50 #define debug_verify_symchain(root, last) ((void) 0)
55 static void fb_label_init PARAMS ((void));
56 static long dollar_label_instance PARAMS ((long));
57 static long fb_label_instance PARAMS ((long));
59 static void print_binary PARAMS ((FILE *, const char *, expressionS *));
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
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. */
73 symbol_new (name, segment, valu, frag)
79 symbolS *symbolP = symbol_create (name, segment, valu, frag);
82 * Link to end of symbol chain.
86 extern int symbol_table_frozen;
87 if (symbol_table_frozen)
91 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
97 symbol_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 */
103 unsigned int name_length;
104 char *preserved_copy_of_name;
107 name_length = strlen (name) + 1; /* +1 for \0 */
108 obstack_grow (¬es, name, name_length);
109 preserved_copy_of_name = obstack_finish (¬es);
110 #ifdef STRIP_UNDERSCORE
111 if (preserved_copy_of_name[0] == '_')
112 preserved_copy_of_name++;
115 #ifdef tc_canonicalize_symbol_name
116 preserved_copy_of_name =
117 tc_canonicalize_symbol_name (preserved_copy_of_name);
120 if (! symbols_case_sensitive)
124 for (s = (unsigned char *) preserved_copy_of_name; *s != '\0'; s++)
129 symbolP = (symbolS *) obstack_alloc (¬es, sizeof (symbolS));
131 /* symbol must be born in some fixed state. This seems as good as any. */
132 memset (symbolP, 0, sizeof (symbolS));
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;
140 S_SET_NAME (symbolP, preserved_copy_of_name);
142 S_SET_SEGMENT (symbolP, segment);
143 S_SET_VALUE (symbolP, valu);
144 symbol_clear_list_pointers (symbolP);
146 symbolP->sy_frag = frag;
147 #ifndef BFD_ASSEMBLER
148 symbolP->sy_number = ~0;
149 symbolP->sy_name_offset = (unsigned int) ~0;
152 obj_symbol_new_hook (symbolP);
154 #ifdef tc_symbol_new_hook
155 tc_symbol_new_hook (symbolP);
165 * We have just seen "<name>:".
166 * Creates a struct symbol unless it already exists.
168 * Gripes if we are redefining a symbol incompatibly (and ignores it).
172 colon (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. */
176 register symbolS *symbolP; /* symbol we are working with */
178 /* Sun local labels go out of scope whenever a non-local symbol is
180 if (LOCAL_LABELS_DOLLAR)
185 local = bfd_is_local_label_name (stdoutput, sym_name);
187 local = LOCAL_LABEL (sym_name);
191 dollar_label_clear ();
194 #ifndef WORKING_DOT_WORD
195 if (new_broken_words)
197 struct broken_word *a;
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);
208 frag_opcode = frag_var (rs_broken_word,
212 (symbolS *) broken_words,
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
220 && (frag_tmp->fr_type != rs_broken_word
221 || frag_tmp->fr_opcode))
222 frag_tmp = frag_tmp->fr_next;
224 frag_tmp->fr_opcode = frag_opcode;
225 new_broken_words = 0;
227 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
228 a->dispfrag = frag_tmp;
230 #endif /* WORKING_DOT_WORD */
232 if ((symbolP = symbol_find (sym_name)) != 0)
234 #ifdef RESOLVE_SYMBOL_REDEFINITION
235 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
239 * Now check for undefined symbols
241 if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
243 if (S_GET_VALUE (symbolP) == 0)
245 symbolP->sy_frag = frag_now;
247 S_SET_OTHER(symbolP, const_flag);
249 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
250 S_SET_SEGMENT (symbolP, now_seg);
253 #endif /* if we have one, it better be zero. */
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
265 * This only used to be allowed on VMS gas, but Sun cc
266 * on the sparc also depends on it.
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)))
277 * Select which of the 2 cases this is
279 if (now_seg != data_section)
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
287 if (S_GET_VALUE (symbolP)
288 < ((unsigned) frag_now_fix ()))
290 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
295 /* It is a .comm/.lcomm being converted to initialized
297 symbolP->sy_frag = frag_now;
299 S_SET_OTHER(symbolP, const_flag);
301 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
302 S_SET_SEGMENT (symbolP, now_seg); /* keep N_EXT bit */
307 #if defined (S_GET_OTHER) && defined (S_GET_DESC)
308 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%d.%d.%ld.",
310 segment_name (S_GET_SEGMENT (symbolP)),
311 S_GET_OTHER (symbolP), S_GET_DESC (symbolP),
312 (long) S_GET_VALUE (symbolP));
314 as_fatal ("Symbol \"%s\" is already defined as \"%s\"/%ld.",
316 segment_name (S_GET_SEGMENT (symbolP)),
317 (long) S_GET_VALUE (symbolP));
320 } /* if the undefined symbol has no value */
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 */
334 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
337 S_SET_OTHER (symbolP, const_flag);
340 symbol_table_insert (symbolP);
341 } /* if we have seen this symbol before */
343 if (mri_common_symbol != NULL)
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;
356 tc_frob_label (symbolP);
358 #ifdef obj_frob_label
359 obj_frob_label (symbolP);
367 * symbol_table_insert()
369 * Die if we can't insert the symbol.
374 symbol_table_insert (symbolP)
377 register const char *error_string;
380 know (S_GET_NAME (symbolP));
382 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
384 as_fatal ("Inserting \"%s\" into symbol table failed: %s",
385 S_GET_NAME (symbolP), error_string);
387 } /* symbol_table_insert() */
390 * symbol_find_or_make()
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.
396 symbol_find_or_make (name)
399 register symbolS *symbolP;
401 symbolP = symbol_find (name);
405 symbolP = symbol_make (name);
407 symbol_table_insert (symbolP);
408 } /* if symbol wasn't found */
411 } /* symbol_find_or_make() */
419 /* Let the machine description default it, e.g. for register names. */
420 symbolP = md_undefined_symbol ((char *) name);
423 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
426 } /* symbol_make() */
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.
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() */
449 symbol_find_base (name, strip_underscore)
451 int strip_underscore;
453 if (strip_underscore && *name == '_')
456 #ifdef tc_canonicalize_symbol_name
460 copy = (char *) alloca (strlen (name) + 1);
462 name = tc_canonicalize_symbol_name (copy);
466 if (! symbols_case_sensitive)
470 copy = (unsigned char *) alloca (strlen (name) + 1);
471 name = (const char *) copy;
472 for (; *copy != '\0'; copy++)
474 *copy = toupper (*copy);
477 return ((symbolS *) hash_find (sy_hash, name));
481 * Once upon a time, symbols were kept in a singly linked list. At
482 * least coff needs to be able to rearrange them from time to time, for
483 * which a doubly linked list is much more convenient. Loic did these
484 * as macros which seemed dangerous to me so they're now functions.
488 /* Link symbol ADDME after symbol TARGET in the chain. */
490 symbol_append (addme, target, rootPP, lastPP)
498 know (*rootPP == NULL);
499 know (*lastPP == NULL);
500 addme->sy_next = NULL;
501 #ifdef SYMBOLS_NEED_BACKPOINTERS
502 addme->sy_previous = NULL;
507 } /* if the list is empty */
509 if (target->sy_next != NULL)
511 #ifdef SYMBOLS_NEED_BACKPOINTERS
512 target->sy_next->sy_previous = addme;
513 #endif /* SYMBOLS_NEED_BACKPOINTERS */
517 know (*lastPP == target);
519 } /* if we have a next */
521 addme->sy_next = target->sy_next;
522 target->sy_next = addme;
524 #ifdef SYMBOLS_NEED_BACKPOINTERS
525 addme->sy_previous = target;
526 #endif /* SYMBOLS_NEED_BACKPOINTERS */
528 debug_verify_symchain (symbol_rootP, symbol_lastP);
531 /* Set the chain pointers of SYMBOL to null. */
533 symbol_clear_list_pointers (symbolP)
536 symbolP->sy_next = NULL;
537 #ifdef SYMBOLS_NEED_BACKPOINTERS
538 symbolP->sy_previous = NULL;
542 #ifdef SYMBOLS_NEED_BACKPOINTERS
543 /* Remove SYMBOLP from the list. */
545 symbol_remove (symbolP, rootPP, lastPP)
550 if (symbolP == *rootPP)
552 *rootPP = symbolP->sy_next;
553 } /* if it was the root */
555 if (symbolP == *lastPP)
557 *lastPP = symbolP->sy_previous;
558 } /* if it was the tail */
560 if (symbolP->sy_next != NULL)
562 symbolP->sy_next->sy_previous = symbolP->sy_previous;
565 if (symbolP->sy_previous != NULL)
567 symbolP->sy_previous->sy_next = symbolP->sy_next;
570 debug_verify_symchain (*rootPP, *lastPP);
573 /* Link symbol ADDME before symbol TARGET in the chain. */
575 symbol_insert (addme, target, rootPP, lastPP)
581 if (target->sy_previous != NULL)
583 target->sy_previous->sy_next = addme;
587 know (*rootPP == target);
591 addme->sy_previous = target->sy_previous;
592 target->sy_previous = addme;
593 addme->sy_next = target;
595 debug_verify_symchain (*rootPP, *lastPP);
598 #endif /* SYMBOLS_NEED_BACKPOINTERS */
601 verify_symbol_chain (rootP, lastP)
605 symbolS *symbolP = rootP;
610 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
612 #ifdef SYMBOLS_NEED_BACKPOINTERS
613 assert (symbolP->sy_next->sy_previous == symbolP);
615 /* Walk the list anyways, to make sure pointers are still good. */
617 #endif /* SYMBOLS_NEED_BACKPOINTERS */
620 assert (lastP == symbolP);
624 verify_symbol_chain_2 (sym)
627 symbolS *p = sym, *n = sym;
628 #ifdef SYMBOLS_NEED_BACKPOINTERS
629 while (symbol_previous (p))
630 p = symbol_previous (p);
632 while (symbol_next (n))
634 verify_symbol_chain (p, n);
637 /* Resolve the value of a symbol. This is called during the final
638 pass over the symbol table to resolve any symbols with complex
642 resolve_symbol_value (symp, finalize)
650 if (symp->sy_resolved)
652 if (symp->sy_value.X_op == O_constant)
653 return (valueT) symp->sy_value.X_add_number;
659 final_seg = S_GET_SEGMENT (symp);
661 if (symp->sy_resolving)
664 as_bad ("Symbol definition loop encountered at %s", S_GET_NAME (symp));
670 symbolS *add_symbol, *op_symbol;
672 segT seg_left, seg_right;
675 symp->sy_resolving = 1;
677 /* Help out with CSE. */
678 add_symbol = symp->sy_value.X_add_symbol;
679 op_symbol = symp->sy_value.X_op_symbol;
680 final_val = symp->sy_value.X_add_number;
681 op = symp->sy_value.X_op;
694 final_val += symp->sy_frag->fr_address;
695 if (final_seg == expr_section)
696 final_seg = absolute_section;
702 left = resolve_symbol_value (add_symbol, finalize);
705 if (symp->sy_mri_common)
707 /* This is a symbol inside an MRI common section. The
708 relocation routines are going to handle it specially.
709 Don't change the value. */
710 resolved = add_symbol->sy_resolved;
714 if (finalize && final_val == 0)
715 copy_symbol_attributes (symp, add_symbol);
717 /* If we have equated this symbol to an undefined symbol, we
718 keep X_op set to O_symbol, and we don't change
719 X_add_number. This permits the routine which writes out
720 relocation to detect this case, and convert the
721 relocation to be against the symbol to which this symbol
723 if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
727 symp->sy_value.X_op = O_symbol;
728 S_SET_SEGMENT (symp, S_GET_SEGMENT (add_symbol));
729 symp->sy_value.X_add_number = final_val;
732 resolved = add_symbol->sy_resolved;
733 goto exit_dont_set_value;
737 final_val += symp->sy_frag->fr_address + left;
738 if (final_seg == expr_section || final_seg == undefined_section)
739 final_seg = S_GET_SEGMENT (add_symbol);
742 resolved = add_symbol->sy_resolved;
748 left = resolve_symbol_value (add_symbol, finalize);
752 else if (op == O_logical_not)
757 final_val += left + symp->sy_frag->fr_address;
758 if (final_seg == expr_section || final_seg == undefined_section)
759 final_seg = absolute_section;
761 resolved = add_symbol->sy_resolved;
769 case O_bit_inclusive_or:
771 case O_bit_exclusive_or:
783 left = resolve_symbol_value (add_symbol, finalize);
784 right = resolve_symbol_value (op_symbol, finalize);
785 seg_left = S_GET_SEGMENT (add_symbol);
786 seg_right = S_GET_SEGMENT (op_symbol);
788 /* Simplify addition or subtraction of a constant by folding the
789 constant into X_add_number. */
790 if (op == O_add || op == O_subtract)
792 if (seg_right == absolute_section)
802 else if (seg_left == absolute_section && op == O_add)
806 add_symbol = op_symbol;
813 /* Subtraction is permitted if both operands are in the same
814 section. Otherwise, both operands must be absolute. We
815 already handled the case of addition or subtraction of a
816 constant above. This will probably need to be changed
817 for an object file format which supports arbitrary
818 expressions, such as IEEE-695. */
819 /* Don't emit messages unless we're finalizing the symbol value,
820 otherwise we may get the same message multiple times. */
821 if ((seg_left != absolute_section || seg_right != absolute_section)
822 && (op != O_subtract || seg_left != seg_right)
828 if (expr_symbol_where (symp, &file, &line))
830 if (seg_left == undefined_section)
831 as_bad_where (file, line,
832 "undefined symbol %s in operation",
833 S_GET_NAME (symp->sy_value.X_add_symbol));
834 if (seg_right == undefined_section)
835 as_bad_where (file, line,
836 "undefined symbol %s in operation",
837 S_GET_NAME (symp->sy_value.X_op_symbol));
838 if (seg_left != undefined_section
839 && seg_right != undefined_section)
840 as_bad_where (file, line, "invalid section for operation");
844 if (seg_left == undefined_section)
845 as_bad ("undefined symbol %s in operation setting %s",
846 S_GET_NAME (symp->sy_value.X_add_symbol),
848 if (seg_right == undefined_section)
849 as_bad ("undefined symbol %s in operation setting %s",
850 S_GET_NAME (symp->sy_value.X_op_symbol),
852 if (seg_left != undefined_section
853 && seg_right != undefined_section)
854 as_bad ("invalid section for operation setting %s",
859 /* Check for division by zero. */
860 if ((op == O_divide || op == O_modulus) && right == 0)
862 /* If seg_right is not absolute_section, then we've
863 already issued a warning about using a bad symbol. */
864 if (seg_right == absolute_section && finalize)
869 if (expr_symbol_where (symp, &file, &line))
870 as_bad_where (file, line, "division by zero");
872 as_bad ("division by zero when setting %s",
879 switch (symp->sy_value.X_op)
881 case O_multiply: left *= right; break;
882 case O_divide: left /= right; break;
883 case O_modulus: left %= right; break;
884 case O_left_shift: left <<= right; break;
885 case O_right_shift: left >>= right; break;
886 case O_bit_inclusive_or: left |= right; break;
887 case O_bit_or_not: left |= ~right; break;
888 case O_bit_exclusive_or: left ^= right; break;
889 case O_bit_and: left &= right; break;
890 case O_add: left += right; break;
891 case O_subtract: left -= right; break;
892 case O_eq: left = left == right ? ~ (offsetT) 0 : 0; break;
893 case O_ne: left = left != right ? ~ (offsetT) 0 : 0; break;
894 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
895 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
896 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
897 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
898 case O_logical_and: left = left && right; break;
899 case O_logical_or: left = left || right; break;
903 final_val += symp->sy_frag->fr_address + left;
904 if (final_seg == expr_section || final_seg == undefined_section)
905 final_seg = absolute_section;
906 resolved = (add_symbol->sy_resolved && op_symbol->sy_resolved);
912 /* Give an error (below) if not in expr_section. We don't
913 want to worry about expr_section symbols, because they
914 are fictional (they are created as part of expression
915 resolution), and any problems may not actually mean
920 symp->sy_resolving = 0;
925 S_SET_VALUE (symp, final_val);
927 #if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
928 /* The old a.out backend does not handle S_SET_SEGMENT correctly
929 for a stab symbol, so we use this bad hack. */
930 if (final_seg != S_GET_SEGMENT (symp))
932 S_SET_SEGMENT (symp, final_seg);
936 /* Don't worry if we can't resolve an expr_section symbol. */
940 symp->sy_resolved = 1;
941 else if (S_GET_SEGMENT (symp) != expr_section)
943 as_bad ("can't resolve value for symbol \"%s\"", S_GET_NAME (symp));
944 symp->sy_resolved = 1;
951 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
952 They are *really* local. That is, they go out of scope whenever we see a
953 label that isn't local. Also, like fb labels, there can be multiple
954 instances of a dollar label. Therefor, we name encode each instance with
955 the instance number, keep a list of defined symbols separate from the real
956 symbol table, and we treat these buggers as a sparse array. */
958 static long *dollar_labels;
959 static long *dollar_label_instances;
960 static char *dollar_label_defines;
961 static unsigned long dollar_label_count;
962 static unsigned long dollar_label_max;
965 dollar_label_defined (label)
970 know ((dollar_labels != NULL) || (dollar_label_count == 0));
972 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
974 return dollar_label_defines[i - dollar_labels];
976 /* if we get here, label isn't defined */
978 } /* dollar_label_defined() */
981 dollar_label_instance (label)
986 know ((dollar_labels != NULL) || (dollar_label_count == 0));
988 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
990 return (dollar_label_instances[i - dollar_labels]);
992 /* If we get here, we haven't seen the label before, therefore its instance
998 dollar_label_clear ()
1000 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1003 #define DOLLAR_LABEL_BUMP_BY 10
1006 define_dollar_label (label)
1011 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1014 ++dollar_label_instances[i - dollar_labels];
1015 dollar_label_defines[i - dollar_labels] = 1;
1019 /* if we get to here, we don't have label listed yet. */
1021 if (dollar_labels == NULL)
1023 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1024 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1025 dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1026 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1027 dollar_label_count = 0;
1029 else if (dollar_label_count == dollar_label_max)
1031 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1032 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1033 dollar_label_max * sizeof (long));
1034 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1035 dollar_label_max * sizeof (long));
1036 dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1037 } /* if we needed to grow */
1039 dollar_labels[dollar_label_count] = label;
1040 dollar_label_instances[dollar_label_count] = 1;
1041 dollar_label_defines[dollar_label_count] = 1;
1042 ++dollar_label_count;
1046 * dollar_label_name()
1048 * Caller must copy returned name: we re-use the area for the next name.
1050 * The mth occurence of label n: is turned into the symbol "Ln^Am"
1051 * where n is the label number and m is the instance number. "L" makes
1052 * it a label discarded unless debugging and "^A"('\1') ensures no
1053 * ordinary symbol SHOULD get the same name as a local label
1054 * symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1056 * fb labels get the same treatment, except that ^B is used in place of ^A.
1059 char * /* Return local label name. */
1060 dollar_label_name (n, augend)
1061 register long n; /* we just saw "n$:" : n a number */
1062 register int augend; /* 0 for current instance, 1 for new instance */
1065 /* Returned to caller, then copied. used for created names ("4f") */
1066 static char symbol_name_build[24];
1069 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1072 know (augend == 0 || augend == 1);
1073 p = symbol_name_build;
1076 /* Next code just does sprintf( {}, "%d", n); */
1078 q = symbol_name_temporary;
1079 for (*q++ = 0, i = n; i; ++q)
1084 while ((*p = *--q) != '\0')
1089 /* instance number */
1090 q = symbol_name_temporary;
1091 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1096 while ((*p++ = *--q) != '\0');;
1098 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1099 return symbol_name_build;
1103 * Sombody else's idea of local labels. They are made by "n:" where n
1104 * is any decimal digit. Refer to them with
1105 * "nb" for previous (backward) n:
1106 * or "nf" for next (forward) n:.
1108 * We do a little better and let n be any number, not just a single digit, but
1109 * since the other guy's assembler only does ten, we treat the first ten
1112 * Like someone else's assembler, we have one set of local label counters for
1113 * entire assembly, not one set per (sub)segment like in most assemblers. This
1114 * implies that one can refer to a label in another segment, and indeed some
1115 * crufty compilers have done just that.
1117 * Since there could be a LOT of these things, treat them as a sparse array.
1120 #define FB_LABEL_SPECIAL (10)
1122 static long fb_low_counter[FB_LABEL_SPECIAL];
1123 static long *fb_labels;
1124 static long *fb_label_instances;
1125 static long fb_label_count;
1126 static long fb_label_max;
1128 /* this must be more than FB_LABEL_SPECIAL */
1129 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1134 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1135 } /* fb_label_init() */
1137 /* add one to the instance number of this fb label */
1139 fb_label_instance_inc (label)
1144 if (label < FB_LABEL_SPECIAL)
1146 ++fb_low_counter[label];
1150 if (fb_labels != NULL)
1152 for (i = fb_labels + FB_LABEL_SPECIAL;
1153 i < fb_labels + fb_label_count; ++i)
1157 ++fb_label_instances[i - fb_labels];
1159 } /* if we find it */
1160 } /* for each existing label */
1163 /* if we get to here, we don't have label listed yet. */
1165 if (fb_labels == NULL)
1167 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1168 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1169 fb_label_max = FB_LABEL_BUMP_BY;
1170 fb_label_count = FB_LABEL_SPECIAL;
1173 else if (fb_label_count == fb_label_max)
1175 fb_label_max += FB_LABEL_BUMP_BY;
1176 fb_labels = (long *) xrealloc ((char *) fb_labels,
1177 fb_label_max * sizeof (long));
1178 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1179 fb_label_max * sizeof (long));
1180 } /* if we needed to grow */
1182 fb_labels[fb_label_count] = label;
1183 fb_label_instances[fb_label_count] = 1;
1188 fb_label_instance (label)
1193 if (label < FB_LABEL_SPECIAL)
1195 return (fb_low_counter[label]);
1198 if (fb_labels != NULL)
1200 for (i = fb_labels + FB_LABEL_SPECIAL;
1201 i < fb_labels + fb_label_count; ++i)
1205 return (fb_label_instances[i - fb_labels]);
1206 } /* if we find it */
1207 } /* for each existing label */
1210 /* We didn't find the label, so this must be a reference to the
1218 * Caller must copy returned name: we re-use the area for the next name.
1220 * The mth occurence of label n: is turned into the symbol "Ln^Bm"
1221 * where n is the label number and m is the instance number. "L" makes
1222 * it a label discarded unless debugging and "^B"('\2') ensures no
1223 * ordinary symbol SHOULD get the same name as a local label
1224 * symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1226 * dollar labels get the same treatment, except that ^A is used in place of ^B. */
1228 char * /* Return local label name. */
1229 fb_label_name (n, augend)
1230 long n; /* we just saw "n:", "nf" or "nb" : n a number */
1231 long augend; /* 0 for nb, 1 for n:, nf */
1234 /* Returned to caller, then copied. used for created names ("4f") */
1235 static char symbol_name_build[24];
1238 char symbol_name_temporary[20]; /* build up a number, BACKWARDS */
1241 know (augend == 0 || augend == 1);
1242 p = symbol_name_build;
1245 /* Next code just does sprintf( {}, "%d", n); */
1247 q = symbol_name_temporary;
1248 for (*q++ = 0, i = n; i; ++q)
1253 while ((*p = *--q) != '\0')
1258 /* instance number */
1259 q = symbol_name_temporary;
1260 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1265 while ((*p++ = *--q) != '\0');;
1267 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1268 return (symbol_name_build);
1269 } /* fb_label_name() */
1272 * decode name that may have been generated by foo_label_name() above. If
1273 * the name wasn't generated by foo_label_name(), then return it unaltered.
1274 * This is used for error messages.
1278 decode_local_label_name (s)
1282 char *symbol_decode;
1284 int instance_number;
1286 const char *message_format = "\"%d\" (instance number %d of a %s label)";
1291 for (label_number = 0, p = s + 1; isdigit ((unsigned char) *p); ++p)
1292 label_number = (10 * label_number) + *p - '0';
1301 for (instance_number = 0, p++; isdigit ((unsigned char) *p); ++p)
1302 instance_number = (10 * instance_number) + *p - '0';
1304 symbol_decode = obstack_alloc (¬es, strlen (message_format) + 30);
1305 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1307 return symbol_decode;
1310 /* Get the value of a symbol. */
1316 if (!s->sy_resolved && s->sy_value.X_op != O_constant)
1317 resolve_symbol_value (s, 1);
1318 if (s->sy_value.X_op != O_constant)
1320 static symbolS *recur;
1322 /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1323 may call S_GET_VALUE. We use a static symbol to avoid the
1324 immediate recursion. */
1326 return (valueT) s->sy_value.X_add_number;
1328 if (! s->sy_resolved
1329 || s->sy_value.X_op != O_symbol
1330 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1331 as_bad ("Attempt to get value of unresolved symbol %s",
1335 return (valueT) s->sy_value.X_add_number;
1338 /* Set the value of a symbol. */
1341 S_SET_VALUE (s, val)
1345 s->sy_value.X_op = O_constant;
1346 s->sy_value.X_add_number = (offsetT) val;
1347 s->sy_value.X_unsigned = 0;
1351 copy_symbol_attributes (dest, src)
1352 symbolS *dest, *src;
1354 #ifdef BFD_ASSEMBLER
1355 /* In an expression, transfer the settings of these flags.
1356 The user can override later, of course. */
1357 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT)
1358 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1361 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1362 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1366 #ifdef BFD_ASSEMBLER
1372 flagword flags = s->bsym->flags;
1375 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1378 return (flags & BSF_GLOBAL) != 0;
1385 return (s->bsym->flags & BSF_WEAK) != 0;
1392 return bfd_is_com_section (s->bsym->section);
1399 return s->bsym->section != undefined_section;
1406 if (s->bsym->flags & BSF_DEBUGGING)
1415 flagword flags = s->bsym->flags;
1419 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1422 if (bfd_get_section (s->bsym) == reg_section)
1425 if (flag_strip_local_absolute
1426 && (flags & BSF_GLOBAL) == 0
1427 && bfd_get_section (s->bsym) == absolute_section)
1430 name = S_GET_NAME (s);
1431 return (name != NULL
1433 && (strchr (name, '\001')
1434 || strchr (name, '\002')
1435 || (! flag_keep_locals
1436 && (bfd_is_local_label (stdoutput, s->bsym)
1439 && name[1] == '?')))));
1446 return S_IS_EXTERNAL (s);
1453 return S_GET_NAME (s) == 0;
1460 return s->bsym->name;
1467 return s->bsym->section;
1471 S_SET_SEGMENT (s, seg)
1475 /* Don't reassign section symbols. The direct reason is to prevent seg
1476 faults assigning back to const global symbols such as *ABS*, but it
1477 shouldn't happen anyway. */
1479 if (s->bsym->flags & BSF_SECTION_SYM)
1481 if (s->bsym->section != seg)
1485 s->bsym->section = seg;
1492 if ((s->bsym->flags & BSF_WEAK) != 0)
1494 /* Let .weak override .global. */
1497 s->bsym->flags |= BSF_GLOBAL;
1498 s->bsym->flags &= ~(BSF_LOCAL|BSF_WEAK);
1502 S_CLEAR_EXTERNAL (s)
1505 if ((s->bsym->flags & BSF_WEAK) != 0)
1507 /* Let .weak override. */
1510 s->bsym->flags |= BSF_LOCAL;
1511 s->bsym->flags &= ~(BSF_GLOBAL|BSF_WEAK);
1518 s->bsym->flags |= BSF_WEAK;
1519 s->bsym->flags &= ~(BSF_GLOBAL|BSF_LOCAL);
1523 S_SET_NAME (s, name)
1527 s->bsym->name = name;
1529 #endif /* BFD_ASSEMBLER */
1534 symbol_lastP = NULL;
1535 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
1536 sy_hash = hash_new ();
1538 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
1539 #ifdef BFD_ASSEMBLER
1540 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
1541 abs_symbol.bsym = bfd_abs_section.symbol;
1544 /* Can't initialise a union. Sigh. */
1545 S_SET_SEGMENT (&abs_symbol, absolute_section);
1547 abs_symbol.sy_value.X_op = O_constant;
1548 abs_symbol.sy_frag = &zero_address_frag;
1550 if (LOCAL_LABELS_FB)
1562 printf ("%*s", indent_level * 4, "");
1568 print_symbol_value_1 (file, sym)
1572 const char *name = S_GET_NAME (sym);
1573 if (!name || !name[0])
1575 fprintf (file, "sym %lx %s", (unsigned long) sym, name);
1576 if (sym->sy_frag != &zero_address_frag)
1577 fprintf (file, " frag %lx", (long) sym->sy_frag);
1579 fprintf (file, " written");
1580 if (sym->sy_resolved)
1581 fprintf (file, " resolved");
1582 else if (sym->sy_resolving)
1583 fprintf (file, " resolving");
1584 if (sym->sy_used_in_reloc)
1585 fprintf (file, " used-in-reloc");
1587 fprintf (file, " used");
1588 if (S_IS_LOCAL (sym))
1589 fprintf (file, " local");
1590 if (S_IS_EXTERN (sym))
1591 fprintf (file, " extern");
1592 if (S_IS_DEBUG (sym))
1593 fprintf (file, " debug");
1594 if (S_IS_DEFINED (sym))
1595 fprintf (file, " defined");
1596 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
1597 if (sym->sy_resolved)
1599 segT s = S_GET_SEGMENT (sym);
1601 if (s != undefined_section
1602 && s != expr_section)
1603 fprintf (file, " %lx", (long) S_GET_VALUE (sym));
1605 else if (indent_level < 8 && S_GET_SEGMENT (sym) != undefined_section)
1608 fprintf (file, "\n%*s<", indent_level * 4, "");
1609 print_expr_1 (file, &sym->sy_value);
1610 fprintf (file, ">");
1617 print_symbol_value (sym)
1621 print_symbol_value_1 (stderr, sym);
1622 fprintf (stderr, "\n");
1626 print_binary (file, name, exp)
1632 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
1633 print_symbol_value_1 (file, exp->X_add_symbol);
1634 fprintf (file, ">\n%*s<", indent_level * 4, "");
1635 print_symbol_value_1 (file, exp->X_op_symbol);
1636 fprintf (file, ">");
1641 print_expr_1 (file, exp)
1645 fprintf (file, "expr %lx ", (long) exp);
1649 fprintf (file, "illegal");
1652 fprintf (file, "absent");
1655 fprintf (file, "constant %lx", (long) exp->X_add_number);
1659 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
1660 print_symbol_value_1 (file, exp->X_add_symbol);
1661 fprintf (file, ">");
1663 if (exp->X_add_number)
1664 fprintf (file, "\n%*s%lx", indent_level * 4, "",
1665 (long) exp->X_add_number);
1669 fprintf (file, "register #%d", (int) exp->X_add_number);
1672 fprintf (file, "big");
1675 fprintf (file, "uminus -<");
1677 print_symbol_value_1 (file, exp->X_add_symbol);
1678 fprintf (file, ">");
1679 goto maybe_print_addnum;
1681 fprintf (file, "bit_not");
1684 print_binary (file, "multiply", exp);
1687 print_binary (file, "divide", exp);
1690 print_binary (file, "modulus", exp);
1693 print_binary (file, "lshift", exp);
1696 print_binary (file, "rshift", exp);
1698 case O_bit_inclusive_or:
1699 print_binary (file, "bit_ior", exp);
1701 case O_bit_exclusive_or:
1702 print_binary (file, "bit_xor", exp);
1705 print_binary (file, "bit_and", exp);
1708 print_binary (file, "eq", exp);
1711 print_binary (file, "ne", exp);
1714 print_binary (file, "lt", exp);
1717 print_binary (file, "le", exp);
1720 print_binary (file, "ge", exp);
1723 print_binary (file, "gt", exp);
1726 print_binary (file, "logical_and", exp);
1729 print_binary (file, "logical_or", exp);
1733 fprintf (file, "add\n%*s<", indent_level * 4, "");
1734 print_symbol_value_1 (file, exp->X_add_symbol);
1735 fprintf (file, ">\n%*s<", indent_level * 4, "");
1736 print_symbol_value_1 (file, exp->X_op_symbol);
1737 fprintf (file, ">");
1738 goto maybe_print_addnum;
1741 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
1742 print_symbol_value_1 (file, exp->X_add_symbol);
1743 fprintf (file, ">\n%*s<", indent_level * 4, "");
1744 print_symbol_value_1 (file, exp->X_op_symbol);
1745 fprintf (file, ">");
1746 goto maybe_print_addnum;
1748 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
1758 print_expr_1 (stderr, exp);
1759 fprintf (stderr, "\n");
1763 symbol_print_statistics (file)
1766 hash_print_statistics (file, "symbol table", sy_hash);
1769 /* end of symbols.c */