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