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