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