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