1 /* symbols.c -symbol table-
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GAS is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 /* #define DEBUG_SYMS / * to debug symbol list maintenance. */
24 #include "safe-ctype.h"
25 #include "obstack.h" /* For "symbols.h" */
27 #include "struc-symbol.h"
30 /* This is non-zero if symbols are case sensitive, which is the
32 int symbols_case_sensitive = 1;
34 #ifndef WORKING_DOT_WORD
35 extern int new_broken_words;
38 /* symbol-name => struct symbol pointer */
39 static struct hash_control *sy_hash;
41 /* Table of local symbols. */
42 static struct hash_control *local_hash;
44 /* Below are commented in "symbols.h". */
45 symbolS *symbol_rootP;
46 symbolS *symbol_lastP;
51 #define debug_verify_symchain verify_symbol_chain
53 #define debug_verify_symchain(root, last) ((void) 0)
56 #define DOLLAR_LABEL_CHAR '\001'
57 #define LOCAL_LABEL_CHAR '\002'
59 #ifndef TC_LABEL_IS_LOCAL
60 #define TC_LABEL_IS_LOCAL(name) 0
65 /* The name of an external symbol which is
66 used to make weak PE symbol names unique. */
67 const char * an_external_name;
70 static const char *save_symbol_name (const char *);
71 static void fb_label_init (void);
72 static long dollar_label_instance (long);
73 static long fb_label_instance (long);
75 static void print_binary (FILE *, const char *, expressionS *);
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
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. */
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)
97 symbol_append (symbolP, symbol_lastP, &symbol_rootP, &symbol_lastP);
102 /* Save a symbol name on a permanent obstack, and convert it according
103 to the object file format. */
106 save_symbol_name (const char *name)
111 name_length = strlen (name) + 1; /* +1 for \0. */
112 obstack_grow (¬es, name, name_length);
113 ret = (char *) obstack_finish (¬es);
115 #ifdef tc_canonicalize_symbol_name
116 ret = tc_canonicalize_symbol_name (ret);
119 if (! symbols_case_sensitive)
123 for (s = ret; *s != '\0'; s++)
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 const char *preserved_copy_of_name;
139 preserved_copy_of_name = save_symbol_name (name);
141 symbolP = (symbolS *) obstack_alloc (¬es, 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);
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->sy_flags.sy_local_symbol \
183 ? (local_symbol_converted_p ((struct local_symbol *) s) \
184 ? (s = local_symbol_get_real_symbol ((struct local_symbol *) s), \
189 /* Create a local symbol and insert it into the local hash table. */
191 struct local_symbol *
192 local_symbol_make (const char *name, segT section, valueT val, fragS *frag)
194 const 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 (¬es, sizeof *ret);
202 ret->lsy_flags.sy_local_symbol = 1;
203 ret->lsy_flags.sy_resolved = 0;
204 ret->lsy_name = name_copy;
205 ret->lsy_section = section;
206 local_symbol_set_frag (ret, frag);
207 ret->lsy_value = val;
209 hash_jam (local_hash, name_copy, (void *) ret);
214 /* Convert a local symbol into a real symbol. Note that we do not
215 reclaim the space used by the local symbol. */
218 local_symbol_convert (struct local_symbol *locsym)
222 gas_assert (locsym->lsy_flags.sy_local_symbol);
223 if (local_symbol_converted_p (locsym))
224 return local_symbol_get_real_symbol (locsym);
226 ++local_symbol_conversion_count;
228 ret = symbol_new (locsym->lsy_name, locsym->lsy_section, locsym->lsy_value,
229 local_symbol_get_frag (locsym));
231 if (local_symbol_resolved_p (locsym))
232 ret->sy_flags.sy_resolved = 1;
234 /* Local symbols are always either defined or used. */
235 ret->sy_flags.sy_used = 1;
237 #ifdef TC_LOCAL_SYMFIELD_CONVERT
238 TC_LOCAL_SYMFIELD_CONVERT (locsym, ret);
241 symbol_table_insert (ret);
243 local_symbol_mark_converted (locsym);
244 local_symbol_set_real_symbol (locsym, ret);
246 hash_jam (local_hash, locsym->lsy_name, NULL);
252 define_sym_at_dot (symbolS *symbolP)
254 symbolP->sy_frag = frag_now;
255 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
256 S_SET_SEGMENT (symbolP, now_seg);
259 /* We have just seen "<name>:".
260 Creates a struct symbol unless it already exists.
262 Gripes if we are redefining a symbol incompatibly (and ignores it). */
265 colon (/* Just seen "x:" - rattle symbols & frags. */
266 const char *sym_name /* Symbol name, as a canonical string. */
267 /* We copy this string: OK to alter later. */)
269 symbolS *symbolP; /* Symbol we are working with. */
271 /* Sun local labels go out of scope whenever a non-local symbol is
273 if (LOCAL_LABELS_DOLLAR
274 && !bfd_is_local_label_name (stdoutput, sym_name))
275 dollar_label_clear ();
277 #ifndef WORKING_DOT_WORD
278 if (new_broken_words)
280 struct broken_word *a;
285 if (now_seg == absolute_section)
287 as_bad (_("cannot define symbol `%s' in absolute section"), sym_name);
291 possible_bytes = (md_short_jump_size
292 + new_broken_words * md_long_jump_size);
295 frag_opcode = frag_var (rs_broken_word,
299 (symbolS *) broken_words,
303 /* We want to store the pointer to where to insert the jump
304 table in the fr_opcode of the rs_broken_word frag. This
305 requires a little hackery. */
307 && (frag_tmp->fr_type != rs_broken_word
308 || frag_tmp->fr_opcode))
309 frag_tmp = frag_tmp->fr_next;
311 frag_tmp->fr_opcode = frag_opcode;
312 new_broken_words = 0;
314 for (a = broken_words; a && a->dispfrag == 0; a = a->next_broken_word)
315 a->dispfrag = frag_tmp;
317 #endif /* WORKING_DOT_WORD */
319 #ifdef obj_frob_colon
320 obj_frob_colon (sym_name);
323 if ((symbolP = symbol_find (sym_name)) != 0)
325 S_CLEAR_WEAKREFR (symbolP);
326 #ifdef RESOLVE_SYMBOL_REDEFINITION
327 if (RESOLVE_SYMBOL_REDEFINITION (symbolP))
330 /* Now check for undefined symbols. */
331 if (LOCAL_SYMBOL_CHECK (symbolP))
333 struct local_symbol *locsym = (struct local_symbol *) symbolP;
335 if (locsym->lsy_section != undefined_section
336 && (local_symbol_get_frag (locsym) != frag_now
337 || locsym->lsy_section != now_seg
338 || locsym->lsy_value != frag_now_fix ()))
340 as_bad (_("symbol `%s' is already defined"), sym_name);
344 locsym->lsy_section = now_seg;
345 local_symbol_set_frag (locsym, frag_now);
346 locsym->lsy_value = frag_now_fix ();
348 else if (!(S_IS_DEFINED (symbolP) || symbol_equated_p (symbolP))
349 || S_IS_COMMON (symbolP)
350 || S_IS_VOLATILE (symbolP))
352 if (S_IS_VOLATILE (symbolP))
354 symbolP = symbol_clone (symbolP, 1);
355 S_SET_VALUE (symbolP, 0);
356 S_CLEAR_VOLATILE (symbolP);
358 if (S_GET_VALUE (symbolP) == 0)
360 define_sym_at_dot (symbolP);
363 #endif /* if we have one, it better be zero. */
368 /* There are still several cases to check:
370 A .comm/.lcomm symbol being redefined as initialized
373 A .comm/.lcomm symbol being redefined with a larger
376 This only used to be allowed on VMS gas, but Sun cc
377 on the sparc also depends on it. */
379 if (((!S_IS_DEBUG (symbolP)
380 && (!S_IS_DEFINED (symbolP) || S_IS_COMMON (symbolP))
381 && S_IS_EXTERNAL (symbolP))
382 || S_GET_SEGMENT (symbolP) == bss_section)
383 && (now_seg == data_section
384 || now_seg == bss_section
385 || now_seg == S_GET_SEGMENT (symbolP)))
387 /* Select which of the 2 cases this is. */
388 if (now_seg != data_section)
390 /* New .comm for prev .comm symbol.
392 If the new size is larger we just change its
393 value. If the new size is smaller, we ignore
395 if (S_GET_VALUE (symbolP)
396 < ((unsigned) frag_now_fix ()))
398 S_SET_VALUE (symbolP, (valueT) frag_now_fix ());
403 /* It is a .comm/.lcomm being converted to initialized
405 define_sym_at_dot (symbolP);
410 #if (!defined (OBJ_AOUT) && !defined (OBJ_MAYBE_AOUT) \
411 && !defined (OBJ_BOUT) && !defined (OBJ_MAYBE_BOUT))
412 static const char *od_buf = "";
416 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
417 sprintf (od_buf, "%d.%d.",
418 S_GET_OTHER (symbolP),
419 S_GET_DESC (symbolP));
421 as_bad (_("symbol `%s' is already defined as \"%s\"/%s%ld"),
423 segment_name (S_GET_SEGMENT (symbolP)),
425 (long) S_GET_VALUE (symbolP));
427 } /* if the undefined symbol has no value */
431 /* Don't blow up if the definition is the same. */
432 if (!(frag_now == symbolP->sy_frag
433 && S_GET_VALUE (symbolP) == frag_now_fix ()
434 && S_GET_SEGMENT (symbolP) == now_seg))
436 as_bad (_("symbol `%s' is already defined"), sym_name);
437 symbolP = symbol_clone (symbolP, 0);
438 define_sym_at_dot (symbolP);
443 else if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, sym_name))
445 symbolP = (symbolS *) local_symbol_make (sym_name, now_seg,
446 (valueT) frag_now_fix (),
451 symbolP = symbol_new (sym_name, now_seg, (valueT) frag_now_fix (),
454 symbol_table_insert (symbolP);
457 if (mri_common_symbol != NULL)
459 /* This symbol is actually being defined within an MRI common
460 section. This requires special handling. */
461 if (LOCAL_SYMBOL_CHECK (symbolP))
462 symbolP = local_symbol_convert ((struct local_symbol *) symbolP);
463 symbolP->sy_value.X_op = O_symbol;
464 symbolP->sy_value.X_add_symbol = mri_common_symbol;
465 symbolP->sy_value.X_add_number = S_GET_VALUE (mri_common_symbol);
466 symbolP->sy_frag = &zero_address_frag;
467 S_SET_SEGMENT (symbolP, expr_section);
468 symbolP->sy_flags.sy_mri_common = 1;
472 tc_frob_label (symbolP);
474 #ifdef obj_frob_label
475 obj_frob_label (symbolP);
481 /* Die if we can't insert the symbol. */
484 symbol_table_insert (symbolS *symbolP)
486 const char *error_string;
489 know (S_GET_NAME (symbolP));
491 if (LOCAL_SYMBOL_CHECK (symbolP))
493 error_string = hash_jam (local_hash, S_GET_NAME (symbolP),
495 if (error_string != NULL)
496 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
497 S_GET_NAME (symbolP), error_string);
501 if ((error_string = hash_jam (sy_hash, S_GET_NAME (symbolP), (void *) symbolP)))
503 as_fatal (_("inserting \"%s\" into symbol table failed: %s"),
504 S_GET_NAME (symbolP), error_string);
508 /* If a symbol name does not exist, create it as undefined, and insert
509 it into the symbol table. Return a pointer to it. */
512 symbol_find_or_make (const char *name)
516 symbolP = symbol_find (name);
520 if (! flag_keep_locals && bfd_is_local_label_name (stdoutput, name))
522 symbolP = md_undefined_symbol ((char *) name);
526 symbolP = (symbolS *) local_symbol_make (name, undefined_section,
532 symbolP = symbol_make (name);
534 symbol_table_insert (symbolP);
535 } /* if symbol wasn't found */
541 symbol_make (const char *name)
545 /* Let the machine description default it, e.g. for register names. */
546 symbolP = md_undefined_symbol ((char *) name);
549 symbolP = symbol_new (name, undefined_section, (valueT) 0, &zero_address_frag);
555 symbol_clone (symbolS *orgsymP, int replace)
558 asymbol *bsymorg, *bsymnew;
560 /* Make sure we never clone the dot special symbol. */
561 gas_assert (orgsymP != &dot_symbol);
563 /* Running local_symbol_convert on a clone that's not the one currently
564 in local_hash would incorrectly replace the hash entry. Thus the
565 symbol must be converted here. Note that the rest of the function
566 depends on not encountering an unconverted symbol. */
567 if (LOCAL_SYMBOL_CHECK (orgsymP))
568 orgsymP = local_symbol_convert ((struct local_symbol *) orgsymP);
569 bsymorg = orgsymP->bsym;
571 newsymP = (symbolS *) obstack_alloc (¬es, sizeof (*newsymP));
573 bsymnew = bfd_make_empty_symbol (bfd_asymbol_bfd (bsymorg));
575 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
576 newsymP->bsym = bsymnew;
577 bsymnew->name = bsymorg->name;
578 bsymnew->flags = bsymorg->flags & ~BSF_SECTION_SYM;
579 bsymnew->section = bsymorg->section;
580 bfd_copy_private_symbol_data (bfd_asymbol_bfd (bsymorg), bsymorg,
581 bfd_asymbol_bfd (bsymnew), bsymnew);
583 #ifdef obj_symbol_clone_hook
584 obj_symbol_clone_hook (newsymP, orgsymP);
587 #ifdef tc_symbol_clone_hook
588 tc_symbol_clone_hook (newsymP, orgsymP);
593 if (symbol_rootP == orgsymP)
594 symbol_rootP = newsymP;
595 else if (orgsymP->sy_previous)
597 orgsymP->sy_previous->sy_next = newsymP;
598 orgsymP->sy_previous = NULL;
600 if (symbol_lastP == orgsymP)
601 symbol_lastP = newsymP;
602 else if (orgsymP->sy_next)
603 orgsymP->sy_next->sy_previous = newsymP;
605 /* Symbols that won't be output can't be external. */
606 S_CLEAR_EXTERNAL (orgsymP);
607 orgsymP->sy_previous = orgsymP->sy_next = orgsymP;
608 debug_verify_symchain (symbol_rootP, symbol_lastP);
610 symbol_table_insert (newsymP);
614 /* Symbols that won't be output can't be external. */
615 S_CLEAR_EXTERNAL (newsymP);
616 newsymP->sy_previous = newsymP->sy_next = newsymP;
622 /* Referenced symbols, if they are forward references, need to be cloned
623 (without replacing the original) so that the value of the referenced
624 symbols at the point of use . */
626 #undef symbol_clone_if_forward_ref
628 symbol_clone_if_forward_ref (symbolS *symbolP, int is_forward)
630 if (symbolP && !LOCAL_SYMBOL_CHECK (symbolP))
632 symbolS *add_symbol = symbolP->sy_value.X_add_symbol;
633 symbolS *op_symbol = symbolP->sy_value.X_op_symbol;
635 if (symbolP->sy_flags.sy_forward_ref)
640 /* assign_symbol() clones volatile symbols; pre-existing expressions
641 hold references to the original instance, but want the current
642 value. Just repeat the lookup. */
643 if (add_symbol && S_IS_VOLATILE (add_symbol))
644 add_symbol = symbol_find_exact (S_GET_NAME (add_symbol));
645 if (op_symbol && S_IS_VOLATILE (op_symbol))
646 op_symbol = symbol_find_exact (S_GET_NAME (op_symbol));
649 /* Re-using sy_resolving here, as this routine cannot get called from
650 symbol resolution code. */
651 if ((symbolP->bsym->section == expr_section
652 || symbolP->sy_flags.sy_forward_ref)
653 && !symbolP->sy_flags.sy_resolving)
655 symbolP->sy_flags.sy_resolving = 1;
656 add_symbol = symbol_clone_if_forward_ref (add_symbol, is_forward);
657 op_symbol = symbol_clone_if_forward_ref (op_symbol, is_forward);
658 symbolP->sy_flags.sy_resolving = 0;
661 if (symbolP->sy_flags.sy_forward_ref
662 || add_symbol != symbolP->sy_value.X_add_symbol
663 || op_symbol != symbolP->sy_value.X_op_symbol)
665 if (symbolP != &dot_symbol)
667 symbolP = symbol_clone (symbolP, 0);
668 symbolP->sy_flags.sy_resolving = 0;
672 symbolP = symbol_temp_new_now ();
673 #ifdef tc_new_dot_label
674 tc_new_dot_label (symbolP);
679 symbolP->sy_value.X_add_symbol = add_symbol;
680 symbolP->sy_value.X_op_symbol = op_symbol;
687 symbol_temp_new (segT seg, valueT ofs, fragS *frag)
689 return symbol_new (FAKE_LABEL_NAME, seg, ofs, frag);
693 symbol_temp_new_now (void)
695 return symbol_temp_new (now_seg, frag_now_fix (), frag_now);
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. */
710 symbol_find_exact (const char *name)
712 return symbol_find_exact_noref (name, 0);
716 symbol_find_exact_noref (const char *name, int noref)
718 struct local_symbol *locsym;
721 locsym = (struct local_symbol *) hash_find (local_hash, name);
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. */
733 S_CLEAR_WEAKREFD (sym);
739 symbol_find (const char *name)
741 return symbol_find_noref (name, 0);
745 symbol_find_noref (const char *name, int noref)
750 #ifdef tc_canonicalize_symbol_name
752 copy = xstrdup (name);
753 name = tc_canonicalize_symbol_name (copy);
757 if (! symbols_case_sensitive)
766 name = copy = XNEWVEC (char, strlen (name) + 1);
768 while ((c = *orig++) != '\0')
769 *copy++ = TOUPPER (c);
774 copy = (char *) name;
777 result = symbol_find_exact_noref (name, noref);
783 /* Once upon a time, symbols were kept in a singly linked list. At
784 least coff needs to be able to rearrange them from time to time, for
785 which a doubly linked list is much more convenient. Loic did these
786 as macros which seemed dangerous to me so they're now functions.
789 /* Link symbol ADDME after symbol TARGET in the chain. */
792 symbol_append (symbolS *addme, symbolS *target,
793 symbolS **rootPP, symbolS **lastPP)
795 if (LOCAL_SYMBOL_CHECK (addme))
797 if (target != NULL && LOCAL_SYMBOL_CHECK (target))
802 know (*rootPP == NULL);
803 know (*lastPP == NULL);
804 addme->sy_next = NULL;
805 addme->sy_previous = NULL;
809 } /* if the list is empty */
811 if (target->sy_next != NULL)
813 target->sy_next->sy_previous = addme;
817 know (*lastPP == target);
819 } /* if we have a next */
821 addme->sy_next = target->sy_next;
822 target->sy_next = addme;
823 addme->sy_previous = target;
825 debug_verify_symchain (symbol_rootP, symbol_lastP);
828 /* Set the chain pointers of SYMBOL to null. */
831 symbol_clear_list_pointers (symbolS *symbolP)
833 if (LOCAL_SYMBOL_CHECK (symbolP))
835 symbolP->sy_next = NULL;
836 symbolP->sy_previous = NULL;
839 /* Remove SYMBOLP from the list. */
842 symbol_remove (symbolS *symbolP, symbolS **rootPP, symbolS **lastPP)
844 if (LOCAL_SYMBOL_CHECK (symbolP))
847 if (symbolP == *rootPP)
849 *rootPP = symbolP->sy_next;
850 } /* if it was the root */
852 if (symbolP == *lastPP)
854 *lastPP = symbolP->sy_previous;
855 } /* if it was the tail */
857 if (symbolP->sy_next != NULL)
859 symbolP->sy_next->sy_previous = symbolP->sy_previous;
862 if (symbolP->sy_previous != NULL)
864 symbolP->sy_previous->sy_next = symbolP->sy_next;
867 debug_verify_symchain (*rootPP, *lastPP);
870 /* Link symbol ADDME before symbol TARGET in the chain. */
873 symbol_insert (symbolS *addme, symbolS *target,
874 symbolS **rootPP, symbolS **lastPP ATTRIBUTE_UNUSED)
876 if (LOCAL_SYMBOL_CHECK (addme))
878 if (LOCAL_SYMBOL_CHECK (target))
881 if (target->sy_previous != NULL)
883 target->sy_previous->sy_next = addme;
887 know (*rootPP == target);
891 addme->sy_previous = target->sy_previous;
892 target->sy_previous = addme;
893 addme->sy_next = target;
895 debug_verify_symchain (*rootPP, *lastPP);
899 verify_symbol_chain (symbolS *rootP, symbolS *lastP)
901 symbolS *symbolP = rootP;
906 for (; symbol_next (symbolP) != NULL; symbolP = symbol_next (symbolP))
908 gas_assert (symbolP->bsym != NULL);
909 gas_assert (symbolP->sy_flags.sy_local_symbol == 0);
910 gas_assert (symbolP->sy_next->sy_previous == symbolP);
913 gas_assert (lastP == symbolP);
916 #ifdef OBJ_COMPLEX_RELC
919 use_complex_relocs_for (symbolS * symp)
921 switch (symp->sy_value.X_op)
931 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
932 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
934 (S_IS_DEFINED (symp->sy_value.X_add_symbol)
935 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section))
944 case O_bit_inclusive_or:
946 case O_bit_exclusive_or:
959 if ( (S_IS_COMMON (symp->sy_value.X_add_symbol)
960 || S_IS_LOCAL (symp->sy_value.X_add_symbol))
962 (S_IS_COMMON (symp->sy_value.X_op_symbol)
963 || S_IS_LOCAL (symp->sy_value.X_op_symbol))
965 && S_IS_DEFINED (symp->sy_value.X_add_symbol)
966 && S_IS_DEFINED (symp->sy_value.X_op_symbol)
967 && S_GET_SEGMENT (symp->sy_value.X_add_symbol) != expr_section
968 && S_GET_SEGMENT (symp->sy_value.X_op_symbol) != expr_section)
980 report_op_error (symbolS *symp, symbolS *left, operatorT op, symbolS *right)
984 segT seg_left = left ? S_GET_SEGMENT (left) : 0;
985 segT seg_right = S_GET_SEGMENT (right);
994 case O_uminus: opname = "-"; break;
995 case O_bit_not: opname = "~"; break;
996 case O_logical_not: opname = "!"; break;
997 case O_multiply: opname = "*"; break;
998 case O_divide: opname = "/"; break;
999 case O_modulus: opname = "%"; break;
1000 case O_left_shift: opname = "<<"; break;
1001 case O_right_shift: opname = ">>"; break;
1002 case O_bit_inclusive_or: opname = "|"; break;
1003 case O_bit_or_not: opname = "|~"; break;
1004 case O_bit_exclusive_or: opname = "^"; break;
1005 case O_bit_and: opname = "&"; break;
1006 case O_add: opname = "+"; break;
1007 case O_subtract: opname = "-"; break;
1008 case O_eq: opname = "=="; break;
1009 case O_ne: opname = "!="; break;
1010 case O_lt: opname = "<"; break;
1011 case O_le: opname = "<="; break;
1012 case O_ge: opname = ">="; break;
1013 case O_gt: opname = ">"; break;
1014 case O_logical_and: opname = "&&"; break;
1015 case O_logical_or: opname = "||"; break;
1018 if (expr_symbol_where (symp, &file, &line))
1021 as_bad_where (file, line,
1022 _("invalid operands (%s and %s sections) for `%s'"),
1023 seg_left->name, seg_right->name, opname);
1025 as_bad_where (file, line,
1026 _("invalid operand (%s section) for `%s'"),
1027 seg_right->name, opname);
1031 const char *sname = S_GET_NAME (symp);
1034 as_bad (_("invalid operands (%s and %s sections) for `%s' when setting `%s'"),
1035 seg_left->name, seg_right->name, opname, sname);
1037 as_bad (_("invalid operand (%s section) for `%s' when setting `%s'"),
1038 seg_right->name, opname, sname);
1042 /* Resolve the value of a symbol. This is called during the final
1043 pass over the symbol table to resolve any symbols with complex
1047 resolve_symbol_value (symbolS *symp)
1050 valueT final_val = 0;
1053 if (LOCAL_SYMBOL_CHECK (symp))
1055 struct local_symbol *locsym = (struct local_symbol *) symp;
1057 final_val = locsym->lsy_value;
1058 if (local_symbol_resolved_p (locsym))
1061 final_val += local_symbol_get_frag (locsym)->fr_address / OCTETS_PER_BYTE;
1065 locsym->lsy_value = final_val;
1066 local_symbol_mark_resolved (locsym);
1072 if (symp->sy_flags.sy_resolved)
1074 if (symp->sy_value.X_op == O_constant)
1075 return (valueT) symp->sy_value.X_add_number;
1081 final_seg = S_GET_SEGMENT (symp);
1083 if (symp->sy_flags.sy_resolving)
1086 as_bad (_("symbol definition loop encountered at `%s'"),
1091 #ifdef OBJ_COMPLEX_RELC
1092 else if (final_seg == expr_section
1093 && use_complex_relocs_for (symp))
1095 symbolS * relc_symbol = NULL;
1096 char * relc_symbol_name = NULL;
1098 relc_symbol_name = symbol_relc_make_expr (& symp->sy_value);
1100 /* For debugging, print out conversion input & output. */
1102 print_expr (& symp->sy_value);
1103 if (relc_symbol_name)
1104 fprintf (stderr, "-> relc symbol: %s\n", relc_symbol_name);
1107 if (relc_symbol_name != NULL)
1108 relc_symbol = symbol_new (relc_symbol_name, undefined_section,
1109 0, & zero_address_frag);
1111 if (relc_symbol == NULL)
1113 as_bad (_("cannot convert expression symbol %s to complex relocation"),
1119 symbol_table_insert (relc_symbol);
1121 /* S_CLEAR_EXTERNAL (relc_symbol); */
1122 if (symp->bsym->flags & BSF_SRELC)
1123 relc_symbol->bsym->flags |= BSF_SRELC;
1125 relc_symbol->bsym->flags |= BSF_RELC;
1126 /* symp->bsym->flags |= BSF_RELC; */
1127 copy_symbol_attributes (symp, relc_symbol);
1128 symp->sy_value.X_op = O_symbol;
1129 symp->sy_value.X_add_symbol = relc_symbol;
1130 symp->sy_value.X_add_number = 0;
1134 final_seg = undefined_section;
1135 goto exit_dont_set_value;
1140 symbolS *add_symbol, *op_symbol;
1141 offsetT left, right;
1142 segT seg_left, seg_right;
1146 symp->sy_flags.sy_resolving = 1;
1148 /* Help out with CSE. */
1149 add_symbol = symp->sy_value.X_add_symbol;
1150 op_symbol = symp->sy_value.X_op_symbol;
1151 final_val = symp->sy_value.X_add_number;
1152 op = symp->sy_value.X_op;
1165 final_val += symp->sy_frag->fr_address / OCTETS_PER_BYTE;
1166 if (final_seg == expr_section)
1167 final_seg = absolute_section;
1176 left = resolve_symbol_value (add_symbol);
1177 seg_left = S_GET_SEGMENT (add_symbol);
1179 symp->sy_value.X_op_symbol = NULL;
1182 if (S_IS_WEAKREFR (symp))
1184 gas_assert (final_val == 0);
1185 if (S_IS_WEAKREFR (add_symbol))
1187 gas_assert (add_symbol->sy_value.X_op == O_symbol
1188 && add_symbol->sy_value.X_add_number == 0);
1189 add_symbol = add_symbol->sy_value.X_add_symbol;
1190 gas_assert (! S_IS_WEAKREFR (add_symbol));
1191 symp->sy_value.X_add_symbol = add_symbol;
1195 if (symp->sy_flags.sy_mri_common)
1197 /* This is a symbol inside an MRI common section. The
1198 relocation routines are going to handle it specially.
1199 Don't change the value. */
1200 resolved = symbol_resolved_p (add_symbol);
1204 if (finalize_syms && final_val == 0)
1206 if (LOCAL_SYMBOL_CHECK (add_symbol))
1207 add_symbol = local_symbol_convert ((struct local_symbol *)
1209 copy_symbol_attributes (symp, add_symbol);
1212 /* If we have equated this symbol to an undefined or common
1213 symbol, keep X_op set to O_symbol, and don't change
1214 X_add_number. This permits the routine which writes out
1215 relocation to detect this case, and convert the
1216 relocation to be against the symbol to which this symbol
1218 if (! S_IS_DEFINED (add_symbol)
1219 #if defined (OBJ_COFF) && defined (TE_PE)
1220 || S_IS_WEAK (add_symbol)
1222 || S_IS_COMMON (add_symbol))
1226 symp->sy_value.X_op = O_symbol;
1227 symp->sy_value.X_add_symbol = add_symbol;
1228 symp->sy_value.X_add_number = final_val;
1229 /* Use X_op_symbol as a flag. */
1230 symp->sy_value.X_op_symbol = add_symbol;
1232 final_seg = seg_left;
1234 resolved = symbol_resolved_p (add_symbol);
1235 symp->sy_flags.sy_resolving = 0;
1236 goto exit_dont_set_value;
1238 else if (finalize_syms
1239 && ((final_seg == expr_section && seg_left != expr_section)
1240 || symbol_shadow_p (symp)))
1242 /* If the symbol is an expression symbol, do similarly
1243 as for undefined and common syms above. Handles
1244 "sym +/- expr" where "expr" cannot be evaluated
1245 immediately, and we want relocations to be against
1246 "sym", eg. because it is weak. */
1247 symp->sy_value.X_op = O_symbol;
1248 symp->sy_value.X_add_symbol = add_symbol;
1249 symp->sy_value.X_add_number = final_val;
1250 symp->sy_value.X_op_symbol = add_symbol;
1251 final_seg = seg_left;
1252 final_val += symp->sy_frag->fr_address + left;
1253 resolved = symbol_resolved_p (add_symbol);
1254 symp->sy_flags.sy_resolving = 0;
1255 goto exit_dont_set_value;
1259 final_val += symp->sy_frag->fr_address + left;
1260 if (final_seg == expr_section || final_seg == undefined_section)
1261 final_seg = seg_left;
1264 resolved = symbol_resolved_p (add_symbol);
1265 if (S_IS_WEAKREFR (symp))
1267 symp->sy_flags.sy_resolving = 0;
1268 goto exit_dont_set_value;
1275 left = resolve_symbol_value (add_symbol);
1276 seg_left = S_GET_SEGMENT (add_symbol);
1278 /* By reducing these to the relevant dyadic operator, we get
1279 !S -> S == 0 permitted on anything,
1280 -S -> 0 - S only permitted on absolute
1281 ~S -> S ^ ~0 only permitted on absolute */
1282 if (op != O_logical_not && seg_left != absolute_section
1284 report_op_error (symp, NULL, op, add_symbol);
1286 if (final_seg == expr_section || final_seg == undefined_section)
1287 final_seg = absolute_section;
1291 else if (op == O_logical_not)
1296 final_val += left + symp->sy_frag->fr_address;
1298 resolved = symbol_resolved_p (add_symbol);
1306 case O_bit_inclusive_or:
1308 case O_bit_exclusive_or:
1320 left = resolve_symbol_value (add_symbol);
1321 right = resolve_symbol_value (op_symbol);
1322 seg_left = S_GET_SEGMENT (add_symbol);
1323 seg_right = S_GET_SEGMENT (op_symbol);
1325 /* Simplify addition or subtraction of a constant by folding the
1326 constant into X_add_number. */
1329 if (seg_right == absolute_section)
1334 else if (seg_left == absolute_section)
1337 add_symbol = op_symbol;
1339 seg_left = seg_right;
1343 else if (op == O_subtract)
1345 if (seg_right == absolute_section)
1353 /* Equality and non-equality tests are permitted on anything.
1354 Subtraction, and other comparison operators are permitted if
1355 both operands are in the same section. Otherwise, both
1356 operands must be absolute. We already handled the case of
1357 addition or subtraction of a constant above. This will
1358 probably need to be changed for an object file format which
1359 supports arbitrary expressions, such as IEEE-695. */
1360 if (!(seg_left == absolute_section
1361 && seg_right == absolute_section)
1362 && !(op == O_eq || op == O_ne)
1363 && !((op == O_subtract
1364 || op == O_lt || op == O_le || op == O_ge || op == O_gt)
1365 && seg_left == seg_right
1366 && (seg_left != undefined_section
1367 || add_symbol == op_symbol)))
1369 /* Don't emit messages unless we're finalizing the symbol value,
1370 otherwise we may get the same message multiple times. */
1372 report_op_error (symp, add_symbol, op, op_symbol);
1373 /* However do not move the symbol into the absolute section
1374 if it cannot currently be resolved - this would confuse
1375 other parts of the assembler into believing that the
1376 expression had been evaluated to zero. */
1382 && (final_seg == expr_section || final_seg == undefined_section))
1383 final_seg = absolute_section;
1385 /* Check for division by zero. */
1386 if ((op == O_divide || op == O_modulus) && right == 0)
1388 /* If seg_right is not absolute_section, then we've
1389 already issued a warning about using a bad symbol. */
1390 if (seg_right == absolute_section && finalize_syms)
1395 if (expr_symbol_where (symp, &file, &line))
1396 as_bad_where (file, line, _("division by zero"));
1398 as_bad (_("division by zero when setting `%s'"),
1405 switch (symp->sy_value.X_op)
1407 case O_multiply: left *= right; break;
1408 case O_divide: left /= right; break;
1409 case O_modulus: left %= right; break;
1410 case O_left_shift: left <<= right; break;
1411 case O_right_shift: left >>= right; break;
1412 case O_bit_inclusive_or: left |= right; break;
1413 case O_bit_or_not: left |= ~right; break;
1414 case O_bit_exclusive_or: left ^= right; break;
1415 case O_bit_and: left &= right; break;
1416 case O_add: left += right; break;
1417 case O_subtract: left -= right; break;
1420 left = (left == right && seg_left == seg_right
1421 && (seg_left != undefined_section
1422 || add_symbol == op_symbol)
1423 ? ~ (offsetT) 0 : 0);
1424 if (symp->sy_value.X_op == O_ne)
1427 case O_lt: left = left < right ? ~ (offsetT) 0 : 0; break;
1428 case O_le: left = left <= right ? ~ (offsetT) 0 : 0; break;
1429 case O_ge: left = left >= right ? ~ (offsetT) 0 : 0; break;
1430 case O_gt: left = left > right ? ~ (offsetT) 0 : 0; break;
1431 case O_logical_and: left = left && right; break;
1432 case O_logical_or: left = left || right; break;
1437 /* See PR 20895 for a reproducer. */
1438 as_bad (_("Invalid operation on symbol"));
1439 goto exit_dont_set_value;
1445 final_val += symp->sy_frag->fr_address + left;
1446 if (final_seg == expr_section || final_seg == undefined_section)
1448 if (seg_left == undefined_section
1449 || seg_right == undefined_section)
1450 final_seg = undefined_section;
1451 else if (seg_left == absolute_section)
1452 final_seg = seg_right;
1454 final_seg = seg_left;
1456 resolved = (symbol_resolved_p (add_symbol)
1457 && symbol_resolved_p (op_symbol));
1462 /* Give an error (below) if not in expr_section. We don't
1463 want to worry about expr_section symbols, because they
1464 are fictional (they are created as part of expression
1465 resolution), and any problems may not actually mean
1470 symp->sy_flags.sy_resolving = 0;
1474 S_SET_VALUE (symp, final_val);
1476 exit_dont_set_value:
1477 /* Always set the segment, even if not finalizing the value.
1478 The segment is used to determine whether a symbol is defined. */
1479 S_SET_SEGMENT (symp, final_seg);
1481 /* Don't worry if we can't resolve an expr_section symbol. */
1485 symp->sy_flags.sy_resolved = 1;
1486 else if (S_GET_SEGMENT (symp) != expr_section)
1488 as_bad (_("can't resolve value for symbol `%s'"),
1490 symp->sy_flags.sy_resolved = 1;
1497 static void resolve_local_symbol (const char *, void *);
1499 /* A static function passed to hash_traverse. */
1502 resolve_local_symbol (const char *key ATTRIBUTE_UNUSED, void *value)
1505 resolve_symbol_value ((symbolS *) value);
1508 /* Resolve all local symbols. */
1511 resolve_local_symbol_values (void)
1513 hash_traverse (local_hash, resolve_local_symbol);
1516 /* Obtain the current value of a symbol without changing any
1517 sub-expressions used. */
1520 snapshot_symbol (symbolS **symbolPP, valueT *valueP, segT *segP, fragS **fragPP)
1522 symbolS *symbolP = *symbolPP;
1524 if (LOCAL_SYMBOL_CHECK (symbolP))
1526 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1528 *valueP = locsym->lsy_value;
1529 *segP = locsym->lsy_section;
1530 *fragPP = local_symbol_get_frag (locsym);
1534 expressionS exp = symbolP->sy_value;
1536 if (!symbolP->sy_flags.sy_resolved && exp.X_op != O_illegal)
1540 if (symbolP->sy_flags.sy_resolving)
1542 symbolP->sy_flags.sy_resolving = 1;
1543 resolved = resolve_expression (&exp);
1544 symbolP->sy_flags.sy_resolving = 0;
1552 if (!symbol_equated_p (symbolP))
1557 symbolP = exp.X_add_symbol;
1564 *symbolPP = symbolP;
1566 /* A bogus input file can result in resolve_expression()
1567 generating a local symbol, so we have to check again. */
1568 if (LOCAL_SYMBOL_CHECK (symbolP))
1570 struct local_symbol *locsym = (struct local_symbol *) symbolP;
1572 *valueP = locsym->lsy_value;
1573 *segP = locsym->lsy_section;
1574 *fragPP = local_symbol_get_frag (locsym);
1578 *valueP = exp.X_add_number;
1579 *segP = symbolP->bsym->section;
1580 *fragPP = symbolP->sy_frag;
1583 if (*segP == expr_section)
1586 case O_constant: *segP = absolute_section; break;
1587 case O_register: *segP = reg_section; break;
1595 /* Dollar labels look like a number followed by a dollar sign. Eg, "42$".
1596 They are *really* local. That is, they go out of scope whenever we see a
1597 label that isn't local. Also, like fb labels, there can be multiple
1598 instances of a dollar label. Therefor, we name encode each instance with
1599 the instance number, keep a list of defined symbols separate from the real
1600 symbol table, and we treat these buggers as a sparse array. */
1602 static long *dollar_labels;
1603 static long *dollar_label_instances;
1604 static char *dollar_label_defines;
1605 static unsigned long dollar_label_count;
1606 static unsigned long dollar_label_max;
1609 dollar_label_defined (long label)
1613 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1615 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1617 return dollar_label_defines[i - dollar_labels];
1619 /* If we get here, label isn't defined. */
1624 dollar_label_instance (long label)
1628 know ((dollar_labels != NULL) || (dollar_label_count == 0));
1630 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1632 return (dollar_label_instances[i - dollar_labels]);
1634 /* If we get here, we haven't seen the label before.
1635 Therefore its instance count is zero. */
1640 dollar_label_clear (void)
1642 memset (dollar_label_defines, '\0', (unsigned int) dollar_label_count);
1645 #define DOLLAR_LABEL_BUMP_BY 10
1648 define_dollar_label (long label)
1652 for (i = dollar_labels; i < dollar_labels + dollar_label_count; ++i)
1655 ++dollar_label_instances[i - dollar_labels];
1656 dollar_label_defines[i - dollar_labels] = 1;
1660 /* If we get to here, we don't have label listed yet. */
1662 if (dollar_labels == NULL)
1664 dollar_labels = XNEWVEC (long, DOLLAR_LABEL_BUMP_BY);
1665 dollar_label_instances = XNEWVEC (long, DOLLAR_LABEL_BUMP_BY);
1666 dollar_label_defines = XNEWVEC (char, DOLLAR_LABEL_BUMP_BY);
1667 dollar_label_max = DOLLAR_LABEL_BUMP_BY;
1668 dollar_label_count = 0;
1670 else if (dollar_label_count == dollar_label_max)
1672 dollar_label_max += DOLLAR_LABEL_BUMP_BY;
1673 dollar_labels = XRESIZEVEC (long, dollar_labels, dollar_label_max);
1674 dollar_label_instances = XRESIZEVEC (long, dollar_label_instances,
1676 dollar_label_defines = XRESIZEVEC (char, dollar_label_defines,
1678 } /* if we needed to grow */
1680 dollar_labels[dollar_label_count] = label;
1681 dollar_label_instances[dollar_label_count] = 1;
1682 dollar_label_defines[dollar_label_count] = 1;
1683 ++dollar_label_count;
1686 /* Caller must copy returned name: we re-use the area for the next name.
1688 The mth occurrence of label n: is turned into the symbol "Ln^Am"
1689 where n is the label number and m is the instance number. "L" makes
1690 it a label discarded unless debugging and "^A"('\1') ensures no
1691 ordinary symbol SHOULD get the same name as a local label
1692 symbol. The first "4:" is "L4^A1" - the m numbers begin at 1.
1694 fb labels get the same treatment, except that ^B is used in place
1697 char * /* Return local label name. */
1698 dollar_label_name (long n, /* we just saw "n$:" : n a number. */
1699 int augend /* 0 for current instance, 1 for new instance. */)
1702 /* Returned to caller, then copied. Used for created names ("4f"). */
1703 static char symbol_name_build[24];
1706 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1709 know (augend == 0 || augend == 1);
1710 p = symbol_name_build;
1711 #ifdef LOCAL_LABEL_PREFIX
1712 *p++ = LOCAL_LABEL_PREFIX;
1716 /* Next code just does sprintf( {}, "%d", n); */
1718 q = symbol_name_temporary;
1719 for (*q++ = 0, i = n; i; ++q)
1724 while ((*p = *--q) != '\0')
1727 *p++ = DOLLAR_LABEL_CHAR; /* ^A */
1729 /* Instance number. */
1730 q = symbol_name_temporary;
1731 for (*q++ = 0, i = dollar_label_instance (n) + augend; i; ++q)
1736 while ((*p++ = *--q) != '\0');
1738 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1739 return symbol_name_build;
1742 /* Somebody else's idea of local labels. They are made by "n:" where n
1743 is any decimal digit. Refer to them with
1744 "nb" for previous (backward) n:
1745 or "nf" for next (forward) n:.
1747 We do a little better and let n be any number, not just a single digit, but
1748 since the other guy's assembler only does ten, we treat the first ten
1751 Like someone else's assembler, we have one set of local label counters for
1752 entire assembly, not one set per (sub)segment like in most assemblers. This
1753 implies that one can refer to a label in another segment, and indeed some
1754 crufty compilers have done just that.
1756 Since there could be a LOT of these things, treat them as a sparse
1759 #define FB_LABEL_SPECIAL (10)
1761 static long fb_low_counter[FB_LABEL_SPECIAL];
1762 static long *fb_labels;
1763 static long *fb_label_instances;
1764 static long fb_label_count;
1765 static long fb_label_max;
1767 /* This must be more than FB_LABEL_SPECIAL. */
1768 #define FB_LABEL_BUMP_BY (FB_LABEL_SPECIAL + 6)
1771 fb_label_init (void)
1773 memset ((void *) fb_low_counter, '\0', sizeof (fb_low_counter));
1776 /* Add one to the instance number of this fb label. */
1779 fb_label_instance_inc (long label)
1783 if ((unsigned long) label < FB_LABEL_SPECIAL)
1785 ++fb_low_counter[label];
1789 if (fb_labels != NULL)
1791 for (i = fb_labels + FB_LABEL_SPECIAL;
1792 i < fb_labels + fb_label_count; ++i)
1796 ++fb_label_instances[i - fb_labels];
1798 } /* if we find it */
1799 } /* for each existing label */
1802 /* If we get to here, we don't have label listed yet. */
1804 if (fb_labels == NULL)
1806 fb_labels = XNEWVEC (long, FB_LABEL_BUMP_BY);
1807 fb_label_instances = XNEWVEC (long, FB_LABEL_BUMP_BY);
1808 fb_label_max = FB_LABEL_BUMP_BY;
1809 fb_label_count = FB_LABEL_SPECIAL;
1812 else if (fb_label_count == fb_label_max)
1814 fb_label_max += FB_LABEL_BUMP_BY;
1815 fb_labels = XRESIZEVEC (long, fb_labels, fb_label_max);
1816 fb_label_instances = XRESIZEVEC (long, fb_label_instances, fb_label_max);
1817 } /* if we needed to grow */
1819 fb_labels[fb_label_count] = label;
1820 fb_label_instances[fb_label_count] = 1;
1825 fb_label_instance (long label)
1829 if ((unsigned long) label < FB_LABEL_SPECIAL)
1831 return (fb_low_counter[label]);
1834 if (fb_labels != NULL)
1836 for (i = fb_labels + FB_LABEL_SPECIAL;
1837 i < fb_labels + fb_label_count; ++i)
1841 return (fb_label_instances[i - fb_labels]);
1842 } /* if we find it */
1843 } /* for each existing label */
1846 /* We didn't find the label, so this must be a reference to the
1851 /* Caller must copy returned name: we re-use the area for the next name.
1853 The mth occurrence of label n: is turned into the symbol "Ln^Bm"
1854 where n is the label number and m is the instance number. "L" makes
1855 it a label discarded unless debugging and "^B"('\2') ensures no
1856 ordinary symbol SHOULD get the same name as a local label
1857 symbol. The first "4:" is "L4^B1" - the m numbers begin at 1.
1859 dollar labels get the same treatment, except that ^A is used in
1862 char * /* Return local label name. */
1863 fb_label_name (long n, /* We just saw "n:", "nf" or "nb" : n a number. */
1864 long augend /* 0 for nb, 1 for n:, nf. */)
1867 /* Returned to caller, then copied. Used for created names ("4f"). */
1868 static char symbol_name_build[24];
1871 char symbol_name_temporary[20]; /* Build up a number, BACKWARDS. */
1875 know ((unsigned long) augend <= 2 /* See mmix_fb_label. */);
1877 know ((unsigned long) augend <= 1);
1879 p = symbol_name_build;
1880 #ifdef LOCAL_LABEL_PREFIX
1881 *p++ = LOCAL_LABEL_PREFIX;
1885 /* Next code just does sprintf( {}, "%d", n); */
1887 q = symbol_name_temporary;
1888 for (*q++ = 0, i = n; i; ++q)
1893 while ((*p = *--q) != '\0')
1896 *p++ = LOCAL_LABEL_CHAR; /* ^B */
1898 /* Instance number. */
1899 q = symbol_name_temporary;
1900 for (*q++ = 0, i = fb_label_instance (n) + augend; i; ++q)
1905 while ((*p++ = *--q) != '\0');
1907 /* The label, as a '\0' ended string, starts at symbol_name_build. */
1908 return (symbol_name_build);
1911 /* Decode name that may have been generated by foo_label_name() above.
1912 If the name wasn't generated by foo_label_name(), then return it
1913 unaltered. This is used for error messages. */
1916 decode_local_label_name (char *s)
1919 char *symbol_decode;
1921 int instance_number;
1923 const char *message_format;
1926 #ifdef LOCAL_LABEL_PREFIX
1927 if (s[lindex] == LOCAL_LABEL_PREFIX)
1931 if (s[lindex] != 'L')
1934 for (label_number = 0, p = s + lindex + 1; ISDIGIT (*p); ++p)
1935 label_number = (10 * label_number) + *p - '0';
1937 if (*p == DOLLAR_LABEL_CHAR)
1939 else if (*p == LOCAL_LABEL_CHAR)
1944 for (instance_number = 0, p++; ISDIGIT (*p); ++p)
1945 instance_number = (10 * instance_number) + *p - '0';
1947 message_format = _("\"%d\" (instance number %d of a %s label)");
1948 symbol_decode = (char *) obstack_alloc (¬es, strlen (message_format) + 30);
1949 sprintf (symbol_decode, message_format, label_number, instance_number, type);
1951 return symbol_decode;
1954 /* Get the value of a symbol. */
1957 S_GET_VALUE (symbolS *s)
1959 if (LOCAL_SYMBOL_CHECK (s))
1960 return resolve_symbol_value (s);
1962 if (!s->sy_flags.sy_resolved)
1964 valueT val = resolve_symbol_value (s);
1968 if (S_IS_WEAKREFR (s))
1969 return S_GET_VALUE (s->sy_value.X_add_symbol);
1971 if (s->sy_value.X_op != O_constant)
1973 if (! s->sy_flags.sy_resolved
1974 || s->sy_value.X_op != O_symbol
1975 || (S_IS_DEFINED (s) && ! S_IS_COMMON (s)))
1976 as_bad (_("attempt to get value of unresolved symbol `%s'"),
1979 return (valueT) s->sy_value.X_add_number;
1982 /* Set the value of a symbol. */
1985 S_SET_VALUE (symbolS *s, valueT val)
1987 if (LOCAL_SYMBOL_CHECK (s))
1989 ((struct local_symbol *) s)->lsy_value = val;
1993 s->sy_value.X_op = O_constant;
1994 s->sy_value.X_add_number = (offsetT) val;
1995 s->sy_value.X_unsigned = 0;
1996 S_CLEAR_WEAKREFR (s);
2000 copy_symbol_attributes (symbolS *dest, symbolS *src)
2002 if (LOCAL_SYMBOL_CHECK (dest))
2003 dest = local_symbol_convert ((struct local_symbol *) dest);
2004 if (LOCAL_SYMBOL_CHECK (src))
2005 src = local_symbol_convert ((struct local_symbol *) src);
2007 /* In an expression, transfer the settings of these flags.
2008 The user can override later, of course. */
2009 #define COPIED_SYMFLAGS (BSF_FUNCTION | BSF_OBJECT \
2010 | BSF_GNU_INDIRECT_FUNCTION)
2011 dest->bsym->flags |= src->bsym->flags & COPIED_SYMFLAGS;
2013 #ifdef OBJ_COPY_SYMBOL_ATTRIBUTES
2014 OBJ_COPY_SYMBOL_ATTRIBUTES (dest, src);
2017 #ifdef TC_COPY_SYMBOL_ATTRIBUTES
2018 TC_COPY_SYMBOL_ATTRIBUTES (dest, src);
2023 S_IS_FUNCTION (symbolS *s)
2027 if (LOCAL_SYMBOL_CHECK (s))
2030 flags = s->bsym->flags;
2032 return (flags & BSF_FUNCTION) != 0;
2036 S_IS_EXTERNAL (symbolS *s)
2040 if (LOCAL_SYMBOL_CHECK (s))
2043 flags = s->bsym->flags;
2046 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2049 return (flags & BSF_GLOBAL) != 0;
2053 S_IS_WEAK (symbolS *s)
2055 if (LOCAL_SYMBOL_CHECK (s))
2057 /* Conceptually, a weakrefr is weak if the referenced symbol is. We
2058 could probably handle a WEAKREFR as always weak though. E.g., if
2059 the referenced symbol has lost its weak status, there's no reason
2060 to keep handling the weakrefr as if it was weak. */
2061 if (S_IS_WEAKREFR (s))
2062 return S_IS_WEAK (s->sy_value.X_add_symbol);
2063 return (s->bsym->flags & BSF_WEAK) != 0;
2067 S_IS_WEAKREFR (symbolS *s)
2069 if (LOCAL_SYMBOL_CHECK (s))
2071 return s->sy_flags.sy_weakrefr != 0;
2075 S_IS_WEAKREFD (symbolS *s)
2077 if (LOCAL_SYMBOL_CHECK (s))
2079 return s->sy_flags.sy_weakrefd != 0;
2083 S_IS_COMMON (symbolS *s)
2085 if (LOCAL_SYMBOL_CHECK (s))
2087 return bfd_is_com_section (s->bsym->section);
2091 S_IS_DEFINED (symbolS *s)
2093 if (LOCAL_SYMBOL_CHECK (s))
2094 return ((struct local_symbol *) s)->lsy_section != undefined_section;
2095 return s->bsym->section != undefined_section;
2099 #ifndef EXTERN_FORCE_RELOC
2100 #define EXTERN_FORCE_RELOC IS_ELF
2103 /* Return true for symbols that should not be reduced to section
2104 symbols or eliminated from expressions, because they may be
2105 overridden by the linker. */
2107 S_FORCE_RELOC (symbolS *s, int strict)
2110 if (LOCAL_SYMBOL_CHECK (s))
2111 sec = ((struct local_symbol *) s)->lsy_section;
2115 && ((s->bsym->flags & BSF_WEAK) != 0
2116 || (EXTERN_FORCE_RELOC
2117 && (s->bsym->flags & BSF_GLOBAL) != 0)))
2118 || (s->bsym->flags & BSF_GNU_INDIRECT_FUNCTION) != 0)
2120 sec = s->bsym->section;
2122 return bfd_is_und_section (sec) || bfd_is_com_section (sec);
2126 S_IS_DEBUG (symbolS *s)
2128 if (LOCAL_SYMBOL_CHECK (s))
2130 if (s->bsym->flags & BSF_DEBUGGING)
2136 S_IS_LOCAL (symbolS *s)
2141 if (LOCAL_SYMBOL_CHECK (s))
2144 flags = s->bsym->flags;
2147 if ((flags & BSF_LOCAL) && (flags & BSF_GLOBAL))
2150 if (bfd_get_section (s->bsym) == reg_section)
2153 if (flag_strip_local_absolute
2154 /* Keep BSF_FILE symbols in order to allow debuggers to identify
2155 the source file even when the object file is stripped. */
2156 && (flags & (BSF_GLOBAL | BSF_FILE)) == 0
2157 && bfd_get_section (s->bsym) == absolute_section)
2160 name = S_GET_NAME (s);
2161 return (name != NULL
2163 && (strchr (name, DOLLAR_LABEL_CHAR)
2164 || strchr (name, LOCAL_LABEL_CHAR)
2165 #if FAKE_LABEL_CHAR != DOLLAR_LABEL_CHAR
2166 || strchr (name, FAKE_LABEL_CHAR)
2168 || TC_LABEL_IS_LOCAL (name)
2169 || (! flag_keep_locals
2170 && (bfd_is_local_label (stdoutput, s->bsym)
2173 && name[1] == '?')))));
2177 S_IS_STABD (symbolS *s)
2179 return S_GET_NAME (s) == 0;
2183 S_CAN_BE_REDEFINED (const symbolS *s)
2185 if (LOCAL_SYMBOL_CHECK (s))
2186 return (local_symbol_get_frag ((struct local_symbol *) s)
2187 == &predefined_address_frag);
2188 /* Permit register names to be redefined. */
2189 return s->bsym->section == reg_section;
2193 S_IS_VOLATILE (const symbolS *s)
2195 if (LOCAL_SYMBOL_CHECK (s))
2197 return s->sy_flags.sy_volatile;
2201 S_IS_FORWARD_REF (const symbolS *s)
2203 if (LOCAL_SYMBOL_CHECK (s))
2205 return s->sy_flags.sy_forward_ref;
2209 S_GET_NAME (symbolS *s)
2211 if (LOCAL_SYMBOL_CHECK (s))
2212 return ((struct local_symbol *) s)->lsy_name;
2213 return s->bsym->name;
2217 S_GET_SEGMENT (symbolS *s)
2219 if (LOCAL_SYMBOL_CHECK (s))
2220 return ((struct local_symbol *) s)->lsy_section;
2221 return s->bsym->section;
2225 S_SET_SEGMENT (symbolS *s, segT seg)
2227 /* Don't reassign section symbols. The direct reason is to prevent seg
2228 faults assigning back to const global symbols such as *ABS*, but it
2229 shouldn't happen anyway. */
2231 if (LOCAL_SYMBOL_CHECK (s))
2233 if (seg == reg_section)
2234 s = local_symbol_convert ((struct local_symbol *) s);
2237 ((struct local_symbol *) s)->lsy_section = seg;
2242 if (s->bsym->flags & BSF_SECTION_SYM)
2244 if (s->bsym->section != seg)
2248 s->bsym->section = seg;
2252 S_SET_EXTERNAL (symbolS *s)
2254 if (LOCAL_SYMBOL_CHECK (s))
2255 s = local_symbol_convert ((struct local_symbol *) s);
2256 if ((s->bsym->flags & BSF_WEAK) != 0)
2258 /* Let .weak override .global. */
2261 if (s->bsym->flags & BSF_SECTION_SYM)
2263 /* Do not reassign section symbols. */
2264 as_warn (_("section symbols are already global"));
2267 #ifndef TC_GLOBAL_REGISTER_SYMBOL_OK
2268 if (S_GET_SEGMENT (s) == reg_section)
2270 as_bad ("can't make register symbol `%s' global",
2275 s->bsym->flags |= BSF_GLOBAL;
2276 s->bsym->flags &= ~(BSF_LOCAL | BSF_WEAK);
2279 if (! an_external_name && S_GET_NAME(s)[0] != '.')
2280 an_external_name = S_GET_NAME (s);
2285 S_CLEAR_EXTERNAL (symbolS *s)
2287 if (LOCAL_SYMBOL_CHECK (s))
2289 if ((s->bsym->flags & BSF_WEAK) != 0)
2291 /* Let .weak override. */
2294 s->bsym->flags |= BSF_LOCAL;
2295 s->bsym->flags &= ~(BSF_GLOBAL | BSF_WEAK);
2299 S_SET_WEAK (symbolS *s)
2301 if (LOCAL_SYMBOL_CHECK (s))
2302 s = local_symbol_convert ((struct local_symbol *) s);
2303 #ifdef obj_set_weak_hook
2304 obj_set_weak_hook (s);
2306 s->bsym->flags |= BSF_WEAK;
2307 s->bsym->flags &= ~(BSF_GLOBAL | BSF_LOCAL);
2311 S_SET_WEAKREFR (symbolS *s)
2313 if (LOCAL_SYMBOL_CHECK (s))
2314 s = local_symbol_convert ((struct local_symbol *) s);
2315 s->sy_flags.sy_weakrefr = 1;
2316 /* If the alias was already used, make sure we mark the target as
2317 used as well, otherwise it might be dropped from the symbol
2318 table. This may have unintended side effects if the alias is
2319 later redirected to another symbol, such as keeping the unused
2320 previous target in the symbol table. Since it will be weak, it's
2322 if (s->sy_flags.sy_used)
2323 symbol_mark_used (s->sy_value.X_add_symbol);
2327 S_CLEAR_WEAKREFR (symbolS *s)
2329 if (LOCAL_SYMBOL_CHECK (s))
2331 s->sy_flags.sy_weakrefr = 0;
2335 S_SET_WEAKREFD (symbolS *s)
2337 if (LOCAL_SYMBOL_CHECK (s))
2338 s = local_symbol_convert ((struct local_symbol *) s);
2339 s->sy_flags.sy_weakrefd = 1;
2344 S_CLEAR_WEAKREFD (symbolS *s)
2346 if (LOCAL_SYMBOL_CHECK (s))
2348 if (s->sy_flags.sy_weakrefd)
2350 s->sy_flags.sy_weakrefd = 0;
2351 /* If a weakref target symbol is weak, then it was never
2352 referenced directly before, not even in a .global directive,
2353 so decay it to local. If it remains undefined, it will be
2354 later turned into a global, like any other undefined
2356 if (s->bsym->flags & BSF_WEAK)
2358 #ifdef obj_clear_weak_hook
2359 obj_clear_weak_hook (s);
2361 s->bsym->flags &= ~BSF_WEAK;
2362 s->bsym->flags |= BSF_LOCAL;
2368 S_SET_THREAD_LOCAL (symbolS *s)
2370 if (LOCAL_SYMBOL_CHECK (s))
2371 s = local_symbol_convert ((struct local_symbol *) s);
2372 if (bfd_is_com_section (s->bsym->section)
2373 && (s->bsym->flags & BSF_THREAD_LOCAL) != 0)
2375 s->bsym->flags |= BSF_THREAD_LOCAL;
2376 if ((s->bsym->flags & BSF_FUNCTION) != 0)
2377 as_bad (_("Accessing function `%s' as thread-local object"),
2379 else if (! bfd_is_und_section (s->bsym->section)
2380 && (s->bsym->section->flags & SEC_THREAD_LOCAL) == 0)
2381 as_bad (_("Accessing `%s' as thread-local object"),
2386 S_SET_NAME (symbolS *s, const char *name)
2388 if (LOCAL_SYMBOL_CHECK (s))
2390 ((struct local_symbol *) s)->lsy_name = name;
2393 s->bsym->name = name;
2397 S_SET_VOLATILE (symbolS *s)
2399 if (LOCAL_SYMBOL_CHECK (s))
2400 s = local_symbol_convert ((struct local_symbol *) s);
2401 s->sy_flags.sy_volatile = 1;
2405 S_CLEAR_VOLATILE (symbolS *s)
2407 if (!LOCAL_SYMBOL_CHECK (s))
2408 s->sy_flags.sy_volatile = 0;
2412 S_SET_FORWARD_REF (symbolS *s)
2414 if (LOCAL_SYMBOL_CHECK (s))
2415 s = local_symbol_convert ((struct local_symbol *) s);
2416 s->sy_flags.sy_forward_ref = 1;
2419 /* Return the previous symbol in a chain. */
2422 symbol_previous (symbolS *s)
2424 if (LOCAL_SYMBOL_CHECK (s))
2426 return s->sy_previous;
2429 /* Return the next symbol in a chain. */
2432 symbol_next (symbolS *s)
2434 if (LOCAL_SYMBOL_CHECK (s))
2439 /* Return a pointer to the value of a symbol as an expression. */
2442 symbol_get_value_expression (symbolS *s)
2444 if (LOCAL_SYMBOL_CHECK (s))
2445 s = local_symbol_convert ((struct local_symbol *) s);
2446 return &s->sy_value;
2449 /* Set the value of a symbol to an expression. */
2452 symbol_set_value_expression (symbolS *s, const expressionS *exp)
2454 if (LOCAL_SYMBOL_CHECK (s))
2455 s = local_symbol_convert ((struct local_symbol *) s);
2457 S_CLEAR_WEAKREFR (s);
2460 /* Return whether 2 symbols are the same. */
2463 symbol_same_p (symbolS *s1, symbolS *s2)
2465 if (s1->sy_flags.sy_local_symbol
2466 && local_symbol_converted_p ((struct local_symbol *) s1))
2467 s1 = local_symbol_get_real_symbol ((struct local_symbol *) s1);
2468 if (s2->sy_flags.sy_local_symbol
2469 && local_symbol_converted_p ((struct local_symbol *) s2))
2470 s2 = local_symbol_get_real_symbol ((struct local_symbol *) s2);
2474 /* Return a pointer to the X_add_number component of a symbol. */
2477 symbol_X_add_number (symbolS *s)
2479 if (LOCAL_SYMBOL_CHECK (s))
2480 return (offsetT *) &((struct local_symbol *) s)->lsy_value;
2482 return &s->sy_value.X_add_number;
2485 /* Set the value of SYM to the current position in the current segment. */
2488 symbol_set_value_now (symbolS *sym)
2490 S_SET_SEGMENT (sym, now_seg);
2491 S_SET_VALUE (sym, frag_now_fix ());
2492 symbol_set_frag (sym, frag_now);
2495 /* Set the frag of a symbol. */
2498 symbol_set_frag (symbolS *s, fragS *f)
2500 if (LOCAL_SYMBOL_CHECK (s))
2502 local_symbol_set_frag ((struct local_symbol *) s, f);
2506 S_CLEAR_WEAKREFR (s);
2509 /* Return the frag of a symbol. */
2512 symbol_get_frag (symbolS *s)
2514 if (LOCAL_SYMBOL_CHECK (s))
2515 return local_symbol_get_frag ((struct local_symbol *) s);
2519 /* Mark a symbol as having been used. */
2522 symbol_mark_used (symbolS *s)
2524 if (LOCAL_SYMBOL_CHECK (s))
2526 s->sy_flags.sy_used = 1;
2527 if (S_IS_WEAKREFR (s))
2528 symbol_mark_used (s->sy_value.X_add_symbol);
2531 /* Clear the mark of whether a symbol has been used. */
2534 symbol_clear_used (symbolS *s)
2536 if (LOCAL_SYMBOL_CHECK (s))
2537 s = local_symbol_convert ((struct local_symbol *) s);
2538 s->sy_flags.sy_used = 0;
2541 /* Return whether a symbol has been used. */
2544 symbol_used_p (symbolS *s)
2546 if (LOCAL_SYMBOL_CHECK (s))
2548 return s->sy_flags.sy_used;
2551 /* Mark a symbol as having been used in a reloc. */
2554 symbol_mark_used_in_reloc (symbolS *s)
2556 if (LOCAL_SYMBOL_CHECK (s))
2557 s = local_symbol_convert ((struct local_symbol *) s);
2558 s->sy_flags.sy_used_in_reloc = 1;
2561 /* Clear the mark of whether a symbol has been used in a reloc. */
2564 symbol_clear_used_in_reloc (symbolS *s)
2566 if (LOCAL_SYMBOL_CHECK (s))
2568 s->sy_flags.sy_used_in_reloc = 0;
2571 /* Return whether a symbol has been used in a reloc. */
2574 symbol_used_in_reloc_p (symbolS *s)
2576 if (LOCAL_SYMBOL_CHECK (s))
2578 return s->sy_flags.sy_used_in_reloc;
2581 /* Mark a symbol as an MRI common symbol. */
2584 symbol_mark_mri_common (symbolS *s)
2586 if (LOCAL_SYMBOL_CHECK (s))
2587 s = local_symbol_convert ((struct local_symbol *) s);
2588 s->sy_flags.sy_mri_common = 1;
2591 /* Clear the mark of whether a symbol is an MRI common symbol. */
2594 symbol_clear_mri_common (symbolS *s)
2596 if (LOCAL_SYMBOL_CHECK (s))
2598 s->sy_flags.sy_mri_common = 0;
2601 /* Return whether a symbol is an MRI common symbol. */
2604 symbol_mri_common_p (symbolS *s)
2606 if (LOCAL_SYMBOL_CHECK (s))
2608 return s->sy_flags.sy_mri_common;
2611 /* Mark a symbol as having been written. */
2614 symbol_mark_written (symbolS *s)
2616 if (LOCAL_SYMBOL_CHECK (s))
2618 s->sy_flags.sy_written = 1;
2621 /* Clear the mark of whether a symbol has been written. */
2624 symbol_clear_written (symbolS *s)
2626 if (LOCAL_SYMBOL_CHECK (s))
2628 s->sy_flags.sy_written = 0;
2631 /* Return whether a symbol has been written. */
2634 symbol_written_p (symbolS *s)
2636 if (LOCAL_SYMBOL_CHECK (s))
2638 return s->sy_flags.sy_written;
2641 /* Mark a symbol has having been resolved. */
2644 symbol_mark_resolved (symbolS *s)
2646 if (LOCAL_SYMBOL_CHECK (s))
2648 local_symbol_mark_resolved ((struct local_symbol *) s);
2651 s->sy_flags.sy_resolved = 1;
2654 /* Return whether a symbol has been resolved. */
2657 symbol_resolved_p (symbolS *s)
2659 if (LOCAL_SYMBOL_CHECK (s))
2660 return local_symbol_resolved_p ((struct local_symbol *) s);
2661 return s->sy_flags.sy_resolved;
2664 /* Return whether a symbol is a section symbol. */
2667 symbol_section_p (symbolS *s ATTRIBUTE_UNUSED)
2669 if (LOCAL_SYMBOL_CHECK (s))
2671 return (s->bsym->flags & BSF_SECTION_SYM) != 0;
2674 /* Return whether a symbol is equated to another symbol. */
2677 symbol_equated_p (symbolS *s)
2679 if (LOCAL_SYMBOL_CHECK (s))
2681 return s->sy_value.X_op == O_symbol;
2684 /* Return whether a symbol is equated to another symbol, and should be
2685 treated specially when writing out relocs. */
2688 symbol_equated_reloc_p (symbolS *s)
2690 if (LOCAL_SYMBOL_CHECK (s))
2692 /* X_op_symbol, normally not used for O_symbol, is set by
2693 resolve_symbol_value to flag expression syms that have been
2695 return (s->sy_value.X_op == O_symbol
2696 #if defined (OBJ_COFF) && defined (TE_PE)
2699 && ((s->sy_flags.sy_resolved && s->sy_value.X_op_symbol != NULL)
2700 || ! S_IS_DEFINED (s)
2701 || S_IS_COMMON (s)));
2704 /* Return whether a symbol has a constant value. */
2707 symbol_constant_p (symbolS *s)
2709 if (LOCAL_SYMBOL_CHECK (s))
2711 return s->sy_value.X_op == O_constant;
2714 /* Return whether a symbol was cloned and thus removed from the global
2718 symbol_shadow_p (symbolS *s)
2720 if (LOCAL_SYMBOL_CHECK (s))
2722 return s->sy_next == s;
2725 /* Return the BFD symbol for a symbol. */
2728 symbol_get_bfdsym (symbolS *s)
2730 if (LOCAL_SYMBOL_CHECK (s))
2731 s = local_symbol_convert ((struct local_symbol *) s);
2735 /* Set the BFD symbol for a symbol. */
2738 symbol_set_bfdsym (symbolS *s, asymbol *bsym)
2740 if (LOCAL_SYMBOL_CHECK (s))
2741 s = local_symbol_convert ((struct local_symbol *) s);
2742 /* Usually, it is harmless to reset a symbol to a BFD section
2743 symbol. For example, obj_elf_change_section sets the BFD symbol
2744 of an old symbol with the newly created section symbol. But when
2745 we have multiple sections with the same name, the newly created
2746 section may have the same name as an old section. We check if the
2747 old symbol has been already marked as a section symbol before
2749 if ((s->bsym->flags & BSF_SECTION_SYM) == 0)
2751 /* else XXX - What do we do now ? */
2754 #ifdef OBJ_SYMFIELD_TYPE
2756 /* Get a pointer to the object format information for a symbol. */
2759 symbol_get_obj (symbolS *s)
2761 if (LOCAL_SYMBOL_CHECK (s))
2762 s = local_symbol_convert ((struct local_symbol *) s);
2766 /* Set the object format information for a symbol. */
2769 symbol_set_obj (symbolS *s, OBJ_SYMFIELD_TYPE *o)
2771 if (LOCAL_SYMBOL_CHECK (s))
2772 s = local_symbol_convert ((struct local_symbol *) s);
2776 #endif /* OBJ_SYMFIELD_TYPE */
2778 #ifdef TC_SYMFIELD_TYPE
2780 /* Get a pointer to the processor information for a symbol. */
2783 symbol_get_tc (symbolS *s)
2785 if (LOCAL_SYMBOL_CHECK (s))
2786 s = local_symbol_convert ((struct local_symbol *) s);
2790 /* Set the processor information for a symbol. */
2793 symbol_set_tc (symbolS *s, TC_SYMFIELD_TYPE *o)
2795 if (LOCAL_SYMBOL_CHECK (s))
2796 s = local_symbol_convert ((struct local_symbol *) s);
2800 #endif /* TC_SYMFIELD_TYPE */
2805 symbol_lastP = NULL;
2806 symbol_rootP = NULL; /* In case we have 0 symbols (!!) */
2807 sy_hash = hash_new ();
2808 local_hash = hash_new ();
2810 memset ((char *) (&abs_symbol), '\0', sizeof (abs_symbol));
2811 #if defined (EMIT_SECTION_SYMBOLS) || !defined (RELOC_REQUIRES_SYMBOL)
2812 abs_symbol.bsym = bfd_abs_section_ptr->symbol;
2814 abs_symbol.sy_value.X_op = O_constant;
2815 abs_symbol.sy_frag = &zero_address_frag;
2817 if (LOCAL_LABELS_FB)
2822 dot_symbol_init (void)
2824 dot_symbol.bsym = bfd_make_empty_symbol (stdoutput);
2825 if (dot_symbol.bsym == NULL)
2826 as_fatal ("bfd_make_empty_symbol: %s", bfd_errmsg (bfd_get_error ()));
2827 dot_symbol.bsym->name = ".";
2828 dot_symbol.sy_flags.sy_forward_ref = 1;
2829 dot_symbol.sy_value.X_op = O_constant;
2834 /* Maximum indent level.
2835 Available for modification inside a gdb session. */
2836 static int max_indent_level = 8;
2839 print_symbol_value_1 (FILE *file, symbolS *sym)
2841 const char *name = S_GET_NAME (sym);
2842 if (!name || !name[0])
2844 fprintf (file, "sym ");
2845 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym));
2846 fprintf (file, " %s", name);
2848 if (LOCAL_SYMBOL_CHECK (sym))
2850 struct local_symbol *locsym = (struct local_symbol *) sym;
2852 if (local_symbol_get_frag (locsym) != & zero_address_frag
2853 && local_symbol_get_frag (locsym) != NULL)
2855 fprintf (file, " frag ");
2856 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) local_symbol_get_frag (locsym)));
2858 if (local_symbol_resolved_p (locsym))
2859 fprintf (file, " resolved");
2860 fprintf (file, " local");
2864 if (sym->sy_frag != &zero_address_frag)
2866 fprintf (file, " frag ");
2867 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) sym->sy_frag));
2869 if (sym->sy_flags.sy_written)
2870 fprintf (file, " written");
2871 if (sym->sy_flags.sy_resolved)
2872 fprintf (file, " resolved");
2873 else if (sym->sy_flags.sy_resolving)
2874 fprintf (file, " resolving");
2875 if (sym->sy_flags.sy_used_in_reloc)
2876 fprintf (file, " used-in-reloc");
2877 if (sym->sy_flags.sy_used)
2878 fprintf (file, " used");
2879 if (S_IS_LOCAL (sym))
2880 fprintf (file, " local");
2881 if (S_IS_EXTERNAL (sym))
2882 fprintf (file, " extern");
2883 if (S_IS_WEAK (sym))
2884 fprintf (file, " weak");
2885 if (S_IS_DEBUG (sym))
2886 fprintf (file, " debug");
2887 if (S_IS_DEFINED (sym))
2888 fprintf (file, " defined");
2890 if (S_IS_WEAKREFR (sym))
2891 fprintf (file, " weakrefr");
2892 if (S_IS_WEAKREFD (sym))
2893 fprintf (file, " weakrefd");
2894 fprintf (file, " %s", segment_name (S_GET_SEGMENT (sym)));
2895 if (symbol_resolved_p (sym))
2897 segT s = S_GET_SEGMENT (sym);
2899 if (s != undefined_section
2900 && s != expr_section)
2901 fprintf (file, " %lx", (unsigned long) S_GET_VALUE (sym));
2903 else if (indent_level < max_indent_level
2904 && S_GET_SEGMENT (sym) != undefined_section)
2907 fprintf (file, "\n%*s<", indent_level * 4, "");
2908 if (LOCAL_SYMBOL_CHECK (sym))
2909 fprintf (file, "constant %lx",
2910 (unsigned long) ((struct local_symbol *) sym)->lsy_value);
2912 print_expr_1 (file, &sym->sy_value);
2913 fprintf (file, ">");
2920 print_symbol_value (symbolS *sym)
2923 print_symbol_value_1 (stderr, sym);
2924 fprintf (stderr, "\n");
2928 print_binary (FILE *file, const char *name, expressionS *exp)
2931 fprintf (file, "%s\n%*s<", name, indent_level * 4, "");
2932 print_symbol_value_1 (file, exp->X_add_symbol);
2933 fprintf (file, ">\n%*s<", indent_level * 4, "");
2934 print_symbol_value_1 (file, exp->X_op_symbol);
2935 fprintf (file, ">");
2940 print_expr_1 (FILE *file, expressionS *exp)
2942 fprintf (file, "expr ");
2943 fprintf_vma (file, (bfd_vma) ((bfd_hostptr_t) exp));
2944 fprintf (file, " ");
2948 fprintf (file, "illegal");
2951 fprintf (file, "absent");
2954 fprintf (file, "constant %lx", (unsigned long) exp->X_add_number);
2958 fprintf (file, "symbol\n%*s<", indent_level * 4, "");
2959 print_symbol_value_1 (file, exp->X_add_symbol);
2960 fprintf (file, ">");
2962 if (exp->X_add_number)
2963 fprintf (file, "\n%*s%lx", indent_level * 4, "",
2964 (unsigned long) exp->X_add_number);
2968 fprintf (file, "register #%d", (int) exp->X_add_number);
2971 fprintf (file, "big");
2974 fprintf (file, "uminus -<");
2976 print_symbol_value_1 (file, exp->X_add_symbol);
2977 fprintf (file, ">");
2978 goto maybe_print_addnum;
2980 fprintf (file, "bit_not");
2983 print_binary (file, "multiply", exp);
2986 print_binary (file, "divide", exp);
2989 print_binary (file, "modulus", exp);
2992 print_binary (file, "lshift", exp);
2995 print_binary (file, "rshift", exp);
2997 case O_bit_inclusive_or:
2998 print_binary (file, "bit_ior", exp);
3000 case O_bit_exclusive_or:
3001 print_binary (file, "bit_xor", exp);
3004 print_binary (file, "bit_and", exp);
3007 print_binary (file, "eq", exp);
3010 print_binary (file, "ne", exp);
3013 print_binary (file, "lt", exp);
3016 print_binary (file, "le", exp);
3019 print_binary (file, "ge", exp);
3022 print_binary (file, "gt", exp);
3025 print_binary (file, "logical_and", exp);
3028 print_binary (file, "logical_or", exp);
3032 fprintf (file, "add\n%*s<", indent_level * 4, "");
3033 print_symbol_value_1 (file, exp->X_add_symbol);
3034 fprintf (file, ">\n%*s<", indent_level * 4, "");
3035 print_symbol_value_1 (file, exp->X_op_symbol);
3036 fprintf (file, ">");
3037 goto maybe_print_addnum;
3040 fprintf (file, "subtract\n%*s<", indent_level * 4, "");
3041 print_symbol_value_1 (file, exp->X_add_symbol);
3042 fprintf (file, ">\n%*s<", indent_level * 4, "");
3043 print_symbol_value_1 (file, exp->X_op_symbol);
3044 fprintf (file, ">");
3045 goto maybe_print_addnum;
3047 fprintf (file, "{unknown opcode %d}", (int) exp->X_op);
3054 print_expr (expressionS *exp)
3056 print_expr_1 (stderr, exp);
3057 fprintf (stderr, "\n");
3061 symbol_print_statistics (FILE *file)
3063 hash_print_statistics (file, "symbol table", sy_hash);
3064 hash_print_statistics (file, "mini local symbol table", local_hash);
3065 fprintf (file, "%lu mini local symbols created, %lu converted\n",
3066 local_symbol_count, local_symbol_conversion_count);
3069 #ifdef OBJ_COMPLEX_RELC
3071 /* Convert given symbol to a new complex-relocation symbol name. This
3072 may be a recursive function, since it might be called for non-leaf
3073 nodes (plain symbols) in the expression tree. The caller owns the
3074 returning string, so should free it eventually. Errors are
3075 indicated via as_bad and a NULL return value. The given symbol
3076 is marked with sy_used_in_reloc. */
3079 symbol_relc_make_sym (symbolS * sym)
3081 char * terminal = NULL;
3086 gas_assert (sym != NULL);
3088 /* Recurse to symbol_relc_make_expr if this symbol
3089 is defined as an expression or a plain value. */
3090 if ( S_GET_SEGMENT (sym) == expr_section
3091 || S_GET_SEGMENT (sym) == absolute_section)
3092 return symbol_relc_make_expr (& sym->sy_value);
3094 /* This may be a "fake symbol", referring to ".".
3095 Write out a special null symbol to refer to this position. */
3096 if (! strcmp (S_GET_NAME (sym), FAKE_LABEL_NAME))
3097 return xstrdup (".");
3099 /* We hope this is a plain leaf symbol. Construct the encoding
3100 as {S,s}II...:CCCCCCC....
3101 where 'S'/'s' means section symbol / plain symbol
3102 III is decimal for the symbol name length
3103 CCC is the symbol name itself. */
3104 symbol_mark_used_in_reloc (sym);
3106 sname = S_GET_NAME (sym);
3107 sname_len = strlen (sname);
3108 typetag = symbol_section_p (sym) ? 'S' : 's';
3110 terminal = XNEWVEC (char, (1 /* S or s */
3111 + 8 /* sname_len in decimal */
3113 + sname_len /* name itself */
3116 sprintf (terminal, "%c%d:%s", typetag, sname_len, sname);
3120 /* Convert given value to a new complex-relocation symbol name. This
3121 is a non-recursive function, since it is be called for leaf nodes
3122 (plain values) in the expression tree. The caller owns the
3123 returning string, so should free() it eventually. No errors. */
3126 symbol_relc_make_value (offsetT val)
3128 char * terminal = XNEWVEC (char, 28); /* Enough for long long. */
3131 bfd_sprintf_vma (stdoutput, terminal + 1, val);
3135 /* Convert given expression to a new complex-relocation symbol name.
3136 This is a recursive function, since it traverses the entire given
3137 expression tree. The caller owns the returning string, so should
3138 free() it eventually. Errors are indicated via as_bad() and a NULL
3142 symbol_relc_make_expr (expressionS * exp)
3144 const char * opstr = NULL; /* Operator prefix string. */
3145 int arity = 0; /* Arity of this operator. */
3146 char * operands[3]; /* Up to three operands. */
3147 char * concat_string = NULL;
3149 operands[0] = operands[1] = operands[2] = NULL;
3151 gas_assert (exp != NULL);
3153 /* Match known operators -> fill in opstr, arity, operands[] and fall
3154 through to construct subexpression fragments; may instead return
3155 string directly for leaf nodes. */
3157 /* See expr.h for the meaning of all these enums. Many operators
3158 have an unnatural arity (X_add_number implicitly added). The
3159 conversion logic expands them to explicit "+" subexpressions. */
3164 as_bad ("Unknown expression operator (enum %d)", exp->X_op);
3169 return symbol_relc_make_value (exp->X_add_number);
3172 if (exp->X_add_number)
3176 operands[0] = symbol_relc_make_sym (exp->X_add_symbol);
3177 operands[1] = symbol_relc_make_value (exp->X_add_number);
3181 return symbol_relc_make_sym (exp->X_add_symbol);
3183 /* Helper macros for nesting nodes. */
3185 #define HANDLE_XADD_OPT1(str_) \
3186 if (exp->X_add_number) \
3189 opstr = "+:" str_; \
3190 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3191 operands[1] = symbol_relc_make_value (exp->X_add_number); \
3198 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3202 #define HANDLE_XADD_OPT2(str_) \
3203 if (exp->X_add_number) \
3206 opstr = "+:" str_; \
3207 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3208 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3209 operands[2] = symbol_relc_make_value (exp->X_add_number); \
3215 operands[0] = symbol_relc_make_sym (exp->X_add_symbol); \
3216 operands[1] = symbol_relc_make_sym (exp->X_op_symbol); \
3220 /* Nesting nodes. */
3222 case O_uminus: HANDLE_XADD_OPT1 ("0-");
3223 case O_bit_not: HANDLE_XADD_OPT1 ("~");
3224 case O_logical_not: HANDLE_XADD_OPT1 ("!");
3225 case O_multiply: HANDLE_XADD_OPT2 ("*");
3226 case O_divide: HANDLE_XADD_OPT2 ("/");
3227 case O_modulus: HANDLE_XADD_OPT2 ("%");
3228 case O_left_shift: HANDLE_XADD_OPT2 ("<<");
3229 case O_right_shift: HANDLE_XADD_OPT2 (">>");
3230 case O_bit_inclusive_or: HANDLE_XADD_OPT2 ("|");
3231 case O_bit_exclusive_or: HANDLE_XADD_OPT2 ("^");
3232 case O_bit_and: HANDLE_XADD_OPT2 ("&");
3233 case O_add: HANDLE_XADD_OPT2 ("+");
3234 case O_subtract: HANDLE_XADD_OPT2 ("-");
3235 case O_eq: HANDLE_XADD_OPT2 ("==");
3236 case O_ne: HANDLE_XADD_OPT2 ("!=");
3237 case O_lt: HANDLE_XADD_OPT2 ("<");
3238 case O_le: HANDLE_XADD_OPT2 ("<=");
3239 case O_ge: HANDLE_XADD_OPT2 (">=");
3240 case O_gt: HANDLE_XADD_OPT2 (">");
3241 case O_logical_and: HANDLE_XADD_OPT2 ("&&");
3242 case O_logical_or: HANDLE_XADD_OPT2 ("||");
3245 /* Validate & reject early. */
3246 if (arity >= 1 && ((operands[0] == NULL) || (strlen (operands[0]) == 0)))
3248 if (arity >= 2 && ((operands[1] == NULL) || (strlen (operands[1]) == 0)))
3250 if (arity >= 3 && ((operands[2] == NULL) || (strlen (operands[2]) == 0)))
3254 concat_string = NULL;
3255 else if (arity == 0)
3256 concat_string = xstrdup (opstr);
3257 else if (arity == 1)
3258 concat_string = concat (opstr, ":", operands[0], (char *) NULL);
3259 else if (arity == 2)
3260 concat_string = concat (opstr, ":", operands[0], ":", operands[1],
3263 concat_string = concat (opstr, ":", operands[0], ":", operands[1], ":",
3264 operands[2], (char *) NULL);
3266 /* Free operand strings (not opstr). */
3267 if (arity >= 1) xfree (operands[0]);
3268 if (arity >= 2) xfree (operands[1]);
3269 if (arity >= 3) xfree (operands[2]);
3271 return concat_string;