OSDN Git Service

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