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