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