* symbols.c (S_SET_THREAD_LOCAL): New function.
[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_THREAD_LOCAL (s)
1955      symbolS *s;
1956 {
1957   if (LOCAL_SYMBOL_CHECK (s))
1958     s = local_symbol_convert ((struct local_symbol *) s);
1959   if (bfd_is_com_section (s->bsym->section)
1960       && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
1961     return;
1962   s->bsym->flags |= BSF_THREAD_LOCAL;
1963   if ((s->bsym->flags & BSF_FUNCTION) != 0)
1964     as_bad (_("Accessing function `%s' as thread-local object"),
1965             S_GET_NAME (s));
1966   else if (! bfd_is_und_section (s->bsym->section)
1967            && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
1968     as_bad (_("Accessing `%s' as thread-local object"),
1969             S_GET_NAME (s));
1970 }
1971
1972 void
1973 S_SET_NAME (s, name)
1974      symbolS *s;
1975      char *name;
1976 {
1977   if (LOCAL_SYMBOL_CHECK (s))
1978     {
1979       ((struct local_symbol *) s)->lsy_name = name;
1980       return;
1981     }
1982   s->bsym->name = name;
1983 }
1984 #endif /* BFD_ASSEMBLER */
1985
1986 #ifdef SYMBOLS_NEED_BACKPOINTERS
1987
1988 /* Return the previous symbol in a chain.  */
1989
1990 symbolS *
1991 symbol_previous (s)
1992      symbolS *s;
1993 {
1994   if (LOCAL_SYMBOL_CHECK (s))
1995     abort ();
1996   return s->sy_previous;
1997 }
1998
1999 #endif /* SYMBOLS_NEED_BACKPOINTERS */
2000
2001 /* Return the next symbol in a chain.  */
2002
2003 symbolS *
2004 symbol_next (s)
2005      symbolS *s;
2006 {
2007   if (LOCAL_SYMBOL_CHECK (s))
2008     abort ();
2009   return s->sy_next;
2010 }
2011
2012 /* Return a pointer to the value of a symbol as an expression.  */
2013
2014 expressionS *
2015 symbol_get_value_expression (s)
2016      symbolS *s;
2017 {
2018   if (LOCAL_SYMBOL_CHECK (s))
2019     s = local_symbol_convert ((struct local_symbol *) s);
2020   return &s->sy_value;
2021 }
2022
2023 /* Set the value of a symbol to an expression.  */
2024
2025 void
2026 symbol_set_value_expression (s, exp)
2027      symbolS *s;
2028      const expressionS *exp;
2029 {
2030   if (LOCAL_SYMBOL_CHECK (s))
2031     s = local_symbol_convert ((struct local_symbol *) s);
2032   s->sy_value = *exp;
2033 }
2034
2035 /* Set the frag of a symbol.  */
2036
2037 void
2038 symbol_set_frag (s, f)
2039      symbolS *s;
2040      fragS *f;
2041 {
2042 #ifdef BFD_ASSEMBLER
2043   if (LOCAL_SYMBOL_CHECK (s))
2044     {
2045       local_symbol_set_frag ((struct local_symbol *) s, f);
2046       return;
2047     }
2048 #endif
2049   s->sy_frag = f;
2050 }
2051
2052 /* Return the frag of a symbol.  */
2053
2054 fragS *
2055 symbol_get_frag (s)
2056      symbolS *s;
2057 {
2058 #ifdef BFD_ASSEMBLER
2059   if (LOCAL_SYMBOL_CHECK (s))
2060     return local_symbol_get_frag ((struct local_symbol *) s);
2061 #endif
2062   return s->sy_frag;
2063 }
2064
2065 /* Mark a symbol as having been used.  */
2066
2067 void
2068 symbol_mark_used (s)
2069      symbolS *s;
2070 {
2071   if (LOCAL_SYMBOL_CHECK (s))
2072     return;
2073   s->sy_used = 1;
2074 }
2075
2076 /* Clear the mark of whether a symbol has been used.  */
2077
2078 void
2079 symbol_clear_used (s)
2080      symbolS *s;
2081 {
2082   if (LOCAL_SYMBOL_CHECK (s))
2083     s = local_symbol_convert ((struct local_symbol *) s);
2084   s->sy_used = 0;
2085 }
2086
2087 /* Return whether a symbol has been used.  */
2088
2089 int
2090 symbol_used_p (s)
2091      symbolS *s;
2092 {
2093   if (LOCAL_SYMBOL_CHECK (s))
2094     return 1;
2095   return s->sy_used;
2096 }
2097
2098 /* Mark a symbol as having been used in a reloc.  */
2099
2100 void
2101 symbol_mark_used_in_reloc (s)
2102      symbolS *s;
2103 {
2104   if (LOCAL_SYMBOL_CHECK (s))
2105     s = local_symbol_convert ((struct local_symbol *) s);
2106   s->sy_used_in_reloc = 1;
2107 }
2108
2109 /* Clear the mark of whether a symbol has been used in a reloc.  */
2110
2111 void
2112 symbol_clear_used_in_reloc (s)
2113      symbolS *s;
2114 {
2115   if (LOCAL_SYMBOL_CHECK (s))
2116     return;
2117   s->sy_used_in_reloc = 0;
2118 }
2119
2120 /* Return whether a symbol has been used in a reloc.  */
2121
2122 int
2123 symbol_used_in_reloc_p (s)
2124      symbolS *s;
2125 {
2126   if (LOCAL_SYMBOL_CHECK (s))
2127     return 0;
2128   return s->sy_used_in_reloc;
2129 }
2130
2131 /* Mark a symbol as an MRI common symbol.  */
2132
2133 void
2134 symbol_mark_mri_common (s)
2135      symbolS *s;
2136 {
2137   if (LOCAL_SYMBOL_CHECK (s))
2138     s = local_symbol_convert ((struct local_symbol *) s);
2139   s->sy_mri_common = 1;
2140 }
2141
2142 /* Clear the mark of whether a symbol is an MRI common symbol.  */
2143
2144 void
2145 symbol_clear_mri_common (s)
2146      symbolS *s;
2147 {
2148   if (LOCAL_SYMBOL_CHECK (s))
2149     return;
2150   s->sy_mri_common = 0;
2151 }
2152
2153 /* Return whether a symbol is an MRI common symbol.  */
2154
2155 int
2156 symbol_mri_common_p (s)
2157      symbolS *s;
2158 {
2159   if (LOCAL_SYMBOL_CHECK (s))
2160     return 0;
2161   return s->sy_mri_common;
2162 }
2163
2164 /* Mark a symbol as having been written.  */
2165
2166 void
2167 symbol_mark_written (s)
2168      symbolS *s;
2169 {
2170   if (LOCAL_SYMBOL_CHECK (s))
2171     return;
2172   s->written = 1;
2173 }
2174
2175 /* Clear the mark of whether a symbol has been written.  */
2176
2177 void
2178 symbol_clear_written (s)
2179      symbolS *s;
2180 {
2181   if (LOCAL_SYMBOL_CHECK (s))
2182     return;
2183   s->written = 0;
2184 }
2185
2186 /* Return whether a symbol has been written.  */
2187
2188 int
2189 symbol_written_p (s)
2190      symbolS *s;
2191 {
2192   if (LOCAL_SYMBOL_CHECK (s))
2193     return 0;
2194   return s->written;
2195 }
2196
2197 /* Mark a symbol has having been resolved.  */
2198
2199 void
2200 symbol_mark_resolved (s)
2201      symbolS *s;
2202 {
2203 #ifdef BFD_ASSEMBLER
2204   if (LOCAL_SYMBOL_CHECK (s))
2205     {
2206       local_symbol_mark_resolved ((struct local_symbol *) s);
2207       return;
2208     }
2209 #endif
2210   s->sy_resolved = 1;
2211 }
2212
2213 /* Return whether a symbol has been resolved.  */
2214
2215 int
2216 symbol_resolved_p (s)
2217      symbolS *s;
2218 {
2219 #ifdef BFD_ASSEMBLER
2220   if (LOCAL_SYMBOL_CHECK (s))
2221     return local_symbol_resolved_p ((struct local_symbol *) s);
2222 #endif
2223   return s->sy_resolved;
2224 }
2225
2226 /* Return whether a symbol is a section symbol.  */
2227
2228 int
2229 symbol_section_p (s)
2230      symbolS *s ATTRIBUTE_UNUSED;
2231 {
2232   if (LOCAL_SYMBOL_CHECK (s))
2233     return 0;
2234 #ifdef BFD_ASSEMBLER
2235   return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2236 #else
2237   /* FIXME.  */
2238   return 0;
2239 #endif
2240 }
2241
2242 /* Return whether a symbol is equated to another symbol.  */
2243
2244 int
2245 symbol_equated_p (s)
2246      symbolS *s;
2247 {
2248   if (LOCAL_SYMBOL_CHECK (s))
2249     return 0;
2250   return s->sy_value.X_op == O_symbol;
2251 }
2252
2253 /* Return whether a symbol is equated to another symbol, and should be
2254    treated specially when writing out relocs.  */
2255
2256 int
2257 symbol_equated_reloc_p (s)
2258      symbolS *s;
2259 {
2260   if (LOCAL_SYMBOL_CHECK (s))
2261     return 0;
2262   /* X_op_symbol, normally not used for O_symbol, is set by
2263      resolve_symbol_value to flag expression syms that have been
2264      equated.  */
2265   return (s->sy_value.X_op == O_symbol
2266           && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2267               || ! S_IS_DEFINED (s)
2268               || S_IS_COMMON (s)));
2269 }
2270
2271 /* Return whether a symbol has a constant value.  */
2272
2273 int
2274 symbol_constant_p (s)
2275      symbolS *s;
2276 {
2277   if (LOCAL_SYMBOL_CHECK (s))
2278     return 1;
2279   return s->sy_value.X_op == O_constant;
2280 }
2281
2282 #ifdef BFD_ASSEMBLER
2283
2284 /* Return the BFD symbol for a symbol.  */
2285
2286 asymbol *
2287 symbol_get_bfdsym (s)
2288      symbolS *s;
2289 {
2290   if (LOCAL_SYMBOL_CHECK (s))
2291     s = local_symbol_convert ((struct local_symbol *) s);
2292   return s->bsym;
2293 }
2294
2295 /* Set the BFD symbol for a symbol.  */
2296
2297 void
2298 symbol_set_bfdsym (s, bsym)
2299      symbolS *s;
2300      asymbol *bsym;
2301 {
2302   if (LOCAL_SYMBOL_CHECK (s))
2303     s = local_symbol_convert ((struct local_symbol *) s);
2304   s->bsym = bsym;
2305 }
2306
2307 #endif /* BFD_ASSEMBLER */
2308
2309 #ifdef OBJ_SYMFIELD_TYPE
2310
2311 /* Get a pointer to the object format information for a symbol.  */
2312
2313 OBJ_SYMFIELD_TYPE *
2314 symbol_get_obj (s)
2315      symbolS *s;
2316 {
2317   if (LOCAL_SYMBOL_CHECK (s))
2318     s = local_symbol_convert ((struct local_symbol *) s);
2319   return &s->sy_obj;
2320 }
2321
2322 /* Set the object format information for a symbol.  */
2323
2324 void
2325 symbol_set_obj (s, o)
2326      symbolS *s;
2327      OBJ_SYMFIELD_TYPE *o;
2328 {
2329   if (LOCAL_SYMBOL_CHECK (s))
2330     s = local_symbol_convert ((struct local_symbol *) s);
2331   s->sy_obj = *o;
2332 }
2333
2334 #endif /* OBJ_SYMFIELD_TYPE */
2335
2336 #ifdef TC_SYMFIELD_TYPE
2337
2338 /* Get a pointer to the processor information for a symbol.  */
2339
2340 TC_SYMFIELD_TYPE *
2341 symbol_get_tc (s)
2342      symbolS *s;
2343 {
2344   if (LOCAL_SYMBOL_CHECK (s))
2345     s = local_symbol_convert ((struct local_symbol *) s);
2346   return &s->sy_tc;
2347 }
2348
2349 /* Set the processor information for a symbol.  */
2350
2351 void
2352 symbol_set_tc (s, o)
2353      symbolS *s;
2354      TC_SYMFIELD_TYPE *o;
2355 {
2356   if (LOCAL_SYMBOL_CHECK (s))
2357     s = local_symbol_convert ((struct local_symbol *) s);
2358   s->sy_tc = *o;
2359 }
2360
2361 #endif /* TC_SYMFIELD_TYPE */
2362
2363 void
2364 symbol_begin ()
2365 {
2366   symbol_lastP = NULL;
2367   symbol_rootP = NULL;          /* In case we have 0 symbols (!!)  */
2368   sy_hash = hash_new ();
2369 #ifdef BFD_ASSEMBLER
2370   local_hash = hash_new ();
2371 #endif
2372
2373   memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2374 #ifdef BFD_ASSEMBLER
2375 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2376   abs_symbol.bsym = bfd_abs_section.symbol;
2377 #endif
2378 #else
2379   /* Can't initialise a union. Sigh.  */
2380   S_SET_SEGMENT (&abs_symbol, absolute_section);
2381 #endif
2382   abs_symbol.sy_value.X_op = O_constant;
2383   abs_symbol.sy_frag = &zero_address_frag;
2384
2385   if (LOCAL_LABELS_FB)
2386     fb_label_init ();
2387 }
2388 \f
2389 int indent_level;
2390
2391 /* Maximum indent level.
2392    Available for modification inside a gdb session.  */
2393 int max_indent_level = 8;
2394
2395 #if 0
2396
2397 static void
2398 indent ()
2399 {
2400   printf ("%*s", indent_level * 4, "");
2401 }
2402
2403 #endif
2404
2405 void
2406 print_symbol_value_1 (file, sym)
2407      FILE *file;
2408      symbolS *sym;
2409 {
2410   const char *name = S_GET_NAME (sym);
2411   if (!name || !name[0])
2412     name = "(unnamed)";
2413   fprintf (file, "sym %lx %s", (unsigned long) sym, name);
2414
2415   if (LOCAL_SYMBOL_CHECK (sym))
2416     {
2417 #ifdef BFD_ASSEMBLER
2418       struct local_symbol *locsym = (struct local_symbol *) sym;
2419       if (local_symbol_get_frag (locsym) != &zero_address_frag
2420           && local_symbol_get_frag (locsym) != NULL)
2421         fprintf (file, " frag %lx", (long) local_symbol_get_frag (locsym));
2422       if (local_symbol_resolved_p (locsym))
2423         fprintf (file, " resolved");
2424       fprintf (file, " local");
2425 #endif
2426     }
2427   else
2428     {
2429       if (sym->sy_frag != &zero_address_frag)
2430         fprintf (file, " frag %lx", (long) sym->sy_frag);
2431       if (sym->written)
2432         fprintf (file, " written");
2433       if (sym->sy_resolved)
2434         fprintf (file, " resolved");
2435       else if (sym->sy_resolving)
2436         fprintf (file, " resolving");
2437       if (sym->sy_used_in_reloc)
2438         fprintf (file, " used-in-reloc");
2439       if (sym->sy_used)
2440         fprintf (file, " used");
2441       if (S_IS_LOCAL (sym))
2442         fprintf (file, " local");
2443       if (S_IS_EXTERN (sym))
2444         fprintf (file, " extern");
2445       if (S_IS_DEBUG (sym))
2446         fprintf (file, " debug");
2447       if (S_IS_DEFINED (sym))
2448         fprintf (file, " defined");
2449     }
2450   fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2451   if (symbol_resolved_p (sym))
2452     {
2453       segT s = S_GET_SEGMENT (sym);
2454
2455       if (s != undefined_section
2456           && s != expr_section)
2457         fprintf (file, " %lx", (long) S_GET_VALUE (sym));
2458     }
2459   else if (indent_level < max_indent_level
2460            && S_GET_SEGMENT (sym) != undefined_section)
2461     {
2462       indent_level++;
2463       fprintf (file, "\n%*s<", indent_level * 4, "");
2464 #ifdef BFD_ASSEMBLER
2465       if (LOCAL_SYMBOL_CHECK (sym))
2466         fprintf (file, "constant %lx",
2467                  (long) ((struct local_symbol *) sym)->lsy_value);
2468       else
2469 #endif
2470         print_expr_1 (file, &sym->sy_value);
2471       fprintf (file, ">");
2472       indent_level--;
2473     }
2474   fflush (file);
2475 }
2476
2477 void
2478 print_symbol_value (sym)
2479      symbolS *sym;
2480 {
2481   indent_level = 0;
2482   print_symbol_value_1 (stderr, sym);
2483   fprintf (stderr, "\n");
2484 }
2485
2486 static void
2487 print_binary (file, name, exp)
2488      FILE *file;
2489      const char *name;
2490      expressionS *exp;
2491 {
2492   indent_level++;
2493   fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2494   print_symbol_value_1 (file, exp->X_add_symbol);
2495   fprintf (file, ">\n%*s<", indent_level * 4, "");
2496   print_symbol_value_1 (file, exp->X_op_symbol);
2497   fprintf (file, ">");
2498   indent_level--;
2499 }
2500
2501 void
2502 print_expr_1 (file, exp)
2503      FILE *file;
2504      expressionS *exp;
2505 {
2506   fprintf (file, "expr %lx ", (long) exp);
2507   switch (exp->X_op)
2508     {
2509     case O_illegal:
2510       fprintf (file, "illegal");
2511       break;
2512     case O_absent:
2513       fprintf (file, "absent");
2514       break;
2515     case O_constant:
2516       fprintf (file, "constant %lx", (long) exp->X_add_number);
2517       break;
2518     case O_symbol:
2519       indent_level++;
2520       fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2521       print_symbol_value_1 (file, exp->X_add_symbol);
2522       fprintf (file, ">");
2523     maybe_print_addnum:
2524       if (exp->X_add_number)
2525         fprintf (file, "\n%*s%lx", indent_level * 4, "",
2526                  (long) exp->X_add_number);
2527       indent_level--;
2528       break;
2529     case O_register:
2530       fprintf (file, "register #%d", (int) exp->X_add_number);
2531       break;
2532     case O_big:
2533       fprintf (file, "big");
2534       break;
2535     case O_uminus:
2536       fprintf (file, "uminus -<");
2537       indent_level++;
2538       print_symbol_value_1 (file, exp->X_add_symbol);
2539       fprintf (file, ">");
2540       goto maybe_print_addnum;
2541     case O_bit_not:
2542       fprintf (file, "bit_not");
2543       break;
2544     case O_multiply:
2545       print_binary (file, "multiply", exp);
2546       break;
2547     case O_divide:
2548       print_binary (file, "divide", exp);
2549       break;
2550     case O_modulus:
2551       print_binary (file, "modulus", exp);
2552       break;
2553     case O_left_shift:
2554       print_binary (file, "lshift", exp);
2555       break;
2556     case O_right_shift:
2557       print_binary (file, "rshift", exp);
2558       break;
2559     case O_bit_inclusive_or:
2560       print_binary (file, "bit_ior", exp);
2561       break;
2562     case O_bit_exclusive_or:
2563       print_binary (file, "bit_xor", exp);
2564       break;
2565     case O_bit_and:
2566       print_binary (file, "bit_and", exp);
2567       break;
2568     case O_eq:
2569       print_binary (file, "eq", exp);
2570       break;
2571     case O_ne:
2572       print_binary (file, "ne", exp);
2573       break;
2574     case O_lt:
2575       print_binary (file, "lt", exp);
2576       break;
2577     case O_le:
2578       print_binary (file, "le", exp);
2579       break;
2580     case O_ge:
2581       print_binary (file, "ge", exp);
2582       break;
2583     case O_gt:
2584       print_binary (file, "gt", exp);
2585       break;
2586     case O_logical_and:
2587       print_binary (file, "logical_and", exp);
2588       break;
2589     case O_logical_or:
2590       print_binary (file, "logical_or", exp);
2591       break;
2592     case O_add:
2593       indent_level++;
2594       fprintf (file, "add\n%*s<", indent_level * 4, "");
2595       print_symbol_value_1 (file, exp->X_add_symbol);
2596       fprintf (file, ">\n%*s<", indent_level * 4, "");
2597       print_symbol_value_1 (file, exp->X_op_symbol);
2598       fprintf (file, ">");
2599       goto maybe_print_addnum;
2600     case O_subtract:
2601       indent_level++;
2602       fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2603       print_symbol_value_1 (file, exp->X_add_symbol);
2604       fprintf (file, ">\n%*s<", indent_level * 4, "");
2605       print_symbol_value_1 (file, exp->X_op_symbol);
2606       fprintf (file, ">");
2607       goto maybe_print_addnum;
2608     default:
2609       fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2610       break;
2611     }
2612   fflush (stdout);
2613 }
2614
2615 void
2616 print_expr (exp)
2617      expressionS *exp;
2618 {
2619   print_expr_1 (stderr, exp);
2620   fprintf (stderr, "\n");
2621 }
2622
2623 void
2624 symbol_print_statistics (file)
2625      FILE *file;
2626 {
2627   hash_print_statistics (file, "symbol table", sy_hash);
2628 #ifdef BFD_ASSEMBLER
2629   hash_print_statistics (file, "mini local symbol table", local_hash);
2630   fprintf (file, "%lu mini local symbols created, %lu converted\n",
2631            local_symbol_count, local_symbol_conversion_count);
2632 #endif
2633 }