PR gas/12390
[binutils.git] / gas / symbols.c
blob9a4e2bef54dc7abcfebdd9eb8621317d35d3c9f2
1 /* symbols.c -symbol table-
2 Copyright 1987, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GAS, the GNU Assembler.
8 GAS is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GAS is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GAS; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
25 #include "as.h"
27 #include "safe-ctype.h"
28 #include "obstack.h" /* For "symbols.h" */
29 #include "subsegs.h"
31 #include "struc-symbol.h"
33 /* This is non-zero if symbols are case sensitive, which is the
34 default. */
35 int symbols_case_sensitive = 1;
37 #ifndef WORKING_DOT_WORD
38 extern int new_broken_words;
39 #endif
41 /* symbol-name => struct symbol pointer */
42 static struct hash_control *sy_hash;
44 /* Table of local symbols. */
45 static struct hash_control *local_hash;
47 /* Below are commented in "symbols.h". */
48 symbolS *symbol_rootP;
49 symbolS *symbol_lastP;
50 symbolS abs_symbol;
51 symbolS dot_symbol;
53 #ifdef DEBUG_SYMS
54 #define debug_verify_symchain verify_symbol_chain
55 #else
56 #define debug_verify_symchain(root, last) ((void) 0)
57 #endif
59 #define DOLLAR_LABEL_CHAR '\001'
60 #define LOCAL_LABEL_CHAR '\002'
62 struct obstack notes;
63 #ifdef TE_PE
64 /* The name of an external symbol which is
65 used to make weak PE symbol names unique. */
66 const char * an_external_name;
67 #endif
69 static char *save_symbol_name (const char *);
70 static void fb_label_init (void);
71 static long dollar_label_instance (long);
72 static long fb_label_instance (long);
74 static void print_binary (FILE *, const char *, expressionS *);
75 static void report_op_error (symbolS *, symbolS *, symbolS *);
77 /* Return a pointer to a new symbol. Die if we can't make a new
78 symbol. Fill in the symbol's values. Add symbol to end of symbol
79 chain.
81 This function should be called in the general case of creating a
82 symbol. However, if the output file symbol table has already been
83 set, and you are certain that this symbol won't be wanted in the
84 output file, you can call symbol_create. */
86 symbolS *
87 symbol_new (const char *name, segT segment, valueT valu, fragS *frag)
89 symbolS *symbolP = symbol_create (name, segment, valu, frag);
91 /* Link to end of symbol chain. */
93 extern int symbol_table_frozen;
94 if (symbol_table_frozen)
95 abort ();
97 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
99 return symbolP;
102 /* Save a symbol name on a permanent obstack, and convert it according
103 to the object file format. */
105 static char *
106 save_symbol_name (const char *name)
108 unsigned int name_length;
109 char *ret;
111 name_length = strlen (name) + 1; /* +1 for \0. */
112 obstack_grow (&notes, name, name_length);
113 ret = (char *) obstack_finish (&notes);
115 #ifdef tc_canonicalize_symbol_name
116 ret = tc_canonicalize_symbol_name (ret);
117 #endif
119 if (! symbols_case_sensitive)
121 char *s;
123 for (s = ret; *s != '\0'; s++)
124 *s = TOUPPER (*s);
127 return ret;
130 symbolS *
131 symbol_create (const char *name, /* It is copied, the caller can destroy/modify. */
132 segT segment, /* Segment identifier (SEG_<something>). */
133 valueT valu, /* Symbol value. */
134 fragS *frag /* Associated fragment. */)
136 char *preserved_copy_of_name;
137 symbolS *symbolP;
139 preserved_copy_of_name = save_symbol_name (name);
141 symbolP = (symbolS *) obstack_alloc (&notes, sizeof (symbolS));
143 /* symbol must be born in some fixed state. This seems as good as any. */
144 memset (symbolP, 0, sizeof (symbolS));
146 symbolP->bsym = bfd_make_empty_symbol (stdoutput);
147 if (symbolP->bsym == NULL)
148 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
149 S_SET_NAME (symbolP, preserved_copy_of_name);
151 S_SET_SEGMENT (symbolP, segment);
152 S_SET_VALUE (symbolP, valu);
153 symbol_clear_list_pointers (symbolP);
155 symbolP->sy_frag = frag;
157 obj_symbol_new_hook (symbolP);
159 #ifdef tc_symbol_new_hook
160 tc_symbol_new_hook (symbolP);
161 #endif
163 return symbolP;
167 /* Local symbol support. If we can get away with it, we keep only a
168 small amount of information for local symbols. */
170 static symbolS *local_symbol_convert (struct local_symbol *);
172 /* Used for statistics. */
174 static unsigned long local_symbol_count;
175 static unsigned long local_symbol_conversion_count;
177 /* This macro is called with a symbol argument passed by reference.
178 It returns whether this is a local symbol. If necessary, it
179 changes its argument to the real symbol. */
181 #define LOCAL_SYMBOL_CHECK(s) \
182 (s->bsym == NULL \
183 ? (local_symbol_converted_p ((struct local_symbol *) s) \
184 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
185 0) \
186 : 1) \
187 : 0)
189 /* Create a local symbol and insert it into the local hash table. */
191 static struct local_symbol *
192 local_symbol_make (const char *name, segT section, valueT val, fragS *frag)
194 char *name_copy;
195 struct local_symbol *ret;
197 ++local_symbol_count;
199 name_copy = save_symbol_name (name);
201 ret = (struct local_symbol *) obstack_alloc (&notes, sizeof *ret);
202 ret->lsy_marker = NULL;
203 ret->lsy_name = name_copy;
204 ret->lsy_section = section;
205 local_symbol_set_frag (ret, frag);
206 ret->lsy_value = val;
208 hash_jam (local_hash, name_copy, (void *) ret);
210 return ret;
213 /* Convert a local symbol into a real symbol. Note that we do not
214 reclaim the space used by the local symbol. */
216 static symbolS *
217 local_symbol_convert (struct local_symbol *locsym)
219 symbolS *ret;
221 gas_assert (locsym->lsy_marker == NULL);
222 if (local_symbol_converted_p (locsym))
223 return local_symbol_get_real_symbol (locsym);
225 ++local_symbol_conversion_count;
227 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
228 local_symbol_get_frag (locsym));
230 if (local_symbol_resolved_p (locsym))
231 ret->sy_resolved = 1;
233 /* Local symbols are always either defined or used. */
234 ret->sy_used = 1;
236 #ifdef TC_LOCAL_SYMFIELD_CONVERT
237 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
238 #endif
240 symbol_table_insert (ret);
242 local_symbol_mark_converted (locsym);
243 local_symbol_set_real_symbol (locsym, ret);
245 hash_jam (local_hash, locsym->lsy_name, NULL);
247 return ret;
250 static void
251 define_sym_at_dot (symbolS *symbolP)
253 symbolP->sy_frag = frag_now;
254 #ifdef OBJ_VMS
255 S_SET_OTHER (symbolP, const_flag);
256 #endif
257 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
258 S_SET_SEGMENT (symbolP, now_seg);
261 /* We have just seen "<name>:".
262 Creates a struct symbol unless it already exists.
264 Gripes if we are redefining a symbol incompatibly (and ignores it). */
266 symbolS *
267 colon (/* Just seen "x:" - rattle symbols & frags. */
268 const char *sym_name /* Symbol name, as a cannonical string. */
269 /* We copy this string: OK to alter later. */)
271 register symbolS *symbolP; /* Symbol we are working with. */
273 /* Sun local labels go out of scope whenever a non-local symbol is
274 defined. */
275 if (LOCAL_LABELS_DOLLAR
276 && !bfd_is_local_label_name (stdoutput, sym_name))
277 dollar_label_clear ();
279 #ifndef WORKING_DOT_WORD
280 if (new_broken_words)
282 struct broken_word *a;
283 int possible_bytes;
284 fragS *frag_tmp;
285 char *frag_opcode;
287 if (now_seg == absolute_section)
289 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
290 return NULL;
293 possible_bytes = (md_short_jump_size
294 + new_broken_words * md_long_jump_size);
296 frag_tmp = frag_now;
297 frag_opcode = frag_var (rs_broken_word,
298 possible_bytes,
299 possible_bytes,
300 (relax_substateT) 0,
301 (symbolS *) broken_words,
302 (offsetT) 0,
303 NULL);
305 /* We want to store the pointer to where to insert the jump
306 table in the fr_opcode of the rs_broken_word frag. This
307 requires a little hackery. */
308 while (frag_tmp
309 && (frag_tmp->fr_type != rs_broken_word
310 || frag_tmp->fr_opcode))
311 frag_tmp = frag_tmp->fr_next;
312 know (frag_tmp);
313 frag_tmp->fr_opcode = frag_opcode;
314 new_broken_words = 0;
316 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
317 a->dispfrag = frag_tmp;
319 #endif /* WORKING_DOT_WORD */
321 if ((symbolP = symbol_find (sym_name)) != 0)
323 S_CLEAR_WEAKREFR (symbolP);
324 #ifdef RESOLVE_SYMBOL_REDEFINITION
325 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
326 return symbolP;
327 #endif
328 /* Now check for undefined symbols. */
329 if (LOCAL_SYMBOL_CHECK (symbolP))
331 struct local_symbol *locsym = (struct local_symbol *) symbolP;
333 if (locsym->lsy_section != undefined_section
334 && (local_symbol_get_frag (locsym) != frag_now
335 || locsym->lsy_section != now_seg
336 || locsym->lsy_value != frag_now_fix ()))
338 as_bad (_("symbol `%s' is already defined"), sym_name);
339 return symbolP;
342 locsym->lsy_section = now_seg;
343 local_symbol_set_frag (locsym, frag_now);
344 locsym->lsy_value = frag_now_fix ();
346 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
347 || S_IS_COMMON (symbolP)
348 || S_IS_VOLATILE (symbolP))
350 if (S_IS_VOLATILE (symbolP))
352 symbolP = symbol_clone (symbolP, 1);
353 S_SET_VALUE (symbolP, 0);
354 S_CLEAR_VOLATILE (symbolP);
356 if (S_GET_VALUE (symbolP) == 0)
358 define_sym_at_dot (symbolP);
359 #ifdef N_UNDF
360 know (N_UNDF == 0);
361 #endif /* if we have one, it better be zero. */
364 else
366 /* There are still several cases to check:
368 A .comm/.lcomm symbol being redefined as initialized
369 data is OK
371 A .comm/.lcomm symbol being redefined with a larger
372 size is also OK
374 This only used to be allowed on VMS gas, but Sun cc
375 on the sparc also depends on it. */
377 if (((!S_IS_DEBUG (symbolP)
378 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
379 && S_IS_EXTERNAL (symbolP))
380 || S_GET_SEGMENT (symbolP) == bss_section)
381 && (now_seg == data_section
382 || now_seg == bss_section
383 || now_seg == S_GET_SEGMENT (symbolP)))
385 /* Select which of the 2 cases this is. */
386 if (now_seg != data_section)
388 /* New .comm for prev .comm symbol.
390 If the new size is larger we just change its
391 value. If the new size is smaller, we ignore
392 this symbol. */
393 if (S_GET_VALUE (symbolP)
394 < ((unsigned) frag_now_fix ()))
396 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
399 else
401 /* It is a .comm/.lcomm being converted to initialized
402 data. */
403 define_sym_at_dot (symbolP);
406 else
408 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
409 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
410 static const char *od_buf = "";
411 #else
412 char od_buf[100];
413 od_buf[0] = '\0';
414 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
415 sprintf (od_buf, "%d.%d.",
416 S_GET_OTHER (symbolP),
417 S_GET_DESC (symbolP));
418 #endif
419 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
420 sym_name,
421 segment_name (S_GET_SEGMENT (symbolP)),
422 od_buf,
423 (long) S_GET_VALUE (symbolP));
425 } /* if the undefined symbol has no value */
427 else
429 /* Don't blow up if the definition is the same. */
430 if (!(frag_now == symbolP->sy_frag
431 && S_GET_VALUE (symbolP) == frag_now_fix ()
432 && S_GET_SEGMENT (symbolP) == now_seg))
434 as_bad (_("symbol `%s' is already defined"), sym_name);
435 symbolP = symbol_clone (symbolP, 0);
436 define_sym_at_dot (symbolP);
441 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
443 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
444 (valueT) frag_now_fix (),
445 frag_now);
447 else
449 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
450 frag_now);
451 #ifdef OBJ_VMS
452 S_SET_OTHER (symbolP, const_flag);
453 #endif /* OBJ_VMS */
455 symbol_table_insert (symbolP);
458 if (mri_common_symbol != NULL)
460 /* This symbol is actually being defined within an MRI common
461 section. This requires special handling. */
462 if (LOCAL_SYMBOL_CHECK (symbolP))
463 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
464 symbolP->sy_value.X_op = O_symbol;
465 symbolP->sy_value.X_add_symbol = mri_common_symbol;
466 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
467 symbolP->sy_frag = &zero_address_frag;
468 S_SET_SEGMENT (symbolP, expr_section);
469 symbolP->sy_mri_common = 1;
472 #ifdef tc_frob_label
473 tc_frob_label (symbolP);
474 #endif
475 #ifdef obj_frob_label
476 obj_frob_label (symbolP);
477 #endif
479 return symbolP;
482 /* Die if we can't insert the symbol. */
484 void
485 symbol_table_insert (symbolS *symbolP)
487 register const char *error_string;
489 know (symbolP);
490 know (S_GET_NAME (symbolP));
492 if (LOCAL_SYMBOL_CHECK (symbolP))
494 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
495 (void *) symbolP);
496 if (error_string != NULL)
497 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
498 S_GET_NAME (symbolP), error_string);
499 return;
502 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (void *) symbolP)))
504 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
505 S_GET_NAME (symbolP), error_string);
506 } /* on error */
509 /* If a symbol name does not exist, create it as undefined, and insert
510 it into the symbol table. Return a pointer to it. */
512 symbolS *
513 symbol_find_or_make (const char *name)
515 register symbolS *symbolP;
517 symbolP = symbol_find (name);
519 if (symbolP == NULL)
521 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
523 symbolP = md_undefined_symbol ((char *) name);
524 if (symbolP != NULL)
525 return symbolP;
527 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
528 (valueT) 0,
529 &zero_address_frag);
530 return symbolP;
533 symbolP = symbol_make (name);
535 symbol_table_insert (symbolP);
536 } /* if symbol wasn't found */
538 return (symbolP);
541 symbolS *
542 symbol_make (const char *name)
544 symbolS *symbolP;
546 /* Let the machine description default it, e.g. for register names. */
547 symbolP = md_undefined_symbol ((char *) name);
549 if (!symbolP)
550 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
552 return (symbolP);
555 symbolS *
556 symbol_clone (symbolS *orgsymP, int replace)
558 symbolS *newsymP;
559 asymbol *bsymorg, *bsymnew;
561 /* Make sure we never clone the dot special symbol. */
562 gas_assert (orgsymP != &dot_symbol);
564 /* Running local_symbol_convert on a clone that's not the one currently
565 in local_hash would incorrectly replace the hash entry. Thus the
566 symbol must be converted here. Note that the rest of the function
567 depends on not encountering an unconverted symbol. */
568 if (LOCAL_SYMBOL_CHECK (orgsymP))
569 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
570 bsymorg = orgsymP->bsym;
572 newsymP = (symbolS *) obstack_alloc (&notes, sizeof (*newsymP));
573 *newsymP = *orgsymP;
574 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
575 if (bsymnew == NULL)
576 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
577 newsymP->bsym = bsymnew;
578 bsymnew->name = bsymorg->name;
579 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
580 bsymnew->section = bsymorg->section;
581 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
582 bfd_asymbol_bfd (bsymnew), bsymnew);
584 #ifdef obj_symbol_clone_hook
585 obj_symbol_clone_hook (newsymP, orgsymP);
586 #endif
588 #ifdef tc_symbol_clone_hook
589 tc_symbol_clone_hook (newsymP, orgsymP);
590 #endif
592 if (replace)
594 if (symbol_rootP == orgsymP)
595 symbol_rootP = newsymP;
596 else if (orgsymP->sy_previous)
598 orgsymP->sy_previous->sy_next = newsymP;
599 orgsymP->sy_previous = NULL;
601 if (symbol_lastP == orgsymP)
602 symbol_lastP = newsymP;
603 else if (orgsymP->sy_next)
604 orgsymP->sy_next->sy_previous = newsymP;
606 /* Symbols that won't be output can't be external. */
607 S_CLEAR_EXTERNAL (orgsymP);
608 orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
609 debug_verify_symchain (symbol_rootP, symbol_lastP);
611 symbol_table_insert (newsymP);
613 else
615 /* Symbols that won't be output can't be external. */
616 S_CLEAR_EXTERNAL (newsymP);
617 newsymP->sy_previous = newsymP->sy_next = newsymP;
620 return newsymP;
623 /* Referenced symbols, if they are forward references, need to be cloned
624 (without replacing the original) so that the value of the referenced
625 symbols at the point of use . */
627 #undef symbol_clone_if_forward_ref
628 symbolS *
629 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
631 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
633 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
634 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
636 if (symbolP->sy_forward_ref)
637 is_forward = 1;
639 if (is_forward)
641 /* assign_symbol() clones volatile symbols; pre-existing expressions
642 hold references to the original instance, but want the current
643 value. Just repeat the lookup. */
644 if (add_symbol && S_IS_VOLATILE (add_symbol))
645 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
646 if (op_symbol && S_IS_VOLATILE (op_symbol))
647 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
650 /* Re-using sy_resolving here, as this routine cannot get called from
651 symbol resolution code. */
652 if ((symbolP->bsym->section == expr_section || symbolP->sy_forward_ref)
653 && !symbolP->sy_resolving)
655 symbolP->sy_resolving = 1;
656 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
657 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
658 symbolP->sy_resolving = 0;
661 if (symbolP->sy_forward_ref
662 || add_symbol != symbolP->sy_value.X_add_symbol
663 || op_symbol != symbolP->sy_value.X_op_symbol)
665 if (symbolP != &dot_symbol)
667 symbolP = symbol_clone (symbolP, 0);
668 symbolP->sy_resolving = 0;
670 else
672 symbolP = symbol_temp_new_now ();
673 #ifdef tc_new_dot_label
674 tc_new_dot_label (symbolP);
675 #endif
679 symbolP->sy_value.X_add_symbol = add_symbol;
680 symbolP->sy_value.X_op_symbol = op_symbol;
683 return symbolP;
686 symbolS *
687 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
689 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
692 symbolS *
693 symbol_temp_new_now (void)
695 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
698 symbolS *
699 symbol_temp_make (void)
701 return symbol_make (FAKE_LABEL_NAME);
704 /* Implement symbol table lookup.
705 In: A symbol's name as a string: '\0' can't be part of a symbol name.
706 Out: NULL if the name was not in the symbol table, else the address
707 of a struct symbol associated with that name. */
709 symbolS *
710 symbol_find_exact (const char *name)
712 return symbol_find_exact_noref (name, 0);
715 symbolS *
716 symbol_find_exact_noref (const char *name, int noref)
718 struct local_symbol *locsym;
719 symbolS* sym;
721 locsym = (struct local_symbol *) hash_find (local_hash, name);
722 if (locsym != NULL)
723 return (symbolS *) locsym;
725 sym = ((symbolS *) hash_find (sy_hash, name));
727 /* Any references to the symbol, except for the reference in
728 .weakref, must clear this flag, such that the symbol does not
729 turn into a weak symbol. Note that we don't have to handle the
730 local_symbol case, since a weakrefd is always promoted out of the
731 local_symbol table when it is turned into a weak symbol. */
732 if (sym && ! noref)
733 S_CLEAR_WEAKREFD (sym);
735 return sym;
738 symbolS *
739 symbol_find (const char *name)
741 return symbol_find_noref (name, 0);
744 symbolS *
745 symbol_find_noref (const char *name, int noref)
747 #ifdef tc_canonicalize_symbol_name
749 char *copy;
750 size_t len = strlen (name) + 1;
752 copy = (char *) alloca (len);
753 memcpy (copy, name, len);
754 name = tc_canonicalize_symbol_name (copy);
756 #endif
758 if (! symbols_case_sensitive)
760 char *copy;
761 const char *orig;
762 unsigned char c;
764 orig = name;
765 name = copy = (char *) alloca (strlen (name) + 1);
767 while ((c = *orig++) != '\0')
769 *copy++ = TOUPPER (c);
771 *copy = '\0';
774 return symbol_find_exact_noref (name, noref);
777 /* Once upon a time, symbols were kept in a singly linked list. At
778 least coff needs to be able to rearrange them from time to time, for
779 which a doubly linked list is much more convenient. Loic did these
780 as macros which seemed dangerous to me so they're now functions.
781 xoxorich. */
783 /* Link symbol ADDME after symbol TARGET in the chain. */
785 void
786 symbol_append (symbolS *addme, symbolS *target,
787 symbolS **rootPP, symbolS **lastPP)
789 if (LOCAL_SYMBOL_CHECK (addme))
790 abort ();
791 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
792 abort ();
794 if (target == NULL)
796 know (*rootPP == NULL);
797 know (*lastPP == NULL);
798 addme->sy_next = NULL;
799 addme->sy_previous = NULL;
800 *rootPP = addme;
801 *lastPP = addme;
802 return;
803 } /* if the list is empty */
805 if (target->sy_next != NULL)
807 target->sy_next->sy_previous = addme;
809 else
811 know (*lastPP == target);
812 *lastPP = addme;
813 } /* if we have a next */
815 addme->sy_next = target->sy_next;
816 target->sy_next = addme;
817 addme->sy_previous = target;
819 debug_verify_symchain (symbol_rootP, symbol_lastP);
822 /* Set the chain pointers of SYMBOL to null. */
824 void
825 symbol_clear_list_pointers (symbolS *symbolP)
827 if (LOCAL_SYMBOL_CHECK (symbolP))
828 abort ();
829 symbolP->sy_next = NULL;
830 symbolP->sy_previous = NULL;
833 /* Remove SYMBOLP from the list. */
835 void
836 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
838 if (LOCAL_SYMBOL_CHECK (symbolP))
839 abort ();
841 if (symbolP == *rootPP)
843 *rootPP = symbolP->sy_next;
844 } /* if it was the root */
846 if (symbolP == *lastPP)
848 *lastPP = symbolP->sy_previous;
849 } /* if it was the tail */
851 if (symbolP->sy_next != NULL)
853 symbolP->sy_next->sy_previous = symbolP->sy_previous;
854 } /* if not last */
856 if (symbolP->sy_previous != NULL)
858 symbolP->sy_previous->sy_next = symbolP->sy_next;
859 } /* if not first */
861 debug_verify_symchain (*rootPP, *lastPP);
864 /* Link symbol ADDME before symbol TARGET in the chain. */
866 void
867 symbol_insert (symbolS *addme, symbolS *target,
868 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
870 if (LOCAL_SYMBOL_CHECK (addme))
871 abort ();
872 if (LOCAL_SYMBOL_CHECK (target))
873 abort ();
875 if (target->sy_previous != NULL)
877 target->sy_previous->sy_next = addme;
879 else
881 know (*rootPP == target);
882 *rootPP = addme;
883 } /* if not first */
885 addme->sy_previous = target->sy_previous;
886 target->sy_previous = addme;
887 addme->sy_next = target;
889 debug_verify_symchain (*rootPP, *lastPP);
892 void
893 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
895 symbolS *symbolP = rootP;
897 if (symbolP == NULL)
898 return;
900 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
902 gas_assert (symbolP->bsym != NULL);
903 gas_assert (symbolP->sy_next->sy_previous == symbolP);
906 gas_assert (lastP == symbolP);
909 #ifdef OBJ_COMPLEX_RELC
911 static int
912 use_complex_relocs_for (symbolS * symp)
914 switch (symp->sy_value.X_op)
916 case O_constant:
917 return 0;
919 case O_symbol:
920 case O_symbol_rva:
921 case O_uminus:
922 case O_bit_not:
923 case O_logical_not:
924 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
925 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
927 (S_IS_DEFINED (symp->sy_value.X_add_symbol)
928 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section))
929 return 0;
930 break;
932 case O_multiply:
933 case O_divide:
934 case O_modulus:
935 case O_left_shift:
936 case O_right_shift:
937 case O_bit_inclusive_or:
938 case O_bit_or_not:
939 case O_bit_exclusive_or:
940 case O_bit_and:
941 case O_add:
942 case O_subtract:
943 case O_eq:
944 case O_ne:
945 case O_lt:
946 case O_le:
947 case O_ge:
948 case O_gt:
949 case O_logical_and:
950 case O_logical_or:
952 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
953 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
955 (S_IS_COMMON (symp->sy_value.X_op_symbol)
956 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
958 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
959 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
960 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section
961 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
962 return 0;
963 break;
965 default:
966 break;
968 return 1;
970 #endif
972 static void
973 report_op_error (symbolS *symp, symbolS *left, symbolS *right)
975 char *file;
976 unsigned int line;
977 segT seg_left = S_GET_SEGMENT (left);
978 segT seg_right = right ? S_GET_SEGMENT (right) : 0;
980 if (expr_symbol_where (symp, &file, &line))
982 if (seg_left == undefined_section)
983 as_bad_where (file, line,
984 _("undefined symbol `%s' in operation"),
985 S_GET_NAME (left));
986 if (seg_right == undefined_section)
987 as_bad_where (file, line,
988 _("undefined symbol `%s' in operation"),
989 S_GET_NAME (right));
990 if (seg_left != undefined_section
991 && seg_right != undefined_section)
993 if (right)
994 as_bad_where (file, line,
995 _("invalid sections for operation on `%s' and `%s'"),
996 S_GET_NAME (left), S_GET_NAME (right));
997 else
998 as_bad_where (file, line,
999 _("invalid section for operation on `%s'"),
1000 S_GET_NAME (left));
1004 else
1006 if (seg_left == undefined_section)
1007 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
1008 S_GET_NAME (left), S_GET_NAME (symp));
1009 if (seg_right == undefined_section)
1010 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
1011 S_GET_NAME (right), S_GET_NAME (symp));
1012 if (seg_left != undefined_section
1013 && seg_right != undefined_section)
1015 if (right)
1016 as_bad (_("invalid sections for operation on `%s' and `%s' setting `%s'"),
1017 S_GET_NAME (left), S_GET_NAME (right), S_GET_NAME (symp));
1018 else
1019 as_bad (_("invalid section for operation on `%s' setting `%s'"),
1020 S_GET_NAME (left), S_GET_NAME (symp));
1025 /* Resolve the value of a symbol. This is called during the final
1026 pass over the symbol table to resolve any symbols with complex
1027 values. */
1029 valueT
1030 resolve_symbol_value (symbolS *symp)
1032 int resolved;
1033 valueT final_val = 0;
1034 segT final_seg;
1036 if (LOCAL_SYMBOL_CHECK (symp))
1038 struct local_symbol *locsym = (struct local_symbol *) symp;
1040 final_val = locsym->lsy_value;
1041 if (local_symbol_resolved_p (locsym))
1042 return final_val;
1044 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1046 if (finalize_syms)
1048 locsym->lsy_value = final_val;
1049 local_symbol_mark_resolved (locsym);
1052 return final_val;
1055 if (symp->sy_resolved)
1057 if (symp->sy_value.X_op == O_constant)
1058 return (valueT) symp->sy_value.X_add_number;
1059 else
1060 return 0;
1063 resolved = 0;
1064 final_seg = S_GET_SEGMENT (symp);
1066 if (symp->sy_resolving)
1068 if (finalize_syms)
1069 as_bad (_("symbol definition loop encountered at `%s'"),
1070 S_GET_NAME (symp));
1071 final_val = 0;
1072 resolved = 1;
1074 #ifdef OBJ_COMPLEX_RELC
1075 else if (final_seg == expr_section
1076 && use_complex_relocs_for (symp))
1078 symbolS * relc_symbol = NULL;
1079 char * relc_symbol_name = NULL;
1081 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1083 /* For debugging, print out conversion input & output. */
1084 #ifdef DEBUG_SYMS
1085 print_expr (& symp->sy_value);
1086 if (relc_symbol_name)
1087 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1088 #endif
1090 if (relc_symbol_name != NULL)
1091 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1092 0, & zero_address_frag);
1094 if (relc_symbol == NULL)
1096 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1097 S_GET_NAME (symp));
1098 resolved = 0;
1100 else
1102 symbol_table_insert (relc_symbol);
1104 /* S_CLEAR_EXTERNAL (relc_symbol); */
1105 if (symp->bsym->flags & BSF_SRELC)
1106 relc_symbol->bsym->flags |= BSF_SRELC;
1107 else
1108 relc_symbol->bsym->flags |= BSF_RELC;
1109 /* symp->bsym->flags |= BSF_RELC; */
1110 copy_symbol_attributes (symp, relc_symbol);
1111 symp->sy_value.X_op = O_symbol;
1112 symp->sy_value.X_add_symbol = relc_symbol;
1113 symp->sy_value.X_add_number = 0;
1114 resolved = 1;
1117 final_seg = undefined_section;
1118 goto exit_dont_set_value;
1120 #endif
1121 else
1123 symbolS *add_symbol, *op_symbol;
1124 offsetT left, right;
1125 segT seg_left, seg_right;
1126 operatorT op;
1127 int move_seg_ok;
1129 symp->sy_resolving = 1;
1131 /* Help out with CSE. */
1132 add_symbol = symp->sy_value.X_add_symbol;
1133 op_symbol = symp->sy_value.X_op_symbol;
1134 final_val = symp->sy_value.X_add_number;
1135 op = symp->sy_value.X_op;
1137 switch (op)
1139 default:
1140 BAD_CASE (op);
1141 break;
1143 case O_absent:
1144 final_val = 0;
1145 /* Fall through. */
1147 case O_constant:
1148 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1149 if (final_seg == expr_section)
1150 final_seg = absolute_section;
1151 /* Fall through. */
1153 case O_register:
1154 resolved = 1;
1155 break;
1157 case O_symbol:
1158 case O_symbol_rva:
1159 left = resolve_symbol_value (add_symbol);
1160 seg_left = S_GET_SEGMENT (add_symbol);
1161 if (finalize_syms)
1162 symp->sy_value.X_op_symbol = NULL;
1164 do_symbol:
1165 if (S_IS_WEAKREFR (symp))
1167 gas_assert (final_val == 0);
1168 if (S_IS_WEAKREFR (add_symbol))
1170 gas_assert (add_symbol->sy_value.X_op == O_symbol
1171 && add_symbol->sy_value.X_add_number == 0);
1172 add_symbol = add_symbol->sy_value.X_add_symbol;
1173 gas_assert (! S_IS_WEAKREFR (add_symbol));
1174 symp->sy_value.X_add_symbol = add_symbol;
1178 if (symp->sy_mri_common)
1180 /* This is a symbol inside an MRI common section. The
1181 relocation routines are going to handle it specially.
1182 Don't change the value. */
1183 resolved = symbol_resolved_p (add_symbol);
1184 break;
1187 if (finalize_syms && final_val == 0)
1189 if (LOCAL_SYMBOL_CHECK (add_symbol))
1190 add_symbol = local_symbol_convert ((struct local_symbol *)
1191 add_symbol);
1192 copy_symbol_attributes (symp, add_symbol);
1195 /* If we have equated this symbol to an undefined or common
1196 symbol, keep X_op set to O_symbol, and don't change
1197 X_add_number. This permits the routine which writes out
1198 relocation to detect this case, and convert the
1199 relocation to be against the symbol to which this symbol
1200 is equated. */
1201 if (! S_IS_DEFINED (add_symbol)
1202 #if defined (OBJ_COFF) && defined (TE_PE)
1203 || S_IS_WEAK (add_symbol)
1204 #endif
1205 || S_IS_COMMON (add_symbol))
1207 if (finalize_syms)
1209 symp->sy_value.X_op = O_symbol;
1210 symp->sy_value.X_add_symbol = add_symbol;
1211 symp->sy_value.X_add_number = final_val;
1212 /* Use X_op_symbol as a flag. */
1213 symp->sy_value.X_op_symbol = add_symbol;
1214 final_seg = seg_left;
1216 final_val = 0;
1217 resolved = symbol_resolved_p (add_symbol);
1218 symp->sy_resolving = 0;
1219 goto exit_dont_set_value;
1221 else if (finalize_syms
1222 && ((final_seg == expr_section && seg_left != expr_section)
1223 || symbol_shadow_p (symp)))
1225 /* If the symbol is an expression symbol, do similarly
1226 as for undefined and common syms above. Handles
1227 "sym +/- expr" where "expr" cannot be evaluated
1228 immediately, and we want relocations to be against
1229 "sym", eg. because it is weak. */
1230 symp->sy_value.X_op = O_symbol;
1231 symp->sy_value.X_add_symbol = add_symbol;
1232 symp->sy_value.X_add_number = final_val;
1233 symp->sy_value.X_op_symbol = add_symbol;
1234 final_seg = seg_left;
1235 final_val += symp->sy_frag->fr_address + left;
1236 resolved = symbol_resolved_p (add_symbol);
1237 symp->sy_resolving = 0;
1238 goto exit_dont_set_value;
1240 else
1242 final_val += symp->sy_frag->fr_address + left;
1243 if (final_seg == expr_section || final_seg == undefined_section)
1244 final_seg = seg_left;
1247 resolved = symbol_resolved_p (add_symbol);
1248 if (S_IS_WEAKREFR (symp))
1249 goto exit_dont_set_value;
1250 break;
1252 case O_uminus:
1253 case O_bit_not:
1254 case O_logical_not:
1255 left = resolve_symbol_value (add_symbol);
1256 seg_left = S_GET_SEGMENT (add_symbol);
1258 /* By reducing these to the relevant dyadic operator, we get
1259 !S -> S == 0 permitted on anything,
1260 -S -> 0 - S only permitted on absolute
1261 ~S -> S ^ ~0 only permitted on absolute */
1262 if (op != O_logical_not && seg_left != absolute_section
1263 && finalize_syms)
1264 report_op_error (symp, add_symbol, NULL);
1266 if (final_seg == expr_section || final_seg == undefined_section)
1267 final_seg = absolute_section;
1269 if (op == O_uminus)
1270 left = -left;
1271 else if (op == O_logical_not)
1272 left = !left;
1273 else
1274 left = ~left;
1276 final_val += left + symp->sy_frag->fr_address;
1278 resolved = symbol_resolved_p (add_symbol);
1279 break;
1281 case O_multiply:
1282 case O_divide:
1283 case O_modulus:
1284 case O_left_shift:
1285 case O_right_shift:
1286 case O_bit_inclusive_or:
1287 case O_bit_or_not:
1288 case O_bit_exclusive_or:
1289 case O_bit_and:
1290 case O_add:
1291 case O_subtract:
1292 case O_eq:
1293 case O_ne:
1294 case O_lt:
1295 case O_le:
1296 case O_ge:
1297 case O_gt:
1298 case O_logical_and:
1299 case O_logical_or:
1300 left = resolve_symbol_value (add_symbol);
1301 right = resolve_symbol_value (op_symbol);
1302 seg_left = S_GET_SEGMENT (add_symbol);
1303 seg_right = S_GET_SEGMENT (op_symbol);
1305 /* Simplify addition or subtraction of a constant by folding the
1306 constant into X_add_number. */
1307 if (op == O_add)
1309 if (seg_right == absolute_section)
1311 final_val += right;
1312 goto do_symbol;
1314 else if (seg_left == absolute_section)
1316 final_val += left;
1317 add_symbol = op_symbol;
1318 left = right;
1319 seg_left = seg_right;
1320 goto do_symbol;
1323 else if (op == O_subtract)
1325 if (seg_right == absolute_section)
1327 final_val -= right;
1328 goto do_symbol;
1332 move_seg_ok = 1;
1333 /* Equality and non-equality tests are permitted on anything.
1334 Subtraction, and other comparison operators are permitted if
1335 both operands are in the same section. Otherwise, both
1336 operands must be absolute. We already handled the case of
1337 addition or subtraction of a constant above. This will
1338 probably need to be changed for an object file format which
1339 supports arbitrary expressions, such as IEEE-695. */
1340 if (!(seg_left == absolute_section
1341 && seg_right == absolute_section)
1342 && !(op == O_eq || op == O_ne)
1343 && !((op == O_subtract
1344 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1345 && seg_left == seg_right
1346 && (seg_left != undefined_section
1347 || add_symbol == op_symbol)))
1349 /* Don't emit messages unless we're finalizing the symbol value,
1350 otherwise we may get the same message multiple times. */
1351 if (finalize_syms)
1352 report_op_error (symp, add_symbol, op_symbol);
1353 /* However do not move the symbol into the absolute section
1354 if it cannot currently be resolved - this would confuse
1355 other parts of the assembler into believing that the
1356 expression had been evaluated to zero. */
1357 else
1358 move_seg_ok = 0;
1361 if (move_seg_ok
1362 && (final_seg == expr_section || final_seg == undefined_section))
1363 final_seg = absolute_section;
1365 /* Check for division by zero. */
1366 if ((op == O_divide || op == O_modulus) && right == 0)
1368 /* If seg_right is not absolute_section, then we've
1369 already issued a warning about using a bad symbol. */
1370 if (seg_right == absolute_section && finalize_syms)
1372 char *file;
1373 unsigned int line;
1375 if (expr_symbol_where (symp, &file, &line))
1376 as_bad_where (file, line, _("division by zero"));
1377 else
1378 as_bad (_("division by zero when setting `%s'"),
1379 S_GET_NAME (symp));
1382 right = 1;
1385 switch (symp->sy_value.X_op)
1387 case O_multiply: left *= right; break;
1388 case O_divide: left /= right; break;
1389 case O_modulus: left %= right; break;
1390 case O_left_shift: left <<= right; break;
1391 case O_right_shift: left >>= right; break;
1392 case O_bit_inclusive_or: left |= right; break;
1393 case O_bit_or_not: left |= ~right; break;
1394 case O_bit_exclusive_or: left ^= right; break;
1395 case O_bit_and: left &= right; break;
1396 case O_add: left += right; break;
1397 case O_subtract: left -= right; break;
1398 case O_eq:
1399 case O_ne:
1400 left = (left == right && seg_left == seg_right
1401 && (seg_left != undefined_section
1402 || add_symbol == op_symbol)
1403 ? ~ (offsetT) 0 : 0);
1404 if (symp->sy_value.X_op == O_ne)
1405 left = ~left;
1406 break;
1407 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1408 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1409 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1410 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1411 case O_logical_and: left = left && right; break;
1412 case O_logical_or: left = left || right; break;
1413 default: abort ();
1416 final_val += symp->sy_frag->fr_address + left;
1417 if (final_seg == expr_section || final_seg == undefined_section)
1419 if (seg_left == undefined_section
1420 || seg_right == undefined_section)
1421 final_seg = undefined_section;
1422 else if (seg_left == absolute_section)
1423 final_seg = seg_right;
1424 else
1425 final_seg = seg_left;
1427 resolved = (symbol_resolved_p (add_symbol)
1428 && symbol_resolved_p (op_symbol));
1429 break;
1431 case O_big:
1432 case O_illegal:
1433 /* Give an error (below) if not in expr_section. We don't
1434 want to worry about expr_section symbols, because they
1435 are fictional (they are created as part of expression
1436 resolution), and any problems may not actually mean
1437 anything. */
1438 break;
1441 symp->sy_resolving = 0;
1444 if (finalize_syms)
1445 S_SET_VALUE (symp, final_val);
1447 exit_dont_set_value:
1448 /* Always set the segment, even if not finalizing the value.
1449 The segment is used to determine whether a symbol is defined. */
1450 S_SET_SEGMENT (symp, final_seg);
1452 /* Don't worry if we can't resolve an expr_section symbol. */
1453 if (finalize_syms)
1455 if (resolved)
1456 symp->sy_resolved = 1;
1457 else if (S_GET_SEGMENT (symp) != expr_section)
1459 as_bad (_("can't resolve value for symbol `%s'"),
1460 S_GET_NAME (symp));
1461 symp->sy_resolved = 1;
1465 return final_val;
1468 static void resolve_local_symbol (const char *, void *);
1470 /* A static function passed to hash_traverse. */
1472 static void
1473 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1475 if (value != NULL)
1476 resolve_symbol_value ((symbolS *) value);
1479 /* Resolve all local symbols. */
1481 void
1482 resolve_local_symbol_values (void)
1484 hash_traverse (local_hash, resolve_local_symbol);
1487 /* Obtain the current value of a symbol without changing any
1488 sub-expressions used. */
1491 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1493 symbolS *symbolP = *symbolPP;
1495 if (LOCAL_SYMBOL_CHECK (symbolP))
1497 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1499 *valueP = locsym->lsy_value;
1500 *segP = locsym->lsy_section;
1501 *fragPP = local_symbol_get_frag (locsym);
1503 else
1505 expressionS exp = symbolP->sy_value;
1507 if (!symbolP->sy_resolved && exp.X_op != O_illegal)
1509 int resolved;
1511 if (symbolP->sy_resolving)
1512 return 0;
1513 symbolP->sy_resolving = 1;
1514 resolved = resolve_expression (&exp);
1515 symbolP->sy_resolving = 0;
1516 if (!resolved)
1517 return 0;
1519 switch (exp.X_op)
1521 case O_constant:
1522 case O_register:
1523 if (!symbol_equated_p (symbolP))
1524 break;
1525 /* Fall thru. */
1526 case O_symbol:
1527 case O_symbol_rva:
1528 symbolP = exp.X_add_symbol;
1529 break;
1530 default:
1531 return 0;
1535 *symbolPP = symbolP;
1536 *valueP = exp.X_add_number;
1537 *segP = symbolP->bsym->section;
1538 *fragPP = symbolP->sy_frag;
1540 if (*segP == expr_section)
1541 switch (exp.X_op)
1543 case O_constant: *segP = absolute_section; break;
1544 case O_register: *segP = reg_section; break;
1545 default: break;
1549 return 1;
1552 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1553 They are *really* local. That is, they go out of scope whenever we see a
1554 label that isn't local. Also, like fb labels, there can be multiple
1555 instances of a dollar label. Therefor, we name encode each instance with
1556 the instance number, keep a list of defined symbols separate from the real
1557 symbol table, and we treat these buggers as a sparse array. */
1559 static long *dollar_labels;
1560 static long *dollar_label_instances;
1561 static char *dollar_label_defines;
1562 static unsigned long dollar_label_count;
1563 static unsigned long dollar_label_max;
1566 dollar_label_defined (long label)
1568 long *i;
1570 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1572 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1573 if (*i == label)
1574 return dollar_label_defines[i - dollar_labels];
1576 /* If we get here, label isn't defined. */
1577 return 0;
1580 static long
1581 dollar_label_instance (long label)
1583 long *i;
1585 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1587 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1588 if (*i == label)
1589 return (dollar_label_instances[i - dollar_labels]);
1591 /* If we get here, we haven't seen the label before.
1592 Therefore its instance count is zero. */
1593 return 0;
1596 void
1597 dollar_label_clear (void)
1599 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1602 #define DOLLAR_LABEL_BUMP_BY 10
1604 void
1605 define_dollar_label (long label)
1607 long *i;
1609 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1610 if (*i == label)
1612 ++dollar_label_instances[i - dollar_labels];
1613 dollar_label_defines[i - dollar_labels] = 1;
1614 return;
1617 /* If we get to here, we don't have label listed yet. */
1619 if (dollar_labels == NULL)
1621 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1622 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1623 dollar_label_defines = (char *) xmalloc (DOLLAR_LABEL_BUMP_BY);
1624 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1625 dollar_label_count = 0;
1627 else if (dollar_label_count == dollar_label_max)
1629 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1630 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1631 dollar_label_max * sizeof (long));
1632 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1633 dollar_label_max * sizeof (long));
1634 dollar_label_defines = (char *) xrealloc (dollar_label_defines, dollar_label_max);
1635 } /* if we needed to grow */
1637 dollar_labels[dollar_label_count] = label;
1638 dollar_label_instances[dollar_label_count] = 1;
1639 dollar_label_defines[dollar_label_count] = 1;
1640 ++dollar_label_count;
1643 /* Caller must copy returned name: we re-use the area for the next name.
1645 The mth occurence of label n: is turned into the symbol "Ln^Am"
1646 where n is the label number and m is the instance number. "L" makes
1647 it a label discarded unless debugging and "^A"('\1') ensures no
1648 ordinary symbol SHOULD get the same name as a local label
1649 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1651 fb labels get the same treatment, except that ^B is used in place
1652 of ^A. */
1654 char * /* Return local label name. */
1655 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1656 register int augend /* 0 for current instance, 1 for new instance. */)
1658 long i;
1659 /* Returned to caller, then copied. Used for created names ("4f"). */
1660 static char symbol_name_build[24];
1661 register char *p;
1662 register char *q;
1663 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1665 know (n >= 0);
1666 know (augend == 0 || augend == 1);
1667 p = symbol_name_build;
1668 #ifdef LOCAL_LABEL_PREFIX
1669 *p++ = LOCAL_LABEL_PREFIX;
1670 #endif
1671 *p++ = 'L';
1673 /* Next code just does sprintf( {}, "%d", n); */
1674 /* Label number. */
1675 q = symbol_name_temporary;
1676 for (*q++ = 0, i = n; i; ++q)
1678 *q = i % 10 + '0';
1679 i /= 10;
1681 while ((*p = *--q) != '\0')
1682 ++p;
1684 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1686 /* Instance number. */
1687 q = symbol_name_temporary;
1688 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1690 *q = i % 10 + '0';
1691 i /= 10;
1693 while ((*p++ = *--q) != '\0');;
1695 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1696 return symbol_name_build;
1699 /* Somebody else's idea of local labels. They are made by "n:" where n
1700 is any decimal digit. Refer to them with
1701 "nb" for previous (backward) n:
1702 or "nf" for next (forward) n:.
1704 We do a little better and let n be any number, not just a single digit, but
1705 since the other guy's assembler only does ten, we treat the first ten
1706 specially.
1708 Like someone else's assembler, we have one set of local label counters for
1709 entire assembly, not one set per (sub)segment like in most assemblers. This
1710 implies that one can refer to a label in another segment, and indeed some
1711 crufty compilers have done just that.
1713 Since there could be a LOT of these things, treat them as a sparse
1714 array. */
1716 #define FB_LABEL_SPECIAL (10)
1718 static long fb_low_counter[FB_LABEL_SPECIAL];
1719 static long *fb_labels;
1720 static long *fb_label_instances;
1721 static long fb_label_count;
1722 static long fb_label_max;
1724 /* This must be more than FB_LABEL_SPECIAL. */
1725 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1727 static void
1728 fb_label_init (void)
1730 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1733 /* Add one to the instance number of this fb label. */
1735 void
1736 fb_label_instance_inc (long label)
1738 long *i;
1740 if (label < FB_LABEL_SPECIAL)
1742 ++fb_low_counter[label];
1743 return;
1746 if (fb_labels != NULL)
1748 for (i = fb_labels + FB_LABEL_SPECIAL;
1749 i < fb_labels + fb_label_count; ++i)
1751 if (*i == label)
1753 ++fb_label_instances[i - fb_labels];
1754 return;
1755 } /* if we find it */
1756 } /* for each existing label */
1759 /* If we get to here, we don't have label listed yet. */
1761 if (fb_labels == NULL)
1763 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1764 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1765 fb_label_max = FB_LABEL_BUMP_BY;
1766 fb_label_count = FB_LABEL_SPECIAL;
1769 else if (fb_label_count == fb_label_max)
1771 fb_label_max += FB_LABEL_BUMP_BY;
1772 fb_labels = (long *) xrealloc ((char *) fb_labels,
1773 fb_label_max * sizeof (long));
1774 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1775 fb_label_max * sizeof (long));
1776 } /* if we needed to grow */
1778 fb_labels[fb_label_count] = label;
1779 fb_label_instances[fb_label_count] = 1;
1780 ++fb_label_count;
1783 static long
1784 fb_label_instance (long label)
1786 long *i;
1788 if (label < FB_LABEL_SPECIAL)
1790 return (fb_low_counter[label]);
1793 if (fb_labels != NULL)
1795 for (i = fb_labels + FB_LABEL_SPECIAL;
1796 i < fb_labels + fb_label_count; ++i)
1798 if (*i == label)
1800 return (fb_label_instances[i - fb_labels]);
1801 } /* if we find it */
1802 } /* for each existing label */
1805 /* We didn't find the label, so this must be a reference to the
1806 first instance. */
1807 return 0;
1810 /* Caller must copy returned name: we re-use the area for the next name.
1812 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1813 where n is the label number and m is the instance number. "L" makes
1814 it a label discarded unless debugging and "^B"('\2') ensures no
1815 ordinary symbol SHOULD get the same name as a local label
1816 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1818 dollar labels get the same treatment, except that ^A is used in
1819 place of ^B. */
1821 char * /* Return local label name. */
1822 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1823 long augend /* 0 for nb, 1 for n:, nf. */)
1825 long i;
1826 /* Returned to caller, then copied. Used for created names ("4f"). */
1827 static char symbol_name_build[24];
1828 register char *p;
1829 register char *q;
1830 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1832 know (n >= 0);
1833 #ifdef TC_MMIX
1834 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1835 #else
1836 know ((unsigned long) augend <= 1);
1837 #endif
1838 p = symbol_name_build;
1839 #ifdef LOCAL_LABEL_PREFIX
1840 *p++ = LOCAL_LABEL_PREFIX;
1841 #endif
1842 *p++ = 'L';
1844 /* Next code just does sprintf( {}, "%d", n); */
1845 /* Label number. */
1846 q = symbol_name_temporary;
1847 for (*q++ = 0, i = n; i; ++q)
1849 *q = i % 10 + '0';
1850 i /= 10;
1852 while ((*p = *--q) != '\0')
1853 ++p;
1855 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1857 /* Instance number. */
1858 q = symbol_name_temporary;
1859 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1861 *q = i % 10 + '0';
1862 i /= 10;
1864 while ((*p++ = *--q) != '\0');;
1866 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1867 return (symbol_name_build);
1870 /* Decode name that may have been generated by foo_label_name() above.
1871 If the name wasn't generated by foo_label_name(), then return it
1872 unaltered. This is used for error messages. */
1874 char *
1875 decode_local_label_name (char *s)
1877 char *p;
1878 char *symbol_decode;
1879 int label_number;
1880 int instance_number;
1881 char *type;
1882 const char *message_format;
1883 int lindex = 0;
1885 #ifdef LOCAL_LABEL_PREFIX
1886 if (s[lindex] == LOCAL_LABEL_PREFIX)
1887 ++lindex;
1888 #endif
1890 if (s[lindex] != 'L')
1891 return s;
1893 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
1894 label_number = (10 * label_number) + *p - '0';
1896 if (*p == DOLLAR_LABEL_CHAR)
1897 type = "dollar";
1898 else if (*p == LOCAL_LABEL_CHAR)
1899 type = "fb";
1900 else
1901 return s;
1903 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1904 instance_number = (10 * instance_number) + *p - '0';
1906 message_format = _("\"%d\" (instance number %d of a %s label)");
1907 symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
1908 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1910 return symbol_decode;
1913 /* Get the value of a symbol. */
1915 valueT
1916 S_GET_VALUE (symbolS *s)
1918 if (LOCAL_SYMBOL_CHECK (s))
1919 return resolve_symbol_value (s);
1921 if (!s->sy_resolved)
1923 valueT val = resolve_symbol_value (s);
1924 if (!finalize_syms)
1925 return val;
1927 if (S_IS_WEAKREFR (s))
1928 return S_GET_VALUE (s->sy_value.X_add_symbol);
1930 if (s->sy_value.X_op != O_constant)
1932 if (! s->sy_resolved
1933 || s->sy_value.X_op != O_symbol
1934 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1935 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1936 S_GET_NAME (s));
1938 return (valueT) s->sy_value.X_add_number;
1941 /* Set the value of a symbol. */
1943 void
1944 S_SET_VALUE (symbolS *s, valueT val)
1946 if (LOCAL_SYMBOL_CHECK (s))
1948 ((struct local_symbol *) s)->lsy_value = val;
1949 return;
1952 s->sy_value.X_op = O_constant;
1953 s->sy_value.X_add_number = (offsetT) val;
1954 s->sy_value.X_unsigned = 0;
1955 S_CLEAR_WEAKREFR (s);
1958 void
1959 copy_symbol_attributes (symbolS *dest, symbolS *src)
1961 if (LOCAL_SYMBOL_CHECK (dest))
1962 dest = local_symbol_convert ((struct local_symbol *) dest);
1963 if (LOCAL_SYMBOL_CHECK (src))
1964 src = local_symbol_convert ((struct local_symbol *) src);
1966 /* In an expression, transfer the settings of these flags.
1967 The user can override later, of course. */
1968 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1969 | BSF_GNU_INDIRECT_FUNCTION)
1970 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1972 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1973 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1974 #endif
1976 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1977 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
1978 #endif
1982 S_IS_FUNCTION (symbolS *s)
1984 flagword flags;
1986 if (LOCAL_SYMBOL_CHECK (s))
1987 return 0;
1989 flags = s->bsym->flags;
1991 return (flags & BSF_FUNCTION) != 0;
1995 S_IS_EXTERNAL (symbolS *s)
1997 flagword flags;
1999 if (LOCAL_SYMBOL_CHECK (s))
2000 return 0;
2002 flags = s->bsym->flags;
2004 /* Sanity check. */
2005 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2006 abort ();
2008 return (flags & BSF_GLOBAL) != 0;
2012 S_IS_WEAK (symbolS *s)
2014 if (LOCAL_SYMBOL_CHECK (s))
2015 return 0;
2016 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2017 could probably handle a WEAKREFR as always weak though. E.g., if
2018 the referenced symbol has lost its weak status, there's no reason
2019 to keep handling the weakrefr as if it was weak. */
2020 if (S_IS_WEAKREFR (s))
2021 return S_IS_WEAK (s->sy_value.X_add_symbol);
2022 return (s->bsym->flags & BSF_WEAK) != 0;
2026 S_IS_WEAKREFR (symbolS *s)
2028 if (LOCAL_SYMBOL_CHECK (s))
2029 return 0;
2030 return s->sy_weakrefr != 0;
2034 S_IS_WEAKREFD (symbolS *s)
2036 if (LOCAL_SYMBOL_CHECK (s))
2037 return 0;
2038 return s->sy_weakrefd != 0;
2042 S_IS_COMMON (symbolS *s)
2044 if (LOCAL_SYMBOL_CHECK (s))
2045 return 0;
2046 return bfd_is_com_section (s->bsym->section);
2050 S_IS_DEFINED (symbolS *s)
2052 if (LOCAL_SYMBOL_CHECK (s))
2053 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2054 return s->bsym->section != undefined_section;
2058 #ifndef EXTERN_FORCE_RELOC
2059 #define EXTERN_FORCE_RELOC IS_ELF
2060 #endif
2062 /* Return true for symbols that should not be reduced to section
2063 symbols or eliminated from expressions, because they may be
2064 overridden by the linker. */
2066 S_FORCE_RELOC (symbolS *s, int strict)
2068 if (LOCAL_SYMBOL_CHECK (s))
2069 return ((struct local_symbol *) s)->lsy_section == undefined_section;
2071 return ((strict
2072 && ((s->bsym->flags & BSF_WEAK) != 0
2073 || (EXTERN_FORCE_RELOC
2074 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2075 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0
2076 || s->bsym->section == undefined_section
2077 || bfd_is_com_section (s->bsym->section));
2081 S_IS_DEBUG (symbolS *s)
2083 if (LOCAL_SYMBOL_CHECK (s))
2084 return 0;
2085 if (s->bsym->flags & BSF_DEBUGGING)
2086 return 1;
2087 return 0;
2091 S_IS_LOCAL (symbolS *s)
2093 flagword flags;
2094 const char *name;
2096 if (LOCAL_SYMBOL_CHECK (s))
2097 return 1;
2099 flags = s->bsym->flags;
2101 /* Sanity check. */
2102 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2103 abort ();
2105 if (bfd_get_section (s->bsym) == reg_section)
2106 return 1;
2108 if (flag_strip_local_absolute
2109 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2110 the source file even when the object file is stripped. */
2111 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2112 && bfd_get_section (s->bsym) == absolute_section)
2113 return 1;
2115 name = S_GET_NAME (s);
2116 return (name != NULL
2117 && ! S_IS_DEBUG (s)
2118 && (strchr (name, DOLLAR_LABEL_CHAR)
2119 || strchr (name, LOCAL_LABEL_CHAR)
2120 || (! flag_keep_locals
2121 && (bfd_is_local_label (stdoutput, s->bsym)
2122 || (flag_mri
2123 && name[0] == '?'
2124 && name[1] == '?')))));
2128 S_IS_STABD (symbolS *s)
2130 return S_GET_NAME (s) == 0;
2134 S_IS_VOLATILE (const symbolS *s)
2136 if (LOCAL_SYMBOL_CHECK (s))
2137 return 0;
2138 return s->sy_volatile;
2142 S_IS_FORWARD_REF (const symbolS *s)
2144 if (LOCAL_SYMBOL_CHECK (s))
2145 return 0;
2146 return s->sy_forward_ref;
2149 const char *
2150 S_GET_NAME (symbolS *s)
2152 if (LOCAL_SYMBOL_CHECK (s))
2153 return ((struct local_symbol *) s)->lsy_name;
2154 return s->bsym->name;
2157 segT
2158 S_GET_SEGMENT (symbolS *s)
2160 if (LOCAL_SYMBOL_CHECK (s))
2161 return ((struct local_symbol *) s)->lsy_section;
2162 return s->bsym->section;
2165 void
2166 S_SET_SEGMENT (symbolS *s, segT seg)
2168 /* Don't reassign section symbols. The direct reason is to prevent seg
2169 faults assigning back to const global symbols such as *ABS*, but it
2170 shouldn't happen anyway. */
2172 if (LOCAL_SYMBOL_CHECK (s))
2174 if (seg == reg_section)
2175 s = local_symbol_convert ((struct local_symbol *) s);
2176 else
2178 ((struct local_symbol *) s)->lsy_section = seg;
2179 return;
2183 if (s->bsym->flags & BSF_SECTION_SYM)
2185 if (s->bsym->section != seg)
2186 abort ();
2188 else
2189 s->bsym->section = seg;
2192 void
2193 S_SET_EXTERNAL (symbolS *s)
2195 if (LOCAL_SYMBOL_CHECK (s))
2196 s = local_symbol_convert ((struct local_symbol *) s);
2197 if ((s->bsym->flags & BSF_WEAK) != 0)
2199 /* Let .weak override .global. */
2200 return;
2202 if (s->bsym->flags & BSF_SECTION_SYM)
2204 char * file;
2205 unsigned int line;
2207 /* Do not reassign section symbols. */
2208 as_where (& file, & line);
2209 as_warn_where (file, line,
2210 _("section symbols are already global"));
2211 return;
2213 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2214 if (S_GET_SEGMENT (s) == reg_section)
2216 as_bad ("can't make register symbol `%s' global",
2217 S_GET_NAME (s));
2218 return;
2220 #endif
2221 s->bsym->flags |= BSF_GLOBAL;
2222 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2224 #ifdef TE_PE
2225 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2226 an_external_name = S_GET_NAME (s);
2227 #endif
2230 void
2231 S_CLEAR_EXTERNAL (symbolS *s)
2233 if (LOCAL_SYMBOL_CHECK (s))
2234 return;
2235 if ((s->bsym->flags & BSF_WEAK) != 0)
2237 /* Let .weak override. */
2238 return;
2240 s->bsym->flags |= BSF_LOCAL;
2241 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2244 void
2245 S_SET_WEAK (symbolS *s)
2247 if (LOCAL_SYMBOL_CHECK (s))
2248 s = local_symbol_convert ((struct local_symbol *) s);
2249 #ifdef obj_set_weak_hook
2250 obj_set_weak_hook (s);
2251 #endif
2252 s->bsym->flags |= BSF_WEAK;
2253 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2256 void
2257 S_SET_WEAKREFR (symbolS *s)
2259 if (LOCAL_SYMBOL_CHECK (s))
2260 s = local_symbol_convert ((struct local_symbol *) s);
2261 s->sy_weakrefr = 1;
2262 /* If the alias was already used, make sure we mark the target as
2263 used as well, otherwise it might be dropped from the symbol
2264 table. This may have unintended side effects if the alias is
2265 later redirected to another symbol, such as keeping the unused
2266 previous target in the symbol table. Since it will be weak, it's
2267 not a big deal. */
2268 if (s->sy_used)
2269 symbol_mark_used (s->sy_value.X_add_symbol);
2272 void
2273 S_CLEAR_WEAKREFR (symbolS *s)
2275 if (LOCAL_SYMBOL_CHECK (s))
2276 return;
2277 s->sy_weakrefr = 0;
2280 void
2281 S_SET_WEAKREFD (symbolS *s)
2283 if (LOCAL_SYMBOL_CHECK (s))
2284 s = local_symbol_convert ((struct local_symbol *) s);
2285 s->sy_weakrefd = 1;
2286 S_SET_WEAK (s);
2289 void
2290 S_CLEAR_WEAKREFD (symbolS *s)
2292 if (LOCAL_SYMBOL_CHECK (s))
2293 return;
2294 if (s->sy_weakrefd)
2296 s->sy_weakrefd = 0;
2297 /* If a weakref target symbol is weak, then it was never
2298 referenced directly before, not even in a .global directive,
2299 so decay it to local. If it remains undefined, it will be
2300 later turned into a global, like any other undefined
2301 symbol. */
2302 if (s->bsym->flags & BSF_WEAK)
2304 #ifdef obj_clear_weak_hook
2305 obj_clear_weak_hook (s);
2306 #endif
2307 s->bsym->flags &= ~BSF_WEAK;
2308 s->bsym->flags |= BSF_LOCAL;
2313 void
2314 S_SET_THREAD_LOCAL (symbolS *s)
2316 if (LOCAL_SYMBOL_CHECK (s))
2317 s = local_symbol_convert ((struct local_symbol *) s);
2318 if (bfd_is_com_section (s->bsym->section)
2319 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2320 return;
2321 s->bsym->flags |= BSF_THREAD_LOCAL;
2322 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2323 as_bad (_("Accessing function `%s' as thread-local object"),
2324 S_GET_NAME (s));
2325 else if (! bfd_is_und_section (s->bsym->section)
2326 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2327 as_bad (_("Accessing `%s' as thread-local object"),
2328 S_GET_NAME (s));
2331 void
2332 S_SET_NAME (symbolS *s, const char *name)
2334 if (LOCAL_SYMBOL_CHECK (s))
2336 ((struct local_symbol *) s)->lsy_name = name;
2337 return;
2339 s->bsym->name = name;
2342 void
2343 S_SET_VOLATILE (symbolS *s)
2345 if (LOCAL_SYMBOL_CHECK (s))
2346 s = local_symbol_convert ((struct local_symbol *) s);
2347 s->sy_volatile = 1;
2350 void
2351 S_CLEAR_VOLATILE (symbolS *s)
2353 if (!LOCAL_SYMBOL_CHECK (s))
2354 s->sy_volatile = 0;
2357 void
2358 S_SET_FORWARD_REF (symbolS *s)
2360 if (LOCAL_SYMBOL_CHECK (s))
2361 s = local_symbol_convert ((struct local_symbol *) s);
2362 s->sy_forward_ref = 1;
2365 /* Return the previous symbol in a chain. */
2367 symbolS *
2368 symbol_previous (symbolS *s)
2370 if (LOCAL_SYMBOL_CHECK (s))
2371 abort ();
2372 return s->sy_previous;
2375 /* Return the next symbol in a chain. */
2377 symbolS *
2378 symbol_next (symbolS *s)
2380 if (LOCAL_SYMBOL_CHECK (s))
2381 abort ();
2382 return s->sy_next;
2385 /* Return a pointer to the value of a symbol as an expression. */
2387 expressionS *
2388 symbol_get_value_expression (symbolS *s)
2390 if (LOCAL_SYMBOL_CHECK (s))
2391 s = local_symbol_convert ((struct local_symbol *) s);
2392 return &s->sy_value;
2395 /* Set the value of a symbol to an expression. */
2397 void
2398 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2400 if (LOCAL_SYMBOL_CHECK (s))
2401 s = local_symbol_convert ((struct local_symbol *) s);
2402 s->sy_value = *exp;
2403 S_CLEAR_WEAKREFR (s);
2406 /* Return whether 2 symbols are the same. */
2409 symbol_same_p (symbolS *s1, symbolS *s2)
2411 if (s1->bsym == NULL
2412 && local_symbol_converted_p ((struct local_symbol *) s1))
2413 s1 = local_symbol_get_real_symbol ((struct local_symbol *) s1);
2414 if (s2->bsym == NULL
2415 && local_symbol_converted_p ((struct local_symbol *) s2))
2416 s2 = local_symbol_get_real_symbol ((struct local_symbol *) s2);
2417 return s1 == s2;
2420 /* Return a pointer to the X_add_number component of a symbol. */
2422 offsetT *
2423 symbol_X_add_number (symbolS *s)
2425 if (LOCAL_SYMBOL_CHECK (s))
2426 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2428 return &s->sy_value.X_add_number;
2431 /* Set the value of SYM to the current position in the current segment. */
2433 void
2434 symbol_set_value_now (symbolS *sym)
2436 S_SET_SEGMENT (sym, now_seg);
2437 S_SET_VALUE (sym, frag_now_fix ());
2438 symbol_set_frag (sym, frag_now);
2441 /* Set the frag of a symbol. */
2443 void
2444 symbol_set_frag (symbolS *s, fragS *f)
2446 if (LOCAL_SYMBOL_CHECK (s))
2448 local_symbol_set_frag ((struct local_symbol *) s, f);
2449 return;
2451 s->sy_frag = f;
2452 S_CLEAR_WEAKREFR (s);
2455 /* Return the frag of a symbol. */
2457 fragS *
2458 symbol_get_frag (symbolS *s)
2460 if (LOCAL_SYMBOL_CHECK (s))
2461 return local_symbol_get_frag ((struct local_symbol *) s);
2462 return s->sy_frag;
2465 /* Mark a symbol as having been used. */
2467 void
2468 symbol_mark_used (symbolS *s)
2470 if (LOCAL_SYMBOL_CHECK (s))
2471 return;
2472 s->sy_used = 1;
2473 if (S_IS_WEAKREFR (s))
2474 symbol_mark_used (s->sy_value.X_add_symbol);
2477 /* Clear the mark of whether a symbol has been used. */
2479 void
2480 symbol_clear_used (symbolS *s)
2482 if (LOCAL_SYMBOL_CHECK (s))
2483 s = local_symbol_convert ((struct local_symbol *) s);
2484 s->sy_used = 0;
2487 /* Return whether a symbol has been used. */
2490 symbol_used_p (symbolS *s)
2492 if (LOCAL_SYMBOL_CHECK (s))
2493 return 1;
2494 return s->sy_used;
2497 /* Mark a symbol as having been used in a reloc. */
2499 void
2500 symbol_mark_used_in_reloc (symbolS *s)
2502 if (LOCAL_SYMBOL_CHECK (s))
2503 s = local_symbol_convert ((struct local_symbol *) s);
2504 s->sy_used_in_reloc = 1;
2507 /* Clear the mark of whether a symbol has been used in a reloc. */
2509 void
2510 symbol_clear_used_in_reloc (symbolS *s)
2512 if (LOCAL_SYMBOL_CHECK (s))
2513 return;
2514 s->sy_used_in_reloc = 0;
2517 /* Return whether a symbol has been used in a reloc. */
2520 symbol_used_in_reloc_p (symbolS *s)
2522 if (LOCAL_SYMBOL_CHECK (s))
2523 return 0;
2524 return s->sy_used_in_reloc;
2527 /* Mark a symbol as an MRI common symbol. */
2529 void
2530 symbol_mark_mri_common (symbolS *s)
2532 if (LOCAL_SYMBOL_CHECK (s))
2533 s = local_symbol_convert ((struct local_symbol *) s);
2534 s->sy_mri_common = 1;
2537 /* Clear the mark of whether a symbol is an MRI common symbol. */
2539 void
2540 symbol_clear_mri_common (symbolS *s)
2542 if (LOCAL_SYMBOL_CHECK (s))
2543 return;
2544 s->sy_mri_common = 0;
2547 /* Return whether a symbol is an MRI common symbol. */
2550 symbol_mri_common_p (symbolS *s)
2552 if (LOCAL_SYMBOL_CHECK (s))
2553 return 0;
2554 return s->sy_mri_common;
2557 /* Mark a symbol as having been written. */
2559 void
2560 symbol_mark_written (symbolS *s)
2562 if (LOCAL_SYMBOL_CHECK (s))
2563 return;
2564 s->written = 1;
2567 /* Clear the mark of whether a symbol has been written. */
2569 void
2570 symbol_clear_written (symbolS *s)
2572 if (LOCAL_SYMBOL_CHECK (s))
2573 return;
2574 s->written = 0;
2577 /* Return whether a symbol has been written. */
2580 symbol_written_p (symbolS *s)
2582 if (LOCAL_SYMBOL_CHECK (s))
2583 return 0;
2584 return s->written;
2587 /* Mark a symbol has having been resolved. */
2589 void
2590 symbol_mark_resolved (symbolS *s)
2592 if (LOCAL_SYMBOL_CHECK (s))
2594 local_symbol_mark_resolved ((struct local_symbol *) s);
2595 return;
2597 s->sy_resolved = 1;
2600 /* Return whether a symbol has been resolved. */
2603 symbol_resolved_p (symbolS *s)
2605 if (LOCAL_SYMBOL_CHECK (s))
2606 return local_symbol_resolved_p ((struct local_symbol *) s);
2607 return s->sy_resolved;
2610 /* Return whether a symbol is a section symbol. */
2613 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2615 if (LOCAL_SYMBOL_CHECK (s))
2616 return 0;
2617 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2620 /* Return whether a symbol is equated to another symbol. */
2623 symbol_equated_p (symbolS *s)
2625 if (LOCAL_SYMBOL_CHECK (s))
2626 return 0;
2627 return s->sy_value.X_op == O_symbol;
2630 /* Return whether a symbol is equated to another symbol, and should be
2631 treated specially when writing out relocs. */
2634 symbol_equated_reloc_p (symbolS *s)
2636 if (LOCAL_SYMBOL_CHECK (s))
2637 return 0;
2638 /* X_op_symbol, normally not used for O_symbol, is set by
2639 resolve_symbol_value to flag expression syms that have been
2640 equated. */
2641 return (s->sy_value.X_op == O_symbol
2642 #if defined (OBJ_COFF) && defined (TE_PE)
2643 && ! S_IS_WEAK (s)
2644 #endif
2645 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2646 || ! S_IS_DEFINED (s)
2647 || S_IS_COMMON (s)));
2650 /* Return whether a symbol has a constant value. */
2653 symbol_constant_p (symbolS *s)
2655 if (LOCAL_SYMBOL_CHECK (s))
2656 return 1;
2657 return s->sy_value.X_op == O_constant;
2660 /* Return whether a symbol was cloned and thus removed from the global
2661 symbol list. */
2664 symbol_shadow_p (symbolS *s)
2666 if (LOCAL_SYMBOL_CHECK (s))
2667 return 0;
2668 return s->sy_next == s;
2671 /* Return the BFD symbol for a symbol. */
2673 asymbol *
2674 symbol_get_bfdsym (symbolS *s)
2676 if (LOCAL_SYMBOL_CHECK (s))
2677 s = local_symbol_convert ((struct local_symbol *) s);
2678 return s->bsym;
2681 /* Set the BFD symbol for a symbol. */
2683 void
2684 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2686 if (LOCAL_SYMBOL_CHECK (s))
2687 s = local_symbol_convert ((struct local_symbol *) s);
2688 /* Usually, it is harmless to reset a symbol to a BFD section
2689 symbol. For example, obj_elf_change_section sets the BFD symbol
2690 of an old symbol with the newly created section symbol. But when
2691 we have multiple sections with the same name, the newly created
2692 section may have the same name as an old section. We check if the
2693 old symbol has been already marked as a section symbol before
2694 resetting it. */
2695 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2696 s->bsym = bsym;
2697 /* else XXX - What do we do now ? */
2700 #ifdef OBJ_SYMFIELD_TYPE
2702 /* Get a pointer to the object format information for a symbol. */
2704 OBJ_SYMFIELD_TYPE *
2705 symbol_get_obj (symbolS *s)
2707 if (LOCAL_SYMBOL_CHECK (s))
2708 s = local_symbol_convert ((struct local_symbol *) s);
2709 return &s->sy_obj;
2712 /* Set the object format information for a symbol. */
2714 void
2715 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2717 if (LOCAL_SYMBOL_CHECK (s))
2718 s = local_symbol_convert ((struct local_symbol *) s);
2719 s->sy_obj = *o;
2722 #endif /* OBJ_SYMFIELD_TYPE */
2724 #ifdef TC_SYMFIELD_TYPE
2726 /* Get a pointer to the processor information for a symbol. */
2728 TC_SYMFIELD_TYPE *
2729 symbol_get_tc (symbolS *s)
2731 if (LOCAL_SYMBOL_CHECK (s))
2732 s = local_symbol_convert ((struct local_symbol *) s);
2733 return &s->sy_tc;
2736 /* Set the processor information for a symbol. */
2738 void
2739 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2741 if (LOCAL_SYMBOL_CHECK (s))
2742 s = local_symbol_convert ((struct local_symbol *) s);
2743 s->sy_tc = *o;
2746 #endif /* TC_SYMFIELD_TYPE */
2748 void
2749 symbol_begin (void)
2751 symbol_lastP = NULL;
2752 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2753 sy_hash = hash_new ();
2754 local_hash = hash_new ();
2756 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2757 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2758 abs_symbol.bsym = bfd_abs_section.symbol;
2759 #endif
2760 abs_symbol.sy_value.X_op = O_constant;
2761 abs_symbol.sy_frag = &zero_address_frag;
2763 if (LOCAL_LABELS_FB)
2764 fb_label_init ();
2767 void
2768 dot_symbol_init (void)
2770 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
2771 if (dot_symbol.bsym == NULL)
2772 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2773 dot_symbol.bsym->name = ".";
2774 dot_symbol.sy_forward_ref = 1;
2775 dot_symbol.sy_value.X_op = O_constant;
2778 int indent_level;
2780 /* Maximum indent level.
2781 Available for modification inside a gdb session. */
2782 static int max_indent_level = 8;
2784 void
2785 print_symbol_value_1 (FILE *file, symbolS *sym)
2787 const char *name = S_GET_NAME (sym);
2788 if (!name || !name[0])
2789 name = "(unnamed)";
2790 fprintf (file, "sym ");
2791 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
2792 fprintf (file, " %s", name);
2794 if (LOCAL_SYMBOL_CHECK (sym))
2796 struct local_symbol *locsym = (struct local_symbol *) sym;
2798 if (local_symbol_get_frag (locsym) != & zero_address_frag
2799 && local_symbol_get_frag (locsym) != NULL)
2801 fprintf (file, " frag ");
2802 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
2804 if (local_symbol_resolved_p (locsym))
2805 fprintf (file, " resolved");
2806 fprintf (file, " local");
2808 else
2810 if (sym->sy_frag != &zero_address_frag)
2812 fprintf (file, " frag ");
2813 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
2815 if (sym->written)
2816 fprintf (file, " written");
2817 if (sym->sy_resolved)
2818 fprintf (file, " resolved");
2819 else if (sym->sy_resolving)
2820 fprintf (file, " resolving");
2821 if (sym->sy_used_in_reloc)
2822 fprintf (file, " used-in-reloc");
2823 if (sym->sy_used)
2824 fprintf (file, " used");
2825 if (S_IS_LOCAL (sym))
2826 fprintf (file, " local");
2827 if (S_IS_EXTERNAL (sym))
2828 fprintf (file, " extern");
2829 if (S_IS_WEAK (sym))
2830 fprintf (file, " weak");
2831 if (S_IS_DEBUG (sym))
2832 fprintf (file, " debug");
2833 if (S_IS_DEFINED (sym))
2834 fprintf (file, " defined");
2836 if (S_IS_WEAKREFR (sym))
2837 fprintf (file, " weakrefr");
2838 if (S_IS_WEAKREFD (sym))
2839 fprintf (file, " weakrefd");
2840 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2841 if (symbol_resolved_p (sym))
2843 segT s = S_GET_SEGMENT (sym);
2845 if (s != undefined_section
2846 && s != expr_section)
2847 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
2849 else if (indent_level < max_indent_level
2850 && S_GET_SEGMENT (sym) != undefined_section)
2852 indent_level++;
2853 fprintf (file, "\n%*s<", indent_level * 4, "");
2854 if (LOCAL_SYMBOL_CHECK (sym))
2855 fprintf (file, "constant %lx",
2856 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
2857 else
2858 print_expr_1 (file, &sym->sy_value);
2859 fprintf (file, ">");
2860 indent_level--;
2862 fflush (file);
2865 void
2866 print_symbol_value (symbolS *sym)
2868 indent_level = 0;
2869 print_symbol_value_1 (stderr, sym);
2870 fprintf (stderr, "\n");
2873 static void
2874 print_binary (FILE *file, const char *name, expressionS *exp)
2876 indent_level++;
2877 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2878 print_symbol_value_1 (file, exp->X_add_symbol);
2879 fprintf (file, ">\n%*s<", indent_level * 4, "");
2880 print_symbol_value_1 (file, exp->X_op_symbol);
2881 fprintf (file, ">");
2882 indent_level--;
2885 void
2886 print_expr_1 (FILE *file, expressionS *exp)
2888 fprintf (file, "expr ");
2889 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
2890 fprintf (file, " ");
2891 switch (exp->X_op)
2893 case O_illegal:
2894 fprintf (file, "illegal");
2895 break;
2896 case O_absent:
2897 fprintf (file, "absent");
2898 break;
2899 case O_constant:
2900 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
2901 break;
2902 case O_symbol:
2903 indent_level++;
2904 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2905 print_symbol_value_1 (file, exp->X_add_symbol);
2906 fprintf (file, ">");
2907 maybe_print_addnum:
2908 if (exp->X_add_number)
2909 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2910 (unsigned long) exp->X_add_number);
2911 indent_level--;
2912 break;
2913 case O_register:
2914 fprintf (file, "register #%d", (int) exp->X_add_number);
2915 break;
2916 case O_big:
2917 fprintf (file, "big");
2918 break;
2919 case O_uminus:
2920 fprintf (file, "uminus -<");
2921 indent_level++;
2922 print_symbol_value_1 (file, exp->X_add_symbol);
2923 fprintf (file, ">");
2924 goto maybe_print_addnum;
2925 case O_bit_not:
2926 fprintf (file, "bit_not");
2927 break;
2928 case O_multiply:
2929 print_binary (file, "multiply", exp);
2930 break;
2931 case O_divide:
2932 print_binary (file, "divide", exp);
2933 break;
2934 case O_modulus:
2935 print_binary (file, "modulus", exp);
2936 break;
2937 case O_left_shift:
2938 print_binary (file, "lshift", exp);
2939 break;
2940 case O_right_shift:
2941 print_binary (file, "rshift", exp);
2942 break;
2943 case O_bit_inclusive_or:
2944 print_binary (file, "bit_ior", exp);
2945 break;
2946 case O_bit_exclusive_or:
2947 print_binary (file, "bit_xor", exp);
2948 break;
2949 case O_bit_and:
2950 print_binary (file, "bit_and", exp);
2951 break;
2952 case O_eq:
2953 print_binary (file, "eq", exp);
2954 break;
2955 case O_ne:
2956 print_binary (file, "ne", exp);
2957 break;
2958 case O_lt:
2959 print_binary (file, "lt", exp);
2960 break;
2961 case O_le:
2962 print_binary (file, "le", exp);
2963 break;
2964 case O_ge:
2965 print_binary (file, "ge", exp);
2966 break;
2967 case O_gt:
2968 print_binary (file, "gt", exp);
2969 break;
2970 case O_logical_and:
2971 print_binary (file, "logical_and", exp);
2972 break;
2973 case O_logical_or:
2974 print_binary (file, "logical_or", exp);
2975 break;
2976 case O_add:
2977 indent_level++;
2978 fprintf (file, "add\n%*s<", indent_level * 4, "");
2979 print_symbol_value_1 (file, exp->X_add_symbol);
2980 fprintf (file, ">\n%*s<", indent_level * 4, "");
2981 print_symbol_value_1 (file, exp->X_op_symbol);
2982 fprintf (file, ">");
2983 goto maybe_print_addnum;
2984 case O_subtract:
2985 indent_level++;
2986 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2987 print_symbol_value_1 (file, exp->X_add_symbol);
2988 fprintf (file, ">\n%*s<", indent_level * 4, "");
2989 print_symbol_value_1 (file, exp->X_op_symbol);
2990 fprintf (file, ">");
2991 goto maybe_print_addnum;
2992 default:
2993 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2994 break;
2996 fflush (stdout);
2999 void
3000 print_expr (expressionS *exp)
3002 print_expr_1 (stderr, exp);
3003 fprintf (stderr, "\n");
3006 void
3007 symbol_print_statistics (FILE *file)
3009 hash_print_statistics (file, "symbol table", sy_hash);
3010 hash_print_statistics (file, "mini local symbol table", local_hash);
3011 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3012 local_symbol_count, local_symbol_conversion_count);
3015 #ifdef OBJ_COMPLEX_RELC
3017 /* Convert given symbol to a new complex-relocation symbol name. This
3018 may be a recursive function, since it might be called for non-leaf
3019 nodes (plain symbols) in the expression tree. The caller owns the
3020 returning string, so should free it eventually. Errors are
3021 indicated via as_bad and a NULL return value. The given symbol
3022 is marked with sy_used_in_reloc. */
3024 char *
3025 symbol_relc_make_sym (symbolS * sym)
3027 char * terminal = NULL;
3028 const char * sname;
3029 char typetag;
3030 int sname_len;
3032 gas_assert (sym != NULL);
3034 /* Recurse to symbol_relc_make_expr if this symbol
3035 is defined as an expression or a plain value. */
3036 if ( S_GET_SEGMENT (sym) == expr_section
3037 || S_GET_SEGMENT (sym) == absolute_section)
3038 return symbol_relc_make_expr (& sym->sy_value);
3040 /* This may be a "fake symbol" L0\001, referring to ".".
3041 Write out a special null symbol to refer to this position. */
3042 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3043 return xstrdup (".");
3045 /* We hope this is a plain leaf symbol. Construct the encoding
3046 as {S,s}II...:CCCCCCC....
3047 where 'S'/'s' means section symbol / plain symbol
3048 III is decimal for the symbol name length
3049 CCC is the symbol name itself. */
3050 symbol_mark_used_in_reloc (sym);
3052 sname = S_GET_NAME (sym);
3053 sname_len = strlen (sname);
3054 typetag = symbol_section_p (sym) ? 'S' : 's';
3056 terminal = xmalloc (1 /* S or s */
3057 + 8 /* sname_len in decimal */
3058 + 1 /* _ spacer */
3059 + sname_len /* name itself */
3060 + 1 /* \0 */ );
3062 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3063 return terminal;
3066 /* Convert given value to a new complex-relocation symbol name. This
3067 is a non-recursive function, since it is be called for leaf nodes
3068 (plain values) in the expression tree. The caller owns the
3069 returning string, so should free() it eventually. No errors. */
3071 char *
3072 symbol_relc_make_value (offsetT val)
3074 char * terminal = xmalloc (28); /* Enough for long long. */
3076 terminal[0] = '#';
3077 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3078 return terminal;
3081 /* Convert given expression to a new complex-relocation symbol name.
3082 This is a recursive function, since it traverses the entire given
3083 expression tree. The caller owns the returning string, so should
3084 free() it eventually. Errors are indicated via as_bad() and a NULL
3085 return value. */
3087 char *
3088 symbol_relc_make_expr (expressionS * exp)
3090 char * opstr = NULL; /* Operator prefix string. */
3091 int arity = 0; /* Arity of this operator. */
3092 char * operands[3]; /* Up to three operands. */
3093 char * concat_string = NULL;
3095 operands[0] = operands[1] = operands[2] = NULL;
3097 gas_assert (exp != NULL);
3099 /* Match known operators -> fill in opstr, arity, operands[] and fall
3100 through to construct subexpression fragments; may instead return
3101 string directly for leaf nodes. */
3103 /* See expr.h for the meaning of all these enums. Many operators
3104 have an unnatural arity (X_add_number implicitly added). The
3105 conversion logic expands them to explicit "+" subexpressions. */
3107 switch (exp->X_op)
3109 default:
3110 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3111 break;
3113 /* Leaf nodes. */
3114 case O_constant:
3115 return symbol_relc_make_value (exp->X_add_number);
3117 case O_symbol:
3118 if (exp->X_add_number)
3120 arity = 2;
3121 opstr = "+";
3122 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3123 operands[1] = symbol_relc_make_value (exp->X_add_number);
3124 break;
3126 else
3127 return symbol_relc_make_sym (exp->X_add_symbol);
3129 /* Helper macros for nesting nodes. */
3131 #define HANDLE_XADD_OPT1(str_) \
3132 if (exp->X_add_number) \
3134 arity = 2; \
3135 opstr = "+:" str_; \
3136 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3137 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3138 break; \
3140 else \
3142 arity = 1; \
3143 opstr = str_; \
3144 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3146 break
3148 #define HANDLE_XADD_OPT2(str_) \
3149 if (exp->X_add_number) \
3151 arity = 3; \
3152 opstr = "+:" str_; \
3153 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3154 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3155 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3157 else \
3159 arity = 2; \
3160 opstr = str_; \
3161 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3162 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3164 break
3166 /* Nesting nodes. */
3168 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3169 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3170 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3171 case O_multiply: HANDLE_XADD_OPT2 ("*");
3172 case O_divide: HANDLE_XADD_OPT2 ("/");
3173 case O_modulus: HANDLE_XADD_OPT2 ("%");
3174 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3175 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3176 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3177 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3178 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3179 case O_add: HANDLE_XADD_OPT2 ("+");
3180 case O_subtract: HANDLE_XADD_OPT2 ("-");
3181 case O_eq: HANDLE_XADD_OPT2 ("==");
3182 case O_ne: HANDLE_XADD_OPT2 ("!=");
3183 case O_lt: HANDLE_XADD_OPT2 ("<");
3184 case O_le: HANDLE_XADD_OPT2 ("<=");
3185 case O_ge: HANDLE_XADD_OPT2 (">=");
3186 case O_gt: HANDLE_XADD_OPT2 (">");
3187 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3188 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3191 /* Validate & reject early. */
3192 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3193 opstr = NULL;
3194 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3195 opstr = NULL;
3196 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3197 opstr = NULL;
3199 if (opstr == NULL)
3200 concat_string = NULL;
3201 else
3203 /* Allocate new string; include inter-operand padding gaps etc. */
3204 concat_string = xmalloc (strlen (opstr)
3206 + (arity >= 1 ? (strlen (operands[0]) + 1 ) : 0)
3207 + (arity >= 2 ? (strlen (operands[1]) + 1 ) : 0)
3208 + (arity >= 3 ? (strlen (operands[2]) + 0 ) : 0)
3209 + 1);
3210 gas_assert (concat_string != NULL);
3212 /* Format the thing. */
3213 sprintf (concat_string,
3214 (arity == 0 ? "%s" :
3215 arity == 1 ? "%s:%s" :
3216 arity == 2 ? "%s:%s:%s" :
3217 /* arity == 3 */ "%s:%s:%s:%s"),
3218 opstr, operands[0], operands[1], operands[2]);
3221 /* Free operand strings (not opstr). */
3222 if (arity >= 1) xfree (operands[0]);
3223 if (arity >= 2) xfree (operands[1]);
3224 if (arity >= 3) xfree (operands[2]);
3226 return concat_string;
3229 #endif