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