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