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