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