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