OSDN Git Service

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