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