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