OSDN Git Service

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