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