symbols.c revision 89857
1/* symbols.c -symbol table-
2   Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3   1999, 2000, 2001
4   Free Software Foundation, Inc.
5
6   This file is part of GAS, the GNU Assembler.
7
8   GAS is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2, or (at your option)
11   any later version.
12
13   GAS is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with GAS; see the file COPYING.  If not, write to the Free
20   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21   02111-1307, USA.  */
22
23/* #define DEBUG_SYMS / * to debug symbol list maintenance.  */
24
25#include "as.h"
26
27#include "safe-ctype.h"
28#include "obstack.h"		/* For "symbols.h" */
29#include "subsegs.h"
30
31#include "struc-symbol.h"
32
33/* This is non-zero if symbols are case sensitive, which is the
34   default.  */
35int symbols_case_sensitive = 1;
36
37#ifndef WORKING_DOT_WORD
38extern int new_broken_words;
39#endif
40
41/* symbol-name => struct symbol pointer */
42static struct hash_control *sy_hash;
43
44/* Table of local symbols.  */
45static struct hash_control *local_hash;
46
47/* Below are commented in "symbols.h".  */
48symbolS *symbol_rootP;
49symbolS *symbol_lastP;
50symbolS abs_symbol;
51
52#ifdef DEBUG_SYMS
53#define debug_verify_symchain verify_symbol_chain
54#else
55#define debug_verify_symchain(root, last) ((void) 0)
56#endif
57
58#define DOLLAR_LABEL_CHAR	'\001'
59#define LOCAL_LABEL_CHAR	'\002'
60
61struct obstack notes;
62
63static char *save_symbol_name PARAMS ((const char *));
64static void fb_label_init PARAMS ((void));
65static long dollar_label_instance PARAMS ((long));
66static long fb_label_instance PARAMS ((long));
67
68static void print_binary PARAMS ((FILE *, const char *, expressionS *));
69
70/* Return a pointer to a new symbol.  Die if we can't make a new
71   symbol.  Fill in the symbol's values.  Add symbol to end of symbol
72   chain.
73
74   This function should be called in the general case of creating a
75   symbol.  However, if the output file symbol table has already been
76   set, and you are certain that this symbol won't be wanted in the
77   output file, you can call symbol_create.  */
78
79symbolS *
80symbol_new (name, segment, valu, frag)
81     const char *name;
82     segT segment;
83     valueT valu;
84     fragS *frag;
85{
86  symbolS *symbolP = symbol_create (name, segment, valu, frag);
87
88  /* Link to end of symbol chain.  */
89#ifdef BFD_ASSEMBLER
90  {
91    extern int symbol_table_frozen;
92    if (symbol_table_frozen)
93      abort ();
94  }
95#endif
96  symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
97
98  return symbolP;
99}
100
101/* Save a symbol name on a permanent obstack, and convert it according
102   to the object file format.  */
103
104static char *
105save_symbol_name (name)
106     const char *name;
107{
108  unsigned int name_length;
109  char *ret;
110
111  name_length = strlen (name) + 1;	/* +1 for \0.  */
112  obstack_grow (&notes, name, name_length);
113  ret = obstack_finish (&notes);
114
115#ifdef STRIP_UNDERSCORE
116  if (ret[0] == '_')
117    ++ret;
118#endif
119
120#ifdef tc_canonicalize_symbol_name
121  ret = tc_canonicalize_symbol_name (ret);
122#endif
123
124  if (! symbols_case_sensitive)
125    {
126      char *s;
127
128      for (s = ret; *s != '\0'; s++)
129	*s = TOUPPER (*s);
130    }
131
132  return ret;
133}
134
135symbolS *
136symbol_create (name, segment, valu, frag)
137     const char *name;		/* It is copied, the caller can destroy/modify.  */
138     segT segment;		/* Segment identifier (SEG_<something>).  */
139     valueT valu;		/* Symbol value.  */
140     fragS *frag;		/* Associated fragment.  */
141{
142  char *preserved_copy_of_name;
143  symbolS *symbolP;
144
145  preserved_copy_of_name = save_symbol_name (name);
146
147  symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
148
149  /* symbol must be born in some fixed state.  This seems as good as any.  */
150  memset (symbolP, 0, sizeof (symbolS));
151
152#ifdef BFD_ASSEMBLER
153  symbolP->bsym = bfd_make_empty_symbol (stdoutput);
154  if (symbolP->bsym == NULL)
155    as_perror ("%s", "bfd_make_empty_symbol");
156  symbolP->bsym->udata.p = (PTR) symbolP;
157#endif
158  S_SET_NAME (symbolP, preserved_copy_of_name);
159
160  S_SET_SEGMENT (symbolP, segment);
161  S_SET_VALUE (symbolP, valu);
162  symbol_clear_list_pointers (symbolP);
163
164  symbolP->sy_frag = frag;
165#ifndef BFD_ASSEMBLER
166  symbolP->sy_number = ~0;
167  symbolP->sy_name_offset = (unsigned int) ~0;
168#endif
169
170  obj_symbol_new_hook (symbolP);
171
172#ifdef tc_symbol_new_hook
173  tc_symbol_new_hook (symbolP);
174#endif
175
176  return symbolP;
177}
178
179#ifdef BFD_ASSEMBLER
180
181/* Local symbol support.  If we can get away with it, we keep only a
182   small amount of information for local symbols.  */
183
184static struct local_symbol *local_symbol_make PARAMS ((const char *, segT,
185						       valueT, fragS *));
186static symbolS *local_symbol_convert PARAMS ((struct local_symbol *));
187
188/* Used for statistics.  */
189
190static unsigned long local_symbol_count;
191static unsigned long local_symbol_conversion_count;
192
193/* This macro is called with a symbol argument passed by reference.
194   It returns whether this is a local symbol.  If necessary, it
195   changes its argument to the real symbol.  */
196
197#define LOCAL_SYMBOL_CHECK(s)						\
198  (s->bsym == NULL							\
199   ? (local_symbol_converted_p ((struct local_symbol *) s)		\
200      ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s),	\
201	 0)								\
202      : 1)								\
203   : 0)
204
205/* Create a local symbol and insert it into the local hash table.  */
206
207static struct local_symbol *
208local_symbol_make (name, section, value, frag)
209     const char *name;
210     segT section;
211     valueT value;
212     fragS *frag;
213{
214  char *name_copy;
215  struct local_symbol *ret;
216
217  ++local_symbol_count;
218
219  name_copy = save_symbol_name (name);
220
221  ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
222  ret->lsy_marker = NULL;
223  ret->lsy_name = name_copy;
224  ret->lsy_section = section;
225  local_symbol_set_frag (ret, frag);
226  ret->lsy_value = value;
227
228  hash_jam (local_hash, name_copy, (PTR) ret);
229
230  return ret;
231}
232
233/* Convert a local symbol into a real symbol.  Note that we do not
234   reclaim the space used by the local symbol.  */
235
236static symbolS *
237local_symbol_convert (locsym)
238     struct local_symbol *locsym;
239{
240  symbolS *ret;
241
242  assert (locsym->lsy_marker == NULL);
243  if (local_symbol_converted_p (locsym))
244    return local_symbol_get_real_symbol (locsym);
245
246  ++local_symbol_conversion_count;
247
248  ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
249		    local_symbol_get_frag (locsym));
250
251  if (local_symbol_resolved_p (locsym))
252    ret->sy_resolved = 1;
253
254  /* Local symbols are always either defined or used.  */
255  ret->sy_used = 1;
256
257#ifdef TC_LOCAL_SYMFIELD_CONVERT
258  TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
259#endif
260
261  symbol_table_insert (ret);
262
263  local_symbol_mark_converted (locsym);
264  local_symbol_set_real_symbol (locsym, ret);
265
266  hash_jam (local_hash, locsym->lsy_name, NULL);
267
268  return ret;
269}
270
271#else /* ! BFD_ASSEMBLER */
272
273#define LOCAL_SYMBOL_CHECK(s) 0
274#define local_symbol_convert(s) ((symbolS *) s)
275
276#endif /* ! BFD_ASSEMBLER */
277
278/* We have just seen "<name>:".
279   Creates a struct symbol unless it already exists.
280
281   Gripes if we are redefining a symbol incompatibly (and ignores it).  */
282
283symbolS *
284colon (sym_name)		/* Just seen "x:" - rattle symbols & frags.  */
285     const char *sym_name;	/* Symbol name, as a cannonical string.  */
286     /* We copy this string: OK to alter later.  */
287{
288  register symbolS *symbolP;	/* Symbol we are working with.  */
289
290  /* Sun local labels go out of scope whenever a non-local symbol is
291     defined.  */
292  if (LOCAL_LABELS_DOLLAR)
293    {
294      int local;
295
296#ifdef BFD_ASSEMBLER
297      local = bfd_is_local_label_name (stdoutput, sym_name);
298#else
299      local = LOCAL_LABEL (sym_name);
300#endif
301
302      if (! local)
303	dollar_label_clear ();
304    }
305
306#ifndef WORKING_DOT_WORD
307  if (new_broken_words)
308    {
309      struct broken_word *a;
310      int possible_bytes;
311      fragS *frag_tmp;
312      char *frag_opcode;
313
314      extern const int md_short_jump_size;
315      extern const int md_long_jump_size;
316      possible_bytes = (md_short_jump_size
317			+ new_broken_words * md_long_jump_size);
318
319      frag_tmp = frag_now;
320      frag_opcode = frag_var (rs_broken_word,
321			      possible_bytes,
322			      possible_bytes,
323			      (relax_substateT) 0,
324			      (symbolS *) broken_words,
325			      (offsetT) 0,
326			      NULL);
327
328      /* We want to store the pointer to where to insert the jump
329	 table in the fr_opcode of the rs_broken_word frag.  This
330	 requires a little hackery.  */
331      while (frag_tmp
332	     && (frag_tmp->fr_type != rs_broken_word
333		 || frag_tmp->fr_opcode))
334	frag_tmp = frag_tmp->fr_next;
335      know (frag_tmp);
336      frag_tmp->fr_opcode = frag_opcode;
337      new_broken_words = 0;
338
339      for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
340	a->dispfrag = frag_tmp;
341    }
342#endif /* WORKING_DOT_WORD */
343
344  if ((symbolP = symbol_find (sym_name)) != 0)
345    {
346#ifdef RESOLVE_SYMBOL_REDEFINITION
347      if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
348	return symbolP;
349#endif
350      /* Now check for undefined symbols.  */
351      if (LOCAL_SYMBOL_CHECK (symbolP))
352	{
353#ifdef BFD_ASSEMBLER
354	  struct local_symbol *locsym = (struct local_symbol *) symbolP;
355
356	  if (locsym->lsy_section != undefined_section
357	      && (local_symbol_get_frag (locsym) != frag_now
358		  || locsym->lsy_section != now_seg
359		  || locsym->lsy_value != frag_now_fix ()))
360	    {
361	      as_bad (_("symbol `%s' is already defined"), sym_name);
362	      return symbolP;
363	    }
364
365	  locsym->lsy_section = now_seg;
366	  local_symbol_set_frag (locsym, frag_now);
367	  locsym->lsy_value = frag_now_fix ();
368#endif
369	}
370      else if (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
371	{
372	  if (S_GET_VALUE (symbolP) == 0)
373	    {
374	      symbolP->sy_frag = frag_now;
375#ifdef OBJ_VMS
376	      S_SET_OTHER (symbolP, const_flag);
377#endif
378	      S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
379	      S_SET_SEGMENT (symbolP, now_seg);
380#ifdef N_UNDF
381	      know (N_UNDF == 0);
382#endif /* if we have one, it better be zero.  */
383
384	    }
385	  else
386	    {
387	      /* There are still several cases to check:
388
389		 A .comm/.lcomm symbol being redefined as initialized
390		 data is OK
391
392		 A .comm/.lcomm symbol being redefined with a larger
393		 size is also OK
394
395		 This only used to be allowed on VMS gas, but Sun cc
396		 on the sparc also depends on it.  */
397
398	      if (((!S_IS_DEBUG (symbolP)
399		    && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
400		    && S_IS_EXTERNAL (symbolP))
401		   || S_GET_SEGMENT (symbolP) == bss_section)
402		  && (now_seg == data_section
403		      || now_seg == S_GET_SEGMENT (symbolP)))
404		{
405		  /* Select which of the 2 cases this is.  */
406		  if (now_seg != data_section)
407		    {
408		      /* New .comm for prev .comm symbol.
409
410			 If the new size is larger we just change its
411			 value.  If the new size is smaller, we ignore
412			 this symbol.  */
413		      if (S_GET_VALUE (symbolP)
414			  < ((unsigned) frag_now_fix ()))
415			{
416			  S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
417			}
418		    }
419		  else
420		    {
421		      /* It is a .comm/.lcomm being converted to initialized
422			 data.  */
423		      symbolP->sy_frag = frag_now;
424#ifdef OBJ_VMS
425		      S_SET_OTHER (symbolP, const_flag);
426#endif
427		      S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
428		      S_SET_SEGMENT (symbolP, now_seg);	/* Keep N_EXT bit.  */
429		    }
430		}
431	      else
432		{
433#if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
434     && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
435		  static const char *od_buf = "";
436#else
437		  char od_buf[100];
438		  od_buf[0] = '\0';
439#ifdef BFD_ASSEMBLER
440		  if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
441#endif
442		    sprintf(od_buf, "%d.%d.",
443			    S_GET_OTHER (symbolP),
444			    S_GET_DESC (symbolP));
445#endif
446		  as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
447			    sym_name,
448			    segment_name (S_GET_SEGMENT (symbolP)),
449			    od_buf,
450			    (long) S_GET_VALUE (symbolP));
451		}
452	    }			/* if the undefined symbol has no value  */
453	}
454      else
455	{
456	  /* Don't blow up if the definition is the same.  */
457	  if (!(frag_now == symbolP->sy_frag
458		&& S_GET_VALUE (symbolP) == frag_now_fix ()
459		&& S_GET_SEGMENT (symbolP) == now_seg))
460	    as_bad (_("symbol `%s' is already defined"), sym_name);
461	}
462
463    }
464#ifdef BFD_ASSEMBLER
465  else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
466    {
467      symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
468					       (valueT) frag_now_fix (),
469					       frag_now);
470    }
471#endif /* BFD_ASSEMBLER */
472  else
473    {
474      symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
475			    frag_now);
476#ifdef OBJ_VMS
477      S_SET_OTHER (symbolP, const_flag);
478#endif /* OBJ_VMS */
479
480      symbol_table_insert (symbolP);
481    }
482
483  if (mri_common_symbol != NULL)
484    {
485      /* This symbol is actually being defined within an MRI common
486         section.  This requires special handling.  */
487      if (LOCAL_SYMBOL_CHECK (symbolP))
488	symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
489      symbolP->sy_value.X_op = O_symbol;
490      symbolP->sy_value.X_add_symbol = mri_common_symbol;
491      symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
492      symbolP->sy_frag = &zero_address_frag;
493      S_SET_SEGMENT (symbolP, expr_section);
494      symbolP->sy_mri_common = 1;
495    }
496
497#ifdef tc_frob_label
498  tc_frob_label (symbolP);
499#endif
500#ifdef obj_frob_label
501  obj_frob_label (symbolP);
502#endif
503
504  return symbolP;
505}
506
507/* Die if we can't insert the symbol.  */
508
509void
510symbol_table_insert (symbolP)
511     symbolS *symbolP;
512{
513  register const char *error_string;
514
515  know (symbolP);
516  know (S_GET_NAME (symbolP));
517
518  if (LOCAL_SYMBOL_CHECK (symbolP))
519    {
520      error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
521			       (PTR) symbolP);
522      if (error_string != NULL)
523	as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
524		  S_GET_NAME (symbolP), error_string);
525      return;
526    }
527
528  if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (PTR) symbolP)))
529    {
530      as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
531		S_GET_NAME (symbolP), error_string);
532    }				/* on error  */
533}
534
535/* If a symbol name does not exist, create it as undefined, and insert
536   it into the symbol table.  Return a pointer to it.  */
537
538symbolS *
539symbol_find_or_make (name)
540     const char *name;
541{
542  register symbolS *symbolP;
543
544  symbolP = symbol_find (name);
545
546  if (symbolP == NULL)
547    {
548#ifdef BFD_ASSEMBLER
549      if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
550	{
551	  symbolP = md_undefined_symbol ((char *) name);
552	  if (symbolP != NULL)
553	    return symbolP;
554
555	  symbolP = (symbolS *) local_symbol_make (name, undefined_section,
556						   (valueT) 0,
557						   &zero_address_frag);
558	  return symbolP;
559	}
560#endif
561
562      symbolP = symbol_make (name);
563
564      symbol_table_insert (symbolP);
565    }				/* if symbol wasn't found */
566
567  return (symbolP);
568}
569
570symbolS *
571symbol_make (name)
572     CONST char *name;
573{
574  symbolS *symbolP;
575
576  /* Let the machine description default it, e.g. for register names.  */
577  symbolP = md_undefined_symbol ((char *) name);
578
579  if (!symbolP)
580    symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
581
582  return (symbolP);
583}
584
585/* Implement symbol table lookup.
586   In:	A symbol's name as a string: '\0' can't be part of a symbol name.
587   Out:	NULL if the name was not in the symbol table, else the address
588   of a struct symbol associated with that name.  */
589
590symbolS *
591symbol_find (name)
592     CONST char *name;
593{
594#ifdef STRIP_UNDERSCORE
595  return (symbol_find_base (name, 1));
596#else /* STRIP_UNDERSCORE */
597  return (symbol_find_base (name, 0));
598#endif /* STRIP_UNDERSCORE */
599}
600
601symbolS *
602symbol_find_base (name, strip_underscore)
603     CONST char *name;
604     int strip_underscore;
605{
606  if (strip_underscore && *name == '_')
607    name++;
608
609#ifdef tc_canonicalize_symbol_name
610  {
611    char *copy;
612    size_t len = strlen (name) + 1;
613
614    copy = (char *) alloca (len);
615    memcpy (copy, name, len);
616    name = tc_canonicalize_symbol_name (copy);
617  }
618#endif
619
620  if (! symbols_case_sensitive)
621    {
622      char *copy;
623      const char *orig;
624      unsigned char c;
625
626      orig = name;
627      name = copy = (char *) alloca (strlen (name) + 1);
628
629      while ((c = *orig++) != '\0')
630	{
631	  *copy++ = TOUPPER (c);
632	}
633      *copy = '\0';
634    }
635
636#ifdef BFD_ASSEMBLER
637  {
638    struct local_symbol *locsym;
639
640    locsym = (struct local_symbol *) hash_find (local_hash, name);
641    if (locsym != NULL)
642      return (symbolS *) locsym;
643  }
644#endif
645
646  return ((symbolS *) hash_find (sy_hash, name));
647}
648
649/* Once upon a time, symbols were kept in a singly linked list.  At
650   least coff needs to be able to rearrange them from time to time, for
651   which a doubly linked list is much more convenient.  Loic did these
652   as macros which seemed dangerous to me so they're now functions.
653   xoxorich.  */
654
655/* Link symbol ADDME after symbol TARGET in the chain.  */
656
657void
658symbol_append (addme, target, rootPP, lastPP)
659     symbolS *addme;
660     symbolS *target;
661     symbolS **rootPP;
662     symbolS **lastPP;
663{
664  if (LOCAL_SYMBOL_CHECK (addme))
665    abort ();
666  if (target != NULL && LOCAL_SYMBOL_CHECK (target))
667    abort ();
668
669  if (target == NULL)
670    {
671      know (*rootPP == NULL);
672      know (*lastPP == NULL);
673      addme->sy_next = NULL;
674#ifdef SYMBOLS_NEED_BACKPOINTERS
675      addme->sy_previous = NULL;
676#endif
677      *rootPP = addme;
678      *lastPP = addme;
679      return;
680    }				/* if the list is empty  */
681
682  if (target->sy_next != NULL)
683    {
684#ifdef SYMBOLS_NEED_BACKPOINTERS
685      target->sy_next->sy_previous = addme;
686#endif /* SYMBOLS_NEED_BACKPOINTERS */
687    }
688  else
689    {
690      know (*lastPP == target);
691      *lastPP = addme;
692    }				/* if we have a next  */
693
694  addme->sy_next = target->sy_next;
695  target->sy_next = addme;
696
697#ifdef SYMBOLS_NEED_BACKPOINTERS
698  addme->sy_previous = target;
699#endif /* SYMBOLS_NEED_BACKPOINTERS */
700
701  debug_verify_symchain (symbol_rootP, symbol_lastP);
702}
703
704/* Set the chain pointers of SYMBOL to null.  */
705
706void
707symbol_clear_list_pointers (symbolP)
708     symbolS *symbolP;
709{
710  if (LOCAL_SYMBOL_CHECK (symbolP))
711    abort ();
712  symbolP->sy_next = NULL;
713#ifdef SYMBOLS_NEED_BACKPOINTERS
714  symbolP->sy_previous = NULL;
715#endif
716}
717
718#ifdef SYMBOLS_NEED_BACKPOINTERS
719/* Remove SYMBOLP from the list.  */
720
721void
722symbol_remove (symbolP, rootPP, lastPP)
723     symbolS *symbolP;
724     symbolS **rootPP;
725     symbolS **lastPP;
726{
727  if (LOCAL_SYMBOL_CHECK (symbolP))
728    abort ();
729
730  if (symbolP == *rootPP)
731    {
732      *rootPP = symbolP->sy_next;
733    }				/* if it was the root  */
734
735  if (symbolP == *lastPP)
736    {
737      *lastPP = symbolP->sy_previous;
738    }				/* if it was the tail  */
739
740  if (symbolP->sy_next != NULL)
741    {
742      symbolP->sy_next->sy_previous = symbolP->sy_previous;
743    }				/* if not last  */
744
745  if (symbolP->sy_previous != NULL)
746    {
747      symbolP->sy_previous->sy_next = symbolP->sy_next;
748    }				/* if not first  */
749
750  debug_verify_symchain (*rootPP, *lastPP);
751}
752
753/* Link symbol ADDME before symbol TARGET in the chain.  */
754
755void
756symbol_insert (addme, target, rootPP, lastPP)
757     symbolS *addme;
758     symbolS *target;
759     symbolS **rootPP;
760     symbolS **lastPP ATTRIBUTE_UNUSED;
761{
762  if (LOCAL_SYMBOL_CHECK (addme))
763    abort ();
764  if (LOCAL_SYMBOL_CHECK (target))
765    abort ();
766
767  if (target->sy_previous != NULL)
768    {
769      target->sy_previous->sy_next = addme;
770    }
771  else
772    {
773      know (*rootPP == target);
774      *rootPP = addme;
775    }				/* if not first  */
776
777  addme->sy_previous = target->sy_previous;
778  target->sy_previous = addme;
779  addme->sy_next = target;
780
781  debug_verify_symchain (*rootPP, *lastPP);
782}
783
784#endif /* SYMBOLS_NEED_BACKPOINTERS */
785
786void
787verify_symbol_chain (rootP, lastP)
788     symbolS *rootP;
789     symbolS *lastP;
790{
791  symbolS *symbolP = rootP;
792
793  if (symbolP == NULL)
794    return;
795
796  for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
797    {
798#ifdef BFD_ASSEMBLER
799      assert (symbolP->bsym != NULL);
800#endif
801#ifdef SYMBOLS_NEED_BACKPOINTERS
802      assert (symbolP->sy_next->sy_previous == symbolP);
803#else
804      /* Walk the list anyways, to make sure pointers are still good.  */
805      ;
806#endif /* SYMBOLS_NEED_BACKPOINTERS */
807    }
808
809  assert (lastP == symbolP);
810}
811
812void
813verify_symbol_chain_2 (sym)
814     symbolS *sym;
815{
816  symbolS *p = sym, *n = sym;
817#ifdef SYMBOLS_NEED_BACKPOINTERS
818  while (symbol_previous (p))
819    p = symbol_previous (p);
820#endif
821  while (symbol_next (n))
822    n = symbol_next (n);
823  verify_symbol_chain (p, n);
824}
825
826/* Resolve the value of a symbol.  This is called during the final
827   pass over the symbol table to resolve any symbols with complex
828   values.  */
829
830valueT
831resolve_symbol_value (symp)
832     symbolS *symp;
833{
834  int resolved;
835  valueT final_val;
836  segT final_seg;
837
838#ifdef BFD_ASSEMBLER
839  if (LOCAL_SYMBOL_CHECK (symp))
840    {
841      struct local_symbol *locsym = (struct local_symbol *) symp;
842
843      final_val = locsym->lsy_value;
844      if (local_symbol_resolved_p (locsym))
845	return final_val;
846
847      final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
848
849      if (finalize_syms)
850	{
851	  locsym->lsy_value = final_val;
852	  local_symbol_mark_resolved (locsym);
853	}
854
855      return final_val;
856    }
857#endif
858
859  if (symp->sy_resolved)
860    {
861      if (symp->sy_value.X_op == O_constant)
862	return (valueT) symp->sy_value.X_add_number;
863      else
864	return 0;
865    }
866
867  resolved = 0;
868  final_seg = S_GET_SEGMENT (symp);
869
870  if (symp->sy_resolving)
871    {
872      if (finalize_syms)
873	as_bad (_("symbol definition loop encountered at `%s'"),
874		S_GET_NAME (symp));
875      final_val = 0;
876      resolved = 1;
877    }
878  else
879    {
880      symbolS *add_symbol, *op_symbol;
881      offsetT left, right;
882      segT seg_left, seg_right;
883      operatorT op;
884
885      symp->sy_resolving = 1;
886
887      /* Help out with CSE.  */
888      add_symbol = symp->sy_value.X_add_symbol;
889      op_symbol = symp->sy_value.X_op_symbol;
890      final_val = symp->sy_value.X_add_number;
891      op = symp->sy_value.X_op;
892
893      switch (op)
894	{
895	default:
896	  BAD_CASE (op);
897	  break;
898
899	case O_absent:
900	  final_val = 0;
901	  /* Fall through.  */
902
903	case O_constant:
904	  final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
905	  if (final_seg == expr_section)
906	    final_seg = absolute_section;
907	  resolved = 1;
908	  break;
909
910	case O_symbol:
911	case O_symbol_rva:
912	  left = resolve_symbol_value (add_symbol);
913	  seg_left = S_GET_SEGMENT (add_symbol);
914	  if (finalize_syms)
915	    symp->sy_value.X_op_symbol = NULL;
916
917	do_symbol:
918	  if (symp->sy_mri_common)
919	    {
920	      /* This is a symbol inside an MRI common section.  The
921		 relocation routines are going to handle it specially.
922		 Don't change the value.  */
923	      resolved = symbol_resolved_p (add_symbol);
924	      break;
925	    }
926
927	  if (finalize_syms && final_val == 0)
928	    {
929	      if (LOCAL_SYMBOL_CHECK (add_symbol))
930		add_symbol = local_symbol_convert ((struct local_symbol *)
931						   add_symbol);
932	      copy_symbol_attributes (symp, add_symbol);
933	    }
934
935	  /* If we have equated this symbol to an undefined or common
936	     symbol, keep X_op set to O_symbol, and don't change
937	     X_add_number.  This permits the routine which writes out
938	     relocation to detect this case, and convert the
939	     relocation to be against the symbol to which this symbol
940	     is equated.  */
941	  if (! S_IS_DEFINED (add_symbol) || S_IS_COMMON (add_symbol))
942	    {
943	      if (finalize_syms)
944		{
945		  symp->sy_value.X_op = O_symbol;
946		  symp->sy_value.X_add_symbol = add_symbol;
947		  symp->sy_value.X_add_number = final_val;
948		  /* Use X_op_symbol as a flag.  */
949		  symp->sy_value.X_op_symbol = add_symbol;
950		  final_seg = seg_left;
951		}
952	      final_val = 0;
953	      resolved = symbol_resolved_p (add_symbol);
954	      symp->sy_resolving = 0;
955	      goto exit_dont_set_value;
956	    }
957	  else if (finalize_syms && final_seg == expr_section
958		   && seg_left != expr_section)
959	    {
960	      /* If the symbol is an expression symbol, do similarly
961		 as for undefined and common syms above.  Handles
962		 "sym +/- expr" where "expr" cannot be evaluated
963		 immediately, and we want relocations to be against
964		 "sym", eg. because it is weak.  */
965	      symp->sy_value.X_op = O_symbol;
966	      symp->sy_value.X_add_symbol = add_symbol;
967	      symp->sy_value.X_add_number = final_val;
968	      symp->sy_value.X_op_symbol = add_symbol;
969	      final_seg = seg_left;
970	      final_val += symp->sy_frag->fr_address + left;
971	      resolved = symbol_resolved_p (add_symbol);
972	      symp->sy_resolving = 0;
973	      goto exit_dont_set_value;
974	    }
975	  else
976	    {
977	      final_val += symp->sy_frag->fr_address + left;
978	      if (final_seg == expr_section || final_seg == undefined_section)
979		final_seg = seg_left;
980	    }
981
982	  resolved = symbol_resolved_p (add_symbol);
983	  break;
984
985	case O_uminus:
986	case O_bit_not:
987	case O_logical_not:
988	  left = resolve_symbol_value (add_symbol);
989
990	  if (op == O_uminus)
991	    left = -left;
992	  else if (op == O_logical_not)
993	    left = !left;
994	  else
995	    left = ~left;
996
997	  final_val += left + symp->sy_frag->fr_address;
998	  if (final_seg == expr_section || final_seg == undefined_section)
999	    final_seg = absolute_section;
1000
1001	  resolved = symbol_resolved_p (add_symbol);
1002	  break;
1003
1004	case O_multiply:
1005	case O_divide:
1006	case O_modulus:
1007	case O_left_shift:
1008	case O_right_shift:
1009	case O_bit_inclusive_or:
1010	case O_bit_or_not:
1011	case O_bit_exclusive_or:
1012	case O_bit_and:
1013	case O_add:
1014	case O_subtract:
1015	case O_eq:
1016	case O_ne:
1017	case O_lt:
1018	case O_le:
1019	case O_ge:
1020	case O_gt:
1021	case O_logical_and:
1022	case O_logical_or:
1023	  left = resolve_symbol_value (add_symbol);
1024	  right = resolve_symbol_value (op_symbol);
1025	  seg_left = S_GET_SEGMENT (add_symbol);
1026	  seg_right = S_GET_SEGMENT (op_symbol);
1027
1028	  /* Simplify addition or subtraction of a constant by folding the
1029	     constant into X_add_number.  */
1030	  if (op == O_add)
1031	    {
1032	      if (seg_right == absolute_section)
1033		{
1034		  final_val += right;
1035		  goto do_symbol;
1036		}
1037	      else if (seg_left == absolute_section)
1038		{
1039		  final_val += left;
1040		  add_symbol = op_symbol;
1041		  left = right;
1042		  seg_left = seg_right;
1043		  goto do_symbol;
1044		}
1045	    }
1046	  else if (op == O_subtract)
1047	    {
1048	      if (seg_right == absolute_section)
1049		{
1050		  final_val -= right;
1051		  goto do_symbol;
1052		}
1053	    }
1054
1055	  /* Equality and non-equality tests are permitted on anything.
1056	     Subtraction, and other comparison operators are permitted if
1057	     both operands are in the same section.  Otherwise, both
1058	     operands must be absolute.  We already handled the case of
1059	     addition or subtraction of a constant above.  This will
1060	     probably need to be changed for an object file format which
1061	     supports arbitrary expressions, such as IEEE-695.
1062
1063	     Don't emit messages unless we're finalizing the symbol value,
1064	     otherwise we may get the same message multiple times.  */
1065	  if (op != O_eq && op != O_ne
1066	      && (seg_left != absolute_section
1067		  || seg_right != absolute_section)
1068	      && ((op != O_subtract
1069		   && op != O_lt && op != O_le && op != O_ge && op != O_gt)
1070		  || seg_left != seg_right
1071		  || (seg_left == undefined_section
1072		      && add_symbol != op_symbol))
1073	      && finalize_syms)
1074	    {
1075	      char *file;
1076	      unsigned int line;
1077
1078	      if (expr_symbol_where (symp, &file, &line))
1079		{
1080		  if (seg_left == undefined_section)
1081		    as_bad_where (file, line,
1082				  _("undefined symbol `%s' in operation"),
1083				  S_GET_NAME (symp->sy_value.X_add_symbol));
1084		  if (seg_right == undefined_section)
1085		    as_bad_where (file, line,
1086				  _("undefined symbol `%s' in operation"),
1087				  S_GET_NAME (symp->sy_value.X_op_symbol));
1088		  if (seg_left != undefined_section
1089		      && seg_right != undefined_section)
1090		    as_bad_where (file, line,
1091				  _("invalid section for operation"));
1092		}
1093	      else
1094		{
1095		  if (seg_left == undefined_section)
1096		    as_bad (_("undefined symbol `%s' in operation setting `%s'"),
1097			    S_GET_NAME (symp->sy_value.X_add_symbol),
1098			    S_GET_NAME (symp));
1099		  if (seg_right == undefined_section)
1100		    as_bad (_("undefined symbol `%s' in operation setting `%s'"),
1101			    S_GET_NAME (symp->sy_value.X_op_symbol),
1102			    S_GET_NAME (symp));
1103		  if (seg_left != undefined_section
1104		      && seg_right != undefined_section)
1105		    as_bad (_("invalid section for operation setting `%s'"),
1106			    S_GET_NAME (symp));
1107		}
1108	    }
1109
1110	  /* Check for division by zero.  */
1111	  if ((op == O_divide || op == O_modulus) && right == 0)
1112	    {
1113	      /* If seg_right is not absolute_section, then we've
1114		 already issued a warning about using a bad symbol.  */
1115	      if (seg_right == absolute_section && finalize_syms)
1116		{
1117		  char *file;
1118		  unsigned int line;
1119
1120		  if (expr_symbol_where (symp, &file, &line))
1121		    as_bad_where (file, line, _("division by zero"));
1122		  else
1123		    as_bad (_("division by zero when setting `%s'"),
1124			    S_GET_NAME (symp));
1125		}
1126
1127	      right = 1;
1128	    }
1129
1130	  switch (symp->sy_value.X_op)
1131	    {
1132	    case O_multiply:		left *= right; break;
1133	    case O_divide:		left /= right; break;
1134	    case O_modulus:		left %= right; break;
1135	    case O_left_shift:		left <<= right; break;
1136	    case O_right_shift:		left >>= right; break;
1137	    case O_bit_inclusive_or:	left |= right; break;
1138	    case O_bit_or_not:		left |= ~right; break;
1139	    case O_bit_exclusive_or:	left ^= right; break;
1140	    case O_bit_and:		left &= right; break;
1141	    case O_add:			left += right; break;
1142	    case O_subtract:		left -= right; break;
1143	    case O_eq:
1144	    case O_ne:
1145	      left = (left == right && seg_left == seg_right
1146		      && (seg_left != undefined_section
1147			  || add_symbol == op_symbol)
1148		      ? ~ (offsetT) 0 : 0);
1149	      if (symp->sy_value.X_op == O_ne)
1150		left = ~left;
1151	      break;
1152	    case O_lt:	left = left <  right ? ~ (offsetT) 0 : 0; break;
1153	    case O_le:	left = left <= right ? ~ (offsetT) 0 : 0; break;
1154	    case O_ge:	left = left >= right ? ~ (offsetT) 0 : 0; break;
1155	    case O_gt:	left = left >  right ? ~ (offsetT) 0 : 0; break;
1156	    case O_logical_and:	left = left && right; break;
1157	    case O_logical_or:	left = left || right; break;
1158	    default:		abort ();
1159	    }
1160
1161	  final_val += symp->sy_frag->fr_address + left;
1162	  if (final_seg == expr_section || final_seg == undefined_section)
1163	    final_seg = absolute_section;
1164	  resolved = (symbol_resolved_p (add_symbol)
1165		      && symbol_resolved_p (op_symbol));
1166	  break;
1167
1168	case O_register:
1169	case O_big:
1170	case O_illegal:
1171	  /* Give an error (below) if not in expr_section.  We don't
1172	     want to worry about expr_section symbols, because they
1173	     are fictional (they are created as part of expression
1174	     resolution), and any problems may not actually mean
1175	     anything.  */
1176	  break;
1177	}
1178
1179      symp->sy_resolving = 0;
1180    }
1181
1182  if (finalize_syms)
1183    S_SET_VALUE (symp, final_val);
1184
1185exit_dont_set_value:
1186  /* Always set the segment, even if not finalizing the value.
1187     The segment is used to determine whether a symbol is defined.  */
1188#if defined (OBJ_AOUT) && ! defined (BFD_ASSEMBLER)
1189  /* The old a.out backend does not handle S_SET_SEGMENT correctly
1190     for a stab symbol, so we use this bad hack.  */
1191  if (final_seg != S_GET_SEGMENT (symp))
1192#endif
1193    S_SET_SEGMENT (symp, final_seg);
1194
1195  /* Don't worry if we can't resolve an expr_section symbol.  */
1196  if (finalize_syms)
1197    {
1198      if (resolved)
1199	symp->sy_resolved = 1;
1200      else if (S_GET_SEGMENT (symp) != expr_section)
1201	{
1202	  as_bad (_("can't resolve value for symbol `%s'"),
1203		  S_GET_NAME (symp));
1204	  symp->sy_resolved = 1;
1205	}
1206    }
1207
1208  return final_val;
1209}
1210
1211#ifdef BFD_ASSEMBLER
1212
1213static void resolve_local_symbol PARAMS ((const char *, PTR));
1214
1215/* A static function passed to hash_traverse.  */
1216
1217static void
1218resolve_local_symbol (key, value)
1219     const char *key ATTRIBUTE_UNUSED;
1220     PTR value;
1221{
1222  if (value != NULL)
1223    resolve_symbol_value (value);
1224}
1225
1226#endif
1227
1228/* Resolve all local symbols.  */
1229
1230void
1231resolve_local_symbol_values ()
1232{
1233#ifdef BFD_ASSEMBLER
1234  hash_traverse (local_hash, resolve_local_symbol);
1235#endif
1236}
1237
1238/* Dollar labels look like a number followed by a dollar sign.  Eg, "42$".
1239   They are *really* local.  That is, they go out of scope whenever we see a
1240   label that isn't local.  Also, like fb labels, there can be multiple
1241   instances of a dollar label.  Therefor, we name encode each instance with
1242   the instance number, keep a list of defined symbols separate from the real
1243   symbol table, and we treat these buggers as a sparse array.  */
1244
1245static long *dollar_labels;
1246static long *dollar_label_instances;
1247static char *dollar_label_defines;
1248static unsigned long dollar_label_count;
1249static unsigned long dollar_label_max;
1250
1251int
1252dollar_label_defined (label)
1253     long label;
1254{
1255  long *i;
1256
1257  know ((dollar_labels != NULL) || (dollar_label_count == 0));
1258
1259  for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1260    if (*i == label)
1261      return dollar_label_defines[i - dollar_labels];
1262
1263  /* If we get here, label isn't defined.  */
1264  return 0;
1265}
1266
1267static long
1268dollar_label_instance (label)
1269     long label;
1270{
1271  long *i;
1272
1273  know ((dollar_labels != NULL) || (dollar_label_count == 0));
1274
1275  for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1276    if (*i == label)
1277      return (dollar_label_instances[i - dollar_labels]);
1278
1279  /* If we get here, we haven't seen the label before.
1280     Therefore its instance count is zero.  */
1281  return 0;
1282}
1283
1284void
1285dollar_label_clear ()
1286{
1287  memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1288}
1289
1290#define DOLLAR_LABEL_BUMP_BY 10
1291
1292void
1293define_dollar_label (label)
1294     long label;
1295{
1296  long *i;
1297
1298  for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1299    if (*i == label)
1300      {
1301	++dollar_label_instances[i - dollar_labels];
1302	dollar_label_defines[i - dollar_labels] = 1;
1303	return;
1304      }
1305
1306  /* If we get to here, we don't have label listed yet.  */
1307
1308  if (dollar_labels == NULL)
1309    {
1310      dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1311      dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1312      dollar_label_defines = xmalloc (DOLLAR_LABEL_BUMP_BY);
1313      dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1314      dollar_label_count = 0;
1315    }
1316  else if (dollar_label_count == dollar_label_max)
1317    {
1318      dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1319      dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1320					 dollar_label_max * sizeof (long));
1321      dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1322					  dollar_label_max * sizeof (long));
1323      dollar_label_defines = xrealloc (dollar_label_defines, dollar_label_max);
1324    }				/* if we needed to grow  */
1325
1326  dollar_labels[dollar_label_count] = label;
1327  dollar_label_instances[dollar_label_count] = 1;
1328  dollar_label_defines[dollar_label_count] = 1;
1329  ++dollar_label_count;
1330}
1331
1332/* Caller must copy returned name: we re-use the area for the next name.
1333
1334   The mth occurence of label n: is turned into the symbol "Ln^Am"
1335   where n is the label number and m is the instance number. "L" makes
1336   it a label discarded unless debugging and "^A"('\1') ensures no
1337   ordinary symbol SHOULD get the same name as a local label
1338   symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1339
1340   fb labels get the same treatment, except that ^B is used in place
1341   of ^A.  */
1342
1343char *				/* Return local label name.  */
1344dollar_label_name (n, augend)
1345     register long n;		/* we just saw "n$:" : n a number.  */
1346     register int augend;	/* 0 for current instance, 1 for new instance.  */
1347{
1348  long i;
1349  /* Returned to caller, then copied.  Used for created names ("4f").  */
1350  static char symbol_name_build[24];
1351  register char *p;
1352  register char *q;
1353  char symbol_name_temporary[20];	/* Build up a number, BACKWARDS.  */
1354
1355  know (n >= 0);
1356  know (augend == 0 || augend == 1);
1357  p = symbol_name_build;
1358#ifdef LOCAL_LABEL_PREFIX
1359  *p++ = LOCAL_LABEL_PREFIX;
1360#endif
1361  *p++ = 'L';
1362
1363  /* Next code just does sprintf( {}, "%d", n);  */
1364  /* Label number.  */
1365  q = symbol_name_temporary;
1366  for (*q++ = 0, i = n; i; ++q)
1367    {
1368      *q = i % 10 + '0';
1369      i /= 10;
1370    }
1371  while ((*p = *--q) != '\0')
1372    ++p;
1373
1374  *p++ = DOLLAR_LABEL_CHAR;		/* ^A  */
1375
1376  /* Instance number.  */
1377  q = symbol_name_temporary;
1378  for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1379    {
1380      *q = i % 10 + '0';
1381      i /= 10;
1382    }
1383  while ((*p++ = *--q) != '\0');;
1384
1385  /* The label, as a '\0' ended string, starts at symbol_name_build.  */
1386  return symbol_name_build;
1387}
1388
1389/* Sombody else's idea of local labels. They are made by "n:" where n
1390   is any decimal digit. Refer to them with
1391    "nb" for previous (backward) n:
1392   or "nf" for next (forward) n:.
1393
1394   We do a little better and let n be any number, not just a single digit, but
1395   since the other guy's assembler only does ten, we treat the first ten
1396   specially.
1397
1398   Like someone else's assembler, we have one set of local label counters for
1399   entire assembly, not one set per (sub)segment like in most assemblers. This
1400   implies that one can refer to a label in another segment, and indeed some
1401   crufty compilers have done just that.
1402
1403   Since there could be a LOT of these things, treat them as a sparse
1404   array.  */
1405
1406#define FB_LABEL_SPECIAL (10)
1407
1408static long fb_low_counter[FB_LABEL_SPECIAL];
1409static long *fb_labels;
1410static long *fb_label_instances;
1411static long fb_label_count;
1412static long fb_label_max;
1413
1414/* This must be more than FB_LABEL_SPECIAL.  */
1415#define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1416
1417static void
1418fb_label_init ()
1419{
1420  memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1421}
1422
1423/* Add one to the instance number of this fb label.  */
1424
1425void
1426fb_label_instance_inc (label)
1427     long label;
1428{
1429  long *i;
1430
1431  if (label < FB_LABEL_SPECIAL)
1432    {
1433      ++fb_low_counter[label];
1434      return;
1435    }
1436
1437  if (fb_labels != NULL)
1438    {
1439      for (i = fb_labels + FB_LABEL_SPECIAL;
1440	   i < fb_labels + fb_label_count; ++i)
1441	{
1442	  if (*i == label)
1443	    {
1444	      ++fb_label_instances[i - fb_labels];
1445	      return;
1446	    }			/* if we find it  */
1447	}			/* for each existing label  */
1448    }
1449
1450  /* If we get to here, we don't have label listed yet.  */
1451
1452  if (fb_labels == NULL)
1453    {
1454      fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1455      fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1456      fb_label_max = FB_LABEL_BUMP_BY;
1457      fb_label_count = FB_LABEL_SPECIAL;
1458
1459    }
1460  else if (fb_label_count == fb_label_max)
1461    {
1462      fb_label_max += FB_LABEL_BUMP_BY;
1463      fb_labels = (long *) xrealloc ((char *) fb_labels,
1464				     fb_label_max * sizeof (long));
1465      fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1466					      fb_label_max * sizeof (long));
1467    }				/* if we needed to grow  */
1468
1469  fb_labels[fb_label_count] = label;
1470  fb_label_instances[fb_label_count] = 1;
1471  ++fb_label_count;
1472}
1473
1474static long
1475fb_label_instance (label)
1476     long label;
1477{
1478  long *i;
1479
1480  if (label < FB_LABEL_SPECIAL)
1481    {
1482      return (fb_low_counter[label]);
1483    }
1484
1485  if (fb_labels != NULL)
1486    {
1487      for (i = fb_labels + FB_LABEL_SPECIAL;
1488	   i < fb_labels + fb_label_count; ++i)
1489	{
1490	  if (*i == label)
1491	    {
1492	      return (fb_label_instances[i - fb_labels]);
1493	    }			/* if we find it  */
1494	}			/* for each existing label  */
1495    }
1496
1497  /* We didn't find the label, so this must be a reference to the
1498     first instance.  */
1499  return 0;
1500}
1501
1502/* Caller must copy returned name: we re-use the area for the next name.
1503
1504   The mth occurence of label n: is turned into the symbol "Ln^Bm"
1505   where n is the label number and m is the instance number. "L" makes
1506   it a label discarded unless debugging and "^B"('\2') ensures no
1507   ordinary symbol SHOULD get the same name as a local label
1508   symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1509
1510   dollar labels get the same treatment, except that ^A is used in
1511   place of ^B.  */
1512
1513char *				/* Return local label name.  */
1514fb_label_name (n, augend)
1515     long n;			/* We just saw "n:", "nf" or "nb" : n a number.  */
1516     long augend;		/* 0 for nb, 1 for n:, nf.  */
1517{
1518  long i;
1519  /* Returned to caller, then copied.  Used for created names ("4f").  */
1520  static char symbol_name_build[24];
1521  register char *p;
1522  register char *q;
1523  char symbol_name_temporary[20];	/* Build up a number, BACKWARDS.  */
1524
1525  know (n >= 0);
1526  know (augend == 0 || augend == 1);
1527  p = symbol_name_build;
1528#ifdef LOCAL_LABEL_PREFIX
1529  *p++ = LOCAL_LABEL_PREFIX;
1530#endif
1531  *p++ = 'L';
1532
1533  /* Next code just does sprintf( {}, "%d", n);  */
1534  /* Label number.  */
1535  q = symbol_name_temporary;
1536  for (*q++ = 0, i = n; i; ++q)
1537    {
1538      *q = i % 10 + '0';
1539      i /= 10;
1540    }
1541  while ((*p = *--q) != '\0')
1542    ++p;
1543
1544  *p++ = LOCAL_LABEL_CHAR;		/* ^B  */
1545
1546  /* Instance number.  */
1547  q = symbol_name_temporary;
1548  for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1549    {
1550      *q = i % 10 + '0';
1551      i /= 10;
1552    }
1553  while ((*p++ = *--q) != '\0');;
1554
1555  /* The label, as a '\0' ended string, starts at symbol_name_build.  */
1556  return (symbol_name_build);
1557}
1558
1559/* Decode name that may have been generated by foo_label_name() above.
1560   If the name wasn't generated by foo_label_name(), then return it
1561   unaltered.  This is used for error messages.  */
1562
1563char *
1564decode_local_label_name (s)
1565     char *s;
1566{
1567  char *p;
1568  char *symbol_decode;
1569  int label_number;
1570  int instance_number;
1571  char *type;
1572  const char *message_format;
1573  int index = 0;
1574
1575#ifdef LOCAL_LABEL_PREFIX
1576  if (s[index] == LOCAL_LABEL_PREFIX)
1577    ++index;
1578#endif
1579
1580  if (s[index] != 'L')
1581    return s;
1582
1583  for (label_number = 0, p = s + index + 1; ISDIGIT (*p); ++p)
1584    label_number = (10 * label_number) + *p - '0';
1585
1586  if (*p == DOLLAR_LABEL_CHAR)
1587    type = "dollar";
1588  else if (*p == LOCAL_LABEL_CHAR)
1589    type = "fb";
1590  else
1591    return s;
1592
1593  for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1594    instance_number = (10 * instance_number) + *p - '0';
1595
1596  message_format = _("\"%d\" (instance number %d of a %s label)");
1597  symbol_decode = obstack_alloc (&notes, strlen (message_format) + 30);
1598  sprintf (symbol_decode, message_format, label_number, instance_number, type);
1599
1600  return symbol_decode;
1601}
1602
1603/* Get the value of a symbol.  */
1604
1605valueT
1606S_GET_VALUE (s)
1607     symbolS *s;
1608{
1609#ifdef BFD_ASSEMBLER
1610  if (LOCAL_SYMBOL_CHECK (s))
1611    return resolve_symbol_value (s);
1612#endif
1613
1614  if (!s->sy_resolved)
1615    {
1616      valueT val = resolve_symbol_value (s);
1617      if (!finalize_syms)
1618	return val;
1619    }
1620  if (s->sy_value.X_op != O_constant)
1621    {
1622      static symbolS *recur;
1623
1624      /* FIXME: In non BFD assemblers, S_IS_DEFINED and S_IS_COMMON
1625         may call S_GET_VALUE.  We use a static symbol to avoid the
1626         immediate recursion.  */
1627      if (recur == s)
1628	return (valueT) s->sy_value.X_add_number;
1629      recur = s;
1630      if (! s->sy_resolved
1631	  || s->sy_value.X_op != O_symbol
1632	  || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1633	as_bad (_("attempt to get value of unresolved symbol `%s'"),
1634		S_GET_NAME (s));
1635      recur = NULL;
1636    }
1637  return (valueT) s->sy_value.X_add_number;
1638}
1639
1640/* Set the value of a symbol.  */
1641
1642void
1643S_SET_VALUE (s, val)
1644     symbolS *s;
1645     valueT val;
1646{
1647#ifdef BFD_ASSEMBLER
1648  if (LOCAL_SYMBOL_CHECK (s))
1649    {
1650      ((struct local_symbol *) s)->lsy_value = val;
1651      return;
1652    }
1653#endif
1654
1655  s->sy_value.X_op = O_constant;
1656  s->sy_value.X_add_number = (offsetT) val;
1657  s->sy_value.X_unsigned = 0;
1658}
1659
1660void
1661copy_symbol_attributes (dest, src)
1662     symbolS *dest, *src;
1663{
1664  if (LOCAL_SYMBOL_CHECK (dest))
1665    dest = local_symbol_convert ((struct local_symbol *) dest);
1666  if (LOCAL_SYMBOL_CHECK (src))
1667    src = local_symbol_convert ((struct local_symbol *) src);
1668
1669#ifdef BFD_ASSEMBLER
1670  /* In an expression, transfer the settings of these flags.
1671     The user can override later, of course.  */
1672#define COPIED_SYMFLAGS	(BSF_FUNCTION | BSF_OBJECT)
1673  dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1674#endif
1675
1676#ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1677  OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1678#endif
1679}
1680
1681#ifdef BFD_ASSEMBLER
1682
1683int
1684S_IS_FUNCTION (s)
1685     symbolS *s;
1686{
1687  flagword flags;
1688
1689  if (LOCAL_SYMBOL_CHECK (s))
1690    return 0;
1691
1692  flags = s->bsym->flags;
1693
1694  return (flags & BSF_FUNCTION) != 0;
1695}
1696
1697int
1698S_IS_EXTERNAL (s)
1699     symbolS *s;
1700{
1701  flagword flags;
1702
1703  if (LOCAL_SYMBOL_CHECK (s))
1704    return 0;
1705
1706  flags = s->bsym->flags;
1707
1708  /* Sanity check.  */
1709  if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1710    abort ();
1711
1712  return (flags & BSF_GLOBAL) != 0;
1713}
1714
1715int
1716S_IS_WEAK (s)
1717     symbolS *s;
1718{
1719  if (LOCAL_SYMBOL_CHECK (s))
1720    return 0;
1721  return (s->bsym->flags & BSF_WEAK) != 0;
1722}
1723
1724int
1725S_IS_COMMON (s)
1726     symbolS *s;
1727{
1728  if (LOCAL_SYMBOL_CHECK (s))
1729    return 0;
1730  return bfd_is_com_section (s->bsym->section);
1731}
1732
1733int
1734S_IS_DEFINED (s)
1735     symbolS *s;
1736{
1737  if (LOCAL_SYMBOL_CHECK (s))
1738    return ((struct local_symbol *) s)->lsy_section != undefined_section;
1739  return s->bsym->section != undefined_section;
1740}
1741
1742int
1743S_IS_DEBUG (s)
1744     symbolS *s;
1745{
1746  if (LOCAL_SYMBOL_CHECK (s))
1747    return 0;
1748  if (s->bsym->flags & BSF_DEBUGGING)
1749    return 1;
1750  return 0;
1751}
1752
1753int
1754S_IS_LOCAL (s)
1755     symbolS *s;
1756{
1757  flagword flags;
1758  const char *name;
1759
1760  if (LOCAL_SYMBOL_CHECK (s))
1761    return 1;
1762
1763  flags = s->bsym->flags;
1764
1765  /* Sanity check.  */
1766  if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
1767    abort ();
1768
1769  if (bfd_get_section (s->bsym) == reg_section)
1770    return 1;
1771
1772  if (flag_strip_local_absolute
1773      && (flags & BSF_GLOBAL) == 0
1774      && bfd_get_section (s->bsym) == absolute_section)
1775    return 1;
1776
1777  name = S_GET_NAME (s);
1778  return (name != NULL
1779	  && ! S_IS_DEBUG (s)
1780	  && (strchr (name, DOLLAR_LABEL_CHAR)
1781	      || strchr (name, LOCAL_LABEL_CHAR)
1782	      || (! flag_keep_locals
1783		  && (bfd_is_local_label (stdoutput, s->bsym)
1784		      || (flag_mri
1785			  && name[0] == '?'
1786			  && name[1] == '?')))));
1787}
1788
1789int
1790S_IS_EXTERN (s)
1791     symbolS *s;
1792{
1793  return S_IS_EXTERNAL (s);
1794}
1795
1796int
1797S_IS_STABD (s)
1798     symbolS *s;
1799{
1800  return S_GET_NAME (s) == 0;
1801}
1802
1803CONST char *
1804S_GET_NAME (s)
1805     symbolS *s;
1806{
1807  if (LOCAL_SYMBOL_CHECK (s))
1808    return ((struct local_symbol *) s)->lsy_name;
1809  return s->bsym->name;
1810}
1811
1812segT
1813S_GET_SEGMENT (s)
1814     symbolS *s;
1815{
1816  if (LOCAL_SYMBOL_CHECK (s))
1817    return ((struct local_symbol *) s)->lsy_section;
1818  return s->bsym->section;
1819}
1820
1821void
1822S_SET_SEGMENT (s, seg)
1823     symbolS *s;
1824     segT seg;
1825{
1826  /* Don't reassign section symbols.  The direct reason is to prevent seg
1827     faults assigning back to const global symbols such as *ABS*, but it
1828     shouldn't happen anyway.  */
1829
1830  if (LOCAL_SYMBOL_CHECK (s))
1831    {
1832      if (seg == reg_section)
1833	s = local_symbol_convert ((struct local_symbol *) s);
1834      else
1835	{
1836	  ((struct local_symbol *) s)->lsy_section = seg;
1837	  return;
1838	}
1839    }
1840
1841  if (s->bsym->flags & BSF_SECTION_SYM)
1842    {
1843      if (s->bsym->section != seg)
1844	abort ();
1845    }
1846  else
1847    s->bsym->section = seg;
1848}
1849
1850void
1851S_SET_EXTERNAL (s)
1852     symbolS *s;
1853{
1854  if (LOCAL_SYMBOL_CHECK (s))
1855    s = local_symbol_convert ((struct local_symbol *) s);
1856  if ((s->bsym->flags & BSF_WEAK) != 0)
1857    {
1858      /* Let .weak override .global.  */
1859      return;
1860    }
1861  if (s->bsym->flags & BSF_SECTION_SYM)
1862    {
1863      char * file;
1864      unsigned int line;
1865
1866      /* Do not reassign section symbols.  */
1867      as_where (& file, & line);
1868      as_warn_where (file, line,
1869		     _("section symbols are already global"));
1870      return;
1871    }
1872  s->bsym->flags |= BSF_GLOBAL;
1873  s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
1874}
1875
1876void
1877S_CLEAR_EXTERNAL (s)
1878     symbolS *s;
1879{
1880  if (LOCAL_SYMBOL_CHECK (s))
1881    return;
1882  if ((s->bsym->flags & BSF_WEAK) != 0)
1883    {
1884      /* Let .weak override.  */
1885      return;
1886    }
1887  s->bsym->flags |= BSF_LOCAL;
1888  s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
1889}
1890
1891void
1892S_SET_WEAK (s)
1893     symbolS *s;
1894{
1895  if (LOCAL_SYMBOL_CHECK (s))
1896    s = local_symbol_convert ((struct local_symbol *) s);
1897  s->bsym->flags |= BSF_WEAK;
1898  s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
1899}
1900
1901void
1902S_SET_NAME (s, name)
1903     symbolS *s;
1904     char *name;
1905{
1906  if (LOCAL_SYMBOL_CHECK (s))
1907    {
1908      ((struct local_symbol *) s)->lsy_name = name;
1909      return;
1910    }
1911  s->bsym->name = name;
1912}
1913#endif /* BFD_ASSEMBLER */
1914
1915#ifdef SYMBOLS_NEED_BACKPOINTERS
1916
1917/* Return the previous symbol in a chain.  */
1918
1919symbolS *
1920symbol_previous (s)
1921     symbolS *s;
1922{
1923  if (LOCAL_SYMBOL_CHECK (s))
1924    abort ();
1925  return s->sy_previous;
1926}
1927
1928#endif /* SYMBOLS_NEED_BACKPOINTERS */
1929
1930/* Return the next symbol in a chain.  */
1931
1932symbolS *
1933symbol_next (s)
1934     symbolS *s;
1935{
1936  if (LOCAL_SYMBOL_CHECK (s))
1937    abort ();
1938  return s->sy_next;
1939}
1940
1941/* Return a pointer to the value of a symbol as an expression.  */
1942
1943expressionS *
1944symbol_get_value_expression (s)
1945     symbolS *s;
1946{
1947  if (LOCAL_SYMBOL_CHECK (s))
1948    s = local_symbol_convert ((struct local_symbol *) s);
1949  return &s->sy_value;
1950}
1951
1952/* Set the value of a symbol to an expression.  */
1953
1954void
1955symbol_set_value_expression (s, exp)
1956     symbolS *s;
1957     const expressionS *exp;
1958{
1959  if (LOCAL_SYMBOL_CHECK (s))
1960    s = local_symbol_convert ((struct local_symbol *) s);
1961  s->sy_value = *exp;
1962}
1963
1964/* Set the frag of a symbol.  */
1965
1966void
1967symbol_set_frag (s, f)
1968     symbolS *s;
1969     fragS *f;
1970{
1971#ifdef BFD_ASSEMBLER
1972  if (LOCAL_SYMBOL_CHECK (s))
1973    {
1974      local_symbol_set_frag ((struct local_symbol *) s, f);
1975      return;
1976    }
1977#endif
1978  s->sy_frag = f;
1979}
1980
1981/* Return the frag of a symbol.  */
1982
1983fragS *
1984symbol_get_frag (s)
1985     symbolS *s;
1986{
1987#ifdef BFD_ASSEMBLER
1988  if (LOCAL_SYMBOL_CHECK (s))
1989    return local_symbol_get_frag ((struct local_symbol *) s);
1990#endif
1991  return s->sy_frag;
1992}
1993
1994/* Mark a symbol as having been used.  */
1995
1996void
1997symbol_mark_used (s)
1998     symbolS *s;
1999{
2000  if (LOCAL_SYMBOL_CHECK (s))
2001    return;
2002  s->sy_used = 1;
2003}
2004
2005/* Clear the mark of whether a symbol has been used.  */
2006
2007void
2008symbol_clear_used (s)
2009     symbolS *s;
2010{
2011  if (LOCAL_SYMBOL_CHECK (s))
2012    s = local_symbol_convert ((struct local_symbol *) s);
2013  s->sy_used = 0;
2014}
2015
2016/* Return whether a symbol has been used.  */
2017
2018int
2019symbol_used_p (s)
2020     symbolS *s;
2021{
2022  if (LOCAL_SYMBOL_CHECK (s))
2023    return 1;
2024  return s->sy_used;
2025}
2026
2027/* Mark a symbol as having been used in a reloc.  */
2028
2029void
2030symbol_mark_used_in_reloc (s)
2031     symbolS *s;
2032{
2033  if (LOCAL_SYMBOL_CHECK (s))
2034    s = local_symbol_convert ((struct local_symbol *) s);
2035  s->sy_used_in_reloc = 1;
2036}
2037
2038/* Clear the mark of whether a symbol has been used in a reloc.  */
2039
2040void
2041symbol_clear_used_in_reloc (s)
2042     symbolS *s;
2043{
2044  if (LOCAL_SYMBOL_CHECK (s))
2045    return;
2046  s->sy_used_in_reloc = 0;
2047}
2048
2049/* Return whether a symbol has been used in a reloc.  */
2050
2051int
2052symbol_used_in_reloc_p (s)
2053     symbolS *s;
2054{
2055  if (LOCAL_SYMBOL_CHECK (s))
2056    return 0;
2057  return s->sy_used_in_reloc;
2058}
2059
2060/* Mark a symbol as an MRI common symbol.  */
2061
2062void
2063symbol_mark_mri_common (s)
2064     symbolS *s;
2065{
2066  if (LOCAL_SYMBOL_CHECK (s))
2067    s = local_symbol_convert ((struct local_symbol *) s);
2068  s->sy_mri_common = 1;
2069}
2070
2071/* Clear the mark of whether a symbol is an MRI common symbol.  */
2072
2073void
2074symbol_clear_mri_common (s)
2075     symbolS *s;
2076{
2077  if (LOCAL_SYMBOL_CHECK (s))
2078    return;
2079  s->sy_mri_common = 0;
2080}
2081
2082/* Return whether a symbol is an MRI common symbol.  */
2083
2084int
2085symbol_mri_common_p (s)
2086     symbolS *s;
2087{
2088  if (LOCAL_SYMBOL_CHECK (s))
2089    return 0;
2090  return s->sy_mri_common;
2091}
2092
2093/* Mark a symbol as having been written.  */
2094
2095void
2096symbol_mark_written (s)
2097     symbolS *s;
2098{
2099  if (LOCAL_SYMBOL_CHECK (s))
2100    return;
2101  s->written = 1;
2102}
2103
2104/* Clear the mark of whether a symbol has been written.  */
2105
2106void
2107symbol_clear_written (s)
2108     symbolS *s;
2109{
2110  if (LOCAL_SYMBOL_CHECK (s))
2111    return;
2112  s->written = 0;
2113}
2114
2115/* Return whether a symbol has been written.  */
2116
2117int
2118symbol_written_p (s)
2119     symbolS *s;
2120{
2121  if (LOCAL_SYMBOL_CHECK (s))
2122    return 0;
2123  return s->written;
2124}
2125
2126/* Mark a symbol has having been resolved.  */
2127
2128void
2129symbol_mark_resolved (s)
2130     symbolS *s;
2131{
2132#ifdef BFD_ASSEMBLER
2133  if (LOCAL_SYMBOL_CHECK (s))
2134    {
2135      local_symbol_mark_resolved ((struct local_symbol *) s);
2136      return;
2137    }
2138#endif
2139  s->sy_resolved = 1;
2140}
2141
2142/* Return whether a symbol has been resolved.  */
2143
2144int
2145symbol_resolved_p (s)
2146     symbolS *s;
2147{
2148#ifdef BFD_ASSEMBLER
2149  if (LOCAL_SYMBOL_CHECK (s))
2150    return local_symbol_resolved_p ((struct local_symbol *) s);
2151#endif
2152  return s->sy_resolved;
2153}
2154
2155/* Return whether a symbol is a section symbol.  */
2156
2157int
2158symbol_section_p (s)
2159     symbolS *s ATTRIBUTE_UNUSED;
2160{
2161  if (LOCAL_SYMBOL_CHECK (s))
2162    return 0;
2163#ifdef BFD_ASSEMBLER
2164  return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2165#else
2166  /* FIXME.  */
2167  return 0;
2168#endif
2169}
2170
2171/* Return whether a symbol is equated to another symbol.  */
2172
2173int
2174symbol_equated_p (s)
2175     symbolS *s;
2176{
2177  if (LOCAL_SYMBOL_CHECK (s))
2178    return 0;
2179  return s->sy_value.X_op == O_symbol;
2180}
2181
2182/* Return whether a symbol is equated to another symbol, and should be
2183   treated specially when writing out relocs.  */
2184
2185int
2186symbol_equated_reloc_p (s)
2187     symbolS *s;
2188{
2189  if (LOCAL_SYMBOL_CHECK (s))
2190    return 0;
2191  /* X_op_symbol, normally not used for O_symbol, is set by
2192     resolve_symbol_value to flag expression syms that have been
2193     equated.  */
2194  return (s->sy_value.X_op == O_symbol
2195	  && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2196	      || ! S_IS_DEFINED (s)
2197	      || S_IS_COMMON (s)));
2198}
2199
2200/* Return whether a symbol has a constant value.  */
2201
2202int
2203symbol_constant_p (s)
2204     symbolS *s;
2205{
2206  if (LOCAL_SYMBOL_CHECK (s))
2207    return 1;
2208  return s->sy_value.X_op == O_constant;
2209}
2210
2211#ifdef BFD_ASSEMBLER
2212
2213/* Return the BFD symbol for a symbol.  */
2214
2215asymbol *
2216symbol_get_bfdsym (s)
2217     symbolS *s;
2218{
2219  if (LOCAL_SYMBOL_CHECK (s))
2220    s = local_symbol_convert ((struct local_symbol *) s);
2221  return s->bsym;
2222}
2223
2224/* Set the BFD symbol for a symbol.  */
2225
2226void
2227symbol_set_bfdsym (s, bsym)
2228     symbolS *s;
2229     asymbol *bsym;
2230{
2231  if (LOCAL_SYMBOL_CHECK (s))
2232    s = local_symbol_convert ((struct local_symbol *) s);
2233  s->bsym = bsym;
2234}
2235
2236#endif /* BFD_ASSEMBLER */
2237
2238#ifdef OBJ_SYMFIELD_TYPE
2239
2240/* Get a pointer to the object format information for a symbol.  */
2241
2242OBJ_SYMFIELD_TYPE *
2243symbol_get_obj (s)
2244     symbolS *s;
2245{
2246  if (LOCAL_SYMBOL_CHECK (s))
2247    s = local_symbol_convert ((struct local_symbol *) s);
2248  return &s->sy_obj;
2249}
2250
2251/* Set the object format information for a symbol.  */
2252
2253void
2254symbol_set_obj (s, o)
2255     symbolS *s;
2256     OBJ_SYMFIELD_TYPE *o;
2257{
2258  if (LOCAL_SYMBOL_CHECK (s))
2259    s = local_symbol_convert ((struct local_symbol *) s);
2260  s->sy_obj = *o;
2261}
2262
2263#endif /* OBJ_SYMFIELD_TYPE */
2264
2265#ifdef TC_SYMFIELD_TYPE
2266
2267/* Get a pointer to the processor information for a symbol.  */
2268
2269TC_SYMFIELD_TYPE *
2270symbol_get_tc (s)
2271     symbolS *s;
2272{
2273  if (LOCAL_SYMBOL_CHECK (s))
2274    s = local_symbol_convert ((struct local_symbol *) s);
2275  return &s->sy_tc;
2276}
2277
2278/* Set the processor information for a symbol.  */
2279
2280void
2281symbol_set_tc (s, o)
2282     symbolS *s;
2283     TC_SYMFIELD_TYPE *o;
2284{
2285  if (LOCAL_SYMBOL_CHECK (s))
2286    s = local_symbol_convert ((struct local_symbol *) s);
2287  s->sy_tc = *o;
2288}
2289
2290#endif /* TC_SYMFIELD_TYPE */
2291
2292void
2293symbol_begin ()
2294{
2295  symbol_lastP = NULL;
2296  symbol_rootP = NULL;		/* In case we have 0 symbols (!!)  */
2297  sy_hash = hash_new ();
2298#ifdef BFD_ASSEMBLER
2299  local_hash = hash_new ();
2300#endif
2301
2302  memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2303#ifdef BFD_ASSEMBLER
2304#if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2305  abs_symbol.bsym = bfd_abs_section.symbol;
2306#endif
2307#else
2308  /* Can't initialise a union. Sigh.  */
2309  S_SET_SEGMENT (&abs_symbol, absolute_section);
2310#endif
2311  abs_symbol.sy_value.X_op = O_constant;
2312  abs_symbol.sy_frag = &zero_address_frag;
2313
2314  if (LOCAL_LABELS_FB)
2315    fb_label_init ();
2316}
2317
2318int indent_level;
2319
2320/* Maximum indent level.
2321   Available for modification inside a gdb session.  */
2322int max_indent_level = 8;
2323
2324#if 0
2325
2326static void
2327indent ()
2328{
2329  printf ("%*s", indent_level * 4, "");
2330}
2331
2332#endif
2333
2334void
2335print_symbol_value_1 (file, sym)
2336     FILE *file;
2337     symbolS *sym;
2338{
2339  const char *name = S_GET_NAME (sym);
2340  if (!name || !name[0])
2341    name = "(unnamed)";
2342  fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2343
2344  if (LOCAL_SYMBOL_CHECK (sym))
2345    {
2346#ifdef BFD_ASSEMBLER
2347      struct local_symbol *locsym = (struct local_symbol *) sym;
2348      if (local_symbol_get_frag (locsym) != &zero_address_frag
2349	  && local_symbol_get_frag (locsym) != NULL)
2350	fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2351      if (local_symbol_resolved_p (locsym))
2352	fprintf (file, " resolved");
2353      fprintf (file, " local");
2354#endif
2355    }
2356  else
2357    {
2358      if (sym->sy_frag != &zero_address_frag)
2359	fprintf (file, " frag %lx", (long) sym->sy_frag);
2360      if (sym->written)
2361	fprintf (file, " written");
2362      if (sym->sy_resolved)
2363	fprintf (file, " resolved");
2364      else if (sym->sy_resolving)
2365	fprintf (file, " resolving");
2366      if (sym->sy_used_in_reloc)
2367	fprintf (file, " used-in-reloc");
2368      if (sym->sy_used)
2369	fprintf (file, " used");
2370      if (S_IS_LOCAL (sym))
2371	fprintf (file, " local");
2372      if (S_IS_EXTERN (sym))
2373	fprintf (file, " extern");
2374      if (S_IS_DEBUG (sym))
2375	fprintf (file, " debug");
2376      if (S_IS_DEFINED (sym))
2377	fprintf (file, " defined");
2378    }
2379  fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2380  if (symbol_resolved_p (sym))
2381    {
2382      segT s = S_GET_SEGMENT (sym);
2383
2384      if (s != undefined_section
2385          && s != expr_section)
2386	fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2387    }
2388  else if (indent_level < max_indent_level
2389	   && S_GET_SEGMENT (sym) != undefined_section)
2390    {
2391      indent_level++;
2392      fprintf (file, "\n%*s<", indent_level * 4, "");
2393#ifdef BFD_ASSEMBLER
2394      if (LOCAL_SYMBOL_CHECK (sym))
2395	fprintf (file, "constant %lx",
2396		 (long) ((struct local_symbol *) sym)->lsy_value);
2397      else
2398#endif
2399	print_expr_1 (file, &sym->sy_value);
2400      fprintf (file, ">");
2401      indent_level--;
2402    }
2403  fflush (file);
2404}
2405
2406void
2407print_symbol_value (sym)
2408     symbolS *sym;
2409{
2410  indent_level = 0;
2411  print_symbol_value_1 (stderr, sym);
2412  fprintf (stderr, "\n");
2413}
2414
2415static void
2416print_binary (file, name, exp)
2417     FILE *file;
2418     const char *name;
2419     expressionS *exp;
2420{
2421  indent_level++;
2422  fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2423  print_symbol_value_1 (file, exp->X_add_symbol);
2424  fprintf (file, ">\n%*s<", indent_level * 4, "");
2425  print_symbol_value_1 (file, exp->X_op_symbol);
2426  fprintf (file, ">");
2427  indent_level--;
2428}
2429
2430void
2431print_expr_1 (file, exp)
2432     FILE *file;
2433     expressionS *exp;
2434{
2435  fprintf (file, "expr %lx ", (long) exp);
2436  switch (exp->X_op)
2437    {
2438    case O_illegal:
2439      fprintf (file, "illegal");
2440      break;
2441    case O_absent:
2442      fprintf (file, "absent");
2443      break;
2444    case O_constant:
2445      fprintf (file, "constant %lx", (long) exp->X_add_number);
2446      break;
2447    case O_symbol:
2448      indent_level++;
2449      fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2450      print_symbol_value_1 (file, exp->X_add_symbol);
2451      fprintf (file, ">");
2452    maybe_print_addnum:
2453      if (exp->X_add_number)
2454	fprintf (file, "\n%*s%lx", indent_level * 4, "",
2455		 (long) exp->X_add_number);
2456      indent_level--;
2457      break;
2458    case O_register:
2459      fprintf (file, "register #%d", (int) exp->X_add_number);
2460      break;
2461    case O_big:
2462      fprintf (file, "big");
2463      break;
2464    case O_uminus:
2465      fprintf (file, "uminus -<");
2466      indent_level++;
2467      print_symbol_value_1 (file, exp->X_add_symbol);
2468      fprintf (file, ">");
2469      goto maybe_print_addnum;
2470    case O_bit_not:
2471      fprintf (file, "bit_not");
2472      break;
2473    case O_multiply:
2474      print_binary (file, "multiply", exp);
2475      break;
2476    case O_divide:
2477      print_binary (file, "divide", exp);
2478      break;
2479    case O_modulus:
2480      print_binary (file, "modulus", exp);
2481      break;
2482    case O_left_shift:
2483      print_binary (file, "lshift", exp);
2484      break;
2485    case O_right_shift:
2486      print_binary (file, "rshift", exp);
2487      break;
2488    case O_bit_inclusive_or:
2489      print_binary (file, "bit_ior", exp);
2490      break;
2491    case O_bit_exclusive_or:
2492      print_binary (file, "bit_xor", exp);
2493      break;
2494    case O_bit_and:
2495      print_binary (file, "bit_and", exp);
2496      break;
2497    case O_eq:
2498      print_binary (file, "eq", exp);
2499      break;
2500    case O_ne:
2501      print_binary (file, "ne", exp);
2502      break;
2503    case O_lt:
2504      print_binary (file, "lt", exp);
2505      break;
2506    case O_le:
2507      print_binary (file, "le", exp);
2508      break;
2509    case O_ge:
2510      print_binary (file, "ge", exp);
2511      break;
2512    case O_gt:
2513      print_binary (file, "gt", exp);
2514      break;
2515    case O_logical_and:
2516      print_binary (file, "logical_and", exp);
2517      break;
2518    case O_logical_or:
2519      print_binary (file, "logical_or", exp);
2520      break;
2521    case O_add:
2522      indent_level++;
2523      fprintf (file, "add\n%*s<", indent_level * 4, "");
2524      print_symbol_value_1 (file, exp->X_add_symbol);
2525      fprintf (file, ">\n%*s<", indent_level * 4, "");
2526      print_symbol_value_1 (file, exp->X_op_symbol);
2527      fprintf (file, ">");
2528      goto maybe_print_addnum;
2529    case O_subtract:
2530      indent_level++;
2531      fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2532      print_symbol_value_1 (file, exp->X_add_symbol);
2533      fprintf (file, ">\n%*s<", indent_level * 4, "");
2534      print_symbol_value_1 (file, exp->X_op_symbol);
2535      fprintf (file, ">");
2536      goto maybe_print_addnum;
2537    default:
2538      fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2539      break;
2540    }
2541  fflush (stdout);
2542}
2543
2544void
2545print_expr (exp)
2546     expressionS *exp;
2547{
2548  print_expr_1 (stderr, exp);
2549  fprintf (stderr, "\n");
2550}
2551
2552void
2553symbol_print_statistics (file)
2554     FILE *file;
2555{
2556  hash_print_statistics (file, "symbol table", sy_hash);
2557#ifdef BFD_ASSEMBLER
2558  hash_print_statistics (file, "mini local symbol table", local_hash);
2559  fprintf (file, "%lu mini local symbols created, %lu converted\n",
2560	   local_symbol_count, local_symbol_conversion_count);
2561#endif
2562}
2563