bfd/
[binutils.git] / gas / symbols.c
blob4e4ad775b3b7bb3c8a93aeb10d524345e0a09818
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
671 symbolP = symbol_temp_new_now ();
674 symbolP->sy_value.X_add_symbol = add_symbol;
675 symbolP->sy_value.X_op_symbol = op_symbol;
678 return symbolP;
681 symbolS *
682 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
684 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
687 symbolS *
688 symbol_temp_new_now (void)
690 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
693 symbolS *
694 symbol_temp_make (void)
696 return symbol_make (FAKE_LABEL_NAME);
699 /* Implement symbol table lookup.
700 In: A symbol's name as a string: '\0' can't be part of a symbol name.
701 Out: NULL if the name was not in the symbol table, else the address
702 of a struct symbol associated with that name. */
704 symbolS *
705 symbol_find_exact (const char *name)
707 return symbol_find_exact_noref (name, 0);
710 symbolS *
711 symbol_find_exact_noref (const char *name, int noref)
713 struct local_symbol *locsym;
714 symbolS* sym;
716 locsym = (struct local_symbol *) hash_find (local_hash, name);
717 if (locsym != NULL)
718 return (symbolS *) locsym;
720 sym = ((symbolS *) hash_find (sy_hash, name));
722 /* Any references to the symbol, except for the reference in
723 .weakref, must clear this flag, such that the symbol does not
724 turn into a weak symbol. Note that we don't have to handle the
725 local_symbol case, since a weakrefd is always promoted out of the
726 local_symbol table when it is turned into a weak symbol. */
727 if (sym && ! noref)
728 S_CLEAR_WEAKREFD (sym);
730 return sym;
733 symbolS *
734 symbol_find (const char *name)
736 return symbol_find_noref (name, 0);
739 symbolS *
740 symbol_find_noref (const char *name, int noref)
742 #ifdef tc_canonicalize_symbol_name
744 char *copy;
745 size_t len = strlen (name) + 1;
747 copy = (char *) alloca (len);
748 memcpy (copy, name, len);
749 name = tc_canonicalize_symbol_name (copy);
751 #endif
753 if (! symbols_case_sensitive)
755 char *copy;
756 const char *orig;
757 unsigned char c;
759 orig = name;
760 name = copy = (char *) alloca (strlen (name) + 1);
762 while ((c = *orig++) != '\0')
764 *copy++ = TOUPPER (c);
766 *copy = '\0';
769 return symbol_find_exact_noref (name, noref);
772 /* Once upon a time, symbols were kept in a singly linked list. At
773 least coff needs to be able to rearrange them from time to time, for
774 which a doubly linked list is much more convenient. Loic did these
775 as macros which seemed dangerous to me so they're now functions.
776 xoxorich. */
778 /* Link symbol ADDME after symbol TARGET in the chain. */
780 void
781 symbol_append (symbolS *addme, symbolS *target,
782 symbolS **rootPP, symbolS **lastPP)
784 if (LOCAL_SYMBOL_CHECK (addme))
785 abort ();
786 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
787 abort ();
789 if (target == NULL)
791 know (*rootPP == NULL);
792 know (*lastPP == NULL);
793 addme->sy_next = NULL;
794 addme->sy_previous = NULL;
795 *rootPP = addme;
796 *lastPP = addme;
797 return;
798 } /* if the list is empty */
800 if (target->sy_next != NULL)
802 target->sy_next->sy_previous = addme;
804 else
806 know (*lastPP == target);
807 *lastPP = addme;
808 } /* if we have a next */
810 addme->sy_next = target->sy_next;
811 target->sy_next = addme;
812 addme->sy_previous = target;
814 debug_verify_symchain (symbol_rootP, symbol_lastP);
817 /* Set the chain pointers of SYMBOL to null. */
819 void
820 symbol_clear_list_pointers (symbolS *symbolP)
822 if (LOCAL_SYMBOL_CHECK (symbolP))
823 abort ();
824 symbolP->sy_next = NULL;
825 symbolP->sy_previous = NULL;
828 /* Remove SYMBOLP from the list. */
830 void
831 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
833 if (LOCAL_SYMBOL_CHECK (symbolP))
834 abort ();
836 if (symbolP == *rootPP)
838 *rootPP = symbolP->sy_next;
839 } /* if it was the root */
841 if (symbolP == *lastPP)
843 *lastPP = symbolP->sy_previous;
844 } /* if it was the tail */
846 if (symbolP->sy_next != NULL)
848 symbolP->sy_next->sy_previous = symbolP->sy_previous;
849 } /* if not last */
851 if (symbolP->sy_previous != NULL)
853 symbolP->sy_previous->sy_next = symbolP->sy_next;
854 } /* if not first */
856 debug_verify_symchain (*rootPP, *lastPP);
859 /* Link symbol ADDME before symbol TARGET in the chain. */
861 void
862 symbol_insert (symbolS *addme, symbolS *target,
863 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
865 if (LOCAL_SYMBOL_CHECK (addme))
866 abort ();
867 if (LOCAL_SYMBOL_CHECK (target))
868 abort ();
870 if (target->sy_previous != NULL)
872 target->sy_previous->sy_next = addme;
874 else
876 know (*rootPP == target);
877 *rootPP = addme;
878 } /* if not first */
880 addme->sy_previous = target->sy_previous;
881 target->sy_previous = addme;
882 addme->sy_next = target;
884 debug_verify_symchain (*rootPP, *lastPP);
887 void
888 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
890 symbolS *symbolP = rootP;
892 if (symbolP == NULL)
893 return;
895 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
897 gas_assert (symbolP->bsym != NULL);
898 gas_assert (symbolP->sy_next->sy_previous == symbolP);
901 gas_assert (lastP == symbolP);
904 #ifdef OBJ_COMPLEX_RELC
906 static int
907 use_complex_relocs_for (symbolS * symp)
909 switch (symp->sy_value.X_op)
911 case O_constant:
912 return 0;
914 case O_symbol:
915 case O_symbol_rva:
916 case O_uminus:
917 case O_bit_not:
918 case O_logical_not:
919 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
920 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
922 (S_IS_DEFINED (symp->sy_value.X_add_symbol)
923 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section))
924 return 0;
925 break;
927 case O_multiply:
928 case O_divide:
929 case O_modulus:
930 case O_left_shift:
931 case O_right_shift:
932 case O_bit_inclusive_or:
933 case O_bit_or_not:
934 case O_bit_exclusive_or:
935 case O_bit_and:
936 case O_add:
937 case O_subtract:
938 case O_eq:
939 case O_ne:
940 case O_lt:
941 case O_le:
942 case O_ge:
943 case O_gt:
944 case O_logical_and:
945 case O_logical_or:
947 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
948 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
950 (S_IS_COMMON (symp->sy_value.X_op_symbol)
951 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
953 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
954 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
955 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section
956 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
957 return 0;
958 break;
960 default:
961 break;
963 return 1;
965 #endif
967 static void
968 report_op_error (symbolS *symp, symbolS *left, symbolS *right)
970 char *file;
971 unsigned int line;
972 segT seg_left = S_GET_SEGMENT (left);
973 segT seg_right = right ? S_GET_SEGMENT (right) : 0;
975 if (expr_symbol_where (symp, &file, &line))
977 if (seg_left == undefined_section)
978 as_bad_where (file, line,
979 _("undefined symbol `%s' in operation"),
980 S_GET_NAME (left));
981 if (seg_right == undefined_section)
982 as_bad_where (file, line,
983 _("undefined symbol `%s' in operation"),
984 S_GET_NAME (right));
985 if (seg_left != undefined_section
986 && seg_right != undefined_section)
988 if (right)
989 as_bad_where (file, line,
990 _("invalid sections for operation on `%s' and `%s'"),
991 S_GET_NAME (left), S_GET_NAME (right));
992 else
993 as_bad_where (file, line,
994 _("invalid section for operation on `%s'"),
995 S_GET_NAME (left));
999 else
1001 if (seg_left == undefined_section)
1002 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
1003 S_GET_NAME (left), S_GET_NAME (symp));
1004 if (seg_right == undefined_section)
1005 as_bad (_("undefined symbol `%s' in operation setting `%s'"),
1006 S_GET_NAME (right), S_GET_NAME (symp));
1007 if (seg_left != undefined_section
1008 && seg_right != undefined_section)
1010 if (right)
1011 as_bad (_("invalid sections for operation on `%s' and `%s' setting `%s'"),
1012 S_GET_NAME (left), S_GET_NAME (right), S_GET_NAME (symp));
1013 else
1014 as_bad (_("invalid section for operation on `%s' setting `%s'"),
1015 S_GET_NAME (left), S_GET_NAME (symp));
1020 /* Resolve the value of a symbol. This is called during the final
1021 pass over the symbol table to resolve any symbols with complex
1022 values. */
1024 valueT
1025 resolve_symbol_value (symbolS *symp)
1027 int resolved;
1028 valueT final_val = 0;
1029 segT final_seg;
1031 if (LOCAL_SYMBOL_CHECK (symp))
1033 struct local_symbol *locsym = (struct local_symbol *) symp;
1035 final_val = locsym->lsy_value;
1036 if (local_symbol_resolved_p (locsym))
1037 return final_val;
1039 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1041 if (finalize_syms)
1043 locsym->lsy_value = final_val;
1044 local_symbol_mark_resolved (locsym);
1047 return final_val;
1050 if (symp->sy_resolved)
1052 if (symp->sy_value.X_op == O_constant)
1053 return (valueT) symp->sy_value.X_add_number;
1054 else
1055 return 0;
1058 resolved = 0;
1059 final_seg = S_GET_SEGMENT (symp);
1061 if (symp->sy_resolving)
1063 if (finalize_syms)
1064 as_bad (_("symbol definition loop encountered at `%s'"),
1065 S_GET_NAME (symp));
1066 final_val = 0;
1067 resolved = 1;
1069 #ifdef OBJ_COMPLEX_RELC
1070 else if (final_seg == expr_section
1071 && use_complex_relocs_for (symp))
1073 symbolS * relc_symbol = NULL;
1074 char * relc_symbol_name = NULL;
1076 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1078 /* For debugging, print out conversion input & output. */
1079 #ifdef DEBUG_SYMS
1080 print_expr (& symp->sy_value);
1081 if (relc_symbol_name)
1082 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1083 #endif
1085 if (relc_symbol_name != NULL)
1086 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1087 0, & zero_address_frag);
1089 if (relc_symbol == NULL)
1091 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1092 S_GET_NAME (symp));
1093 resolved = 0;
1095 else
1097 symbol_table_insert (relc_symbol);
1099 /* S_CLEAR_EXTERNAL (relc_symbol); */
1100 if (symp->bsym->flags & BSF_SRELC)
1101 relc_symbol->bsym->flags |= BSF_SRELC;
1102 else
1103 relc_symbol->bsym->flags |= BSF_RELC;
1104 /* symp->bsym->flags |= BSF_RELC; */
1105 copy_symbol_attributes (symp, relc_symbol);
1106 symp->sy_value.X_op = O_symbol;
1107 symp->sy_value.X_add_symbol = relc_symbol;
1108 symp->sy_value.X_add_number = 0;
1109 resolved = 1;
1112 final_seg = undefined_section;
1113 goto exit_dont_set_value;
1115 #endif
1116 else
1118 symbolS *add_symbol, *op_symbol;
1119 offsetT left, right;
1120 segT seg_left, seg_right;
1121 operatorT op;
1122 int move_seg_ok;
1124 symp->sy_resolving = 1;
1126 /* Help out with CSE. */
1127 add_symbol = symp->sy_value.X_add_symbol;
1128 op_symbol = symp->sy_value.X_op_symbol;
1129 final_val = symp->sy_value.X_add_number;
1130 op = symp->sy_value.X_op;
1132 switch (op)
1134 default:
1135 BAD_CASE (op);
1136 break;
1138 case O_absent:
1139 final_val = 0;
1140 /* Fall through. */
1142 case O_constant:
1143 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1144 if (final_seg == expr_section)
1145 final_seg = absolute_section;
1146 /* Fall through. */
1148 case O_register:
1149 resolved = 1;
1150 break;
1152 case O_symbol:
1153 case O_symbol_rva:
1154 left = resolve_symbol_value (add_symbol);
1155 seg_left = S_GET_SEGMENT (add_symbol);
1156 if (finalize_syms)
1157 symp->sy_value.X_op_symbol = NULL;
1159 do_symbol:
1160 if (S_IS_WEAKREFR (symp))
1162 gas_assert (final_val == 0);
1163 if (S_IS_WEAKREFR (add_symbol))
1165 gas_assert (add_symbol->sy_value.X_op == O_symbol
1166 && add_symbol->sy_value.X_add_number == 0);
1167 add_symbol = add_symbol->sy_value.X_add_symbol;
1168 gas_assert (! S_IS_WEAKREFR (add_symbol));
1169 symp->sy_value.X_add_symbol = add_symbol;
1173 if (symp->sy_mri_common)
1175 /* This is a symbol inside an MRI common section. The
1176 relocation routines are going to handle it specially.
1177 Don't change the value. */
1178 resolved = symbol_resolved_p (add_symbol);
1179 break;
1182 if (finalize_syms && final_val == 0)
1184 if (LOCAL_SYMBOL_CHECK (add_symbol))
1185 add_symbol = local_symbol_convert ((struct local_symbol *)
1186 add_symbol);
1187 copy_symbol_attributes (symp, add_symbol);
1190 /* If we have equated this symbol to an undefined or common
1191 symbol, keep X_op set to O_symbol, and don't change
1192 X_add_number. This permits the routine which writes out
1193 relocation to detect this case, and convert the
1194 relocation to be against the symbol to which this symbol
1195 is equated. */
1196 if (! S_IS_DEFINED (add_symbol)
1197 #if defined (OBJ_COFF) && defined (TE_PE)
1198 || S_IS_WEAK (add_symbol)
1199 #endif
1200 || S_IS_COMMON (add_symbol))
1202 if (finalize_syms)
1204 symp->sy_value.X_op = O_symbol;
1205 symp->sy_value.X_add_symbol = add_symbol;
1206 symp->sy_value.X_add_number = final_val;
1207 /* Use X_op_symbol as a flag. */
1208 symp->sy_value.X_op_symbol = add_symbol;
1209 final_seg = seg_left;
1211 final_val = 0;
1212 resolved = symbol_resolved_p (add_symbol);
1213 symp->sy_resolving = 0;
1214 goto exit_dont_set_value;
1216 else if (finalize_syms
1217 && ((final_seg == expr_section && seg_left != expr_section)
1218 || symbol_shadow_p (symp)))
1220 /* If the symbol is an expression symbol, do similarly
1221 as for undefined and common syms above. Handles
1222 "sym +/- expr" where "expr" cannot be evaluated
1223 immediately, and we want relocations to be against
1224 "sym", eg. because it is weak. */
1225 symp->sy_value.X_op = O_symbol;
1226 symp->sy_value.X_add_symbol = add_symbol;
1227 symp->sy_value.X_add_number = final_val;
1228 symp->sy_value.X_op_symbol = add_symbol;
1229 final_seg = seg_left;
1230 final_val += symp->sy_frag->fr_address + left;
1231 resolved = symbol_resolved_p (add_symbol);
1232 symp->sy_resolving = 0;
1233 goto exit_dont_set_value;
1235 else
1237 final_val += symp->sy_frag->fr_address + left;
1238 if (final_seg == expr_section || final_seg == undefined_section)
1239 final_seg = seg_left;
1242 resolved = symbol_resolved_p (add_symbol);
1243 if (S_IS_WEAKREFR (symp))
1244 goto exit_dont_set_value;
1245 break;
1247 case O_uminus:
1248 case O_bit_not:
1249 case O_logical_not:
1250 left = resolve_symbol_value (add_symbol);
1251 seg_left = S_GET_SEGMENT (add_symbol);
1253 /* By reducing these to the relevant dyadic operator, we get
1254 !S -> S == 0 permitted on anything,
1255 -S -> 0 - S only permitted on absolute
1256 ~S -> S ^ ~0 only permitted on absolute */
1257 if (op != O_logical_not && seg_left != absolute_section
1258 && finalize_syms)
1259 report_op_error (symp, add_symbol, NULL);
1261 if (final_seg == expr_section || final_seg == undefined_section)
1262 final_seg = absolute_section;
1264 if (op == O_uminus)
1265 left = -left;
1266 else if (op == O_logical_not)
1267 left = !left;
1268 else
1269 left = ~left;
1271 final_val += left + symp->sy_frag->fr_address;
1273 resolved = symbol_resolved_p (add_symbol);
1274 break;
1276 case O_multiply:
1277 case O_divide:
1278 case O_modulus:
1279 case O_left_shift:
1280 case O_right_shift:
1281 case O_bit_inclusive_or:
1282 case O_bit_or_not:
1283 case O_bit_exclusive_or:
1284 case O_bit_and:
1285 case O_add:
1286 case O_subtract:
1287 case O_eq:
1288 case O_ne:
1289 case O_lt:
1290 case O_le:
1291 case O_ge:
1292 case O_gt:
1293 case O_logical_and:
1294 case O_logical_or:
1295 left = resolve_symbol_value (add_symbol);
1296 right = resolve_symbol_value (op_symbol);
1297 seg_left = S_GET_SEGMENT (add_symbol);
1298 seg_right = S_GET_SEGMENT (op_symbol);
1300 /* Simplify addition or subtraction of a constant by folding the
1301 constant into X_add_number. */
1302 if (op == O_add)
1304 if (seg_right == absolute_section)
1306 final_val += right;
1307 goto do_symbol;
1309 else if (seg_left == absolute_section)
1311 final_val += left;
1312 add_symbol = op_symbol;
1313 left = right;
1314 seg_left = seg_right;
1315 goto do_symbol;
1318 else if (op == O_subtract)
1320 if (seg_right == absolute_section)
1322 final_val -= right;
1323 goto do_symbol;
1327 move_seg_ok = 1;
1328 /* Equality and non-equality tests are permitted on anything.
1329 Subtraction, and other comparison operators are permitted if
1330 both operands are in the same section. Otherwise, both
1331 operands must be absolute. We already handled the case of
1332 addition or subtraction of a constant above. This will
1333 probably need to be changed for an object file format which
1334 supports arbitrary expressions, such as IEEE-695. */
1335 if (!(seg_left == absolute_section
1336 && seg_right == absolute_section)
1337 && !(op == O_eq || op == O_ne)
1338 && !((op == O_subtract
1339 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1340 && seg_left == seg_right
1341 && (seg_left != undefined_section
1342 || add_symbol == op_symbol)))
1344 /* Don't emit messages unless we're finalizing the symbol value,
1345 otherwise we may get the same message multiple times. */
1346 if (finalize_syms)
1347 report_op_error (symp, add_symbol, op_symbol);
1348 /* However do not move the symbol into the absolute section
1349 if it cannot currently be resolved - this would confuse
1350 other parts of the assembler into believing that the
1351 expression had been evaluated to zero. */
1352 else
1353 move_seg_ok = 0;
1356 if (move_seg_ok
1357 && (final_seg == expr_section || final_seg == undefined_section))
1358 final_seg = absolute_section;
1360 /* Check for division by zero. */
1361 if ((op == O_divide || op == O_modulus) && right == 0)
1363 /* If seg_right is not absolute_section, then we've
1364 already issued a warning about using a bad symbol. */
1365 if (seg_right == absolute_section && finalize_syms)
1367 char *file;
1368 unsigned int line;
1370 if (expr_symbol_where (symp, &file, &line))
1371 as_bad_where (file, line, _("division by zero"));
1372 else
1373 as_bad (_("division by zero when setting `%s'"),
1374 S_GET_NAME (symp));
1377 right = 1;
1380 switch (symp->sy_value.X_op)
1382 case O_multiply: left *= right; break;
1383 case O_divide: left /= right; break;
1384 case O_modulus: left %= right; break;
1385 case O_left_shift: left <<= right; break;
1386 case O_right_shift: left >>= right; break;
1387 case O_bit_inclusive_or: left |= right; break;
1388 case O_bit_or_not: left |= ~right; break;
1389 case O_bit_exclusive_or: left ^= right; break;
1390 case O_bit_and: left &= right; break;
1391 case O_add: left += right; break;
1392 case O_subtract: left -= right; break;
1393 case O_eq:
1394 case O_ne:
1395 left = (left == right && seg_left == seg_right
1396 && (seg_left != undefined_section
1397 || add_symbol == op_symbol)
1398 ? ~ (offsetT) 0 : 0);
1399 if (symp->sy_value.X_op == O_ne)
1400 left = ~left;
1401 break;
1402 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1403 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1404 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1405 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1406 case O_logical_and: left = left && right; break;
1407 case O_logical_or: left = left || right; break;
1408 default: abort ();
1411 final_val += symp->sy_frag->fr_address + left;
1412 if (final_seg == expr_section || final_seg == undefined_section)
1414 if (seg_left == undefined_section
1415 || seg_right == undefined_section)
1416 final_seg = undefined_section;
1417 else if (seg_left == absolute_section)
1418 final_seg = seg_right;
1419 else
1420 final_seg = seg_left;
1422 resolved = (symbol_resolved_p (add_symbol)
1423 && symbol_resolved_p (op_symbol));
1424 break;
1426 case O_big:
1427 case O_illegal:
1428 /* Give an error (below) if not in expr_section. We don't
1429 want to worry about expr_section symbols, because they
1430 are fictional (they are created as part of expression
1431 resolution), and any problems may not actually mean
1432 anything. */
1433 break;
1436 symp->sy_resolving = 0;
1439 if (finalize_syms)
1440 S_SET_VALUE (symp, final_val);
1442 exit_dont_set_value:
1443 /* Always set the segment, even if not finalizing the value.
1444 The segment is used to determine whether a symbol is defined. */
1445 S_SET_SEGMENT (symp, final_seg);
1447 /* Don't worry if we can't resolve an expr_section symbol. */
1448 if (finalize_syms)
1450 if (resolved)
1451 symp->sy_resolved = 1;
1452 else if (S_GET_SEGMENT (symp) != expr_section)
1454 as_bad (_("can't resolve value for symbol `%s'"),
1455 S_GET_NAME (symp));
1456 symp->sy_resolved = 1;
1460 return final_val;
1463 static void resolve_local_symbol (const char *, void *);
1465 /* A static function passed to hash_traverse. */
1467 static void
1468 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1470 if (value != NULL)
1471 resolve_symbol_value ((symbolS *) value);
1474 /* Resolve all local symbols. */
1476 void
1477 resolve_local_symbol_values (void)
1479 hash_traverse (local_hash, resolve_local_symbol);
1482 /* Obtain the current value of a symbol without changing any
1483 sub-expressions used. */
1486 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1488 symbolS *symbolP = *symbolPP;
1490 if (LOCAL_SYMBOL_CHECK (symbolP))
1492 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1494 *valueP = locsym->lsy_value;
1495 *segP = locsym->lsy_section;
1496 *fragPP = local_symbol_get_frag (locsym);
1498 else
1500 expressionS exp = symbolP->sy_value;
1502 if (!symbolP->sy_resolved && exp.X_op != O_illegal)
1504 int resolved;
1506 if (symbolP->sy_resolving)
1507 return 0;
1508 symbolP->sy_resolving = 1;
1509 resolved = resolve_expression (&exp);
1510 symbolP->sy_resolving = 0;
1511 if (!resolved)
1512 return 0;
1514 switch (exp.X_op)
1516 case O_constant:
1517 case O_register:
1518 if (!symbol_equated_p (symbolP))
1519 break;
1520 /* Fall thru. */
1521 case O_symbol:
1522 case O_symbol_rva:
1523 symbolP = exp.X_add_symbol;
1524 break;
1525 default:
1526 return 0;
1530 *symbolPP = symbolP;
1531 *valueP = exp.X_add_number;
1532 *segP = symbolP->bsym->section;
1533 *fragPP = symbolP->sy_frag;
1535 if (*segP == expr_section)
1536 switch (exp.X_op)
1538 case O_constant: *segP = absolute_section; break;
1539 case O_register: *segP = reg_section; break;
1540 default: break;
1544 return 1;
1547 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1548 They are *really* local. That is, they go out of scope whenever we see a
1549 label that isn't local. Also, like fb labels, there can be multiple
1550 instances of a dollar label. Therefor, we name encode each instance with
1551 the instance number, keep a list of defined symbols separate from the real
1552 symbol table, and we treat these buggers as a sparse array. */
1554 static long *dollar_labels;
1555 static long *dollar_label_instances;
1556 static char *dollar_label_defines;
1557 static unsigned long dollar_label_count;
1558 static unsigned long dollar_label_max;
1561 dollar_label_defined (long label)
1563 long *i;
1565 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1567 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1568 if (*i == label)
1569 return dollar_label_defines[i - dollar_labels];
1571 /* If we get here, label isn't defined. */
1572 return 0;
1575 static long
1576 dollar_label_instance (long label)
1578 long *i;
1580 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1582 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1583 if (*i == label)
1584 return (dollar_label_instances[i - dollar_labels]);
1586 /* If we get here, we haven't seen the label before.
1587 Therefore its instance count is zero. */
1588 return 0;
1591 void
1592 dollar_label_clear (void)
1594 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1597 #define DOLLAR_LABEL_BUMP_BY 10
1599 void
1600 define_dollar_label (long label)
1602 long *i;
1604 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1605 if (*i == label)
1607 ++dollar_label_instances[i - dollar_labels];
1608 dollar_label_defines[i - dollar_labels] = 1;
1609 return;
1612 /* If we get to here, we don't have label listed yet. */
1614 if (dollar_labels == NULL)
1616 dollar_labels = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1617 dollar_label_instances = (long *) xmalloc (DOLLAR_LABEL_BUMP_BY * sizeof (long));
1618 dollar_label_defines = (char *) xmalloc (DOLLAR_LABEL_BUMP_BY);
1619 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1620 dollar_label_count = 0;
1622 else if (dollar_label_count == dollar_label_max)
1624 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1625 dollar_labels = (long *) xrealloc ((char *) dollar_labels,
1626 dollar_label_max * sizeof (long));
1627 dollar_label_instances = (long *) xrealloc ((char *) dollar_label_instances,
1628 dollar_label_max * sizeof (long));
1629 dollar_label_defines = (char *) xrealloc (dollar_label_defines, dollar_label_max);
1630 } /* if we needed to grow */
1632 dollar_labels[dollar_label_count] = label;
1633 dollar_label_instances[dollar_label_count] = 1;
1634 dollar_label_defines[dollar_label_count] = 1;
1635 ++dollar_label_count;
1638 /* Caller must copy returned name: we re-use the area for the next name.
1640 The mth occurence of label n: is turned into the symbol "Ln^Am"
1641 where n is the label number and m is the instance number. "L" makes
1642 it a label discarded unless debugging and "^A"('\1') ensures no
1643 ordinary symbol SHOULD get the same name as a local label
1644 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1646 fb labels get the same treatment, except that ^B is used in place
1647 of ^A. */
1649 char * /* Return local label name. */
1650 dollar_label_name (register long n, /* we just saw "n$:" : n a number. */
1651 register int augend /* 0 for current instance, 1 for new instance. */)
1653 long i;
1654 /* Returned to caller, then copied. Used for created names ("4f"). */
1655 static char symbol_name_build[24];
1656 register char *p;
1657 register char *q;
1658 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1660 know (n >= 0);
1661 know (augend == 0 || augend == 1);
1662 p = symbol_name_build;
1663 #ifdef LOCAL_LABEL_PREFIX
1664 *p++ = LOCAL_LABEL_PREFIX;
1665 #endif
1666 *p++ = 'L';
1668 /* Next code just does sprintf( {}, "%d", n); */
1669 /* Label number. */
1670 q = symbol_name_temporary;
1671 for (*q++ = 0, i = n; i; ++q)
1673 *q = i % 10 + '0';
1674 i /= 10;
1676 while ((*p = *--q) != '\0')
1677 ++p;
1679 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1681 /* Instance number. */
1682 q = symbol_name_temporary;
1683 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1685 *q = i % 10 + '0';
1686 i /= 10;
1688 while ((*p++ = *--q) != '\0');;
1690 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1691 return symbol_name_build;
1694 /* Somebody else's idea of local labels. They are made by "n:" where n
1695 is any decimal digit. Refer to them with
1696 "nb" for previous (backward) n:
1697 or "nf" for next (forward) n:.
1699 We do a little better and let n be any number, not just a single digit, but
1700 since the other guy's assembler only does ten, we treat the first ten
1701 specially.
1703 Like someone else's assembler, we have one set of local label counters for
1704 entire assembly, not one set per (sub)segment like in most assemblers. This
1705 implies that one can refer to a label in another segment, and indeed some
1706 crufty compilers have done just that.
1708 Since there could be a LOT of these things, treat them as a sparse
1709 array. */
1711 #define FB_LABEL_SPECIAL (10)
1713 static long fb_low_counter[FB_LABEL_SPECIAL];
1714 static long *fb_labels;
1715 static long *fb_label_instances;
1716 static long fb_label_count;
1717 static long fb_label_max;
1719 /* This must be more than FB_LABEL_SPECIAL. */
1720 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1722 static void
1723 fb_label_init (void)
1725 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1728 /* Add one to the instance number of this fb label. */
1730 void
1731 fb_label_instance_inc (long label)
1733 long *i;
1735 if (label < FB_LABEL_SPECIAL)
1737 ++fb_low_counter[label];
1738 return;
1741 if (fb_labels != NULL)
1743 for (i = fb_labels + FB_LABEL_SPECIAL;
1744 i < fb_labels + fb_label_count; ++i)
1746 if (*i == label)
1748 ++fb_label_instances[i - fb_labels];
1749 return;
1750 } /* if we find it */
1751 } /* for each existing label */
1754 /* If we get to here, we don't have label listed yet. */
1756 if (fb_labels == NULL)
1758 fb_labels = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1759 fb_label_instances = (long *) xmalloc (FB_LABEL_BUMP_BY * sizeof (long));
1760 fb_label_max = FB_LABEL_BUMP_BY;
1761 fb_label_count = FB_LABEL_SPECIAL;
1764 else if (fb_label_count == fb_label_max)
1766 fb_label_max += FB_LABEL_BUMP_BY;
1767 fb_labels = (long *) xrealloc ((char *) fb_labels,
1768 fb_label_max * sizeof (long));
1769 fb_label_instances = (long *) xrealloc ((char *) fb_label_instances,
1770 fb_label_max * sizeof (long));
1771 } /* if we needed to grow */
1773 fb_labels[fb_label_count] = label;
1774 fb_label_instances[fb_label_count] = 1;
1775 ++fb_label_count;
1778 static long
1779 fb_label_instance (long label)
1781 long *i;
1783 if (label < FB_LABEL_SPECIAL)
1785 return (fb_low_counter[label]);
1788 if (fb_labels != NULL)
1790 for (i = fb_labels + FB_LABEL_SPECIAL;
1791 i < fb_labels + fb_label_count; ++i)
1793 if (*i == label)
1795 return (fb_label_instances[i - fb_labels]);
1796 } /* if we find it */
1797 } /* for each existing label */
1800 /* We didn't find the label, so this must be a reference to the
1801 first instance. */
1802 return 0;
1805 /* Caller must copy returned name: we re-use the area for the next name.
1807 The mth occurence of label n: is turned into the symbol "Ln^Bm"
1808 where n is the label number and m is the instance number. "L" makes
1809 it a label discarded unless debugging and "^B"('\2') ensures no
1810 ordinary symbol SHOULD get the same name as a local label
1811 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1813 dollar labels get the same treatment, except that ^A is used in
1814 place of ^B. */
1816 char * /* Return local label name. */
1817 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1818 long augend /* 0 for nb, 1 for n:, nf. */)
1820 long i;
1821 /* Returned to caller, then copied. Used for created names ("4f"). */
1822 static char symbol_name_build[24];
1823 register char *p;
1824 register char *q;
1825 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1827 know (n >= 0);
1828 #ifdef TC_MMIX
1829 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1830 #else
1831 know ((unsigned long) augend <= 1);
1832 #endif
1833 p = symbol_name_build;
1834 #ifdef LOCAL_LABEL_PREFIX
1835 *p++ = LOCAL_LABEL_PREFIX;
1836 #endif
1837 *p++ = 'L';
1839 /* Next code just does sprintf( {}, "%d", n); */
1840 /* Label number. */
1841 q = symbol_name_temporary;
1842 for (*q++ = 0, i = n; i; ++q)
1844 *q = i % 10 + '0';
1845 i /= 10;
1847 while ((*p = *--q) != '\0')
1848 ++p;
1850 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1852 /* Instance number. */
1853 q = symbol_name_temporary;
1854 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1856 *q = i % 10 + '0';
1857 i /= 10;
1859 while ((*p++ = *--q) != '\0');;
1861 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1862 return (symbol_name_build);
1865 /* Decode name that may have been generated by foo_label_name() above.
1866 If the name wasn't generated by foo_label_name(), then return it
1867 unaltered. This is used for error messages. */
1869 char *
1870 decode_local_label_name (char *s)
1872 char *p;
1873 char *symbol_decode;
1874 int label_number;
1875 int instance_number;
1876 char *type;
1877 const char *message_format;
1878 int lindex = 0;
1880 #ifdef LOCAL_LABEL_PREFIX
1881 if (s[lindex] == LOCAL_LABEL_PREFIX)
1882 ++lindex;
1883 #endif
1885 if (s[lindex] != 'L')
1886 return s;
1888 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
1889 label_number = (10 * label_number) + *p - '0';
1891 if (*p == DOLLAR_LABEL_CHAR)
1892 type = "dollar";
1893 else if (*p == LOCAL_LABEL_CHAR)
1894 type = "fb";
1895 else
1896 return s;
1898 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1899 instance_number = (10 * instance_number) + *p - '0';
1901 message_format = _("\"%d\" (instance number %d of a %s label)");
1902 symbol_decode = (char *) obstack_alloc (&notes, strlen (message_format) + 30);
1903 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1905 return symbol_decode;
1908 /* Get the value of a symbol. */
1910 valueT
1911 S_GET_VALUE (symbolS *s)
1913 if (LOCAL_SYMBOL_CHECK (s))
1914 return resolve_symbol_value (s);
1916 if (!s->sy_resolved)
1918 valueT val = resolve_symbol_value (s);
1919 if (!finalize_syms)
1920 return val;
1922 if (S_IS_WEAKREFR (s))
1923 return S_GET_VALUE (s->sy_value.X_add_symbol);
1925 if (s->sy_value.X_op != O_constant)
1927 if (! s->sy_resolved
1928 || s->sy_value.X_op != O_symbol
1929 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1930 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1931 S_GET_NAME (s));
1933 return (valueT) s->sy_value.X_add_number;
1936 /* Set the value of a symbol. */
1938 void
1939 S_SET_VALUE (symbolS *s, valueT val)
1941 if (LOCAL_SYMBOL_CHECK (s))
1943 ((struct local_symbol *) s)->lsy_value = val;
1944 return;
1947 s->sy_value.X_op = O_constant;
1948 s->sy_value.X_add_number = (offsetT) val;
1949 s->sy_value.X_unsigned = 0;
1950 S_CLEAR_WEAKREFR (s);
1953 void
1954 copy_symbol_attributes (symbolS *dest, symbolS *src)
1956 if (LOCAL_SYMBOL_CHECK (dest))
1957 dest = local_symbol_convert ((struct local_symbol *) dest);
1958 if (LOCAL_SYMBOL_CHECK (src))
1959 src = local_symbol_convert ((struct local_symbol *) src);
1961 /* In an expression, transfer the settings of these flags.
1962 The user can override later, of course. */
1963 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
1964 | BSF_GNU_INDIRECT_FUNCTION)
1965 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
1967 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
1968 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
1969 #endif
1971 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
1972 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
1973 #endif
1977 S_IS_FUNCTION (symbolS *s)
1979 flagword flags;
1981 if (LOCAL_SYMBOL_CHECK (s))
1982 return 0;
1984 flags = s->bsym->flags;
1986 return (flags & BSF_FUNCTION) != 0;
1990 S_IS_EXTERNAL (symbolS *s)
1992 flagword flags;
1994 if (LOCAL_SYMBOL_CHECK (s))
1995 return 0;
1997 flags = s->bsym->flags;
1999 /* Sanity check. */
2000 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2001 abort ();
2003 return (flags & BSF_GLOBAL) != 0;
2007 S_IS_WEAK (symbolS *s)
2009 if (LOCAL_SYMBOL_CHECK (s))
2010 return 0;
2011 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2012 could probably handle a WEAKREFR as always weak though. E.g., if
2013 the referenced symbol has lost its weak status, there's no reason
2014 to keep handling the weakrefr as if it was weak. */
2015 if (S_IS_WEAKREFR (s))
2016 return S_IS_WEAK (s->sy_value.X_add_symbol);
2017 return (s->bsym->flags & BSF_WEAK) != 0;
2021 S_IS_WEAKREFR (symbolS *s)
2023 if (LOCAL_SYMBOL_CHECK (s))
2024 return 0;
2025 return s->sy_weakrefr != 0;
2029 S_IS_WEAKREFD (symbolS *s)
2031 if (LOCAL_SYMBOL_CHECK (s))
2032 return 0;
2033 return s->sy_weakrefd != 0;
2037 S_IS_COMMON (symbolS *s)
2039 if (LOCAL_SYMBOL_CHECK (s))
2040 return 0;
2041 return bfd_is_com_section (s->bsym->section);
2045 S_IS_DEFINED (symbolS *s)
2047 if (LOCAL_SYMBOL_CHECK (s))
2048 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2049 return s->bsym->section != undefined_section;
2053 #ifndef EXTERN_FORCE_RELOC
2054 #define EXTERN_FORCE_RELOC IS_ELF
2055 #endif
2057 /* Return true for symbols that should not be reduced to section
2058 symbols or eliminated from expressions, because they may be
2059 overridden by the linker. */
2061 S_FORCE_RELOC (symbolS *s, int strict)
2063 if (LOCAL_SYMBOL_CHECK (s))
2064 return ((struct local_symbol *) s)->lsy_section == undefined_section;
2066 return ((strict
2067 && ((s->bsym->flags & BSF_WEAK) != 0
2068 || (EXTERN_FORCE_RELOC
2069 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2070 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0
2071 || s->bsym->section == undefined_section
2072 || bfd_is_com_section (s->bsym->section));
2076 S_IS_DEBUG (symbolS *s)
2078 if (LOCAL_SYMBOL_CHECK (s))
2079 return 0;
2080 if (s->bsym->flags & BSF_DEBUGGING)
2081 return 1;
2082 return 0;
2086 S_IS_LOCAL (symbolS *s)
2088 flagword flags;
2089 const char *name;
2091 if (LOCAL_SYMBOL_CHECK (s))
2092 return 1;
2094 flags = s->bsym->flags;
2096 /* Sanity check. */
2097 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2098 abort ();
2100 if (bfd_get_section (s->bsym) == reg_section)
2101 return 1;
2103 if (flag_strip_local_absolute
2104 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2105 the source file even when the object file is stripped. */
2106 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2107 && bfd_get_section (s->bsym) == absolute_section)
2108 return 1;
2110 name = S_GET_NAME (s);
2111 return (name != NULL
2112 && ! S_IS_DEBUG (s)
2113 && (strchr (name, DOLLAR_LABEL_CHAR)
2114 || strchr (name, LOCAL_LABEL_CHAR)
2115 || (! flag_keep_locals
2116 && (bfd_is_local_label (stdoutput, s->bsym)
2117 || (flag_mri
2118 && name[0] == '?'
2119 && name[1] == '?')))));
2123 S_IS_STABD (symbolS *s)
2125 return S_GET_NAME (s) == 0;
2129 S_IS_VOLATILE (const symbolS *s)
2131 if (LOCAL_SYMBOL_CHECK (s))
2132 return 0;
2133 return s->sy_volatile;
2137 S_IS_FORWARD_REF (const symbolS *s)
2139 if (LOCAL_SYMBOL_CHECK (s))
2140 return 0;
2141 return s->sy_forward_ref;
2144 const char *
2145 S_GET_NAME (symbolS *s)
2147 if (LOCAL_SYMBOL_CHECK (s))
2148 return ((struct local_symbol *) s)->lsy_name;
2149 return s->bsym->name;
2152 segT
2153 S_GET_SEGMENT (symbolS *s)
2155 if (LOCAL_SYMBOL_CHECK (s))
2156 return ((struct local_symbol *) s)->lsy_section;
2157 return s->bsym->section;
2160 void
2161 S_SET_SEGMENT (symbolS *s, segT seg)
2163 /* Don't reassign section symbols. The direct reason is to prevent seg
2164 faults assigning back to const global symbols such as *ABS*, but it
2165 shouldn't happen anyway. */
2167 if (LOCAL_SYMBOL_CHECK (s))
2169 if (seg == reg_section)
2170 s = local_symbol_convert ((struct local_symbol *) s);
2171 else
2173 ((struct local_symbol *) s)->lsy_section = seg;
2174 return;
2178 if (s->bsym->flags & BSF_SECTION_SYM)
2180 if (s->bsym->section != seg)
2181 abort ();
2183 else
2184 s->bsym->section = seg;
2187 void
2188 S_SET_EXTERNAL (symbolS *s)
2190 if (LOCAL_SYMBOL_CHECK (s))
2191 s = local_symbol_convert ((struct local_symbol *) s);
2192 if ((s->bsym->flags & BSF_WEAK) != 0)
2194 /* Let .weak override .global. */
2195 return;
2197 if (s->bsym->flags & BSF_SECTION_SYM)
2199 char * file;
2200 unsigned int line;
2202 /* Do not reassign section symbols. */
2203 as_where (& file, & line);
2204 as_warn_where (file, line,
2205 _("section symbols are already global"));
2206 return;
2208 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2209 if (S_GET_SEGMENT (s) == reg_section)
2211 as_bad ("can't make register symbol `%s' global",
2212 S_GET_NAME (s));
2213 return;
2215 #endif
2216 s->bsym->flags |= BSF_GLOBAL;
2217 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2219 #ifdef TE_PE
2220 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2221 an_external_name = S_GET_NAME (s);
2222 #endif
2225 void
2226 S_CLEAR_EXTERNAL (symbolS *s)
2228 if (LOCAL_SYMBOL_CHECK (s))
2229 return;
2230 if ((s->bsym->flags & BSF_WEAK) != 0)
2232 /* Let .weak override. */
2233 return;
2235 s->bsym->flags |= BSF_LOCAL;
2236 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2239 void
2240 S_SET_WEAK (symbolS *s)
2242 if (LOCAL_SYMBOL_CHECK (s))
2243 s = local_symbol_convert ((struct local_symbol *) s);
2244 #ifdef obj_set_weak_hook
2245 obj_set_weak_hook (s);
2246 #endif
2247 s->bsym->flags |= BSF_WEAK;
2248 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2251 void
2252 S_SET_WEAKREFR (symbolS *s)
2254 if (LOCAL_SYMBOL_CHECK (s))
2255 s = local_symbol_convert ((struct local_symbol *) s);
2256 s->sy_weakrefr = 1;
2257 /* If the alias was already used, make sure we mark the target as
2258 used as well, otherwise it might be dropped from the symbol
2259 table. This may have unintended side effects if the alias is
2260 later redirected to another symbol, such as keeping the unused
2261 previous target in the symbol table. Since it will be weak, it's
2262 not a big deal. */
2263 if (s->sy_used)
2264 symbol_mark_used (s->sy_value.X_add_symbol);
2267 void
2268 S_CLEAR_WEAKREFR (symbolS *s)
2270 if (LOCAL_SYMBOL_CHECK (s))
2271 return;
2272 s->sy_weakrefr = 0;
2275 void
2276 S_SET_WEAKREFD (symbolS *s)
2278 if (LOCAL_SYMBOL_CHECK (s))
2279 s = local_symbol_convert ((struct local_symbol *) s);
2280 s->sy_weakrefd = 1;
2281 S_SET_WEAK (s);
2284 void
2285 S_CLEAR_WEAKREFD (symbolS *s)
2287 if (LOCAL_SYMBOL_CHECK (s))
2288 return;
2289 if (s->sy_weakrefd)
2291 s->sy_weakrefd = 0;
2292 /* If a weakref target symbol is weak, then it was never
2293 referenced directly before, not even in a .global directive,
2294 so decay it to local. If it remains undefined, it will be
2295 later turned into a global, like any other undefined
2296 symbol. */
2297 if (s->bsym->flags & BSF_WEAK)
2299 #ifdef obj_clear_weak_hook
2300 obj_clear_weak_hook (s);
2301 #endif
2302 s->bsym->flags &= ~BSF_WEAK;
2303 s->bsym->flags |= BSF_LOCAL;
2308 void
2309 S_SET_THREAD_LOCAL (symbolS *s)
2311 if (LOCAL_SYMBOL_CHECK (s))
2312 s = local_symbol_convert ((struct local_symbol *) s);
2313 if (bfd_is_com_section (s->bsym->section)
2314 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2315 return;
2316 s->bsym->flags |= BSF_THREAD_LOCAL;
2317 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2318 as_bad (_("Accessing function `%s' as thread-local object"),
2319 S_GET_NAME (s));
2320 else if (! bfd_is_und_section (s->bsym->section)
2321 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2322 as_bad (_("Accessing `%s' as thread-local object"),
2323 S_GET_NAME (s));
2326 void
2327 S_SET_NAME (symbolS *s, const char *name)
2329 if (LOCAL_SYMBOL_CHECK (s))
2331 ((struct local_symbol *) s)->lsy_name = name;
2332 return;
2334 s->bsym->name = name;
2337 void
2338 S_SET_VOLATILE (symbolS *s)
2340 if (LOCAL_SYMBOL_CHECK (s))
2341 s = local_symbol_convert ((struct local_symbol *) s);
2342 s->sy_volatile = 1;
2345 void
2346 S_CLEAR_VOLATILE (symbolS *s)
2348 if (!LOCAL_SYMBOL_CHECK (s))
2349 s->sy_volatile = 0;
2352 void
2353 S_SET_FORWARD_REF (symbolS *s)
2355 if (LOCAL_SYMBOL_CHECK (s))
2356 s = local_symbol_convert ((struct local_symbol *) s);
2357 s->sy_forward_ref = 1;
2360 /* Return the previous symbol in a chain. */
2362 symbolS *
2363 symbol_previous (symbolS *s)
2365 if (LOCAL_SYMBOL_CHECK (s))
2366 abort ();
2367 return s->sy_previous;
2370 /* Return the next symbol in a chain. */
2372 symbolS *
2373 symbol_next (symbolS *s)
2375 if (LOCAL_SYMBOL_CHECK (s))
2376 abort ();
2377 return s->sy_next;
2380 /* Return a pointer to the value of a symbol as an expression. */
2382 expressionS *
2383 symbol_get_value_expression (symbolS *s)
2385 if (LOCAL_SYMBOL_CHECK (s))
2386 s = local_symbol_convert ((struct local_symbol *) s);
2387 return &s->sy_value;
2390 /* Set the value of a symbol to an expression. */
2392 void
2393 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2395 if (LOCAL_SYMBOL_CHECK (s))
2396 s = local_symbol_convert ((struct local_symbol *) s);
2397 s->sy_value = *exp;
2398 S_CLEAR_WEAKREFR (s);
2401 /* Return whether 2 symbols are the same. */
2404 symbol_same_p (symbolS *s1, symbolS *s2)
2406 if (s1->bsym == NULL
2407 && local_symbol_converted_p ((struct local_symbol *) s1))
2408 s1 = local_symbol_get_real_symbol ((struct local_symbol *) s1);
2409 if (s2->bsym == NULL
2410 && local_symbol_converted_p ((struct local_symbol *) s2))
2411 s2 = local_symbol_get_real_symbol ((struct local_symbol *) s2);
2412 return s1 == s2;
2415 /* Return a pointer to the X_add_number component of a symbol. */
2417 offsetT *
2418 symbol_X_add_number (symbolS *s)
2420 if (LOCAL_SYMBOL_CHECK (s))
2421 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2423 return &s->sy_value.X_add_number;
2426 /* Set the value of SYM to the current position in the current segment. */
2428 void
2429 symbol_set_value_now (symbolS *sym)
2431 S_SET_SEGMENT (sym, now_seg);
2432 S_SET_VALUE (sym, frag_now_fix ());
2433 symbol_set_frag (sym, frag_now);
2436 /* Set the frag of a symbol. */
2438 void
2439 symbol_set_frag (symbolS *s, fragS *f)
2441 if (LOCAL_SYMBOL_CHECK (s))
2443 local_symbol_set_frag ((struct local_symbol *) s, f);
2444 return;
2446 s->sy_frag = f;
2447 S_CLEAR_WEAKREFR (s);
2450 /* Return the frag of a symbol. */
2452 fragS *
2453 symbol_get_frag (symbolS *s)
2455 if (LOCAL_SYMBOL_CHECK (s))
2456 return local_symbol_get_frag ((struct local_symbol *) s);
2457 return s->sy_frag;
2460 /* Mark a symbol as having been used. */
2462 void
2463 symbol_mark_used (symbolS *s)
2465 if (LOCAL_SYMBOL_CHECK (s))
2466 return;
2467 s->sy_used = 1;
2468 if (S_IS_WEAKREFR (s))
2469 symbol_mark_used (s->sy_value.X_add_symbol);
2472 /* Clear the mark of whether a symbol has been used. */
2474 void
2475 symbol_clear_used (symbolS *s)
2477 if (LOCAL_SYMBOL_CHECK (s))
2478 s = local_symbol_convert ((struct local_symbol *) s);
2479 s->sy_used = 0;
2482 /* Return whether a symbol has been used. */
2485 symbol_used_p (symbolS *s)
2487 if (LOCAL_SYMBOL_CHECK (s))
2488 return 1;
2489 return s->sy_used;
2492 /* Mark a symbol as having been used in a reloc. */
2494 void
2495 symbol_mark_used_in_reloc (symbolS *s)
2497 if (LOCAL_SYMBOL_CHECK (s))
2498 s = local_symbol_convert ((struct local_symbol *) s);
2499 s->sy_used_in_reloc = 1;
2502 /* Clear the mark of whether a symbol has been used in a reloc. */
2504 void
2505 symbol_clear_used_in_reloc (symbolS *s)
2507 if (LOCAL_SYMBOL_CHECK (s))
2508 return;
2509 s->sy_used_in_reloc = 0;
2512 /* Return whether a symbol has been used in a reloc. */
2515 symbol_used_in_reloc_p (symbolS *s)
2517 if (LOCAL_SYMBOL_CHECK (s))
2518 return 0;
2519 return s->sy_used_in_reloc;
2522 /* Mark a symbol as an MRI common symbol. */
2524 void
2525 symbol_mark_mri_common (symbolS *s)
2527 if (LOCAL_SYMBOL_CHECK (s))
2528 s = local_symbol_convert ((struct local_symbol *) s);
2529 s->sy_mri_common = 1;
2532 /* Clear the mark of whether a symbol is an MRI common symbol. */
2534 void
2535 symbol_clear_mri_common (symbolS *s)
2537 if (LOCAL_SYMBOL_CHECK (s))
2538 return;
2539 s->sy_mri_common = 0;
2542 /* Return whether a symbol is an MRI common symbol. */
2545 symbol_mri_common_p (symbolS *s)
2547 if (LOCAL_SYMBOL_CHECK (s))
2548 return 0;
2549 return s->sy_mri_common;
2552 /* Mark a symbol as having been written. */
2554 void
2555 symbol_mark_written (symbolS *s)
2557 if (LOCAL_SYMBOL_CHECK (s))
2558 return;
2559 s->written = 1;
2562 /* Clear the mark of whether a symbol has been written. */
2564 void
2565 symbol_clear_written (symbolS *s)
2567 if (LOCAL_SYMBOL_CHECK (s))
2568 return;
2569 s->written = 0;
2572 /* Return whether a symbol has been written. */
2575 symbol_written_p (symbolS *s)
2577 if (LOCAL_SYMBOL_CHECK (s))
2578 return 0;
2579 return s->written;
2582 /* Mark a symbol has having been resolved. */
2584 void
2585 symbol_mark_resolved (symbolS *s)
2587 if (LOCAL_SYMBOL_CHECK (s))
2589 local_symbol_mark_resolved ((struct local_symbol *) s);
2590 return;
2592 s->sy_resolved = 1;
2595 /* Return whether a symbol has been resolved. */
2598 symbol_resolved_p (symbolS *s)
2600 if (LOCAL_SYMBOL_CHECK (s))
2601 return local_symbol_resolved_p ((struct local_symbol *) s);
2602 return s->sy_resolved;
2605 /* Return whether a symbol is a section symbol. */
2608 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2610 if (LOCAL_SYMBOL_CHECK (s))
2611 return 0;
2612 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2615 /* Return whether a symbol is equated to another symbol. */
2618 symbol_equated_p (symbolS *s)
2620 if (LOCAL_SYMBOL_CHECK (s))
2621 return 0;
2622 return s->sy_value.X_op == O_symbol;
2625 /* Return whether a symbol is equated to another symbol, and should be
2626 treated specially when writing out relocs. */
2629 symbol_equated_reloc_p (symbolS *s)
2631 if (LOCAL_SYMBOL_CHECK (s))
2632 return 0;
2633 /* X_op_symbol, normally not used for O_symbol, is set by
2634 resolve_symbol_value to flag expression syms that have been
2635 equated. */
2636 return (s->sy_value.X_op == O_symbol
2637 #if defined (OBJ_COFF) && defined (TE_PE)
2638 && ! S_IS_WEAK (s)
2639 #endif
2640 && ((s->sy_resolved && s->sy_value.X_op_symbol != NULL)
2641 || ! S_IS_DEFINED (s)
2642 || S_IS_COMMON (s)));
2645 /* Return whether a symbol has a constant value. */
2648 symbol_constant_p (symbolS *s)
2650 if (LOCAL_SYMBOL_CHECK (s))
2651 return 1;
2652 return s->sy_value.X_op == O_constant;
2655 /* Return whether a symbol was cloned and thus removed from the global
2656 symbol list. */
2659 symbol_shadow_p (symbolS *s)
2661 if (LOCAL_SYMBOL_CHECK (s))
2662 return 0;
2663 return s->sy_next == s;
2666 /* Return the BFD symbol for a symbol. */
2668 asymbol *
2669 symbol_get_bfdsym (symbolS *s)
2671 if (LOCAL_SYMBOL_CHECK (s))
2672 s = local_symbol_convert ((struct local_symbol *) s);
2673 return s->bsym;
2676 /* Set the BFD symbol for a symbol. */
2678 void
2679 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2681 if (LOCAL_SYMBOL_CHECK (s))
2682 s = local_symbol_convert ((struct local_symbol *) s);
2683 /* Usually, it is harmless to reset a symbol to a BFD section
2684 symbol. For example, obj_elf_change_section sets the BFD symbol
2685 of an old symbol with the newly created section symbol. But when
2686 we have multiple sections with the same name, the newly created
2687 section may have the same name as an old section. We check if the
2688 old symbol has been already marked as a section symbol before
2689 resetting it. */
2690 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2691 s->bsym = bsym;
2692 /* else XXX - What do we do now ? */
2695 #ifdef OBJ_SYMFIELD_TYPE
2697 /* Get a pointer to the object format information for a symbol. */
2699 OBJ_SYMFIELD_TYPE *
2700 symbol_get_obj (symbolS *s)
2702 if (LOCAL_SYMBOL_CHECK (s))
2703 s = local_symbol_convert ((struct local_symbol *) s);
2704 return &s->sy_obj;
2707 /* Set the object format information for a symbol. */
2709 void
2710 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2712 if (LOCAL_SYMBOL_CHECK (s))
2713 s = local_symbol_convert ((struct local_symbol *) s);
2714 s->sy_obj = *o;
2717 #endif /* OBJ_SYMFIELD_TYPE */
2719 #ifdef TC_SYMFIELD_TYPE
2721 /* Get a pointer to the processor information for a symbol. */
2723 TC_SYMFIELD_TYPE *
2724 symbol_get_tc (symbolS *s)
2726 if (LOCAL_SYMBOL_CHECK (s))
2727 s = local_symbol_convert ((struct local_symbol *) s);
2728 return &s->sy_tc;
2731 /* Set the processor information for a symbol. */
2733 void
2734 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2736 if (LOCAL_SYMBOL_CHECK (s))
2737 s = local_symbol_convert ((struct local_symbol *) s);
2738 s->sy_tc = *o;
2741 #endif /* TC_SYMFIELD_TYPE */
2743 void
2744 symbol_begin (void)
2746 symbol_lastP = NULL;
2747 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2748 sy_hash = hash_new ();
2749 local_hash = hash_new ();
2751 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2752 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2753 abs_symbol.bsym = bfd_abs_section.symbol;
2754 #endif
2755 abs_symbol.sy_value.X_op = O_constant;
2756 abs_symbol.sy_frag = &zero_address_frag;
2758 if (LOCAL_LABELS_FB)
2759 fb_label_init ();
2762 void
2763 dot_symbol_init (void)
2765 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
2766 if (dot_symbol.bsym == NULL)
2767 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2768 dot_symbol.bsym->name = ".";
2769 dot_symbol.sy_forward_ref = 1;
2770 dot_symbol.sy_value.X_op = O_constant;
2773 int indent_level;
2775 /* Maximum indent level.
2776 Available for modification inside a gdb session. */
2777 static int max_indent_level = 8;
2779 void
2780 print_symbol_value_1 (FILE *file, symbolS *sym)
2782 const char *name = S_GET_NAME (sym);
2783 if (!name || !name[0])
2784 name = "(unnamed)";
2785 fprintf (file, "sym ");
2786 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
2787 fprintf (file, " %s", name);
2789 if (LOCAL_SYMBOL_CHECK (sym))
2791 struct local_symbol *locsym = (struct local_symbol *) sym;
2793 if (local_symbol_get_frag (locsym) != & zero_address_frag
2794 && local_symbol_get_frag (locsym) != NULL)
2796 fprintf (file, " frag ");
2797 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
2799 if (local_symbol_resolved_p (locsym))
2800 fprintf (file, " resolved");
2801 fprintf (file, " local");
2803 else
2805 if (sym->sy_frag != &zero_address_frag)
2807 fprintf (file, " frag ");
2808 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
2810 if (sym->written)
2811 fprintf (file, " written");
2812 if (sym->sy_resolved)
2813 fprintf (file, " resolved");
2814 else if (sym->sy_resolving)
2815 fprintf (file, " resolving");
2816 if (sym->sy_used_in_reloc)
2817 fprintf (file, " used-in-reloc");
2818 if (sym->sy_used)
2819 fprintf (file, " used");
2820 if (S_IS_LOCAL (sym))
2821 fprintf (file, " local");
2822 if (S_IS_EXTERNAL (sym))
2823 fprintf (file, " extern");
2824 if (S_IS_WEAK (sym))
2825 fprintf (file, " weak");
2826 if (S_IS_DEBUG (sym))
2827 fprintf (file, " debug");
2828 if (S_IS_DEFINED (sym))
2829 fprintf (file, " defined");
2831 if (S_IS_WEAKREFR (sym))
2832 fprintf (file, " weakrefr");
2833 if (S_IS_WEAKREFD (sym))
2834 fprintf (file, " weakrefd");
2835 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2836 if (symbol_resolved_p (sym))
2838 segT s = S_GET_SEGMENT (sym);
2840 if (s != undefined_section
2841 && s != expr_section)
2842 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
2844 else if (indent_level < max_indent_level
2845 && S_GET_SEGMENT (sym) != undefined_section)
2847 indent_level++;
2848 fprintf (file, "\n%*s<", indent_level * 4, "");
2849 if (LOCAL_SYMBOL_CHECK (sym))
2850 fprintf (file, "constant %lx",
2851 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
2852 else
2853 print_expr_1 (file, &sym->sy_value);
2854 fprintf (file, ">");
2855 indent_level--;
2857 fflush (file);
2860 void
2861 print_symbol_value (symbolS *sym)
2863 indent_level = 0;
2864 print_symbol_value_1 (stderr, sym);
2865 fprintf (stderr, "\n");
2868 static void
2869 print_binary (FILE *file, const char *name, expressionS *exp)
2871 indent_level++;
2872 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2873 print_symbol_value_1 (file, exp->X_add_symbol);
2874 fprintf (file, ">\n%*s<", indent_level * 4, "");
2875 print_symbol_value_1 (file, exp->X_op_symbol);
2876 fprintf (file, ">");
2877 indent_level--;
2880 void
2881 print_expr_1 (FILE *file, expressionS *exp)
2883 fprintf (file, "expr ");
2884 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
2885 fprintf (file, " ");
2886 switch (exp->X_op)
2888 case O_illegal:
2889 fprintf (file, "illegal");
2890 break;
2891 case O_absent:
2892 fprintf (file, "absent");
2893 break;
2894 case O_constant:
2895 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
2896 break;
2897 case O_symbol:
2898 indent_level++;
2899 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2900 print_symbol_value_1 (file, exp->X_add_symbol);
2901 fprintf (file, ">");
2902 maybe_print_addnum:
2903 if (exp->X_add_number)
2904 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2905 (unsigned long) exp->X_add_number);
2906 indent_level--;
2907 break;
2908 case O_register:
2909 fprintf (file, "register #%d", (int) exp->X_add_number);
2910 break;
2911 case O_big:
2912 fprintf (file, "big");
2913 break;
2914 case O_uminus:
2915 fprintf (file, "uminus -<");
2916 indent_level++;
2917 print_symbol_value_1 (file, exp->X_add_symbol);
2918 fprintf (file, ">");
2919 goto maybe_print_addnum;
2920 case O_bit_not:
2921 fprintf (file, "bit_not");
2922 break;
2923 case O_multiply:
2924 print_binary (file, "multiply", exp);
2925 break;
2926 case O_divide:
2927 print_binary (file, "divide", exp);
2928 break;
2929 case O_modulus:
2930 print_binary (file, "modulus", exp);
2931 break;
2932 case O_left_shift:
2933 print_binary (file, "lshift", exp);
2934 break;
2935 case O_right_shift:
2936 print_binary (file, "rshift", exp);
2937 break;
2938 case O_bit_inclusive_or:
2939 print_binary (file, "bit_ior", exp);
2940 break;
2941 case O_bit_exclusive_or:
2942 print_binary (file, "bit_xor", exp);
2943 break;
2944 case O_bit_and:
2945 print_binary (file, "bit_and", exp);
2946 break;
2947 case O_eq:
2948 print_binary (file, "eq", exp);
2949 break;
2950 case O_ne:
2951 print_binary (file, "ne", exp);
2952 break;
2953 case O_lt:
2954 print_binary (file, "lt", exp);
2955 break;
2956 case O_le:
2957 print_binary (file, "le", exp);
2958 break;
2959 case O_ge:
2960 print_binary (file, "ge", exp);
2961 break;
2962 case O_gt:
2963 print_binary (file, "gt", exp);
2964 break;
2965 case O_logical_and:
2966 print_binary (file, "logical_and", exp);
2967 break;
2968 case O_logical_or:
2969 print_binary (file, "logical_or", exp);
2970 break;
2971 case O_add:
2972 indent_level++;
2973 fprintf (file, "add\n%*s<", indent_level * 4, "");
2974 print_symbol_value_1 (file, exp->X_add_symbol);
2975 fprintf (file, ">\n%*s<", indent_level * 4, "");
2976 print_symbol_value_1 (file, exp->X_op_symbol);
2977 fprintf (file, ">");
2978 goto maybe_print_addnum;
2979 case O_subtract:
2980 indent_level++;
2981 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
2982 print_symbol_value_1 (file, exp->X_add_symbol);
2983 fprintf (file, ">\n%*s<", indent_level * 4, "");
2984 print_symbol_value_1 (file, exp->X_op_symbol);
2985 fprintf (file, ">");
2986 goto maybe_print_addnum;
2987 default:
2988 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
2989 break;
2991 fflush (stdout);
2994 void
2995 print_expr (expressionS *exp)
2997 print_expr_1 (stderr, exp);
2998 fprintf (stderr, "\n");
3001 void
3002 symbol_print_statistics (FILE *file)
3004 hash_print_statistics (file, "symbol table", sy_hash);
3005 hash_print_statistics (file, "mini local symbol table", local_hash);
3006 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3007 local_symbol_count, local_symbol_conversion_count);
3010 #ifdef OBJ_COMPLEX_RELC
3012 /* Convert given symbol to a new complex-relocation symbol name. This
3013 may be a recursive function, since it might be called for non-leaf
3014 nodes (plain symbols) in the expression tree. The caller owns the
3015 returning string, so should free it eventually. Errors are
3016 indicated via as_bad and a NULL return value. The given symbol
3017 is marked with sy_used_in_reloc. */
3019 char *
3020 symbol_relc_make_sym (symbolS * sym)
3022 char * terminal = NULL;
3023 const char * sname;
3024 char typetag;
3025 int sname_len;
3027 gas_assert (sym != NULL);
3029 /* Recurse to symbol_relc_make_expr if this symbol
3030 is defined as an expression or a plain value. */
3031 if ( S_GET_SEGMENT (sym) == expr_section
3032 || S_GET_SEGMENT (sym) == absolute_section)
3033 return symbol_relc_make_expr (& sym->sy_value);
3035 /* This may be a "fake symbol" L0\001, referring to ".".
3036 Write out a special null symbol to refer to this position. */
3037 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3038 return xstrdup (".");
3040 /* We hope this is a plain leaf symbol. Construct the encoding
3041 as {S,s}II...:CCCCCCC....
3042 where 'S'/'s' means section symbol / plain symbol
3043 III is decimal for the symbol name length
3044 CCC is the symbol name itself. */
3045 symbol_mark_used_in_reloc (sym);
3047 sname = S_GET_NAME (sym);
3048 sname_len = strlen (sname);
3049 typetag = symbol_section_p (sym) ? 'S' : 's';
3051 terminal = xmalloc (1 /* S or s */
3052 + 8 /* sname_len in decimal */
3053 + 1 /* _ spacer */
3054 + sname_len /* name itself */
3055 + 1 /* \0 */ );
3057 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3058 return terminal;
3061 /* Convert given value to a new complex-relocation symbol name. This
3062 is a non-recursive function, since it is be called for leaf nodes
3063 (plain values) in the expression tree. The caller owns the
3064 returning string, so should free() it eventually. No errors. */
3066 char *
3067 symbol_relc_make_value (offsetT val)
3069 char * terminal = xmalloc (28); /* Enough for long long. */
3071 terminal[0] = '#';
3072 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3073 return terminal;
3076 /* Convert given expression to a new complex-relocation symbol name.
3077 This is a recursive function, since it traverses the entire given
3078 expression tree. The caller owns the returning string, so should
3079 free() it eventually. Errors are indicated via as_bad() and a NULL
3080 return value. */
3082 char *
3083 symbol_relc_make_expr (expressionS * exp)
3085 char * opstr = NULL; /* Operator prefix string. */
3086 int arity = 0; /* Arity of this operator. */
3087 char * operands[3]; /* Up to three operands. */
3088 char * concat_string = NULL;
3090 operands[0] = operands[1] = operands[2] = NULL;
3092 gas_assert (exp != NULL);
3094 /* Match known operators -> fill in opstr, arity, operands[] and fall
3095 through to construct subexpression fragments; may instead return
3096 string directly for leaf nodes. */
3098 /* See expr.h for the meaning of all these enums. Many operators
3099 have an unnatural arity (X_add_number implicitly added). The
3100 conversion logic expands them to explicit "+" subexpressions. */
3102 switch (exp->X_op)
3104 default:
3105 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3106 break;
3108 /* Leaf nodes. */
3109 case O_constant:
3110 return symbol_relc_make_value (exp->X_add_number);
3112 case O_symbol:
3113 if (exp->X_add_number)
3115 arity = 2;
3116 opstr = "+";
3117 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3118 operands[1] = symbol_relc_make_value (exp->X_add_number);
3119 break;
3121 else
3122 return symbol_relc_make_sym (exp->X_add_symbol);
3124 /* Helper macros for nesting nodes. */
3126 #define HANDLE_XADD_OPT1(str_) \
3127 if (exp->X_add_number) \
3129 arity = 2; \
3130 opstr = "+:" str_; \
3131 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3132 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3133 break; \
3135 else \
3137 arity = 1; \
3138 opstr = str_; \
3139 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3141 break
3143 #define HANDLE_XADD_OPT2(str_) \
3144 if (exp->X_add_number) \
3146 arity = 3; \
3147 opstr = "+:" str_; \
3148 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3149 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3150 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3152 else \
3154 arity = 2; \
3155 opstr = str_; \
3156 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3157 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3159 break
3161 /* Nesting nodes. */
3163 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3164 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3165 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3166 case O_multiply: HANDLE_XADD_OPT2 ("*");
3167 case O_divide: HANDLE_XADD_OPT2 ("/");
3168 case O_modulus: HANDLE_XADD_OPT2 ("%");
3169 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3170 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3171 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3172 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3173 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3174 case O_add: HANDLE_XADD_OPT2 ("+");
3175 case O_subtract: HANDLE_XADD_OPT2 ("-");
3176 case O_eq: HANDLE_XADD_OPT2 ("==");
3177 case O_ne: HANDLE_XADD_OPT2 ("!=");
3178 case O_lt: HANDLE_XADD_OPT2 ("<");
3179 case O_le: HANDLE_XADD_OPT2 ("<=");
3180 case O_ge: HANDLE_XADD_OPT2 (">=");
3181 case O_gt: HANDLE_XADD_OPT2 (">");
3182 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3183 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3186 /* Validate & reject early. */
3187 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3188 opstr = NULL;
3189 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3190 opstr = NULL;
3191 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3192 opstr = NULL;
3194 if (opstr == NULL)
3195 concat_string = NULL;
3196 else
3198 /* Allocate new string; include inter-operand padding gaps etc. */
3199 concat_string = xmalloc (strlen (opstr)
3201 + (arity >= 1 ? (strlen (operands[0]) + 1 ) : 0)
3202 + (arity >= 2 ? (strlen (operands[1]) + 1 ) : 0)
3203 + (arity >= 3 ? (strlen (operands[2]) + 0 ) : 0)
3204 + 1);
3205 gas_assert (concat_string != NULL);
3207 /* Format the thing. */
3208 sprintf (concat_string,
3209 (arity == 0 ? "%s" :
3210 arity == 1 ? "%s:%s" :
3211 arity == 2 ? "%s:%s:%s" :
3212 /* arity == 3 */ "%s:%s:%s:%s"),
3213 opstr, operands[0], operands[1], operands[2]);
3216 /* Free operand strings (not opstr). */
3217 if (arity >= 1) xfree (operands[0]);
3218 if (arity >= 2) xfree (operands[1]);
3219 if (arity >= 3) xfree (operands[2]);
3221 return concat_string;
3224 #endif