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